cloudrop 0.0.0-stage → 0.1.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 +56 -0
- package/LICENSE +21 -0
- package/README.md +101 -2
- package/dist/adapters/drizzle.d.ts +204 -0
- package/dist/adapters/drizzle.js +216 -0
- package/dist/adapters/drizzle.js.map +1 -0
- package/dist/adapters/prisma.d.ts +81 -0
- package/dist/adapters/prisma.js +32 -0
- package/dist/adapters/prisma.js.map +1 -0
- package/dist/adapters/sql.d.ts +98 -0
- package/dist/adapters/sql.js +70 -0
- package/dist/adapters/sql.js.map +1 -0
- package/dist/client-Dj6ON4MN.d.ts +214 -0
- package/dist/client-Y1zUEbCj.js +1561 -0
- package/dist/client-Y1zUEbCj.js.map +1 -0
- package/dist/client.d.ts +3 -0
- package/dist/client.js +3 -0
- package/dist/d1-BY2c5bcL.js +16 -0
- package/dist/d1-BY2c5bcL.js.map +1 -0
- package/dist/errors-CUxMmFAe.js +40 -0
- package/dist/errors-CUxMmFAe.js.map +1 -0
- package/dist/executor-CEA7WV2W.d.ts +27 -0
- package/dist/index.d.ts +421 -0
- package/dist/index.js +3007 -0
- package/dist/index.js.map +1 -0
- package/dist/input-DTC476_I.js +13 -0
- package/dist/input-DTC476_I.js.map +1 -0
- package/dist/next.d.ts +9 -0
- package/dist/next.js +14 -0
- package/dist/next.js.map +1 -0
- package/dist/node.d.ts +32 -0
- package/dist/node.js +100 -0
- package/dist/node.js.map +1 -0
- package/dist/part-size-CvMoobfC.js +9 -0
- package/dist/part-size-CvMoobfC.js.map +1 -0
- package/dist/react.d.ts +123 -0
- package/dist/react.js +355 -0
- package/dist/react.js.map +1 -0
- package/dist/route-CspI1Ypt.js +72 -0
- package/dist/route-CspI1Ypt.js.map +1 -0
- package/dist/store-BfCJejGh.d.ts +382 -0
- package/dist/tables-DZnw2sPK.d.ts +11 -0
- package/dist/transactional-DnvnGsVH.js +672 -0
- package/dist/transactional-DnvnGsVH.js.map +1 -0
- package/dist/types-BZUOaz_B.js +5 -0
- package/dist/types-BZUOaz_B.js.map +1 -0
- package/dist/types-BrAx3dA4.d.ts +185 -0
- package/package.json +110 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,3007 @@
|
|
|
1
|
+
import { a as quotaExceeded, i as ok, n as configError, r as fail, t as CloudropError } from "./errors-CUxMmFAe.js";
|
|
2
|
+
import { n as isPlainObject, t as invalidInput } from "./input-DTC476_I.js";
|
|
3
|
+
import { a as route, c as unauthorized, i as refusalError, n as forbidden, r as isRefusal, s as storeOfRoute, t as allowAnyone } from "./route-CspI1Ypt.js";
|
|
4
|
+
import { n as sizeOfPart, t as countParts } from "./part-size-CvMoobfC.js";
|
|
5
|
+
import "./types-BZUOaz_B.js";
|
|
6
|
+
// src/storage/sigv4.ts
|
|
7
|
+
const encoder$4 = new TextEncoder();
|
|
8
|
+
const UNSIGNED_PAYLOAD = "UNSIGNED-PAYLOAD";
|
|
9
|
+
const EMPTY_SHA256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
|
|
10
|
+
function toHex(bytes) {
|
|
11
|
+
const view = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
12
|
+
return Array.from(view, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
13
|
+
}
|
|
14
|
+
async function sha256Hex(data) {
|
|
15
|
+
const bytes = typeof data === "string" ? encoder$4.encode(data) : data;
|
|
16
|
+
return toHex(await crypto.subtle.digest("SHA-256", bytes));
|
|
17
|
+
}
|
|
18
|
+
function importHmacKey(raw) {
|
|
19
|
+
return crypto.subtle.importKey("raw", raw, {
|
|
20
|
+
name: "HMAC",
|
|
21
|
+
hash: "SHA-256"
|
|
22
|
+
}, false, ["sign"]);
|
|
23
|
+
}
|
|
24
|
+
async function hmac(key, data) {
|
|
25
|
+
return new Uint8Array(await crypto.subtle.sign("HMAC", key, encoder$4.encode(data)));
|
|
26
|
+
}
|
|
27
|
+
async function deriveSigningKey(secretAccessKey, day, region) {
|
|
28
|
+
let raw = await hmac(await importHmacKey(encoder$4.encode(`AWS4${secretAccessKey}`)), day);
|
|
29
|
+
for (const part of [
|
|
30
|
+
region,
|
|
31
|
+
"s3",
|
|
32
|
+
"aws4_request"
|
|
33
|
+
]) raw = await hmac(await importHmacKey(raw), part);
|
|
34
|
+
return importHmacKey(raw);
|
|
35
|
+
}
|
|
36
|
+
const signingKeys = /* @__PURE__ */ new WeakMap();
|
|
37
|
+
const MAX_CACHED_SCOPES = 8;
|
|
38
|
+
function signingKey(credentials, day, region) {
|
|
39
|
+
let cached = signingKeys.get(credentials);
|
|
40
|
+
if (cached?.secretAccessKey !== credentials.secretAccessKey) {
|
|
41
|
+
cached = {
|
|
42
|
+
secretAccessKey: credentials.secretAccessKey,
|
|
43
|
+
byScope: /* @__PURE__ */ new Map()
|
|
44
|
+
};
|
|
45
|
+
signingKeys.set(credentials, cached);
|
|
46
|
+
}
|
|
47
|
+
const scope = `${day}/${region}`;
|
|
48
|
+
let derived = cached.byScope.get(scope);
|
|
49
|
+
if (!derived) {
|
|
50
|
+
const { byScope } = cached;
|
|
51
|
+
if (byScope.size >= MAX_CACHED_SCOPES) byScope.clear();
|
|
52
|
+
derived = deriveSigningKey(credentials.secretAccessKey, day, region);
|
|
53
|
+
derived.catch(() => byScope.delete(scope));
|
|
54
|
+
byScope.set(scope, derived);
|
|
55
|
+
}
|
|
56
|
+
return derived;
|
|
57
|
+
}
|
|
58
|
+
function encodeRfc3986(value) {
|
|
59
|
+
return encodeURIComponent(value).replace(/[!'()*]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
60
|
+
}
|
|
61
|
+
function encodeKeyPath(key) {
|
|
62
|
+
return key.split("/").map(encodeRfc3986).join("/");
|
|
63
|
+
}
|
|
64
|
+
function toAmzDate(date) {
|
|
65
|
+
return date.toISOString().replace(/[-:]/g, "").replace(/\.\d{3}/, "");
|
|
66
|
+
}
|
|
67
|
+
function canonicalPath(url) {
|
|
68
|
+
return url.pathname.split("/").map((segment) => encodeRfc3986(decodeURIComponent(segment))).join("/");
|
|
69
|
+
}
|
|
70
|
+
function canonicalQuery(params) {
|
|
71
|
+
return Array.from(params, ([name, value]) => [encodeRfc3986(name), encodeRfc3986(value)]).sort(([aName, aValue], [bName, bValue]) => {
|
|
72
|
+
if (aName !== bName) return aName < bName ? -1 : 1;
|
|
73
|
+
if (aValue === bValue) return 0;
|
|
74
|
+
return aValue < bValue ? -1 : 1;
|
|
75
|
+
}).map(([name, value]) => `${name}=${value}`).join("&");
|
|
76
|
+
}
|
|
77
|
+
function canonicalHeaders(url, headers) {
|
|
78
|
+
const all = { host: url.host };
|
|
79
|
+
for (const [name, value] of Object.entries(headers)) all[name.toLowerCase()] = value.trim().replace(/\s+/g, " ");
|
|
80
|
+
const names = Object.keys(all).sort();
|
|
81
|
+
return {
|
|
82
|
+
canonical: names.map((name) => `${name}:${all[name]}\n`).join(""),
|
|
83
|
+
signedHeaders: names.join(";")
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
async function sign(input, amzDate, scope, canonicalRequest) {
|
|
87
|
+
const stringToSign = [
|
|
88
|
+
"AWS4-HMAC-SHA256",
|
|
89
|
+
amzDate,
|
|
90
|
+
scope,
|
|
91
|
+
await sha256Hex(canonicalRequest)
|
|
92
|
+
].join("\n");
|
|
93
|
+
return toHex(await hmac(await signingKey(input.credentials, amzDate.slice(0, 8), input.region), stringToSign));
|
|
94
|
+
}
|
|
95
|
+
async function presignUrl(input) {
|
|
96
|
+
const amzDate = toAmzDate(input.date);
|
|
97
|
+
const scope = `${amzDate.slice(0, 8)}/${input.region}/s3/aws4_request`;
|
|
98
|
+
const { canonical, signedHeaders } = canonicalHeaders(input.url, input.headers ?? {});
|
|
99
|
+
const url = new URL(input.url);
|
|
100
|
+
url.searchParams.set("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
|
|
101
|
+
url.searchParams.set("X-Amz-Credential", `${input.credentials.accessKeyId}/${scope}`);
|
|
102
|
+
url.searchParams.set("X-Amz-Date", amzDate);
|
|
103
|
+
url.searchParams.set("X-Amz-Expires", String(input.expiresIn));
|
|
104
|
+
url.searchParams.set("X-Amz-SignedHeaders", signedHeaders);
|
|
105
|
+
const query = canonicalQuery(url.searchParams);
|
|
106
|
+
url.search = `${query}&X-Amz-Signature=${await sign(input, amzDate, scope, [
|
|
107
|
+
input.method,
|
|
108
|
+
canonicalPath(url),
|
|
109
|
+
query,
|
|
110
|
+
canonical,
|
|
111
|
+
signedHeaders,
|
|
112
|
+
UNSIGNED_PAYLOAD
|
|
113
|
+
].join("\n"))}`;
|
|
114
|
+
return url;
|
|
115
|
+
}
|
|
116
|
+
async function signRequest(input) {
|
|
117
|
+
const amzDate = toAmzDate(input.date);
|
|
118
|
+
const scope = `${amzDate.slice(0, 8)}/${input.region}/s3/aws4_request`;
|
|
119
|
+
const headers = {
|
|
120
|
+
...input.headers,
|
|
121
|
+
"x-amz-content-sha256": input.payloadHash,
|
|
122
|
+
"x-amz-date": amzDate
|
|
123
|
+
};
|
|
124
|
+
const { canonical, signedHeaders } = canonicalHeaders(input.url, headers);
|
|
125
|
+
const signature = await sign(input, amzDate, scope, [
|
|
126
|
+
input.method,
|
|
127
|
+
canonicalPath(input.url),
|
|
128
|
+
canonicalQuery(input.url.searchParams),
|
|
129
|
+
canonical,
|
|
130
|
+
signedHeaders,
|
|
131
|
+
input.payloadHash
|
|
132
|
+
].join("\n"));
|
|
133
|
+
return {
|
|
134
|
+
...headers,
|
|
135
|
+
authorization: `AWS4-HMAC-SHA256 Credential=${input.credentials.accessKeyId}/${scope}, SignedHeaders=${signedHeaders}, Signature=${signature}`
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
// src/rules.ts
|
|
139
|
+
const MiB = 1048576;
|
|
140
|
+
const GiB = 1024 * MiB;
|
|
141
|
+
const R2_RULES = {
|
|
142
|
+
minPartSize: 5 * MiB,
|
|
143
|
+
maxPartSize: 5 * GiB - 5 * MiB,
|
|
144
|
+
maxParts: 1e4,
|
|
145
|
+
maxSinglePutSize: 5 * GiB - 5 * MiB,
|
|
146
|
+
maxObjectSize: 5 * (1024 * GiB) - 5 * GiB,
|
|
147
|
+
maxPresignSeconds: 604800
|
|
148
|
+
};
|
|
149
|
+
const S3_RULES = {
|
|
150
|
+
minPartSize: 5 * MiB,
|
|
151
|
+
maxPartSize: 5 * GiB,
|
|
152
|
+
maxParts: 1e4,
|
|
153
|
+
maxSinglePutSize: 5 * GiB,
|
|
154
|
+
maxObjectSize: 5e4 * GiB,
|
|
155
|
+
maxPresignSeconds: 604800
|
|
156
|
+
};
|
|
157
|
+
// src/storage/validate.ts
|
|
158
|
+
const encoder$3 = new TextEncoder();
|
|
159
|
+
const MAX_KEY_BYTES = 1024;
|
|
160
|
+
const MAX_METADATA_BYTES = 2048;
|
|
161
|
+
function keyProblem(key) {
|
|
162
|
+
if (typeof key !== "string") return "The object key must be a string. Pass the key as text.";
|
|
163
|
+
if (key.length === 0) return "The object key is empty. Pass a non-empty key.";
|
|
164
|
+
if (!key.isWellFormed()) return "The object key is not valid Unicode (it has a lone surrogate). Remove or replace that character.";
|
|
165
|
+
if (encoder$3.encode(key).length > MAX_KEY_BYTES) return "The object key is longer than 1024 bytes. Use a shorter key.";
|
|
166
|
+
if (key.startsWith("/")) return "The object key starts with a slash. Remove the leading slash.";
|
|
167
|
+
for (const char of key) {
|
|
168
|
+
const code = char.codePointAt(0) ?? 0;
|
|
169
|
+
if (code < 32 || code >= 127 && code <= 159) return "The object key contains control characters. Remove them.";
|
|
170
|
+
}
|
|
171
|
+
if (key.split("/").some((segment) => segment === "." || segment === "..")) return "The object key contains a '.' or '..' segment. Use plain path segments.";
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
const isPrintableAscii = (value) => typeof value === "string" && /^[\x20-\x7e]*$/.test(value);
|
|
175
|
+
function metadataProblem(metadata) {
|
|
176
|
+
if (metadata === void 0) return null;
|
|
177
|
+
if (!isPlainObject(metadata)) return "Metadata must be an object of names and text values, such as { \"cloudrop-id\": \"f1\" }.";
|
|
178
|
+
let bytes = 0;
|
|
179
|
+
for (const [name, value] of Object.entries(metadata)) {
|
|
180
|
+
if (!/^[a-z0-9-]+$/.test(name)) return "A metadata name may contain only lowercase letters, digits and hyphens. Rename it.";
|
|
181
|
+
if (typeof value !== "string") return `Metadata value for "${name}" must be a string. Convert it first.`;
|
|
182
|
+
if (!isPrintableAscii(value)) return `Metadata value for "${name}" must be printable ASCII. Encode other text first.`;
|
|
183
|
+
if (value.startsWith(" ") || value.endsWith(" ")) return `Metadata value for "${name}" must not start or end with a space. Trim it.`;
|
|
184
|
+
bytes += name.length + value.length;
|
|
185
|
+
}
|
|
186
|
+
return bytes > MAX_METADATA_BYTES ? "Metadata exceeds 2 KiB. Store large values in your database instead." : null;
|
|
187
|
+
}
|
|
188
|
+
function objectOptionsProblem(options) {
|
|
189
|
+
if (options.type !== void 0 && !isPrintableAscii(options.type)) return "The content type must be printable ASCII, such as \"text/plain; charset=utf-8\".";
|
|
190
|
+
if (options.disposition !== void 0 && !isPrintableAscii(options.disposition)) return "The content disposition must be printable ASCII. Percent-encode other characters, for example filename*=UTF-8''r%C3%A9sum%C3%A9.txt.";
|
|
191
|
+
return metadataProblem(options.metadata);
|
|
192
|
+
}
|
|
193
|
+
function bodyProblem(body) {
|
|
194
|
+
if (body instanceof Uint8Array || body instanceof ArrayBuffer) {
|
|
195
|
+
const buffer = body instanceof Uint8Array ? body.buffer : body;
|
|
196
|
+
if (buffer instanceof ArrayBuffer && !buffer.detached) return null;
|
|
197
|
+
return "The body's buffer is detached or shared (a SharedArrayBuffer). Copy the bytes into a new Uint8Array first.";
|
|
198
|
+
}
|
|
199
|
+
if (typeof body === "string" || body instanceof Blob) return null;
|
|
200
|
+
return "The body must be a Uint8Array, an ArrayBuffer, a string or a Blob.";
|
|
201
|
+
}
|
|
202
|
+
function checksumProblem(checksum) {
|
|
203
|
+
if (checksum === void 0 || /^[A-Za-z0-9+/]{5}[AQgw]==$/.test(checksum)) return null;
|
|
204
|
+
return "The CRC32C checksum must be the base64 of 4 bytes, such as \"4waSgw==\". Compute it from the exact bytes you send.";
|
|
205
|
+
}
|
|
206
|
+
const isToken = (value) => typeof value === "string" && /^[\x21-\x7e]+$/.test(value);
|
|
207
|
+
function uploadIdProblem(uploadId) {
|
|
208
|
+
return isToken(uploadId) ? null : "The upload ID is empty or malformed. Pass the uploadId that createMultipart returned.";
|
|
209
|
+
}
|
|
210
|
+
function cursorProblem(cursor) {
|
|
211
|
+
return isToken(cursor) ? null : "The list cursor is empty or malformed. Pass the nextCursor of the previous page.";
|
|
212
|
+
}
|
|
213
|
+
function prefixProblem(prefix) {
|
|
214
|
+
if (typeof prefix !== "string") return "The list prefix must be a string. Pass \"\" to list every object.";
|
|
215
|
+
if (prefix.isWellFormed()) return null;
|
|
216
|
+
return "The list prefix is not valid Unicode (it has a lone surrogate). Remove or replace that character.";
|
|
217
|
+
}
|
|
218
|
+
const MAX_LIST_PAGE = 1e3;
|
|
219
|
+
function limitProblem(limit) {
|
|
220
|
+
if (Number.isInteger(limit) && limit >= 1 && limit <= 1e3) return null;
|
|
221
|
+
return `The page size must be between 1 and ${MAX_LIST_PAGE}.`;
|
|
222
|
+
}
|
|
223
|
+
function partsProblem(parts, maxParts) {
|
|
224
|
+
return Array.isArray(parts) && parts.length > 0 && parts.every((part) => partNumberProblem(part?.partNumber, maxParts) === null && typeof part.etag === "string" && part.etag !== "") ? null : `Pass at least one part, each with a part number between 1 and ${maxParts} and the ETag the storage returned for it.`;
|
|
225
|
+
}
|
|
226
|
+
function keyListProblem(keys) {
|
|
227
|
+
return Array.isArray(keys) ? null : "deleteMany takes an array of keys. Wrap a single key in an array.";
|
|
228
|
+
}
|
|
229
|
+
const BYTE_UNITS = [
|
|
230
|
+
["GiB", GiB],
|
|
231
|
+
["MiB", MiB],
|
|
232
|
+
["KiB", 1024]
|
|
233
|
+
];
|
|
234
|
+
function formatBytes(bytes) {
|
|
235
|
+
const unit = BYTE_UNITS.find(([, size]) => bytes >= size);
|
|
236
|
+
return unit ? `${Number((bytes / unit[1]).toFixed(3))} ${unit[0]}` : `${bytes} bytes`;
|
|
237
|
+
}
|
|
238
|
+
function sizeProblem(size, max, of = "file") {
|
|
239
|
+
if (!Number.isSafeInteger(size) || size < 0) return "Size must be a whole number of bytes, zero or more.";
|
|
240
|
+
if (size <= max) return null;
|
|
241
|
+
return of === "part" ? `The part is over the part size limit of ${formatBytes(max)}. Use smaller parts.` : `Size is over the limit of ${formatBytes(max)}. Upload a smaller file or split it.`;
|
|
242
|
+
}
|
|
243
|
+
function expiryProblem(seconds, max) {
|
|
244
|
+
if (Number.isInteger(seconds) && seconds >= 1 && seconds <= max) return null;
|
|
245
|
+
return `Expiry must be a whole number of seconds between 1 and ${max}.`;
|
|
246
|
+
}
|
|
247
|
+
function partNumberProblem(partNumber, maxParts) {
|
|
248
|
+
if (Number.isInteger(partNumber) && partNumber >= 1 && partNumber <= maxParts) return null;
|
|
249
|
+
return `Part number must be between 1 and ${maxParts}.`;
|
|
250
|
+
}
|
|
251
|
+
function rangeProblem(start, end) {
|
|
252
|
+
if (Number.isSafeInteger(start) && Number.isSafeInteger(end) && start >= 0 && end >= start) return null;
|
|
253
|
+
return "The byte range must satisfy 0 <= start <= end.";
|
|
254
|
+
}
|
|
255
|
+
// src/server/bucket-check.ts
|
|
256
|
+
const UPLOAD_HEADERS = [
|
|
257
|
+
"content-type",
|
|
258
|
+
"content-disposition",
|
|
259
|
+
"if-none-match",
|
|
260
|
+
"x-amz-meta-cloudrop-id"
|
|
261
|
+
];
|
|
262
|
+
function matchesAny(patterns, value) {
|
|
263
|
+
return patterns.some((pattern) => {
|
|
264
|
+
const [head = "", tail] = pattern.toLowerCase().split("*");
|
|
265
|
+
if (tail === void 0) return head === value;
|
|
266
|
+
return value.length >= head.length + tail.length && value.startsWith(head) && value.endsWith(tail);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
const allowsUploads = (rule, origin) => matchesAny(rule.allowedOrigins, origin) && rule.allowedMethods.includes("PUT") && UPLOAD_HEADERS.every((header) => matchesAny(rule.allowedHeaders, header)) && rule.exposeHeaders.some((header) => header.toLowerCase() === "etag");
|
|
270
|
+
function corsCheck(rules, origins) {
|
|
271
|
+
if (origins.length === 0) return {
|
|
272
|
+
status: "unknown",
|
|
273
|
+
message: "Name the origins your pages upload from: api.checkBucket({ origins: [\"https://app.example.com\"] })."
|
|
274
|
+
};
|
|
275
|
+
const blocked = origins.filter((origin) => !rules.some((rule) => allowsUploads(rule, origin)));
|
|
276
|
+
if (blocked.length === 0) return {
|
|
277
|
+
status: "ok",
|
|
278
|
+
message: `Browsers on ${origins.join(", ")} can upload and read ETag.`
|
|
279
|
+
};
|
|
280
|
+
return {
|
|
281
|
+
status: "problem",
|
|
282
|
+
message: `The bucket's CORS rules do not let ${blocked.join(", ")} upload. Add a rule that allows PUT from there with the headers ${UPLOAD_HEADERS.join(", ")} and exposes ETag.`
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
function lifecycleCheck(rules, hasStore) {
|
|
286
|
+
const rule = rules.find((candidate) => candidate.enabled && candidate.prefix === "" && candidate.abortIncompleteMultipartUploadDays !== void 0);
|
|
287
|
+
if (rule) {
|
|
288
|
+
const days = rule.abortIncompleteMultipartUploadDays;
|
|
289
|
+
return {
|
|
290
|
+
status: "ok",
|
|
291
|
+
message: `Incomplete multipart uploads are aborted after ${days} day${days === 1 ? "" : "s"}.`
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
return {
|
|
295
|
+
status: "problem",
|
|
296
|
+
message: `No enabled lifecycle rule aborts incomplete multipart uploads across the bucket, so abandoned uploads keep their parts stored.${hasStore ? " With a store it is required: an upload created just before a crash has no row to clean it up." : ""} Add one (R2 creates a 7-day rule by default).`
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
function settingCheck(read, setting, check) {
|
|
300
|
+
if (read.error === null) return ok(check(read.data));
|
|
301
|
+
if (read.error.code === "STORAGE_DENIED") return ok({
|
|
302
|
+
status: "unknown",
|
|
303
|
+
message: `These storage credentials may not read the bucket's ${setting} (R2 object tokens cannot). Check it with an admin read token or in the dashboard.`
|
|
304
|
+
});
|
|
305
|
+
if (read.error.status === 501) return ok({
|
|
306
|
+
status: "unknown",
|
|
307
|
+
message: `This storage service cannot report the bucket's ${setting}. Check them where the bucket is configured (for R2, the dashboard).`
|
|
308
|
+
});
|
|
309
|
+
return fail(read.error);
|
|
310
|
+
}
|
|
311
|
+
async function checkBucket$1(config, origins) {
|
|
312
|
+
const { storage } = config;
|
|
313
|
+
const [corsRules, lifecycleRules] = await Promise.all([storage.getBucketCors(), storage.getBucketLifecycle()]);
|
|
314
|
+
const cors = settingCheck(corsRules, "CORS rules", (rules) => corsCheck(rules, origins));
|
|
315
|
+
if (cors.error) return cors;
|
|
316
|
+
const lifecycle = settingCheck(lifecycleRules, "lifecycle rules", (rules) => lifecycleCheck(rules, config.store !== void 0));
|
|
317
|
+
if (lifecycle.error) return lifecycle;
|
|
318
|
+
const privateRoutes = [...config.routes.values()].filter((route) => route.visibility === "private");
|
|
319
|
+
const publicAccess = privateRoutes.length > 0 && storage.publicUrl ? {
|
|
320
|
+
status: "problem",
|
|
321
|
+
message: `Private routes (${privateRoutes.map((route) => route.name).join(", ")}) store files on a bucket with a public URL: anyone who learns a key can read the file.`
|
|
322
|
+
} : {
|
|
323
|
+
status: "ok",
|
|
324
|
+
message: "No private route stores files on a public bucket."
|
|
325
|
+
};
|
|
326
|
+
return ok({
|
|
327
|
+
cors: cors.data,
|
|
328
|
+
lifecycle: lifecycle.data,
|
|
329
|
+
publicAccess
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
// src/env.ts
|
|
333
|
+
function fromEnv(name) {
|
|
334
|
+
return (globalThis.process?.env)?.[name] || void 0;
|
|
335
|
+
}
|
|
336
|
+
// src/storage/lazy-driver.ts
|
|
337
|
+
function readSetting(read) {
|
|
338
|
+
try {
|
|
339
|
+
return ok(read());
|
|
340
|
+
} catch (error) {
|
|
341
|
+
if (error instanceof CloudropError) return fail(error);
|
|
342
|
+
throw error;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
const builders = /* @__PURE__ */ new WeakMap();
|
|
346
|
+
function storageSettingsProblem(storage) {
|
|
347
|
+
return builders.get(storage)?.().error ?? null;
|
|
348
|
+
}
|
|
349
|
+
function lazyDriver(checked, build) {
|
|
350
|
+
let built;
|
|
351
|
+
const resolve = () => readSetting(() => built ??= build());
|
|
352
|
+
const use = async (call) => {
|
|
353
|
+
const resolved = resolve();
|
|
354
|
+
return resolved.error ? resolved : call(resolved.data);
|
|
355
|
+
};
|
|
356
|
+
const lazy = {
|
|
357
|
+
rules: checked.rules,
|
|
358
|
+
publicUrl: checked.publicUrl,
|
|
359
|
+
presignPut: (...args) => use((driver) => driver.presignPut(...args)),
|
|
360
|
+
createMultipart: (...args) => use((driver) => driver.createMultipart(...args)),
|
|
361
|
+
presignPart: (...args) => use((driver) => driver.presignPart(...args)),
|
|
362
|
+
listParts: (...args) => use((driver) => driver.listParts(...args)),
|
|
363
|
+
completeMultipart: (...args) => use((driver) => driver.completeMultipart(...args)),
|
|
364
|
+
abortMultipart: (...args) => use((driver) => driver.abortMultipart(...args)),
|
|
365
|
+
presignGet: (...args) => use((driver) => driver.presignGet(...args)),
|
|
366
|
+
getBucketCors: () => use((driver) => driver.getBucketCors()),
|
|
367
|
+
getBucketLifecycle: () => use((driver) => driver.getBucketLifecycle()),
|
|
368
|
+
head: (...args) => use((driver) => driver.head(...args)),
|
|
369
|
+
getRange: (...args) => use((driver) => driver.getRange(...args)),
|
|
370
|
+
put: (...args) => use((driver) => driver.put(...args)),
|
|
371
|
+
copy: (...args) => use((driver) => driver.copy(...args)),
|
|
372
|
+
delete: (...args) => use((driver) => driver.delete(...args)),
|
|
373
|
+
deleteMany: (...args) => use((driver) => driver.deleteMany(...args)),
|
|
374
|
+
list: (...args) => use((driver) => driver.list(...args))
|
|
375
|
+
};
|
|
376
|
+
builders.set(lazy, resolve);
|
|
377
|
+
return lazy;
|
|
378
|
+
}
|
|
379
|
+
// src/server/token.ts
|
|
380
|
+
const isMultipart = (token) => token.uploadId !== void 0;
|
|
381
|
+
const MAX_TOKEN_LENGTH = 16384;
|
|
382
|
+
const encoder$2 = new TextEncoder();
|
|
383
|
+
const decoder$1 = new TextDecoder();
|
|
384
|
+
function base64url(bytes) {
|
|
385
|
+
let binary = "";
|
|
386
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
387
|
+
return btoa(binary).replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
|
|
388
|
+
}
|
|
389
|
+
function readPayload(body) {
|
|
390
|
+
try {
|
|
391
|
+
const bytes = Uint8Array.from(atob(body.replaceAll("-", "+").replaceAll("_", "/")), (char) => char.charCodeAt(0));
|
|
392
|
+
const payload = JSON.parse(decoder$1.decode(bytes));
|
|
393
|
+
return payload?.v === 1 && Number.isSafeInteger(payload.exp) ? payload : null;
|
|
394
|
+
} catch {
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
function importTokenKey(secret) {
|
|
399
|
+
return crypto.subtle.importKey("raw", encoder$2.encode(secret), {
|
|
400
|
+
name: "HMAC",
|
|
401
|
+
hash: "SHA-256"
|
|
402
|
+
}, false, ["sign"]);
|
|
403
|
+
}
|
|
404
|
+
async function importTokenKeys([current, ...previous]) {
|
|
405
|
+
const signing = await importTokenKey(current);
|
|
406
|
+
return {
|
|
407
|
+
signing,
|
|
408
|
+
all: [signing, ...await Promise.all(previous.map(importTokenKey))]
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
async function signature(key, body) {
|
|
412
|
+
return base64url(new Uint8Array(await crypto.subtle.sign("HMAC", key, encoder$2.encode(body))));
|
|
413
|
+
}
|
|
414
|
+
function sameText(a, b) {
|
|
415
|
+
if (a.length !== b.length) return false;
|
|
416
|
+
let difference = 0;
|
|
417
|
+
for (let index = 0; index < a.length; index++) difference |= a.charCodeAt(index) ^ b.charCodeAt(index);
|
|
418
|
+
return difference === 0;
|
|
419
|
+
}
|
|
420
|
+
async function signToken(payload, key) {
|
|
421
|
+
const body = base64url(encoder$2.encode(JSON.stringify(payload)));
|
|
422
|
+
return `${body}.${await signature(key, body)}`;
|
|
423
|
+
}
|
|
424
|
+
const invalidToken = () => new CloudropError("INVALID_TOKEN", "The upload token is not valid. Start the upload again.");
|
|
425
|
+
const uploadExpired = () => new CloudropError("UPLOAD_EXPIRED", "This upload expired. Start it again to upload the file.");
|
|
426
|
+
async function verifyToken(token, keys, nowSeconds, options = {}) {
|
|
427
|
+
if (typeof token !== "string" || token.length > MAX_TOKEN_LENGTH) return fail(invalidToken());
|
|
428
|
+
const [body = "", given = "", extra] = token.split(".");
|
|
429
|
+
if (extra !== void 0) return fail(invalidToken());
|
|
430
|
+
const payload = (await Promise.all(keys.map(async (key) => sameText(await signature(key, body), given)))).includes(true) ? readPayload(body) : null;
|
|
431
|
+
if (payload === null) return fail(invalidToken());
|
|
432
|
+
if (nowSeconds >= payload.exp && !options.allowExpired) return fail(uploadExpired());
|
|
433
|
+
return ok(payload);
|
|
434
|
+
}
|
|
435
|
+
// src/server/environment.ts
|
|
436
|
+
const MIN_SECRET_LENGTH = 32;
|
|
437
|
+
function readSecrets(secret) {
|
|
438
|
+
const secrets = secret === void 0 ? [fromEnv("CLOUDROP_SECRET")].filter(Boolean) : [secret].flat();
|
|
439
|
+
if (secrets.length === 0) throw configError("Missing the upload token secret. Set CLOUDROP_SECRET or pass secret to createCloudrop(); generate one with `openssl rand -base64 32`.");
|
|
440
|
+
if (!secrets.every((value) => typeof value === "string" && value.length >= MIN_SECRET_LENGTH)) throw configError(`${secret === void 0 ? "CLOUDROP_SECRET" : "Each upload token secret"} must be text of at least 32 characters. Generate one with \`openssl rand -base64 32\`.`);
|
|
441
|
+
return secrets;
|
|
442
|
+
}
|
|
443
|
+
function environmentReader(secret, storage) {
|
|
444
|
+
let tokenKeys = secret !== void 0 || fromEnv("NODE_ENV") === "development" ? importTokenKeys(readSecrets(secret)) : void 0;
|
|
445
|
+
let resolved;
|
|
446
|
+
return async () => {
|
|
447
|
+
if (resolved) return resolved;
|
|
448
|
+
const keys = tokenKeys ? ok(tokenKeys) : readSetting(() => importTokenKeys(readSecrets(secret)));
|
|
449
|
+
if (keys.data) tokenKeys = keys.data;
|
|
450
|
+
const storageProblem = storageSettingsProblem(storage);
|
|
451
|
+
if (keys.error || storageProblem) return fail(configError([storageProblem?.message, keys.error?.message].filter(Boolean).join(" ")));
|
|
452
|
+
resolved = ok(await keys.data);
|
|
453
|
+
return resolved;
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
// src/server/accept.ts
|
|
457
|
+
const EXTENSIONS = {
|
|
458
|
+
"image/png": ["png"],
|
|
459
|
+
"image/jpeg": ["jpg", "jpeg"],
|
|
460
|
+
"image/webp": ["webp"],
|
|
461
|
+
"image/gif": ["gif"],
|
|
462
|
+
"image/avif": ["avif"],
|
|
463
|
+
"image/heic": ["heic"],
|
|
464
|
+
"image/heif": ["heif"],
|
|
465
|
+
"image/bmp": ["bmp"],
|
|
466
|
+
"image/tiff": ["tif", "tiff"],
|
|
467
|
+
"image/svg+xml": ["svg"],
|
|
468
|
+
"image/x-icon": ["ico"],
|
|
469
|
+
"image/vnd.microsoft.icon": ["ico"],
|
|
470
|
+
"video/mp4": ["mp4", "m4v"],
|
|
471
|
+
"video/webm": ["webm"],
|
|
472
|
+
"video/quicktime": ["mov"],
|
|
473
|
+
"audio/mpeg": ["mp3"],
|
|
474
|
+
"audio/mp4": ["m4a"],
|
|
475
|
+
"audio/wav": ["wav"],
|
|
476
|
+
"audio/x-wav": ["wav"],
|
|
477
|
+
"audio/ogg": ["ogg", "oga"],
|
|
478
|
+
"audio/webm": ["weba", "webm"],
|
|
479
|
+
"audio/flac": ["flac"],
|
|
480
|
+
"application/pdf": ["pdf"],
|
|
481
|
+
"application/zip": ["zip"],
|
|
482
|
+
"application/json": ["json"],
|
|
483
|
+
"application/xml": ["xml"],
|
|
484
|
+
"text/xml": ["xml"],
|
|
485
|
+
"text/plain": ["txt"],
|
|
486
|
+
"text/csv": ["csv"],
|
|
487
|
+
"text/markdown": ["md", "markdown"],
|
|
488
|
+
"text/html": ["html", "htm"],
|
|
489
|
+
"application/msword": ["doc"],
|
|
490
|
+
"application/vnd.ms-excel": ["xls"],
|
|
491
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": ["docx"],
|
|
492
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ["xlsx"],
|
|
493
|
+
"application/vnd.openxmlformats-officedocument.presentationml.presentation": ["pptx"]
|
|
494
|
+
};
|
|
495
|
+
const KNOWN_EXTENSIONS = new Set(Object.values(EXTENSIONS).flat());
|
|
496
|
+
const PLAIN_EXTENSION = /^[a-z0-9]{1,16}$/;
|
|
497
|
+
const MIME_TYPE = /^[a-z0-9][a-z0-9!#$&^_.+-]{0,63}\/[a-z0-9][a-z0-9!#$&^_.+-]{0,63}$/;
|
|
498
|
+
const ACCEPT_ENTRY = /^(\*\/\*|[a-z0-9][a-z0-9!#$&^_.+-]{0,63}\/(\*|[a-z0-9][a-z0-9!#$&^_.+-]{0,63}))$/;
|
|
499
|
+
function normalizeType(type) {
|
|
500
|
+
const lower = type === "" ? "application/octet-stream" : type.toLowerCase();
|
|
501
|
+
return MIME_TYPE.test(lower) ? lower : null;
|
|
502
|
+
}
|
|
503
|
+
function isAcceptEntry(entry) {
|
|
504
|
+
return ACCEPT_ENTRY.test(entry);
|
|
505
|
+
}
|
|
506
|
+
function extensionOf(name) {
|
|
507
|
+
const trimmed = name.replace(/\.+$/, "");
|
|
508
|
+
const dot = trimmed.lastIndexOf(".");
|
|
509
|
+
return dot > 0 ? trimmed.slice(dot + 1).toLowerCase() : null;
|
|
510
|
+
}
|
|
511
|
+
function matchAccept(accept, type, name) {
|
|
512
|
+
if (!accept.some((entry) => entry === "*/*" || entry === type || entry.endsWith("/*") && type.startsWith(entry.slice(0, -1)))) return { problem: `This upload does not accept ${type} files.` };
|
|
513
|
+
const ext = extensionOf(name);
|
|
514
|
+
const known = EXTENSIONS[type];
|
|
515
|
+
const plain = ext !== null && PLAIN_EXTENSION.test(ext);
|
|
516
|
+
if (known === void 0) return { ext: plain && !KNOWN_EXTENSIONS.has(ext) ? `.${ext}` : "" };
|
|
517
|
+
if (ext === null) return { ext: `.${known[0]}` };
|
|
518
|
+
if (known.includes(ext)) return { ext: `.${ext}` };
|
|
519
|
+
return { problem: plain ? `The file name ends in .${ext}, which does not match its type ${type}. Rename the file or pick another.` : `The file name's extension does not match its type ${type}. Rename the file or pick another.` };
|
|
520
|
+
}
|
|
521
|
+
// src/server/size.ts
|
|
522
|
+
const UNITS = {
|
|
523
|
+
B: 1,
|
|
524
|
+
KB: 1024,
|
|
525
|
+
KiB: 1024,
|
|
526
|
+
MB: MiB,
|
|
527
|
+
MiB,
|
|
528
|
+
GB: GiB,
|
|
529
|
+
GiB,
|
|
530
|
+
TB: 1024 * GiB,
|
|
531
|
+
TiB: 1024 * GiB
|
|
532
|
+
};
|
|
533
|
+
const SIZE_STRING = /^(\d+)(?:\.(\d+))?(B|KB|KiB|MB|MiB|GB|GiB|TB|TiB)$/;
|
|
534
|
+
function parseSize(size) {
|
|
535
|
+
if (typeof size === "number") return Number.isSafeInteger(size) && size >= 0 ? size + 0 : null;
|
|
536
|
+
const match = typeof size === "string" ? SIZE_STRING.exec(size) : null;
|
|
537
|
+
if (match === null) return null;
|
|
538
|
+
const fraction = match[2] ?? "";
|
|
539
|
+
const scaled = BigInt(`${match[1]}${fraction}`) * BigInt(UNITS[match[3] ?? ""]);
|
|
540
|
+
const divisor = 10n ** BigInt(fraction.length);
|
|
541
|
+
if (scaled % divisor !== 0n) return null;
|
|
542
|
+
const bytes = scaled / divisor;
|
|
543
|
+
return bytes <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(bytes) : null;
|
|
544
|
+
}
|
|
545
|
+
// src/server/route-config.ts
|
|
546
|
+
const MAX_FILES_LIMIT = 100;
|
|
547
|
+
const DEFAULT_MAX_FILES = 10;
|
|
548
|
+
const isFunction = (value) => typeof value === "function";
|
|
549
|
+
function readAccept(where, definition) {
|
|
550
|
+
const accept = [definition.accept].flat();
|
|
551
|
+
if (accept.length === 0 || !accept.every((entry) => typeof entry === "string" && isAcceptEntry(entry))) throw configError(`${where}: accept takes MIME types in lower case, such as "image/png", "image/*" or "*/*".`);
|
|
552
|
+
return accept;
|
|
553
|
+
}
|
|
554
|
+
function readMaxSize(where, definition, storage) {
|
|
555
|
+
const maxSize = parseSize(definition.maxSize);
|
|
556
|
+
const limit = storage.rules.maxObjectSize;
|
|
557
|
+
if (maxSize === null || maxSize < 1 || maxSize > limit) throw configError(`${where}: maxSize must be between 1 byte and ${formatBytes(limit)}, such as 5242880 or "5MB".`);
|
|
558
|
+
return maxSize;
|
|
559
|
+
}
|
|
560
|
+
function readMaxFiles(where, definition) {
|
|
561
|
+
const maxFiles = definition.maxFiles ?? DEFAULT_MAX_FILES;
|
|
562
|
+
if (!Number.isInteger(maxFiles) || maxFiles < 1 || maxFiles > MAX_FILES_LIMIT) throw configError(`${where}: maxFiles must be a whole number from 1 to ${MAX_FILES_LIMIT}.`);
|
|
563
|
+
return maxFiles;
|
|
564
|
+
}
|
|
565
|
+
function checkCallbacks(where, definition) {
|
|
566
|
+
if (definition.metadata !== void 0 && !isFunction(definition.metadata?.["~standard"]?.validate)) throw configError(`${where}: metadata must be a Standard Schema, such as a zod, valibot or arktype schema.`);
|
|
567
|
+
for (const option of [
|
|
568
|
+
"prefix",
|
|
569
|
+
"key",
|
|
570
|
+
"owner",
|
|
571
|
+
"onComplete",
|
|
572
|
+
"afterComplete"
|
|
573
|
+
]) if (definition[option] !== void 0 && !isFunction(definition[option])) throw configError(`${where}: ${option} must be a function.`);
|
|
574
|
+
if (definition.prefix && definition.key) throw configError(`${where}: pass prefix or key, not both. key builds the whole key; prefix only its folder.`);
|
|
575
|
+
}
|
|
576
|
+
function readVisibility(where, definition, storage) {
|
|
577
|
+
const visibility = definition.visibility ?? (storage.publicUrl ? "public" : "private");
|
|
578
|
+
if (visibility !== "public" && visibility !== "private") throw configError(`${where}: visibility must be "public" or "private".`);
|
|
579
|
+
if (visibility === "public" && !storage.publicUrl) throw configError(`${where} is public, but the storage has no publicUrl. Pass publicUrl to the storage preset, or make the route private.`);
|
|
580
|
+
return visibility;
|
|
581
|
+
}
|
|
582
|
+
function readRoute(name, route, storage) {
|
|
583
|
+
const where = `Route "${name}"`;
|
|
584
|
+
const definition = route?.definition;
|
|
585
|
+
if (typeof definition !== "object" || definition === null) throw configError(`${where} is not a route. Wrap its options in route({ ... }).`);
|
|
586
|
+
if (!isFunction(definition.authorize)) throw configError(`${where} needs authorize. Use authorize: allowAnyone for a route anyone may upload to.`);
|
|
587
|
+
const accept = readAccept(where, definition);
|
|
588
|
+
const maxSize = readMaxSize(where, definition, storage);
|
|
589
|
+
const maxFiles = readMaxFiles(where, definition);
|
|
590
|
+
checkCallbacks(where, definition);
|
|
591
|
+
return {
|
|
592
|
+
name,
|
|
593
|
+
definition,
|
|
594
|
+
accept,
|
|
595
|
+
maxSize,
|
|
596
|
+
maxFiles,
|
|
597
|
+
visibility: readVisibility(where, definition, storage)
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
// src/server/store-config.ts
|
|
601
|
+
const DEFAULT_LEASE_MS = 3e5;
|
|
602
|
+
const STORE_METHODS = [
|
|
603
|
+
"reserve",
|
|
604
|
+
"attach",
|
|
605
|
+
"get",
|
|
606
|
+
"claim",
|
|
607
|
+
"release",
|
|
608
|
+
"complete",
|
|
609
|
+
"end",
|
|
610
|
+
"revoke",
|
|
611
|
+
"stale",
|
|
612
|
+
"cleaned",
|
|
613
|
+
"usage",
|
|
614
|
+
"reconcile",
|
|
615
|
+
"owners"
|
|
616
|
+
];
|
|
617
|
+
function readStoreObject(given) {
|
|
618
|
+
if (given === void 0) return void 0;
|
|
619
|
+
const store = typeof given === "object" && given !== null ? given : {};
|
|
620
|
+
const missing = STORE_METHODS.find((method) => !isFunction(store[method]));
|
|
621
|
+
if (missing) throw configError(`store must be a CloudropStore, such as sqlStore(...) from cloudrop/adapters/sql. Its ${missing} method is missing.`);
|
|
622
|
+
if (store.transaction !== void 0 && !isFunction(store.transaction)) throw configError("store.transaction must be a function, or left out for a store without transactions.");
|
|
623
|
+
return given;
|
|
624
|
+
}
|
|
625
|
+
function checkRoutes(routes, store) {
|
|
626
|
+
for (const { name, definition } of routes) {
|
|
627
|
+
const needsStore = ["owner", "afterComplete"].find((callback) => definition[callback]);
|
|
628
|
+
if (needsStore && !store) throw configError(`Route "${name}": ${needsStore} needs a database store. Pass store to createCloudrop, or remove ${needsStore}.`);
|
|
629
|
+
const routeStore = storeOfRoute(definition);
|
|
630
|
+
if (routeStore === void 0) continue;
|
|
631
|
+
if (routeStore !== store) throw configError(`Route "${name}" was made by another store's route(), so its onComplete expects that store's tx. Pass that store to createCloudrop, or build the route with route().`);
|
|
632
|
+
if (!store?.transaction) throw configError(`Route "${name}" expects a tx in onComplete, but the store has no transaction. Build the store with transaction: true, or build the route with route().`);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
function readStoreConfig(options, routes) {
|
|
636
|
+
const store = readStoreObject(options.store);
|
|
637
|
+
const { quota, completionLeaseMs } = options;
|
|
638
|
+
if (!store && quota !== void 0) throw configError("quota needs a database store to count usage. Pass store to createCloudrop, or remove quota.");
|
|
639
|
+
if (!store && completionLeaseMs !== void 0) throw configError("completionLeaseMs needs a database store. Pass store to createCloudrop, or remove completionLeaseMs.");
|
|
640
|
+
if (quota !== void 0 && !isFunction(quota)) throw configError("quota must be a function from an owner to their limit in bytes, or null for no limit.");
|
|
641
|
+
const leaseMs = completionLeaseMs ?? DEFAULT_LEASE_MS;
|
|
642
|
+
if (!Number.isSafeInteger(leaseMs) || leaseMs < 1e3) throw configError("completionLeaseMs must be a whole number of milliseconds of at least 1000, such as 300000.");
|
|
643
|
+
checkRoutes(routes, store);
|
|
644
|
+
return {
|
|
645
|
+
store,
|
|
646
|
+
quota,
|
|
647
|
+
completionLeaseMs: leaseMs
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
function unownedRoutes(config) {
|
|
651
|
+
if (!config.quota) return [];
|
|
652
|
+
return [...config.routes.values()].filter((route) => !route.definition.owner).map((route) => route.name);
|
|
653
|
+
}
|
|
654
|
+
// src/server/config.ts
|
|
655
|
+
const ROUTE_NAME = /^[A-Za-z0-9_-]{1,64}$/;
|
|
656
|
+
const BASE_PATH = /^(\/(?!\.{1,2}(?:\/|$))[A-Za-z0-9._~-]+)+$/;
|
|
657
|
+
const PLUGIN_HOOKS = [
|
|
658
|
+
"beforeUpload",
|
|
659
|
+
"afterVerify",
|
|
660
|
+
"onAbort",
|
|
661
|
+
"extendRoute"
|
|
662
|
+
];
|
|
663
|
+
const isExactOrigin = (origin) => typeof origin === "string" && URL.canParse(origin) && new URL(origin).origin === origin;
|
|
664
|
+
function readOrigins(origins) {
|
|
665
|
+
const list = origins ?? [];
|
|
666
|
+
if (!(Array.isArray(list) && list.every(isExactOrigin))) throw configError("trustedOrigins must list exact origins such as \"https://app.example.com\": scheme and host, no path or trailing slash, and never \"*\".");
|
|
667
|
+
return new Set(list);
|
|
668
|
+
}
|
|
669
|
+
function readStorage(storage) {
|
|
670
|
+
if (!storage || !isFunction(storage.presignPut) || !Number.isSafeInteger(storage.rules?.maxObjectSize)) throw configError("Pass storage to createCloudrop: r2({ bucket }) or s3({ ... }).");
|
|
671
|
+
return storage;
|
|
672
|
+
}
|
|
673
|
+
function readRouteEntries(routes) {
|
|
674
|
+
const entries = typeof routes === "object" && !Array.isArray(routes) ? Object.entries(routes ?? {}) : [];
|
|
675
|
+
if (entries.length === 0) throw configError("Pass at least one route: routes: { avatar: route({ ... }) }.");
|
|
676
|
+
const badName = entries.find(([name]) => !ROUTE_NAME.test(name));
|
|
677
|
+
if (badName) throw configError(`Route name ${JSON.stringify(badName[0].slice(0, 64))} may use only letters, digits, - and _ (at most 64), such as avatar or post-image.`);
|
|
678
|
+
return entries;
|
|
679
|
+
}
|
|
680
|
+
function readBasePath(given) {
|
|
681
|
+
const basePath = given ?? "/api/cloudrop";
|
|
682
|
+
if (typeof basePath !== "string" || !BASE_PATH.test(basePath)) throw configError("basePath must be a path such as \"/api/cloudrop\": a leading slash, no trailing slash, no query.");
|
|
683
|
+
return basePath;
|
|
684
|
+
}
|
|
685
|
+
function readThreshold(threshold) {
|
|
686
|
+
const multipartThreshold = threshold === void 0 ? void 0 : parseSize(threshold);
|
|
687
|
+
if (multipartThreshold === null) throw configError("multipartThreshold must be a whole number of bytes or a size such as \"64MB\".");
|
|
688
|
+
return multipartThreshold;
|
|
689
|
+
}
|
|
690
|
+
function readPlugins(given) {
|
|
691
|
+
const plugins = given ?? [];
|
|
692
|
+
if (!Array.isArray(plugins)) throw configError("plugins must be a list, such as plugins: [limits()].");
|
|
693
|
+
const ids = plugins.map((plugin) => plugin?.id);
|
|
694
|
+
if (!ids.every((id) => typeof id === "string" && id !== "") || new Set(ids).size !== ids.length) throw configError("Every plugin needs its own id. Pass each plugin once.");
|
|
695
|
+
for (const { id, server } of plugins) {
|
|
696
|
+
const hooks = server ?? {};
|
|
697
|
+
if (typeof hooks !== "object" || !PLUGIN_HOOKS.every((hook) => hooks[hook] === void 0 || isFunction(hooks[hook]))) throw configError(`Plugin "${id}": server takes only functions for beforeUpload, afterVerify, onAbort and extendRoute.`);
|
|
698
|
+
}
|
|
699
|
+
return plugins;
|
|
700
|
+
}
|
|
701
|
+
function readLogger(given) {
|
|
702
|
+
const logger = given ?? console;
|
|
703
|
+
if (typeof logger?.error !== "function" || typeof logger.warn !== "function") throw configError("logger needs error(message, error) and warn(message) methods, such as console or a pino logger.");
|
|
704
|
+
return logger;
|
|
705
|
+
}
|
|
706
|
+
function extendRoute(plugin, route) {
|
|
707
|
+
try {
|
|
708
|
+
plugin.server?.extendRoute?.({
|
|
709
|
+
name: route.name,
|
|
710
|
+
definition: route.definition
|
|
711
|
+
});
|
|
712
|
+
} catch (error) {
|
|
713
|
+
if (error instanceof CloudropError) throw error;
|
|
714
|
+
throw new CloudropError("INVALID_CONFIG", `Plugin "${plugin.id}": extendRoute failed for route "${route.name}". Its cause says why; fix the plugin or the route.`, { cause: error });
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
function announce(config) {
|
|
718
|
+
const { routes, plugins, storage, logger } = config;
|
|
719
|
+
const unowned = unownedRoutes(config);
|
|
720
|
+
if (unowned.length > 0) logger.warn(`cloudrop: quota is set, but routes ${unowned.join(", ")} define no owner, so their uploads count against no limit. Add owner to those routes.`);
|
|
721
|
+
for (const route of routes.values()) {
|
|
722
|
+
for (const plugin of plugins) extendRoute(plugin, route);
|
|
723
|
+
if (route.visibility === "private" && storage.publicUrl) logger.warn(`cloudrop: route "${route.name}" is private, but the storage has a publicUrl, so anyone who learns a key can read the file. Serve private files from a bucket without a public domain.`);
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
function readConfig(given) {
|
|
727
|
+
const options = given ?? {};
|
|
728
|
+
const storage = readStorage(options.storage);
|
|
729
|
+
const entries = readRouteEntries(options.routes);
|
|
730
|
+
const basePath = readBasePath(options.basePath);
|
|
731
|
+
const multipartThreshold = readThreshold(options.multipartThreshold);
|
|
732
|
+
const plugins = readPlugins(options.plugins);
|
|
733
|
+
const logger = readLogger(options.logger);
|
|
734
|
+
const ready = environmentReader(options.secret, storage);
|
|
735
|
+
const trustedOrigins = readOrigins(options.trustedOrigins);
|
|
736
|
+
const routes = new Map(entries.map(([name, route]) => [name, readRoute(name, route, storage)]));
|
|
737
|
+
const config = {
|
|
738
|
+
storage,
|
|
739
|
+
routes,
|
|
740
|
+
ready,
|
|
741
|
+
basePath,
|
|
742
|
+
trustedOrigins,
|
|
743
|
+
multipartThreshold,
|
|
744
|
+
plugins,
|
|
745
|
+
logger,
|
|
746
|
+
...readStoreConfig(options, [...routes.values()])
|
|
747
|
+
};
|
|
748
|
+
announce(config);
|
|
749
|
+
return config;
|
|
750
|
+
}
|
|
751
|
+
// src/server/key.ts
|
|
752
|
+
const CROCKFORD = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
753
|
+
function ulid(nowMs, random) {
|
|
754
|
+
let time = "";
|
|
755
|
+
for (let rest = nowMs, index = 0; index < 10; index++, rest = Math.floor(rest / 32)) time = CROCKFORD.charAt(rest % 32) + time;
|
|
756
|
+
let tail = "";
|
|
757
|
+
let bits = 0;
|
|
758
|
+
let value = 0;
|
|
759
|
+
for (const byte of random) {
|
|
760
|
+
value = value << 8 | byte;
|
|
761
|
+
bits += 8;
|
|
762
|
+
while (bits >= 5) {
|
|
763
|
+
bits -= 5;
|
|
764
|
+
tail += CROCKFORD.charAt(value >>> bits & 31);
|
|
765
|
+
}
|
|
766
|
+
value &= (1 << bits) - 1;
|
|
767
|
+
}
|
|
768
|
+
return time + tail;
|
|
769
|
+
}
|
|
770
|
+
function newFileId() {
|
|
771
|
+
return ulid(Date.now(), crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(10)));
|
|
772
|
+
}
|
|
773
|
+
function generatedKey(prefix, id, ext) {
|
|
774
|
+
const folder = prefix.replace(/\/+$/, "");
|
|
775
|
+
return folder === "" ? `${id}${ext}` : `${folder}/${id}${ext}`;
|
|
776
|
+
}
|
|
777
|
+
function normalizeKey(key) {
|
|
778
|
+
if (typeof key !== "string") return { problem: "The object key must be a string." };
|
|
779
|
+
const normalized = key.normalize("NFC");
|
|
780
|
+
const problem = keyProblem(normalized) ?? (normalized.split("/").includes("") ? "The object key has an empty segment (a doubled or trailing slash). Remove it." : null);
|
|
781
|
+
return problem === null ? { key: normalized } : { problem };
|
|
782
|
+
}
|
|
783
|
+
const INLINE = /^(?:image\/(?!svg\+xml$)|video\/|audio\/)[a-z0-9][a-z0-9!#$&^_.+-]*$/;
|
|
784
|
+
function contentDisposition(type, name) {
|
|
785
|
+
const fallback = name.replace(/[^\x20-\x7e]|["\\]/g, "_");
|
|
786
|
+
return `${INLINE.test(type) ? "inline" : "attachment"}; filename="${fallback}"; filename*=UTF-8''${encodeRfc3986(name)}`;
|
|
787
|
+
}
|
|
788
|
+
// src/plan.ts
|
|
789
|
+
const DEFAULT_MULTIPART_THRESHOLD = 64 * MiB;
|
|
790
|
+
const FIRST_PART_SIZE = 8 * MiB;
|
|
791
|
+
const LARGE_FILE_SIZE = 256 * MiB;
|
|
792
|
+
const LARGE_FIRST_PART_SIZE = 16 * MiB;
|
|
793
|
+
function assertStorable(size, rules) {
|
|
794
|
+
if (!Number.isSafeInteger(size) || size < 0) throw new RangeError(`File size must be a whole number of bytes, got ${size}.`);
|
|
795
|
+
if (size > rules.maxObjectSize) throw new RangeError(`File size ${size} exceeds the storage limit of ${rules.maxObjectSize} bytes.`);
|
|
796
|
+
}
|
|
797
|
+
function planParts(size, rules) {
|
|
798
|
+
assertStorable(size, rules);
|
|
799
|
+
if (size === 0) throw new RangeError("An empty file has no parts. Upload it with a single PUT.");
|
|
800
|
+
let partSize = Math.max(size >= LARGE_FILE_SIZE ? LARGE_FIRST_PART_SIZE : FIRST_PART_SIZE, rules.minPartSize);
|
|
801
|
+
while (countParts(size, partSize) > rules.maxParts) partSize = Math.min(partSize * 2, rules.maxPartSize);
|
|
802
|
+
const partCount = countParts(size, partSize);
|
|
803
|
+
return {
|
|
804
|
+
kind: "multipart",
|
|
805
|
+
size,
|
|
806
|
+
partSize,
|
|
807
|
+
partCount,
|
|
808
|
+
lastPartSize: sizeOfPart(size, partSize, partCount)
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
function planUpload(size, rules, options = {}) {
|
|
812
|
+
assertStorable(size, rules);
|
|
813
|
+
const threshold = Math.min(options.multipartThreshold ?? DEFAULT_MULTIPART_THRESHOLD, rules.maxSinglePutSize);
|
|
814
|
+
if (size === 0 || size <= threshold) return {
|
|
815
|
+
kind: "single",
|
|
816
|
+
size
|
|
817
|
+
};
|
|
818
|
+
return planParts(size, rules);
|
|
819
|
+
}
|
|
820
|
+
// src/storage/xml.ts
|
|
821
|
+
const ENTITIES = {
|
|
822
|
+
amp: "&",
|
|
823
|
+
lt: "<",
|
|
824
|
+
gt: ">",
|
|
825
|
+
quot: "\"",
|
|
826
|
+
apos: "'"
|
|
827
|
+
};
|
|
828
|
+
const ESCAPES = {
|
|
829
|
+
"<": "<",
|
|
830
|
+
">": ">",
|
|
831
|
+
"&": "&",
|
|
832
|
+
"\"": """,
|
|
833
|
+
"'": "'"
|
|
834
|
+
};
|
|
835
|
+
function decodeXml(text) {
|
|
836
|
+
return text.replace(/&(#x[0-9a-fA-F]+|#\d+|amp|lt|gt|quot|apos);/g, (match, entity) => {
|
|
837
|
+
if (!entity.startsWith("#")) return ENTITIES[entity] ?? "";
|
|
838
|
+
const codePoint = entity[1] === "x" ? Number.parseInt(entity.slice(2), 16) : Number.parseInt(entity.slice(1), 10);
|
|
839
|
+
return Number.isFinite(codePoint) && codePoint <= 1114111 ? String.fromCodePoint(codePoint) : match;
|
|
840
|
+
});
|
|
841
|
+
}
|
|
842
|
+
function xmlBlocks(xml, tag) {
|
|
843
|
+
const pattern = new RegExp(`<${tag}(?:\\s[^>]*)?(?<!/)>([\\s\\S]*?)</${tag}>`, "g");
|
|
844
|
+
return Array.from(xml.matchAll(pattern), (match) => match[1] ?? "");
|
|
845
|
+
}
|
|
846
|
+
function xmlText(xml, tag) {
|
|
847
|
+
const block = xmlBlocks(xml, tag)[0];
|
|
848
|
+
return block === void 0 ? void 0 : decodeXml(block);
|
|
849
|
+
}
|
|
850
|
+
function xmlValue(xml, tag) {
|
|
851
|
+
return xmlText(xml, tag)?.trim();
|
|
852
|
+
}
|
|
853
|
+
function xmlValues(xml, tag) {
|
|
854
|
+
return xmlBlocks(xml, tag).map((block) => decodeXml(block).trim());
|
|
855
|
+
}
|
|
856
|
+
function stripQuotes(value) {
|
|
857
|
+
return value.replaceAll("\"", "");
|
|
858
|
+
}
|
|
859
|
+
function escapeXml(value) {
|
|
860
|
+
return value.replace(/[<>&"']/g, (char) => ESCAPES[char] ?? char);
|
|
861
|
+
}
|
|
862
|
+
// src/storage/s3/responses.ts
|
|
863
|
+
const etagIn = (xml) => stripQuotes(xmlValue(xml, "ETag") ?? "");
|
|
864
|
+
function incompleteResponse(missing) {
|
|
865
|
+
return new CloudropError("STORAGE_ERROR", `The storage service's response had no ${missing}. Retry the request.`, { retryable: true });
|
|
866
|
+
}
|
|
867
|
+
function wholeNumber(text) {
|
|
868
|
+
if (text == null || !/^\d+$/.test(text)) return void 0;
|
|
869
|
+
const value = Number(text);
|
|
870
|
+
return Number.isSafeInteger(value) ? value : void 0;
|
|
871
|
+
}
|
|
872
|
+
function validDate(text) {
|
|
873
|
+
const date = new Date(text ?? NaN);
|
|
874
|
+
return Number.isNaN(date.getTime()) ? void 0 : date;
|
|
875
|
+
}
|
|
876
|
+
function allOk(results) {
|
|
877
|
+
const values = [];
|
|
878
|
+
for (const result of results) {
|
|
879
|
+
if (result.error) return result;
|
|
880
|
+
values.push(result.data);
|
|
881
|
+
}
|
|
882
|
+
return ok(values);
|
|
883
|
+
}
|
|
884
|
+
// src/server/presign.ts
|
|
885
|
+
const TOKEN_LIFETIME_SECONDS = 86400;
|
|
886
|
+
const nowSeconds = () => Math.floor(Date.now() / 1e3);
|
|
887
|
+
function urlLifetime(token) {
|
|
888
|
+
const seconds = Math.min(900, token.exp - Math.ceil(Date.now() / 1e3));
|
|
889
|
+
return seconds > 0 ? seconds : null;
|
|
890
|
+
}
|
|
891
|
+
function toWire(signed) {
|
|
892
|
+
return {
|
|
893
|
+
url: signed.url,
|
|
894
|
+
headers: signed.headers,
|
|
895
|
+
expiresAt: signed.expiresAt.toISOString()
|
|
896
|
+
};
|
|
897
|
+
}
|
|
898
|
+
function objectOptions(token) {
|
|
899
|
+
return {
|
|
900
|
+
type: token.type,
|
|
901
|
+
disposition: contentDisposition(token.type, token.name),
|
|
902
|
+
metadata: { "cloudrop-id": token.id }
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
async function presignSingle(storage, token) {
|
|
906
|
+
const expiresIn = urlLifetime(token);
|
|
907
|
+
if (expiresIn === null) return fail(uploadExpired());
|
|
908
|
+
const signed = await storage.presignPut(token.key, {
|
|
909
|
+
...objectOptions(token),
|
|
910
|
+
size: token.size,
|
|
911
|
+
ifNoneMatch: true,
|
|
912
|
+
expiresIn
|
|
913
|
+
});
|
|
914
|
+
return signed.error ? signed : ok(toWire(signed.data));
|
|
915
|
+
}
|
|
916
|
+
async function presignParts(storage, token, partNumbers) {
|
|
917
|
+
const expiresIn = urlLifetime(token);
|
|
918
|
+
if (expiresIn === null) return fail(uploadExpired());
|
|
919
|
+
return allOk(await Promise.all(partNumbers.map(async (partNumber) => {
|
|
920
|
+
const result = await storage.presignPart(token.key, token.uploadId, partNumber, {
|
|
921
|
+
size: sizeOfPart(token.size, token.partSize, partNumber),
|
|
922
|
+
expiresIn
|
|
923
|
+
});
|
|
924
|
+
return result.error ? result : ok({
|
|
925
|
+
partNumber,
|
|
926
|
+
...toWire(result.data)
|
|
927
|
+
});
|
|
928
|
+
})));
|
|
929
|
+
}
|
|
930
|
+
// src/server/store-calls.ts
|
|
931
|
+
const storeUnavailable = () => new CloudropError("SERVER_ERROR", "The server could not reach its upload records. Try again in a moment.", { retryable: true });
|
|
932
|
+
const cancelled = () => new CloudropError("UPLOAD_EXPIRED", "This upload was cancelled or has expired. Start it again.");
|
|
933
|
+
async function storeCall(logger, what, call) {
|
|
934
|
+
try {
|
|
935
|
+
return ok(await call());
|
|
936
|
+
} catch (error) {
|
|
937
|
+
logger.error(`cloudrop: the store failed to ${what}.`, error);
|
|
938
|
+
return fail(storeUnavailable());
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
function fileCalls(store, logger, token) {
|
|
942
|
+
const call = (method, run) => storeCall(logger, `${method} file ${token.id} on route "${token.route}"`, run);
|
|
943
|
+
const { id } = token;
|
|
944
|
+
return {
|
|
945
|
+
get: () => call("get", () => store.get(id)),
|
|
946
|
+
claim: (claim, leaseMs) => call("claim", () => store.claim(id, claim, /* @__PURE__ */ new Date(), leaseMs)),
|
|
947
|
+
release: (claim) => call("release", () => store.release(id, claim, /* @__PURE__ */ new Date())),
|
|
948
|
+
complete: (claim, result) => call("complete", () => store.complete(id, claim, result, /* @__PURE__ */ new Date())),
|
|
949
|
+
end: (from, to) => call("end", () => store.end(id, from, to, /* @__PURE__ */ new Date()))
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
// src/server/plugins.ts
|
|
953
|
+
function refusal(plugin, hook, value) {
|
|
954
|
+
if (value === void 0 || value instanceof CloudropError) return value;
|
|
955
|
+
throw new TypeError(`Plugin "${plugin.id}": ${hook} must return nothing or a CloudropError from the same cloudrop the server uses. Install cloudrop once, so the plugin and the server share it.`);
|
|
956
|
+
}
|
|
957
|
+
function abortContexts(shared, files, reason) {
|
|
958
|
+
const { route, metadata, auth } = shared;
|
|
959
|
+
return files.map((file) => ({
|
|
960
|
+
route,
|
|
961
|
+
file,
|
|
962
|
+
metadata,
|
|
963
|
+
auth,
|
|
964
|
+
reason
|
|
965
|
+
}));
|
|
966
|
+
}
|
|
967
|
+
async function runOnAbort(plugins, contexts, logger) {
|
|
968
|
+
for (const plugin of plugins) for (const context of contexts) try {
|
|
969
|
+
await plugin.server?.onAbort?.(context);
|
|
970
|
+
} catch (error) {
|
|
971
|
+
logger.error(`cloudrop: plugin "${plugin.id}" failed in onAbort for file ${context.file.id}.`, error);
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
async function runBeforeUpload(plugins, context, logger) {
|
|
975
|
+
for (const [index, plugin] of plugins.entries()) {
|
|
976
|
+
const release = (reason) => runOnAbort(plugins.slice(0, index), abortContexts(context, context.files, reason), logger);
|
|
977
|
+
let refused;
|
|
978
|
+
try {
|
|
979
|
+
refused = refusal(plugin, "beforeUpload", await plugin.server?.beforeUpload?.(context));
|
|
980
|
+
} catch (error) {
|
|
981
|
+
await release("failed");
|
|
982
|
+
throw error;
|
|
983
|
+
}
|
|
984
|
+
if (refused) {
|
|
985
|
+
await release("rejected");
|
|
986
|
+
return refused;
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
async function runAfterVerify(plugins, context) {
|
|
991
|
+
for (const plugin of plugins) {
|
|
992
|
+
const refused = refusal(plugin, "afterVerify", await plugin.server?.afterVerify?.(context));
|
|
993
|
+
if (refused) return refused;
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
async function readOwner(route, context) {
|
|
997
|
+
if (!route.definition.owner) return null;
|
|
998
|
+
const owner = await route.definition.owner(context);
|
|
999
|
+
if (owner === null) return null;
|
|
1000
|
+
if (typeof owner !== "string" || owner.length < 1 || owner.length > 255) throw new TypeError(`Route "${route.name}": owner must return null or a string of 1 to 255 characters.`);
|
|
1001
|
+
return owner;
|
|
1002
|
+
}
|
|
1003
|
+
const quotaUnavailable = () => new CloudropError("SERVER_ERROR", "The server could not check your storage limit. Try again in a moment.", { retryable: true });
|
|
1004
|
+
async function readLimit(config, route, owner) {
|
|
1005
|
+
if (owner === null || !config.quota) return ok(null);
|
|
1006
|
+
try {
|
|
1007
|
+
const limit = await config.quota(owner);
|
|
1008
|
+
if (limit === null || Number.isSafeInteger(limit) && limit >= 0) return ok(limit);
|
|
1009
|
+
throw new TypeError(`quota must return null or a whole number of bytes, got ${String(limit)}.`);
|
|
1010
|
+
} catch (error) {
|
|
1011
|
+
config.logger.error(`cloudrop: the quota lookup failed for an upload on route "${route.name}".`, error);
|
|
1012
|
+
return fail(quotaUnavailable());
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
async function reserveFiles(config, input) {
|
|
1016
|
+
const { store } = config;
|
|
1017
|
+
const { route, owner, files } = input;
|
|
1018
|
+
if (!store || files.length === 0) return /* @__PURE__ */ new Map();
|
|
1019
|
+
const everyFile = (error) => new Map(files.map((file) => [file.id, error]));
|
|
1020
|
+
const limit = await readLimit(config, route, owner);
|
|
1021
|
+
if (limit.error) return everyFile(limit.error);
|
|
1022
|
+
const metadata = input.metadataJson ?? null;
|
|
1023
|
+
const rows = files.map(({ id, key, name, type, size }) => ({
|
|
1024
|
+
id,
|
|
1025
|
+
route: route.name,
|
|
1026
|
+
key,
|
|
1027
|
+
name,
|
|
1028
|
+
type,
|
|
1029
|
+
size,
|
|
1030
|
+
metadata
|
|
1031
|
+
}));
|
|
1032
|
+
const expiresAt = /* @__PURE__ */ new Date(input.exp * 1e3);
|
|
1033
|
+
const requestKey = owner === null ? null : input.requestKey;
|
|
1034
|
+
const reserved = await storeCall(config.logger, `reserve the files of an upload on route "${route.name}"`, () => store.reserve({
|
|
1035
|
+
owner,
|
|
1036
|
+
limit: limit.data,
|
|
1037
|
+
expiresAt,
|
|
1038
|
+
requestKey,
|
|
1039
|
+
files: rows
|
|
1040
|
+
}, /* @__PURE__ */ new Date()));
|
|
1041
|
+
if (reserved.error) return everyFile(reserved.error);
|
|
1042
|
+
const refused = files.filter((_, index) => reserved.data[index] !== "reserved");
|
|
1043
|
+
return new Map(refused.map((file) => [file.id, quotaExceeded()]));
|
|
1044
|
+
}
|
|
1045
|
+
async function attachUpload(config, store, token) {
|
|
1046
|
+
const attached = await storeCall(config.logger, `attach the upload of file ${token.id} on route "${token.route}"`, () => store.attach(token.id, token.uploadId, /* @__PURE__ */ new Date()));
|
|
1047
|
+
if (attached.error) return attached.error;
|
|
1048
|
+
return attached.data ? null : cancelled();
|
|
1049
|
+
}
|
|
1050
|
+
async function releaseUnstarted(config, shared, unstarted, refusals) {
|
|
1051
|
+
const { store, plugins, logger } = config;
|
|
1052
|
+
const overQuota = unstarted.filter((file) => refusals.get(file.id)?.code === "QUOTA_EXCEEDED");
|
|
1053
|
+
const failed = unstarted.filter((file) => !overQuota.includes(file));
|
|
1054
|
+
if (store) {
|
|
1055
|
+
const reserved = failed.filter(({ id }) => !refusals.has(id));
|
|
1056
|
+
await Promise.all(reserved.map((file) => storeCall(logger, `end the unstarted file ${file.id}`, () => store.end(file.id, "pending", "failed", /* @__PURE__ */ new Date()))));
|
|
1057
|
+
}
|
|
1058
|
+
await runOnAbort(plugins, abortContexts(shared, overQuota, "rejected"), logger);
|
|
1059
|
+
await runOnAbort(plugins, abortContexts(shared, failed, "failed"), logger);
|
|
1060
|
+
}
|
|
1061
|
+
// src/server/sweep.ts
|
|
1062
|
+
const DEFAULT_LIMIT = 100;
|
|
1063
|
+
const MAX_LIMIT = 1e3;
|
|
1064
|
+
const DEFAULT_GRACE_MS = 36e5;
|
|
1065
|
+
const MIN_GRACE_MS = 9e5;
|
|
1066
|
+
const CURSOR = /^(expire|cleanup):((?:\d{1,15}:.+)?)$/s;
|
|
1067
|
+
function readOptions(given) {
|
|
1068
|
+
const options = given ?? {};
|
|
1069
|
+
if (!isPlainObject(options)) return fail(invalidInput("sweep takes options such as { limit: 100 }."));
|
|
1070
|
+
const { limit = DEFAULT_LIMIT, graceMs = DEFAULT_GRACE_MS, cursor } = options;
|
|
1071
|
+
if (typeof limit !== "number" || !Number.isInteger(limit) || limit < 1 || limit > MAX_LIMIT) return fail(invalidInput(`limit must be a whole number from 1 to ${MAX_LIMIT}.`));
|
|
1072
|
+
if (typeof graceMs !== "number" || !Number.isSafeInteger(graceMs) || graceMs < MIN_GRACE_MS) return fail(invalidInput(`graceMs must be at least ${MIN_GRACE_MS} (15 minutes), the life of an upload URL, so no upload can start after a row's deadline once it may be cleaned up.`));
|
|
1073
|
+
const match = cursor === void 0 ? [
|
|
1074
|
+
"",
|
|
1075
|
+
"expire",
|
|
1076
|
+
""
|
|
1077
|
+
] : typeof cursor === "string" ? CURSOR.exec(cursor) : null;
|
|
1078
|
+
if (!match) return fail(invalidInput("cursor must be one an earlier sweep returned. Leave it out to start over."));
|
|
1079
|
+
return ok({
|
|
1080
|
+
limit,
|
|
1081
|
+
graceMs,
|
|
1082
|
+
pass: match[1],
|
|
1083
|
+
cursor: match[2] || null
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1086
|
+
async function removeLeftovers(config, record) {
|
|
1087
|
+
const { storage } = config;
|
|
1088
|
+
if (record.uploadId && record.completedAt === null) {
|
|
1089
|
+
const aborted = await storage.abortMultipart(record.key, record.uploadId);
|
|
1090
|
+
if (aborted.error) return aborted;
|
|
1091
|
+
}
|
|
1092
|
+
const head = await storage.head(record.key);
|
|
1093
|
+
if (head.error) return head;
|
|
1094
|
+
if (head.data?.metadata["cloudrop-id"] !== record.id) return ok(void 0);
|
|
1095
|
+
return storage.delete(record.key);
|
|
1096
|
+
}
|
|
1097
|
+
async function removeOrThrow(config, record) {
|
|
1098
|
+
const removed = await removeLeftovers(config, record);
|
|
1099
|
+
if (removed.error) throw removed.error;
|
|
1100
|
+
}
|
|
1101
|
+
async function expire({ config, store, report }, record) {
|
|
1102
|
+
if (record.status === "completing") {
|
|
1103
|
+
const ended = await store.end(record.id, "completing", "failed", /* @__PURE__ */ new Date(), { cleaned: true });
|
|
1104
|
+
if (ended.changed) report.reported.push(ended.record);
|
|
1105
|
+
return;
|
|
1106
|
+
}
|
|
1107
|
+
const ended = await store.end(record.id, "pending", "expired", /* @__PURE__ */ new Date());
|
|
1108
|
+
if (!ended.changed) return;
|
|
1109
|
+
await removeOrThrow(config, ended.record);
|
|
1110
|
+
await store.cleaned(record.id, /* @__PURE__ */ new Date());
|
|
1111
|
+
report.expired += 1;
|
|
1112
|
+
}
|
|
1113
|
+
async function cleanUp({ config, store, report }, record) {
|
|
1114
|
+
await removeOrThrow(config, record);
|
|
1115
|
+
await store.cleaned(record.id, /* @__PURE__ */ new Date());
|
|
1116
|
+
report.cleaned += 1;
|
|
1117
|
+
}
|
|
1118
|
+
async function sweepPage(sweeper, pass, page) {
|
|
1119
|
+
const { config, store, report } = sweeper;
|
|
1120
|
+
const claimedBefore = new Date(Date.now() - config.completionLeaseMs);
|
|
1121
|
+
const found = await storeCall(config.logger, `find ${pass} work`, () => store.stale({
|
|
1122
|
+
kind: pass,
|
|
1123
|
+
before: new Date(page.before),
|
|
1124
|
+
claimedBefore,
|
|
1125
|
+
limit: page.limit,
|
|
1126
|
+
cursor: page.cursor
|
|
1127
|
+
}));
|
|
1128
|
+
if (found.error) return found;
|
|
1129
|
+
for (const record of found.data.records) try {
|
|
1130
|
+
await (pass === "expire" ? expire(sweeper, record) : cleanUp(sweeper, record));
|
|
1131
|
+
} catch (error) {
|
|
1132
|
+
config.logger.error(`cloudrop: the sweep could not finish file ${record.id}. The next sweep tries again.`, error);
|
|
1133
|
+
report.failed += 1;
|
|
1134
|
+
}
|
|
1135
|
+
return ok({
|
|
1136
|
+
handled: found.data.records.length,
|
|
1137
|
+
cursor: found.data.cursor
|
|
1138
|
+
});
|
|
1139
|
+
}
|
|
1140
|
+
const stoppedAt = (report, cursor) => ok({
|
|
1141
|
+
...report,
|
|
1142
|
+
cursor
|
|
1143
|
+
});
|
|
1144
|
+
function listingFailed(report, handled, error) {
|
|
1145
|
+
if (handled === 0) return fail(error);
|
|
1146
|
+
return stoppedAt({
|
|
1147
|
+
...report,
|
|
1148
|
+
failed: report.failed + 1
|
|
1149
|
+
}, null);
|
|
1150
|
+
}
|
|
1151
|
+
async function sweep(config, store, given) {
|
|
1152
|
+
const options = readOptions(given);
|
|
1153
|
+
if (options.error) return options;
|
|
1154
|
+
const { limit, graceMs, pass } = options.data;
|
|
1155
|
+
const report = {
|
|
1156
|
+
expired: 0,
|
|
1157
|
+
cleaned: 0,
|
|
1158
|
+
failed: 0,
|
|
1159
|
+
reported: [],
|
|
1160
|
+
cursor: null
|
|
1161
|
+
};
|
|
1162
|
+
const sweeper = {
|
|
1163
|
+
config,
|
|
1164
|
+
store,
|
|
1165
|
+
report
|
|
1166
|
+
};
|
|
1167
|
+
const now = Date.now();
|
|
1168
|
+
let budget = limit;
|
|
1169
|
+
let cursor = options.data.cursor;
|
|
1170
|
+
if (pass === "expire") {
|
|
1171
|
+
const page = await sweepPage(sweeper, "expire", {
|
|
1172
|
+
before: now - graceMs,
|
|
1173
|
+
limit,
|
|
1174
|
+
cursor
|
|
1175
|
+
});
|
|
1176
|
+
if (page.error) return fail(page.error);
|
|
1177
|
+
if (page.data.cursor !== null) return stoppedAt(report, `expire:${page.data.cursor}`);
|
|
1178
|
+
budget -= page.data.handled;
|
|
1179
|
+
cursor = null;
|
|
1180
|
+
if (budget === 0) return stoppedAt(report, "cleanup:");
|
|
1181
|
+
}
|
|
1182
|
+
const page = await sweepPage(sweeper, "cleanup", {
|
|
1183
|
+
before: now - graceMs,
|
|
1184
|
+
limit: budget,
|
|
1185
|
+
cursor
|
|
1186
|
+
});
|
|
1187
|
+
if (page.error) return listingFailed(report, limit - budget, page.error);
|
|
1188
|
+
return stoppedAt(report, page.data.cursor === null ? null : `cleanup:${page.data.cursor}`);
|
|
1189
|
+
}
|
|
1190
|
+
// src/server/store-api.ts
|
|
1191
|
+
const OWNERS_PAGE = 100;
|
|
1192
|
+
const isOwnerOrId = (value) => typeof value === "string" && value !== "" && value.length <= 255;
|
|
1193
|
+
const badId = () => fail(invalidInput("Pass the file id, as returned with the uploaded file."));
|
|
1194
|
+
const badOwner = () => fail(invalidInput(`Pass the owner, a string of 1 to 255 characters, as owner() returned it.`));
|
|
1195
|
+
const notCompleted = () => new CloudropError("NOT_FOUND", "No completed file has this id. deleteFile deletes only files whose upload completed.");
|
|
1196
|
+
async function deleteFile(config, store, id) {
|
|
1197
|
+
const ended = await storeCall(config.logger, `delete file ${id}`, () => store.end(id, "completed", "deleted", /* @__PURE__ */ new Date()));
|
|
1198
|
+
if (ended.error) return ended;
|
|
1199
|
+
const { changed, record } = ended.data;
|
|
1200
|
+
if (record?.status !== "deleted") return fail(notCompleted());
|
|
1201
|
+
if (!changed && record.cleanedAt !== null) return ok({ id });
|
|
1202
|
+
const removed = await removeLeftovers(config, record);
|
|
1203
|
+
if (removed.error) return removed;
|
|
1204
|
+
const marked = await storeCall(config.logger, `mark file ${id} cleaned`, () => store.cleaned(id, /* @__PURE__ */ new Date()));
|
|
1205
|
+
return marked.error ? marked : ok({ id });
|
|
1206
|
+
}
|
|
1207
|
+
async function reconcile(config, store, given) {
|
|
1208
|
+
const options = given ?? {};
|
|
1209
|
+
if (!isPlainObject(options)) return fail(invalidInput("reconcile takes { owner } or nothing."));
|
|
1210
|
+
const repair = (owner) => storeCall(config.logger, `reconcile owner ${owner}`, () => store.reconcile(owner));
|
|
1211
|
+
if (options.owner !== void 0) {
|
|
1212
|
+
if (!isOwnerOrId(options.owner)) return badOwner();
|
|
1213
|
+
const repaired = await repair(options.owner);
|
|
1214
|
+
return repaired.error ? repaired : ok({
|
|
1215
|
+
checked: 1,
|
|
1216
|
+
repaired: Number(repaired.data)
|
|
1217
|
+
});
|
|
1218
|
+
}
|
|
1219
|
+
const totals = {
|
|
1220
|
+
checked: 0,
|
|
1221
|
+
repaired: 0
|
|
1222
|
+
};
|
|
1223
|
+
let cursor = null;
|
|
1224
|
+
do {
|
|
1225
|
+
const page = await storeCall(config.logger, "list owners", () => store.owners(cursor, OWNERS_PAGE));
|
|
1226
|
+
if (page.error) return page;
|
|
1227
|
+
for (const owner of page.data.owners) {
|
|
1228
|
+
const repaired = await repair(owner);
|
|
1229
|
+
if (repaired.error) return repaired;
|
|
1230
|
+
totals.checked += 1;
|
|
1231
|
+
totals.repaired += Number(repaired.data);
|
|
1232
|
+
}
|
|
1233
|
+
cursor = page.data.cursor;
|
|
1234
|
+
} while (cursor !== null);
|
|
1235
|
+
return ok(totals);
|
|
1236
|
+
}
|
|
1237
|
+
function storeApi(config, whenReady) {
|
|
1238
|
+
const { logger } = config;
|
|
1239
|
+
const withStore = (name, call) => whenReady(() => config.store ? call(config.store) : Promise.resolve(fail(configError(`api.${name} needs a database store. Pass store to createCloudrop.`))));
|
|
1240
|
+
return {
|
|
1241
|
+
file: (id) => withStore("file", async (store) => isOwnerOrId(id) ? storeCall(logger, `get file ${id}`, () => store.get(id)) : badId()),
|
|
1242
|
+
usage: (owner) => withStore("usage", async (store) => isOwnerOrId(owner) ? storeCall(logger, `read the usage of ${owner}`, () => store.usage(owner)) : badOwner()),
|
|
1243
|
+
revokeUploads: (options) => withStore("revokeUploads", async (store) => {
|
|
1244
|
+
const owner = isPlainObject(options) ? options.owner : void 0;
|
|
1245
|
+
if (!isOwnerOrId(owner)) return badOwner();
|
|
1246
|
+
const revoked = await storeCall(logger, `revoke the uploads of ${owner}`, () => store.revoke(owner, /* @__PURE__ */ new Date()));
|
|
1247
|
+
return revoked.error ? revoked : ok({ revoked: revoked.data });
|
|
1248
|
+
}),
|
|
1249
|
+
deleteFile: (id) => withStore("deleteFile", async (store) => isOwnerOrId(id) ? deleteFile(config, store, id) : badId()),
|
|
1250
|
+
reconcile: (options) => withStore("reconcile", (store) => reconcile(config, store, options)),
|
|
1251
|
+
sweep: (options) => withStore("sweep", (store) => sweep(config, store, options))
|
|
1252
|
+
};
|
|
1253
|
+
}
|
|
1254
|
+
// src/server/api.ts
|
|
1255
|
+
const DEFAULT_URL_WINDOW_SECONDS = 3600;
|
|
1256
|
+
const MAX_DELETE_PAGES = 1e4;
|
|
1257
|
+
async function getUrl(storage, key, given) {
|
|
1258
|
+
const options = given ?? {};
|
|
1259
|
+
if (!isPlainObject(options)) return fail(invalidInput("getUrl options must be an object, such as { expiresIn: 3600 }."));
|
|
1260
|
+
const { expiresIn, download } = options;
|
|
1261
|
+
if (storage.publicUrl && expiresIn === void 0 && download === void 0) {
|
|
1262
|
+
const problem = keyProblem(key);
|
|
1263
|
+
return problem ? fail(invalidInput(problem)) : ok({
|
|
1264
|
+
url: `${storage.publicUrl}/${encodeKeyPath(key)}`,
|
|
1265
|
+
expiresAt: null
|
|
1266
|
+
});
|
|
1267
|
+
}
|
|
1268
|
+
const window = expiresIn === void 0 ? DEFAULT_URL_WINDOW_SECONDS : expiresIn;
|
|
1269
|
+
const maxWindow = Math.floor(storage.rules.maxPresignSeconds / 2);
|
|
1270
|
+
if (typeof window !== "number" || !Number.isInteger(window) || window < 1 || window > maxWindow) return fail(invalidInput(`expiresIn must be a whole number of seconds from 1 to ${maxWindow}.`));
|
|
1271
|
+
if (download !== void 0 && (typeof download !== "string" || download === "" || !download.isWellFormed())) return fail(invalidInput("download must be a file name, such as \"report.pdf\"."));
|
|
1272
|
+
const windowStart = Math.floor(nowSeconds() / window) * window;
|
|
1273
|
+
return storage.presignGet(key, {
|
|
1274
|
+
expiresIn: 2 * window,
|
|
1275
|
+
signedAt: /* @__PURE__ */ new Date(windowStart * 1e3),
|
|
1276
|
+
disposition: download === void 0 ? void 0 : contentDisposition("application/octet-stream", download)
|
|
1277
|
+
});
|
|
1278
|
+
}
|
|
1279
|
+
function pageProblem(keys, prefix, previous) {
|
|
1280
|
+
if (keys.some((key) => !key.startsWith(prefix))) return new CloudropError("STORAGE_ERROR", "The storage listed keys outside the prefix, so nothing more was deleted.");
|
|
1281
|
+
if (keys.some((key) => previous.has(key))) return new CloudropError("STORAGE_ERROR", "Some keys were still listed after they were deleted. Retry deletePrefix.", { retryable: true });
|
|
1282
|
+
}
|
|
1283
|
+
async function deletePrefix(storage, prefix) {
|
|
1284
|
+
if (typeof prefix !== "string" || !prefix.endsWith("/") || prefix === "/") return fail(invalidInput("deletePrefix takes a folder prefix ending in \"/\", such as \"users/42/\"."));
|
|
1285
|
+
if (keyProblem(prefix) !== null) return fail(invalidInput("No key can start with this prefix. Write it the way keys are written, such as \"users/42/\": no leading slash, no \".\" or \"..\" segment, no control characters, at most 1024 bytes."));
|
|
1286
|
+
let deleted = 0;
|
|
1287
|
+
let previous = /* @__PURE__ */ new Set();
|
|
1288
|
+
for (let page = 0; page < MAX_DELETE_PAGES; page++) {
|
|
1289
|
+
const listed = await storage.list(prefix);
|
|
1290
|
+
if (listed.error) return listed;
|
|
1291
|
+
const keys = listed.data.items.map((item) => item.key);
|
|
1292
|
+
if (keys.length === 0) return ok({
|
|
1293
|
+
deleted,
|
|
1294
|
+
verifiedAt: /* @__PURE__ */ new Date()
|
|
1295
|
+
});
|
|
1296
|
+
const problem = pageProblem(keys, prefix, previous);
|
|
1297
|
+
if (problem) return fail(problem);
|
|
1298
|
+
const removed = await storage.deleteMany(keys);
|
|
1299
|
+
if (removed.error) return removed;
|
|
1300
|
+
const [first] = removed.data.failed;
|
|
1301
|
+
if (first) return fail(new CloudropError("STORAGE_ERROR", `Could not delete ${removed.data.failed.length} of ${keys.length} keys, such as ${first.key}: ${first.error.message}`, {
|
|
1302
|
+
retryable: first.error.retryable,
|
|
1303
|
+
cause: first.error
|
|
1304
|
+
}));
|
|
1305
|
+
deleted += keys.length;
|
|
1306
|
+
previous = new Set(keys);
|
|
1307
|
+
}
|
|
1308
|
+
return fail(new CloudropError("STORAGE_ERROR", `Objects kept appearing under ${prefix} after ${MAX_DELETE_PAGES} pages were deleted. Stop writing there, then retry.`));
|
|
1309
|
+
}
|
|
1310
|
+
function createFileApi(config) {
|
|
1311
|
+
const { storage } = config;
|
|
1312
|
+
const whenReady = async (call) => {
|
|
1313
|
+
const ready = await config.ready();
|
|
1314
|
+
return ready.error ? fail(ready.error) : call();
|
|
1315
|
+
};
|
|
1316
|
+
return {
|
|
1317
|
+
...storeApi(config, whenReady),
|
|
1318
|
+
getUrl: (key, options) => whenReady(() => getUrl(storage, key, options)),
|
|
1319
|
+
head: (key) => whenReady(() => storage.head(key)),
|
|
1320
|
+
put: (key, body, options) => whenReady(() => storage.put(key, body, options)),
|
|
1321
|
+
delete: (key) => whenReady(() => storage.delete(key)),
|
|
1322
|
+
deleteMany: (keys) => whenReady(() => storage.deleteMany(keys)),
|
|
1323
|
+
copy: (from, to) => whenReady(() => storage.copy(from, to)),
|
|
1324
|
+
deletePrefix: (prefix) => whenReady(() => deletePrefix(storage, prefix)),
|
|
1325
|
+
checkBucket: async (given) => {
|
|
1326
|
+
const options = given ?? {};
|
|
1327
|
+
const origins = isPlainObject(options) ? options.origins ?? [...config.trustedOrigins] : null;
|
|
1328
|
+
if (!Array.isArray(origins) || !origins.every(isExactOrigin)) return fail(invalidInput("checkBucket takes { origins } with exact origins such as \"https://app.example.com\": scheme and host, no path or trailing slash."));
|
|
1329
|
+
return whenReady(() => checkBucket$1(config, origins));
|
|
1330
|
+
}
|
|
1331
|
+
};
|
|
1332
|
+
}
|
|
1333
|
+
// src/server/http.ts
|
|
1334
|
+
const STATUS = {
|
|
1335
|
+
INVALID_INPUT: 400,
|
|
1336
|
+
INVALID_CONFIG: 500,
|
|
1337
|
+
STORAGE_UNREACHABLE: 502,
|
|
1338
|
+
STORAGE_DENIED: 502,
|
|
1339
|
+
STORAGE_ERROR: 502,
|
|
1340
|
+
RATE_LIMITED: 503,
|
|
1341
|
+
NOT_FOUND: 404,
|
|
1342
|
+
ALREADY_EXISTS: 409,
|
|
1343
|
+
NO_SUCH_UPLOAD: 410,
|
|
1344
|
+
INVALID_PART: 400,
|
|
1345
|
+
CHECKSUM_MISMATCH: 400,
|
|
1346
|
+
UNAUTHORIZED: 401,
|
|
1347
|
+
FORBIDDEN: 403,
|
|
1348
|
+
ORIGIN_NOT_ALLOWED: 403,
|
|
1349
|
+
METHOD_NOT_ALLOWED: 405,
|
|
1350
|
+
UNSUPPORTED_MEDIA_TYPE: 415,
|
|
1351
|
+
BODY_TOO_LARGE: 413,
|
|
1352
|
+
UNKNOWN_ROUTE: 404,
|
|
1353
|
+
INVALID_METADATA: 400,
|
|
1354
|
+
TOO_MANY_FILES: 400,
|
|
1355
|
+
FILE_TOO_LARGE: 400,
|
|
1356
|
+
FILE_TYPE_NOT_ALLOWED: 400,
|
|
1357
|
+
QUOTA_EXCEEDED: 403,
|
|
1358
|
+
INVALID_TOKEN: 400,
|
|
1359
|
+
UPLOAD_EXPIRED: 410,
|
|
1360
|
+
NOT_UPLOADED: 409,
|
|
1361
|
+
PARTS_MISSING: 409,
|
|
1362
|
+
VERIFICATION_FAILED: 422,
|
|
1363
|
+
SERVER_ERROR: 500
|
|
1364
|
+
};
|
|
1365
|
+
function errorBody(error) {
|
|
1366
|
+
const body = {
|
|
1367
|
+
code: error.code,
|
|
1368
|
+
message: error.message,
|
|
1369
|
+
retryable: error.retryable
|
|
1370
|
+
};
|
|
1371
|
+
if (error.parts) body.parts = [...error.parts];
|
|
1372
|
+
return body;
|
|
1373
|
+
}
|
|
1374
|
+
function jsonResponse(body, status, headers = {}) {
|
|
1375
|
+
return new Response(JSON.stringify(body), {
|
|
1376
|
+
status,
|
|
1377
|
+
headers: {
|
|
1378
|
+
"content-type": "application/json",
|
|
1379
|
+
"cache-control": "no-store",
|
|
1380
|
+
...headers
|
|
1381
|
+
}
|
|
1382
|
+
});
|
|
1383
|
+
}
|
|
1384
|
+
const serverError = () => new CloudropError("SERVER_ERROR", "The server could not handle the upload. Contact the site owner if this keeps happening.");
|
|
1385
|
+
const hasKnownCode = (error) => Object.hasOwn(STATUS, error.code);
|
|
1386
|
+
const BUSY = /* @__PURE__ */ new WeakSet();
|
|
1387
|
+
function busy() {
|
|
1388
|
+
const error = new CloudropError("SERVER_ERROR", "Another request is completing this upload. Try again in a moment.", { retryable: true });
|
|
1389
|
+
BUSY.add(error);
|
|
1390
|
+
return error;
|
|
1391
|
+
}
|
|
1392
|
+
function errorResponse(error, headers = {}) {
|
|
1393
|
+
if (!hasKnownCode(error)) return errorResponse(serverError(), headers);
|
|
1394
|
+
const exposed = headers["access-control-allow-origin"] ? { "access-control-expose-headers": "Retry-After" } : {};
|
|
1395
|
+
const busyHeaders = BUSY.has(error) ? {
|
|
1396
|
+
...exposed,
|
|
1397
|
+
"retry-after": "2"
|
|
1398
|
+
} : {};
|
|
1399
|
+
return jsonResponse({ error: errorBody(error) }, STATUS[error.code], {
|
|
1400
|
+
...headers,
|
|
1401
|
+
...busyHeaders
|
|
1402
|
+
});
|
|
1403
|
+
}
|
|
1404
|
+
function storageFailure(error, logger) {
|
|
1405
|
+
logger.error("cloudrop: a storage request failed.", error);
|
|
1406
|
+
if (error.code === "RATE_LIMITED") return new CloudropError("RATE_LIMITED", "The storage service is busy. Try again in a moment.", { retryable: true });
|
|
1407
|
+
return error.retryable ? new CloudropError("STORAGE_ERROR", "The storage service failed. Try again in a moment.", { retryable: true }) : new CloudropError("STORAGE_ERROR", "The storage service refused the request. Contact the site owner if this keeps happening.");
|
|
1408
|
+
}
|
|
1409
|
+
function checkOrigin(request, trustedOrigins) {
|
|
1410
|
+
const origin = request.headers.get("origin");
|
|
1411
|
+
if (origin === null) return request.headers.get("sec-fetch-site") === "cross-site" ? { allowed: false } : {
|
|
1412
|
+
allowed: true,
|
|
1413
|
+
cors: {}
|
|
1414
|
+
};
|
|
1415
|
+
if (origin === new URL(request.url).origin) return {
|
|
1416
|
+
allowed: true,
|
|
1417
|
+
cors: {}
|
|
1418
|
+
};
|
|
1419
|
+
if (!trustedOrigins.has(origin)) return { allowed: false };
|
|
1420
|
+
return {
|
|
1421
|
+
allowed: true,
|
|
1422
|
+
cors: {
|
|
1423
|
+
"access-control-allow-origin": origin,
|
|
1424
|
+
"access-control-allow-credentials": "true",
|
|
1425
|
+
vary: "Origin"
|
|
1426
|
+
}
|
|
1427
|
+
};
|
|
1428
|
+
}
|
|
1429
|
+
const originNotAllowed = () => new CloudropError("ORIGIN_NOT_ALLOWED", "This site may not upload here. Add its origin to trustedOrigins in createCloudrop.");
|
|
1430
|
+
const HEADER_LIST = /^[A-Za-z0-9-]+(\s*,\s*[A-Za-z0-9-]+)*$/;
|
|
1431
|
+
function preflight(request, trustedOrigins) {
|
|
1432
|
+
const verdict = checkOrigin(request, trustedOrigins);
|
|
1433
|
+
if (!verdict.allowed || request.headers.get("access-control-request-method") !== "POST") return errorResponse(originNotAllowed());
|
|
1434
|
+
const requested = request.headers.get("access-control-request-headers") ?? "";
|
|
1435
|
+
return new Response(null, {
|
|
1436
|
+
status: 204,
|
|
1437
|
+
headers: {
|
|
1438
|
+
...verdict.cors,
|
|
1439
|
+
"access-control-allow-methods": "POST",
|
|
1440
|
+
"access-control-allow-headers": HEADER_LIST.test(requested) ? requested : "content-type",
|
|
1441
|
+
"access-control-max-age": "600",
|
|
1442
|
+
"cache-control": "no-store",
|
|
1443
|
+
vary: "Origin, Access-Control-Request-Headers"
|
|
1444
|
+
}
|
|
1445
|
+
});
|
|
1446
|
+
}
|
|
1447
|
+
function isJsonRequest(request) {
|
|
1448
|
+
return /^application\/json\s*(;|$)/i.test(request.headers.get("content-type") ?? "");
|
|
1449
|
+
}
|
|
1450
|
+
const bodyTooLarge = () => new CloudropError("BODY_TOO_LARGE", "The request is larger than 64 KiB. Send fewer files at once.");
|
|
1451
|
+
async function readJson(request) {
|
|
1452
|
+
if (Number(request.headers.get("content-length")) > 65536) return fail(bodyTooLarge());
|
|
1453
|
+
const chunks = [];
|
|
1454
|
+
let length = 0;
|
|
1455
|
+
try {
|
|
1456
|
+
const reader = request.body?.getReader();
|
|
1457
|
+
for (let chunk = await reader?.read(); chunk && !chunk.done; chunk = await reader?.read()) {
|
|
1458
|
+
length += chunk.value.length;
|
|
1459
|
+
if (length > 65536) {
|
|
1460
|
+
await reader?.cancel();
|
|
1461
|
+
return fail(bodyTooLarge());
|
|
1462
|
+
}
|
|
1463
|
+
chunks.push(chunk.value);
|
|
1464
|
+
}
|
|
1465
|
+
} catch {
|
|
1466
|
+
return fail(invalidInput("The request body could not be read. Send the request again."));
|
|
1467
|
+
}
|
|
1468
|
+
const bytes = await new Blob(chunks).arrayBuffer();
|
|
1469
|
+
try {
|
|
1470
|
+
return ok(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes)));
|
|
1471
|
+
} catch {
|
|
1472
|
+
return fail(invalidInput("The request body is not valid JSON. Send a JSON object."));
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
// src/server/token-request.ts
|
|
1476
|
+
async function readToken(body, config, options = {}) {
|
|
1477
|
+
if (!isPlainObject(body)) return fail(invalidInput("Send a JSON object with the upload token: { \"token\": \"...\" }."));
|
|
1478
|
+
return verifyToken(body.token, config.tokenKeys.all, nowSeconds(), options);
|
|
1479
|
+
}
|
|
1480
|
+
async function readTokenRequest(body, config, options = {}) {
|
|
1481
|
+
const verified = await readToken(body, config, { allowExpired: options.storedAfterExpiry && config.store !== void 0 });
|
|
1482
|
+
if (verified.error) return verified;
|
|
1483
|
+
if (nowSeconds() >= verified.data.exp && !(config.store && verified.data.stored)) return fail(uploadExpired());
|
|
1484
|
+
const route = config.routes.get(verified.data.route);
|
|
1485
|
+
if (!route) return fail(new CloudropError("UNKNOWN_ROUTE", "The upload route of this token no longer exists. Start a new upload."));
|
|
1486
|
+
return ok({
|
|
1487
|
+
token: verified.data,
|
|
1488
|
+
route
|
|
1489
|
+
});
|
|
1490
|
+
}
|
|
1491
|
+
function tokenAbortContexts(token, reason) {
|
|
1492
|
+
const { id, key, name, size, type } = token;
|
|
1493
|
+
return abortContexts({
|
|
1494
|
+
route: token.route,
|
|
1495
|
+
metadata: token.metadata,
|
|
1496
|
+
auth: token.owner
|
|
1497
|
+
}, [{
|
|
1498
|
+
id,
|
|
1499
|
+
key,
|
|
1500
|
+
name,
|
|
1501
|
+
size,
|
|
1502
|
+
type
|
|
1503
|
+
}], reason);
|
|
1504
|
+
}
|
|
1505
|
+
// src/server/abort.ts
|
|
1506
|
+
const UNFINISHED = /* @__PURE__ */ new Set([
|
|
1507
|
+
"aborted",
|
|
1508
|
+
"rejected",
|
|
1509
|
+
"failed",
|
|
1510
|
+
"expired"
|
|
1511
|
+
]);
|
|
1512
|
+
async function abortAndDelete(config, token, keepStored) {
|
|
1513
|
+
const { storage, logger } = config;
|
|
1514
|
+
if (isMultipart(token)) {
|
|
1515
|
+
const aborted = await storage.abortMultipart(token.key, token.uploadId);
|
|
1516
|
+
if (aborted.error) return fail(storageFailure(aborted.error, logger));
|
|
1517
|
+
}
|
|
1518
|
+
const head = await storage.head(token.key);
|
|
1519
|
+
if (head.error) return fail(storageFailure(head.error, logger));
|
|
1520
|
+
if (head.data?.metadata["cloudrop-id"] === token.id) {
|
|
1521
|
+
if (keepStored) return ok({ id: token.id });
|
|
1522
|
+
const removed = await storage.delete(token.key);
|
|
1523
|
+
if (removed.error) return fail(storageFailure(removed.error, logger));
|
|
1524
|
+
}
|
|
1525
|
+
await runOnAbort(config.plugins, tokenAbortContexts(token, "aborted"), logger);
|
|
1526
|
+
return ok({ id: token.id });
|
|
1527
|
+
}
|
|
1528
|
+
async function handleAbort({ body, config }) {
|
|
1529
|
+
const verified = await readToken(body, config, { allowExpired: true });
|
|
1530
|
+
if (verified.error) return verified;
|
|
1531
|
+
const token = verified.data;
|
|
1532
|
+
const keep = isPlainObject(body) ? body.keep : void 0;
|
|
1533
|
+
if (keep !== void 0 && typeof keep !== "boolean") return fail(invalidInput("keep must be true or false. Send { \"token\": \"...\", \"keep\": true }, or leave keep out."));
|
|
1534
|
+
if (!config.store || !token.stored) return abortAndDelete(config, token, keep === true || nowSeconds() >= token.exp);
|
|
1535
|
+
const ended = await fileCalls(config.store, config.logger, token).end("pending", "aborted");
|
|
1536
|
+
if (ended.error) return ended;
|
|
1537
|
+
const { changed, record } = ended.data;
|
|
1538
|
+
return changed || record !== null && UNFINISHED.has(record.status) && record.cleanedAt === null ? abortAndDelete(config, token, false) : ok({ id: token.id });
|
|
1539
|
+
}
|
|
1540
|
+
// src/server/complete-steps.ts
|
|
1541
|
+
const unfinished = () => new CloudropError("SERVER_ERROR", "The file was uploaded, but the server could not finish processing it. Try again in a moment.", { retryable: true });
|
|
1542
|
+
function uploadedFile(config, token, route) {
|
|
1543
|
+
const { id, key, name, size, type } = token;
|
|
1544
|
+
return {
|
|
1545
|
+
id,
|
|
1546
|
+
key,
|
|
1547
|
+
name,
|
|
1548
|
+
size,
|
|
1549
|
+
type,
|
|
1550
|
+
url: route.visibility === "public" ? `${config.storage.publicUrl}/${encodeKeyPath(key)}` : null
|
|
1551
|
+
};
|
|
1552
|
+
}
|
|
1553
|
+
async function checkAfterVerify(request, config, token, file) {
|
|
1554
|
+
const { route, metadata, owner: auth } = token;
|
|
1555
|
+
let refused;
|
|
1556
|
+
try {
|
|
1557
|
+
refused = await runAfterVerify(config.plugins, {
|
|
1558
|
+
request,
|
|
1559
|
+
route,
|
|
1560
|
+
file,
|
|
1561
|
+
metadata,
|
|
1562
|
+
auth,
|
|
1563
|
+
storage: config.storage
|
|
1564
|
+
});
|
|
1565
|
+
} catch (error) {
|
|
1566
|
+
config.logger.error(`cloudrop: a plugin's afterVerify failed for route "${route}".`, error);
|
|
1567
|
+
return {
|
|
1568
|
+
kind: "failed",
|
|
1569
|
+
error: unfinished()
|
|
1570
|
+
};
|
|
1571
|
+
}
|
|
1572
|
+
return refused ? {
|
|
1573
|
+
kind: "refused",
|
|
1574
|
+
error: refused,
|
|
1575
|
+
cleanup: "delete"
|
|
1576
|
+
} : { kind: "verified" };
|
|
1577
|
+
}
|
|
1578
|
+
function completeContext(request, token, file) {
|
|
1579
|
+
return {
|
|
1580
|
+
...token.owner,
|
|
1581
|
+
request,
|
|
1582
|
+
file,
|
|
1583
|
+
metadata: token.metadata
|
|
1584
|
+
};
|
|
1585
|
+
}
|
|
1586
|
+
function encodeResult(logger, route, data) {
|
|
1587
|
+
let json;
|
|
1588
|
+
try {
|
|
1589
|
+
json = JSON.stringify(data);
|
|
1590
|
+
} catch (error) {
|
|
1591
|
+
logger.error(`cloudrop: onComplete for route "${route}" returned a value JSON cannot encode (a BigInt or a cycle).`, error);
|
|
1592
|
+
return fail(unfinished());
|
|
1593
|
+
}
|
|
1594
|
+
return ok(json);
|
|
1595
|
+
}
|
|
1596
|
+
// src/server/complete-verify.ts
|
|
1597
|
+
const DISCARDED = "The uploaded file does not match what was declared, so it was discarded. Upload it again.";
|
|
1598
|
+
const VERIFIED = { kind: "verified" };
|
|
1599
|
+
const failed = (error) => ({
|
|
1600
|
+
kind: "failed",
|
|
1601
|
+
error
|
|
1602
|
+
});
|
|
1603
|
+
const partChanged = () => new CloudropError("STORAGE_ERROR", "A part of the file changed while the upload was being completed. Complete the upload again.", { retryable: true });
|
|
1604
|
+
async function verifyObject(config, token) {
|
|
1605
|
+
const head = await config.storage.head(token.key);
|
|
1606
|
+
if (head.error) return failed(storageFailure(head.error, config.logger));
|
|
1607
|
+
const object = head.data;
|
|
1608
|
+
if (object === null) return failed(new CloudropError("NOT_UPLOADED", "The file has not been uploaded yet. Upload it, then complete the upload."));
|
|
1609
|
+
if (object.metadata["cloudrop-id"] !== token.id) return {
|
|
1610
|
+
kind: "refused",
|
|
1611
|
+
error: new CloudropError("VERIFICATION_FAILED", "Another file is stored under this upload's key. Start a new upload."),
|
|
1612
|
+
cleanup: "keep"
|
|
1613
|
+
};
|
|
1614
|
+
if (object.size !== token.size || object.type !== token.type) return {
|
|
1615
|
+
kind: "refused",
|
|
1616
|
+
error: new CloudropError("VERIFICATION_FAILED", DISCARDED),
|
|
1617
|
+
cleanup: "delete"
|
|
1618
|
+
};
|
|
1619
|
+
return VERIFIED;
|
|
1620
|
+
}
|
|
1621
|
+
async function completedBefore(config, token) {
|
|
1622
|
+
const head = await config.storage.head(token.key);
|
|
1623
|
+
if (head.error) return fail(storageFailure(head.error, config.logger));
|
|
1624
|
+
if (head.data?.metadata["cloudrop-id"] === token.id && head.data.size === token.size) return ok(void 0);
|
|
1625
|
+
return fail(new CloudropError("NO_SUCH_UPLOAD", "This upload was aborted or has expired. Start it again."));
|
|
1626
|
+
}
|
|
1627
|
+
const fromResult = (result) => result.error ? failed(result.error) : VERIFIED;
|
|
1628
|
+
async function completeParts(config, token) {
|
|
1629
|
+
const { storage, logger } = config;
|
|
1630
|
+
const listed = await storage.listParts(token.key, token.uploadId);
|
|
1631
|
+
if (listed.error) return listed.error.code === "NO_SUCH_UPLOAD" ? fromResult(await completedBefore(config, token)) : failed(storageFailure(listed.error, logger));
|
|
1632
|
+
const byNumber = new Map(listed.data.map((part) => [part.partNumber, part]));
|
|
1633
|
+
const numbers = Array.from({ length: countParts(token.size, token.partSize) }, (_, index) => index + 1);
|
|
1634
|
+
const missing = numbers.filter((partNumber) => byNumber.get(partNumber)?.size !== sizeOfPart(token.size, token.partSize, partNumber));
|
|
1635
|
+
if (missing.length > 0) return failed(new CloudropError("PARTS_MISSING", `${missing.length} of ${numbers.length} parts are missing or have the wrong size. Upload them again, then complete the upload.`, { parts: missing }));
|
|
1636
|
+
const parts = numbers.map((partNumber) => ({
|
|
1637
|
+
partNumber,
|
|
1638
|
+
etag: byNumber.get(partNumber)?.etag ?? ""
|
|
1639
|
+
}));
|
|
1640
|
+
const completed = await storage.completeMultipart(token.key, token.uploadId, parts, { ifNoneMatch: true });
|
|
1641
|
+
if (!completed.error) return VERIFIED;
|
|
1642
|
+
if (completed.error.code === "NO_SUCH_UPLOAD") return fromResult(await completedBefore(config, token));
|
|
1643
|
+
if (completed.error.code === "INVALID_PART") {
|
|
1644
|
+
logger.error(`cloudrop: a part of file ${token.id} on route "${token.route}" changed while its upload was being completed.`, completed.error);
|
|
1645
|
+
return failed(partChanged());
|
|
1646
|
+
}
|
|
1647
|
+
if (completed.error.code === "ALREADY_EXISTS") {
|
|
1648
|
+
const own = await completedBefore(config, token);
|
|
1649
|
+
if (own.error?.code !== "NO_SUCH_UPLOAD") return fromResult(own);
|
|
1650
|
+
return {
|
|
1651
|
+
kind: "refused",
|
|
1652
|
+
error: new CloudropError("ALREADY_EXISTS", "Another file already has this upload's key. Start a new upload."),
|
|
1653
|
+
cleanup: "abort"
|
|
1654
|
+
};
|
|
1655
|
+
}
|
|
1656
|
+
return failed(storageFailure(completed.error, logger));
|
|
1657
|
+
}
|
|
1658
|
+
function verifyUpload(config, token) {
|
|
1659
|
+
return isMultipart(token) ? completeParts(config, token) : verifyObject(config, token);
|
|
1660
|
+
}
|
|
1661
|
+
async function cleanUpRefused(config, token, { error, cleanup }, { rowEnded = false } = {}) {
|
|
1662
|
+
const { storage, logger } = config;
|
|
1663
|
+
if (cleanup === "delete") {
|
|
1664
|
+
const removed = await storage.delete(token.key);
|
|
1665
|
+
if (removed.error && !rowEnded) return fail(storageFailure(removed.error, logger));
|
|
1666
|
+
if (removed.error) logger.error(`cloudrop: could not delete the refused file ${token.id} on route "${token.route}". The sweep's cleanup pass will delete it.`, removed.error);
|
|
1667
|
+
}
|
|
1668
|
+
if (cleanup === "abort" && isMultipart(token)) {
|
|
1669
|
+
const aborted = await storage.abortMultipart(token.key, token.uploadId);
|
|
1670
|
+
if (aborted.error) logger.error(`cloudrop: could not abort the collided upload of file ${token.id} on route "${token.route}".`, aborted.error);
|
|
1671
|
+
}
|
|
1672
|
+
await runOnAbort(config.plugins, tokenAbortContexts(token, "rejected"), logger);
|
|
1673
|
+
return fail(error);
|
|
1674
|
+
}
|
|
1675
|
+
// src/server/store-complete.ts
|
|
1676
|
+
const MAX_RESULT_JSON_BYTES = 16384;
|
|
1677
|
+
const replay = (record, file) => ok({
|
|
1678
|
+
file,
|
|
1679
|
+
data: record.result === null ? null : JSON.parse(record.result)
|
|
1680
|
+
});
|
|
1681
|
+
function settled(record, file) {
|
|
1682
|
+
if (record?.completedAt) return replay(record, file);
|
|
1683
|
+
return fail(record?.status === "completing" ? busy() : cancelled());
|
|
1684
|
+
}
|
|
1685
|
+
function answerFromRow(record, { token, file, config }) {
|
|
1686
|
+
const claimLive = record?.status === "completing" && (record.claimedAt?.getTime() ?? 0) + config.completionLeaseMs >= Date.now();
|
|
1687
|
+
if (!(record?.status === "pending" || record?.status === "completing" && !claimLive)) return settled(record, file);
|
|
1688
|
+
return nowSeconds() >= token.exp ? fail(uploadExpired()) : null;
|
|
1689
|
+
}
|
|
1690
|
+
function claimAnswer(outcome, file) {
|
|
1691
|
+
if (outcome.kind === "busy") return fail(busy());
|
|
1692
|
+
return settled(outcome.kind === "missing" ? null : outcome.record, file);
|
|
1693
|
+
}
|
|
1694
|
+
const lostClaim = (record, file) => record?.completedAt ? replay(record, file) : fail(unfinished());
|
|
1695
|
+
async function refuse(completion, refusal) {
|
|
1696
|
+
const ended = await completion.calls.end("pending", "rejected");
|
|
1697
|
+
if (ended.error) return ended;
|
|
1698
|
+
if (ended.data.changed) return cleanUpRefused(completion.config, completion.token, refusal, { rowEnded: true });
|
|
1699
|
+
return settled(ended.data.record, completion.file);
|
|
1700
|
+
}
|
|
1701
|
+
async function runOnComplete({ request, config, token, route, file }, extra) {
|
|
1702
|
+
const { onComplete } = route.definition;
|
|
1703
|
+
let result = null;
|
|
1704
|
+
try {
|
|
1705
|
+
if (onComplete) result = await onComplete({
|
|
1706
|
+
...completeContext(request, token, file),
|
|
1707
|
+
...extra
|
|
1708
|
+
});
|
|
1709
|
+
} catch (error) {
|
|
1710
|
+
config.logger.error(`cloudrop: onComplete failed for route "${route.name}".`, error);
|
|
1711
|
+
return fail(unfinished());
|
|
1712
|
+
}
|
|
1713
|
+
if (result === void 0 || result === null) return ok({
|
|
1714
|
+
data: null,
|
|
1715
|
+
json: null
|
|
1716
|
+
});
|
|
1717
|
+
const encoded = encodeResult(config.logger, route.name, result);
|
|
1718
|
+
if (encoded.error) return encoded;
|
|
1719
|
+
const json = encoded.data ?? null;
|
|
1720
|
+
if (json !== null && new TextEncoder().encode(json).length > MAX_RESULT_JSON_BYTES) {
|
|
1721
|
+
config.logger.error(`cloudrop: onComplete for route "${route.name}" returned more than 16 KiB of JSON. Return ids, not records.`);
|
|
1722
|
+
return fail(unfinished());
|
|
1723
|
+
}
|
|
1724
|
+
return ok({
|
|
1725
|
+
data: json === null ? null : JSON.parse(json),
|
|
1726
|
+
json
|
|
1727
|
+
});
|
|
1728
|
+
}
|
|
1729
|
+
async function withLease(completion) {
|
|
1730
|
+
const { calls, file, config } = completion;
|
|
1731
|
+
const claim = crypto.randomUUID();
|
|
1732
|
+
const claimed = await calls.claim(claim, config.completionLeaseMs);
|
|
1733
|
+
if (claimed.error) return {
|
|
1734
|
+
answer: claimed,
|
|
1735
|
+
recorded: false
|
|
1736
|
+
};
|
|
1737
|
+
if (claimed.data.kind !== "claimed") return {
|
|
1738
|
+
answer: claimAnswer(claimed.data, file),
|
|
1739
|
+
recorded: false
|
|
1740
|
+
};
|
|
1741
|
+
const ran = await runOnComplete(completion, {});
|
|
1742
|
+
if (ran.error) {
|
|
1743
|
+
await calls.release(claim);
|
|
1744
|
+
return {
|
|
1745
|
+
answer: ran,
|
|
1746
|
+
recorded: false
|
|
1747
|
+
};
|
|
1748
|
+
}
|
|
1749
|
+
const done = await calls.complete(claim, ran.data.json);
|
|
1750
|
+
if (done.error) return {
|
|
1751
|
+
answer: done,
|
|
1752
|
+
recorded: false
|
|
1753
|
+
};
|
|
1754
|
+
if (!done.data.changed) return {
|
|
1755
|
+
answer: lostClaim(done.data.record, file),
|
|
1756
|
+
recorded: false
|
|
1757
|
+
};
|
|
1758
|
+
return {
|
|
1759
|
+
answer: ok({
|
|
1760
|
+
file,
|
|
1761
|
+
data: ran.data.data
|
|
1762
|
+
}),
|
|
1763
|
+
recorded: true
|
|
1764
|
+
};
|
|
1765
|
+
}
|
|
1766
|
+
async function inTransaction(completion, transaction) {
|
|
1767
|
+
const { token, file, config } = completion;
|
|
1768
|
+
const claim = crypto.randomUUID();
|
|
1769
|
+
let attempt = {
|
|
1770
|
+
answer: fail(storeUnavailable()),
|
|
1771
|
+
recorded: false
|
|
1772
|
+
};
|
|
1773
|
+
let rolledBack = false;
|
|
1774
|
+
const rollBack = (answer) => {
|
|
1775
|
+
attempt = {
|
|
1776
|
+
answer,
|
|
1777
|
+
recorded: false
|
|
1778
|
+
};
|
|
1779
|
+
rolledBack = true;
|
|
1780
|
+
throw new Error("cloudrop: rolled back the completion.");
|
|
1781
|
+
};
|
|
1782
|
+
try {
|
|
1783
|
+
await transaction(async (tx, store) => {
|
|
1784
|
+
const claimed = await store.claim(token.id, claim, /* @__PURE__ */ new Date(), config.completionLeaseMs);
|
|
1785
|
+
if (claimed.kind !== "claimed") {
|
|
1786
|
+
attempt = {
|
|
1787
|
+
answer: claimAnswer(claimed, file),
|
|
1788
|
+
recorded: false
|
|
1789
|
+
};
|
|
1790
|
+
return;
|
|
1791
|
+
}
|
|
1792
|
+
const ran = await runOnComplete(completion, storeOfRoute(completion.route.definition) ? { tx } : {});
|
|
1793
|
+
if (ran.error) return rollBack(ran);
|
|
1794
|
+
const done = await store.complete(token.id, claim, ran.data.json, /* @__PURE__ */ new Date());
|
|
1795
|
+
if (!done.changed) return rollBack(lostClaim(done.record, file));
|
|
1796
|
+
attempt = {
|
|
1797
|
+
answer: ok({
|
|
1798
|
+
file,
|
|
1799
|
+
data: ran.data.data
|
|
1800
|
+
}),
|
|
1801
|
+
recorded: true
|
|
1802
|
+
};
|
|
1803
|
+
});
|
|
1804
|
+
} catch (error) {
|
|
1805
|
+
if (rolledBack) return attempt;
|
|
1806
|
+
config.logger.error(`cloudrop: the store failed to complete file ${token.id} on route "${token.route}".`, error);
|
|
1807
|
+
return {
|
|
1808
|
+
answer: fail(storeUnavailable()),
|
|
1809
|
+
recorded: false
|
|
1810
|
+
};
|
|
1811
|
+
}
|
|
1812
|
+
return attempt;
|
|
1813
|
+
}
|
|
1814
|
+
async function runAfterComplete({ request, config, token, route, file }, data) {
|
|
1815
|
+
try {
|
|
1816
|
+
await route.definition.afterComplete?.({
|
|
1817
|
+
...completeContext(request, token, file),
|
|
1818
|
+
data
|
|
1819
|
+
});
|
|
1820
|
+
} catch (error) {
|
|
1821
|
+
config.logger.error(`cloudrop: afterComplete failed for route "${route.name}".`, error);
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
async function completeStored({ request, config }, store, token, route) {
|
|
1825
|
+
const calls = fileCalls(store, config.logger, token);
|
|
1826
|
+
const completion = {
|
|
1827
|
+
request,
|
|
1828
|
+
config,
|
|
1829
|
+
token,
|
|
1830
|
+
route,
|
|
1831
|
+
file: uploadedFile(config, token, route),
|
|
1832
|
+
calls
|
|
1833
|
+
};
|
|
1834
|
+
const row = await calls.get();
|
|
1835
|
+
if (row.error) return row;
|
|
1836
|
+
const early = answerFromRow(row.data, completion);
|
|
1837
|
+
if (early) return early;
|
|
1838
|
+
const stored = await verifyUpload(config, token);
|
|
1839
|
+
const checked = stored.kind === "verified" ? await checkAfterVerify(request, config, token, completion.file) : stored;
|
|
1840
|
+
if (checked.kind === "failed") return fail(checked.error);
|
|
1841
|
+
if (checked.kind === "refused") return refuse(completion, checked);
|
|
1842
|
+
const { answer, recorded } = store.transaction ? await inTransaction(completion, store.transaction.bind(store)) : await withLease(completion);
|
|
1843
|
+
if (recorded) await runAfterComplete(completion, answer.data?.data);
|
|
1844
|
+
return answer;
|
|
1845
|
+
}
|
|
1846
|
+
// src/server/complete.ts
|
|
1847
|
+
async function handleComplete(input) {
|
|
1848
|
+
const { request, body, config } = input;
|
|
1849
|
+
const verified = await readTokenRequest(body, config, { storedAfterExpiry: true });
|
|
1850
|
+
if (verified.error) return verified;
|
|
1851
|
+
const { token, route } = verified.data;
|
|
1852
|
+
if (config.store && token.stored) return completeStored(input, config.store, token, route);
|
|
1853
|
+
const { logger } = config;
|
|
1854
|
+
const stored = await verifyUpload(config, token);
|
|
1855
|
+
if (stored.kind === "failed") return fail(stored.error);
|
|
1856
|
+
if (stored.kind === "refused") return cleanUpRefused(config, token, stored);
|
|
1857
|
+
const file = uploadedFile(config, token, route);
|
|
1858
|
+
const checked = await checkAfterVerify(request, config, token, file);
|
|
1859
|
+
if (checked.kind === "failed") return fail(checked.error);
|
|
1860
|
+
if (checked.kind === "refused") return cleanUpRefused(config, token, checked);
|
|
1861
|
+
const { onComplete } = route.definition;
|
|
1862
|
+
if (!onComplete) return ok({
|
|
1863
|
+
file,
|
|
1864
|
+
data: null
|
|
1865
|
+
});
|
|
1866
|
+
let data;
|
|
1867
|
+
try {
|
|
1868
|
+
data = await onComplete(completeContext(request, token, file));
|
|
1869
|
+
} catch (error) {
|
|
1870
|
+
logger.error(`cloudrop: onComplete failed for route "${route.name}".`, error);
|
|
1871
|
+
return fail(unfinished());
|
|
1872
|
+
}
|
|
1873
|
+
const encoded = encodeResult(logger, route.name, data);
|
|
1874
|
+
if (encoded.error) return encoded;
|
|
1875
|
+
return ok({
|
|
1876
|
+
file,
|
|
1877
|
+
data: data === void 0 ? null : data
|
|
1878
|
+
});
|
|
1879
|
+
}
|
|
1880
|
+
// src/server/sign.ts
|
|
1881
|
+
const isPartList = (parts, count) => Array.isArray(parts) && parts.length >= 1 && parts.length <= 20 && new Set(parts).size === parts.length && parts.every((part) => Number.isInteger(part) && part >= 1 && part <= count);
|
|
1882
|
+
async function handleSign({ body, config }) {
|
|
1883
|
+
const failure = (error) => fail(error.code === "UPLOAD_EXPIRED" ? error : storageFailure(error, config.logger));
|
|
1884
|
+
const request = await readTokenRequest(body, config);
|
|
1885
|
+
if (request.error) return request;
|
|
1886
|
+
const { token } = request.data;
|
|
1887
|
+
if (config.store && token.stored) {
|
|
1888
|
+
const row = await fileCalls(config.store, config.logger, token).get();
|
|
1889
|
+
if (row.error) return row;
|
|
1890
|
+
if (row.data?.status !== "pending") return fail(cancelled());
|
|
1891
|
+
}
|
|
1892
|
+
const { parts } = body;
|
|
1893
|
+
if (!isMultipart(token)) {
|
|
1894
|
+
if (parts !== void 0) return fail(invalidInput("This upload is a single PUT: send { token } without parts."));
|
|
1895
|
+
const put = await presignSingle(config.storage, token);
|
|
1896
|
+
return put.error ? failure(put.error) : ok({ put: put.data });
|
|
1897
|
+
}
|
|
1898
|
+
const count = countParts(token.size, token.partSize);
|
|
1899
|
+
if (!isPartList(parts, count)) return fail(invalidInput(`Ask for 1 to 20 different part numbers, each from 1 to ${count}.`));
|
|
1900
|
+
const signed = await presignParts(config.storage, token, parts);
|
|
1901
|
+
return signed.error ? failure(signed.error) : ok({ parts: signed.data });
|
|
1902
|
+
}
|
|
1903
|
+
const invalidMetadata = (message) => new CloudropError("INVALID_METADATA", message);
|
|
1904
|
+
const UNSAFE_IN_LOGS = /[\p{Cc}\u2028\u2029\u202a-\u202e\u2066-\u2069]/gu;
|
|
1905
|
+
function describeIssue(issue) {
|
|
1906
|
+
const path = (issue.path ?? []).map((segment) => String(typeof segment === "object" ? segment.key : segment));
|
|
1907
|
+
const text = path.length > 0 ? `${String(issue.message)} (at ${path.join(".")})` : String(issue.message);
|
|
1908
|
+
return Array.from(text.replace(UNSAFE_IN_LOGS, " ")).slice(0, 200).join("");
|
|
1909
|
+
}
|
|
1910
|
+
async function validateMetadata(schema, value) {
|
|
1911
|
+
if (schema === void 0) return value === void 0 ? ok({
|
|
1912
|
+
value: void 0,
|
|
1913
|
+
json: void 0
|
|
1914
|
+
}) : fail(invalidMetadata("This upload takes no metadata. Remove it."));
|
|
1915
|
+
const result = await schema["~standard"].validate(value);
|
|
1916
|
+
if (result.issues) {
|
|
1917
|
+
const first = result.issues[0];
|
|
1918
|
+
const detail = first ? `: ${describeIssue(first)}` : "";
|
|
1919
|
+
return fail(invalidMetadata(`The metadata is not valid${detail}. Correct it and try again.`));
|
|
1920
|
+
}
|
|
1921
|
+
const json = JSON.stringify(result.value);
|
|
1922
|
+
if (json === void 0 && result.value !== void 0) throw new TypeError("The metadata schema returned a value with no JSON form, such as a function. Return plain data from the schema.");
|
|
1923
|
+
if (json !== void 0 && new TextEncoder().encode(json).length > 2048) return fail(invalidMetadata("The metadata is larger than 2 KiB. Send less, and keep large values in your database."));
|
|
1924
|
+
return ok({
|
|
1925
|
+
value: result.value,
|
|
1926
|
+
json
|
|
1927
|
+
});
|
|
1928
|
+
}
|
|
1929
|
+
// src/server/upload-request.ts
|
|
1930
|
+
function readDescriptor(file) {
|
|
1931
|
+
if (!isPlainObject(file) || typeof file.name !== "string" || !file.name.isWellFormed() || typeof file.size !== "number" || !Number.isSafeInteger(file.size) || file.size < 0 || typeof file.type !== "string") return null;
|
|
1932
|
+
const name = file.name.normalize("NFC");
|
|
1933
|
+
const length = [...name].length;
|
|
1934
|
+
return length > 0 && length <= 255 ? {
|
|
1935
|
+
name,
|
|
1936
|
+
size: file.size,
|
|
1937
|
+
type: file.type
|
|
1938
|
+
} : null;
|
|
1939
|
+
}
|
|
1940
|
+
const IDEMPOTENCY_KEY = /^[A-Za-z0-9_-]{16,128}$/;
|
|
1941
|
+
function readRequest(body, config) {
|
|
1942
|
+
if (!isPlainObject(body)) return fail(invalidInput("Send a JSON object: { route, files, metadata }."));
|
|
1943
|
+
const { idempotencyKey } = body;
|
|
1944
|
+
if (idempotencyKey !== void 0 && (typeof idempotencyKey !== "string" || !IDEMPOTENCY_KEY.test(idempotencyKey))) return fail(invalidInput("Send idempotencyKey as 16 to 128 letters, digits, \"-\" or \"_\", such as a UUID, or leave it out."));
|
|
1945
|
+
if (typeof body.route !== "string") return fail(invalidInput("Name the upload route, such as { \"route\": \"avatar\" }."));
|
|
1946
|
+
const route = config.routes.get(body.route);
|
|
1947
|
+
if (!route) {
|
|
1948
|
+
const named = ROUTE_NAME.test(body.route) ? ` named "${body.route}"` : " with that name";
|
|
1949
|
+
return fail(new CloudropError("UNKNOWN_ROUTE", `There is no upload route${named}. Check the route name.`));
|
|
1950
|
+
}
|
|
1951
|
+
const { files } = body;
|
|
1952
|
+
if (!Array.isArray(files) || files.length === 0) return fail(invalidInput("Send at least one file as { \"name\", \"size\", \"type\" }."));
|
|
1953
|
+
if (files.length > route.maxFiles) return fail(new CloudropError("TOO_MANY_FILES", `This upload takes at most ${route.maxFiles} files at once. Send fewer.`));
|
|
1954
|
+
const descriptors = files.map(readDescriptor);
|
|
1955
|
+
const bad = descriptors.indexOf(null);
|
|
1956
|
+
if (bad >= 0) return fail(invalidInput(`File ${bad + 1} needs a name of 1 to 255 characters, a size in bytes and a type, such as { "name": "a.png", "size": 1024, "type": "image/png" }.`));
|
|
1957
|
+
return ok({
|
|
1958
|
+
route,
|
|
1959
|
+
files: descriptors,
|
|
1960
|
+
metadata: body.metadata,
|
|
1961
|
+
idempotencyKey: idempotencyKey ?? null
|
|
1962
|
+
});
|
|
1963
|
+
}
|
|
1964
|
+
function prepareFile(route, descriptor, context) {
|
|
1965
|
+
const type = normalizeType(descriptor.type);
|
|
1966
|
+
if (type === null) return new CloudropError("FILE_TYPE_NOT_ALLOWED", "The file's type is not a valid MIME type. Pick another file.");
|
|
1967
|
+
if (descriptor.size > route.maxSize) return new CloudropError("FILE_TOO_LARGE", `This file is over the ${formatBytes(route.maxSize)} limit. Pick a smaller file.`);
|
|
1968
|
+
const { name } = descriptor;
|
|
1969
|
+
const match = matchAccept(route.accept, type, name);
|
|
1970
|
+
if ("problem" in match) return new CloudropError("FILE_TYPE_NOT_ALLOWED", match.problem);
|
|
1971
|
+
const id = newFileId();
|
|
1972
|
+
const keyContext = {
|
|
1973
|
+
...context.auth,
|
|
1974
|
+
request: context.request,
|
|
1975
|
+
metadata: context.metadata,
|
|
1976
|
+
file: {
|
|
1977
|
+
id,
|
|
1978
|
+
name,
|
|
1979
|
+
size: descriptor.size,
|
|
1980
|
+
type,
|
|
1981
|
+
ext: match.ext
|
|
1982
|
+
}
|
|
1983
|
+
};
|
|
1984
|
+
const { definition } = route;
|
|
1985
|
+
const folder = definition.prefix ? definition.prefix(keyContext) : "";
|
|
1986
|
+
if (typeof folder !== "string") throw new TypeError(`Route "${route.name}": prefix must return a string, got ${typeof folder}.`);
|
|
1987
|
+
if (folder.startsWith("/")) throw new TypeError(`Route "${route.name}": prefix must be a relative folder such as "users/42", without a leading slash.`);
|
|
1988
|
+
const key = normalizeKey(definition.key ? definition.key(keyContext) : generatedKey(folder, id, match.ext));
|
|
1989
|
+
if ("problem" in key) throw new TypeError(`Route "${route.name}" built an unusable key: ${key.problem}`);
|
|
1990
|
+
return {
|
|
1991
|
+
id,
|
|
1992
|
+
key: key.key,
|
|
1993
|
+
name,
|
|
1994
|
+
size: descriptor.size,
|
|
1995
|
+
type
|
|
1996
|
+
};
|
|
1997
|
+
}
|
|
1998
|
+
// src/server/upload.ts
|
|
1999
|
+
async function startFile(config, base) {
|
|
2000
|
+
const { storage, logger } = config;
|
|
2001
|
+
const { signing } = config.tokenKeys;
|
|
2002
|
+
const expiresAt = (/* @__PURE__ */ new Date(base.exp * 1e3)).toISOString();
|
|
2003
|
+
const ticket = {
|
|
2004
|
+
id: base.id,
|
|
2005
|
+
key: base.key,
|
|
2006
|
+
expiresAt
|
|
2007
|
+
};
|
|
2008
|
+
const rejected = (error) => ({
|
|
2009
|
+
kind: "rejected",
|
|
2010
|
+
error: errorBody(storageFailure(error, logger))
|
|
2011
|
+
});
|
|
2012
|
+
const plan = planUpload(base.size, storage.rules, { multipartThreshold: config.multipartThreshold });
|
|
2013
|
+
if (plan.kind === "single") {
|
|
2014
|
+
const [put, token] = await Promise.all([presignSingle(storage, base), signToken(base, signing)]);
|
|
2015
|
+
return put.error ? rejected(put.error) : {
|
|
2016
|
+
kind: "single",
|
|
2017
|
+
...ticket,
|
|
2018
|
+
token,
|
|
2019
|
+
put: put.data
|
|
2020
|
+
};
|
|
2021
|
+
}
|
|
2022
|
+
const created = await storage.createMultipart(base.key, objectOptions(base));
|
|
2023
|
+
if (created.error) return rejected(created.error);
|
|
2024
|
+
const token = {
|
|
2025
|
+
...base,
|
|
2026
|
+
uploadId: created.data.uploadId,
|
|
2027
|
+
partSize: plan.partSize
|
|
2028
|
+
};
|
|
2029
|
+
const first = Array.from({ length: Math.min(20, plan.partCount) }, (_, index) => index + 1);
|
|
2030
|
+
const abort = () => storage.abortMultipart(token.key, token.uploadId);
|
|
2031
|
+
const unattached = config.store && token.stored ? await attachUpload(config, config.store, token) : null;
|
|
2032
|
+
if (unattached) {
|
|
2033
|
+
await abort();
|
|
2034
|
+
return {
|
|
2035
|
+
kind: "rejected",
|
|
2036
|
+
error: errorBody(unattached)
|
|
2037
|
+
};
|
|
2038
|
+
}
|
|
2039
|
+
const [parts, signed] = await Promise.all([presignParts(storage, token, first), signToken(token, signing)]).catch(async (error) => {
|
|
2040
|
+
await abort();
|
|
2041
|
+
throw error;
|
|
2042
|
+
});
|
|
2043
|
+
if (parts.error) {
|
|
2044
|
+
await abort();
|
|
2045
|
+
return rejected(parts.error);
|
|
2046
|
+
}
|
|
2047
|
+
return {
|
|
2048
|
+
kind: "multipart",
|
|
2049
|
+
...ticket,
|
|
2050
|
+
token: signed,
|
|
2051
|
+
partSize: plan.partSize,
|
|
2052
|
+
partCount: plan.partCount,
|
|
2053
|
+
parts: parts.data
|
|
2054
|
+
};
|
|
2055
|
+
}
|
|
2056
|
+
async function startFileSafely(config, base) {
|
|
2057
|
+
try {
|
|
2058
|
+
return await startFile(config, base);
|
|
2059
|
+
} catch (error) {
|
|
2060
|
+
config.logger.error(`cloudrop: could not start the upload of file ${base.id} on route "${base.route}".`, error);
|
|
2061
|
+
return {
|
|
2062
|
+
kind: "rejected",
|
|
2063
|
+
error: errorBody(serverError())
|
|
2064
|
+
};
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
async function handleUpload({ request, body, config }) {
|
|
2068
|
+
const parsed = readRequest(body, config);
|
|
2069
|
+
if (parsed.error) return parsed;
|
|
2070
|
+
const { route, files } = parsed.data;
|
|
2071
|
+
const validated = await validateMetadata(route.definition.metadata, parsed.data.metadata);
|
|
2072
|
+
if (validated.error) return validated;
|
|
2073
|
+
const { value: metadata, json: metadataJson } = validated.data;
|
|
2074
|
+
const auth = await route.definition.authorize({
|
|
2075
|
+
request,
|
|
2076
|
+
metadata
|
|
2077
|
+
});
|
|
2078
|
+
if (isRefusal(auth)) return fail(refusalError(auth));
|
|
2079
|
+
if (!isPlainObject(auth)) throw new TypeError(`Route "${route.name}": authorize must return a plain object, unauthorized() or forbidden().`);
|
|
2080
|
+
const ownerJson = JSON.stringify(auth);
|
|
2081
|
+
if (ownerJson?.[0] !== "{") throw new TypeError(`Route "${route.name}": authorize returned an object whose toJSON does not give a JSON object. Return plain data or unauthorized().`);
|
|
2082
|
+
if (new TextEncoder().encode(ownerJson).length > 2048) throw new TypeError(`Route "${route.name}": authorize returned more than 2 KiB of JSON. Return ids, not records.`);
|
|
2083
|
+
const owner = JSON.parse(ownerJson);
|
|
2084
|
+
const signedMetadata = metadataJson === void 0 ? void 0 : JSON.parse(metadataJson);
|
|
2085
|
+
const prepared = files.map((file) => prepareFile(route, file, {
|
|
2086
|
+
request,
|
|
2087
|
+
metadata,
|
|
2088
|
+
auth
|
|
2089
|
+
}));
|
|
2090
|
+
const accepted = prepared.filter((file) => !(file instanceof CloudropError));
|
|
2091
|
+
const { plugins, store } = config;
|
|
2092
|
+
const rowOwner = store && accepted.length > 0 ? await readOwner(route, {
|
|
2093
|
+
...auth,
|
|
2094
|
+
request,
|
|
2095
|
+
metadata
|
|
2096
|
+
}) : null;
|
|
2097
|
+
if (accepted.length > 0) {
|
|
2098
|
+
const refused = await runBeforeUpload(plugins, {
|
|
2099
|
+
request,
|
|
2100
|
+
route: route.name,
|
|
2101
|
+
files: accepted,
|
|
2102
|
+
metadata,
|
|
2103
|
+
auth
|
|
2104
|
+
}, config.logger);
|
|
2105
|
+
if (refused) return fail(refused);
|
|
2106
|
+
}
|
|
2107
|
+
const exp = nowSeconds() + TOKEN_LIFETIME_SECONDS;
|
|
2108
|
+
const refusals = await reserveFiles(config, {
|
|
2109
|
+
route,
|
|
2110
|
+
owner: rowOwner,
|
|
2111
|
+
files: accepted,
|
|
2112
|
+
metadataJson,
|
|
2113
|
+
exp,
|
|
2114
|
+
requestKey: parsed.data.idempotencyKey
|
|
2115
|
+
});
|
|
2116
|
+
const tickets = await Promise.all(prepared.map((file) => {
|
|
2117
|
+
if (file instanceof CloudropError) return {
|
|
2118
|
+
kind: "rejected",
|
|
2119
|
+
error: errorBody(file)
|
|
2120
|
+
};
|
|
2121
|
+
const refused = refusals.get(file.id);
|
|
2122
|
+
return refused ? {
|
|
2123
|
+
kind: "rejected",
|
|
2124
|
+
error: errorBody(refused)
|
|
2125
|
+
} : startFileSafely(config, {
|
|
2126
|
+
v: 1,
|
|
2127
|
+
route: route.name,
|
|
2128
|
+
id: file.id,
|
|
2129
|
+
key: file.key,
|
|
2130
|
+
name: file.name,
|
|
2131
|
+
size: file.size,
|
|
2132
|
+
type: file.type,
|
|
2133
|
+
owner,
|
|
2134
|
+
metadata: signedMetadata,
|
|
2135
|
+
exp,
|
|
2136
|
+
...store && { stored: true }
|
|
2137
|
+
});
|
|
2138
|
+
}));
|
|
2139
|
+
const unstarted = prepared.filter((file, index) => !(file instanceof CloudropError) && tickets[index]?.kind === "rejected");
|
|
2140
|
+
await releaseUnstarted(config, {
|
|
2141
|
+
route: route.name,
|
|
2142
|
+
metadata,
|
|
2143
|
+
auth
|
|
2144
|
+
}, unstarted, refusals);
|
|
2145
|
+
return ok({ files: tickets });
|
|
2146
|
+
}
|
|
2147
|
+
// src/server/handler.ts
|
|
2148
|
+
const ENDPOINTS = {
|
|
2149
|
+
upload: handleUpload,
|
|
2150
|
+
sign: handleSign,
|
|
2151
|
+
complete: handleComplete,
|
|
2152
|
+
abort: handleAbort
|
|
2153
|
+
};
|
|
2154
|
+
function endpointOf(request, basePath) {
|
|
2155
|
+
const { pathname } = new URL(request.url);
|
|
2156
|
+
if (!pathname.startsWith(`${basePath}/`)) return null;
|
|
2157
|
+
const name = pathname.slice(basePath.length + 1);
|
|
2158
|
+
return Object.hasOwn(ENDPOINTS, name) ? name : null;
|
|
2159
|
+
}
|
|
2160
|
+
function contained(logger) {
|
|
2161
|
+
const call = (log) => {
|
|
2162
|
+
try {
|
|
2163
|
+
const result = log();
|
|
2164
|
+
if (typeof result?.then === "function") Promise.resolve(result).catch(() => {});
|
|
2165
|
+
} catch {}
|
|
2166
|
+
};
|
|
2167
|
+
return {
|
|
2168
|
+
error: (message, error) => call(() => logger.error(message, error)),
|
|
2169
|
+
warn: (message) => call(() => logger.warn(message))
|
|
2170
|
+
};
|
|
2171
|
+
}
|
|
2172
|
+
async function answer(request, config, report) {
|
|
2173
|
+
const endpoint = endpointOf(request, config.basePath);
|
|
2174
|
+
if (endpoint === null) return errorResponse(new CloudropError("NOT_FOUND", "There is no cloudrop endpoint at this path. Check basePath."));
|
|
2175
|
+
if (request.method === "OPTIONS") return preflight(request, config.trustedOrigins);
|
|
2176
|
+
if (request.method !== "POST") return errorResponse(new CloudropError("METHOD_NOT_ALLOWED", "Send this request as POST."), { allow: "POST, OPTIONS" });
|
|
2177
|
+
const origin = checkOrigin(request, config.trustedOrigins);
|
|
2178
|
+
if (!origin.allowed) {
|
|
2179
|
+
report.origin(request);
|
|
2180
|
+
return errorResponse(originNotAllowed());
|
|
2181
|
+
}
|
|
2182
|
+
if (!isJsonRequest(request)) return errorResponse(new CloudropError("UNSUPPORTED_MEDIA_TYPE", "Send the request body as JSON with Content-Type: application/json."), origin.cors);
|
|
2183
|
+
const body = await readJson(request);
|
|
2184
|
+
if (body.error) return errorResponse(body.error, origin.cors);
|
|
2185
|
+
try {
|
|
2186
|
+
const ready = await config.ready();
|
|
2187
|
+
if (ready.error) {
|
|
2188
|
+
report.notReady(ready.error);
|
|
2189
|
+
return errorResponse(new CloudropError("SERVER_ERROR", "The upload server is not ready. Try again in a moment.", { retryable: true }), origin.cors);
|
|
2190
|
+
}
|
|
2191
|
+
const result = await ENDPOINTS[endpoint]({
|
|
2192
|
+
request,
|
|
2193
|
+
body: body.data,
|
|
2194
|
+
config: {
|
|
2195
|
+
...config,
|
|
2196
|
+
tokenKeys: ready.data
|
|
2197
|
+
}
|
|
2198
|
+
});
|
|
2199
|
+
if (!result.error) return jsonResponse(result.data, 200, origin.cors);
|
|
2200
|
+
if (!hasKnownCode(result.error)) config.logger.error(`cloudrop: /${endpoint} failed with an error code the server cannot send, so the client got a generic 500. Return a CloudropError with one of cloudrop's server codes, not a client-only code such as ABORTED.`, result.error);
|
|
2201
|
+
return errorResponse(result.error, origin.cors);
|
|
2202
|
+
} catch (error) {
|
|
2203
|
+
config.logger.error(`cloudrop: /${endpoint} failed on the server.`, error);
|
|
2204
|
+
return errorResponse(serverError(), origin.cors);
|
|
2205
|
+
}
|
|
2206
|
+
}
|
|
2207
|
+
async function warnAboutBucket(config, pageOrigin) {
|
|
2208
|
+
const report = await checkBucket$1(config, [.../* @__PURE__ */ new Set([pageOrigin, ...config.trustedOrigins])]);
|
|
2209
|
+
if (report.error) {
|
|
2210
|
+
config.logger.warn(`cloudrop: could not check the bucket settings: ${report.error.message}`);
|
|
2211
|
+
return;
|
|
2212
|
+
}
|
|
2213
|
+
for (const check of Object.values(report.data)) if (check.status === "problem") config.logger.warn(`cloudrop: ${check.message}`);
|
|
2214
|
+
}
|
|
2215
|
+
const MAX_HINTED_ORIGINS = 16;
|
|
2216
|
+
function originHint(logger) {
|
|
2217
|
+
const hinted = /* @__PURE__ */ new Set();
|
|
2218
|
+
return (request) => {
|
|
2219
|
+
const origin = request.headers.get("origin");
|
|
2220
|
+
if (!isExactOrigin(origin) || hinted.has(origin) || hinted.size >= MAX_HINTED_ORIGINS) return;
|
|
2221
|
+
hinted.add(origin);
|
|
2222
|
+
logger.warn(`cloudrop: refused a request from ${origin}, but this server sees its own origin as ${new URL(request.url).origin}. If a proxy terminates TLS or rewrites Host, add the app's public origin to trustedOrigins.`);
|
|
2223
|
+
};
|
|
2224
|
+
}
|
|
2225
|
+
function notReadyReport(logger) {
|
|
2226
|
+
let reported;
|
|
2227
|
+
return (error) => {
|
|
2228
|
+
if (error.message === reported) return;
|
|
2229
|
+
reported = error.message;
|
|
2230
|
+
logger.error(`cloudrop: every request answers 500 until this is fixed. ${error.message}`, error);
|
|
2231
|
+
};
|
|
2232
|
+
}
|
|
2233
|
+
function createHandler(given) {
|
|
2234
|
+
const config = {
|
|
2235
|
+
...given,
|
|
2236
|
+
logger: contained(given.logger)
|
|
2237
|
+
};
|
|
2238
|
+
const report = {
|
|
2239
|
+
origin: originHint(config.logger),
|
|
2240
|
+
notReady: notReadyReport(config.logger)
|
|
2241
|
+
};
|
|
2242
|
+
let bucketChecked = false;
|
|
2243
|
+
return async (request) => {
|
|
2244
|
+
try {
|
|
2245
|
+
if (!bucketChecked && fromEnv("NODE_ENV") === "development") {
|
|
2246
|
+
const pageOrigin = new URL(request.url).origin;
|
|
2247
|
+
bucketChecked = true;
|
|
2248
|
+
warnAboutBucket(config, pageOrigin).catch((error) => {
|
|
2249
|
+
config.logger.error("cloudrop: the development bucket check failed.", error);
|
|
2250
|
+
});
|
|
2251
|
+
}
|
|
2252
|
+
return await answer(request, config, report);
|
|
2253
|
+
} catch (error) {
|
|
2254
|
+
config.logger.error("cloudrop: the handler failed before any endpoint ran. Pass it a Fetch API Request.", error);
|
|
2255
|
+
return errorResponse(serverError());
|
|
2256
|
+
}
|
|
2257
|
+
};
|
|
2258
|
+
}
|
|
2259
|
+
// src/server/cloudrop.ts
|
|
2260
|
+
function createCloudrop(options) {
|
|
2261
|
+
const config = readConfig(options);
|
|
2262
|
+
return {
|
|
2263
|
+
handler: createHandler(config),
|
|
2264
|
+
api: createFileApi(config)
|
|
2265
|
+
};
|
|
2266
|
+
}
|
|
2267
|
+
// src/storage/storage-error.ts
|
|
2268
|
+
const INVALID_PART_CODES = /* @__PURE__ */ new Set([
|
|
2269
|
+
"InvalidPart",
|
|
2270
|
+
"InvalidPartOrder",
|
|
2271
|
+
"EntityTooSmall"
|
|
2272
|
+
]);
|
|
2273
|
+
const RULES = [
|
|
2274
|
+
{
|
|
2275
|
+
matches: (status, storageCode) => status === 404 && storageCode === "NoSuchUpload",
|
|
2276
|
+
code: "NO_SUCH_UPLOAD",
|
|
2277
|
+
message: "The multipart upload no longer exists. Start a new upload."
|
|
2278
|
+
},
|
|
2279
|
+
{
|
|
2280
|
+
matches: (status, storageCode) => status === 404 && storageCode === "NoSuchBucket",
|
|
2281
|
+
code: "STORAGE_ERROR",
|
|
2282
|
+
message: "The bucket does not exist. Check the bucket name in your configuration."
|
|
2283
|
+
},
|
|
2284
|
+
{
|
|
2285
|
+
matches: (status) => status === 404,
|
|
2286
|
+
code: "NOT_FOUND",
|
|
2287
|
+
message: "The object does not exist. Check the key."
|
|
2288
|
+
},
|
|
2289
|
+
{
|
|
2290
|
+
matches: (status) => status === 412,
|
|
2291
|
+
code: "ALREADY_EXISTS",
|
|
2292
|
+
message: "An object with this key already exists. Choose a different key."
|
|
2293
|
+
},
|
|
2294
|
+
{
|
|
2295
|
+
matches: (_status, storageCode) => storageCode !== void 0 && INVALID_PART_CODES.has(storageCode),
|
|
2296
|
+
code: "INVALID_PART",
|
|
2297
|
+
message: "One or more parts are missing or have the wrong size. Upload them again, then complete the upload."
|
|
2298
|
+
},
|
|
2299
|
+
{
|
|
2300
|
+
matches: (_status, storageCode) => storageCode === "BadDigest",
|
|
2301
|
+
code: "CHECKSUM_MISMATCH",
|
|
2302
|
+
message: "The uploaded bytes do not match their checksum. Upload them again."
|
|
2303
|
+
},
|
|
2304
|
+
{
|
|
2305
|
+
matches: (_status, storageCode) => storageCode === "RequestTimeTooSkewed",
|
|
2306
|
+
code: "STORAGE_DENIED",
|
|
2307
|
+
message: "This server's clock is more than 15 minutes off from the storage service's clock. Sync the clock (NTP), then retry."
|
|
2308
|
+
},
|
|
2309
|
+
{
|
|
2310
|
+
matches: (_status, storageCode) => storageCode === "ExpiredRequest",
|
|
2311
|
+
code: "STORAGE_DENIED",
|
|
2312
|
+
message: "The signed request expired. Sign it again and retry."
|
|
2313
|
+
},
|
|
2314
|
+
{
|
|
2315
|
+
matches: (status) => status === 403,
|
|
2316
|
+
code: "STORAGE_DENIED",
|
|
2317
|
+
message: "The storage service refused the request. Check the access key, its permissions and the bucket name."
|
|
2318
|
+
},
|
|
2319
|
+
{
|
|
2320
|
+
matches: (status) => status === 429 || status === 503,
|
|
2321
|
+
code: "RATE_LIMITED",
|
|
2322
|
+
message: "The storage service is throttling requests. Retry after a short delay.",
|
|
2323
|
+
retryable: true
|
|
2324
|
+
},
|
|
2325
|
+
{
|
|
2326
|
+
matches: (status) => status === 501,
|
|
2327
|
+
code: "STORAGE_ERROR",
|
|
2328
|
+
message: "The storage service does not support this request. Use R2 or another backend that implements it."
|
|
2329
|
+
},
|
|
2330
|
+
{
|
|
2331
|
+
matches: (status, storageCode) => status >= 500 || status === 408 || storageCode === "OperationAborted",
|
|
2332
|
+
code: "STORAGE_ERROR",
|
|
2333
|
+
message: "The storage service failed. Retry the request.",
|
|
2334
|
+
retryable: true
|
|
2335
|
+
}
|
|
2336
|
+
];
|
|
2337
|
+
function storageError(status, body) {
|
|
2338
|
+
const rawCode = xmlValue(body, "Code");
|
|
2339
|
+
const storageCode = rawCode && /^[A-Za-z0-9]{1,64}$/.test(rawCode) ? rawCode : void 0;
|
|
2340
|
+
const rule = RULES.find((candidate) => candidate.matches(status, storageCode));
|
|
2341
|
+
if (rule) return new CloudropError(rule.code, rule.message, {
|
|
2342
|
+
status,
|
|
2343
|
+
storageCode,
|
|
2344
|
+
retryable: rule.retryable
|
|
2345
|
+
});
|
|
2346
|
+
return new CloudropError("STORAGE_ERROR", `The storage service rejected the request (HTTP ${status}). Check the key, size and headers you passed, then retry.`, {
|
|
2347
|
+
status,
|
|
2348
|
+
storageCode
|
|
2349
|
+
});
|
|
2350
|
+
}
|
|
2351
|
+
// src/storage/s3/transport.ts
|
|
2352
|
+
const decoder = new TextDecoder();
|
|
2353
|
+
const REQUEST_TIMEOUT_MS = 3e4;
|
|
2354
|
+
const SLOW_REQUEST_TIMEOUT_MS = 3e5;
|
|
2355
|
+
function quotesCredentials(error, depth = 0) {
|
|
2356
|
+
if (depth > 8) return true;
|
|
2357
|
+
if (typeof error === "string") return error.includes("Credential=");
|
|
2358
|
+
if (typeof error !== "object" || error === null) return false;
|
|
2359
|
+
const { message, cause } = error;
|
|
2360
|
+
return typeof message === "string" && message.includes("Credential=") || quotesCredentials(cause, depth + 1);
|
|
2361
|
+
}
|
|
2362
|
+
function unreachable(cause, timeoutMs) {
|
|
2363
|
+
let timedOut = false;
|
|
2364
|
+
let safeCause;
|
|
2365
|
+
try {
|
|
2366
|
+
timedOut = cause instanceof DOMException && cause.name === "TimeoutError";
|
|
2367
|
+
if (!quotesCredentials(cause)) safeCause = cause;
|
|
2368
|
+
} catch {}
|
|
2369
|
+
return new CloudropError("STORAGE_UNREACHABLE", timedOut ? `The storage service did not answer within ${timeoutMs / 1e3} seconds. Retry the request.` : "Could not reach the storage service. Check the endpoint and the network connection, then retry.", {
|
|
2370
|
+
retryable: true,
|
|
2371
|
+
cause: safeCause
|
|
2372
|
+
});
|
|
2373
|
+
}
|
|
2374
|
+
async function send(ctx, method, url, options = {}) {
|
|
2375
|
+
const { http, now, credentials, region } = ctx;
|
|
2376
|
+
url.search = canonicalQuery(url.searchParams);
|
|
2377
|
+
const { headers = {}, body, timeoutMs = REQUEST_TIMEOUT_MS } = options;
|
|
2378
|
+
const signed = await signRequest({
|
|
2379
|
+
method,
|
|
2380
|
+
url,
|
|
2381
|
+
headers,
|
|
2382
|
+
credentials,
|
|
2383
|
+
region,
|
|
2384
|
+
date: now(),
|
|
2385
|
+
payloadHash: body instanceof Blob ? UNSIGNED_PAYLOAD : body ? await sha256Hex(body) : EMPTY_SHA256
|
|
2386
|
+
});
|
|
2387
|
+
try {
|
|
2388
|
+
const response = await http(url, {
|
|
2389
|
+
method,
|
|
2390
|
+
headers: signed,
|
|
2391
|
+
body,
|
|
2392
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
2393
|
+
});
|
|
2394
|
+
if (!response.ok) return fail(storageError(response.status, await response.text()));
|
|
2395
|
+
if (headers.range !== void 0 && response.status !== 206) {
|
|
2396
|
+
await response.body?.cancel();
|
|
2397
|
+
return fail(new CloudropError("STORAGE_ERROR", "The storage service ignored the byte range. Check that the endpoint supports ranged GET.", { status: response.status }));
|
|
2398
|
+
}
|
|
2399
|
+
return ok({
|
|
2400
|
+
headers: response.headers,
|
|
2401
|
+
body: new Uint8Array(await response.arrayBuffer())
|
|
2402
|
+
});
|
|
2403
|
+
} catch (cause) {
|
|
2404
|
+
return fail(unreachable(cause, timeoutMs));
|
|
2405
|
+
}
|
|
2406
|
+
}
|
|
2407
|
+
async function sendForXml(ctx, method, url, options) {
|
|
2408
|
+
const result = await send(ctx, method, url, options);
|
|
2409
|
+
if (result.error) return result;
|
|
2410
|
+
const text = decoder.decode(result.data.body);
|
|
2411
|
+
if (text.includes("<Error>")) return fail(storageError(500, text));
|
|
2412
|
+
return ok(text);
|
|
2413
|
+
}
|
|
2414
|
+
// src/storage/s3/bucket-settings.ts
|
|
2415
|
+
function configurationReadError(error, missingCode, setting) {
|
|
2416
|
+
if (error.status === 404 && error.storageCode === missingCode) return ok([]);
|
|
2417
|
+
if (error.code !== "NOT_FOUND") return fail(error);
|
|
2418
|
+
return fail(new CloudropError("STORAGE_ERROR", `The storage service answered 404 without saying that the bucket has no ${setting} configuration. Check the endpoint, then retry.`, {
|
|
2419
|
+
status: 404,
|
|
2420
|
+
storageCode: error.storageCode
|
|
2421
|
+
}));
|
|
2422
|
+
}
|
|
2423
|
+
function readLifecycleRule(rule) {
|
|
2424
|
+
const days = xmlValue(xmlBlocks(rule, "AbortIncompleteMultipartUpload")[0] ?? "", "DaysAfterInitiation");
|
|
2425
|
+
if (days !== void 0 && !/^\d{1,9}$/.test(days)) return fail(incompleteResponse("valid multipart abort days"));
|
|
2426
|
+
return ok({
|
|
2427
|
+
id: xmlText(rule, "ID") ?? "",
|
|
2428
|
+
enabled: xmlValue(rule, "Status") === "Enabled",
|
|
2429
|
+
prefix: xmlText(rule, "Prefix") ?? "",
|
|
2430
|
+
...days === void 0 ? {} : { abortIncompleteMultipartUploadDays: Number(days) }
|
|
2431
|
+
});
|
|
2432
|
+
}
|
|
2433
|
+
function bucketSettingsMethods(ctx) {
|
|
2434
|
+
return {
|
|
2435
|
+
async getBucketCors() {
|
|
2436
|
+
const result = await sendForXml(ctx, "GET", new URL(`${ctx.bucketUrl}?cors=`));
|
|
2437
|
+
if (result.error) return configurationReadError(result.error, "NoSuchCORSConfiguration", "CORS");
|
|
2438
|
+
const configuration = xmlBlocks(result.data, "CORSConfiguration")[0];
|
|
2439
|
+
if (configuration === void 0) return fail(incompleteResponse("CORS configuration"));
|
|
2440
|
+
return ok(xmlBlocks(configuration, "CORSRule").map((rule) => ({
|
|
2441
|
+
allowedOrigins: xmlValues(rule, "AllowedOrigin"),
|
|
2442
|
+
allowedMethods: xmlValues(rule, "AllowedMethod"),
|
|
2443
|
+
allowedHeaders: xmlValues(rule, "AllowedHeader"),
|
|
2444
|
+
exposeHeaders: xmlValues(rule, "ExposeHeader")
|
|
2445
|
+
})));
|
|
2446
|
+
},
|
|
2447
|
+
async getBucketLifecycle() {
|
|
2448
|
+
const result = await sendForXml(ctx, "GET", new URL(`${ctx.bucketUrl}?lifecycle=`));
|
|
2449
|
+
if (result.error) return configurationReadError(result.error, "NoSuchLifecycleConfiguration", "lifecycle");
|
|
2450
|
+
const configuration = xmlBlocks(result.data, "LifecycleConfiguration")[0];
|
|
2451
|
+
if (configuration === void 0) return fail(incompleteResponse("lifecycle configuration"));
|
|
2452
|
+
return allOk(xmlBlocks(configuration, "Rule").map(readLifecycleRule));
|
|
2453
|
+
}
|
|
2454
|
+
};
|
|
2455
|
+
}
|
|
2456
|
+
// src/storage/s3/settings.ts
|
|
2457
|
+
function checkBaseUrl(value, setting, example) {
|
|
2458
|
+
const url = typeof value === "string" && URL.canParse(value) ? new URL(value) : null;
|
|
2459
|
+
if (url?.username || url?.password) throw configError(`The ${setting} contains a user name or password. Remove it; cloudrop takes credentials only as accessKeyId and secretAccessKey.`);
|
|
2460
|
+
if (!url || !/^https?:$/.test(url.protocol) || url.href !== `${url.origin}${url.pathname}`) throw configError(`The ${setting} must be an http or https URL with no query or fragment, such as ${example}.`);
|
|
2461
|
+
try {
|
|
2462
|
+
for (const segment of url.pathname.split("/")) decodeURIComponent(segment);
|
|
2463
|
+
} catch {
|
|
2464
|
+
throw configError(`The ${setting}'s path contains a malformed percent escape, or one that is not valid UTF-8 (such as %zz or %FF). Write a literal percent sign as %25.`);
|
|
2465
|
+
}
|
|
2466
|
+
return url;
|
|
2467
|
+
}
|
|
2468
|
+
const SIGNABLE = /^[\x21-\x7e]+$/;
|
|
2469
|
+
function checkSigningSettings({ credentials, region }) {
|
|
2470
|
+
for (const [setting, value] of Object.entries(credentials)) if (typeof value !== "string" || !SIGNABLE.test(value)) throw configError(`The ${setting} setting must be printable ASCII text with no whitespace or line break; secret files often end in a newline. Copy the value again without it.`);
|
|
2471
|
+
if (typeof region !== "string" || !SIGNABLE.test(region) || region.includes("/")) throw configError("The region setting must be printable ASCII text with no whitespace, line break or slash, such as us-east-1 or auto.");
|
|
2472
|
+
}
|
|
2473
|
+
// src/storage/s3/context.ts
|
|
2474
|
+
function createContext(config) {
|
|
2475
|
+
const http = config.fetch ?? ((input, init) => globalThis.fetch(input, init));
|
|
2476
|
+
if (typeof http !== "function") throw configError("The fetch option must be a function, such as globalThis.fetch. Omit it to use the global fetch.");
|
|
2477
|
+
checkSigningSettings(config);
|
|
2478
|
+
const now = config.now ?? (() => /* @__PURE__ */ new Date());
|
|
2479
|
+
const { credentials, region, rules } = config;
|
|
2480
|
+
const endpoint = checkBaseUrl(config.endpoint, "storage endpoint", "https://<account ID>.r2.cloudflarestorage.com");
|
|
2481
|
+
const bucketUrl = `${endpoint.origin}${endpoint.pathname.replace(/\/$/, "")}/${encodeRfc3986(config.bucket)}`;
|
|
2482
|
+
return {
|
|
2483
|
+
http,
|
|
2484
|
+
now,
|
|
2485
|
+
credentials,
|
|
2486
|
+
region,
|
|
2487
|
+
rules,
|
|
2488
|
+
bucket: config.bucket,
|
|
2489
|
+
bucketUrl
|
|
2490
|
+
};
|
|
2491
|
+
}
|
|
2492
|
+
function objectUrl(ctx, key, query = {}) {
|
|
2493
|
+
const url = new URL(`${ctx.bucketUrl}/${encodeKeyPath(key)}`);
|
|
2494
|
+
for (const [name, value] of Object.entries(query)) url.searchParams.set(name, value);
|
|
2495
|
+
return url;
|
|
2496
|
+
}
|
|
2497
|
+
// src/storage/s3/options.ts
|
|
2498
|
+
const META_PREFIX = "x-amz-meta-";
|
|
2499
|
+
function objectHeaders(options) {
|
|
2500
|
+
const headers = {};
|
|
2501
|
+
if (options.type) headers["content-type"] = options.type;
|
|
2502
|
+
if (options.disposition) headers["content-disposition"] = options.disposition;
|
|
2503
|
+
for (const [name, value] of Object.entries(options.metadata ?? {})) headers[`${META_PREFIX}${name}`] = value;
|
|
2504
|
+
return headers;
|
|
2505
|
+
}
|
|
2506
|
+
function writeHeaders(options) {
|
|
2507
|
+
const headers = objectHeaders(options);
|
|
2508
|
+
if (options.ifNoneMatch) headers["if-none-match"] = "*";
|
|
2509
|
+
if (options.checksumCrc32c) headers["x-amz-checksum-crc32c"] = options.checksumCrc32c;
|
|
2510
|
+
return headers;
|
|
2511
|
+
}
|
|
2512
|
+
function orEmpty(options) {
|
|
2513
|
+
return options ?? {};
|
|
2514
|
+
}
|
|
2515
|
+
// src/storage/s3/multipart.ts
|
|
2516
|
+
const encoder$1 = new TextEncoder();
|
|
2517
|
+
function readPart(block, maxParts) {
|
|
2518
|
+
const partNumber = wholeNumber(xmlValue(block, "PartNumber"));
|
|
2519
|
+
if (partNumber === void 0 || partNumber < 1 || partNumber > maxParts) return fail(incompleteResponse("valid part number"));
|
|
2520
|
+
const size = wholeNumber(xmlValue(block, "Size"));
|
|
2521
|
+
if (size === void 0) return fail(incompleteResponse("valid part size"));
|
|
2522
|
+
const etag = etagIn(block);
|
|
2523
|
+
if (etag === "") return fail(incompleteResponse("part ETag"));
|
|
2524
|
+
return ok({
|
|
2525
|
+
partNumber,
|
|
2526
|
+
size,
|
|
2527
|
+
etag,
|
|
2528
|
+
checksumCrc32c: xmlValue(block, "ChecksumCRC32C")
|
|
2529
|
+
});
|
|
2530
|
+
}
|
|
2531
|
+
async function partsPage(ctx, key, uploadId, marker) {
|
|
2532
|
+
const result = await sendForXml(ctx, "GET", objectUrl(ctx, key, {
|
|
2533
|
+
uploadId,
|
|
2534
|
+
"max-parts": String(MAX_LIST_PAGE),
|
|
2535
|
+
"part-number-marker": marker
|
|
2536
|
+
}));
|
|
2537
|
+
if (result.error) return result;
|
|
2538
|
+
const parts = allOk(xmlBlocks(result.data, "Part").map((block) => readPart(block, ctx.rules.maxParts)));
|
|
2539
|
+
if (parts.error) return parts;
|
|
2540
|
+
if (xmlValue(result.data, "IsTruncated") !== "true") return ok({
|
|
2541
|
+
parts: parts.data,
|
|
2542
|
+
next: null
|
|
2543
|
+
});
|
|
2544
|
+
const next = xmlValue(result.data, "NextPartNumberMarker") ?? "";
|
|
2545
|
+
if (!(Number(next) > Number(marker)) || Number(next) > ctx.rules.maxParts) return fail(incompleteResponse("valid next part marker"));
|
|
2546
|
+
return ok({
|
|
2547
|
+
parts: parts.data,
|
|
2548
|
+
next
|
|
2549
|
+
});
|
|
2550
|
+
}
|
|
2551
|
+
async function startMultipart(ctx, key, options) {
|
|
2552
|
+
const result = await sendForXml(ctx, "POST", objectUrl(ctx, key, { uploads: "" }), { headers: objectHeaders(options) });
|
|
2553
|
+
if (result.error) return result;
|
|
2554
|
+
const uploadId = xmlText(result.data, "UploadId");
|
|
2555
|
+
return uploadId ? ok({ uploadId }) : fail(incompleteResponse("upload ID"));
|
|
2556
|
+
}
|
|
2557
|
+
function multipartMethods(ctx) {
|
|
2558
|
+
const { rules } = ctx;
|
|
2559
|
+
return {
|
|
2560
|
+
async createMultipart(key, given) {
|
|
2561
|
+
const options = orEmpty(given);
|
|
2562
|
+
const problem = keyProblem(key) ?? objectOptionsProblem(options);
|
|
2563
|
+
if (problem) return fail(invalidInput(problem));
|
|
2564
|
+
return startMultipart(ctx, key, options);
|
|
2565
|
+
},
|
|
2566
|
+
async listParts(key, uploadId) {
|
|
2567
|
+
const problem = keyProblem(key) ?? uploadIdProblem(uploadId);
|
|
2568
|
+
if (problem) return fail(invalidInput(problem));
|
|
2569
|
+
const parts = [];
|
|
2570
|
+
let marker = "0";
|
|
2571
|
+
while (marker !== null) {
|
|
2572
|
+
const page = await partsPage(ctx, key, uploadId, marker);
|
|
2573
|
+
if (page.error) return page;
|
|
2574
|
+
parts.push(...page.data.parts);
|
|
2575
|
+
marker = page.data.next;
|
|
2576
|
+
}
|
|
2577
|
+
return ok(parts);
|
|
2578
|
+
},
|
|
2579
|
+
async completeMultipart(key, uploadId, parts, given) {
|
|
2580
|
+
const options = orEmpty(given);
|
|
2581
|
+
const problem = keyProblem(key) ?? uploadIdProblem(uploadId) ?? partsProblem(parts, rules.maxParts);
|
|
2582
|
+
if (problem) return fail(invalidInput(problem));
|
|
2583
|
+
const xml = parts.toSorted((a, b) => a.partNumber - b.partNumber).map((part) => `<Part><PartNumber>${part.partNumber}</PartNumber><ETag>"${escapeXml(stripQuotes(part.etag))}"</ETag></Part>`).join("");
|
|
2584
|
+
const result = await sendForXml(ctx, "POST", objectUrl(ctx, key, { uploadId }), {
|
|
2585
|
+
headers: options.ifNoneMatch ? { "if-none-match": "*" } : {},
|
|
2586
|
+
body: encoder$1.encode(`<CompleteMultipartUpload>${xml}</CompleteMultipartUpload>`),
|
|
2587
|
+
timeoutMs: SLOW_REQUEST_TIMEOUT_MS
|
|
2588
|
+
});
|
|
2589
|
+
if (result.error) return result;
|
|
2590
|
+
const etag = etagIn(result.data);
|
|
2591
|
+
return etag ? ok({ etag }) : fail(incompleteResponse("ETag"));
|
|
2592
|
+
},
|
|
2593
|
+
async abortMultipart(key, uploadId) {
|
|
2594
|
+
const problem = keyProblem(key) ?? uploadIdProblem(uploadId);
|
|
2595
|
+
if (problem) return fail(invalidInput(problem));
|
|
2596
|
+
const result = await send(ctx, "DELETE", objectUrl(ctx, key, { uploadId }));
|
|
2597
|
+
if (result.error && result.error.code !== "NO_SUCH_UPLOAD") return result;
|
|
2598
|
+
return ok(void 0);
|
|
2599
|
+
}
|
|
2600
|
+
};
|
|
2601
|
+
}
|
|
2602
|
+
// src/storage/s3/copy.ts
|
|
2603
|
+
function copyError(error) {
|
|
2604
|
+
if (error.status !== 412) return error;
|
|
2605
|
+
return new CloudropError("STORAGE_ERROR", "The object to copy changed during the copy. Retry the copy.", {
|
|
2606
|
+
status: 412,
|
|
2607
|
+
storageCode: error.storageCode,
|
|
2608
|
+
retryable: true
|
|
2609
|
+
});
|
|
2610
|
+
}
|
|
2611
|
+
async function copyInParts(ctx, steps, source, copyHeaders, to) {
|
|
2612
|
+
const partSize = ctx.rules.maxSinglePutSize;
|
|
2613
|
+
const partCount = Math.ceil(source.size / partSize);
|
|
2614
|
+
const created = await startMultipart(ctx, to, source);
|
|
2615
|
+
if (created.error) return created;
|
|
2616
|
+
const { uploadId } = created.data;
|
|
2617
|
+
const abortWith = async (error) => {
|
|
2618
|
+
await steps.abortMultipart(to, uploadId);
|
|
2619
|
+
return fail(error);
|
|
2620
|
+
};
|
|
2621
|
+
const parts = [];
|
|
2622
|
+
for (let partNumber = 1; partNumber <= partCount; partNumber++) {
|
|
2623
|
+
const start = (partNumber - 1) * partSize;
|
|
2624
|
+
const end = Math.min(start + partSize, source.size) - 1;
|
|
2625
|
+
const result = await sendForXml(ctx, "PUT", objectUrl(ctx, to, {
|
|
2626
|
+
partNumber: String(partNumber),
|
|
2627
|
+
uploadId
|
|
2628
|
+
}), {
|
|
2629
|
+
headers: {
|
|
2630
|
+
...copyHeaders,
|
|
2631
|
+
"x-amz-copy-source-range": `bytes=${start}-${end}`
|
|
2632
|
+
},
|
|
2633
|
+
timeoutMs: SLOW_REQUEST_TIMEOUT_MS
|
|
2634
|
+
});
|
|
2635
|
+
if (result.error) return abortWith(copyError(result.error));
|
|
2636
|
+
const etag = etagIn(result.data);
|
|
2637
|
+
if (!etag) return abortWith(incompleteResponse("ETag"));
|
|
2638
|
+
parts.push({
|
|
2639
|
+
partNumber,
|
|
2640
|
+
etag
|
|
2641
|
+
});
|
|
2642
|
+
}
|
|
2643
|
+
const completed = await steps.completeMultipart(to, uploadId, parts);
|
|
2644
|
+
return completed.error ? abortWith(completed.error) : completed;
|
|
2645
|
+
}
|
|
2646
|
+
function copyMethods(ctx, steps) {
|
|
2647
|
+
return { async copy(from, to) {
|
|
2648
|
+
const problem = keyProblem(from) ?? keyProblem(to);
|
|
2649
|
+
if (problem) return fail(invalidInput(problem));
|
|
2650
|
+
const source = await steps.head(from);
|
|
2651
|
+
if (source.error) return source;
|
|
2652
|
+
if (!source.data) return fail(new CloudropError("NOT_FOUND", "The object to copy does not exist. Check the source key."));
|
|
2653
|
+
const copyHeaders = {
|
|
2654
|
+
"x-amz-copy-source": `/${encodeRfc3986(ctx.bucket)}/${encodeKeyPath(from)}`,
|
|
2655
|
+
"x-amz-copy-source-if-match": `"${source.data.etag}"`
|
|
2656
|
+
};
|
|
2657
|
+
if (source.data.size > ctx.rules.maxSinglePutSize) return copyInParts(ctx, steps, source.data, copyHeaders, to);
|
|
2658
|
+
const result = await sendForXml(ctx, "PUT", objectUrl(ctx, to), {
|
|
2659
|
+
headers: copyHeaders,
|
|
2660
|
+
timeoutMs: SLOW_REQUEST_TIMEOUT_MS
|
|
2661
|
+
});
|
|
2662
|
+
if (result.error) return fail(copyError(result.error));
|
|
2663
|
+
const etag = etagIn(result.data);
|
|
2664
|
+
return etag ? ok({ etag }) : fail(incompleteResponse("ETag"));
|
|
2665
|
+
} };
|
|
2666
|
+
}
|
|
2667
|
+
// src/storage/s3/objects.ts
|
|
2668
|
+
const encoder = new TextEncoder();
|
|
2669
|
+
const DELETE_CONCURRENCY = 16;
|
|
2670
|
+
function readObjectInfo(key, headers) {
|
|
2671
|
+
if (!headers.has("content-length")) return fail(incompleteResponse("content length"));
|
|
2672
|
+
const size = wholeNumber(headers.get("content-length"));
|
|
2673
|
+
if (size === void 0) return fail(incompleteResponse("valid content length"));
|
|
2674
|
+
const etag = stripQuotes(headers.get("etag") ?? "");
|
|
2675
|
+
if (etag === "") return fail(incompleteResponse("ETag"));
|
|
2676
|
+
const uploadedAt = validDate(headers.get("last-modified"));
|
|
2677
|
+
if (uploadedAt === void 0) return fail(incompleteResponse("valid last-modified date"));
|
|
2678
|
+
const metadata = {};
|
|
2679
|
+
headers.forEach((value, name) => {
|
|
2680
|
+
if (name.startsWith("x-amz-meta-")) metadata[name.slice(11)] = value;
|
|
2681
|
+
});
|
|
2682
|
+
return ok({
|
|
2683
|
+
key,
|
|
2684
|
+
size,
|
|
2685
|
+
type: headers.get("content-type") ?? void 0,
|
|
2686
|
+
disposition: headers.get("content-disposition") ?? void 0,
|
|
2687
|
+
etag,
|
|
2688
|
+
uploadedAt,
|
|
2689
|
+
metadata,
|
|
2690
|
+
checksumCrc64nvme: headers.get("x-amz-checksum-crc64nvme") ?? void 0
|
|
2691
|
+
});
|
|
2692
|
+
}
|
|
2693
|
+
function toPayload(body) {
|
|
2694
|
+
if (typeof body === "string") return encoder.encode(body);
|
|
2695
|
+
return body instanceof ArrayBuffer ? new Uint8Array(body) : body;
|
|
2696
|
+
}
|
|
2697
|
+
function readListedObject(block) {
|
|
2698
|
+
const key = xmlText(block, "Key");
|
|
2699
|
+
if (!key) return fail(incompleteResponse("object key"));
|
|
2700
|
+
const size = wholeNumber(xmlValue(block, "Size"));
|
|
2701
|
+
if (size === void 0) return fail(incompleteResponse("valid object size"));
|
|
2702
|
+
const uploadedAt = validDate(xmlValue(block, "LastModified"));
|
|
2703
|
+
if (uploadedAt === void 0) return fail(incompleteResponse("valid last-modified date"));
|
|
2704
|
+
return ok({
|
|
2705
|
+
key,
|
|
2706
|
+
size,
|
|
2707
|
+
etag: etagIn(block),
|
|
2708
|
+
uploadedAt
|
|
2709
|
+
});
|
|
2710
|
+
}
|
|
2711
|
+
async function forEachConcurrently(items, limit, task) {
|
|
2712
|
+
let next = 0;
|
|
2713
|
+
const worker = async () => {
|
|
2714
|
+
while (next < items.length) {
|
|
2715
|
+
const item = items[next];
|
|
2716
|
+
next += 1;
|
|
2717
|
+
await task(item);
|
|
2718
|
+
}
|
|
2719
|
+
};
|
|
2720
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
|
|
2721
|
+
}
|
|
2722
|
+
function objectMethods(ctx) {
|
|
2723
|
+
const { rules } = ctx;
|
|
2724
|
+
const methods = {
|
|
2725
|
+
async head(key) {
|
|
2726
|
+
const problem = keyProblem(key);
|
|
2727
|
+
if (problem) return fail(invalidInput(problem));
|
|
2728
|
+
const result = await send(ctx, "HEAD", objectUrl(ctx, key), { headers: { "x-amz-checksum-mode": "ENABLED" } });
|
|
2729
|
+
if (result.error) return result.error.code === "NOT_FOUND" ? ok(null) : result;
|
|
2730
|
+
return readObjectInfo(key, result.data.headers);
|
|
2731
|
+
},
|
|
2732
|
+
async getRange(key, start, end) {
|
|
2733
|
+
const problem = keyProblem(key) ?? rangeProblem(start, end);
|
|
2734
|
+
if (problem) return fail(invalidInput(problem));
|
|
2735
|
+
const result = await send(ctx, "GET", objectUrl(ctx, key), { headers: { range: `bytes=${start}-${end}` } });
|
|
2736
|
+
if (result.error) return result;
|
|
2737
|
+
return ok(result.data.body);
|
|
2738
|
+
},
|
|
2739
|
+
async put(key, body, given) {
|
|
2740
|
+
const options = orEmpty(given);
|
|
2741
|
+
const problem = keyProblem(key) ?? bodyProblem(body) ?? objectOptionsProblem(options) ?? checksumProblem(options.checksumCrc32c);
|
|
2742
|
+
if (problem) return fail(invalidInput(problem));
|
|
2743
|
+
const payload = toPayload(body);
|
|
2744
|
+
const size = payload instanceof Blob ? payload.size : payload.length;
|
|
2745
|
+
const tooLarge = sizeProblem(size, rules.maxSinglePutSize);
|
|
2746
|
+
if (tooLarge) return fail(invalidInput(tooLarge));
|
|
2747
|
+
const result = await send(ctx, "PUT", objectUrl(ctx, key), {
|
|
2748
|
+
headers: writeHeaders(options),
|
|
2749
|
+
body: payload,
|
|
2750
|
+
timeoutMs: REQUEST_TIMEOUT_MS + Math.ceil(size / MiB) * 1e3
|
|
2751
|
+
});
|
|
2752
|
+
if (result.error) return result;
|
|
2753
|
+
const etag = stripQuotes(result.data.headers.get("etag") ?? "");
|
|
2754
|
+
return etag ? ok({ etag }) : fail(incompleteResponse("ETag"));
|
|
2755
|
+
},
|
|
2756
|
+
async delete(key) {
|
|
2757
|
+
const problem = keyProblem(key);
|
|
2758
|
+
if (problem) return fail(invalidInput(problem));
|
|
2759
|
+
const result = await send(ctx, "DELETE", objectUrl(ctx, key));
|
|
2760
|
+
if (result.error && result.error.code !== "NOT_FOUND") return result;
|
|
2761
|
+
return ok(void 0);
|
|
2762
|
+
},
|
|
2763
|
+
async deleteMany(keys) {
|
|
2764
|
+
const problem = keyListProblem(keys);
|
|
2765
|
+
if (problem) return fail(invalidInput(problem));
|
|
2766
|
+
const deleted = [];
|
|
2767
|
+
const failed = [];
|
|
2768
|
+
await forEachConcurrently(keys, DELETE_CONCURRENCY, async (key) => {
|
|
2769
|
+
const result = await methods.delete(key);
|
|
2770
|
+
if (result.error) failed.push({
|
|
2771
|
+
key,
|
|
2772
|
+
error: result.error
|
|
2773
|
+
});
|
|
2774
|
+
else deleted.push(key);
|
|
2775
|
+
});
|
|
2776
|
+
return ok({
|
|
2777
|
+
deleted,
|
|
2778
|
+
failed
|
|
2779
|
+
});
|
|
2780
|
+
},
|
|
2781
|
+
async list(prefix, given) {
|
|
2782
|
+
const { cursor, limit = MAX_LIST_PAGE } = orEmpty(given);
|
|
2783
|
+
const problem = prefixProblem(prefix) ?? limitProblem(limit) ?? (cursor === void 0 ? null : cursorProblem(cursor));
|
|
2784
|
+
if (problem) return fail(invalidInput(problem));
|
|
2785
|
+
const url = new URL(ctx.bucketUrl);
|
|
2786
|
+
url.searchParams.set("list-type", "2");
|
|
2787
|
+
url.searchParams.set("prefix", prefix);
|
|
2788
|
+
url.searchParams.set("max-keys", String(limit));
|
|
2789
|
+
if (cursor !== void 0) url.searchParams.set("continuation-token", cursor);
|
|
2790
|
+
const result = await sendForXml(ctx, "GET", url);
|
|
2791
|
+
if (result.error) return result;
|
|
2792
|
+
const listed = allOk(xmlBlocks(result.data, "Contents").map(readListedObject));
|
|
2793
|
+
if (listed.error) return listed;
|
|
2794
|
+
const items = listed.data;
|
|
2795
|
+
if (xmlValue(result.data, "IsTruncated") !== "true") return ok({
|
|
2796
|
+
items,
|
|
2797
|
+
nextCursor: null
|
|
2798
|
+
});
|
|
2799
|
+
const nextCursor = xmlText(result.data, "NextContinuationToken");
|
|
2800
|
+
return nextCursor ? ok({
|
|
2801
|
+
items,
|
|
2802
|
+
nextCursor
|
|
2803
|
+
}) : fail(incompleteResponse("continuation token"));
|
|
2804
|
+
}
|
|
2805
|
+
};
|
|
2806
|
+
return methods;
|
|
2807
|
+
}
|
|
2808
|
+
// src/storage/s3/presign.ts
|
|
2809
|
+
const SIGNABLE_UNTIL_MS = Date.UTC(1e4, 0, 1);
|
|
2810
|
+
function expiryOf(signedAt, expiresIn) {
|
|
2811
|
+
return /* @__PURE__ */ new Date(Math.floor(signedAt.getTime() / 1e3) * 1e3 + expiresIn * 1e3);
|
|
2812
|
+
}
|
|
2813
|
+
async function presign(ctx, url, headers, size, expiresIn) {
|
|
2814
|
+
const { now, credentials, region } = ctx;
|
|
2815
|
+
const date = now();
|
|
2816
|
+
return {
|
|
2817
|
+
method: "PUT",
|
|
2818
|
+
url: (await presignUrl({
|
|
2819
|
+
method: "PUT",
|
|
2820
|
+
url,
|
|
2821
|
+
headers: {
|
|
2822
|
+
...headers,
|
|
2823
|
+
"content-length": String(size)
|
|
2824
|
+
},
|
|
2825
|
+
credentials,
|
|
2826
|
+
region,
|
|
2827
|
+
date,
|
|
2828
|
+
expiresIn
|
|
2829
|
+
})).toString(),
|
|
2830
|
+
headers,
|
|
2831
|
+
expiresAt: expiryOf(date, expiresIn)
|
|
2832
|
+
};
|
|
2833
|
+
}
|
|
2834
|
+
function presignMethods(ctx) {
|
|
2835
|
+
const { now, credentials, region, rules } = ctx;
|
|
2836
|
+
return {
|
|
2837
|
+
async presignPut(key, given) {
|
|
2838
|
+
const options = orEmpty(given);
|
|
2839
|
+
const problem = keyProblem(key) ?? objectOptionsProblem(options) ?? checksumProblem(options.checksumCrc32c) ?? sizeProblem(options.size, rules.maxSinglePutSize) ?? expiryProblem(options.expiresIn, rules.maxPresignSeconds);
|
|
2840
|
+
if (problem) return fail(invalidInput(problem));
|
|
2841
|
+
return ok(await presign(ctx, objectUrl(ctx, key), writeHeaders(options), options.size, options.expiresIn));
|
|
2842
|
+
},
|
|
2843
|
+
async presignPart(key, uploadId, partNumber, given) {
|
|
2844
|
+
const options = orEmpty(given);
|
|
2845
|
+
const problem = keyProblem(key) ?? uploadIdProblem(uploadId) ?? partNumberProblem(partNumber, rules.maxParts) ?? checksumProblem(options.checksumCrc32c) ?? sizeProblem(options.size, rules.maxPartSize, "part") ?? expiryProblem(options.expiresIn, rules.maxPresignSeconds);
|
|
2846
|
+
if (problem) return fail(invalidInput(problem));
|
|
2847
|
+
const headers = options.checksumCrc32c ? { "x-amz-checksum-crc32c": options.checksumCrc32c } : {};
|
|
2848
|
+
const url = objectUrl(ctx, key, {
|
|
2849
|
+
partNumber: String(partNumber),
|
|
2850
|
+
uploadId
|
|
2851
|
+
});
|
|
2852
|
+
return ok(await presign(ctx, url, headers, options.size, options.expiresIn));
|
|
2853
|
+
},
|
|
2854
|
+
async presignGet(key, given) {
|
|
2855
|
+
const options = orEmpty(given);
|
|
2856
|
+
const signedAt = options.signedAt ?? now();
|
|
2857
|
+
const problem = keyProblem(key) ?? expiryProblem(options.expiresIn, rules.maxPresignSeconds) ?? objectOptionsProblem({ disposition: options.disposition }) ?? (signedAt instanceof Date && signedAt.getTime() >= 0 && signedAt.getTime() + options.expiresIn * 1e3 < SIGNABLE_UNTIL_MS ? null : "signedAt must be a valid Date from 1970 on, and the URL must expire before the year 10000. Pass a Date, or omit it to sign at the current time.");
|
|
2858
|
+
if (problem) return fail(invalidInput(problem));
|
|
2859
|
+
const url = await presignUrl({
|
|
2860
|
+
method: "GET",
|
|
2861
|
+
url: objectUrl(ctx, key, options.disposition ? { "response-content-disposition": options.disposition } : {}),
|
|
2862
|
+
credentials,
|
|
2863
|
+
region,
|
|
2864
|
+
date: signedAt,
|
|
2865
|
+
expiresIn: options.expiresIn
|
|
2866
|
+
});
|
|
2867
|
+
return ok({
|
|
2868
|
+
url: url.toString(),
|
|
2869
|
+
expiresAt: expiryOf(signedAt, options.expiresIn)
|
|
2870
|
+
});
|
|
2871
|
+
}
|
|
2872
|
+
};
|
|
2873
|
+
}
|
|
2874
|
+
// src/storage/s3-driver.ts
|
|
2875
|
+
function createS3Driver(config) {
|
|
2876
|
+
const ctx = createContext(config);
|
|
2877
|
+
const publicUrl = config.publicUrl === void 0 ? void 0 : checkBaseUrl(config.publicUrl, "publicUrl", "https://cdn.example.com/files").href.replace(/\/+$/, "");
|
|
2878
|
+
const objects = objectMethods(ctx);
|
|
2879
|
+
const multipart = multipartMethods(ctx);
|
|
2880
|
+
return {
|
|
2881
|
+
rules: ctx.rules,
|
|
2882
|
+
publicUrl,
|
|
2883
|
+
...presignMethods(ctx),
|
|
2884
|
+
...bucketSettingsMethods(ctx),
|
|
2885
|
+
...multipart,
|
|
2886
|
+
...objects,
|
|
2887
|
+
...copyMethods(ctx, {
|
|
2888
|
+
head: objects.head,
|
|
2889
|
+
...multipart
|
|
2890
|
+
})
|
|
2891
|
+
};
|
|
2892
|
+
}
|
|
2893
|
+
// src/storage/presets.ts
|
|
2894
|
+
const R2_VARIABLES = {
|
|
2895
|
+
bucket: "CLOUDROP_R2_BUCKET",
|
|
2896
|
+
accountId: "CLOUDROP_R2_ACCOUNT_ID",
|
|
2897
|
+
accessKeyId: "CLOUDROP_R2_ACCESS_KEY_ID",
|
|
2898
|
+
secretAccessKey: "CLOUDROP_R2_SECRET_ACCESS_KEY"
|
|
2899
|
+
};
|
|
2900
|
+
const S3_VARIABLES = {
|
|
2901
|
+
endpoint: "CLOUDROP_S3_ENDPOINT",
|
|
2902
|
+
region: "CLOUDROP_S3_REGION",
|
|
2903
|
+
bucket: "CLOUDROP_S3_BUCKET",
|
|
2904
|
+
accessKeyId: "CLOUDROP_S3_ACCESS_KEY_ID",
|
|
2905
|
+
secretAccessKey: "CLOUDROP_S3_SECRET_ACCESS_KEY"
|
|
2906
|
+
};
|
|
2907
|
+
const STAND_INS = {
|
|
2908
|
+
bucket: "bucket",
|
|
2909
|
+
accountId: "0".repeat(32),
|
|
2910
|
+
accessKeyId: "key",
|
|
2911
|
+
secretAccessKey: "secret",
|
|
2912
|
+
endpoint: "https://s3.example.com",
|
|
2913
|
+
region: "auto"
|
|
2914
|
+
};
|
|
2915
|
+
function readSettings(service, options, variables, read) {
|
|
2916
|
+
const settings = {};
|
|
2917
|
+
const missing = [];
|
|
2918
|
+
for (const setting of Object.keys(variables)) {
|
|
2919
|
+
const value = options[setting] || read(variables[setting], setting);
|
|
2920
|
+
if (value) settings[setting] = value;
|
|
2921
|
+
else missing.push(`${setting} (${variables[setting]})`);
|
|
2922
|
+
}
|
|
2923
|
+
if (missing.length > 0) throw configError(`Missing ${service} settings: ${missing.join(", ")}. Pass them to ${service.toLowerCase()}() or set the environment variables in parentheses.`);
|
|
2924
|
+
return settings;
|
|
2925
|
+
}
|
|
2926
|
+
function preset(build) {
|
|
2927
|
+
if (fromEnv("NODE_ENV") === "development") return buildFromEnvironment(build);
|
|
2928
|
+
let readsEnvironment = false;
|
|
2929
|
+
const checked = build((_variable, setting) => {
|
|
2930
|
+
readsEnvironment = true;
|
|
2931
|
+
return STAND_INS[setting];
|
|
2932
|
+
});
|
|
2933
|
+
return readsEnvironment ? lazyDriver(checked, () => buildFromEnvironment(build)) : checked;
|
|
2934
|
+
}
|
|
2935
|
+
function buildFromEnvironment(build) {
|
|
2936
|
+
const read = [];
|
|
2937
|
+
try {
|
|
2938
|
+
return build((variable) => {
|
|
2939
|
+
const value = fromEnv(variable);
|
|
2940
|
+
if (value) read.push(variable);
|
|
2941
|
+
return value;
|
|
2942
|
+
});
|
|
2943
|
+
} catch (error) {
|
|
2944
|
+
if (!(error instanceof CloudropError)) throw error;
|
|
2945
|
+
const invalid = read.filter((variable) => readSetting(() => build((name, setting) => name === variable ? fromEnv(name) : STAND_INS[setting])).error);
|
|
2946
|
+
if (invalid.length === 0) throw error;
|
|
2947
|
+
throw configError(`${error.message} Environment variables with an invalid value: ${invalid.join(", ")}.`);
|
|
2948
|
+
}
|
|
2949
|
+
}
|
|
2950
|
+
const R2_BUCKET_NAME = /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/;
|
|
2951
|
+
const S3_BUCKET_NAME = /^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$/;
|
|
2952
|
+
const IPV4_ADDRESS = /^\d{1,3}(\.\d{1,3}){3}$/;
|
|
2953
|
+
function checkBucket(bucket, service) {
|
|
2954
|
+
if (!(typeof bucket === "string" && (service === "R2" ? R2_BUCKET_NAME.test(bucket) : S3_BUCKET_NAME.test(bucket) && !bucket.includes("..") && !IPV4_ADDRESS.test(bucket)))) throw configError(`The ${service} bucket name must be 3 to 63 characters: ${service === "R2" ? "lowercase letters, digits and hyphens, starting and ending with a letter or digit" : "lowercase letters, digits, hyphens and dots, starting and ending with a letter or digit, with no two dots in a row, and not formatted like an IP address"}. Copy the exact name from your ${service} bucket list.`);
|
|
2955
|
+
return bucket;
|
|
2956
|
+
}
|
|
2957
|
+
function checkAccountId(accountId) {
|
|
2958
|
+
const account = typeof accountId === "string" ? accountId.toLowerCase() : void 0;
|
|
2959
|
+
if (account === void 0 || !/^[0-9a-f]{32}$/.test(account)) {
|
|
2960
|
+
const given = account === void 0 ? "is not text" : `has ${account.length} characters`;
|
|
2961
|
+
throw configError(`The R2 account ID must be 32 hexadecimal characters (0-9, a-f); this one ${given}. Copy it from the R2 overview page in the Cloudflare dashboard.`);
|
|
2962
|
+
}
|
|
2963
|
+
return account;
|
|
2964
|
+
}
|
|
2965
|
+
function r2(given) {
|
|
2966
|
+
const options = given ?? {};
|
|
2967
|
+
const { jurisdiction } = options;
|
|
2968
|
+
if (jurisdiction !== void 0 && jurisdiction !== "eu" && jurisdiction !== "fedramp") throw configError("The R2 jurisdiction must be \"eu\" or \"fedramp\". Omit it for the default location.");
|
|
2969
|
+
return preset((read) => {
|
|
2970
|
+
const settings = readSettings("R2", options, R2_VARIABLES, read);
|
|
2971
|
+
const bucket = checkBucket(settings.bucket, "R2");
|
|
2972
|
+
const account = checkAccountId(settings.accountId);
|
|
2973
|
+
return createS3Driver({
|
|
2974
|
+
endpoint: `https://${jurisdiction ? `${account}.${jurisdiction}` : account}.r2.cloudflarestorage.com`,
|
|
2975
|
+
region: "auto",
|
|
2976
|
+
bucket,
|
|
2977
|
+
credentials: {
|
|
2978
|
+
accessKeyId: settings.accessKeyId,
|
|
2979
|
+
secretAccessKey: settings.secretAccessKey
|
|
2980
|
+
},
|
|
2981
|
+
rules: R2_RULES,
|
|
2982
|
+
publicUrl: options.publicUrl,
|
|
2983
|
+
fetch: options.fetch
|
|
2984
|
+
});
|
|
2985
|
+
});
|
|
2986
|
+
}
|
|
2987
|
+
function s3(given) {
|
|
2988
|
+
const options = given ?? {};
|
|
2989
|
+
return preset((read) => {
|
|
2990
|
+
const { endpoint, region, bucket, accessKeyId, secretAccessKey } = readSettings("S3", options, S3_VARIABLES, read);
|
|
2991
|
+
return createS3Driver({
|
|
2992
|
+
endpoint,
|
|
2993
|
+
region,
|
|
2994
|
+
bucket: checkBucket(bucket, "S3"),
|
|
2995
|
+
credentials: {
|
|
2996
|
+
accessKeyId,
|
|
2997
|
+
secretAccessKey
|
|
2998
|
+
},
|
|
2999
|
+
rules: S3_RULES,
|
|
3000
|
+
publicUrl: options.publicUrl,
|
|
3001
|
+
fetch: options.fetch
|
|
3002
|
+
});
|
|
3003
|
+
});
|
|
3004
|
+
}
|
|
3005
|
+
export { CloudropError, allowAnyone, createCloudrop, forbidden, r2, route, s3, unauthorized };
|
|
3006
|
+
|
|
3007
|
+
//# sourceMappingURL=index.js.map
|