@needmoretruth/nmts-cli 0.44.0 → 0.46.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 (248) hide show
  1. package/AGENTS.md +2 -2
  2. package/CHANGELOG.md +73 -0
  3. package/README.ko.md +6 -3
  4. package/README.md +15 -3
  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 +14 -0
  9. package/dist/args.js +11 -2
  10. package/dist/artifact-about.d.ts +1 -1
  11. package/dist/collision.d.ts +20 -3
  12. package/dist/collision.js +21 -3
  13. package/dist/commands/erase.js +1 -0
  14. package/dist/commands/get.d.ts +2 -0
  15. package/dist/commands/get.js +13 -1
  16. package/dist/commands/handover.d.ts +13 -0
  17. package/dist/commands/handover.js +328 -0
  18. package/dist/commands/heavy-run.d.ts +59 -0
  19. package/dist/commands/heavy-run.js +247 -0
  20. package/dist/commands/heavy.d.ts +7 -0
  21. package/dist/commands/heavy.js +123 -0
  22. package/dist/commands/ls.d.ts +7 -0
  23. package/dist/commands/ls.js +27 -6
  24. package/dist/commands/public-code.d.ts +6 -0
  25. package/dist/commands/public-code.js +46 -0
  26. package/dist/commands/push-heavy.d.ts +6 -0
  27. package/dist/commands/push-heavy.js +128 -0
  28. package/dist/commands/push-tree.d.ts +24 -0
  29. package/dist/commands/push-tree.js +91 -0
  30. package/dist/commands/push.d.ts +4 -13
  31. package/dist/commands/push.js +7 -77
  32. package/dist/commands/put-heavy.d.ts +9 -0
  33. package/dist/commands/put-heavy.js +120 -0
  34. package/dist/commands/put-payer.d.ts +21 -0
  35. package/dist/commands/put-payer.js +24 -0
  36. package/dist/commands/put-thumbnail.d.ts +4 -0
  37. package/dist/commands/put-thumbnail.js +119 -0
  38. package/dist/commands/put-wallet.d.ts +2 -0
  39. package/dist/commands/put-wallet.js +3 -2
  40. package/dist/commands/put.d.ts +10 -2
  41. package/dist/commands/put.js +13 -7
  42. package/dist/commands/receive.js +3 -3
  43. package/dist/commands/s3.js +44 -10
  44. package/dist/crypto-surface.d.ts +8 -0
  45. package/dist/crypto-surface.js +3 -0
  46. package/dist/crypto.d.ts +3 -0
  47. package/dist/crypto.js +3 -0
  48. package/dist/download-part.d.ts +32 -1
  49. package/dist/download-part.js +95 -6
  50. package/dist/download-range.d.ts +19 -0
  51. package/dist/download-range.js +108 -0
  52. package/dist/download-sink.d.ts +15 -0
  53. package/dist/download.d.ts +14 -0
  54. package/dist/download.js +19 -4
  55. package/dist/drive-edit/trash.js +5 -3
  56. package/dist/drive-edit/tree.d.ts +6 -0
  57. package/dist/drive-edit/tree.js +10 -0
  58. package/dist/drive-erase.js +2 -2
  59. package/dist/drive-paths.d.ts +2 -2
  60. package/dist/drive-paths.js +2 -2
  61. package/dist/handover.d.ts +44 -0
  62. package/dist/handover.js +199 -0
  63. package/dist/heavy-api.d.ts +36 -0
  64. package/dist/heavy-api.js +107 -0
  65. package/dist/heavy-copy.d.ts +72 -0
  66. package/dist/heavy-copy.js +108 -0
  67. package/dist/heavy-evm.d.ts +20 -0
  68. package/dist/heavy-evm.js +106 -0
  69. package/dist/heavy-self-pay.d.ts +92 -0
  70. package/dist/heavy-self-pay.js +167 -0
  71. package/dist/heavy-upload.d.ts +87 -0
  72. package/dist/heavy-upload.js +154 -0
  73. package/dist/heavy-wallet-pay.d.ts +39 -0
  74. package/dist/heavy-wallet-pay.js +73 -0
  75. package/dist/help-options.js +8 -2
  76. package/dist/help.js +16 -3
  77. package/dist/list-tier.d.ts +7 -0
  78. package/dist/list-tier.js +47 -0
  79. package/dist/main.js +14 -9
  80. package/dist/manifest-write.d.ts +4 -3
  81. package/dist/mcp-tools/files.js +21 -2
  82. package/dist/mcp-tools/reads.js +9 -1
  83. package/dist/platform-sign.d.ts +4 -4
  84. package/dist/platform-sign.js +3 -3
  85. package/dist/portable.d.ts +17 -1
  86. package/dist/portable.js +13 -1
  87. package/dist/product.d.ts +1 -1
  88. package/dist/product.js +1 -1
  89. package/dist/recovery-build.js +16 -0
  90. package/dist/recovery-map-part.d.ts +82 -0
  91. package/dist/recovery-map-part.js +83 -0
  92. package/dist/recovery-map.d.ts +5 -40
  93. package/dist/recovery-map.js +18 -1
  94. package/dist/recovery-source.d.ts +3 -0
  95. package/dist/recovery-source.js +9 -0
  96. package/dist/risk.d.ts +30 -0
  97. package/dist/risk.js +23 -2
  98. package/dist/run-log.d.ts +1 -1
  99. package/dist/run-log.js +7 -1
  100. package/dist/s3/address.d.ts +20 -0
  101. package/dist/s3/address.js +97 -0
  102. package/dist/s3/answer.d.ts +48 -0
  103. package/dist/s3/answer.js +330 -0
  104. package/dist/s3/body-deframe.d.ts +49 -0
  105. package/dist/s3/body-deframe.js +264 -0
  106. package/dist/s3/body-measure.d.ts +13 -0
  107. package/dist/s3/body-measure.js +48 -0
  108. package/dist/s3/body-plan.d.ts +31 -0
  109. package/dist/s3/body-plan.js +159 -0
  110. package/dist/s3/body.d.ts +46 -0
  111. package/dist/s3/body.js +117 -0
  112. package/dist/s3/call.d.ts +36 -0
  113. package/dist/s3/call.js +42 -0
  114. package/dist/s3/checksum.d.ts +26 -0
  115. package/dist/s3/checksum.js +176 -0
  116. package/dist/s3/content-type.d.ts +3 -0
  117. package/dist/s3/content-type.js +49 -0
  118. package/dist/s3/contract.d.ts +91 -14
  119. package/dist/s3/drive-conditions.d.ts +47 -0
  120. package/dist/s3/drive-conditions.js +134 -0
  121. package/dist/s3/drive-key.d.ts +11 -0
  122. package/dist/s3/drive-key.js +58 -0
  123. package/dist/s3/drive-limits.d.ts +33 -0
  124. package/dist/s3/drive-limits.js +53 -0
  125. package/dist/s3/drive-lock.d.ts +11 -0
  126. package/dist/s3/drive-lock.js +47 -0
  127. package/dist/s3/drive-trashed-name.d.ts +13 -0
  128. package/dist/s3/drive-trashed-name.js +59 -0
  129. package/dist/s3/drive-write.d.ts +38 -0
  130. package/dist/s3/drive-write.js +258 -0
  131. package/dist/s3/drive.d.ts +59 -25
  132. package/dist/s3/drive.js +46 -77
  133. package/dist/s3/listing.d.ts +27 -1
  134. package/dist/s3/listing.js +62 -20
  135. package/dist/s3/long-answer.d.ts +12 -0
  136. package/dist/s3/long-answer.js +104 -0
  137. package/dist/s3/multipart-assemble.d.ts +17 -0
  138. package/dist/s3/multipart-assemble.js +68 -0
  139. package/dist/s3/multipart-sweep.d.ts +15 -0
  140. package/dist/s3/multipart-sweep.js +87 -0
  141. package/dist/s3/multipart-view.d.ts +45 -0
  142. package/dist/s3/multipart-view.js +232 -0
  143. package/dist/s3/multipart.d.ts +26 -19
  144. package/dist/s3/multipart.js +193 -78
  145. package/dist/s3/object-read.d.ts +17 -0
  146. package/dist/s3/object-read.js +127 -0
  147. package/dist/s3/object-write.d.ts +35 -0
  148. package/dist/s3/object-write.js +198 -0
  149. package/dist/s3/range.d.ts +14 -0
  150. package/dist/s3/range.js +80 -0
  151. package/dist/s3/response-sink.d.ts +12 -2
  152. package/dist/s3/response-sink.js +71 -7
  153. package/dist/s3/routes.d.ts +0 -1
  154. package/dist/s3/routes.js +242 -217
  155. package/dist/s3/same-file.d.ts +4 -1
  156. package/dist/s3/same-file.js +12 -3
  157. package/dist/s3/server.d.ts +54 -4
  158. package/dist/s3/server.js +174 -14
  159. package/dist/s3/sigv4-canonical.d.ts +91 -0
  160. package/dist/s3/sigv4-canonical.js +148 -0
  161. package/dist/s3/sigv4-presigned.d.ts +13 -0
  162. package/dist/s3/sigv4-presigned.js +82 -0
  163. package/dist/s3/sigv4.d.ts +26 -27
  164. package/dist/s3/sigv4.js +137 -102
  165. package/dist/s3/spool.d.ts +37 -0
  166. package/dist/s3/spool.js +104 -0
  167. package/dist/s3/staging.d.ts +90 -7
  168. package/dist/s3/staging.js +97 -66
  169. package/dist/s3/xml-multipart.d.ts +38 -0
  170. package/dist/s3/xml-multipart.js +47 -0
  171. package/dist/s3/xml-read.d.ts +28 -0
  172. package/dist/s3/xml-read.js +235 -0
  173. package/dist/s3/xml.d.ts +45 -2
  174. package/dist/s3/xml.js +72 -13
  175. package/dist/s3-gateway.d.ts +11 -5
  176. package/dist/s3-gateway.js +5 -2
  177. package/dist/seal.d.ts +1 -1
  178. package/dist/share.d.ts +5 -0
  179. package/dist/share.js +1 -1
  180. package/dist/shared/lib/api/types-heavy.d.ts +167 -0
  181. package/dist/shared/lib/api/types-heavy.js +60 -0
  182. package/dist/shared/lib/drive/manifest-codec.d.ts +11 -31
  183. package/dist/shared/lib/drive/manifest-codec.js +15 -23
  184. package/dist/shared/lib/drive/manifest-index.d.ts +10 -1
  185. package/dist/shared/lib/drive/manifest-index.js +30 -8
  186. package/dist/shared/lib/drive/manifest-wire-extras.d.ts +50 -0
  187. package/dist/shared/lib/drive/manifest-wire-extras.js +65 -0
  188. package/dist/shared/lib/drive/preview-classify.d.ts +17 -0
  189. package/dist/shared/lib/drive/preview-classify.js +96 -0
  190. package/dist/shared/lib/drive/rebuild-verify.js +1 -0
  191. package/dist/shared/lib/filecoin/providers.d.ts +25 -0
  192. package/dist/shared/lib/filecoin/providers.js +58 -0
  193. package/dist/shared/lib/heavy/download.d.ts +48 -0
  194. package/dist/shared/lib/heavy/download.js +116 -0
  195. package/dist/shared/lib/heavy/order-plan.d.ts +77 -0
  196. package/dist/shared/lib/heavy/order-plan.js +83 -0
  197. package/dist/shared/lib/heavy/order-runner.d.ts +9 -0
  198. package/dist/shared/lib/heavy/order-runner.js +300 -0
  199. package/dist/shared/lib/heavy/order-types.d.ts +90 -0
  200. package/dist/shared/lib/heavy/order-types.js +6 -0
  201. package/dist/shared/lib/heavy/order-wire.d.ts +55 -0
  202. package/dist/shared/lib/heavy/order-wire.js +113 -0
  203. package/dist/shared/lib/heavy/piece-cid.d.ts +24 -0
  204. package/dist/shared/lib/heavy/piece-cid.js +55 -0
  205. package/dist/shared/lib/heavy/sp-upload.d.ts +37 -0
  206. package/dist/shared/lib/heavy/sp-upload.js +112 -0
  207. package/dist/shared/lib/heavy/synapse-piece.d.ts +1 -0
  208. package/dist/shared/lib/heavy/synapse-piece.js +8 -0
  209. package/dist/shared/lib/share/handover-format.d.ts +129 -0
  210. package/dist/shared/lib/share/handover-format.js +323 -0
  211. package/dist/shared/lib/share/shared-file-info.js +1 -0
  212. package/dist/shared/lib/storage-network.d.ts +4 -4
  213. package/dist/shared/lib/storage-network.js +7 -7
  214. package/dist/shared/lib/storage-tier.d.ts +28 -0
  215. package/dist/shared/lib/storage-tier.js +63 -0
  216. package/dist/shared/lib/wallet/send-rules.js +1 -0
  217. package/dist/upload-file.d.ts +8 -0
  218. package/dist/upload-file.js +57 -25
  219. package/dist/upload-price-node.d.ts +3 -0
  220. package/dist/upload-price-node.js +3 -6
  221. package/dist/upload-price.d.ts +10 -0
  222. package/dist/upload-price.js +13 -1
  223. package/dist/upload-run.d.ts +80 -0
  224. package/dist/upload-run.js +173 -0
  225. package/dist/upload-steps.js +15 -4
  226. package/dist/upload-store.d.ts +15 -6
  227. package/dist/upload-store.js +6 -0
  228. package/dist/upload-wallet-put.d.ts +11 -4
  229. package/dist/upload-wallet-put.js +17 -6
  230. package/dist/upload-wallet.js +4 -0
  231. package/dist/upload-wire.d.ts +25 -0
  232. package/dist/upload-wire.js +20 -1
  233. package/dist/upload.js +28 -13
  234. package/dist/wallet-sign.js +2 -2
  235. package/docs/commands/get.md +3 -0
  236. package/docs/commands/handover.md +38 -0
  237. package/docs/commands/heavy.md +35 -0
  238. package/docs/commands/ls.md +5 -0
  239. package/docs/commands/public-code.md +7 -0
  240. package/docs/commands/push.md +4 -1
  241. package/docs/commands/put.md +23 -1
  242. package/docs/commands/share.md +3 -1
  243. package/package.json +5 -2
  244. package/server.json +2 -2
  245. package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +17 -1
  246. package/vendor/nmts-crypto/nmts_crypto_wasm.js +28 -0
  247. package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
  248. package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +2 -1
@@ -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;
@@ -0,0 +1,134 @@
1
+ // What a write asks of the key before it will happen: `If-Match` and `If-None-Match` on an upload,
2
+ // a finish, a copy's destination or a delete, and the four `x-amz-copy-source-if-*` on a copy's
3
+ // source.
4
+ //
5
+ // ⛔ A CONDITION THIS GATEWAY CANNOT HONOUR IS REFUSED WITH 501, NEVER IGNORED. A client that sends
6
+ // `If-None-Match: *` is relying on it to stop two writers overwriting each other; answering 200
7
+ // while ignoring it tells that client the one thing that did not happen.
8
+ //
9
+ // ⛔ JUDGED AGAINST THE LIST READ INSIDE THE KEY'S LOCK (`drive-lock.ts`), where the drive decides
10
+ // the write. A check made earlier, on a list cached for seconds, only lets an answer that is
11
+ // already known keep its own status; it is never the one that decides.
12
+ import { S3Refusal } from "./answer.js";
13
+ import { headerOf } from "./call.js";
14
+ export const UNCONDITIONAL = { ifMatch: null, ifNoneMatch: false };
15
+ function notHonoured(header, operation) {
16
+ return new S3Refusal(501, "NotImplemented", `This gateway does not honour ${header} on ${operation}. Nothing was changed.`);
17
+ }
18
+ function preconditionFailed() {
19
+ return new S3Refusal(412, "PreconditionFailed", "At least one of the preconditions you specified did not hold.");
20
+ }
21
+ function bareTag(tag) {
22
+ return tag.trim().replace(/^W\//, "").replace(/^"|"$/g, "");
23
+ }
24
+ /** Whether a list of tags in a header names this one. `*` names any object that exists. */
25
+ function tagListHas(header, etag) {
26
+ const mine = bareTag(etag);
27
+ return header.split(",").some((tag) => tag.trim() === "*" || bareTag(tag) === mine);
28
+ }
29
+ /** The HTTP conditions a request carries. */
30
+ const CONDITIONAL = ["if-match", "if-none-match", "if-modified-since", "if-unmodified-since"];
31
+ /**
32
+ * Refuse a request that carries a condition, for an operation that honours none.
33
+ *
34
+ * ⚠ `UploadPart`, `CreateMultipartUpload`, `AbortMultipartUpload` and `DeleteObjects` take no
35
+ * conditions in S3 either; a client that sends one anyway is told so rather than answered as if
36
+ * it had held.
37
+ */
38
+ export function refuseConditions(req, operation) {
39
+ for (const name of CONDITIONAL) {
40
+ if (headerOf(req, name) !== undefined)
41
+ throw notHonoured(name, operation);
42
+ }
43
+ }
44
+ /** What `If-Match` and `If-None-Match` ask of a write's destination. */
45
+ export function writeConditionOf(req, operation) {
46
+ for (const name of ["if-modified-since", "if-unmodified-since"]) {
47
+ if (headerOf(req, name) !== undefined)
48
+ throw notHonoured(name, operation);
49
+ }
50
+ const noneMatch = headerOf(req, "if-none-match")?.trim();
51
+ if (noneMatch !== undefined && noneMatch !== "*")
52
+ throw notHonoured("If-None-Match other than `*`", operation);
53
+ const match = headerOf(req, "if-match")?.trim();
54
+ return { ifMatch: match === undefined || match === "" ? null : match, ifNoneMatch: noneMatch === "*" };
55
+ }
56
+ /**
57
+ * What a `DeleteObject` asks: `If-Match` is honoured; the rest of S3's conditions on a delete are
58
+ * refused, since nothing here can judge them.
59
+ */
60
+ export function deleteConditionOf(req) {
61
+ for (const name of [
62
+ "if-none-match",
63
+ "if-modified-since",
64
+ "if-unmodified-since",
65
+ "x-amz-if-match-last-modified-time",
66
+ "x-amz-if-match-size",
67
+ ]) {
68
+ if (headerOf(req, name) !== undefined)
69
+ throw notHonoured(name, "DeleteObject");
70
+ }
71
+ const match = headerOf(req, "if-match")?.trim();
72
+ return { ifMatch: match === undefined || match === "" ? null : match, ifNoneMatch: false };
73
+ }
74
+ /**
75
+ * Whether what stands at the key now meets the condition. Throws 412 when it does not, and 404 for
76
+ * `If-Match` on a key that holds nothing — S3's two answers.
77
+ */
78
+ export function checkWriteCondition(standing, condition) {
79
+ if (condition.ifNoneMatch && standing !== undefined)
80
+ throw preconditionFailed();
81
+ if (condition.ifMatch !== null) {
82
+ if (standing === undefined) {
83
+ throw new S3Refusal(404, "NoSuchKey", "If-Match names a tag, and nothing is at that key. Nothing was changed.");
84
+ }
85
+ if (!tagListHas(condition.ifMatch, standing.etag))
86
+ throw preconditionFailed();
87
+ }
88
+ }
89
+ /** An HTTP date as whole seconds. An unreadable one is refused: a condition nobody can read is not held. */
90
+ function secondsOf(name, value) {
91
+ if (value === undefined)
92
+ return null;
93
+ const ms = Date.parse(value);
94
+ if (Number.isNaN(ms))
95
+ throw new S3Refusal(400, "InvalidArgument", `${name} is not a date.`);
96
+ return Math.floor(ms / 1000);
97
+ }
98
+ /** The four `x-amz-copy-source-if-*` headers. */
99
+ export function copySourceConditionOf(req) {
100
+ const tag = (name) => {
101
+ const value = headerOf(req, name)?.trim();
102
+ return value === undefined || value === "" ? null : value;
103
+ };
104
+ return {
105
+ ifMatch: tag("x-amz-copy-source-if-match"),
106
+ ifNoneMatch: tag("x-amz-copy-source-if-none-match"),
107
+ ifModifiedSince: secondsOf("x-amz-copy-source-if-modified-since", headerOf(req, "x-amz-copy-source-if-modified-since")),
108
+ ifUnmodifiedSince: secondsOf("x-amz-copy-source-if-unmodified-since", headerOf(req, "x-amz-copy-source-if-unmodified-since")),
109
+ };
110
+ }
111
+ /**
112
+ * Whether a copy's source meets its conditions. Throws 412 when it does not.
113
+ *
114
+ * ⚠ S3'S PAIRINGS: a matching `if-match` copies whatever `if-unmodified-since` says, and a failing
115
+ * `if-none-match` refuses whatever `if-modified-since` says — each tag condition, when present,
116
+ * decides in place of its date.
117
+ */
118
+ export function checkCopySource(object, condition) {
119
+ const changed = Math.floor(object.entry.updatedAt / 1000);
120
+ if (condition.ifMatch !== null) {
121
+ if (!tagListHas(condition.ifMatch, object.etag))
122
+ throw preconditionFailed();
123
+ }
124
+ else if (condition.ifUnmodifiedSince !== null && changed > condition.ifUnmodifiedSince) {
125
+ throw preconditionFailed();
126
+ }
127
+ if (condition.ifNoneMatch !== null) {
128
+ if (tagListHas(condition.ifNoneMatch, object.etag))
129
+ throw preconditionFailed();
130
+ }
131
+ else if (condition.ifModifiedSince !== null && changed <= condition.ifModifiedSince) {
132
+ throw preconditionFailed();
133
+ }
134
+ }
@@ -0,0 +1,11 @@
1
+ /** S3's own ceiling on a key, in UTF-8 bytes. */
2
+ export declare const MAX_KEY_BYTES = 1024;
3
+ /** The form two keys are compared in: the drive's own folding of names. */
4
+ export declare function sameKey(key: string): string;
5
+ /**
6
+ * Refuse a key this drive cannot hold as the path it names. A key ending in `/` is a folder marker
7
+ * and is judged as the folder it names.
8
+ *
9
+ * ⛔ EVERY CHECK IS ON THE KEY ALONE, so this runs before the body is read and before any list is.
10
+ */
11
+ export declare function checkKey(key: string): void;
@@ -0,0 +1,58 @@
1
+ // Which S3 keys a drive can hold, and when two keys are the same key.
2
+ //
3
+ // ⛔ A KEY IS A PATH IN A DRIVE, AND NOT EVERY S3 KEY IS ONE. S3 takes any bytes up to 1,024 of
4
+ // them; a drive has folders and names, so `/x`, `a//b`, `a/./b` and a name made of control
5
+ // characters have nowhere to go. Stored anyway, they came back under a different key — `/x`
6
+ // listed as `x` — and the gateway answered 500 "stored but not shown" for a file it had paid
7
+ // for, which a client retries, paying again each time. So they are refused BEFORE the body is
8
+ // read, with a 400 a client does not retry.
9
+ //
10
+ // ⛔ TWO SPELLINGS OF ONE NAME ARE ONE KEY. The drive folds names to Unicode NFC when it compares
11
+ // them — macOS writes `café` decomposed and a browser composed — so a key is compared the same
12
+ // way here. Compared byte for byte instead, an upload to the other spelling was "free", stored
13
+ // beside the file a person sees as the same one, or replaced it without asking whether its bytes
14
+ // were the same.
15
+ import { S3Refusal } from "./answer.js";
16
+ import { isFolderKey } from "./listing.js";
17
+ /** S3's own ceiling on a key, in UTF-8 bytes. */
18
+ export const MAX_KEY_BYTES = 1024;
19
+ /** The form two keys are compared in: the drive's own folding of names. */
20
+ export function sameKey(key) {
21
+ return key.normalize("NFC");
22
+ }
23
+ function badKey(why) {
24
+ return new S3Refusal(400, "InvalidArgument", `${why} Nothing was read or stored.`);
25
+ }
26
+ /** True when a string holds a C0 control character (U+0000 to U+001F). */
27
+ function hasControl(text) {
28
+ for (let i = 0; i < text.length; i += 1) {
29
+ if (text.charCodeAt(i) < 0x20)
30
+ return true;
31
+ }
32
+ return false;
33
+ }
34
+ /**
35
+ * Refuse a key this drive cannot hold as the path it names. A key ending in `/` is a folder marker
36
+ * and is judged as the folder it names.
37
+ *
38
+ * ⛔ EVERY CHECK IS ON THE KEY ALONE, so this runs before the body is read and before any list is.
39
+ */
40
+ export function checkKey(key) {
41
+ if (Buffer.byteLength(key, "utf8") > MAX_KEY_BYTES) {
42
+ throw new S3Refusal(400, "KeyTooLongError", `The key is longer than ${MAX_KEY_BYTES} bytes. Nothing was read or stored.`);
43
+ }
44
+ if (hasControl(key))
45
+ throw badKey("The key holds a control character, which no name in a drive can hold.");
46
+ if (key.startsWith("/")) {
47
+ throw badKey("The key begins with `/`, and a path in a drive has no empty name at its start.");
48
+ }
49
+ const path = isFolderKey(key) ? key.slice(0, -1) : key;
50
+ for (const name of path.split("/")) {
51
+ if (name === "")
52
+ throw badKey("The key has an empty name between two `/`.");
53
+ if (name === "." || name === "..")
54
+ throw badKey("The key has `.` or `..` as a name, which a path in a drive cannot.");
55
+ if (name.trim() === "")
56
+ throw badKey("The key has a name that is only spaces.");
57
+ }
58
+ }
@@ -0,0 +1,33 @@
1
+ import { S3Refusal } from "./answer.ts";
2
+ /** How many writes one gateway runs at once before it answers `SlowDown`. */
3
+ export declare const MAX_CONCURRENT_WRITES = 16;
4
+ /** How many uploads in pieces one bucket may have begun and not finished. */
5
+ export declare const MAX_UPLOADS_PER_BUCKET = 1000;
6
+ /** How long a client told `SlowDown` is asked to wait, in seconds. */
7
+ export declare const RETRY_AFTER_SECONDS = 1;
8
+ /**
9
+ * `SlowDown`, with the wait it names.
10
+ *
11
+ * ⚠ NOT AN `S3Refusal`, which carries no wait: this is read by its `status` (503) and `retryAfter`,
12
+ * which is how the answer layer turns any 503 into `SlowDown` with a `Retry-After` header.
13
+ */
14
+ export declare class SlowDown extends Error {
15
+ readonly status = 503;
16
+ readonly retryAfter = 1;
17
+ constructor(message: string);
18
+ }
19
+ /** The object, or the part of it that arrived, is larger than this gateway takes. */
20
+ export declare function tooLarge(max: number): S3Refusal;
21
+ /** Refuse a declared size over the limit, before a byte of it is read. */
22
+ export declare function checkDeclaredSize(size: number | null, max: number | undefined): void;
23
+ /**
24
+ * Whether NMTS stores a file with no bytes, which is not the same thing as a folder marker.
25
+ *
26
+ * ⛔ THE ONE PLACE THAT DECIDES IT. The upload path stores an empty file (sealed, it is a header and
27
+ * one empty final chunk, and costs one credit), so an empty object is stored like any other.
28
+ * Set this to `false` and an empty object is refused before anything is spent, with a 400 a
29
+ * client does not retry — never handed to a store that would fail with a 500 naming a path.
30
+ */
31
+ export declare const STORES_EMPTY_FILES: boolean;
32
+ /** Refuse an object with no bytes, unless empty files are stored. A folder marker never comes here. */
33
+ export declare function checkNotEmpty(size: number | null): void;