cloudrop 0.2.0 → 0.3.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 +55 -0
- package/README.md +29 -1
- package/dist/adapters/sql.d.ts +3 -0
- package/dist/adapters/sql.js +7 -0
- package/dist/adapters/sql.js.map +1 -1
- package/dist/cli.js +2045 -0
- package/dist/{client-ePVHwvMv.js → client-DxP3uv8V.js} +47 -5
- package/dist/client-DxP3uv8V.js.map +1 -0
- package/dist/client.js +1 -1
- package/dist/index.d.ts +25 -1
- package/dist/index.js +86 -45
- package/dist/index.js.map +1 -1
- package/dist/react.js +1 -1
- package/dist/testing.d.ts +81 -0
- package/dist/testing.js +1243 -0
- package/dist/testing.js.map +1 -0
- package/docs/README.md +30 -0
- package/docs/bucket-setup.md +226 -0
- package/docs/client.md +386 -0
- package/docs/command.md +129 -0
- package/docs/deployment.md +169 -0
- package/docs/errors.md +150 -0
- package/docs/hono.md +276 -0
- package/docs/react.md +508 -0
- package/docs/resume.md +253 -0
- package/docs/server.md +603 -0
- package/docs/store.md +804 -0
- package/docs/testing-your-app.md +328 -0
- package/docs/troubleshooting.md +176 -0
- package/package.json +10 -2
- package/dist/client-ePVHwvMv.js.map +0 -1
package/dist/cli.js
ADDED
|
@@ -0,0 +1,2045 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// src/cli/args.ts
|
|
3
|
+
const USAGE = `Usage: cloudrop <doctor|setup> --origin <origin> [options]
|
|
4
|
+
|
|
5
|
+
Commands:
|
|
6
|
+
doctor Check the storage settings, the bucket and its CORS and lifecycle rules.
|
|
7
|
+
setup Print the bucket's CORS and lifecycle rules and the commands that set them.
|
|
8
|
+
With --apply, add them to the bucket and keep every other rule.
|
|
9
|
+
|
|
10
|
+
Options:
|
|
11
|
+
--origin <origin> An origin your pages upload from, such as https://app.example.com.
|
|
12
|
+
Repeat it for each origin. Required.
|
|
13
|
+
--env-file <path> Also read settings from this file of NAME=value lines. A variable
|
|
14
|
+
set in the environment wins over the file.
|
|
15
|
+
--jurisdiction <eu|fedramp>
|
|
16
|
+
The R2 bucket's jurisdiction, when it has one.
|
|
17
|
+
--apply setup: write the rules to the bucket, keeping every other rule.
|
|
18
|
+
--dry-run setup: show what --apply would write, and write nothing.
|
|
19
|
+
--replace setup: write even when the current rules cannot be read, which
|
|
20
|
+
replaces them.
|
|
21
|
+
-h, --help Show this help.
|
|
22
|
+
|
|
23
|
+
Settings come from the environment, as cloudrop reads them: CLOUDROP_R2_* (or CLOUDROP_S3_*)
|
|
24
|
+
and CLOUDROP_SECRET. The command prints the names of settings, never their values.
|
|
25
|
+
|
|
26
|
+
Exit codes: 0 nothing is wrong, 1 a problem was found or a write failed, 2 a usage error.
|
|
27
|
+
`;
|
|
28
|
+
const SETUP_FLAGS = {
|
|
29
|
+
"--apply": "apply",
|
|
30
|
+
"--dry-run": "dryRun",
|
|
31
|
+
"--replace": "replace"
|
|
32
|
+
};
|
|
33
|
+
const VALUE_OPTIONS = new Set([
|
|
34
|
+
"--origin",
|
|
35
|
+
"--env-file",
|
|
36
|
+
"--jurisdiction"
|
|
37
|
+
]);
|
|
38
|
+
function originProblem(value) {
|
|
39
|
+
if (value.includes("*")) return `"${value}" is a pattern, not an origin. Name each origin your pages upload from with its own --origin, such as https://app.example.com.`;
|
|
40
|
+
const origin = URL.canParse(value) ? new URL(value).origin : "null";
|
|
41
|
+
if (origin !== "null" && origin === value) return null;
|
|
42
|
+
return origin === "null" ? `"${value}" is not an origin. Write it as scheme://host[:port] with no path, such as https://app.example.com.` : `"${value}" is not an origin. Use ${origin}: an origin is the scheme, host and port, with no path.`;
|
|
43
|
+
}
|
|
44
|
+
function setValue(options, name, value) {
|
|
45
|
+
if (name === "--env-file") {
|
|
46
|
+
options.envFile = value;
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
if (name === "--jurisdiction") {
|
|
50
|
+
if (value !== "eu" && value !== "fedramp") return "--jurisdiction must be \"eu\" or \"fedramp\". Leave it out for a bucket in the default location.";
|
|
51
|
+
options.jurisdiction = value;
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
options.origins.push(value);
|
|
55
|
+
return originProblem(value);
|
|
56
|
+
}
|
|
57
|
+
function setFlag(options, name, inline) {
|
|
58
|
+
if (options.command !== "setup") return `${name} works only with setup.`;
|
|
59
|
+
if (inline !== void 0) return `${name} takes no value.`;
|
|
60
|
+
options[SETUP_FLAGS[name]] = true;
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
function splitArgs(args) {
|
|
64
|
+
const split = [];
|
|
65
|
+
for (let index = 0; index < args.length; index++) {
|
|
66
|
+
const arg = args[index] ?? "";
|
|
67
|
+
const equals = arg.startsWith("--") ? arg.indexOf("=") : -1;
|
|
68
|
+
if (equals > 0) split.push({
|
|
69
|
+
name: arg.slice(0, equals),
|
|
70
|
+
value: arg.slice(equals + 1),
|
|
71
|
+
inline: true
|
|
72
|
+
});
|
|
73
|
+
else split.push({
|
|
74
|
+
name: arg,
|
|
75
|
+
value: VALUE_OPTIONS.has(arg) ? args[++index] : void 0,
|
|
76
|
+
inline: false
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return split;
|
|
80
|
+
}
|
|
81
|
+
function readOption(options, { name, value, inline }) {
|
|
82
|
+
if (Object.hasOwn(SETUP_FLAGS, name)) return setFlag(options, name, inline ? value : void 0);
|
|
83
|
+
if (!VALUE_OPTIONS.has(name)) return `Unknown option "${name}". Run cloudrop --help to see the options.`;
|
|
84
|
+
return value ? setValue(options, name, value) : `${name} needs a value.`;
|
|
85
|
+
}
|
|
86
|
+
function readOptions(options, args) {
|
|
87
|
+
for (const arg of splitArgs(args)) {
|
|
88
|
+
const problem = readOption(options, arg);
|
|
89
|
+
if (problem) return problem;
|
|
90
|
+
}
|
|
91
|
+
return options.origins.length > 0 ? null : "Name at least one origin your pages upload from: --origin https://app.example.com.";
|
|
92
|
+
}
|
|
93
|
+
function parseArgs(argv) {
|
|
94
|
+
if (argv.includes("--help") || argv.includes("-h") || argv[0] === "help") return { help: true };
|
|
95
|
+
const [command, ...rest] = argv;
|
|
96
|
+
if (command === void 0) return {
|
|
97
|
+
help: false,
|
|
98
|
+
error: "Name a command: doctor or setup."
|
|
99
|
+
};
|
|
100
|
+
if (command !== "doctor" && command !== "setup") return {
|
|
101
|
+
help: false,
|
|
102
|
+
error: `Unknown command "${command}". Use doctor or setup.`
|
|
103
|
+
};
|
|
104
|
+
const options = {
|
|
105
|
+
command,
|
|
106
|
+
origins: [],
|
|
107
|
+
apply: false,
|
|
108
|
+
dryRun: false,
|
|
109
|
+
replace: false
|
|
110
|
+
};
|
|
111
|
+
const error = readOptions(options, rest) ?? (options.replace && !options.apply && !options.dryRun ? "--replace works only with --apply or --dry-run." : null);
|
|
112
|
+
if (error) return {
|
|
113
|
+
help: false,
|
|
114
|
+
error
|
|
115
|
+
};
|
|
116
|
+
options.origins = [...new Set(options.origins)];
|
|
117
|
+
return {
|
|
118
|
+
help: false,
|
|
119
|
+
options
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
// src/errors.ts
|
|
123
|
+
var CloudropError = class extends Error {
|
|
124
|
+
code;
|
|
125
|
+
retryable;
|
|
126
|
+
status;
|
|
127
|
+
storageCode;
|
|
128
|
+
parts;
|
|
129
|
+
file;
|
|
130
|
+
constructor(code, message, options = {}) {
|
|
131
|
+
super(message, { cause: options.cause });
|
|
132
|
+
this.name = "CloudropError";
|
|
133
|
+
this.code = code;
|
|
134
|
+
this.retryable = options.retryable ?? false;
|
|
135
|
+
this.status = options.status;
|
|
136
|
+
this.storageCode = options.storageCode;
|
|
137
|
+
this.parts = options.parts;
|
|
138
|
+
this.file = options.file;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
function ok(data) {
|
|
142
|
+
return {
|
|
143
|
+
data,
|
|
144
|
+
error: null
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function fail(error) {
|
|
148
|
+
return {
|
|
149
|
+
data: null,
|
|
150
|
+
error
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function configError(message) {
|
|
154
|
+
return new CloudropError("INVALID_CONFIG", message);
|
|
155
|
+
}
|
|
156
|
+
// src/server/bucket-check.ts
|
|
157
|
+
const UPLOAD_HEADERS = [
|
|
158
|
+
"content-type",
|
|
159
|
+
"content-disposition",
|
|
160
|
+
"if-none-match",
|
|
161
|
+
"x-amz-meta-cloudrop-id"
|
|
162
|
+
];
|
|
163
|
+
function matchesAny(patterns, value) {
|
|
164
|
+
return patterns.some((pattern) => {
|
|
165
|
+
const [head = "", tail] = pattern.toLowerCase().split("*");
|
|
166
|
+
if (tail === void 0) return head === value;
|
|
167
|
+
return value.length >= head.length + tail.length && value.startsWith(head) && value.endsWith(tail);
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
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");
|
|
171
|
+
function corsCheck(rules, origins) {
|
|
172
|
+
if (origins.length === 0) return {
|
|
173
|
+
status: "unknown",
|
|
174
|
+
message: "Name the origins your pages upload from: api.checkBucket({ origins: [\"https://app.example.com\"] })."
|
|
175
|
+
};
|
|
176
|
+
const blocked = origins.filter((origin) => !rules.some((rule) => allowsUploads(rule, origin)));
|
|
177
|
+
if (blocked.length === 0) return {
|
|
178
|
+
status: "ok",
|
|
179
|
+
message: `Browsers on ${origins.join(", ")} can upload and read ETag.`
|
|
180
|
+
};
|
|
181
|
+
return {
|
|
182
|
+
status: "problem",
|
|
183
|
+
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.`
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
function lifecycleCheck(rules, hasStore) {
|
|
187
|
+
const rule = rules.find((candidate) => candidate.enabled && candidate.prefix === "" && candidate.abortIncompleteMultipartUploadDays !== void 0);
|
|
188
|
+
if (rule) {
|
|
189
|
+
const days = rule.abortIncompleteMultipartUploadDays;
|
|
190
|
+
return {
|
|
191
|
+
status: "ok",
|
|
192
|
+
message: `Incomplete multipart uploads are aborted after ${days} day${days === 1 ? "" : "s"}.`
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
return {
|
|
196
|
+
status: "problem",
|
|
197
|
+
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).`
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function settingCheck(read, setting, check) {
|
|
201
|
+
if (read.error === null) return ok(check(read.data));
|
|
202
|
+
if (read.error.code === "STORAGE_DENIED") return ok({
|
|
203
|
+
status: "unknown",
|
|
204
|
+
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.`
|
|
205
|
+
});
|
|
206
|
+
if (read.error.status === 501) return ok({
|
|
207
|
+
status: "unknown",
|
|
208
|
+
message: `This storage service cannot report the bucket's ${setting}. Check them where the bucket is configured (for R2, the dashboard).`
|
|
209
|
+
});
|
|
210
|
+
return fail(read.error);
|
|
211
|
+
}
|
|
212
|
+
async function checkBucket$1(config, origins) {
|
|
213
|
+
const { storage } = config;
|
|
214
|
+
const [corsRules, lifecycleRules] = await Promise.all([storage.getBucketCors(), storage.getBucketLifecycle()]);
|
|
215
|
+
const cors = settingCheck(corsRules, "CORS rules", (rules) => corsCheck(rules, origins));
|
|
216
|
+
if (cors.error) return cors;
|
|
217
|
+
const lifecycle = settingCheck(lifecycleRules, "lifecycle rules", (rules) => lifecycleCheck(rules, config.store !== void 0));
|
|
218
|
+
if (lifecycle.error) return lifecycle;
|
|
219
|
+
const privateRoutes = [...config.routes.values()].filter((route) => route.visibility === "private");
|
|
220
|
+
const publicAccess = privateRoutes.length > 0 && storage.publicUrl ? {
|
|
221
|
+
status: "problem",
|
|
222
|
+
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.`
|
|
223
|
+
} : {
|
|
224
|
+
status: "ok",
|
|
225
|
+
message: "No private route stores files on a public bucket."
|
|
226
|
+
};
|
|
227
|
+
return ok({
|
|
228
|
+
cors: cors.data,
|
|
229
|
+
lifecycle: lifecycle.data,
|
|
230
|
+
publicAccess
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
// src/cli/bucket-access.ts
|
|
234
|
+
const NOT_CREDENTIALS = new Set(["RequestTimeTooSkewed", "ExpiredRequest"]);
|
|
235
|
+
const OTHER_REGION = new Set(["AuthorizationHeaderMalformed", "PermanentRedirect"]);
|
|
236
|
+
function credentialsHint(service, jurisdiction) {
|
|
237
|
+
if (service === "S3") return "Check CLOUDROP_S3_ACCESS_KEY_ID and CLOUDROP_S3_SECRET_ACCESS_KEY, that they may read and write objects in the bucket named by CLOUDROP_S3_BUCKET, and CLOUDROP_S3_REGION.";
|
|
238
|
+
return `Check CLOUDROP_R2_ACCESS_KEY_ID and CLOUDROP_R2_SECRET_ACCESS_KEY, that the token may read and write objects in the bucket named by CLOUDROP_R2_BUCKET, and that the bucket exists${jurisdiction ? ` in the ${jurisdiction} jurisdiction.` : ". A bucket in a jurisdiction also needs --jurisdiction."}`;
|
|
239
|
+
}
|
|
240
|
+
function endpointHint(service, jurisdiction) {
|
|
241
|
+
if (service === "S3") return "The endpoint is CLOUDROP_S3_ENDPOINT.";
|
|
242
|
+
return jurisdiction ? `The R2 endpoint is built from CLOUDROP_R2_ACCOUNT_ID and --jurisdiction ${jurisdiction}: check that it is your account ID and the bucket's jurisdiction.` : "The R2 endpoint is built from CLOUDROP_R2_ACCOUNT_ID: check that it is your account ID.";
|
|
243
|
+
}
|
|
244
|
+
function bucketProblem(error, service, jurisdiction) {
|
|
245
|
+
if (error.code === "STORAGE_UNREACHABLE") return `${error.message} ${endpointHint(service, jurisdiction)}`;
|
|
246
|
+
if (service === "S3" && OTHER_REGION.has(error.storageCode ?? "")) return `The bucket is in another region (${error.storageCode}). Check CLOUDROP_S3_REGION, and that CLOUDROP_S3_ENDPOINT serves that region.`;
|
|
247
|
+
if (!((error.status === 401 || error.status === 403) && !NOT_CREDENTIALS.has(error.storageCode ?? ""))) return error.message;
|
|
248
|
+
return `The storage service refused these credentials (HTTP ${error.status}). ${credentialsHint(service, jurisdiction)}`;
|
|
249
|
+
}
|
|
250
|
+
async function reachBucket(driver, service, jurisdiction) {
|
|
251
|
+
const listed = await driver.list("cloudrop-doctor/", { limit: 1 });
|
|
252
|
+
if (!listed.error) return {
|
|
253
|
+
status: "ok",
|
|
254
|
+
text: "The bucket answers, and these credentials may list it."
|
|
255
|
+
};
|
|
256
|
+
return {
|
|
257
|
+
status: "problem",
|
|
258
|
+
text: `Bucket: ${bucketProblem(listed.error, service, jurisdiction)}`
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
// src/cli/report.ts
|
|
262
|
+
const LABELS = {
|
|
263
|
+
ok: "ok",
|
|
264
|
+
problem: "PROBLEM",
|
|
265
|
+
unknown: "NOT CHECKED",
|
|
266
|
+
note: "note",
|
|
267
|
+
"dry run": "dry run"
|
|
268
|
+
};
|
|
269
|
+
function printFindings(out, findings) {
|
|
270
|
+
for (const { status, text } of findings) out.write(`${LABELS[status].padEnd(12)} ${text}\n`);
|
|
271
|
+
}
|
|
272
|
+
const count = (findings, status) => findings.filter((finding) => finding.status === status).length;
|
|
273
|
+
const plural = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
274
|
+
const CONTROL = /[\u0000-\u001f\u007f-\u009f]/g;
|
|
275
|
+
const quoted = (id) => `"${id.replace(CONTROL, (char) => `\\u${char.charCodeAt(0).toString(16).padStart(4, "0")}`)}"`;
|
|
276
|
+
const ruleName = (rule) => rule.id ? `rule ${quoted(rule.id)}` : "a rule without an ID";
|
|
277
|
+
const ruleNames = (rules) => rules.map((rule) => rule.id ? quoted(rule.id) : "a rule without an ID").join(", ");
|
|
278
|
+
// src/cli/rules.ts
|
|
279
|
+
const CORS_RULE_ID = "cloudrop-uploads";
|
|
280
|
+
const LIFECYCLE_RULE_ID = "cloudrop-abort-incomplete";
|
|
281
|
+
function corsRule(origins) {
|
|
282
|
+
return {
|
|
283
|
+
id: CORS_RULE_ID,
|
|
284
|
+
allowedOrigins: [...origins],
|
|
285
|
+
allowedMethods: [
|
|
286
|
+
"PUT",
|
|
287
|
+
"GET",
|
|
288
|
+
"HEAD"
|
|
289
|
+
],
|
|
290
|
+
allowedHeaders: [
|
|
291
|
+
"content-type",
|
|
292
|
+
"content-disposition",
|
|
293
|
+
"if-none-match",
|
|
294
|
+
"x-amz-meta-cloudrop-id"
|
|
295
|
+
],
|
|
296
|
+
exposeHeaders: ["ETag", "Retry-After"],
|
|
297
|
+
maxAgeSeconds: 3600
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
const LIFECYCLE_RULE = {
|
|
301
|
+
id: LIFECYCLE_RULE_ID,
|
|
302
|
+
enabled: true,
|
|
303
|
+
prefix: "",
|
|
304
|
+
abortIncompleteMultipartUploadDays: 1
|
|
305
|
+
};
|
|
306
|
+
function missing(existing, wanted, ignoreCase) {
|
|
307
|
+
const normal = (value) => ignoreCase ? value.toLowerCase() : value;
|
|
308
|
+
const have = new Set(existing.map(normal));
|
|
309
|
+
return wanted.filter((value) => !have.has(normal(value)));
|
|
310
|
+
}
|
|
311
|
+
const CORS_LISTS = [
|
|
312
|
+
[
|
|
313
|
+
"allowedOrigins",
|
|
314
|
+
"origins",
|
|
315
|
+
false
|
|
316
|
+
],
|
|
317
|
+
[
|
|
318
|
+
"allowedMethods",
|
|
319
|
+
"methods",
|
|
320
|
+
true
|
|
321
|
+
],
|
|
322
|
+
[
|
|
323
|
+
"allowedHeaders",
|
|
324
|
+
"headers",
|
|
325
|
+
true
|
|
326
|
+
],
|
|
327
|
+
[
|
|
328
|
+
"exposeHeaders",
|
|
329
|
+
"exposed headers",
|
|
330
|
+
true
|
|
331
|
+
]
|
|
332
|
+
];
|
|
333
|
+
function grow(existing, wanted) {
|
|
334
|
+
const rule = { ...existing };
|
|
335
|
+
const added = [];
|
|
336
|
+
for (const [field, name, ignoreCase] of CORS_LISTS) {
|
|
337
|
+
const extra = missing(existing[field], wanted[field], ignoreCase);
|
|
338
|
+
rule[field] = [...existing[field], ...extra];
|
|
339
|
+
if (extra.length > 0) added.push(`${name} ${extra.join(", ")}`);
|
|
340
|
+
}
|
|
341
|
+
if (existing.maxAgeSeconds === void 0 && wanted.maxAgeSeconds !== void 0) {
|
|
342
|
+
rule.maxAgeSeconds = wanted.maxAgeSeconds;
|
|
343
|
+
added.push(`a max age of ${wanted.maxAgeSeconds} seconds`);
|
|
344
|
+
}
|
|
345
|
+
return {
|
|
346
|
+
rule,
|
|
347
|
+
added: added.join("; ")
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
const allowsAll = (rule, ours) => `${ruleName(rule)} already allows uploads from ${ours.allowedOrigins.join(", ")} as cloudrop's rule does`;
|
|
351
|
+
function mergeCors(current, ours) {
|
|
352
|
+
const kept = current.filter((rule) => rule.id !== ours.id);
|
|
353
|
+
const [first, ...others] = current.filter((rule) => rule.id === ours.id);
|
|
354
|
+
if (first === void 0) {
|
|
355
|
+
const twin = current.find((rule) => rule.id === void 0 && grow(rule, ours).added === "");
|
|
356
|
+
if (twin) return {
|
|
357
|
+
rules: [...current],
|
|
358
|
+
change: "none",
|
|
359
|
+
kept,
|
|
360
|
+
reason: allowsAll(twin, ours)
|
|
361
|
+
};
|
|
362
|
+
return {
|
|
363
|
+
rules: [...current, ours],
|
|
364
|
+
change: "add",
|
|
365
|
+
kept
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
const { rule, added } = grow(others.reduce((rule, copy) => grow(rule, copy).rule, first), ours);
|
|
369
|
+
if (added === "" && others.length === 0) return {
|
|
370
|
+
rules: [...current],
|
|
371
|
+
change: "none",
|
|
372
|
+
kept,
|
|
373
|
+
reason: allowsAll(first, ours)
|
|
374
|
+
};
|
|
375
|
+
return {
|
|
376
|
+
rules: current.flatMap((each) => each.id !== ours.id ? [each] : each === first ? [rule] : []),
|
|
377
|
+
change: "update",
|
|
378
|
+
kept,
|
|
379
|
+
added: added || "its copies folded into one"
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
const abortsEverywhere = (rule) => rule.enabled && rule.prefix === "" && rule.abortIncompleteMultipartUploadDays !== void 0;
|
|
383
|
+
function mergeLifecycle(current, ours) {
|
|
384
|
+
const kept = current.filter((rule) => rule.id !== ours.id);
|
|
385
|
+
const covering = current.find(abortsEverywhere);
|
|
386
|
+
if (covering) {
|
|
387
|
+
const days = plural(covering.abortIncompleteMultipartUploadDays ?? 0, "day");
|
|
388
|
+
const reason = `${ruleName(covering)} already aborts incomplete multipart uploads after ${days}`;
|
|
389
|
+
return {
|
|
390
|
+
rules: [...current],
|
|
391
|
+
change: "none",
|
|
392
|
+
kept,
|
|
393
|
+
reason
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
if (kept.length < current.length) {
|
|
397
|
+
const reason = `rule "${ours.id}" exists but does not abort incomplete multipart uploads across the whole bucket, so someone changed it (disabled it, or gave it a prefix). setup does not change an edited rule: fix or delete it by hand, then run setup again.`;
|
|
398
|
+
return {
|
|
399
|
+
rules: [...current],
|
|
400
|
+
change: "blocked",
|
|
401
|
+
kept,
|
|
402
|
+
reason
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
return {
|
|
406
|
+
rules: [...current, ours],
|
|
407
|
+
change: "add",
|
|
408
|
+
kept
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
// src/cli/instructions.ts
|
|
412
|
+
const json = (value) => JSON.stringify(value, null, 2);
|
|
413
|
+
function wrangler(args, jurisdiction) {
|
|
414
|
+
return `wrangler r2 bucket ${args.replace("<bucket>", "\"$CLOUDROP_R2_BUCKET\"")}${jurisdiction ? ` --jurisdiction ${jurisdiction}` : ""}`;
|
|
415
|
+
}
|
|
416
|
+
function aws(call, args = "") {
|
|
417
|
+
return `aws s3api ${call} --bucket "$CLOUDROP_S3_BUCKET"${args} --endpoint-url "$CLOUDROP_S3_ENDPOINT"`;
|
|
418
|
+
}
|
|
419
|
+
function r2Commands(jurisdiction) {
|
|
420
|
+
return [
|
|
421
|
+
wrangler("cors list <bucket>", jurisdiction),
|
|
422
|
+
wrangler("cors set <bucket> --file cors.json", jurisdiction),
|
|
423
|
+
wrangler(`lifecycle add <bucket> ${LIFECYCLE_RULE_ID} --abort-multipart-days 1`, jurisdiction)
|
|
424
|
+
];
|
|
425
|
+
}
|
|
426
|
+
const S3_COMMANDS = [
|
|
427
|
+
aws("get-bucket-cors"),
|
|
428
|
+
aws("put-bucket-cors", " --cors-configuration file://cors-s3.json"),
|
|
429
|
+
aws("get-bucket-lifecycle-configuration"),
|
|
430
|
+
aws("put-bucket-lifecycle-configuration", " --lifecycle-configuration file://lifecycle.json")
|
|
431
|
+
];
|
|
432
|
+
function printInstructions(out, { origins, service, jurisdiction }) {
|
|
433
|
+
const rule = corsRule(origins);
|
|
434
|
+
const r2Format = { rules: [{
|
|
435
|
+
id: rule.id,
|
|
436
|
+
allowed: {
|
|
437
|
+
origins: rule.allowedOrigins,
|
|
438
|
+
methods: rule.allowedMethods,
|
|
439
|
+
headers: rule.allowedHeaders
|
|
440
|
+
},
|
|
441
|
+
exposeHeaders: rule.exposeHeaders,
|
|
442
|
+
maxAgeSeconds: rule.maxAgeSeconds
|
|
443
|
+
}] };
|
|
444
|
+
const s3Format = { CORSRules: [{
|
|
445
|
+
ID: rule.id,
|
|
446
|
+
AllowedOrigins: rule.allowedOrigins,
|
|
447
|
+
AllowedMethods: rule.allowedMethods,
|
|
448
|
+
AllowedHeaders: rule.allowedHeaders,
|
|
449
|
+
ExposeHeaders: rule.exposeHeaders,
|
|
450
|
+
MaxAgeSeconds: rule.maxAgeSeconds
|
|
451
|
+
}] };
|
|
452
|
+
const lifecycle = { Rules: [{
|
|
453
|
+
ID: LIFECYCLE_RULE_ID,
|
|
454
|
+
Status: "Enabled",
|
|
455
|
+
Filter: { Prefix: "" },
|
|
456
|
+
AbortIncompleteMultipartUpload: { DaysAfterInitiation: 1 }
|
|
457
|
+
}] };
|
|
458
|
+
const sections = [
|
|
459
|
+
`cloudrop setup: the bucket rules for ${origins.join(", ")}.`,
|
|
460
|
+
`CORS rule, R2 format. Save it as cors.json for wrangler r2 bucket cors set:\n${json(r2Format)}`,
|
|
461
|
+
`CORS rule, S3 format. Save it as cors-s3.json for aws s3api put-bucket-cors:\n${json(s3Format)}`,
|
|
462
|
+
`Lifecycle rule. Save it as lifecycle.json for aws s3api put-bucket-lifecycle-configuration:\n${json(lifecycle)}`
|
|
463
|
+
];
|
|
464
|
+
if (service !== "S3") sections.push(`Commands for R2:\n${indent(r2Commands(jurisdiction))}`);
|
|
465
|
+
if (service !== "R2") sections.push(`Commands for S3-compatible services:\n${indent(S3_COMMANDS)}`);
|
|
466
|
+
sections.push("The commands read the bucket from CLOUDROP_R2_BUCKET or CLOUDROP_S3_BUCKET in your shell. Setting CORS, and setting lifecycle rules with the AWS CLI, replaces every rule the bucket has: list them first and add the ones you keep to the file, or run this command with --apply, which keeps them.");
|
|
467
|
+
out.write(`${sections.join("\n\n")}\n`);
|
|
468
|
+
}
|
|
469
|
+
const indent = (lines) => lines.map((line) => ` ${line}`).join("\n");
|
|
470
|
+
// src/env.ts
|
|
471
|
+
function fromEnv(name) {
|
|
472
|
+
return (globalThis.process?.env)?.[name] || void 0;
|
|
473
|
+
}
|
|
474
|
+
// src/rules.ts
|
|
475
|
+
const MiB = 1048576;
|
|
476
|
+
const GiB = 1024 * MiB;
|
|
477
|
+
const R2_RULES = {
|
|
478
|
+
minPartSize: 5 * MiB,
|
|
479
|
+
maxPartSize: 5 * GiB - 5 * MiB,
|
|
480
|
+
maxParts: 1e4,
|
|
481
|
+
maxSinglePutSize: 5 * GiB - 5 * MiB,
|
|
482
|
+
maxObjectSize: 5 * (1024 * GiB) - 5 * GiB,
|
|
483
|
+
maxPresignSeconds: 604800
|
|
484
|
+
};
|
|
485
|
+
const S3_RULES = {
|
|
486
|
+
minPartSize: 5 * MiB,
|
|
487
|
+
maxPartSize: 5 * GiB,
|
|
488
|
+
maxParts: 1e4,
|
|
489
|
+
maxSinglePutSize: 5 * GiB,
|
|
490
|
+
maxObjectSize: 5e4 * GiB,
|
|
491
|
+
maxPresignSeconds: 604800
|
|
492
|
+
};
|
|
493
|
+
// src/storage/lazy-driver.ts
|
|
494
|
+
function readSetting(read) {
|
|
495
|
+
try {
|
|
496
|
+
return ok(read());
|
|
497
|
+
} catch (error) {
|
|
498
|
+
if (error instanceof CloudropError) return fail(error);
|
|
499
|
+
throw error;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
// src/storage/xml.ts
|
|
503
|
+
const ENTITIES = {
|
|
504
|
+
amp: "&",
|
|
505
|
+
lt: "<",
|
|
506
|
+
gt: ">",
|
|
507
|
+
quot: "\"",
|
|
508
|
+
apos: "'"
|
|
509
|
+
};
|
|
510
|
+
const ESCAPES = {
|
|
511
|
+
"<": "<",
|
|
512
|
+
">": ">",
|
|
513
|
+
"&": "&",
|
|
514
|
+
"\"": """,
|
|
515
|
+
"'": "'"
|
|
516
|
+
};
|
|
517
|
+
function decodeXml(text) {
|
|
518
|
+
return text.replace(/&(#x[0-9a-fA-F]+|#\d+|amp|lt|gt|quot|apos);/g, (match, entity) => {
|
|
519
|
+
if (!entity.startsWith("#")) return ENTITIES[entity] ?? "";
|
|
520
|
+
const codePoint = entity[1] === "x" ? Number.parseInt(entity.slice(2), 16) : Number.parseInt(entity.slice(1), 10);
|
|
521
|
+
return Number.isFinite(codePoint) && codePoint <= 1114111 ? String.fromCodePoint(codePoint) : match;
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
function xmlBlocks(xml, tag) {
|
|
525
|
+
const pattern = new RegExp(`<${tag}(?:\\s[^>]*)?(?<!/)>([\\s\\S]*?)</${tag}>`, "g");
|
|
526
|
+
return Array.from(xml.matchAll(pattern), (match) => match[1] ?? "");
|
|
527
|
+
}
|
|
528
|
+
function xmlText(xml, tag) {
|
|
529
|
+
const block = xmlBlocks(xml, tag)[0];
|
|
530
|
+
return block === void 0 ? void 0 : decodeXml(block);
|
|
531
|
+
}
|
|
532
|
+
function xmlValue(xml, tag) {
|
|
533
|
+
return xmlText(xml, tag)?.trim();
|
|
534
|
+
}
|
|
535
|
+
function xmlValues(xml, tag) {
|
|
536
|
+
return xmlBlocks(xml, tag).map((block) => decodeXml(block).trim());
|
|
537
|
+
}
|
|
538
|
+
function stripQuotes(value) {
|
|
539
|
+
return value.replaceAll("\"", "");
|
|
540
|
+
}
|
|
541
|
+
function escapeXml(value) {
|
|
542
|
+
return value.replace(/[<>&"']/g, (char) => ESCAPES[char] ?? char);
|
|
543
|
+
}
|
|
544
|
+
// src/storage/s3/responses.ts
|
|
545
|
+
const etagIn = (xml) => stripQuotes(xmlValue(xml, "ETag") ?? "");
|
|
546
|
+
function incompleteResponse(missing) {
|
|
547
|
+
return new CloudropError("STORAGE_ERROR", `The storage service's response had no ${missing}. Retry the request.`, { retryable: true });
|
|
548
|
+
}
|
|
549
|
+
function wholeNumber(text) {
|
|
550
|
+
if (text == null || !/^\d+$/.test(text)) return void 0;
|
|
551
|
+
const value = Number(text);
|
|
552
|
+
return Number.isSafeInteger(value) ? value : void 0;
|
|
553
|
+
}
|
|
554
|
+
function validDate(text) {
|
|
555
|
+
const date = new Date(text ?? NaN);
|
|
556
|
+
return Number.isNaN(date.getTime()) ? void 0 : date;
|
|
557
|
+
}
|
|
558
|
+
function allOk(results) {
|
|
559
|
+
const values = [];
|
|
560
|
+
for (const result of results) {
|
|
561
|
+
if (result.error) return result;
|
|
562
|
+
values.push(result.data);
|
|
563
|
+
}
|
|
564
|
+
return ok(values);
|
|
565
|
+
}
|
|
566
|
+
// src/storage/sigv4.ts
|
|
567
|
+
const encoder$4 = new TextEncoder();
|
|
568
|
+
const UNSIGNED_PAYLOAD = "UNSIGNED-PAYLOAD";
|
|
569
|
+
const EMPTY_SHA256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
|
|
570
|
+
function toHex(bytes) {
|
|
571
|
+
const view = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
572
|
+
return Array.from(view, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
573
|
+
}
|
|
574
|
+
async function sha256Hex(data) {
|
|
575
|
+
const bytes = typeof data === "string" ? encoder$4.encode(data) : data;
|
|
576
|
+
return toHex(await crypto.subtle.digest("SHA-256", bytes));
|
|
577
|
+
}
|
|
578
|
+
function importHmacKey(raw) {
|
|
579
|
+
return crypto.subtle.importKey("raw", raw, {
|
|
580
|
+
name: "HMAC",
|
|
581
|
+
hash: "SHA-256"
|
|
582
|
+
}, false, ["sign"]);
|
|
583
|
+
}
|
|
584
|
+
async function hmac(key, data) {
|
|
585
|
+
return new Uint8Array(await crypto.subtle.sign("HMAC", key, encoder$4.encode(data)));
|
|
586
|
+
}
|
|
587
|
+
async function deriveSigningKey(secretAccessKey, day, region) {
|
|
588
|
+
let raw = await hmac(await importHmacKey(encoder$4.encode(`AWS4${secretAccessKey}`)), day);
|
|
589
|
+
for (const part of [
|
|
590
|
+
region,
|
|
591
|
+
"s3",
|
|
592
|
+
"aws4_request"
|
|
593
|
+
]) raw = await hmac(await importHmacKey(raw), part);
|
|
594
|
+
return importHmacKey(raw);
|
|
595
|
+
}
|
|
596
|
+
const signingKeys = new WeakMap();
|
|
597
|
+
const MAX_CACHED_SCOPES = 8;
|
|
598
|
+
function signingKey(credentials, day, region) {
|
|
599
|
+
let cached = signingKeys.get(credentials);
|
|
600
|
+
if (cached?.secretAccessKey !== credentials.secretAccessKey) {
|
|
601
|
+
cached = {
|
|
602
|
+
secretAccessKey: credentials.secretAccessKey,
|
|
603
|
+
byScope: new Map()
|
|
604
|
+
};
|
|
605
|
+
signingKeys.set(credentials, cached);
|
|
606
|
+
}
|
|
607
|
+
const scope = `${day}/${region}`;
|
|
608
|
+
let derived = cached.byScope.get(scope);
|
|
609
|
+
if (!derived) {
|
|
610
|
+
const { byScope } = cached;
|
|
611
|
+
if (byScope.size >= MAX_CACHED_SCOPES) byScope.clear();
|
|
612
|
+
derived = deriveSigningKey(credentials.secretAccessKey, day, region);
|
|
613
|
+
derived.catch(() => byScope.delete(scope));
|
|
614
|
+
byScope.set(scope, derived);
|
|
615
|
+
}
|
|
616
|
+
return derived;
|
|
617
|
+
}
|
|
618
|
+
function encodeRfc3986(value) {
|
|
619
|
+
return encodeURIComponent(value).replace(/[!'()*]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
620
|
+
}
|
|
621
|
+
function encodeKeyPath(key) {
|
|
622
|
+
return key.split("/").map(encodeRfc3986).join("/");
|
|
623
|
+
}
|
|
624
|
+
function toAmzDate(date) {
|
|
625
|
+
return date.toISOString().replace(/[-:]|\.\d{3}/g, "");
|
|
626
|
+
}
|
|
627
|
+
function canonicalPath(url) {
|
|
628
|
+
return url.pathname.split("/").map((segment) => encodeRfc3986(decodeURIComponent(segment))).join("/");
|
|
629
|
+
}
|
|
630
|
+
function canonicalQuery(params) {
|
|
631
|
+
return Array.from(params, ([name, value]) => [encodeRfc3986(name), encodeRfc3986(value)]).sort(([aName, aValue], [bName, bValue]) => {
|
|
632
|
+
if (aName !== bName) return aName < bName ? -1 : 1;
|
|
633
|
+
if (aValue === bValue) return 0;
|
|
634
|
+
return aValue < bValue ? -1 : 1;
|
|
635
|
+
}).map(([name, value]) => `${name}=${value}`).join("&");
|
|
636
|
+
}
|
|
637
|
+
function canonicalHeaders(url, headers) {
|
|
638
|
+
const all = { host: url.host };
|
|
639
|
+
for (const [name, value] of Object.entries(headers)) all[name.toLowerCase()] = value.trim().replace(/\s+/g, " ");
|
|
640
|
+
const names = Object.keys(all).sort();
|
|
641
|
+
return {
|
|
642
|
+
canonical: names.map((name) => `${name}:${all[name]}\n`).join(""),
|
|
643
|
+
signedHeaders: names.join(";")
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
async function sign(input, amzDate, scope, canonicalRequest) {
|
|
647
|
+
const stringToSign = [
|
|
648
|
+
"AWS4-HMAC-SHA256",
|
|
649
|
+
amzDate,
|
|
650
|
+
scope,
|
|
651
|
+
await sha256Hex(canonicalRequest)
|
|
652
|
+
].join("\n");
|
|
653
|
+
return toHex(await hmac(await signingKey(input.credentials, amzDate.slice(0, 8), input.region), stringToSign));
|
|
654
|
+
}
|
|
655
|
+
async function presignUrl(input) {
|
|
656
|
+
const amzDate = toAmzDate(input.date);
|
|
657
|
+
const scope = `${amzDate.slice(0, 8)}/${input.region}/s3/aws4_request`;
|
|
658
|
+
const { canonical, signedHeaders } = canonicalHeaders(input.url, input.headers ?? {});
|
|
659
|
+
const url = new URL(input.url);
|
|
660
|
+
url.searchParams.set("X-Amz-Algorithm", "AWS4-HMAC-SHA256");
|
|
661
|
+
url.searchParams.set("X-Amz-Credential", `${input.credentials.accessKeyId}/${scope}`);
|
|
662
|
+
url.searchParams.set("X-Amz-Date", amzDate);
|
|
663
|
+
url.searchParams.set("X-Amz-Expires", String(input.expiresIn));
|
|
664
|
+
url.searchParams.set("X-Amz-SignedHeaders", signedHeaders);
|
|
665
|
+
const query = canonicalQuery(url.searchParams);
|
|
666
|
+
url.search = `${query}&X-Amz-Signature=${await sign(input, amzDate, scope, [
|
|
667
|
+
input.method,
|
|
668
|
+
canonicalPath(url),
|
|
669
|
+
query,
|
|
670
|
+
canonical,
|
|
671
|
+
signedHeaders,
|
|
672
|
+
UNSIGNED_PAYLOAD
|
|
673
|
+
].join("\n"))}`;
|
|
674
|
+
return url;
|
|
675
|
+
}
|
|
676
|
+
async function signRequest(input) {
|
|
677
|
+
const amzDate = toAmzDate(input.date);
|
|
678
|
+
const scope = `${amzDate.slice(0, 8)}/${input.region}/s3/aws4_request`;
|
|
679
|
+
const headers = {
|
|
680
|
+
...input.headers,
|
|
681
|
+
"x-amz-content-sha256": input.payloadHash,
|
|
682
|
+
"x-amz-date": amzDate
|
|
683
|
+
};
|
|
684
|
+
const { canonical, signedHeaders } = canonicalHeaders(input.url, headers);
|
|
685
|
+
const signature = await sign(input, amzDate, scope, [
|
|
686
|
+
input.method,
|
|
687
|
+
canonicalPath(input.url),
|
|
688
|
+
canonicalQuery(input.url.searchParams),
|
|
689
|
+
canonical,
|
|
690
|
+
signedHeaders,
|
|
691
|
+
input.payloadHash
|
|
692
|
+
].join("\n"));
|
|
693
|
+
return {
|
|
694
|
+
...headers,
|
|
695
|
+
authorization: `AWS4-HMAC-SHA256 Credential=${input.credentials.accessKeyId}/${scope}, SignedHeaders=${signedHeaders}, Signature=${signature}`
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
// src/storage/storage-error.ts
|
|
699
|
+
const INVALID_PART_CODES = new Set([
|
|
700
|
+
"InvalidPart",
|
|
701
|
+
"InvalidPartOrder",
|
|
702
|
+
"EntityTooSmall"
|
|
703
|
+
]);
|
|
704
|
+
const MAX_CLOCK_SKEW_MS = 899e3;
|
|
705
|
+
const CLOCK_SKEWED = {
|
|
706
|
+
matches: (_status, storageCode) => storageCode === "RequestTimeTooSkewed",
|
|
707
|
+
code: "STORAGE_DENIED",
|
|
708
|
+
message: "This server's clock is more than 15 minutes off from the storage service's clock. Sync the clock (NTP), then retry."
|
|
709
|
+
};
|
|
710
|
+
const RULES = [
|
|
711
|
+
{
|
|
712
|
+
matches: (status, storageCode) => status === 404 && storageCode === "NoSuchUpload",
|
|
713
|
+
code: "NO_SUCH_UPLOAD",
|
|
714
|
+
message: "The multipart upload no longer exists. Start a new upload."
|
|
715
|
+
},
|
|
716
|
+
{
|
|
717
|
+
matches: (status, storageCode) => status === 404 && storageCode === "NoSuchBucket",
|
|
718
|
+
code: "STORAGE_ERROR",
|
|
719
|
+
message: "The bucket does not exist. Check the bucket name in your configuration."
|
|
720
|
+
},
|
|
721
|
+
{
|
|
722
|
+
matches: (status) => status === 404,
|
|
723
|
+
code: "NOT_FOUND",
|
|
724
|
+
message: "The object does not exist. Check the key."
|
|
725
|
+
},
|
|
726
|
+
{
|
|
727
|
+
matches: (status) => status === 412,
|
|
728
|
+
code: "ALREADY_EXISTS",
|
|
729
|
+
message: "An object with this key already exists. Choose a different key."
|
|
730
|
+
},
|
|
731
|
+
{
|
|
732
|
+
matches: (_status, storageCode) => storageCode !== void 0 && INVALID_PART_CODES.has(storageCode),
|
|
733
|
+
code: "INVALID_PART",
|
|
734
|
+
message: "One or more parts are missing or have the wrong size. Upload them again, then complete the upload."
|
|
735
|
+
},
|
|
736
|
+
{
|
|
737
|
+
matches: (_status, storageCode) => storageCode === "BadDigest",
|
|
738
|
+
code: "CHECKSUM_MISMATCH",
|
|
739
|
+
message: "The uploaded bytes do not match their checksum. Upload them again."
|
|
740
|
+
},
|
|
741
|
+
CLOCK_SKEWED,
|
|
742
|
+
{
|
|
743
|
+
matches: (_status, storageCode) => storageCode === "ExpiredRequest",
|
|
744
|
+
code: "STORAGE_DENIED",
|
|
745
|
+
message: "The signed request expired. Sign it again and retry."
|
|
746
|
+
},
|
|
747
|
+
{
|
|
748
|
+
matches: (status) => status === 401,
|
|
749
|
+
code: "STORAGE_DENIED",
|
|
750
|
+
message: "The storage service did not accept these credentials. Check the access key ID and the secret access key."
|
|
751
|
+
},
|
|
752
|
+
{
|
|
753
|
+
matches: (status) => status === 403,
|
|
754
|
+
code: "STORAGE_DENIED",
|
|
755
|
+
message: "The storage service refused the request. Check the access key, its permissions and the bucket name."
|
|
756
|
+
},
|
|
757
|
+
{
|
|
758
|
+
matches: (status) => status === 429 || status === 503,
|
|
759
|
+
code: "RATE_LIMITED",
|
|
760
|
+
message: "The storage service is throttling requests. Retry after a short delay.",
|
|
761
|
+
retryable: true
|
|
762
|
+
},
|
|
763
|
+
{
|
|
764
|
+
matches: (status) => status === 501,
|
|
765
|
+
code: "STORAGE_ERROR",
|
|
766
|
+
message: "The storage service does not support this request. Use R2 or another backend that implements it."
|
|
767
|
+
},
|
|
768
|
+
{
|
|
769
|
+
matches: (status, storageCode) => status >= 500 || status === 408 || storageCode === "OperationAborted",
|
|
770
|
+
code: "STORAGE_ERROR",
|
|
771
|
+
message: "The storage service failed. Retry the request.",
|
|
772
|
+
retryable: true
|
|
773
|
+
}
|
|
774
|
+
];
|
|
775
|
+
function storageError(status, body, clockOffsetMs = 0) {
|
|
776
|
+
const rawCode = xmlValue(body, "Code");
|
|
777
|
+
const storageCode = rawCode && /^[A-Za-z0-9]{1,64}$/.test(rawCode) ? rawCode : void 0;
|
|
778
|
+
const rule = status === 403 && storageCode === void 0 && Math.abs(clockOffsetMs) > MAX_CLOCK_SKEW_MS ? CLOCK_SKEWED : RULES.find((candidate) => candidate.matches(status, storageCode));
|
|
779
|
+
if (rule) return new CloudropError(rule.code, rule.message, {
|
|
780
|
+
status,
|
|
781
|
+
storageCode,
|
|
782
|
+
retryable: rule.retryable
|
|
783
|
+
});
|
|
784
|
+
return new CloudropError("STORAGE_ERROR", `The storage service rejected the request (HTTP ${status}). Check the key, size and headers you passed, then retry.`, {
|
|
785
|
+
status,
|
|
786
|
+
storageCode
|
|
787
|
+
});
|
|
788
|
+
}
|
|
789
|
+
// src/storage/s3/transport.ts
|
|
790
|
+
const decoder = new TextDecoder();
|
|
791
|
+
const REQUEST_TIMEOUT_MS = 3e4;
|
|
792
|
+
const SLOW_REQUEST_TIMEOUT_MS = 3e5;
|
|
793
|
+
function quotesCredentials(error, depth = 0) {
|
|
794
|
+
if (depth > 8) return true;
|
|
795
|
+
if (typeof error === "string") return error.includes("Credential=");
|
|
796
|
+
if (typeof error !== "object" || error === null) return false;
|
|
797
|
+
const { message, cause } = error;
|
|
798
|
+
return typeof message === "string" && message.includes("Credential=") || quotesCredentials(cause, depth + 1);
|
|
799
|
+
}
|
|
800
|
+
function unreachable(cause, timeoutMs) {
|
|
801
|
+
let timedOut = false;
|
|
802
|
+
let safeCause;
|
|
803
|
+
try {
|
|
804
|
+
timedOut = cause instanceof DOMException && cause.name === "TimeoutError";
|
|
805
|
+
if (!quotesCredentials(cause)) safeCause = cause;
|
|
806
|
+
} catch {}
|
|
807
|
+
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.", {
|
|
808
|
+
retryable: true,
|
|
809
|
+
cause: safeCause
|
|
810
|
+
});
|
|
811
|
+
}
|
|
812
|
+
async function send(ctx, method, url, options = {}) {
|
|
813
|
+
const { http, now, credentials, region } = ctx;
|
|
814
|
+
url.search = canonicalQuery(url.searchParams);
|
|
815
|
+
const { headers = {}, body, timeoutMs = REQUEST_TIMEOUT_MS } = options;
|
|
816
|
+
const signedAt = now();
|
|
817
|
+
const signed = await signRequest({
|
|
818
|
+
method,
|
|
819
|
+
url,
|
|
820
|
+
headers,
|
|
821
|
+
credentials,
|
|
822
|
+
region,
|
|
823
|
+
date: signedAt,
|
|
824
|
+
payloadHash: body instanceof Blob ? UNSIGNED_PAYLOAD : body ? await sha256Hex(body) : EMPTY_SHA256
|
|
825
|
+
});
|
|
826
|
+
const sent = method === "HEAD" ? {
|
|
827
|
+
...signed,
|
|
828
|
+
"accept-encoding": "identity"
|
|
829
|
+
} : signed;
|
|
830
|
+
try {
|
|
831
|
+
const response = await http(url, {
|
|
832
|
+
method,
|
|
833
|
+
headers: sent,
|
|
834
|
+
body,
|
|
835
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
836
|
+
});
|
|
837
|
+
if (!response.ok) {
|
|
838
|
+
const clockOffsetMs = Date.parse(response.headers.get("date") ?? "") - signedAt.getTime();
|
|
839
|
+
return fail(storageError(response.status, await response.text(), clockOffsetMs));
|
|
840
|
+
}
|
|
841
|
+
if (headers.range !== void 0 && response.status !== 206) {
|
|
842
|
+
await response.body?.cancel();
|
|
843
|
+
return fail(new CloudropError("STORAGE_ERROR", "The storage service ignored the byte range. Check that the endpoint supports ranged GET.", { status: response.status }));
|
|
844
|
+
}
|
|
845
|
+
return ok({
|
|
846
|
+
headers: response.headers,
|
|
847
|
+
body: new Uint8Array(await response.arrayBuffer())
|
|
848
|
+
});
|
|
849
|
+
} catch (cause) {
|
|
850
|
+
return fail(unreachable(cause, timeoutMs));
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
async function sendForXml(ctx, method, url, options) {
|
|
854
|
+
const result = await send(ctx, method, url, options);
|
|
855
|
+
if (result.error) return result;
|
|
856
|
+
const text = decoder.decode(result.data.body);
|
|
857
|
+
if (text.includes("<Error>")) return fail(storageError(500, text));
|
|
858
|
+
return ok(text);
|
|
859
|
+
}
|
|
860
|
+
// src/storage/s3/bucket-settings.ts
|
|
861
|
+
function configurationReadError(error, missingCode, setting) {
|
|
862
|
+
if (error.status === 404 && error.storageCode === missingCode) return ok([]);
|
|
863
|
+
if (error.code !== "NOT_FOUND") return fail(error);
|
|
864
|
+
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.`, {
|
|
865
|
+
status: 404,
|
|
866
|
+
storageCode: error.storageCode
|
|
867
|
+
}));
|
|
868
|
+
}
|
|
869
|
+
const UNREADABLE_XML = /<!\[CDATA\[|<!--|<\/?[A-Za-z_][\w.-]*:/;
|
|
870
|
+
function readConfiguration(xml, root, setting) {
|
|
871
|
+
if (new RegExp(`<${root}(?:\\s[^>]*)?/>`).test(xml)) return ok("");
|
|
872
|
+
const configuration = xmlBlocks(xml, root)[0];
|
|
873
|
+
if (configuration === void 0) return fail(incompleteResponse(`${setting} configuration`));
|
|
874
|
+
if (UNREADABLE_XML.test(configuration)) return fail(new CloudropError("STORAGE_ERROR", `The bucket's ${setting} configuration uses XML that cloudrop does not read (CDATA, comments or namespace prefixes). Check its rules by hand.`));
|
|
875
|
+
return ok(configuration);
|
|
876
|
+
}
|
|
877
|
+
function readCorsRule(rule) {
|
|
878
|
+
const id = xmlText(rule, "ID");
|
|
879
|
+
const maxAge = xmlValue(rule, "MaxAgeSeconds");
|
|
880
|
+
const maxAgeSeconds = wholeNumber(maxAge);
|
|
881
|
+
if (maxAge !== void 0 && maxAgeSeconds === void 0) return fail(incompleteResponse("valid CORS max age"));
|
|
882
|
+
return ok({
|
|
883
|
+
...id === void 0 ? {} : { id },
|
|
884
|
+
allowedOrigins: xmlValues(rule, "AllowedOrigin"),
|
|
885
|
+
allowedMethods: xmlValues(rule, "AllowedMethod"),
|
|
886
|
+
allowedHeaders: xmlValues(rule, "AllowedHeader"),
|
|
887
|
+
exposeHeaders: xmlValues(rule, "ExposeHeader"),
|
|
888
|
+
...maxAgeSeconds === void 0 ? {} : { maxAgeSeconds }
|
|
889
|
+
});
|
|
890
|
+
}
|
|
891
|
+
function readLifecycleRule(rule) {
|
|
892
|
+
const days = xmlValue(xmlBlocks(rule, "AbortIncompleteMultipartUpload")[0] ?? "", "DaysAfterInitiation");
|
|
893
|
+
if (days !== void 0 && !/^\d{1,9}$/.test(days)) return fail(incompleteResponse("valid multipart abort days"));
|
|
894
|
+
return ok({
|
|
895
|
+
id: xmlText(rule, "ID") ?? "",
|
|
896
|
+
enabled: xmlValue(rule, "Status") === "Enabled",
|
|
897
|
+
prefix: xmlText(rule, "Prefix") ?? "",
|
|
898
|
+
...days === void 0 ? {} : { abortIncompleteMultipartUploadDays: Number(days) },
|
|
899
|
+
sourceXml: rule
|
|
900
|
+
});
|
|
901
|
+
}
|
|
902
|
+
function bucketSettingsMethods(ctx) {
|
|
903
|
+
return {
|
|
904
|
+
async getBucketCors() {
|
|
905
|
+
const result = await sendForXml(ctx, "GET", new URL(`${ctx.bucketUrl}?cors=`));
|
|
906
|
+
if (result.error) return configurationReadError(result.error, "NoSuchCORSConfiguration", "CORS");
|
|
907
|
+
const configuration = readConfiguration(result.data, "CORSConfiguration", "CORS");
|
|
908
|
+
if (configuration.error) return configuration;
|
|
909
|
+
return allOk(xmlBlocks(configuration.data, "CORSRule").map(readCorsRule));
|
|
910
|
+
},
|
|
911
|
+
async getBucketLifecycle() {
|
|
912
|
+
const result = await sendForXml(ctx, "GET", new URL(`${ctx.bucketUrl}?lifecycle=`));
|
|
913
|
+
if (result.error) return configurationReadError(result.error, "NoSuchLifecycleConfiguration", "lifecycle");
|
|
914
|
+
const configuration = readConfiguration(result.data, "LifecycleConfiguration", "lifecycle");
|
|
915
|
+
if (configuration.error) return configuration;
|
|
916
|
+
return allOk(xmlBlocks(configuration.data, "Rule").map(readLifecycleRule));
|
|
917
|
+
}
|
|
918
|
+
};
|
|
919
|
+
}
|
|
920
|
+
// src/input.ts
|
|
921
|
+
function invalidInput(message) {
|
|
922
|
+
return new CloudropError("INVALID_INPUT", message);
|
|
923
|
+
}
|
|
924
|
+
function isPlainObject(value) {
|
|
925
|
+
if (typeof value !== "object" || value === null) return false;
|
|
926
|
+
const prototype = Object.getPrototypeOf(value);
|
|
927
|
+
return prototype === Object.prototype || prototype === null;
|
|
928
|
+
}
|
|
929
|
+
// src/storage/s3/bucket-writes.ts
|
|
930
|
+
const encoder$3 = new TextEncoder();
|
|
931
|
+
const element = (name, value) => `<${name}>${escapeXml(String(value))}</${name}>`;
|
|
932
|
+
const elements = (name, values) => values.map((v) => element(name, v)).join("");
|
|
933
|
+
const optional = (name, value) => value === void 0 || value === "" ? "" : element(name, value);
|
|
934
|
+
function corsRuleXml(rule) {
|
|
935
|
+
return `<CORSRule>${optional("ID", rule.id)}${elements("AllowedOrigin", rule.allowedOrigins)}${elements("AllowedMethod", rule.allowedMethods)}${elements("AllowedHeader", rule.allowedHeaders)}${elements("ExposeHeader", rule.exposeHeaders)}${optional("MaxAgeSeconds", rule.maxAgeSeconds)}</CORSRule>`;
|
|
936
|
+
}
|
|
937
|
+
function lifecycleRuleXml(rule) {
|
|
938
|
+
if (rule.sourceXml !== void 0) return `<Rule>${rule.sourceXml}</Rule>`;
|
|
939
|
+
const abort = element("DaysAfterInitiation", rule.abortIncompleteMultipartUploadDays ?? "");
|
|
940
|
+
return `<Rule>${optional("ID", rule.id)}<Filter>${element("Prefix", rule.prefix)}</Filter><Status>${rule.enabled ? "Enabled" : "Disabled"}</Status><AbortIncompleteMultipartUpload>${abort}</AbortIncompleteMultipartUpload></Rule>`;
|
|
941
|
+
}
|
|
942
|
+
const emptyProblem = (rules, setting) => Array.isArray(rules) && rules.length > 0 ? null : `Pass at least one ${setting} rule: an empty list would remove every rule.`;
|
|
943
|
+
async function putConfiguration(ctx, subresource, xml, extraHeaders = {}) {
|
|
944
|
+
const body = encoder$3.encode(`<?xml version="1.0" encoding="UTF-8"?>${xml}`);
|
|
945
|
+
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", body));
|
|
946
|
+
const headers = {
|
|
947
|
+
...extraHeaders,
|
|
948
|
+
"content-type": "application/xml",
|
|
949
|
+
"x-amz-checksum-sha256": btoa(String.fromCharCode(...digest))
|
|
950
|
+
};
|
|
951
|
+
const result = await send(ctx, "PUT", new URL(`${ctx.bucketUrl}?${subresource}=`), {
|
|
952
|
+
headers,
|
|
953
|
+
body
|
|
954
|
+
});
|
|
955
|
+
return result.error ? result : ok(void 0);
|
|
956
|
+
}
|
|
957
|
+
const NAMESPACE = " xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"";
|
|
958
|
+
const TRANSITION_DEFAULT = "x-amz-transition-default-minimum-object-size";
|
|
959
|
+
async function keptLifecycleHeaders(ctx) {
|
|
960
|
+
const kept = {};
|
|
961
|
+
const current = await send(ctx, "GET", new URL(`${ctx.bucketUrl}?lifecycle=`));
|
|
962
|
+
if (current.error) {
|
|
963
|
+
if (current.error.code === "STORAGE_DENIED" || current.error.status === 501) return ok(kept);
|
|
964
|
+
const read = configurationReadError(current.error, "NoSuchLifecycleConfiguration", "lifecycle");
|
|
965
|
+
return read.error ? read : ok(kept);
|
|
966
|
+
}
|
|
967
|
+
const value = current.data.headers.get(TRANSITION_DEFAULT);
|
|
968
|
+
if (value) kept[TRANSITION_DEFAULT] = value;
|
|
969
|
+
return ok(kept);
|
|
970
|
+
}
|
|
971
|
+
function bucketWriteMethods(ctx) {
|
|
972
|
+
return {
|
|
973
|
+
async putBucketCors(rules) {
|
|
974
|
+
const problem = emptyProblem(rules, "CORS");
|
|
975
|
+
if (problem) return fail(invalidInput(problem));
|
|
976
|
+
return putConfiguration(ctx, "cors", `<CORSConfiguration${NAMESPACE}>${rules.map(corsRuleXml).join("")}</CORSConfiguration>`);
|
|
977
|
+
},
|
|
978
|
+
async putBucketLifecycle(rules) {
|
|
979
|
+
const problem = emptyProblem(rules, "lifecycle");
|
|
980
|
+
if (problem) return fail(invalidInput(problem));
|
|
981
|
+
const kept = await keptLifecycleHeaders(ctx);
|
|
982
|
+
if (kept.error) return kept;
|
|
983
|
+
return putConfiguration(ctx, "lifecycle", `<LifecycleConfiguration${NAMESPACE}>${rules.map(lifecycleRuleXml).join("")}</LifecycleConfiguration>`, kept.data);
|
|
984
|
+
}
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
// src/storage/s3/settings.ts
|
|
988
|
+
function checkBaseUrl(value, setting, example) {
|
|
989
|
+
const url = typeof value === "string" && URL.canParse(value) ? new URL(value) : null;
|
|
990
|
+
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.`);
|
|
991
|
+
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}.`);
|
|
992
|
+
try {
|
|
993
|
+
for (const segment of url.pathname.split("/")) decodeURIComponent(segment);
|
|
994
|
+
} catch {
|
|
995
|
+
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.`);
|
|
996
|
+
}
|
|
997
|
+
return url;
|
|
998
|
+
}
|
|
999
|
+
const SIGNABLE = /^[\x21-\x7e]+$/;
|
|
1000
|
+
function checkSigningSettings({ credentials, region }) {
|
|
1001
|
+
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.`);
|
|
1002
|
+
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.");
|
|
1003
|
+
}
|
|
1004
|
+
// src/storage/s3/context.ts
|
|
1005
|
+
function createContext(config) {
|
|
1006
|
+
const http = config.fetch ?? ((input, init) => globalThis.fetch(input, init));
|
|
1007
|
+
if (typeof http !== "function") throw configError("The fetch option must be a function, such as globalThis.fetch. Omit it to use the global fetch.");
|
|
1008
|
+
checkSigningSettings(config);
|
|
1009
|
+
const now = config.now ?? (() => new Date());
|
|
1010
|
+
const { credentials, region, rules } = config;
|
|
1011
|
+
const endpoint = checkBaseUrl(config.endpoint, "storage endpoint", "https://<account ID>.r2.cloudflarestorage.com");
|
|
1012
|
+
const bucketUrl = `${endpoint.origin}${endpoint.pathname.replace(/\/$/, "")}/${encodeRfc3986(config.bucket)}`;
|
|
1013
|
+
return {
|
|
1014
|
+
http,
|
|
1015
|
+
now,
|
|
1016
|
+
credentials,
|
|
1017
|
+
region,
|
|
1018
|
+
rules,
|
|
1019
|
+
bucket: config.bucket,
|
|
1020
|
+
bucketUrl
|
|
1021
|
+
};
|
|
1022
|
+
}
|
|
1023
|
+
function objectUrl(ctx, key, query = {}) {
|
|
1024
|
+
const url = new URL(`${ctx.bucketUrl}/${encodeKeyPath(key)}`);
|
|
1025
|
+
for (const [name, value] of Object.entries(query)) url.searchParams.set(name, value);
|
|
1026
|
+
return url;
|
|
1027
|
+
}
|
|
1028
|
+
// src/storage/validate.ts
|
|
1029
|
+
const encoder$2 = new TextEncoder();
|
|
1030
|
+
const MAX_KEY_BYTES = 1024;
|
|
1031
|
+
const MAX_METADATA_BYTES = 2048;
|
|
1032
|
+
function keyProblem(key) {
|
|
1033
|
+
if (typeof key !== "string") return "The object key must be a string. Pass the key as text.";
|
|
1034
|
+
if (key.length === 0) return "The object key is empty. Pass a non-empty key.";
|
|
1035
|
+
if (!key.isWellFormed()) return "The object key is not valid Unicode (it has a lone surrogate). Remove or replace that character.";
|
|
1036
|
+
if (encoder$2.encode(key).length > MAX_KEY_BYTES) return "The object key is longer than 1024 bytes. Use a shorter key.";
|
|
1037
|
+
if (key.startsWith("/")) return "The object key starts with a slash. Remove the leading slash.";
|
|
1038
|
+
for (const char of key) {
|
|
1039
|
+
const code = char.codePointAt(0) ?? 0;
|
|
1040
|
+
if (code < 32 || code >= 127 && code <= 159) return "The object key contains control characters. Remove them.";
|
|
1041
|
+
}
|
|
1042
|
+
if (key.split("/").some((segment) => segment === "." || segment === "..")) return "The object key contains a '.' or '..' segment. Use plain path segments.";
|
|
1043
|
+
return null;
|
|
1044
|
+
}
|
|
1045
|
+
const isPrintableAscii = (value) => typeof value === "string" && /^[\x20-\x7e]*$/.test(value);
|
|
1046
|
+
function metadataProblem(metadata) {
|
|
1047
|
+
if (metadata === void 0) return null;
|
|
1048
|
+
if (!isPlainObject(metadata)) return "Metadata must be an object of names and text values, such as { \"cloudrop-id\": \"f1\" }.";
|
|
1049
|
+
let bytes = 0;
|
|
1050
|
+
for (const [name, value] of Object.entries(metadata)) {
|
|
1051
|
+
if (!/^[a-z0-9-]+$/.test(name)) return "A metadata name may contain only lowercase letters, digits and hyphens. Rename it.";
|
|
1052
|
+
if (typeof value !== "string") return `Metadata value for "${name}" must be a string. Convert it first.`;
|
|
1053
|
+
if (!isPrintableAscii(value)) return `Metadata value for "${name}" must be printable ASCII. Encode other text first.`;
|
|
1054
|
+
if (value.startsWith(" ") || value.endsWith(" ")) return `Metadata value for "${name}" must not start or end with a space. Trim it.`;
|
|
1055
|
+
bytes += name.length + value.length;
|
|
1056
|
+
}
|
|
1057
|
+
return bytes > MAX_METADATA_BYTES ? "Metadata exceeds 2 KiB. Store large values in your database instead." : null;
|
|
1058
|
+
}
|
|
1059
|
+
function objectOptionsProblem(options) {
|
|
1060
|
+
if (options.type !== void 0 && !isPrintableAscii(options.type)) return "The content type must be printable ASCII, such as \"text/plain; charset=utf-8\".";
|
|
1061
|
+
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.";
|
|
1062
|
+
return metadataProblem(options.metadata);
|
|
1063
|
+
}
|
|
1064
|
+
function bodyProblem(body) {
|
|
1065
|
+
if (body instanceof Uint8Array || body instanceof ArrayBuffer) {
|
|
1066
|
+
const buffer = body instanceof Uint8Array ? body.buffer : body;
|
|
1067
|
+
if (buffer instanceof ArrayBuffer && !buffer.detached) return null;
|
|
1068
|
+
return "The body's buffer is detached or shared (a SharedArrayBuffer). Copy the bytes into a new Uint8Array first.";
|
|
1069
|
+
}
|
|
1070
|
+
if (typeof body === "string" || body instanceof Blob) return null;
|
|
1071
|
+
return "The body must be a Uint8Array, an ArrayBuffer, a string or a Blob.";
|
|
1072
|
+
}
|
|
1073
|
+
function checksumProblem(checksum) {
|
|
1074
|
+
if (checksum === void 0 || /^[A-Za-z0-9+/]{5}[AQgw]==$/.test(checksum)) return null;
|
|
1075
|
+
return "The CRC32C checksum must be the base64 of 4 bytes, such as \"4waSgw==\". Compute it from the exact bytes you send.";
|
|
1076
|
+
}
|
|
1077
|
+
const isToken = (value) => typeof value === "string" && /^[\x21-\x7e]+$/.test(value);
|
|
1078
|
+
function uploadIdProblem(uploadId) {
|
|
1079
|
+
return isToken(uploadId) ? null : "The upload ID is empty or malformed. Pass the uploadId that createMultipart returned.";
|
|
1080
|
+
}
|
|
1081
|
+
function cursorProblem(cursor) {
|
|
1082
|
+
return isToken(cursor) ? null : "The list cursor is empty or malformed. Pass the nextCursor of the previous page.";
|
|
1083
|
+
}
|
|
1084
|
+
function prefixProblem(prefix) {
|
|
1085
|
+
if (typeof prefix !== "string") return "The list prefix must be a string. Pass \"\" to list every object.";
|
|
1086
|
+
if (prefix.isWellFormed()) return null;
|
|
1087
|
+
return "The list prefix is not valid Unicode (it has a lone surrogate). Remove or replace that character.";
|
|
1088
|
+
}
|
|
1089
|
+
const MAX_LIST_PAGE = 1e3;
|
|
1090
|
+
function limitProblem(limit) {
|
|
1091
|
+
if (Number.isInteger(limit) && limit >= 1 && limit <= 1e3) return null;
|
|
1092
|
+
return `The page size must be between 1 and ${MAX_LIST_PAGE}.`;
|
|
1093
|
+
}
|
|
1094
|
+
function partsProblem(parts, maxParts) {
|
|
1095
|
+
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.`;
|
|
1096
|
+
}
|
|
1097
|
+
function keyListProblem(keys) {
|
|
1098
|
+
return Array.isArray(keys) ? null : "deleteMany takes an array of keys. Wrap a single key in an array.";
|
|
1099
|
+
}
|
|
1100
|
+
const BYTE_UNITS = [
|
|
1101
|
+
["GiB", GiB],
|
|
1102
|
+
["MiB", MiB],
|
|
1103
|
+
["KiB", 1024]
|
|
1104
|
+
];
|
|
1105
|
+
function formatBytes(bytes) {
|
|
1106
|
+
const unit = BYTE_UNITS.find(([, size]) => bytes >= size);
|
|
1107
|
+
return unit ? `${Number((bytes / unit[1]).toFixed(3))} ${unit[0]}` : `${bytes} bytes`;
|
|
1108
|
+
}
|
|
1109
|
+
function sizeProblem(size, max, of = "file") {
|
|
1110
|
+
if (!Number.isSafeInteger(size) || size < 0) return "Size must be a whole number of bytes, zero or more.";
|
|
1111
|
+
if (size <= max) return null;
|
|
1112
|
+
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.`;
|
|
1113
|
+
}
|
|
1114
|
+
function expiryProblem(seconds, max) {
|
|
1115
|
+
if (Number.isInteger(seconds) && seconds >= 1 && seconds <= max) return null;
|
|
1116
|
+
return `Expiry must be a whole number of seconds between 1 and ${max}.`;
|
|
1117
|
+
}
|
|
1118
|
+
function partNumberProblem(partNumber, maxParts) {
|
|
1119
|
+
if (Number.isInteger(partNumber) && partNumber >= 1 && partNumber <= maxParts) return null;
|
|
1120
|
+
return `Part number must be between 1 and ${maxParts}.`;
|
|
1121
|
+
}
|
|
1122
|
+
function rangeProblem(start, end) {
|
|
1123
|
+
if (Number.isSafeInteger(start) && Number.isSafeInteger(end) && start >= 0 && end >= start) return null;
|
|
1124
|
+
return "The byte range must satisfy 0 <= start <= end.";
|
|
1125
|
+
}
|
|
1126
|
+
// src/storage/s3/options.ts
|
|
1127
|
+
const META_PREFIX = "x-amz-meta-";
|
|
1128
|
+
function objectHeaders(options) {
|
|
1129
|
+
const headers = {};
|
|
1130
|
+
if (options.type) headers["content-type"] = options.type;
|
|
1131
|
+
if (options.disposition) headers["content-disposition"] = options.disposition;
|
|
1132
|
+
for (const [name, value] of Object.entries(options.metadata ?? {})) headers[`${META_PREFIX}${name}`] = value;
|
|
1133
|
+
return headers;
|
|
1134
|
+
}
|
|
1135
|
+
function writeHeaders(options) {
|
|
1136
|
+
const headers = objectHeaders(options);
|
|
1137
|
+
if (options.ifNoneMatch) headers["if-none-match"] = "*";
|
|
1138
|
+
if (options.checksumCrc32c) headers["x-amz-checksum-crc32c"] = options.checksumCrc32c;
|
|
1139
|
+
return headers;
|
|
1140
|
+
}
|
|
1141
|
+
function orEmpty(options) {
|
|
1142
|
+
return options ?? {};
|
|
1143
|
+
}
|
|
1144
|
+
// src/storage/s3/multipart.ts
|
|
1145
|
+
const encoder$1 = new TextEncoder();
|
|
1146
|
+
function readPart(block, maxParts) {
|
|
1147
|
+
const partNumber = wholeNumber(xmlValue(block, "PartNumber"));
|
|
1148
|
+
if (partNumber === void 0 || partNumber < 1 || partNumber > maxParts) return fail(incompleteResponse("valid part number"));
|
|
1149
|
+
const size = wholeNumber(xmlValue(block, "Size"));
|
|
1150
|
+
if (size === void 0) return fail(incompleteResponse("valid part size"));
|
|
1151
|
+
const etag = etagIn(block);
|
|
1152
|
+
if (etag === "") return fail(incompleteResponse("part ETag"));
|
|
1153
|
+
return ok({
|
|
1154
|
+
partNumber,
|
|
1155
|
+
size,
|
|
1156
|
+
etag,
|
|
1157
|
+
checksumCrc32c: xmlValue(block, "ChecksumCRC32C")
|
|
1158
|
+
});
|
|
1159
|
+
}
|
|
1160
|
+
async function partsPage(ctx, key, uploadId, marker) {
|
|
1161
|
+
const result = await sendForXml(ctx, "GET", objectUrl(ctx, key, {
|
|
1162
|
+
uploadId,
|
|
1163
|
+
"max-parts": String(MAX_LIST_PAGE),
|
|
1164
|
+
"part-number-marker": marker
|
|
1165
|
+
}));
|
|
1166
|
+
if (result.error) return result;
|
|
1167
|
+
const parts = allOk(xmlBlocks(result.data, "Part").map((block) => readPart(block, ctx.rules.maxParts)));
|
|
1168
|
+
if (parts.error) return parts;
|
|
1169
|
+
if (xmlValue(result.data, "IsTruncated") !== "true") return ok({
|
|
1170
|
+
parts: parts.data,
|
|
1171
|
+
next: null
|
|
1172
|
+
});
|
|
1173
|
+
const next = xmlValue(result.data, "NextPartNumberMarker") ?? "";
|
|
1174
|
+
if (!(Number(next) > Number(marker)) || Number(next) > ctx.rules.maxParts) return fail(incompleteResponse("valid next part marker"));
|
|
1175
|
+
return ok({
|
|
1176
|
+
parts: parts.data,
|
|
1177
|
+
next
|
|
1178
|
+
});
|
|
1179
|
+
}
|
|
1180
|
+
async function startMultipart(ctx, key, options) {
|
|
1181
|
+
const result = await sendForXml(ctx, "POST", objectUrl(ctx, key, { uploads: "" }), { headers: objectHeaders(options) });
|
|
1182
|
+
if (result.error) return result;
|
|
1183
|
+
const uploadId = xmlText(result.data, "UploadId");
|
|
1184
|
+
return uploadId ? ok({ uploadId }) : fail(incompleteResponse("upload ID"));
|
|
1185
|
+
}
|
|
1186
|
+
function multipartMethods(ctx) {
|
|
1187
|
+
const { rules } = ctx;
|
|
1188
|
+
return {
|
|
1189
|
+
async createMultipart(key, given) {
|
|
1190
|
+
const options = orEmpty(given);
|
|
1191
|
+
const problem = keyProblem(key) ?? objectOptionsProblem(options);
|
|
1192
|
+
if (problem) return fail(invalidInput(problem));
|
|
1193
|
+
return startMultipart(ctx, key, options);
|
|
1194
|
+
},
|
|
1195
|
+
async listParts(key, uploadId) {
|
|
1196
|
+
const problem = keyProblem(key) ?? uploadIdProblem(uploadId);
|
|
1197
|
+
if (problem) return fail(invalidInput(problem));
|
|
1198
|
+
const parts = [];
|
|
1199
|
+
let marker = "0";
|
|
1200
|
+
while (marker !== null) {
|
|
1201
|
+
const page = await partsPage(ctx, key, uploadId, marker);
|
|
1202
|
+
if (page.error) return page;
|
|
1203
|
+
parts.push(...page.data.parts);
|
|
1204
|
+
marker = page.data.next;
|
|
1205
|
+
}
|
|
1206
|
+
return ok(parts);
|
|
1207
|
+
},
|
|
1208
|
+
async completeMultipart(key, uploadId, parts, given) {
|
|
1209
|
+
const options = orEmpty(given);
|
|
1210
|
+
const problem = keyProblem(key) ?? uploadIdProblem(uploadId) ?? partsProblem(parts, rules.maxParts);
|
|
1211
|
+
if (problem) return fail(invalidInput(problem));
|
|
1212
|
+
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("");
|
|
1213
|
+
const result = await sendForXml(ctx, "POST", objectUrl(ctx, key, { uploadId }), {
|
|
1214
|
+
headers: options.ifNoneMatch ? { "if-none-match": "*" } : {},
|
|
1215
|
+
body: encoder$1.encode(`<CompleteMultipartUpload>${xml}</CompleteMultipartUpload>`),
|
|
1216
|
+
timeoutMs: SLOW_REQUEST_TIMEOUT_MS
|
|
1217
|
+
});
|
|
1218
|
+
if (result.error) return result;
|
|
1219
|
+
const etag = etagIn(result.data);
|
|
1220
|
+
return etag ? ok({ etag }) : fail(incompleteResponse("ETag"));
|
|
1221
|
+
},
|
|
1222
|
+
async abortMultipart(key, uploadId) {
|
|
1223
|
+
const problem = keyProblem(key) ?? uploadIdProblem(uploadId);
|
|
1224
|
+
if (problem) return fail(invalidInput(problem));
|
|
1225
|
+
const result = await send(ctx, "DELETE", objectUrl(ctx, key, { uploadId }));
|
|
1226
|
+
if (result.error && result.error.code !== "NO_SUCH_UPLOAD") return result;
|
|
1227
|
+
return ok(void 0);
|
|
1228
|
+
}
|
|
1229
|
+
};
|
|
1230
|
+
}
|
|
1231
|
+
// src/storage/s3/copy.ts
|
|
1232
|
+
function copyError(error) {
|
|
1233
|
+
if (error.status !== 412) return error;
|
|
1234
|
+
return new CloudropError("STORAGE_ERROR", "The object to copy changed during the copy. Retry the copy.", {
|
|
1235
|
+
status: 412,
|
|
1236
|
+
storageCode: error.storageCode,
|
|
1237
|
+
retryable: true
|
|
1238
|
+
});
|
|
1239
|
+
}
|
|
1240
|
+
async function copyInParts(ctx, steps, source, copyHeaders, to) {
|
|
1241
|
+
const partSize = ctx.rules.maxSinglePutSize;
|
|
1242
|
+
const partCount = Math.ceil(source.size / partSize);
|
|
1243
|
+
const created = await startMultipart(ctx, to, source);
|
|
1244
|
+
if (created.error) return created;
|
|
1245
|
+
const { uploadId } = created.data;
|
|
1246
|
+
const abortWith = async (error) => {
|
|
1247
|
+
await steps.abortMultipart(to, uploadId);
|
|
1248
|
+
return fail(error);
|
|
1249
|
+
};
|
|
1250
|
+
const parts = [];
|
|
1251
|
+
for (let partNumber = 1; partNumber <= partCount; partNumber++) {
|
|
1252
|
+
const start = (partNumber - 1) * partSize;
|
|
1253
|
+
const end = Math.min(start + partSize, source.size) - 1;
|
|
1254
|
+
const result = await sendForXml(ctx, "PUT", objectUrl(ctx, to, {
|
|
1255
|
+
partNumber: String(partNumber),
|
|
1256
|
+
uploadId
|
|
1257
|
+
}), {
|
|
1258
|
+
headers: {
|
|
1259
|
+
...copyHeaders,
|
|
1260
|
+
"x-amz-copy-source-range": `bytes=${start}-${end}`
|
|
1261
|
+
},
|
|
1262
|
+
timeoutMs: SLOW_REQUEST_TIMEOUT_MS
|
|
1263
|
+
});
|
|
1264
|
+
if (result.error) return abortWith(copyError(result.error));
|
|
1265
|
+
const etag = etagIn(result.data);
|
|
1266
|
+
if (!etag) return abortWith(incompleteResponse("ETag"));
|
|
1267
|
+
parts.push({
|
|
1268
|
+
partNumber,
|
|
1269
|
+
etag
|
|
1270
|
+
});
|
|
1271
|
+
}
|
|
1272
|
+
const completed = await steps.completeMultipart(to, uploadId, parts);
|
|
1273
|
+
return completed.error ? abortWith(completed.error) : completed;
|
|
1274
|
+
}
|
|
1275
|
+
function copyMethods(ctx, steps) {
|
|
1276
|
+
return { async copy(from, to) {
|
|
1277
|
+
const problem = keyProblem(from) ?? keyProblem(to);
|
|
1278
|
+
if (problem) return fail(invalidInput(problem));
|
|
1279
|
+
const source = await steps.head(from);
|
|
1280
|
+
if (source.error) return source;
|
|
1281
|
+
if (!source.data) return fail(new CloudropError("NOT_FOUND", "The object to copy does not exist. Check the source key."));
|
|
1282
|
+
const copyHeaders = {
|
|
1283
|
+
"x-amz-copy-source": `/${encodeRfc3986(ctx.bucket)}/${encodeKeyPath(from)}`,
|
|
1284
|
+
"x-amz-copy-source-if-match": `"${source.data.etag}"`
|
|
1285
|
+
};
|
|
1286
|
+
if (source.data.size > ctx.rules.maxSinglePutSize) return copyInParts(ctx, steps, source.data, copyHeaders, to);
|
|
1287
|
+
const result = await sendForXml(ctx, "PUT", objectUrl(ctx, to), {
|
|
1288
|
+
headers: copyHeaders,
|
|
1289
|
+
timeoutMs: SLOW_REQUEST_TIMEOUT_MS
|
|
1290
|
+
});
|
|
1291
|
+
if (result.error) return fail(copyError(result.error));
|
|
1292
|
+
const etag = etagIn(result.data);
|
|
1293
|
+
return etag ? ok({ etag }) : fail(incompleteResponse("ETag"));
|
|
1294
|
+
} };
|
|
1295
|
+
}
|
|
1296
|
+
// src/storage/s3/objects.ts
|
|
1297
|
+
const encoder = new TextEncoder();
|
|
1298
|
+
const DELETE_CONCURRENCY = 16;
|
|
1299
|
+
function readObjectInfo(key, headers) {
|
|
1300
|
+
if (!headers.has("content-length")) return fail(incompleteResponse("content length"));
|
|
1301
|
+
const size = wholeNumber(headers.get("content-length"));
|
|
1302
|
+
if (size === void 0) return fail(incompleteResponse("valid content length"));
|
|
1303
|
+
const etag = stripQuotes(headers.get("etag") ?? "");
|
|
1304
|
+
if (etag === "") return fail(incompleteResponse("ETag"));
|
|
1305
|
+
const uploadedAt = validDate(headers.get("last-modified"));
|
|
1306
|
+
if (uploadedAt === void 0) return fail(incompleteResponse("valid last-modified date"));
|
|
1307
|
+
const metadata = {};
|
|
1308
|
+
headers.forEach((value, name) => {
|
|
1309
|
+
if (name.startsWith("x-amz-meta-")) metadata[name.slice(11)] = value;
|
|
1310
|
+
});
|
|
1311
|
+
return ok({
|
|
1312
|
+
key,
|
|
1313
|
+
size,
|
|
1314
|
+
type: headers.get("content-type") ?? void 0,
|
|
1315
|
+
disposition: headers.get("content-disposition") ?? void 0,
|
|
1316
|
+
etag,
|
|
1317
|
+
uploadedAt,
|
|
1318
|
+
metadata,
|
|
1319
|
+
checksumCrc64nvme: headers.get("x-amz-checksum-crc64nvme") ?? void 0
|
|
1320
|
+
});
|
|
1321
|
+
}
|
|
1322
|
+
function toPayload(body) {
|
|
1323
|
+
if (typeof body === "string") return encoder.encode(body);
|
|
1324
|
+
return body instanceof ArrayBuffer ? new Uint8Array(body) : body;
|
|
1325
|
+
}
|
|
1326
|
+
function readListedObject(block) {
|
|
1327
|
+
const key = xmlText(block, "Key");
|
|
1328
|
+
if (!key) return fail(incompleteResponse("object key"));
|
|
1329
|
+
const size = wholeNumber(xmlValue(block, "Size"));
|
|
1330
|
+
if (size === void 0) return fail(incompleteResponse("valid object size"));
|
|
1331
|
+
const uploadedAt = validDate(xmlValue(block, "LastModified"));
|
|
1332
|
+
if (uploadedAt === void 0) return fail(incompleteResponse("valid last-modified date"));
|
|
1333
|
+
return ok({
|
|
1334
|
+
key,
|
|
1335
|
+
size,
|
|
1336
|
+
etag: etagIn(block),
|
|
1337
|
+
uploadedAt
|
|
1338
|
+
});
|
|
1339
|
+
}
|
|
1340
|
+
async function forEachConcurrently(items, limit, task) {
|
|
1341
|
+
const queue = items.values();
|
|
1342
|
+
const worker = async () => {
|
|
1343
|
+
for (const item of queue) await task(item);
|
|
1344
|
+
};
|
|
1345
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
|
|
1346
|
+
}
|
|
1347
|
+
function objectMethods(ctx) {
|
|
1348
|
+
const { rules } = ctx;
|
|
1349
|
+
const methods = {
|
|
1350
|
+
async head(key) {
|
|
1351
|
+
const problem = keyProblem(key);
|
|
1352
|
+
if (problem) return fail(invalidInput(problem));
|
|
1353
|
+
const result = await send(ctx, "HEAD", objectUrl(ctx, key), { headers: { "x-amz-checksum-mode": "ENABLED" } });
|
|
1354
|
+
if (result.error) return result.error.code === "NOT_FOUND" ? ok(null) : result;
|
|
1355
|
+
return readObjectInfo(key, result.data.headers);
|
|
1356
|
+
},
|
|
1357
|
+
async getRange(key, start, end) {
|
|
1358
|
+
const problem = keyProblem(key) ?? rangeProblem(start, end);
|
|
1359
|
+
if (problem) return fail(invalidInput(problem));
|
|
1360
|
+
const result = await send(ctx, "GET", objectUrl(ctx, key), { headers: { range: `bytes=${start}-${end}` } });
|
|
1361
|
+
if (result.error) return result;
|
|
1362
|
+
return ok(result.data.body);
|
|
1363
|
+
},
|
|
1364
|
+
async put(key, body, given) {
|
|
1365
|
+
const options = orEmpty(given);
|
|
1366
|
+
const problem = keyProblem(key) ?? bodyProblem(body) ?? objectOptionsProblem(options) ?? checksumProblem(options.checksumCrc32c);
|
|
1367
|
+
if (problem) return fail(invalidInput(problem));
|
|
1368
|
+
const payload = toPayload(body);
|
|
1369
|
+
const size = payload instanceof Blob ? payload.size : payload.length;
|
|
1370
|
+
const tooLarge = sizeProblem(size, rules.maxSinglePutSize);
|
|
1371
|
+
if (tooLarge) return fail(invalidInput(tooLarge));
|
|
1372
|
+
const result = await send(ctx, "PUT", objectUrl(ctx, key), {
|
|
1373
|
+
headers: writeHeaders(options),
|
|
1374
|
+
body: payload,
|
|
1375
|
+
timeoutMs: REQUEST_TIMEOUT_MS + Math.ceil(size / MiB) * 1e3
|
|
1376
|
+
});
|
|
1377
|
+
if (result.error) return result;
|
|
1378
|
+
const etag = stripQuotes(result.data.headers.get("etag") ?? "");
|
|
1379
|
+
return etag ? ok({ etag }) : fail(incompleteResponse("ETag"));
|
|
1380
|
+
},
|
|
1381
|
+
async delete(key) {
|
|
1382
|
+
const problem = keyProblem(key);
|
|
1383
|
+
if (problem) return fail(invalidInput(problem));
|
|
1384
|
+
const result = await send(ctx, "DELETE", objectUrl(ctx, key));
|
|
1385
|
+
if (result.error && result.error.code !== "NOT_FOUND") return result;
|
|
1386
|
+
return ok(void 0);
|
|
1387
|
+
},
|
|
1388
|
+
async deleteMany(keys) {
|
|
1389
|
+
const problem = keyListProblem(keys);
|
|
1390
|
+
if (problem) return fail(invalidInput(problem));
|
|
1391
|
+
const deleted = [];
|
|
1392
|
+
const failed = [];
|
|
1393
|
+
await forEachConcurrently(keys, DELETE_CONCURRENCY, async (key) => {
|
|
1394
|
+
const result = await methods.delete(key);
|
|
1395
|
+
if (result.error) failed.push({
|
|
1396
|
+
key,
|
|
1397
|
+
error: result.error
|
|
1398
|
+
});
|
|
1399
|
+
else deleted.push(key);
|
|
1400
|
+
});
|
|
1401
|
+
return ok({
|
|
1402
|
+
deleted,
|
|
1403
|
+
failed
|
|
1404
|
+
});
|
|
1405
|
+
},
|
|
1406
|
+
async list(prefix, given) {
|
|
1407
|
+
const { cursor, limit = MAX_LIST_PAGE } = orEmpty(given);
|
|
1408
|
+
const problem = prefixProblem(prefix) ?? limitProblem(limit) ?? (cursor === void 0 ? null : cursorProblem(cursor));
|
|
1409
|
+
if (problem) return fail(invalidInput(problem));
|
|
1410
|
+
const url = new URL(ctx.bucketUrl);
|
|
1411
|
+
url.searchParams.set("list-type", "2");
|
|
1412
|
+
url.searchParams.set("prefix", prefix);
|
|
1413
|
+
url.searchParams.set("max-keys", String(limit));
|
|
1414
|
+
if (cursor !== void 0) url.searchParams.set("continuation-token", cursor);
|
|
1415
|
+
const result = await sendForXml(ctx, "GET", url);
|
|
1416
|
+
if (result.error) return result;
|
|
1417
|
+
const listed = allOk(xmlBlocks(result.data, "Contents").map(readListedObject));
|
|
1418
|
+
if (listed.error) return listed;
|
|
1419
|
+
const items = listed.data;
|
|
1420
|
+
if (xmlValue(result.data, "IsTruncated") !== "true") return ok({
|
|
1421
|
+
items,
|
|
1422
|
+
nextCursor: null
|
|
1423
|
+
});
|
|
1424
|
+
const nextCursor = xmlText(result.data, "NextContinuationToken");
|
|
1425
|
+
return nextCursor ? ok({
|
|
1426
|
+
items,
|
|
1427
|
+
nextCursor
|
|
1428
|
+
}) : fail(incompleteResponse("continuation token"));
|
|
1429
|
+
}
|
|
1430
|
+
};
|
|
1431
|
+
return methods;
|
|
1432
|
+
}
|
|
1433
|
+
// src/storage/s3/presign.ts
|
|
1434
|
+
const SIGNABLE_UNTIL_MS = Date.UTC(1e4, 0, 1);
|
|
1435
|
+
function expiryOf(signedAt, expiresIn) {
|
|
1436
|
+
return new Date(Math.floor(signedAt.getTime() / 1e3) * 1e3 + expiresIn * 1e3);
|
|
1437
|
+
}
|
|
1438
|
+
async function presign(ctx, url, headers, size, expiresIn) {
|
|
1439
|
+
const { now, credentials, region } = ctx;
|
|
1440
|
+
const date = now();
|
|
1441
|
+
return {
|
|
1442
|
+
method: "PUT",
|
|
1443
|
+
url: (await presignUrl({
|
|
1444
|
+
method: "PUT",
|
|
1445
|
+
url,
|
|
1446
|
+
headers: {
|
|
1447
|
+
...headers,
|
|
1448
|
+
"content-length": String(size)
|
|
1449
|
+
},
|
|
1450
|
+
credentials,
|
|
1451
|
+
region,
|
|
1452
|
+
date,
|
|
1453
|
+
expiresIn
|
|
1454
|
+
})).toString(),
|
|
1455
|
+
headers,
|
|
1456
|
+
expiresAt: expiryOf(date, expiresIn)
|
|
1457
|
+
};
|
|
1458
|
+
}
|
|
1459
|
+
function presignMethods(ctx) {
|
|
1460
|
+
const { now, credentials, region, rules } = ctx;
|
|
1461
|
+
return {
|
|
1462
|
+
async presignPut(key, given) {
|
|
1463
|
+
const options = orEmpty(given);
|
|
1464
|
+
const problem = keyProblem(key) ?? objectOptionsProblem(options) ?? checksumProblem(options.checksumCrc32c) ?? sizeProblem(options.size, rules.maxSinglePutSize) ?? expiryProblem(options.expiresIn, rules.maxPresignSeconds);
|
|
1465
|
+
if (problem) return fail(invalidInput(problem));
|
|
1466
|
+
return ok(await presign(ctx, objectUrl(ctx, key), writeHeaders(options), options.size, options.expiresIn));
|
|
1467
|
+
},
|
|
1468
|
+
async presignPart(key, uploadId, partNumber, given) {
|
|
1469
|
+
const options = orEmpty(given);
|
|
1470
|
+
const problem = keyProblem(key) ?? uploadIdProblem(uploadId) ?? partNumberProblem(partNumber, rules.maxParts) ?? checksumProblem(options.checksumCrc32c) ?? sizeProblem(options.size, rules.maxPartSize, "part") ?? expiryProblem(options.expiresIn, rules.maxPresignSeconds);
|
|
1471
|
+
if (problem) return fail(invalidInput(problem));
|
|
1472
|
+
const headers = options.checksumCrc32c ? { "x-amz-checksum-crc32c": options.checksumCrc32c } : {};
|
|
1473
|
+
return ok(await presign(ctx, objectUrl(ctx, key, {
|
|
1474
|
+
partNumber: String(partNumber),
|
|
1475
|
+
uploadId
|
|
1476
|
+
}), headers, options.size, options.expiresIn));
|
|
1477
|
+
},
|
|
1478
|
+
async presignGet(key, given) {
|
|
1479
|
+
const options = orEmpty(given);
|
|
1480
|
+
const signedAt = options.signedAt ?? now();
|
|
1481
|
+
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.");
|
|
1482
|
+
if (problem) return fail(invalidInput(problem));
|
|
1483
|
+
return ok({
|
|
1484
|
+
url: (await presignUrl({
|
|
1485
|
+
method: "GET",
|
|
1486
|
+
url: objectUrl(ctx, key, options.disposition ? { "response-content-disposition": options.disposition } : {}),
|
|
1487
|
+
credentials,
|
|
1488
|
+
region,
|
|
1489
|
+
date: signedAt,
|
|
1490
|
+
expiresIn: options.expiresIn
|
|
1491
|
+
})).toString(),
|
|
1492
|
+
expiresAt: expiryOf(signedAt, options.expiresIn)
|
|
1493
|
+
});
|
|
1494
|
+
}
|
|
1495
|
+
};
|
|
1496
|
+
}
|
|
1497
|
+
// src/storage/s3-driver.ts
|
|
1498
|
+
function createS3Driver(config) {
|
|
1499
|
+
const ctx = createContext(config);
|
|
1500
|
+
const publicUrl = config.publicUrl === void 0 ? void 0 : checkBaseUrl(config.publicUrl, "publicUrl", "https://cdn.example.com/files").href.replace(/\/+$/, "");
|
|
1501
|
+
const objects = objectMethods(ctx);
|
|
1502
|
+
const multipart = multipartMethods(ctx);
|
|
1503
|
+
return {
|
|
1504
|
+
rules: ctx.rules,
|
|
1505
|
+
publicUrl,
|
|
1506
|
+
...presignMethods(ctx),
|
|
1507
|
+
...bucketSettingsMethods(ctx),
|
|
1508
|
+
...multipart,
|
|
1509
|
+
...objects,
|
|
1510
|
+
...copyMethods(ctx, {
|
|
1511
|
+
head: objects.head,
|
|
1512
|
+
...multipart
|
|
1513
|
+
})
|
|
1514
|
+
};
|
|
1515
|
+
}
|
|
1516
|
+
function createS3DriverWithWrites(config) {
|
|
1517
|
+
return {
|
|
1518
|
+
...createS3Driver(config),
|
|
1519
|
+
...bucketWriteMethods(createContext(config))
|
|
1520
|
+
};
|
|
1521
|
+
}
|
|
1522
|
+
// src/storage/presets.ts
|
|
1523
|
+
const R2_VARIABLES = {
|
|
1524
|
+
bucket: "CLOUDROP_R2_BUCKET",
|
|
1525
|
+
accountId: "CLOUDROP_R2_ACCOUNT_ID",
|
|
1526
|
+
accessKeyId: "CLOUDROP_R2_ACCESS_KEY_ID",
|
|
1527
|
+
secretAccessKey: "CLOUDROP_R2_SECRET_ACCESS_KEY"
|
|
1528
|
+
};
|
|
1529
|
+
const S3_VARIABLES = {
|
|
1530
|
+
endpoint: "CLOUDROP_S3_ENDPOINT",
|
|
1531
|
+
region: "CLOUDROP_S3_REGION",
|
|
1532
|
+
bucket: "CLOUDROP_S3_BUCKET",
|
|
1533
|
+
accessKeyId: "CLOUDROP_S3_ACCESS_KEY_ID",
|
|
1534
|
+
secretAccessKey: "CLOUDROP_S3_SECRET_ACCESS_KEY"
|
|
1535
|
+
};
|
|
1536
|
+
const STAND_INS = {
|
|
1537
|
+
bucket: "bucket",
|
|
1538
|
+
accountId: "0".repeat(32),
|
|
1539
|
+
accessKeyId: "key",
|
|
1540
|
+
secretAccessKey: "secret",
|
|
1541
|
+
endpoint: "https://s3.example.com",
|
|
1542
|
+
region: "auto"
|
|
1543
|
+
};
|
|
1544
|
+
function readSettings(service, options, variables, read) {
|
|
1545
|
+
const settings = {};
|
|
1546
|
+
const missing = [];
|
|
1547
|
+
for (const setting of Object.keys(variables)) {
|
|
1548
|
+
const value = options[setting] || read(variables[setting], setting);
|
|
1549
|
+
if (value) settings[setting] = value;
|
|
1550
|
+
else missing.push(`${setting} (${variables[setting]})`);
|
|
1551
|
+
}
|
|
1552
|
+
if (missing.length > 0) throw configError(`Missing ${service} settings: ${missing.join(", ")}. Pass them to ${service.toLowerCase()}() or set the environment variables in parentheses.`);
|
|
1553
|
+
return settings;
|
|
1554
|
+
}
|
|
1555
|
+
function buildFromEnvironment(build, env = fromEnv) {
|
|
1556
|
+
const read = [];
|
|
1557
|
+
try {
|
|
1558
|
+
return build((variable) => {
|
|
1559
|
+
const value = env(variable);
|
|
1560
|
+
if (value) read.push(variable);
|
|
1561
|
+
return value;
|
|
1562
|
+
});
|
|
1563
|
+
} catch (error) {
|
|
1564
|
+
if (!(error instanceof CloudropError)) throw error;
|
|
1565
|
+
const invalid = read.filter((variable) => readSetting(() => build((name, setting) => name === variable ? env(name) : STAND_INS[setting])).error);
|
|
1566
|
+
if (invalid.length === 0) throw error;
|
|
1567
|
+
throw configError(`${error.message} Environment variables with an invalid value: ${invalid.join(", ")}.`);
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
const R2_BUCKET_NAME = /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/;
|
|
1571
|
+
const S3_BUCKET_NAME = /^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$/;
|
|
1572
|
+
const IPV4_ADDRESS = /^\d{1,3}(\.\d{1,3}){3}$/;
|
|
1573
|
+
function checkBucket(bucket, service) {
|
|
1574
|
+
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.`);
|
|
1575
|
+
return bucket;
|
|
1576
|
+
}
|
|
1577
|
+
function checkAccountId(accountId) {
|
|
1578
|
+
const account = typeof accountId === "string" ? accountId.toLowerCase() : void 0;
|
|
1579
|
+
if (account === void 0 || !/^[0-9a-f]{32}$/.test(account)) throw configError(`The R2 account ID must be 32 hexadecimal characters (0-9, a-f); this one ${account === void 0 ? "is not text" : `has ${account.length} characters`}. Copy it from the R2 overview page in the Cloudflare dashboard.`);
|
|
1580
|
+
return account;
|
|
1581
|
+
}
|
|
1582
|
+
function buildR2(options, create) {
|
|
1583
|
+
const { jurisdiction } = options;
|
|
1584
|
+
if (jurisdiction !== void 0 && jurisdiction !== "eu" && jurisdiction !== "fedramp") throw configError("The R2 jurisdiction must be \"eu\" or \"fedramp\". Omit it for the default location.");
|
|
1585
|
+
return (read) => {
|
|
1586
|
+
const settings = readSettings("R2", options, R2_VARIABLES, read);
|
|
1587
|
+
const bucket = checkBucket(settings.bucket, "R2");
|
|
1588
|
+
const account = checkAccountId(settings.accountId);
|
|
1589
|
+
return create({
|
|
1590
|
+
endpoint: `https://${jurisdiction ? `${account}.${jurisdiction}` : account}.r2.cloudflarestorage.com`,
|
|
1591
|
+
region: "auto",
|
|
1592
|
+
bucket,
|
|
1593
|
+
credentials: {
|
|
1594
|
+
accessKeyId: settings.accessKeyId,
|
|
1595
|
+
secretAccessKey: settings.secretAccessKey
|
|
1596
|
+
},
|
|
1597
|
+
rules: R2_RULES,
|
|
1598
|
+
publicUrl: options.publicUrl,
|
|
1599
|
+
fetch: options.fetch
|
|
1600
|
+
});
|
|
1601
|
+
};
|
|
1602
|
+
}
|
|
1603
|
+
function buildS3(options, create) {
|
|
1604
|
+
return (read) => {
|
|
1605
|
+
const { endpoint, region, bucket, accessKeyId, secretAccessKey } = readSettings("S3", options, S3_VARIABLES, read);
|
|
1606
|
+
return create({
|
|
1607
|
+
endpoint,
|
|
1608
|
+
region,
|
|
1609
|
+
bucket: checkBucket(bucket, "S3"),
|
|
1610
|
+
credentials: {
|
|
1611
|
+
accessKeyId,
|
|
1612
|
+
secretAccessKey
|
|
1613
|
+
},
|
|
1614
|
+
rules: S3_RULES,
|
|
1615
|
+
publicUrl: options.publicUrl,
|
|
1616
|
+
fetch: options.fetch
|
|
1617
|
+
});
|
|
1618
|
+
};
|
|
1619
|
+
}
|
|
1620
|
+
function storageFromVariables(service, variables, options) {
|
|
1621
|
+
return readSetting(() => {
|
|
1622
|
+
return buildFromEnvironment(service === "R2" ? buildR2(options, createS3DriverWithWrites) : buildS3({ fetch: options.fetch }, createS3DriverWithWrites), variables);
|
|
1623
|
+
});
|
|
1624
|
+
}
|
|
1625
|
+
// src/cli/env-file.ts
|
|
1626
|
+
const LINE = /^\s*(?:export\s+)?([^\s=#][^\s=]*)\s*=\s*(.*?)\s*$/;
|
|
1627
|
+
const QUOTES = new Set([
|
|
1628
|
+
"\"",
|
|
1629
|
+
"'",
|
|
1630
|
+
"`"
|
|
1631
|
+
]);
|
|
1632
|
+
function quotedValue(lines, start, raw) {
|
|
1633
|
+
const quote = raw[0] ?? "";
|
|
1634
|
+
let body = raw.slice(1);
|
|
1635
|
+
let last = start;
|
|
1636
|
+
let end = body.indexOf(quote);
|
|
1637
|
+
while (end === -1 && last + 1 < lines.length) {
|
|
1638
|
+
last++;
|
|
1639
|
+
body += `\n${lines[last]}`;
|
|
1640
|
+
end = body.indexOf(quote);
|
|
1641
|
+
}
|
|
1642
|
+
if (end === -1) return {
|
|
1643
|
+
value: raw,
|
|
1644
|
+
last: start
|
|
1645
|
+
};
|
|
1646
|
+
const inner = body.slice(0, end);
|
|
1647
|
+
return {
|
|
1648
|
+
value: quote === "\"" ? inner.replaceAll("\\n", "\n") : inner,
|
|
1649
|
+
last
|
|
1650
|
+
};
|
|
1651
|
+
}
|
|
1652
|
+
function parseEnvFile(text) {
|
|
1653
|
+
const values = {};
|
|
1654
|
+
const lines = text.split(/\r?\n/);
|
|
1655
|
+
for (let index = 0; index < lines.length; index++) {
|
|
1656
|
+
const [, name, raw = ""] = LINE.exec(lines[index] ?? "") ?? [];
|
|
1657
|
+
if (name === void 0) continue;
|
|
1658
|
+
if (!QUOTES.has(raw[0] ?? "")) {
|
|
1659
|
+
values[name] = (raw.split("#")[0] ?? "").trimEnd();
|
|
1660
|
+
continue;
|
|
1661
|
+
}
|
|
1662
|
+
const { value, last } = quotedValue(lines, index, raw);
|
|
1663
|
+
values[name] = value;
|
|
1664
|
+
index = last;
|
|
1665
|
+
}
|
|
1666
|
+
return values;
|
|
1667
|
+
}
|
|
1668
|
+
// src/cli/settings.ts
|
|
1669
|
+
const NAMES = {
|
|
1670
|
+
R2: Object.values(R2_VARIABLES),
|
|
1671
|
+
S3: Object.values(S3_VARIABLES)
|
|
1672
|
+
};
|
|
1673
|
+
const SECRET = "CLOUDROP_SECRET";
|
|
1674
|
+
const KNOWN = [
|
|
1675
|
+
...NAMES.R2,
|
|
1676
|
+
...NAMES.S3,
|
|
1677
|
+
SECRET
|
|
1678
|
+
];
|
|
1679
|
+
const MIN_SECRET_LENGTH = 32;
|
|
1680
|
+
const WHAT_TO_SET = {
|
|
1681
|
+
CLOUDROP_R2_ACCOUNT_ID: "the account ID on the R2 overview page of the Cloudflare dashboard",
|
|
1682
|
+
CLOUDROP_R2_BUCKET: "the bucket's name",
|
|
1683
|
+
CLOUDROP_R2_ACCESS_KEY_ID: "the access key ID of an R2 API token",
|
|
1684
|
+
CLOUDROP_R2_SECRET_ACCESS_KEY: "the secret access key of an R2 API token",
|
|
1685
|
+
CLOUDROP_S3_ENDPOINT: "the service's S3 endpoint, such as https://s3.us-east-1.amazonaws.com",
|
|
1686
|
+
CLOUDROP_S3_REGION: "the bucket's region, such as us-east-1",
|
|
1687
|
+
CLOUDROP_S3_BUCKET: "the bucket's name",
|
|
1688
|
+
CLOUDROP_S3_ACCESS_KEY_ID: "the access key ID of the service's credentials",
|
|
1689
|
+
CLOUDROP_S3_SECRET_ACCESS_KEY: "the secret access key of the service's credentials"
|
|
1690
|
+
};
|
|
1691
|
+
const READ_ADVICE = new Map([["EISDIR", "Name a file, not a directory."], ["EACCES", "Check that you may read it."]]);
|
|
1692
|
+
function readEnvFile(path, readFile) {
|
|
1693
|
+
let text;
|
|
1694
|
+
try {
|
|
1695
|
+
text = readFile(path);
|
|
1696
|
+
} catch (error) {
|
|
1697
|
+
const code = error?.code;
|
|
1698
|
+
const known = typeof code === "string" ? code : "unknown error";
|
|
1699
|
+
return { error: `Cannot read ${path} (${known}). ${READ_ADVICE.get(known) ?? "Check the path."}` };
|
|
1700
|
+
}
|
|
1701
|
+
return { values: parseEnvFile(text) };
|
|
1702
|
+
}
|
|
1703
|
+
function readVariables(env, envFile, readFile) {
|
|
1704
|
+
const read = envFile === void 0 ? { values: {} } : readEnvFile(envFile, readFile);
|
|
1705
|
+
if ("error" in read) return read;
|
|
1706
|
+
const file = read.values;
|
|
1707
|
+
const variables = (name) => env[name] || file[name] || void 0;
|
|
1708
|
+
const notes = KNOWN.filter((name) => env[name] && file[name]).map((name) => ({
|
|
1709
|
+
status: "note",
|
|
1710
|
+
text: `${name} is set in the environment and in ${envFile}; the environment's value is used.`
|
|
1711
|
+
}));
|
|
1712
|
+
notes.push(...misspelt([...Object.keys(env), ...Object.keys(file)], variables));
|
|
1713
|
+
if (serviceOf(variables) === "R2" && NAMES.S3.some((name) => variables(name))) notes.push({
|
|
1714
|
+
status: "note",
|
|
1715
|
+
text: "CLOUDROP_R2_* and CLOUDROP_S3_* settings are both set; the command checks R2. Unset the CLOUDROP_R2_* settings to check S3."
|
|
1716
|
+
});
|
|
1717
|
+
return {
|
|
1718
|
+
variables,
|
|
1719
|
+
notes
|
|
1720
|
+
};
|
|
1721
|
+
}
|
|
1722
|
+
function distance(a, b) {
|
|
1723
|
+
let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
|
|
1724
|
+
for (let i = 1; i <= a.length; i++) {
|
|
1725
|
+
const current = [i];
|
|
1726
|
+
for (let j = 1; j <= b.length; j++) {
|
|
1727
|
+
const replace = (previous[j - 1] ?? 0) + (a[i - 1] === b[j - 1] ? 0 : 1);
|
|
1728
|
+
current[j] = Math.min((previous[j] ?? 0) + 1, (current[j - 1] ?? 0) + 1, replace);
|
|
1729
|
+
}
|
|
1730
|
+
previous = current;
|
|
1731
|
+
}
|
|
1732
|
+
return previous[b.length] ?? 0;
|
|
1733
|
+
}
|
|
1734
|
+
const NOT_FOR_R2 = new Map([["CLOUDROP_R2_ENDPOINT", "R2's endpoint is built from CLOUDROP_R2_ACCOUNT_ID."], ["CLOUDROP_R2_REGION", "R2 needs no region."]]);
|
|
1735
|
+
function meant(upper, variables) {
|
|
1736
|
+
const service = upper.startsWith("CLOUDROP_R2_") ? "R2" : upper.startsWith("CLOUDROP_S3_") ? "S3" : void 0;
|
|
1737
|
+
const best = (service ? NAMES[service] : KNOWN).map((known) => ({
|
|
1738
|
+
known,
|
|
1739
|
+
score: distance(upper, known)
|
|
1740
|
+
})).sort((a, b) => a.score - b.score)[0];
|
|
1741
|
+
return best && best.score <= 2 && !variables(best.known) ? `Did you mean ${best.known}?` : void 0;
|
|
1742
|
+
}
|
|
1743
|
+
function misspelt(names, variables) {
|
|
1744
|
+
return [...new Set(names)].flatMap((name) => {
|
|
1745
|
+
if (!/^cloudrop_/i.test(name) || KNOWN.includes(name)) return [];
|
|
1746
|
+
const upper = name.toUpperCase();
|
|
1747
|
+
const hint = NOT_FOR_R2.get(upper) ?? meant(upper, variables);
|
|
1748
|
+
return hint ? [{
|
|
1749
|
+
status: "note",
|
|
1750
|
+
text: `${name} is not a cloudrop setting. ${hint}`
|
|
1751
|
+
}] : [];
|
|
1752
|
+
});
|
|
1753
|
+
}
|
|
1754
|
+
function serviceOf(variables) {
|
|
1755
|
+
if (NAMES.R2.some((name) => variables(name))) return "R2";
|
|
1756
|
+
return NAMES.S3.some((name) => variables(name)) ? "S3" : void 0;
|
|
1757
|
+
}
|
|
1758
|
+
function readStorage(variables, options) {
|
|
1759
|
+
const service = serviceOf(variables);
|
|
1760
|
+
if (service === void 0) return {
|
|
1761
|
+
service,
|
|
1762
|
+
driver: void 0,
|
|
1763
|
+
findings: [{
|
|
1764
|
+
status: "problem",
|
|
1765
|
+
text: "No storage settings found. Set CLOUDROP_R2_ACCOUNT_ID, CLOUDROP_R2_BUCKET, CLOUDROP_R2_ACCESS_KEY_ID and CLOUDROP_R2_SECRET_ACCESS_KEY for R2, or the CLOUDROP_S3_* settings for another service; --env-file reads them from a file."
|
|
1766
|
+
}]
|
|
1767
|
+
};
|
|
1768
|
+
const missing = NAMES[service].filter((name) => !variables(name));
|
|
1769
|
+
if (missing.length > 0) return {
|
|
1770
|
+
service,
|
|
1771
|
+
driver: void 0,
|
|
1772
|
+
findings: missing.map((name) => ({
|
|
1773
|
+
status: "problem",
|
|
1774
|
+
text: `${name} is not set. Set it to ${WHAT_TO_SET[name]}.`
|
|
1775
|
+
}))
|
|
1776
|
+
};
|
|
1777
|
+
const built = storageFromVariables(service, variables, options);
|
|
1778
|
+
if (built.error) return {
|
|
1779
|
+
service,
|
|
1780
|
+
driver: void 0,
|
|
1781
|
+
findings: [{
|
|
1782
|
+
status: "problem",
|
|
1783
|
+
text: built.error.message
|
|
1784
|
+
}]
|
|
1785
|
+
};
|
|
1786
|
+
const findings = [{
|
|
1787
|
+
status: "ok",
|
|
1788
|
+
text: `${service} settings are present and valid (CLOUDROP_${service}_*).`
|
|
1789
|
+
}];
|
|
1790
|
+
return {
|
|
1791
|
+
service,
|
|
1792
|
+
driver: built.data,
|
|
1793
|
+
findings
|
|
1794
|
+
};
|
|
1795
|
+
}
|
|
1796
|
+
function secretFinding(variables) {
|
|
1797
|
+
const secret = variables(SECRET);
|
|
1798
|
+
if (!secret) return {
|
|
1799
|
+
status: "problem",
|
|
1800
|
+
text: `${SECRET} is not set. Generate one with \`openssl rand -base64 32\` and set it, unless your code passes secret to createCloudrop().`
|
|
1801
|
+
};
|
|
1802
|
+
if (secret.length < MIN_SECRET_LENGTH) return {
|
|
1803
|
+
status: "problem",
|
|
1804
|
+
text: `${SECRET} has fewer than ${MIN_SECRET_LENGTH} characters. Generate one with \`openssl rand -base64 32\`.`
|
|
1805
|
+
};
|
|
1806
|
+
return {
|
|
1807
|
+
status: "ok",
|
|
1808
|
+
text: `${SECRET} is set.`
|
|
1809
|
+
};
|
|
1810
|
+
}
|
|
1811
|
+
// src/cli/doctor.ts
|
|
1812
|
+
function byHand(service, setting, jurisdiction) {
|
|
1813
|
+
if (service === "R2") return ` By hand: ${wrangler(`${setting} list <bucket>`, jurisdiction)}`;
|
|
1814
|
+
return ` By hand: ${aws(setting === "cors" ? "get-bucket-cors" : "get-bucket-lifecycle-configuration")}`;
|
|
1815
|
+
}
|
|
1816
|
+
function ruleFinding(label, check, hint) {
|
|
1817
|
+
if (check.status === "ok") return {
|
|
1818
|
+
status: "ok",
|
|
1819
|
+
text: `${label}: ${check.message}`
|
|
1820
|
+
};
|
|
1821
|
+
if (check.status === "unknown") return {
|
|
1822
|
+
status: "unknown",
|
|
1823
|
+
text: `${label}: ${check.message}${hint.byHand}`
|
|
1824
|
+
};
|
|
1825
|
+
return {
|
|
1826
|
+
status: "problem",
|
|
1827
|
+
text: `${label}: ${check.message}${hint.fix}`
|
|
1828
|
+
};
|
|
1829
|
+
}
|
|
1830
|
+
async function bucketFindings(driver, service, { origins, jurisdiction }) {
|
|
1831
|
+
const answered = await reachBucket(driver, service, jurisdiction);
|
|
1832
|
+
if (answered.status === "problem") return [answered, {
|
|
1833
|
+
status: "unknown",
|
|
1834
|
+
text: "CORS and lifecycle rules: the bucket did not answer."
|
|
1835
|
+
}];
|
|
1836
|
+
const report = await checkBucket$1({
|
|
1837
|
+
storage: driver,
|
|
1838
|
+
routes: new Map(),
|
|
1839
|
+
store: void 0
|
|
1840
|
+
}, origins);
|
|
1841
|
+
if (report.error) return [answered, {
|
|
1842
|
+
status: "problem",
|
|
1843
|
+
text: `Bucket rules: ${report.error.message}`
|
|
1844
|
+
}];
|
|
1845
|
+
const apply = ` Run: cloudrop setup ${origins.map((origin) => `--origin ${origin}`).join(" ")} --apply`;
|
|
1846
|
+
return [
|
|
1847
|
+
answered,
|
|
1848
|
+
ruleFinding("CORS", report.data.cors, {
|
|
1849
|
+
fix: apply,
|
|
1850
|
+
byHand: byHand(service, "cors", jurisdiction)
|
|
1851
|
+
}),
|
|
1852
|
+
ruleFinding("Lifecycle", report.data.lifecycle, {
|
|
1853
|
+
fix: apply,
|
|
1854
|
+
byHand: byHand(service, "lifecycle", jurisdiction)
|
|
1855
|
+
})
|
|
1856
|
+
];
|
|
1857
|
+
}
|
|
1858
|
+
async function doctor(options, variables, notes, { fetch, out }) {
|
|
1859
|
+
const service = serviceOf(variables) ?? "storage";
|
|
1860
|
+
out.write(`cloudrop doctor: checking ${service} settings, the bucket and its rules for ${options.origins.join(", ")}.\n`);
|
|
1861
|
+
const storage = readStorage(variables, {
|
|
1862
|
+
jurisdiction: options.jurisdiction,
|
|
1863
|
+
fetch
|
|
1864
|
+
});
|
|
1865
|
+
const findings = [
|
|
1866
|
+
...notes,
|
|
1867
|
+
...storage.findings,
|
|
1868
|
+
secretFinding(variables)
|
|
1869
|
+
];
|
|
1870
|
+
if (storage.driver && storage.service) findings.push(...await bucketFindings(storage.driver, storage.service, options));
|
|
1871
|
+
else findings.push({
|
|
1872
|
+
status: "unknown",
|
|
1873
|
+
text: "The bucket and its rules: fix the settings above first."
|
|
1874
|
+
});
|
|
1875
|
+
printFindings(out, findings);
|
|
1876
|
+
const problems = count(findings, "problem");
|
|
1877
|
+
const unknown = count(findings, "unknown");
|
|
1878
|
+
if (problems > 0) {
|
|
1879
|
+
out.write(`${plural(problems, "problem")} found. Fix ${problems === 1 ? "it" : "them"}, then run cloudrop doctor again.\n`);
|
|
1880
|
+
return 1;
|
|
1881
|
+
}
|
|
1882
|
+
out.write(unknown > 0 ? `No problems found. ${plural(unknown, "check")} could not run; see above.\n` : "No problems found.\n");
|
|
1883
|
+
return 0;
|
|
1884
|
+
}
|
|
1885
|
+
// src/cli/setup.ts
|
|
1886
|
+
const S3_PERMISSIONS = {
|
|
1887
|
+
CORS: "s3:GetBucketCORS and s3:PutBucketCORS",
|
|
1888
|
+
Lifecycle: "s3:GetLifecycleConfiguration and s3:PutLifecycleConfiguration"
|
|
1889
|
+
};
|
|
1890
|
+
function unreadable(label, error, replace, service) {
|
|
1891
|
+
if (error.code === "STORAGE_DENIED") {
|
|
1892
|
+
const [rules, admin] = service === "R2" ? ["rules (R2 object tokens cannot)", ", such as an R2 Admin Read & Write token,"] : ["rules", ` (${S3_PERMISSIONS[label]}),`];
|
|
1893
|
+
return `${label}: these credentials may not read the bucket's ${label === "CORS" ? "CORS" : "lifecycle"} ${rules}, and a write replaces every rule, so none was written. Use credentials that may read and change bucket settings for this step${admin} or pass --replace to replace every rule with cloudrop's alone.`;
|
|
1894
|
+
}
|
|
1895
|
+
const failed = `${label}: could not read the current rules: ${error.message} A write replaces every rule, so none was written.`;
|
|
1896
|
+
return replace ? `${failed} --replace applies only when the credentials refuse the read.` : failed;
|
|
1897
|
+
}
|
|
1898
|
+
function plan(setting, replace, service) {
|
|
1899
|
+
const { label, ours, describe, read } = setting;
|
|
1900
|
+
if (read.error && !(replace && read.error.code === "STORAGE_DENIED")) return {
|
|
1901
|
+
status: "problem",
|
|
1902
|
+
text: unreadable(label, read.error, replace, service)
|
|
1903
|
+
};
|
|
1904
|
+
const merged = read.error ? {
|
|
1905
|
+
rules: [ours],
|
|
1906
|
+
change: "add",
|
|
1907
|
+
kept: []
|
|
1908
|
+
} : setting.merge(read.data, ours);
|
|
1909
|
+
const { change, kept, reason, added } = merged;
|
|
1910
|
+
return {
|
|
1911
|
+
label,
|
|
1912
|
+
ruleId: ours.id,
|
|
1913
|
+
describe,
|
|
1914
|
+
change,
|
|
1915
|
+
kept,
|
|
1916
|
+
replacing: read.error !== null,
|
|
1917
|
+
reason,
|
|
1918
|
+
added,
|
|
1919
|
+
write: () => setting.write(merged.rules)
|
|
1920
|
+
};
|
|
1921
|
+
}
|
|
1922
|
+
function describeChange(step, dryRun) {
|
|
1923
|
+
const { label, ruleId, describe, kept } = step;
|
|
1924
|
+
if (step.replacing) return `${label}: ${dryRun ? "would replace" : "replaced"} every rule with "${ruleId}" ${describe} (--replace: the current rules could not be read).`;
|
|
1925
|
+
const keeps = kept.length > 0 ? ` and ${dryRun ? "keep" : "kept"} ${plural(kept.length, "other rule")} (${ruleNames(kept)})` : "";
|
|
1926
|
+
if (step.change === "update") return `${label}: ${dryRun ? "would add" : "added"} ${step.added} to rule "${ruleId}"${keeps}.`;
|
|
1927
|
+
return `${label}: ${dryRun ? "would add" : "added"} rule "${ruleId}" ${describe}${keeps}.`;
|
|
1928
|
+
}
|
|
1929
|
+
async function carryOut(step, dryRun) {
|
|
1930
|
+
if (step.change === "blocked") return {
|
|
1931
|
+
status: "problem",
|
|
1932
|
+
text: `${step.label}: ${step.reason}`
|
|
1933
|
+
};
|
|
1934
|
+
if (step.change === "none") return {
|
|
1935
|
+
status: "ok",
|
|
1936
|
+
text: `${step.label}: ${step.reason}; nothing to change.`
|
|
1937
|
+
};
|
|
1938
|
+
if (dryRun) return {
|
|
1939
|
+
status: "dry run",
|
|
1940
|
+
text: describeChange(step, true)
|
|
1941
|
+
};
|
|
1942
|
+
const written = await step.write();
|
|
1943
|
+
if (written.error) return {
|
|
1944
|
+
status: "problem",
|
|
1945
|
+
text: `${step.label}: the write failed. ${written.error.message}`
|
|
1946
|
+
};
|
|
1947
|
+
return {
|
|
1948
|
+
status: "ok",
|
|
1949
|
+
text: describeChange(step, false)
|
|
1950
|
+
};
|
|
1951
|
+
}
|
|
1952
|
+
async function planBoth(driver, options, service) {
|
|
1953
|
+
const [cors, lifecycle] = await Promise.all([driver.getBucketCors(), driver.getBucketLifecycle()]);
|
|
1954
|
+
const origins = options.origins.join(", ");
|
|
1955
|
+
const days = plural(1, "day");
|
|
1956
|
+
return [plan({
|
|
1957
|
+
label: "CORS",
|
|
1958
|
+
ours: corsRule(options.origins),
|
|
1959
|
+
describe: `for ${origins}`,
|
|
1960
|
+
read: cors,
|
|
1961
|
+
merge: mergeCors,
|
|
1962
|
+
write: driver.putBucketCors
|
|
1963
|
+
}, options.replace, service), plan({
|
|
1964
|
+
label: "Lifecycle",
|
|
1965
|
+
ours: LIFECYCLE_RULE,
|
|
1966
|
+
describe: `(aborts incomplete multipart uploads after ${days}, whole bucket)`,
|
|
1967
|
+
read: lifecycle,
|
|
1968
|
+
merge: mergeLifecycle,
|
|
1969
|
+
write: driver.putBucketLifecycle
|
|
1970
|
+
}, options.replace, service)];
|
|
1971
|
+
}
|
|
1972
|
+
const isFinding = (step) => "status" in step;
|
|
1973
|
+
const isStep = (step) => !isFinding(step);
|
|
1974
|
+
async function applySetup(options, variables, notes, { fetch, out }) {
|
|
1975
|
+
const mode = options.dryRun ? "showing what --apply would change" : "adding cloudrop's rules";
|
|
1976
|
+
out.write(`cloudrop setup: ${mode} for ${options.origins.join(", ")}.\n`);
|
|
1977
|
+
const storage = readStorage(variables, {
|
|
1978
|
+
jurisdiction: options.jurisdiction,
|
|
1979
|
+
fetch
|
|
1980
|
+
});
|
|
1981
|
+
if (!storage.driver || !storage.service) {
|
|
1982
|
+
printFindings(out, [...notes, ...storage.findings]);
|
|
1983
|
+
out.write("Nothing was written. Fix the settings above, then run the command again.\n");
|
|
1984
|
+
return 1;
|
|
1985
|
+
}
|
|
1986
|
+
const [steps, access] = await Promise.all([planBoth(storage.driver, options, storage.service), reachBucket(storage.driver, storage.service, options.jurisdiction)]);
|
|
1987
|
+
const refused = steps.filter(isFinding);
|
|
1988
|
+
if (refused.length > 0) {
|
|
1989
|
+
printFindings(out, [...notes, ...access.status === "problem" ? [access] : refused]);
|
|
1990
|
+
out.write("Nothing was written.\n");
|
|
1991
|
+
return 1;
|
|
1992
|
+
}
|
|
1993
|
+
const findings = [...notes];
|
|
1994
|
+
for (const step of steps.filter(isStep)) findings.push(await carryOut(step, options.dryRun));
|
|
1995
|
+
printFindings(out, findings);
|
|
1996
|
+
if (count(findings, "problem") > 0) {
|
|
1997
|
+
out.write("Some rules were not written; see above.\n");
|
|
1998
|
+
return 1;
|
|
1999
|
+
}
|
|
2000
|
+
const doctorCommand = `cloudrop doctor ${options.origins.map((origin) => `--origin ${origin}`).join(" ")}`;
|
|
2001
|
+
out.write(options.dryRun ? "Dry run: nothing was written. To write, run it with --apply and without --dry-run.\n" : `Done. Check the bucket with ${doctorCommand}; R2 can take up to 30 seconds to apply a change.\n`);
|
|
2002
|
+
return 0;
|
|
2003
|
+
}
|
|
2004
|
+
// src/cli/run.ts
|
|
2005
|
+
const USAGE_ERROR = 2;
|
|
2006
|
+
async function runCommand(io) {
|
|
2007
|
+
const parsed = parseArgs(io.argv);
|
|
2008
|
+
if (parsed.help) {
|
|
2009
|
+
io.stdout.write(USAGE);
|
|
2010
|
+
return 0;
|
|
2011
|
+
}
|
|
2012
|
+
if ("error" in parsed) {
|
|
2013
|
+
io.stderr.write(`${parsed.error}\n\n${USAGE}`);
|
|
2014
|
+
return USAGE_ERROR;
|
|
2015
|
+
}
|
|
2016
|
+
const { options } = parsed;
|
|
2017
|
+
const read = readVariables(io.env, options.envFile, io.readFile);
|
|
2018
|
+
if ("error" in read) {
|
|
2019
|
+
io.stderr.write(`${read.error}\n`);
|
|
2020
|
+
return USAGE_ERROR;
|
|
2021
|
+
}
|
|
2022
|
+
const { variables, notes } = read;
|
|
2023
|
+
const context = {
|
|
2024
|
+
fetch: io.fetch,
|
|
2025
|
+
out: io.stdout
|
|
2026
|
+
};
|
|
2027
|
+
if (options.command === "doctor") return doctor(options, variables, notes, context);
|
|
2028
|
+
if (options.apply || options.dryRun) return applySetup(options, variables, notes, context);
|
|
2029
|
+
printInstructions(io.stdout, {
|
|
2030
|
+
origins: options.origins,
|
|
2031
|
+
service: serviceOf(variables),
|
|
2032
|
+
jurisdiction: options.jurisdiction
|
|
2033
|
+
});
|
|
2034
|
+
return 0;
|
|
2035
|
+
}
|
|
2036
|
+
// src/cli.ts
|
|
2037
|
+
const nodeProcess = globalThis.process;
|
|
2038
|
+
nodeProcess.exitCode = await runCommand({
|
|
2039
|
+
argv: nodeProcess.argv.slice(2),
|
|
2040
|
+
env: nodeProcess.env,
|
|
2041
|
+
readFile: (path) => nodeProcess.getBuiltinModule("node:fs").readFileSync(path, "utf8"),
|
|
2042
|
+
fetch: (input, init) => globalThis.fetch(input, init),
|
|
2043
|
+
stdout: nodeProcess.stdout,
|
|
2044
|
+
stderr: nodeProcess.stderr
|
|
2045
|
+
});
|