@ipab/cli 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.
Files changed (120) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -2
  3. package/dist/app-authoring.d.ts +23 -0
  4. package/dist/app-authoring.d.ts.map +1 -0
  5. package/dist/app-authoring.js +449 -0
  6. package/dist/app-authoring.js.map +1 -0
  7. package/dist/app-definition.d.ts +109 -0
  8. package/dist/app-definition.d.ts.map +1 -0
  9. package/dist/app-definition.js +207 -0
  10. package/dist/app-definition.js.map +1 -0
  11. package/dist/app-prebuild.d.ts +12 -0
  12. package/dist/app-prebuild.d.ts.map +1 -0
  13. package/dist/app-prebuild.js +231 -0
  14. package/dist/app-prebuild.js.map +1 -0
  15. package/dist/app-templates.d.ts +13 -0
  16. package/dist/app-templates.d.ts.map +1 -0
  17. package/dist/app-templates.js +287 -0
  18. package/dist/app-templates.js.map +1 -0
  19. package/dist/authoring.d.ts +72 -0
  20. package/dist/authoring.d.ts.map +1 -0
  21. package/dist/authoring.js +440 -0
  22. package/dist/authoring.js.map +1 -0
  23. package/dist/builtins/template.node.car +0 -0
  24. package/dist/builtins/template.web-app.car +0 -0
  25. package/dist/builtins.d.ts +6 -0
  26. package/dist/builtins.d.ts.map +1 -0
  27. package/dist/builtins.js +46 -0
  28. package/dist/builtins.js.map +1 -0
  29. package/dist/child.d.ts +5 -0
  30. package/dist/child.d.ts.map +1 -0
  31. package/dist/child.js +28 -0
  32. package/dist/child.js.map +1 -0
  33. package/dist/cli.d.ts +24 -0
  34. package/dist/cli.d.ts.map +1 -0
  35. package/dist/cli.js +1072 -0
  36. package/dist/cli.js.map +1 -0
  37. package/dist/config.d.ts +26 -0
  38. package/dist/config.d.ts.map +1 -0
  39. package/dist/config.js +156 -0
  40. package/dist/config.js.map +1 -0
  41. package/dist/context.d.ts +42 -0
  42. package/dist/context.d.ts.map +1 -0
  43. package/dist/context.js +164 -0
  44. package/dist/context.js.map +1 -0
  45. package/dist/errors.d.ts +10 -0
  46. package/dist/errors.d.ts.map +1 -0
  47. package/dist/errors.js +40 -0
  48. package/dist/errors.js.map +1 -0
  49. package/dist/history.d.ts +34 -0
  50. package/dist/history.d.ts.map +1 -0
  51. package/dist/history.js +286 -0
  52. package/dist/history.js.map +1 -0
  53. package/dist/ipc.d.ts +26 -0
  54. package/dist/ipc.d.ts.map +1 -0
  55. package/dist/ipc.js +332 -0
  56. package/dist/ipc.js.map +1 -0
  57. package/dist/logs.d.ts +15 -0
  58. package/dist/logs.d.ts.map +1 -0
  59. package/dist/logs.js +118 -0
  60. package/dist/logs.js.map +1 -0
  61. package/dist/main.d.ts +3 -0
  62. package/dist/main.d.ts.map +1 -0
  63. package/dist/main.js +4 -0
  64. package/dist/main.js.map +1 -0
  65. package/dist/pins.d.ts +33 -0
  66. package/dist/pins.d.ts.map +1 -0
  67. package/dist/pins.js +183 -0
  68. package/dist/pins.js.map +1 -0
  69. package/dist/runtime.d.ts +20 -0
  70. package/dist/runtime.d.ts.map +1 -0
  71. package/dist/runtime.js +237 -0
  72. package/dist/runtime.js.map +1 -0
  73. package/dist/source-bundle.d.ts +16 -0
  74. package/dist/source-bundle.d.ts.map +1 -0
  75. package/dist/source-bundle.js +103 -0
  76. package/dist/source-bundle.js.map +1 -0
  77. package/dist/space.d.ts +33 -0
  78. package/dist/space.d.ts.map +1 -0
  79. package/dist/space.js +269 -0
  80. package/dist/space.js.map +1 -0
  81. package/dist/state.d.ts +92 -0
  82. package/dist/state.d.ts.map +1 -0
  83. package/dist/state.js +324 -0
  84. package/dist/state.js.map +1 -0
  85. package/dist/supported-schemas.d.ts +5 -0
  86. package/dist/supported-schemas.d.ts.map +1 -0
  87. package/dist/supported-schemas.js +90 -0
  88. package/dist/supported-schemas.js.map +1 -0
  89. package/dist/template-definition.d.ts +115 -0
  90. package/dist/template-definition.d.ts.map +1 -0
  91. package/dist/template-definition.js +188 -0
  92. package/dist/template-definition.js.map +1 -0
  93. package/dist/template-source.d.ts +12 -0
  94. package/dist/template-source.d.ts.map +1 -0
  95. package/dist/template-source.js +97 -0
  96. package/dist/template-source.js.map +1 -0
  97. package/package.json +67 -3
  98. package/src/app-authoring.ts +614 -0
  99. package/src/app-definition.ts +256 -0
  100. package/src/app-prebuild.ts +289 -0
  101. package/src/app-templates.ts +382 -0
  102. package/src/authoring.ts +610 -0
  103. package/src/builtins.ts +55 -0
  104. package/src/child.ts +32 -0
  105. package/src/cli.ts +1285 -0
  106. package/src/config.ts +220 -0
  107. package/src/context.ts +232 -0
  108. package/src/errors.ts +46 -0
  109. package/src/history.ts +391 -0
  110. package/src/ipc.ts +358 -0
  111. package/src/logs.ts +132 -0
  112. package/src/main.ts +4 -0
  113. package/src/pins.ts +214 -0
  114. package/src/runtime.ts +300 -0
  115. package/src/source-bundle.ts +136 -0
  116. package/src/space.ts +379 -0
  117. package/src/state.ts +416 -0
  118. package/src/supported-schemas.ts +151 -0
  119. package/src/template-definition.ts +242 -0
  120. package/src/template-source.ts +128 -0
@@ -0,0 +1,286 @@
1
+ import { IpfsStore } from "@ipab/ipfs-store";
2
+ import { HostError, message, syntax } from "./errors.js";
3
+ import { removeLogs } from "./logs.js";
4
+ export function timestamp(value) {
5
+ if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?(?:Z|[+-]\d{2}:\d{2})$/.test(value))
6
+ syntax("Time must be RFC3339 with Z or an explicit timezone offset and at most three fractional second digits.");
7
+ const number = Date.parse(value);
8
+ const [, year, month, day, hour, minute, second] = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})/.exec(value);
9
+ const calendar = new Date(0);
10
+ calendar.setUTCFullYear(Number(year), Number(month), 0);
11
+ const days = calendar.getUTCDate();
12
+ if (!Number.isFinite(number) ||
13
+ Number(month) < 1 ||
14
+ Number(month) > 12 ||
15
+ Number(day) < 1 ||
16
+ Number(day) > days ||
17
+ Number(hour) > 23 ||
18
+ Number(minute) > 59 ||
19
+ Number(second) > 59)
20
+ syntax("Invalid RFC3339 timestamp.");
21
+ return number;
22
+ }
23
+ export function filters(state, options) {
24
+ const result = {};
25
+ if (typeof options.app === "string")
26
+ result.app = state.resolve(options.app);
27
+ if (typeof options.status === "string") {
28
+ if (!["succeeded", "failed", "cancelled"].includes(options.status))
29
+ syntax("Invalid execution status.");
30
+ result.status = options.status;
31
+ }
32
+ if (typeof options.since === "string")
33
+ result.since = timestamp(options.since);
34
+ if (typeof options.until === "string")
35
+ result.until = timestamp(options.until);
36
+ if (result.since !== undefined &&
37
+ result.until !== undefined &&
38
+ result.since >= result.until)
39
+ syntax("--since must precede --until.");
40
+ return result;
41
+ }
42
+ const endedTime = "CAST(round((julianday(json_extract(value,'$.endedAt'))-2440587.5)*86400000) AS INTEGER)";
43
+ function selection(filter, before) {
44
+ const conditions = [];
45
+ const parameters = [];
46
+ for (const [field, value] of [
47
+ ["appCid", filter.app],
48
+ ["status", filter.status],
49
+ ]) {
50
+ if (value !== undefined) {
51
+ conditions.push(`json_extract(value,'$.${field}')=?`);
52
+ parameters.push(value);
53
+ }
54
+ }
55
+ if (filter.since !== undefined) {
56
+ conditions.push(`${endedTime}>=?`);
57
+ parameters.push(filter.since);
58
+ }
59
+ if (filter.until !== undefined) {
60
+ conditions.push(`${endedTime}<?`);
61
+ parameters.push(filter.until);
62
+ }
63
+ if (before) {
64
+ conditions.push(`(${endedTime}<? OR (${endedTime}=? AND id<?))`);
65
+ parameters.push(Date.parse(before.endedAt), Date.parse(before.endedAt), before.id);
66
+ }
67
+ return {
68
+ where: conditions.length ? `WHERE ${conditions.join(" AND ")}` : "",
69
+ parameters,
70
+ };
71
+ }
72
+ export function selectedIds(state, filter) {
73
+ const query = selection(filter);
74
+ return state.db
75
+ .prepare(`SELECT id FROM runs ${query.where} ORDER BY ${endedTime} DESC,id DESC`)
76
+ .all(...query.parameters)
77
+ .map((row) => String(row.id));
78
+ }
79
+ export function listHistory(state, filter, limit, before) {
80
+ const query = selection(filter, before !== undefined ? state.run(before) : undefined);
81
+ const records = state.db
82
+ .prepare(`SELECT value FROM runs ${query.where} ORDER BY ${endedTime} DESC,id DESC LIMIT ?`)
83
+ .all(...query.parameters, limit < Number.MAX_SAFE_INTEGER ? limit + 1 : limit)
84
+ .map((row) => JSON.parse(String(row.value)));
85
+ const page = records.slice(0, limit);
86
+ return {
87
+ items: page.map(({ id, appCid, argv, startedAt, endedAt, status, outputCid }) => ({
88
+ id,
89
+ appCid,
90
+ argv,
91
+ startedAt,
92
+ endedAt,
93
+ status,
94
+ outputCid,
95
+ })),
96
+ nextBefore: records.length > page.length ? page.at(-1).id : null,
97
+ };
98
+ }
99
+ export function alive(pid) {
100
+ try {
101
+ process.kill(pid, 0);
102
+ return true;
103
+ }
104
+ catch (error) {
105
+ return error.code !== "ESRCH";
106
+ }
107
+ }
108
+ export class Ownership {
109
+ state;
110
+ store;
111
+ owner;
112
+ constructor(state, store, owner) {
113
+ this.state = state;
114
+ this.store = store;
115
+ this.owner = owner;
116
+ }
117
+ static async create(state, store, id, session) {
118
+ const owner = `ipab:${state.identity}:${id}`;
119
+ const hold = {
120
+ owner,
121
+ backend: store.configuration,
122
+ roots: [],
123
+ pid: process.pid,
124
+ session,
125
+ recordId: null,
126
+ };
127
+ state.saveHold(hold);
128
+ const deadline = Date.now() + 30_000;
129
+ for (;;) {
130
+ try {
131
+ store.registerOwner(owner, []);
132
+ break;
133
+ }
134
+ catch (error) {
135
+ if (error.code !== "retention_unfinished" ||
136
+ Date.now() >= deadline) {
137
+ state.saveHold({ ...hold, pid: null });
138
+ throw error;
139
+ }
140
+ await new Promise((resolve) => setTimeout(resolve, 10));
141
+ }
142
+ }
143
+ return new Ownership(state, store, owner);
144
+ }
145
+ async retain(roots, signal) {
146
+ const current = this.state.hold(this.owner);
147
+ const added = roots.filter((root) => !current.roots.includes(root));
148
+ if (!added.length)
149
+ return;
150
+ await this.store.retain(this.owner, added, signal ? { signal } : {});
151
+ // Read again after await: concurrent requests must not lose each other's references.
152
+ const latest = this.state.hold(this.owner);
153
+ this.state.saveHold({
154
+ ...latest,
155
+ roots: [...new Set([...latest.roots, ...added])],
156
+ });
157
+ await this.store.confirmRetained(this.owner, added, signal ? { signal } : {});
158
+ }
159
+ async release() {
160
+ const hold = this.state.hold(this.owner);
161
+ if (!hold)
162
+ return;
163
+ this.state.saveHold({ ...hold, roots: [] });
164
+ try {
165
+ if (hold.session)
166
+ await this.store.discardImport(hold.session);
167
+ await this.store.recoverOwner(this.owner, () => this.state.hold(this.owner)?.roots ?? []);
168
+ await this.store.reconcile(this.owner, () => this.state.hold(this.owner)?.roots ?? []);
169
+ this.state.deleteHold(this.owner);
170
+ }
171
+ catch (error) {
172
+ const current = this.state.hold(this.owner);
173
+ if (current)
174
+ this.state.saveHold({ ...current, pid: null });
175
+ throw error;
176
+ }
177
+ }
178
+ }
179
+ const createStore = (config) => new IpfsStore(config);
180
+ export async function recoverInactive(state, factory = createStore) {
181
+ for (const hold of state.holds()) {
182
+ if (hold.recordId ||
183
+ state.isPinOwner(hold.owner) ||
184
+ (hold.pid !== null && alive(hold.pid)))
185
+ continue;
186
+ const store = factory(hold.backend);
187
+ try {
188
+ await store.recoverOwner(hold.owner, () => state.hold(hold.owner)?.roots ?? [], {
189
+ isIdle: () => {
190
+ const current = state.hold(hold.owner);
191
+ return !current || current.pid === null || !alive(current.pid);
192
+ },
193
+ });
194
+ const current = state.hold(hold.owner);
195
+ if (current && (current.pid === null || !alive(current.pid))) {
196
+ await new Ownership(state, store, hold.owner).release();
197
+ }
198
+ }
199
+ finally {
200
+ await store.close();
201
+ }
202
+ }
203
+ }
204
+ export async function saveHistory(state, store, record, retained) {
205
+ if (!retained || !record.outputCid) {
206
+ state.saveRun(record);
207
+ return;
208
+ }
209
+ const ownership = await Ownership.create(state, store, `history:${record.id}`, null);
210
+ record.retention = { owned: true, state: "pending", owner: ownership.owner };
211
+ try {
212
+ await store.retain(ownership.owner, [record.outputCid]);
213
+ state.transaction(() => {
214
+ const hold = state.hold(ownership.owner);
215
+ state.saveHold({
216
+ ...hold,
217
+ roots: [record.outputCid],
218
+ recordId: record.id,
219
+ });
220
+ state.saveRun(record);
221
+ });
222
+ await store.confirmRetained(ownership.owner, [record.outputCid]);
223
+ state.transaction(() => {
224
+ record.retention.state = "retained";
225
+ state.saveRun(record);
226
+ state.saveHold({ ...state.hold(ownership.owner), pid: null });
227
+ });
228
+ }
229
+ catch (error) {
230
+ state.saveHold({ ...state.hold(ownership.owner), pid: null });
231
+ throw error;
232
+ }
233
+ }
234
+ export async function removeHistory(state, ids, factory = createStore) {
235
+ const removed = [];
236
+ for (const id of ids) {
237
+ try {
238
+ const record = state.run(id);
239
+ if (record.retention.owned) {
240
+ const hold = record.retention.owner
241
+ ? state.hold(record.retention.owner)
242
+ : undefined;
243
+ if (!hold)
244
+ throw new HostError("Retention ownership metadata is missing; deletion is blocked.");
245
+ if (hold.pid !== null && alive(hold.pid))
246
+ throw new HostError("Record retention commit is active; retry after it finishes.");
247
+ const store = factory(hold.backend);
248
+ try {
249
+ state.transaction(() => {
250
+ const latest = state.run(id);
251
+ latest.retention.state = "releasing";
252
+ state.saveRun(latest);
253
+ state.saveHold({ ...hold, roots: [], pid: null });
254
+ });
255
+ // These callbacks read persisted references while the Store participant mutex is held.
256
+ await store.recoverOwner(hold.owner, () => state.hold(hold.owner)?.roots ?? []);
257
+ await store.reconcile(hold.owner, () => state.hold(hold.owner)?.roots ?? []);
258
+ if (record.log)
259
+ await removeLogs(state.directory, id);
260
+ state.transaction(() => {
261
+ state.deleteRun(id);
262
+ state.deleteHold(hold.owner);
263
+ });
264
+ }
265
+ finally {
266
+ await store.close();
267
+ }
268
+ }
269
+ else {
270
+ if (record.log)
271
+ await removeLogs(state.directory, id);
272
+ state.deleteRun(id);
273
+ }
274
+ removed.push(id);
275
+ }
276
+ catch (error) {
277
+ throw new HostError(`History removal incomplete. Completed: ${JSON.stringify(removed)}; remaining: ${JSON.stringify(ids.filter((target) => !removed.includes(target)))}. ${message(error)} Ownership remains recoverable; no garbage collection requested.`, 1, "RELEASE_FAILED", "cleanup");
278
+ }
279
+ }
280
+ return {
281
+ removed,
282
+ retentionClaimsReleased: true,
283
+ garbageCollectionGuaranteed: false,
284
+ };
285
+ }
286
+ //# sourceMappingURL=history.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"history.js","sourceRoot":"","sources":["../src/history.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAA2B,MAAM,kBAAkB,CAAC;AAEtE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAEzD,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AASvC,MAAM,UAAU,SAAS,CAAC,KAAa;IACrC,IACE,CAAC,0EAA0E,CAAC,IAAI,CAC9E,KAAK,CACN;QAED,MAAM,CACJ,wGAAwG,CACzG,CAAC;IACJ,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACjC,MAAM,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,GAC9C,kDAAkD,CAAC,IAAI,CAAC,KAAK,CAAE,CAAC;IAClE,MAAM,QAAQ,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC;IAC7B,QAAQ,CAAC,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;IACxD,MAAM,IAAI,GAAG,QAAQ,CAAC,UAAU,EAAE,CAAC;IACnC,IACE,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC;QACxB,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC;QACjB,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE;QAClB,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC;QACf,MAAM,CAAC,G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AAM,CAAC,GAAG;oBAAE,MAAM,UAAU,CAAC,KAAK,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;gBACtD,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;YACtB,CAAC;YACD,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACnB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,SAAS,CACjB,0CAA0C,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,gBAAgB,IAAI,CAAC,SAAS,CAC7F,GAAG,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAClD,KAAK,OAAO,CAAC,KAAK,CAAC,kEAAkE,EACtF,CAAC,EACD,gBAAgB,EAChB,SAAS,CACV,CAAC;QACJ,CAAC;IACH,CAAC;IACD,OAAO;QACL,OAAO;QACP,uBAAuB,EAAE,IAAI;QAC7B,2BAA2B,EAAE,KAAK;KACnC,CAAC;AACJ,CAAC"}
package/dist/ipc.d.ts ADDED
@@ -0,0 +1,26 @@
1
+ import type { ChildProcess } from "node:child_process";
2
+ import type { IpfsStore } from "@ipab/ipfs-store";
3
+ import { type OutputSpace } from "./space.js";
4
+ import type { Ownership } from "./history.js";
5
+ export declare class ContextServer {
6
+ readonly child: ChildProcess;
7
+ readonly store: IpfsStore;
8
+ readonly owner: Ownership;
9
+ readonly output: OutputSpace;
10
+ readonly fatal: (error: Error) => void;
11
+ readonly appCid: string;
12
+ private readonly requests;
13
+ private readonly reads;
14
+ private readonly writes;
15
+ private readonly tasks;
16
+ private openingReads;
17
+ private sequence;
18
+ private closed;
19
+ constructor(child: ChildProcess, store: IpfsStore, owner: Ownership, output: OutputSpace, fatal: (error: Error) => void, appCid: string);
20
+ private reply;
21
+ private receive;
22
+ private cancelRead;
23
+ private handle;
24
+ close(): Promise<void>;
25
+ }
26
+ //# sourceMappingURL=ipc.d.ts.map
@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
2
+ import { HostError, message, object } from "./errors.js";
3
+ import { requestFrame } from "./context.js";
4
+ import { CHUNK_BYTES, relativePath } from "./space.js";
5
+ class ByteQueue {
6
+ item;
7
+ wake;
8
+ ended = false;
9
+ error;
10
+ async push(bytes) {
11
+ if (this.ended || this.error || this.item)
12
+ throw new HostError("Output stream is closed or exceeds backpressure capacity.");
13
+ await new Promise((resolve) => {
14
+ this.item = { bytes, consumed: resolve };
15
+ this.wake?.();
16
+ });
17
+ const failure = this.failure();
18
+ if (failure)
19
+ throw failure;
20
+ }
21
+ end(error) {
22
+ this.ended = true;
23
+ this.error = error;
24
+ this.item?.consumed();
25
+ this.wake?.();
26
+ }
27
+ failure() {
28
+ return this.error;
29
+ }
30
+ async *[Symbol.asyncIterator]() {
31
+ for (;;) {
32
+ if (this.error)
33
+ throw this.error;
34
+ if (this.item) {
35
+ const item = this.item;
36
+ this.item = undefined;
37
+ item.consumed();
38
+ yield item.bytes;
39
+ }
40
+ else if (this.ended)
41
+ return;
42
+ else
43
+ await new Promise((resolve) => {
44
+ this.wake = resolve;
45
+ });
46
+ }
47
+ }
48
+ }
49
+ export class ContextServer {
50
+ child;
51
+ store;
52
+ owner;
53
+ output;
54
+ fatal;
55
+ appCid;
56
+ requests = new Map();
57
+ reads = new Map();
58
+ writes = new Map();
59
+ tasks = new Set();
60
+ openingReads = 0;
61
+ sequence = 0;
62
+ closed = false;
63
+ constructor(child, store, owner, output, fatal, appCid) {
64
+ this.child = child;
65
+ this.store = store;
66
+ this.owner = owner;
67
+ this.output = output;
68
+ this.fatal = fatal;
69
+ this.appCid = appCid;
70
+ child.on("message", this.receive);
71
+ }
72
+ reply(frame) {
73
+ if (this.child.connected)
74
+ this.child.send(frame, (error) => {
75
+ if (error && !this.closed)
76
+ this.fatal(error);
77
+ });
78
+ }
79
+ receive = (frame) => {
80
+ if (!requestFrame(frame)) {
81
+ this.fatal(new HostError("Invalid IPAB context frame."));
82
+ return;
83
+ }
84
+ if (this.closed) {
85
+ this.reply({
86
+ version: 1,
87
+ id: frame.id,
88
+ error: "Context server is closed.",
89
+ });
90
+ return;
91
+ }
92
+ if (frame.operation === "cancel") {
93
+ this.requests
94
+ .get(Number(frame.args))
95
+ ?.abort(new HostError("Context operation cancelled."));
96
+ return;
97
+ }
98
+ if (this.requests.size >= 32 || this.requests.has(frame.id)) {
99
+ this.fatal(new HostError("Context request capacity or identity violation."));
100
+ return;
101
+ }
102
+ const controller = new AbortController();
103
+ this.requests.set(frame.id, controller);
104
+ const task = this.handle(frame.operation, frame.args, controller.signal)
105
+ .then(async (result) => {
106
+ if (controller.signal.aborted) {
107
+ if (frame.operation === "read")
108
+ await this.cancelRead(Number(result), controller.signal.reason);
109
+ if (frame.operation === "link")
110
+ this.output.fail(controller.signal.reason);
111
+ controller.signal.throwIfAborted();
112
+ }
113
+ return result;
114
+ })
115
+ .then((result) => {
116
+ this.reply({ version: 1, id: frame.id, result });
117
+ }, (error) => {
118
+ this.reply({ version: 1, id: frame.id, error: message(error) });
119
+ })
120
+ .finally(() => {
121
+ this.requests.delete(frame.id);
122
+ this.tasks.delete(task);
123
+ });
124
+ this.tasks.add(task);
125
+ };
126
+ async cancelRead(id, reason) {
127
+ const stream = this.reads.get(id);
128
+ if (!stream)
129
+ return;
130
+ stream.controller.abort(reason);
131
+ try {
132
+ await stream.iterator.return?.();
133
+ }
134
+ finally {
135
+ this.reads.delete(id);
136
+ }
137
+ }
138
+ async handle(operation, args, signal) {
139
+ signal.throwIfAborted();
140
+ if (operation === "identity")
141
+ return this.appCid;
142
+ if (operation === "read") {
143
+ if (this.reads.size + this.writes.size + this.openingReads >= 32)
144
+ throw new HostError("Context stream capacity exceeded.");
145
+ const data = object(args, "read request");
146
+ if (typeof data.rootCid !== "string" || typeof data.path !== "string")
147
+ throw new HostError("Invalid read request.");
148
+ const root = CID.parse(data.rootCid).toV1().toString();
149
+ relativePath(data.path);
150
+ this.openingReads++;
151
+ try {
152
+ await this.owner.retain([root], signal);
153
+ signal.throwIfAborted();
154
+ const directory = await this.store.read(root, { signal });
155
+ signal.throwIfAborted();
156
+ if (directory.type !== "directory")
157
+ throw new HostError("Space root must be a UnixFS directory.");
158
+ const controller = new AbortController();
159
+ const readSignal = AbortSignal.any([signal, controller.signal]);
160
+ let iterator;
161
+ try {
162
+ const node = await this.store.read(`${root}/${data.path}`, {
163
+ signal: readSignal,
164
+ });
165
+ readSignal.throwIfAborted();
166
+ if (node.type === "directory")
167
+ throw new HostError("readFile requires a regular UnixFS file.");
168
+ iterator = node.content()[Symbol.asyncIterator]();
169
+ readSignal.throwIfAborted();
170
+ const id = ++this.sequence;
171
+ this.reads.set(id, {
172
+ iterator,
173
+ pending: undefined,
174
+ controller,
175
+ pulling: false,
176
+ });
177
+ return id;
178
+ }
179
+ catch (error) {
180
+ controller.abort(error);
181
+ await iterator?.return?.();
182
+ throw error;
183
+ }
184
+ }
185
+ finally {
186
+ this.openingReads--;
187
+ }
188
+ }
189
+ if (operation === "pull" || operation === "readCancel") {
190
+ const id = Number(args);
191
+ const stream = this.reads.get(id);
192
+ if (!stream)
193
+ throw new HostError("Unknown read stream.");
194
+ if (operation === "readCancel") {
195
+ await this.cancelRead(id, new HostError("Read cancelled."));
196
+ return null;
197
+ }
198
+ if (stream.pulling)
199
+ throw new HostError("Concurrent pulls on one read stream are not supported.");
200
+ stream.pulling = true;
201
+ const abort = () => {
202
+ stream.controller.abort(signal.reason);
203
+ };
204
+ signal.addEventListener("abort", abort, { once: true });
205
+ try {
206
+ stream.controller.signal.throwIfAborted();
207
+ if (!stream.pending) {
208
+ const result = await stream.iterator.next();
209
+ signal.throwIfAborted();
210
+ stream.controller.signal.throwIfAborted();
211
+ if (result.done) {
212
+ this.reads.delete(id);
213
+ return null;
214
+ }
215
+ signal.throwIfAborted();
216
+ stream.controller.signal.throwIfAborted();
217
+ stream.pending = result.value;
218
+ }
219
+ const result = stream.pending.slice(0, CHUNK_BYTES);
220
+ stream.pending =
221
+ stream.pending.length > CHUNK_BYTES
222
+ ? stream.pending.slice(CHUNK_BYTES)
223
+ : undefined;
224
+ return result;
225
+ }
226
+ finally {
227
+ stream.pulling = false;
228
+ signal.removeEventListener("abort", abort);
229
+ }
230
+ }
231
+ if (operation === "write") {
232
+ if (this.reads.size + this.writes.size + this.openingReads >= 32)
233
+ throw new HostError("Context stream capacity exceeded.");
234
+ const data = object(args, "write request");
235
+ if (typeof data.path !== "string")
236
+ throw new HostError("Invalid write path.");
237
+ const id = ++this.sequence;
238
+ const queue = new ByteQueue();
239
+ const task = this.output.write(data.path, queue);
240
+ const stream = { queue, task, writing: false };
241
+ this.writes.set(id, stream);
242
+ void task.catch((error) => {
243
+ queue.end(new HostError(message(error)));
244
+ });
245
+ return id;
246
+ }
247
+ if (operation === "chunk" ||
248
+ operation === "end" ||
249
+ operation === "writeCancel") {
250
+ const data = operation === "chunk" ? object(args, "write chunk") : { id: args };
251
+ const id = Number(data.id);
252
+ const stream = this.writes.get(id);
253
+ if (!stream)
254
+ throw new HostError("Unknown write stream.");
255
+ if (operation === "writeCancel") {
256
+ const error = new HostError("Output write cancelled.", 1, "OUTPUT_CANCELLED", "commit");
257
+ this.output.fail(error);
258
+ stream.queue.end(error);
259
+ await stream.task.catch(() => { });
260
+ this.writes.delete(id);
261
+ return null;
262
+ }
263
+ if (stream.writing)
264
+ throw new HostError("Concurrent writes violate stream backpressure.");
265
+ stream.writing = true;
266
+ try {
267
+ if (operation === "end") {
268
+ stream.queue.end();
269
+ const cid = await stream.task;
270
+ this.writes.delete(id);
271
+ return cid;
272
+ }
273
+ if (!(data.bytes instanceof Uint8Array) ||
274
+ data.bytes.byteLength > CHUNK_BYTES)
275
+ throw new HostError("Write chunk exceeds 65536 bytes or is not bytes.");
276
+ signal.throwIfAborted();
277
+ const cancel = () => {
278
+ stream.queue.end(new HostError("Write cancelled."));
279
+ };
280
+ signal.addEventListener("abort", cancel, { once: true });
281
+ try {
282
+ await stream.queue.push(data.bytes);
283
+ }
284
+ finally {
285
+ signal.removeEventListener("abort", cancel);
286
+ }
287
+ return null;
288
+ }
289
+ catch (error) {
290
+ this.output.fail(error);
291
+ stream.queue.end(new HostError(message(error)));
292
+ throw error;
293
+ }
294
+ finally {
295
+ stream.writing = false;
296
+ }
297
+ }
298
+ if (operation === "link") {
299
+ const data = object(args, "link request");
300
+ if (typeof data.path !== "string" || typeof data.rootCid !== "string")
301
+ throw new HostError("Invalid link request.");
302
+ signal.throwIfAborted();
303
+ await this.output.link(data.path, data.rootCid, signal);
304
+ signal.throwIfAborted();
305
+ return null;
306
+ }
307
+ throw new HostError("Unknown context operation.");
308
+ }
309
+ async close() {
310
+ this.closed = true;
311
+ this.child.off("message", this.receive);
312
+ for (const request of this.requests.values())
313
+ request.abort(new HostError("App finished."));
314
+ for (const stream of this.writes.values()) {
315
+ const error = new HostError("App exited with unfinished output writes.", 1, "OUTPUT_PENDING", "commit");
316
+ this.output.fail(error);
317
+ stream.queue.end(error);
318
+ }
319
+ for (const stream of this.reads.values())
320
+ stream.controller.abort(new HostError("App finished."));
321
+ await Promise.allSettled([
322
+ ...this.tasks,
323
+ ...[...this.writes.values()].map((stream) => stream.task),
324
+ ]);
325
+ await Promise.all([...this.reads.values()].map(async (stream) => {
326
+ await stream.iterator.return?.();
327
+ }));
328
+ this.reads.clear();
329
+ this.writes.clear();
330
+ }
331
+ }
332
+ //# sourceMappingURL=ipc.js.map