@ipab/ipfs-store 0.1.0-rc.4 → 0.1.0-rc.6
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 +32 -17
- package/dist/dag.d.ts.map +1 -1
- package/dist/dag.js +8 -2
- package/dist/dag.js.map +1 -1
- package/dist/errors.d.ts +2 -2
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/retention.d.ts +27 -0
- package/dist/retention.d.ts.map +1 -1
- package/dist/retention.js +296 -72
- package/dist/retention.js.map +1 -1
- package/dist/store.d.ts +18 -0
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +57 -2
- package/dist/store.js.map +1 -1
- package/package.json +1 -1
- package/src/dag.ts +13 -2
- package/src/errors.ts +1 -0
- package/src/index.ts +5 -1
- package/src/retention.ts +397 -80
- package/src/store.ts +139 -16
package/src/store.ts
CHANGED
|
@@ -35,6 +35,13 @@ export interface IpfsReadOptions {
|
|
|
35
35
|
hints?: readonly string[];
|
|
36
36
|
allowGateway?: (url: URL) => boolean;
|
|
37
37
|
}
|
|
38
|
+
export interface StagedBlock {
|
|
39
|
+
cid: CID;
|
|
40
|
+
bytes: Uint8Array;
|
|
41
|
+
}
|
|
42
|
+
export interface ImportBlocksOptions extends IpfsReadOptions {
|
|
43
|
+
onRetained?: () => Promise<void> | void;
|
|
44
|
+
}
|
|
38
45
|
export interface StoreUsage {
|
|
39
46
|
cachedBytes: number;
|
|
40
47
|
pinnedBytes: number;
|
|
@@ -234,6 +241,11 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
|
|
|
234
241
|
retainedBlocks: new Map(),
|
|
235
242
|
});
|
|
236
243
|
},
|
|
244
|
+
prepareDirect: async (root, signal) => {
|
|
245
|
+
await this.backend.readBlock(CID.parse(root).toString(), {
|
|
246
|
+
signal: signal ?? this.signal({}),
|
|
247
|
+
});
|
|
248
|
+
},
|
|
237
249
|
});
|
|
238
250
|
}
|
|
239
251
|
return this.retention;
|
|
@@ -242,6 +254,28 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
|
|
|
242
254
|
this.signal({}).throwIfAborted();
|
|
243
255
|
this.coordinator().register(owner, roots);
|
|
244
256
|
}
|
|
257
|
+
registerDirectOwner(owner: string, roots: readonly string[]): void {
|
|
258
|
+
this.signal({}).throwIfAborted();
|
|
259
|
+
this.coordinator().registerDirect(owner, roots);
|
|
260
|
+
}
|
|
261
|
+
async registerOwnerAsync(
|
|
262
|
+
owner: string,
|
|
263
|
+
roots: readonly string[],
|
|
264
|
+
options: IpfsReadOptions = {},
|
|
265
|
+
): Promise<void> {
|
|
266
|
+
return this.coordinator().registerAsync(owner, roots, this.signal(options));
|
|
267
|
+
}
|
|
268
|
+
async registerDirectOwnerAsync(
|
|
269
|
+
owner: string,
|
|
270
|
+
roots: readonly string[],
|
|
271
|
+
options: IpfsReadOptions = {},
|
|
272
|
+
): Promise<void> {
|
|
273
|
+
return this.coordinator().registerDirectAsync(
|
|
274
|
+
owner,
|
|
275
|
+
roots,
|
|
276
|
+
this.signal(options),
|
|
277
|
+
);
|
|
278
|
+
}
|
|
245
279
|
async retain(
|
|
246
280
|
owner: string,
|
|
247
281
|
roots: readonly string[],
|
|
@@ -249,6 +283,13 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
|
|
|
249
283
|
): Promise<void> {
|
|
250
284
|
return this.coordinator().retain(owner, roots, this.signal(options));
|
|
251
285
|
}
|
|
286
|
+
async retainDirect(
|
|
287
|
+
owner: string,
|
|
288
|
+
roots: readonly string[],
|
|
289
|
+
options: IpfsReadOptions = {},
|
|
290
|
+
): Promise<void> {
|
|
291
|
+
return this.coordinator().retainDirect(owner, roots, this.signal(options));
|
|
292
|
+
}
|
|
252
293
|
async confirmRetained(
|
|
253
294
|
owner: string,
|
|
254
295
|
roots: readonly string[],
|
|
@@ -256,6 +297,13 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
|
|
|
256
297
|
): Promise<void> {
|
|
257
298
|
return this.coordinator().confirm(owner, roots, this.signal(options));
|
|
258
299
|
}
|
|
300
|
+
async confirmDirectRetained(
|
|
301
|
+
owner: string,
|
|
302
|
+
roots: readonly string[],
|
|
303
|
+
options: IpfsReadOptions = {},
|
|
304
|
+
): Promise<void> {
|
|
305
|
+
return this.coordinator().confirmDirect(owner, roots, this.signal(options));
|
|
306
|
+
}
|
|
259
307
|
async recoverOwner(
|
|
260
308
|
owner: string,
|
|
261
309
|
roots: readonly string[] | (() => readonly string[]),
|
|
@@ -275,18 +323,47 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
|
|
|
275
323
|
): Promise<number> {
|
|
276
324
|
return this.coordinator().reconcile(owner, roots, this.signal(options));
|
|
277
325
|
}
|
|
326
|
+
async reconcileDirect(
|
|
327
|
+
owner: string,
|
|
328
|
+
roots: readonly string[] | (() => readonly string[]),
|
|
329
|
+
options: IpfsReadOptions = {},
|
|
330
|
+
): Promise<number> {
|
|
331
|
+
return this.coordinator().reconcileDirect(
|
|
332
|
+
owner,
|
|
333
|
+
roots,
|
|
334
|
+
this.signal(options),
|
|
335
|
+
);
|
|
336
|
+
}
|
|
278
337
|
async pin(root: CID | string, options: IpfsReadOptions = {}): Promise<void> {
|
|
279
338
|
return this.coordinator().pin(root.toString(), this.signal(options));
|
|
280
339
|
}
|
|
340
|
+
async pinDirect(
|
|
341
|
+
root: CID | string,
|
|
342
|
+
options: IpfsReadOptions = {},
|
|
343
|
+
): Promise<void> {
|
|
344
|
+
return this.coordinator().pinDirect(root.toString(), this.signal(options));
|
|
345
|
+
}
|
|
281
346
|
async unpin(
|
|
282
347
|
root: CID | string,
|
|
283
348
|
options: IpfsReadOptions = {},
|
|
284
349
|
): Promise<void> {
|
|
285
350
|
return this.coordinator().unpin(root.toString(), this.signal(options));
|
|
286
351
|
}
|
|
352
|
+
async unpinDirect(
|
|
353
|
+
root: CID | string,
|
|
354
|
+
options: IpfsReadOptions = {},
|
|
355
|
+
): Promise<void> {
|
|
356
|
+
return this.coordinator().unpinDirect(
|
|
357
|
+
root.toString(),
|
|
358
|
+
this.signal(options),
|
|
359
|
+
);
|
|
360
|
+
}
|
|
287
361
|
pins(): readonly string[] {
|
|
288
362
|
return this.coordinator().pins();
|
|
289
363
|
}
|
|
364
|
+
directPins(): readonly string[] {
|
|
365
|
+
return this.coordinator().directPins();
|
|
366
|
+
}
|
|
290
367
|
private localMaintenance(): SharedContent {
|
|
291
368
|
this.signal({}).throwIfAborted();
|
|
292
369
|
if (!this.supportsRetention || !(this.backend instanceof SharedContent))
|
|
@@ -328,9 +405,13 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
|
|
|
328
405
|
async usage(options: IpfsReadOptions = {}): Promise<StoreUsage> {
|
|
329
406
|
const backend = this.localMaintenance();
|
|
330
407
|
const signal = this.signal(options);
|
|
331
|
-
return this.coordinator().maintain(async ({ pinned }) => {
|
|
408
|
+
return this.coordinator().maintain(async ({ pinned, directPinned }) => {
|
|
332
409
|
const inventory = await backend.inventory(signal);
|
|
333
410
|
const blocks = await this.localClosure(backend, pinned, signal);
|
|
411
|
+
for (const key of directPinned) {
|
|
412
|
+
const bytes = await backend.readLocalBlock(CID.parse(key), signal);
|
|
413
|
+
blocks.set(key, bytes.byteLength);
|
|
414
|
+
}
|
|
334
415
|
return {
|
|
335
416
|
cachedBytes: [...inventory.values()].reduce(
|
|
336
417
|
(sum, size) => sum + size,
|
|
@@ -343,21 +424,28 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
|
|
|
343
424
|
async gc(options: IpfsReadOptions = {}): Promise<StoreGcResult> {
|
|
344
425
|
const backend = this.localMaintenance();
|
|
345
426
|
const signal = this.signal(options);
|
|
346
|
-
return this.coordinator().maintain(
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
reclaimedBytes
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
427
|
+
return this.coordinator().maintain(
|
|
428
|
+
async ({ roots, directRoots, staged }) => {
|
|
429
|
+
const inventory = await backend.inventory(signal);
|
|
430
|
+
const marked = await this.localClosure(backend, roots, signal);
|
|
431
|
+
for (const key of directRoots) {
|
|
432
|
+
const bytes = await backend.readLocalBlock(CID.parse(key), signal);
|
|
433
|
+
marked.set(key, bytes.byteLength);
|
|
434
|
+
}
|
|
435
|
+
for (const key of staged) marked.set(key, 0);
|
|
436
|
+
let removedBlocks = 0;
|
|
437
|
+
let reclaimedBytes = 0;
|
|
438
|
+
// Complete verification before any deletion. New publications outside this snapshot survive.
|
|
439
|
+
for (const key of inventory.keys()) {
|
|
440
|
+
signal.throwIfAborted();
|
|
441
|
+
if (marked.has(key)) continue;
|
|
442
|
+
reclaimedBytes += await backend.removeBlock(key, signal);
|
|
443
|
+
removedBlocks++;
|
|
444
|
+
}
|
|
445
|
+
return { removedBlocks, reclaimedBytes };
|
|
446
|
+
},
|
|
447
|
+
signal,
|
|
448
|
+
);
|
|
361
449
|
}
|
|
362
450
|
async stageBlock(
|
|
363
451
|
session: string,
|
|
@@ -373,6 +461,21 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
|
|
|
373
461
|
signal,
|
|
374
462
|
);
|
|
375
463
|
}
|
|
464
|
+
async stageBlocks(
|
|
465
|
+
session: string,
|
|
466
|
+
blocks: readonly StagedBlock[],
|
|
467
|
+
options: IpfsReadOptions = {},
|
|
468
|
+
): Promise<void> {
|
|
469
|
+
const signal = this.signal(options);
|
|
470
|
+
return this.coordinator().stageBatch(
|
|
471
|
+
session,
|
|
472
|
+
blocks.map(({ cid, bytes }) => ({
|
|
473
|
+
cid: cid.toString(),
|
|
474
|
+
write: () => this.backend.put(cid, bytes, { signal }),
|
|
475
|
+
})),
|
|
476
|
+
signal,
|
|
477
|
+
);
|
|
478
|
+
}
|
|
376
479
|
async finishImport(
|
|
377
480
|
session: string,
|
|
378
481
|
root: CID | string,
|
|
@@ -386,6 +489,26 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
|
|
|
386
489
|
this.signal(options),
|
|
387
490
|
);
|
|
388
491
|
}
|
|
492
|
+
async importBlocks(
|
|
493
|
+
session: string,
|
|
494
|
+
blocks: readonly StagedBlock[],
|
|
495
|
+
root: CID | string,
|
|
496
|
+
owner?: string,
|
|
497
|
+
options: ImportBlocksOptions = {},
|
|
498
|
+
): Promise<void> {
|
|
499
|
+
const signal = this.signal(options);
|
|
500
|
+
return this.coordinator().importBatch(
|
|
501
|
+
session,
|
|
502
|
+
blocks.map(({ cid, bytes }) => ({
|
|
503
|
+
cid: cid.toString(),
|
|
504
|
+
write: () => this.backend.put(cid, bytes, { signal }),
|
|
505
|
+
})),
|
|
506
|
+
root.toString(),
|
|
507
|
+
owner,
|
|
508
|
+
signal,
|
|
509
|
+
options.onRetained,
|
|
510
|
+
);
|
|
511
|
+
}
|
|
389
512
|
async discardImport(
|
|
390
513
|
session: string,
|
|
391
514
|
options: IpfsReadOptions = {},
|