@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.
Files changed (252) hide show
  1. package/AGENTS.md +8 -8
  2. package/CHANGELOG.md +78 -0
  3. package/README.ko.md +6 -3
  4. package/README.md +31 -11
  5. package/dist/api-advice.js +17 -2
  6. package/dist/api.d.ts +22 -0
  7. package/dist/api.js +49 -1
  8. package/dist/arg-options.d.ts +19 -1
  9. package/dist/args.js +14 -2
  10. package/dist/artifact-about.d.ts +1 -1
  11. package/dist/autonomy.js +3 -3
  12. package/dist/collision.d.ts +20 -3
  13. package/dist/collision.js +21 -3
  14. package/dist/commands/erase.js +1 -0
  15. package/dist/commands/handover.d.ts +15 -0
  16. package/dist/commands/handover.js +376 -0
  17. package/dist/commands/heavy-run.d.ts +59 -0
  18. package/dist/commands/heavy-run.js +247 -0
  19. package/dist/commands/heavy.d.ts +7 -0
  20. package/dist/commands/heavy.js +123 -0
  21. package/dist/commands/link.d.ts +12 -0
  22. package/dist/commands/link.js +121 -0
  23. package/dist/commands/ls.d.ts +5 -0
  24. package/dist/commands/ls.js +9 -1
  25. package/dist/commands/public-code-manage.d.ts +24 -0
  26. package/dist/commands/public-code-manage.js +217 -0
  27. package/dist/commands/public-code.d.ts +12 -1
  28. package/dist/commands/public-code.js +140 -45
  29. package/dist/commands/push-heavy.d.ts +6 -0
  30. package/dist/commands/push-heavy.js +128 -0
  31. package/dist/commands/push-tree.d.ts +24 -0
  32. package/dist/commands/push-tree.js +91 -0
  33. package/dist/commands/push.d.ts +4 -13
  34. package/dist/commands/push.js +7 -77
  35. package/dist/commands/put-heavy.d.ts +9 -0
  36. package/dist/commands/put-heavy.js +120 -0
  37. package/dist/commands/put-payer.d.ts +21 -0
  38. package/dist/commands/put-payer.js +24 -0
  39. package/dist/commands/put.d.ts +2 -2
  40. package/dist/commands/put.js +8 -7
  41. package/dist/commands/receive.js +14 -8
  42. package/dist/commands/s3.js +44 -10
  43. package/dist/commands/share-dispatch.js +14 -1
  44. package/dist/commands/share.d.ts +2 -0
  45. package/dist/commands/share.js +45 -35
  46. package/dist/commands/shares-sent.js +8 -3
  47. package/dist/crypto-surface.d.ts +35 -0
  48. package/dist/crypto-surface.js +13 -0
  49. package/dist/crypto.d.ts +8 -0
  50. package/dist/crypto.js +8 -0
  51. package/dist/download-part.d.ts +32 -1
  52. package/dist/download-part.js +95 -6
  53. package/dist/download-range.d.ts +19 -0
  54. package/dist/download-range.js +108 -0
  55. package/dist/download-sink.d.ts +15 -0
  56. package/dist/download.d.ts +14 -0
  57. package/dist/download.js +19 -4
  58. package/dist/handover-codes.d.ts +25 -0
  59. package/dist/handover-codes.js +106 -0
  60. package/dist/handover.d.ts +62 -0
  61. package/dist/handover.js +217 -0
  62. package/dist/heavy-api.d.ts +36 -0
  63. package/dist/heavy-api.js +107 -0
  64. package/dist/heavy-copy.d.ts +72 -0
  65. package/dist/heavy-copy.js +108 -0
  66. package/dist/heavy-evm.d.ts +20 -0
  67. package/dist/heavy-evm.js +106 -0
  68. package/dist/heavy-self-pay.d.ts +92 -0
  69. package/dist/heavy-self-pay.js +167 -0
  70. package/dist/heavy-upload.d.ts +87 -0
  71. package/dist/heavy-upload.js +154 -0
  72. package/dist/heavy-wallet-pay.d.ts +39 -0
  73. package/dist/heavy-wallet-pay.js +73 -0
  74. package/dist/help-options.js +13 -4
  75. package/dist/help.js +24 -3
  76. package/dist/links.d.ts +58 -0
  77. package/dist/links.js +206 -0
  78. package/dist/list-tier.d.ts +7 -0
  79. package/dist/list-tier.js +47 -0
  80. package/dist/main.js +13 -10
  81. package/dist/manifest-write.d.ts +4 -3
  82. package/dist/mcp-tools/reads.js +20 -6
  83. package/dist/mcp-tools/share.js +47 -1
  84. package/dist/mcp-tools/tiers.js +16 -1
  85. package/dist/platform-sign.d.ts +4 -4
  86. package/dist/platform-sign.js +3 -3
  87. package/dist/portable.d.ts +23 -0
  88. package/dist/portable.js +19 -0
  89. package/dist/product.d.ts +1 -1
  90. package/dist/product.js +1 -1
  91. package/dist/public-code-activity.d.ts +35 -0
  92. package/dist/public-code-activity.js +88 -0
  93. package/dist/public-code-refusals.d.ts +25 -0
  94. package/dist/public-code-refusals.js +87 -0
  95. package/dist/public-codes.d.ts +76 -0
  96. package/dist/public-codes.js +182 -0
  97. package/dist/recovery-build.js +16 -0
  98. package/dist/recovery-map-part.d.ts +82 -0
  99. package/dist/recovery-map-part.js +83 -0
  100. package/dist/recovery-map.d.ts +5 -40
  101. package/dist/recovery-map.js +18 -1
  102. package/dist/recovery-source.d.ts +3 -0
  103. package/dist/recovery-source.js +9 -0
  104. package/dist/risk.d.ts +55 -1
  105. package/dist/risk.js +55 -3
  106. package/dist/run-log.d.ts +1 -1
  107. package/dist/run-log.js +7 -1
  108. package/dist/s3/address.d.ts +20 -0
  109. package/dist/s3/address.js +97 -0
  110. package/dist/s3/answer.d.ts +48 -0
  111. package/dist/s3/answer.js +330 -0
  112. package/dist/s3/body-deframe.d.ts +49 -0
  113. package/dist/s3/body-deframe.js +264 -0
  114. package/dist/s3/body-measure.d.ts +13 -0
  115. package/dist/s3/body-measure.js +48 -0
  116. package/dist/s3/body-plan.d.ts +31 -0
  117. package/dist/s3/body-plan.js +159 -0
  118. package/dist/s3/body.d.ts +46 -0
  119. package/dist/s3/body.js +117 -0
  120. package/dist/s3/call.d.ts +36 -0
  121. package/dist/s3/call.js +42 -0
  122. package/dist/s3/checksum.d.ts +26 -0
  123. package/dist/s3/checksum.js +176 -0
  124. package/dist/s3/content-type.d.ts +3 -0
  125. package/dist/s3/content-type.js +49 -0
  126. package/dist/s3/contract.d.ts +91 -14
  127. package/dist/s3/drive-conditions.d.ts +47 -0
  128. package/dist/s3/drive-conditions.js +134 -0
  129. package/dist/s3/drive-key.d.ts +11 -0
  130. package/dist/s3/drive-key.js +58 -0
  131. package/dist/s3/drive-limits.d.ts +33 -0
  132. package/dist/s3/drive-limits.js +53 -0
  133. package/dist/s3/drive-lock.d.ts +11 -0
  134. package/dist/s3/drive-lock.js +47 -0
  135. package/dist/s3/drive-trashed-name.d.ts +13 -0
  136. package/dist/s3/drive-trashed-name.js +59 -0
  137. package/dist/s3/drive-write.d.ts +38 -0
  138. package/dist/s3/drive-write.js +258 -0
  139. package/dist/s3/drive.d.ts +59 -25
  140. package/dist/s3/drive.js +46 -77
  141. package/dist/s3/listing.d.ts +27 -1
  142. package/dist/s3/listing.js +62 -20
  143. package/dist/s3/long-answer.d.ts +12 -0
  144. package/dist/s3/long-answer.js +104 -0
  145. package/dist/s3/multipart-assemble.d.ts +17 -0
  146. package/dist/s3/multipart-assemble.js +68 -0
  147. package/dist/s3/multipart-sweep.d.ts +15 -0
  148. package/dist/s3/multipart-sweep.js +87 -0
  149. package/dist/s3/multipart-view.d.ts +45 -0
  150. package/dist/s3/multipart-view.js +232 -0
  151. package/dist/s3/multipart.d.ts +26 -19
  152. package/dist/s3/multipart.js +193 -78
  153. package/dist/s3/object-read.d.ts +17 -0
  154. package/dist/s3/object-read.js +127 -0
  155. package/dist/s3/object-write.d.ts +35 -0
  156. package/dist/s3/object-write.js +198 -0
  157. package/dist/s3/range.d.ts +14 -0
  158. package/dist/s3/range.js +80 -0
  159. package/dist/s3/response-sink.d.ts +12 -2
  160. package/dist/s3/response-sink.js +71 -7
  161. package/dist/s3/routes.d.ts +0 -1
  162. package/dist/s3/routes.js +242 -217
  163. package/dist/s3/same-file.d.ts +4 -1
  164. package/dist/s3/same-file.js +12 -3
  165. package/dist/s3/server.d.ts +54 -4
  166. package/dist/s3/server.js +174 -14
  167. package/dist/s3/sigv4-canonical.d.ts +91 -0
  168. package/dist/s3/sigv4-canonical.js +148 -0
  169. package/dist/s3/sigv4-presigned.d.ts +13 -0
  170. package/dist/s3/sigv4-presigned.js +82 -0
  171. package/dist/s3/sigv4.d.ts +26 -27
  172. package/dist/s3/sigv4.js +137 -102
  173. package/dist/s3/spool.d.ts +37 -0
  174. package/dist/s3/spool.js +104 -0
  175. package/dist/s3/staging.d.ts +90 -7
  176. package/dist/s3/staging.js +97 -66
  177. package/dist/s3/xml-multipart.d.ts +38 -0
  178. package/dist/s3/xml-multipart.js +47 -0
  179. package/dist/s3/xml-read.d.ts +28 -0
  180. package/dist/s3/xml-read.js +235 -0
  181. package/dist/s3/xml.d.ts +45 -2
  182. package/dist/s3/xml.js +72 -13
  183. package/dist/s3-gateway.d.ts +11 -5
  184. package/dist/s3-gateway.js +5 -2
  185. package/dist/seal.d.ts +1 -1
  186. package/dist/share-codes.d.ts +40 -0
  187. package/dist/share-codes.js +151 -0
  188. package/dist/share.d.ts +39 -0
  189. package/dist/share.js +54 -13
  190. package/dist/shared/lib/api/types-heavy.d.ts +167 -0
  191. package/dist/shared/lib/api/types-heavy.js +60 -0
  192. package/dist/shared/lib/drive/rebuild-verify.js +1 -0
  193. package/dist/shared/lib/filecoin/providers.d.ts +25 -0
  194. package/dist/shared/lib/filecoin/providers.js +58 -0
  195. package/dist/shared/lib/heavy/download.d.ts +48 -0
  196. package/dist/shared/lib/heavy/download.js +116 -0
  197. package/dist/shared/lib/heavy/order-plan.d.ts +77 -0
  198. package/dist/shared/lib/heavy/order-plan.js +83 -0
  199. package/dist/shared/lib/heavy/order-runner.d.ts +9 -0
  200. package/dist/shared/lib/heavy/order-runner.js +300 -0
  201. package/dist/shared/lib/heavy/order-types.d.ts +90 -0
  202. package/dist/shared/lib/heavy/order-types.js +6 -0
  203. package/dist/shared/lib/heavy/order-wire.d.ts +55 -0
  204. package/dist/shared/lib/heavy/order-wire.js +113 -0
  205. package/dist/shared/lib/heavy/piece-cid.d.ts +24 -0
  206. package/dist/shared/lib/heavy/piece-cid.js +55 -0
  207. package/dist/shared/lib/heavy/sp-upload.d.ts +37 -0
  208. package/dist/shared/lib/heavy/sp-upload.js +112 -0
  209. package/dist/shared/lib/heavy/synapse-piece.d.ts +1 -0
  210. package/dist/shared/lib/heavy/synapse-piece.js +8 -0
  211. package/dist/shared/lib/share/handover-format.d.ts +129 -0
  212. package/dist/shared/lib/share/handover-format.js +323 -0
  213. package/dist/shared/lib/share/public-link.d.ts +41 -0
  214. package/dist/shared/lib/share/public-link.js +107 -0
  215. package/dist/shared/lib/share/shared-file-info.js +1 -0
  216. package/dist/shared/lib/storage-network.d.ts +4 -4
  217. package/dist/shared/lib/storage-network.js +7 -7
  218. package/dist/shared/lib/storage-tier.d.ts +28 -0
  219. package/dist/shared/lib/storage-tier.js +63 -0
  220. package/dist/shared/lib/wallet/send-rules.js +1 -0
  221. package/dist/upload-file.d.ts +8 -0
  222. package/dist/upload-file.js +57 -25
  223. package/dist/upload-price-node.d.ts +3 -0
  224. package/dist/upload-price-node.js +3 -6
  225. package/dist/upload-price.d.ts +10 -0
  226. package/dist/upload-price.js +13 -1
  227. package/dist/upload-run.d.ts +80 -0
  228. package/dist/upload-run.js +173 -0
  229. package/dist/upload-steps.js +15 -4
  230. package/dist/upload-store.d.ts +15 -6
  231. package/dist/upload-store.js +6 -0
  232. package/dist/upload-wallet-put.d.ts +9 -4
  233. package/dist/upload-wallet-put.js +16 -6
  234. package/dist/upload-wallet.js +4 -0
  235. package/dist/upload-wire.d.ts +25 -0
  236. package/dist/upload-wire.js +20 -1
  237. package/dist/upload.js +28 -13
  238. package/dist/wallet-sign.js +2 -2
  239. package/docs/commands/handover.md +54 -0
  240. package/docs/commands/heavy.md +35 -0
  241. package/docs/commands/link.md +27 -0
  242. package/docs/commands/mcp.md +2 -1
  243. package/docs/commands/public-code.md +25 -6
  244. package/docs/commands/push.md +4 -1
  245. package/docs/commands/put.md +15 -1
  246. package/docs/commands/share.md +8 -3
  247. package/package.json +5 -2
  248. package/server.json +2 -2
  249. package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +106 -4
  250. package/vendor/nmts-crypto/nmts_crypto_wasm.js +286 -3
  251. package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
  252. package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +12 -1
@@ -0,0 +1,117 @@
1
+ // The bytes of an upload as the object will hold them: whatever framing the client wrapped them in
2
+ // taken off, and every integrity promise the request made checked before anything is stored.
3
+ //
4
+ // ⛔ ONE PLACE FOR EVERY WAY A BODY CAN ARRIVE. A plain body with a hex digest, `UNSIGNED-PAYLOAD`,
5
+ // a presigned URL, and the three `aws-chunked` forms (chunk-signed, chunk-signed with a trailer,
6
+ // unsigned with a trailer) all end here and leave as the same shape. The protocol layer never
7
+ // reads `content-encoding` or a trailer itself; a second reader would be a second place for the
8
+ // framing to be stored as somebody's file. `body-plan.ts` (what the headers promise),
9
+ // `body-deframe.ts` (the framing and its signatures) and `body-measure.ts` (digests and
10
+ // checksums) are this module's parts, and nothing else imports them.
11
+ //
12
+ // ⛔ `verified` IS THE GATE, NOT A REPORT. The stream can be spooled while it arrives, but nothing
13
+ // may be stored until `verified` resolves: a digest, a chunk signature or a checksum can only be
14
+ // judged once the last byte is in.
15
+ //
16
+ // ⛔ THE STREAM NEVER FAILS; `verified` DOES. When the body turns out to be wrong halfway, `verified`
17
+ // rejects first and the stream then simply ends, early. A caller that spools and awaits
18
+ // `verified` alongside therefore always sees the refusal with its S3 status and code, instead of
19
+ // a stream error it would have to translate -- or, worse, a short file and a success.
20
+ import { PassThrough } from "node:stream";
21
+ import { Deframer } from "./body-deframe.js";
22
+ import { Measure } from "./body-measure.js";
23
+ import { BodyRefusal, incomplete, planOf } from "./body-plan.js";
24
+ export { BodyRefusal } from "./body-plan.js";
25
+ /**
26
+ * Decode one request body, or say at once why it cannot be.
27
+ *
28
+ * ⚠ THE STREAM MUST BE READ. It is fed as the request arrives and pauses the request when nobody
29
+ * reads it, so a caller that awaits `verified` without consuming `stream` waits forever.
30
+ */
31
+ export function decodeBody(req, verdict, options = {}) {
32
+ const plan = planOf(req, verdict, options.checksumsDescribeObject === true);
33
+ if (plan instanceof BodyRefusal)
34
+ return plan;
35
+ const held = new Map();
36
+ const out = new PassThrough();
37
+ const measure = new Measure(plan);
38
+ let settled = false;
39
+ let accept = () => undefined;
40
+ let reject = () => undefined;
41
+ const verified = new Promise((resolve, fail) => {
42
+ accept = resolve;
43
+ reject = fail;
44
+ });
45
+ // ⚠ Whoever awaits `verified` still sees the refusal; this only keeps an unawaited one -- a caller
46
+ // that gave up on the upload already -- from being reported as an unhandled rejection.
47
+ verified.catch(() => undefined);
48
+ const refuse = (refusal) => {
49
+ if (settled)
50
+ return;
51
+ settled = true;
52
+ reject(refusal);
53
+ if (!out.destroyed && !out.writableEnded)
54
+ out.end();
55
+ // The rest of the request is read and dropped, so the refusal can still be answered on this
56
+ // connection rather than the client seeing it reset mid-upload.
57
+ req.resume();
58
+ };
59
+ const emit = (bytes) => {
60
+ measure.update(bytes);
61
+ if (out.destroyed)
62
+ return;
63
+ if (!out.write(bytes))
64
+ req.pause();
65
+ };
66
+ out.on("drain", () => {
67
+ if (!settled)
68
+ req.resume();
69
+ });
70
+ out.on("close", () => {
71
+ if (!settled)
72
+ refuse(incomplete("the body's reader stopped before the end"));
73
+ });
74
+ const deframer = plan.framing === "plain" ? null : new Deframer(plan, emit);
75
+ req.on("data", (chunk) => {
76
+ if (settled)
77
+ return;
78
+ if (deframer === null) {
79
+ if (measure.count + chunk.length > plan.size) {
80
+ refuse(incomplete("the body is longer than its declared length"));
81
+ return;
82
+ }
83
+ emit(chunk);
84
+ return;
85
+ }
86
+ const refused = deframer.feed(chunk);
87
+ if (refused !== null)
88
+ refuse(refused);
89
+ });
90
+ req.on("end", () => {
91
+ if (settled)
92
+ return;
93
+ const refused = deframer?.finish() ?? null;
94
+ const trailerValues = deframer?.trailerValues ?? new Map();
95
+ const verdictOnBytes = refused ?? measure.judge(plan, trailerValues);
96
+ if (verdictOnBytes !== null) {
97
+ refuse(verdictOnBytes);
98
+ return;
99
+ }
100
+ for (const [algorithm, value] of plan.headerChecksums)
101
+ held.set(algorithm, value);
102
+ for (const algorithm of plan.trailers) {
103
+ const value = trailerValues.get(algorithm);
104
+ if (value !== undefined)
105
+ held.set(algorithm, value);
106
+ }
107
+ settled = true;
108
+ accept();
109
+ out.end();
110
+ });
111
+ req.on("error", () => refuse(incomplete("the connection closed before the body was complete")));
112
+ req.on("close", () => {
113
+ if (!settled)
114
+ refuse(incomplete("the connection closed before the body was complete"));
115
+ });
116
+ return { stream: out, size: plan.size, verified, checksums: () => held };
117
+ }
@@ -0,0 +1,36 @@
1
+ import type { IncomingMessage, ServerResponse } from "node:http";
2
+ import type { DriveSource, GatewayOptions, WriteMeta } from "./contract.ts";
3
+ import type { GatewayCredential, VerifiedAgainst } from "./sigv4.ts";
4
+ export interface Call {
5
+ readonly req: IncomingMessage;
6
+ readonly res: ServerResponse;
7
+ readonly method: string;
8
+ readonly query: URLSearchParams;
9
+ readonly bucket: string;
10
+ readonly key: string;
11
+ /** The path as it arrived, which is what an error document names as its resource. */
12
+ readonly resource: string;
13
+ readonly verdict: Extract<VerifiedAgainst, {
14
+ ok: true;
15
+ }>;
16
+ readonly options: GatewayOptions;
17
+ readonly source: DriveSource;
18
+ }
19
+ /**
20
+ * What a caller signed with a pair it may not use here.
21
+ *
22
+ * ⛔ THE SAME ANSWER WHETHER OR NOT THE BUCKET EXISTS, which is why the restriction is checked
23
+ * before the resolver is asked. Answering `NoSuchBucket` for a name the caller may not touch
24
+ * would turn this gateway into a way of asking "does this business have a customer called…",
25
+ * one guess at a time.
26
+ */
27
+ export declare const NOT_YOURS = "That access key may not use that bucket.";
28
+ /** Whether the pair that signed is allowed anywhere near this bucket name. */
29
+ export declare function mayTouch(credential: GatewayCredential, bucket: string): boolean;
30
+ export declare function headerOf(req: IncomingMessage, name: string): string | undefined;
31
+ /** What the client said about the object it is writing, as the drive is handed it. */
32
+ export declare function metaOf(req: IncomingMessage): WriteMeta;
33
+ /** The one sentence a write gets from `nmts s3` when this machine has not agreed to spending. */
34
+ export declare function readOnlyOnThisMachine(): string;
35
+ /** Why this drive takes no writes, in the words of whoever runs the gateway. */
36
+ export declare function readOnlyBecause(options: GatewayOptions): string;
@@ -0,0 +1,42 @@
1
+ // One request as the operations see it: already signed for, already addressed, its drive resolved.
2
+ //
3
+ // ⚠ EVERYTHING HERE HAS BEEN DECIDED BY `routes.ts` BEFORE AN OPERATION RUNS — who signed, which
4
+ // bucket and key, and that the pair may touch that bucket. An operation that needs a second
5
+ // bucket (a copy's source) asks `mayTouch` itself, and asks it before the resolver, for the
6
+ // reason `NOT_YOURS` gives.
7
+ /**
8
+ * What a caller signed with a pair it may not use here.
9
+ *
10
+ * ⛔ THE SAME ANSWER WHETHER OR NOT THE BUCKET EXISTS, which is why the restriction is checked
11
+ * before the resolver is asked. Answering `NoSuchBucket` for a name the caller may not touch
12
+ * would turn this gateway into a way of asking "does this business have a customer called…",
13
+ * one guess at a time.
14
+ */
15
+ export const NOT_YOURS = "That access key may not use that bucket.";
16
+ /** Whether the pair that signed is allowed anywhere near this bucket name. */
17
+ export function mayTouch(credential, bucket) {
18
+ const only = credential.buckets;
19
+ return only === undefined || only.includes(bucket);
20
+ }
21
+ export function headerOf(req, name) {
22
+ const raw = req.headers[name];
23
+ return Array.isArray(raw) ? raw.join(",") : raw;
24
+ }
25
+ /** What the client said about the object it is writing, as the drive is handed it. */
26
+ export function metaOf(req) {
27
+ const storageClass = headerOf(req, "x-amz-storage-class")?.trim().toUpperCase();
28
+ return {
29
+ storageClass: storageClass === undefined || storageClass === "" ? null : storageClass,
30
+ contentType: headerOf(req, "content-type") ?? null,
31
+ };
32
+ }
33
+ /** The one sentence a write gets from `nmts s3` when this machine has not agreed to spending. */
34
+ export function readOnlyOnThisMachine() {
35
+ return ("This gateway is read only. Uploading spends credits, and this machine has not agreed to " +
36
+ "spending — `nmts consent grant spend`, run by the person whose account this is, is what " +
37
+ "changes that. Nothing was written.");
38
+ }
39
+ /** Why this drive takes no writes, in the words of whoever runs the gateway. */
40
+ export function readOnlyBecause(options) {
41
+ return options.readOnlyBecause ?? readOnlyOnThisMachine();
42
+ }
@@ -0,0 +1,26 @@
1
+ export type ChecksumAlgorithm = "crc32" | "crc32c" | "crc64nvme" | "sha1" | "sha256";
2
+ export declare const CHECKSUM_ALGORITHMS: readonly ChecksumAlgorithm[];
3
+ /** `crc32c` → `x-amz-checksum-crc32c`. */
4
+ export declare function checksumHeaderName(algorithm: ChecksumAlgorithm): string;
5
+ /** `x-amz-checksum-crc32c` → `crc32c`; anything else → null. Case does not matter. */
6
+ export declare function algorithmOfHeader(name: string): ChecksumAlgorithm | null;
7
+ /** `CRC32C` (as `x-amz-sdk-checksum-algorithm` spells it) → `crc32c`; anything else → null. */
8
+ export declare function algorithmOfName(name: string): ChecksumAlgorithm | null;
9
+ /**
10
+ * The bytes a checksum header's value stands for, or null when it is not canonical base64 of the
11
+ * right length for its algorithm.
12
+ *
13
+ * ⚠ `Buffer.from(…, "base64")` accepts almost anything and silently drops what it cannot read, so
14
+ * the shape is checked first: a value that decodes "leniently" to the right length is still not
15
+ * a value a client computed.
16
+ */
17
+ export declare function parseChecksumValue(algorithm: ChecksumAlgorithm, value: string): Buffer | null;
18
+ /** A checksum being computed while bytes stream past. */
19
+ export interface RunningChecksum {
20
+ update(bytes: Uint8Array): void;
21
+ /** The finished value, big-endian for the CRCs, as S3 encodes it before base64. */
22
+ digest(): Buffer;
23
+ }
24
+ export declare function runningChecksum(algorithm: ChecksumAlgorithm): RunningChecksum;
25
+ /** The whole value for some bytes, base64 as it travels in a header or a trailer. */
26
+ export declare function checksumOf(algorithm: ChecksumAlgorithm, bytes: Uint8Array): string;
@@ -0,0 +1,176 @@
1
+ // The object checksums an S3 client may attach to an upload, computed over the bytes as they pass.
2
+ //
3
+ // ⛔ THE FIVE S3 NAMES, AND NOTHING ELSE. `x-amz-checksum-crc32`, `-crc32c`, `-crc64nvme`, `-sha1`
4
+ // and `-sha256` are the whole list S3 accepts; a name outside it is refused by the caller rather
5
+ // than ignored, because an ignored checksum is a promise the client thinks was kept.
6
+ //
7
+ // ⚠ TWO OF THEM ARE TABLE-DRIVEN HERE BECAUSE NODE HAS NEITHER. CRC32 is `node:zlib`'s and the two
8
+ // digests are `node:crypto`'s. CRC32C (Castagnoli) and CRC64NVME are plain table CRCs over their
9
+ // published parameters; they check integrity, not authenticity, and nothing secret goes through
10
+ // them. The tests hold both to the parameter sets' published check values and to values a real
11
+ // AWS SDK computed.
12
+ import { createHash } from "node:crypto";
13
+ import { crc32 } from "node:zlib";
14
+ export const CHECKSUM_ALGORITHMS = ["crc32", "crc32c", "crc64nvme", "sha1", "sha256"];
15
+ /** How many bytes each algorithm's value has once its base64 is decoded. */
16
+ const DIGEST_BYTES = {
17
+ crc32: 4,
18
+ crc32c: 4,
19
+ crc64nvme: 8,
20
+ sha1: 20,
21
+ sha256: 32,
22
+ };
23
+ /** `crc32c` → `x-amz-checksum-crc32c`. */
24
+ export function checksumHeaderName(algorithm) {
25
+ return `x-amz-checksum-${algorithm}`;
26
+ }
27
+ /** `x-amz-checksum-crc32c` → `crc32c`; anything else → null. Case does not matter. */
28
+ export function algorithmOfHeader(name) {
29
+ const lower = name.toLowerCase();
30
+ for (const algorithm of CHECKSUM_ALGORITHMS) {
31
+ if (lower === checksumHeaderName(algorithm))
32
+ return algorithm;
33
+ }
34
+ return null;
35
+ }
36
+ /** `CRC32C` (as `x-amz-sdk-checksum-algorithm` spells it) → `crc32c`; anything else → null. */
37
+ export function algorithmOfName(name) {
38
+ const lower = name.trim().toLowerCase();
39
+ for (const algorithm of CHECKSUM_ALGORITHMS) {
40
+ if (lower === algorithm)
41
+ return algorithm;
42
+ }
43
+ return null;
44
+ }
45
+ /**
46
+ * The bytes a checksum header's value stands for, or null when it is not canonical base64 of the
47
+ * right length for its algorithm.
48
+ *
49
+ * ⚠ `Buffer.from(…, "base64")` accepts almost anything and silently drops what it cannot read, so
50
+ * the shape is checked first: a value that decodes "leniently" to the right length is still not
51
+ * a value a client computed.
52
+ */
53
+ export function parseChecksumValue(algorithm, value) {
54
+ const trimmed = value.trim();
55
+ if (!/^[A-Za-z0-9+/]*={0,2}$/.test(trimmed) || trimmed.length % 4 !== 0)
56
+ return null;
57
+ const bytes = Buffer.from(trimmed, "base64");
58
+ if (bytes.length !== DIGEST_BYTES[algorithm])
59
+ return null;
60
+ if (bytes.toString("base64") !== trimmed)
61
+ return null;
62
+ return bytes;
63
+ }
64
+ function table32(polynomial) {
65
+ const table = new Uint32Array(256);
66
+ for (let n = 0; n < 256; n++) {
67
+ let c = n;
68
+ for (let k = 0; k < 8; k++)
69
+ c = c & 1 ? (c >>> 1) ^ polynomial : c >>> 1;
70
+ table[n] = c >>> 0;
71
+ }
72
+ return table;
73
+ }
74
+ /** CRC-32C (Castagnoli): reflected, polynomial 0x82F63B78, init and final XOR all ones. */
75
+ const CRC32C_TABLE = table32(0x82f63b78);
76
+ /**
77
+ * CRC-64/NVME: reflected, polynomial 0x9A6C9329AC4BC9B5 (0xAD93D23594C93659 unreflected), init and
78
+ * final XOR all ones. Kept as two 32-bit halves so no BigInt is made per byte.
79
+ */
80
+ const CRC64_POLY_HIGH = 0x9a6c9329;
81
+ const CRC64_POLY_LOW = 0xac4bc9b5;
82
+ const CRC64_TABLE_HIGH = new Uint32Array(256);
83
+ const CRC64_TABLE_LOW = new Uint32Array(256);
84
+ for (let n = 0; n < 256; n++) {
85
+ let high = 0;
86
+ let low = n;
87
+ for (let k = 0; k < 8; k++) {
88
+ const carry = low & 1;
89
+ low = ((low >>> 1) | ((high & 1) << 31)) >>> 0;
90
+ high = high >>> 1;
91
+ if (carry === 1) {
92
+ high = (high ^ CRC64_POLY_HIGH) >>> 0;
93
+ low = (low ^ CRC64_POLY_LOW) >>> 0;
94
+ }
95
+ }
96
+ CRC64_TABLE_HIGH[n] = high;
97
+ CRC64_TABLE_LOW[n] = low;
98
+ }
99
+ class Crc32 {
100
+ value = 0;
101
+ update(bytes) {
102
+ this.value = crc32(bytes, this.value);
103
+ }
104
+ digest() {
105
+ const out = Buffer.alloc(4);
106
+ out.writeUInt32BE(this.value >>> 0);
107
+ return out;
108
+ }
109
+ }
110
+ class Crc32c {
111
+ value = 0xffffffff;
112
+ update(bytes) {
113
+ let c = this.value;
114
+ for (let i = 0; i < bytes.length; i++) {
115
+ c = (CRC32C_TABLE[(c ^ (bytes[i] ?? 0)) & 0xff] ?? 0) ^ (c >>> 8);
116
+ }
117
+ this.value = c >>> 0;
118
+ }
119
+ digest() {
120
+ const out = Buffer.alloc(4);
121
+ out.writeUInt32BE((this.value ^ 0xffffffff) >>> 0);
122
+ return out;
123
+ }
124
+ }
125
+ class Crc64Nvme {
126
+ high = 0xffffffff;
127
+ low = 0xffffffff;
128
+ update(bytes) {
129
+ let high = this.high;
130
+ let low = this.low;
131
+ for (let i = 0; i < bytes.length; i++) {
132
+ const index = (low ^ (bytes[i] ?? 0)) & 0xff;
133
+ low = ((CRC64_TABLE_LOW[index] ?? 0) ^ ((low >>> 8) | (high << 24))) >>> 0;
134
+ high = ((CRC64_TABLE_HIGH[index] ?? 0) ^ (high >>> 8)) >>> 0;
135
+ }
136
+ this.high = high;
137
+ this.low = low;
138
+ }
139
+ digest() {
140
+ const out = Buffer.alloc(8);
141
+ out.writeUInt32BE((this.high ^ 0xffffffff) >>> 0, 0);
142
+ out.writeUInt32BE((this.low ^ 0xffffffff) >>> 0, 4);
143
+ return out;
144
+ }
145
+ }
146
+ class Digest {
147
+ hash;
148
+ constructor(algorithm) {
149
+ this.hash = createHash(algorithm);
150
+ }
151
+ update(bytes) {
152
+ this.hash.update(bytes);
153
+ }
154
+ digest() {
155
+ return this.hash.digest();
156
+ }
157
+ }
158
+ export function runningChecksum(algorithm) {
159
+ switch (algorithm) {
160
+ case "crc32":
161
+ return new Crc32();
162
+ case "crc32c":
163
+ return new Crc32c();
164
+ case "crc64nvme":
165
+ return new Crc64Nvme();
166
+ case "sha1":
167
+ case "sha256":
168
+ return new Digest(algorithm);
169
+ }
170
+ }
171
+ /** The whole value for some bytes, base64 as it travels in a header or a trailer. */
172
+ export function checksumOf(algorithm, bytes) {
173
+ const running = runningChecksum(algorithm);
174
+ running.update(bytes);
175
+ return running.digest().toString("base64");
176
+ }
@@ -0,0 +1,3 @@
1
+ export declare const UNKNOWN_TYPE = "application/octet-stream";
2
+ /** `photos/a.JPG` → `image/jpeg`. The extension is what follows the last dot of the last segment. */
3
+ export declare function contentTypeOf(key: string): string;
@@ -0,0 +1,49 @@
1
+ // The `Content-Type` a GET answers, guessed from the key.
2
+ //
3
+ // ⚠ GUESSED BECAUSE IT IS NOT KEPT. The file list holds a name, a size and two times, and nothing a
4
+ // client sent beside the bytes. A type from the extension is what a browser loading an image
5
+ // through a presigned link, or a framework serving a stylesheet, needs to see; a file whose
6
+ // extension is not below is `application/octet-stream`, which every client treats as bytes.
7
+ /**
8
+ * ⛔ A MAP, NOT AN OBJECT LITERAL. Looked up in a plain object, `a.constructor` found the function
9
+ * every object inherits and answered it as the file's type.
10
+ */
11
+ const BY_EXTENSION = new Map([
12
+ ["html", "text/html; charset=utf-8"],
13
+ ["css", "text/css; charset=utf-8"],
14
+ ["js", "text/javascript; charset=utf-8"],
15
+ ["mjs", "text/javascript; charset=utf-8"],
16
+ ["json", "application/json"],
17
+ ["txt", "text/plain; charset=utf-8"],
18
+ ["csv", "text/csv; charset=utf-8"],
19
+ ["xml", "application/xml"],
20
+ ["svg", "image/svg+xml"],
21
+ ["png", "image/png"],
22
+ ["jpg", "image/jpeg"],
23
+ ["jpeg", "image/jpeg"],
24
+ ["gif", "image/gif"],
25
+ ["webp", "image/webp"],
26
+ ["avif", "image/avif"],
27
+ ["ico", "image/x-icon"],
28
+ ["pdf", "application/pdf"],
29
+ ["zip", "application/zip"],
30
+ ["gz", "application/gzip"],
31
+ ["tar", "application/x-tar"],
32
+ ["mp4", "video/mp4"],
33
+ ["webm", "video/webm"],
34
+ ["mp3", "audio/mpeg"],
35
+ ["wav", "audio/wav"],
36
+ ["ogg", "audio/ogg"],
37
+ ["wasm", "application/wasm"],
38
+ ["woff", "font/woff"],
39
+ ["woff2", "font/woff2"],
40
+ ]);
41
+ export const UNKNOWN_TYPE = "application/octet-stream";
42
+ /** `photos/a.JPG` → `image/jpeg`. The extension is what follows the last dot of the last segment. */
43
+ export function contentTypeOf(key) {
44
+ const name = key.slice(key.lastIndexOf("/") + 1);
45
+ const dot = name.lastIndexOf(".");
46
+ if (dot <= 0)
47
+ return UNKNOWN_TYPE;
48
+ return BY_EXTENSION.get(name.slice(dot + 1).toLowerCase()) ?? UNKNOWN_TYPE;
49
+ }
@@ -1,11 +1,54 @@
1
- import type { Readable } from "node:stream";
2
1
  import type { PlaintextSink } from "../download-sink.ts";
3
2
  import type { ManifestEntry } from "../shared/lib/drive/manifest-codec.ts";
3
+ import type { DecodedBody } from "./body.ts";
4
+ import type { WriteCondition } from "./drive-conditions.ts";
4
5
  import type { DriveObject } from "./listing.ts";
5
6
  import type { GatewayCredential } from "./sigv4.ts";
7
+ import type { Staging } from "./staging.ts";
8
+ /**
9
+ * What a client said about an object it is writing, beyond its bytes.
10
+ *
11
+ * ⚠ HANDED ON, NOT KEPT. The file list holds a name, a size and two times; this is what the client
12
+ * sent, passed to whoever stores the file so that it can act on it (a storage class, say). A GET
13
+ * later answers a type guessed from the key's extension, not this one.
14
+ */
15
+ export interface WriteMeta {
16
+ /** `x-amz-storage-class`, trimmed and upper-cased. Null when the client sent none. */
17
+ readonly storageClass: string | null;
18
+ /** `Content-Type` as the client sent it. Null when absent. */
19
+ readonly contentType: string | null;
20
+ }
21
+ /** What storing a file at a key came to. */
22
+ export interface StoreOutcome {
23
+ /** The tag a HEAD of this key answers afterwards. */
24
+ readonly etag: string;
25
+ /**
26
+ * `stored`: nothing was at the key. `replaced`: the new file is stored and the one that was at
27
+ * the key went to the trash. `unchanged`: exactly these bytes were already there, and nothing
28
+ * was sent.
29
+ */
30
+ readonly outcome: "stored" | "replaced" | "unchanged";
31
+ }
32
+ /** The file a copy reads from: the drive that holds it, and which of its files. */
33
+ export interface CopySource {
34
+ readonly source: DriveSource;
35
+ readonly object: DriveObject;
36
+ }
37
+ /** What sending one key to the trash came to: null when it went (or was already gone), the failure otherwise. */
38
+ export interface RemoveOutcome {
39
+ readonly key: string;
40
+ readonly error: unknown;
41
+ }
6
42
  export interface DriveSource {
7
- /** The account's live file list. Called per request; the caller decides what to cache. */
8
- entries(): Promise<readonly ManifestEntry[]>;
43
+ /**
44
+ * The account's live file list. Called per request; the caller decides what to cache.
45
+ *
46
+ * `fresh` asks for the list as the server holds it now, past any cache — what a decision that
47
+ * must not be made on a stale list (a condition, a copy's source) asks for.
48
+ */
49
+ entries(options?: {
50
+ readonly fresh?: boolean;
51
+ }): Promise<readonly ManifestEntry[]>;
9
52
  /**
10
53
  * Fetch, decrypt and deliver one file into the sink.
11
54
  *
@@ -25,22 +68,31 @@ export interface DriveSource {
25
68
  readonly write?: DriveWriter;
26
69
  }
27
70
  export interface DriveWriter {
28
- /** Store `body` at this key. `size` is the byte count the client declared. */
29
- put(key: string, body: Readable, size: number): Promise<void>;
30
- /** Send one file to the trash, where it stays recoverable for thirty days. */
31
- trash(object: DriveObject): Promise<void>;
71
+ /**
72
+ * Store a request body at this key.
73
+ *
74
+ * ⛔ NOTHING IS STORED UNTIL `body.verified` RESOLVES. The bytes may be spooled as they arrive,
75
+ * but a digest, a chunk signature or a checksum is only known once the last byte is in, and a
76
+ * body that fails one must not become somebody's file.
77
+ */
78
+ put(key: string, body: DecodedBody, meta: WriteMeta, condition?: WriteCondition): Promise<StoreOutcome>;
79
+ /** Store a copy of a file this gateway can already read — in this drive or another — at this key. */
80
+ copy(from: CopySource, key: string, meta: WriteMeta, condition?: WriteCondition): Promise<StoreOutcome>;
81
+ /**
82
+ * Send what is at these keys to the trash, where it stays recoverable for thirty days, in as few
83
+ * writes to the file list as the drive allows. Each key is answered on its own; a key that holds
84
+ * nothing — never did, or is in the trash already — is answered as gone, which is S3's answer.
85
+ */
86
+ remove(keys: readonly string[], condition?: WriteCondition): Promise<readonly RemoveOutcome[]>;
32
87
  /**
33
88
  * Staging for uploads that arrive in pieces. Absent means this gateway refuses them.
34
89
  *
35
90
  * ⚠ Separate from `put` because the pieces have to land somewhere before they are one file, and
36
91
  * where that is belongs to whoever is running this rather than to the protocol.
37
92
  */
38
- readonly multipart?: {
39
- begin(key: string): Promise<string>;
40
- part(uploadId: string, partNumber: number, body: Readable, size: number, expectedSha256: string | null): Promise<string>;
41
- complete(uploadId: string): Promise<string>;
42
- abort(uploadId: string): Promise<void>;
43
- };
93
+ readonly multipart?: Staging;
94
+ /** The most bytes one object may have here. Absent: no limit but the upload path's own. */
95
+ readonly maxObjectBytes?: number | undefined;
44
96
  }
45
97
  export interface GatewayOptions {
46
98
  /**
@@ -68,13 +120,38 @@ export interface GatewayOptions {
68
120
  * held to — and an unrestricted pair on such a gateway is told nothing, which is true.
69
121
  */
70
122
  readonly bucketNames?: () => readonly string[] | Promise<readonly string[]>;
71
- /** Called with one line whenever a request is answered, so a person can watch what a tool does. */
123
+ /**
124
+ * The host that virtual-hosted requests are addressed under, such as `s3.example.com`.
125
+ *
126
+ * When set, a request whose `Host` is `<bucket>.s3.example.com` (any port) names its bucket in
127
+ * the host and its key in the whole path — the form the AWS SDKs use unless told otherwise.
128
+ * Every other request is path style, `/<bucket>/<key>`, which is all there is when this is absent.
129
+ */
130
+ readonly virtualHostBase?: string | undefined;
131
+ /**
132
+ * Called with one line whenever a request is answered, so a person can watch what a tool does.
133
+ *
134
+ * ⛔ THE LINE NAMES THE OPERATION AND THE STATUS, NEVER A KEY OR A PREFIX. A key is a file's
135
+ * path, and a path is a file name — the thing this product keeps from the server it stores on.
136
+ */
72
137
  readonly log?: (line: string) => void;
73
138
  /** Passed in so a test can hold the clock still. */
74
139
  readonly now?: () => number;
140
+ /**
141
+ * How long `CompleteMultipartUpload`, `CopyObject` and `DeleteObjects` may work before their 200
142
+ * begins — work that ends sooner is answered with its own status — and then how often a space is
143
+ * written until it ends. `KEEP_ALIVE_MS` (10 seconds) unless a test needs it shorter.
144
+ */
145
+ readonly keepAliveMs?: number | undefined;
75
146
  /**
76
147
  * The sentence a write gets from a read-only drive. `nmts s3` says what a person runs on this
77
148
  * machine to allow spending; a gateway somebody else runs has a different way in, and says its own.
78
149
  */
79
150
  readonly readOnlyBecause?: string | undefined;
151
+ /**
152
+ * How many writes — uploads, parts, finishes, copies, deletes — run at once. One past it is
153
+ * answered 503 `SlowDown` with `Retry-After`, which every S3 client waits on and sends again.
154
+ * `MAX_CONCURRENT_WRITES` (16) unless a caller says.
155
+ */
156
+ readonly maxConcurrentWrites?: number | undefined;
80
157
  }
@@ -0,0 +1,47 @@
1
+ import type { IncomingMessage } from "node:http";
2
+ import type { DriveObject } from "./listing.ts";
3
+ /** What a write says the key must hold when it lands. */
4
+ export interface WriteCondition {
5
+ /** `If-Match`: the tag (or tags, or `*`) the key must hold. Null when absent. */
6
+ readonly ifMatch: string | null;
7
+ /** `If-None-Match: *`: the key must hold nothing. */
8
+ readonly ifNoneMatch: boolean;
9
+ }
10
+ export declare const UNCONDITIONAL: WriteCondition;
11
+ /** The conditions a copy puts on its SOURCE. Times are whole seconds, as an HTTP date carries. */
12
+ export interface CopySourceCondition {
13
+ readonly ifMatch: string | null;
14
+ readonly ifNoneMatch: string | null;
15
+ readonly ifModifiedSince: number | null;
16
+ readonly ifUnmodifiedSince: number | null;
17
+ }
18
+ /**
19
+ * Refuse a request that carries a condition, for an operation that honours none.
20
+ *
21
+ * ⚠ `UploadPart`, `CreateMultipartUpload`, `AbortMultipartUpload` and `DeleteObjects` take no
22
+ * conditions in S3 either; a client that sends one anyway is told so rather than answered as if
23
+ * it had held.
24
+ */
25
+ export declare function refuseConditions(req: IncomingMessage, operation: string): void;
26
+ /** What `If-Match` and `If-None-Match` ask of a write's destination. */
27
+ export declare function writeConditionOf(req: IncomingMessage, operation: string): WriteCondition;
28
+ /**
29
+ * What a `DeleteObject` asks: `If-Match` is honoured; the rest of S3's conditions on a delete are
30
+ * refused, since nothing here can judge them.
31
+ */
32
+ export declare function deleteConditionOf(req: IncomingMessage): WriteCondition;
33
+ /**
34
+ * Whether what stands at the key now meets the condition. Throws 412 when it does not, and 404 for
35
+ * `If-Match` on a key that holds nothing — S3's two answers.
36
+ */
37
+ export declare function checkWriteCondition(standing: DriveObject | undefined, condition: WriteCondition): void;
38
+ /** The four `x-amz-copy-source-if-*` headers. */
39
+ export declare function copySourceConditionOf(req: IncomingMessage): CopySourceCondition;
40
+ /**
41
+ * Whether a copy's source meets its conditions. Throws 412 when it does not.
42
+ *
43
+ * ⚠ S3'S PAIRINGS: a matching `if-match` copies whatever `if-unmodified-since` says, and a failing
44
+ * `if-none-match` refuses whatever `if-modified-since` says — each tag condition, when present,
45
+ * decides in place of its date.
46
+ */
47
+ export declare function checkCopySource(object: DriveObject, condition: CopySourceCondition): void;