deepspace 0.12.0 → 0.13.0
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/CHANGELOG.md +153 -0
- package/dist/{chunk-ZJ43EK3N.js → chunk-233GQU2H.js} +492 -365
- package/dist/chunk-233GQU2H.js.map +1 -0
- package/dist/cli.js +441 -167
- package/dist/cli.js.map +1 -1
- package/dist/documentation-client-core.d.ts +2 -2
- package/dist/documentation-client-core.js +1 -1
- package/dist/documentation-default-renderer.cjs +34 -34
- package/dist/documentation-react.d.ts +1 -1
- package/dist/documentation-react.js +1 -1
- package/dist/documentation-runtime.js +10 -10
- package/dist/documentation-server-core.d.ts +2 -2
- package/dist/documentation-server-core.js +1 -1
- package/dist/documentation.js +2 -2
- package/dist/documentation.js.map +1 -1
- package/dist/index.d.ts +28 -3
- package/dist/index.js +86 -8
- package/dist/index.js.map +1 -1
- package/dist/{public-BGRX_Cjd.d.ts → public-BtArwbVM.d.ts} +8 -0
- package/dist/server.d.ts +94 -23
- package/dist/server.js +340 -87
- package/dist/server.js.map +1 -1
- package/dist/worker.d.ts +95 -24
- package/dist/worker.js +345 -87
- package/dist/worker.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-ZJ43EK3N.js.map +0 -1
package/dist/server.js
CHANGED
|
@@ -175,8 +175,28 @@ var BUILT_IN_TOOLS = [
|
|
|
175
175
|
];
|
|
176
176
|
|
|
177
177
|
// src/shared/app-files.ts
|
|
178
|
-
var MAX_APP_FILE_BYTES =
|
|
178
|
+
var MAX_APP_FILE_BYTES = 1024 * 1024 * 1024;
|
|
179
|
+
var MAX_UPLOAD_REQUEST_BYTES = 25 * 1024 * 1024;
|
|
180
|
+
var UPLOAD_PART_BYTES = 20 * 1024 * 1024;
|
|
181
|
+
var MAX_UPLOAD_PARTS = Math.ceil(MAX_APP_FILE_BYTES / UPLOAD_PART_BYTES);
|
|
182
|
+
var MAX_BASE64_UPLOAD_BYTES = Math.floor(MAX_UPLOAD_REQUEST_BYTES * 3 / 4);
|
|
183
|
+
var MAX_DEPLOY_ASSET_FILE_BYTES = 25 * 1024 * 1024;
|
|
184
|
+
var DANGEROUS_MIME_TYPES = /* @__PURE__ */ new Set([
|
|
185
|
+
"text/html",
|
|
186
|
+
"application/xhtml+xml",
|
|
187
|
+
"image/svg+xml",
|
|
188
|
+
"application/xml",
|
|
189
|
+
"text/xml",
|
|
190
|
+
"application/javascript",
|
|
191
|
+
"text/javascript",
|
|
192
|
+
"application/ecmascript",
|
|
193
|
+
"text/ecmascript"
|
|
194
|
+
]);
|
|
195
|
+
function activeContentMessage(mimeType) {
|
|
196
|
+
return `${mimeType} is not accepted: the files API serves your app's own origin, so stored HTML/SVG/JavaScript would execute there with your users' sessions. Ship it as a build asset under \`public/\` instead, where it is served as part of your app rather than as user-uploaded content.`;
|
|
197
|
+
}
|
|
179
198
|
function formatBytes(bytes) {
|
|
199
|
+
if (bytes >= 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GiB`;
|
|
180
200
|
if (bytes >= 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MiB`;
|
|
181
201
|
if (bytes >= 1024) return `${Math.round(bytes / 1024)} KiB`;
|
|
182
202
|
return `${bytes} B`;
|
|
@@ -184,12 +204,24 @@ function formatBytes(bytes) {
|
|
|
184
204
|
function encodeKeyPath(key) {
|
|
185
205
|
return key.split("/").map(encodeURIComponent).join("/");
|
|
186
206
|
}
|
|
187
|
-
function oversizeMessage(bytes) {
|
|
188
|
-
return `That file is ${formatBytes(bytes)}; the limit is ${formatBytes(
|
|
207
|
+
function oversizeMessage(bytes, limit = MAX_APP_FILE_BYTES) {
|
|
208
|
+
return `That file is ${formatBytes(bytes)}; the limit is ${formatBytes(limit)} per file.`;
|
|
209
|
+
}
|
|
210
|
+
function oversizeRequestMessage(bytes) {
|
|
211
|
+
return `This request carries ${formatBytes(bytes)}; one request may carry ${formatBytes(MAX_UPLOAD_REQUEST_BYTES)}. Files above ${formatBytes(UPLOAD_PART_BYTES)} upload in parts.`;
|
|
212
|
+
}
|
|
213
|
+
function oversizePartMessage(bytes) {
|
|
214
|
+
return `This part carries ${formatBytes(bytes)}; a part may carry ${formatBytes(UPLOAD_PART_BYTES)} \u2014 the size the upload advertised.`;
|
|
189
215
|
}
|
|
190
216
|
|
|
191
217
|
// src/server/utils/scoped-r2-files.ts
|
|
192
218
|
var CORS_HEADERS = { "Access-Control-Allow-Origin": "*" };
|
|
219
|
+
function fail(status, error, code, extra) {
|
|
220
|
+
return Response.json(
|
|
221
|
+
{ error, ...code ? { code } : {}, ...extra },
|
|
222
|
+
{ status, headers: CORS_HEADERS }
|
|
223
|
+
);
|
|
224
|
+
}
|
|
193
225
|
function contentDisposition(originalName) {
|
|
194
226
|
const safe = originalName.replace(/[^\x20-\x7e]|["\\]/g, "_") || "download";
|
|
195
227
|
const encoded = encodeURIComponent(originalName).replace(
|
|
@@ -204,29 +236,89 @@ function sanitizeSubpath(raw) {
|
|
|
204
236
|
if (segments.some((s) => s === ".." || s === ".")) return null;
|
|
205
237
|
return raw;
|
|
206
238
|
}
|
|
207
|
-
function
|
|
208
|
-
if (bytes <=
|
|
209
|
-
return
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
);
|
|
239
|
+
function overRequestBound(bytes, slack = 0) {
|
|
240
|
+
if (bytes <= MAX_UPLOAD_REQUEST_BYTES + slack) return null;
|
|
241
|
+
return fail(413, oversizeRequestMessage(bytes), "too_large", {
|
|
242
|
+
maxBytes: MAX_UPLOAD_REQUEST_BYTES,
|
|
243
|
+
partSize: UPLOAD_PART_BYTES
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
function overCeiling(bytes) {
|
|
247
|
+
if (bytes <= MAX_APP_FILE_BYTES) return null;
|
|
248
|
+
return fail(413, oversizeMessage(bytes), "too_large", { maxBytes: MAX_APP_FILE_BYTES });
|
|
249
|
+
}
|
|
250
|
+
function overPartBound(bytes) {
|
|
251
|
+
if (bytes <= UPLOAD_PART_BYTES) return null;
|
|
252
|
+
return fail(413, oversizePartMessage(bytes), "too_large", { maxBytes: UPLOAD_PART_BYTES });
|
|
253
|
+
}
|
|
254
|
+
function declaredLength(request, what) {
|
|
255
|
+
const raw = request.headers.get("content-length");
|
|
256
|
+
const declared = raw === null ? NaN : Number(raw);
|
|
257
|
+
if (!Number.isSafeInteger(declared) || declared < 0) {
|
|
258
|
+
return fail(
|
|
259
|
+
411,
|
|
260
|
+
`${what} must declare a Content-Length. A chunked or length-less body is refused: R2 cannot size a write it has no length for.`,
|
|
261
|
+
"length_required"
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
return declared;
|
|
213
265
|
}
|
|
214
266
|
function isKeyWithinPrefix(key, prefix) {
|
|
215
267
|
return key.startsWith(prefix);
|
|
216
268
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
"
|
|
220
|
-
"
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
"
|
|
225
|
-
"
|
|
226
|
-
"
|
|
269
|
+
function anchorKey(raw, prefix) {
|
|
270
|
+
const safe = sanitizeSubpath(raw);
|
|
271
|
+
if (safe === null) return fail(400, "Invalid key: traversal not allowed", "invalid_key");
|
|
272
|
+
if (!safe) return fail(400, "Invalid key: empty", "invalid_key");
|
|
273
|
+
return { key: `${prefix}${safe}` };
|
|
274
|
+
}
|
|
275
|
+
var VERB_ROUTES = /* @__PURE__ */ new Set([
|
|
276
|
+
"POST upload",
|
|
277
|
+
"POST multipart",
|
|
278
|
+
"DELETE multipart",
|
|
279
|
+
"PUT multipart/part",
|
|
280
|
+
"POST multipart/complete"
|
|
227
281
|
]);
|
|
228
|
-
function
|
|
229
|
-
return
|
|
282
|
+
function isFilesVerbPath(method, subpath) {
|
|
283
|
+
return VERB_ROUTES.has(`${method} ${subpath}`);
|
|
284
|
+
}
|
|
285
|
+
function resolveMimeType(raw) {
|
|
286
|
+
const mimeType = raw.split(";")[0].trim().toLowerCase() || "application/octet-stream";
|
|
287
|
+
return DANGEROUS_MIME_TYPES.has(mimeType) ? fail(415, activeContentMessage(mimeType), "active_content") : { mimeType };
|
|
288
|
+
}
|
|
289
|
+
function r2ErrorCode(error) {
|
|
290
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
291
|
+
const match = /\((\d{4,6})\)\s*$/.exec(message.trim());
|
|
292
|
+
return match ? Number(match[1]) : null;
|
|
293
|
+
}
|
|
294
|
+
function multipartFailure(error) {
|
|
295
|
+
switch (r2ErrorCode(error)) {
|
|
296
|
+
case 10024:
|
|
297
|
+
return fail(
|
|
298
|
+
404,
|
|
299
|
+
"That upload does not exist \u2014 it was completed, abandoned, or expired. Start a new one.",
|
|
300
|
+
"unknown_upload"
|
|
301
|
+
);
|
|
302
|
+
case 10011:
|
|
303
|
+
// EntityTooSmall
|
|
304
|
+
case 10025:
|
|
305
|
+
// InvalidPart — a named part was never uploaded
|
|
306
|
+
case 10048:
|
|
307
|
+
return fail(
|
|
308
|
+
400,
|
|
309
|
+
"The parts do not assemble: every part except the last must be exactly the size the upload advertised, and each must be uploaded before it is named.",
|
|
310
|
+
"bad_parts"
|
|
311
|
+
);
|
|
312
|
+
case 10012:
|
|
313
|
+
// MetadataTooLarge
|
|
314
|
+
case 10020:
|
|
315
|
+
return fail(400, "The upload names a key or metadata R2 will not accept.", "invalid_key");
|
|
316
|
+
default: {
|
|
317
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
318
|
+
console.error("[files:multipart] R2 error:", message);
|
|
319
|
+
return fail(503, "The files service could not complete that upload. Retry shortly.", "r2_error");
|
|
320
|
+
}
|
|
321
|
+
}
|
|
230
322
|
}
|
|
231
323
|
function createScopedR2Handler(config) {
|
|
232
324
|
const requireAuthForMutations = config.requireAuthForMutations ?? true;
|
|
@@ -258,14 +350,12 @@ function createScopedR2Handler(config) {
|
|
|
258
350
|
return Response.json({ error: errorMsg }, { status, headers: CORS_HEADERS });
|
|
259
351
|
}
|
|
260
352
|
const prefix = result.prefix;
|
|
261
|
-
if (
|
|
353
|
+
if (isFilesVerbPath(request.method, subpath)) {
|
|
262
354
|
if (requireAuthForMutations && !auth.userId) {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
{ status: 401, headers: CORS_HEADERS }
|
|
266
|
-
);
|
|
355
|
+
await request.body?.cancel().catch(() => void 0);
|
|
356
|
+
return fail(401, "Authentication required", "unauthorized");
|
|
267
357
|
}
|
|
268
|
-
return handleUpload(request, url, bucket, prefix, auth.userId, scope);
|
|
358
|
+
return subpath === "upload" ? handleUpload(request, url, bucket, prefix, auth.userId, scope) : handleMultipart(request, url, bucket, prefix, auth.userId, scope, subpath);
|
|
269
359
|
}
|
|
270
360
|
if (!subpath && request.method === "GET") {
|
|
271
361
|
return handleList(bucket, prefix, url, scope);
|
|
@@ -292,93 +382,250 @@ function createScopedR2Handler(config) {
|
|
|
292
382
|
{ status: 403, headers: CORS_HEADERS }
|
|
293
383
|
);
|
|
294
384
|
}
|
|
295
|
-
await bucket.
|
|
296
|
-
|
|
385
|
+
const existed = await bucket.head(subpath) !== null;
|
|
386
|
+
if (existed) await bucket.delete(subpath);
|
|
387
|
+
return Response.json(
|
|
388
|
+
{ success: true, deleted: existed ? subpath : null, existed },
|
|
389
|
+
{ headers: CORS_HEADERS }
|
|
390
|
+
);
|
|
297
391
|
}
|
|
298
392
|
return Response.json({ error: "Method not allowed" }, { status: 405, headers: CORS_HEADERS });
|
|
299
393
|
};
|
|
300
394
|
}
|
|
301
395
|
async function handleUpload(request, url, bucket, prefix, userId, scope) {
|
|
302
396
|
try {
|
|
303
|
-
const declared =
|
|
397
|
+
const declared = overRequestBound(Number(request.headers.get("content-length")), 8 * 1024);
|
|
304
398
|
if (declared) return declared;
|
|
305
399
|
const contentType = request.headers.get("content-type") || "";
|
|
306
400
|
let fileData;
|
|
307
401
|
let fileName;
|
|
308
|
-
let
|
|
402
|
+
let rawMimeType;
|
|
309
403
|
if (contentType.includes("multipart/form-data")) {
|
|
310
404
|
const formData = await request.formData();
|
|
311
405
|
const file = formData.get("file");
|
|
312
|
-
if (!file)
|
|
313
|
-
|
|
314
|
-
}
|
|
315
|
-
const tooBig = oversize(file.size);
|
|
406
|
+
if (!file) return fail(400, "No file provided", "bad_request");
|
|
407
|
+
const tooBig = overRequestBound(file.size);
|
|
316
408
|
if (tooBig) return tooBig;
|
|
317
409
|
fileData = await file.arrayBuffer();
|
|
318
410
|
fileName = formData.get("name")?.toString() || file.name;
|
|
319
|
-
|
|
411
|
+
rawMimeType = file.type || "application/octet-stream";
|
|
320
412
|
} else {
|
|
321
413
|
const body = await request.json();
|
|
322
|
-
if (!body.data || !body.name)
|
|
323
|
-
return Response.json(
|
|
324
|
-
{ error: "Missing data or name" },
|
|
325
|
-
{ status: 400, headers: CORS_HEADERS }
|
|
326
|
-
);
|
|
327
|
-
}
|
|
414
|
+
if (!body.data || !body.name) return fail(400, "Missing data or name", "bad_request");
|
|
328
415
|
const base64Data = body.data.replace(/^data:[^;]+;base64,/, "");
|
|
329
|
-
const tooBig =
|
|
416
|
+
const tooBig = overRequestBound(Math.floor(base64Data.length * 3 / 4));
|
|
330
417
|
if (tooBig) return tooBig;
|
|
331
418
|
fileData = Uint8Array.from(atob(base64Data), (c) => c.charCodeAt(0)).buffer;
|
|
332
419
|
fileName = body.name;
|
|
333
|
-
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
if (
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
const requestedKey = url.searchParams.get("key");
|
|
343
|
-
let key;
|
|
344
|
-
if (requestedKey !== null) {
|
|
345
|
-
const safe = sanitizeSubpath(requestedKey);
|
|
346
|
-
if (safe === null) {
|
|
347
|
-
return Response.json(
|
|
348
|
-
{ error: "Invalid key: traversal not allowed" },
|
|
349
|
-
{ status: 400, headers: CORS_HEADERS }
|
|
350
|
-
);
|
|
351
|
-
}
|
|
352
|
-
if (!safe) {
|
|
353
|
-
return Response.json(
|
|
354
|
-
{ error: "Invalid key: empty" },
|
|
355
|
-
{ status: 400, headers: CORS_HEADERS }
|
|
356
|
-
);
|
|
357
|
-
}
|
|
358
|
-
key = `${prefix}${safe}`;
|
|
359
|
-
} else {
|
|
360
|
-
const fileKey = `${Date.now()}-${Math.random().toString(36).slice(2, 11)}-${fileName}`;
|
|
361
|
-
key = `${prefix}${fileKey}`;
|
|
362
|
-
}
|
|
363
|
-
await bucket.put(key, fileData, {
|
|
364
|
-
httpMetadata: { contentType: mimeType },
|
|
365
|
-
customMetadata: {
|
|
366
|
-
originalName: fileName,
|
|
367
|
-
...userId ? { uploadedBy: userId } : {},
|
|
368
|
-
uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
369
|
-
}
|
|
420
|
+
rawMimeType = body.mimeType || "application/octet-stream";
|
|
421
|
+
}
|
|
422
|
+
const mime = resolveMimeType(rawMimeType);
|
|
423
|
+
if (mime instanceof Response) return mime;
|
|
424
|
+
const located = locateUpload(url, prefix, fileName);
|
|
425
|
+
if (located instanceof Response) return located;
|
|
426
|
+
await bucket.put(located.key, fileData, {
|
|
427
|
+
httpMetadata: { contentType: mime.mimeType },
|
|
428
|
+
customMetadata: fileMetadata(fileName, userId)
|
|
370
429
|
});
|
|
371
|
-
|
|
372
|
-
fileUrl.searchParams.set("scope", scope);
|
|
373
|
-
return Response.json(
|
|
374
|
-
{ success: true, key, url: fileUrl.toString(), name: fileName },
|
|
375
|
-
{ headers: CORS_HEADERS }
|
|
376
|
-
);
|
|
430
|
+
return storedFileJson(located.key, fileName, url, scope);
|
|
377
431
|
} catch (err) {
|
|
378
432
|
const msg = err instanceof Error ? err.message : "Upload failed";
|
|
379
433
|
console.error("[handleUpload] Error:", msg, err instanceof Error ? err.stack : "");
|
|
380
|
-
return
|
|
434
|
+
return fail(500, msg, "upload_failed");
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
function locateUpload(url, prefix, fileName) {
|
|
438
|
+
const requested = url.searchParams.get("key");
|
|
439
|
+
const subkey = requested ?? `${Date.now()}-${Math.random().toString(36).slice(2, 11)}-${fileName}`;
|
|
440
|
+
const anchored = anchorKey(subkey, prefix);
|
|
441
|
+
return anchored instanceof Response ? anchored : { key: anchored.key, subkey };
|
|
442
|
+
}
|
|
443
|
+
function fileMetadata(fileName, userId) {
|
|
444
|
+
return {
|
|
445
|
+
originalName: fileName,
|
|
446
|
+
...userId ? { uploadedBy: userId } : {},
|
|
447
|
+
uploadedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
function storedFileJson(key, name, url, scope) {
|
|
451
|
+
const fileUrl = new URL(`/api/files/${encodeKeyPath(key)}`, url.origin);
|
|
452
|
+
fileUrl.searchParams.set("scope", scope);
|
|
453
|
+
return Response.json(
|
|
454
|
+
{ success: true, key, url: fileUrl.toString(), name },
|
|
455
|
+
{ headers: CORS_HEADERS }
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
var MAX_CONTROL_BODY_BYTES = 64 * 1024;
|
|
459
|
+
async function readControlJson(request) {
|
|
460
|
+
const declared = declaredLength(request, "This request");
|
|
461
|
+
if (declared instanceof Response) return declared;
|
|
462
|
+
if (declared > MAX_CONTROL_BODY_BYTES) {
|
|
463
|
+
return fail(413, `Request body exceeds ${MAX_CONTROL_BODY_BYTES} bytes`, "too_large");
|
|
464
|
+
}
|
|
465
|
+
let parsed;
|
|
466
|
+
try {
|
|
467
|
+
parsed = await request.json();
|
|
468
|
+
} catch {
|
|
469
|
+
return fail(400, "Body must be JSON", "bad_request");
|
|
470
|
+
}
|
|
471
|
+
return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed : fail(400, "Body must be a JSON object", "bad_request");
|
|
472
|
+
}
|
|
473
|
+
function resolveSession(url, prefix) {
|
|
474
|
+
const uploadKey = url.searchParams.get("uploadKey");
|
|
475
|
+
const uploadId = url.searchParams.get("uploadId");
|
|
476
|
+
if (!uploadKey || !uploadId) {
|
|
477
|
+
return fail(400, "uploadKey and uploadId are required", "bad_request");
|
|
478
|
+
}
|
|
479
|
+
const anchored = anchorKey(uploadKey, prefix);
|
|
480
|
+
return anchored instanceof Response ? anchored : { key: anchored.key, uploadId };
|
|
481
|
+
}
|
|
482
|
+
async function streamPart(upload, partNumber, body, expectedBytes) {
|
|
483
|
+
let observedBytes = 0;
|
|
484
|
+
const sized = new FixedLengthStream(expectedBytes);
|
|
485
|
+
const pumped = body.pipeThrough(
|
|
486
|
+
new TransformStream({
|
|
487
|
+
transform(chunk, controller) {
|
|
488
|
+
observedBytes += chunk.byteLength;
|
|
489
|
+
controller.enqueue(chunk);
|
|
490
|
+
}
|
|
491
|
+
})
|
|
492
|
+
).pipeTo(sized.writable);
|
|
493
|
+
const [uploaded, drained] = await Promise.allSettled([
|
|
494
|
+
upload.uploadPart(partNumber, sized.readable),
|
|
495
|
+
pumped
|
|
496
|
+
]);
|
|
497
|
+
if (uploaded.status === "rejected" || drained.status === "rejected") {
|
|
498
|
+
const reason = uploaded.status === "rejected" ? uploaded.reason : drained.reason;
|
|
499
|
+
return { ok: false, lengthMismatch: observedBytes !== expectedBytes, error: reason };
|
|
500
|
+
}
|
|
501
|
+
return { ok: true, part: uploaded.value };
|
|
502
|
+
}
|
|
503
|
+
async function handleMultipart(request, url, bucket, prefix, userId, scope, subpath) {
|
|
504
|
+
if (subpath === "multipart" && request.method === "POST") {
|
|
505
|
+
return multipartInit(request, url, bucket, prefix, userId);
|
|
506
|
+
}
|
|
507
|
+
if (subpath === "multipart/part") return multipartPart(request, url, bucket, prefix);
|
|
508
|
+
if (subpath === "multipart/complete") {
|
|
509
|
+
return multipartComplete(request, url, bucket, prefix, scope);
|
|
510
|
+
}
|
|
511
|
+
return multipartAbort(url, bucket, prefix);
|
|
512
|
+
}
|
|
513
|
+
async function multipartInit(request, url, bucket, prefix, userId) {
|
|
514
|
+
const body = await readControlJson(request);
|
|
515
|
+
if (body instanceof Response) return body;
|
|
516
|
+
const size = body.size;
|
|
517
|
+
if (!Number.isSafeInteger(size) || size < 0) {
|
|
518
|
+
return fail(400, "size must be a non-negative integer number of bytes", "bad_request");
|
|
519
|
+
}
|
|
520
|
+
const tooBig = overCeiling(size);
|
|
521
|
+
if (tooBig) return tooBig;
|
|
522
|
+
const name = typeof body.name === "string" ? body.name.trim() : "";
|
|
523
|
+
if (!name) return fail(400, "name is required", "bad_request");
|
|
524
|
+
const mime = resolveMimeType(typeof body.mimeType === "string" ? body.mimeType : "");
|
|
525
|
+
if (mime instanceof Response) return mime;
|
|
526
|
+
const located = locateUpload(url, prefix, name);
|
|
527
|
+
if (located instanceof Response) return located;
|
|
528
|
+
let upload;
|
|
529
|
+
try {
|
|
530
|
+
upload = await bucket.createMultipartUpload(located.key, {
|
|
531
|
+
httpMetadata: { contentType: mime.mimeType },
|
|
532
|
+
customMetadata: fileMetadata(name, userId)
|
|
533
|
+
});
|
|
534
|
+
} catch (error) {
|
|
535
|
+
return multipartFailure(error);
|
|
536
|
+
}
|
|
537
|
+
return Response.json(
|
|
538
|
+
{
|
|
539
|
+
uploadId: upload.uploadId,
|
|
540
|
+
// Echoed back on every later call. Relative to the app, exactly like
|
|
541
|
+
// `?key=`, so the mount that anchors keys anchors this one too.
|
|
542
|
+
uploadKey: located.subkey,
|
|
543
|
+
partSize: UPLOAD_PART_BYTES,
|
|
544
|
+
maxParts: MAX_UPLOAD_PARTS,
|
|
545
|
+
maxBytes: MAX_APP_FILE_BYTES
|
|
546
|
+
},
|
|
547
|
+
{ headers: CORS_HEADERS }
|
|
548
|
+
);
|
|
549
|
+
}
|
|
550
|
+
async function multipartPart(request, url, bucket, prefix) {
|
|
551
|
+
const refuse = async (response) => {
|
|
552
|
+
await request.body?.cancel().catch(() => void 0);
|
|
553
|
+
return response;
|
|
554
|
+
};
|
|
555
|
+
const declaredBytes = declaredLength(request, "Part uploads");
|
|
556
|
+
if (declaredBytes instanceof Response) return refuse(declaredBytes);
|
|
557
|
+
const tooBig = overPartBound(declaredBytes);
|
|
558
|
+
if (tooBig) return refuse(tooBig);
|
|
559
|
+
const partNumber = Number(url.searchParams.get("partNumber"));
|
|
560
|
+
if (!Number.isSafeInteger(partNumber) || partNumber < 1 || partNumber > MAX_UPLOAD_PARTS) {
|
|
561
|
+
return refuse(
|
|
562
|
+
fail(400, `partNumber must be a whole number from 1 to ${MAX_UPLOAD_PARTS}`, "bad_request")
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
const session = resolveSession(url, prefix);
|
|
566
|
+
if (session instanceof Response) return refuse(session);
|
|
567
|
+
if (!request.body) return fail(400, "A part body is required", "bad_request");
|
|
568
|
+
const upload = bucket.resumeMultipartUpload(session.key, session.uploadId);
|
|
569
|
+
const stored = await streamPart(upload, partNumber, request.body, declaredBytes);
|
|
570
|
+
if (!stored.ok) {
|
|
571
|
+
return stored.lengthMismatch ? fail(
|
|
572
|
+
400,
|
|
573
|
+
`Part ${partNumber} declared ${declaredBytes} bytes but its body did not carry them`,
|
|
574
|
+
"bad_request"
|
|
575
|
+
) : multipartFailure(stored.error);
|
|
576
|
+
}
|
|
577
|
+
return Response.json(
|
|
578
|
+
{ partNumber: stored.part.partNumber, etag: stored.part.etag },
|
|
579
|
+
{ headers: CORS_HEADERS }
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
async function multipartComplete(request, url, bucket, prefix, scope) {
|
|
583
|
+
const body = await readControlJson(request);
|
|
584
|
+
if (body instanceof Response) return body;
|
|
585
|
+
const session = resolveSession(url, prefix);
|
|
586
|
+
if (session instanceof Response) return session;
|
|
587
|
+
const raw = body.parts;
|
|
588
|
+
if (!Array.isArray(raw) || raw.length === 0) {
|
|
589
|
+
return fail(400, "parts must be a non-empty array", "bad_request");
|
|
590
|
+
}
|
|
591
|
+
if (raw.length > MAX_UPLOAD_PARTS) {
|
|
592
|
+
return fail(400, `An upload may have at most ${MAX_UPLOAD_PARTS} parts`, "bad_request");
|
|
593
|
+
}
|
|
594
|
+
const parts = [];
|
|
595
|
+
for (let index = 0; index < raw.length; index++) {
|
|
596
|
+
const { partNumber, etag } = raw[index] ?? {};
|
|
597
|
+
if (!Number.isSafeInteger(partNumber) || partNumber < 1) {
|
|
598
|
+
return fail(400, `parts[${index}].partNumber must be a positive integer`, "bad_request");
|
|
599
|
+
}
|
|
600
|
+
if (typeof etag !== "string" || !etag) {
|
|
601
|
+
return fail(400, `parts[${index}].etag must be the string uploadPart returned`, "bad_request");
|
|
602
|
+
}
|
|
603
|
+
parts.push({ partNumber, etag });
|
|
604
|
+
}
|
|
605
|
+
let object;
|
|
606
|
+
try {
|
|
607
|
+
object = await bucket.resumeMultipartUpload(session.key, session.uploadId).complete(parts);
|
|
608
|
+
} catch (error) {
|
|
609
|
+
return multipartFailure(error);
|
|
610
|
+
}
|
|
611
|
+
const tooBig = overCeiling(object.size);
|
|
612
|
+
if (tooBig) {
|
|
613
|
+
await bucket.delete(session.key).catch(() => void 0);
|
|
614
|
+
return tooBig;
|
|
615
|
+
}
|
|
616
|
+
const name = object.customMetadata?.originalName ?? session.key.split("/").pop() ?? "file";
|
|
617
|
+
return storedFileJson(session.key, name, url, scope);
|
|
618
|
+
}
|
|
619
|
+
async function multipartAbort(url, bucket, prefix) {
|
|
620
|
+
const session = resolveSession(url, prefix);
|
|
621
|
+
if (session instanceof Response) return session;
|
|
622
|
+
try {
|
|
623
|
+
await bucket.resumeMultipartUpload(session.key, session.uploadId).abort();
|
|
624
|
+
} catch (error) {
|
|
625
|
+
console.error("[files:multipart] abort:", error instanceof Error ? error.message : error);
|
|
626
|
+
return Response.json({ success: true, aborted: false }, { headers: CORS_HEADERS });
|
|
381
627
|
}
|
|
628
|
+
return Response.json({ success: true, aborted: true }, { headers: CORS_HEADERS });
|
|
382
629
|
}
|
|
383
630
|
async function handleList(bucket, prefix, url, scope) {
|
|
384
631
|
const userPrefix = url.searchParams.get("prefix") || "";
|
|
@@ -7687,6 +7934,9 @@ export {
|
|
|
7687
7934
|
GameRoom,
|
|
7688
7935
|
JobRoom,
|
|
7689
7936
|
MAX_APP_FILE_BYTES,
|
|
7937
|
+
MAX_DEPLOY_ASSET_FILE_BYTES,
|
|
7938
|
+
MAX_UPLOAD_PARTS,
|
|
7939
|
+
MAX_UPLOAD_REQUEST_BYTES,
|
|
7690
7940
|
MESSAGES_SCHEMA,
|
|
7691
7941
|
PresenceRoom,
|
|
7692
7942
|
REACTIONS_SCHEMA,
|
|
@@ -7705,6 +7955,7 @@ export {
|
|
|
7705
7955
|
SubscriptionAuthError,
|
|
7706
7956
|
SubscriptionRequiredError,
|
|
7707
7957
|
TEST_ACCOUNT_EMAIL_SUFFIX,
|
|
7958
|
+
UPLOAD_PART_BYTES,
|
|
7708
7959
|
USERS_COLUMNS,
|
|
7709
7960
|
VOTING_SCHEMAS,
|
|
7710
7961
|
WORKSPACE_SCHEMAS,
|
|
@@ -7747,6 +7998,7 @@ export {
|
|
|
7747
7998
|
deleteRecord,
|
|
7748
7999
|
enqueueJob,
|
|
7749
8000
|
executeQuery,
|
|
8001
|
+
formatBytes,
|
|
7750
8002
|
getAllUsers,
|
|
7751
8003
|
getChat,
|
|
7752
8004
|
getGlobalDOSchemas,
|
|
@@ -7770,6 +8022,7 @@ export {
|
|
|
7770
8022
|
handleYjsJoin,
|
|
7771
8023
|
handleYjsLeave,
|
|
7772
8024
|
isAutoProvision,
|
|
8025
|
+
isFilesVerbPath,
|
|
7773
8026
|
isOwner,
|
|
7774
8027
|
isTestAccountClaims,
|
|
7775
8028
|
isTestAccountEmail,
|