@le-space/orbitdb-storage-bridge 0.27.1 → 0.28.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/README.md CHANGED
@@ -56,7 +56,7 @@ entry is the Storacha-era class and the helpers it grew.
56
56
  | `/restore-cid` | `restoreFromCID`, `restoreFromBlocks`: a database back from a CID, or from blocks verified one by one |
57
57
  | `/extract-blocks` | `extractDatabaseBlocks`, `bundleDatabases`: one database's blocks, or several databases' in one bundle |
58
58
  | `/app-backup` | one sealed file for an application: `buildAppBackup`, `openAppBackup`, `restoreAppBackup`, `readAppBackupHeader`, `isAppBackup` |
59
- | `/live-backup` | increments: `createLiveBackup`, `collectIncrement`, `readIncrement`, `mergeIncrements`, `restoreFromIncrements`, `joinIncrements` |
59
+ | `/live-backup` | increments: `createLiveBackup`, `collectIncrement`, `readIncrement`, `mergeIncrements`, `restoreFromIncrements`, `joinIncrements`, and `advanceConfirmation` / `reconcileConfirmation` for a store that refuses later |
60
60
  | `/dehydrate` | `dehydrate`, `hydrate`: a database where a second device finds it from a secret alone |
61
61
  | `/pointer-ipns` | `publishPointer`, `resolvePointer`: the IPNS pointer `dehydrate` publishes, under a name derived from a seed |
62
62
  | `/gateway-fetch` | `fetchFromGateways`: bytes by CID over HTTP, verified against the CID |
@@ -66,7 +66,7 @@ entry is the Storacha-era class and the helpers it grew.
66
66
  | `/backends/types` | the contract: `defineBackend`, `BackendError`, the capability flags |
67
67
  | `/backends/memory` | the in-process driver |
68
68
  | `/backends/aleph` | `createAlephBackend`: ingest to Aleph – its IPFS host, else an API node – with no key, or with `aleph-pin`'s pin to keep in the same call; `onProgress` for the upload |
69
- | `/backends/aleph-pin` | `createAlephPin`, `createAlephAuthorizer`, `listAlephStores`, `waitForMessage`, `createAlephForget`, `forgetAlephStores`: what makes Aleph keep a file, and lets it go |
69
+ | `/backends/aleph-pin` | `createAlephPin`, `createAlephAuthorizer`, `listAlephStores`, `waitForMessage`, `messageStatus`, `alephCredits`, `createAlephForget`, `forgetAlephStores`: what makes Aleph keep a file, and lets it go |
70
70
  | `/backends/aleph-upload` | `unixfsCid`: the CID Aleph gives a file, before the upload; `postFile`, the limits and the upload errors |
71
71
  | `/backends/pinata` | Pinata: pin by CID or upload, with a scoped JWT; `remove()` by file id |
72
72
  | `/backends/lighthouse` | Lighthouse: upload with an API key; `list()` and `remove()` |
@@ -130,6 +130,7 @@ Galaxy A57 with the same key brought the database back and wrote to it. Source:
130
130
  | [docs/APP-BACKUP.md](docs/APP-BACKUP.md) | an application's backup in one sealed file: several databases, a keyring header, kept on Aleph from the browser, restored by merging |
131
131
  | [docs/LIVE-BACKUP.md](docs/LIVE-BACKUP.md) | increments between two full backups: what was written since the last one, shipped as one small CAR, with nobody clicking and no peer online |
132
132
  | [SVELTE-COMPONENTS.md](SVELTE-COMPONENTS.md) | the browser components — older than this repository's copy of them; [docs/STORACHA-UI-HISTORY.md](docs/STORACHA-UI-HISTORY.md) records what each piece could do and where it went |
133
+ | [docs/POST-QUANTUM.md](docs/POST-QUANTUM.md) | what a quantum computer would and would not break: the encryption holds, the signatures do not, and the restore is gated on the AEAD rather than on a signature |
133
134
  | [docs/LOGGING.md](docs/LOGGING.md) | debug namespaces, in Node and in a browser |
134
135
  | [ROADMAP.md](ROADMAP.md) | what is planned |
135
136
  | [docs/STORACHA-SUNSET.md](docs/STORACHA-SUNSET.md) | why Storacha stopped being the default, with dates and evidence |
@@ -104,6 +104,10 @@ async function sha256Hex(payload) {
104
104
  * @param {"credit" | "hold" | null} [args.payment] - `credit` by default; `null`
105
105
  * sends no `payment`, as every version up to 0.16.1 did, which Aleph books as
106
106
  * `hold`
107
+ * @param {number} [args.size] - the file's size in bytes, as Aleph's STORE
108
+ * content carries it (`content.size`). Nothing checks it against the file;
109
+ * it lets whoever lists the STOREs know how much a download is before it
110
+ * starts, and refuse one that came back short
107
111
  * @param {number} [args.now] - seconds; injected so a test is not a clock
108
112
  * @param {(payload: string) => Promise<string>} [args.hasher]
109
113
  */
@@ -113,6 +117,7 @@ export async function buildStoreMessage({
113
117
  cid,
114
118
  channel = DEFAULT_ALEPH_CHANNEL,
115
119
  ref,
120
+ size,
116
121
  payment = "credit",
117
122
  now,
118
123
  hasher = sha256Hex,
@@ -123,6 +128,12 @@ export async function buildStoreMessage({
123
128
  "a STORE's ref is a non-empty string, or absent",
124
129
  );
125
130
  }
131
+ if (size != null && !(Number.isInteger(size) && size >= 0)) {
132
+ throw new BackendError(
133
+ "INVALID_BACKEND",
134
+ "a STORE's size is a whole number of bytes, or absent",
135
+ );
136
+ }
126
137
  if (payment !== null && !STORE_PAYMENTS.includes(payment)) {
127
138
  throw new BackendError(
128
139
  "INVALID_BACKEND",
@@ -138,6 +149,7 @@ export async function buildStoreMessage({
138
149
  item_type: "ipfs",
139
150
  item_hash: cid,
140
151
  ...(ref ? { ref } : {}),
152
+ ...(size != null ? { size } : {}),
141
153
  // How that account pays. Without it Aleph books the STORE as `hold`, and an
142
154
  // account funded with credits covers nothing (see the file's header).
143
155
  ...(payment ? { payment: { type: payment } } : {}),
@@ -211,7 +223,7 @@ export function createAlephPin(options = {}) {
211
223
  * the node keeps the file in the same call.
212
224
  *
213
225
  * @param {string} cid
214
- * @param {{ ref?: string }} [meta]
226
+ * @param {{ ref?: string, size?: number }} [meta]
215
227
  */
216
228
  async function signed(cid, meta = {}) {
217
229
  const unsigned = await buildStoreMessage({
@@ -220,6 +232,7 @@ export function createAlephPin(options = {}) {
220
232
  cid,
221
233
  channel,
222
234
  ref: meta?.ref ?? options.ref,
235
+ ...(meta?.size != null ? { size: meta.size } : {}),
223
236
  payment,
224
237
  hasher,
225
238
  now: now?.(),
@@ -337,6 +350,67 @@ export async function waitForMessage(itemHash, options = {}) {
337
350
  return { itemHash, status, timedOut: true };
338
351
  }
339
352
 
353
+ /**
354
+ * What Aleph says about a message now, asked once: `processed`, `pending`,
355
+ * `rejected`, `removing`, `removed`, `forgotten` – or `unknown` when it does
356
+ * not know the message at all (a request that never arrived, or was dropped
357
+ * before it was checked). `waitForMessage` follows a pending one; this is for
358
+ * coming back later, e.g. to an increment that was still pending when its
359
+ * flush ended (`createLiveBackup().reconcile`).
360
+ *
361
+ * @param {string} itemHash
362
+ * @param {object} [options]
363
+ * @param {string} [options.apiHost]
364
+ * @param {typeof fetch} [options.fetch]
365
+ * @returns {Promise<{ itemHash: string, status: string, errorCode?: number, details?: unknown }>}
366
+ * @throws when Aleph does not answer (anything but 200 or 404)
367
+ */
368
+ export async function messageStatus(itemHash, options = {}) {
369
+ const apiHost = options.apiHost || DEFAULT_ALEPH_API_HOST;
370
+ const doFetch = options.fetch || globalThis.fetch;
371
+ if (!itemHash)
372
+ throw new BackendError(
373
+ "INVALID_BACKEND",
374
+ "messageStatus needs the message's item hash",
375
+ );
376
+ const response = await doFetch(
377
+ `${apiHost}/api/v0/messages/${encodeURIComponent(itemHash)}`,
378
+ );
379
+ if (response.status === 404) return { itemHash, status: "unknown" };
380
+ if (!response.ok)
381
+ throw new BackendError(
382
+ "UNSUPPORTED",
383
+ `Aleph did not answer: ${response.status}`,
384
+ );
385
+ const body = await response.json().catch(() => ({}));
386
+ return {
387
+ itemHash,
388
+ status: typeof body?.status === "string" ? body.status : "unknown",
389
+ ...(body?.error_code != null ? { errorCode: body.error_code } : {}),
390
+ ...(body?.details != null ? { details: body.details } : {}),
391
+ };
392
+ }
393
+
394
+ /**
395
+ * An account's credits, or null when Aleph does not say.
396
+ *
397
+ * @param {string} address - EIP-55 checksummed (Aleph keys accounts by that form)
398
+ * @param {object} [options]
399
+ * @param {string} [options.apiHost]
400
+ * @param {typeof fetch} [options.fetch]
401
+ * @returns {Promise<number|null>}
402
+ */
403
+ export async function alephCredits(address, options = {}) {
404
+ const apiHost = options.apiHost || DEFAULT_ALEPH_API_HOST;
405
+ const doFetch = options.fetch || globalThis.fetch;
406
+ const response = await doFetch(
407
+ `${apiHost}/api/v0/addresses/${address}/balance`,
408
+ ).catch(() => null);
409
+ if (!response?.ok) return null;
410
+ const body = await response.json().catch(() => ({}));
411
+ return Number.isFinite(body?.credit_balance) ? body.credit_balance : null;
412
+ }
413
+
340
414
  /**
341
415
  * The STORE messages kept for an account, newest first: its own, and those a
342
416
  * delegate sent for it.
@@ -355,7 +429,7 @@ export async function waitForMessage(itemHash, options = {}) {
355
429
  * @param {typeof fetch} [options.fetch]
356
430
  * @param {number} [options.pagination] - per page; default 50
357
431
  * @param {number} [options.page] - from 1
358
- * @returns {Promise<{ stores: Array<{ cid: string, itemHash: string, sender: string, owner: string, time: number, channel?: string, payment?: string }>, total: number | null }>}
432
+ * @returns {Promise<{ stores: Array<{ cid: string, itemHash: string, sender: string, owner: string, time: number, channel?: string, ref?: string, size?: number, payment?: string }>, total: number | null }>}
359
433
  * `cid` is what the STORE keeps — for an upload through `createAlephBackend`,
360
434
  * the handle's `id`
361
435
  */
@@ -405,6 +479,11 @@ export async function listAlephStores(options = {}) {
405
479
  ...(typeof m?.content?.ref === "string" && m.content.ref
406
480
  ? { ref: m.content.ref }
407
481
  : {}),
482
+ // The size the STORE names (buildStoreMessage `size`): a download knows
483
+ // what to expect, and what to refuse. Absent on older STOREs.
484
+ ...(Number.isInteger(m?.content?.size) && m.content.size >= 0
485
+ ? { size: m.content.size }
486
+ : {}),
408
487
  ...(m?.content?.payment?.type ? { payment: m.content.payment.type } : {}),
409
488
  }))
410
489
  // Aleph answers what it was asked; keeping only this owner's costs nothing
@@ -43,6 +43,7 @@
43
43
  /* global FormData, XMLHttpRequest */
44
44
 
45
45
  import { importer } from "ipfs-unixfs-importer";
46
+ import { CID } from "multiformats/cid";
46
47
 
47
48
  /**
48
49
  * What an API node takes without a signed STORE message: 25 MiB – of the
@@ -95,6 +96,39 @@ export async function unixfsCid(bytes) {
95
96
  return last.cid.toString();
96
97
  }
97
98
 
99
+ /**
100
+ * Whether `bytes` are the file a UnixFS CID names – one made with `ipfs add`'s
101
+ * defaults, as Aleph's hosts and `unixfsCid` make it. A gateway assembles the
102
+ * file from the DAG, so the bytes themselves hash to nothing the CID says;
103
+ * building the DAG again from them does. Passes a CID this check cannot
104
+ * prove (raw, dag-cbor, another hash, an unparsable string): the raw case is
105
+ * gateway-fetch's own check.
106
+ *
107
+ * What it catches is a download that came back short or damaged. A sealed
108
+ * file then fails to open, and nothing tells that apart from a wrong key
109
+ * (Le-Space/belege, a phone's restore).
110
+ *
111
+ * @param {string} cid
112
+ * @param {Uint8Array} bytes
113
+ * @returns {Promise<boolean>}
114
+ */
115
+ export async function matchesUnixfsCid(cid, bytes) {
116
+ let parsed;
117
+ try {
118
+ parsed = CID.parse(String(cid));
119
+ } catch {
120
+ return true;
121
+ }
122
+ // dag-pb, sha-256: what `ipfs add` makes by default.
123
+ if (parsed.code !== 0x70 || parsed.multihash.code !== 0x12) return true;
124
+ const made = CID.parse(await unixfsCid(bytes)).multihash.digest;
125
+ const expected = parsed.multihash.digest;
126
+ if (made.length !== expected.length) return false;
127
+ for (let i = 0; i < made.length; i++)
128
+ if (made[i] !== expected[i]) return false;
129
+ return true;
130
+ }
131
+
98
132
  /** Thrown when a host answers that the endpoint is not there (404, 405). */
99
133
  export class EndpointGoneError extends Error {
100
134
  constructor(url, status) {
@@ -38,6 +38,27 @@
38
38
  * being ingest-only. `./aleph-pin.js` is that implementation: `createAlephPin`
39
39
  * builds and signs the STORE, and is what a caller passes as `pin`.
40
40
  *
41
+ * ## Upload and STORE in one call, when the pin can sign
42
+ *
43
+ * Ingest first and a STORE afterwards is two steps with a gap between them:
44
+ * the STORE names a CID that Aleph's nodes (CCNs) must then fetch from the
45
+ * IPFS host, which holds the upload unpinned. Since about 2026-11-04 that
46
+ * fetch fails — Belege's backups were rejected with `error_code` 4, "File not
47
+ * found: <cid> (could not retrieve IPFS content at this time)", and the host
48
+ * dropped the file later (gateway 504). Measured again on 2026-11-11 with a
49
+ * throwaway account: `/api/v0/add` on the IPFS host answers 200 again, the
50
+ * file is on the gateway, the API node's `/storage/raw/<cid>` does not have
51
+ * it, and the separate STORE stays `pending` for many minutes.
52
+ *
53
+ * So a `pin` with `sign` (`createAlephPin`) now sends every upload to the API
54
+ * node's `add_file` with the signed STORE beside the file: the node pins it
55
+ * itself and answers with the message's status, nothing has to be fetched
56
+ * from anywhere. The ingest host is used only without a signing pin, where
57
+ * nothing would keep the file anyway. `pinCid` (a STORE for a CID already
58
+ * elsewhere) still exists and still depends on the CCNs finding the file;
59
+ * follow its item hash with `waitForMessage` and treat `error_code` 4 as
60
+ * "upload it again, signed".
61
+ *
41
62
  * @author @NiKrause
42
63
  * @requires ./types.js - the backend contract
43
64
  */
@@ -53,6 +74,7 @@ import {
53
74
  hashFromAddAnswer,
54
75
  postFile,
55
76
  unixfsCid,
77
+ matchesUnixfsCid,
56
78
  uploadTimeout,
57
79
  } from "./aleph-upload.js";
58
80
 
@@ -70,10 +92,11 @@ export const ALEPH_GATEWAYS = Object.freeze(["https://ipfs.aleph.cloud/ipfs"]);
70
92
 
71
93
  const ALEPH_INGEST = "https://ipfs.aleph.cloud/api/v0/add";
72
94
  /**
73
- * Where an upload goes when the IPFS host takes none: an API node's
74
- * `/api/v0/ipfs/add_file`. Since 2026-10-26 every `/api/v0/` call on Aleph's
75
- * IPFS hosts answers 404 (they serve the gateway only); the API nodes still
76
- * take a file, with the limits `./aleph-upload.js` names.
95
+ * Where an upload goes when the IPFS host takes none, and always when the pin
96
+ * can sign: an API node's `/api/v0/ipfs/add_file`. From 2026-10-26 every
97
+ * `/api/v0/` call on Aleph's IPFS hosts answered 404 (on 2026-11-11 `add`
98
+ * answered 200 again); the API nodes take a file, with the limits
99
+ * `./aleph-upload.js` names.
77
100
  */
78
101
  export const DEFAULT_ALEPH_API_HOST = "https://api2.aleph.im";
79
102
  const DEFAULT_TIMEOUT_MS = 120_000;
@@ -100,8 +123,12 @@ const DEFAULT_TIMEOUT_MS = 120_000;
100
123
  * @returns {import("./types.js").StorageBackend}
101
124
  */
102
125
  export function createAlephBackend(options = {}) {
103
- const ingestUrl = options.ingestUrl === null ? null : options.ingestUrl || ALEPH_INGEST;
104
- const apiHost = (options.apiHost || DEFAULT_ALEPH_API_HOST).replace(/\/+$/, "");
126
+ const ingestUrl =
127
+ options.ingestUrl === null ? null : options.ingestUrl || ALEPH_INGEST;
128
+ const apiHost = (options.apiHost || DEFAULT_ALEPH_API_HOST).replace(
129
+ /\/+$/,
130
+ "",
131
+ );
105
132
  const gateways = options.gateways || ALEPH_GATEWAYS;
106
133
  const timeout = options.timeout ?? DEFAULT_TIMEOUT_MS;
107
134
  const pin = options.pin;
@@ -188,17 +215,23 @@ export function createAlephBackend(options = {}) {
188
215
  async putBlob(bytes, meta = {}) {
189
216
  const name = meta.name || "blob";
190
217
  const contentType = meta.contentType || "application/octet-stream";
191
- const onProgress = typeof meta.onProgress === "function" ? meta.onProgress : undefined;
218
+ const onProgress =
219
+ typeof meta.onProgress === "function" ? meta.onProgress : undefined;
192
220
  const handle = {
193
221
  backend: "aleph",
194
222
  size: bytes.length,
195
223
  ...(meta.name ? { name: meta.name } : {}),
196
224
  };
197
225
 
198
- // The IPFS host first, while it answers at all: an upload there is
199
- // free of any limit below 1 GiB and needs no message. A 404 or 405 says
200
- // it takes none, and is remembered for this backend's lifetime.
201
- if (ingestUrl && !ingestGone) {
226
+ const signed = typeof pin?.sign === "function";
227
+
228
+ // The IPFS host first, while it answers at all and no STORE can go
229
+ // beside the file: an upload there is free of any limit below 1 GiB and
230
+ // needs no message. A 404 or 405 says it takes none, and is remembered
231
+ // for this backend's lifetime. With a pin that signs it is skipped: a
232
+ // STORE sent after it, separately, has to fetch the file back from that
233
+ // host, and since 2026-11-04 that fails (see the header).
234
+ if (ingestUrl && !ingestGone && !signed) {
202
235
  try {
203
236
  const id = await viaIngest({ bytes, name, contentType, onProgress });
204
237
  // Said out loud in the handle, because "stored" and "kept" are not
@@ -215,10 +248,10 @@ export function createAlephBackend(options = {}) {
215
248
  // The API node. With a signed STORE message beside the file the node
216
249
  // keeps it in the same call; without one it takes at most 25 MiB and
217
250
  // keeps it for a day, until a STORE names it.
218
- const signed = typeof pin?.sign === "function";
219
251
  // The node measures the request body, the form around the file included.
220
252
  const limit =
221
- (signed ? ALEPH_API_SIGNED_LIMIT : ALEPH_API_UNSIGNED_LIMIT) - ALEPH_API_FORM_HEADROOM;
253
+ (signed ? ALEPH_API_SIGNED_LIMIT : ALEPH_API_UNSIGNED_LIMIT) -
254
+ ALEPH_API_FORM_HEADROOM;
222
255
  if (bytes.length > limit) {
223
256
  const error = new BackendError(
224
257
  "UNSUPPORTED",
@@ -239,7 +272,7 @@ export function createAlephBackend(options = {}) {
239
272
  // message names: so the CID is computed here first, the way the node
240
273
  // computes it (aleph-upload.js `unixfsCid`).
241
274
  const cid = await unixfsCid(bytes);
242
- const message = await pin.sign(cid, meta);
275
+ const message = await pin.sign(cid, { ...meta, size: bytes.length });
243
276
  itemHash = message.item_hash;
244
277
  fields.metadata = JSON.stringify({ message, sync: true });
245
278
  }
@@ -263,14 +296,21 @@ export function createAlephBackend(options = {}) {
263
296
  try {
264
297
  body = JSON.parse(text);
265
298
  } catch {
266
- throw new BackendError("UNSUPPORTED", "Aleph's API answered something that is not JSON");
299
+ throw new BackendError(
300
+ "UNSUPPORTED",
301
+ "Aleph's API answered something that is not JSON",
302
+ );
267
303
  }
268
304
  if (typeof body?.hash !== "string" || !body.hash) {
269
305
  throw new BackendError("UNSUPPORTED", "Aleph's API returned no hash");
270
306
  }
271
307
  // A signed upload answers with the message's status (200 processed,
272
308
  // 202 pending); a plain one is stored, not kept, as on the IPFS host.
273
- const messageStatus = signed ? (status === 200 ? "processed" : "pending") : undefined;
309
+ const messageStatus = signed
310
+ ? status === 200
311
+ ? "processed"
312
+ : "pending"
313
+ : undefined;
274
314
  return {
275
315
  ...handle,
276
316
  id: body.hash,
@@ -282,14 +322,31 @@ export function createAlephBackend(options = {}) {
282
322
 
283
323
  async getBlob(handle, options = {}) {
284
324
  const id = handleId(handle);
325
+ // What came back is checked against the CID (a UnixFS one is built
326
+ // again from the bytes) and, when known, against the size the STORE
327
+ // names: a short or damaged download is refused at the gateway, not
328
+ // found out when a sealed file fails to open.
329
+ const size = Number.isInteger(options?.size)
330
+ ? options.size
331
+ : Number.isInteger(handle?.size)
332
+ ? handle.size
333
+ : null;
285
334
  try {
286
335
  return await fetchFromGateways(id, {
287
336
  gateways,
288
337
  timeout,
289
- ...(typeof options?.onProgress === "function" ? { onProgress: options.onProgress } : {}),
338
+ verify: matchesUnixfsCid,
339
+ fetchImpl: doFetch,
340
+ ...(size != null && size > 0 ? { expectedSize: size } : {}),
341
+ ...(typeof options?.onProgress === "function"
342
+ ? { onProgress: options.onProgress }
343
+ : {}),
290
344
  });
291
345
  } catch (error) {
292
- throw new BackendError("NOT_FOUND", `No blob for ${id}: ${error.message}`);
346
+ throw new BackendError(
347
+ "NOT_FOUND",
348
+ `No blob for ${id}: ${error.message}`,
349
+ );
293
350
  }
294
351
  },
295
352
  };
@@ -203,6 +203,12 @@ async function readBody(
203
203
  * @param {(ms: number) => Promise<void>} [options.sleep] injectable for tests
204
204
  * @param {typeof fetch} [options.fetchImpl] injectable for tests
205
205
  * @param {(received: number, total: number) => void} [options.onProgress]
206
+ * @param {number | null} [options.expectedSize] the file's length in bytes
207
+ * when the caller knows it (an Aleph STORE's `content.size`, say): bytes of
208
+ * another length are refused and the next gateway asked
209
+ * @param {((cid: string, bytes: Uint8Array) => Promise<boolean>) | null} [options.verify]
210
+ * a check of the caller's beyond the raw-CID one – the Aleph driver passes
211
+ * `matchesUnixfsCid`. `false` refuses the bytes and asks the next gateway
206
212
  * called as the bytes arrive, with how many so far and how many the gateway
207
213
  * announced (`Content-Length`; 0 when it did not). A backup of several
208
214
  * hundred MB takes minutes on a phone, and without this a page can only
@@ -222,6 +228,8 @@ export async function fetchFromGateways(
222
228
  sleep = waitFor,
223
229
  fetchImpl = fetch,
224
230
  onProgress = null,
231
+ expectedSize = null,
232
+ verify = null,
225
233
  } = {},
226
234
  ) {
227
235
  let lastError = null;
@@ -295,8 +303,27 @@ export async function fetchFromGateways(
295
303
  // DAG the gateway assembled, and the chunking that made it is the
296
304
  // service's; those bytes are trusted as far as the envelope around
297
305
  // them goes, which is why a sealed backup refuses plaintext.
306
+ // A download that came back short, or with something else in it,
307
+ // must not pass as the file: sealed, it fails to open later, and
308
+ // nothing then tells it apart from a wrong key. The length first:
309
+ // it costs nothing, and its message says what happened.
310
+ if (expectedSize != null && bytes.length !== expectedSize) {
311
+ lastError = new Error(
312
+ `${gateway} served ${bytes.length} bytes of ${cid}, not the ${expectedSize} it has`,
313
+ );
314
+ log.warn(` ⚠️ ${lastError.message}`);
315
+ break;
316
+ }
298
317
  if (!(await matchesRawCid(cid, bytes))) {
299
- log.warn(` ⚠️ ${gateway} served bytes that do not hash to ${cid}`);
318
+ lastError = new Error(
319
+ `${gateway} served bytes that do not hash to ${cid}`,
320
+ );
321
+ log.warn(` ⚠️ ${lastError.message}`);
322
+ break;
323
+ }
324
+ if (verify && !(await verify(cid, bytes))) {
325
+ lastError = new Error(`${gateway} served bytes that are not ${cid}`);
326
+ log.warn(` ⚠️ ${lastError.message}`);
300
327
  break;
301
328
  }
302
329
  log.info(` ✅ ${bytes.length} bytes from ${gateway}`);
@@ -46,6 +46,19 @@
46
46
  * says how many updates are waiting, when the last increment went out and
47
47
  * what the last failure was, so an application can show "last backed up …".
48
48
  *
49
+ * ## A store that answers "pending" and refuses later
50
+ *
51
+ * Aleph takes a STORE with "pending" and may reject it minutes later – a file
52
+ * its nodes could not fetch, credit that ran out. An increment shipped under
53
+ * such an answer has moved the chain on, and nothing would ship its content
54
+ * again. So the chain also remembers how far the store has *confirmed* it
55
+ * (`kept`) and the increments shipped since (`unconfirmed`); `reconcile()`
56
+ * asks about those again and, when one turns out refused or unknown, goes
57
+ * back to `kept`: the next flush ships everything since, under the next
58
+ * number after the last one kept, so a restore finds no gap. The bookkeeping
59
+ * is `advanceConfirmation` / `reconcileConfirmation`, exported for an
60
+ * application that keeps its own chain state.
61
+ *
49
62
  * @requires ./backup-car.js - createCARFromBlocks
50
63
  * @requires ./restore-cid.js - readBlocksFromCAR, restoreFromBlocks
51
64
  */
@@ -56,7 +69,12 @@ import { CID } from "multiformats/cid";
56
69
  import { sha256 } from "multiformats/hashes/sha2";
57
70
  import { base58btc } from "multiformats/bases/base58";
58
71
  import { createCARFromBlocks } from "./backup-car.js";
59
- import { readBlocksFromCAR, restoreFromBlocks, entryDatabaseId, standalone } from "./restore-cid.js";
72
+ import {
73
+ readBlocksFromCAR,
74
+ restoreFromBlocks,
75
+ entryDatabaseId,
76
+ standalone,
77
+ } from "./restore-cid.js";
60
78
 
61
79
  /** What the manifest at an increment's root calls itself. */
62
80
  export const INCREMENT_KIND = "increment";
@@ -76,6 +94,10 @@ const DAG_CBOR = dagCbor.code;
76
94
  * @property {string|null} base - the full backup the increments build on, as the caller named it
77
95
  * @property {string|null} previous - the backend's id of the last increment
78
96
  * @property {Object<string, { heads: string[] }>} databases - by address: the heads last shipped
97
+ * @property {{ sequence: number, previous: string|null, databases: object }|null} [kept] - the last
98
+ * point the store confirmed; null before any
99
+ * @property {Array<{ sequence: number, previous: string|null, databases: object, cid: string }>} [unconfirmed] -
100
+ * increments shipped since that the store has not confirmed, oldest first
79
101
  */
80
102
 
81
103
  /**
@@ -517,7 +539,10 @@ export async function joinIncrements(
517
539
  // OrbitDB names its blocks in base58btc; a CAR in base32. The log reads
518
540
  // its own names, and identities are looked up by theirs, so each block
519
541
  // goes in under the name its reader will ask for.
520
- await database.log.storage.put(cid.toV1().toString(base58btc), standalone(bytes));
542
+ await database.log.storage.put(
543
+ cid.toV1().toString(base58btc),
544
+ standalone(bytes),
545
+ );
521
546
  }
522
547
 
523
548
  let joined = 0;
@@ -538,6 +563,95 @@ export async function joinIncrements(
538
563
  };
539
564
  }
540
565
 
566
+ /** Statuses that mean the store does not keep a blob. */
567
+ export const NOT_KEPT = Object.freeze(["rejected", "unknown", "forgotten"]);
568
+
569
+ /**
570
+ * How far a store has confirmed a chain, and what was shipped since.
571
+ *
572
+ * A *point* is whatever the caller needs to go back to – `sequence`,
573
+ * `previous` and its own fields (the heads shipped by then, a count of
574
+ * files, …). It is kept as given, never read here beyond `sequence`.
575
+ *
576
+ * @typedef {object} Confirmation
577
+ * @property {object|null} kept - the last point the store confirmed
578
+ * @property {Array<object & { cid: string }>} pending - points shipped since, oldest first
579
+ */
580
+
581
+ /**
582
+ * The confirmation after an increment was shipped.
583
+ *
584
+ * `processed` with nothing open before it moves `kept` to this point; any
585
+ * other answer puts the point aside as pending, with the blob's id to ask
586
+ * about later.
587
+ *
588
+ * @param {Confirmation|null|undefined} confirmation
589
+ * @param {object} point - where the chain stands with this increment
590
+ * @param {string} cid - the increment's id in the store
591
+ * @param {string} [status="processed"] - the store's answer
592
+ * @returns {Confirmation}
593
+ */
594
+ export function advanceConfirmation(
595
+ confirmation,
596
+ point,
597
+ cid,
598
+ status = "processed",
599
+ ) {
600
+ const pending = confirmation?.pending ?? [];
601
+ if (status === "processed" && pending.length === 0) {
602
+ return { kept: point, pending: [] };
603
+ }
604
+ return {
605
+ kept: confirmation?.kept ?? null,
606
+ pending: [...pending, { ...point, cid }],
607
+ };
608
+ }
609
+
610
+ /**
611
+ * The confirmation by what the store says now.
612
+ *
613
+ * Walks the pending points oldest first: a `processed` one moves `kept` on; a
614
+ * refused, unknown or forgotten one ends it – everything pending is dropped
615
+ * and `rewind` is the point to go back to (`null`: before anything was
616
+ * confirmed, the caller's own start). A point the store has not decided on
617
+ * stops the walk. `gap` says an increment *after* a refused one is kept: a
618
+ * restore cannot cross that, and only a new full backup mends it.
619
+ *
620
+ * @param {Confirmation} confirmation
621
+ * @param {(cid: string) => string|undefined} statusOf - the store's word on a blob, if known
622
+ * @returns {{ kept: object|null, pending: object[], changed: boolean, rewound: boolean, rewind: object|null, gap: boolean }}
623
+ */
624
+ export function reconcileConfirmation(confirmation, statusOf) {
625
+ let kept = confirmation?.kept ?? null;
626
+ const pending = [...(confirmation?.pending ?? [])];
627
+ let changed = false;
628
+ while (pending.length) {
629
+ const status = statusOf(pending[0].cid);
630
+ if (status === "processed") {
631
+ const point = { ...pending.shift() };
632
+ delete point.cid;
633
+ kept = point;
634
+ changed = true;
635
+ continue;
636
+ }
637
+ if (status && NOT_KEPT.includes(status)) {
638
+ const gap = pending
639
+ .slice(1)
640
+ .some((point) => statusOf(point.cid) === "processed");
641
+ return {
642
+ kept,
643
+ pending: [],
644
+ changed: true,
645
+ rewound: true,
646
+ rewind: kept,
647
+ gap,
648
+ };
649
+ }
650
+ break;
651
+ }
652
+ return { kept, pending, changed, rewound: false, rewind: null, gap: false };
653
+ }
654
+
541
655
  /** A state with nothing shipped yet. */
542
656
  export const emptyLiveBackupState = () => ({
543
657
  v: LIVE_BACKUP_STATE_VERSION,
@@ -545,6 +659,8 @@ export const emptyLiveBackupState = () => ({
545
659
  base: null,
546
660
  previous: null,
547
661
  databases: {},
662
+ kept: null,
663
+ unconfirmed: [],
548
664
  });
549
665
 
550
666
  /**
@@ -576,7 +692,7 @@ export const emptyLiveBackupState = () => ({
576
692
  * flush and the state stays, so the next one ships it all again.
577
693
  * @param {() => Date} [params.now]
578
694
  * @returns {{ watch: Function, unwatch: Function, start: Function, stop: Function, close: Function,
579
- * flush: () => Promise<object|null>, rebase: Function, status: () => object }}
695
+ * flush: () => Promise<object|null>, rebase: Function, reconcile: Function, status: () => object }}
580
696
  */
581
697
  export function createLiveBackup({
582
698
  databases,
@@ -754,6 +870,20 @@ export function createLiveBackup({
754
870
  w.shipped.heads = heads;
755
871
  state.databases[address] = { heads };
756
872
  }
873
+ // How far the store has confirmed the chain: a pending answer is put
874
+ // aside, so `reconcile()` can go back if it is refused later.
875
+ const confirmation = advanceConfirmation(
876
+ { kept: state.kept, pending: state.unconfirmed },
877
+ {
878
+ sequence: state.sequence,
879
+ previous: state.previous,
880
+ databases: JSON.parse(JSON.stringify(state.databases)),
881
+ },
882
+ state.previous,
883
+ kept?.status ?? "processed",
884
+ );
885
+ state.kept = confirmation.kept;
886
+ state.unconfirmed = confirmation.pending;
757
887
  pending = Math.max(0, pending - countedPending);
758
888
  await save();
759
889
 
@@ -819,10 +949,77 @@ export function createLiveBackup({
819
949
  w.shipped.heads = heads;
820
950
  state.databases[address] = { heads };
821
951
  }
952
+ // A full backup is where the chain is kept from.
953
+ if (base !== undefined) {
954
+ state.kept = {
955
+ sequence: 0,
956
+ previous: null,
957
+ databases: JSON.parse(JSON.stringify(state.databases)),
958
+ };
959
+ state.unconfirmed = [];
960
+ }
822
961
  pending = 0;
823
962
  await save();
824
963
  };
825
964
 
965
+ /**
966
+ * Ask the store again about the increments it had not confirmed, and go
967
+ * back to the last confirmed point when one was refused.
968
+ *
969
+ * @param {(cid: string, point: object) => Promise<string|undefined>|string|undefined} statusOf -
970
+ * the store's word on an increment by its id: `processed`, `pending`,
971
+ * `rejected`, `unknown`, `forgotten` (for Aleph: `messageStatus` on the
972
+ * STORE's item hash, which the caller kept from `onShipped`)
973
+ * @returns {Promise<{ changed: boolean, rewound: boolean, gap: boolean, sequence: number }>}
974
+ * `gap`: an increment after a refused one is kept – make a full backup and `rebase({ base })`
975
+ */
976
+ const reconcile = async (statusOf) => {
977
+ if (inflight) await inflight.catch(() => {});
978
+ await load();
979
+ const answers = new Map();
980
+ for (const point of state.unconfirmed ?? []) {
981
+ answers.set(point.cid, await statusOf(point.cid, point));
982
+ }
983
+ const now_ = reconcileConfirmation(
984
+ { kept: state.kept ?? null, pending: state.unconfirmed ?? [] },
985
+ (cid) => answers.get(cid),
986
+ );
987
+ if (!now_.changed)
988
+ return {
989
+ changed: false,
990
+ rewound: false,
991
+ gap: false,
992
+ sequence: state.sequence,
993
+ };
994
+ state.kept = now_.kept;
995
+ state.unconfirmed = now_.pending;
996
+ if (now_.rewound) {
997
+ const back = now_.rewind ?? {
998
+ sequence: 0,
999
+ previous: null,
1000
+ databases: {},
1001
+ };
1002
+ state.sequence = back.sequence;
1003
+ state.previous = back.previous;
1004
+ state.databases = JSON.parse(JSON.stringify(back.databases ?? {}));
1005
+ for (const [address, w] of watched) {
1006
+ const heads = state.databases[address]?.heads ?? [];
1007
+ w.shipped = await reachable(w.database, heads);
1008
+ w.shipped.heads = heads;
1009
+ }
1010
+ // Nothing announces what has to go again: count it, so a flush ships it.
1011
+ pending = Math.max(pending, 1);
1012
+ if (running && (after || every)) scheduleFlush();
1013
+ }
1014
+ await save();
1015
+ return {
1016
+ changed: true,
1017
+ rewound: now_.rewound,
1018
+ gap: now_.gap,
1019
+ sequence: state.sequence,
1020
+ };
1021
+ };
1022
+
826
1023
  const start = async () => {
827
1024
  if (running) return;
828
1025
  running = true;
@@ -853,13 +1050,25 @@ export function createLiveBackup({
853
1050
  base: state.base,
854
1051
  previous: state.previous,
855
1052
  pending,
1053
+ unconfirmed: (state.unconfirmed ?? []).length,
1054
+ keptSequence: state.kept?.sequence ?? null,
856
1055
  watched: watched.size,
857
1056
  lastShipped,
858
1057
  lastError,
859
1058
  failures,
860
1059
  });
861
1060
 
862
- return { watch, unwatch, start, stop, close, flush, rebase, status };
1061
+ return {
1062
+ watch,
1063
+ unwatch,
1064
+ start,
1065
+ stop,
1066
+ close,
1067
+ flush,
1068
+ rebase,
1069
+ reconcile,
1070
+ status,
1071
+ };
863
1072
  }
864
1073
 
865
1074
  export default createLiveBackup;
package/lib/peer-fetch.js CHANGED
@@ -76,35 +76,65 @@ export const PINATA_BITSWAP =
76
76
  "/dns4/bitswap-v3.pinata.cloud/tcp/443/wss/p2p/Qmdv6yNikmUWUWXufLJLRNkv6Y9sY5cmgeX5RVWA4WNMz4";
77
77
 
78
78
  /**
79
- * Aleph's own node, which took some finding.
79
+ * Aleph's own node — a fallback, not an address to trust.
80
80
  *
81
81
  * It publishes no `_dnsaddr` and answers `404` to `/api/v0/id`, so looking for
82
- * it the way Pinata is found says it has no peer — which is what an earlier
83
- * version of this file claimed. It does: ask a router for the providers of a
84
- * CID Aleph holds, and the record is `46.255.204.211`, the address
85
- * `ipfs.aleph.cloud` resolves to, with `webrtc-direct` and `webtransport`
86
- * among its addresses. A page dialled it in 450 ms and had the block 345 ms
87
- * later.
82
+ * it the way Pinata is found says it has no peer. It does: ask a router for
83
+ * the providers of a CID Aleph holds.
84
+ *
85
+ * **Prefer {@link providersFor}.** Three things that were written here as
86
+ * guarantees have since been disproved, each by measurement on 2026-10-29:
88
87
  *
89
- * Two caveats make this a starting point rather than a guarantee:
88
+ * - **The peer id is not the stable part.** This file said the certhash
89
+ * rotates while the peer id outlives it. The routing now answers with
90
+ * `12D3KooWJBw9…`, where it used to answer `12D3KooWACE5…` — a different
91
+ * node, so every address here was dead and nothing said so.
92
+ * - **A page may look up after all.** `delegated-ipfs.dev` does send
93
+ * `access-control-allow-origin: *` for provider lookups now (200 with the
94
+ * header, from an origin of our own), so the reason this constant existed —
95
+ * that a browser could not ask — no longer holds. `cid.contact` still does
96
+ * not know Aleph's CIDs, because Aleph announces over the DHT and not IPNI.
97
+ * - **The node is reachable over IPv6 only.** Every address it announces is
98
+ * `/ip6/` or `/dns6/`, so a page on a network without IPv6 cannot dial it at
99
+ * whatever address this constant holds. See `offNetworkRisk` in the
100
+ * webrtc-qr package for how differently browsers report that.
90
101
  *
91
- * - **The certhash rotates.** A `webrtc-direct` address carries the
92
- * certificate's hash, and the node generates a new certificate when it
93
- * restarts. When this address stops working, look the CID up again — the
94
- * peer id is the stable part.
95
- * - **Only one router will tell you.** Aleph announces over the DHT rather
96
- * than IPNI, so `cid.contact` — the one router that answers a browser with
97
- * CORS — does not know its CIDs, while `delegated-ipfs.dev` does and sends
98
- * no CORS header for provider lookups. From a page, this constant is the way
99
- * in; from Node, {@link providersFor} with `delegated-ipfs.dev` is better.
102
+ * - **There is more than one node, and the routing names one at a time.**
103
+ * On 2026-11-11 it answered `12D3KooWACE5…` again, for six fresh blobs in a
104
+ * row — now over IPv4 (`46.255.204.211`), with a `/dns4/…libp2p.direct`
105
+ * name — while `12D3KooWJBw9…` (IPv6 only) was what it said two weeks
106
+ * earlier. So this constant holds both, the one the routing names now
107
+ * first; a dial to the other costs a failed attempt, not a wrong answer.
108
+ *
109
+ * The `/dns4/` and `/dns6/…libp2p.direct/tcp/4001/tls/ws` addresses carry no
110
+ * certificate hash and so do not rot when a node restarts; the
111
+ * `webrtc-direct` ones do. `test/peer-fetch-live.test.js` checks, every day in
112
+ * the live smoke test, that the routing names one of {@link ALEPH_PEER_IDS},
113
+ * which is how this gets noticed next time rather than in a demo six weeks
114
+ * later.
100
115
  */
116
+ const ACE5 = "12D3KooWACE5dRw5V9WXuDTcngjE3ZaDSZ4qYJGfuhXZbENnL54y";
117
+ const JBW9 = "12D3KooWJBw9CSUWQi7P7amZsjrsBAzoB2gJzyUUGkuBkdLc87co";
118
+
101
119
  export const ALEPH_BITSWAP = Object.freeze([
102
- "/ip4/46.255.204.211/udp/4001/webrtc-direct/certhash/uEiCbM9yMfnP02vviIL26n8bI0-vU0DGUHx20POBLKGjEmg/p2p/12D3KooWACE5dRw5V9WXuDTcngjE3ZaDSZ4qYJGfuhXZbENnL54y",
103
- "/ip4/46.255.204.211/udp/4001/quic-v1/webtransport/certhash/uEiDMxOK9kZFH5SW6zcmNpXU4EGgBgvZYqqlJhw1cci50KA/certhash/uEiCeRpuyQWojWx2cP8guNiY3EDfIF25D2k8CMTZd1qtSfw/p2p/12D3KooWACE5dRw5V9WXuDTcngjE3ZaDSZ4qYJGfuhXZbENnL54y",
120
+ // What the routing announced on 2026-11-11.
121
+ `/dns4/46-255-204-211.k51qzi5uqu5dgbm3weyum8p0k8k4a97eea0fkepym4bhy1g862d5av7yezl2fy.libp2p.direct/tcp/4001/tls/ws/p2p/${ACE5}`,
122
+ `/ip4/46.255.204.211/udp/4001/webrtc-direct/certhash/uEiCbM9yMfnP02vviIL26n8bI0-vU0DGUHx20POBLKGjEmg/p2p/${ACE5}`,
123
+ // What it announced on 2026-10-29.
124
+ `/dns6/2a01-240-ad00-0-a81e-30ff-fe24-e1c8.k51qzi5uqu5dja77dpum225l7jiqrxsfjg2cflzyewf8ixq3r7dmcku10s6l5o.libp2p.direct/tcp/4001/tls/ws/p2p/${JBW9}`,
125
+ `/dns6/2a01-240-ad00-2-a81e-30ff-fe24-e1c8.k51qzi5uqu5dja77dpum225l7jiqrxsfjg2cflzyewf8ixq3r7dmcku10s6l5o.libp2p.direct/tcp/4001/tls/ws/p2p/${JBW9}`,
126
+ `/ip6/2a01:240:ad00:0:a81e:30ff:fe24:e1c8/udp/4001/webrtc-direct/certhash/uEiBshqkW727Q236d7ivxBGMuc0E9Tsj3FZJMKJJgDuP4Lg/p2p/${JBW9}`,
127
+ `/ip6/2a01:240:ad00:2:a81e:30ff:fe24:e1c8/udp/4001/webrtc-direct/certhash/uEiBshqkW727Q236d7ivxBGMuc0E9Tsj3FZJMKJJgDuP4Lg/p2p/${JBW9}`,
104
128
  ]);
105
129
 
106
- /** Aleph's peer id, which outlives the certificate hashes above. */
107
- export const ALEPH_PEER_ID = "12D3KooWACE5dRw5V9WXuDTcngjE3ZaDSZ4qYJGfuhXZbENnL54y";
130
+ /**
131
+ * Aleph's peer ids, the one the routing named most recently (2026-11-11)
132
+ * first. Not the stable part: the routing has switched between them.
133
+ */
134
+ export const ALEPH_PEER_IDS = Object.freeze([ACE5, JBW9]);
135
+
136
+ /** The first of {@link ALEPH_PEER_IDS}: the node the routing named last. */
137
+ export const ALEPH_PEER_ID = ACE5;
108
138
 
109
139
  /**
110
140
  * Routers that answer a page.
@@ -151,7 +181,8 @@ export const SILENT = { info() {}, warn() {}, debug() {} };
151
181
  /** Does this multiaddr use a transport a page can open? */
152
182
  export function isBrowserDialable(addr) {
153
183
  if (typeof addr !== "string") return false;
154
- if (!BROWSER_TRANSPORTS.some((transport) => addr.includes(transport))) return false;
184
+ if (!BROWSER_TRANSPORTS.some((transport) => addr.includes(transport)))
185
+ return false;
155
186
  // A certificate cannot be issued for a bare IP, so ws/wss on one is not
156
187
  // dialable however well-formed it looks; webtransport and webrtc-direct
157
188
  // carry a certhash instead and are fine.
@@ -168,23 +199,35 @@ export function isBrowserDialable(addr) {
168
199
  * @param {boolean} [options.dialableOnly=true] - keep only what a page can open
169
200
  * @param {AbortSignal} [options.signal]
170
201
  * @param {typeof fetch} [options.fetchImpl]
202
+ * The addresses come back **with `/p2p/<id>` appended**, because that is what
203
+ * dialling one needs and composing it is the kind of step every caller gets
204
+ * right until the day it does not: the recovery example dropped the id on the
205
+ * floor and its peer path had never once worked. The `id` is returned beside
206
+ * them for identifying a provider, not for rebuilding the address.
207
+ *
171
208
  * @returns {Promise<Array<{ id: string, addrs: string[] }>>}
172
209
  */
173
- export async function providersFor(cid, {
174
- routers = DEFAULT_ROUTERS,
175
- dialableOnly = true,
176
- signal = null,
177
- fetchImpl = fetch,
178
- log = SILENT,
179
- } = {}) {
210
+ export async function providersFor(
211
+ cid,
212
+ {
213
+ routers = DEFAULT_ROUTERS,
214
+ dialableOnly = true,
215
+ signal = null,
216
+ fetchImpl = fetch,
217
+ log = SILENT,
218
+ } = {},
219
+ ) {
180
220
  const found = new Map();
181
221
 
182
222
  for (const router of routers) {
183
223
  try {
184
- const response = await fetchImpl(`${router}/routing/v1/providers/${cid}`, {
185
- headers: { Accept: "application/json" },
186
- signal,
187
- });
224
+ const response = await fetchImpl(
225
+ `${router}/routing/v1/providers/${cid}`,
226
+ {
227
+ headers: { Accept: "application/json" },
228
+ signal,
229
+ },
230
+ );
188
231
  if (!response.ok) {
189
232
  log.debug(` ⚠️ ${router} answered ${response.status}`);
190
233
  continue;
@@ -193,13 +236,14 @@ export async function providersFor(cid, {
193
236
  for (const record of parseProviderRecords(body)) {
194
237
  const id = record.ID ?? record.id;
195
238
  if (!id) continue;
196
- const addrs = (record.Addrs ?? record.addrs ?? []).filter(
197
- (addr) => !dialableOnly || isBrowserDialable(addr),
198
- );
239
+ const addrs = (record.Addrs ?? record.addrs ?? [])
240
+ .filter((addr) => !dialableOnly || isBrowserDialable(addr))
241
+ .map((addr) => (addr.includes("/p2p/") ? addr : `${addr}/p2p/${id}`));
199
242
  if (addrs.length === 0) continue;
200
243
  const already = found.get(id);
201
244
  if (already) {
202
- for (const addr of addrs) if (!already.addrs.includes(addr)) already.addrs.push(addr);
245
+ for (const addr of addrs)
246
+ if (!already.addrs.includes(addr)) already.addrs.push(addr);
203
247
  } else {
204
248
  found.set(id, { id, addrs: [...addrs] });
205
249
  }
@@ -272,7 +316,8 @@ export function createPeerFetch({
272
316
  }
273
317
 
274
318
  return async function fetchOverPeers(cid, options = {}) {
275
- const signal = options.signal ?? AbortSignal.timeout(options.timeout ?? timeout);
319
+ const signal =
320
+ options.signal ?? AbortSignal.timeout(options.timeout ?? timeout);
276
321
  const addrs =
277
322
  typeof providers === "function" ? await providers(cid) : [...providers];
278
323
 
@@ -280,10 +325,12 @@ export function createPeerFetch({
280
325
  throw new Error(`No provider to dial for ${cid}`);
281
326
  }
282
327
 
283
- const dialOne = dial ?? (async (addr) => {
284
- const { multiaddr } = await import("@multiformats/multiaddr");
285
- return helia.libp2p.dial(multiaddr(addr), { signal });
286
- });
328
+ const dialOne =
329
+ dial ??
330
+ (async (addr) => {
331
+ const { multiaddr } = await import("@multiformats/multiaddr");
332
+ return helia.libp2p.dial(multiaddr(addr), { signal });
333
+ });
287
334
 
288
335
  // One connection per peer, not one per address. A provider usually
289
336
  // advertises the same peer several times — Aleph offers webrtc-direct and
@@ -303,7 +350,9 @@ export function createPeerFetch({
303
350
  }
304
351
  const dialed = reached.size;
305
352
  if (dialed === 0) {
306
- throw new Error(`Could not dial any provider for ${cid} (tried ${addrs.length})`);
353
+ throw new Error(
354
+ `Could not dial any provider for ${cid} (tried ${addrs.length})`,
355
+ );
307
356
  }
308
357
 
309
358
  const read = cat ?? (await unixfsCat(helia));
@@ -335,7 +384,8 @@ async function unixfsCat(helia) {
335
384
  import("multiformats/cid"),
336
385
  ]);
337
386
  const fs = unixfs(helia);
338
- return (cid, options) => fs.cat(typeof cid === "string" ? CID.parse(cid) : cid, options);
387
+ return (cid, options) =>
388
+ fs.cat(typeof cid === "string" ? CID.parse(cid) : cid, options);
339
389
  }
340
390
 
341
391
  /**
@@ -365,7 +415,10 @@ export function createGatewayFirstFetch({
365
415
  return async function fetchBytes(cid, options = {}) {
366
416
  const started = now();
367
417
  try {
368
- const bytes = await viaGateway(cid, { ...options, timeout: gatewayTimeout });
418
+ const bytes = await viaGateway(cid, {
419
+ ...options,
420
+ timeout: gatewayTimeout,
421
+ });
369
422
  onPath("gateway", { cid, ms: now() - started, bytes: bytes.length });
370
423
  return bytes;
371
424
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@le-space/orbitdb-storage-bridge",
3
- "version": "0.27.1",
3
+ "version": "0.28.0",
4
4
  "description": "Back up, restore and replicate OrbitDB databases through pluggable storage backends, with hash and identity preservation",
5
5
  "main": "lib/orbitdb-storacha-bridge.js",
6
6
  "type": "module",
@@ -119,6 +119,8 @@
119
119
  "@jest/globals": "^30.5.1",
120
120
  "@le-space/ucanto-principal": "^9.1.1",
121
121
  "@libp2p/websockets": "^10.1.17",
122
+ "@noble/curves": "^1.9.7",
123
+ "@noble/hashes": "^1.8.0",
122
124
  "@orbitdb/identity-provider-did": "^1.0.2",
123
125
  "@storacha/capabilities": "^2.0.0",
124
126
  "@storacha/client": "^2.0.0",