@ipab/ipfs-store 0.1.0-rc.5 → 0.1.0-rc.7

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/src/store.ts CHANGED
@@ -23,18 +23,38 @@ import {
23
23
  import { SharedContent } from "./storage.js";
24
24
  import { ContentError } from "./errors.js";
25
25
  import { join } from "node:path";
26
- import { Retention } from "./retention.js";
26
+ import {
27
+ Retention,
28
+ type RetentionPort,
29
+ type RetentionCoordinationEvent,
30
+ type RetentionOptions,
31
+ type RetentionSqlEvent,
32
+ type RetentionTelemetry,
33
+ } from "./retention.js";
27
34
  import {
28
35
  validateConfiguration,
29
36
  type IpfsStoreOptions,
30
37
  type StoreConfiguration,
31
38
  } from "./configuration.js";
32
39
  export type { IpfsStoreOptions, StoreConfiguration } from "./configuration.js";
40
+ export type {
41
+ RetentionCoordinationEvent,
42
+ RetentionOptions,
43
+ RetentionSqlEvent,
44
+ RetentionTelemetry,
45
+ };
33
46
  export interface IpfsReadOptions {
34
47
  signal?: AbortSignal;
35
48
  hints?: readonly string[];
36
49
  allowGateway?: (url: URL) => boolean;
37
50
  }
51
+ export interface StagedBlock {
52
+ cid: CID;
53
+ bytes: Uint8Array;
54
+ }
55
+ export interface ImportBlocksOptions extends IpfsReadOptions {
56
+ onRetained?: () => Promise<void> | void;
57
+ }
38
58
  export interface StoreUsage {
39
59
  cachedBytes: number;
40
60
  pinnedBytes: number;
@@ -131,6 +151,7 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
131
151
  source,
132
152
  this.limits.blockBytes,
133
153
  );
154
+ store.retentionStore = this.retentionStore ?? this;
134
155
  return store;
135
156
  }
136
157
  /** Independent reader budget/lifetime; closing it never closes the shared backing store. */
@@ -211,6 +232,25 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
211
232
  ): Promise<void> {
212
233
  await this.backend.put(cid, bytes, { signal: this.signal(options) });
213
234
  }
235
+ private retentionPort(): RetentionPort {
236
+ return {
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
+ prepareDirect: async (root, signal) => {
248
+ await this.backend.readBlock(CID.parse(root).toString(), {
249
+ signal: signal ?? this.signal({}),
250
+ });
251
+ },
252
+ };
253
+ }
214
254
  private coordinator(): Retention {
215
255
  this.signal({}).throwIfAborted();
216
256
  if (!this.supportsRetention)
@@ -223,22 +263,14 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
223
263
  const config = this.configuration;
224
264
  const directory =
225
265
  config.coordinationDir ?? join(config.dataDir, "retention");
226
- this.retention = new Retention(directory, {
227
- prepare: async (root, signal) => {
228
- await prepareContent(this.backend, root, {
229
- signal: signal ?? this.signal({}),
230
- maxBlockBytes: this.limits.blockBytes,
231
- maxDagBlocks: this.limits.blocks,
232
- depth: this.limits.depth,
233
- retentionBudget: { bytes: 0, limit: this.limits.bytes },
234
- retainedBlocks: new Map(),
235
- });
236
- },
237
- prepareDirect: async (root, signal) => {
238
- await this.backend.readBlock(CID.parse(root).toString(), {
239
- signal: signal ?? this.signal({}),
240
- });
241
- },
266
+ this.retention = new Retention(directory, this.retentionPort(), {
267
+ ...(config.singleProcessCoordination !== undefined
268
+ ? { singleProcessCoordination: config.singleProcessCoordination }
269
+ : {}),
270
+ ...(config.onCoordinationEvent
271
+ ? { onCoordinationEvent: config.onCoordinationEvent }
272
+ : {}),
273
+ ...(config.onSqlEvent ? { onSqlEvent: config.onSqlEvent } : {}),
242
274
  });
243
275
  }
244
276
  return this.retention;
@@ -274,14 +306,24 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
274
306
  roots: readonly string[],
275
307
  options: IpfsReadOptions = {},
276
308
  ): Promise<void> {
277
- return this.coordinator().retain(owner, roots, this.signal(options));
309
+ return this.coordinator().retain(
310
+ owner,
311
+ roots,
312
+ this.signal(options),
313
+ this.retentionPort(),
314
+ );
278
315
  }
279
316
  async retainDirect(
280
317
  owner: string,
281
318
  roots: readonly string[],
282
319
  options: IpfsReadOptions = {},
283
320
  ): Promise<void> {
284
- return this.coordinator().retainDirect(owner, roots, this.signal(options));
321
+ return this.coordinator().retainDirect(
322
+ owner,
323
+ roots,
324
+ this.signal(options),
325
+ this.retentionPort(),
326
+ );
285
327
  }
286
328
  async confirmRetained(
287
329
  owner: string,
@@ -314,7 +356,12 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
314
356
  roots: readonly string[] | (() => readonly string[]),
315
357
  options: IpfsReadOptions = {},
316
358
  ): Promise<number> {
317
- return this.coordinator().reconcile(owner, roots, this.signal(options));
359
+ return this.coordinator().reconcile(
360
+ owner,
361
+ roots,
362
+ this.signal(options),
363
+ this.retentionPort(),
364
+ );
318
365
  }
319
366
  async reconcileDirect(
320
367
  owner: string,
@@ -325,16 +372,25 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
325
372
  owner,
326
373
  roots,
327
374
  this.signal(options),
375
+ this.retentionPort(),
328
376
  );
329
377
  }
330
378
  async pin(root: CID | string, options: IpfsReadOptions = {}): Promise<void> {
331
- return this.coordinator().pin(root.toString(), this.signal(options));
379
+ return this.coordinator().pin(
380
+ root.toString(),
381
+ this.signal(options),
382
+ this.retentionPort(),
383
+ );
332
384
  }
333
385
  async pinDirect(
334
386
  root: CID | string,
335
387
  options: IpfsReadOptions = {},
336
388
  ): Promise<void> {
337
- return this.coordinator().pinDirect(root.toString(), this.signal(options));
389
+ return this.coordinator().pinDirect(
390
+ root.toString(),
391
+ this.signal(options),
392
+ this.retentionPort(),
393
+ );
338
394
  }
339
395
  async unpin(
340
396
  root: CID | string,
@@ -454,6 +510,21 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
454
510
  signal,
455
511
  );
456
512
  }
513
+ async stageBlocks(
514
+ session: string,
515
+ blocks: readonly StagedBlock[],
516
+ options: IpfsReadOptions = {},
517
+ ): Promise<void> {
518
+ const signal = this.signal(options);
519
+ return this.coordinator().stageBatch(
520
+ session,
521
+ blocks.map(({ cid, bytes }) => ({
522
+ cid: cid.toString(),
523
+ write: () => this.backend.put(cid, bytes, { signal }),
524
+ })),
525
+ signal,
526
+ );
527
+ }
457
528
  async finishImport(
458
529
  session: string,
459
530
  root: CID | string,
@@ -465,6 +536,28 @@ export class IpfsStore implements IpfsClient, IpfsAccess {
465
536
  root.toString(),
466
537
  owner,
467
538
  this.signal(options),
539
+ this.retentionPort(),
540
+ );
541
+ }
542
+ async importBlocks(
543
+ session: string,
544
+ blocks: readonly StagedBlock[],
545
+ root: CID | string,
546
+ owner?: string,
547
+ options: ImportBlocksOptions = {},
548
+ ): Promise<void> {
549
+ const signal = this.signal(options);
550
+ return this.coordinator().importBatch(
551
+ session,
552
+ blocks.map(({ cid, bytes }) => ({
553
+ cid: cid.toString(),
554
+ write: () => this.backend.put(cid, bytes, { signal }),
555
+ })),
556
+ root.toString(),
557
+ owner,
558
+ signal,
559
+ options.onRetained,
560
+ this.retentionPort(),
468
561
  );
469
562
  }
470
563
  async discardImport(