@artblocks/abx-storage 0.1.0-alpha.2 → 0.1.0-alpha.21

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/dist/probe.js ADDED
@@ -0,0 +1,155 @@
1
+ import { randomBytes } from 'node:crypto';
2
+ import { arweaveFunding, turboIdentity, turboIdentityAddress, isEthIdentity } from './arweave.js';
3
+ import { resolveBackend } from './resolve.js';
4
+ /** Matches readiness.ts's gateway-probe default — this module's other real-network default. */
5
+ const DEFAULT_CHECK_TIMEOUT_MS = 6000;
6
+ /** Race a call against a `ms` deadline, rejecting with `message` if it wins. Most methods probed
7
+ * here have no abort hook of their own, so this stops WAITING for a hung request rather than
8
+ * cancelling it — good enough to keep a caller (doctor) from hanging. Always clears its timer, so
9
+ * a fast call doesn't leave one pending. */
10
+ async function race(p, ms, message) {
11
+ let timer;
12
+ const timeout = new Promise((_, reject) => {
13
+ timer = setTimeout(() => reject(new Error(message)), ms);
14
+ });
15
+ try {
16
+ return await Promise.race([p, timeout]);
17
+ }
18
+ finally {
19
+ clearTimeout(timer);
20
+ }
21
+ }
22
+ /** fs + ipfs (and any future backend with nothing more to add): delegate straight to the existing
23
+ * `health()` — see the module doc for why this is reuse, not a stand-in pending real logic. */
24
+ export async function checkViaHealth(backend, opts = {}) {
25
+ const timeoutMs = opts.timeoutMs ?? DEFAULT_CHECK_TIMEOUT_MS;
26
+ try {
27
+ const h = await race(backend.health?.() ?? Promise.resolve({ ok: true, detail: 'no check defined for this backend' }), timeoutMs, `timed out after ${timeoutMs}ms`);
28
+ return { backend: backend.id, ok: h.ok, detail: h.detail ?? '' };
29
+ }
30
+ catch (e) {
31
+ return { backend: backend.id, ok: false, detail: e.message };
32
+ }
33
+ }
34
+ /**
35
+ * `cloud`: PUT a tiny probe object through the signed API, then GET it back with a plain,
36
+ * UNSIGNED fetch against `publicBase` — the same request a marketplace/browser would make. Reuses
37
+ * one well-known key (`abx-probe/check.txt`) rather than minting a new one per run, so repeated
38
+ * checks don't litter the bucket. `cfg` (when given) names the API write target too — the R2/S3
39
+ * endpoint-vs-public-base trap is only diagnosable if BOTH URLs are on screen, not just the one
40
+ * that failed.
41
+ */
42
+ export async function checkCloudBackend(backend, cfg, opts = {}) {
43
+ const timeoutMs = opts.timeoutMs ?? DEFAULT_CHECK_TIMEOUT_MS;
44
+ const key = 'abx-probe/check.txt';
45
+ const putUrl = cfg ? `${cfg.endpoint.replace(/\/+$/, '')}/${cfg.bucket}/${key}` : undefined;
46
+ if (!backend.publicBase) {
47
+ return {
48
+ backend: 'cloud',
49
+ ok: false,
50
+ detail: "no public read base configured — set ABX_S3_PUBLIC_BASE (or --public-base) to the bucket's public URL.",
51
+ putUrl,
52
+ };
53
+ }
54
+ if (!backend.putObject || !backend.getObject) {
55
+ return { backend: 'cloud', ok: false, detail: 'this cloud backend has no putObject/getObject — cannot round-trip check it.', putUrl };
56
+ }
57
+ const publicUrl = `${backend.publicBase.replace(/\/+$/, '')}/${key}`;
58
+ const token = randomBytes(8).toString('hex');
59
+ const body = `abx storage check ${token}`;
60
+ const bytes = new TextEncoder().encode(body);
61
+ try {
62
+ await race(backend.putObject(key, { bytes, contentType: 'text/plain' }), timeoutMs, `PUT timed out after ${timeoutMs}ms`);
63
+ }
64
+ catch (e) {
65
+ return { backend: 'cloud', ok: false, detail: `PUT via the API failed: ${e.message}`, putUrl, publicUrl };
66
+ }
67
+ const fetchFn = opts.fetchFn ?? fetch;
68
+ const ctrl = new AbortController();
69
+ const timer = setTimeout(() => ctrl.abort(), timeoutMs);
70
+ try {
71
+ const res = await fetchFn(publicUrl, { signal: ctrl.signal });
72
+ if (!res.ok) {
73
+ return {
74
+ backend: 'cloud',
75
+ ok: false,
76
+ detail: `GET via the public base failed (${res.status}) — the public base may point at a different bucket/endpoint than the write API.`,
77
+ putUrl,
78
+ publicUrl,
79
+ };
80
+ }
81
+ const got = new TextDecoder().decode(new Uint8Array(await res.arrayBuffer()));
82
+ if (got !== body) {
83
+ return {
84
+ backend: 'cloud',
85
+ ok: false,
86
+ detail: 'GET via the public base returned different bytes than were PUT — check for a caching layer or a public base pointed at a different bucket.',
87
+ putUrl,
88
+ publicUrl,
89
+ };
90
+ }
91
+ return { backend: 'cloud', ok: true, detail: `round-trip ok via ${publicUrl}`, putUrl, publicUrl };
92
+ }
93
+ catch (e) {
94
+ const aborted = e.name === 'AbortError';
95
+ return {
96
+ backend: 'cloud',
97
+ ok: false,
98
+ detail: aborted ? `GET via the public base timed out after ${timeoutMs}ms` : `GET via the public base failed: ${e.message}`,
99
+ putUrl,
100
+ publicUrl,
101
+ };
102
+ }
103
+ finally {
104
+ clearTimeout(timer);
105
+ }
106
+ }
107
+ /**
108
+ * `arweave`: the existing `health()` (gateway reachability + identity presence) plus a Turbo
109
+ * BALANCE read when an identity already exists — never a paid upload (no identity yet is reported
110
+ * as-is: it's created free on first real upload, so there's nothing to check).
111
+ */
112
+ export async function checkArweaveBackend(backend, cfg, opts = {}) {
113
+ const timeoutMs = opts.timeoutMs ?? DEFAULT_CHECK_TIMEOUT_MS;
114
+ let base;
115
+ try {
116
+ base = await race(backend.health?.() ?? Promise.resolve({ ok: true, detail: '' }), timeoutMs, `timed out after ${timeoutMs}ms`);
117
+ }
118
+ catch (e) {
119
+ base = { ok: false, detail: e.message };
120
+ }
121
+ if (!cfg)
122
+ return { backend: 'arweave', ok: base.ok, detail: base.detail ?? '' };
123
+ const id = turboIdentity(cfg);
124
+ if (!id) {
125
+ return { backend: 'arweave', ok: base.ok, detail: [base.detail, 'no identity yet — created free on first upload (nothing to check)'].filter(Boolean).join(' · ') };
126
+ }
127
+ try {
128
+ const funding = arweaveFunding(cfg);
129
+ const { credits } = await race(funding.balance(), timeoutMs, `balance check timed out after ${timeoutMs}ms`);
130
+ const address = turboIdentityAddress(id);
131
+ return {
132
+ backend: 'arweave',
133
+ ok: base.ok,
134
+ detail: [base.detail, `${credits} credits (identity ${address}${isEthIdentity(id) ? ', eth' : ''})`].filter(Boolean).join(' · '),
135
+ };
136
+ }
137
+ catch (e) {
138
+ return { backend: 'arweave', ok: base.ok, detail: [base.detail, `balance check failed: ${e.message}`].filter(Boolean).join(' · ') };
139
+ }
140
+ }
141
+ /**
142
+ * The one storage-config probe both `abx storage show --check` and `abx doctor` call — dispatches to
143
+ * the right per-backend check above. Resolving the backend can throw (missing required config, e.g.
144
+ * cloud with no endpoint/bucket) — same as every other `resolveBackend` call site, and the caller's
145
+ * existing try/catch around it is the right place to handle that, not this function.
146
+ */
147
+ export async function probeStorageBackend(opts, checkOpts = {}) {
148
+ const backend = resolveBackend(opts);
149
+ if (backend.id === 'cloud' || backend.id === 's3')
150
+ return checkCloudBackend(backend, opts.cloud, checkOpts);
151
+ if (backend.id === 'arweave')
152
+ return checkArweaveBackend(backend, opts.arweave, checkOpts);
153
+ return checkViaHealth(backend, checkOpts);
154
+ }
155
+ //# sourceMappingURL=probe.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"probe.js","sourceRoot":"","sources":["../src/probe.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,WAAW,EAAC,MAAM,aAAa,CAAC;AAGxC,OAAO,EAAC,cAAc,EAAE,aAAa,EAAE,oBAAoB,EAAE,aAAa,EAAqB,MAAM,cAAc,CAAC;AACpH,OAAO,EAAC,cAAc,EAA6B,MAAM,cAAc,CAAC;AAwCxE,+FAA+F;AAC/F,MAAM,wBAAwB,GAAG,IAAI,CAAC;AAEtC;;;6CAG6C;AAC7C,KAAK,UAAU,IAAI,CAAI,CAAa,EAAE,EAAU,EAAE,OAAe;IAC/D,IAAI,KAAoC,CAAC;IACzC,MAAM,OAAO,GAAG,IAAI,OAAO,CAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE;QAC/C,KAAK,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAC3D,CAAC,CAAC,CAAC;IACH,IAAI,C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rlFor } from '@artblocks/abx-sdk';
3
+ export type { LocatorNetwork } from '@artblocks/abx-sdk';
4
+ /**
5
+ * Is a locator **retrievable** yet — not just accepted?
6
+ *
7
+ * An upload service answers "accepted" the moment it has your bytes; a gateway answers "serving"
8
+ * only once they have propagated to it, and on Arweave that gap runs to minutes. Nothing in the
9
+ * upload result distinguishes the two, so the natural implementation — upload during a mint, write
10
+ * the locator into the token — mints a token that renders broken for the first minutes of its life.
11
+ *
12
+ * Two independent integrators hit this eight days apart. The first rebuilt this layer themselves
13
+ * (ranged GETs, a propagating/ready model, retry ladders lengthened after measuring real times) and
14
+ * concluded "every serious integrator will rebuild some version of this." The second published 32
15
+ * renders and found **32/32 404ing on `arweave.net` while 22/32 already served from `permagate.io`
16
+ * and `vilenarios.com`**, with the uploader reporting `CONFIRMED` throughout. That second
17
+ * observation is why this probes several gateways rather than one: propagation is per-gateway, so
18
+ * "your gateway doesn't have it" and "the network doesn't have it" are different answers, and only
19
+ * one of them means something is wrong.
20
+ */
21
+ export type Readiness =
22
+ /** The gateway this toolkit would actually use serves the bytes. Safe to reference. */
23
+ 'ready'
24
+ /** Another gateway serves it, so the data provably exists on the network — yours hasn't caught
25
+ * up. Waiting is the fix. */
26
+ | 'propagating'
27
+ /** No probed gateway serves it. Deliberately NOT called "propagating": from outside, a locator
28
+ * that is still settling and one that is simply wrong look identical, and claiming the friendlier
29
+ * of the two is how a tool teaches someone to ignore it. */
30
+ | 'unreachable';
31
+ export interface GatewayProbe {
32
+ /** The exact URL asked. */
33
+ url: string;
34
+ /** Host only — what a human recognises. */
35
+ gateway: string;
36
+ /** Whether this gateway served the bytes. */
37
+ serving: boolean;
38
+ /** HTTP status, or null when the request never completed (timeout, DNS, refused). */
39
+ status: number | null;
40
+ contentType: string | null;
41
+ /** Total size when the gateway reported one, else null. A ranged request is used, so this comes
42
+ * from `content-range`'s total rather than `content-length` (which is 1 byte on a 206). */
43
+ bytes: number | null;
44
+ ms: number;
45
+ /** Why the request didn't complete. Absent when it did, whatever the status. */
46
+ error?: string;
47
+ }
48
+ export interface LocatorStatus {
49
+ /** The locator as given. */
50
+ locator: string;
51
+ network: LocatorNetwork;
52
+ /** The txid / CID / absolute URL the locator resolves to. */
53
+ id: string;
54
+ readiness: Readiness;
55
+ /** The gateway a baked locator would actually resolve through — the one that decides whether a
56
+ * marketplace can render this today. */
57
+ primary: GatewayProbe;
58
+ /** Well-known others for the same network. Their whole job is to tell propagation from a bad id. */
59
+ alternates: GatewayProbe[];
60
+ }
61
+ export interface LocatorStatusOptions {
62
+ /** The gateway to treat as primary — normally the one the active backend is configured with, so
63
+ * the verdict is about the gateway that will really be used. Defaults per network. */
64
+ gateway?: string;
65
+ /** Skip the alternates (one request instead of three). The cost is the `propagating` verdict:
66
+ * without them, a locator that hasn't reached your gateway is indistinguishable from a bad one. */
67
+ primaryOnly?: boolean;
68
+ timeoutMs?: number;
69
+ fetchFn?: typeof fetch;
70
+ }
71
+ /** Parse any accepted locator form into its network + id. */
72
+ export declare function parseLocator(locator: string): {
73
+ network: LocatorNetwork;
74
+ id: string;
75
+ };
76
+ /**
77
+ * The gateway BASE for a locator network — **override → env → generic public default**
78
+ * (`ABX_IPFS_GATEWAY`/`ABX_ARWEAVE_GATEWAY`, then `ipfs.io`/`arweave.net`). This is the READ-TIME
79
+ * default for resolving an arbitrary on-chain locator whose upload backend is unknown to the
80
+ * resolver — distinct from {@link ipfsConfigFromEnv}/{@link arweaveConfigFromEnv}'s MODE-aware
81
+ * defaults for a backend this toolkit itself configured to write through (e.g. a `kubo` IPFS
82
+ * gateway defaults to `127.0.0.1:8080`, a locator resolver has no such context and must default
83
+ * to something that resolves for anyone). `http` locators carry their own base (they are already
84
+ * an absolute URL) and never reach here — callers return them verbatim before consulting a gateway.
85
+ *
86
+ * A thin compose over the SDK's split: {@link resolveGatewayBase} used to own the
87
+ * `override ?? env ?? default` line itself (`process.env` read inline); now the pure decision
88
+ * lives in `@artblocks/abx-sdk`'s `resolveGatewayBase` (src/gateways.ts) and the env read in its
89
+ * `gatewayConfigFromEnv`, so this function's whole body is just wiring override → (env or
90
+ * nothing) → the SDK's pure default. Kept here, under this same name and signature, because
91
+ * every existing caller (this package's own `locatorStatus`, token-api's `resolveLocatorUrl`)
92
+ * still wants exactly this three-rung precedence with one call.
93
+ */
94
+ export declare function resolveGatewayBase(network: LocatorNetwork, override?: string): string;
95
+ /**
96
+ * Ask one gateway whether it serves these bytes, reading **headers only**.
97
+ *
98
+ * A ranged request (`Range: bytes=0-0`) is what keeps this cheap: the content in question can be tens of
99
+ * megabytes and the question is only whether it is there. Gateways that ignore `Range` answer 200
100
+ * with the whole body, so the body is cancelled rather than read — without that, checking a large
101
+ * locator would download it.
102
+ */
103
+ export declare function probeGateway(url: string, opts?: {
104
+ timeoutMs?: number;
105
+ fetchFn?: typeof fetch;
106
+ }): Promise<GatewayProbe>;
107
+ /** Accepted-vs-retrievable for one locator, across the gateway that matters and a couple of others. */
108
+ export declare function locatorStatus(locator: string, opts?: LocatorStatusOptions): Promise<LocatorStatus>;
109
+ /** One poll attempt from {@link awaitLocatorReady} — lets a caller narrate progress (a CLI spinner,
110
+ * a log line) without this module printing anything itself (the SDK/storage layers never print). */
111
+ export interface AwaitLocatorReadyEvent {
112
+ /** 1-indexed poll attempt. */
113
+ attempt: number;
114
+ /** Milliseconds since {@link awaitLocatorReady} was called. */
115
+ elapsedMs: number;
116
+ status: LocatorStatus;
117
+ }
118
+ export interface AwaitLocatorReadyOptions {
119
+ /** Forwarded to every {@link locatorStatus} probe. */
120
+ gateway?: string;
121
+ primaryOnly?: boolean;
122
+ /** Per-probe network timeout, forwarded as {@link LocatorStatusOptions.timeoutMs} (NOT the
123
+ * poll's overall deadline — see `timeoutMs` below). Default 6000ms, same as `locatorStatus`. */
124
+ probeTimeoutMs?: number;
125
+ fetchFn?: typeof fetch;
126
+ /** Give up polling once this much total time has elapsed, returning the last status with
127
+ * `ready: false` rather than throwing — a caller decides what "still not ready" means for its
128
+ * own flow. Default 5 minutes: long enough for typical Arweave propagation (this module's whole
129
+ * reason to exist — see the module doc, "the gap runs to minutes"). */
130
+ timeoutMs?: number;
131
+ /** Base delay for the linear backoff between polls (`attempt * pollBaseMs`, via the SDK's
132
+ * {@link linearBackoffDelay} — the same primitive `service.ts`'s retry ladder uses). Default
133
+ * 3000ms, so the very first re-poll is quick and later ones back off. */
134
+ pollBaseMs?: number;
135
+ onEvent?: (e: AwaitLocatorReadyEvent) => void;
136
+ }
137
+ export interface AwaitLocatorReadyResult {
138
+ /** The last status observed — `'ready'` iff {@link ready} is true. */
139
+ status: LocatorStatus;
140
+ attempts: number;
141
+ elapsedMs: number;
142
+ /** False when the deadline was hit before `status.readiness` reached `'ready'`. */
143
+ ready: boolean;
144
+ }
145
+ /**
146
+ * Poll {@link locatorStatus} until it reports `'ready'` or the deadline passes.
147
+ *
148
+ * `locatorStatus` alone answers "right now" — the natural next question, for a caller that would
149
+ * otherwise upload during a mint and write the locator into a token that renders broken for its
150
+ * first minutes of life (see the module doc), is "wait until it's actually there." This is that,
151
+ * factored out once so a caller doesn't hand-roll its own poll/backoff loop around a one-shot check.
152
+ *
153
+ * `'propagating'` is treated the same as `'unreachable'` here — both mean "not yet what THIS caller
154
+ * can rely on" — but every intermediate status still reaches `onEvent`, so a caller that wants to
155
+ * distinguish "provably on the network, just behind" from "no evidence yet" can from the events.
156
+ */
157
+ export declare function awaitLocatorReady(locator: string, opts?: AwaitLocatorReadyOptions): Promise<AwaitLocatorReadyResult>;
158
+ //# sourceMappingURL=readiness.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"readiness.d.ts","sourceRoot":"","sources":["../src/readiness.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,KAAK,cAAc,EACpB,MAAM,oBAAoB,CAAC;AAK5B,OAAO,EAAC,aAAa,EAAC,MAAM,oBAAoB,CAAC;AACjD,YAAY,EAAC,cAAc,EAAC,MAAM,oBAAoB,CAAC;AAEvD;;;;;;;;;;;;;;;;GAgBG;AAEH,M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0 +1,185 @@
1
+ import { gatewayConfigFromEnv, gatewayUrlFor, linearBackoffDelay, resolveGatewayBase as sdkResolveGatewayBase, sleep, } from '@artblocks/abx-sdk';
2
+ // `gatewayUrlFor` and `LocatorNetwork` moved to `@artblocks/abx-sdk` (src/gateways.ts) — the
3
+ // abx-services integrator ask. Re-exported here unchanged; this package's own public API
4
+ // (including `./index.ts`'s re-export of both) stays identical.
5
+ export { gatewayUrlFor } from '@artblocks/abx-sdk';
6
+ /**
7
+ * Gateways probed as alternates, per network. Kept short on purpose — this is a diagnosis, not a
8
+ * survey, and each entry is a request someone waits on. The Arweave pair is the one the second
9
+ * report actually measured serving ahead of `arweave.net`, so they are evidence rather than taste.
10
+ */
11
+ const ALTERNATE_GATEWAYS = {
12
+ arweave: ['https://permagate.io', 'https://vilenarios.com'],
13
+ ipfs: ['https://dweb.link', 'https://w3s.link'],
14
+ };
15
+ const DEFAULT_TIMEOUT_MS = 6000;
16
+ /** Parse any accepted locator form into its network + id. */
17
+ export function parseLocator(locator) {
18
+ const raw = locator.trim();
19
+ if (/^ar:\/\//i.test(raw))
20
+ return { network: 'arweave', id: raw.slice(5).replace(/^\/+/, '') };
21
+ if (/^ipfs:\/\//i.test(raw))
22
+ return { network: 'ipfs', id: raw.slice(7).replace(/^\/+/, '') };
23
+ if (/^https?:\/\//i.test(raw)) {
24
+ // A gateway URL already names its own gateway; keep it whole and ask exactly it. Detecting the
25
+ // network from the shape lets the alternates still apply (that is the point of the whole probe).
26
+ if (/\/ipfs\//.test(raw) || /\.ipfs\./.test(raw))
27
+ return { network: 'ipfs', id: raw };
28
+ if (/arweave\.net|permagate\.io|vilenarios\.com|ar-io\.dev/.test(raw))
29
+ return { network: 'arweave', id: raw };
30
+ return { network: 'http', id: raw };
31
+ }
32
+ // Bare ids: an Arweave txid is 43 chars of base64url; a CIDv0 starts Qm…, a CIDv1 b…/f…
33
+ if (/^[A-Za-z0-9_-]{43}$/.test(raw))
34
+ return { network: 'arweave', id: raw };
35
+ if (/^(Qm[1-9A-HJ-NP-Za-km-z]{44}|b[a-z2-7]{20,}|f[0-9a-f]{20,})/.test(raw))
36
+ return { network: 'ipfs', id: raw };
37
+ throw new Error(`'${locator}' isn't a recognisable locator. Expected ar://<txid>, ipfs://<cid>, an https:// gateway URL, or a bare txid/CID.`);
38
+ }
39
+ /**
40
+ * The gateway BASE for a locator network — **override → env → generic public default**
41
+ * (`ABX_IPFS_GATEWAY`/`ABX_ARWEAVE_GATEWAY`, then `ipfs.io`/`arweave.net`). This is the READ-TIME
42
+ * default for resolving an arbitrary on-chain locator whose upload backend is unknown to the
43
+ * resolver — distinct from {@link ipfsConfigFromEnv}/{@link arweaveConfigFromEnv}'s MODE-aware
44
+ * defaults for a backend this toolkit itself configured to write through (e.g. a `kubo` IPFS
45
+ * gateway defaults to `127.0.0.1:8080`, a locator resolver has no such context and must default
46
+ * to something that resolves for anyone). `http` locators carry their own base (they are already
47
+ * an absolute URL) and never reach here — callers return them verbatim before consulting a gateway.
48
+ *
49
+ * A thin compose over the SDK's split: {@link resolveGatewayBase} used to own the
50
+ * `override ?? env ?? default` line itself (`process.env` read inline); now the pure decision
51
+ * lives in `@artblocks/abx-sdk`'s `resolveGatewayBase` (src/gateways.ts) and the env read in its
52
+ * `gatewayConfigFromEnv`, so this function's whole body is just wiring override → (env or
53
+ * nothing) → the SDK's pure default. Kept here, under this same name and signature, because
54
+ * every existing caller (this package's own `locatorStatus`, token-api's `resolveLocatorUrl`)
55
+ * still wants exactly this three-rung precedence with one call.
56
+ */
57
+ export function resolveGatewayBase(network, override) {
58
+ if (network === 'http')
59
+ return '';
60
+ if (override)
61
+ return sdkResolveGatewayBase(network, network === 'ipfs' ? { ipfs: override } : { arweave: override });
62
+ return sdkResolveGatewayBase(network, gatewayConfigFromEnv());
63
+ }
64
+ /**
65
+ * Ask one gateway whether it serves these bytes, reading **headers only**.
66
+ *
67
+ * A ranged request (`Range: bytes=0-0`) is what keeps this cheap: the content in question can be tens of
68
+ * megabytes and the question is only whether it is there. Gateways that ignore `Range` answer 200
69
+ * with the whole body, so the body is cancelled rather than read — without that, checking a large
70
+ * locator would download it.
71
+ */
72
+ export async function probeGateway(url, opts = {}) {
73
+ const doFetch = opts.fetchFn ?? fetch;
74
+ const gateway = safeHost(url);
75
+ const started = Date.now();
76
+ const ctrl = new AbortController();
77
+ const timer = setTimeout(() => ctrl.abort(), opts.timeoutMs ?? DEFAULT_TIMEOUT_MS);
78
+ try {
79
+ const res = await doFetch(url, { headers: { range: 'bytes=0-0' }, signal: ctrl.signal, redirect: 'follow' });
80
+ // Never read the body: a Range-ignoring gateway would otherwise stream the whole asset.
81
+ try {
82
+ await res.body?.cancel();
83
+ }
84
+ catch {
85
+ /* already consumed or unsupported — nothing to release */
86
+ }
87
+ return {
88
+ url,
89
+ gateway,
90
+ serving: res.status >= 200 && res.status < 300,
91
+ status: res.status,
92
+ contentType: res.headers.get('content-type'),
93
+ bytes: totalBytesFrom(res.headers.get('content-range'), res.headers.get('content-length'), res.status),
94
+ ms: Date.now() - started,
95
+ };
96
+ }
97
+ catch (err) {
98
+ const aborted = err.name === 'AbortError' || ctrl.signal.aborted;
99
+ return {
100
+ url,
101
+ gateway,
102
+ serving: false,
103
+ status: null,
104
+ contentType: null,
105
+ bytes: null,
106
+ ms: Date.now() - started,
107
+ error: aborted ? `no response in ${opts.timeoutMs ?? DEFAULT_TIMEOUT_MS}ms` : err.message,
108
+ };
109
+ }
110
+ finally {
111
+ clearTimeout(timer);
112
+ }
113
+ }
114
+ /** Accepted-vs-retrievable for one locator, across the gateway that matters and a couple of others. */
115
+ export async function locatorStatus(locator, opts = {}) {
116
+ const { network, id } = parseLocator(locator);
117
+ const primaryGateway = resolveGatewayBase(network, opts.gateway);
118
+ const primaryUrl = gatewayUrlFor(network, id, primaryGateway);
119
+ // `http` locators have no notion of an alternate — the URL IS the address, so a second host would
120
+ // be answering about different bytes.
121
+ const alternateBases = opts.primaryOnly || network === 'http' ? [] : ALTERNATE_GATEWAYS[network];
122
+ const alternateUrls = alternateBases
123
+ .map((base) => gatewayUrlFor(network, id, base))
124
+ .filter((u) => u !== primaryUrl);
125
+ const probe = (u) => probeGateway(u, { timeoutMs: opts.timeoutMs, fetchFn: opts.fetchFn });
126
+ const [primary, ...alternates] = await Promise.all([probe(primaryUrl), ...alternateUrls.map(probe)]);
127
+ const readiness = primary.serving
128
+ ? 'ready'
129
+ : alternates.some((a) => a.serving)
130
+ ? 'propagating'
131
+ : 'unreachable';
132
+ return { locator, network, id, readiness, primary, alternates };
133
+ }
134
+ const DEFAULT_AWAIT_TIMEOUT_MS = 5 * 60_000;
135
+ const DEFAULT_POLL_BASE_MS = 3000;
136
+ /**
137
+ * Poll {@link locatorStatus} until it reports `'ready'` or the deadline passes.
138
+ *
139
+ * `locatorStatus` alone answers "right now" — the natural next question, for a caller that would
140
+ * otherwise upload during a mint and write the locator into a token that renders broken for its
141
+ * first minutes of life (see the module doc), is "wait until it's actually there." This is that,
142
+ * factored out once so a caller doesn't hand-roll its own poll/backoff loop around a one-shot check.
143
+ *
144
+ * `'propagating'` is treated the same as `'unreachable'` here — both mean "not yet what THIS caller
145
+ * can rely on" — but every intermediate status still reaches `onEvent`, so a caller that wants to
146
+ * distinguish "provably on the network, just behind" from "no evidence yet" can from the events.
147
+ */
148
+ export async function awaitLocatorReady(locator, opts = {}) {
149
+ const deadlineMs = opts.timeoutMs ?? DEFAULT_AWAIT_TIMEOUT_MS;
150
+ const pollBaseMs = opts.pollBaseMs ?? DEFAULT_POLL_BASE_MS;
151
+ const started = Date.now();
152
+ const probeOpts = { gateway: opts.gateway, primaryOnly: opts.primaryOnly, timeoutMs: opts.probeTimeoutMs, fetchFn: opts.fetchFn };
153
+ let attempt = 0;
154
+ for (;;) {
155
+ attempt++;
156
+ const status = await locatorStatus(locator, probeOpts);
157
+ const elapsedMs = Date.now() - started;
158
+ opts.onEvent?.({ attempt, elapsedMs, status });
159
+ if (status.readiness === 'ready')
160
+ return { status, attempts: attempt, elapsedMs, ready: true };
161
+ const delay = linearBackoffDelay(attempt, pollBaseMs);
162
+ if (elapsedMs + delay >= deadlineMs)
163
+ return { status, attempts: attempt, elapsedMs, ready: false };
164
+ await sleep(delay);
165
+ }
166
+ }
167
+ /** Total size from a ranged response's `content-range` (`bytes 0-0/12345`), else `content-length`
168
+ * when the gateway ignored the range and answered 200 with the whole thing. */
169
+ function totalBytesFrom(contentRange, contentLength, status) {
170
+ const m = contentRange?.match(/\/\s*(\d+)\s*$/);
171
+ if (m)
172
+ return Number(m[1]);
173
+ if (status === 200 && contentLength && /^\d+$/.test(contentLength))
174
+ return Number(contentLength);
175
+ return null;
176
+ }
177
+ function safeHost(url) {
178
+ try {
179
+ return new URL(url).host;
180
+ }
181
+ catch {
182
+ return url;
183
+ }
184
+ }
185
+ //# sourceMappingURL=readiness.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"readiness.js","sourceRoot":"","sources":["../src/readiness.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,aAAa,EACb,kBAAkB,EAClB,kBAAkB,IAAI,qBAAqB,EAC3C,KAAK,GAEN,MAAM,oBAAoB,CAAC;AAE5B,6FAA6F;AAC7F,yFAAyF;AACzF,gEAAgE;AAChE,OAAO,EAAC,aAAa,EAAC,MAAM,oBAAoB,CAAC;AAgEjD;;;;GAIG;AACH,MAAM,kBAAkB,GAAyC;IAC/D,OAAO,EAAE,CAAC,sBAAsB,EAAE,wBAAwB,CAAC;IAC3D,IAAI,EAAE,CAAC,mBAAmB,EAAE,kBAAkB,CAAC;CAChD,CAAC;AAEF,MAAM,kBAAkB,GAAG,IAAI,CAAC;AAahC,6DAA6D;AAC7D,MAAM,UAAU,YAAY,CAAC,OAAe;IAC1C,MAAM,GAAG,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC;IAC3B,IAAI,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC;QAAE,OAAO,EAAC,OAAO,EAAE,SAAS,EAAE,EAAE,EAAE,GAAG,CAA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AAC,IAAI,CAAC,aAAa,CAAC;QAAE,OAAO,MAAM,CAAC,aAAa,CAAC,CAAC;IACjG,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,QAAQ,CAAC,GAAW;IAC3B,IAAI,CAAC;QACH,OAAO,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;IAC3B,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,GAAG,CAAC;IACb,CAAC;AACH,CAAC"}
@@ -0,0 +1,89 @@
1
+ import type { ResolveStorageOptions } from './resolve.js';
2
+ import { type ArweaveConfig } from './arweave.js';
3
+ /**
4
+ * Deploy-time storage readiness — what a REAL upload needs, decided from local facts (and, for the
5
+ * best-effort USD estimate, one price-API call) so "dry-run passes, then the real deploy fails at
6
+ * upload" can't happen. Three questions, each pure aside from the one noted network call:
7
+ *
8
+ * - {@link planTurboUpload} — is THIS ONE file free (Turbo's 100 KiB tier) or chargeable?
9
+ * - {@link assessStorageReadiness} — across every backend, what does a real upload of `sizes` need
10
+ * (Arweave credits, a Pinata JWT, a cloud public base)?
11
+ * - {@link assessTurboFunds} — does the resolved Turbo identity have ENOUGH prepaid credits for
12
+ * `sizes`, and (when it's short) does the creator's own wallet already hold some?
13
+ *
14
+ * None of this mints or spends anything — it only INTERROGATES the resolved provider. Identity
15
+ * creation is the caller's job (the CLI's managed-key flow owns the key file location; see
16
+ * `arweave-identity.ts` / `ArweaveConfig.ensureJwk`), so `assessTurboFunds` expects a config that
17
+ * already has one.
18
+ */
19
+ /** Whether `opts` resolves to Arweave via the Turbo (prepaid-credits) provider — the only provider
20
+ * with a free tier and a funds balance to check; `http-bundler` skips every readiness/funds
21
+ * question below (it has its own auth, not a balance this package knows how to read). */
22
+ export declare function isTurboArweave(opts: ResolveStorageOptions): boolean;
23
+ export interface TurboUploadPlan {
24
+ /** Whether this upload is over Turbo's 100 KiB free tier (so it draws on prepaid credits). */
25
+ chargeable: boolean;
26
+ /** `size` in KB, formatted to 1 decimal — for the free-vs-credit readout. */
27
+ sizeKb: string;
28
+ }
29
+ /** The free-vs-credit decision for ONE Turbo upload of `size` bytes — from the local byte size
30
+ * alone, so it works under `--dry-run` too. Null when `opts` doesn't resolve to Turbo-Arweave
31
+ * (nothing to decide). */
32
+ export declare function planTurboUpload(opts: ResolveStorageOptions, size: number): TurboUploadPlan | null;
33
+ /** Per-backend readiness facts for a real upload of `sizes` (per-file byte counts). Each variant
34
+ * carries only what its backend needs checked; a backend with nothing to check (`fs`, or anything
35
+ * this package doesn't know about) reports `'other'` — a literal, not the resolved id, so the
36
+ * union stays a real discriminated union (a plain `string` member would swallow the other
37
+ * branches on narrowing) — callers that branch on `backend` never needed the id anyway. */
38
+ export type StorageReadinessReport = {
39
+ backend: 'arweave';
40
+ overCount: number;
41
+ totalCount: number;
42
+ totalKb: number;
43
+ usd: number | null;
44
+ } | {
45
+ backend: 'ipfs';
46
+ pinataMissingJwt: boolean;
47
+ } | {
48
+ backend: 'cloud';
49
+ missingPublicBase: boolean;
50
+ } | {
51
+ backend: 'other';
52
+ };
53
+ /**
54
+ * What a REAL deploy against `opts` needs, from `sizes` (per-file byte sizes) alone — Arweave
55
+ * credit needs (plus a best-effort USD estimate for the chargeable bytes), a missing Pinata JWT, or
56
+ * a missing cloud public base. The one network call (Turbo's price API) is best-effort: a failure
57
+ * there degrades `usd` to `null` rather than failing the whole check.
58
+ */
59
+ export declare function assessStorageReadiness(opts: ResolveStorageOptions, sizes: number[]): Promise<StorageReadinessReport>;
60
+ export interface TurboFundsCheck {
61
+ /** True when the resolved identity does NOT have enough prepaid credits for `sizes`. */
62
+ short: boolean;
63
+ address: string;
64
+ /** Whether the identity is an EVM one (`.env` key or a remote wallet) rather than the CLI-managed
65
+ * Arweave key — decides which recovery message applies. */
66
+ isEth: boolean;
67
+ haveWinc: number;
68
+ needWinc: number;
69
+ /** How many of `sizes` are over the free tier (what this shortfall is actually for). */
70
+ chargeableCount: number;
71
+ /** Set only when checked (short, a non-eth identity, and a wallet address was given): the
72
+ * creator's OWN wallet's Turbo balance, queryable by address with no key — so "spend those
73
+ * instead of topping up again" can be recommended before ever proposing a top-up. Null if that
74
+ * wallet has never had a Turbo balance. */
75
+ walletCredits?: {
76
+ winc: number;
77
+ credits: string;
78
+ } | null;
79
+ }
80
+ /**
81
+ * The pre-upload funds decision for a Turbo (Arweave) upload of `sizes` (per-file byte sizes):
82
+ * chargeable bytes → the Winston cost → compare against the identity's prepaid balance. `cfg` must
83
+ * already have a resolved identity (`jwk`/`ethSignerKey`/`remoteEth`) — this module only
84
+ * INTERROGATES the resolved provider, never mints one (see the module doc). Returns `null` when
85
+ * nothing is chargeable (every file is free) or the provider can't be reached right now — a
86
+ * best-effort check never false-blocks; the upload itself will surface any real error.
87
+ */
88
+ export declare function assessTurboFunds(cfg: ArweaveConfig, sizes: number[], walletAddr?: string): Promise<TurboFundsCheck | null>;
89
+ //# sourceMappingURL=upload-readiness.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upload-readiness.d.ts","sourceRoot":"","sources":["../src/upload-readiness.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,qBAAqB,EAAC,MAAM,cAAc,CAAC;AACxD,OAAO,EAAyG,KAAK,aAAa,EAAC,MAAM,cAAc,CAAC;AAExJ;;;;;;;;;;;;;;;GAeG;AAEH;;0FAE0F;AAC1F,wBAAgB,cAAc,CAAC,IAAI,EAAE,qBAAqB,GAAG,OAAO,CAEnE;AAED,MAAM,WAAW,eAAe;IAC9B,8FAA8F;IAC9F,UAAU,EAAE,OAAO,CAAC;IACpB,6EAA6E;IAC7E,MAAM,EAAE,MAAM,CAAC;CAChB;AAED;;2BAE2B;AAC3B,wBAAgB,eAAe,CAAC,IAAI,EAAE,qBAAqB,EAAE,IAAI,EAAE,MAAM,GAAG,eAAe,GAAG,IAAI,CAGjG;AAED;;;;4FAI4F;AAC5F,MAAM,MAAM,sBAAsB,GAC9B;IAAC,OAAO,EAAE,SAAS,CAAC;IAAC,SAAS,EAAE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