@softure-ai/files 0.2.0 → 0.4.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 +33 -0
- package/README.md +131 -4
- package/dist/client/index.d.ts +3 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +6 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/send-upload.d.ts +26 -0
- package/dist/client/send-upload.d.ts.map +1 -0
- package/dist/client/send-upload.js +66 -0
- package/dist/client/send-upload.js.map +1 -0
- package/dist/client/upload-queue.d.ts +52 -0
- package/dist/client/upload-queue.d.ts.map +1 -0
- package/dist/client/upload-queue.js +164 -0
- package/dist/client/upload-queue.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/jobs/index.d.ts +23 -1
- package/dist/jobs/index.d.ts.map +1 -1
- package/dist/jobs/index.js +46 -11
- package/dist/jobs/index.js.map +1 -1
- package/dist/mcp/index.d.ts +25 -1
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +31 -14
- package/dist/mcp/index.js.map +1 -1
- package/dist/messages/en.d.ts +5 -0
- package/dist/messages/en.d.ts.map +1 -1
- package/dist/messages/en.js +7 -1
- package/dist/messages/en.js.map +1 -1
- package/dist/messages/index.d.ts +5 -0
- package/dist/messages/index.d.ts.map +1 -1
- package/dist/next/index.d.ts +1 -0
- package/dist/next/index.d.ts.map +1 -1
- package/dist/next/index.js +3 -2
- package/dist/next/index.js.map +1 -1
- package/dist/next/stored-file-route.d.ts +25 -0
- package/dist/next/stored-file-route.d.ts.map +1 -0
- package/dist/next/stored-file-route.js +14 -0
- package/dist/next/stored-file-route.js.map +1 -0
- package/dist/server/index.d.ts +2 -2
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +2 -2
- package/dist/server/index.js.map +1 -1
- package/dist/server/prune.d.ts +4 -0
- package/dist/server/prune.d.ts.map +1 -1
- package/dist/server/prune.js +8 -2
- package/dist/server/prune.js.map +1 -1
- package/dist/server/serve.d.ts +15 -0
- package/dist/server/serve.d.ts.map +1 -1
- package/dist/server/serve.js +38 -14
- package/dist/server/serve.js.map +1 -1
- package/dist/server/upload-page.d.ts.map +1 -1
- package/dist/server/upload-page.js +16 -23
- package/dist/server/upload-page.js.map +1 -1
- package/module.json +1 -1
- package/package.json +8 -3
- package/src/client/index.ts +12 -0
- package/src/client/send-upload.ts +89 -0
- package/src/client/upload-queue.ts +220 -0
- package/src/index.ts +1 -1
- package/src/jobs/index.ts +66 -13
- package/src/mcp/index.ts +52 -15
- package/src/messages/en.ts +7 -1
- package/src/next/index.ts +9 -2
- package/src/next/stored-file-route.ts +43 -0
- package/src/server/index.ts +2 -2
- package/src/server/prune.ts +10 -1
- package/src/server/serve.ts +51 -14
- package/src/server/upload-page.ts +16 -23
package/dist/server/serve.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
// The
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// The answer for a stored file, framework-free, behind a public link (`serveFileLink`) or for its signed-in owner
|
|
2
|
+
// (`serveStoredFile`): `GET` and `HEAD` with `Content-Length`, `Accept-Ranges`, single byte ranges (`206`, `416`),
|
|
3
|
+
// `Content-Disposition`, and `Cache-Control: private, no-store` so no CDN keeps a copy. Every dead link or lookup
|
|
4
|
+
// (forged, expired, revoked, deleted, another owner's, missing from storage) gets the same `404`. A stored file is user
|
|
4
5
|
// content on the app's origin, so it is sent with `nosniff` and a sandboxing CSP: an HTML or SVG file never runs.
|
|
5
6
|
import { errorLogLabel } from "@softure-ai/core";
|
|
6
7
|
import { findLiveRow } from "./files.js";
|
|
@@ -14,8 +15,8 @@ const BASE_HEADERS = {
|
|
|
14
15
|
};
|
|
15
16
|
const ALLOWED_METHODS = "GET, HEAD";
|
|
16
17
|
const NOT_FOUND_BODY = "Not found\n";
|
|
17
|
-
/** The one answer for every link that does not lead to a live file. */
|
|
18
|
-
function notFound() {
|
|
18
|
+
/** The one answer for every link or lookup that does not lead to a live file. */
|
|
19
|
+
export function notFound() {
|
|
19
20
|
return new Response(NOT_FOUND_BODY, {
|
|
20
21
|
status: 404,
|
|
21
22
|
headers: {
|
|
@@ -27,12 +28,8 @@ function notFound() {
|
|
|
27
28
|
}
|
|
28
29
|
/** Answers a request for a public link (`GET` or `HEAD`). */
|
|
29
30
|
export async function serveFileLink(ctx, request) {
|
|
30
|
-
if (request
|
|
31
|
-
return
|
|
32
|
-
status: 405,
|
|
33
|
-
headers: { ...BASE_HEADERS, allow: ALLOWED_METHODS },
|
|
34
|
-
});
|
|
35
|
-
}
|
|
31
|
+
if (!isReadMethod(request))
|
|
32
|
+
return methodNotAllowed();
|
|
36
33
|
const link = verifyFileLink(ctx, new URL(request.url).pathname);
|
|
37
34
|
if (!link.ok) {
|
|
38
35
|
if (link.error !== "files.not_found")
|
|
@@ -42,12 +39,39 @@ export async function serveFileLink(ctx, request) {
|
|
|
42
39
|
const row = await findLiveRow(ctx, link.value.fileId);
|
|
43
40
|
if (row?.linkGeneration !== link.value.generation)
|
|
44
41
|
return notFound();
|
|
45
|
-
const
|
|
42
|
+
const disposition = getFilesOptions(ctx.config).links.disposition;
|
|
43
|
+
return answerStoredFile(ctx, request, row, { name: link.value.name, disposition });
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Answers a request (`GET` or `HEAD`) for a stored file on behalf of its signed-in owner: the answer of a public link
|
|
47
|
+
* (single byte ranges, `private, no-store`, `nosniff`, the sandboxing CSP) under the file's stored name, with no link
|
|
48
|
+
* to mint or verify. The caller has already checked the session; a file that is missing, expired, deleted or another
|
|
49
|
+
* owner's gets the same `404`.
|
|
50
|
+
*/
|
|
51
|
+
export async function serveStoredFile(ctx, fileId, request, options) {
|
|
52
|
+
if (!isReadMethod(request))
|
|
53
|
+
return methodNotAllowed();
|
|
54
|
+
const row = await findLiveRow(ctx, fileId, { ownerId: options.ownerId });
|
|
55
|
+
if (row === undefined)
|
|
56
|
+
return notFound();
|
|
57
|
+
return answerStoredFile(ctx, request, row, { name: row.originalName, disposition: options.disposition ?? "inline" });
|
|
58
|
+
}
|
|
59
|
+
function isReadMethod(request) {
|
|
60
|
+
return request.method === "GET" || request.method === "HEAD";
|
|
61
|
+
}
|
|
62
|
+
function methodNotAllowed() {
|
|
63
|
+
return new Response(null, {
|
|
64
|
+
status: 405,
|
|
65
|
+
headers: { ...BASE_HEADERS, allow: ALLOWED_METHODS },
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/** The bytes of a live row as the answer to a `GET` or `HEAD`, honouring a single byte range. */
|
|
69
|
+
async function answerStoredFile(ctx, request, row, options) {
|
|
46
70
|
const headers = {
|
|
47
71
|
...BASE_HEADERS,
|
|
48
72
|
"content-type": row.contentType,
|
|
49
73
|
"accept-ranges": "bytes",
|
|
50
|
-
"content-disposition": formatContentDisposition(options.
|
|
74
|
+
"content-disposition": formatContentDisposition(options.disposition, options.name),
|
|
51
75
|
"content-security-policy": "sandbox; default-src 'none'",
|
|
52
76
|
};
|
|
53
77
|
const range = parseRangeHeader(request.headers.get("range"), row.size);
|
|
@@ -65,7 +89,7 @@ export async function serveFileLink(ctx, request) {
|
|
|
65
89
|
return new Response(null, { status, headers });
|
|
66
90
|
let body;
|
|
67
91
|
try {
|
|
68
|
-
body = await
|
|
92
|
+
body = await getFilesOptions(ctx.config).storage.get(row.storageKey, range ?? undefined);
|
|
69
93
|
}
|
|
70
94
|
catch (error) {
|
|
71
95
|
console.error(`@softure-ai/files: reading file ${row.id} from storage failed: ${errorLogLabel(error)}`);
|
package/dist/server/serve.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serve.js","sourceRoot":"","sources":["../../src/server/serve.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"serve.js","sourceRoot":"","sources":["../../src/server/serve.ts"],"names":[],"mappings":"AAAA,kHAAkH;AAClH,mHAAmH;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GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;QAC5B,IAAI,MAAM,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC;YAAE,OAAO,eAAe,CAAC;QACvD,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,GAAG,MAAM,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,IAAI,GAAG,CAAC,EAAE,CAAC;IAC9D,CAAC;IACD,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAC5B,IAAI,IAAI,KAAK,EAAE,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK;QAAE,OAAO,IAAI,CAAC;IACrD,IAAI,KAAK,IAAI,IAAI;QAAE,OAAO,eAAe,CAAC;IAC1C,OAAO;QACL,KAAK;QACL,GAAG,EAAE,IAAI,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,GAAG,CAAC,CAAC;KAC/D,CAAC;AACJ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upload-page.d.ts","sourceRoot":"","sources":["../../src/server/upload-page.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"upload-page.d.ts","sourceRoot":"","sources":["../../src/server/upload-page.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAEnD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAiCjD,iFAAiF;AACjF,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,IAAI,CAAC,YAAY,EAAE,QAAQ,CAAC,EAAE,MAAM,EAAE,YAAY,GAAG,QAAQ,CA0ClG"}
|
|
@@ -1,45 +1,38 @@
|
|
|
1
1
|
// The browser fallback for an upload session: a small page on the upload URL itself, for a person on a phone or an
|
|
2
2
|
// assistant that cannot run `curl`. It sends the missing chunks of the chosen file one by one with `fetch`, so a
|
|
3
|
-
// broken connection resumes where it stopped.
|
|
4
|
-
// style, allowed by a per-response nonce. The copy
|
|
3
|
+
// broken connection resumes where it stopped. The loop is `sendUpload` of `@softure-ai/files/client`, inlined. No
|
|
4
|
+
// framework and no external asset: one inline script and one inline style, allowed by a per-response nonce. The copy
|
|
5
|
+
// comes from the module dictionary (`uploadPage`).
|
|
5
6
|
import { randomBytes } from "node:crypto";
|
|
6
7
|
import { selectMessages } from "@softure-ai/core";
|
|
8
|
+
import { sendUpload } from "../client/send-upload.js";
|
|
7
9
|
import { getFilesModule } from "./options.js";
|
|
8
10
|
const STYLE = `body{font:16px/1.5 system-ui,sans-serif;max-width:32rem;margin:2rem auto;padding:0 1rem}
|
|
9
11
|
button,input{font:inherit}button{margin-top:1rem;padding:.5rem 1rem}progress{width:100%;margin-top:1rem}
|
|
10
12
|
p[role=status]{min-height:1.5em}`;
|
|
11
|
-
|
|
13
|
+
// The page's script: the client's own `sendUpload`, inlined from its source, then the few lines that wire it to the
|
|
14
|
+
// form. `sendUpload` references nothing outside its body (see `client/send-upload.ts`).
|
|
15
|
+
const SCRIPT = `${sendUpload.toString()}
|
|
16
|
+
const config = JSON.parse(document.getElementById("upload-config").textContent);
|
|
12
17
|
const input = document.getElementById("file");
|
|
13
18
|
const button = document.getElementById("start");
|
|
14
19
|
const bar = document.getElementById("bar");
|
|
15
20
|
const status = document.getElementById("status");
|
|
16
21
|
const megabytes = (bytes) => (bytes / 1048576).toFixed(1) + " MB";
|
|
17
22
|
function show(text) { status.textContent = text; }
|
|
23
|
+
function showProgress(progress) {
|
|
24
|
+
bar.value = progress.totalBytes === 0 ? 1 : progress.sentBytes / progress.totalBytes;
|
|
25
|
+
show(config.text.progress.replace("{done}", megabytes(progress.sentBytes)).replace("{total}", megabytes(progress.totalBytes)));
|
|
26
|
+
}
|
|
18
27
|
async function upload() {
|
|
19
28
|
const file = input.files[0];
|
|
20
29
|
if (!file) return;
|
|
21
30
|
if (file.size !== config.size) { show(config.text.wrongSize); return; }
|
|
22
31
|
button.disabled = true;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
let done = state.receivedBytes;
|
|
28
|
-
for (const index of state.missingChunks) {
|
|
29
|
-
const start = index * config.chunkSize;
|
|
30
|
-
const end = Math.min(start + config.chunkSize, config.size) - 1;
|
|
31
|
-
const sent = await fetch(location.href, { method: "PUT", headers: { "content-range": "bytes " + start + "-" + end + "/" + config.size }, body: file.slice(start, end + 1) });
|
|
32
|
-
if (!sent.ok) throw new Error("status " + sent.status);
|
|
33
|
-
done += end - start + 1;
|
|
34
|
-
bar.value = done / config.size;
|
|
35
|
-
show(config.text.progress.replace("{done}", megabytes(done)).replace("{total}", megabytes(config.size)));
|
|
36
|
-
}
|
|
37
|
-
bar.value = 1;
|
|
38
|
-
show(config.text.done);
|
|
39
|
-
} catch {
|
|
40
|
-
show(config.text.failed);
|
|
41
|
-
button.disabled = false;
|
|
42
|
-
}
|
|
32
|
+
const sent = await sendUpload(file, { uploadUrl: location.href, onProgress: showProgress });
|
|
33
|
+
if (sent.ok) { bar.value = 1; show(config.text.done); return; }
|
|
34
|
+
show(sent.error === "files.upload_expired" ? config.text.expired : config.text.failed);
|
|
35
|
+
button.disabled = sent.error === "files.upload_expired";
|
|
43
36
|
}
|
|
44
37
|
button.addEventListener("click", upload);`;
|
|
45
38
|
/** The page for a session; a finalized or complete one shows that it is done. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upload-page.js","sourceRoot":"","sources":["../../src/server/upload-page.ts"],"names":[],"mappings":"AAAA,mHAAmH;AACnH,iHAAiH;AACjH,
|
|
1
|
+
{"version":3,"file":"upload-page.js","sourceRoot":"","sources":["../../src/server/upload-page.ts"],"names":[],"mappings":"AAAA,mHAAmH;AACnH,iHAAiH;AACjH,kHAAkH;AAClH,qHAAqH;AACrH,mDAAmD;AACnD,OAAO,EAAE,WA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"SOFTURE AI files module: an app's stored files (content hash, size, type, owner, kind, expiry, a per-owner quota, opt-in dedupe) on a storage driver (local disk with atomic writes, S3-compatible with SigV4 and multipart uploads, in memory for tests), streamed and never buffered, public links that expire (HMAC-signed, checked before any database read, revocable per file, a rotating keyring) served by a Next.js route with byte ranges and one 404 for every dead link, resumable chunked uploads through signed URLs (with a browser page and MCP upload tools), a prune job, a privacy contributor and a health check.",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "SOFTURE AI files module: an app's stored files (content hash, size, type, owner, kind, expiry, a per-owner quota, opt-in dedupe) on a storage driver (local disk with atomic writes, S3-compatible with SigV4 and multipart uploads, in memory for tests), streamed and never buffered, public links that expire (HMAC-signed, checked before any database read, revocable per file, a rotating keyring) served by a Next.js route with byte ranges and one 404 for every dead link, the same answer for the signed-in owner by id without a link, resumable chunked uploads through signed URLs (with a browser client, an upload list, a browser page and MCP upload tools), a prune job, a privacy contributor and a health check.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"sideEffects": false,
|
|
@@ -32,6 +32,11 @@
|
|
|
32
32
|
"types": "./dist/index.d.ts",
|
|
33
33
|
"default": "./dist/index.js"
|
|
34
34
|
},
|
|
35
|
+
"./client": {
|
|
36
|
+
"@softure-ai/source": "./src/client/index.ts",
|
|
37
|
+
"types": "./dist/client/index.d.ts",
|
|
38
|
+
"default": "./dist/client/index.js"
|
|
39
|
+
},
|
|
35
40
|
"./server": {
|
|
36
41
|
"@softure-ai/source": "./src/server/index.ts",
|
|
37
42
|
"types": "./dist/server/index.d.ts",
|
|
@@ -65,7 +70,7 @@
|
|
|
65
70
|
"@softure-ai/core": "^0.2.7",
|
|
66
71
|
"@softure-ai/db": "^0.1.14",
|
|
67
72
|
"@softure-ai/security": "^0.2.5",
|
|
68
|
-
"zod": "^4.6.
|
|
73
|
+
"zod": "^4.6.7"
|
|
69
74
|
},
|
|
70
75
|
"peerDependencies": {
|
|
71
76
|
"@modelcontextprotocol/server": "^2.0.0",
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// The browser side of upload sessions: `sendUpload` sends a `File` to a session's signed URL in chunks, resumably,
|
|
2
|
+
// and `createUploadQueue` keeps a list of uploads for an upload screen (theme-notion's `UploadItem` takes its items
|
|
3
|
+
// as they are). No Node built-ins and no server code: safe to import from a client component.
|
|
4
|
+
export { sendUpload, type SendUploadErrorCode, type SendUploadOptions, type SendUploadResult, type UploadProgress } from "./send-upload.js";
|
|
5
|
+
export {
|
|
6
|
+
createUploadQueue,
|
|
7
|
+
type UploadQueue,
|
|
8
|
+
type UploadQueueItem,
|
|
9
|
+
type UploadQueueOptions,
|
|
10
|
+
type UploadQueueSession,
|
|
11
|
+
type UploadQueueState,
|
|
12
|
+
} from "./upload-queue.js";
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// Sends a browser `File` (any `Blob`) to an upload session: reads what arrived from the session's `HEAD`, then `PUT`s
|
|
2
|
+
// only the missing chunks, in order, with `Content-Range`. The session decides the chunk size; the route refuses any
|
|
3
|
+
// other alignment. Every expected failure is a value, never a throw.
|
|
4
|
+
//
|
|
5
|
+
// `sendUpload` is self-contained on purpose: it references nothing outside its own body, so the package's upload
|
|
6
|
+
// page inlines its source (`sendUpload.toString()`) and runs the very same loop without a bundler or a second copy.
|
|
7
|
+
// Keep every helper inside the function and import types only.
|
|
8
|
+
import type { Err, Ok } from "@softure-ai/core";
|
|
9
|
+
|
|
10
|
+
/** Why a send stopped: the session is gone, the route refused it, the network failed, or the caller aborted. */
|
|
11
|
+
export type SendUploadErrorCode = "files.upload_expired" | "files.upload_rejected" | "files.network" | "files.upload_aborted";
|
|
12
|
+
|
|
13
|
+
/** Bytes the session holds so far out of the file's size. */
|
|
14
|
+
export interface UploadProgress {
|
|
15
|
+
readonly sentBytes: number;
|
|
16
|
+
readonly totalBytes: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface SendUploadOptions {
|
|
20
|
+
/** The session's signed upload URL, as `createUpload` returned it. */
|
|
21
|
+
readonly uploadUrl: string;
|
|
22
|
+
/** Aborts between chunks and the chunk in flight; the send answers `files.upload_aborted`. */
|
|
23
|
+
readonly signal?: AbortSignal;
|
|
24
|
+
/** Called once with what already arrived, then after every chunk. */
|
|
25
|
+
readonly onProgress?: (progress: UploadProgress) => void;
|
|
26
|
+
/** The `fetch` to use; the global one by default. */
|
|
27
|
+
readonly fetch?: (url: string, init: RequestInit) => Promise<Response>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type SendUploadResult = Ok<UploadProgress> | Err<SendUploadErrorCode>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Sends the chunks of `file` the session does not hold yet. Resuming is calling it again: a chunk that arrived is
|
|
34
|
+
* never sent twice. `404` answers `files.upload_expired`, another `4xx` (or a file whose size is not the session's)
|
|
35
|
+
* `files.upload_rejected`, a failed request, `429` or `5xx` `files.network`.
|
|
36
|
+
*/
|
|
37
|
+
export async function sendUpload(file: Blob, options: SendUploadOptions): Promise<SendUploadResult> {
|
|
38
|
+
const { uploadUrl, signal, onProgress } = options;
|
|
39
|
+
const request = options.fetch ?? ((url: string, init: RequestInit) => fetch(url, init));
|
|
40
|
+
const fail = (error: SendUploadErrorCode): Err<SendUploadErrorCode> => ({ ok: false, error });
|
|
41
|
+
const failForStatus = (status: number): Err<SendUploadErrorCode> => {
|
|
42
|
+
if (status === 404) return fail("files.upload_expired");
|
|
43
|
+
return fail(status === 429 || status >= 500 ? "files.network" : "files.upload_rejected");
|
|
44
|
+
};
|
|
45
|
+
const isAborted = (): boolean => signal?.aborted === true;
|
|
46
|
+
const call = async (init: RequestInit): Promise<Response | SendUploadErrorCode> => {
|
|
47
|
+
if (isAborted()) return "files.upload_aborted";
|
|
48
|
+
try {
|
|
49
|
+
return await request(uploadUrl, { ...init, signal: signal ?? null });
|
|
50
|
+
} catch {
|
|
51
|
+
// A thrown fetch is the network or the abort; both are expected and answered as a code.
|
|
52
|
+
return isAborted() ? "files.upload_aborted" : "files.network";
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const head = await call({ method: "HEAD" });
|
|
57
|
+
if (typeof head === "string") return fail(head);
|
|
58
|
+
if (!head.ok) return failForStatus(head.status);
|
|
59
|
+
const totalBytes = Number(head.headers.get("upload-size"));
|
|
60
|
+
const chunkSize = Number(head.headers.get("upload-chunk-size"));
|
|
61
|
+
if (!Number.isSafeInteger(totalBytes) || !Number.isSafeInteger(chunkSize) || chunkSize <= 0 || totalBytes !== file.size) {
|
|
62
|
+
return fail("files.upload_rejected");
|
|
63
|
+
}
|
|
64
|
+
const received: { start: number; end: number }[] = [];
|
|
65
|
+
for (const part of (head.headers.get("upload-received") ?? "").split(",")) {
|
|
66
|
+
const match = /^(\d+)-(\d+)$/.exec(part.trim());
|
|
67
|
+
if (match !== null) received.push({ start: Number(match[1]), end: Number(match[2]) });
|
|
68
|
+
}
|
|
69
|
+
const missing: { start: number; end: number }[] = [];
|
|
70
|
+
for (let start = 0; start < totalBytes; start += chunkSize) {
|
|
71
|
+
const end = Math.min(start + chunkSize, totalBytes) - 1;
|
|
72
|
+
if (!received.some((range) => range.start <= start && range.end >= end)) missing.push({ start, end });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let sentBytes = totalBytes - missing.reduce((sum, chunk) => sum + chunk.end - chunk.start + 1, 0);
|
|
76
|
+
onProgress?.({ sentBytes, totalBytes });
|
|
77
|
+
for (const chunk of missing) {
|
|
78
|
+
const answer = await call({
|
|
79
|
+
method: "PUT",
|
|
80
|
+
headers: { "content-range": `bytes ${String(chunk.start)}-${String(chunk.end)}/${String(totalBytes)}` },
|
|
81
|
+
body: file.slice(chunk.start, chunk.end + 1),
|
|
82
|
+
});
|
|
83
|
+
if (typeof answer === "string") return fail(answer);
|
|
84
|
+
if (!answer.ok) return failForStatus(answer.status);
|
|
85
|
+
sentBytes += chunk.end - chunk.start + 1;
|
|
86
|
+
onProgress?.({ sentBytes, totalBytes });
|
|
87
|
+
}
|
|
88
|
+
return { ok: true, value: { sentBytes, totalBytes } };
|
|
89
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// A list of uploads for an upload screen, without a framework: files go in, each is opened as a session by the app,
|
|
2
|
+
// sent with `sendUpload` and finalized by the app, and the list says where each one stands. Its items fit
|
|
3
|
+
// theme-notion's `UploadItem` as they are (`UploadFileItem`: id, name, size, state, progress 0-100, error).
|
|
4
|
+
//
|
|
5
|
+
// React reads it with `useSyncExternalStore(queue.subscribe, queue.getItems)`: `getItems` answers the same array until
|
|
6
|
+
// something changes.
|
|
7
|
+
import type { ErrorCode, Result } from "@softure-ai/core";
|
|
8
|
+
import { filesMessages, getFilesErrorMessage } from "../messages/index.js";
|
|
9
|
+
import { sendUpload, type SendUploadOptions, type UploadProgress } from "./send-upload.js";
|
|
10
|
+
|
|
11
|
+
export type UploadQueueState = "queued" | "uploading" | "paused" | "done" | "failed";
|
|
12
|
+
|
|
13
|
+
/** One upload of the list. */
|
|
14
|
+
export interface UploadQueueItem {
|
|
15
|
+
readonly id: string;
|
|
16
|
+
readonly name: string;
|
|
17
|
+
/** In bytes. */
|
|
18
|
+
readonly size: number;
|
|
19
|
+
readonly state: UploadQueueState;
|
|
20
|
+
/** 0-100, what the session holds; 100 only once finalized. */
|
|
21
|
+
readonly progress: number;
|
|
22
|
+
/** The copy for `errorCode`, on a failed item. */
|
|
23
|
+
readonly error?: string;
|
|
24
|
+
/** Why it failed: a `files.*` code of the client or whatever code the app's `open` or `finalize` answered. */
|
|
25
|
+
readonly errorCode?: ErrorCode;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** What the app's `open` answers: at least the session's upload URL (`createUpload`'s answer fits). */
|
|
29
|
+
export interface UploadQueueSession {
|
|
30
|
+
readonly uploadUrl: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface UploadQueueOptions<TSession extends UploadQueueSession> {
|
|
34
|
+
/** Opens a session for a file, usually a server action around `createUpload`. */
|
|
35
|
+
readonly open: (file: File) => Promise<Result<TSession>>;
|
|
36
|
+
/** Turns a sent session into a stored file, usually a server action around `finalizeUpload`. */
|
|
37
|
+
readonly finalize: (session: TSession, file: File) => Promise<Result<unknown>>;
|
|
38
|
+
/** Uploads sent at the same time; 1 by default. */
|
|
39
|
+
readonly concurrency?: number;
|
|
40
|
+
/** The copy for a failure code; the built-in English `files` messages by default. */
|
|
41
|
+
readonly describeError?: (code: ErrorCode) => string;
|
|
42
|
+
/** The `fetch` the sender uses; the global one by default. */
|
|
43
|
+
readonly fetch?: SendUploadOptions["fetch"];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface UploadQueue {
|
|
47
|
+
/** Queues files (a `FileList` works) and answers their item ids. */
|
|
48
|
+
add(files: Iterable<File> | ArrayLike<File>): string[];
|
|
49
|
+
/** Queues a failed upload again; it keeps its session unless the session expired. */
|
|
50
|
+
retry(id: string): void;
|
|
51
|
+
/** Stops a queued or running upload; what arrived stays in the session. */
|
|
52
|
+
pause(id: string): void;
|
|
53
|
+
/** Queues a paused upload again; it sends only what is missing. */
|
|
54
|
+
resume(id: string): void;
|
|
55
|
+
/** Stops an upload and drops it from the list. An unfinished session expires and `files.prune` removes it. */
|
|
56
|
+
remove(id: string): void;
|
|
57
|
+
/** The list, in the order files were added; the same array until something changes. */
|
|
58
|
+
getItems(): readonly UploadQueueItem[];
|
|
59
|
+
/** Calls `listener` after every change; answers the function that stops it. */
|
|
60
|
+
subscribe(listener: () => void): () => void;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface Entry<TSession> {
|
|
64
|
+
item: UploadQueueItem;
|
|
65
|
+
readonly file: File;
|
|
66
|
+
session: TSession | null;
|
|
67
|
+
// Bumped by pause and remove: a run that settles under an older number changes nothing.
|
|
68
|
+
run: number;
|
|
69
|
+
controller: AbortController | null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// A session the server no longer knows: a retry opens a new one.
|
|
73
|
+
const SESSION_GONE_CODES: ReadonlySet<string> = new Set(["files.upload_expired", "files.not_found"]);
|
|
74
|
+
|
|
75
|
+
/** Creates an upload list; `open` and `finalize` are the app's, the sending is the package's. */
|
|
76
|
+
export function createUploadQueue<TSession extends UploadQueueSession>(options: UploadQueueOptions<TSession>): UploadQueue {
|
|
77
|
+
const concurrency = Math.max(1, Math.floor(options.concurrency ?? 1));
|
|
78
|
+
const describeError = options.describeError ?? ((code: ErrorCode) => getFilesErrorMessage(filesMessages.en, code));
|
|
79
|
+
const entries = new Map<string, Entry<TSession>>();
|
|
80
|
+
const listeners = new Set<() => void>();
|
|
81
|
+
let items: readonly UploadQueueItem[] = [];
|
|
82
|
+
let nextId = 1;
|
|
83
|
+
|
|
84
|
+
function publish(): void {
|
|
85
|
+
items = [...entries.values()].map((entry) => entry.item);
|
|
86
|
+
for (const listener of [...listeners]) listener();
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function setItem(entry: Entry<TSession>, state: UploadQueueState, progress = entry.item.progress, errorCode?: ErrorCode): void {
|
|
90
|
+
const { id, name, size } = entry.item;
|
|
91
|
+
entry.item = { id, name, size, state, progress, ...(errorCode === undefined ? {} : { errorCode, error: describeError(errorCode) }) };
|
|
92
|
+
publish();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function fail(entry: Entry<TSession>, code: ErrorCode): void {
|
|
96
|
+
if (SESSION_GONE_CODES.has(code)) entry.session = null;
|
|
97
|
+
setItem(entry, "failed", entry.item.progress, code);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function stop(entry: Entry<TSession>): void {
|
|
101
|
+
entry.run += 1;
|
|
102
|
+
entry.controller?.abort();
|
|
103
|
+
entry.controller = null;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function pump(): void {
|
|
107
|
+
for (const entry of entries.values()) {
|
|
108
|
+
if (countRunning() >= concurrency) return;
|
|
109
|
+
if (entry.item.state === "queued") void start(entry);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function countRunning(): number {
|
|
114
|
+
let running = 0;
|
|
115
|
+
for (const entry of entries.values()) if (entry.item.state === "uploading") running += 1;
|
|
116
|
+
return running;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function start(entry: Entry<TSession>): Promise<void> {
|
|
120
|
+
entry.run += 1;
|
|
121
|
+
const run = entry.run;
|
|
122
|
+
const controller = new AbortController();
|
|
123
|
+
entry.controller = controller;
|
|
124
|
+
const isCurrent = (): boolean => entry.run === run && entries.get(entry.item.id) === entry;
|
|
125
|
+
setItem(entry, "uploading");
|
|
126
|
+
try {
|
|
127
|
+
let session = entry.session;
|
|
128
|
+
if (session === null) {
|
|
129
|
+
const opened = await options.open(entry.file);
|
|
130
|
+
// A session opened while the item was paused is kept, so the resume does not open a second one.
|
|
131
|
+
if (opened.ok) entry.session = opened.value;
|
|
132
|
+
if (!isCurrent()) return;
|
|
133
|
+
if (!opened.ok) return fail(entry, opened.error);
|
|
134
|
+
session = opened.value;
|
|
135
|
+
}
|
|
136
|
+
const sent = await sendUpload(entry.file, {
|
|
137
|
+
uploadUrl: session.uploadUrl,
|
|
138
|
+
signal: controller.signal,
|
|
139
|
+
onProgress: (progress) => {
|
|
140
|
+
if (isCurrent()) setItem(entry, "uploading", toPercent(progress));
|
|
141
|
+
},
|
|
142
|
+
...(options.fetch === undefined ? {} : { fetch: options.fetch }),
|
|
143
|
+
});
|
|
144
|
+
if (!isCurrent()) return;
|
|
145
|
+
if (!sent.ok) return fail(entry, sent.error);
|
|
146
|
+
const finalized = await options.finalize(session, entry.file);
|
|
147
|
+
if (!isCurrent()) return;
|
|
148
|
+
if (!finalized.ok) return fail(entry, finalized.error);
|
|
149
|
+
setItem(entry, "done", 100);
|
|
150
|
+
} catch (error) {
|
|
151
|
+
// The app's `open` or `finalize` threw, e.g. a server action whose request failed: the item fails with a
|
|
152
|
+
// retryable code, and the cause goes to the console for the developer.
|
|
153
|
+
console.error(`@softure-ai/files/client: an upload of "${entry.item.name}" stopped on a thrown open or finalize`, error);
|
|
154
|
+
if (isCurrent()) fail(entry, "files.network");
|
|
155
|
+
} finally {
|
|
156
|
+
if (entry.controller === controller) entry.controller = null;
|
|
157
|
+
pump();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function find(id: string, states: readonly UploadQueueState[]): Entry<TSession> | null {
|
|
162
|
+
const entry = entries.get(id);
|
|
163
|
+
return entry !== undefined && states.includes(entry.item.state) ? entry : null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
add(files) {
|
|
168
|
+
const ids: string[] = [];
|
|
169
|
+
for (const file of Array.from(files)) {
|
|
170
|
+
const id = `upload-${String(nextId)}`;
|
|
171
|
+
nextId += 1;
|
|
172
|
+
entries.set(id, { item: { id, name: file.name, size: file.size, state: "queued", progress: 0 }, file, session: null, run: 0, controller: null });
|
|
173
|
+
ids.push(id);
|
|
174
|
+
}
|
|
175
|
+
publish();
|
|
176
|
+
pump();
|
|
177
|
+
return ids;
|
|
178
|
+
},
|
|
179
|
+
retry(id) {
|
|
180
|
+
const entry = find(id, ["failed"]);
|
|
181
|
+
if (entry === null) return;
|
|
182
|
+
setItem(entry, "queued");
|
|
183
|
+
pump();
|
|
184
|
+
},
|
|
185
|
+
pause(id) {
|
|
186
|
+
const entry = find(id, ["queued", "uploading"]);
|
|
187
|
+
if (entry === null) return;
|
|
188
|
+
stop(entry);
|
|
189
|
+
setItem(entry, "paused");
|
|
190
|
+
pump();
|
|
191
|
+
},
|
|
192
|
+
resume(id) {
|
|
193
|
+
const entry = find(id, ["paused"]);
|
|
194
|
+
if (entry === null) return;
|
|
195
|
+
setItem(entry, "queued");
|
|
196
|
+
pump();
|
|
197
|
+
},
|
|
198
|
+
remove(id) {
|
|
199
|
+
const entry = entries.get(id);
|
|
200
|
+
if (entry === undefined) return;
|
|
201
|
+
stop(entry);
|
|
202
|
+
entries.delete(id);
|
|
203
|
+
publish();
|
|
204
|
+
pump();
|
|
205
|
+
},
|
|
206
|
+
getItems: () => items,
|
|
207
|
+
subscribe(listener) {
|
|
208
|
+
listeners.add(listener);
|
|
209
|
+
return () => {
|
|
210
|
+
listeners.delete(listener);
|
|
211
|
+
};
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function toPercent(progress: UploadProgress): number {
|
|
217
|
+
// 100 is kept for a finalized upload: a sent but unfinalized one shows 99 at most.
|
|
218
|
+
if (progress.totalBytes === 0) return 99;
|
|
219
|
+
return Math.min(99, Math.floor((progress.sentBytes / progress.totalBytes) * 100));
|
|
220
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -26,7 +26,7 @@ export const FILES_RATE_LIMIT_BUCKETS = {
|
|
|
26
26
|
export const files = defineModule({
|
|
27
27
|
manifest: {
|
|
28
28
|
id: MODULE_ID,
|
|
29
|
-
version: "0.
|
|
29
|
+
version: "0.4.0",
|
|
30
30
|
dependsOn: { jobs: "^0.1.6?", security: "^0.2.5?" },
|
|
31
31
|
dbSchema: "files",
|
|
32
32
|
tables: ["files", "uploads"],
|