@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,323 @@
1
+ // The two files a person can pass around outside NMTS: the HANDOVER file, which carries one file
2
+ // sealed to one recipient, and the PUBLIC CODE file, which lets a sender seal to somebody without
3
+ // asking the server for their identity. The byte layouts are fixed by the format specification
4
+ // (NCF-3 §5.6 and §5.7); this module is the JSON around them, and nothing else.
5
+ //
6
+ // ⛔ NO CRYPTOGRAPHY HERE, AND NO IMPORTS. Everything that needs a key or a hash happens in the
7
+ // caller — the browser's crypto worker, or the command-line tool's engine. This module only
8
+ // builds and reads the text, so both programs refuse the same files for the same reasons. It is
9
+ // copied byte for byte into the command-line package.
10
+ //
11
+ // ⛔ IT REFUSES EXACTLY WHAT §5.6 AND §5.7 SAY A READER REFUSES, and nothing else. The specification
12
+ // lists every refusal (the size cap, the closed key sets, the `note` shape, the version rule, the
13
+ // network, the lengths, canonical base64url); a refusal added here and not there makes two
14
+ // readers of one published format disagree about one file.
15
+ //
16
+ // ⚠ THE ITEM ID IS COMPARED AS WRITTEN. §5.3 lowercases it before binding, so "ABC" and "abc" bind
17
+ // the same. A reader that accepted "ABC" would show text different from what was bound, so an item
18
+ // id that is not already lowercase is refused here.
19
+ /** Wire values of the handover file. Fixed forever once a file carries them. */
20
+ export const HANDOVER_FORMAT = "nmts-handover";
21
+ export const HANDOVER_VERSION = 1;
22
+ /** Marker inside the sealed parts document. */
23
+ export const HANDOVER_PARTS_FORMAT = "nmts-handover-parts/1";
24
+ /** Marker of the name document — the §5.4 share document, which a handover extends by two keys. */
25
+ export const SHARE_FILE_FORMAT = "nmts-share-file/1";
26
+ /** Wire values of the public code file. */
27
+ export const PUBLIC_CODE_FORMAT = "nmts-public-code";
28
+ export const PUBLIC_CODE_VERSION = 1;
29
+ /** File name endings. */
30
+ export const HANDOVER_EXTENSION = ".nmtshandover";
31
+ export const PUBLIC_CODE_EXTENSION = ".nmtscode";
32
+ /**
33
+ * What a person who opens the file in a text editor reads. Informational; not read back.
34
+ * The two place names are the labels on the screen: the side panel entry and the button.
35
+ */
36
+ export const HANDOVER_NOTE = [
37
+ "An NMTS handover file. Sign in at nmts.me with the NMTS key it was made for, then open it from Shared with me → Open handover file.",
38
+ "NMTS 건네기 파일입니다. 이 파일을 받을 NMTS 키로 nmts.me에 로그인한 뒤 「받은 공유함」 → 「건네기 파일 열기」에서 여십시오.", // shown in Korean
39
+ ];
40
+ /** Size caps on the whole text, checked before it is parsed (§5.6, §5.7). */
41
+ export const MAX_HANDOVER_FILE_BYTES = 1024 * 1024;
42
+ export const MAX_PUBLIC_CODE_FILE_BYTES = 8 * 1024;
43
+ /** Exact byte lengths the specification fixes. */
44
+ const IDENTITY_LEN = 4989;
45
+ const ENVELOPE_LEN = 1240;
46
+ const DIGEST_ENVELOPE_LEN = 104;
47
+ const SHA256_LEN = 32;
48
+ /** nonce(24) + commitment(32) + tag(16): the smallest sealed value, with an empty plaintext. */
49
+ const MIN_SEALED_LEN = 72;
50
+ /** Field bounds (§5.6). Together they keep a file under MAX_HANDOVER_FILE_BYTES. */
51
+ export const MAX_NAME_SEALED_LEN = 64 * 1024;
52
+ const MAX_PARTS_SEALED_LEN = 640 * 1024;
53
+ const MAX_PARTS = 4096;
54
+ const MAX_EPOCH = 0xffff_ffff;
55
+ const MAX_CODE_CHARS = 64;
56
+ const UUID_LOWER = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
57
+ const B64URL_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
58
+ const B64URL = /^[A-Za-z0-9_-]*$/;
59
+ /** Blob and patch ids go into an aggregator URL, so they are held to the base64url alphabet. */
60
+ const STORAGE_ID = /^[A-Za-z0-9_-]{1,256}$/;
61
+ export class HandoverFormatError extends Error {
62
+ problem;
63
+ /** The field at fault, for `bad-field`. */
64
+ field;
65
+ constructor(problem, field = null) {
66
+ super(field === null ? `handover file: ${problem}` : `handover file: ${problem} (${field})`);
67
+ this.name = "HandoverFormatError";
68
+ this.problem = problem;
69
+ this.field = field;
70
+ }
71
+ }
72
+ /**
73
+ * Bytes behind an unpadded base64url string, or null when it is not one in CANONICAL form: the
74
+ * bits the last character carries beyond the data must be zero, so one byte string has one text.
75
+ */
76
+ export function base64UrlByteLength(text) {
77
+ if (!B64URL.test(text))
78
+ return null;
79
+ const rest = text.length % 4;
80
+ if (rest === 1)
81
+ return null;
82
+ if (rest !== 0) {
83
+ const last = B64URL_ALPHABET.indexOf(text.charAt(text.length - 1));
84
+ const unused = rest === 2 ? 0b1111 : 0b11;
85
+ if ((last & unused) !== 0)
86
+ return null;
87
+ }
88
+ return Math.floor(text.length / 4) * 3 + (rest === 0 ? 0 : rest - 1);
89
+ }
90
+ /** `nmts-handover-<YYYY-MM-DD>.nmtshandover`, the date in UTC. The file's own name is not in it. */
91
+ export function handoverFileName(now) {
92
+ return `nmts-handover-${now.toISOString().slice(0, 10)}${HANDOVER_EXTENSION}`;
93
+ }
94
+ /** `nmts-public-code-<public code>.nmtscode`. The code is public, and it tells two such files apart. */
95
+ export function publicCodeFileName(code) {
96
+ return `nmts-public-code-${code.replace(/[^0-9A-Za-z-]/g, "")}${PUBLIC_CODE_EXTENSION}`;
97
+ }
98
+ /** The handover file's text. Pretty-printed: somebody may well open it to see what it is. */
99
+ export function encodeHandover(file) {
100
+ checkHandover(file);
101
+ const { network, item, sender, envelope, name, hash, parts } = file;
102
+ const doc = { format: HANDOVER_FORMAT, version: HANDOVER_VERSION, network, item, sender, envelope, name, hash, parts, note: HANDOVER_NOTE };
103
+ return `${JSON.stringify(doc, null, 2)}\n`;
104
+ }
105
+ /**
106
+ * Read a handover file's text, in the order §5.6 gives: size, JSON, format, version, network, key
107
+ * set, `note`, fields. `network` is the reader's own. Throws `HandoverFormatError`; never returns a
108
+ * partial file.
109
+ */
110
+ export function decodeHandover(text, network) {
111
+ capBytes(text, MAX_HANDOVER_FILE_BYTES);
112
+ const doc = objectOf(text);
113
+ if (doc["format"] !== HANDOVER_FORMAT)
114
+ throw new HandoverFormatError("wrong-format");
115
+ checkVersion(doc["version"], HANDOVER_VERSION);
116
+ const fileNetwork = doc["network"];
117
+ if (fileNetwork !== "mainnet" && fileNetwork !== "testnet")
118
+ throw new HandoverFormatError("bad-field", "network");
119
+ if (fileNetwork !== network)
120
+ throw new HandoverFormatError("other-network");
121
+ closedKeys(doc, ["format", "version", "network", "item", "sender", "envelope", "name", "hash", "parts", "note"]);
122
+ const note = doc["note"];
123
+ if (note !== undefined && !(Array.isArray(note) && note.every((line) => typeof line === "string"))) {
124
+ throw new HandoverFormatError("bad-field", "note");
125
+ }
126
+ const file = {
127
+ network: fileNetwork,
128
+ item: stringField(doc, "item"),
129
+ sender: stringField(doc, "sender"),
130
+ envelope: stringField(doc, "envelope"),
131
+ name: stringField(doc, "name"),
132
+ hash: stringField(doc, "hash"),
133
+ parts: stringField(doc, "parts"),
134
+ };
135
+ checkHandover(file);
136
+ return file;
137
+ }
138
+ /** The name document, exactly as it is sealed. */
139
+ export function encodeHandoverName(doc) {
140
+ checkName(doc);
141
+ return JSON.stringify({ f: SHARE_FILE_FORMAT, name: doc.name, size: doc.size, network: doc.network, parts_sha256: doc.partsSha256 });
142
+ }
143
+ /** Whether a name document, once sealed, fits the `name` field. The caller checks before sealing. */
144
+ export function handoverNameFits(encoded) {
145
+ return new TextEncoder().encode(encoded).length + MIN_SEALED_LEN <= MAX_NAME_SEALED_LEN;
146
+ }
147
+ /** Read an opened name document. Throws `HandoverFormatError` (`bad-field`, `name`). */
148
+ export function decodeHandoverName(text) {
149
+ const doc = objectOf(text);
150
+ const bad = new HandoverFormatError("bad-field", "name");
151
+ const keys = Object.keys(doc).sort().join(",");
152
+ if (keys !== "f,name,network,parts_sha256,size" || doc["f"] !== SHARE_FILE_FORMAT)
153
+ throw bad;
154
+ const name = doc["name"];
155
+ const size = doc["size"];
156
+ const network = doc["network"];
157
+ const partsSha256 = doc["parts_sha256"];
158
+ if (typeof name !== "string" || typeof size !== "number" || typeof partsSha256 !== "string")
159
+ throw bad;
160
+ if (network !== "mainnet" && network !== "testnet")
161
+ throw bad;
162
+ const out = { name, size, network, partsSha256 };
163
+ checkName(out);
164
+ return out;
165
+ }
166
+ /** The parts document, exactly as it is sealed. Compact: nobody reads it but the recipient's engine. */
167
+ export function encodePartsList(parts) {
168
+ checkParts(parts);
169
+ return JSON.stringify({
170
+ f: HANDOVER_PARTS_FORMAT,
171
+ parts: parts.map((p, i) => ({ i, blob: p.blob, patch: p.patch, len: p.len, exp: p.exp })),
172
+ });
173
+ }
174
+ /** Read an opened parts document. Throws `HandoverFormatError` (`bad-field`, `parts`). */
175
+ export function decodePartsList(text) {
176
+ const doc = objectOf(text);
177
+ const bad = new HandoverFormatError("bad-field", "parts");
178
+ if (Object.keys(doc).sort().join(",") !== "f,parts" || doc["f"] !== HANDOVER_PARTS_FORMAT)
179
+ throw bad;
180
+ const raw = doc["parts"];
181
+ if (!Array.isArray(raw))
182
+ throw bad;
183
+ const parts = raw.map((value, index) => {
184
+ if (!isRecord(value))
185
+ throw bad;
186
+ const keys = Object.keys(value).sort().join(",");
187
+ if (keys !== "blob,exp,i,len,patch" || value["i"] !== index)
188
+ throw bad;
189
+ const blob = value["blob"];
190
+ const patch = value["patch"];
191
+ const len = value["len"];
192
+ const exp = value["exp"];
193
+ if ((blob !== null && typeof blob !== "string") || (patch !== null && typeof patch !== "string"))
194
+ throw bad;
195
+ if (typeof len !== "number" || typeof exp !== "number")
196
+ throw bad;
197
+ return { blob, patch, len, exp };
198
+ });
199
+ checkParts(parts);
200
+ return parts;
201
+ }
202
+ /**
203
+ * The smallest recorded end epoch, or 0 when any piece's is unknown (0). Exclusive, and recorded
204
+ * when the file was made — see `HandoverPart.exp`.
205
+ */
206
+ export function earliestExpiry(parts) {
207
+ if (parts.some((p) => p.exp === 0))
208
+ return 0;
209
+ return parts.reduce((min, p) => Math.min(min, p.exp), MAX_EPOCH);
210
+ }
211
+ /** The public code file's text. */
212
+ export function encodePublicCodeFile(file) {
213
+ checkPublicCode(file);
214
+ const doc = { format: PUBLIC_CODE_FORMAT, version: PUBLIC_CODE_VERSION, code: file.code, identity: file.identity };
215
+ return `${JSON.stringify(doc, null, 2)}\n`;
216
+ }
217
+ /**
218
+ * Read a public code file's text. Throws `HandoverFormatError`.
219
+ *
220
+ * ⚠ This checks the SHAPE only. Whether `identity` really fingerprints to `code` is the engine's
221
+ * question, and a caller must ask it before sealing anything to the identity.
222
+ */
223
+ export function decodePublicCodeFile(text) {
224
+ capBytes(text, MAX_PUBLIC_CODE_FILE_BYTES);
225
+ const doc = objectOf(text);
226
+ if (doc["format"] !== PUBLIC_CODE_FORMAT)
227
+ throw new HandoverFormatError("wrong-format");
228
+ checkVersion(doc["version"], PUBLIC_CODE_VERSION);
229
+ closedKeys(doc, ["format", "version", "code", "identity"]);
230
+ const file = { code: stringField(doc, "code"), identity: stringField(doc, "identity") };
231
+ checkPublicCode(file);
232
+ return file;
233
+ }
234
+ // ── checks ───────────────────────────────────────────────────────────────────────────────────
235
+ function capBytes(text, max) {
236
+ // UTF-16 units never outnumber UTF-8 bytes, so a long string is refused before it is encoded.
237
+ if (text.length > max || new TextEncoder().encode(text).length > max) {
238
+ throw new HandoverFormatError("too-large");
239
+ }
240
+ }
241
+ /** An integer above the known version is "newer"; anything else that is not the version is wrong. */
242
+ function checkVersion(version, known) {
243
+ if (version === known)
244
+ return;
245
+ if (typeof version === "number" && Number.isSafeInteger(version) && version > known) {
246
+ throw new HandoverFormatError("unknown-version");
247
+ }
248
+ throw new HandoverFormatError("bad-field", "version");
249
+ }
250
+ function closedKeys(doc, allowed) {
251
+ for (const key of Object.keys(doc)) {
252
+ if (!allowed.includes(key))
253
+ throw new HandoverFormatError("bad-field", key);
254
+ }
255
+ }
256
+ function checkHandover(file) {
257
+ if (file.network !== "mainnet" && file.network !== "testnet")
258
+ throw new HandoverFormatError("bad-field", "network");
259
+ if (!UUID_LOWER.test(file.item))
260
+ throw new HandoverFormatError("bad-field", "item");
261
+ exactly(file.sender, IDENTITY_LEN, "sender");
262
+ exactly(file.envelope, ENVELOPE_LEN, "envelope");
263
+ exactly(file.hash, DIGEST_ENVELOPE_LEN, "hash");
264
+ within(file.name, MIN_SEALED_LEN + 1, MAX_NAME_SEALED_LEN, "name");
265
+ within(file.parts, MIN_SEALED_LEN + 1, MAX_PARTS_SEALED_LEN, "parts");
266
+ }
267
+ function checkName(doc) {
268
+ const network = doc.network === "mainnet" || doc.network === "testnet";
269
+ const ok = Number.isSafeInteger(doc.size) && doc.size >= 0 && network && base64UrlByteLength(doc.partsSha256) === SHA256_LEN;
270
+ if (!ok)
271
+ throw new HandoverFormatError("bad-field", "name");
272
+ }
273
+ function checkParts(parts) {
274
+ if (parts.length === 0 || parts.length > MAX_PARTS)
275
+ throw new HandoverFormatError("bad-field", "parts");
276
+ for (const p of parts) {
277
+ const ok = (p.blob === null) !== (p.patch === null) &&
278
+ (p.blob === null || STORAGE_ID.test(p.blob)) &&
279
+ (p.patch === null || STORAGE_ID.test(p.patch)) &&
280
+ Number.isSafeInteger(p.len) &&
281
+ p.len > 0 &&
282
+ Number.isSafeInteger(p.exp) &&
283
+ p.exp >= 0 &&
284
+ p.exp <= MAX_EPOCH;
285
+ if (!ok)
286
+ throw new HandoverFormatError("bad-field", "parts");
287
+ }
288
+ }
289
+ function checkPublicCode(file) {
290
+ if (file.code.length === 0 || file.code.length > MAX_CODE_CHARS)
291
+ throw new HandoverFormatError("bad-field", "code");
292
+ exactly(file.identity, IDENTITY_LEN, "identity");
293
+ }
294
+ function exactly(value, bytes, field) {
295
+ if (base64UrlByteLength(value) !== bytes)
296
+ throw new HandoverFormatError("bad-field", field);
297
+ }
298
+ function within(value, min, max, field) {
299
+ const n = base64UrlByteLength(value);
300
+ if (n === null || n < min || n > max)
301
+ throw new HandoverFormatError("bad-field", field);
302
+ }
303
+ function isRecord(value) {
304
+ return typeof value === "object" && value !== null && !Array.isArray(value);
305
+ }
306
+ function objectOf(text) {
307
+ let parsed;
308
+ try {
309
+ parsed = JSON.parse(text);
310
+ }
311
+ catch {
312
+ throw new HandoverFormatError("not-json");
313
+ }
314
+ if (!isRecord(parsed))
315
+ throw new HandoverFormatError("not-json");
316
+ return parsed;
317
+ }
318
+ function stringField(doc, field) {
319
+ const value = doc[field];
320
+ if (typeof value !== "string")
321
+ throw new HandoverFormatError("bad-field", field);
322
+ return value;
323
+ }
@@ -51,6 +51,7 @@ export function decodeSharedFileInfo(sealed) {
51
51
  parsed = JSON.parse(sealed);
52
52
  }
53
53
  catch {
54
+ // See above: text that is not JSON is the name itself.
54
55
  return { name: sealed };
55
56
  }
56
57
  if (typeof parsed !== "object" || parsed === null)
@@ -1,6 +1,6 @@
1
1
  /** Network codes, as stored. Fixed forever once a row carries one. */
2
2
  export declare const NETWORK_WALRUS = 0;
3
- /** Filecoin — product tier 「NMTS Heavy」. Reserved; no upload path exists yet. */
3
+ /** Filecoin — what the "NMTS Heavy" tier stores on (whole copies kept by storage companies). */
4
4
  export declare const NETWORK_FILECOIN = 1;
5
5
  /** Arweave — reserved and deliberately last: it cannot be deleted, ever (BACKLOG §2.2). */
6
6
  export declare const NETWORK_ARWEAVE = 2;
@@ -18,8 +18,8 @@ export declare function networkCode(name: string): number | null;
18
18
  /**
19
19
  * What an ABSENT network field means: Walrus.
20
20
  *
21
- * This is a fact, not a fallback. No other network has ever had an upload path, so every part,
22
- * entry and map written before the field existed is on Walrus by construction. Stated as a named
23
- * constant so the reasoning sits next to every use of it rather than being re-derived.
21
+ * This is a fact, not a fallback. No other network had an upload path until well after the field
22
+ * existed, so every part, entry and map written without it is on Walrus by construction. Stated as
23
+ * a named constant so the reasoning sits next to every use of it rather than being re-derived.
24
24
  */
25
25
  export declare const NETWORK_WHEN_UNRECORDED = 0;
@@ -18,16 +18,16 @@
18
18
  // with none of our code beside it. A magic number in that document is a trap; a word is not.
19
19
  // This mirrors the existing `kind` field, which is `1` in the file list and `"file"` in the map.
20
20
  //
21
- // ⚠ NOT A TIER MAPPING. 「NMTS Fast」/「NMTS Heavy」 are product names for what the person buys;
22
- // these are the networks underneath. The screen keeps its own mapping (components/drive/
23
- // StorageTier.tsx) so a rename of either never silently redefines the other.
21
+ // ⚠ NOT A TIER MAPPING. "NMTS Standard" and "NMTS Heavy" are product names for what the person
22
+ // picks; these are the networks underneath. The one mapping between the two lives in
23
+ // `storage-tier.ts`, so a rename of either never silently redefines the other.
24
24
  //
25
25
  // FAILURE MODES: none at runtime — pure functions over constants. An UNKNOWN code answers `null`
26
26
  // rather than falling back to Walrus: a wrong network recorded in a recovery map is unfixable
27
27
  // years later, so callers are made to decide what to do about it.
28
28
  /** Network codes, as stored. Fixed forever once a row carries one. */
29
29
  export const NETWORK_WALRUS = 0;
30
- /** Filecoin — product tier 「NMTS Heavy」. Reserved; no upload path exists yet. */
30
+ /** Filecoin — what the "NMTS Heavy" tier stores on (whole copies kept by storage companies). */
31
31
  export const NETWORK_FILECOIN = 1;
32
32
  /** Arweave — reserved and deliberately last: it cannot be deleted, ever (BACKLOG §2.2). */
33
33
  export const NETWORK_ARWEAVE = 2;
@@ -57,8 +57,8 @@ export function networkCode(name) {
57
57
  /**
58
58
  * What an ABSENT network field means: Walrus.
59
59
  *
60
- * This is a fact, not a fallback. No other network has ever had an upload path, so every part,
61
- * entry and map written before the field existed is on Walrus by construction. Stated as a named
62
- * constant so the reasoning sits next to every use of it rather than being re-derived.
60
+ * This is a fact, not a fallback. No other network had an upload path until well after the field
61
+ * existed, so every part, entry and map written without it is on Walrus by construction. Stated as
62
+ * a named constant so the reasoning sits next to every use of it rather than being re-derived.
63
63
  */
64
64
  export const NETWORK_WHEN_UNRECORDED = NETWORK_WALRUS;
@@ -0,0 +1,28 @@
1
+ /** The tiers a person can pick, by the word every interface uses. */
2
+ export type StorageTier = "standard" | "heavy";
3
+ /** Every tier, in the order an interface lists them. */
4
+ export declare const STORAGE_TIERS: readonly StorageTier[];
5
+ /** What an upload uses when nobody chose. */
6
+ export declare const DEFAULT_STORAGE_TIER: StorageTier;
7
+ /** The storage network code a tier stores on. */
8
+ export declare function networkForTier(tier: StorageTier): number;
9
+ /**
10
+ * The tier a stored network code belongs to, or `null` for a code no tier uses.
11
+ *
12
+ * An absent network field means Walrus (every part written before the field existed is on Walrus),
13
+ * so `undefined` answers "standard". An unknown code answers `null` rather than a guess: a newer
14
+ * client may have stored something this one has never heard of, and calling it Standard would send
15
+ * a reader to the wrong network.
16
+ */
17
+ export declare function tierForNetwork(network: number | undefined): StorageTier | null;
18
+ /** Whether a string is one of the tier words, exactly as written. */
19
+ export declare function isStorageTier(value: unknown): value is StorageTier;
20
+ /**
21
+ * Read a tier word typed by a person (a `--tier` flag, an SDK option, a config file).
22
+ *
23
+ * Surrounding space and letter case are forgiven, because nothing else about the word is ambiguous.
24
+ * Anything else is refused with a message naming the accepted words — never mapped to the default,
25
+ * because a typo that silently stores a file on the other network is the one mistake a person cannot
26
+ * see afterwards.
27
+ */
28
+ export declare function parseStorageTier(raw: string): StorageTier;
@@ -0,0 +1,63 @@
1
+ // WHICH TIER a person picks, and which storage network that tier means — the one place the two
2
+ // are tied together.
3
+ //
4
+ // ⚠ THIS FILE IS PUBLISHED. It is copied byte-for-byte into the `nmts` command-line package.
5
+ // Keep the comments self-contained English, with no pointer only we can follow.
6
+ //
7
+ // A TIER IS A PRODUCT NAME; A NETWORK IS WHAT IS UNDERNEATH. A person chooses "NMTS Standard" or
8
+ // "NMTS Heavy" and never has to learn what Walrus or Filecoin is. Stored rows, sealed file lists and
9
+ // recovery maps record the NETWORK (`storage-network.ts`), because that is what a reader needs years
10
+ // later to find the bytes. Keeping the mapping here, and only here, means renaming a tier can never
11
+ // silently change where anybody's bytes are recorded to be.
12
+ //
13
+ // standard ↔ network 0 (Walrus) — erasure-coded across many storage nodes, paid per epoch
14
+ // heavy ↔ network 1 (Filecoin) — whole copies kept by two separate storage companies
15
+ //
16
+ // The same word is used everywhere: `--tier standard|heavy` on the command line, `tier:` in the SDK.
17
+ // The default is "standard".
18
+ import { NETWORK_FILECOIN, NETWORK_WALRUS } from "./storage-network.js";
19
+ /** Every tier, in the order an interface lists them. */
20
+ export const STORAGE_TIERS = ["standard", "heavy"];
21
+ /** What an upload uses when nobody chose. */
22
+ export const DEFAULT_STORAGE_TIER = "standard";
23
+ const NETWORK_OF = {
24
+ standard: NETWORK_WALRUS,
25
+ heavy: NETWORK_FILECOIN,
26
+ };
27
+ /** The storage network code a tier stores on. */
28
+ export function networkForTier(tier) {
29
+ return NETWORK_OF[tier];
30
+ }
31
+ /**
32
+ * The tier a stored network code belongs to, or `null` for a code no tier uses.
33
+ *
34
+ * An absent network field means Walrus (every part written before the field existed is on Walrus),
35
+ * so `undefined` answers "standard". An unknown code answers `null` rather than a guess: a newer
36
+ * client may have stored something this one has never heard of, and calling it Standard would send
37
+ * a reader to the wrong network.
38
+ */
39
+ export function tierForNetwork(network) {
40
+ if (network === undefined || network === NETWORK_WALRUS)
41
+ return "standard";
42
+ if (network === NETWORK_FILECOIN)
43
+ return "heavy";
44
+ return null;
45
+ }
46
+ /** Whether a string is one of the tier words, exactly as written. */
47
+ export function isStorageTier(value) {
48
+ return value === "standard" || value === "heavy";
49
+ }
50
+ /**
51
+ * Read a tier word typed by a person (a `--tier` flag, an SDK option, a config file).
52
+ *
53
+ * Surrounding space and letter case are forgiven, because nothing else about the word is ambiguous.
54
+ * Anything else is refused with a message naming the accepted words — never mapped to the default,
55
+ * because a typo that silently stores a file on the other network is the one mistake a person cannot
56
+ * see afterwards.
57
+ */
58
+ export function parseStorageTier(raw) {
59
+ const value = raw.trim().toLowerCase();
60
+ if (isStorageTier(value))
61
+ return value;
62
+ throw new RangeError(`Unknown storage tier "${raw}". Use "standard" or "heavy".`);
63
+ }
@@ -45,6 +45,7 @@ export function parseTokenAmountToBaseUnits(input, decimals = TOKEN_DECIMALS) {
45
45
  return BigInt(`${intPart === "" ? "0" : intPart}${frac}`);
46
46
  }
47
47
  catch {
48
+ // The pattern above admits only digits; a BigInt failure still means not an amount.
48
49
  return null;
49
50
  }
50
51
  }
@@ -94,4 +94,12 @@ export type FileUploadStep = {
94
94
  export declare function uploadFile(input: FileUploadInput): Promise<UploadResult>;
95
95
  /** The file key's records, so a caller can clear them once the list is written. */
96
96
  export declare function partKeysOf(fileKey: string, parts: number): string[];
97
+ /**
98
+ * The part's bytes, followed by zeros up to the length it will declare.
99
+ *
100
+ * ⛔ ZEROS APPENDED TO THE PLAINTEXT, NOT BYTES TACKED ONTO THE STORED STREAM. A stream's header is
101
+ * authenticated but not encrypted, so padding added after sealing would leave the real length
102
+ * legible in the header of a public object — which is the exact thing this is for.
103
+ */
104
+ export declare function padded(chunks: AsyncIterable<Uint8Array>, real: number, sealFrom: number): AsyncIterable<Uint8Array>;
97
105
  export { entryOf };
@@ -18,12 +18,12 @@
18
18
  import { sha256 } from "@noble/hashes/sha2.js";
19
19
  import { fromBase64Url } from "./bytes.js";
20
20
  import { AAD } from "./crypto.js";
21
- import { NmtsError } from "./errors.js";
22
21
  import { fileSecrets, sealPart } from "./seal.js";
23
- import { planParts } from "./shared/lib/upload/part-plan.js";
22
+ import { planFor } from "./upload-price.js";
24
23
  import { paddedPlaintextLen, } from "./shared/lib/crypto/size-padding.js";
25
24
  import { NCF3_SHAPE } from "./seal.js";
26
25
  import { buyAndPushPart, entryOf } from "./upload.js";
26
+ import { confirmParts, dropLeftovers, oneUploadAtATime, runIdFor } from "./upload-run.js";
27
27
  import { commitItem } from "./upload-steps.js";
28
28
  import { finishReservationKey, partKey, readItemRecord, readReservationBytes, readReservationRecord, startReservationKey, } from "./upload-store.js";
29
29
  /** How much plaintext is handed to the engine at a time. Matches the format's own chunk size. */
@@ -37,31 +37,39 @@ export const READ_CHUNK_BYTES = 4 * 2 ** 20;
37
37
  */
38
38
  export async function uploadFile(input) {
39
39
  const { dataKey, source, onStep } = input;
40
- if (source.size <= 0) {
41
- throw new NmtsError("An empty file cannot be uploaded.", {
42
- nextStep: "The storage network has nothing to store and would refuse the reservation.",
43
- });
44
- }
45
- const plan = planParts(source.size, input.partSize);
40
+ // ⚠ An empty file is one part that carries nothing — `planFor` says why it is a file at all.
41
+ const plan = planFor(source.size, input.partSize);
46
42
  onStep?.({ step: "planning", parts: plan.length, partSize: input.partSize });
47
43
  // ── pass one: the reservation key and the content hash, from a single read ──
48
44
  onStep?.({ step: "hashing", parts: plan.length });
49
45
  const keyHash = startReservationKey(dataKey);
50
46
  const contentDigest = await hashWhole(source, keyHash);
51
47
  const fileKey = finishReservationKey(keyHash, input.name, input.destination);
52
- // ⛔ ALREADY COMMITTED STOPS HERE, BEFORE ANY PART IS TOUCHED. A file that got as far as
53
- // `POST /v1/items` exists and is paid for; all that can still be missing is the account's own
54
- // list. Asking the server about every part again would be a round trip per part to learn
55
- // something the record already says.
56
- const committed = await readItemRecord(fileKey);
57
- if (committed?.itemId !== undefined) {
58
- const entry = await recordedEntry(fileKey, plan.length, source.size);
59
- if (entry !== null) {
60
- return { itemId: committed.itemId, resumed: true, ledgerIds: [], fileKey, parts: plan.length, entry };
48
+ // ⛔ FROM HERE TO THE COMMIT, ONE RUN OF THIS FILE AT A TIME in this process (`upload-run.ts`).
49
+ return oneUploadAtATime(fileKey, async () => {
50
+ await dropLeftovers(fileKey, plan.length);
51
+ const runId = await runIdFor(fileKey, plan.length);
52
+ // ⛔ ALREADY COMMITTED STOPS HERE, BEFORE ANY PART IS TOUCHED. A file that got as far as
53
+ // `POST /v1/items` exists and is paid for; all that can still be missing is the account's own
54
+ // list. Asking the server about every part again would be a round trip per part to learn
55
+ // something the record already says. ⚠ Only THIS upload's commit: a record naming another run
56
+ // is what an earlier upload of the same file left, and its item is not these bytes.
57
+ const committed = await readItemRecord(fileKey);
58
+ if (committed?.itemId !== undefined && (committed.runId === undefined || committed.runId === runId)) {
59
+ const entry = await recordedEntry(fileKey, plan.length, source.size);
60
+ if (entry !== null) {
61
+ return { itemId: committed.itemId, resumed: true, ledgerIds: [], fileKey, parts: plan.length, entry };
62
+ }
61
63
  }
62
- }
64
+ return uploadParts(input, { fileKey, runId, plan, contentDigest });
65
+ });
66
+ }
67
+ /** Seal, buy and push every part not yet bought, then commit them as one file. */
68
+ async function uploadParts(input, run) {
69
+ const { source, onStep } = input;
70
+ const { fileKey, runId, plan } = run;
63
71
  // ── the file's secrets: from the record if one exists, otherwise made now ──
64
- const secrets = await openSecrets(input, fileKey, plan.length, contentDigest);
72
+ const secrets = await openSecrets(input, fileKey, plan.length, run.contentDigest);
65
73
  try {
66
74
  const entry = {
67
75
  name: input.name,
@@ -93,6 +101,7 @@ export async function uploadFile(input) {
93
101
  paid.push(await (input.buy ?? buyAndPushPart)({
94
102
  api: input.api,
95
103
  protocol: input.protocol,
104
+ runId,
96
105
  key,
97
106
  sealed,
98
107
  relayUrl: input.relayUrl,
@@ -106,8 +115,10 @@ export async function uploadFile(input) {
106
115
  onStep: (step) => onStep?.({ ...step, partIndex: range.partIndex, parts: plan.length }),
107
116
  }));
108
117
  }
118
+ await confirmParts(fileKey, paid, entry);
109
119
  const itemId = await commitItem({
110
120
  api: input.api,
121
+ runId,
111
122
  epochs: input.epochs,
112
123
  currentEpoch: input.currentEpoch,
113
124
  entry,
@@ -164,11 +175,32 @@ async function sealPartOf(input, dek, range, parts, sealFrom) {
164
175
  parts,
165
176
  bytes: sealFrom,
166
177
  });
167
- return sealPart(input.crypt, dek, padded(input.source.read(range.offset, range.length), range.length, sealFrom), {
168
- index: range.partIndex,
169
- total: parts,
170
- plaintextLen: sealFrom,
171
- });
178
+ const placement = { index: range.partIndex, total: parts, plaintextLen: sealFrom };
179
+ if (sealFrom === 0)
180
+ return sealNothing(input.crypt, dek, placement);
181
+ return sealPart(input.crypt, dek, padded(input.source.read(range.offset, range.length), range.length, sealFrom), placement);
182
+ }
183
+ /**
184
+ * The one part of an empty file that nothing pads: a header and one empty final chunk (NCF-3 §4.1).
185
+ *
186
+ * ⚠ HERE ONLY BECAUSE `sealPart` REFUSES A ZERO LENGTH, from before an empty file could be stored.
187
+ * The engine seals it as it seals every stream — this opens a session, adds nothing and finishes —
188
+ * and once `sealPart` takes zero this can go. It is reached only when the account stores exact
189
+ * sizes: every other rule rounds an empty file up to the room its one credit already bought.
190
+ */
191
+ function sealNothing(crypt, dek, placement) {
192
+ const sealer = new crypt.StreamEncryptor(dek, 0, placement.index, placement.total);
193
+ try {
194
+ const header = sealer.header();
195
+ const end = sealer.finish();
196
+ const out = new Uint8Array(header.length + end.length);
197
+ out.set(header, 0);
198
+ out.set(end, header.length);
199
+ return out;
200
+ }
201
+ finally {
202
+ sealer.free();
203
+ }
172
204
  }
173
205
  /**
174
206
  * The part's bytes, followed by zeros up to the length it will declare.
@@ -177,7 +209,7 @@ async function sealPartOf(input, dek, range, parts, sealFrom) {
177
209
  * authenticated but not encrypted, so padding added after sealing would leave the real length
178
210
  * legible in the header of a public object — which is the exact thing this is for.
179
211
  */
180
- async function* padded(chunks, real, sealFrom) {
212
+ export async function* padded(chunks, real, sealFrom) {
181
213
  for await (const chunk of chunks)
182
214
  yield chunk;
183
215
  let left = sealFrom - real;
@@ -4,5 +4,8 @@
4
4
  * ⛔ IT IS NOT READ HERE. A file large enough to need several parts is a file too large to hold,
5
5
  * and the size is all that is needed to plan the upload and quote its price. The bytes are read
6
6
  * later, a slice at a time, by the part that is being sealed.
7
+ *
8
+ * ⚠ ZERO IS A SIZE. An empty file is sealed and stored like any other (`planFor` in
9
+ * `upload-price.ts`), so it is not refused here.
7
10
  */
8
11
  export declare function measureLocal(path: string): number;