@ipab/cli 0.1.0-rc.12 → 0.1.0-rc.13

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.
Files changed (71) hide show
  1. package/dist/cli-daemon.d.ts +14 -0
  2. package/dist/cli-daemon.d.ts.map +1 -0
  3. package/dist/cli-daemon.js +400 -0
  4. package/dist/cli-daemon.js.map +1 -0
  5. package/dist/cli.d.ts.map +1 -1
  6. package/dist/cli.js +1260 -1404
  7. package/dist/cli.js.map +1 -1
  8. package/dist/daemon-api.d.ts +80 -0
  9. package/dist/daemon-api.d.ts.map +1 -0
  10. package/dist/daemon-api.js +2411 -0
  11. package/dist/daemon-api.js.map +1 -0
  12. package/dist/daemon-client.d.ts +322 -0
  13. package/dist/daemon-client.d.ts.map +1 -0
  14. package/dist/daemon-client.js +400 -0
  15. package/dist/daemon-client.js.map +1 -0
  16. package/dist/daemon-protocol.d.ts +22 -0
  17. package/dist/daemon-protocol.d.ts.map +1 -0
  18. package/dist/daemon-protocol.js +39 -0
  19. package/dist/daemon-protocol.js.map +1 -0
  20. package/dist/daemon-telemetry.d.ts +86 -0
  21. package/dist/daemon-telemetry.d.ts.map +1 -0
  22. package/dist/daemon-telemetry.js +502 -0
  23. package/dist/daemon-telemetry.js.map +1 -0
  24. package/dist/daemon-ui.d.ts +68 -0
  25. package/dist/daemon-ui.d.ts.map +1 -1
  26. package/dist/daemon-ui.js +283 -4
  27. package/dist/daemon-ui.js.map +1 -1
  28. package/dist/daemon.d.ts +10 -0
  29. package/dist/daemon.d.ts.map +1 -1
  30. package/dist/daemon.js +289 -38
  31. package/dist/daemon.js.map +1 -1
  32. package/dist/identity.d.ts.map +1 -1
  33. package/dist/identity.js +7 -2
  34. package/dist/identity.js.map +1 -1
  35. package/dist/ipc.d.ts +19 -7
  36. package/dist/ipc.d.ts.map +1 -1
  37. package/dist/ipc.js +96 -70
  38. package/dist/ipc.js.map +1 -1
  39. package/dist/pins.d.ts.map +1 -1
  40. package/dist/pins.js +18 -3
  41. package/dist/pins.js.map +1 -1
  42. package/dist/state-storage.d.ts.map +1 -1
  43. package/dist/state-storage.js +2 -1
  44. package/dist/state-storage.js.map +1 -1
  45. package/dist/state.d.ts +1 -1
  46. package/dist/state.d.ts.map +1 -1
  47. package/dist/state.js +3 -2
  48. package/dist/state.js.map +1 -1
  49. package/dist/swarm-files.d.ts.map +1 -1
  50. package/dist/swarm-files.js +7 -2
  51. package/dist/swarm-files.js.map +1 -1
  52. package/dist/swarm.d.ts +10 -1
  53. package/dist/swarm.d.ts.map +1 -1
  54. package/dist/swarm.js +230 -56
  55. package/dist/swarm.js.map +1 -1
  56. package/package.json +4 -4
  57. package/src/cli-daemon.ts +482 -0
  58. package/src/cli.ts +1689 -1948
  59. package/src/daemon-api.ts +3148 -0
  60. package/src/daemon-client.ts +975 -0
  61. package/src/daemon-protocol.ts +61 -0
  62. package/src/daemon-telemetry.ts +656 -0
  63. package/src/daemon-ui.ts +361 -4
  64. package/src/daemon.ts +337 -43
  65. package/src/identity.ts +10 -2
  66. package/src/ipc.ts +113 -79
  67. package/src/pins.ts +23 -3
  68. package/src/state-storage.ts +5 -1
  69. package/src/state.ts +3 -1
  70. package/src/swarm-files.ts +10 -2
  71. package/src/swarm.ts +698 -464
package/src/ipc.ts CHANGED
@@ -67,21 +67,17 @@ interface WriteStream {
67
67
  writing: boolean;
68
68
  }
69
69
 
70
- export class ContextServer {
71
- private readonly requests = new Map<number, AbortController>();
70
+ export class ContextHandler {
72
71
  private readonly reads = new Map<number, ReadStream>();
73
72
  private readonly writes = new Map<number, WriteStream>();
74
- private readonly tasks = new Set<Promise<void>>();
75
73
  private readonly shallowRoots = new Set<string>();
76
74
  private openingReads = 0;
77
75
  private sequence = 0;
78
76
  private closed = false;
79
77
  constructor(
80
- readonly child: ChildProcess,
81
78
  readonly store: IpfsStore,
82
79
  readonly owner: Ownership,
83
80
  readonly output: OutputSpace,
84
- readonly fatal: (error: Error) => void,
85
81
  readonly appCid: string,
86
82
  readonly resolveBundleReference?: (
87
83
  reference: string,
@@ -89,74 +85,8 @@ export class ContextServer {
89
85
  swarm?: string,
90
86
  ) => Promise<Pick<BundleInspection, "cid" | "targetCid" | "swarm">>,
91
87
  readonly supportedTypes?: ReadonlySet<string>,
92
- ) {
93
- child.on("message", this.receive);
94
- }
95
- private reply(frame: Reply): void {
96
- if (this.child.connected)
97
- this.child.send(frame, (error) => {
98
- if (error && !this.closed) this.fatal(error);
99
- });
100
- }
101
- private receive = (frame: unknown): void => {
102
- if (!requestFrame(frame)) {
103
- this.fatal(new HostError("Invalid IPAB context frame."));
104
- return;
105
- }
106
- if (this.closed) {
107
- this.reply({
108
- version: 1,
109
- id: frame.id,
110
- error: "Context server is closed.",
111
- });
112
- return;
113
- }
114
- if (frame.operation === "cancel") {
115
- this.requests
116
- .get(Number(frame.args))
117
- ?.abort(new HostError("Context operation cancelled."));
118
- return;
119
- }
120
- if (this.requests.size >= 32 || this.requests.has(frame.id)) {
121
- this.fatal(
122
- new HostError("Context request capacity or identity violation."),
123
- );
124
- return;
125
- }
126
- const controller = new AbortController();
127
- this.requests.set(frame.id, controller);
128
- const task = this.handle(frame.operation, frame.args, controller.signal)
129
- .then(async (result) => {
130
- if (controller.signal.aborted) {
131
- if (frame.operation === "read")
132
- await this.cancelRead(Number(result), controller.signal.reason);
133
- if (
134
- ["link", "beginOpenSession", "cancelOpenSession"].includes(
135
- frame.operation,
136
- )
137
- )
138
- this.output.fail(controller.signal.reason);
139
- if (frame.operation === "submitBundleResult")
140
- this.output.discardBundleResult();
141
- controller.signal.throwIfAborted();
142
- }
143
- return result;
144
- })
145
- .then(
146
- (result) => {
147
- this.reply({ version: 1, id: frame.id, result });
148
- },
149
- (error: unknown) => {
150
- this.reply({ version: 1, id: frame.id, error: message(error) });
151
- },
152
- )
153
- .finally(() => {
154
- this.requests.delete(frame.id);
155
- this.tasks.delete(task);
156
- });
157
- this.tasks.add(task);
158
- };
159
- private async cancelRead(id: number, reason: unknown): Promise<void> {
88
+ ) {}
89
+ async cancelRead(id: number, reason: unknown): Promise<void> {
160
90
  const stream = this.reads.get(id);
161
91
  if (!stream) return;
162
92
  stream.controller.abort(reason);
@@ -166,11 +96,12 @@ export class ContextServer {
166
96
  this.reads.delete(id);
167
97
  }
168
98
  }
169
- private async handle(
99
+ async handle(
170
100
  operation: string,
171
101
  args: unknown,
172
102
  signal: AbortSignal,
173
103
  ): Promise<unknown> {
104
+ if (this.closed) throw new HostError("Context handler is closed.");
174
105
  signal.throwIfAborted();
175
106
  if (operation === "identity") return this.appCid;
176
107
  if (operation === "readBlock") {
@@ -483,11 +414,8 @@ export class ContextServer {
483
414
  }
484
415
  throw new HostError("Unknown context operation.");
485
416
  }
486
- async close(): Promise<void> {
417
+ async close(extraTasks: Iterable<Promise<void>> = []): Promise<void> {
487
418
  this.closed = true;
488
- this.child.off("message", this.receive);
489
- for (const request of this.requests.values())
490
- request.abort(new HostError("App finished."));
491
419
  for (const stream of this.writes.values()) {
492
420
  const error = new HostError(
493
421
  "App exited with unfinished output writes.",
@@ -501,7 +429,7 @@ export class ContextServer {
501
429
  for (const stream of this.reads.values())
502
430
  stream.controller.abort(new HostError("App finished."));
503
431
  await Promise.allSettled([
504
- ...this.tasks,
432
+ ...extraTasks,
505
433
  ...[...this.writes.values()].map((stream) => stream.task),
506
434
  ]);
507
435
  await Promise.all(
@@ -513,3 +441,109 @@ export class ContextServer {
513
441
  this.writes.clear();
514
442
  }
515
443
  }
444
+
445
+ export class ContextServer {
446
+ private readonly requests = new Map<number, AbortController>();
447
+ private readonly tasks = new Set<Promise<void>>();
448
+ private readonly handler: ContextHandler;
449
+ private closed = false;
450
+ constructor(
451
+ readonly child: ChildProcess,
452
+ readonly store: IpfsStore,
453
+ readonly owner: Ownership,
454
+ readonly output: OutputSpace,
455
+ readonly fatal: (error: Error) => void,
456
+ readonly appCid: string,
457
+ readonly resolveBundleReference?: (
458
+ reference: string,
459
+ signal: AbortSignal,
460
+ swarm?: string,
461
+ ) => Promise<Pick<BundleInspection, "cid" | "targetCid" | "swarm">>,
462
+ readonly supportedTypes?: ReadonlySet<string>,
463
+ ) {
464
+ this.handler = new ContextHandler(
465
+ store,
466
+ owner,
467
+ output,
468
+ appCid,
469
+ resolveBundleReference,
470
+ supportedTypes,
471
+ );
472
+ child.on("message", this.receive);
473
+ }
474
+ private reply(frame: Reply): void {
475
+ if (this.child.connected)
476
+ this.child.send(frame, (error) => {
477
+ if (error && !this.closed) this.fatal(error);
478
+ });
479
+ }
480
+ private receive = (frame: unknown): void => {
481
+ if (!requestFrame(frame)) {
482
+ this.fatal(new HostError("Invalid IPAB context frame."));
483
+ return;
484
+ }
485
+ if (this.closed) {
486
+ this.reply({
487
+ version: 1,
488
+ id: frame.id,
489
+ error: "Context server is closed.",
490
+ });
491
+ return;
492
+ }
493
+ if (frame.operation === "cancel") {
494
+ this.requests
495
+ .get(Number(frame.args))
496
+ ?.abort(new HostError("Context operation cancelled."));
497
+ return;
498
+ }
499
+ if (this.requests.size >= 32 || this.requests.has(frame.id)) {
500
+ this.fatal(
501
+ new HostError("Context request capacity or identity violation."),
502
+ );
503
+ return;
504
+ }
505
+ const controller = new AbortController();
506
+ this.requests.set(frame.id, controller);
507
+ const task = this.handler
508
+ .handle(frame.operation, frame.args, controller.signal)
509
+ .then(async (result) => {
510
+ if (controller.signal.aborted) {
511
+ if (frame.operation === "read")
512
+ await this.handler.cancelRead(
513
+ Number(result),
514
+ controller.signal.reason,
515
+ );
516
+ if (
517
+ ["link", "beginOpenSession", "cancelOpenSession"].includes(
518
+ frame.operation,
519
+ )
520
+ )
521
+ this.output.fail(controller.signal.reason);
522
+ if (frame.operation === "submitBundleResult")
523
+ this.output.discardBundleResult();
524
+ controller.signal.throwIfAborted();
525
+ }
526
+ return result;
527
+ })
528
+ .then(
529
+ (result) => {
530
+ this.reply({ version: 1, id: frame.id, result });
531
+ },
532
+ (error: unknown) => {
533
+ this.reply({ version: 1, id: frame.id, error: message(error) });
534
+ },
535
+ )
536
+ .finally(() => {
537
+ this.requests.delete(frame.id);
538
+ this.tasks.delete(task);
539
+ });
540
+ this.tasks.add(task);
541
+ };
542
+ async close(): Promise<void> {
543
+ this.closed = true;
544
+ this.child.off("message", this.receive);
545
+ for (const request of this.requests.values())
546
+ request.abort(new HostError("App finished."));
547
+ await this.handler.close(this.tasks);
548
+ }
549
+ }
package/src/pins.ts CHANGED
@@ -50,6 +50,24 @@ export function refreshPinClaim(
50
50
  });
51
51
  }
52
52
 
53
+ function persistentStoreBackend(
54
+ configuration: IpfsStore["configuration"],
55
+ ): IpfsStore["configuration"] {
56
+ return {
57
+ backend: configuration.backend,
58
+ dataDir: configuration.dataDir,
59
+ ...(configuration.coordinationDir !== undefined
60
+ ? { coordinationDir: configuration.coordinationDir }
61
+ : {}),
62
+ ...(configuration.gateway !== undefined
63
+ ? { gateway: configuration.gateway }
64
+ : {}),
65
+ ...(configuration.limits !== undefined
66
+ ? { limits: configuration.limits }
67
+ : {}),
68
+ };
69
+ }
70
+
53
71
  export function reservePin(
54
72
  state: State,
55
73
  reference: string,
@@ -78,7 +96,7 @@ export function reservePin(
78
96
  state.saveContentPin(pin);
79
97
  state.saveHold({
80
98
  owner: pin.owner,
81
- backend: configuration,
99
+ backend: persistentStoreBackend(configuration),
82
100
  roots: [],
83
101
  pid: process.pid,
84
102
  session: null,
@@ -99,14 +117,16 @@ export async function retainPin(
99
117
  let base: IpfsStore | undefined;
100
118
  let original: IpfsStore | undefined;
101
119
  try {
102
- if (JSON.stringify(backend) !== JSON.stringify(store.configuration)) {
120
+ if (
121
+ JSON.stringify(persistentStoreBackend(backend)) !==
122
+ JSON.stringify(persistentStoreBackend(store.configuration))
123
+ ) {
103
124
  base = new IpfsStore(backend);
104
125
  original = base.withLocalSource({
105
126
  block: { get: (cid, options) => store.block.get(cid, options) },
106
127
  });
107
128
  }
108
129
  const retainedStore = original ?? store;
109
- await base?.close();
110
130
  await retainedStore.recoverOwner(
111
131
  pin.owner,
112
132
  () => state.hold(pin.owner)!.roots,
@@ -266,7 +266,11 @@ function access(directory: string, initialize: boolean, sealed = false): void {
266
266
  join(directory, database),
267
267
  names.includes(`${database}-wal`),
268
268
  ));
269
- if (!managed && entries.some((e) => e.name !== "kubo-bin")) unsafe();
269
+ if (
270
+ !managed &&
271
+ entries.some((e) => e.name !== "kubo-bin" && e.name !== "swarm-daemon")
272
+ )
273
+ unsafe();
270
274
 
271
275
  if (windows()) {
272
276
  if (
package/src/state.ts CHANGED
@@ -262,9 +262,11 @@ export class State {
262
262
  directory: string,
263
263
  private readonly builtins: readonly NamedPin[] = [],
264
264
  env: NodeJS.ProcessEnv = process.env,
265
+ wrapDb?: (db: DatabaseSync) => DatabaseSync,
265
266
  ) {
266
267
  this.directory = resolve(directory);
267
- this.db = new DatabaseSync(openStateStorage(this.directory, env));
268
+ const rawDb = new DatabaseSync(openStateStorage(this.directory, env));
269
+ this.db = wrapDb ? wrapDb(rawDb) : rawDb;
268
270
  try {
269
271
  enableWal(this.db);
270
272
  this.db.exec(`PRAGMA busy_timeout=5000; PRAGMA synchronous=FULL;
@@ -8,8 +8,16 @@ import {
8
8
  secureNew,
9
9
  type ProtectedPath,
10
10
  } from "./identity.js";
11
- import { nodeError } from "./authoring.js";
12
- import { HostError } from "./errors.js";
11
+ import { HostError } from "./host-error.js";
12
+
13
+ function nodeError(cause: unknown, code: string): boolean {
14
+ return (
15
+ typeof cause === "object" &&
16
+ cause !== null &&
17
+ "code" in cause &&
18
+ (cause as { code?: unknown }).code === code
19
+ );
20
+ }
13
21
 
14
22
  export async function privateDirectory(
15
23
  parent: string,