ocel 0.0.0 → 0.0.1-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/run.js +39 -0
- package/dist/blob/bucket.d.ts +55 -0
- package/dist/blob/bucket.js +65 -0
- package/dist/blob/index.d.ts +8 -0
- package/dist/blob/index.js +13 -0
- package/dist/blob/integrations/client.d.ts +25 -0
- package/dist/blob/integrations/client.js +71 -0
- package/dist/blob/integrations/hono.d.ts +31 -0
- package/dist/blob/integrations/hono.js +11 -0
- package/dist/blob/mime-types.d.ts +1 -0
- package/dist/blob/mime-types.js +1 -0
- package/dist/blob/parse.d.ts +16 -0
- package/dist/blob/parse.js +69 -0
- package/dist/blob/s3-listener.d.ts +7 -0
- package/dist/blob/s3-listener.js +35 -0
- package/dist/blob/types.d.ts +128 -0
- package/dist/blob/types.js +1 -0
- package/dist/blob/upload-error.d.ts +35 -0
- package/dist/blob/upload-error.js +54 -0
- package/dist/blob/uploader.d.ts +67 -0
- package/dist/blob/uploader.js +323 -0
- package/dist/internal/db/index.d.ts +68 -0
- package/dist/internal/db/index.js +73 -0
- package/dist/lambda/index.d.ts +2 -0
- package/dist/lambda/index.js +4 -0
- package/dist/lambda/integrations/express.d.ts +3 -0
- package/dist/lambda/integrations/express.js +13 -0
- package/dist/lambda/integrations/hono.d.ts +3 -0
- package/dist/lambda/integrations/hono.js +13 -0
- package/dist/lambda/lambda.d.ts +43 -0
- package/dist/lambda/lambda.js +30 -0
- package/dist/postgres/index.d.ts +2 -0
- package/dist/postgres/index.js +4 -0
- package/dist/postgres/pg.d.ts +11 -0
- package/dist/postgres/pg.js +24 -0
- package/dist/utils/component.d.ts +3 -0
- package/dist/utils/component.js +1 -0
- package/dist/utils/constants.d.ts +2 -0
- package/dist/utils/constants.js +2 -0
- package/dist/utils/nanoid.d.ts +1 -0
- package/dist/utils/nanoid.js +6 -0
- package/dist/utils/rpc.d.ts +11 -0
- package/dist/utils/rpc.js +17 -0
- package/dist/utils/stack.d.ts +4 -0
- package/dist/utils/stack.js +23 -0
- package/dist/workflow/index.d.ts +3 -0
- package/dist/workflow/index.js +4 -0
- package/dist/workflow/workflow.d.ts +34 -0
- package/dist/workflow/workflow.js +146 -0
- package/package.json +93 -8
- package/src/blob/bucket.ts +92 -0
- package/src/blob/index.ts +34 -0
- package/src/blob/integrations/client.ts +127 -0
- package/src/blob/integrations/hono.ts +16 -0
- package/src/blob/mime-types.ts +68 -0
- package/src/blob/parse.ts +93 -0
- package/src/blob/s3-listener.ts +48 -0
- package/src/blob/types.ts +142 -0
- package/src/blob/upload-error.ts +67 -0
- package/src/blob/uploader.ts +426 -0
- package/src/internal/db/index.ts +78 -0
- package/src/lambda/index.ts +8 -0
- package/src/lambda/integrations/express.ts +25 -0
- package/src/lambda/integrations/hono.ts +25 -0
- package/src/lambda/lambda.ts +94 -0
- package/src/postgres/index.ts +5 -0
- package/src/postgres/pg.ts +33 -0
- package/src/utils/component.ts +3 -0
- package/src/utils/constants.ts +2 -0
- package/src/utils/nanoid.ts +9 -0
- package/src/utils/rpc.ts +28 -0
- package/src/utils/stack.ts +26 -0
- package/src/workflow/index.ts +9 -0
- package/src/workflow/workflow.ts +235 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export class UploadError extends Error {
|
|
2
|
+
statusCode;
|
|
3
|
+
code;
|
|
4
|
+
constructor(message, statusCode = 500, code = "INTERNAL_SERVER_ERROR") {
|
|
5
|
+
super(message);
|
|
6
|
+
this.statusCode = statusCode;
|
|
7
|
+
this.code = code;
|
|
8
|
+
// Essential for 'instanceof' to work correctly when extending built-in classes in TypeScript
|
|
9
|
+
Object.setPrototypeOf(this, UploadError.prototype);
|
|
10
|
+
}
|
|
11
|
+
// --- Static Factory Helpers (The DX Magic) ---
|
|
12
|
+
/**
|
|
13
|
+
* 400 Bad Request
|
|
14
|
+
* Use when the input is invalid (e.g. file too big, wrong extension)
|
|
15
|
+
*/
|
|
16
|
+
static badRequest(message = "Bad Request") {
|
|
17
|
+
return new UploadError(message, 400, "BAD_REQUEST");
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* 401 Unauthorized
|
|
21
|
+
* Use when the user is not logged in or missing headers
|
|
22
|
+
*/
|
|
23
|
+
static unauthorized(message = "Unauthorized") {
|
|
24
|
+
return new UploadError(message, 401, "UNAUTHORIZED");
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 403 Forbidden
|
|
28
|
+
* Use when the user is logged in but doesn't have permission (e.g. wrong plan)
|
|
29
|
+
*/
|
|
30
|
+
static forbidden(message = "Forbidden") {
|
|
31
|
+
return new UploadError(message, 403, "FORBIDDEN");
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 404 Not Found
|
|
35
|
+
* Use when a resource required for the upload is missing
|
|
36
|
+
*/
|
|
37
|
+
static notFound(message = "Not Found") {
|
|
38
|
+
return new UploadError(message, 404, "NOT_FOUND");
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 429 Too Many Requests
|
|
42
|
+
* Use for rate limiting logic
|
|
43
|
+
*/
|
|
44
|
+
static tooManyRequests(message = "Too many requests") {
|
|
45
|
+
return new UploadError(message, 429, "TOO_MANY_REQUESTS");
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* 500 Internal Server Error
|
|
49
|
+
* Use for unexpected crashes or database failures
|
|
50
|
+
*/
|
|
51
|
+
static internal(message = "Internal Server Error") {
|
|
52
|
+
return new UploadError(message, 500, "INTERNAL_SERVER_ERROR");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { HandleUploadBody, PathConfig, PathGenerator, TUploaderConfig, UploadFileBody } from "./types";
|
|
2
|
+
import type { IncomingMessage } from "node:http";
|
|
3
|
+
import type { Bucket } from "./bucket";
|
|
4
|
+
export declare class Uploader<TMeta = unknown> {
|
|
5
|
+
private bucket;
|
|
6
|
+
private name;
|
|
7
|
+
private config;
|
|
8
|
+
private pathGenerator;
|
|
9
|
+
constructor(bucket: Bucket<any>, name: string, config: TUploaderConfig<any, TMeta>);
|
|
10
|
+
handleUpload({ action, body, request, sessionId, }: {
|
|
11
|
+
request: Request | IncomingMessage;
|
|
12
|
+
body: HandleUploadBody;
|
|
13
|
+
action: "presign" | "callback" | "poll";
|
|
14
|
+
sessionId?: string;
|
|
15
|
+
}): Promise<{
|
|
16
|
+
files: {
|
|
17
|
+
url: string;
|
|
18
|
+
key: string;
|
|
19
|
+
name: string;
|
|
20
|
+
}[];
|
|
21
|
+
sessionId: string;
|
|
22
|
+
} | {
|
|
23
|
+
files: {
|
|
24
|
+
fileKey: string;
|
|
25
|
+
status: "PENDING" | "SUCCESS" | "FAILED";
|
|
26
|
+
file: {
|
|
27
|
+
path: string;
|
|
28
|
+
contentDisposition: string;
|
|
29
|
+
contentType: string;
|
|
30
|
+
} | undefined;
|
|
31
|
+
metadata: TMeta;
|
|
32
|
+
}[];
|
|
33
|
+
completed: boolean;
|
|
34
|
+
type?: undefined;
|
|
35
|
+
response?: undefined;
|
|
36
|
+
error?: undefined;
|
|
37
|
+
} | {
|
|
38
|
+
type: "UPLOAD_COMPLETED";
|
|
39
|
+
response: "ok";
|
|
40
|
+
files?: undefined;
|
|
41
|
+
completed?: undefined;
|
|
42
|
+
error?: undefined;
|
|
43
|
+
} | {
|
|
44
|
+
error: {
|
|
45
|
+
message: string;
|
|
46
|
+
code: string;
|
|
47
|
+
status: number;
|
|
48
|
+
};
|
|
49
|
+
files?: undefined;
|
|
50
|
+
completed?: undefined;
|
|
51
|
+
type?: undefined;
|
|
52
|
+
response?: undefined;
|
|
53
|
+
}>;
|
|
54
|
+
private startPolling;
|
|
55
|
+
getProvider(): {
|
|
56
|
+
presign: (files: UploadFileBody[]) => Promise<{
|
|
57
|
+
files: {
|
|
58
|
+
url: string;
|
|
59
|
+
key: string;
|
|
60
|
+
name: string;
|
|
61
|
+
}[];
|
|
62
|
+
sessionId: string;
|
|
63
|
+
}>;
|
|
64
|
+
};
|
|
65
|
+
createConfigBasedGenerator(config: PathConfig): PathGenerator<any>;
|
|
66
|
+
private validateFiles;
|
|
67
|
+
}
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import slugify from "@sindresorhus/slugify";
|
|
2
|
+
import { UploadError } from "./upload-error";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
|
5
|
+
import { getNanoid } from "../utils/nanoid";
|
|
6
|
+
import { UploadSession } from "../internal/db";
|
|
7
|
+
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
8
|
+
import { parseFileSize, parseReq } from "./parse";
|
|
9
|
+
export class Uploader {
|
|
10
|
+
bucket;
|
|
11
|
+
name;
|
|
12
|
+
config;
|
|
13
|
+
pathGenerator = null;
|
|
14
|
+
constructor(bucket, name, config) {
|
|
15
|
+
this.bucket = bucket;
|
|
16
|
+
this.name = name;
|
|
17
|
+
this.config = config;
|
|
18
|
+
const path = this.config.path;
|
|
19
|
+
if (path) {
|
|
20
|
+
if (typeof path === "function") {
|
|
21
|
+
this.pathGenerator = path;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
this.pathGenerator = this.createConfigBasedGenerator(path);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
async handleUpload({ action, body, request, sessionId, }) {
|
|
29
|
+
try {
|
|
30
|
+
const req = parseReq(request);
|
|
31
|
+
switch (action) {
|
|
32
|
+
case "presign": {
|
|
33
|
+
if (!("files" in body)) {
|
|
34
|
+
throw UploadError.badRequest("Invalid presign body");
|
|
35
|
+
}
|
|
36
|
+
const presignBody = body;
|
|
37
|
+
const rawInput = presignBody.input || {};
|
|
38
|
+
const parser = this.config.input;
|
|
39
|
+
let parsedInput;
|
|
40
|
+
// TODO: error handling
|
|
41
|
+
if (parser) {
|
|
42
|
+
const parsed = parser.safeParse(rawInput);
|
|
43
|
+
if (!parsed.success) {
|
|
44
|
+
throw UploadError.badRequest(parsed.error.issues[0]?.message);
|
|
45
|
+
}
|
|
46
|
+
parsedInput = parsed.data;
|
|
47
|
+
}
|
|
48
|
+
const middleware = this.config.onBeforeUpload;
|
|
49
|
+
const customMeta = await middleware?.({
|
|
50
|
+
req: request,
|
|
51
|
+
input: parsedInput,
|
|
52
|
+
files: presignBody.files,
|
|
53
|
+
});
|
|
54
|
+
const metadata = {
|
|
55
|
+
"x-ocel-path": req.path,
|
|
56
|
+
"x-ocel-uploader": this.name,
|
|
57
|
+
...(customMeta
|
|
58
|
+
? { "x-ocel-metadata": JSON.stringify(customMeta) }
|
|
59
|
+
: {}),
|
|
60
|
+
};
|
|
61
|
+
// TODO: validate files against uploader config (size, count, types, etc)
|
|
62
|
+
this.validateFiles(presignBody.files, customMeta);
|
|
63
|
+
const files = body.files.map((f) => {
|
|
64
|
+
const name = slugify(f.name, { preserveCharacters: ["."] });
|
|
65
|
+
return {
|
|
66
|
+
...f,
|
|
67
|
+
key: this.pathGenerator?.({
|
|
68
|
+
fileNameWithExt: name,
|
|
69
|
+
metadata: customMeta,
|
|
70
|
+
fileNameWithoutExt: name.replace(path.extname(name), ""),
|
|
71
|
+
ext: path.extname(name),
|
|
72
|
+
}) || name,
|
|
73
|
+
disposition: "inline",
|
|
74
|
+
metadata: {
|
|
75
|
+
...metadata,
|
|
76
|
+
"x-ocel-original-filename": f.name,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
});
|
|
80
|
+
const provider = this.getProvider();
|
|
81
|
+
return provider.presign(files);
|
|
82
|
+
}
|
|
83
|
+
case "poll": {
|
|
84
|
+
if (!sessionId) {
|
|
85
|
+
throw UploadError.badRequest("sessionId is required for poll");
|
|
86
|
+
}
|
|
87
|
+
const result = await UploadSession.query
|
|
88
|
+
.sessions({ sessionId })
|
|
89
|
+
.go();
|
|
90
|
+
const files = result.data.map((record) => {
|
|
91
|
+
let parsedMeta = {};
|
|
92
|
+
try {
|
|
93
|
+
if (record.metadata) {
|
|
94
|
+
parsedMeta = JSON.parse(record.metadata);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
// ignore parse errors
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
fileKey: record.fileKey,
|
|
102
|
+
status: record.status,
|
|
103
|
+
file: record.status === "SUCCESS"
|
|
104
|
+
? {
|
|
105
|
+
path: record.fileKey,
|
|
106
|
+
contentDisposition: record.contentDisposition || "inline",
|
|
107
|
+
contentType: record.contentType,
|
|
108
|
+
}
|
|
109
|
+
: undefined,
|
|
110
|
+
metadata: parsedMeta,
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
const allComplete = files.every((f) => f.status === "SUCCESS" || f.status === "FAILED");
|
|
114
|
+
return {
|
|
115
|
+
files,
|
|
116
|
+
completed: allComplete,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
case "callback": {
|
|
120
|
+
// TODO: validate callback body
|
|
121
|
+
if (!("metadata" in body)) {
|
|
122
|
+
throw UploadError.badRequest("Invalid callback body");
|
|
123
|
+
}
|
|
124
|
+
const data = body;
|
|
125
|
+
await this.config.onUploadComplete?.(data);
|
|
126
|
+
return {
|
|
127
|
+
type: "UPLOAD_COMPLETED",
|
|
128
|
+
response: "ok",
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
default:
|
|
132
|
+
throw UploadError.badRequest(`Unhandled action type: ${action}`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
catch (err) {
|
|
136
|
+
if (err instanceof UploadError) {
|
|
137
|
+
this.config.onUploadError?.({ error: err });
|
|
138
|
+
return {
|
|
139
|
+
error: {
|
|
140
|
+
message: err.message,
|
|
141
|
+
code: err.code, // e.g. "FORBIDDEN"
|
|
142
|
+
status: err.statusCode, // e.g. 403
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
error: {
|
|
148
|
+
message: "Internal Server Error",
|
|
149
|
+
code: "INTERNAL_SERVER_ERROR",
|
|
150
|
+
status: 500,
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
startPolling(sessionId) {
|
|
156
|
+
const POLL_INTERVAL_MS = 1000;
|
|
157
|
+
const MAX_DURATION_MS = 10 * 60 * 1000; // 10 minutes
|
|
158
|
+
const startTime = Date.now();
|
|
159
|
+
const tick = async () => {
|
|
160
|
+
if (Date.now() - startTime > MAX_DURATION_MS) {
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
const result = await UploadSession.query
|
|
164
|
+
.sessions({ sessionId })
|
|
165
|
+
.go();
|
|
166
|
+
const allComplete = result.data.every((r) => r.status === "SUCCESS" || r.status === "FAILED");
|
|
167
|
+
if (!allComplete) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
// Session complete - invoke callbacks
|
|
171
|
+
for (const record of result.data) {
|
|
172
|
+
let parsedMeta = {};
|
|
173
|
+
try {
|
|
174
|
+
if (record.metadata) {
|
|
175
|
+
parsedMeta = JSON.parse(record.metadata);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
// ignore
|
|
180
|
+
}
|
|
181
|
+
if (record.status === "SUCCESS") {
|
|
182
|
+
await this.config.onUploadComplete?.({
|
|
183
|
+
metadata: parsedMeta,
|
|
184
|
+
file: {
|
|
185
|
+
path: record.fileKey,
|
|
186
|
+
contentDisposition: record.contentDisposition || "inline",
|
|
187
|
+
contentType: record.contentType,
|
|
188
|
+
},
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
else if (record.status === "FAILED" && this.config.onUploadError) {
|
|
192
|
+
this.config.onUploadError({
|
|
193
|
+
error: new UploadError(`Upload failed for ${record.fileKey}`, 500, "UPLOAD_FAILED"),
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return true;
|
|
198
|
+
};
|
|
199
|
+
const intervalId = setInterval(async () => {
|
|
200
|
+
try {
|
|
201
|
+
const done = await tick();
|
|
202
|
+
if (done) {
|
|
203
|
+
clearInterval(intervalId);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
catch {
|
|
207
|
+
clearInterval(intervalId);
|
|
208
|
+
}
|
|
209
|
+
}, POLL_INTERVAL_MS);
|
|
210
|
+
}
|
|
211
|
+
getProvider() {
|
|
212
|
+
const bucket = this.bucket;
|
|
213
|
+
const bucketId = bucket.__name();
|
|
214
|
+
return {
|
|
215
|
+
presign: async (files) => {
|
|
216
|
+
const s3Client = new S3Client();
|
|
217
|
+
const sessionId = getNanoid(8);
|
|
218
|
+
const presignedFiles = await Promise.all(files.map(async (file) => {
|
|
219
|
+
const metadataWithSession = {
|
|
220
|
+
...file.metadata,
|
|
221
|
+
"x-ocel-session-id": sessionId,
|
|
222
|
+
};
|
|
223
|
+
await UploadSession.create({
|
|
224
|
+
sessionId,
|
|
225
|
+
bucketName: bucketId,
|
|
226
|
+
fileKey: file.key,
|
|
227
|
+
createdAt: new Date().toISOString(),
|
|
228
|
+
status: "PENDING",
|
|
229
|
+
contentType: file.mimeType,
|
|
230
|
+
contentDisposition: file.disposition,
|
|
231
|
+
fileSize: file.size,
|
|
232
|
+
metadata: file.metadata
|
|
233
|
+
? JSON.stringify(file.metadata)
|
|
234
|
+
: undefined,
|
|
235
|
+
}).go();
|
|
236
|
+
const url = await getSignedUrl(s3Client, new PutObjectCommand({
|
|
237
|
+
Key: file.key,
|
|
238
|
+
Bucket: bucketId,
|
|
239
|
+
ContentType: file.mimeType,
|
|
240
|
+
Metadata: metadataWithSession,
|
|
241
|
+
}), { expiresIn: 3600 });
|
|
242
|
+
return {
|
|
243
|
+
url,
|
|
244
|
+
key: file.key,
|
|
245
|
+
name: file.name,
|
|
246
|
+
};
|
|
247
|
+
}));
|
|
248
|
+
this.startPolling(sessionId);
|
|
249
|
+
return {
|
|
250
|
+
files: presignedFiles,
|
|
251
|
+
sessionId,
|
|
252
|
+
};
|
|
253
|
+
},
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
createConfigBasedGenerator(config) {
|
|
257
|
+
return (ctx) => {
|
|
258
|
+
let key = ctx.fileNameWithoutExt;
|
|
259
|
+
// random suffix
|
|
260
|
+
if (config.randomSuffix) {
|
|
261
|
+
key += `-${Math.random().toString(36).slice(2, 10)}`;
|
|
262
|
+
}
|
|
263
|
+
// static suffix
|
|
264
|
+
else if (config.suffix) {
|
|
265
|
+
key += `-${config.suffix}`;
|
|
266
|
+
}
|
|
267
|
+
// add extension
|
|
268
|
+
key += ctx.ext;
|
|
269
|
+
// prefix
|
|
270
|
+
if (config.prefix) {
|
|
271
|
+
return `${config.prefix.replace(/\/$/, "")}/${key}`;
|
|
272
|
+
}
|
|
273
|
+
return key;
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
validateFiles(files, metadata) {
|
|
277
|
+
const { accept, limits } = this.config;
|
|
278
|
+
if (limits?.maxFileCount !== undefined) {
|
|
279
|
+
const max = typeof limits.maxFileCount === "function"
|
|
280
|
+
? limits.maxFileCount({ metadata })
|
|
281
|
+
: limits.maxFileCount;
|
|
282
|
+
if (files.length > max) {
|
|
283
|
+
throw UploadError.badRequest(`File limit exceeded. Maximum files allowed: ${max}.`);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (limits?.minFileCount !== undefined) {
|
|
287
|
+
const min = typeof limits.minFileCount === "function"
|
|
288
|
+
? limits.minFileCount({ metadata })
|
|
289
|
+
: limits.minFileCount;
|
|
290
|
+
if (files.length < min) {
|
|
291
|
+
throw UploadError.badRequest(`File limit not met. Minimum files required: ${min}.`);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
// 2. Per-File Validation (Size and MIME Type)
|
|
295
|
+
for (const file of files) {
|
|
296
|
+
// MIME Type Validation
|
|
297
|
+
if (accept && accept.length > 0) {
|
|
298
|
+
const isAccepted = accept.some((allowedType) => {
|
|
299
|
+
// Simple check for wildcards (*/*) and direct matches (image/png)
|
|
300
|
+
if (allowedType.endsWith("/*")) {
|
|
301
|
+
const typeGroup = allowedType.slice(0, -2);
|
|
302
|
+
return file.mimeType.startsWith(typeGroup);
|
|
303
|
+
}
|
|
304
|
+
return file.mimeType === allowedType;
|
|
305
|
+
});
|
|
306
|
+
if (!isAccepted) {
|
|
307
|
+
throw UploadError.badRequest(`File type not allowed: ${file.mimeType}. Must be one of: ${accept.join(", ")}.`);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
// Max File Size Validation
|
|
311
|
+
if (limits?.maxFileSize !== undefined) {
|
|
312
|
+
const sizeValue = typeof limits.maxFileSize === "function"
|
|
313
|
+
? limits.maxFileSize({ metadata })
|
|
314
|
+
: limits.maxFileSize;
|
|
315
|
+
const maxSizeInBytes = parseFileSize(sizeValue);
|
|
316
|
+
if (file.size > maxSizeInBytes) {
|
|
317
|
+
// You might want to convert maxSize from bytes to KB/MB for a better error message
|
|
318
|
+
throw UploadError.badRequest(`File size too large. Maximum size: ${maxSizeInBytes} bytes.`);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Entity } from "electrodb";
|
|
2
|
+
export declare const UploadSession: Entity<string, string, string, {
|
|
3
|
+
model: {
|
|
4
|
+
entity: string;
|
|
5
|
+
version: string;
|
|
6
|
+
service: string;
|
|
7
|
+
};
|
|
8
|
+
attributes: {
|
|
9
|
+
sessionId: {
|
|
10
|
+
type: "string";
|
|
11
|
+
required: true;
|
|
12
|
+
};
|
|
13
|
+
bucketName: {
|
|
14
|
+
type: "string";
|
|
15
|
+
required: true;
|
|
16
|
+
};
|
|
17
|
+
fileKey: {
|
|
18
|
+
type: "string";
|
|
19
|
+
required: true;
|
|
20
|
+
};
|
|
21
|
+
contentType: {
|
|
22
|
+
type: "string";
|
|
23
|
+
required: true;
|
|
24
|
+
};
|
|
25
|
+
contentDisposition: {
|
|
26
|
+
type: "string";
|
|
27
|
+
required: false;
|
|
28
|
+
};
|
|
29
|
+
fileSize: {
|
|
30
|
+
type: "number";
|
|
31
|
+
required: true;
|
|
32
|
+
};
|
|
33
|
+
createdAt: {
|
|
34
|
+
type: "string";
|
|
35
|
+
required: true;
|
|
36
|
+
default: () => string;
|
|
37
|
+
};
|
|
38
|
+
updatedAt: {
|
|
39
|
+
type: "string";
|
|
40
|
+
required: true;
|
|
41
|
+
default: () => string;
|
|
42
|
+
set: () => string;
|
|
43
|
+
};
|
|
44
|
+
status: {
|
|
45
|
+
type: readonly ["PENDING", "SUCCESS", "FAILED"];
|
|
46
|
+
required: true;
|
|
47
|
+
default: string;
|
|
48
|
+
};
|
|
49
|
+
metadata: {
|
|
50
|
+
type: "string";
|
|
51
|
+
required: false;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
indexes: {
|
|
55
|
+
sessions: {
|
|
56
|
+
pk: {
|
|
57
|
+
field: string;
|
|
58
|
+
composite: "sessionId"[];
|
|
59
|
+
template: string;
|
|
60
|
+
};
|
|
61
|
+
sk: {
|
|
62
|
+
field: string;
|
|
63
|
+
composite: ("bucketName" | "fileKey")[];
|
|
64
|
+
template: string;
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
}>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Entity } from "electrodb";
|
|
2
|
+
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
|
|
3
|
+
const client = new DynamoDBClient({});
|
|
4
|
+
export const UploadSession = new Entity({
|
|
5
|
+
model: {
|
|
6
|
+
entity: "upload-session",
|
|
7
|
+
version: "1",
|
|
8
|
+
service: "blob",
|
|
9
|
+
},
|
|
10
|
+
attributes: {
|
|
11
|
+
sessionId: {
|
|
12
|
+
type: "string",
|
|
13
|
+
required: true,
|
|
14
|
+
},
|
|
15
|
+
bucketName: {
|
|
16
|
+
type: "string",
|
|
17
|
+
required: true,
|
|
18
|
+
},
|
|
19
|
+
fileKey: {
|
|
20
|
+
type: "string",
|
|
21
|
+
required: true,
|
|
22
|
+
},
|
|
23
|
+
contentType: {
|
|
24
|
+
type: "string",
|
|
25
|
+
required: true,
|
|
26
|
+
},
|
|
27
|
+
contentDisposition: {
|
|
28
|
+
type: "string",
|
|
29
|
+
required: false,
|
|
30
|
+
},
|
|
31
|
+
fileSize: {
|
|
32
|
+
type: "number",
|
|
33
|
+
required: true,
|
|
34
|
+
},
|
|
35
|
+
createdAt: {
|
|
36
|
+
type: "string",
|
|
37
|
+
required: true,
|
|
38
|
+
default: () => new Date().toISOString(),
|
|
39
|
+
},
|
|
40
|
+
updatedAt: {
|
|
41
|
+
type: "string",
|
|
42
|
+
required: true,
|
|
43
|
+
default: () => new Date().toISOString(),
|
|
44
|
+
set: () => new Date().toISOString(),
|
|
45
|
+
},
|
|
46
|
+
status: {
|
|
47
|
+
type: ["PENDING", "SUCCESS", "FAILED"],
|
|
48
|
+
required: true,
|
|
49
|
+
default: "PENDING",
|
|
50
|
+
},
|
|
51
|
+
metadata: {
|
|
52
|
+
type: "string",
|
|
53
|
+
required: false,
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
indexes: {
|
|
57
|
+
sessions: {
|
|
58
|
+
pk: {
|
|
59
|
+
field: "pk",
|
|
60
|
+
composite: ["sessionId"],
|
|
61
|
+
template: "SESSION#${sessionId}",
|
|
62
|
+
},
|
|
63
|
+
sk: {
|
|
64
|
+
field: "sk",
|
|
65
|
+
composite: ["bucketName", "fileKey"],
|
|
66
|
+
template: "FILE#${bucketName}#${fileKey}",
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
}, {
|
|
71
|
+
client,
|
|
72
|
+
table: "OcelTable", // required for all ocel logic
|
|
73
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Lambda, } from "../lambda";
|
|
2
|
+
import sls from "serverless-http";
|
|
3
|
+
export function lambda(id, app, config) {
|
|
4
|
+
const handler = sls(app);
|
|
5
|
+
return new Lambda(id, {
|
|
6
|
+
...config,
|
|
7
|
+
handler: handler,
|
|
8
|
+
trigger: {
|
|
9
|
+
type: "url",
|
|
10
|
+
config: {},
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Lambda, } from "../lambda";
|
|
2
|
+
import { handle } from "hono/aws-lambda";
|
|
3
|
+
export function lambda(id, app, config) {
|
|
4
|
+
const handler = handle(app);
|
|
5
|
+
return new Lambda(id, {
|
|
6
|
+
...config,
|
|
7
|
+
handler: handler,
|
|
8
|
+
trigger: {
|
|
9
|
+
type: "url",
|
|
10
|
+
config: {},
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Component } from "../utils/component";
|
|
2
|
+
import type { Context, APIGatewayEvent, S3Event, LambdaFunctionURLEvent } from "aws-lambda";
|
|
3
|
+
export type TriggerMap = {
|
|
4
|
+
s3: {
|
|
5
|
+
bucket: string;
|
|
6
|
+
events: string[];
|
|
7
|
+
};
|
|
8
|
+
cron: {
|
|
9
|
+
schedule: string;
|
|
10
|
+
};
|
|
11
|
+
url: {
|
|
12
|
+
streaming?: boolean;
|
|
13
|
+
};
|
|
14
|
+
api: {
|
|
15
|
+
id?: string;
|
|
16
|
+
path: string;
|
|
17
|
+
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "*";
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export type TriggerUnion = {
|
|
21
|
+
[K in keyof TriggerMap]: {
|
|
22
|
+
type: K;
|
|
23
|
+
config: TriggerMap[K];
|
|
24
|
+
};
|
|
25
|
+
}[keyof TriggerMap];
|
|
26
|
+
export type LambdaHandlerOutput<T extends keyof TriggerMap> = T extends "api" ? {
|
|
27
|
+
statusCode: number;
|
|
28
|
+
headers: Record<string, string>;
|
|
29
|
+
body: string;
|
|
30
|
+
} : any;
|
|
31
|
+
export type LambdaEvent<T extends keyof TriggerMap> = T extends "s3" ? S3Event : T extends "api" ? APIGatewayEvent : T extends "url" ? LambdaFunctionURLEvent : any;
|
|
32
|
+
export interface LambdaConfig<T extends keyof TriggerMap> {
|
|
33
|
+
handler: (event: LambdaEvent<T>, context: Context) => Promise<LambdaHandlerOutput<T>> | LambdaHandlerOutput<T>;
|
|
34
|
+
trigger: Extract<TriggerUnion, {
|
|
35
|
+
type: T;
|
|
36
|
+
}>;
|
|
37
|
+
link?: Component[];
|
|
38
|
+
}
|
|
39
|
+
export declare class Lambda<T extends keyof TriggerMap> implements Component {
|
|
40
|
+
id: string;
|
|
41
|
+
constructor(id: string, config: LambdaConfig<T>);
|
|
42
|
+
__id(): string;
|
|
43
|
+
}
|