@weft-framework/server 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +4 -2
- package/dist/account.d.ts +124 -0
- package/dist/account.js +344 -0
- package/dist/action.d.ts +43 -0
- package/dist/action.js +135 -0
- package/dist/admin.d.ts +99 -0
- package/dist/admin.js +431 -0
- package/dist/api.d.ts +21 -0
- package/dist/api.js +72 -0
- package/dist/app.d.ts +202 -0
- package/dist/app.js +3692 -0
- package/dist/assets.d.ts +4 -0
- package/dist/assets.js +91 -0
- package/dist/auth.d.ts +150 -0
- package/dist/auth.js +215 -0
- package/dist/backup.d.ts +46 -0
- package/dist/backup.js +345 -0
- package/dist/build.d.ts +33 -0
- package/dist/build.js +274 -0
- package/dist/bun.d.ts +30 -0
- package/dist/bun.js +53 -0
- package/dist/calendar.d.ts +161 -0
- package/dist/calendar.js +690 -0
- package/dist/compiler.d.ts +13 -0
- package/dist/compiler.js +58 -0
- package/dist/compress.d.ts +6 -0
- package/dist/compress.js +78 -0
- package/dist/config.d.ts +26 -0
- package/dist/config.js +317 -0
- package/dist/context.d.ts +9 -0
- package/dist/context.js +1 -0
- package/dist/contract.d.ts +18 -0
- package/dist/contract.js +171 -0
- package/dist/datamap.d.ts +24 -0
- package/dist/datamap.js +365 -0
- package/dist/db-backup.d.ts +27 -0
- package/dist/db-backup.js +115 -0
- package/dist/dev-events.d.ts +14 -0
- package/dist/dev-events.js +135 -0
- package/dist/dev.d.ts +1 -0
- package/dist/dev.js +240 -0
- package/dist/devtools-client.d.ts +1 -0
- package/dist/devtools-client.js +362 -0
- package/dist/devtools.d.ts +87 -0
- package/dist/devtools.js +68 -0
- package/dist/edge.d.ts +3 -0
- package/dist/edge.js +7 -0
- package/dist/email-signin.d.ts +92 -0
- package/dist/email-signin.js +259 -0
- package/dist/env.d.ts +1 -0
- package/dist/env.js +15 -0
- package/dist/explain.d.ts +12 -0
- package/dist/explain.js +217 -0
- package/dist/files.d.ts +6 -0
- package/dist/files.js +170 -0
- package/dist/grants.d.ts +69 -0
- package/dist/grants.js +206 -0
- package/dist/image/bitmap.d.ts +11 -0
- package/dist/image/bitmap.js +172 -0
- package/dist/image/jpeg.d.ts +10 -0
- package/dist/image/jpeg.js +697 -0
- package/dist/image/png.d.ts +8 -0
- package/dist/image/png.js +282 -0
- package/dist/image/transform.d.ts +20 -0
- package/dist/image/transform.js +52 -0
- package/dist/image/worker.d.ts +1 -0
- package/dist/image/worker.js +11 -0
- package/dist/images.d.ts +39 -0
- package/dist/images.js +283 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.js +31 -0
- package/dist/inspect-hub.d.ts +42 -0
- package/dist/inspect-hub.js +14 -0
- package/dist/inspector.d.ts +51 -0
- package/dist/inspector.js +193 -0
- package/dist/job-admin.d.ts +55 -0
- package/dist/job-admin.js +89 -0
- package/dist/jobs.d.ts +222 -0
- package/dist/jobs.js +789 -0
- package/dist/live.d.ts +72 -0
- package/dist/live.js +481 -0
- package/dist/load-config.d.ts +2 -0
- package/dist/load-config.js +41 -0
- package/dist/log.d.ts +20 -0
- package/dist/log.js +143 -0
- package/dist/mail.d.ts +189 -0
- package/dist/mail.js +999 -0
- package/dist/media.d.ts +129 -0
- package/dist/media.js +470 -0
- package/dist/meta.d.ts +54 -0
- package/dist/meta.js +146 -0
- package/dist/middleware.d.ts +20 -0
- package/dist/middleware.js +101 -0
- package/dist/migrate.d.ts +12 -0
- package/dist/migrate.js +88 -0
- package/dist/node.d.ts +26 -0
- package/dist/node.js +96 -0
- package/dist/notice.d.ts +21 -0
- package/dist/notice.js +48 -0
- package/dist/npm.d.ts +16 -0
- package/dist/npm.js +347 -0
- package/dist/oauth.d.ts +76 -0
- package/dist/oauth.js +408 -0
- package/dist/otel.d.ts +57 -0
- package/dist/otel.js +293 -0
- package/dist/passkey.d.ts +99 -0
- package/dist/passkey.js +572 -0
- package/dist/password.d.ts +8 -0
- package/dist/password.js +65 -0
- package/dist/pdf.d.ts +46 -0
- package/dist/pdf.js +332 -0
- package/dist/personal-data.d.ts +22 -0
- package/dist/personal-data.js +148 -0
- package/dist/pointers.d.ts +9 -0
- package/dist/pointers.js +148 -0
- package/dist/postcss.d.ts +3 -0
- package/dist/postcss.js +89 -0
- package/dist/presence.d.ts +27 -0
- package/dist/presence.js +161 -0
- package/dist/preview.d.ts +25 -0
- package/dist/preview.js +353 -0
- package/dist/production.d.ts +16 -0
- package/dist/production.js +119 -0
- package/dist/project.d.ts +23 -0
- package/dist/project.js +536 -0
- package/dist/regions.d.ts +47 -0
- package/dist/regions.js +511 -0
- package/dist/replay.d.ts +44 -0
- package/dist/replay.js +259 -0
- package/dist/router.d.ts +29 -0
- package/dist/router.js +146 -0
- package/dist/rules-diff.d.ts +18 -0
- package/dist/rules-diff.js +165 -0
- package/dist/rules-test.d.ts +17 -0
- package/dist/rules-test.js +108 -0
- package/dist/saml.d.ts +160 -0
- package/dist/saml.js +959 -0
- package/dist/seal.d.ts +35 -0
- package/dist/seal.js +107 -0
- package/dist/secret.d.ts +14 -0
- package/dist/secret.js +110 -0
- package/dist/security.d.ts +53 -0
- package/dist/security.js +250 -0
- package/dist/seed.d.ts +16 -0
- package/dist/seed.js +94 -0
- package/dist/seo.d.ts +42 -0
- package/dist/seo.js +175 -0
- package/dist/settings.d.ts +79 -0
- package/dist/settings.js +212 -0
- package/dist/shared.d.ts +15 -0
- package/dist/shared.js +283 -0
- package/dist/shell.d.ts +12 -0
- package/dist/shell.js +118 -0
- package/dist/storage.d.ts +13 -0
- package/dist/storage.js +55 -0
- package/dist/support.d.ts +83 -0
- package/dist/support.js +236 -0
- package/dist/sync.d.ts +35 -0
- package/dist/sync.js +167 -0
- package/dist/tailwind.d.ts +14 -0
- package/dist/tailwind.js +117 -0
- package/dist/two-factor.d.ts +103 -0
- package/dist/two-factor.js +435 -0
- package/dist/uploads.d.ts +185 -0
- package/dist/uploads.js +991 -0
- package/dist/webhooks.d.ts +169 -0
- package/dist/webhooks.js +758 -0
- package/dist/webpush.d.ts +97 -0
- package/dist/webpush.js +372 -0
- package/dist/wizard.d.ts +141 -0
- package/dist/wizard.js +410 -0
- package/dist/workflows.d.ts +134 -0
- package/dist/workflows.js +511 -0
- package/package.json +186 -4
package/dist/uploads.js
ADDED
|
@@ -0,0 +1,991 @@
|
|
|
1
|
+
import { holdFile, onFileRelease, sweepHeldFiles } from "@weft-framework/data";
|
|
2
|
+
import { ActionError } from "./action.js";
|
|
3
|
+
import { authState } from "./auth.js";
|
|
4
|
+
import { isSecret } from "./secret.js";
|
|
5
|
+
import { MIN_SECRET_LENGTH } from "./security.js";
|
|
6
|
+
const utf8 = new TextEncoder();
|
|
7
|
+
export const MB = 1024 * 1024;
|
|
8
|
+
export const GB = 1024 * MB;
|
|
9
|
+
export class MemoryFileStore {
|
|
10
|
+
#rows = new Map();
|
|
11
|
+
put(r) {
|
|
12
|
+
this.#rows.set(r.id, { ...r });
|
|
13
|
+
}
|
|
14
|
+
get(id) {
|
|
15
|
+
const r = this.#rows.get(id);
|
|
16
|
+
return r ? { ...r } : null;
|
|
17
|
+
}
|
|
18
|
+
remove(id) {
|
|
19
|
+
this.#rows.delete(id);
|
|
20
|
+
}
|
|
21
|
+
pending(owner) {
|
|
22
|
+
let n = 0;
|
|
23
|
+
for (const r of this.#rows.values())
|
|
24
|
+
if (r.owner === owner && r.status === "uploading")
|
|
25
|
+
n++;
|
|
26
|
+
return n;
|
|
27
|
+
}
|
|
28
|
+
stale(uploadingBefore, unclaimedBefore, limit) {
|
|
29
|
+
return [...this.#rows.values()]
|
|
30
|
+
.filter((r) => (r.status === "uploading" && r.created < uploadingBefore) || (r.status === "ready" && !r.claimed && r.created < unclaimedBefore))
|
|
31
|
+
.slice(0, limit)
|
|
32
|
+
.map((r) => ({ ...r }));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export class SqlFileStore {
|
|
36
|
+
#db;
|
|
37
|
+
#t;
|
|
38
|
+
constructor(db, table = "weft_files") {
|
|
39
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(table))
|
|
40
|
+
throw new Error(`@weft-framework/server: not a valid table name: ${table}`);
|
|
41
|
+
this.#db = db;
|
|
42
|
+
this.#t = `"${table}"`;
|
|
43
|
+
db.exec(`CREATE TABLE IF NOT EXISTS ${this.#t} (id TEXT PRIMARY KEY NOT NULL, kind TEXT NOT NULL, owner TEXT NOT NULL, ` +
|
|
44
|
+
`name TEXT NOT NULL, type TEXT NOT NULL, size INTEGER NOT NULL, part_bytes INTEGER NOT NULL, status TEXT NOT NULL, ` +
|
|
45
|
+
`upload TEXT NOT NULL, claimed INTEGER NOT NULL, created INTEGER NOT NULL)`);
|
|
46
|
+
db.exec(`CREATE INDEX IF NOT EXISTS "${table}_owner" ON ${this.#t} (owner, status)`);
|
|
47
|
+
db.exec(`CREATE INDEX IF NOT EXISTS "${table}_created" ON ${this.#t} (created)`);
|
|
48
|
+
}
|
|
49
|
+
put(r) {
|
|
50
|
+
this.#db
|
|
51
|
+
.prepare(`INSERT OR REPLACE INTO ${this.#t} (id, kind, owner, name, type, size, part_bytes, status, upload, claimed, created) ` +
|
|
52
|
+
`VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
53
|
+
.run(r.id, r.kind, r.owner, r.name, r.type, r.size, r.partBytes, r.status, r.upload, r.claimed ? 1 : 0, r.created);
|
|
54
|
+
}
|
|
55
|
+
static #row(x) {
|
|
56
|
+
return {
|
|
57
|
+
id: String(x.id),
|
|
58
|
+
kind: String(x.kind),
|
|
59
|
+
owner: String(x.owner),
|
|
60
|
+
name: String(x.name),
|
|
61
|
+
type: String(x.type),
|
|
62
|
+
size: Number(x.size),
|
|
63
|
+
partBytes: Number(x.part_bytes),
|
|
64
|
+
status: String(x.status),
|
|
65
|
+
upload: String(x.upload),
|
|
66
|
+
claimed: Number(x.claimed) === 1,
|
|
67
|
+
created: Number(x.created),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
get(id) {
|
|
71
|
+
const r = this.#db.prepare(`SELECT * FROM ${this.#t} WHERE id = ?`).all(id)[0];
|
|
72
|
+
return r ? SqlFileStore.#row(r) : null;
|
|
73
|
+
}
|
|
74
|
+
remove(id) {
|
|
75
|
+
this.#db.prepare(`DELETE FROM ${this.#t} WHERE id = ?`).run(id);
|
|
76
|
+
}
|
|
77
|
+
pending(owner) {
|
|
78
|
+
const r = this.#db.prepare(`SELECT COUNT(*) AS n FROM ${this.#t} WHERE owner = ? AND status = 'uploading'`).all(owner)[0];
|
|
79
|
+
return Number(r.n);
|
|
80
|
+
}
|
|
81
|
+
stale(uploadingBefore, unclaimedBefore, limit) {
|
|
82
|
+
return this.#db
|
|
83
|
+
.prepare(`SELECT * FROM ${this.#t} WHERE (status = 'uploading' AND created < ?) OR (status = 'ready' AND claimed = 0 AND created < ?) LIMIT ?`)
|
|
84
|
+
.all(uploadingBefore, unclaimedBefore, limit).map(SqlFileStore.#row);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const KEY_RE = /^[a-z0-9]{32}$/;
|
|
88
|
+
function checkKey(key) {
|
|
89
|
+
if (!KEY_RE.test(key))
|
|
90
|
+
throw new Error("uploads: bad storage key");
|
|
91
|
+
return key;
|
|
92
|
+
}
|
|
93
|
+
export function diskStorage(o) {
|
|
94
|
+
if (typeof o?.dir !== "string" || o.dir === "")
|
|
95
|
+
throw new Error("diskStorage: dir is a directory path");
|
|
96
|
+
const io = async () => {
|
|
97
|
+
const [fs, path, stream] = await Promise.all([import("node:fs"), import("node:path"), import("node:stream")]);
|
|
98
|
+
return { fs, path, stream };
|
|
99
|
+
};
|
|
100
|
+
const partDir = async (upload) => {
|
|
101
|
+
const { path } = await io();
|
|
102
|
+
return path.join(o.dir, ".parts", checkKey(upload));
|
|
103
|
+
};
|
|
104
|
+
return {
|
|
105
|
+
async begin(key) {
|
|
106
|
+
checkKey(key);
|
|
107
|
+
const { fs } = await io();
|
|
108
|
+
const upload = randomId();
|
|
109
|
+
await fs.promises.mkdir(await partDir(upload), { recursive: true });
|
|
110
|
+
return upload;
|
|
111
|
+
},
|
|
112
|
+
async partTarget() {
|
|
113
|
+
return null;
|
|
114
|
+
},
|
|
115
|
+
async putPart(key, upload, index, body, size) {
|
|
116
|
+
checkKey(key);
|
|
117
|
+
const { fs, path } = await io();
|
|
118
|
+
const dir = await partDir(upload);
|
|
119
|
+
const tmp = path.join(dir, `${index}.tmp-${randomId().slice(0, 8)}`);
|
|
120
|
+
const out = fs.createWriteStream(tmp, { flags: "wx" });
|
|
121
|
+
let got = 0;
|
|
122
|
+
const hash = await import("node:crypto").then((c) => c.createHash("sha256"));
|
|
123
|
+
try {
|
|
124
|
+
const reader = body.getReader();
|
|
125
|
+
for (;;) {
|
|
126
|
+
const { done, value } = await reader.read();
|
|
127
|
+
if (done)
|
|
128
|
+
break;
|
|
129
|
+
got += value.length;
|
|
130
|
+
if (got > size) {
|
|
131
|
+
await reader.cancel().catch(() => { });
|
|
132
|
+
throw new UploadError("too-large", `part ${index} is over ${size} bytes`);
|
|
133
|
+
}
|
|
134
|
+
hash.update(value);
|
|
135
|
+
if (!out.write(value))
|
|
136
|
+
await new Promise((r) => out.once("drain", () => r()));
|
|
137
|
+
}
|
|
138
|
+
await new Promise((res, rej) => out.end((err) => (err ? rej(err) : res())));
|
|
139
|
+
if (got !== size)
|
|
140
|
+
throw new UploadError("invalid", `part ${index} is ${got} bytes, not ${size}`);
|
|
141
|
+
await fs.promises.rename(tmp, path.join(dir, `${index}`));
|
|
142
|
+
await fs.promises.writeFile(path.join(dir, `${index}.etag`), hash.digest("hex"));
|
|
143
|
+
}
|
|
144
|
+
catch (err) {
|
|
145
|
+
out.destroy();
|
|
146
|
+
await fs.promises.rm(tmp, { force: true });
|
|
147
|
+
throw err;
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
async parts(key, upload) {
|
|
151
|
+
checkKey(key);
|
|
152
|
+
const { fs, path } = await io();
|
|
153
|
+
const dir = await partDir(upload);
|
|
154
|
+
let names;
|
|
155
|
+
try {
|
|
156
|
+
names = await fs.promises.readdir(dir);
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
return [];
|
|
160
|
+
}
|
|
161
|
+
const out = [];
|
|
162
|
+
for (const n of names) {
|
|
163
|
+
if (!/^[1-9][0-9]{0,4}$/.test(n))
|
|
164
|
+
continue;
|
|
165
|
+
try {
|
|
166
|
+
const etag = await fs.promises.readFile(path.join(dir, `${n}.etag`), "utf8");
|
|
167
|
+
out.push({ index: Number(n), etag, size: (await fs.promises.stat(path.join(dir, n))).size });
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return out.sort((a, b) => a.index - b.index);
|
|
173
|
+
},
|
|
174
|
+
async complete(key, upload, parts) {
|
|
175
|
+
checkKey(key);
|
|
176
|
+
const { fs, path } = await io();
|
|
177
|
+
const dir = await partDir(upload);
|
|
178
|
+
const have = new Map((await this.parts(key, upload)).map((p) => [p.index, p.etag]));
|
|
179
|
+
for (const p of parts)
|
|
180
|
+
if (have.get(p.index) !== p.etag)
|
|
181
|
+
throw new UploadError("invalid", `part ${p.index} is missing or changed`);
|
|
182
|
+
const final = path.join(o.dir, key);
|
|
183
|
+
const tmp = `${final}.tmp-${randomId().slice(0, 8)}`;
|
|
184
|
+
const handle = await fs.promises.open(tmp, "wx");
|
|
185
|
+
try {
|
|
186
|
+
for (const p of parts) {
|
|
187
|
+
const data = fs.createReadStream(path.join(dir, String(p.index)));
|
|
188
|
+
for await (const chunk of data)
|
|
189
|
+
await handle.write(chunk);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
finally {
|
|
193
|
+
await handle.close();
|
|
194
|
+
}
|
|
195
|
+
await fs.promises.rename(tmp, final);
|
|
196
|
+
await fs.promises.rm(dir, { recursive: true, force: true });
|
|
197
|
+
return (await fs.promises.stat(final)).size;
|
|
198
|
+
},
|
|
199
|
+
async abort(key, upload) {
|
|
200
|
+
checkKey(key);
|
|
201
|
+
const { fs } = await io();
|
|
202
|
+
await fs.promises.rm(await partDir(upload), { recursive: true, force: true });
|
|
203
|
+
},
|
|
204
|
+
async read(key, range) {
|
|
205
|
+
checkKey(key);
|
|
206
|
+
const { fs, path, stream } = await io();
|
|
207
|
+
const file = path.join(o.dir, key);
|
|
208
|
+
try {
|
|
209
|
+
await fs.promises.access(file);
|
|
210
|
+
}
|
|
211
|
+
catch {
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
const rs = fs.createReadStream(file, range ? { start: range.start, end: range.end } : {});
|
|
215
|
+
return stream.Readable.toWeb(rs);
|
|
216
|
+
},
|
|
217
|
+
async size(key) {
|
|
218
|
+
checkKey(key);
|
|
219
|
+
const { fs, path } = await io();
|
|
220
|
+
try {
|
|
221
|
+
return (await fs.promises.stat(path.join(o.dir, key))).size;
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
},
|
|
227
|
+
async remove(key) {
|
|
228
|
+
checkKey(key);
|
|
229
|
+
const { fs, path } = await io();
|
|
230
|
+
await fs.promises.rm(path.join(o.dir, key), { force: true });
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
function awsEncode(s) {
|
|
235
|
+
return encodeURIComponent(s).replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
236
|
+
}
|
|
237
|
+
function hex(b) {
|
|
238
|
+
return [...b].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
239
|
+
}
|
|
240
|
+
async function sha256Hex(data) {
|
|
241
|
+
return hex(new Uint8Array(await crypto.subtle.digest("SHA-256", typeof data === "string" ? utf8.encode(data) : data)));
|
|
242
|
+
}
|
|
243
|
+
async function hmacRaw(key, data) {
|
|
244
|
+
const k = await crypto.subtle.importKey("raw", key, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
245
|
+
return new Uint8Array(await crypto.subtle.sign("HMAC", k, utf8.encode(data)));
|
|
246
|
+
}
|
|
247
|
+
const reveal = (v) => (isSecret(v) ? v.reveal() : v);
|
|
248
|
+
function canonicalQuery(params) {
|
|
249
|
+
return params
|
|
250
|
+
.map(([k, v]) => [awsEncode(k), awsEncode(v)])
|
|
251
|
+
.sort(([a, x], [b, y]) => (a < b ? -1 : a > b ? 1 : x < y ? -1 : x > y ? 1 : 0))
|
|
252
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
253
|
+
.join("&");
|
|
254
|
+
}
|
|
255
|
+
async function signingKey(secret, day, region, service) {
|
|
256
|
+
let k = await hmacRaw(utf8.encode(`AWS4${secret}`), day);
|
|
257
|
+
k = await hmacRaw(k, region);
|
|
258
|
+
k = await hmacRaw(k, service);
|
|
259
|
+
return hmacRaw(k, "aws4_request");
|
|
260
|
+
}
|
|
261
|
+
export async function presignS3(r) {
|
|
262
|
+
const u = new URL(r.url);
|
|
263
|
+
const amzDate = (r.now ?? new Date()).toISOString().replace(/[-:]|\.\d{3}/g, "");
|
|
264
|
+
const day = amzDate.slice(0, 8);
|
|
265
|
+
const scope = `${day}/${r.region}/s3/aws4_request`;
|
|
266
|
+
const headers = { host: u.host };
|
|
267
|
+
for (const [k, v] of Object.entries(r.headers ?? {}))
|
|
268
|
+
headers[k.toLowerCase()] = v;
|
|
269
|
+
const names = Object.keys(headers).sort();
|
|
270
|
+
const params = [...u.searchParams.entries()];
|
|
271
|
+
params.push(["X-Amz-Algorithm", "AWS4-HMAC-SHA256"], ["X-Amz-Credential", `${r.accessKeyId}/${scope}`], ["X-Amz-Date", amzDate], ["X-Amz-Expires", String(r.expiresSeconds)], ["X-Amz-SignedHeaders", names.join(";")]);
|
|
272
|
+
if (r.sessionToken)
|
|
273
|
+
params.push(["X-Amz-Security-Token", r.sessionToken]);
|
|
274
|
+
const query = canonicalQuery(params);
|
|
275
|
+
const path = u.pathname.split("/").map((s) => awsEncode(decodeURIComponent(s))).join("/") || "/";
|
|
276
|
+
const canonical = [
|
|
277
|
+
r.method.toUpperCase(),
|
|
278
|
+
path,
|
|
279
|
+
query,
|
|
280
|
+
names.map((n) => `${n}:${headers[n].trim()}\n`).join(""),
|
|
281
|
+
names.join(";"),
|
|
282
|
+
"UNSIGNED-PAYLOAD",
|
|
283
|
+
].join("\n");
|
|
284
|
+
const toSign = `AWS4-HMAC-SHA256\n${amzDate}\n${scope}\n${await sha256Hex(canonical)}`;
|
|
285
|
+
const sig = hex(await hmacRaw(await signingKey(r.secretAccessKey, day, r.region, "s3"), toSign));
|
|
286
|
+
return `${u.origin}${path}?${query}&X-Amz-Signature=${sig}`;
|
|
287
|
+
}
|
|
288
|
+
export function s3Storage(o) {
|
|
289
|
+
let base;
|
|
290
|
+
try {
|
|
291
|
+
base = new URL(o.endpoint);
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
throw new Error("s3Storage: endpoint is a URL");
|
|
295
|
+
}
|
|
296
|
+
if (base.protocol !== "https:" && !["localhost", "127.0.0.1", "[::1]"].includes(base.hostname)) {
|
|
297
|
+
throw new Error("s3Storage: endpoint must be https (http only for a local MinIO)");
|
|
298
|
+
}
|
|
299
|
+
if (typeof o.bucket !== "string" || !/^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$/.test(o.bucket))
|
|
300
|
+
throw new Error("s3Storage: bucket is an S3 bucket name");
|
|
301
|
+
if (typeof o.region !== "string" || !/^[a-z0-9-]{1,32}$/.test(o.region))
|
|
302
|
+
throw new Error("s3Storage: region is like us-east-1 (auto for R2)");
|
|
303
|
+
if (typeof o.accessKeyId !== "string" || o.accessKeyId === "")
|
|
304
|
+
throw new Error("s3Storage: accessKeyId is required");
|
|
305
|
+
const prefix = o.prefix ?? "";
|
|
306
|
+
if (typeof prefix !== "string" || !/^([A-Za-z0-9_-]+\/)*$/.test(prefix))
|
|
307
|
+
throw new Error("s3Storage: prefix is like uploads/");
|
|
308
|
+
const doFetch = o.fetch ?? ((u, init) => fetch(u, init));
|
|
309
|
+
const now = o.now ?? (() => new Date());
|
|
310
|
+
const objectUrl = (key, query = "") => {
|
|
311
|
+
const k = awsEncode(prefix + checkKey(key)).replace(/%2F/g, "/");
|
|
312
|
+
const root = base.href.replace(/\/$/, "");
|
|
313
|
+
const url = o.pathStyle ? `${root}/${o.bucket}/${k}` : `${base.protocol}//${o.bucket}.${base.host}${base.pathname.replace(/\/$/, "")}/${k}`;
|
|
314
|
+
return query ? `${url}?${query}` : url;
|
|
315
|
+
};
|
|
316
|
+
const creds = () => ({ secret: reveal(o.secretAccessKey), token: o.sessionToken !== undefined ? reveal(o.sessionToken) : undefined });
|
|
317
|
+
async function call(method, url, body = "", extra = {}) {
|
|
318
|
+
const u = new URL(url);
|
|
319
|
+
const amzDate = now().toISOString().replace(/[-:]|\.\d{3}/g, "");
|
|
320
|
+
const day = amzDate.slice(0, 8);
|
|
321
|
+
const payload = await sha256Hex(body);
|
|
322
|
+
const { secret, token } = creds();
|
|
323
|
+
const headers = { host: u.host, "x-amz-date": amzDate, "x-amz-content-sha256": payload };
|
|
324
|
+
for (const [k, v] of Object.entries(extra))
|
|
325
|
+
headers[k.toLowerCase()] = v;
|
|
326
|
+
if (token)
|
|
327
|
+
headers["x-amz-security-token"] = token;
|
|
328
|
+
const names = Object.keys(headers).sort();
|
|
329
|
+
const path = u.pathname.split("/").map((s) => awsEncode(decodeURIComponent(s))).join("/") || "/";
|
|
330
|
+
const canonical = [method, path, canonicalQuery([...u.searchParams.entries()]), names.map((n) => `${n}:${headers[n].trim()}\n`).join(""), names.join(";"), payload].join("\n");
|
|
331
|
+
const scope = `${day}/${o.region}/s3/aws4_request`;
|
|
332
|
+
const toSign = `AWS4-HMAC-SHA256\n${amzDate}\n${scope}\n${await sha256Hex(canonical)}`;
|
|
333
|
+
const sig = hex(await hmacRaw(await signingKey(secret, day, o.region, "s3"), toSign));
|
|
334
|
+
const { host: _h, ...send } = headers;
|
|
335
|
+
send.authorization = `AWS4-HMAC-SHA256 Credential=${o.accessKeyId}/${scope}, SignedHeaders=${names.join(";")}, Signature=${sig}`;
|
|
336
|
+
const res = await doFetch(url, { method, headers: send, ...(body ? { body } : {}), redirect: "error" });
|
|
337
|
+
return res;
|
|
338
|
+
}
|
|
339
|
+
async function ok(res, what) {
|
|
340
|
+
const text = await res.text();
|
|
341
|
+
if (!res.ok) {
|
|
342
|
+
const code = /<Code>([^<]{1,64})<\/Code>/.exec(text)?.[1] ?? `HTTP ${res.status}`;
|
|
343
|
+
throw new Error(`s3: ${what} failed: ${code}`);
|
|
344
|
+
}
|
|
345
|
+
return text;
|
|
346
|
+
}
|
|
347
|
+
const xmlEscape = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
348
|
+
return {
|
|
349
|
+
async begin(key, type) {
|
|
350
|
+
const text = await ok(await call("POST", objectUrl(key, "uploads="), "", { "content-type": type }), "CreateMultipartUpload");
|
|
351
|
+
const id = /<UploadId>([^<]{1,1024})<\/UploadId>/.exec(text)?.[1];
|
|
352
|
+
if (!id)
|
|
353
|
+
throw new Error("s3: CreateMultipartUpload returned no UploadId");
|
|
354
|
+
return id.replace(/&/g, "&");
|
|
355
|
+
},
|
|
356
|
+
async partTarget(key, upload, index, size, expiresSeconds) {
|
|
357
|
+
const url = objectUrl(key, `partNumber=${index}&uploadId=${awsEncode(upload)}`);
|
|
358
|
+
const headers = { "content-length": String(size) };
|
|
359
|
+
const { secret, token } = creds();
|
|
360
|
+
const signed = await presignS3({
|
|
361
|
+
method: "PUT",
|
|
362
|
+
url,
|
|
363
|
+
region: o.region,
|
|
364
|
+
accessKeyId: o.accessKeyId,
|
|
365
|
+
secretAccessKey: secret,
|
|
366
|
+
...(token ? { sessionToken: token } : {}),
|
|
367
|
+
expiresSeconds,
|
|
368
|
+
headers,
|
|
369
|
+
now: now(),
|
|
370
|
+
});
|
|
371
|
+
return { url: signed, headers: {} };
|
|
372
|
+
},
|
|
373
|
+
async parts(key, upload) {
|
|
374
|
+
const out = [];
|
|
375
|
+
let marker = "";
|
|
376
|
+
for (let page = 0; page < 20; page++) {
|
|
377
|
+
const text = await ok(await call("GET", objectUrl(key, `uploadId=${awsEncode(upload)}${marker ? `&part-number-marker=${marker}` : ""}`)), "ListParts");
|
|
378
|
+
for (const m of text.matchAll(/<Part>([\s\S]*?)<\/Part>/g)) {
|
|
379
|
+
const body = m[1];
|
|
380
|
+
const index = Number(/<PartNumber>(\d+)<\/PartNumber>/.exec(body)?.[1]);
|
|
381
|
+
const etag = (/<ETag>([^<]*)<\/ETag>/.exec(body)?.[1] ?? "").replace(/"/g, '"');
|
|
382
|
+
const size = Number(/<Size>(\d+)<\/Size>/.exec(body)?.[1]);
|
|
383
|
+
if (index > 0)
|
|
384
|
+
out.push({ index, etag, size });
|
|
385
|
+
}
|
|
386
|
+
if (!/<IsTruncated>true<\/IsTruncated>/.test(text))
|
|
387
|
+
break;
|
|
388
|
+
marker = /<NextPartNumberMarker>(\d+)<\/NextPartNumberMarker>/.exec(text)?.[1] ?? "";
|
|
389
|
+
if (!marker)
|
|
390
|
+
break;
|
|
391
|
+
}
|
|
392
|
+
return out.sort((a, b) => a.index - b.index);
|
|
393
|
+
},
|
|
394
|
+
async complete(key, upload, parts) {
|
|
395
|
+
const xml = `<CompleteMultipartUpload>` +
|
|
396
|
+
parts.map((p) => `<Part><PartNumber>${p.index}</PartNumber><ETag>${xmlEscape(p.etag)}</ETag></Part>`).join("") +
|
|
397
|
+
`</CompleteMultipartUpload>`;
|
|
398
|
+
const text = await ok(await call("POST", objectUrl(key, `uploadId=${awsEncode(upload)}`), xml, { "content-type": "application/xml" }), "CompleteMultipartUpload");
|
|
399
|
+
if (/<Error>/.test(text))
|
|
400
|
+
throw new Error(`s3: CompleteMultipartUpload failed: ${/<Code>([^<]{1,64})<\/Code>/.exec(text)?.[1] ?? "error"}`);
|
|
401
|
+
const size = await this.size(key);
|
|
402
|
+
if (size === null)
|
|
403
|
+
throw new Error("s3: the completed object is missing");
|
|
404
|
+
return size;
|
|
405
|
+
},
|
|
406
|
+
async abort(key, upload) {
|
|
407
|
+
const res = await call("DELETE", objectUrl(key, `uploadId=${awsEncode(upload)}`));
|
|
408
|
+
if (!res.ok && res.status !== 404)
|
|
409
|
+
await ok(res, "AbortMultipartUpload");
|
|
410
|
+
else
|
|
411
|
+
await res.body?.cancel();
|
|
412
|
+
},
|
|
413
|
+
async read(key, range) {
|
|
414
|
+
const res = await call("GET", objectUrl(key), "", range ? { range: `bytes=${range.start}-${range.end}` } : {});
|
|
415
|
+
if (res.status === 404)
|
|
416
|
+
return (await res.body?.cancel(), null);
|
|
417
|
+
if (!res.ok)
|
|
418
|
+
await ok(res, "GetObject");
|
|
419
|
+
return res.body;
|
|
420
|
+
},
|
|
421
|
+
async size(key) {
|
|
422
|
+
const res = await call("HEAD", objectUrl(key));
|
|
423
|
+
await res.body?.cancel();
|
|
424
|
+
if (res.status === 404)
|
|
425
|
+
return null;
|
|
426
|
+
if (!res.ok)
|
|
427
|
+
throw new Error(`s3: HeadObject failed: HTTP ${res.status}`);
|
|
428
|
+
return Number(res.headers.get("content-length"));
|
|
429
|
+
},
|
|
430
|
+
async remove(key) {
|
|
431
|
+
const res = await call("DELETE", objectUrl(key));
|
|
432
|
+
if (!res.ok && res.status !== 404)
|
|
433
|
+
await ok(res, "DeleteObject");
|
|
434
|
+
else
|
|
435
|
+
await res.body?.cancel();
|
|
436
|
+
},
|
|
437
|
+
async downloadUrl(key, d) {
|
|
438
|
+
const { secret, token } = creds();
|
|
439
|
+
const url = objectUrl(key, `response-content-disposition=${awsEncode(d.disposition)}&response-content-type=${awsEncode(d.type)}`);
|
|
440
|
+
return presignS3({ method: "GET", url, region: o.region, accessKeyId: o.accessKeyId, secretAccessKey: secret, ...(token ? { sessionToken: token } : {}), expiresSeconds: d.expiresSeconds, now: now() });
|
|
441
|
+
},
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
const at = (b, off, ...bytes) => bytes.every((x, i) => b[off + i] === x);
|
|
445
|
+
const ascii = (b, off, s) => at(b, off, ...[...s].map((c) => c.charCodeAt(0)));
|
|
446
|
+
export function sniffType(head) {
|
|
447
|
+
if (at(head, 0, 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a))
|
|
448
|
+
return "image/png";
|
|
449
|
+
if (at(head, 0, 0xff, 0xd8, 0xff))
|
|
450
|
+
return "image/jpeg";
|
|
451
|
+
if (ascii(head, 0, "GIF87a") || ascii(head, 0, "GIF89a"))
|
|
452
|
+
return "image/gif";
|
|
453
|
+
if (ascii(head, 0, "RIFF") && ascii(head, 8, "WEBP"))
|
|
454
|
+
return "image/webp";
|
|
455
|
+
if (ascii(head, 0, "RIFF") && ascii(head, 8, "WAVE"))
|
|
456
|
+
return "audio/wav";
|
|
457
|
+
if (ascii(head, 4, "ftyp")) {
|
|
458
|
+
const brand = String.fromCharCode(...head.subarray(8, 12));
|
|
459
|
+
if (brand === "avif" || brand === "avis")
|
|
460
|
+
return "image/avif";
|
|
461
|
+
if (["heic", "heix", "heim", "heis", "mif1"].includes(brand))
|
|
462
|
+
return "image/heic";
|
|
463
|
+
if (brand === "qt ")
|
|
464
|
+
return "video/quicktime";
|
|
465
|
+
if (brand.startsWith("M4A"))
|
|
466
|
+
return "audio/mp4";
|
|
467
|
+
return "video/mp4";
|
|
468
|
+
}
|
|
469
|
+
if (ascii(head, 0, "%PDF-"))
|
|
470
|
+
return "application/pdf";
|
|
471
|
+
if (at(head, 0, 0x50, 0x4b, 0x03, 0x04) || at(head, 0, 0x50, 0x4b, 0x05, 0x06))
|
|
472
|
+
return "application/zip";
|
|
473
|
+
if (at(head, 0, 0x1f, 0x8b))
|
|
474
|
+
return "application/gzip";
|
|
475
|
+
if (at(head, 0, 0x1a, 0x45, 0xdf, 0xa3))
|
|
476
|
+
return "video/webm";
|
|
477
|
+
if (ascii(head, 0, "ID3") || (head[0] === 0xff && head.length > 1 && (head[1] & 0xe0) === 0xe0))
|
|
478
|
+
return "audio/mpeg";
|
|
479
|
+
if (ascii(head, 0, "OggS"))
|
|
480
|
+
return "audio/ogg";
|
|
481
|
+
if (ascii(head, 0, "fLaC"))
|
|
482
|
+
return "audio/flac";
|
|
483
|
+
return null;
|
|
484
|
+
}
|
|
485
|
+
const TEXT_TYPES = new Set(["text/plain", "text/csv", "text/markdown", "application/json", "image/svg+xml", "text/html", "text/xml", "application/xml"]);
|
|
486
|
+
const SAME_AS = {
|
|
487
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "application/zip",
|
|
488
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "application/zip",
|
|
489
|
+
"application/vnd.openxmlformats-officedocument.presentationml.presentation": "application/zip",
|
|
490
|
+
"application/epub+zip": "application/zip",
|
|
491
|
+
"video/ogg": "audio/ogg",
|
|
492
|
+
"audio/webm": "video/webm",
|
|
493
|
+
"image/heif": "image/heic",
|
|
494
|
+
"audio/x-m4a": "audio/mp4",
|
|
495
|
+
};
|
|
496
|
+
function looksText(head) {
|
|
497
|
+
let text;
|
|
498
|
+
try {
|
|
499
|
+
text = new TextDecoder("utf-8", { fatal: true }).decode(head, { stream: true });
|
|
500
|
+
}
|
|
501
|
+
catch {
|
|
502
|
+
return false;
|
|
503
|
+
}
|
|
504
|
+
return !/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/.test(text);
|
|
505
|
+
}
|
|
506
|
+
export function contentMatches(type, head) {
|
|
507
|
+
if (type === "application/octet-stream")
|
|
508
|
+
return true;
|
|
509
|
+
const sniffed = sniffType(head);
|
|
510
|
+
if (TEXT_TYPES.has(type))
|
|
511
|
+
return sniffed === null && looksText(head);
|
|
512
|
+
return sniffed !== null && (sniffed === type || SAME_AS[type] === sniffed);
|
|
513
|
+
}
|
|
514
|
+
const INLINE = /^(image\/(png|jpeg|gif|webp|avif)|audio\/|video\/|application\/pdf$|text\/plain$)/;
|
|
515
|
+
const STATUS = { invalid: 400, forbidden: 403, "not-found": 404, "too-large": 413, type: 415, limit: 429, rejected: 422 };
|
|
516
|
+
export class UploadError extends ActionError {
|
|
517
|
+
code;
|
|
518
|
+
constructor(code, message) {
|
|
519
|
+
super(message, STATUS[code]);
|
|
520
|
+
this.name = "UploadError";
|
|
521
|
+
this.code = code;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
function randomId() {
|
|
525
|
+
return hex(crypto.getRandomValues(new Uint8Array(16)));
|
|
526
|
+
}
|
|
527
|
+
const UPLOADS = "/_weft/uploads/";
|
|
528
|
+
const FILES = "/_weft/files/";
|
|
529
|
+
function json(status, body) {
|
|
530
|
+
return new Response(JSON.stringify(body), {
|
|
531
|
+
status,
|
|
532
|
+
headers: { "content-type": "application/json; charset=utf-8", "cache-control": "no-store", "x-content-type-options": "nosniff" },
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
function cleanName(name) {
|
|
536
|
+
if (typeof name !== "string")
|
|
537
|
+
throw new UploadError("invalid", "name is the file's name");
|
|
538
|
+
const base = name.split(/[\\/]/).pop().replace(/[\x00-\x1f\x7f"]/g, "").trim();
|
|
539
|
+
if (base === "" || base === "." || base === "..")
|
|
540
|
+
throw new UploadError("invalid", "name is the file's name");
|
|
541
|
+
return [...base].slice(0, 200).join("");
|
|
542
|
+
}
|
|
543
|
+
export class Uploads {
|
|
544
|
+
kinds;
|
|
545
|
+
#storage;
|
|
546
|
+
#store;
|
|
547
|
+
#keyText;
|
|
548
|
+
#o;
|
|
549
|
+
#mac = null;
|
|
550
|
+
constructor(o) {
|
|
551
|
+
const fail = (m) => {
|
|
552
|
+
throw new Error(`defineUploads: ${m}`);
|
|
553
|
+
};
|
|
554
|
+
const key = isSecret(o.key) ? o.key.reveal() : o.key;
|
|
555
|
+
if (typeof key !== "string" || key.length < MIN_SECRET_LENGTH)
|
|
556
|
+
fail(`key must be at least ${MIN_SECRET_LENGTH} characters`);
|
|
557
|
+
if (!o.storage || typeof o.storage.begin !== "function")
|
|
558
|
+
fail("storage is diskStorage() or s3Storage()");
|
|
559
|
+
const kinds = {};
|
|
560
|
+
const names = Object.keys(o.kinds ?? {});
|
|
561
|
+
if (names.length === 0)
|
|
562
|
+
fail("kinds names at least one kind of upload");
|
|
563
|
+
for (const name of names) {
|
|
564
|
+
const k = o.kinds[name];
|
|
565
|
+
if (!/^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(name))
|
|
566
|
+
fail(`not a kind name: ${name}`);
|
|
567
|
+
if (!Number.isInteger(k?.maxBytes) || k.maxBytes < 1 || k.maxBytes > 5 * 1024 * GB)
|
|
568
|
+
fail(`${name}.maxBytes is 1 byte to 5 TiB`);
|
|
569
|
+
if (!Array.isArray(k.types) || k.types.length === 0)
|
|
570
|
+
fail(`${name}.types lists MIME types`);
|
|
571
|
+
for (const t of k.types)
|
|
572
|
+
if (typeof t !== "string" || !/^[a-z]+\/[a-z0-9.+-]+$/.test(t))
|
|
573
|
+
fail(`${name}: not a MIME type: ${JSON.stringify(t)}`);
|
|
574
|
+
kinds[name] = Object.freeze({ maxBytes: k.maxBytes, types: Object.freeze([...k.types]), anonymous: k.anonymous === true });
|
|
575
|
+
}
|
|
576
|
+
const int = (what, v, lo, hi) => (Number.isInteger(v) && v >= lo && v <= hi ? v : fail(`${what} is ${lo} to ${hi}`));
|
|
577
|
+
if (o.scan !== undefined && typeof o.scan !== "function")
|
|
578
|
+
fail("scan is a function");
|
|
579
|
+
this.kinds = Object.freeze(kinds);
|
|
580
|
+
this.#storage = o.storage;
|
|
581
|
+
this.#store = o.store ?? new MemoryFileStore();
|
|
582
|
+
this.#keyText = key;
|
|
583
|
+
this.#o = {
|
|
584
|
+
partBytes: int("partBytes", o.partBytes ?? 8 * MB, 5 * MB, 512 * MB),
|
|
585
|
+
maxPending: int("maxPending", o.maxPending ?? 10, 1, 100),
|
|
586
|
+
pendingTtlMs: int("pendingTtlMs", o.pendingTtlMs ?? 24 * 3600_000, 60_000, 30 * 24 * 3600_000),
|
|
587
|
+
unclaimedTtlMs: int("unclaimedTtlMs", o.unclaimedTtlMs ?? 24 * 3600_000, 60_000, 30 * 24 * 3600_000),
|
|
588
|
+
scan: o.scan,
|
|
589
|
+
now: o.now ?? Date.now,
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
async #sign(data) {
|
|
593
|
+
this.#mac ??= (async () => {
|
|
594
|
+
const base = await crypto.subtle.importKey("raw", utf8.encode(this.#keyText), "HKDF", false, ["deriveKey"]);
|
|
595
|
+
return crypto.subtle.deriveKey({ name: "HKDF", hash: "SHA-256", salt: utf8.encode("weft uploads"), info: utf8.encode("url") }, base, { name: "HMAC", hash: "SHA-256", length: 256 }, false, ["sign"]);
|
|
596
|
+
})();
|
|
597
|
+
const sig = new Uint8Array(await crypto.subtle.sign("HMAC", await this.#mac, utf8.encode(data)));
|
|
598
|
+
return hex(sig.subarray(0, 20));
|
|
599
|
+
}
|
|
600
|
+
async #verify(data, sig) {
|
|
601
|
+
if (!sig || !/^[0-9a-f]{40}$/.test(sig))
|
|
602
|
+
return false;
|
|
603
|
+
const want = await this.#sign(data);
|
|
604
|
+
let d = 0;
|
|
605
|
+
for (let i = 0; i < want.length; i++)
|
|
606
|
+
d |= want.charCodeAt(i) ^ sig.charCodeAt(i);
|
|
607
|
+
return d === 0;
|
|
608
|
+
}
|
|
609
|
+
async #owner(ctx, kind) {
|
|
610
|
+
const s = await authState(ctx);
|
|
611
|
+
if (s && !s.pending)
|
|
612
|
+
return `user:${s.user}`;
|
|
613
|
+
if (kind && !kind.anonymous)
|
|
614
|
+
return null;
|
|
615
|
+
if (!ctx.session)
|
|
616
|
+
return null;
|
|
617
|
+
return `session:${await this.#sign(`session:${ctx.session}`)}`;
|
|
618
|
+
}
|
|
619
|
+
async #mine(ctx, id) {
|
|
620
|
+
const r = typeof id === "string" && KEY_RE.test(id) ? await this.#store.get(id) : null;
|
|
621
|
+
if (!r)
|
|
622
|
+
throw new UploadError("not-found", "no such upload");
|
|
623
|
+
const kind = this.kinds[r.kind] ?? null;
|
|
624
|
+
const owner = await this.#owner(ctx, kind);
|
|
625
|
+
if (owner === null || owner !== r.owner)
|
|
626
|
+
throw new UploadError("not-found", "no such upload");
|
|
627
|
+
return r;
|
|
628
|
+
}
|
|
629
|
+
#info(r) {
|
|
630
|
+
return { id: r.id, kind: r.kind, name: r.name, type: r.type, size: r.size };
|
|
631
|
+
}
|
|
632
|
+
#partCount(r) {
|
|
633
|
+
return Math.max(1, Math.ceil(r.size / r.partBytes));
|
|
634
|
+
}
|
|
635
|
+
#partSize(r, index) {
|
|
636
|
+
const n = this.#partCount(r);
|
|
637
|
+
if (!Number.isInteger(index) || index < 1 || index > n)
|
|
638
|
+
throw new UploadError("invalid", `part is 1 to ${n}`);
|
|
639
|
+
return index < n ? r.partBytes : r.size - r.partBytes * (n - 1);
|
|
640
|
+
}
|
|
641
|
+
async start(ctx, kindName, file) {
|
|
642
|
+
const kind = this.kinds[kindName];
|
|
643
|
+
if (!kind)
|
|
644
|
+
throw new UploadError("invalid", "unknown kind of upload");
|
|
645
|
+
const owner = await this.#owner(ctx, kind);
|
|
646
|
+
if (owner === null)
|
|
647
|
+
throw new UploadError("forbidden", "sign in to upload files");
|
|
648
|
+
const name = cleanName(file?.name);
|
|
649
|
+
if (!Number.isSafeInteger(file.size) || file.size < 1)
|
|
650
|
+
throw new UploadError("invalid", "size is the file's size in bytes");
|
|
651
|
+
if (file.size > kind.maxBytes)
|
|
652
|
+
throw new UploadError("too-large", `files of this kind are at most ${kind.maxBytes} bytes`);
|
|
653
|
+
if (typeof file.type !== "string" || !kind.types.includes(file.type))
|
|
654
|
+
throw new UploadError("type", `files of this kind are ${kind.types.join(", ")}`);
|
|
655
|
+
if ((await this.#store.pending(owner)) >= this.#o.maxPending)
|
|
656
|
+
throw new UploadError("limit", "too many unfinished uploads");
|
|
657
|
+
const id = randomId();
|
|
658
|
+
const upload = await this.#storage.begin(id, file.type);
|
|
659
|
+
const r = { id, kind: kindName, owner, name, type: file.type, size: file.size, partBytes: this.#o.partBytes, status: "uploading", upload, claimed: false, created: this.#o.now() };
|
|
660
|
+
await this.#store.put(r);
|
|
661
|
+
return { id, partBytes: r.partBytes, parts: this.#partCount(r) };
|
|
662
|
+
}
|
|
663
|
+
async part(ctx, id, index) {
|
|
664
|
+
const r = await this.#mine(ctx, id);
|
|
665
|
+
if (r.status !== "uploading" || r.upload === "")
|
|
666
|
+
throw new UploadError("invalid", "the upload is finished");
|
|
667
|
+
const size = this.#partSize(r, index);
|
|
668
|
+
const direct = await this.#storage.partTarget(r.id, r.upload, index, size, 900);
|
|
669
|
+
if (direct)
|
|
670
|
+
return direct;
|
|
671
|
+
const exp = Math.floor(this.#o.now() / 1000) + 900;
|
|
672
|
+
const sig = await this.#sign(`part:${r.id}:${index}:${size}:${r.owner}:${exp}`);
|
|
673
|
+
return { url: `${UPLOADS}${r.id}/${index}?e=${exp}&s=${sig}`, headers: {} };
|
|
674
|
+
}
|
|
675
|
+
async status(ctx, id) {
|
|
676
|
+
const r = await this.#mine(ctx, id);
|
|
677
|
+
const received = r.status === "ready" ? [] : r.upload === "" ? Array.from({ length: this.#partCount(r) }, (_, i) => i + 1) : (await this.#storage.parts(r.id, r.upload)).filter((p) => p.size === this.#partSizeOr0(r, p.index)).map((p) => p.index);
|
|
678
|
+
return { ...this.#info(r), parts: this.#partCount(r), partBytes: r.partBytes, received, ready: r.status === "ready" };
|
|
679
|
+
}
|
|
680
|
+
#partSizeOr0(r, index) {
|
|
681
|
+
try {
|
|
682
|
+
return this.#partSize(r, index);
|
|
683
|
+
}
|
|
684
|
+
catch {
|
|
685
|
+
return -1;
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
async #head(id, size) {
|
|
689
|
+
const want = Math.min(size, 4096);
|
|
690
|
+
const stream = await this.#storage.read(id, { start: 0, end: want - 1 });
|
|
691
|
+
if (!stream)
|
|
692
|
+
throw new UploadError("invalid", "the file is missing");
|
|
693
|
+
const chunks = [];
|
|
694
|
+
let got = 0;
|
|
695
|
+
const reader = stream.getReader();
|
|
696
|
+
while (got < want) {
|
|
697
|
+
const { done, value } = await reader.read();
|
|
698
|
+
if (done)
|
|
699
|
+
break;
|
|
700
|
+
chunks.push(value);
|
|
701
|
+
got += value.length;
|
|
702
|
+
}
|
|
703
|
+
await reader.cancel().catch(() => { });
|
|
704
|
+
const out = new Uint8Array(Math.min(got, want));
|
|
705
|
+
let off = 0;
|
|
706
|
+
for (const c of chunks) {
|
|
707
|
+
const take = Math.min(c.length, out.length - off);
|
|
708
|
+
out.set(c.subarray(0, take), off);
|
|
709
|
+
off += take;
|
|
710
|
+
if (off >= out.length)
|
|
711
|
+
break;
|
|
712
|
+
}
|
|
713
|
+
return out;
|
|
714
|
+
}
|
|
715
|
+
async complete(ctx, id) {
|
|
716
|
+
const r = await this.#mine(ctx, id);
|
|
717
|
+
if (r.status === "ready")
|
|
718
|
+
return this.#info(r);
|
|
719
|
+
let size;
|
|
720
|
+
if (r.upload !== "") {
|
|
721
|
+
const n = this.#partCount(r);
|
|
722
|
+
const have = new Map((await this.#storage.parts(r.id, r.upload)).map((p) => [p.index, p]));
|
|
723
|
+
const parts = [];
|
|
724
|
+
for (let i = 1; i <= n; i++) {
|
|
725
|
+
const p = have.get(i);
|
|
726
|
+
if (!p || p.size !== this.#partSize(r, i))
|
|
727
|
+
throw new UploadError("invalid", `part ${i} has not arrived`);
|
|
728
|
+
parts.push({ index: i, etag: p.etag });
|
|
729
|
+
}
|
|
730
|
+
size = await this.#storage.complete(r.id, r.upload, parts);
|
|
731
|
+
r.upload = "";
|
|
732
|
+
await this.#store.put(r);
|
|
733
|
+
}
|
|
734
|
+
else {
|
|
735
|
+
size = (await this.#storage.size(r.id)) ?? -1;
|
|
736
|
+
}
|
|
737
|
+
const reject = async (code, message) => {
|
|
738
|
+
await this.#storage.remove(r.id).catch(() => { });
|
|
739
|
+
await this.#store.remove(r.id);
|
|
740
|
+
throw new UploadError(code, message);
|
|
741
|
+
};
|
|
742
|
+
if (size !== r.size)
|
|
743
|
+
await reject("invalid", `the file is ${size} bytes, not ${r.size}`);
|
|
744
|
+
if (!contentMatches(r.type, await this.#head(r.id, r.size)))
|
|
745
|
+
await reject("type", `the file's content is not ${r.type}`);
|
|
746
|
+
if (this.#o.scan) {
|
|
747
|
+
let verdict;
|
|
748
|
+
try {
|
|
749
|
+
verdict = await this.#o.scan({ ...this.#info(r), read: async () => (await this.#storage.read(r.id)) ?? new ReadableStream() });
|
|
750
|
+
}
|
|
751
|
+
catch (err) {
|
|
752
|
+
throw new UploadError("limit", `the file could not be scanned yet: ${err instanceof Error ? err.message : String(err)}`);
|
|
753
|
+
}
|
|
754
|
+
if (verdict !== true)
|
|
755
|
+
await reject("rejected", typeof verdict === "string" ? verdict : "the file was rejected by the scanner");
|
|
756
|
+
}
|
|
757
|
+
r.status = "ready";
|
|
758
|
+
await this.#store.put(r);
|
|
759
|
+
return this.#info(r);
|
|
760
|
+
}
|
|
761
|
+
async abort(ctx, id) {
|
|
762
|
+
let r;
|
|
763
|
+
try {
|
|
764
|
+
r = await this.#mine(ctx, id);
|
|
765
|
+
}
|
|
766
|
+
catch {
|
|
767
|
+
return false;
|
|
768
|
+
}
|
|
769
|
+
if (r.status !== "uploading")
|
|
770
|
+
return false;
|
|
771
|
+
if (r.upload === "")
|
|
772
|
+
await this.#storage.remove(r.id).catch(() => { });
|
|
773
|
+
else
|
|
774
|
+
await this.#storage.abort(r.id, r.upload).catch(() => { });
|
|
775
|
+
await this.#store.remove(r.id);
|
|
776
|
+
return true;
|
|
777
|
+
}
|
|
778
|
+
async claim(ctx, id, kind) {
|
|
779
|
+
const r = await this.#mine(ctx, id);
|
|
780
|
+
if (r.status !== "ready")
|
|
781
|
+
throw new UploadError("invalid", "the upload is not finished");
|
|
782
|
+
if (kind !== undefined && r.kind !== kind)
|
|
783
|
+
throw new UploadError("type", `not a ${kind} upload`);
|
|
784
|
+
if (r.claimed)
|
|
785
|
+
throw new UploadError("invalid", "the file is already in use");
|
|
786
|
+
r.claimed = true;
|
|
787
|
+
r.owner = `claimed:${r.owner}`;
|
|
788
|
+
await this.#store.put(r);
|
|
789
|
+
holdFile(r.id, ctx.user);
|
|
790
|
+
return this.#info(r);
|
|
791
|
+
}
|
|
792
|
+
track(onError = () => { }) {
|
|
793
|
+
this.#tracking ??= { count: 0, stop: () => { } };
|
|
794
|
+
const t = this.#tracking;
|
|
795
|
+
if (t.count++ === 0) {
|
|
796
|
+
t.stop = onFileRelease((id) => {
|
|
797
|
+
if (KEY_RE.test(id))
|
|
798
|
+
this.remove(id).catch(onError);
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
let stopped = false;
|
|
802
|
+
return () => {
|
|
803
|
+
if (stopped)
|
|
804
|
+
return;
|
|
805
|
+
stopped = true;
|
|
806
|
+
if (--t.count === 0)
|
|
807
|
+
t.stop();
|
|
808
|
+
};
|
|
809
|
+
}
|
|
810
|
+
#tracking = null;
|
|
811
|
+
async link(id, o = {}) {
|
|
812
|
+
const info = typeof id === "string" ? await this.info(id) : null;
|
|
813
|
+
if (!info)
|
|
814
|
+
return null;
|
|
815
|
+
const url = await this.url(info.id, o);
|
|
816
|
+
return url === null ? null : { ...info, url };
|
|
817
|
+
}
|
|
818
|
+
async info(id) {
|
|
819
|
+
const r = typeof id === "string" && KEY_RE.test(id) ? await this.#store.get(id) : null;
|
|
820
|
+
return r && r.status === "ready" ? this.#info(r) : null;
|
|
821
|
+
}
|
|
822
|
+
async remove(id) {
|
|
823
|
+
const r = typeof id === "string" && KEY_RE.test(id) ? await this.#store.get(id) : null;
|
|
824
|
+
if (!r)
|
|
825
|
+
return false;
|
|
826
|
+
if (r.status === "uploading" && r.upload !== "")
|
|
827
|
+
await this.#storage.abort(r.id, r.upload).catch(() => { });
|
|
828
|
+
else
|
|
829
|
+
await this.#storage.remove(r.id);
|
|
830
|
+
await this.#store.remove(r.id);
|
|
831
|
+
return true;
|
|
832
|
+
}
|
|
833
|
+
async url(id, o = {}) {
|
|
834
|
+
if (typeof id !== "string" || !KEY_RE.test(id))
|
|
835
|
+
return null;
|
|
836
|
+
const r = await this.#store.get(id);
|
|
837
|
+
if (!r || r.status !== "ready")
|
|
838
|
+
return null;
|
|
839
|
+
const ttl = o.ttlSeconds ?? 300;
|
|
840
|
+
if (!Number.isInteger(ttl) || ttl < 1 || ttl > 7 * 24 * 3600)
|
|
841
|
+
throw new UploadError("invalid", "ttlSeconds is 1 second to 7 days");
|
|
842
|
+
const disposition = this.#disposition(r, o.download === true);
|
|
843
|
+
if (this.#storage.downloadUrl)
|
|
844
|
+
return this.#storage.downloadUrl(r.id, { expiresSeconds: ttl, type: r.type, disposition });
|
|
845
|
+
const exp = Math.floor(this.#o.now() / 1000) + ttl;
|
|
846
|
+
const d = o.download ? 1 : 0;
|
|
847
|
+
return `${FILES}${r.id}?e=${exp}&d=${d}&s=${await this.#sign(`file:${r.id}:${exp}:${d}`)}`;
|
|
848
|
+
}
|
|
849
|
+
#disposition(r, download) {
|
|
850
|
+
const inline = !download && INLINE.test(r.type);
|
|
851
|
+
const fallback = r.name.replace(/[^\x20-\x7e]|[\\"%;]/g, "_");
|
|
852
|
+
return `${inline ? "inline" : "attachment"}; filename="${fallback}"; filename*=UTF-8''${encodeURIComponent(r.name)}`;
|
|
853
|
+
}
|
|
854
|
+
async sweep(limit = 500) {
|
|
855
|
+
sweepHeldFiles();
|
|
856
|
+
const now = this.#o.now();
|
|
857
|
+
let n = 0;
|
|
858
|
+
for (const r of await this.#store.stale(now - this.#o.pendingTtlMs, now - this.#o.unclaimedTtlMs, limit)) {
|
|
859
|
+
try {
|
|
860
|
+
await this.remove(r.id);
|
|
861
|
+
n++;
|
|
862
|
+
}
|
|
863
|
+
catch {
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
return n;
|
|
867
|
+
}
|
|
868
|
+
async handle(ctx, req, path) {
|
|
869
|
+
try {
|
|
870
|
+
const rest = path.slice(UPLOADS.length).split("/");
|
|
871
|
+
if (req.method === "PUT" && rest.length === 2)
|
|
872
|
+
return await this.#putPart(ctx, req, rest[0], rest[1]);
|
|
873
|
+
if (req.method === "GET" && rest.length === 1)
|
|
874
|
+
return json(200, await this.status(ctx, rest[0]));
|
|
875
|
+
if (req.method !== "POST")
|
|
876
|
+
return json(405, { ok: false, error: "Method not allowed" });
|
|
877
|
+
const declared = Number(req.headers.get("content-length") ?? "0");
|
|
878
|
+
if (declared > 4096)
|
|
879
|
+
return json(413, { ok: false, error: "Request too large" });
|
|
880
|
+
const text = await req.text();
|
|
881
|
+
if (text.length > 4096)
|
|
882
|
+
return json(413, { ok: false, error: "Request too large" });
|
|
883
|
+
let body = {};
|
|
884
|
+
if (text) {
|
|
885
|
+
try {
|
|
886
|
+
body = JSON.parse(text);
|
|
887
|
+
}
|
|
888
|
+
catch {
|
|
889
|
+
return json(400, { ok: false, error: "Malformed request body" });
|
|
890
|
+
}
|
|
891
|
+
if (!body || typeof body !== "object" || Array.isArray(body))
|
|
892
|
+
return json(400, { ok: false, error: "Malformed request body" });
|
|
893
|
+
}
|
|
894
|
+
if (rest.length === 1 && rest[0] === "start") {
|
|
895
|
+
return json(200, await this.start(ctx, body.kind, { name: body.name, size: body.size, type: body.type }));
|
|
896
|
+
}
|
|
897
|
+
if (rest.length === 3 && rest[1] === "part")
|
|
898
|
+
return json(200, await this.part(ctx, rest[0], Number(rest[2])));
|
|
899
|
+
if (rest.length === 2 && rest[1] === "complete")
|
|
900
|
+
return json(200, await this.complete(ctx, rest[0]));
|
|
901
|
+
if (rest.length === 2 && rest[1] === "abort")
|
|
902
|
+
return json(200, { ok: await this.abort(ctx, rest[0]) });
|
|
903
|
+
return json(404, { ok: false, error: "Not found" });
|
|
904
|
+
}
|
|
905
|
+
catch (err) {
|
|
906
|
+
if (err instanceof UploadError)
|
|
907
|
+
return json(STATUS[err.code], { ok: false, code: err.code, error: err.message });
|
|
908
|
+
throw err;
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
async #putPart(ctx, req, id, indexText) {
|
|
912
|
+
if (!this.#storage.putPart)
|
|
913
|
+
return json(404, { ok: false, error: "Not found" });
|
|
914
|
+
const r = await this.#mine(ctx, id);
|
|
915
|
+
if (r.status !== "uploading" || r.upload === "")
|
|
916
|
+
throw new UploadError("invalid", "the upload is finished");
|
|
917
|
+
const index = /^[1-9][0-9]{0,4}$/.test(indexText) ? Number(indexText) : NaN;
|
|
918
|
+
const size = this.#partSize(r, index);
|
|
919
|
+
const url = new URL(req.url);
|
|
920
|
+
const exp = Number(url.searchParams.get("e"));
|
|
921
|
+
if (!Number.isInteger(exp) || exp < Math.floor(this.#o.now() / 1000) || !(await this.#verify(`part:${r.id}:${index}:${size}:${r.owner}:${exp}`, url.searchParams.get("s")))) {
|
|
922
|
+
throw new UploadError("forbidden", "the part link expired; ask for a new one");
|
|
923
|
+
}
|
|
924
|
+
const declared = req.headers.get("content-length");
|
|
925
|
+
if (declared !== null && Number(declared) !== size)
|
|
926
|
+
throw new UploadError("invalid", `part ${index} is ${size} bytes`);
|
|
927
|
+
if (!req.body)
|
|
928
|
+
throw new UploadError("invalid", `part ${index} is ${size} bytes`);
|
|
929
|
+
await this.#storage.putPart(r.id, r.upload, index, req.body, size);
|
|
930
|
+
return json(200, { ok: true });
|
|
931
|
+
}
|
|
932
|
+
async serve(req, path) {
|
|
933
|
+
const plain = (status) => new Response(null, { status, headers: { "cache-control": "no-store", "x-content-type-options": "nosniff" } });
|
|
934
|
+
if (req.method !== "GET" && req.method !== "HEAD")
|
|
935
|
+
return plain(405);
|
|
936
|
+
const id = path.slice(FILES.length);
|
|
937
|
+
if (!KEY_RE.test(id))
|
|
938
|
+
return plain(404);
|
|
939
|
+
const url = new URL(req.url);
|
|
940
|
+
const exp = Number(url.searchParams.get("e"));
|
|
941
|
+
const d = url.searchParams.get("d") === "1" ? 1 : 0;
|
|
942
|
+
if (url.searchParams.get("d") !== String(d))
|
|
943
|
+
return plain(404);
|
|
944
|
+
const now = Math.floor(this.#o.now() / 1000);
|
|
945
|
+
if (!Number.isInteger(exp) || exp < now || !(await this.#verify(`file:${id}:${exp}:${d}`, url.searchParams.get("s"))))
|
|
946
|
+
return plain(404);
|
|
947
|
+
const r = await this.#store.get(id);
|
|
948
|
+
if (!r || r.status !== "ready")
|
|
949
|
+
return plain(404);
|
|
950
|
+
const headers = {
|
|
951
|
+
"content-type": r.type,
|
|
952
|
+
"content-disposition": this.#disposition(r, d === 1),
|
|
953
|
+
"x-content-type-options": "nosniff",
|
|
954
|
+
"content-security-policy": "default-src 'none'; img-src 'self' data:; media-src 'self'; style-src 'unsafe-inline'; sandbox",
|
|
955
|
+
"cross-origin-resource-policy": "same-origin",
|
|
956
|
+
"cache-control": `private, max-age=${Math.max(0, exp - now)}`,
|
|
957
|
+
"accept-ranges": "bytes",
|
|
958
|
+
"referrer-policy": "no-referrer",
|
|
959
|
+
};
|
|
960
|
+
let range;
|
|
961
|
+
const want = req.headers.get("range");
|
|
962
|
+
if (want) {
|
|
963
|
+
const m = /^bytes=(\d{0,15})-(\d{0,15})$/.exec(want.trim());
|
|
964
|
+
let start = m && m[1] !== "" ? Number(m[1]) : NaN;
|
|
965
|
+
let end = m && m[2] !== "" ? Number(m[2]) : r.size - 1;
|
|
966
|
+
if (m && m[1] === "" && m[2] !== "") {
|
|
967
|
+
start = Math.max(0, r.size - Number(m[2]));
|
|
968
|
+
end = r.size - 1;
|
|
969
|
+
}
|
|
970
|
+
end = Math.min(end, r.size - 1);
|
|
971
|
+
if (!m || Number.isNaN(start) || start > end || start >= r.size) {
|
|
972
|
+
return new Response(null, { status: 416, headers: { ...headers, "content-range": `bytes */${r.size}` } });
|
|
973
|
+
}
|
|
974
|
+
range = { start, end };
|
|
975
|
+
}
|
|
976
|
+
const length = range ? range.end - range.start + 1 : r.size;
|
|
977
|
+
headers["content-length"] = String(length);
|
|
978
|
+
if (range)
|
|
979
|
+
headers["content-range"] = `bytes ${range.start}-${range.end}/${r.size}`;
|
|
980
|
+
if (req.method === "HEAD")
|
|
981
|
+
return new Response(null, { status: range ? 206 : 200, headers });
|
|
982
|
+
const body = await this.#storage.read(r.id, range);
|
|
983
|
+
if (!body)
|
|
984
|
+
return plain(404);
|
|
985
|
+
return new Response(body, { status: range ? 206 : 200, headers });
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
export function defineUploads(o) {
|
|
989
|
+
return new Uploads(o);
|
|
990
|
+
}
|
|
991
|
+
export { UPLOADS as UPLOADS_PATH, FILES as FILES_PATH };
|