@ipab/cli 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 +88 -16
- package/dist/app-templates.js +1 -1
- package/dist/app-templates.js.map +1 -1
- package/dist/builtins/skill.ipab.car +0 -0
- package/dist/builtins/template.node.car +0 -0
- package/dist/builtins/template.web-app.car +0 -0
- package/dist/cli.d.ts +6 -2
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +656 -278
- package/dist/cli.js.map +1 -1
- package/dist/config-secret.d.ts +1 -1
- package/dist/config-secret.d.ts.map +1 -1
- package/dist/config-secret.js +2 -73
- package/dist/config-secret.js.map +1 -1
- package/dist/config.d.ts +1 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +12 -1
- package/dist/config.js.map +1 -1
- package/dist/content-provider.d.ts +1 -1
- package/dist/content-provider.d.ts.map +1 -1
- package/dist/content-provider.js +4 -3
- package/dist/content-provider.js.map +1 -1
- package/dist/context-types.d.ts +12 -0
- package/dist/context-types.d.ts.map +1 -0
- package/dist/context-types.js +2 -0
- package/dist/context-types.js.map +1 -0
- package/dist/context.d.ts +6 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +24 -9
- package/dist/context.js.map +1 -1
- package/dist/doctor.d.ts +29 -0
- package/dist/doctor.d.ts.map +1 -0
- package/dist/doctor.js +576 -0
- package/dist/doctor.js.map +1 -0
- package/dist/identity.d.ts +1 -1
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +9 -9
- package/dist/identity.js.map +1 -1
- package/dist/ipc.d.ts +3 -2
- package/dist/ipc.d.ts.map +1 -1
- package/dist/ipc.js +33 -9
- package/dist/ipc.js.map +1 -1
- package/dist/kubo-install.d.ts +26 -0
- package/dist/kubo-install.d.ts.map +1 -0
- package/dist/kubo-install.js +724 -0
- package/dist/kubo-install.js.map +1 -0
- package/dist/kubo-peering.d.ts +11 -0
- package/dist/kubo-peering.d.ts.map +1 -0
- package/dist/kubo-peering.js +93 -0
- package/dist/kubo-peering.js.map +1 -0
- package/dist/kubo-peers.d.ts +12 -0
- package/dist/kubo-peers.d.ts.map +1 -0
- package/dist/kubo-peers.js +118 -0
- package/dist/kubo-peers.js.map +1 -0
- package/dist/kubo-release.d.ts +22 -0
- package/dist/kubo-release.d.ts.map +1 -0
- package/dist/kubo-release.js +176 -0
- package/dist/kubo-release.js.map +1 -0
- package/dist/kubo.d.ts +12 -4
- package/dist/kubo.d.ts.map +1 -1
- package/dist/kubo.js +218 -259
- package/dist/kubo.js.map +1 -1
- package/dist/pins.d.ts +15 -17
- package/dist/pins.d.ts.map +1 -1
- package/dist/pins.js +144 -106
- package/dist/pins.js.map +1 -1
- package/dist/publish-source.d.ts +3 -0
- package/dist/publish-source.d.ts.map +1 -0
- package/dist/publish-source.js +85 -0
- package/dist/publish-source.js.map +1 -0
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +4 -2
- package/dist/runtime.js.map +1 -1
- package/dist/state-storage-windows.d.ts +2 -0
- package/dist/state-storage-windows.d.ts.map +1 -0
- package/dist/state-storage-windows.js +114 -0
- package/dist/state-storage-windows.js.map +1 -0
- package/dist/state-storage.d.ts +5 -0
- package/dist/state-storage.d.ts.map +1 -0
- package/dist/state-storage.js +330 -0
- package/dist/state-storage.js.map +1 -0
- package/dist/state.d.ts +42 -8
- package/dist/state.d.ts.map +1 -1
- package/dist/state.js +254 -98
- package/dist/state.js.map +1 -1
- package/dist/swarm-envelope.d.ts +1 -0
- package/dist/swarm-envelope.d.ts.map +1 -1
- package/dist/swarm-envelope.js +12 -0
- package/dist/swarm-envelope.js.map +1 -1
- package/dist/swarm-files.d.ts +1 -1
- package/dist/swarm-files.d.ts.map +1 -1
- package/dist/swarm-files.js +4 -1
- package/dist/swarm-files.js.map +1 -1
- package/dist/swarm.d.ts +70 -11
- package/dist/swarm.d.ts.map +1 -1
- package/dist/swarm.js +1033 -167
- package/dist/swarm.js.map +1 -1
- package/package.json +4 -4
- package/src/app-templates.ts +1 -1
- package/src/cli.ts +827 -356
- package/src/config-secret.ts +2 -84
- package/src/config.ts +14 -1
- package/src/content-provider.ts +4 -2
- package/src/context-types.ts +11 -0
- package/src/context.ts +39 -9
- package/src/doctor.ts +902 -0
- package/src/identity.ts +10 -10
- package/src/ipc.ts +49 -8
- package/src/kubo-install.ts +936 -0
- package/src/kubo-peering.ts +126 -0
- package/src/kubo-peers.ts +145 -0
- package/src/kubo-release.ts +230 -0
- package/src/kubo.ts +302 -336
- package/src/pins.ts +188 -126
- package/src/publish-source.ts +103 -0
- package/src/runtime.ts +10 -2
- package/src/state-storage-windows.ts +134 -0
- package/src/state-storage.ts +375 -0
- package/src/state.ts +410 -118
- package/src/swarm-envelope.ts +14 -0
- package/src/swarm-files.ts +4 -0
- package/src/swarm.ts +1482 -207
- package/dist/setup.d.ts +0 -7
- package/dist/setup.d.ts.map +0 -1
- package/dist/setup.js +0 -22
- package/dist/setup.js.map +0 -1
- package/src/setup.ts +0 -21
package/src/pins.ts
CHANGED
|
@@ -5,19 +5,14 @@ import { openBundle } from "@ipab/bundle";
|
|
|
5
5
|
import { HostError, message } from "./errors.js";
|
|
6
6
|
import { alive, type StoreFactory } from "./history.js";
|
|
7
7
|
import {
|
|
8
|
-
exactCid,
|
|
9
|
-
pinName,
|
|
10
8
|
bundlePinMetadata,
|
|
9
|
+
exactCid,
|
|
10
|
+
type ContentPin,
|
|
11
11
|
type NamedPin,
|
|
12
|
-
type PinMetadata,
|
|
13
12
|
type State,
|
|
14
13
|
} from "./state.js";
|
|
15
14
|
|
|
16
15
|
export function pinDetails(pin: NamedPin) {
|
|
17
|
-
if (pin.status !== "ready" || !pin.metadata)
|
|
18
|
-
throw new HostError(
|
|
19
|
-
`Pin "${pin.name}" is ${pin.status}. Retry ipab pin add ${pin.name} ${pin.cid}, or ipab pin delete ${pin.name}.`,
|
|
20
|
-
);
|
|
21
16
|
return {
|
|
22
17
|
name: pin.name,
|
|
23
18
|
cid: pin.cid,
|
|
@@ -26,15 +21,7 @@ export function pinDetails(pin: NamedPin) {
|
|
|
26
21
|
};
|
|
27
22
|
}
|
|
28
23
|
|
|
29
|
-
function
|
|
30
|
-
pinName(name);
|
|
31
|
-
const pin = state.pin(name);
|
|
32
|
-
if (!pin)
|
|
33
|
-
throw new HostError(`Pin "${name}" does not exist. Run ipab pin list.`);
|
|
34
|
-
return pin;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function idleHold(state: State, pin: NamedPin) {
|
|
24
|
+
function claim(state: State, pin: ContentPin) {
|
|
38
25
|
const hold = state.hold(pin.owner);
|
|
39
26
|
if (!hold)
|
|
40
27
|
throw new HostError(
|
|
@@ -42,159 +29,234 @@ function idleHold(state: State, pin: NamedPin) {
|
|
|
42
29
|
);
|
|
43
30
|
if (hold.pid !== null && alive(hold.pid))
|
|
44
31
|
throw new HostError(
|
|
45
|
-
`Pin
|
|
32
|
+
`Pin ${pin.reference} has an active operation; retry after it finishes.`,
|
|
46
33
|
);
|
|
47
|
-
|
|
34
|
+
state.saveHold({ ...hold, pid: process.pid });
|
|
48
35
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
36
|
+
export function refreshPinClaim(
|
|
37
|
+
state: State,
|
|
38
|
+
saved: ContentPin,
|
|
39
|
+
): ContentPin | undefined {
|
|
40
|
+
return state.transaction(() => {
|
|
41
|
+
const current = state.contentPin(saved.reference);
|
|
42
|
+
if (
|
|
43
|
+
!current ||
|
|
44
|
+
current.owner !== saved.owner ||
|
|
45
|
+
current.status === "deleting"
|
|
46
|
+
)
|
|
47
|
+
return undefined;
|
|
48
|
+
claim(state, current);
|
|
49
|
+
return current;
|
|
50
|
+
});
|
|
63
51
|
}
|
|
64
52
|
|
|
65
|
-
export
|
|
53
|
+
export function reservePin(
|
|
66
54
|
state: State,
|
|
67
|
-
|
|
68
|
-
input: string,
|
|
55
|
+
reference: string,
|
|
69
56
|
configuration: IpfsStore["configuration"],
|
|
70
|
-
|
|
71
|
-
) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const pin = state.transaction(() => {
|
|
75
|
-
const old = state.pin(name);
|
|
57
|
+
shared?: { swarm: string; name: string },
|
|
58
|
+
): ContentPin {
|
|
59
|
+
return state.transaction(() => {
|
|
60
|
+
const old = state.contentPin(reference);
|
|
76
61
|
if (old) {
|
|
77
|
-
if (old.status
|
|
78
|
-
throw new HostError(
|
|
79
|
-
|
|
80
|
-
);
|
|
81
|
-
const hold = idleHold(state, old);
|
|
82
|
-
state.saveHold({ ...hold, pid: process.pid });
|
|
62
|
+
if (old.status === "deleting")
|
|
63
|
+
throw new HostError("Pin deletion is incomplete; retry unpin first.");
|
|
64
|
+
claim(state, old);
|
|
83
65
|
return old;
|
|
84
66
|
}
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
cid,
|
|
89
|
-
|
|
67
|
+
const pin: ContentPin = {
|
|
68
|
+
reference,
|
|
69
|
+
owner: `ipab:${state.identity}:pin:${randomUUID()}`,
|
|
70
|
+
cid: null,
|
|
71
|
+
targetCid: null,
|
|
72
|
+
swarm: shared?.swarm ?? null,
|
|
73
|
+
name: shared?.name ?? null,
|
|
90
74
|
status: "pending",
|
|
91
|
-
|
|
75
|
+
freshness: "stale",
|
|
76
|
+
error: null,
|
|
92
77
|
};
|
|
93
|
-
state.
|
|
94
|
-
// Reserve the name and original backend before any Store side effect.
|
|
78
|
+
state.saveContentPin(pin);
|
|
95
79
|
state.saveHold({
|
|
96
|
-
owner,
|
|
80
|
+
owner: pin.owner,
|
|
97
81
|
backend: configuration,
|
|
98
82
|
roots: [],
|
|
99
83
|
pid: process.pid,
|
|
100
84
|
session: null,
|
|
101
85
|
recordId: null,
|
|
102
86
|
});
|
|
103
|
-
return
|
|
87
|
+
return pin;
|
|
104
88
|
});
|
|
105
|
-
|
|
106
|
-
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Confirm the complete new version before reducing this owner's saved roots. */
|
|
92
|
+
export async function retainPin(
|
|
93
|
+
state: State,
|
|
94
|
+
pin: ContentPin,
|
|
95
|
+
store: IpfsStore,
|
|
96
|
+
version: { cid: string; targetCid: string; roots: readonly string[] },
|
|
97
|
+
): Promise<ContentPin> {
|
|
98
|
+
const backend = state.hold(pin.owner)!.backend;
|
|
99
|
+
let base: IpfsStore | undefined;
|
|
100
|
+
let original: IpfsStore | undefined;
|
|
107
101
|
try {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
102
|
+
if (JSON.stringify(backend) !== JSON.stringify(store.configuration)) {
|
|
103
|
+
base = new IpfsStore(backend);
|
|
104
|
+
original = base.withLocalSource({
|
|
105
|
+
block: { get: (cid, options) => store.block.get(cid, options) },
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
const retainedStore = original ?? store;
|
|
109
|
+
await base?.close();
|
|
110
|
+
await retainedStore.recoverOwner(
|
|
111
|
+
pin.owner,
|
|
112
|
+
() => state.hold(pin.owner)!.roots,
|
|
113
|
+
);
|
|
114
|
+
await openBundle(CID.parse(version.cid), {
|
|
115
|
+
resolveCid: (cid) => retainedStore.block.get(cid.toString()),
|
|
115
116
|
});
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
117
|
+
const roots = [...new Set(version.roots.map((root) => exactCid(root)))];
|
|
118
|
+
if (
|
|
119
|
+
!roots.includes(exactCid(version.cid)) ||
|
|
120
|
+
!roots.includes(exactCid(version.targetCid))
|
|
121
|
+
)
|
|
122
|
+
throw new HostError(
|
|
123
|
+
"Pin roots must include its exact payload and target version.",
|
|
124
|
+
);
|
|
125
|
+
await retainedStore.retain(pin.owner, roots);
|
|
126
|
+
const hold = state.hold(pin.owner)!;
|
|
127
|
+
state.saveHold({ ...hold, roots: [...new Set([...hold.roots, ...roots])] });
|
|
128
|
+
await retainedStore.confirmRetained(pin.owner, roots);
|
|
129
|
+
const ready: ContentPin = {
|
|
130
|
+
...pin,
|
|
131
|
+
cid: version.cid,
|
|
132
|
+
targetCid: version.targetCid,
|
|
133
|
+
status: "ready",
|
|
134
|
+
freshness: "current",
|
|
135
|
+
error: null,
|
|
136
|
+
};
|
|
120
137
|
state.transaction(() => {
|
|
121
|
-
state.
|
|
122
|
-
state.saveHold({ ...state.hold(pin.owner)!,
|
|
138
|
+
state.saveContentPin(ready);
|
|
139
|
+
state.saveHold({ ...state.hold(pin.owner)!, roots });
|
|
123
140
|
});
|
|
124
|
-
|
|
125
|
-
|
|
141
|
+
await retainedStore.reconcile(
|
|
142
|
+
pin.owner,
|
|
143
|
+
() => state.hold(pin.owner)!.roots,
|
|
144
|
+
);
|
|
145
|
+
return ready;
|
|
126
146
|
} catch (cause) {
|
|
147
|
+
const current = state.contentPin(pin.reference)!;
|
|
148
|
+
state.saveContentPin({
|
|
149
|
+
...current,
|
|
150
|
+
freshness: "stale",
|
|
151
|
+
error: message(cause),
|
|
152
|
+
});
|
|
127
153
|
throw new HostError(
|
|
128
|
-
`Pin
|
|
154
|
+
`Pin ${pin.reference} is stale: ${message(cause)}. Its last verified version and durable recovery data are preserved; retry pin or unpin.`,
|
|
129
155
|
1,
|
|
130
156
|
"PIN_INCOMPLETE",
|
|
131
157
|
);
|
|
132
158
|
} finally {
|
|
133
159
|
try {
|
|
134
|
-
await
|
|
160
|
+
await original?.close();
|
|
161
|
+
await base?.close();
|
|
135
162
|
} finally {
|
|
136
163
|
const hold = state.hold(pin.owner);
|
|
137
|
-
if (hold
|
|
164
|
+
if (hold) state.saveHold({ ...hold, pid: null });
|
|
138
165
|
}
|
|
139
166
|
}
|
|
140
167
|
}
|
|
141
168
|
|
|
142
|
-
export async function
|
|
169
|
+
export async function pinCid(
|
|
143
170
|
state: State,
|
|
144
|
-
|
|
145
|
-
|
|
171
|
+
input: string,
|
|
172
|
+
store: IpfsStore,
|
|
173
|
+
roots: readonly string[] = [input],
|
|
146
174
|
) {
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
175
|
+
const cid = exactCid(input);
|
|
176
|
+
const pin = reservePin(state, cid, store.configuration);
|
|
177
|
+
return retainPin(state, pin, store, { cid, targetCid: cid, roots });
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function failedPin(
|
|
181
|
+
state: State,
|
|
182
|
+
reference: string,
|
|
183
|
+
cause: unknown,
|
|
184
|
+
missing = false,
|
|
185
|
+
) {
|
|
186
|
+
const pin = state.contentPin(reference);
|
|
187
|
+
if (!pin) return;
|
|
188
|
+
state.saveContentPin({
|
|
189
|
+
...pin,
|
|
190
|
+
freshness: missing ? "missing" : "stale",
|
|
191
|
+
error: message(cause),
|
|
153
192
|
});
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
193
|
+
const hold = state.hold(pin.owner);
|
|
194
|
+
if (hold && !missing) state.saveHold({ ...hold, pid: null });
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export async function unpin(
|
|
198
|
+
state: State,
|
|
199
|
+
reference: string,
|
|
200
|
+
factory: StoreFactory = (config) => new IpfsStore(config),
|
|
201
|
+
) {
|
|
202
|
+
const pin = state.contentPin(reference);
|
|
203
|
+
const archived = state.archivedPins().filter((pin) => pin.cid === reference);
|
|
204
|
+
const owners = [...(pin ? [pin] : []), ...archived];
|
|
205
|
+
for (const record of owners) {
|
|
161
206
|
state.transaction(() => {
|
|
162
|
-
|
|
163
|
-
|
|
207
|
+
if ("reference" in record) claim(state, record);
|
|
208
|
+
else {
|
|
209
|
+
const hold = state.hold(record.owner);
|
|
210
|
+
if (!hold)
|
|
211
|
+
throw new HostError(
|
|
212
|
+
"Archived pin retention metadata is missing; recovery is blocked.",
|
|
213
|
+
);
|
|
214
|
+
if (hold.pid !== null && alive(hold.pid))
|
|
215
|
+
throw new HostError("Archived pin has an active operation.");
|
|
216
|
+
state.saveHold({ ...hold, pid: process.pid });
|
|
217
|
+
}
|
|
218
|
+
if ("reference" in record)
|
|
219
|
+
state.saveContentPin({ ...record, status: "deleting" });
|
|
220
|
+
state.saveHold({ ...state.hold(record.owner)!, roots: [] });
|
|
164
221
|
});
|
|
165
|
-
|
|
166
|
-
removed: [name],
|
|
167
|
-
retentionClaimsReleased: true,
|
|
168
|
-
garbageCollectionGuaranteed: false,
|
|
169
|
-
};
|
|
170
|
-
} catch (cause) {
|
|
171
|
-
throw new HostError(
|
|
172
|
-
`Pin deletion incomplete for "${name}": ${message(cause)}. Retry ipab pin delete ${name}; original backend and recovery data are preserved. No GC requested.`,
|
|
173
|
-
1,
|
|
174
|
-
"RELEASE_FAILED",
|
|
175
|
-
"cleanup",
|
|
176
|
-
);
|
|
177
|
-
} finally {
|
|
222
|
+
let store: IpfsStore | undefined;
|
|
178
223
|
try {
|
|
179
|
-
|
|
224
|
+
store = factory(state.hold(record.owner)!.backend);
|
|
225
|
+
await store.recoverOwner(
|
|
226
|
+
record.owner,
|
|
227
|
+
() => state.hold(record.owner)!.roots,
|
|
228
|
+
);
|
|
229
|
+
await store.reconcile(
|
|
230
|
+
record.owner,
|
|
231
|
+
() => state.hold(record.owner)!.roots,
|
|
232
|
+
);
|
|
233
|
+
await store.close();
|
|
234
|
+
store = undefined;
|
|
235
|
+
state.transaction(() => {
|
|
236
|
+
if ("reference" in record) state.deleteContentPin(record.reference);
|
|
237
|
+
else state.deletePin(record.name);
|
|
238
|
+
state.deleteHold(record.owner);
|
|
239
|
+
});
|
|
240
|
+
} catch (cause) {
|
|
241
|
+
throw new HostError(
|
|
242
|
+
`Unpin incomplete for ${reference}: ${message(cause)}. Retry the same unpin; original Store ownership and recovery data are preserved. No GC requested.`,
|
|
243
|
+
1,
|
|
244
|
+
"RELEASE_FAILED",
|
|
245
|
+
"cleanup",
|
|
246
|
+
);
|
|
180
247
|
} finally {
|
|
181
|
-
|
|
182
|
-
|
|
248
|
+
try {
|
|
249
|
+
await store?.close();
|
|
250
|
+
} finally {
|
|
251
|
+
const hold = state.hold(record.owner);
|
|
252
|
+
if (hold) state.saveHold({ ...hold, pid: null });
|
|
253
|
+
}
|
|
183
254
|
}
|
|
184
255
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
const details = pinDetails(pin);
|
|
192
|
-
idleHold(state, pin);
|
|
193
|
-
if (oldName === newName) return details;
|
|
194
|
-
if (state.pin(newName))
|
|
195
|
-
throw new HostError(`Pin name "${newName}" is occupied.`);
|
|
196
|
-
state.deletePin(oldName);
|
|
197
|
-
state.savePin({ ...pin, name: newName });
|
|
198
|
-
return { ...details, name: newName };
|
|
199
|
-
});
|
|
256
|
+
return {
|
|
257
|
+
reference,
|
|
258
|
+
releasedOwners: owners.map((record) => record.owner),
|
|
259
|
+
retentionClaimsReleased: true,
|
|
260
|
+
garbageCollectionGuaranteed: false,
|
|
261
|
+
};
|
|
200
262
|
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { lstat } from "node:fs/promises";
|
|
2
|
+
import { basename, dirname, extname, resolve } from "node:path";
|
|
3
|
+
import { CID } from "multiformats/cid";
|
|
4
|
+
import { CarBlockIterator } from "@ipld/car/iterator";
|
|
5
|
+
import { encodeBundleCar, memoryTree, readBytes } from "@ipab/bundle-builder";
|
|
6
|
+
import { filesystemTree } from "@ipab/bundle-builder/node";
|
|
7
|
+
import { bundleLimits } from "@ipab/bundle";
|
|
8
|
+
import type { IpfsStore } from "@ipab/ipfs-store";
|
|
9
|
+
import { HostError } from "./errors.js";
|
|
10
|
+
import { importBundle, nodeError, selectedTree } from "./authoring.js";
|
|
11
|
+
import { captureSource, sourceFingerprint } from "./source-bundle.js";
|
|
12
|
+
import { exactCid } from "./state.js";
|
|
13
|
+
|
|
14
|
+
export async function publicationSource(
|
|
15
|
+
input: string,
|
|
16
|
+
store: IpfsStore,
|
|
17
|
+
): Promise<string> {
|
|
18
|
+
try {
|
|
19
|
+
CID.parse(input);
|
|
20
|
+
return exactCid(input);
|
|
21
|
+
} catch {
|
|
22
|
+
// Non-CID inputs must be explicit existing local sources.
|
|
23
|
+
}
|
|
24
|
+
const path = resolve(input);
|
|
25
|
+
let stat;
|
|
26
|
+
try {
|
|
27
|
+
stat = await lstat(path);
|
|
28
|
+
} catch (cause) {
|
|
29
|
+
if (nodeError(cause, "ENOENT"))
|
|
30
|
+
throw new HostError(
|
|
31
|
+
"Publication SOURCE must be an exact CID, a CAR, a regular file or a directory.",
|
|
32
|
+
);
|
|
33
|
+
throw cause;
|
|
34
|
+
}
|
|
35
|
+
if (stat.isSymbolicLink() || (!stat.isFile() && !stat.isDirectory()))
|
|
36
|
+
throw new HostError(
|
|
37
|
+
"Publication SOURCE must be a regular file or directory, never a symlink.",
|
|
38
|
+
);
|
|
39
|
+
if (stat.isFile() && extname(path).toLowerCase() === ".car")
|
|
40
|
+
return (await importBundle(path, () => Promise.resolve(store))).cid;
|
|
41
|
+
let tree;
|
|
42
|
+
let snapshot: Map<string, Uint8Array> | undefined;
|
|
43
|
+
if (stat.isDirectory()) {
|
|
44
|
+
try {
|
|
45
|
+
await lstat(resolve(path, ".bundle.yaml"));
|
|
46
|
+
tree = await selectedTree(path, undefined, resolve(path, "dist"), {});
|
|
47
|
+
} catch (cause) {
|
|
48
|
+
if (!nodeError(cause, "ENOENT")) throw cause;
|
|
49
|
+
snapshot = await captureSource(path, []);
|
|
50
|
+
tree = memoryTree(snapshot);
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
53
|
+
const name = basename(path);
|
|
54
|
+
const types: Record<string, string> = {
|
|
55
|
+
".json": "application/json",
|
|
56
|
+
".yaml": "application/yaml",
|
|
57
|
+
".yml": "application/yaml",
|
|
58
|
+
".md": "text/markdown",
|
|
59
|
+
".txt": "text/plain",
|
|
60
|
+
};
|
|
61
|
+
const contentType = types[extname(path).toLowerCase()];
|
|
62
|
+
if (!contentType || name.startsWith("."))
|
|
63
|
+
throw new HostError(
|
|
64
|
+
"File entry/type is ambiguous. Put the file in a directory with an explicit .bundle.yaml and referenced type file; standalone .json, .yaml, .yml, .md and .txt files are supported.",
|
|
65
|
+
);
|
|
66
|
+
const data = await readBytes(
|
|
67
|
+
await filesystemTree(dirname(path)),
|
|
68
|
+
name,
|
|
69
|
+
bundleLimits.bytes,
|
|
70
|
+
{},
|
|
71
|
+
);
|
|
72
|
+
const bytes = (value: unknown) =>
|
|
73
|
+
new TextEncoder().encode(JSON.stringify(value));
|
|
74
|
+
tree = memoryTree(
|
|
75
|
+
new Map([
|
|
76
|
+
[
|
|
77
|
+
".bundle.yaml",
|
|
78
|
+
bytes({ title: name, entry: name, type: ".bundle-type.yaml" }),
|
|
79
|
+
],
|
|
80
|
+
[".bundle-type.yaml", bytes({ contentType })],
|
|
81
|
+
[name, data],
|
|
82
|
+
]),
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
const encoded = await encodeBundleCar(tree);
|
|
86
|
+
try {
|
|
87
|
+
for await (const block of await CarBlockIterator.fromIterable(
|
|
88
|
+
encoded.stream,
|
|
89
|
+
))
|
|
90
|
+
await store.put(block.cid, block.bytes);
|
|
91
|
+
if (
|
|
92
|
+
snapshot &&
|
|
93
|
+
sourceFingerprint(snapshot) !==
|
|
94
|
+
sourceFingerprint(await captureSource(path, []))
|
|
95
|
+
)
|
|
96
|
+
throw new HostError(
|
|
97
|
+
"Publication source changed while capturing its Bundle; retry.",
|
|
98
|
+
);
|
|
99
|
+
return encoded.cid.toString();
|
|
100
|
+
} finally {
|
|
101
|
+
await encoded.close();
|
|
102
|
+
}
|
|
103
|
+
}
|
package/src/runtime.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { ContextServer } from "./ipc.js";
|
|
|
10
10
|
import { loadApp, OutputSpace } from "./space.js";
|
|
11
11
|
import type { RunRecord, State } from "./state.js";
|
|
12
12
|
import { ExecutionLogs } from "./logs.js";
|
|
13
|
+
import type { BundleInspection } from "./context.js";
|
|
13
14
|
|
|
14
15
|
export interface Invocation {
|
|
15
16
|
origin: "exec" | "open";
|
|
@@ -23,6 +24,11 @@ export interface ExecutionOptions {
|
|
|
23
24
|
log?: boolean;
|
|
24
25
|
signal?: AbortSignal;
|
|
25
26
|
inputSchema?: string;
|
|
27
|
+
resolveBundleReference?: (
|
|
28
|
+
reference: string,
|
|
29
|
+
signal: AbortSignal,
|
|
30
|
+
swarm?: string,
|
|
31
|
+
) => Promise<Pick<BundleInspection, "cid" | "targetCid" | "swarm">>;
|
|
26
32
|
}
|
|
27
33
|
|
|
28
34
|
export async function execute(
|
|
@@ -114,7 +120,9 @@ export async function execute(
|
|
|
114
120
|
"validation",
|
|
115
121
|
);
|
|
116
122
|
phase = "startup";
|
|
117
|
-
|
|
123
|
+
const runtime = join(state.directory, "runtime");
|
|
124
|
+
await mkdir(runtime, { recursive: true, mode: 0o700 });
|
|
125
|
+
await mkdir(directory, { mode: 0o700 });
|
|
118
126
|
directoryCreated = true;
|
|
119
127
|
await writeFile(entry, app.source, { flag: "wx", mode: 0o600 });
|
|
120
128
|
entryCreated = true;
|
|
@@ -161,7 +169,7 @@ export async function execute(
|
|
|
161
169
|
stop(error);
|
|
162
170
|
},
|
|
163
171
|
invocation.appCid,
|
|
164
|
-
|
|
172
|
+
options.resolveBundleReference,
|
|
165
173
|
);
|
|
166
174
|
const result = await new Promise<{
|
|
167
175
|
code: number | null;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
|
|
4
|
+
// One process per preflight, not one per path or SQL statement. Use framework
|
|
5
|
+
// APIs directly: loading PowerShell's filesystem/ACL cmdlets dominates startup.
|
|
6
|
+
const script = `
|
|
7
|
+
$ErrorActionPreference = 'Stop'
|
|
8
|
+
$sid = [Security.Principal.WindowsIdentity]::GetCurrent().User
|
|
9
|
+
$sidType = [Security.Principal.SecurityIdentifier]
|
|
10
|
+
$root = $env:IPAB_STATE_ROOT
|
|
11
|
+
$repair = $env:IPAB_STATE_ACTION -eq 'prepare'
|
|
12
|
+
$managed = $env:IPAB_STATE_MANAGED -eq '1'
|
|
13
|
+
function New-PrivateAcl($dir) {
|
|
14
|
+
if ($dir) {
|
|
15
|
+
$a = [Security.AccessControl.DirectorySecurity]::new()
|
|
16
|
+
$r = [Security.AccessControl.FileSystemAccessRule]::new($sid,'FullControl','ContainerInherit,ObjectInherit','None','Allow')
|
|
17
|
+
} else {
|
|
18
|
+
$a = [Security.AccessControl.FileSecurity]::new()
|
|
19
|
+
$r = [Security.AccessControl.FileSystemAccessRule]::new($sid,'FullControl','Allow')
|
|
20
|
+
}
|
|
21
|
+
$a.SetAccessRuleProtection($true,$false)
|
|
22
|
+
$a.AddAccessRule($r)
|
|
23
|
+
return $a
|
|
24
|
+
}
|
|
25
|
+
if ($repair -and ![IO.Directory]::Exists($root)) {
|
|
26
|
+
$new = New-PrivateAcl $true
|
|
27
|
+
$new.SetOwner($sid)
|
|
28
|
+
[void][IO.Directory]::CreateDirectory($root,$new)
|
|
29
|
+
}
|
|
30
|
+
$changes = @()
|
|
31
|
+
$paths = @($root)
|
|
32
|
+
foreach ($name in ([string]$env:IPAB_STATE_ENTRIES).Split('|',[StringSplitOptions]::RemoveEmptyEntries)) {
|
|
33
|
+
$paths += [IO.Path]::Combine($root,$name)
|
|
34
|
+
}
|
|
35
|
+
function Check-Path($path) {
|
|
36
|
+
try {
|
|
37
|
+
$attrs = [IO.File]::GetAttributes($path)
|
|
38
|
+
if ($attrs -band [IO.FileAttributes]::ReparsePoint) { throw 'Invalid state path' }
|
|
39
|
+
$dir = [bool]($attrs -band [IO.FileAttributes]::Directory)
|
|
40
|
+
$isDir = $path -eq $root -or [IO.Path]::GetFileName($path) -in 'runtime','logs','kubo-bin','kubo-cache','swarm-nodes','swarm-credentials'
|
|
41
|
+
if ($dir -ne $isDir) { throw 'Invalid state type' }
|
|
42
|
+
if ($dir) { $acl = [IO.Directory]::GetAccessControl($path) }
|
|
43
|
+
else { $acl = [IO.File]::GetAccessControl($path) }
|
|
44
|
+
}
|
|
45
|
+
catch [IO.FileNotFoundException] {
|
|
46
|
+
if ($path -match 'state\\.sqlite-(wal|shm|journal)$') { return }
|
|
47
|
+
throw
|
|
48
|
+
}
|
|
49
|
+
if ($acl.GetOwner($sidType).Value -ne $sid.Value) { throw 'Invalid owner' }
|
|
50
|
+
if ($env:IPAB_STATE_ACTION -eq 'owners') { return }
|
|
51
|
+
if ($dir -and $path -ne $root -and [IO.Path]::GetFileName($path) -notin 'runtime','logs') { return }
|
|
52
|
+
$private = $false
|
|
53
|
+
$raw = [Security.AccessControl.RawSecurityDescriptor]::new($acl.GetSecurityDescriptorBinaryForm(),0)
|
|
54
|
+
foreach ($ace in $raw.DiscretionaryAcl) {
|
|
55
|
+
if ($ace.AceType -ne 'AccessAllowed' -or $ace.SecurityIdentifier.Value -ne $sid.Value) { $private = $false; break }
|
|
56
|
+
$flags = [int]$ace.AceFlags
|
|
57
|
+
if ($ace.AccessMask -eq [int][Security.AccessControl.FileSystemRights]::FullControl -and !($flags -band 8) -and (!$dir -or ($flags -band 15) -eq 3)) { $private = $true }
|
|
58
|
+
}
|
|
59
|
+
if (!$private -or ($path -eq $root -and !$acl.AreAccessRulesProtected)) {
|
|
60
|
+
if (!$repair -or (!$managed -and !$private)) { throw 'Not account-only' }
|
|
61
|
+
return @{path=$path; dir=$dir}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
foreach ($path in $paths) { $change = Check-Path $path; if ($change) { $changes += $change } }
|
|
65
|
+
foreach ($c in $changes) {
|
|
66
|
+
$acl = New-PrivateAcl $c.dir
|
|
67
|
+
if ($c.dir) {
|
|
68
|
+
# SetKernelObjectSecurity only updates this directory; SetAccessControl
|
|
69
|
+
# would propagate inheritable ACEs through existing, uninspected children.
|
|
70
|
+
if (!('IpabDirectoryAcl' -as [type])) {
|
|
71
|
+
Add-Type -TypeDefinition @'
|
|
72
|
+
using System;
|
|
73
|
+
using System.Runtime.InteropServices;
|
|
74
|
+
using Microsoft.Win32.SafeHandles;
|
|
75
|
+
public static class IpabDirectoryAcl {
|
|
76
|
+
[DllImport("kernel32", CharSet=CharSet.Unicode, SetLastError=true)]
|
|
77
|
+
static extern SafeFileHandle CreateFile(string p, uint a, uint s, IntPtr sa, uint d, uint f, IntPtr t);
|
|
78
|
+
[DllImport("advapi32", SetLastError=true)]
|
|
79
|
+
static extern bool SetKernelObjectSecurity(SafeFileHandle h, uint i, byte[] sd);
|
|
80
|
+
public static void Set(string p, byte[] sd) {
|
|
81
|
+
using (var h = CreateFile(p, 0x40000, 3, IntPtr.Zero, 3, 0x02200000, IntPtr.Zero)) {
|
|
82
|
+
if (h.IsInvalid || !SetKernelObjectSecurity(h, 0x80000004, sd)) throw new System.ComponentModel.Win32Exception();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
'@
|
|
87
|
+
}
|
|
88
|
+
[IpabDirectoryAcl]::Set($c.path,$acl.GetSecurityDescriptorBinaryForm())
|
|
89
|
+
} else { [IO.File]::SetAccessControl($c.path,$acl) }
|
|
90
|
+
if (Check-Path $c.path) { throw 'ACL did not persist' }
|
|
91
|
+
if ($c.path -eq $root) { 'sealed'; break }
|
|
92
|
+
}
|
|
93
|
+
`;
|
|
94
|
+
|
|
95
|
+
export function windowsStateAccess(
|
|
96
|
+
root: string,
|
|
97
|
+
entries: string[],
|
|
98
|
+
action: "owners" | "prepare" | "verify",
|
|
99
|
+
managed: boolean,
|
|
100
|
+
): boolean {
|
|
101
|
+
const result = execFileSync(
|
|
102
|
+
join(
|
|
103
|
+
process.env.SystemRoot!,
|
|
104
|
+
"System32",
|
|
105
|
+
"WindowsPowerShell",
|
|
106
|
+
"v1.0",
|
|
107
|
+
"powershell.exe",
|
|
108
|
+
),
|
|
109
|
+
[
|
|
110
|
+
"-NoLogo",
|
|
111
|
+
"-NoProfile",
|
|
112
|
+
"-NonInteractive",
|
|
113
|
+
"-EncodedCommand",
|
|
114
|
+
Buffer.from(script, "utf16le").toString("base64"),
|
|
115
|
+
],
|
|
116
|
+
{
|
|
117
|
+
windowsHide: true,
|
|
118
|
+
encoding: "utf8",
|
|
119
|
+
env: {
|
|
120
|
+
...Object.fromEntries(
|
|
121
|
+
Object.entries(process.env).filter(
|
|
122
|
+
([key]) => key.toLowerCase() !== "psmodulepath",
|
|
123
|
+
),
|
|
124
|
+
),
|
|
125
|
+
IPAB_STATE_ROOT: root,
|
|
126
|
+
IPAB_STATE_ENTRIES: entries.join("|"),
|
|
127
|
+
IPAB_STATE_ACTION: action,
|
|
128
|
+
IPAB_STATE_MANAGED: managed ? "1" : "0",
|
|
129
|
+
},
|
|
130
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
131
|
+
},
|
|
132
|
+
);
|
|
133
|
+
return result.trim() === "sealed";
|
|
134
|
+
}
|