@needmoretruth/nmts-cli 0.45.0 → 0.47.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/AGENTS.md +8 -8
- package/CHANGELOG.md +78 -0
- package/README.ko.md +6 -3
- package/README.md +31 -11
- package/dist/api-advice.js +17 -2
- package/dist/api.d.ts +22 -0
- package/dist/api.js +49 -1
- package/dist/arg-options.d.ts +19 -1
- package/dist/args.js +14 -2
- package/dist/artifact-about.d.ts +1 -1
- package/dist/autonomy.js +3 -3
- package/dist/collision.d.ts +20 -3
- package/dist/collision.js +21 -3
- package/dist/commands/erase.js +1 -0
- package/dist/commands/handover.d.ts +15 -0
- package/dist/commands/handover.js +376 -0
- package/dist/commands/heavy-run.d.ts +59 -0
- package/dist/commands/heavy-run.js +247 -0
- package/dist/commands/heavy.d.ts +7 -0
- package/dist/commands/heavy.js +123 -0
- package/dist/commands/link.d.ts +12 -0
- package/dist/commands/link.js +121 -0
- package/dist/commands/ls.d.ts +5 -0
- package/dist/commands/ls.js +9 -1
- package/dist/commands/public-code-manage.d.ts +24 -0
- package/dist/commands/public-code-manage.js +217 -0
- package/dist/commands/public-code.d.ts +12 -1
- package/dist/commands/public-code.js +140 -45
- package/dist/commands/push-heavy.d.ts +6 -0
- package/dist/commands/push-heavy.js +128 -0
- package/dist/commands/push-tree.d.ts +24 -0
- package/dist/commands/push-tree.js +91 -0
- package/dist/commands/push.d.ts +4 -13
- package/dist/commands/push.js +7 -77
- package/dist/commands/put-heavy.d.ts +9 -0
- package/dist/commands/put-heavy.js +120 -0
- package/dist/commands/put-payer.d.ts +21 -0
- package/dist/commands/put-payer.js +24 -0
- package/dist/commands/put.d.ts +2 -2
- package/dist/commands/put.js +8 -7
- package/dist/commands/receive.js +14 -8
- package/dist/commands/s3.js +44 -10
- package/dist/commands/share-dispatch.js +14 -1
- package/dist/commands/share.d.ts +2 -0
- package/dist/commands/share.js +45 -35
- package/dist/commands/shares-sent.js +8 -3
- package/dist/crypto-surface.d.ts +35 -0
- package/dist/crypto-surface.js +13 -0
- package/dist/crypto.d.ts +8 -0
- package/dist/crypto.js +8 -0
- package/dist/download-part.d.ts +32 -1
- package/dist/download-part.js +95 -6
- package/dist/download-range.d.ts +19 -0
- package/dist/download-range.js +108 -0
- package/dist/download-sink.d.ts +15 -0
- package/dist/download.d.ts +14 -0
- package/dist/download.js +19 -4
- package/dist/handover-codes.d.ts +25 -0
- package/dist/handover-codes.js +106 -0
- package/dist/handover.d.ts +62 -0
- package/dist/handover.js +217 -0
- package/dist/heavy-api.d.ts +36 -0
- package/dist/heavy-api.js +107 -0
- package/dist/heavy-copy.d.ts +72 -0
- package/dist/heavy-copy.js +108 -0
- package/dist/heavy-evm.d.ts +20 -0
- package/dist/heavy-evm.js +106 -0
- package/dist/heavy-self-pay.d.ts +92 -0
- package/dist/heavy-self-pay.js +167 -0
- package/dist/heavy-upload.d.ts +87 -0
- package/dist/heavy-upload.js +154 -0
- package/dist/heavy-wallet-pay.d.ts +39 -0
- package/dist/heavy-wallet-pay.js +73 -0
- package/dist/help-options.js +13 -4
- package/dist/help.js +24 -3
- package/dist/links.d.ts +58 -0
- package/dist/links.js +206 -0
- package/dist/list-tier.d.ts +7 -0
- package/dist/list-tier.js +47 -0
- package/dist/main.js +13 -10
- package/dist/manifest-write.d.ts +4 -3
- package/dist/mcp-tools/reads.js +20 -6
- package/dist/mcp-tools/share.js +47 -1
- package/dist/mcp-tools/tiers.js +16 -1
- package/dist/platform-sign.d.ts +4 -4
- package/dist/platform-sign.js +3 -3
- package/dist/portable.d.ts +23 -0
- package/dist/portable.js +19 -0
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/public-code-activity.d.ts +35 -0
- package/dist/public-code-activity.js +88 -0
- package/dist/public-code-refusals.d.ts +25 -0
- package/dist/public-code-refusals.js +87 -0
- package/dist/public-codes.d.ts +76 -0
- package/dist/public-codes.js +182 -0
- package/dist/recovery-build.js +16 -0
- package/dist/recovery-map-part.d.ts +82 -0
- package/dist/recovery-map-part.js +83 -0
- package/dist/recovery-map.d.ts +5 -40
- package/dist/recovery-map.js +18 -1
- package/dist/recovery-source.d.ts +3 -0
- package/dist/recovery-source.js +9 -0
- package/dist/risk.d.ts +55 -1
- package/dist/risk.js +55 -3
- package/dist/run-log.d.ts +1 -1
- package/dist/run-log.js +7 -1
- package/dist/s3/address.d.ts +20 -0
- package/dist/s3/address.js +97 -0
- package/dist/s3/answer.d.ts +48 -0
- package/dist/s3/answer.js +330 -0
- package/dist/s3/body-deframe.d.ts +49 -0
- package/dist/s3/body-deframe.js +264 -0
- package/dist/s3/body-measure.d.ts +13 -0
- package/dist/s3/body-measure.js +48 -0
- package/dist/s3/body-plan.d.ts +31 -0
- package/dist/s3/body-plan.js +159 -0
- package/dist/s3/body.d.ts +46 -0
- package/dist/s3/body.js +117 -0
- package/dist/s3/call.d.ts +36 -0
- package/dist/s3/call.js +42 -0
- package/dist/s3/checksum.d.ts +26 -0
- package/dist/s3/checksum.js +176 -0
- package/dist/s3/content-type.d.ts +3 -0
- package/dist/s3/content-type.js +49 -0
- package/dist/s3/contract.d.ts +91 -14
- package/dist/s3/drive-conditions.d.ts +47 -0
- package/dist/s3/drive-conditions.js +134 -0
- package/dist/s3/drive-key.d.ts +11 -0
- package/dist/s3/drive-key.js +58 -0
- package/dist/s3/drive-limits.d.ts +33 -0
- package/dist/s3/drive-limits.js +53 -0
- package/dist/s3/drive-lock.d.ts +11 -0
- package/dist/s3/drive-lock.js +47 -0
- package/dist/s3/drive-trashed-name.d.ts +13 -0
- package/dist/s3/drive-trashed-name.js +59 -0
- package/dist/s3/drive-write.d.ts +38 -0
- package/dist/s3/drive-write.js +258 -0
- package/dist/s3/drive.d.ts +59 -25
- package/dist/s3/drive.js +46 -77
- package/dist/s3/listing.d.ts +27 -1
- package/dist/s3/listing.js +62 -20
- package/dist/s3/long-answer.d.ts +12 -0
- package/dist/s3/long-answer.js +104 -0
- package/dist/s3/multipart-assemble.d.ts +17 -0
- package/dist/s3/multipart-assemble.js +68 -0
- package/dist/s3/multipart-sweep.d.ts +15 -0
- package/dist/s3/multipart-sweep.js +87 -0
- package/dist/s3/multipart-view.d.ts +45 -0
- package/dist/s3/multipart-view.js +232 -0
- package/dist/s3/multipart.d.ts +26 -19
- package/dist/s3/multipart.js +193 -78
- package/dist/s3/object-read.d.ts +17 -0
- package/dist/s3/object-read.js +127 -0
- package/dist/s3/object-write.d.ts +35 -0
- package/dist/s3/object-write.js +198 -0
- package/dist/s3/range.d.ts +14 -0
- package/dist/s3/range.js +80 -0
- package/dist/s3/response-sink.d.ts +12 -2
- package/dist/s3/response-sink.js +71 -7
- package/dist/s3/routes.d.ts +0 -1
- package/dist/s3/routes.js +242 -217
- package/dist/s3/same-file.d.ts +4 -1
- package/dist/s3/same-file.js +12 -3
- package/dist/s3/server.d.ts +54 -4
- package/dist/s3/server.js +174 -14
- package/dist/s3/sigv4-canonical.d.ts +91 -0
- package/dist/s3/sigv4-canonical.js +148 -0
- package/dist/s3/sigv4-presigned.d.ts +13 -0
- package/dist/s3/sigv4-presigned.js +82 -0
- package/dist/s3/sigv4.d.ts +26 -27
- package/dist/s3/sigv4.js +137 -102
- package/dist/s3/spool.d.ts +37 -0
- package/dist/s3/spool.js +104 -0
- package/dist/s3/staging.d.ts +90 -7
- package/dist/s3/staging.js +97 -66
- package/dist/s3/xml-multipart.d.ts +38 -0
- package/dist/s3/xml-multipart.js +47 -0
- package/dist/s3/xml-read.d.ts +28 -0
- package/dist/s3/xml-read.js +235 -0
- package/dist/s3/xml.d.ts +45 -2
- package/dist/s3/xml.js +72 -13
- package/dist/s3-gateway.d.ts +11 -5
- package/dist/s3-gateway.js +5 -2
- package/dist/seal.d.ts +1 -1
- package/dist/share-codes.d.ts +40 -0
- package/dist/share-codes.js +151 -0
- package/dist/share.d.ts +39 -0
- package/dist/share.js +54 -13
- package/dist/shared/lib/api/types-heavy.d.ts +167 -0
- package/dist/shared/lib/api/types-heavy.js +60 -0
- package/dist/shared/lib/drive/rebuild-verify.js +1 -0
- package/dist/shared/lib/filecoin/providers.d.ts +25 -0
- package/dist/shared/lib/filecoin/providers.js +58 -0
- package/dist/shared/lib/heavy/download.d.ts +48 -0
- package/dist/shared/lib/heavy/download.js +116 -0
- package/dist/shared/lib/heavy/order-plan.d.ts +77 -0
- package/dist/shared/lib/heavy/order-plan.js +83 -0
- package/dist/shared/lib/heavy/order-runner.d.ts +9 -0
- package/dist/shared/lib/heavy/order-runner.js +300 -0
- package/dist/shared/lib/heavy/order-types.d.ts +90 -0
- package/dist/shared/lib/heavy/order-types.js +6 -0
- package/dist/shared/lib/heavy/order-wire.d.ts +55 -0
- package/dist/shared/lib/heavy/order-wire.js +113 -0
- package/dist/shared/lib/heavy/piece-cid.d.ts +24 -0
- package/dist/shared/lib/heavy/piece-cid.js +55 -0
- package/dist/shared/lib/heavy/sp-upload.d.ts +37 -0
- package/dist/shared/lib/heavy/sp-upload.js +112 -0
- package/dist/shared/lib/heavy/synapse-piece.d.ts +1 -0
- package/dist/shared/lib/heavy/synapse-piece.js +8 -0
- package/dist/shared/lib/share/handover-format.d.ts +129 -0
- package/dist/shared/lib/share/handover-format.js +323 -0
- package/dist/shared/lib/share/public-link.d.ts +41 -0
- package/dist/shared/lib/share/public-link.js +107 -0
- package/dist/shared/lib/share/shared-file-info.js +1 -0
- package/dist/shared/lib/storage-network.d.ts +4 -4
- package/dist/shared/lib/storage-network.js +7 -7
- package/dist/shared/lib/storage-tier.d.ts +28 -0
- package/dist/shared/lib/storage-tier.js +63 -0
- package/dist/shared/lib/wallet/send-rules.js +1 -0
- package/dist/upload-file.d.ts +8 -0
- package/dist/upload-file.js +57 -25
- package/dist/upload-price-node.d.ts +3 -0
- package/dist/upload-price-node.js +3 -6
- package/dist/upload-price.d.ts +10 -0
- package/dist/upload-price.js +13 -1
- package/dist/upload-run.d.ts +80 -0
- package/dist/upload-run.js +173 -0
- package/dist/upload-steps.js +15 -4
- package/dist/upload-store.d.ts +15 -6
- package/dist/upload-store.js +6 -0
- package/dist/upload-wallet-put.d.ts +9 -4
- package/dist/upload-wallet-put.js +16 -6
- package/dist/upload-wallet.js +4 -0
- package/dist/upload-wire.d.ts +25 -0
- package/dist/upload-wire.js +20 -1
- package/dist/upload.js +28 -13
- package/dist/wallet-sign.js +2 -2
- package/docs/commands/handover.md +54 -0
- package/docs/commands/heavy.md +35 -0
- package/docs/commands/link.md +27 -0
- package/docs/commands/mcp.md +2 -1
- package/docs/commands/public-code.md +25 -6
- package/docs/commands/push.md +4 -1
- package/docs/commands/put.md +15 -1
- package/docs/commands/share.md +8 -3
- package/package.json +5 -2
- package/server.json +2 -2
- package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +106 -4
- package/vendor/nmts-crypto/nmts_crypto_wasm.js +286 -3
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +12 -1
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// `GetObject` and `HeadObject`: one file, whole or in part, or only what is known about it.
|
|
2
|
+
//
|
|
3
|
+
// ⛔ THE ETAG HERE IS THE ETAG EVERYWHERE. A listing, a HEAD, a GET and the answer to the upload
|
|
4
|
+
// that stored the file all name the same tag, because all of them come from `etagOf` on the
|
|
5
|
+
// same entry. A sync tool compares them to decide what to fetch again; two that disagree make
|
|
6
|
+
// it fetch everything, every time.
|
|
7
|
+
import { validateHeaderValue } from "node:http";
|
|
8
|
+
import { failWith, refuse } from "./answer.js";
|
|
9
|
+
import { headerOf } from "./call.js";
|
|
10
|
+
import { contentTypeOf } from "./content-type.js";
|
|
11
|
+
import { objectsOf } from "./listing.js";
|
|
12
|
+
import { preconditionOf, rangeOf } from "./range.js";
|
|
13
|
+
import { responseSink } from "./response-sink.js";
|
|
14
|
+
function httpDate(ms) {
|
|
15
|
+
return new Date(ms).toUTCString();
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The headers a file is answered with.
|
|
19
|
+
*
|
|
20
|
+
* ⛔ A FILE IS NEVER A PAGE OF THE GATEWAY'S ORIGIN. A business serves its users' files from its own
|
|
21
|
+
* host, often through presigned links a browser opens, and a file named `.html` or `.svg` is
|
|
22
|
+
* answered with a type a browser runs. `Content-Security-Policy: sandbox` makes whatever it runs
|
|
23
|
+
* run in an origin of its own, with no scripts and no reach into the host's cookies or storage;
|
|
24
|
+
* `nosniff` keeps a browser from deciding that a file sent as bytes or text is script after all.
|
|
25
|
+
* A business that wants its users' files downloaded rather than shown says so per link, with
|
|
26
|
+
* `response-content-disposition` (below).
|
|
27
|
+
*/
|
|
28
|
+
export function objectHeaders(object) {
|
|
29
|
+
return {
|
|
30
|
+
"content-type": contentTypeOf(object.key),
|
|
31
|
+
"last-modified": httpDate(object.entry.updatedAt),
|
|
32
|
+
etag: object.etag,
|
|
33
|
+
"accept-ranges": "bytes",
|
|
34
|
+
"x-content-type-options": "nosniff",
|
|
35
|
+
"content-security-policy": "sandbox",
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The query parameters S3 lets a signed GET or HEAD use to choose headers of its answer, and the
|
|
40
|
+
* header each one sets. Every request here is signed, so every request may use them -- a presigned
|
|
41
|
+
* link above all, which is how a business makes a link download a file instead of showing it.
|
|
42
|
+
*/
|
|
43
|
+
const OVERRIDES = [
|
|
44
|
+
["response-content-type", "content-type"],
|
|
45
|
+
["response-content-disposition", "content-disposition"],
|
|
46
|
+
];
|
|
47
|
+
/** The headers this request's `response-*` parameters ask for, or the parameter no header can carry. */
|
|
48
|
+
function overridesOf(query) {
|
|
49
|
+
const headers = {};
|
|
50
|
+
for (const [parameter, header] of OVERRIDES) {
|
|
51
|
+
const value = query.get(parameter);
|
|
52
|
+
if (value === null)
|
|
53
|
+
continue;
|
|
54
|
+
try {
|
|
55
|
+
validateHeaderValue(header, value);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return { bad: parameter };
|
|
59
|
+
}
|
|
60
|
+
headers[header] = value;
|
|
61
|
+
}
|
|
62
|
+
return { headers };
|
|
63
|
+
}
|
|
64
|
+
/** The live file at this call's key, or a 404 already answered. */
|
|
65
|
+
export async function objectAt(call) {
|
|
66
|
+
const object = objectsOf(await call.source.entries()).find((o) => o.key === call.key);
|
|
67
|
+
if (object === undefined) {
|
|
68
|
+
refuse(call.res, 404, "NoSuchKey", "This account's file list has no such file.", call.resource);
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
return object;
|
|
72
|
+
}
|
|
73
|
+
export async function readObject(call, head) {
|
|
74
|
+
const { res, req } = call;
|
|
75
|
+
const object = await objectAt(call);
|
|
76
|
+
if (object === null)
|
|
77
|
+
return;
|
|
78
|
+
const condition = preconditionOf(req, object.etag, object.entry.updatedAt);
|
|
79
|
+
if (condition === 412) {
|
|
80
|
+
const why = "At least one of the preconditions you specified did not hold.";
|
|
81
|
+
refuse(res, 412, "PreconditionFailed", why, call.resource);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (condition === 304) {
|
|
85
|
+
res.writeHead(304, { etag: object.etag, "last-modified": httpDate(object.entry.updatedAt) });
|
|
86
|
+
res.end();
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const chosen = overridesOf(call.query);
|
|
90
|
+
if ("bad" in chosen) {
|
|
91
|
+
refuse(res, 400, "InvalidArgument", `${chosen.bad} holds a character no HTTP header can carry.`, call.resource);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
const headers = { ...objectHeaders(object), ...chosen.headers };
|
|
95
|
+
const range = rangeOf(headerOf(req, "range"), object.size);
|
|
96
|
+
if (range.kind === "unsatisfiable") {
|
|
97
|
+
refuse(res, 416, "InvalidRange", "The requested range is not satisfiable.", call.resource, {
|
|
98
|
+
"content-range": `bytes */${object.size}`,
|
|
99
|
+
});
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const window = range.kind === "window" ? { start: range.start, end: range.end } : null;
|
|
103
|
+
if (head) {
|
|
104
|
+
res.writeHead(window === null ? 200 : 206, {
|
|
105
|
+
...headers,
|
|
106
|
+
"content-length": String(window === null ? object.size : window.end - window.start + 1),
|
|
107
|
+
...(window === null ? {} : { "content-range": `bytes ${window.start}-${window.end}/${object.size}` }),
|
|
108
|
+
});
|
|
109
|
+
res.end();
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (object.entry.dekWrapped === undefined) {
|
|
113
|
+
refuse(res, 500, "InternalError", "That entry has no key in the file list.", call.resource);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const sink = responseSink(res, { headers, window });
|
|
117
|
+
try {
|
|
118
|
+
await call.source.fetch(object, sink);
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
// The range was delivered and the reader was stopped on purpose: that is the success path.
|
|
122
|
+
if (sink.windowDelivered())
|
|
123
|
+
return;
|
|
124
|
+
await sink.abandon();
|
|
125
|
+
failWith(res, error, call.resource);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type Call } from "./call.ts";
|
|
2
|
+
import type { DriveWriter } from "./contract.ts";
|
|
3
|
+
/** S3's own ceiling on a `DeleteObjects` body. */
|
|
4
|
+
export declare const MAX_DELETE_BODY: number;
|
|
5
|
+
export declare function putObject(call: Call, writer: DriveWriter): Promise<void>;
|
|
6
|
+
/** `x-amz-copy-source`: `bucket/key`, percent-encoded, with or without a leading slash. */
|
|
7
|
+
export declare function copySourceOf(header: string): {
|
|
8
|
+
bucket: string;
|
|
9
|
+
key: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* `CopyObject`: the source's plaintext, fetched and stored again at this key.
|
|
13
|
+
*
|
|
14
|
+
* ⛔ THE SOURCE BUCKET IS ASKED ABOUT EXACTLY AS THE DESTINATION WAS — the pair's restriction
|
|
15
|
+
* first, the resolver second — or a copy would be a way to read a bucket the pair may not.
|
|
16
|
+
*
|
|
17
|
+
* ⛔ THE SOURCE IS READ FROM THE LIST AS IT IS NOW, not as cached: its conditions
|
|
18
|
+
* (`x-amz-copy-source-if-*`) are about the file that will be copied, and a cached list can name
|
|
19
|
+
* one that has since been replaced.
|
|
20
|
+
*/
|
|
21
|
+
export declare function copyObject(call: Call, writer: DriveWriter, header: string): Promise<void>;
|
|
22
|
+
export declare function deleteObject(call: Call, writer: DriveWriter): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* `DeleteObjects`: up to a thousand keys in one request, each answered on its own.
|
|
25
|
+
*
|
|
26
|
+
* ⛔ ONE KEY'S FAILURE IS THAT KEY'S ANSWER, not the request's. S3 answers 200 with an `Error` row
|
|
27
|
+
* for the key that failed and a `Deleted` row for every other; a client retries the rows that
|
|
28
|
+
* failed. Failing the whole request for one would have it retry — and re-send — all of them.
|
|
29
|
+
*
|
|
30
|
+
* ⛔ THE LIST OF KEYS MUST CARRY A DIGEST, AS S3 REQUIRES. Signed as `UNSIGNED-PAYLOAD`, nothing
|
|
31
|
+
* else binds the body to the signature, and a body swapped on the way would trash files the
|
|
32
|
+
* client never named. `Content-MD5` or an `x-amz-checksum-*` is required, and the body decoder
|
|
33
|
+
* holds the bytes to it.
|
|
34
|
+
*/
|
|
35
|
+
export declare function deleteObjects(call: Call, writer: DriveWriter): Promise<void>;
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// The four ways a client changes a bucket: put a file, copy one, delete one, delete many.
|
|
2
|
+
//
|
|
3
|
+
// ⛔ WHETHER A TAKEN KEY IS A CONFLICT IS NOT DECIDED HERE. It is a question about CONTENT — is
|
|
4
|
+
// the file arriving the file already there — and it cannot be answered until the bytes are in,
|
|
5
|
+
// so the writer answers it, once, at the point every way of storing meets (`drive-write.ts`).
|
|
6
|
+
// What reaches this layer is the outcome, and the tag of the file that was stored.
|
|
7
|
+
//
|
|
8
|
+
// ⛔ WHAT CAN BE REFUSED BEFORE THE BODY IS, IS. A key the drive cannot hold, a size over the
|
|
9
|
+
// limit, a folder in the way, a condition that already fails: each is known from the request
|
|
10
|
+
// line, the headers and the list, and reading gigabytes first to refuse them afterwards costs a
|
|
11
|
+
// client its whole upload for an answer it could have had at once.
|
|
12
|
+
import { answerFor, refuse, S3Refusal, sendXml } from "./answer.js";
|
|
13
|
+
import { BodyRefusal, decodeBody } from "./body.js";
|
|
14
|
+
import { headerOf, mayTouch, metaOf, NOT_YOURS } from "./call.js";
|
|
15
|
+
import { checkCopySource, checkWriteCondition, copySourceConditionOf, deleteConditionOf, refuseConditions, writeConditionOf, } from "./drive-conditions.js";
|
|
16
|
+
import { checkKey } from "./drive-key.js";
|
|
17
|
+
import { checkDeclaredSize, checkNotEmpty } from "./drive-limits.js";
|
|
18
|
+
import { precheck } from "./drive-write.js";
|
|
19
|
+
import { isFolderKey, objectsOf } from "./listing.js";
|
|
20
|
+
import { answerAfter } from "./long-answer.js";
|
|
21
|
+
import { consuming, readBodyText } from "./spool.js";
|
|
22
|
+
import { copyObjectXml, deleteResultXml } from "./xml.js";
|
|
23
|
+
import { deleteAskOf, parseXml } from "./xml-read.js";
|
|
24
|
+
/** S3's own ceiling on a `DeleteObjects` body. */
|
|
25
|
+
export const MAX_DELETE_BODY = 1024 * 1024;
|
|
26
|
+
export async function putObject(call, writer) {
|
|
27
|
+
checkKey(call.key);
|
|
28
|
+
const condition = writeConditionOf(call.req, "PutObject");
|
|
29
|
+
const body = decodeBody(call.req, call.verdict);
|
|
30
|
+
if (body instanceof BodyRefusal)
|
|
31
|
+
throw body;
|
|
32
|
+
const outcome = await consuming(body, async () => {
|
|
33
|
+
if (!isFolderKey(call.key)) {
|
|
34
|
+
checkNotEmpty(body.size);
|
|
35
|
+
checkDeclaredSize(body.size, writer.maxObjectBytes);
|
|
36
|
+
}
|
|
37
|
+
precheck(objectsOf(await call.source.entries()), call.key, condition);
|
|
38
|
+
return await writer.put(call.key, body, metaOf(call.req), condition);
|
|
39
|
+
});
|
|
40
|
+
call.res.writeHead(200, { etag: outcome.etag, "content-length": "0" });
|
|
41
|
+
call.res.end();
|
|
42
|
+
}
|
|
43
|
+
/** `x-amz-copy-source`: `bucket/key`, percent-encoded, with or without a leading slash. */
|
|
44
|
+
export function copySourceOf(header) {
|
|
45
|
+
const at = header.indexOf("?");
|
|
46
|
+
if (at >= 0) {
|
|
47
|
+
const version = new URLSearchParams(header.slice(at + 1)).get("versionId");
|
|
48
|
+
if (version !== null) {
|
|
49
|
+
throw new S3Refusal(501, "NotImplemented", "This gateway keeps no versions, so a copy cannot name one.");
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
let path;
|
|
53
|
+
try {
|
|
54
|
+
path = decodeURIComponent(at < 0 ? header : header.slice(0, at)).replace(/^\//, "");
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
throw new S3Refusal(400, "InvalidArgument", "x-amz-copy-source is not valid percent-encoding.");
|
|
58
|
+
}
|
|
59
|
+
const slash = path.indexOf("/");
|
|
60
|
+
if (slash <= 0 || slash === path.length - 1) {
|
|
61
|
+
throw new S3Refusal(400, "InvalidArgument", "x-amz-copy-source must name a bucket and a key: bucket/key.");
|
|
62
|
+
}
|
|
63
|
+
return { bucket: path.slice(0, slash), key: path.slice(slash + 1) };
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* `CopyObject`: the source's plaintext, fetched and stored again at this key.
|
|
67
|
+
*
|
|
68
|
+
* ⛔ THE SOURCE BUCKET IS ASKED ABOUT EXACTLY AS THE DESTINATION WAS — the pair's restriction
|
|
69
|
+
* first, the resolver second — or a copy would be a way to read a bucket the pair may not.
|
|
70
|
+
*
|
|
71
|
+
* ⛔ THE SOURCE IS READ FROM THE LIST AS IT IS NOW, not as cached: its conditions
|
|
72
|
+
* (`x-amz-copy-source-if-*`) are about the file that will be copied, and a cached list can name
|
|
73
|
+
* one that has since been replaced.
|
|
74
|
+
*/
|
|
75
|
+
export async function copyObject(call, writer, header) {
|
|
76
|
+
const from = copySourceOf(header);
|
|
77
|
+
checkKey(call.key);
|
|
78
|
+
const condition = writeConditionOf(call.req, "CopyObject");
|
|
79
|
+
const sourceCondition = copySourceConditionOf(call.req);
|
|
80
|
+
if (!mayTouch(call.verdict.credential, from.bucket)) {
|
|
81
|
+
refuse(call.res, 403, "AccessDenied", NOT_YOURS, call.resource);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const source = from.bucket === call.bucket ? call.source : await call.options.bucketOf(from.bucket);
|
|
85
|
+
if (source === null) {
|
|
86
|
+
refuse(call.res, 404, "NoSuchBucket", `No bucket named ${from.bucket} is served here.`, call.resource);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const object = objectsOf(await source.entries({ fresh: true })).find((o) => o.key === from.key);
|
|
90
|
+
if (object === undefined) {
|
|
91
|
+
refuse(call.res, 404, "NoSuchKey", "The copy source is not in that bucket's file list.", call.resource);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
checkCopySource(object, sourceCondition);
|
|
95
|
+
const asked = metaOf(call.req);
|
|
96
|
+
const replacing = (headerOf(call.req, "x-amz-metadata-directive") ?? "COPY").trim().toUpperCase() === "REPLACE";
|
|
97
|
+
if (from.bucket === call.bucket && from.key === call.key) {
|
|
98
|
+
// ⚠ A COPY ONTO ITSELF CHANGES NOTHING HERE BUT WHAT IS NOT KEPT. Nothing about a file but its
|
|
99
|
+
// bytes is kept, so "the same bytes with new metadata" or "in another storage class" is
|
|
100
|
+
// already true of the file as it stands, and nothing is sent. S3 refuses only a self-copy
|
|
101
|
+
// that asks for no change at all.
|
|
102
|
+
if (!replacing && asked.storageClass === null) {
|
|
103
|
+
throw new S3Refusal(400, "InvalidRequest", "This copy request is illegal because it is trying to copy an object to itself without changing the " +
|
|
104
|
+
"object's metadata, storage class, website redirect location or encryption attributes.");
|
|
105
|
+
}
|
|
106
|
+
checkWriteCondition(object, condition);
|
|
107
|
+
sendXml(call.res, 200, copyObjectXml(object.etag, object.lastModified));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (isFolderKey(call.key)) {
|
|
111
|
+
throw new S3Refusal(400, "InvalidArgument", "A copy cannot be stored at a key ending in `/`, which names a folder.");
|
|
112
|
+
}
|
|
113
|
+
checkNotEmpty(object.size);
|
|
114
|
+
checkDeclaredSize(object.size, writer.maxObjectBytes);
|
|
115
|
+
precheck(objectsOf(await call.source.entries()), call.key, condition);
|
|
116
|
+
// ⛔ EVERY REFUSAL ABOVE KEEPS ITS OWN STATUS, AND SO DOES ONE BELOW THAT COMES WITHIN ONE
|
|
117
|
+
// KEEP-ALIVE INTERVAL (`long-answer.ts`). Only a fetch and store that outlasts it begins the
|
|
118
|
+
// 200, and what that comes to -- a taken key included, which only the bytes can decide -- is
|
|
119
|
+
// then an error document in the 200 body.
|
|
120
|
+
await answerAfter(call, async (accepted) => {
|
|
121
|
+
accepted();
|
|
122
|
+
const outcome = await writer.copy({ source, object }, call.key, { storageClass: asked.storageClass, contentType: replacing ? asked.contentType : null }, condition);
|
|
123
|
+
const stored = objectsOf(await call.source.entries()).find((o) => o.etag === outcome.etag);
|
|
124
|
+
return copyObjectXml(outcome.etag, stored?.lastModified ?? new Date().toISOString());
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
export async function deleteObject(call, writer) {
|
|
128
|
+
const condition = deleteConditionOf(call.req);
|
|
129
|
+
// S3 answers 204 for a key that is not there, and clients rely on it: a sync that deletes the
|
|
130
|
+
// same key twice must not fail the second time.
|
|
131
|
+
const [outcome] = await writer.remove([call.key], condition);
|
|
132
|
+
if (outcome !== undefined && outcome.error !== null)
|
|
133
|
+
throw outcome.error;
|
|
134
|
+
call.res.writeHead(204);
|
|
135
|
+
call.res.end();
|
|
136
|
+
}
|
|
137
|
+
/** The integrity headers S3 requires on a `DeleteObjects`: one of them has to be there. */
|
|
138
|
+
const INTEGRITY = /^(content-md5|x-amz-checksum-(crc32|crc32c|crc64nvme|sha1|sha256)|x-amz-trailer)$/;
|
|
139
|
+
/**
|
|
140
|
+
* `DeleteObjects`: up to a thousand keys in one request, each answered on its own.
|
|
141
|
+
*
|
|
142
|
+
* ⛔ ONE KEY'S FAILURE IS THAT KEY'S ANSWER, not the request's. S3 answers 200 with an `Error` row
|
|
143
|
+
* for the key that failed and a `Deleted` row for every other; a client retries the rows that
|
|
144
|
+
* failed. Failing the whole request for one would have it retry — and re-send — all of them.
|
|
145
|
+
*
|
|
146
|
+
* ⛔ THE LIST OF KEYS MUST CARRY A DIGEST, AS S3 REQUIRES. Signed as `UNSIGNED-PAYLOAD`, nothing
|
|
147
|
+
* else binds the body to the signature, and a body swapped on the way would trash files the
|
|
148
|
+
* client never named. `Content-MD5` or an `x-amz-checksum-*` is required, and the body decoder
|
|
149
|
+
* holds the bytes to it.
|
|
150
|
+
*/
|
|
151
|
+
export async function deleteObjects(call, writer) {
|
|
152
|
+
refuseConditions(call.req, "DeleteObjects");
|
|
153
|
+
const body = decodeBody(call.req, call.verdict);
|
|
154
|
+
if (body instanceof BodyRefusal)
|
|
155
|
+
throw body;
|
|
156
|
+
const text = await consuming(body, () => readBodyText(body, MAX_DELETE_BODY, "The list of keys to delete"));
|
|
157
|
+
// ⚠ Refused once the body is read — a mebibyte at most — so the connection is left clean for the
|
|
158
|
+
// client's next request rather than closed under it with its list unread.
|
|
159
|
+
if (!Object.keys(call.req.headers).some((name) => INTEGRITY.test(name))) {
|
|
160
|
+
throw new S3Refusal(400, "InvalidRequest", "Missing required header for this request: Content-MD5 or an x-amz-checksum-* header. Nothing was deleted.");
|
|
161
|
+
}
|
|
162
|
+
const root = parseXml(text);
|
|
163
|
+
const ask = deleteAskOf(root);
|
|
164
|
+
// ⛔ A CONDITION ON ONE KEY OF THE BATCH IS REFUSED, NOT DROPPED. S3 lets each `<Object>` carry
|
|
165
|
+
// an `<ETag>`, a `<LastModifiedTime>` or a `<Size>` it must match; the list read here keeps
|
|
166
|
+
// none of them, and deleting a key whose condition was never checked is the one thing the
|
|
167
|
+
// client sent it to prevent.
|
|
168
|
+
const conditional = root.children.some((object) => object.name === "Object" &&
|
|
169
|
+
object.children.some((field) => field.name === "ETag" || field.name === "LastModifiedTime" || field.name === "Size"));
|
|
170
|
+
if (conditional) {
|
|
171
|
+
throw new S3Refusal(501, "NotImplemented", "This gateway does not honour a condition on a key in a batch delete. Delete that key with DeleteObject and If-Match. Nothing was deleted.");
|
|
172
|
+
}
|
|
173
|
+
// ⚠ A THOUSAND KEYS IS A LONG WRITE, and the answer is begun first so the connection is not
|
|
174
|
+
// silent while it runs (`long-answer.ts`).
|
|
175
|
+
await answerAfter(call, async (accepted) => {
|
|
176
|
+
accepted();
|
|
177
|
+
const errors = [];
|
|
178
|
+
const versioned = new Set();
|
|
179
|
+
ask.objects.forEach(({ key, versionId }, at) => {
|
|
180
|
+
if (versionId !== null && versionId !== "null") {
|
|
181
|
+
versioned.add(at);
|
|
182
|
+
errors.push({ key, code: "NotImplemented", message: "This gateway keeps no versions." });
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
const keys = ask.objects.filter((_, at) => !versioned.has(at)).map((o) => o.key);
|
|
186
|
+
const outcomes = keys.length === 0 ? [] : await writer.remove(keys);
|
|
187
|
+
const deleted = [];
|
|
188
|
+
for (const { key, error } of outcomes) {
|
|
189
|
+
if (error === null) {
|
|
190
|
+
deleted.push(key);
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
const answer = answerFor(error);
|
|
194
|
+
errors.push({ key, code: answer.code, message: answer.message });
|
|
195
|
+
}
|
|
196
|
+
return deleteResultXml({ deleted: ask.quiet ? [] : deleted, errors });
|
|
197
|
+
});
|
|
198
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { IncomingMessage } from "node:http";
|
|
2
|
+
export type RangeAsk = {
|
|
3
|
+
readonly kind: "whole";
|
|
4
|
+
} | {
|
|
5
|
+
readonly kind: "window";
|
|
6
|
+
readonly start: number;
|
|
7
|
+
readonly end: number;
|
|
8
|
+
} | {
|
|
9
|
+
readonly kind: "unsatisfiable";
|
|
10
|
+
};
|
|
11
|
+
/** What a `Range` header asks of an object this many bytes long. */
|
|
12
|
+
export declare function rangeOf(header: string | undefined, size: number): RangeAsk;
|
|
13
|
+
/** What the conditional headers of a GET or HEAD come to for this object. */
|
|
14
|
+
export declare function preconditionOf(req: IncomingMessage, etag: string, updatedAt: number): 200 | 304 | 412;
|
package/dist/s3/range.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// The two questions a GET may ask before it wants bytes: which bytes, and only if what?
|
|
2
|
+
//
|
|
3
|
+
// ⛔ A RANGE IS ANSWERED OR IGNORED, NEVER GUESSED AT. One range in a form S3 knows is answered
|
|
4
|
+
// 206 with exactly those bytes; one that starts past the end is 416 with the size, so the client
|
|
5
|
+
// can ask again; anything else — several ranges, a unit that is not bytes, a range written
|
|
6
|
+
// backwards — is ignored and the whole object is answered 200, which is what HTTP says to do
|
|
7
|
+
// and what S3 does. Every client already handles a 200 to a ranged request.
|
|
8
|
+
//
|
|
9
|
+
// ⚠ THE CONDITIONS ARE THE HTTP ONES, IN HTTP'S ORDER. `If-Match`, else `If-Unmodified-Since`;
|
|
10
|
+
// then `If-None-Match`, else `If-Modified-Since`. Times compare at whole seconds, because that is
|
|
11
|
+
// all an HTTP date carries.
|
|
12
|
+
import { headerOf } from "./call.js";
|
|
13
|
+
const WHOLE = { kind: "whole" };
|
|
14
|
+
/** What a `Range` header asks of an object this many bytes long. */
|
|
15
|
+
export function rangeOf(header, size) {
|
|
16
|
+
if (header === undefined)
|
|
17
|
+
return WHOLE;
|
|
18
|
+
const spec = /^\s*bytes\s*=\s*(.*)$/i.exec(header)?.[1];
|
|
19
|
+
if (spec === undefined || spec.includes(","))
|
|
20
|
+
return WHOLE;
|
|
21
|
+
const one = /^\s*(\d*)\s*-\s*(\d*)\s*$/.exec(spec);
|
|
22
|
+
const first = one?.[1];
|
|
23
|
+
const last = one?.[2];
|
|
24
|
+
if (first === undefined || last === undefined || (first === "" && last === ""))
|
|
25
|
+
return WHOLE;
|
|
26
|
+
if (first === "") {
|
|
27
|
+
// `bytes=-n`: the last n bytes, or all of them when there are fewer.
|
|
28
|
+
const n = Number(last);
|
|
29
|
+
if (n === 0 || size === 0)
|
|
30
|
+
return { kind: "unsatisfiable" };
|
|
31
|
+
return { kind: "window", start: Math.max(0, size - n), end: size - 1 };
|
|
32
|
+
}
|
|
33
|
+
const start = Number(first);
|
|
34
|
+
// Written backwards it is not a range and is ignored; one that starts past the end is refused.
|
|
35
|
+
if (last !== "" && Number(last) < start)
|
|
36
|
+
return WHOLE;
|
|
37
|
+
if (start >= size)
|
|
38
|
+
return { kind: "unsatisfiable" };
|
|
39
|
+
return { kind: "window", start, end: last === "" ? size - 1 : Math.min(Number(last), size - 1) };
|
|
40
|
+
}
|
|
41
|
+
function bareTag(tag) {
|
|
42
|
+
return tag.trim().replace(/^W\//, "").replace(/^"|"$/g, "");
|
|
43
|
+
}
|
|
44
|
+
/** Whether a list of tags in a header names this one. `*` names every object that exists. */
|
|
45
|
+
function tagListHas(header, etag) {
|
|
46
|
+
const mine = bareTag(etag);
|
|
47
|
+
return header.split(",").some((tag) => tag.trim() === "*" || bareTag(tag) === mine);
|
|
48
|
+
}
|
|
49
|
+
/** An HTTP date as whole seconds, or null when it is not one — and an unreadable date is ignored. */
|
|
50
|
+
function secondsOf(header) {
|
|
51
|
+
const ms = Date.parse(header);
|
|
52
|
+
return Number.isNaN(ms) ? null : Math.floor(ms / 1000);
|
|
53
|
+
}
|
|
54
|
+
/** What the conditional headers of a GET or HEAD come to for this object. */
|
|
55
|
+
export function preconditionOf(req, etag, updatedAt) {
|
|
56
|
+
const changed = Math.floor(updatedAt / 1000);
|
|
57
|
+
const ifMatch = headerOf(req, "if-match");
|
|
58
|
+
const ifUnmodifiedSince = headerOf(req, "if-unmodified-since");
|
|
59
|
+
if (ifMatch !== undefined) {
|
|
60
|
+
if (!tagListHas(ifMatch, etag))
|
|
61
|
+
return 412;
|
|
62
|
+
}
|
|
63
|
+
else if (ifUnmodifiedSince !== undefined) {
|
|
64
|
+
const at = secondsOf(ifUnmodifiedSince);
|
|
65
|
+
if (at !== null && changed > at)
|
|
66
|
+
return 412;
|
|
67
|
+
}
|
|
68
|
+
const ifNoneMatch = headerOf(req, "if-none-match");
|
|
69
|
+
const ifModifiedSince = headerOf(req, "if-modified-since");
|
|
70
|
+
if (ifNoneMatch !== undefined) {
|
|
71
|
+
if (tagListHas(ifNoneMatch, etag))
|
|
72
|
+
return 304;
|
|
73
|
+
}
|
|
74
|
+
else if (ifModifiedSince !== undefined) {
|
|
75
|
+
const at = secondsOf(ifModifiedSince);
|
|
76
|
+
if (at !== null && changed <= at)
|
|
77
|
+
return 304;
|
|
78
|
+
}
|
|
79
|
+
return 200;
|
|
80
|
+
}
|
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
import type { ServerResponse } from "node:http";
|
|
2
2
|
import type { PlaintextSink } from "../download-sink.ts";
|
|
3
3
|
export interface ResponseSinkOptions {
|
|
4
|
-
/** Headers to send with the 200, once the size is known. */
|
|
4
|
+
/** Headers to send with the 200 or 206, once the size is known. */
|
|
5
5
|
readonly headers: Readonly<Record<string, string>>;
|
|
6
|
+
/** Only these bytes, inclusive, answered 206. Null or absent answers the whole file. */
|
|
7
|
+
readonly window?: {
|
|
8
|
+
readonly start: number;
|
|
9
|
+
readonly end: number;
|
|
10
|
+
} | null | undefined;
|
|
11
|
+
}
|
|
12
|
+
/** A response sink, and whether a range it was cutting has been delivered in full. */
|
|
13
|
+
export interface ResponseSink extends PlaintextSink {
|
|
14
|
+
/** True once every byte of the window went out and the response was ended. */
|
|
15
|
+
windowDelivered(): boolean;
|
|
6
16
|
}
|
|
7
17
|
/** A sink that writes one file into an HTTP response and never leaves a short body looking whole. */
|
|
8
|
-
export declare function responseSink(res: ServerResponse, options: ResponseSinkOptions):
|
|
18
|
+
export declare function responseSink(res: ServerResponse, options: ResponseSinkOptions): ResponseSink;
|
package/dist/s3/response-sink.js
CHANGED
|
@@ -10,32 +10,96 @@
|
|
|
10
10
|
// rather than ended. A client that was promised `Content-Length` bytes and gets fewer, with no
|
|
11
11
|
// clean end, reports a failed transfer — which is the truth. Ending the response normally would
|
|
12
12
|
// hand over a short file that every client would file away as complete.
|
|
13
|
+
//
|
|
14
|
+
// ⚠ A RANGE IS CUT FROM THE PLAINTEXT AS IT PASSES. The sink names its window, so the reader can
|
|
15
|
+
// leave out the stored parts that end before it (`skip` moves this sink's count past them) and
|
|
16
|
+
// start at the part the window begins in; inside that part it still reads from the part's first
|
|
17
|
+
// chunk, because the sequential opener is the only one this tool's engine surface has. The write
|
|
18
|
+
// that carries the window's last byte ends the response and refuses, which stops the reader
|
|
19
|
+
// rather than fetching the rest of the file for nobody. ⚠ The whole-file digest is reached only
|
|
20
|
+
// by a range read from the file's first byte to its last: every part is still authenticated,
|
|
21
|
+
// and put in its place by its sealed header, but any other range is not checked against the
|
|
22
|
+
// file's recorded hash.
|
|
23
|
+
import { NmtsError } from "../errors.js";
|
|
24
|
+
/** Thrown into the reader once the window is out: the rest of the file is not wanted. */
|
|
25
|
+
class WindowDelivered extends Error {
|
|
26
|
+
constructor() {
|
|
27
|
+
super("Every byte of the requested range was delivered.");
|
|
28
|
+
this.name = "WindowDelivered";
|
|
29
|
+
}
|
|
30
|
+
}
|
|
13
31
|
/** A sink that writes one file into an HTTP response and never leaves a short body looking whole. */
|
|
14
32
|
export function responseSink(res, options) {
|
|
33
|
+
const window = options.window ?? null;
|
|
15
34
|
let started = false;
|
|
35
|
+
let delivered = false;
|
|
36
|
+
let size = 0;
|
|
37
|
+
let offset = 0;
|
|
38
|
+
const send = (bytes) => new Promise((resolve, reject) => {
|
|
39
|
+
res.write(bytes, (error) => (error === null || error === undefined ? resolve() : reject(error)));
|
|
40
|
+
});
|
|
16
41
|
return {
|
|
17
|
-
expect(
|
|
18
|
-
|
|
42
|
+
expect(total) {
|
|
43
|
+
size = total;
|
|
44
|
+
if (window === null) {
|
|
45
|
+
res.writeHead(200, { ...options.headers, "content-length": String(total) });
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
if (window.end >= total)
|
|
49
|
+
throw new NmtsError("The stored file is shorter than its listing says.");
|
|
50
|
+
res.writeHead(206, {
|
|
51
|
+
...options.headers,
|
|
52
|
+
"content-range": `bytes ${window.start}-${window.end}/${total}`,
|
|
53
|
+
"content-length": String(window.end - window.start + 1),
|
|
54
|
+
});
|
|
55
|
+
}
|
|
19
56
|
started = true;
|
|
20
57
|
},
|
|
21
58
|
async write(bytes) {
|
|
59
|
+
if (delivered)
|
|
60
|
+
throw new WindowDelivered();
|
|
22
61
|
if (res.writableEnded || res.destroyed)
|
|
23
62
|
return;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
63
|
+
if (window === null) {
|
|
64
|
+
await send(bytes);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const from = offset;
|
|
68
|
+
offset += bytes.length;
|
|
69
|
+
const lo = Math.max(from, window.start);
|
|
70
|
+
const hi = Math.min(offset, window.end + 1);
|
|
71
|
+
if (lo < hi)
|
|
72
|
+
await send(bytes.subarray(lo - from, hi - from));
|
|
73
|
+
// A window that runs to the file's own end is left to `commit`, so the reader's checks still
|
|
74
|
+
// run before the response ends; one that stops short ends here and stops the reader.
|
|
75
|
+
if (offset > window.end && offset < size) {
|
|
76
|
+
delivered = true;
|
|
77
|
+
await new Promise((resolve) => res.end(resolve));
|
|
78
|
+
throw new WindowDelivered();
|
|
79
|
+
}
|
|
27
80
|
},
|
|
28
81
|
async commit() {
|
|
29
82
|
if (res.destroyed)
|
|
30
83
|
return false;
|
|
31
|
-
|
|
84
|
+
if (!res.writableEnded)
|
|
85
|
+
await new Promise((resolve) => res.end(resolve));
|
|
32
86
|
return true;
|
|
33
87
|
},
|
|
34
88
|
async abandon() {
|
|
35
89
|
// Nothing was written yet: the caller can still answer with a proper S3 error document.
|
|
36
|
-
if (!started)
|
|
90
|
+
if (!started || delivered)
|
|
37
91
|
return;
|
|
38
92
|
res.destroy();
|
|
39
93
|
},
|
|
94
|
+
window,
|
|
95
|
+
skip(bytes) {
|
|
96
|
+
// ⛔ ONLY BYTES THE CLIENT DID NOT ASK FOR. Skipping into the window would send a body that
|
|
97
|
+
// is short by exactly what was skipped, under a length that promised it.
|
|
98
|
+
if (window === null || offset + bytes > window.start) {
|
|
99
|
+
throw new NmtsError("The reader left out bytes this range needs.");
|
|
100
|
+
}
|
|
101
|
+
offset += bytes;
|
|
102
|
+
},
|
|
103
|
+
windowDelivered: () => delivered,
|
|
40
104
|
};
|
|
41
105
|
}
|
package/dist/s3/routes.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
2
|
import type { GatewayOptions } from "./contract.ts";
|
|
3
|
-
export declare function fail(res: ServerResponse, status: number, code: string, message: string, resource: string): void;
|
|
4
3
|
export declare function handle(req: IncomingMessage, res: ServerResponse, options: GatewayOptions): Promise<void>;
|