@le-space/orbitdb-storage-bridge 0.25.1 → 0.27.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 +4 -3
- package/lib/backends/aleph-pin.js +19 -4
- package/lib/backends/aleph-upload.js +243 -0
- package/lib/backends/aleph.js +147 -49
- package/package.json +7 -6
- package/dist/components/StorachaAuth.svelte +0 -801
- package/dist/components/StorachaIntegration.svelte +0 -965
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ Where "somewhere else" is, is a choice:
|
|
|
25
25
|
|
|
26
26
|
| Backend | Needs | |
|
|
27
27
|
| --- | --- | --- |
|
|
28
|
-
| `aleph` | nothing | the only backend a browser can write to with no key; ingest only — nothing is kept, listed or deleted here, see `aleph-pin
|
|
28
|
+
| `aleph` | nothing; or `aleph-pin`'s pin, to keep in the same call | the only backend a browser can write to with no key; ingest only — nothing is kept, listed or deleted here, see `aleph-pin`. Since Aleph's IPFS hosts stopped taking uploads (2026-10-26) it uploads through an API node: up to 25 MiB with no key, up to 1 GiB with a pin that signs |
|
|
29
29
|
| `aleph-pin` | a signing key: the paying account's, or one it authorised | the signed STORE message that makes Aleph keep it, paid in credits — and the FORGET that releases it |
|
|
30
30
|
| `pinata` | a scoped JWT | pin-by-CID as well as CAR upload |
|
|
31
31
|
| `lighthouse` | an API key | pay once, stored in perpetuity |
|
|
@@ -65,8 +65,9 @@ entry is the Storacha-era class and the helpers it grew.
|
|
|
65
65
|
| `/memory-courier` | `createMemoryCourierPair`: the in-process courier for tests and demos |
|
|
66
66
|
| `/backends/types` | the contract: `defineBackend`, `BackendError`, the capability flags |
|
|
67
67
|
| `/backends/memory` | the in-process driver |
|
|
68
|
-
| `/backends/aleph` | `createAlephBackend`: ingest to Aleph
|
|
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
69
|
| `/backends/aleph-pin` | `createAlephPin`, `createAlephAuthorizer`, `listAlephStores`, `waitForMessage`, `createAlephForget`, `forgetAlephStores`: what makes Aleph keep a file, and lets it go |
|
|
70
|
+
| `/backends/aleph-upload` | `unixfsCid`: the CID Aleph gives a file, before the upload; `postFile`, the limits and the upload errors |
|
|
70
71
|
| `/backends/pinata` | Pinata: pin by CID or upload, with a scoped JWT; `remove()` by file id |
|
|
71
72
|
| `/backends/lighthouse` | Lighthouse: upload with an API key; `list()` and `remove()` |
|
|
72
73
|
| `/backends/storacha` | the Storacha driver; needs the optional peer `@storacha/client`, and a service that no longer answers |
|
|
@@ -74,7 +75,7 @@ entry is the Storacha-era class and the helpers it grew.
|
|
|
74
75
|
| `/backends/encryption` | `withEncryption`: the service holds ciphertext |
|
|
75
76
|
| `/backends/choose` | `createBackendFromChoice`: a backend from a name and a key, for a page's form |
|
|
76
77
|
| `/backends/resolve` | `resolveBackend`: the options-to-backend step the older functions share |
|
|
77
|
-
| `/
|
|
78
|
+
| `/storacha-backup`, `/components/*` | the JavaScript of the old browser widget: `storacha-backup.js`, `WebAuthnDIDProvider.js`, `theme.js`. The two `.svelte` components were dropped — they imported four files no release ever contained ([docs/STORACHA-UI-HISTORY.md](docs/STORACHA-UI-HISTORY.md)) |
|
|
78
79
|
|
|
79
80
|
## Install
|
|
80
81
|
|
|
@@ -205,7 +205,15 @@ export function createAlephPin(options = {}) {
|
|
|
205
205
|
if (typeof doFetch !== "function")
|
|
206
206
|
throw new BackendError("INVALID_BACKEND", "createAlephPin needs fetch");
|
|
207
207
|
|
|
208
|
-
|
|
208
|
+
/**
|
|
209
|
+
* The STORE message for `cid`, signed and not sent: what an upload through
|
|
210
|
+
* an API node takes beside the file (`createAlephBackend`, `putBlob`), so
|
|
211
|
+
* the node keeps the file in the same call.
|
|
212
|
+
*
|
|
213
|
+
* @param {string} cid
|
|
214
|
+
* @param {{ ref?: string }} [meta]
|
|
215
|
+
*/
|
|
216
|
+
async function signed(cid, meta = {}) {
|
|
209
217
|
const unsigned = await buildStoreMessage({
|
|
210
218
|
sender,
|
|
211
219
|
owner,
|
|
@@ -217,10 +225,14 @@ export function createAlephPin(options = {}) {
|
|
|
217
225
|
now: now?.(),
|
|
218
226
|
});
|
|
219
227
|
const signature = await sign(sender, signaturePayload(unsigned));
|
|
220
|
-
|
|
228
|
+
return {
|
|
221
229
|
...unsigned,
|
|
222
230
|
signature: signature.startsWith("0x") ? signature : `0x${signature}`,
|
|
223
231
|
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async function pin(cid, meta = {}) {
|
|
235
|
+
const message = await signed(cid, meta);
|
|
224
236
|
|
|
225
237
|
const response = await doFetch(`${apiHost}/api/v0/messages`, {
|
|
226
238
|
method: "POST",
|
|
@@ -242,10 +254,13 @@ export function createAlephPin(options = {}) {
|
|
|
242
254
|
|
|
243
255
|
const body = await response.json().catch(() => ({}));
|
|
244
256
|
return {
|
|
245
|
-
itemHash:
|
|
257
|
+
itemHash: message.item_hash,
|
|
246
258
|
status: body?.message_status ?? "pending",
|
|
247
259
|
};
|
|
248
|
-
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
pin.sign = signed;
|
|
263
|
+
return pin;
|
|
249
264
|
}
|
|
250
265
|
|
|
251
266
|
/** Statuses after which a message no longer changes by itself. */
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview The upload itself: one file in a multipart form, with the
|
|
3
|
+
* progress a browser can report, and the CID Aleph will give the file.
|
|
4
|
+
*
|
|
5
|
+
* Two hosts take a file (`./aleph.js` says which is tried when):
|
|
6
|
+
*
|
|
7
|
+
* - the IPFS host's `POST /api/v0/add`, which answers one JSON object per
|
|
8
|
+
* line, `{ Name, Hash, Size }` for the file and one more for every directory
|
|
9
|
+
* a name with a path made it create;
|
|
10
|
+
* - an API node's `POST /api/v0/ipfs/add_file`, which answers one object,
|
|
11
|
+
* `{ status, hash, name, size }`, and takes a signed STORE message in a
|
|
12
|
+
* `metadata` part beside the file. With the message the node keeps the file
|
|
13
|
+
* in the same call and lets it be up to 1 GiB; without, 25 MiB
|
|
14
|
+
* (pyaleph's `max_unauthenticated_upload_file_size`) and a 24-hour grace
|
|
15
|
+
* period until a STORE names it. The node checks the message's signature
|
|
16
|
+
* and the account's balance before it pins, and after pinning that the CID
|
|
17
|
+
* it got is the one the message names — so the CID has to be known before
|
|
18
|
+
* the upload, which is what `unixfsCid` is for.
|
|
19
|
+
*
|
|
20
|
+
* ## The CID, computed here
|
|
21
|
+
*
|
|
22
|
+
* Both hosts wrap the bytes in UnixFS the way `ipfs add` does by default:
|
|
23
|
+
* CIDv0, dag-pb leaves (no raw leaves), 256 KiB chunks, a balanced tree with
|
|
24
|
+
* up to 174 links a node, a single chunk reduced to the file node itself.
|
|
25
|
+
* `ipfs-unixfs-importer` with those options gave the same CID as the API node
|
|
26
|
+
* for a 16-byte and a 600 KB file (measured 2026-10-27). A file above about
|
|
27
|
+
* 45 MB gets a second level of links; the same defaults cover it, and the live
|
|
28
|
+
* test is where that claim is checked.
|
|
29
|
+
*
|
|
30
|
+
* ## Progress
|
|
31
|
+
*
|
|
32
|
+
* `fetch` says nothing until the last byte is through. Where
|
|
33
|
+
* `XMLHttpRequest` exists (a browser) and progress is asked for, the form goes
|
|
34
|
+
* through it and `onProgress(sent, total)` follows the bytes. Listening to
|
|
35
|
+
* upload progress makes the browser send a CORS preflight first; the API
|
|
36
|
+
* nodes answer it for any origin (measured 2026-10-26), the IPFS host did not
|
|
37
|
+
* (aleph-im/pyaleph#1280) — so on the IPFS host a refused preflight is
|
|
38
|
+
* retried once without progress, by `./aleph.js`.
|
|
39
|
+
*
|
|
40
|
+
* @author @NiKrause
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/* global FormData, XMLHttpRequest */
|
|
44
|
+
|
|
45
|
+
import { importer } from "ipfs-unixfs-importer";
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* What an API node takes without a signed STORE message: 25 MiB – of the
|
|
49
|
+
* request body, the form around the file included ("Maximum request body
|
|
50
|
+
* size 26214400 exceeded", measured 2026-10-27 with 26.3 MB). So a file this
|
|
51
|
+
* close to it needs `ALEPH_API_FORM_HEADROOM` less.
|
|
52
|
+
*/
|
|
53
|
+
export const ALEPH_API_UNSIGNED_LIMIT = 25 * 1024 * 1024;
|
|
54
|
+
|
|
55
|
+
/** Room for the multipart form around the file: its boundaries and headers. */
|
|
56
|
+
export const ALEPH_API_FORM_HEADROOM = 4096;
|
|
57
|
+
|
|
58
|
+
/** What an API node takes with one: 1 GiB (pyaleph's `max_upload_file_size`). */
|
|
59
|
+
export const ALEPH_API_SIGNED_LIMIT = 1024 * 1024 * 1024;
|
|
60
|
+
|
|
61
|
+
/** Two minutes for the host to answer, and the bytes' own time on top. */
|
|
62
|
+
const TIMEOUT_MS = 120_000;
|
|
63
|
+
/**
|
|
64
|
+
* At least 100 kB/s: a 500 MB file may take its 85 minutes. Two minutes and
|
|
65
|
+
* 1 MB/s were too little for a home uplink — a full backup never went through
|
|
66
|
+
* (Le-Space/belege#505).
|
|
67
|
+
*/
|
|
68
|
+
const BYTES_PER_MS = 100;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The CID Aleph's hosts give `bytes`: UnixFS with `ipfs add`'s defaults.
|
|
72
|
+
*
|
|
73
|
+
* @param {Uint8Array} bytes
|
|
74
|
+
* @returns {Promise<string>} a CIDv0, base58btc
|
|
75
|
+
*/
|
|
76
|
+
export async function unixfsCid(bytes) {
|
|
77
|
+
// The importer writes every block it makes; none is read back, so a sink
|
|
78
|
+
// that keeps nothing is a blockstore enough.
|
|
79
|
+
const sink = {
|
|
80
|
+
put: async (cid) => cid,
|
|
81
|
+
get: async () => {
|
|
82
|
+
throw new Error("unixfsCid keeps no blocks");
|
|
83
|
+
},
|
|
84
|
+
has: async () => false,
|
|
85
|
+
};
|
|
86
|
+
let last;
|
|
87
|
+
for await (const entry of importer([{ content: bytes }], sink, {
|
|
88
|
+
cidVersion: 0,
|
|
89
|
+
rawLeaves: false,
|
|
90
|
+
reduceSingleLeafToSelf: true,
|
|
91
|
+
})) {
|
|
92
|
+
last = entry;
|
|
93
|
+
}
|
|
94
|
+
if (!last) throw new Error("unixfsCid: the importer gave no entry");
|
|
95
|
+
return last.cid.toString();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Thrown when a host answers that the endpoint is not there (404, 405). */
|
|
99
|
+
export class EndpointGoneError extends Error {
|
|
100
|
+
constructor(url, status) {
|
|
101
|
+
super(`${url} answered ${status}: no upload there`);
|
|
102
|
+
this.name = "EndpointGoneError";
|
|
103
|
+
this.status = status;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Thrown when the browser refused before a byte went: the CORS preflight. */
|
|
108
|
+
export class PreflightRefusedError extends Error {
|
|
109
|
+
constructor(url) {
|
|
110
|
+
super(`${url} did not allow this page's preflight`);
|
|
111
|
+
this.name = "PreflightRefusedError";
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The upload took longer than a slow line would. */
|
|
116
|
+
export class UploadTimeoutError extends Error {
|
|
117
|
+
constructor(url, ms, size) {
|
|
118
|
+
super(
|
|
119
|
+
`${url} did not answer within ${Math.round(ms / 60_000)} minutes for ${size} bytes`,
|
|
120
|
+
);
|
|
121
|
+
this.name = "UploadTimeoutError";
|
|
122
|
+
this.minutes = Math.round(ms / 60_000);
|
|
123
|
+
this.size = size;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** How long a request for `size` bytes may take. */
|
|
128
|
+
export const uploadTimeout = (size, base = TIMEOUT_MS) =>
|
|
129
|
+
Math.ceil(base + size / BYTES_PER_MS);
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Post `bytes` as the `file` part of a form, with any other string fields
|
|
133
|
+
* beside it, and give back the answer's text.
|
|
134
|
+
*
|
|
135
|
+
* @param {object} params
|
|
136
|
+
* @param {string} params.url
|
|
137
|
+
* @param {Uint8Array} params.bytes
|
|
138
|
+
* @param {string} params.name - the file name, as the host sees it
|
|
139
|
+
* @param {Record<string, string>} [params.fields] - more parts, e.g. `metadata`
|
|
140
|
+
* @param {string} [params.contentType]
|
|
141
|
+
* @param {(sent: number, total: number) => void} [params.onProgress]
|
|
142
|
+
* @param {number} [params.timeout] - ms; default from the size
|
|
143
|
+
* @param {typeof fetch} [params.fetch]
|
|
144
|
+
* @returns {Promise<{ status: number, text: string }>}
|
|
145
|
+
* @throws {EndpointGoneError} 404 or 405
|
|
146
|
+
* @throws {PreflightRefusedError} a browser's preflight was refused (progress asked)
|
|
147
|
+
* @throws {UploadTimeoutError}
|
|
148
|
+
*/
|
|
149
|
+
export async function postFile({
|
|
150
|
+
url,
|
|
151
|
+
bytes,
|
|
152
|
+
name,
|
|
153
|
+
fields = {},
|
|
154
|
+
contentType = "application/octet-stream",
|
|
155
|
+
onProgress,
|
|
156
|
+
timeout,
|
|
157
|
+
fetch: doFetch,
|
|
158
|
+
}) {
|
|
159
|
+
const form = new FormData();
|
|
160
|
+
form.append("file", new Blob([bytes], { type: contentType }), name);
|
|
161
|
+
for (const [field, value] of Object.entries(fields))
|
|
162
|
+
form.append(field, value);
|
|
163
|
+
const ms = Math.ceil(timeout ?? uploadTimeout(bytes.length));
|
|
164
|
+
|
|
165
|
+
if (onProgress && typeof XMLHttpRequest !== "undefined") {
|
|
166
|
+
return postWithProgress({ url, form, size: bytes.length, onProgress, ms });
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const fetchImpl = doFetch || globalThis.fetch;
|
|
170
|
+
let response;
|
|
171
|
+
try {
|
|
172
|
+
response = await fetchImpl(url, {
|
|
173
|
+
method: "POST",
|
|
174
|
+
body: form,
|
|
175
|
+
signal: AbortSignal.timeout ? AbortSignal.timeout(ms) : undefined,
|
|
176
|
+
});
|
|
177
|
+
} catch (error) {
|
|
178
|
+
if (error?.name === "TimeoutError" || error?.name === "AbortError") {
|
|
179
|
+
throw new UploadTimeoutError(url, ms, bytes.length);
|
|
180
|
+
}
|
|
181
|
+
throw error;
|
|
182
|
+
}
|
|
183
|
+
if (response.status === 404 || response.status === 405) {
|
|
184
|
+
throw new EndpointGoneError(url, response.status);
|
|
185
|
+
}
|
|
186
|
+
const text = await response.text();
|
|
187
|
+
onProgress?.(bytes.length, bytes.length);
|
|
188
|
+
return { status: response.status, text };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** The same POST through XMLHttpRequest, for the upload events only it reports. */
|
|
192
|
+
function postWithProgress({ url, form, size, onProgress, ms }) {
|
|
193
|
+
return new Promise((resolve, reject) => {
|
|
194
|
+
const xhr = new XMLHttpRequest();
|
|
195
|
+
xhr.open("POST", url);
|
|
196
|
+
xhr.timeout = ms;
|
|
197
|
+
let sent = 0;
|
|
198
|
+
xhr.upload.onprogress = (event) => {
|
|
199
|
+
sent = event.loaded;
|
|
200
|
+
onProgress(event.loaded, event.lengthComputable ? event.total : size);
|
|
201
|
+
};
|
|
202
|
+
xhr.onload = () => {
|
|
203
|
+
if (xhr.status === 404 || xhr.status === 405) {
|
|
204
|
+
reject(new EndpointGoneError(url, xhr.status));
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
resolve({ status: xhr.status, text: xhr.responseText });
|
|
208
|
+
};
|
|
209
|
+
// A network error before the first byte went out is, in a browser, the
|
|
210
|
+
// preflight: the host did not allow this origin to count its upload.
|
|
211
|
+
xhr.onerror = () =>
|
|
212
|
+
reject(
|
|
213
|
+
sent === 0
|
|
214
|
+
? new PreflightRefusedError(url)
|
|
215
|
+
: new Error(`${url} is unreachable`),
|
|
216
|
+
);
|
|
217
|
+
xhr.ontimeout = () => reject(new UploadTimeoutError(url, ms, size));
|
|
218
|
+
xhr.send(form);
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* The file's own hash out of the IPFS host's `add` answer: one JSON object
|
|
224
|
+
* per line; the line named like the file, else the first.
|
|
225
|
+
*
|
|
226
|
+
* @param {string} text
|
|
227
|
+
* @param {string} name
|
|
228
|
+
* @returns {string}
|
|
229
|
+
*/
|
|
230
|
+
export function hashFromAddAnswer(text, name) {
|
|
231
|
+
let entries;
|
|
232
|
+
try {
|
|
233
|
+
entries = text
|
|
234
|
+
.split("\n")
|
|
235
|
+
.filter((line) => line.trim())
|
|
236
|
+
.map((line) => JSON.parse(line));
|
|
237
|
+
} catch {
|
|
238
|
+
throw new Error("Aleph's IPFS host answered something that is not JSON");
|
|
239
|
+
}
|
|
240
|
+
const body = entries.find((entry) => entry?.Name === name) ?? entries[0];
|
|
241
|
+
if (!body?.Hash) throw new Error("Aleph's IPFS host returned no Hash");
|
|
242
|
+
return String(body.Hash);
|
|
243
|
+
}
|
package/lib/backends/aleph.js
CHANGED
|
@@ -42,10 +42,19 @@
|
|
|
42
42
|
* @requires ./types.js - the backend contract
|
|
43
43
|
*/
|
|
44
44
|
|
|
45
|
-
/* global FormData */
|
|
46
|
-
|
|
47
45
|
import { defineBackend, handleId, BackendError } from "./types.js";
|
|
48
46
|
import { fetchFromGateways } from "../gateway-fetch.js";
|
|
47
|
+
import {
|
|
48
|
+
ALEPH_API_FORM_HEADROOM,
|
|
49
|
+
ALEPH_API_SIGNED_LIMIT,
|
|
50
|
+
ALEPH_API_UNSIGNED_LIMIT,
|
|
51
|
+
EndpointGoneError,
|
|
52
|
+
PreflightRefusedError,
|
|
53
|
+
hashFromAddAnswer,
|
|
54
|
+
postFile,
|
|
55
|
+
unixfsCid,
|
|
56
|
+
uploadTimeout,
|
|
57
|
+
} from "./aleph-upload.js";
|
|
49
58
|
|
|
50
59
|
/**
|
|
51
60
|
* Aleph's own, and only Aleph's own.
|
|
@@ -60,24 +69,39 @@ import { fetchFromGateways } from "../gateway-fetch.js";
|
|
|
60
69
|
export const ALEPH_GATEWAYS = Object.freeze(["https://ipfs.aleph.cloud/ipfs"]);
|
|
61
70
|
|
|
62
71
|
const ALEPH_INGEST = "https://ipfs.aleph.cloud/api/v0/add";
|
|
72
|
+
/**
|
|
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.
|
|
77
|
+
*/
|
|
78
|
+
export const DEFAULT_ALEPH_API_HOST = "https://api2.aleph.im";
|
|
63
79
|
const DEFAULT_TIMEOUT_MS = 120_000;
|
|
64
80
|
|
|
65
81
|
/**
|
|
66
82
|
* Create an Aleph backend.
|
|
67
83
|
*
|
|
68
84
|
* @param {object} [options]
|
|
69
|
-
* @param {string} [options.ingestUrl] - defaults to Aleph's public IPFS
|
|
85
|
+
* @param {string | null} [options.ingestUrl] - defaults to Aleph's public IPFS
|
|
86
|
+
* host; `null` skips it and uploads through the API node from the start
|
|
87
|
+
* @param {string} [options.apiHost] - an Aleph API node, for when the IPFS host
|
|
88
|
+
* takes no uploads (404); defaults to `api2.aleph.im`
|
|
70
89
|
* @param {string[]} [options.gateways] - retrieval, tried in order
|
|
71
|
-
* @param {number} [options.timeout] - per request, in ms
|
|
90
|
+
* @param {number} [options.timeout] - per request, in ms; an upload gets its
|
|
91
|
+
* bytes' own time on top (`aleph-upload.js`)
|
|
72
92
|
* @param {(cid: string, meta?: object) => Promise<any>} [options.pin] - make it
|
|
73
93
|
* stick. Aleph retains what a wallet-signed STORE message names, so this is
|
|
74
94
|
* the caller's wallet, not ours. Supplying it is what declares `pinByCid`;
|
|
75
95
|
* without it the driver stores and says plainly that it does not retain.
|
|
96
|
+
* A pin with a `sign(cid, meta)` method (`createAlephPin` has one) lets an
|
|
97
|
+
* upload through the API node carry its STORE message: the node keeps the
|
|
98
|
+
* file in the same call, up to 1 GiB, and the handle says `retained`.
|
|
76
99
|
* @param {typeof fetch} [options.fetch] - for tests, or a browser with its own
|
|
77
100
|
* @returns {import("./types.js").StorageBackend}
|
|
78
101
|
*/
|
|
79
102
|
export function createAlephBackend(options = {}) {
|
|
80
|
-
const ingestUrl = options.ingestUrl || ALEPH_INGEST;
|
|
103
|
+
const ingestUrl = options.ingestUrl === null ? null : options.ingestUrl || ALEPH_INGEST;
|
|
104
|
+
const apiHost = (options.apiHost || DEFAULT_ALEPH_API_HOST).replace(/\/+$/, "");
|
|
81
105
|
const gateways = options.gateways || ALEPH_GATEWAYS;
|
|
82
106
|
const timeout = options.timeout ?? DEFAULT_TIMEOUT_MS;
|
|
83
107
|
const pin = options.pin;
|
|
@@ -90,6 +114,46 @@ export function createAlephBackend(options = {}) {
|
|
|
90
114
|
);
|
|
91
115
|
}
|
|
92
116
|
|
|
117
|
+
/** The IPFS host answered 404 or 405 once: it takes no uploads, not asked again. */
|
|
118
|
+
let ingestGone = false;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The IPFS host's `add`. With progress once; a browser whose preflight the
|
|
122
|
+
* host refuses (it allows its own console only, aleph-im/pyaleph#1280) sends
|
|
123
|
+
* again without, and the host is not asked for progress a second time.
|
|
124
|
+
*/
|
|
125
|
+
let ingestUnmeasured = false;
|
|
126
|
+
async function viaIngest({ bytes, name, contentType, onProgress }) {
|
|
127
|
+
const ms = uploadTimeout(bytes.length, timeout);
|
|
128
|
+
const post = (progress) =>
|
|
129
|
+
postFile({
|
|
130
|
+
url: ingestUrl,
|
|
131
|
+
bytes,
|
|
132
|
+
name,
|
|
133
|
+
contentType,
|
|
134
|
+
onProgress: progress,
|
|
135
|
+
timeout: ms,
|
|
136
|
+
fetch: doFetch,
|
|
137
|
+
});
|
|
138
|
+
let answer;
|
|
139
|
+
if (onProgress && !ingestUnmeasured) {
|
|
140
|
+
try {
|
|
141
|
+
answer = await post(onProgress);
|
|
142
|
+
} catch (error) {
|
|
143
|
+
if (!(error instanceof PreflightRefusedError)) throw error;
|
|
144
|
+
ingestUnmeasured = true;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (!answer) answer = await post(undefined);
|
|
148
|
+
if (answer.status < 200 || answer.status >= 300) {
|
|
149
|
+
throw new BackendError(
|
|
150
|
+
"UNSUPPORTED",
|
|
151
|
+
`Aleph ingest answered ${answer.status} ${answer.text.slice(0, 200)}`,
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
return hashFromAddAnswer(answer.text, name);
|
|
155
|
+
}
|
|
156
|
+
|
|
93
157
|
const backend = {
|
|
94
158
|
name: "aleph",
|
|
95
159
|
|
|
@@ -123,62 +187,96 @@ export function createAlephBackend(options = {}) {
|
|
|
123
187
|
|
|
124
188
|
async putBlob(bytes, meta = {}) {
|
|
125
189
|
const name = meta.name || "blob";
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
190
|
+
const contentType = meta.contentType || "application/octet-stream";
|
|
191
|
+
const onProgress = typeof meta.onProgress === "function" ? meta.onProgress : undefined;
|
|
192
|
+
const handle = {
|
|
193
|
+
backend: "aleph",
|
|
194
|
+
size: bytes.length,
|
|
195
|
+
...(meta.name ? { name: meta.name } : {}),
|
|
196
|
+
};
|
|
132
197
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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) {
|
|
202
|
+
try {
|
|
203
|
+
const id = await viaIngest({ bytes, name, contentType, onProgress });
|
|
204
|
+
// Said out loud in the handle, because "stored" and "kept" are not
|
|
205
|
+
// the same thing here and a caller should not have to read this
|
|
206
|
+
// file. An upload here is never kept by itself, `pin` or not:
|
|
207
|
+
// keeping is a STORE, which `pinCid` sends.
|
|
208
|
+
return { ...handle, id, retained: false };
|
|
209
|
+
} catch (error) {
|
|
210
|
+
if (!(error instanceof EndpointGoneError)) throw error;
|
|
211
|
+
ingestGone = true;
|
|
212
|
+
}
|
|
142
213
|
}
|
|
143
214
|
|
|
144
|
-
|
|
215
|
+
// The API node. With a signed STORE message beside the file the node
|
|
216
|
+
// keeps it in the same call; without one it takes at most 25 MiB and
|
|
217
|
+
// keeps it for a day, until a STORE names it.
|
|
218
|
+
const signed = typeof pin?.sign === "function";
|
|
219
|
+
// The node measures the request body, the form around the file included.
|
|
220
|
+
const limit =
|
|
221
|
+
(signed ? ALEPH_API_SIGNED_LIMIT : ALEPH_API_UNSIGNED_LIMIT) - ALEPH_API_FORM_HEADROOM;
|
|
222
|
+
if (bytes.length > limit) {
|
|
223
|
+
const error = new BackendError(
|
|
224
|
+
"UNSUPPORTED",
|
|
225
|
+
signed
|
|
226
|
+
? `Aleph's API takes at most ${limit} bytes in one upload; this blob has ${bytes.length}`
|
|
227
|
+
: `Aleph's IPFS host takes no uploads, and its API takes at most ${limit} bytes without a signed STORE message; this blob has ${bytes.length}. Pass a pin with \`sign\` (createAlephPin) to upload up to 1 GiB`,
|
|
228
|
+
);
|
|
229
|
+
// For a caller that wants to say it in its own words.
|
|
230
|
+
error.reason = signed ? "signed-limit" : "unsigned-limit";
|
|
231
|
+
error.limit = limit;
|
|
232
|
+
error.size = bytes.length;
|
|
233
|
+
throw error;
|
|
234
|
+
}
|
|
235
|
+
const fields = {};
|
|
236
|
+
let itemHash;
|
|
237
|
+
if (signed) {
|
|
238
|
+
// The node checks, after pinning, that the CID it made is the one the
|
|
239
|
+
// message names: so the CID is computed here first, the way the node
|
|
240
|
+
// computes it (aleph-upload.js `unixfsCid`).
|
|
241
|
+
const cid = await unixfsCid(bytes);
|
|
242
|
+
const message = await pin.sign(cid, meta);
|
|
243
|
+
itemHash = message.item_hash;
|
|
244
|
+
fields.metadata = JSON.stringify({ message, sync: true });
|
|
245
|
+
}
|
|
246
|
+
const { status, text } = await postFile({
|
|
247
|
+
url: `${apiHost}/api/v0/ipfs/add_file`,
|
|
248
|
+
bytes,
|
|
249
|
+
name,
|
|
250
|
+
fields,
|
|
251
|
+
contentType,
|
|
252
|
+
onProgress,
|
|
253
|
+
timeout: uploadTimeout(bytes.length, timeout),
|
|
254
|
+
fetch: doFetch,
|
|
255
|
+
});
|
|
256
|
+
if (status < 200 || status >= 300) {
|
|
145
257
|
throw new BackendError(
|
|
146
258
|
"UNSUPPORTED",
|
|
147
|
-
`Aleph
|
|
259
|
+
`Aleph's API answered ${status} ${text.slice(0, 200)}`,
|
|
148
260
|
);
|
|
149
261
|
}
|
|
150
|
-
|
|
151
|
-
// `add` answers one JSON object per line. A name with a path in it — and
|
|
152
|
-
// backupDatabase gives every file one — makes the host wrap the file in
|
|
153
|
-
// directories and add a line for each (measured 2026-09-17). The id we
|
|
154
|
-
// keep is the file's own, never a wrapper's.
|
|
155
|
-
let entries;
|
|
262
|
+
let body;
|
|
156
263
|
try {
|
|
157
|
-
|
|
158
|
-
.split("\n")
|
|
159
|
-
.filter((line) => line.trim())
|
|
160
|
-
.map((line) => JSON.parse(line));
|
|
264
|
+
body = JSON.parse(text);
|
|
161
265
|
} catch {
|
|
162
|
-
throw new BackendError("UNSUPPORTED", "Aleph
|
|
266
|
+
throw new BackendError("UNSUPPORTED", "Aleph's API answered something that is not JSON");
|
|
163
267
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
throw new BackendError("UNSUPPORTED", "Aleph ingest returned no Hash");
|
|
268
|
+
if (typeof body?.hash !== "string" || !body.hash) {
|
|
269
|
+
throw new BackendError("UNSUPPORTED", "Aleph's API returned no hash");
|
|
167
270
|
}
|
|
168
|
-
|
|
271
|
+
// A signed upload answers with the message's status (200 processed,
|
|
272
|
+
// 202 pending); a plain one is stored, not kept, as on the IPFS host.
|
|
273
|
+
const messageStatus = signed ? (status === 200 ? "processed" : "pending") : undefined;
|
|
169
274
|
return {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
...(meta.name ? { name: meta.name } : {}),
|
|
176
|
-
// Said out loud in the handle, because "stored" and "kept" are not the
|
|
177
|
-
// same thing here and a caller should not have to read this file. An
|
|
178
|
-
// upload is never kept by itself, `pin` or not: keeping is a STORE,
|
|
179
|
-
// which `pinCid` sends. (Up to 0.16.1 this said `Boolean(pin)`, a
|
|
180
|
-
// promise no call had kept.)
|
|
181
|
-
retained: false,
|
|
275
|
+
...handle,
|
|
276
|
+
id: body.hash,
|
|
277
|
+
retained: messageStatus === "processed",
|
|
278
|
+
...(messageStatus ? { status: messageStatus } : {}),
|
|
279
|
+
...(itemHash ? { itemHash } : {}),
|
|
182
280
|
};
|
|
183
281
|
},
|
|
184
282
|
|
package/package.json
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@le-space/orbitdb-storage-bridge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.27.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
|
-
"svelte": "dist/components/",
|
|
7
6
|
"type": "module",
|
|
8
7
|
"exports": {
|
|
9
8
|
".": "./lib/orbitdb-storacha-bridge.js",
|
|
@@ -22,6 +21,7 @@
|
|
|
22
21
|
"./backends/storacha": "./lib/backends/storacha.js",
|
|
23
22
|
"./backends/aleph": "./lib/backends/aleph.js",
|
|
24
23
|
"./backends/aleph-pin": "./lib/backends/aleph-pin.js",
|
|
24
|
+
"./backends/aleph-upload": "./lib/backends/aleph-upload.js",
|
|
25
25
|
"./backends/pinata": "./lib/backends/pinata.js",
|
|
26
26
|
"./backends/lighthouse": "./lib/backends/lighthouse.js",
|
|
27
27
|
"./backends/mirror": "./lib/backends/mirror.js",
|
|
@@ -29,8 +29,6 @@
|
|
|
29
29
|
"./backends/choose": "./lib/backends/choose.js",
|
|
30
30
|
"./backends/resolve": "./lib/backends/resolve.js",
|
|
31
31
|
"./memory-courier": "./lib/memory-courier.js",
|
|
32
|
-
"./StorachaIntegration.svelte": "./dist/components/StorachaIntegration.svelte",
|
|
33
|
-
"./StorachaAuth.svelte": "./dist/components/StorachaAuth.svelte",
|
|
34
32
|
"./storacha-backup": "./dist/components/storacha-backup.js",
|
|
35
33
|
"./components/*": "./dist/components/*"
|
|
36
34
|
},
|
|
@@ -76,6 +74,8 @@
|
|
|
76
74
|
"files": [
|
|
77
75
|
"lib",
|
|
78
76
|
"dist",
|
|
77
|
+
"!dist/components/StorachaAuth.svelte",
|
|
78
|
+
"!dist/components/StorachaIntegration.svelte",
|
|
79
79
|
"!dist/components/StorachaTest.svelte",
|
|
80
80
|
"!dist/components/StorachaTestWithReplication.svelte",
|
|
81
81
|
"!dist/components/StorachaTestWithWebAuthn.svelte",
|
|
@@ -108,6 +108,7 @@
|
|
|
108
108
|
"cbor-web": "^10.0.11",
|
|
109
109
|
"datastore-level": "^13.0.1",
|
|
110
110
|
"helia": "^7.1.15",
|
|
111
|
+
"ipfs-unixfs-importer": "^17.1.1",
|
|
111
112
|
"libp2p": "^3.3.6",
|
|
112
113
|
"multiformats": "^14.0.5"
|
|
113
114
|
},
|
|
@@ -142,9 +143,9 @@
|
|
|
142
143
|
"prettier-plugin-tailwindcss": "^0.7.0"
|
|
143
144
|
},
|
|
144
145
|
"peerDependencies": {
|
|
146
|
+
"@storacha/client": "^2.0.0",
|
|
145
147
|
"lucide-svelte": ">=0.400.0 <1.0.0",
|
|
146
|
-
"svelte": "^3.0.0 || ^4.0.0 || ^5.0.0"
|
|
147
|
-
"@storacha/client": "^2.0.0"
|
|
148
|
+
"svelte": "^3.0.0 || ^4.0.0 || ^5.0.0"
|
|
148
149
|
},
|
|
149
150
|
"engines": {
|
|
150
151
|
"node": ">=22.0.0"
|