@le-space/orbitdb-storage-bridge 0.27.1 → 0.27.2
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/lib/backends/aleph-pin.js +20 -2
- package/lib/backends/aleph-upload.js +34 -0
- package/lib/backends/aleph.js +40 -9
- package/lib/gateway-fetch.js +28 -1
- package/lib/peer-fetch.js +83 -45
- package/package.json +1 -1
|
@@ -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?.(),
|
|
@@ -355,7 +368,7 @@ export async function waitForMessage(itemHash, options = {}) {
|
|
|
355
368
|
* @param {typeof fetch} [options.fetch]
|
|
356
369
|
* @param {number} [options.pagination] - per page; default 50
|
|
357
370
|
* @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 }>}
|
|
371
|
+
* @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
372
|
* `cid` is what the STORE keeps — for an upload through `createAlephBackend`,
|
|
360
373
|
* the handle's `id`
|
|
361
374
|
*/
|
|
@@ -405,6 +418,11 @@ export async function listAlephStores(options = {}) {
|
|
|
405
418
|
...(typeof m?.content?.ref === "string" && m.content.ref
|
|
406
419
|
? { ref: m.content.ref }
|
|
407
420
|
: {}),
|
|
421
|
+
// The size the STORE names (buildStoreMessage `size`): a download knows
|
|
422
|
+
// what to expect, and what to refuse. Absent on older STOREs.
|
|
423
|
+
...(Number.isInteger(m?.content?.size) && m.content.size >= 0
|
|
424
|
+
? { size: m.content.size }
|
|
425
|
+
: {}),
|
|
408
426
|
...(m?.content?.payment?.type ? { payment: m.content.payment.type } : {}),
|
|
409
427
|
}))
|
|
410
428
|
// 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) {
|
package/lib/backends/aleph.js
CHANGED
|
@@ -53,6 +53,7 @@ import {
|
|
|
53
53
|
hashFromAddAnswer,
|
|
54
54
|
postFile,
|
|
55
55
|
unixfsCid,
|
|
56
|
+
matchesUnixfsCid,
|
|
56
57
|
uploadTimeout,
|
|
57
58
|
} from "./aleph-upload.js";
|
|
58
59
|
|
|
@@ -100,8 +101,12 @@ const DEFAULT_TIMEOUT_MS = 120_000;
|
|
|
100
101
|
* @returns {import("./types.js").StorageBackend}
|
|
101
102
|
*/
|
|
102
103
|
export function createAlephBackend(options = {}) {
|
|
103
|
-
const ingestUrl =
|
|
104
|
-
|
|
104
|
+
const ingestUrl =
|
|
105
|
+
options.ingestUrl === null ? null : options.ingestUrl || ALEPH_INGEST;
|
|
106
|
+
const apiHost = (options.apiHost || DEFAULT_ALEPH_API_HOST).replace(
|
|
107
|
+
/\/+$/,
|
|
108
|
+
"",
|
|
109
|
+
);
|
|
105
110
|
const gateways = options.gateways || ALEPH_GATEWAYS;
|
|
106
111
|
const timeout = options.timeout ?? DEFAULT_TIMEOUT_MS;
|
|
107
112
|
const pin = options.pin;
|
|
@@ -188,7 +193,8 @@ export function createAlephBackend(options = {}) {
|
|
|
188
193
|
async putBlob(bytes, meta = {}) {
|
|
189
194
|
const name = meta.name || "blob";
|
|
190
195
|
const contentType = meta.contentType || "application/octet-stream";
|
|
191
|
-
const onProgress =
|
|
196
|
+
const onProgress =
|
|
197
|
+
typeof meta.onProgress === "function" ? meta.onProgress : undefined;
|
|
192
198
|
const handle = {
|
|
193
199
|
backend: "aleph",
|
|
194
200
|
size: bytes.length,
|
|
@@ -218,7 +224,8 @@ export function createAlephBackend(options = {}) {
|
|
|
218
224
|
const signed = typeof pin?.sign === "function";
|
|
219
225
|
// The node measures the request body, the form around the file included.
|
|
220
226
|
const limit =
|
|
221
|
-
(signed ? ALEPH_API_SIGNED_LIMIT : ALEPH_API_UNSIGNED_LIMIT) -
|
|
227
|
+
(signed ? ALEPH_API_SIGNED_LIMIT : ALEPH_API_UNSIGNED_LIMIT) -
|
|
228
|
+
ALEPH_API_FORM_HEADROOM;
|
|
222
229
|
if (bytes.length > limit) {
|
|
223
230
|
const error = new BackendError(
|
|
224
231
|
"UNSUPPORTED",
|
|
@@ -239,7 +246,7 @@ export function createAlephBackend(options = {}) {
|
|
|
239
246
|
// message names: so the CID is computed here first, the way the node
|
|
240
247
|
// computes it (aleph-upload.js `unixfsCid`).
|
|
241
248
|
const cid = await unixfsCid(bytes);
|
|
242
|
-
const message = await pin.sign(cid, meta);
|
|
249
|
+
const message = await pin.sign(cid, { ...meta, size: bytes.length });
|
|
243
250
|
itemHash = message.item_hash;
|
|
244
251
|
fields.metadata = JSON.stringify({ message, sync: true });
|
|
245
252
|
}
|
|
@@ -263,14 +270,21 @@ export function createAlephBackend(options = {}) {
|
|
|
263
270
|
try {
|
|
264
271
|
body = JSON.parse(text);
|
|
265
272
|
} catch {
|
|
266
|
-
throw new BackendError(
|
|
273
|
+
throw new BackendError(
|
|
274
|
+
"UNSUPPORTED",
|
|
275
|
+
"Aleph's API answered something that is not JSON",
|
|
276
|
+
);
|
|
267
277
|
}
|
|
268
278
|
if (typeof body?.hash !== "string" || !body.hash) {
|
|
269
279
|
throw new BackendError("UNSUPPORTED", "Aleph's API returned no hash");
|
|
270
280
|
}
|
|
271
281
|
// A signed upload answers with the message's status (200 processed,
|
|
272
282
|
// 202 pending); a plain one is stored, not kept, as on the IPFS host.
|
|
273
|
-
const messageStatus = signed
|
|
283
|
+
const messageStatus = signed
|
|
284
|
+
? status === 200
|
|
285
|
+
? "processed"
|
|
286
|
+
: "pending"
|
|
287
|
+
: undefined;
|
|
274
288
|
return {
|
|
275
289
|
...handle,
|
|
276
290
|
id: body.hash,
|
|
@@ -282,14 +296,31 @@ export function createAlephBackend(options = {}) {
|
|
|
282
296
|
|
|
283
297
|
async getBlob(handle, options = {}) {
|
|
284
298
|
const id = handleId(handle);
|
|
299
|
+
// What came back is checked against the CID (a UnixFS one is built
|
|
300
|
+
// again from the bytes) and, when known, against the size the STORE
|
|
301
|
+
// names: a short or damaged download is refused at the gateway, not
|
|
302
|
+
// found out when a sealed file fails to open.
|
|
303
|
+
const size = Number.isInteger(options?.size)
|
|
304
|
+
? options.size
|
|
305
|
+
: Number.isInteger(handle?.size)
|
|
306
|
+
? handle.size
|
|
307
|
+
: null;
|
|
285
308
|
try {
|
|
286
309
|
return await fetchFromGateways(id, {
|
|
287
310
|
gateways,
|
|
288
311
|
timeout,
|
|
289
|
-
|
|
312
|
+
verify: matchesUnixfsCid,
|
|
313
|
+
fetchImpl: doFetch,
|
|
314
|
+
...(size != null && size > 0 ? { expectedSize: size } : {}),
|
|
315
|
+
...(typeof options?.onProgress === "function"
|
|
316
|
+
? { onProgress: options.onProgress }
|
|
317
|
+
: {}),
|
|
290
318
|
});
|
|
291
319
|
} catch (error) {
|
|
292
|
-
throw new BackendError(
|
|
320
|
+
throw new BackendError(
|
|
321
|
+
"NOT_FOUND",
|
|
322
|
+
`No blob for ${id}: ${error.message}`,
|
|
323
|
+
);
|
|
293
324
|
}
|
|
294
325
|
},
|
|
295
326
|
};
|
package/lib/gateway-fetch.js
CHANGED
|
@@ -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
|
-
|
|
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}`);
|
package/lib/peer-fetch.js
CHANGED
|
@@ -76,35 +76,50 @@ 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,
|
|
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
|
|
83
|
-
*
|
|
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.
|
|
88
84
|
*
|
|
89
|
-
*
|
|
85
|
+
* **Prefer {@link providersFor}.** Three things that were written here as
|
|
86
|
+
* guarantees have since been disproved, each by measurement on 2026-10-29:
|
|
90
87
|
*
|
|
91
|
-
* - **The
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
* - **
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
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.
|
|
101
|
+
*
|
|
102
|
+
* What is below is what the routing announced on 2026-10-29. The two
|
|
103
|
+
* `/dns6/…libp2p.direct/tcp/4001/tls/ws` addresses carry no certificate hash
|
|
104
|
+
* and so do not rot when the node restarts; the `webrtc-direct` ones do.
|
|
105
|
+
* `test/peer-fetch-live.test.js` checks the peer id against the routing, which
|
|
106
|
+
* is how this gets noticed next time rather than in a demo six weeks later.
|
|
100
107
|
*/
|
|
101
108
|
export const ALEPH_BITSWAP = Object.freeze([
|
|
102
|
-
"/
|
|
103
|
-
"/
|
|
109
|
+
"/dns6/2a01-240-ad00-0-a81e-30ff-fe24-e1c8.k51qzi5uqu5dja77dpum225l7jiqrxsfjg2cflzyewf8ixq3r7dmcku10s6l5o.libp2p.direct/tcp/4001/tls/ws/p2p/12D3KooWJBw9CSUWQi7P7amZsjrsBAzoB2gJzyUUGkuBkdLc87co",
|
|
110
|
+
"/dns6/2a01-240-ad00-2-a81e-30ff-fe24-e1c8.k51qzi5uqu5dja77dpum225l7jiqrxsfjg2cflzyewf8ixq3r7dmcku10s6l5o.libp2p.direct/tcp/4001/tls/ws/p2p/12D3KooWJBw9CSUWQi7P7amZsjrsBAzoB2gJzyUUGkuBkdLc87co",
|
|
111
|
+
"/ip6/2a01:240:ad00:0:a81e:30ff:fe24:e1c8/udp/4001/webrtc-direct/certhash/uEiBshqkW727Q236d7ivxBGMuc0E9Tsj3FZJMKJJgDuP4Lg/p2p/12D3KooWJBw9CSUWQi7P7amZsjrsBAzoB2gJzyUUGkuBkdLc87co",
|
|
112
|
+
"/ip6/2a01:240:ad00:2:a81e:30ff:fe24:e1c8/udp/4001/webrtc-direct/certhash/uEiBshqkW727Q236d7ivxBGMuc0E9Tsj3FZJMKJJgDuP4Lg/p2p/12D3KooWJBw9CSUWQi7P7amZsjrsBAzoB2gJzyUUGkuBkdLc87co",
|
|
104
113
|
]);
|
|
105
114
|
|
|
106
|
-
/**
|
|
107
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Aleph's peer id, as announced on 2026-10-29.
|
|
117
|
+
*
|
|
118
|
+
* Not the stable part: it changed once already, which is what made every
|
|
119
|
+
* address above dead until this commit.
|
|
120
|
+
*/
|
|
121
|
+
export const ALEPH_PEER_ID =
|
|
122
|
+
"12D3KooWJBw9CSUWQi7P7amZsjrsBAzoB2gJzyUUGkuBkdLc87co";
|
|
108
123
|
|
|
109
124
|
/**
|
|
110
125
|
* Routers that answer a page.
|
|
@@ -151,7 +166,8 @@ export const SILENT = { info() {}, warn() {}, debug() {} };
|
|
|
151
166
|
/** Does this multiaddr use a transport a page can open? */
|
|
152
167
|
export function isBrowserDialable(addr) {
|
|
153
168
|
if (typeof addr !== "string") return false;
|
|
154
|
-
if (!BROWSER_TRANSPORTS.some((transport) => addr.includes(transport)))
|
|
169
|
+
if (!BROWSER_TRANSPORTS.some((transport) => addr.includes(transport)))
|
|
170
|
+
return false;
|
|
155
171
|
// A certificate cannot be issued for a bare IP, so ws/wss on one is not
|
|
156
172
|
// dialable however well-formed it looks; webtransport and webrtc-direct
|
|
157
173
|
// carry a certhash instead and are fine.
|
|
@@ -168,23 +184,35 @@ export function isBrowserDialable(addr) {
|
|
|
168
184
|
* @param {boolean} [options.dialableOnly=true] - keep only what a page can open
|
|
169
185
|
* @param {AbortSignal} [options.signal]
|
|
170
186
|
* @param {typeof fetch} [options.fetchImpl]
|
|
187
|
+
* The addresses come back **with `/p2p/<id>` appended**, because that is what
|
|
188
|
+
* dialling one needs and composing it is the kind of step every caller gets
|
|
189
|
+
* right until the day it does not: the recovery example dropped the id on the
|
|
190
|
+
* floor and its peer path had never once worked. The `id` is returned beside
|
|
191
|
+
* them for identifying a provider, not for rebuilding the address.
|
|
192
|
+
*
|
|
171
193
|
* @returns {Promise<Array<{ id: string, addrs: string[] }>>}
|
|
172
194
|
*/
|
|
173
|
-
export async function providersFor(
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
195
|
+
export async function providersFor(
|
|
196
|
+
cid,
|
|
197
|
+
{
|
|
198
|
+
routers = DEFAULT_ROUTERS,
|
|
199
|
+
dialableOnly = true,
|
|
200
|
+
signal = null,
|
|
201
|
+
fetchImpl = fetch,
|
|
202
|
+
log = SILENT,
|
|
203
|
+
} = {},
|
|
204
|
+
) {
|
|
180
205
|
const found = new Map();
|
|
181
206
|
|
|
182
207
|
for (const router of routers) {
|
|
183
208
|
try {
|
|
184
|
-
const response = await fetchImpl(
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
209
|
+
const response = await fetchImpl(
|
|
210
|
+
`${router}/routing/v1/providers/${cid}`,
|
|
211
|
+
{
|
|
212
|
+
headers: { Accept: "application/json" },
|
|
213
|
+
signal,
|
|
214
|
+
},
|
|
215
|
+
);
|
|
188
216
|
if (!response.ok) {
|
|
189
217
|
log.debug(` ⚠️ ${router} answered ${response.status}`);
|
|
190
218
|
continue;
|
|
@@ -193,13 +221,14 @@ export async function providersFor(cid, {
|
|
|
193
221
|
for (const record of parseProviderRecords(body)) {
|
|
194
222
|
const id = record.ID ?? record.id;
|
|
195
223
|
if (!id) continue;
|
|
196
|
-
const addrs = (record.Addrs ?? record.addrs ?? [])
|
|
197
|
-
(addr) => !dialableOnly || isBrowserDialable(addr)
|
|
198
|
-
|
|
224
|
+
const addrs = (record.Addrs ?? record.addrs ?? [])
|
|
225
|
+
.filter((addr) => !dialableOnly || isBrowserDialable(addr))
|
|
226
|
+
.map((addr) => (addr.includes("/p2p/") ? addr : `${addr}/p2p/${id}`));
|
|
199
227
|
if (addrs.length === 0) continue;
|
|
200
228
|
const already = found.get(id);
|
|
201
229
|
if (already) {
|
|
202
|
-
for (const addr of addrs)
|
|
230
|
+
for (const addr of addrs)
|
|
231
|
+
if (!already.addrs.includes(addr)) already.addrs.push(addr);
|
|
203
232
|
} else {
|
|
204
233
|
found.set(id, { id, addrs: [...addrs] });
|
|
205
234
|
}
|
|
@@ -272,7 +301,8 @@ export function createPeerFetch({
|
|
|
272
301
|
}
|
|
273
302
|
|
|
274
303
|
return async function fetchOverPeers(cid, options = {}) {
|
|
275
|
-
const signal =
|
|
304
|
+
const signal =
|
|
305
|
+
options.signal ?? AbortSignal.timeout(options.timeout ?? timeout);
|
|
276
306
|
const addrs =
|
|
277
307
|
typeof providers === "function" ? await providers(cid) : [...providers];
|
|
278
308
|
|
|
@@ -280,10 +310,12 @@ export function createPeerFetch({
|
|
|
280
310
|
throw new Error(`No provider to dial for ${cid}`);
|
|
281
311
|
}
|
|
282
312
|
|
|
283
|
-
const dialOne =
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
313
|
+
const dialOne =
|
|
314
|
+
dial ??
|
|
315
|
+
(async (addr) => {
|
|
316
|
+
const { multiaddr } = await import("@multiformats/multiaddr");
|
|
317
|
+
return helia.libp2p.dial(multiaddr(addr), { signal });
|
|
318
|
+
});
|
|
287
319
|
|
|
288
320
|
// One connection per peer, not one per address. A provider usually
|
|
289
321
|
// advertises the same peer several times — Aleph offers webrtc-direct and
|
|
@@ -303,7 +335,9 @@ export function createPeerFetch({
|
|
|
303
335
|
}
|
|
304
336
|
const dialed = reached.size;
|
|
305
337
|
if (dialed === 0) {
|
|
306
|
-
throw new Error(
|
|
338
|
+
throw new Error(
|
|
339
|
+
`Could not dial any provider for ${cid} (tried ${addrs.length})`,
|
|
340
|
+
);
|
|
307
341
|
}
|
|
308
342
|
|
|
309
343
|
const read = cat ?? (await unixfsCat(helia));
|
|
@@ -335,7 +369,8 @@ async function unixfsCat(helia) {
|
|
|
335
369
|
import("multiformats/cid"),
|
|
336
370
|
]);
|
|
337
371
|
const fs = unixfs(helia);
|
|
338
|
-
return (cid, options) =>
|
|
372
|
+
return (cid, options) =>
|
|
373
|
+
fs.cat(typeof cid === "string" ? CID.parse(cid) : cid, options);
|
|
339
374
|
}
|
|
340
375
|
|
|
341
376
|
/**
|
|
@@ -365,7 +400,10 @@ export function createGatewayFirstFetch({
|
|
|
365
400
|
return async function fetchBytes(cid, options = {}) {
|
|
366
401
|
const started = now();
|
|
367
402
|
try {
|
|
368
|
-
const bytes = await viaGateway(cid, {
|
|
403
|
+
const bytes = await viaGateway(cid, {
|
|
404
|
+
...options,
|
|
405
|
+
timeout: gatewayTimeout,
|
|
406
|
+
});
|
|
369
407
|
onPath("gateway", { cid, ms: now() - started, bytes: bytes.length });
|
|
370
408
|
return bytes;
|
|
371
409
|
} catch (error) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@le-space/orbitdb-storage-bridge",
|
|
3
|
-
"version": "0.27.
|
|
3
|
+
"version": "0.27.2",
|
|
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",
|