@artblocks/abx-storage 0.1.0-alpha.4 → 0.1.0-alpha.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/dist/arweave.d.ts +45 -44
  3. package/dist/arweave.d.ts.map +1 -1
  4. package/dist/arweave.js +98 -155
  5. package/dist/arweave.js.map +1 -1
  6. package/dist/backend.d.ts +1 -4
  7. package/dist/backend.d.ts.map +1 -1
  8. package/dist/backend.js +4 -33
  9. package/dist/backend.js.map +1 -1
  10. package/dist/content-index.d.ts.map +1 -1
  11. package/dist/content-index.js +3 -1
  12. package/dist/content-index.js.map +1 -1
  13. package/dist/content-plan.d.ts +78 -0
  14. package/dist/content-plan.d.ts.map +1 -0
  15. package/dist/content-plan.js +68 -0
  16. package/dist/content-plan.js.map +1 -0
  17. package/dist/fs.d.ts.map +1 -1
  18. package/dist/fs.js +3 -1
  19. package/dist/fs.js.map +1 -1
  20. package/dist/index.d.ts +7 -1
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +7 -1
  23. package/dist/index.js.map +1 -1
  24. package/dist/ipfs.d.ts +17 -1
  25. package/dist/ipfs.d.ts.map +1 -1
  26. package/dist/ipfs.js +60 -2
  27. package/dist/ipfs.js.map +1 -1
  28. package/dist/migrate.d.ts +37 -0
  29. package/dist/migrate.d.ts.map +1 -0
  30. package/dist/migrate.js +55 -0
  31. package/dist/migrate.js.map +1 -0
  32. package/dist/probe.d.ts +66 -0
  33. package/dist/probe.d.ts.map +1 -0
  34. package/dist/probe.js +155 -0
  35. package/dist/probe.js.map +1 -0
  36. package/dist/readiness.d.ts +158 -0
  37. package/dist/readiness.d.ts.map +1 -0
  38. package/dist/readiness.js +183 -0
  39. package/dist/readiness.js.map +1 -0
  40. package/dist/upload-readiness.d.ts +89 -0
  41. package/dist/upload-readiness.d.ts.map +1 -0
  42. package/dist/upload-readiness.js +85 -0
  43. package/dist/upload-readiness.js.map +1 -0
  44. package/dist/upload.d.ts +28 -0
  45. package/dist/upload.d.ts.map +1 -0
  46. package/dist/upload.js +41 -0
  47. package/dist/upload.js.map +1 -0
  48. package/package.json +17 -9
@@ -0,0 +1,66 @@
1
+ import type { StorageBackend } from './backend.js';
2
+ import type { CloudStorageConfig } from './cloud.js';
3
+ import { type ArweaveConfig } from './arweave.js';
4
+ import { type ResolveStorageOptions } from './resolve.js';
5
+ /**
6
+ * `abx storage show --check` / `abx doctor`'s storage section — go beyond "is this backend
7
+ * CONFIGURED" (what {@link resolveBackend} plus each backend's own `health()` already answer) to
8
+ * "does a write actually reach the URL a token would bake on-chain."
9
+ *
10
+ * The gap this closes is the R2/S3 endpoint-vs-public-base confusion: a backend's `health()` signs
11
+ * a request against the API `endpoint` — proving credentials work — but never touches the SEPARATE
12
+ * `publicBase` a marketplace would actually fetch from. Those can point at different buckets (or one
13
+ * can simply be wrong) with `health()` reporting green the whole time. A PUT through the API,
14
+ * fetched back over the PUBLIC base with a plain unsigned GET, is the only check that proves the two
15
+ * agree.
16
+ *
17
+ * `fs` and `ipfs` are NOT reimplemented here — their existing `health()` already does exactly what's
18
+ * asked (fs: writability; ipfs: gateway/API reachability, no upload) and duplicating that logic is
19
+ * how the two copies drift. `arweave` layers a balance READ (never a paid upload) on top of its
20
+ * existing identity check. `cloud` gets the new round trip.
21
+ */
22
+ export interface StorageCheckResult {
23
+ backend: string;
24
+ ok: boolean;
25
+ detail: string;
26
+ /** Set on a `cloud` check — the API write target and the public read URL, so a mismatch between
27
+ * them (the R2/S3 endpoint-vs-public-base trap) is visible on failure rather than a bare
28
+ * "fetch failed". Equal in shape, not necessarily in host — that's exactly what's being checked. */
29
+ putUrl?: string;
30
+ publicUrl?: string;
31
+ }
32
+ export interface StorageCheckOptions {
33
+ /** Bound on each network step. A backend method with no abort hook (cloud's `putObject`/
34
+ * `getObject`, arweave's balance read) is raced against this rather than truly cancelled — good
35
+ * enough to keep a caller (doctor) from hanging, not a guarantee the underlying request stopped. */
36
+ timeoutMs?: number;
37
+ /** Injectable for tests — used for the public-base GET only (the PUT/API side goes through the
38
+ * resolved backend, which owns its own signing). */
39
+ fetchFn?: typeof fetch;
40
+ }
41
+ /** fs + ipfs (and any future backend with nothing more to add): delegate straight to the existing
42
+ * `health()` — see the module doc for why this is reuse, not a stand-in pending real logic. */
43
+ export declare function checkViaHealth(backend: StorageBackend, opts?: StorageCheckOptions): Promise<StorageCheckResult>;
44
+ /**
45
+ * `cloud`: PUT a tiny probe object through the signed API, then GET it back with a plain,
46
+ * UNSIGNED fetch against `publicBase` — the same request a marketplace/browser would make. Reuses
47
+ * one well-known key (`abx-probe/check.txt`) rather than minting a new one per run, so repeated
48
+ * checks don't litter the bucket. `cfg` (when given) names the API write target too — the R2/S3
49
+ * endpoint-vs-public-base trap is only diagnosable if BOTH URLs are on screen, not just the one
50
+ * that failed.
51
+ */
52
+ export declare function checkCloudBackend(backend: StorageBackend, cfg?: CloudStorageConfig, opts?: StorageCheckOptions): Promise<StorageCheckResult>;
53
+ /**
54
+ * `arweave`: the existing `health()` (gateway reachability + identity presence) plus a Turbo
55
+ * BALANCE read when an identity already exists — never a paid upload (no identity yet is reported
56
+ * as-is: it's created free on first real upload, so there's nothing to check).
57
+ */
58
+ export declare function checkArweaveBackend(backend: StorageBackend, cfg: ArweaveConfig | undefined, opts?: StorageCheckOptions): Promise<StorageCheckResult>;
59
+ /**
60
+ * The one storage-config probe both `abx storage show --check` and `abx doctor` call — dispatches to
61
+ * the right per-backend check above. Resolving the backend can throw (missing required config, e.g.
62
+ * cloud with no endpoint/bucket) — same as every other `resolveBackend` call site, and the caller's
63
+ * existing try/catch around it is the right place to handle that, not this function.
64
+ */
65
+ export declare function probeStorageBackend(opts: ResolveStorageOptions, checkOpts?: StorageCheckOptions): Promise<StorageCheckResult>;
66
+ //# sourceMappingURL=probe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"probe.d.ts","sourceRoot":"","sources":["../src/probe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+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 = await 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,CAAC;QACH,OAAO,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;IAC1C,CAAC;YAAS,CAAC;QACT,YAAY,CAAC,KAAM,CAAC,CAAC;IACvB,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EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,OAAO,sBAAsB,OAAO,GAAG,aAAa,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC;SACjI,CAAC;IACJ,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,EAAC,OAAO,EAAE,SAAS,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,CAAC,IAAI,CAAC,MAAM,EAAE,yBAA0B,CAAW,CAAC,OAAO,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,EAAC,CAAC;IAC/I,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CAAC,IAA2B,EAAE,YAAiC,EAAE;IACxG,MAAM,OAAO,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;IACrC,IAAI,OAAO,CAAC,EAAE,KAAK,OAAO,IAAI,OAAO,CAAC,EAAE,KAAK,IAAI;QAAE,OAAO,iBAAiB,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;IAC5G,IAAI,OAAO,CAAC,EAAE,KAAK,SAAS;QAAE,OAAO,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IAC3F,OAAO,cAAc,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;AAC5C,CAAC"}
@@ -0,0 +1,158 @@
1
+ import { type LocatorNetwork } from '@artblocks/abx-sdk';
2
+ export { gatewayUrlFor } 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;AAG5B,OAAO,EAAC,aAAa,EAAC,MAAM,oBAAoB,CAAC;AACjD,YAAY,EAAC,cAAc,EAAC,MAAM,oBAAoB,CAAC;AAEvD;;;;;;;;;;;;;;;;GAgBG;AAEH,MAAM,MAAM,SAAS;AACnB,uFAAuF;AACrF,OAAO;AACT;8BAC8B;GAC5B,aAAa;AACf;;6DAE6D;GAC3D,aAAa,CAAC;AAElB,MAAM,WAAW,YAAY;IAC3B,2BAA2B;IAC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C,OAAO,CAAC,EAAE,OAAO,KAAK,CAAA;CAAM,GACtD,OAAO,CAAC,YAAY,CAAC,CAsCvB;AAED,uGAAuG;AACvG,wBAAsB,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,GAAE,oBAAyB,GAAG,OAAO,CAAC,aAAa,CAAC,CAsB5G;AAED;qGACqG;AACrG,MAAM,WAAW,sBAAsB;IACrC,8BAA8B;IAC9B,OAAO,EAAE,MAAM,CAAC;IAChB,+DAA+D;IAC/D,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,aAAa,CAAC;CACvB;AAED,MAAM,WAAW,wBAAwB;IACvC,sDAAsD;IACtD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB;qGACiG;IACjG,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,OAAO,CAAC,EAAE,OAAO,KAAK,CAAC;IACvB;;;4EAGwE;IACxE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;8EAE0E;IAC1E,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,CAAC,CAAC,EAAE,sBAAsB,KAAK,IAAI,CAAC;CAC/C;AAED,MAAM,WAAW,uBAAuB;IACtC,sEAAsE;IACtE,MAAM,EAAE,aAAa,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,mFAAmF;IACnF,KAAK,EAAE,OAAO,CAAC;CAChB;AAKD;;;;;;;;;;;GAWG;AACH,wBAAsB,iBAAiB,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,GAAE,wBAA6B,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAgB9H"}
@@ -0,0 +1,183 @@
1
+ import { gatewayConfigFromEnv, gatewayUrlFor, linearBackoffDelay, resolveGatewayBase as sdkResolveGatewayBase, sleep, } from '@artblocks/abx-sdk';
2
+ // Re-export the SDK gateway types and helpers unchanged so this package's public API remains stable.
3
+ export { gatewayUrlFor } from '@artblocks/abx-sdk';
4
+ /**
5
+ * Gateways probed as alternates, per network. Kept short on purpose — this is a diagnosis, not a
6
+ * survey, and each entry is a request someone waits on. The Arweave pair is the one the second
7
+ * report actually measured serving ahead of `arweave.net`, so they are evidence rather than taste.
8
+ */
9
+ const ALTERNATE_GATEWAYS = {
10
+ arweave: ['https://permagate.io', 'https://vilenarios.com'],
11
+ ipfs: ['https://dweb.link', 'https://w3s.link'],
12
+ };
13
+ const DEFAULT_TIMEOUT_MS = 6000;
14
+ /** Parse any accepted locator form into its network + id. */
15
+ export function parseLocator(locator) {
16
+ const raw = locator.trim();
17
+ if (/^ar:\/\//i.test(raw))
18
+ return { network: 'arweave', id: raw.slice(5).replace(/^\/+/, '') };
19
+ if (/^ipfs:\/\//i.test(raw))
20
+ return { network: 'ipfs', id: raw.slice(7).replace(/^\/+/, '') };
21
+ if (/^https?:\/\//i.test(raw)) {
22
+ // A gateway URL already names its own gateway; keep it whole and ask exactly it. Detecting the
23
+ // network from the shape lets the alternates still apply (that is the point of the whole probe).
24
+ if (/\/ipfs\//.test(raw) || /\.ipfs\./.test(raw))
25
+ return { network: 'ipfs', id: raw };
26
+ if (/arweave\.net|permagate\.io|vilenarios\.com|ar-io\.dev/.test(raw))
27
+ return { network: 'arweave', id: raw };
28
+ return { network: 'http', id: raw };
29
+ }
30
+ // Bare ids: an Arweave txid is 43 chars of base64url; a CIDv0 starts Qm…, a CIDv1 b…/f…
31
+ if (/^[A-Za-z0-9_-]{43}$/.test(raw))
32
+ return { network: 'arweave', id: raw };
33
+ if (/^(Qm[1-9A-HJ-NP-Za-km-z]{44}|b[a-z2-7]{20,}|f[0-9a-f]{20,})/.test(raw))
34
+ return { network: 'ipfs', id: raw };
35
+ throw new Error(`'${locator}' isn't a recognisable locator. Expected ar://<txid>, ipfs://<cid>, an https:// gateway URL, or a bare txid/CID.`);
36
+ }
37
+ /**
38
+ * The gateway BASE for a locator network — **override → env → generic public default**
39
+ * (`ABX_IPFS_GATEWAY`/`ABX_ARWEAVE_GATEWAY`, then `ipfs.io`/`arweave.net`). This is the READ-TIME
40
+ * default for resolving an arbitrary on-chain locator whose upload backend is unknown to the
41
+ * resolver — distinct from {@link ipfsConfigFromEnv}/{@link arweaveConfigFromEnv}'s MODE-aware
42
+ * defaults for a backend this toolkit itself configured to write through (e.g. a `kubo` IPFS
43
+ * gateway defaults to `127.0.0.1:8080`, a locator resolver has no such context and must default
44
+ * to something that resolves for anyone). `http` locators carry their own base (they are already
45
+ * an absolute URL) and never reach here — callers return them verbatim before consulting a gateway.
46
+ *
47
+ * A thin compose over the SDK's split: {@link resolveGatewayBase} used to own the
48
+ * `override ?? env ?? default` line itself (`process.env` read inline); now the pure decision
49
+ * lives in `@artblocks/abx-sdk`'s `resolveGatewayBase` (src/gateways.ts) and the env read in its
50
+ * `gatewayConfigFromEnv`, so this function's whole body is just wiring override → (env or
51
+ * nothing) → the SDK's pure default. Kept here, under this same name and signature, because
52
+ * every existing caller (this package's own `locatorStatus`, token-api's `resolveLocatorUrl`)
53
+ * still wants exactly this three-rung precedence with one call.
54
+ */
55
+ export function resolveGatewayBase(network, override) {
56
+ if (network === 'http')
57
+ return '';
58
+ if (override)
59
+ return sdkResolveGatewayBase(network, network === 'ipfs' ? { ipfs: override } : { arweave: override });
60
+ return sdkResolveGatewayBase(network, gatewayConfigFromEnv());
61
+ }
62
+ /**
63
+ * Ask one gateway whether it serves these bytes, reading **headers only**.
64
+ *
65
+ * A ranged request (`Range: bytes=0-0`) is what keeps this cheap: the content in question can be tens of
66
+ * megabytes and the question is only whether it is there. Gateways that ignore `Range` answer 200
67
+ * with the whole body, so the body is cancelled rather than read — without that, checking a large
68
+ * locator would download it.
69
+ */
70
+ export async function probeGateway(url, opts = {}) {
71
+ const doFetch = opts.fetchFn ?? fetch;
72
+ const gateway = safeHost(url);
73
+ const started = Date.now();
74
+ const ctrl = new AbortController();
75
+ const timer = setTimeout(() => ctrl.abort(), opts.timeoutMs ?? DEFAULT_TIMEOUT_MS);
76
+ try {
77
+ const res = await doFetch(url, { headers: { range: 'bytes=0-0' }, signal: ctrl.signal, redirect: 'follow' });
78
+ // Never read the body: a Range-ignoring gateway would otherwise stream the whole asset.
79
+ try {
80
+ await res.body?.cancel();
81
+ }
82
+ catch {
83
+ /* already consumed or unsupported — nothing to release */
84
+ }
85
+ return {
86
+ url,
87
+ gateway,
88
+ serving: res.status >= 200 && res.status < 300,
89
+ status: res.status,
90
+ contentType: res.headers.get('content-type'),
91
+ bytes: totalBytesFrom(res.headers.get('content-range'), res.headers.get('content-length'), res.status),
92
+ ms: Date.now() - started,
93
+ };
94
+ }
95
+ catch (err) {
96
+ const aborted = err.name === 'AbortError' || ctrl.signal.aborted;
97
+ return {
98
+ url,
99
+ gateway,
100
+ serving: false,
101
+ status: null,
102
+ contentType: null,
103
+ bytes: null,
104
+ ms: Date.now() - started,
105
+ error: aborted ? `no response in ${opts.timeoutMs ?? DEFAULT_TIMEOUT_MS}ms` : err.message,
106
+ };
107
+ }
108
+ finally {
109
+ clearTimeout(timer);
110
+ }
111
+ }
112
+ /** Accepted-vs-retrievable for one locator, across the gateway that matters and a couple of others. */
113
+ export async function locatorStatus(locator, opts = {}) {
114
+ const { network, id } = parseLocator(locator);
115
+ const primaryGateway = resolveGatewayBase(network, opts.gateway);
116
+ const primaryUrl = gatewayUrlFor(network, id, primaryGateway);
117
+ // `http` locators have no notion of an alternate — the URL IS the address, so a second host would
118
+ // be answering about different bytes.
119
+ const alternateBases = opts.primaryOnly || network === 'http' ? [] : ALTERNATE_GATEWAYS[network];
120
+ const alternateUrls = alternateBases
121
+ .map((base) => gatewayUrlFor(network, id, base))
122
+ .filter((u) => u !== primaryUrl);
123
+ const probe = (u) => probeGateway(u, { timeoutMs: opts.timeoutMs, fetchFn: opts.fetchFn });
124
+ const [primary, ...alternates] = await Promise.all([probe(primaryUrl), ...alternateUrls.map(probe)]);
125
+ const readiness = primary.serving
126
+ ? 'ready'
127
+ : alternates.some((a) => a.serving)
128
+ ? 'propagating'
129
+ : 'unreachable';
130
+ return { locator, network, id, readiness, primary, alternates };
131
+ }
132
+ const DEFAULT_AWAIT_TIMEOUT_MS = 5 * 60_000;
133
+ const DEFAULT_POLL_BASE_MS = 3000;
134
+ /**
135
+ * Poll {@link locatorStatus} until it reports `'ready'` or the deadline passes.
136
+ *
137
+ * `locatorStatus` alone answers "right now" — the natural next question, for a caller that would
138
+ * otherwise upload during a mint and write the locator into a token that renders broken for its
139
+ * first minutes of life (see the module doc), is "wait until it's actually there." This is that,
140
+ * factored out once so a caller doesn't hand-roll its own poll/backoff loop around a one-shot check.
141
+ *
142
+ * `'propagating'` is treated the same as `'unreachable'` here — both mean "not yet what THIS caller
143
+ * can rely on" — but every intermediate status still reaches `onEvent`, so a caller that wants to
144
+ * distinguish "provably on the network, just behind" from "no evidence yet" can from the events.
145
+ */
146
+ export async function awaitLocatorReady(locator, opts = {}) {
147
+ const deadlineMs = opts.timeoutMs ?? DEFAULT_AWAIT_TIMEOUT_MS;
148
+ const pollBaseMs = opts.pollBaseMs ?? DEFAULT_POLL_BASE_MS;
149
+ const started = Date.now();
150
+ const probeOpts = { gateway: opts.gateway, primaryOnly: opts.primaryOnly, timeoutMs: opts.probeTimeoutMs, fetchFn: opts.fetchFn };
151
+ let attempt = 0;
152
+ for (;;) {
153
+ attempt++;
154
+ const status = await locatorStatus(locator, probeOpts);
155
+ const elapsedMs = Date.now() - started;
156
+ opts.onEvent?.({ attempt, elapsedMs, status });
157
+ if (status.readiness === 'ready')
158
+ return { status, attempts: attempt, elapsedMs, ready: true };
159
+ const delay = linearBackoffDelay(attempt, pollBaseMs);
160
+ if (elapsedMs + delay >= deadlineMs)
161
+ return { status, attempts: attempt, elapsedMs, ready: false };
162
+ await sleep(delay);
163
+ }
164
+ }
165
+ /** Total size from a ranged response's `content-range` (`bytes 0-0/12345`), else `content-length`
166
+ * when the gateway ignored the range and answered 200 with the whole thing. */
167
+ function totalBytesFrom(contentRange, contentLength, status) {
168
+ const m = contentRange?.match(/\/\s*(\d+)\s*$/);
169
+ if (m)
170
+ return Number(m[1]);
171
+ if (status === 200 && contentLength && /^\d+$/.test(contentLength))
172
+ return Number(contentLength);
173
+ return null;
174
+ }
175
+ function safeHost(url) {
176
+ try {
177
+ return new URL(url).host;
178
+ }
179
+ catch {
180
+ return url;
181
+ }
182
+ }
183
+ //# 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,qGAAqG;AACrG,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,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,EAAC,CAAC;IAC7F,IAAI,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC;QAAE,OAAO,EAAC,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,EAAC,CAAC;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