@ipab/ipfs-store 0.0.0-stage → 0.1.0-rc.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/LICENSE +21 -0
- package/README.md +245 -2
- package/dist/configuration.d.ts +21 -0
- package/dist/configuration.d.ts.map +1 -0
- package/dist/configuration.js +62 -0
- package/dist/configuration.js.map +1 -0
- package/dist/dag.d.ts +49 -0
- package/dist/dag.d.ts.map +1 -0
- package/dist/dag.js +216 -0
- package/dist/dag.js.map +1 -0
- package/dist/errors.d.ts +6 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +15 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +417 -0
- package/dist/index.js.map +1 -0
- package/dist/kubo.d.ts +38 -0
- package/dist/kubo.d.ts.map +1 -0
- package/dist/kubo.js +189 -0
- package/dist/kubo.js.map +1 -0
- package/dist/node.d.ts +11 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +60 -0
- package/dist/node.js.map +1 -0
- package/dist/retention.d.ts +44 -0
- package/dist/retention.d.ts.map +1 -0
- package/dist/retention.js +328 -0
- package/dist/retention.js.map +1 -0
- package/dist/storage.d.ts +28 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +161 -0
- package/dist/storage.js.map +1 -0
- package/dist/store.d.ts +71 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +412 -0
- package/dist/store.js.map +1 -0
- package/package.json +72 -3
- package/src/configuration.ts +93 -0
- package/src/dag.ts +281 -0
- package/src/errors.ts +25 -0
- package/src/index.ts +520 -0
- package/src/kubo.ts +293 -0
- package/src/node.ts +78 -0
- package/src/retention.ts +398 -0
- package/src/storage.ts +181 -0
- package/src/store.ts +577 -0
package/src/store.ts
ADDED
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
import { CID } from "multiformats/cid";
|
|
2
|
+
import { unixfs } from "@helia/unixfs";
|
|
3
|
+
import type {
|
|
4
|
+
IpfsAccess,
|
|
5
|
+
IpfsEntry,
|
|
6
|
+
IpfsNode,
|
|
7
|
+
} from "@silvermoon-work/ipfs-access";
|
|
8
|
+
import type { ExportContentOptions, UnixFSEntry } from "ipfs-unixfs-exporter";
|
|
9
|
+
import {
|
|
10
|
+
createIpfsGatewayClient,
|
|
11
|
+
contentStore,
|
|
12
|
+
openContent,
|
|
13
|
+
prepareContent,
|
|
14
|
+
withGatewayHints,
|
|
15
|
+
type GatewayClient,
|
|
16
|
+
type IpfsClient,
|
|
17
|
+
} from "./index.js";
|
|
18
|
+
import {
|
|
19
|
+
ContentBlocks,
|
|
20
|
+
defaultContentLimits,
|
|
21
|
+
type ContentLimits,
|
|
22
|
+
} from "./dag.js";
|
|
23
|
+
import { SharedContent } from "./storage.js";
|
|
24
|
+
import { ContentError } from "./errors.js";
|
|
25
|
+
import { createHash } from "node:crypto";
|
|
26
|
+
import { homedir } from "node:os";
|
|
27
|
+
import { join } from "node:path";
|
|
28
|
+
import { KuboContent } from "./kubo.js";
|
|
29
|
+
import { Retention } from "./retention.js";
|
|
30
|
+
import {
|
|
31
|
+
validateConfiguration,
|
|
32
|
+
type IpfsStoreOptions,
|
|
33
|
+
type StoreConfiguration,
|
|
34
|
+
} from "./configuration.js";
|
|
35
|
+
export type { IpfsStoreOptions, StoreConfiguration } from "./configuration.js";
|
|
36
|
+
export interface IpfsReadOptions {
|
|
37
|
+
signal?: AbortSignal;
|
|
38
|
+
hints?: readonly string[];
|
|
39
|
+
allowGateway?: (url: URL) => boolean;
|
|
40
|
+
}
|
|
41
|
+
export type UnixfsNode = Extract<
|
|
42
|
+
UnixFSEntry,
|
|
43
|
+
{ type: "file" | "raw" | "directory" }
|
|
44
|
+
>;
|
|
45
|
+
|
|
46
|
+
/** Verified IPFS/UnixFS content only. Configuration and source ownership belong to the host. */
|
|
47
|
+
export class IpfsStore implements IpfsClient, IpfsAccess {
|
|
48
|
+
readonly configuration: StoreConfiguration;
|
|
49
|
+
readonly limits: Readonly<ContentLimits>;
|
|
50
|
+
private readonly lifetime = new AbortController();
|
|
51
|
+
private readonly retainedBlocks = new Map<string, Uint8Array>();
|
|
52
|
+
private readonly retentionBudget: { bytes: number; limit: number };
|
|
53
|
+
private readonly gateway: GatewayClient | undefined;
|
|
54
|
+
private backend: SharedContent | ContentBlocks | KuboContent;
|
|
55
|
+
private retention?: Retention;
|
|
56
|
+
private retentionStore?: IpfsStore;
|
|
57
|
+
private coordinated = true;
|
|
58
|
+
private source: IpfsClient | undefined;
|
|
59
|
+
private closing?: Promise<void>;
|
|
60
|
+
private ownsBackend = true;
|
|
61
|
+
private upstream?: AbortSignal;
|
|
62
|
+
constructor(options: IpfsStoreOptions) {
|
|
63
|
+
this.configuration = validateConfiguration(options);
|
|
64
|
+
const config = this.configuration;
|
|
65
|
+
this.limits = Object.freeze({ ...defaultContentLimits, ...config.limits });
|
|
66
|
+
for (const [key, value] of Object.entries(this.limits))
|
|
67
|
+
if (
|
|
68
|
+
!Object.hasOwn(defaultContentLimits, key) ||
|
|
69
|
+
!Number.isSafeInteger(value) ||
|
|
70
|
+
value <= 0
|
|
71
|
+
)
|
|
72
|
+
throw new Error("Invalid IPFS content capacity.");
|
|
73
|
+
this.retentionBudget = { bytes: 0, limit: this.limits.bytes };
|
|
74
|
+
this.gateway =
|
|
75
|
+
config.backend === "local" && config.gateway
|
|
76
|
+
? createIpfsGatewayClient(config.gateway, { allowPath: true })
|
|
77
|
+
: undefined;
|
|
78
|
+
this.source = this.gateway;
|
|
79
|
+
this.backend =
|
|
80
|
+
config.backend === "local"
|
|
81
|
+
? new SharedContent(config.dataDir, this.source, this.limits.blockBytes)
|
|
82
|
+
: new KuboContent(
|
|
83
|
+
config.apiUrl,
|
|
84
|
+
this.limits.blockBytes,
|
|
85
|
+
config.timeoutMs,
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
/** Adapt a caller-owned CAR or block port without reading configuration or owning its close. */
|
|
89
|
+
static fromClient(
|
|
90
|
+
client: IpfsClient,
|
|
91
|
+
limits: Partial<ContentLimits> = {},
|
|
92
|
+
dataDir?: string,
|
|
93
|
+
): IpfsStore {
|
|
94
|
+
const value: unknown = client;
|
|
95
|
+
if (
|
|
96
|
+
!value ||
|
|
97
|
+
typeof value !== "object" ||
|
|
98
|
+
!("block" in value) ||
|
|
99
|
+
!value.block ||
|
|
100
|
+
typeof value.block !== "object" ||
|
|
101
|
+
!("get" in value.block) ||
|
|
102
|
+
typeof value.block.get !== "function"
|
|
103
|
+
)
|
|
104
|
+
throw new Error("An IPFS block port is required.");
|
|
105
|
+
const store = new IpfsStore({ dataDir: dataDir ?? ".", limits });
|
|
106
|
+
store.source = client;
|
|
107
|
+
store.coordinated = false;
|
|
108
|
+
store.backend = dataDir
|
|
109
|
+
? new SharedContent(dataDir, client, store.limits.blockBytes)
|
|
110
|
+
: new ContentBlocks(client, store.limits);
|
|
111
|
+
return store;
|
|
112
|
+
}
|
|
113
|
+
get retainedBytes(): number {
|
|
114
|
+
return this.retentionBudget.bytes;
|
|
115
|
+
}
|
|
116
|
+
get supportsRetention(): boolean {
|
|
117
|
+
return this.coordinated;
|
|
118
|
+
}
|
|
119
|
+
/** Explicit host read-through port for legacy local content; never used by Kubo. */
|
|
120
|
+
withLocalSource(source: IpfsClient): IpfsStore {
|
|
121
|
+
if (this.configuration.backend !== "local" || !this.supportsRetention)
|
|
122
|
+
throw new ContentError(
|
|
123
|
+
"read_only",
|
|
124
|
+
"Only coordinated local stores accept a local source.",
|
|
125
|
+
);
|
|
126
|
+
const store = new IpfsStore(this.configuration);
|
|
127
|
+
store.source = source;
|
|
128
|
+
store.backend = new SharedContent(
|
|
129
|
+
this.configuration.dataDir,
|
|
130
|
+
source,
|
|
131
|
+
this.limits.blockBytes,
|
|
132
|
+
);
|
|
133
|
+
return store;
|
|
134
|
+
}
|
|
135
|
+
/** Independent reader budget/lifetime; closing it never closes the shared backing store. */
|
|
136
|
+
scope(limits: Partial<ContentLimits> = {}): IpfsStore {
|
|
137
|
+
const effective = { ...this.limits, ...limits };
|
|
138
|
+
for (const key of Object.keys(this.limits) as (keyof ContentLimits)[])
|
|
139
|
+
effective[key] = Math.min(effective[key], this.limits[key]);
|
|
140
|
+
const reader = new IpfsStore({ dataDir: ".", limits: effective });
|
|
141
|
+
reader.backend = this.backend;
|
|
142
|
+
reader.source = this.source;
|
|
143
|
+
reader.ownsBackend = false;
|
|
144
|
+
reader.upstream = this.signal({});
|
|
145
|
+
reader.coordinated = this.coordinated;
|
|
146
|
+
Object.defineProperty(reader, "configuration", {
|
|
147
|
+
value: this.configuration,
|
|
148
|
+
});
|
|
149
|
+
reader.retentionStore = this;
|
|
150
|
+
return reader;
|
|
151
|
+
}
|
|
152
|
+
private signal(options: IpfsReadOptions): AbortSignal {
|
|
153
|
+
return AbortSignal.any([
|
|
154
|
+
this.lifetime.signal,
|
|
155
|
+
...(this.upstream ? [this.upstream] : []),
|
|
156
|
+
...(options.signal ? [options.signal] : []),
|
|
157
|
+
]);
|
|
158
|
+
}
|
|
159
|
+
private client(options: IpfsReadOptions): IpfsClient {
|
|
160
|
+
if (this.backend instanceof KuboContent) return this.backend;
|
|
161
|
+
if (!options.hints?.length || !options.allowGateway) return this.backend;
|
|
162
|
+
const source = withGatewayHints(
|
|
163
|
+
this.source ?? {
|
|
164
|
+
block: {
|
|
165
|
+
get: () => Promise.reject(new Error("Missing local content block.")),
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
options.hints,
|
|
169
|
+
options.allowGateway,
|
|
170
|
+
);
|
|
171
|
+
return {
|
|
172
|
+
block: {
|
|
173
|
+
get: async (cid, request) => {
|
|
174
|
+
return this.backend.readBlock(cid, request, source);
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
private access(options: IpfsReadOptions) {
|
|
180
|
+
return {
|
|
181
|
+
signal: this.signal(options),
|
|
182
|
+
retainedBlocks: this.retainedBlocks,
|
|
183
|
+
retentionBudget: this.retentionBudget,
|
|
184
|
+
maxBlockBytes: this.limits.blockBytes,
|
|
185
|
+
maxDagBlocks: this.limits.blocks,
|
|
186
|
+
depth: this.limits.depth,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
readonly block = {
|
|
190
|
+
get: async (
|
|
191
|
+
cid: string,
|
|
192
|
+
options: { signal?: AbortSignal } = {},
|
|
193
|
+
): Promise<Uint8Array> => {
|
|
194
|
+
const access = this.access(options);
|
|
195
|
+
access.signal.throwIfAborted();
|
|
196
|
+
const key = CID.parse(cid).toV1().toString();
|
|
197
|
+
const retained = this.retainedBlocks.get(key);
|
|
198
|
+
if (retained) return Uint8Array.from(retained);
|
|
199
|
+
// Low-level reads retain and verify through the same bounded content path as UnixFS reads.
|
|
200
|
+
for await (const bytes of contentStore(this.backend, access).get(
|
|
201
|
+
CID.parse(cid),
|
|
202
|
+
))
|
|
203
|
+
return Uint8Array.from(bytes);
|
|
204
|
+
throw new Error("Missing local content block.");
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
async put(
|
|
208
|
+
cid: CID,
|
|
209
|
+
bytes: Uint8Array,
|
|
210
|
+
options: IpfsReadOptions = {},
|
|
211
|
+
): Promise<void> {
|
|
212
|
+
await this.backend.put(cid, bytes, { signal: this.signal(options) });
|
|
213
|
+
}
|
|
214
|
+
private coordinator(): Retention {
|
|
215
|
+
this.signal({}).throwIfAborted();
|
|
216
|
+
if (!this.supportsRetention)
|
|
217
|
+
throw new ContentError(
|
|
218
|
+
"read_only",
|
|
219
|
+
"Source adapters do not support coordinated retention.",
|
|
220
|
+
);
|
|
221
|
+
if (this.retentionStore) return this.retentionStore.coordinator();
|
|
222
|
+
if (!this.retention) {
|
|
223
|
+
const config = this.configuration;
|
|
224
|
+
const directory =
|
|
225
|
+
config.coordinationDir ??
|
|
226
|
+
(config.backend === "local"
|
|
227
|
+
? join(config.dataDir, "retention")
|
|
228
|
+
: join(
|
|
229
|
+
homedir(),
|
|
230
|
+
".silvermoon.work",
|
|
231
|
+
"retention",
|
|
232
|
+
createHash("sha256").update(config.apiUrl).digest("hex"),
|
|
233
|
+
));
|
|
234
|
+
const kubo =
|
|
235
|
+
this.backend instanceof KuboContent ? this.backend : undefined;
|
|
236
|
+
this.retention = new Retention(directory, {
|
|
237
|
+
prepare: async (root, signal) => {
|
|
238
|
+
await prepareContent(this.backend, root, {
|
|
239
|
+
signal: signal ?? this.signal({}),
|
|
240
|
+
maxBlockBytes: this.limits.blockBytes,
|
|
241
|
+
maxDagBlocks: this.limits.blocks,
|
|
242
|
+
depth: this.limits.depth,
|
|
243
|
+
retentionBudget: { bytes: 0, limit: this.limits.bytes },
|
|
244
|
+
retainedBlocks: new Map(),
|
|
245
|
+
});
|
|
246
|
+
},
|
|
247
|
+
...(kubo
|
|
248
|
+
? {
|
|
249
|
+
status: (root, signal) =>
|
|
250
|
+
kubo.pinStatus(root, signal ? { signal } : {}),
|
|
251
|
+
pin: (root, name, signal) =>
|
|
252
|
+
kubo.pin(root, name, signal ? { signal } : {}),
|
|
253
|
+
unpin: (root, signal) =>
|
|
254
|
+
kubo.unpin(root, signal ? { signal } : {}),
|
|
255
|
+
}
|
|
256
|
+
: {}),
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
return this.retention;
|
|
260
|
+
}
|
|
261
|
+
registerOwner(owner: string, roots: readonly string[]): void {
|
|
262
|
+
this.signal({}).throwIfAborted();
|
|
263
|
+
this.coordinator().register(owner, roots);
|
|
264
|
+
}
|
|
265
|
+
async retain(
|
|
266
|
+
owner: string,
|
|
267
|
+
roots: readonly string[],
|
|
268
|
+
options: IpfsReadOptions = {},
|
|
269
|
+
): Promise<void> {
|
|
270
|
+
return this.coordinator().retain(owner, roots, this.signal(options));
|
|
271
|
+
}
|
|
272
|
+
async confirmRetained(
|
|
273
|
+
owner: string,
|
|
274
|
+
roots: readonly string[],
|
|
275
|
+
options: IpfsReadOptions = {},
|
|
276
|
+
): Promise<void> {
|
|
277
|
+
return this.coordinator().confirm(owner, roots, this.signal(options));
|
|
278
|
+
}
|
|
279
|
+
async recoverOwner(
|
|
280
|
+
owner: string,
|
|
281
|
+
roots: readonly string[] | (() => readonly string[]),
|
|
282
|
+
options: IpfsReadOptions & { isIdle?: () => boolean } = {},
|
|
283
|
+
): Promise<void> {
|
|
284
|
+
return this.coordinator().recover(
|
|
285
|
+
owner,
|
|
286
|
+
roots,
|
|
287
|
+
this.signal(options),
|
|
288
|
+
options.isIdle,
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
async reconcile(
|
|
292
|
+
owner: string,
|
|
293
|
+
roots: readonly string[] | (() => readonly string[]),
|
|
294
|
+
options: IpfsReadOptions = {},
|
|
295
|
+
): Promise<number> {
|
|
296
|
+
return this.coordinator().reconcile(owner, roots, this.signal(options));
|
|
297
|
+
}
|
|
298
|
+
async pin(root: CID | string, options: IpfsReadOptions = {}): Promise<void> {
|
|
299
|
+
return this.coordinator().pin(root.toString(), this.signal(options));
|
|
300
|
+
}
|
|
301
|
+
async unpin(
|
|
302
|
+
root: CID | string,
|
|
303
|
+
options: IpfsReadOptions = {},
|
|
304
|
+
): Promise<void> {
|
|
305
|
+
return this.coordinator().unpin(root.toString(), this.signal(options));
|
|
306
|
+
}
|
|
307
|
+
pins(): readonly string[] {
|
|
308
|
+
return this.coordinator().pins();
|
|
309
|
+
}
|
|
310
|
+
async stageBlock(
|
|
311
|
+
session: string,
|
|
312
|
+
cid: CID,
|
|
313
|
+
bytes: Uint8Array,
|
|
314
|
+
options: IpfsReadOptions = {},
|
|
315
|
+
): Promise<void> {
|
|
316
|
+
const signal = this.signal(options);
|
|
317
|
+
// Atomic raw-leaf pins protect even a parent arriving before its children.
|
|
318
|
+
// Kubo keys block bytes by multihash; the alias neither reencodes nor duplicates the payload.
|
|
319
|
+
const staged =
|
|
320
|
+
this.backend instanceof KuboContent
|
|
321
|
+
? CID.createV1(0x55, cid.multihash)
|
|
322
|
+
: cid;
|
|
323
|
+
return this.coordinator().stage(
|
|
324
|
+
session,
|
|
325
|
+
staged.toString(),
|
|
326
|
+
async (name, claim) => {
|
|
327
|
+
if (this.backend instanceof KuboContent) {
|
|
328
|
+
await this.backend.put(staged, bytes, { signal }, claim);
|
|
329
|
+
if (claim)
|
|
330
|
+
await this.backend.pin(staged.toString(), name, { signal });
|
|
331
|
+
if (cid.code !== staged.code)
|
|
332
|
+
await this.backend.put(cid, bytes, { signal });
|
|
333
|
+
} else {
|
|
334
|
+
await this.backend.put(cid, bytes, { signal });
|
|
335
|
+
}
|
|
336
|
+
},
|
|
337
|
+
signal,
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
async finishImport(
|
|
341
|
+
session: string,
|
|
342
|
+
root: CID | string,
|
|
343
|
+
owner?: string,
|
|
344
|
+
options: IpfsReadOptions = {},
|
|
345
|
+
): Promise<void> {
|
|
346
|
+
return this.coordinator().finish(
|
|
347
|
+
session,
|
|
348
|
+
root.toString(),
|
|
349
|
+
owner,
|
|
350
|
+
this.signal(options),
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
async discardImport(
|
|
354
|
+
session: string,
|
|
355
|
+
options: IpfsReadOptions = {},
|
|
356
|
+
): Promise<void> {
|
|
357
|
+
return this.coordinator().discard(session, this.signal(options));
|
|
358
|
+
}
|
|
359
|
+
async read(
|
|
360
|
+
path: CID | string,
|
|
361
|
+
options: IpfsReadOptions = {},
|
|
362
|
+
): Promise<UnixfsNode> {
|
|
363
|
+
const root = CID.parse(path.toString().split("/")[0]!);
|
|
364
|
+
if (root.code !== 0x55 && root.code !== 0x70)
|
|
365
|
+
throw new ContentError(
|
|
366
|
+
"unsupported_content",
|
|
367
|
+
"Only regular UnixFS files and directories are supported.",
|
|
368
|
+
);
|
|
369
|
+
const node = await openContent(
|
|
370
|
+
this.client(options),
|
|
371
|
+
path.toString(),
|
|
372
|
+
this.access(options),
|
|
373
|
+
);
|
|
374
|
+
if (node.type === "directory") {
|
|
375
|
+
const ownerSignal = this.signal(options);
|
|
376
|
+
return {
|
|
377
|
+
...node,
|
|
378
|
+
entries: async function* (entryOptions: ExportContentOptions = {}) {
|
|
379
|
+
const signal = AbortSignal.any([
|
|
380
|
+
ownerSignal,
|
|
381
|
+
...(entryOptions.signal ? [entryOptions.signal] : []),
|
|
382
|
+
]);
|
|
383
|
+
signal.throwIfAborted();
|
|
384
|
+
for await (const entry of node.entries({ ...entryOptions, signal })) {
|
|
385
|
+
signal.throwIfAborted();
|
|
386
|
+
yield entry;
|
|
387
|
+
}
|
|
388
|
+
signal.throwIfAborted();
|
|
389
|
+
},
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
if (
|
|
393
|
+
node.type !== "raw" &&
|
|
394
|
+
!(node.type === "file" && ["file", "raw"].includes(node.unixfs.type))
|
|
395
|
+
)
|
|
396
|
+
throw new ContentError(
|
|
397
|
+
"unsupported_content",
|
|
398
|
+
"Only regular UnixFS files and directories are supported.",
|
|
399
|
+
);
|
|
400
|
+
return node;
|
|
401
|
+
}
|
|
402
|
+
async readContent(
|
|
403
|
+
cid: CID,
|
|
404
|
+
options: IpfsReadOptions = {},
|
|
405
|
+
): Promise<IpfsNode> {
|
|
406
|
+
const lifetime = new AbortController();
|
|
407
|
+
const signal = AbortSignal.any([
|
|
408
|
+
lifetime.signal,
|
|
409
|
+
...(options.signal ? [options.signal] : []),
|
|
410
|
+
]);
|
|
411
|
+
const node = await this.read(cid, { ...options, signal });
|
|
412
|
+
if (node.type === "directory") {
|
|
413
|
+
const read = this.read.bind(this);
|
|
414
|
+
const entries = async function* (): AsyncIterable<IpfsEntry> {
|
|
415
|
+
for await (const entry of node.entries()) {
|
|
416
|
+
const target = await read(entry.cid, { ...options, signal });
|
|
417
|
+
yield {
|
|
418
|
+
name: entry.name,
|
|
419
|
+
kind: target.type === "directory" ? "directory" : "file",
|
|
420
|
+
cid: entry.cid,
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
};
|
|
424
|
+
return { kind: "directory", entries: entries() };
|
|
425
|
+
}
|
|
426
|
+
const iterator = node.content()[Symbol.asyncIterator]();
|
|
427
|
+
return {
|
|
428
|
+
kind: "file",
|
|
429
|
+
content: new ReadableStream<Uint8Array>(
|
|
430
|
+
{
|
|
431
|
+
async pull(controller) {
|
|
432
|
+
try {
|
|
433
|
+
const result = await iterator.next();
|
|
434
|
+
if (result.done) controller.close();
|
|
435
|
+
else controller.enqueue(result.value);
|
|
436
|
+
} catch (error) {
|
|
437
|
+
controller.error(error);
|
|
438
|
+
}
|
|
439
|
+
},
|
|
440
|
+
async cancel() {
|
|
441
|
+
lifetime.abort(new Error("IPFS file stream canceled."));
|
|
442
|
+
await iterator.return();
|
|
443
|
+
},
|
|
444
|
+
},
|
|
445
|
+
{ highWaterMark: 0 },
|
|
446
|
+
),
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
async putContent(
|
|
450
|
+
content: ReadableStream<Uint8Array>,
|
|
451
|
+
options: IpfsReadOptions = {},
|
|
452
|
+
): Promise<CID> {
|
|
453
|
+
const signal = this.signal(options);
|
|
454
|
+
signal.throwIfAborted();
|
|
455
|
+
if (!(content instanceof ReadableStream))
|
|
456
|
+
throw new ContentError(
|
|
457
|
+
"unsupported_content",
|
|
458
|
+
"putContent requires a byte stream.",
|
|
459
|
+
);
|
|
460
|
+
const reader = content.getReader();
|
|
461
|
+
let imported = 0;
|
|
462
|
+
const getBlock = this.block.get;
|
|
463
|
+
const putBlock = this.put.bind(this);
|
|
464
|
+
const limits = this.limits;
|
|
465
|
+
const blockstore = {
|
|
466
|
+
get: async function* (cid: CID) {
|
|
467
|
+
yield await getBlock(cid.toString(), { signal });
|
|
468
|
+
},
|
|
469
|
+
put: async (cid: CID, bytes: Uint8Array) => {
|
|
470
|
+
signal.throwIfAborted();
|
|
471
|
+
if (!(bytes instanceof Uint8Array))
|
|
472
|
+
throw new ContentError(
|
|
473
|
+
"unsupported_content",
|
|
474
|
+
"putContent requires bytes.",
|
|
475
|
+
);
|
|
476
|
+
if (bytes.byteLength > limits.blockBytes)
|
|
477
|
+
throw new ContentError(
|
|
478
|
+
"capacity_exceeded",
|
|
479
|
+
"IPFS block byte capacity exceeded.",
|
|
480
|
+
);
|
|
481
|
+
await putBlock(cid, bytes, options);
|
|
482
|
+
return cid;
|
|
483
|
+
},
|
|
484
|
+
has: async (cid: CID) => {
|
|
485
|
+
try {
|
|
486
|
+
await getBlock(cid.toString(), { signal });
|
|
487
|
+
return true;
|
|
488
|
+
} catch {
|
|
489
|
+
return false;
|
|
490
|
+
}
|
|
491
|
+
},
|
|
492
|
+
};
|
|
493
|
+
async function* input() {
|
|
494
|
+
for (;;) {
|
|
495
|
+
signal.throwIfAborted();
|
|
496
|
+
const item = await reader.read();
|
|
497
|
+
if (item.done) return;
|
|
498
|
+
if (!(item.value instanceof Uint8Array))
|
|
499
|
+
throw new ContentError(
|
|
500
|
+
"unsupported_content",
|
|
501
|
+
"putContent requires bytes.",
|
|
502
|
+
);
|
|
503
|
+
imported += item.value.byteLength;
|
|
504
|
+
if (imported > limits.bytes)
|
|
505
|
+
throw new ContentError(
|
|
506
|
+
"capacity_exceeded",
|
|
507
|
+
"IPFS import byte capacity exceeded.",
|
|
508
|
+
);
|
|
509
|
+
for (let offset = 0; offset < item.value.length; offset += 262_144)
|
|
510
|
+
yield item.value.subarray(offset, offset + 262_144);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
try {
|
|
514
|
+
const cid = await unixfs({ blockstore }).addByteStream(input(), {
|
|
515
|
+
signal,
|
|
516
|
+
cidVersion: 1,
|
|
517
|
+
rawLeaves: true,
|
|
518
|
+
blockWriteConcurrency: 1,
|
|
519
|
+
});
|
|
520
|
+
await this.read(cid, options);
|
|
521
|
+
return cid;
|
|
522
|
+
} finally {
|
|
523
|
+
try {
|
|
524
|
+
await reader.cancel();
|
|
525
|
+
} finally {
|
|
526
|
+
reader.releaseLock();
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
async resolveContent(
|
|
531
|
+
cid: CID,
|
|
532
|
+
path: string,
|
|
533
|
+
options: IpfsReadOptions = {},
|
|
534
|
+
): Promise<CID> {
|
|
535
|
+
if (
|
|
536
|
+
typeof path !== "string" ||
|
|
537
|
+
path.includes("\0") ||
|
|
538
|
+
path.includes("\\") ||
|
|
539
|
+
(path !== "" &&
|
|
540
|
+
path.split("/").some((part) => !part || part === "." || part === ".."))
|
|
541
|
+
)
|
|
542
|
+
throw new ContentError(
|
|
543
|
+
"unsupported_content",
|
|
544
|
+
"Expected a directory-relative content path.",
|
|
545
|
+
);
|
|
546
|
+
if (path === "") return (await this.read(cid, options)).cid;
|
|
547
|
+
let target = cid;
|
|
548
|
+
for (const part of path.split("/")) {
|
|
549
|
+
const directory = await this.read(target, options);
|
|
550
|
+
if (directory.type !== "directory")
|
|
551
|
+
throw new ContentError(
|
|
552
|
+
"unsupported_content",
|
|
553
|
+
"Cannot resolve a path through a file.",
|
|
554
|
+
);
|
|
555
|
+
target = (await this.read(`${target.toString()}/${part}`, options)).cid;
|
|
556
|
+
}
|
|
557
|
+
return target;
|
|
558
|
+
}
|
|
559
|
+
prepare(root: CID | string, options: IpfsReadOptions = {}): Promise<void> {
|
|
560
|
+
return prepareContent(
|
|
561
|
+
this.client(options),
|
|
562
|
+
root.toString(),
|
|
563
|
+
this.access(options),
|
|
564
|
+
);
|
|
565
|
+
}
|
|
566
|
+
close(): Promise<void> {
|
|
567
|
+
this.closing ??= (async () => {
|
|
568
|
+
this.lifetime.abort(new Error("IPFS store is closed."));
|
|
569
|
+
if (this.ownsBackend) await this.backend.close();
|
|
570
|
+
if (this.ownsBackend) await this.retention?.close();
|
|
571
|
+
await this.gateway?.close();
|
|
572
|
+
this.retainedBlocks.clear();
|
|
573
|
+
this.retentionBudget.bytes = 0;
|
|
574
|
+
})();
|
|
575
|
+
return this.closing;
|
|
576
|
+
}
|
|
577
|
+
}
|