@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.
- package/AGENTS.md +2 -2
- package/CHANGELOG.md +73 -0
- package/README.ko.md +6 -3
- package/README.md +15 -3
- package/dist/api-advice.js +17 -2
- package/dist/api.d.ts +22 -0
- package/dist/api.js +49 -1
- package/dist/arg-options.d.ts +14 -0
- package/dist/args.js +11 -2
- package/dist/artifact-about.d.ts +1 -1
- package/dist/collision.d.ts +20 -3
- package/dist/collision.js +21 -3
- package/dist/commands/erase.js +1 -0
- package/dist/commands/get.d.ts +2 -0
- package/dist/commands/get.js +13 -1
- package/dist/commands/handover.d.ts +13 -0
- package/dist/commands/handover.js +328 -0
- package/dist/commands/heavy-run.d.ts +59 -0
- package/dist/commands/heavy-run.js +247 -0
- package/dist/commands/heavy.d.ts +7 -0
- package/dist/commands/heavy.js +123 -0
- package/dist/commands/ls.d.ts +7 -0
- package/dist/commands/ls.js +27 -6
- package/dist/commands/public-code.d.ts +6 -0
- package/dist/commands/public-code.js +46 -0
- package/dist/commands/push-heavy.d.ts +6 -0
- package/dist/commands/push-heavy.js +128 -0
- package/dist/commands/push-tree.d.ts +24 -0
- package/dist/commands/push-tree.js +91 -0
- package/dist/commands/push.d.ts +4 -13
- package/dist/commands/push.js +7 -77
- package/dist/commands/put-heavy.d.ts +9 -0
- package/dist/commands/put-heavy.js +120 -0
- package/dist/commands/put-payer.d.ts +21 -0
- package/dist/commands/put-payer.js +24 -0
- package/dist/commands/put-thumbnail.d.ts +4 -0
- package/dist/commands/put-thumbnail.js +119 -0
- package/dist/commands/put-wallet.d.ts +2 -0
- package/dist/commands/put-wallet.js +3 -2
- package/dist/commands/put.d.ts +10 -2
- package/dist/commands/put.js +13 -7
- package/dist/commands/receive.js +3 -3
- package/dist/commands/s3.js +44 -10
- package/dist/crypto-surface.d.ts +8 -0
- package/dist/crypto-surface.js +3 -0
- package/dist/crypto.d.ts +3 -0
- package/dist/crypto.js +3 -0
- package/dist/download-part.d.ts +32 -1
- package/dist/download-part.js +95 -6
- package/dist/download-range.d.ts +19 -0
- package/dist/download-range.js +108 -0
- package/dist/download-sink.d.ts +15 -0
- package/dist/download.d.ts +14 -0
- package/dist/download.js +19 -4
- package/dist/drive-edit/trash.js +5 -3
- package/dist/drive-edit/tree.d.ts +6 -0
- package/dist/drive-edit/tree.js +10 -0
- package/dist/drive-erase.js +2 -2
- package/dist/drive-paths.d.ts +2 -2
- package/dist/drive-paths.js +2 -2
- package/dist/handover.d.ts +44 -0
- package/dist/handover.js +199 -0
- package/dist/heavy-api.d.ts +36 -0
- package/dist/heavy-api.js +107 -0
- package/dist/heavy-copy.d.ts +72 -0
- package/dist/heavy-copy.js +108 -0
- package/dist/heavy-evm.d.ts +20 -0
- package/dist/heavy-evm.js +106 -0
- package/dist/heavy-self-pay.d.ts +92 -0
- package/dist/heavy-self-pay.js +167 -0
- package/dist/heavy-upload.d.ts +87 -0
- package/dist/heavy-upload.js +154 -0
- package/dist/heavy-wallet-pay.d.ts +39 -0
- package/dist/heavy-wallet-pay.js +73 -0
- package/dist/help-options.js +8 -2
- package/dist/help.js +16 -3
- package/dist/list-tier.d.ts +7 -0
- package/dist/list-tier.js +47 -0
- package/dist/main.js +14 -9
- package/dist/manifest-write.d.ts +4 -3
- package/dist/mcp-tools/files.js +21 -2
- package/dist/mcp-tools/reads.js +9 -1
- package/dist/platform-sign.d.ts +4 -4
- package/dist/platform-sign.js +3 -3
- package/dist/portable.d.ts +17 -1
- package/dist/portable.js +13 -1
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/recovery-build.js +16 -0
- package/dist/recovery-map-part.d.ts +82 -0
- package/dist/recovery-map-part.js +83 -0
- package/dist/recovery-map.d.ts +5 -40
- package/dist/recovery-map.js +18 -1
- package/dist/recovery-source.d.ts +3 -0
- package/dist/recovery-source.js +9 -0
- package/dist/risk.d.ts +30 -0
- package/dist/risk.js +23 -2
- package/dist/run-log.d.ts +1 -1
- package/dist/run-log.js +7 -1
- package/dist/s3/address.d.ts +20 -0
- package/dist/s3/address.js +97 -0
- package/dist/s3/answer.d.ts +48 -0
- package/dist/s3/answer.js +330 -0
- package/dist/s3/body-deframe.d.ts +49 -0
- package/dist/s3/body-deframe.js +264 -0
- package/dist/s3/body-measure.d.ts +13 -0
- package/dist/s3/body-measure.js +48 -0
- package/dist/s3/body-plan.d.ts +31 -0
- package/dist/s3/body-plan.js +159 -0
- package/dist/s3/body.d.ts +46 -0
- package/dist/s3/body.js +117 -0
- package/dist/s3/call.d.ts +36 -0
- package/dist/s3/call.js +42 -0
- package/dist/s3/checksum.d.ts +26 -0
- package/dist/s3/checksum.js +176 -0
- package/dist/s3/content-type.d.ts +3 -0
- package/dist/s3/content-type.js +49 -0
- package/dist/s3/contract.d.ts +91 -14
- package/dist/s3/drive-conditions.d.ts +47 -0
- package/dist/s3/drive-conditions.js +134 -0
- package/dist/s3/drive-key.d.ts +11 -0
- package/dist/s3/drive-key.js +58 -0
- package/dist/s3/drive-limits.d.ts +33 -0
- package/dist/s3/drive-limits.js +53 -0
- package/dist/s3/drive-lock.d.ts +11 -0
- package/dist/s3/drive-lock.js +47 -0
- package/dist/s3/drive-trashed-name.d.ts +13 -0
- package/dist/s3/drive-trashed-name.js +59 -0
- package/dist/s3/drive-write.d.ts +38 -0
- package/dist/s3/drive-write.js +258 -0
- package/dist/s3/drive.d.ts +59 -25
- package/dist/s3/drive.js +46 -77
- package/dist/s3/listing.d.ts +27 -1
- package/dist/s3/listing.js +62 -20
- package/dist/s3/long-answer.d.ts +12 -0
- package/dist/s3/long-answer.js +104 -0
- package/dist/s3/multipart-assemble.d.ts +17 -0
- package/dist/s3/multipart-assemble.js +68 -0
- package/dist/s3/multipart-sweep.d.ts +15 -0
- package/dist/s3/multipart-sweep.js +87 -0
- package/dist/s3/multipart-view.d.ts +45 -0
- package/dist/s3/multipart-view.js +232 -0
- package/dist/s3/multipart.d.ts +26 -19
- package/dist/s3/multipart.js +193 -78
- package/dist/s3/object-read.d.ts +17 -0
- package/dist/s3/object-read.js +127 -0
- package/dist/s3/object-write.d.ts +35 -0
- package/dist/s3/object-write.js +198 -0
- package/dist/s3/range.d.ts +14 -0
- package/dist/s3/range.js +80 -0
- package/dist/s3/response-sink.d.ts +12 -2
- package/dist/s3/response-sink.js +71 -7
- package/dist/s3/routes.d.ts +0 -1
- package/dist/s3/routes.js +242 -217
- package/dist/s3/same-file.d.ts +4 -1
- package/dist/s3/same-file.js +12 -3
- package/dist/s3/server.d.ts +54 -4
- package/dist/s3/server.js +174 -14
- package/dist/s3/sigv4-canonical.d.ts +91 -0
- package/dist/s3/sigv4-canonical.js +148 -0
- package/dist/s3/sigv4-presigned.d.ts +13 -0
- package/dist/s3/sigv4-presigned.js +82 -0
- package/dist/s3/sigv4.d.ts +26 -27
- package/dist/s3/sigv4.js +137 -102
- package/dist/s3/spool.d.ts +37 -0
- package/dist/s3/spool.js +104 -0
- package/dist/s3/staging.d.ts +90 -7
- package/dist/s3/staging.js +97 -66
- package/dist/s3/xml-multipart.d.ts +38 -0
- package/dist/s3/xml-multipart.js +47 -0
- package/dist/s3/xml-read.d.ts +28 -0
- package/dist/s3/xml-read.js +235 -0
- package/dist/s3/xml.d.ts +45 -2
- package/dist/s3/xml.js +72 -13
- package/dist/s3-gateway.d.ts +11 -5
- package/dist/s3-gateway.js +5 -2
- package/dist/seal.d.ts +1 -1
- package/dist/share.d.ts +5 -0
- package/dist/share.js +1 -1
- package/dist/shared/lib/api/types-heavy.d.ts +167 -0
- package/dist/shared/lib/api/types-heavy.js +60 -0
- package/dist/shared/lib/drive/manifest-codec.d.ts +11 -31
- package/dist/shared/lib/drive/manifest-codec.js +15 -23
- package/dist/shared/lib/drive/manifest-index.d.ts +10 -1
- package/dist/shared/lib/drive/manifest-index.js +30 -8
- package/dist/shared/lib/drive/manifest-wire-extras.d.ts +50 -0
- package/dist/shared/lib/drive/manifest-wire-extras.js +65 -0
- package/dist/shared/lib/drive/preview-classify.d.ts +17 -0
- package/dist/shared/lib/drive/preview-classify.js +96 -0
- package/dist/shared/lib/drive/rebuild-verify.js +1 -0
- package/dist/shared/lib/filecoin/providers.d.ts +25 -0
- package/dist/shared/lib/filecoin/providers.js +58 -0
- package/dist/shared/lib/heavy/download.d.ts +48 -0
- package/dist/shared/lib/heavy/download.js +116 -0
- package/dist/shared/lib/heavy/order-plan.d.ts +77 -0
- package/dist/shared/lib/heavy/order-plan.js +83 -0
- package/dist/shared/lib/heavy/order-runner.d.ts +9 -0
- package/dist/shared/lib/heavy/order-runner.js +300 -0
- package/dist/shared/lib/heavy/order-types.d.ts +90 -0
- package/dist/shared/lib/heavy/order-types.js +6 -0
- package/dist/shared/lib/heavy/order-wire.d.ts +55 -0
- package/dist/shared/lib/heavy/order-wire.js +113 -0
- package/dist/shared/lib/heavy/piece-cid.d.ts +24 -0
- package/dist/shared/lib/heavy/piece-cid.js +55 -0
- package/dist/shared/lib/heavy/sp-upload.d.ts +37 -0
- package/dist/shared/lib/heavy/sp-upload.js +112 -0
- package/dist/shared/lib/heavy/synapse-piece.d.ts +1 -0
- package/dist/shared/lib/heavy/synapse-piece.js +8 -0
- package/dist/shared/lib/share/handover-format.d.ts +129 -0
- package/dist/shared/lib/share/handover-format.js +323 -0
- package/dist/shared/lib/share/shared-file-info.js +1 -0
- package/dist/shared/lib/storage-network.d.ts +4 -4
- package/dist/shared/lib/storage-network.js +7 -7
- package/dist/shared/lib/storage-tier.d.ts +28 -0
- package/dist/shared/lib/storage-tier.js +63 -0
- package/dist/shared/lib/wallet/send-rules.js +1 -0
- package/dist/upload-file.d.ts +8 -0
- package/dist/upload-file.js +57 -25
- package/dist/upload-price-node.d.ts +3 -0
- package/dist/upload-price-node.js +3 -6
- package/dist/upload-price.d.ts +10 -0
- package/dist/upload-price.js +13 -1
- package/dist/upload-run.d.ts +80 -0
- package/dist/upload-run.js +173 -0
- package/dist/upload-steps.js +15 -4
- package/dist/upload-store.d.ts +15 -6
- package/dist/upload-store.js +6 -0
- package/dist/upload-wallet-put.d.ts +11 -4
- package/dist/upload-wallet-put.js +17 -6
- package/dist/upload-wallet.js +4 -0
- package/dist/upload-wire.d.ts +25 -0
- package/dist/upload-wire.js +20 -1
- package/dist/upload.js +28 -13
- package/dist/wallet-sign.js +2 -2
- package/docs/commands/get.md +3 -0
- package/docs/commands/handover.md +38 -0
- package/docs/commands/heavy.md +35 -0
- package/docs/commands/ls.md +5 -0
- package/docs/commands/public-code.md +7 -0
- package/docs/commands/push.md +4 -1
- package/docs/commands/put.md +23 -1
- package/docs/commands/share.md +3 -1
- package/package.json +5 -2
- package/server.json +2 -2
- package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +17 -1
- package/vendor/nmts-crypto/nmts_crypto_wasm.js +28 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +2 -1
|
@@ -16,7 +16,23 @@ export function buildIndex(entries) {
|
|
|
16
16
|
else
|
|
17
17
|
childrenByParent.set(key, [e]);
|
|
18
18
|
}
|
|
19
|
-
|
|
19
|
+
const hidden = new Set();
|
|
20
|
+
const previews = new Map();
|
|
21
|
+
for (const e of entries) {
|
|
22
|
+
if (e.thumbOf === undefined || e.thumbOf === e.id || byId.get(e.thumbOf)?.kind !== KIND_FILE)
|
|
23
|
+
continue;
|
|
24
|
+
hidden.add(e.id);
|
|
25
|
+
const bucket = previews.get(e.thumbOf);
|
|
26
|
+
if (bucket)
|
|
27
|
+
bucket.push(e.id);
|
|
28
|
+
else
|
|
29
|
+
previews.set(e.thumbOf, [e.id]);
|
|
30
|
+
}
|
|
31
|
+
return { all: entries, byId, childrenByParent, hidden, previews };
|
|
32
|
+
}
|
|
33
|
+
/** Would a view show this entry at all? False only for a preview picture whose video is listed. */
|
|
34
|
+
export function shown(index, entry) {
|
|
35
|
+
return !index.hidden.has(entry.id);
|
|
20
36
|
}
|
|
21
37
|
/**
|
|
22
38
|
* The instant this item became trash — its own, or the nearest trashed ancestor's.
|
|
@@ -54,7 +70,7 @@ export function isLive(index, entry) {
|
|
|
54
70
|
* writes the live drive, so a drive whose every file is in the trash has nothing to export.
|
|
55
71
|
*/
|
|
56
72
|
export function hasLiveFile(index) {
|
|
57
|
-
return index.all.some((e) => e.kind === KIND_FILE && isLive(index, e));
|
|
73
|
+
return index.all.some((e) => e.kind === KIND_FILE && shown(index, e) && isLive(index, e));
|
|
58
74
|
}
|
|
59
75
|
/**
|
|
60
76
|
* Live children of a folder (`null` = drive root), in list order.
|
|
@@ -64,7 +80,7 @@ export function childrenOf(index, parentId) {
|
|
|
64
80
|
const bucket = index.childrenByParent.get(parentId ?? ROOT);
|
|
65
81
|
if (!bucket)
|
|
66
82
|
return [];
|
|
67
|
-
return bucket.filter((e) => isLive(index, e));
|
|
83
|
+
return bucket.filter((e) => shown(index, e) && isLive(index, e));
|
|
68
84
|
}
|
|
69
85
|
/**
|
|
70
86
|
* What the trash view shows: items the person deleted directly, newest first.
|
|
@@ -72,7 +88,7 @@ export function childrenOf(index, parentId) {
|
|
|
72
88
|
*/
|
|
73
89
|
export function trashRoots(index) {
|
|
74
90
|
const roots = index.all.filter((e) => {
|
|
75
|
-
if (e.deletedAt === undefined)
|
|
91
|
+
if (e.deletedAt === undefined || !shown(index, e))
|
|
76
92
|
return false;
|
|
77
93
|
if (e.parentId === null)
|
|
78
94
|
return true;
|
|
@@ -159,7 +175,7 @@ export function searchByName(index, query, limit = 500) {
|
|
|
159
175
|
break;
|
|
160
176
|
if (!e.name.toLocaleLowerCase().includes(needle))
|
|
161
177
|
continue;
|
|
162
|
-
if (!isLive(index, e))
|
|
178
|
+
if (!shown(index, e) || !isLive(index, e))
|
|
163
179
|
continue;
|
|
164
180
|
out.push(e);
|
|
165
181
|
}
|
|
@@ -173,7 +189,7 @@ export function searchByName(index, query, limit = 500) {
|
|
|
173
189
|
*/
|
|
174
190
|
export function favoriteFiles(index) {
|
|
175
191
|
return index.all
|
|
176
|
-
.filter((e) => e.kind === KIND_FILE && e.favorite === true && isLive(index, e))
|
|
192
|
+
.filter((e) => e.kind === KIND_FILE && e.favorite === true && shown(index, e) && isLive(index, e))
|
|
177
193
|
.slice()
|
|
178
194
|
.sort((a, b) => b.updatedAt - a.updatedAt);
|
|
179
195
|
}
|
|
@@ -188,7 +204,7 @@ export function labelCounts(index) {
|
|
|
188
204
|
for (const e of index.all) {
|
|
189
205
|
if (e.kind !== KIND_FILE || !e.labels || e.labels.length === 0)
|
|
190
206
|
continue;
|
|
191
|
-
if (!isLive(index, e))
|
|
207
|
+
if (!shown(index, e) || !isLive(index, e))
|
|
192
208
|
continue;
|
|
193
209
|
for (const label of e.labels)
|
|
194
210
|
counts.set(label, (counts.get(label) ?? 0) + 1);
|
|
@@ -202,11 +218,15 @@ export function filesWithLabel(index, label) {
|
|
|
202
218
|
return index.all
|
|
203
219
|
.filter((e) => e.kind === KIND_FILE &&
|
|
204
220
|
(e.labels ?? []).includes(label) &&
|
|
221
|
+
shown(index, e) &&
|
|
205
222
|
isLive(index, e))
|
|
206
223
|
.slice()
|
|
207
224
|
.sort((a, b) => b.updatedAt - a.updatedAt);
|
|
208
225
|
}
|
|
209
|
-
/**
|
|
226
|
+
/**
|
|
227
|
+
* Whole-drive counts. Exact, because the list is complete by construction.
|
|
228
|
+
* ⚠ A hidden preview picture is not counted: the totals describe what the lists show.
|
|
229
|
+
*/
|
|
210
230
|
export function totalsOf(index) {
|
|
211
231
|
const totals = {
|
|
212
232
|
files: 0,
|
|
@@ -216,6 +236,8 @@ export function totalsOf(index) {
|
|
|
216
236
|
trashedBytes: 0,
|
|
217
237
|
};
|
|
218
238
|
for (const e of index.all) {
|
|
239
|
+
if (!shown(index, e))
|
|
240
|
+
continue;
|
|
219
241
|
const live = isLive(index, e);
|
|
220
242
|
if (e.kind === KIND_FOLDER) {
|
|
221
243
|
if (live)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One share this device made, kept where the server cannot reach it (`ManifestEntry.shares`).
|
|
3
|
+
*
|
|
4
|
+
* A receipt is written only AFTER the server's created row was checked to carry the very address
|
|
5
|
+
* the sender typed — so what is stored is the address she asked for, never one the server chose.
|
|
6
|
+
*/
|
|
7
|
+
export interface ShareReceipt {
|
|
8
|
+
/** Recipient address in WIRE form: exactly what the create call was checked against. */
|
|
9
|
+
address: string;
|
|
10
|
+
/** When this device wrote the receipt, ms since the Unix epoch. This browser's clock. */
|
|
11
|
+
at: number;
|
|
12
|
+
/**
|
|
13
|
+
* A revoke was sent for this receipt and the listing has not yet come back without the row.
|
|
14
|
+
*
|
|
15
|
+
* The receipt outlives the revoke ON PURPOSE: a revoke this side cannot verify is exactly the
|
|
16
|
+
* case worth keeping, and a listing that still carries the address is the only evidence the
|
|
17
|
+
* removal did not happen. Dropped once a listing no longer names it (`sharePrune`), which is
|
|
18
|
+
* what keeps this array from growing forever.
|
|
19
|
+
*/
|
|
20
|
+
revoked?: true;
|
|
21
|
+
}
|
|
22
|
+
/** One share receipt on the wire. Same short-key reason as the entry it sits on. */
|
|
23
|
+
export interface WireShareReceipt {
|
|
24
|
+
/** address. */
|
|
25
|
+
a: string;
|
|
26
|
+
/** at. */
|
|
27
|
+
t: number;
|
|
28
|
+
/** revoked. */
|
|
29
|
+
r?: 1;
|
|
30
|
+
}
|
|
31
|
+
export declare function sharesToWire(shares: readonly ShareReceipt[]): WireShareReceipt[];
|
|
32
|
+
/**
|
|
33
|
+
* Receipts that can be checked, or `undefined` when none can.
|
|
34
|
+
*
|
|
35
|
+
* Defensive for a sharp reason: a receipt with a blank address or a broken instant would be
|
|
36
|
+
* compared against the server's rows and could produce a warning about a share nobody ever made.
|
|
37
|
+
* Anything unusable is dropped — a receipt that cannot be checked says nothing, and saying nothing
|
|
38
|
+
* is the honest outcome.
|
|
39
|
+
*/
|
|
40
|
+
export declare function sharesFromWire(raw: unknown): ShareReceipt[] | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Every entry key this build reads. ⛔ A key added to `WireEntry` and not here would ALSO be
|
|
43
|
+
* carried, and a mark this build clears would come back from the carried copy on the next save —
|
|
44
|
+
* the codec test pins the two lists together.
|
|
45
|
+
*/
|
|
46
|
+
export declare const KNOWN_ENTRY_KEYS: ReadonlySet<string>;
|
|
47
|
+
/** The keys of a wire entry this build does not know, or `undefined` when there are none. */
|
|
48
|
+
export declare function carriedFromWire(w: object): Readonly<Record<string, unknown>> | undefined;
|
|
49
|
+
/** `w` with the carried keys laid UNDER it — a key this build writes always wins over one it carries. */
|
|
50
|
+
export declare function withCarried<T extends object>(w: T, carried: Readonly<Record<string, unknown>> | undefined): T;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// NMF-1 — the parts of an entry's wire form that are not its core facts. ⚠ PUBLISHED — copied
|
|
2
|
+
// byte-for-byte into the `nmts` command-line package with the codec that imports it; keep comments
|
|
3
|
+
// self-contained English.
|
|
4
|
+
//
|
|
5
|
+
// Two things live here, apart from `manifest-codec.ts` only because that file is at its length
|
|
6
|
+
// ceiling: the share receipts (their own small wire shape) and the CARRY rule for keys this build
|
|
7
|
+
// does not know.
|
|
8
|
+
//
|
|
9
|
+
// THE CARRY RULE. An entry key this build has never heard of is kept verbatim on the entry and
|
|
10
|
+
// written back on the next save. Before it, a key was dropped when read, so an older build that
|
|
11
|
+
// SAVED rewrote every entry without the newer build's fields — a stale tab could clear stars,
|
|
12
|
+
// labels and anything added later. ⚠ It protects only from THIS build onward: a build without
|
|
13
|
+
// this rule still drops what it does not know. That is why every optional field must still be
|
|
14
|
+
// safe to lose (see `MANIFEST_FORMAT_VERSION`).
|
|
15
|
+
export function sharesToWire(shares) {
|
|
16
|
+
return shares.map((r) => (r.revoked ? { a: r.address, t: r.at, r: 1 } : { a: r.address, t: r.at }));
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Receipts that can be checked, or `undefined` when none can.
|
|
20
|
+
*
|
|
21
|
+
* Defensive for a sharp reason: a receipt with a blank address or a broken instant would be
|
|
22
|
+
* compared against the server's rows and could produce a warning about a share nobody ever made.
|
|
23
|
+
* Anything unusable is dropped — a receipt that cannot be checked says nothing, and saying nothing
|
|
24
|
+
* is the honest outcome.
|
|
25
|
+
*/
|
|
26
|
+
export function sharesFromWire(raw) {
|
|
27
|
+
if (!Array.isArray(raw))
|
|
28
|
+
return undefined;
|
|
29
|
+
const clean = [];
|
|
30
|
+
for (const item of raw) {
|
|
31
|
+
if (!item || typeof item !== "object")
|
|
32
|
+
continue;
|
|
33
|
+
const a = "a" in item ? item.a : undefined;
|
|
34
|
+
const t = "t" in item ? item.t : undefined;
|
|
35
|
+
const r = "r" in item ? item.r : undefined;
|
|
36
|
+
if (typeof a !== "string" || a === "")
|
|
37
|
+
continue;
|
|
38
|
+
if (typeof t !== "number" || !Number.isFinite(t))
|
|
39
|
+
continue;
|
|
40
|
+
clean.push(r === 1 ? { address: a, at: t, revoked: true } : { address: a, at: t });
|
|
41
|
+
}
|
|
42
|
+
return clean.length > 0 ? clean : undefined;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Every entry key this build reads. ⛔ A key added to `WireEntry` and not here would ALSO be
|
|
46
|
+
* carried, and a mark this build clears would come back from the carried copy on the next save —
|
|
47
|
+
* the codec test pins the two lists together.
|
|
48
|
+
*/
|
|
49
|
+
export const KNOWN_ENTRY_KEYS = new Set([
|
|
50
|
+
"i", "p", "k", "n", "s", "c", "u", "d", "w", "h", "f", "pn", "l", "sn", "sh", "to",
|
|
51
|
+
]);
|
|
52
|
+
/** The keys of a wire entry this build does not know, or `undefined` when there are none. */
|
|
53
|
+
export function carriedFromWire(w) {
|
|
54
|
+
let carried;
|
|
55
|
+
for (const [key, value] of Object.entries(w)) {
|
|
56
|
+
if (KNOWN_ENTRY_KEYS.has(key))
|
|
57
|
+
continue;
|
|
58
|
+
(carried ??= {})[key] = value;
|
|
59
|
+
}
|
|
60
|
+
return carried;
|
|
61
|
+
}
|
|
62
|
+
/** `w` with the carried keys laid UNDER it — a key this build writes always wins over one it carries. */
|
|
63
|
+
export function withCarried(w, carried) {
|
|
64
|
+
return carried ? { ...carried, ...w } : w;
|
|
65
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** How a file may be shown. `none` means "not previewed", which is a valid and often correct answer. */
|
|
2
|
+
export type PreviewKind = "image" | "video" | "audio" | "text" | "pdf" | "none";
|
|
3
|
+
/**
|
|
4
|
+
* Decide how a file named `name` may be shown, from its extension alone.
|
|
5
|
+
*
|
|
6
|
+
* Extension-only is deliberate: the alternative is sniffing the decrypted bytes, which means
|
|
7
|
+
* deciding "is this really a PNG?" on content an attacker chose. An extension the person can see
|
|
8
|
+
* in their own file list is a worse signal about the format and a much better one about intent —
|
|
9
|
+
* and every branch below is safe even when the extension lies, because none of them hands the
|
|
10
|
+
* bytes to a renderer that would execute them.
|
|
11
|
+
*
|
|
12
|
+
* Unknown ⇒ `none`. The default must be refusal, never a guess about whether a format can execute.
|
|
13
|
+
*/
|
|
14
|
+
export declare function classify(name: string): {
|
|
15
|
+
kind: PreviewKind;
|
|
16
|
+
mime: string;
|
|
17
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// The one decision that decides whether a previewed file can run code. Split out of
|
|
2
|
+
// FilePreview.tsx during the E-stage audit (2026-07-28) so it can be tested on every commit —
|
|
3
|
+
// `node --test` cannot load a `.tsx`, which is the whole reason this rule went untested for as
|
|
4
|
+
// long as it did.
|
|
5
|
+
//
|
|
6
|
+
// WHY IT MATTERS MORE HERE THAN IN AN ORDINARY VIEWER. A file in this drive can come from ANOTHER
|
|
7
|
+
// PERSON (private shares). Rendering one as a live document would run it in OUR origin, and this
|
|
8
|
+
// origin's IndexedDB is where "remember this device" keeps the account code. Stored XSS on an E2EE
|
|
9
|
+
// drive is not defacement — it is key theft, and the account code has no reset (GUIDE invariant
|
|
10
|
+
// #10).
|
|
11
|
+
//
|
|
12
|
+
// The rendering side of the contract, in FilePreview.tsx:
|
|
13
|
+
// · image → `<img src=blob:>` — an <img> never executes its contents, whatever the bytes are
|
|
14
|
+
// · text → textContent in `<pre>` — React escapes; NEVER innerHTML
|
|
15
|
+
// · pdf → `<iframe sandbox="">` — no scripts, opaque origin, download fallback beside it
|
|
16
|
+
// · video → `<video src=blob:>` — a media element plays its bytes, it never executes them
|
|
17
|
+
// · audio → `<audio src=blob:>` — the same, for sound
|
|
18
|
+
// · none → not previewed at all — the honest fallback points at the download
|
|
19
|
+
//
|
|
20
|
+
// ⭐ 2026-09-22 (C30 ①): video and audio joined so the gallery (image ∪ video) and the music place
|
|
21
|
+
// (audio) can gather files by kind. Stage ① gives them no player yet — FilePreview still treats
|
|
22
|
+
// both as "not previewed" until the streaming stages land.
|
|
23
|
+
//
|
|
24
|
+
// Pure: no DOM, no I/O, no state.
|
|
25
|
+
// ⚠ THE ORDER INSIDE `classify` IS THE SAFETY PROPERTY, not this table's contents.
|
|
26
|
+
// `svg` is present here, and only the early return below keeps it out of an
|
|
27
|
+
// `<img src=blob:image/svg+xml>`. Reorder those two lines — or delete the early return as
|
|
28
|
+
// "dead code, svg is right there in the table" — and an SVG someone else sent becomes a live
|
|
29
|
+
// document that can read this origin. `web/test/preview-classify.test.ts` makes that fail.
|
|
30
|
+
const IMAGE_MIME = {
|
|
31
|
+
png: "image/png",
|
|
32
|
+
jpg: "image/jpeg",
|
|
33
|
+
jpeg: "image/jpeg",
|
|
34
|
+
gif: "image/gif",
|
|
35
|
+
webp: "image/webp",
|
|
36
|
+
avif: "image/avif",
|
|
37
|
+
bmp: "image/bmp",
|
|
38
|
+
svg: "image/svg+xml",
|
|
39
|
+
};
|
|
40
|
+
// ⚠ No video/audio format can carry script into a `<video>`/`<audio>` element, and none of these
|
|
41
|
+
// extensions is also a document format — which is what lets them sit AFTER the svg early return
|
|
42
|
+
// without an early return of their own.
|
|
43
|
+
const VIDEO_MIME = {
|
|
44
|
+
mp4: "video/mp4",
|
|
45
|
+
m4v: "video/mp4",
|
|
46
|
+
mov: "video/quicktime",
|
|
47
|
+
webm: "video/webm",
|
|
48
|
+
mkv: "video/x-matroska",
|
|
49
|
+
};
|
|
50
|
+
const AUDIO_MIME = {
|
|
51
|
+
mp3: "audio/mpeg",
|
|
52
|
+
m4a: "audio/mp4",
|
|
53
|
+
aac: "audio/aac",
|
|
54
|
+
flac: "audio/flac",
|
|
55
|
+
ogg: "audio/ogg",
|
|
56
|
+
opus: "audio/ogg",
|
|
57
|
+
wav: "audio/wav",
|
|
58
|
+
};
|
|
59
|
+
// HTML/SVG source files are TEXT here on purpose — shown as source, never rendered as a document.
|
|
60
|
+
const TEXT_EXT = new Set([
|
|
61
|
+
"txt", "text", "md", "markdown", "csv", "tsv", "json", "log", "xml",
|
|
62
|
+
"yaml", "yml", "ini", "conf", "toml", "js", "mjs", "ts", "css", "html", "htm", "svg",
|
|
63
|
+
]);
|
|
64
|
+
/**
|
|
65
|
+
* Decide how a file named `name` may be shown, from its extension alone.
|
|
66
|
+
*
|
|
67
|
+
* Extension-only is deliberate: the alternative is sniffing the decrypted bytes, which means
|
|
68
|
+
* deciding "is this really a PNG?" on content an attacker chose. An extension the person can see
|
|
69
|
+
* in their own file list is a worse signal about the format and a much better one about intent —
|
|
70
|
+
* and every branch below is safe even when the extension lies, because none of them hands the
|
|
71
|
+
* bytes to a renderer that would execute them.
|
|
72
|
+
*
|
|
73
|
+
* Unknown ⇒ `none`. The default must be refusal, never a guess about whether a format can execute.
|
|
74
|
+
*/
|
|
75
|
+
export function classify(name) {
|
|
76
|
+
const dot = name.lastIndexOf(".");
|
|
77
|
+
const ext = dot >= 0 ? name.slice(dot + 1).toLowerCase() : "";
|
|
78
|
+
if (ext === "svg")
|
|
79
|
+
return { kind: "text", mime: "text/plain" }; // source, not a rendered SVG
|
|
80
|
+
// Read through `hasOwn` so `constructor` or `__proto__` is never an extension; the second check is
|
|
81
|
+
// for the command-line build, which reads every index as possibly missing.
|
|
82
|
+
const image = Object.hasOwn(IMAGE_MIME, ext) ? IMAGE_MIME[ext] : undefined;
|
|
83
|
+
if (image !== undefined)
|
|
84
|
+
return { kind: "image", mime: image };
|
|
85
|
+
const video = Object.hasOwn(VIDEO_MIME, ext) ? VIDEO_MIME[ext] : undefined;
|
|
86
|
+
if (video !== undefined)
|
|
87
|
+
return { kind: "video", mime: video };
|
|
88
|
+
const audio = Object.hasOwn(AUDIO_MIME, ext) ? AUDIO_MIME[ext] : undefined;
|
|
89
|
+
if (audio !== undefined)
|
|
90
|
+
return { kind: "audio", mime: audio };
|
|
91
|
+
if (ext === "pdf")
|
|
92
|
+
return { kind: "pdf", mime: "application/pdf" };
|
|
93
|
+
if (TEXT_EXT.has(ext))
|
|
94
|
+
return { kind: "text", mime: "text/plain" };
|
|
95
|
+
return { kind: "none", mime: "application/octet-stream" };
|
|
96
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** A Filecoin chain NMTS Heavy can run on. */
|
|
2
|
+
export type FilecoinChain = "calibration" | "mainnet";
|
|
3
|
+
/** One storage company NMTS may place a copy with. */
|
|
4
|
+
export interface FilecoinProvider {
|
|
5
|
+
/** The company's id in the chain's service-provider registry. */
|
|
6
|
+
readonly id: bigint;
|
|
7
|
+
/** The host of its storage service (`https://<host>`); uploads and reads go here. */
|
|
8
|
+
readonly host: string;
|
|
9
|
+
/** Who runs it. Two copies of one file never go to the same operator when another exists. */
|
|
10
|
+
readonly operator: string;
|
|
11
|
+
/** Whether the chain's endorsement list names it. The first copy always goes to an endorsed one. */
|
|
12
|
+
readonly endorsed: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** The allowlist, per chain, in preference order. */
|
|
15
|
+
export declare const FILECOIN_PROVIDERS: Readonly<Record<FilecoinChain, readonly FilecoinProvider[]>>;
|
|
16
|
+
/**
|
|
17
|
+
* Which Filecoin chain goes with which NMTS network: test data stays on test networks.
|
|
18
|
+
*
|
|
19
|
+
* Keyed by the NMTS network name ("testnet" | "mainnet") so that this file needs no import.
|
|
20
|
+
*/
|
|
21
|
+
export declare const FILECOIN_CHAIN_FOR_NETWORK: Readonly<Record<"testnet" | "mainnet", FilecoinChain>>;
|
|
22
|
+
/** The `https://<host>` origins of one chain's list, in order. */
|
|
23
|
+
export declare function filecoinProviderOrigins(chain: FilecoinChain): string[];
|
|
24
|
+
/** The allowlisted company serving `host` on `chain`, or `null` when the host is not on the list. */
|
|
25
|
+
export declare function providerForHost(chain: FilecoinChain, host: string): FilecoinProvider | null;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// The Filecoin storage companies NMTS Heavy may place a file with, per Filecoin chain.
|
|
2
|
+
//
|
|
3
|
+
// ⚠ THIS FILE IS PUBLISHED. It is copied byte-for-byte into the `nmts` command-line package, so
|
|
4
|
+
// its comments stay self-contained English and it imports nothing.
|
|
5
|
+
//
|
|
6
|
+
// WHY A FIXED LIST AND NOT "EVERY APPROVED COMPANY ON CHAIN". A browser may only talk to hosts its
|
|
7
|
+
// Content-Security-Policy names, and the policy is written at build time. Accepting whatever the
|
|
8
|
+
// chain approves at the moment of an upload would mean opening the policy to any host at all. So
|
|
9
|
+
// the candidates are this list; adding one is a code change and a release. The chain's own
|
|
10
|
+
// approved set still applies on top: a company here that the chain no longer approves is skipped.
|
|
11
|
+
//
|
|
12
|
+
// HOW A PAIR IS CHOSEN (the server does it, from this same list): the first copy goes to an
|
|
13
|
+
// endorsed company, the second to a company run by a DIFFERENT operator, so that one operator
|
|
14
|
+
// failing never takes both copies. `operator` exists only to make that comparison.
|
|
15
|
+
//
|
|
16
|
+
// ⛔ THE SERVER KEEPS AN IDENTICAL TABLE. Both sides must agree on every row, or the server places a
|
|
17
|
+
// copy with a company the browser is not allowed to read from. A test compares the two files.
|
|
18
|
+
//
|
|
19
|
+
// Recorded 2026-09-23 on Filecoin Calibration: approved providers 2, 4 and 9; endorsed 4 and 9;
|
|
20
|
+
// 2 and 9 are run by the same operator.
|
|
21
|
+
// Recorded 2026-09-26 on Filecoin mainnet: approved providers 1, 5, 7, 32, 36 and 37; endorsed 1, 5,
|
|
22
|
+
// 7 and 32; 1 and 32 are run by one operator, 36 and 37 by another. Every one answers the browser's
|
|
23
|
+
// cross-origin upload and read requests. A company that stops answering is skipped when a pair is
|
|
24
|
+
// chosen, not removed from this list.
|
|
25
|
+
/** The allowlist, per chain, in preference order. */
|
|
26
|
+
export const FILECOIN_PROVIDERS = {
|
|
27
|
+
calibration: [
|
|
28
|
+
{ id: 4n, host: "caliberation-pdp.infrafolio.com", operator: "infrafolio", endorsed: true },
|
|
29
|
+
{ id: 9n, host: "calib.ezpdpz.net", operator: "ezpdpz", endorsed: true },
|
|
30
|
+
{ id: 2n, host: "calib2.ezpdpz.net", operator: "ezpdpz", endorsed: false },
|
|
31
|
+
],
|
|
32
|
+
mainnet: [
|
|
33
|
+
{ id: 1n, host: "main.ezpdpz.net", operator: "ezpdpz", endorsed: true },
|
|
34
|
+
{ id: 5n, host: "pdp.lotus.dedyn.io", operator: "mongo2stor", endorsed: true },
|
|
35
|
+
{ id: 7n, host: "mainnet-pdp.infrafolio.com", operator: "infrafolio", endorsed: true },
|
|
36
|
+
{ id: 32n, host: "main2.ezpdpz.net", operator: "ezpdpz", endorsed: true },
|
|
37
|
+
{ id: 36n, host: "cello.fidl.tech", operator: "fidl", endorsed: false },
|
|
38
|
+
{ id: 37n, host: "bass.fidl.tech", operator: "fidl", endorsed: false },
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Which Filecoin chain goes with which NMTS network: test data stays on test networks.
|
|
43
|
+
*
|
|
44
|
+
* Keyed by the NMTS network name ("testnet" | "mainnet") so that this file needs no import.
|
|
45
|
+
*/
|
|
46
|
+
export const FILECOIN_CHAIN_FOR_NETWORK = {
|
|
47
|
+
testnet: "calibration",
|
|
48
|
+
mainnet: "mainnet",
|
|
49
|
+
};
|
|
50
|
+
/** The `https://<host>` origins of one chain's list, in order. */
|
|
51
|
+
export function filecoinProviderOrigins(chain) {
|
|
52
|
+
return FILECOIN_PROVIDERS[chain].map((p) => `https://${p.host}`);
|
|
53
|
+
}
|
|
54
|
+
/** The allowlisted company serving `host` on `chain`, or `null` when the host is not on the list. */
|
|
55
|
+
export function providerForHost(chain, host) {
|
|
56
|
+
const wanted = host.toLowerCase();
|
|
57
|
+
return FILECOIN_PROVIDERS[chain].find((p) => p.host === wanted) ?? null;
|
|
58
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { HeavyCopy } from "../api/types-heavy.ts";
|
|
2
|
+
import { type FilecoinChain } from "../filecoin/providers.ts";
|
|
3
|
+
/** Inclusive start, exclusive end — the same shape the Walrus reads use. */
|
|
4
|
+
export interface HeavyByteRange {
|
|
5
|
+
start: number;
|
|
6
|
+
end?: number;
|
|
7
|
+
}
|
|
8
|
+
/** Why one copy was not used. */
|
|
9
|
+
export type HeavyCopySkip = "not_https" | "host_not_allowed" | "not_found" | "http_error" | "network_error";
|
|
10
|
+
export interface HeavyCopyAttempt {
|
|
11
|
+
providerId: string;
|
|
12
|
+
reason: HeavyCopySkip;
|
|
13
|
+
/** The HTTP status, when an answer was the reason. */
|
|
14
|
+
status?: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Every copy failed. `notFound` is true only when every company actually asked answered 404 — one
|
|
18
|
+
* that was down or erred may still hold the piece, which is the same rule the Walrus reads keep.
|
|
19
|
+
*/
|
|
20
|
+
export declare class HeavyDownloadError extends Error {
|
|
21
|
+
readonly pieceCid: string;
|
|
22
|
+
readonly attempts: readonly HeavyCopyAttempt[];
|
|
23
|
+
readonly notFound: boolean;
|
|
24
|
+
constructor(pieceCid: string, attempts: readonly HeavyCopyAttempt[]);
|
|
25
|
+
}
|
|
26
|
+
export interface FetchHeavyPartInput {
|
|
27
|
+
/** The part's PieceCID — its `blob_id`. Used in messages only; each copy's address already names it. */
|
|
28
|
+
pieceCid: string;
|
|
29
|
+
/** The copies, in the order to try them. */
|
|
30
|
+
copies: readonly HeavyCopy[];
|
|
31
|
+
/** Which chain's allowlist the companies must be on. */
|
|
32
|
+
chain: FilecoinChain;
|
|
33
|
+
range?: HeavyByteRange;
|
|
34
|
+
signal?: AbortSignal;
|
|
35
|
+
/** Ask https companies outside the allowlist too (a program with no browser security policy). */
|
|
36
|
+
allowUnlisted?: boolean;
|
|
37
|
+
/** Default: the global `fetch`. */
|
|
38
|
+
fetchImpl?: (input: string, init?: RequestInit) => Promise<Response>;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Open a streaming read of one Heavy part from the first copy that serves it.
|
|
42
|
+
*
|
|
43
|
+
* Resolves with the response (status 200, or 206 for a range) whose body is the sealed part;
|
|
44
|
+
* rejects with `HeavyDownloadError` when no copy serves it, or with the AbortError of `signal`.
|
|
45
|
+
* A company that ignores `Range` and answers 200 with the whole part is accepted — the caller reads
|
|
46
|
+
* only what it needs and cancels the rest.
|
|
47
|
+
*/
|
|
48
|
+
export declare function fetchHeavyPart(input: FetchHeavyPartInput): Promise<Response>;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// Read one NMTS Heavy part back from the storage companies that keep its copies.
|
|
2
|
+
//
|
|
3
|
+
// ⚠ THIS FILE IS PUBLISHED. It is copied byte-for-byte into the `nmts` command-line package.
|
|
4
|
+
// Keep the comments self-contained English, with no pointer only we can follow.
|
|
5
|
+
//
|
|
6
|
+
// Each copy names a public address, `https://<company>/piece/<PieceCID>` — no credentials, open CORS,
|
|
7
|
+
// byte ranges allowed. The copies are asked in order; the first that answers with the bytes is used,
|
|
8
|
+
// and any failure moves on to the next one.
|
|
9
|
+
//
|
|
10
|
+
// ⛔ ONLY ALLOWLISTED COMPANIES, ONLY HTTPS. An address comes from our server, and a browser page may
|
|
11
|
+
// only talk to hosts its security policy names — so a copy at a company outside the allowlist for
|
|
12
|
+
// this chain is skipped with a reason rather than asked (the request would be blocked anyway, as a
|
|
13
|
+
// bare "Failed to fetch"). A program with no such policy may pass `allowUnlisted` to ask any https
|
|
14
|
+
// company — somebody paying from their own wallet may have picked one the list does not name.
|
|
15
|
+
//
|
|
16
|
+
// ⛔ THE BYTES ARE NOT CHECKED HERE. They are an end-to-end sealed stream, and opening it is the check:
|
|
17
|
+
// a wrong or altered byte fails authentication when it is decrypted. Recomputing the PieceCID would
|
|
18
|
+
// cost a full hash of the part and tell us nothing the decryption does not.
|
|
19
|
+
import { providerForHost } from "../filecoin/providers.js";
|
|
20
|
+
/**
|
|
21
|
+
* Every copy failed. `notFound` is true only when every company actually asked answered 404 — one
|
|
22
|
+
* that was down or erred may still hold the piece, which is the same rule the Walrus reads keep.
|
|
23
|
+
*/
|
|
24
|
+
export class HeavyDownloadError extends Error {
|
|
25
|
+
pieceCid;
|
|
26
|
+
attempts;
|
|
27
|
+
notFound;
|
|
28
|
+
constructor(pieceCid, attempts) {
|
|
29
|
+
const asked = attempts.filter((a) => a.reason !== "not_https" && a.reason !== "host_not_allowed");
|
|
30
|
+
const notFound = asked.length > 0 && asked.every((a) => a.reason === "not_found");
|
|
31
|
+
super(attempts.length === 0
|
|
32
|
+
? `Piece ${pieceCid} has no recorded copies to read.`
|
|
33
|
+
: `No copy of piece ${pieceCid} could be read: ` +
|
|
34
|
+
attempts.map((a) => `${a.providerId} ${a.reason}${a.status ? ` ${a.status}` : ""}`).join(" | "));
|
|
35
|
+
// Two names, so a reader that sorts failures by name can tell "gone" from "unreachable".
|
|
36
|
+
this.name = notFound ? "HeavyPieceNotFoundError" : "HeavyDownloadError";
|
|
37
|
+
this.pieceCid = pieceCid;
|
|
38
|
+
this.attempts = attempts;
|
|
39
|
+
this.notFound = notFound;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** Build the HTTP `Range` value from an exclusive-end range, or `null` for the whole part. */
|
|
43
|
+
function rangeHeader(range) {
|
|
44
|
+
if (range === undefined)
|
|
45
|
+
return null;
|
|
46
|
+
const { start, end } = range;
|
|
47
|
+
if (!Number.isSafeInteger(start) || start < 0)
|
|
48
|
+
throw new RangeError(`Invalid range start ${start}.`);
|
|
49
|
+
if (end === undefined)
|
|
50
|
+
return `bytes=${start}-`;
|
|
51
|
+
if (!Number.isSafeInteger(end) || end <= start)
|
|
52
|
+
throw new RangeError(`Invalid range end ${end}.`);
|
|
53
|
+
return `bytes=${start}-${end - 1}`;
|
|
54
|
+
}
|
|
55
|
+
/** Why this copy may not be asked at all, or `null` when it may. */
|
|
56
|
+
function refusalFor(copy, chain, allowUnlisted) {
|
|
57
|
+
let url;
|
|
58
|
+
try {
|
|
59
|
+
url = new URL(copy.retrieval_url);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// An address that does not parse is refused the same way as one that is not https.
|
|
63
|
+
return "not_https";
|
|
64
|
+
}
|
|
65
|
+
if (url.protocol !== "https:")
|
|
66
|
+
return "not_https";
|
|
67
|
+
if (!allowUnlisted && providerForHost(chain, url.hostname) === null)
|
|
68
|
+
return "host_not_allowed";
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Open a streaming read of one Heavy part from the first copy that serves it.
|
|
73
|
+
*
|
|
74
|
+
* Resolves with the response (status 200, or 206 for a range) whose body is the sealed part;
|
|
75
|
+
* rejects with `HeavyDownloadError` when no copy serves it, or with the AbortError of `signal`.
|
|
76
|
+
* A company that ignores `Range` and answers 200 with the whole part is accepted — the caller reads
|
|
77
|
+
* only what it needs and cancels the rest.
|
|
78
|
+
*/
|
|
79
|
+
export async function fetchHeavyPart(input) {
|
|
80
|
+
const fetchImpl = input.fetchImpl ?? ((url, init) => fetch(url, init));
|
|
81
|
+
const range = rangeHeader(input.range);
|
|
82
|
+
const attempts = [];
|
|
83
|
+
for (const copy of input.copies) {
|
|
84
|
+
if (input.signal?.aborted)
|
|
85
|
+
throw new DOMException("The operation was aborted.", "AbortError");
|
|
86
|
+
const refused = refusalFor(copy, input.chain, input.allowUnlisted === true);
|
|
87
|
+
if (refused !== null) {
|
|
88
|
+
attempts.push({ providerId: copy.provider_id, reason: refused });
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
let res;
|
|
92
|
+
try {
|
|
93
|
+
res = await fetchImpl(copy.retrieval_url, {
|
|
94
|
+
method: "GET",
|
|
95
|
+
...(range === null ? {} : { headers: { Range: range } }),
|
|
96
|
+
...(input.signal === undefined ? {} : { signal: input.signal }),
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
catch (err) {
|
|
100
|
+
if (input.signal?.aborted)
|
|
101
|
+
throw err;
|
|
102
|
+
attempts.push({ providerId: copy.provider_id, reason: "network_error" });
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if ((res.status === 200 || res.status === 206) && res.body !== null)
|
|
106
|
+
return res;
|
|
107
|
+
attempts.push({
|
|
108
|
+
providerId: copy.provider_id,
|
|
109
|
+
reason: res.status === 404 ? "not_found" : "http_error",
|
|
110
|
+
status: res.status,
|
|
111
|
+
});
|
|
112
|
+
// This answer is not going to be read; free the connection before asking the next company.
|
|
113
|
+
await res.body?.cancel().catch(() => undefined); // a failed cancel changes nothing we do next
|
|
114
|
+
}
|
|
115
|
+
throw new HeavyDownloadError(input.pieceCid, attempts);
|
|
116
|
+
}
|