@needmoretruth/nmts-cli 0.45.0 → 0.47.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +8 -8
- package/CHANGELOG.md +78 -0
- package/README.ko.md +6 -3
- package/README.md +31 -11
- package/dist/api-advice.js +17 -2
- package/dist/api.d.ts +22 -0
- package/dist/api.js +49 -1
- package/dist/arg-options.d.ts +19 -1
- package/dist/args.js +14 -2
- package/dist/artifact-about.d.ts +1 -1
- package/dist/autonomy.js +3 -3
- package/dist/collision.d.ts +20 -3
- package/dist/collision.js +21 -3
- package/dist/commands/erase.js +1 -0
- package/dist/commands/handover.d.ts +15 -0
- package/dist/commands/handover.js +376 -0
- package/dist/commands/heavy-run.d.ts +59 -0
- package/dist/commands/heavy-run.js +247 -0
- package/dist/commands/heavy.d.ts +7 -0
- package/dist/commands/heavy.js +123 -0
- package/dist/commands/link.d.ts +12 -0
- package/dist/commands/link.js +121 -0
- package/dist/commands/ls.d.ts +5 -0
- package/dist/commands/ls.js +9 -1
- package/dist/commands/public-code-manage.d.ts +24 -0
- package/dist/commands/public-code-manage.js +217 -0
- package/dist/commands/public-code.d.ts +12 -1
- package/dist/commands/public-code.js +140 -45
- package/dist/commands/push-heavy.d.ts +6 -0
- package/dist/commands/push-heavy.js +128 -0
- package/dist/commands/push-tree.d.ts +24 -0
- package/dist/commands/push-tree.js +91 -0
- package/dist/commands/push.d.ts +4 -13
- package/dist/commands/push.js +7 -77
- package/dist/commands/put-heavy.d.ts +9 -0
- package/dist/commands/put-heavy.js +120 -0
- package/dist/commands/put-payer.d.ts +21 -0
- package/dist/commands/put-payer.js +24 -0
- package/dist/commands/put.d.ts +2 -2
- package/dist/commands/put.js +8 -7
- package/dist/commands/receive.js +14 -8
- package/dist/commands/s3.js +44 -10
- package/dist/commands/share-dispatch.js +14 -1
- package/dist/commands/share.d.ts +2 -0
- package/dist/commands/share.js +45 -35
- package/dist/commands/shares-sent.js +8 -3
- package/dist/crypto-surface.d.ts +35 -0
- package/dist/crypto-surface.js +13 -0
- package/dist/crypto.d.ts +8 -0
- package/dist/crypto.js +8 -0
- package/dist/download-part.d.ts +32 -1
- package/dist/download-part.js +95 -6
- package/dist/download-range.d.ts +19 -0
- package/dist/download-range.js +108 -0
- package/dist/download-sink.d.ts +15 -0
- package/dist/download.d.ts +14 -0
- package/dist/download.js +19 -4
- package/dist/handover-codes.d.ts +25 -0
- package/dist/handover-codes.js +106 -0
- package/dist/handover.d.ts +62 -0
- package/dist/handover.js +217 -0
- package/dist/heavy-api.d.ts +36 -0
- package/dist/heavy-api.js +107 -0
- package/dist/heavy-copy.d.ts +72 -0
- package/dist/heavy-copy.js +108 -0
- package/dist/heavy-evm.d.ts +20 -0
- package/dist/heavy-evm.js +106 -0
- package/dist/heavy-self-pay.d.ts +92 -0
- package/dist/heavy-self-pay.js +167 -0
- package/dist/heavy-upload.d.ts +87 -0
- package/dist/heavy-upload.js +154 -0
- package/dist/heavy-wallet-pay.d.ts +39 -0
- package/dist/heavy-wallet-pay.js +73 -0
- package/dist/help-options.js +13 -4
- package/dist/help.js +24 -3
- package/dist/links.d.ts +58 -0
- package/dist/links.js +206 -0
- package/dist/list-tier.d.ts +7 -0
- package/dist/list-tier.js +47 -0
- package/dist/main.js +13 -10
- package/dist/manifest-write.d.ts +4 -3
- package/dist/mcp-tools/reads.js +20 -6
- package/dist/mcp-tools/share.js +47 -1
- package/dist/mcp-tools/tiers.js +16 -1
- package/dist/platform-sign.d.ts +4 -4
- package/dist/platform-sign.js +3 -3
- package/dist/portable.d.ts +23 -0
- package/dist/portable.js +19 -0
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/public-code-activity.d.ts +35 -0
- package/dist/public-code-activity.js +88 -0
- package/dist/public-code-refusals.d.ts +25 -0
- package/dist/public-code-refusals.js +87 -0
- package/dist/public-codes.d.ts +76 -0
- package/dist/public-codes.js +182 -0
- package/dist/recovery-build.js +16 -0
- package/dist/recovery-map-part.d.ts +82 -0
- package/dist/recovery-map-part.js +83 -0
- package/dist/recovery-map.d.ts +5 -40
- package/dist/recovery-map.js +18 -1
- package/dist/recovery-source.d.ts +3 -0
- package/dist/recovery-source.js +9 -0
- package/dist/risk.d.ts +55 -1
- package/dist/risk.js +55 -3
- package/dist/run-log.d.ts +1 -1
- package/dist/run-log.js +7 -1
- package/dist/s3/address.d.ts +20 -0
- package/dist/s3/address.js +97 -0
- package/dist/s3/answer.d.ts +48 -0
- package/dist/s3/answer.js +330 -0
- package/dist/s3/body-deframe.d.ts +49 -0
- package/dist/s3/body-deframe.js +264 -0
- package/dist/s3/body-measure.d.ts +13 -0
- package/dist/s3/body-measure.js +48 -0
- package/dist/s3/body-plan.d.ts +31 -0
- package/dist/s3/body-plan.js +159 -0
- package/dist/s3/body.d.ts +46 -0
- package/dist/s3/body.js +117 -0
- package/dist/s3/call.d.ts +36 -0
- package/dist/s3/call.js +42 -0
- package/dist/s3/checksum.d.ts +26 -0
- package/dist/s3/checksum.js +176 -0
- package/dist/s3/content-type.d.ts +3 -0
- package/dist/s3/content-type.js +49 -0
- package/dist/s3/contract.d.ts +91 -14
- package/dist/s3/drive-conditions.d.ts +47 -0
- package/dist/s3/drive-conditions.js +134 -0
- package/dist/s3/drive-key.d.ts +11 -0
- package/dist/s3/drive-key.js +58 -0
- package/dist/s3/drive-limits.d.ts +33 -0
- package/dist/s3/drive-limits.js +53 -0
- package/dist/s3/drive-lock.d.ts +11 -0
- package/dist/s3/drive-lock.js +47 -0
- package/dist/s3/drive-trashed-name.d.ts +13 -0
- package/dist/s3/drive-trashed-name.js +59 -0
- package/dist/s3/drive-write.d.ts +38 -0
- package/dist/s3/drive-write.js +258 -0
- package/dist/s3/drive.d.ts +59 -25
- package/dist/s3/drive.js +46 -77
- package/dist/s3/listing.d.ts +27 -1
- package/dist/s3/listing.js +62 -20
- package/dist/s3/long-answer.d.ts +12 -0
- package/dist/s3/long-answer.js +104 -0
- package/dist/s3/multipart-assemble.d.ts +17 -0
- package/dist/s3/multipart-assemble.js +68 -0
- package/dist/s3/multipart-sweep.d.ts +15 -0
- package/dist/s3/multipart-sweep.js +87 -0
- package/dist/s3/multipart-view.d.ts +45 -0
- package/dist/s3/multipart-view.js +232 -0
- package/dist/s3/multipart.d.ts +26 -19
- package/dist/s3/multipart.js +193 -78
- package/dist/s3/object-read.d.ts +17 -0
- package/dist/s3/object-read.js +127 -0
- package/dist/s3/object-write.d.ts +35 -0
- package/dist/s3/object-write.js +198 -0
- package/dist/s3/range.d.ts +14 -0
- package/dist/s3/range.js +80 -0
- package/dist/s3/response-sink.d.ts +12 -2
- package/dist/s3/response-sink.js +71 -7
- package/dist/s3/routes.d.ts +0 -1
- package/dist/s3/routes.js +242 -217
- package/dist/s3/same-file.d.ts +4 -1
- package/dist/s3/same-file.js +12 -3
- package/dist/s3/server.d.ts +54 -4
- package/dist/s3/server.js +174 -14
- package/dist/s3/sigv4-canonical.d.ts +91 -0
- package/dist/s3/sigv4-canonical.js +148 -0
- package/dist/s3/sigv4-presigned.d.ts +13 -0
- package/dist/s3/sigv4-presigned.js +82 -0
- package/dist/s3/sigv4.d.ts +26 -27
- package/dist/s3/sigv4.js +137 -102
- package/dist/s3/spool.d.ts +37 -0
- package/dist/s3/spool.js +104 -0
- package/dist/s3/staging.d.ts +90 -7
- package/dist/s3/staging.js +97 -66
- package/dist/s3/xml-multipart.d.ts +38 -0
- package/dist/s3/xml-multipart.js +47 -0
- package/dist/s3/xml-read.d.ts +28 -0
- package/dist/s3/xml-read.js +235 -0
- package/dist/s3/xml.d.ts +45 -2
- package/dist/s3/xml.js +72 -13
- package/dist/s3-gateway.d.ts +11 -5
- package/dist/s3-gateway.js +5 -2
- package/dist/seal.d.ts +1 -1
- package/dist/share-codes.d.ts +40 -0
- package/dist/share-codes.js +151 -0
- package/dist/share.d.ts +39 -0
- package/dist/share.js +54 -13
- package/dist/shared/lib/api/types-heavy.d.ts +167 -0
- package/dist/shared/lib/api/types-heavy.js +60 -0
- package/dist/shared/lib/drive/rebuild-verify.js +1 -0
- package/dist/shared/lib/filecoin/providers.d.ts +25 -0
- package/dist/shared/lib/filecoin/providers.js +58 -0
- package/dist/shared/lib/heavy/download.d.ts +48 -0
- package/dist/shared/lib/heavy/download.js +116 -0
- package/dist/shared/lib/heavy/order-plan.d.ts +77 -0
- package/dist/shared/lib/heavy/order-plan.js +83 -0
- package/dist/shared/lib/heavy/order-runner.d.ts +9 -0
- package/dist/shared/lib/heavy/order-runner.js +300 -0
- package/dist/shared/lib/heavy/order-types.d.ts +90 -0
- package/dist/shared/lib/heavy/order-types.js +6 -0
- package/dist/shared/lib/heavy/order-wire.d.ts +55 -0
- package/dist/shared/lib/heavy/order-wire.js +113 -0
- package/dist/shared/lib/heavy/piece-cid.d.ts +24 -0
- package/dist/shared/lib/heavy/piece-cid.js +55 -0
- package/dist/shared/lib/heavy/sp-upload.d.ts +37 -0
- package/dist/shared/lib/heavy/sp-upload.js +112 -0
- package/dist/shared/lib/heavy/synapse-piece.d.ts +1 -0
- package/dist/shared/lib/heavy/synapse-piece.js +8 -0
- package/dist/shared/lib/share/handover-format.d.ts +129 -0
- package/dist/shared/lib/share/handover-format.js +323 -0
- package/dist/shared/lib/share/public-link.d.ts +41 -0
- package/dist/shared/lib/share/public-link.js +107 -0
- package/dist/shared/lib/share/shared-file-info.js +1 -0
- package/dist/shared/lib/storage-network.d.ts +4 -4
- package/dist/shared/lib/storage-network.js +7 -7
- package/dist/shared/lib/storage-tier.d.ts +28 -0
- package/dist/shared/lib/storage-tier.js +63 -0
- package/dist/shared/lib/wallet/send-rules.js +1 -0
- package/dist/upload-file.d.ts +8 -0
- package/dist/upload-file.js +57 -25
- package/dist/upload-price-node.d.ts +3 -0
- package/dist/upload-price-node.js +3 -6
- package/dist/upload-price.d.ts +10 -0
- package/dist/upload-price.js +13 -1
- package/dist/upload-run.d.ts +80 -0
- package/dist/upload-run.js +173 -0
- package/dist/upload-steps.js +15 -4
- package/dist/upload-store.d.ts +15 -6
- package/dist/upload-store.js +6 -0
- package/dist/upload-wallet-put.d.ts +9 -4
- package/dist/upload-wallet-put.js +16 -6
- package/dist/upload-wallet.js +4 -0
- package/dist/upload-wire.d.ts +25 -0
- package/dist/upload-wire.js +20 -1
- package/dist/upload.js +28 -13
- package/dist/wallet-sign.js +2 -2
- package/docs/commands/handover.md +54 -0
- package/docs/commands/heavy.md +35 -0
- package/docs/commands/link.md +27 -0
- package/docs/commands/mcp.md +2 -1
- package/docs/commands/public-code.md +25 -6
- package/docs/commands/push.md +4 -1
- package/docs/commands/put.md +15 -1
- package/docs/commands/share.md +8 -3
- package/package.json +5 -2
- package/server.json +2 -2
- package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +106 -4
- package/vendor/nmts-crypto/nmts_crypto_wasm.js +286 -3
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +12 -1
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
// Drive one NMTS Heavy upload from start to finish: open the order, place and upload every sealed
|
|
2
|
+
// part, wait until each is stored, pay if the wallet pays, and hand back the parts to commit.
|
|
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
|
+
// EVERY EDGE IS INJECTED — the order routes, the sealer, the PieceCID, the upload, the clock and the
|
|
8
|
+
// wait. The browser and the command line plug in their own; a test plugs in fakes. Nothing in here
|
|
9
|
+
// touches the network or a key on its own.
|
|
10
|
+
//
|
|
11
|
+
// THE ORDER OF THINGS (one upload = one order, however many files):
|
|
12
|
+
// 1. open `createOrder` with one slot per sealed part, numbered across the files in order.
|
|
13
|
+
// Retried under the SAME idempotency key when the answer is lost, so it is charged once.
|
|
14
|
+
// 2. per slot, a few at a time (default 2):
|
|
15
|
+
// seal → check the sealed size is the one the order was opened with → PieceCID →
|
|
16
|
+
// `targetSlot` (where to upload) → upload to that company → `slotUploaded` →
|
|
17
|
+
// read the order until the slot is `stored` (bounded, with backoff) or `failed`.
|
|
18
|
+
// 3. pay wallet orders only, after EVERY slot is stored: the injected `payWallet` sends the WAL
|
|
19
|
+
// and returns the Sui transaction digest, which is reported with `markPaid`.
|
|
20
|
+
// 4. return per file, the parts to commit (`network` 1, `blob_id` = PieceCID, the order and slot,
|
|
21
|
+
// the copies, and the Filecoin epoch they are kept until).
|
|
22
|
+
//
|
|
23
|
+
// ⛔ A FAILURE STOPS EVERYTHING AND SAYS WHY IN A WORD (`HeavyOrderError.reason`). Nothing is cleaned
|
|
24
|
+
// up from here: the server removes whatever an order stored if no file is committed from it within
|
|
25
|
+
// a day, and gives back the credits for slots that were never stored.
|
|
26
|
+
import { heavyRefusalOf } from "../api/types-heavy.js";
|
|
27
|
+
import { heavyOrderRequest, planSlots } from "./order-plan.js";
|
|
28
|
+
import { abortError, codeOf, defaultSleep, HeavyOrderError, isAbort, isHttpsUrl, isTransient, readCopies, } from "./order-wire.js";
|
|
29
|
+
export { HEAVY_MAX_SLOTS, HEAVY_MIN_SEAL_FROM_BYTES, HEAVY_PART_SIZE_BYTES, HEAVY_PIECE_MAX_BYTES, HEAVY_PIECE_MIN_BYTES, heavyOrderRequest, } from "./order-plan.js";
|
|
30
|
+
export { HeavyOrderError } from "./order-wire.js";
|
|
31
|
+
function sizeOf(bytes) {
|
|
32
|
+
return bytes instanceof Uint8Array ? bytes.byteLength : bytes.size;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Run one Heavy upload to the end. Resolves with the parts to commit; rejects with a
|
|
36
|
+
* `HeavyOrderError`, or with an AbortError when `options.signal` aborts.
|
|
37
|
+
*/
|
|
38
|
+
export async function runHeavyOrder(options, deps) {
|
|
39
|
+
const plans = planSlots(options.files);
|
|
40
|
+
const { api } = deps;
|
|
41
|
+
const sleep = deps.sleep ?? defaultSleep;
|
|
42
|
+
const now = deps.now ?? (() => Date.now());
|
|
43
|
+
const concurrency = Math.max(1, options.concurrency ?? 2);
|
|
44
|
+
const pollStart = options.pollIntervalMs ?? 3_000;
|
|
45
|
+
const pollMax = options.pollMaxIntervalMs ?? 30_000;
|
|
46
|
+
const storeTimeout = options.storeTimeoutMs ?? 60 * 60 * 1000;
|
|
47
|
+
const uploadAttempts = Math.max(1, options.uploadAttempts ?? 3);
|
|
48
|
+
const idempotencyKey = options.idempotencyKey ?? deps.newIdempotencyKey?.() ?? globalThis.crypto.randomUUID();
|
|
49
|
+
const emit = (event) => options.onProgress?.(event);
|
|
50
|
+
// One controller for the run: the caller's abort, or the first slot that fails, stops the rest.
|
|
51
|
+
if (options.signal?.aborted)
|
|
52
|
+
throw abortError();
|
|
53
|
+
const run = new AbortController();
|
|
54
|
+
const onOuterAbort = () => run.abort();
|
|
55
|
+
options.signal?.addEventListener("abort", onOuterAbort, { once: true });
|
|
56
|
+
const signal = run.signal;
|
|
57
|
+
let orderId = null;
|
|
58
|
+
/** One api call, made again after 1, 2, 4… s when it failed for a reason unrelated to the request. */
|
|
59
|
+
async function call(what, fn, slot) {
|
|
60
|
+
const attempts = 5;
|
|
61
|
+
for (let attempt = 1;; attempt += 1) {
|
|
62
|
+
if (signal.aborted)
|
|
63
|
+
throw abortError();
|
|
64
|
+
try {
|
|
65
|
+
return await fn();
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
if (isAbort(err))
|
|
69
|
+
throw err;
|
|
70
|
+
const code = codeOf(err);
|
|
71
|
+
if (!isTransient(err)) {
|
|
72
|
+
throw new HeavyOrderError("refused", `${what} was refused (${code ?? "no code"}).`, {
|
|
73
|
+
refusal: code === null ? null : heavyRefusalOf(code),
|
|
74
|
+
orderId,
|
|
75
|
+
slot,
|
|
76
|
+
cause: err,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
if (attempt >= attempts) {
|
|
80
|
+
throw new HeavyOrderError("unreachable", `${what} failed ${attempts} times.`, {
|
|
81
|
+
orderId,
|
|
82
|
+
slot,
|
|
83
|
+
cause: err,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
await sleep(1000 * 2 ** (attempt - 1), signal);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const readOrder = (id, slot) => call("Reading the order", () => api.getOrder(id, { signal }), slot);
|
|
91
|
+
/**
|
|
92
|
+
* Report the payment. A just-sent transaction may not be readable yet where the server looks, so
|
|
93
|
+
* "not found" is asked again a few times; "already used" means an earlier report landed and its
|
|
94
|
+
* answer was lost, which the order's own state confirms.
|
|
95
|
+
*/
|
|
96
|
+
async function reportPayment(id, digest) {
|
|
97
|
+
for (let attempt = 1;; attempt += 1) {
|
|
98
|
+
try {
|
|
99
|
+
await call("Reporting the payment", () => api.markPaid(id, { tx_digest: digest }, { signal }), null);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
const refusal = err instanceof HeavyOrderError ? err.refusal : null;
|
|
104
|
+
if (refusal === "payment_not_found" && attempt < 6) {
|
|
105
|
+
await sleep(5000 * attempt, signal);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
if (refusal === "digest_used") {
|
|
109
|
+
const view = await readOrder(id, null);
|
|
110
|
+
if (view.state === "paid" || view.state === "bound")
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (err instanceof HeavyOrderError && err.reason === "refused") {
|
|
114
|
+
throw new HeavyOrderError("payment_refused", `The payment ${digest} was not accepted.`, {
|
|
115
|
+
refusal,
|
|
116
|
+
orderId: id,
|
|
117
|
+
cause: err,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
throw err;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
// 1. Open (or reach again) the order.
|
|
126
|
+
const request = heavyOrderRequest(options.files, options.payment, idempotencyKey);
|
|
127
|
+
const created = await call("Opening the order", () => api.createOrder(request, { signal }), null);
|
|
128
|
+
if (typeof created.order_id !== "string" || created.slots?.length !== plans.length) {
|
|
129
|
+
throw new HeavyOrderError("bad_answer", "The order came back without the slots it was opened with.");
|
|
130
|
+
}
|
|
131
|
+
const id = created.order_id;
|
|
132
|
+
orderId = id;
|
|
133
|
+
if (options.payment.pay === "wallet" && created.price === undefined) {
|
|
134
|
+
throw new HeavyOrderError("bad_answer", "A wallet order came back without a price.", { orderId: id });
|
|
135
|
+
}
|
|
136
|
+
emit({ phase: "opened", orderId: id, order: created });
|
|
137
|
+
// What the order already holds: a run that reaches an existing order does not redo stored slots.
|
|
138
|
+
const before = await readOrder(id, null);
|
|
139
|
+
let latest = before;
|
|
140
|
+
const results = new Map();
|
|
141
|
+
/** Read the order until this slot is stored; fail on a failed slot, a failed order or the deadline. */
|
|
142
|
+
async function waitStored(plan, cid) {
|
|
143
|
+
const deadline = now() + storeTimeout;
|
|
144
|
+
const where = { orderId: id, slot: plan.slot };
|
|
145
|
+
for (let interval = pollStart;; interval = Math.min(pollMax, Math.round(interval * 1.5))) {
|
|
146
|
+
const view = await readOrder(id, plan.slot);
|
|
147
|
+
latest = view;
|
|
148
|
+
if (view.state === "failed" || view.state === "expired") {
|
|
149
|
+
throw new HeavyOrderError("order_failed", `The order is ${view.state}.`, where);
|
|
150
|
+
}
|
|
151
|
+
const slot = view.slots?.find((s) => s.slot === plan.slot);
|
|
152
|
+
if (slot?.state === "failed" || slot?.state === "removed") {
|
|
153
|
+
const why = slot.error ? `: ${slot.error}` : "";
|
|
154
|
+
throw new HeavyOrderError("slot_failed", `Part ${plan.partIndex} of "${plan.fileKey}" was not stored (${slot.state}${why}).`, where);
|
|
155
|
+
}
|
|
156
|
+
if (slot?.state === "stored") {
|
|
157
|
+
const copies = readCopies(slot.copies);
|
|
158
|
+
const stored = slot.piece_cid ?? cid;
|
|
159
|
+
if (copies === null || typeof stored !== "string" || (cid !== null && stored !== cid)) {
|
|
160
|
+
throw new HeavyOrderError("bad_answer", `Slot ${plan.slot} is stored, but its copies or piece id do not add up.`, where);
|
|
161
|
+
}
|
|
162
|
+
return { cid: stored, copies };
|
|
163
|
+
}
|
|
164
|
+
if (now() >= deadline) {
|
|
165
|
+
const minutes = Math.round(storeTimeout / 60_000);
|
|
166
|
+
throw new HeavyOrderError("store_timeout", `Part ${plan.partIndex} of "${plan.fileKey}" was not stored within ${minutes} minutes.`, where);
|
|
167
|
+
}
|
|
168
|
+
await sleep(interval, signal);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
async function processSlot(plan) {
|
|
172
|
+
const at = { slot: plan.slot, fileKey: plan.fileKey, partIndex: plan.partIndex };
|
|
173
|
+
const where = { orderId: id, slot: plan.slot };
|
|
174
|
+
const known = before.slots?.find((s) => s.slot === plan.slot);
|
|
175
|
+
if (known?.state === "stored" || known?.state === "committing") {
|
|
176
|
+
results.set(plan.slot, await waitStored(plan, known.piece_cid ?? null));
|
|
177
|
+
emit({ phase: "stored", ...at });
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
emit({ phase: "sealing", ...at });
|
|
181
|
+
const job = { fileKey: plan.fileKey, partIndex: plan.partIndex, slot: plan.slot, sealedLen: plan.sealedLen };
|
|
182
|
+
const bytes = await deps.seal(job, signal);
|
|
183
|
+
if (signal.aborted)
|
|
184
|
+
throw abortError();
|
|
185
|
+
if (sizeOf(bytes) !== plan.sealedLen) {
|
|
186
|
+
throw new HeavyOrderError("sealed_len_mismatch", `Part ${plan.partIndex} of "${plan.fileKey}" sealed to ${sizeOf(bytes)} bytes, ` +
|
|
187
|
+
`not the ${plan.sealedLen} the order was opened for.`, where);
|
|
188
|
+
}
|
|
189
|
+
emit({ phase: "hashing", ...at });
|
|
190
|
+
const cid = await deps.pieceCid(bytes);
|
|
191
|
+
emit({ phase: "placing", ...at });
|
|
192
|
+
const target = await call("Placing the part", () => api.targetSlot(id, plan.slot, { piece_cid: cid }, { signal }), plan.slot);
|
|
193
|
+
if (!isHttpsUrl(target.service_url)) {
|
|
194
|
+
throw new HeavyOrderError("bad_answer", "The server named no https storage service for this part.", where);
|
|
195
|
+
}
|
|
196
|
+
for (let attempt = 1;; attempt += 1) {
|
|
197
|
+
try {
|
|
198
|
+
await deps.upload({
|
|
199
|
+
serviceUrl: target.service_url,
|
|
200
|
+
bytes,
|
|
201
|
+
pieceCid: cid,
|
|
202
|
+
signal,
|
|
203
|
+
onProgress: (sentBytes, totalBytes) => emit({ phase: "uploading", ...at, sentBytes, totalBytes }),
|
|
204
|
+
});
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
catch (err) {
|
|
208
|
+
if (isAbort(err) || signal.aborted)
|
|
209
|
+
throw err;
|
|
210
|
+
if (attempt >= uploadAttempts) {
|
|
211
|
+
throw new HeavyOrderError("upload_failed", `Uploading part ${plan.partIndex} of "${plan.fileKey}" failed ${uploadAttempts} times.`, { ...where, cause: err });
|
|
212
|
+
}
|
|
213
|
+
await sleep(2000 * 2 ** (attempt - 1), signal);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
emit({ phase: "committing", ...at });
|
|
217
|
+
await call("Reporting the upload", () => api.slotUploaded(id, plan.slot, { signal }), plan.slot);
|
|
218
|
+
results.set(plan.slot, await waitStored(plan, cid));
|
|
219
|
+
emit({ phase: "stored", ...at });
|
|
220
|
+
}
|
|
221
|
+
// 2. The slots, a few at a time. The first failure aborts the others and is the one reported.
|
|
222
|
+
let firstFailure = null;
|
|
223
|
+
let next = 0;
|
|
224
|
+
const worker = async () => {
|
|
225
|
+
for (let plan = plans[next]; firstFailure === null && plan !== undefined; plan = plans[next]) {
|
|
226
|
+
next += 1;
|
|
227
|
+
try {
|
|
228
|
+
await processSlot(plan);
|
|
229
|
+
}
|
|
230
|
+
catch (err) {
|
|
231
|
+
if (firstFailure === null)
|
|
232
|
+
firstFailure = err;
|
|
233
|
+
run.abort();
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, plans.length) }, worker));
|
|
239
|
+
if (firstFailure !== null) {
|
|
240
|
+
// When the caller aborted, every slot reports an abort; say so rather than the first of them.
|
|
241
|
+
if (options.signal?.aborted)
|
|
242
|
+
throw abortError();
|
|
243
|
+
throw firstFailure;
|
|
244
|
+
}
|
|
245
|
+
// The epoch the copies are kept until is set once every slot is stored: the last read has it,
|
|
246
|
+
// or a read or two later does.
|
|
247
|
+
let view = latest;
|
|
248
|
+
for (let tries = 0; typeof view.expiry_epoch !== "number" && tries < 5; tries += 1) {
|
|
249
|
+
await sleep(pollStart, signal);
|
|
250
|
+
view = await readOrder(id, null);
|
|
251
|
+
}
|
|
252
|
+
const expiryEpoch = view.expiry_epoch;
|
|
253
|
+
if (typeof expiryEpoch !== "number") {
|
|
254
|
+
throw new HeavyOrderError("bad_answer", "Every part is stored but the order names no end epoch.", {
|
|
255
|
+
orderId: id,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
// 3. Pay, when the wallet pays — once, after everything is stored, and not again on a rerun.
|
|
259
|
+
let txDigest;
|
|
260
|
+
const price = created.price;
|
|
261
|
+
if (options.payment.pay === "wallet" && price && view.state !== "paid" && view.state !== "bound") {
|
|
262
|
+
emit({ phase: "paying", orderId: id, price });
|
|
263
|
+
txDigest = await options.payment.payWallet(price, id);
|
|
264
|
+
await reportPayment(id, txDigest);
|
|
265
|
+
emit({ phase: "paid", orderId: id, txDigest });
|
|
266
|
+
}
|
|
267
|
+
// 4. The parts to commit, per file.
|
|
268
|
+
const files = options.files.map((file) => ({ key: file.key, parts: [] }));
|
|
269
|
+
for (const plan of plans) {
|
|
270
|
+
const done = results.get(plan.slot);
|
|
271
|
+
const file = files[plan.fileIndex];
|
|
272
|
+
if (done === undefined || file === undefined) {
|
|
273
|
+
throw new HeavyOrderError("bad_answer", `Slot ${plan.slot} finished without a result.`, { orderId: id });
|
|
274
|
+
}
|
|
275
|
+
file.parts.push({
|
|
276
|
+
part_index: plan.partIndex,
|
|
277
|
+
storage_kind: 0,
|
|
278
|
+
network: 1,
|
|
279
|
+
blob_id: done.cid,
|
|
280
|
+
sealed_len: plan.sealedLen,
|
|
281
|
+
owner_kind: 1,
|
|
282
|
+
expiry_epoch: expiryEpoch,
|
|
283
|
+
heavy_order_id: id,
|
|
284
|
+
heavy_slot: plan.slot,
|
|
285
|
+
copies: done.copies,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
return {
|
|
289
|
+
orderId: id,
|
|
290
|
+
idempotencyKey,
|
|
291
|
+
expiryEpoch,
|
|
292
|
+
...(created.credits === undefined ? {} : { credits: created.credits }),
|
|
293
|
+
...(txDigest === undefined ? {} : { txDigest }),
|
|
294
|
+
files,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
finally {
|
|
298
|
+
options.signal?.removeEventListener("abort", onOuterAbort);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { HeavyOrderApi, HeavyOrderCreated, HeavyPartCommit, HeavyPrice } from "../api/types-heavy.ts";
|
|
2
|
+
import type { HeavyPayment, HeavyRunFile } from "./order-plan.ts";
|
|
3
|
+
/** A sealed part as the sealer returns it: a Blob (may be disk-backed) or bytes in memory. */
|
|
4
|
+
export type SealedBytes = Blob | Uint8Array<ArrayBuffer>;
|
|
5
|
+
/** What the sealer is asked to seal. */
|
|
6
|
+
export interface HeavySealJob {
|
|
7
|
+
fileKey: string;
|
|
8
|
+
partIndex: number;
|
|
9
|
+
slot: number;
|
|
10
|
+
sealedLen: number;
|
|
11
|
+
}
|
|
12
|
+
/** The moments a caller can show. */
|
|
13
|
+
export type HeavyProgress = {
|
|
14
|
+
phase: "opened";
|
|
15
|
+
orderId: string;
|
|
16
|
+
order: HeavyOrderCreated;
|
|
17
|
+
} | {
|
|
18
|
+
phase: "sealing" | "hashing" | "placing" | "committing" | "stored";
|
|
19
|
+
slot: number;
|
|
20
|
+
fileKey: string;
|
|
21
|
+
partIndex: number;
|
|
22
|
+
} | {
|
|
23
|
+
phase: "uploading";
|
|
24
|
+
slot: number;
|
|
25
|
+
fileKey: string;
|
|
26
|
+
partIndex: number;
|
|
27
|
+
sentBytes: number;
|
|
28
|
+
totalBytes: number;
|
|
29
|
+
} | {
|
|
30
|
+
phase: "paying";
|
|
31
|
+
orderId: string;
|
|
32
|
+
price: HeavyPrice;
|
|
33
|
+
} | {
|
|
34
|
+
phase: "paid";
|
|
35
|
+
orderId: string;
|
|
36
|
+
txDigest: string;
|
|
37
|
+
};
|
|
38
|
+
export interface HeavyRunDeps {
|
|
39
|
+
api: HeavyOrderApi;
|
|
40
|
+
seal: (job: HeavySealJob, signal?: AbortSignal) => Promise<SealedBytes>;
|
|
41
|
+
/** The part's FRC-0069 PieceCID (`bafkzcib…`), e.g. `calculate` from `@filoz/synapse-core/piece`. */
|
|
42
|
+
pieceCid: (bytes: SealedBytes) => Promise<string>;
|
|
43
|
+
/** Upload to the company — `uploadPieceToProvider` in `sp-upload.ts`, with the caller's transport. */
|
|
44
|
+
upload: (input: {
|
|
45
|
+
serviceUrl: string;
|
|
46
|
+
bytes: SealedBytes;
|
|
47
|
+
pieceCid: string;
|
|
48
|
+
signal?: AbortSignal;
|
|
49
|
+
onProgress?: (sentBytes: number, totalBytes: number) => void;
|
|
50
|
+
}) => Promise<void>;
|
|
51
|
+
/** Wait `ms`, rejecting with an AbortError when `signal` aborts. Default: a timer. */
|
|
52
|
+
sleep?: (ms: number, signal?: AbortSignal) => Promise<void>;
|
|
53
|
+
/** Milliseconds since some fixed point. Default: `Date.now`. */
|
|
54
|
+
now?: () => number;
|
|
55
|
+
/** A fresh idempotency key (16..64 of `[A-Za-z0-9_-]`). Default: a random UUID. */
|
|
56
|
+
newIdempotencyKey?: () => string;
|
|
57
|
+
}
|
|
58
|
+
export interface HeavyRunOptions {
|
|
59
|
+
files: readonly HeavyRunFile[];
|
|
60
|
+
payment: HeavyPayment;
|
|
61
|
+
/** Pass the key of an earlier run to reach the same order again; otherwise a new one is made. */
|
|
62
|
+
idempotencyKey?: string;
|
|
63
|
+
/** Slots worked on at once. Default 2. */
|
|
64
|
+
concurrency?: number;
|
|
65
|
+
/** First wait between order reads while a slot commits. Default 3 s; it grows by half each time. */
|
|
66
|
+
pollIntervalMs?: number;
|
|
67
|
+
/** Longest wait between order reads. Default 30 s. */
|
|
68
|
+
pollMaxIntervalMs?: number;
|
|
69
|
+
/** How long one slot may take from `uploaded` to `stored`. Default 60 minutes. */
|
|
70
|
+
storeTimeoutMs?: number;
|
|
71
|
+
/** Upload attempts per slot before giving up. Default 3. */
|
|
72
|
+
uploadAttempts?: number;
|
|
73
|
+
signal?: AbortSignal;
|
|
74
|
+
onProgress?: (event: HeavyProgress) => void;
|
|
75
|
+
}
|
|
76
|
+
export interface HeavyRunResult {
|
|
77
|
+
orderId: string;
|
|
78
|
+
idempotencyKey: string;
|
|
79
|
+
/** The Filecoin epoch every copy is kept until. */
|
|
80
|
+
expiryEpoch: number;
|
|
81
|
+
/** Credit orders: what was taken. */
|
|
82
|
+
credits?: number;
|
|
83
|
+
/** Wallet orders: the payment's Sui transaction digest. */
|
|
84
|
+
txDigest?: string;
|
|
85
|
+
/** Per file, in the order given: the parts to put in that file's `POST /v1/items`. */
|
|
86
|
+
files: {
|
|
87
|
+
key: string;
|
|
88
|
+
parts: HeavyPartCommit[];
|
|
89
|
+
}[];
|
|
90
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// The shapes an NMTS Heavy upload is described in: what the caller hands the orchestrator, what it
|
|
2
|
+
// injects, what it hears along the way and what it gets back (`order-runner.ts` runs it).
|
|
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
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type HeavyCopy, type HeavyRefusal } from "../api/types-heavy.ts";
|
|
2
|
+
/** Why a Heavy upload stopped, as a machine-readable word. */
|
|
3
|
+
export type HeavyFailure =
|
|
4
|
+
/** A sealed part is under 127 bytes. The caller pads the plaintext and tries again. */
|
|
5
|
+
"piece_too_small"
|
|
6
|
+
/** A sealed part is over the per-piece limit. The caller cuts smaller parts. */
|
|
7
|
+
| "piece_too_large"
|
|
8
|
+
/** More parts than one order may hold, none at all, or parts not numbered 0..n-1. */
|
|
9
|
+
| "slot_count"
|
|
10
|
+
/** The sealer produced a different number of bytes than the order was opened for. */
|
|
11
|
+
| "sealed_len_mismatch"
|
|
12
|
+
/** The server refused a call; `refusal` names which refusal when it is one of the contract's. */
|
|
13
|
+
| "refused"
|
|
14
|
+
/** The server's answer did not have the shape the contract gives it. */
|
|
15
|
+
| "bad_answer"
|
|
16
|
+
/** Every upload attempt to the storage company failed. */
|
|
17
|
+
| "upload_failed"
|
|
18
|
+
/** The server reports the slot failed (or was removed) instead of stored. */
|
|
19
|
+
| "slot_failed"
|
|
20
|
+
/** The whole order failed or expired. */
|
|
21
|
+
| "order_failed"
|
|
22
|
+
/** The slot was not stored within the time allowed. */
|
|
23
|
+
| "store_timeout"
|
|
24
|
+
/** The server could not be reached, repeatedly. */
|
|
25
|
+
| "unreachable"
|
|
26
|
+
/** The wallet payment was sent but the server would not accept it. */
|
|
27
|
+
| "payment_refused";
|
|
28
|
+
export declare class HeavyOrderError extends Error {
|
|
29
|
+
readonly reason: HeavyFailure;
|
|
30
|
+
/** The contract refusal behind `refused` or `payment_refused`, when there was one. */
|
|
31
|
+
readonly refusal: HeavyRefusal | null;
|
|
32
|
+
readonly orderId: string | null;
|
|
33
|
+
readonly slot: number | null;
|
|
34
|
+
constructor(reason: HeavyFailure, message: string, details?: {
|
|
35
|
+
refusal?: HeavyRefusal | null;
|
|
36
|
+
orderId?: string | null;
|
|
37
|
+
slot?: number | null;
|
|
38
|
+
cause?: unknown;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
export declare function abortError(): DOMException;
|
|
42
|
+
export declare function isAbort(err: unknown): boolean;
|
|
43
|
+
/** The error's string `code`, if it has one. */
|
|
44
|
+
export declare function codeOf(err: unknown): string | null;
|
|
45
|
+
/**
|
|
46
|
+
* A failure that says nothing about the request itself — the line dropped, the server had a bad
|
|
47
|
+
* moment, or a limiter answered without a named reason — so the same call may simply be made again.
|
|
48
|
+
* A named refusal never is: asking again gets the same answer.
|
|
49
|
+
*/
|
|
50
|
+
export declare function isTransient(err: unknown): boolean;
|
|
51
|
+
/** Wait `ms`, rejecting with an AbortError as soon as `signal` aborts. */
|
|
52
|
+
export declare function defaultSleep(ms: number, signal?: AbortSignal): Promise<void>;
|
|
53
|
+
export declare function isHttpsUrl(value: unknown): value is string;
|
|
54
|
+
/** A stored slot's copies, checked field by field; `null` when the answer is not usable. */
|
|
55
|
+
export declare function readCopies(raw: unknown): HeavyCopy[] | null;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// How an NMTS Heavy upload fails, and how it reads what the server and the network hand back.
|
|
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
|
+
// The orchestrator (`order-runner.ts`) takes its api from the caller, so it cannot know the caller's
|
|
7
|
+
// error class. It reads errors by SHAPE instead — a string `code` (the server's error code, or a
|
|
8
|
+
// transport word such as NETWORK) and a number `status` — which both the browser's and the command
|
|
9
|
+
// line's api clients carry.
|
|
10
|
+
import { heavyRefusalOf } from "../api/types-heavy.js";
|
|
11
|
+
export class HeavyOrderError extends Error {
|
|
12
|
+
reason;
|
|
13
|
+
/** The contract refusal behind `refused` or `payment_refused`, when there was one. */
|
|
14
|
+
refusal;
|
|
15
|
+
orderId;
|
|
16
|
+
slot;
|
|
17
|
+
constructor(reason, message, details = {}) {
|
|
18
|
+
super(message, details.cause === undefined ? undefined : { cause: details.cause });
|
|
19
|
+
this.name = "HeavyOrderError";
|
|
20
|
+
this.reason = reason;
|
|
21
|
+
this.refusal = details.refusal ?? null;
|
|
22
|
+
this.orderId = details.orderId ?? null;
|
|
23
|
+
this.slot = details.slot ?? null;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export function abortError() {
|
|
27
|
+
return new DOMException("The operation was aborted.", "AbortError");
|
|
28
|
+
}
|
|
29
|
+
export function isAbort(err) {
|
|
30
|
+
return typeof err === "object" && err !== null && "name" in err && err.name === "AbortError";
|
|
31
|
+
}
|
|
32
|
+
/** The error's string `code`, if it has one. */
|
|
33
|
+
export function codeOf(err) {
|
|
34
|
+
return typeof err === "object" && err !== null && "code" in err && typeof err.code === "string"
|
|
35
|
+
? err.code
|
|
36
|
+
: null;
|
|
37
|
+
}
|
|
38
|
+
function statusOf(err) {
|
|
39
|
+
return typeof err === "object" && err !== null && "status" in err && typeof err.status === "number"
|
|
40
|
+
? err.status
|
|
41
|
+
: null;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* A failure that says nothing about the request itself — the line dropped, the server had a bad
|
|
45
|
+
* moment, or a limiter answered without a named reason — so the same call may simply be made again.
|
|
46
|
+
* A named refusal never is: asking again gets the same answer.
|
|
47
|
+
*/
|
|
48
|
+
export function isTransient(err) {
|
|
49
|
+
if (isAbort(err))
|
|
50
|
+
return false;
|
|
51
|
+
const code = codeOf(err);
|
|
52
|
+
if (code !== null && heavyRefusalOf(code) !== null)
|
|
53
|
+
return false;
|
|
54
|
+
if (code === "NETWORK" || code === "INTERNAL" || code === "BAD_RESPONSE" || code === "RATE_LIMITED") {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
const status = statusOf(err);
|
|
58
|
+
if (status !== null)
|
|
59
|
+
return status >= 500;
|
|
60
|
+
// A bare TypeError is what `fetch` throws when the line is down.
|
|
61
|
+
return err instanceof TypeError;
|
|
62
|
+
}
|
|
63
|
+
/** Wait `ms`, rejecting with an AbortError as soon as `signal` aborts. */
|
|
64
|
+
export function defaultSleep(ms, signal) {
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
if (signal?.aborted) {
|
|
67
|
+
reject(abortError());
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const onAbort = () => {
|
|
71
|
+
clearTimeout(timer);
|
|
72
|
+
reject(abortError());
|
|
73
|
+
};
|
|
74
|
+
const timer = setTimeout(() => {
|
|
75
|
+
signal?.removeEventListener("abort", onAbort);
|
|
76
|
+
resolve();
|
|
77
|
+
}, ms);
|
|
78
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
export function isHttpsUrl(value) {
|
|
82
|
+
if (typeof value !== "string")
|
|
83
|
+
return false;
|
|
84
|
+
try {
|
|
85
|
+
return new URL(value).protocol === "https:";
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
// Not a URL at all — the same answer as "not https".
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** A stored slot's copies, checked field by field; `null` when the answer is not usable. */
|
|
93
|
+
export function readCopies(raw) {
|
|
94
|
+
if (!Array.isArray(raw) || raw.length === 0)
|
|
95
|
+
return null;
|
|
96
|
+
const out = [];
|
|
97
|
+
for (const c of raw) {
|
|
98
|
+
if (typeof c !== "object" ||
|
|
99
|
+
c === null ||
|
|
100
|
+
!("provider_id" in c && "data_set_id" in c && "piece_id" in c && "retrieval_url" in c)) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
const { provider_id, data_set_id, piece_id, retrieval_url } = c;
|
|
104
|
+
if (typeof provider_id !== "string" ||
|
|
105
|
+
typeof data_set_id !== "string" ||
|
|
106
|
+
typeof piece_id !== "string" ||
|
|
107
|
+
!isHttpsUrl(retrieval_url)) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
out.push({ provider_id, data_set_id, piece_id, retrieval_url });
|
|
111
|
+
}
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** What a PieceCID can be computed over: bytes in memory, a Blob (read as a stream), or a stream. */
|
|
2
|
+
export type PieceSource = Uint8Array | Blob | ReadableStream<Uint8Array>;
|
|
3
|
+
/** Bytes a single Filecoin piece may carry at least, and at most (the storage companies' limit). */
|
|
4
|
+
export declare const PIECE_MIN_BYTES = 127;
|
|
5
|
+
export declare const PIECE_MAX_BYTES = 1065353216;
|
|
6
|
+
/** The PieceCID of `source`, in its string form (`bafkzcib…`). */
|
|
7
|
+
export declare function pieceCidOf(source: PieceSource): Promise<string>;
|
|
8
|
+
/**
|
|
9
|
+
* A pass-through stream that computes the PieceCID of whatever flows through it.
|
|
10
|
+
*
|
|
11
|
+
* For a caller that already streams the part somewhere and wants the id from the same read. The
|
|
12
|
+
* `result` settles once the stream is closed.
|
|
13
|
+
*/
|
|
14
|
+
export declare function pieceCidTransform(): Promise<{
|
|
15
|
+
transform: TransformStream<Uint8Array, Uint8Array>;
|
|
16
|
+
result: Promise<string>;
|
|
17
|
+
}>;
|
|
18
|
+
/**
|
|
19
|
+
* Whether a string is a well-formed PieceCID, and the raw byte size it names.
|
|
20
|
+
*
|
|
21
|
+
* Used to check what a server hands back before trusting it as the id of our bytes. `null` means
|
|
22
|
+
* the string is not a PieceCID at all.
|
|
23
|
+
*/
|
|
24
|
+
export declare function pieceSizeOf(cid: string): Promise<number | null>;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// The Filecoin piece id (PieceCID, FRC-0069) of one sealed part.
|
|
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
|
+
// WHAT IT IS. A PieceCID names bytes by the root of a binary Merkle tree over their FR32-padded form,
|
|
7
|
+
// plus how much padding the tree needed and its height (`bafkzcib…`). The storage company checks the
|
|
8
|
+
// bytes it received against it, and the chain records it. We do not implement the tree: it comes
|
|
9
|
+
// from `@filoz/synapse-core/piece`, the library the Filecoin Onchain Cloud tooling itself uses.
|
|
10
|
+
//
|
|
11
|
+
// ⛔ LOADED ONLY WHEN SOMEBODY STORES ON FILECOIN. The library is imported dynamically on first use,
|
|
12
|
+
// so a program (or a browser page) that never touches NMTS Heavy never loads it.
|
|
13
|
+
//
|
|
14
|
+
// ⚠ THE BYTES ARE READ ONCE MORE. Computing the id is a full pass over the part (seconds for half a
|
|
15
|
+
// gigabyte); uploading is a second pass. The id is needed before the upload starts, because the
|
|
16
|
+
// server places the part only once it knows exactly what the part is.
|
|
17
|
+
/** Bytes a single Filecoin piece may carry at least, and at most (the storage companies' limit). */
|
|
18
|
+
export const PIECE_MIN_BYTES = 127;
|
|
19
|
+
export const PIECE_MAX_BYTES = 1_065_353_216;
|
|
20
|
+
/** The PieceCID of `source`, in its string form (`bafkzcib…`). */
|
|
21
|
+
export async function pieceCidOf(source) {
|
|
22
|
+
const { calculate } = await import("./synapse-piece.js");
|
|
23
|
+
return (await calculate(source)).toString();
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A pass-through stream that computes the PieceCID of whatever flows through it.
|
|
27
|
+
*
|
|
28
|
+
* For a caller that already streams the part somewhere and wants the id from the same read. The
|
|
29
|
+
* `result` settles once the stream is closed.
|
|
30
|
+
*/
|
|
31
|
+
export async function pieceCidTransform() {
|
|
32
|
+
const { transformStream } = await import("./synapse-piece.js");
|
|
33
|
+
const { transform, result } = transformStream();
|
|
34
|
+
return { transform, result: result.then((cid) => cid.toString()) };
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Whether a string is a well-formed PieceCID, and the raw byte size it names.
|
|
38
|
+
*
|
|
39
|
+
* Used to check what a server hands back before trusting it as the id of our bytes. `null` means
|
|
40
|
+
* the string is not a PieceCID at all.
|
|
41
|
+
*/
|
|
42
|
+
export async function pieceSizeOf(cid) {
|
|
43
|
+
const { tryFrom } = await import("./synapse-piece.js");
|
|
44
|
+
const piece = tryFrom(cid);
|
|
45
|
+
if (piece === null)
|
|
46
|
+
return null;
|
|
47
|
+
try {
|
|
48
|
+
return piece.size;
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
// The library refuses a size past the safe-integer range. No part NMTS stores is near it, so
|
|
52
|
+
// such a string is not the id of our bytes, whatever else it may be.
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|