@ipab/cli 0.0.0 → 0.1.0-rc.1
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/LICENSE +21 -0
- package/README.md +24 -1
- package/dist/app-authoring.d.ts +22 -0
- package/dist/app-authoring.d.ts.map +1 -0
- package/dist/app-authoring.js +448 -0
- package/dist/app-authoring.js.map +1 -0
- package/dist/app-definition.d.ts +103 -0
- package/dist/app-definition.d.ts.map +1 -0
- package/dist/app-definition.js +201 -0
- package/dist/app-definition.js.map +1 -0
- package/dist/app-prebuild.d.ts +12 -0
- package/dist/app-prebuild.d.ts.map +1 -0
- package/dist/app-prebuild.js +231 -0
- package/dist/app-prebuild.js.map +1 -0
- package/dist/app-templates.d.ts +13 -0
- package/dist/app-templates.d.ts.map +1 -0
- package/dist/app-templates.js +295 -0
- package/dist/app-templates.js.map +1 -0
- package/dist/authoring.d.ts +63 -0
- package/dist/authoring.d.ts.map +1 -0
- package/dist/authoring.js +411 -0
- package/dist/authoring.js.map +1 -0
- package/dist/builtins/template.node.car +0 -0
- package/dist/builtins/template.web-app.car +0 -0
- package/dist/builtins.d.ts +6 -0
- package/dist/builtins.d.ts.map +1 -0
- package/dist/builtins.js +41 -0
- package/dist/builtins.js.map +1 -0
- package/dist/child.d.ts +5 -0
- package/dist/child.d.ts.map +1 -0
- package/dist/child.js +28 -0
- package/dist/child.js.map +1 -0
- package/dist/cli.d.ts +24 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +1110 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +26 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +154 -0
- package/dist/config.js.map +1 -0
- package/dist/context.d.ts +43 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +174 -0
- package/dist/context.js.map +1 -0
- package/dist/errors.d.ts +10 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +40 -0
- package/dist/errors.js.map +1 -0
- package/dist/history.d.ts +34 -0
- package/dist/history.d.ts.map +1 -0
- package/dist/history.js +286 -0
- package/dist/history.js.map +1 -0
- package/dist/ipc.d.ts +27 -0
- package/dist/ipc.d.ts.map +1 -0
- package/dist/ipc.js +341 -0
- package/dist/ipc.js.map +1 -0
- package/dist/logs.d.ts +15 -0
- package/dist/logs.d.ts.map +1 -0
- package/dist/logs.js +118 -0
- package/dist/logs.js.map +1 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +4 -0
- package/dist/main.js.map +1 -0
- package/dist/pins.d.ts +33 -0
- package/dist/pins.d.ts.map +1 -0
- package/dist/pins.js +165 -0
- package/dist/pins.js.map +1 -0
- package/dist/runtime.d.ts +20 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +237 -0
- package/dist/runtime.js.map +1 -0
- package/dist/source-bundle.d.ts +15 -0
- package/dist/source-bundle.d.ts.map +1 -0
- package/dist/source-bundle.js +102 -0
- package/dist/source-bundle.js.map +1 -0
- package/dist/space.d.ts +33 -0
- package/dist/space.d.ts.map +1 -0
- package/dist/space.js +269 -0
- package/dist/space.js.map +1 -0
- package/dist/state.d.ts +102 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +335 -0
- package/dist/state.js.map +1 -0
- package/dist/supported-schemas.d.ts +5 -0
- package/dist/supported-schemas.d.ts.map +1 -0
- package/dist/supported-schemas.js +90 -0
- package/dist/supported-schemas.js.map +1 -0
- package/dist/template-definition.d.ts +98 -0
- package/dist/template-definition.d.ts.map +1 -0
- package/dist/template-definition.js +179 -0
- package/dist/template-definition.js.map +1 -0
- package/dist/template-source.d.ts +12 -0
- package/dist/template-source.d.ts.map +1 -0
- package/dist/template-source.js +97 -0
- package/dist/template-source.js.map +1 -0
- package/package.json +60 -3
- package/src/app-authoring.ts +616 -0
- package/src/app-definition.ts +250 -0
- package/src/app-prebuild.ts +289 -0
- package/src/app-templates.ts +395 -0
- package/src/authoring.ts +577 -0
- package/src/builtins.ts +49 -0
- package/src/child.ts +32 -0
- package/src/cli.ts +1330 -0
- package/src/config.ts +221 -0
- package/src/context.ts +242 -0
- package/src/errors.ts +46 -0
- package/src/history.ts +391 -0
- package/src/ipc.ts +366 -0
- package/src/logs.ts +132 -0
- package/src/main.ts +4 -0
- package/src/pins.ts +200 -0
- package/src/runtime.ts +301 -0
- package/src/source-bundle.ts +135 -0
- package/src/space.ts +379 -0
- package/src/state.ts +430 -0
- package/src/supported-schemas.ts +151 -0
- package/src/template-definition.ts +233 -0
- package/src/template-source.ts +128 -0
package/src/config.ts
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { readContentConfiguration } from "@ipab/ipfs-store/node";
|
|
4
|
+
import type { StoreConfiguration } from "@ipab/ipfs-store";
|
|
5
|
+
import { HostError, object, syntax } from "./errors.js";
|
|
6
|
+
|
|
7
|
+
export type ConfigKey =
|
|
8
|
+
| "store.gateway"
|
|
9
|
+
| "history.enabled"
|
|
10
|
+
| "history.limit"
|
|
11
|
+
| "output.retention"
|
|
12
|
+
| "bundle.outputDir";
|
|
13
|
+
export type ConfigValue = string | boolean | number | null;
|
|
14
|
+
export type Source = "flag" | "env" | "user" | "default";
|
|
15
|
+
export interface GlobalOptions {
|
|
16
|
+
stateDir?: string;
|
|
17
|
+
storeGateway?: string;
|
|
18
|
+
json: boolean;
|
|
19
|
+
}
|
|
20
|
+
export interface ConfigItem {
|
|
21
|
+
key: ConfigKey | "state-dir";
|
|
22
|
+
value: ConfigValue;
|
|
23
|
+
source: Source;
|
|
24
|
+
}
|
|
25
|
+
export const defaults: Record<ConfigKey, ConfigValue> = {
|
|
26
|
+
"store.gateway": null,
|
|
27
|
+
"history.enabled": true,
|
|
28
|
+
"history.limit": 20,
|
|
29
|
+
"output.retention": "history",
|
|
30
|
+
"bundle.outputDir": "dist",
|
|
31
|
+
};
|
|
32
|
+
const environments: Record<ConfigKey, string> = {
|
|
33
|
+
"store.gateway": "IPAB_STORE_GATEWAY",
|
|
34
|
+
"history.enabled": "IPAB_HISTORY_ENABLED",
|
|
35
|
+
"history.limit": "IPAB_HISTORY_LIMIT",
|
|
36
|
+
"output.retention": "IPAB_OUTPUT_RETENTION",
|
|
37
|
+
"bundle.outputDir": "IPAB_BUNDLE_OUTPUT_DIR",
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export function configKey(key: string): ConfigKey {
|
|
41
|
+
if (!Object.hasOwn(defaults, key))
|
|
42
|
+
syntax(`Unknown configuration key "${key}".`);
|
|
43
|
+
return key as ConfigKey;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function publicUrl(value: string): string {
|
|
47
|
+
let url: URL;
|
|
48
|
+
try {
|
|
49
|
+
url = new URL(value);
|
|
50
|
+
} catch {
|
|
51
|
+
syntax(
|
|
52
|
+
"Store URL must be an HTTP(S) URL without credentials, query or fragment.",
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
if (
|
|
56
|
+
!["http:", "https:"].includes(url.protocol) ||
|
|
57
|
+
url.username ||
|
|
58
|
+
url.password ||
|
|
59
|
+
url.search ||
|
|
60
|
+
url.hash
|
|
61
|
+
)
|
|
62
|
+
syntax(
|
|
63
|
+
"Store URL must be an HTTP(S) URL without credentials, query or fragment.",
|
|
64
|
+
);
|
|
65
|
+
return url.toString();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function parseValue(
|
|
69
|
+
key: ConfigKey,
|
|
70
|
+
value: unknown,
|
|
71
|
+
text = false,
|
|
72
|
+
): ConfigValue {
|
|
73
|
+
if (text && typeof value === "string") {
|
|
74
|
+
if (key === "history.enabled") {
|
|
75
|
+
if (value === "true") return true;
|
|
76
|
+
if (value === "false") return false;
|
|
77
|
+
}
|
|
78
|
+
if (key === "history.limit" && /^[1-9]\d*$/.test(value))
|
|
79
|
+
value = Number(value);
|
|
80
|
+
if (key === "store.gateway" && value === "default") return null;
|
|
81
|
+
}
|
|
82
|
+
if (key === "store.gateway" && value === null) return null;
|
|
83
|
+
if (
|
|
84
|
+
key === "bundle.outputDir" &&
|
|
85
|
+
typeof value === "string" &&
|
|
86
|
+
value.trim() &&
|
|
87
|
+
!value.includes("\0")
|
|
88
|
+
)
|
|
89
|
+
return value;
|
|
90
|
+
if (key === "store.gateway" && typeof value === "string")
|
|
91
|
+
return publicUrl(value);
|
|
92
|
+
if (key === "history.enabled" && typeof value === "boolean") return value;
|
|
93
|
+
if (
|
|
94
|
+
key === "history.limit" &&
|
|
95
|
+
typeof value === "number" &&
|
|
96
|
+
Number.isSafeInteger(value) &&
|
|
97
|
+
value > 0
|
|
98
|
+
)
|
|
99
|
+
return value;
|
|
100
|
+
if (key === "output.retention" && (value === "history" || value === "none"))
|
|
101
|
+
return value;
|
|
102
|
+
syntax(
|
|
103
|
+
`Invalid value for ${key}; expected ${
|
|
104
|
+
key === "history.enabled"
|
|
105
|
+
? "true or false"
|
|
106
|
+
: key === "history.limit"
|
|
107
|
+
? "a positive safe integer"
|
|
108
|
+
: key === "output.retention"
|
|
109
|
+
? "history or none"
|
|
110
|
+
: key === "bundle.outputDir"
|
|
111
|
+
? "a nonempty directory path"
|
|
112
|
+
: "a public HTTP(S) URL or default"
|
|
113
|
+
}.`,
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function validateUserConfig(
|
|
118
|
+
value: unknown,
|
|
119
|
+
): Partial<Record<ConfigKey, ConfigValue>> {
|
|
120
|
+
const record = object(value, "user configuration");
|
|
121
|
+
const result: Partial<Record<ConfigKey, ConfigValue>> = {};
|
|
122
|
+
try {
|
|
123
|
+
for (const [key, value] of Object.entries(record))
|
|
124
|
+
result[configKey(key)] = parseValue(configKey(key), value);
|
|
125
|
+
} catch {
|
|
126
|
+
throw new HostError(
|
|
127
|
+
"Malformed user configuration; inspect the state database configuration.",
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export function stateLocation(
|
|
134
|
+
flags: GlobalOptions,
|
|
135
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
136
|
+
): { path: string; source: Source } {
|
|
137
|
+
const value = flags.stateDir ?? env.IPAB_STATE_DIR;
|
|
138
|
+
if (value !== undefined && !value.trim())
|
|
139
|
+
syntax("State directory must be nonempty.");
|
|
140
|
+
return {
|
|
141
|
+
path: resolve(
|
|
142
|
+
value ??
|
|
143
|
+
(process.platform === "win32"
|
|
144
|
+
? join(
|
|
145
|
+
env.LOCALAPPDATA ?? join(homedir(), "AppData", "Local"),
|
|
146
|
+
"ipab",
|
|
147
|
+
)
|
|
148
|
+
: join(
|
|
149
|
+
env.XDG_DATA_HOME ?? join(homedir(), ".local", "share"),
|
|
150
|
+
"ipab",
|
|
151
|
+
)),
|
|
152
|
+
),
|
|
153
|
+
source:
|
|
154
|
+
flags.stateDir !== undefined
|
|
155
|
+
? "flag"
|
|
156
|
+
: env.IPAB_STATE_DIR !== undefined
|
|
157
|
+
? "env"
|
|
158
|
+
: "default",
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function effectiveConfig(
|
|
163
|
+
user: Partial<Record<ConfigKey, ConfigValue>>,
|
|
164
|
+
flags: GlobalOptions,
|
|
165
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
166
|
+
): ConfigItem[] {
|
|
167
|
+
if (env.IPAB_STORE_RPC !== undefined)
|
|
168
|
+
syntax(
|
|
169
|
+
"IPAB_STORE_RPC is no longer supported. Remove it and use IPAB_STORE_GATEWAY with an HTTP gateway URL.",
|
|
170
|
+
);
|
|
171
|
+
const result = (Object.keys(defaults) as ConfigKey[]).map(
|
|
172
|
+
(key): ConfigItem => {
|
|
173
|
+
const variable = env[environments[key]];
|
|
174
|
+
const flag = key === "store.gateway" ? flags.storeGateway : undefined;
|
|
175
|
+
// Invalid lower layers are not hidden by a higher-priority override.
|
|
176
|
+
const environmental =
|
|
177
|
+
variable === undefined ? undefined : parseValue(key, variable, true);
|
|
178
|
+
return {
|
|
179
|
+
key,
|
|
180
|
+
value:
|
|
181
|
+
flag !== undefined
|
|
182
|
+
? parseValue(key, flag, true)
|
|
183
|
+
: variable !== undefined
|
|
184
|
+
? environmental!
|
|
185
|
+
: Object.hasOwn(user, key)
|
|
186
|
+
? user[key]!
|
|
187
|
+
: defaults[key],
|
|
188
|
+
source:
|
|
189
|
+
flag !== undefined
|
|
190
|
+
? "flag"
|
|
191
|
+
: variable !== undefined
|
|
192
|
+
? "env"
|
|
193
|
+
: Object.hasOwn(user, key)
|
|
194
|
+
? "user"
|
|
195
|
+
: "default",
|
|
196
|
+
};
|
|
197
|
+
},
|
|
198
|
+
);
|
|
199
|
+
const location = stateLocation(flags, env);
|
|
200
|
+
result.push({
|
|
201
|
+
key: "state-dir",
|
|
202
|
+
value: location.path,
|
|
203
|
+
source: location.source,
|
|
204
|
+
});
|
|
205
|
+
return result;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export async function storeConfiguration(
|
|
209
|
+
gateway: ConfigValue,
|
|
210
|
+
): Promise<StoreConfiguration> {
|
|
211
|
+
const config = await readContentConfiguration(
|
|
212
|
+
gateway === null ? {} : { gateway: publicUrl(String(gateway)) },
|
|
213
|
+
);
|
|
214
|
+
const urls = config.gateway
|
|
215
|
+
? typeof config.gateway === "string"
|
|
216
|
+
? [config.gateway]
|
|
217
|
+
: config.gateway
|
|
218
|
+
: [];
|
|
219
|
+
for (const url of urls) publicUrl(url);
|
|
220
|
+
return config;
|
|
221
|
+
}
|
package/src/context.ts
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { HostError } from "./errors.js";
|
|
2
|
+
import { CHUNK_BYTES, relativePath } from "./space.js";
|
|
3
|
+
|
|
4
|
+
export interface AppContext {
|
|
5
|
+
readonly appCid: string;
|
|
6
|
+
resolvePin(name: string): Promise<string | undefined>;
|
|
7
|
+
readFile(
|
|
8
|
+
rootCid: string,
|
|
9
|
+
path: string,
|
|
10
|
+
options?: { signal?: AbortSignal },
|
|
11
|
+
): Promise<ReadableStream<Uint8Array>>;
|
|
12
|
+
output: {
|
|
13
|
+
writeFile(
|
|
14
|
+
path: string,
|
|
15
|
+
stream: ReadableStream<Uint8Array>,
|
|
16
|
+
options?: { signal?: AbortSignal },
|
|
17
|
+
): Promise<string>;
|
|
18
|
+
link(
|
|
19
|
+
path: string,
|
|
20
|
+
unixfsCid: string,
|
|
21
|
+
options?: { signal?: AbortSignal },
|
|
22
|
+
): Promise<void>;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export interface Request {
|
|
26
|
+
version: 1;
|
|
27
|
+
id: number;
|
|
28
|
+
operation: string;
|
|
29
|
+
args: unknown;
|
|
30
|
+
}
|
|
31
|
+
export interface Reply {
|
|
32
|
+
version: 1;
|
|
33
|
+
id: number;
|
|
34
|
+
result?: unknown;
|
|
35
|
+
error?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface Port {
|
|
38
|
+
send(frame: Request | Reply, callback: (error: Error | null) => void): void;
|
|
39
|
+
on(event: "message", listener: (frame: unknown) => void): void;
|
|
40
|
+
on(event: "disconnect", listener: () => void): void;
|
|
41
|
+
off(event: "message", listener: (frame: unknown) => void): void;
|
|
42
|
+
off(event: "disconnect", listener: () => void): void;
|
|
43
|
+
ref?(): void;
|
|
44
|
+
unref?(): void;
|
|
45
|
+
}
|
|
46
|
+
export function requestFrame(value: unknown): value is Request {
|
|
47
|
+
if (!value || typeof value !== "object") return false;
|
|
48
|
+
const frame = value as Partial<Request>;
|
|
49
|
+
return (
|
|
50
|
+
frame.version === 1 &&
|
|
51
|
+
Number.isSafeInteger(frame.id) &&
|
|
52
|
+
Number(frame.id) > 0 &&
|
|
53
|
+
typeof frame.operation === "string" &&
|
|
54
|
+
frame.operation.length <= 32
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function createContext(port: Port): {
|
|
59
|
+
context: AppContext;
|
|
60
|
+
initialize: () => Promise<void>;
|
|
61
|
+
close: () => void;
|
|
62
|
+
} {
|
|
63
|
+
let sequence = 0;
|
|
64
|
+
let appCid: string | undefined;
|
|
65
|
+
const pending = new Map<
|
|
66
|
+
number,
|
|
67
|
+
{ resolve: (value: unknown) => void; reject: (error: Error) => void }
|
|
68
|
+
>();
|
|
69
|
+
const close = () => {
|
|
70
|
+
for (const entry of pending.values())
|
|
71
|
+
entry.reject(new HostError("IPAB context channel disconnected."));
|
|
72
|
+
pending.clear();
|
|
73
|
+
port.off("message", receive);
|
|
74
|
+
port.off("disconnect", close);
|
|
75
|
+
port.unref?.();
|
|
76
|
+
};
|
|
77
|
+
const receive = (value: unknown) => {
|
|
78
|
+
if (!value || typeof value !== "object") return;
|
|
79
|
+
const frame = value as {
|
|
80
|
+
version?: unknown;
|
|
81
|
+
id?: number;
|
|
82
|
+
result?: unknown;
|
|
83
|
+
error?: unknown;
|
|
84
|
+
};
|
|
85
|
+
if (frame.version !== 1 || !Number.isSafeInteger(frame.id)) return;
|
|
86
|
+
const entry = pending.get(frame.id!);
|
|
87
|
+
if (!entry) return;
|
|
88
|
+
pending.delete(frame.id!);
|
|
89
|
+
if (!pending.size) port.unref?.();
|
|
90
|
+
if (typeof frame.error === "string")
|
|
91
|
+
entry.reject(new HostError(frame.error));
|
|
92
|
+
else entry.resolve(frame.result);
|
|
93
|
+
};
|
|
94
|
+
port.on("message", receive);
|
|
95
|
+
port.on("disconnect", close);
|
|
96
|
+
port.unref?.();
|
|
97
|
+
const rpc = async (
|
|
98
|
+
operation: string,
|
|
99
|
+
args: unknown,
|
|
100
|
+
signal?: AbortSignal,
|
|
101
|
+
): Promise<unknown> => {
|
|
102
|
+
signal?.throwIfAborted();
|
|
103
|
+
if (pending.size >= 32)
|
|
104
|
+
throw new HostError("IPAB context request capacity exceeded.");
|
|
105
|
+
const id = ++sequence;
|
|
106
|
+
port.ref?.();
|
|
107
|
+
const cancellation = () => {
|
|
108
|
+
port.send(
|
|
109
|
+
{ version: 1, id: ++sequence, operation: "cancel", args: id },
|
|
110
|
+
() => {},
|
|
111
|
+
);
|
|
112
|
+
};
|
|
113
|
+
signal?.addEventListener("abort", cancellation, { once: true });
|
|
114
|
+
try {
|
|
115
|
+
return await new Promise((resolve, reject) => {
|
|
116
|
+
pending.set(id, { resolve, reject });
|
|
117
|
+
port.send({ version: 1, id, operation, args }, (error) => {
|
|
118
|
+
if (error) {
|
|
119
|
+
pending.delete(id);
|
|
120
|
+
if (!pending.size) port.unref?.();
|
|
121
|
+
reject(error);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
} finally {
|
|
126
|
+
signal?.removeEventListener("abort", cancellation);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const context: AppContext = {
|
|
130
|
+
async resolvePin(name) {
|
|
131
|
+
if (!name || name.length > 256)
|
|
132
|
+
throw new HostError("Pin name must contain 1..256 characters.");
|
|
133
|
+
const result = await rpc("resolvePin", name);
|
|
134
|
+
if (result === null) return undefined;
|
|
135
|
+
if (typeof result !== "string")
|
|
136
|
+
throw new HostError("Invalid pin lookup response.");
|
|
137
|
+
return result;
|
|
138
|
+
},
|
|
139
|
+
get appCid() {
|
|
140
|
+
if (!appCid)
|
|
141
|
+
throw new HostError("IPAB context identity is not initialized.");
|
|
142
|
+
return appCid;
|
|
143
|
+
},
|
|
144
|
+
async readFile(rootCid, path, options = {}) {
|
|
145
|
+
relativePath(path);
|
|
146
|
+
const id = (await rpc(
|
|
147
|
+
"read",
|
|
148
|
+
{ rootCid, path },
|
|
149
|
+
options.signal,
|
|
150
|
+
)) as number;
|
|
151
|
+
if (options.signal?.aborted) {
|
|
152
|
+
await rpc("readCancel", id).catch(() => {});
|
|
153
|
+
options.signal.throwIfAborted();
|
|
154
|
+
}
|
|
155
|
+
const abort = () => {
|
|
156
|
+
void rpc("readCancel", id).catch(() => {});
|
|
157
|
+
};
|
|
158
|
+
const finished = () => {
|
|
159
|
+
options.signal?.removeEventListener("abort", abort);
|
|
160
|
+
};
|
|
161
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
162
|
+
return new ReadableStream<Uint8Array>(
|
|
163
|
+
{
|
|
164
|
+
async pull(controller) {
|
|
165
|
+
try {
|
|
166
|
+
const value = (await rpc(
|
|
167
|
+
"pull",
|
|
168
|
+
id,
|
|
169
|
+
options.signal,
|
|
170
|
+
)) as Uint8Array | null;
|
|
171
|
+
if (value === null) {
|
|
172
|
+
finished();
|
|
173
|
+
controller.close();
|
|
174
|
+
} else controller.enqueue(value);
|
|
175
|
+
} catch (error) {
|
|
176
|
+
finished();
|
|
177
|
+
controller.error(error);
|
|
178
|
+
await rpc("readCancel", id).catch(() => {});
|
|
179
|
+
}
|
|
180
|
+
},
|
|
181
|
+
async cancel() {
|
|
182
|
+
finished();
|
|
183
|
+
await rpc("readCancel", id);
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
{ highWaterMark: 0 },
|
|
187
|
+
);
|
|
188
|
+
},
|
|
189
|
+
output: {
|
|
190
|
+
async writeFile(path, stream, options = {}) {
|
|
191
|
+
relativePath(path);
|
|
192
|
+
const id = (await rpc("write", { path }, options.signal)) as number;
|
|
193
|
+
const reader = stream.getReader();
|
|
194
|
+
const abort = () => {
|
|
195
|
+
void reader.cancel(options.signal?.reason).catch(() => {});
|
|
196
|
+
};
|
|
197
|
+
options.signal?.addEventListener("abort", abort, { once: true });
|
|
198
|
+
try {
|
|
199
|
+
for (;;) {
|
|
200
|
+
options.signal?.throwIfAborted();
|
|
201
|
+
const item = await reader.read();
|
|
202
|
+
if (item.done) break;
|
|
203
|
+
if (!(item.value instanceof Uint8Array))
|
|
204
|
+
throw new HostError("Output stream must contain bytes.");
|
|
205
|
+
for (
|
|
206
|
+
let offset = 0;
|
|
207
|
+
offset < item.value.length;
|
|
208
|
+
offset += CHUNK_BYTES
|
|
209
|
+
)
|
|
210
|
+
await rpc(
|
|
211
|
+
"chunk",
|
|
212
|
+
{ id, bytes: item.value.slice(offset, offset + CHUNK_BYTES) },
|
|
213
|
+
options.signal,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
return (await rpc("end", id, options.signal)) as string;
|
|
217
|
+
} catch (error) {
|
|
218
|
+
await reader.cancel(error);
|
|
219
|
+
await rpc("writeCancel", id).catch(() => {});
|
|
220
|
+
throw error;
|
|
221
|
+
} finally {
|
|
222
|
+
options.signal?.removeEventListener("abort", abort);
|
|
223
|
+
reader.releaseLock();
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
async link(path, unixfsCid, options = {}) {
|
|
227
|
+
relativePath(path);
|
|
228
|
+
await rpc("link", { path, rootCid: unixfsCid }, options.signal);
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
};
|
|
232
|
+
return {
|
|
233
|
+
context,
|
|
234
|
+
close,
|
|
235
|
+
async initialize() {
|
|
236
|
+
const value = await rpc("identity", null);
|
|
237
|
+
if (typeof value !== "string" || !value)
|
|
238
|
+
throw new HostError("Invalid IPAB context identity.");
|
|
239
|
+
appCid = value;
|
|
240
|
+
},
|
|
241
|
+
};
|
|
242
|
+
}
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export class HostError extends Error {
|
|
2
|
+
constructor(
|
|
3
|
+
message: string,
|
|
4
|
+
readonly exitCode = 1,
|
|
5
|
+
readonly code = "OPERATION_FAILED",
|
|
6
|
+
readonly phase = "state",
|
|
7
|
+
) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "HostError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function syntax(message: string): never {
|
|
14
|
+
throw new HostError(
|
|
15
|
+
`${message} Run ipab --help.`,
|
|
16
|
+
2,
|
|
17
|
+
"INVALID_ARGUMENT",
|
|
18
|
+
"arguments",
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function message(error: unknown): string {
|
|
23
|
+
let text =
|
|
24
|
+
error instanceof Error ? error.message : "Unknown operation failure.";
|
|
25
|
+
if (error instanceof ContentBundleError)
|
|
26
|
+
text += `${error.path ? ` [${error.path}]` : ""}${error.pointer ? ` at ${error.pointer}` : ""}`;
|
|
27
|
+
return text.replace(/https?:\/\/[^\s"']+/g, (value) => {
|
|
28
|
+
try {
|
|
29
|
+
const url = new URL(value);
|
|
30
|
+
url.username = "";
|
|
31
|
+
url.password = "";
|
|
32
|
+
url.search = "";
|
|
33
|
+
url.hash = "";
|
|
34
|
+
return url.toString();
|
|
35
|
+
} catch {
|
|
36
|
+
return "[redacted URL]";
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function object(value: unknown, label: string): Record<string, unknown> {
|
|
42
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
43
|
+
throw new HostError(`Invalid ${label}.`);
|
|
44
|
+
return value as Record<string, unknown>;
|
|
45
|
+
}
|
|
46
|
+
import { ContentBundleError } from "@ipab/bundle";
|