Repository navigation
Expand file tree
/
Copy pathdriver-packages.ts
More file actions
491 lines (470 loc) · 17.9 KB
/
Copy pathdriver-packages.ts
File metadata and controls
491 lines (470 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import { InstalledDriver } from "@openclaw-enterprise/occ";
import { asRecord, isNonEmptyString } from "@openclaw-enterprise/utils";
import { readFile, realpath, stat } from "node:fs/promises";
import { findPackageJSON } from "node:module";
import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import type {
ComputeDriver,
ConfigurationDriver,
DriverImplementation,
DriverRegistration,
IAMDriver,
SandboxDriver,
} from "@openclaw-enterprise/contracts";
import type { NativeIAMStateStore } from "@openclaw-enterprise/iam";
import { currentComputeAbortSignal } from "../drivers/compute/operation-context.ts";
import type { SelectedDriverConfiguration } from "./installation-config.ts";
import { withoutByteOrderMark } from "./startup-file.ts";
type ConfigurationRecord = Readonly<Record<string, unknown>>;
interface ExternalDriverModule extends DriverImplementation {
createDriver(options: {
readonly id: string;
readonly implementation: string;
readonly configuration: ConfigurationRecord;
readonly platformState?: NativeIAMStateStore;
readonly getOperationAbortSignal?: () => AbortSignal | undefined;
}): unknown;
}
interface LoadedDriverPackage {
readonly module: ExternalDriverModule;
readonly implementation: string;
}
const PACKAGE_NAME = /^(?:@[a-zA-Z0-9][a-zA-Z0-9._~-]*\/)?[a-zA-Z0-9][a-zA-Z0-9._~-]*$/;
const INVALID_EXPORT_TARGET = Symbol("invalid package export target");
type ExportTarget = URL | null | undefined | typeof INVALID_EXPORT_TARGET;
// Node's default conditions for `import()` (no --conditions, --no-addons or
// --no-experimental-require-module flags; the controller sets none).
const IMPORT_CONDITIONS: ReadonlySet<string> = new Set([
"node",
"import",
"module-sync",
"node-addons",
"default",
]);
// Node refuses ".", ".." and "node_modules" segments, including percent-encoded letters.
const INVALID_TARGET_SEGMENT =
/(?:^|\\|\/)(?:(?:\.|%2e)(?:\.|%2e)?|(?:n|%6e|%4e)(?:o|%6f|%4f)(?:d|%64|%44)(?:e|%65|%45)(?:_|%5f)(?:m|%6d|%4d)(?:o|%6f|%4f)(?:d|%64|%44)(?:u|%75|%55)(?:l|%6c|%4c)(?:e|%65|%45)(?:s|%73|%53))(?:\\|\/|$)/i;
function isArrayIndex(key: string): boolean {
const index = Number(key);
return String(index) === key && index >= 0 && index < 0xffff_ffff;
}
/**
* Node's PACKAGE_TARGET_RESOLVE for the package root under import conditions: a
* "." key is a condition name here, so only the top level selects a subpath.
*/
function exportTarget(target: unknown, manifestUrl: URL, path: string): ExportTarget {
if (typeof target === "string") {
if (!target.startsWith("./") || INVALID_TARGET_SEGMENT.test(target.slice(2))) {
return INVALID_EXPORT_TARGET;
}
// URL parsing drops tabs and newlines, so containment is checked after it.
const resolved = new URL(target, manifestUrl);
return resolved.pathname.startsWith(new URL(".", manifestUrl).pathname)
? resolved
: INVALID_EXPORT_TARGET;
}
if (Array.isArray(target)) {
// Invalid targets and unmatched conditions fall through to the next entry; the
// result is the last null or invalid entry when none selects a file.
let last: ExportTarget = target.length === 0 ? null : undefined;
for (const item of target) {
const resolved = exportTarget(item, manifestUrl, path);
if (resolved instanceof URL) {
return resolved;
}
if (resolved !== undefined) {
last = resolved;
}
}
return last;
}
if (target === null) {
return null;
}
if (typeof target !== "object") {
return INVALID_EXPORT_TARGET;
}
const conditions = target as Record<string, unknown>;
const keys = Object.getOwnPropertyNames(conditions);
if (keys.some(isArrayIndex)) {
throw new Error(`${path}.package exports must not contain numeric condition keys.`);
}
for (const key of keys) {
if (IMPORT_CONDITIONS.has(key)) {
const resolved = exportTarget(conditions[key], manifestUrl, path);
if (resolved !== undefined) {
return resolved;
}
}
}
return undefined;
}
/** Node's PACKAGE_EXPORTS_RESOLVE for the "." subpath. */
function rootExportTarget(exports: unknown, manifestUrl: URL, path: string): ExportTarget {
if (exports !== null && typeof exports === "object" && !Array.isArray(exports)) {
const keys = Object.getOwnPropertyNames(exports);
const subpaths = keys.filter((key) => key.startsWith("."));
if (subpaths.length > 0 && subpaths.length < keys.length) {
throw new Error(`${path}.package exports must not mix subpath and condition keys.`);
}
if (subpaths.length > 0) {
return Object.hasOwn(exports, ".")
? exportTarget((exports as Record<string, unknown>)["."], manifestUrl, path)
: undefined;
}
}
return exportTarget(exports, manifestUrl, path);
}
const ABSENT_MANIFEST_ERRORS: ReadonlySet<string> = new Set([
"EACCES",
"EISDIR",
"ELOOP",
"ENAMETOOLONG",
"ENOENT",
"ENOTDIR",
"EPERM",
]);
/**
* The "type" Node's package.json reader gives a package scope. It matches top-level keys by
* their raw (still escaped) text and keeps the last "type" string that is "module" or
* "commonjs". A "type" that is not a string makes Node refuse the manifest (undefined here).
*/
function nodePackageType(text: string): string | undefined {
let type = "none";
let depth = 0;
let key: string | undefined;
let previous = "";
// JSON.parse has accepted the text, so strings and single characters tokenize it.
for (const token of text.match(/"(?:[^"\\]|\\.)*"|\S/g) ?? []) {
if (depth === 1 && previous === ":" && key === "type") {
if (!token.startsWith('"')) {
return undefined;
}
const value: unknown = JSON.parse(token);
if (value === "module" || value === "commonjs") {
type = value;
}
}
if (depth === 1 && (previous === "{" || previous === ",")) {
key = token.slice(1, -1);
}
if (token === "{" || token === "[") {
depth += 1;
} else if (token === "}" || token === "]") {
depth -= 1;
}
previous = token;
}
return type;
}
/**
* Node's package scope for a `.js` entry: the nearest package.json from the entry's directory,
* never at or past a node_modules directory. The walk stops at the package root. Returns the
* scope's "type" ("none" when absent) and the manifest that decided it.
*/
async function entryPackageScope(
entryPath: string,
packageDirectory: string,
path: string,
): Promise<{ readonly type: string; readonly manifest: string } | undefined> {
for (
let directory = dirname(entryPath);
basename(directory) !== "node_modules";
directory = dirname(directory)
) {
let read: string | undefined;
try {
read = await readFile(join(directory, "package.json"), "utf8");
} catch (error) {
// Node treats a package.json it cannot open or read as absent and keeps walking. Any
// other failure, such as ERR_STRING_TOO_LONG for a manifest Node still reads, fails closed.
if (!ABSENT_MANIFEST_ERRORS.has(String((error as NodeJS.ErrnoException).code))) {
throw new Error(`${path}.package cannot read package scope metadata.`, {
cause: error,
});
}
}
if (read !== undefined) {
const text = withoutByteOrderMark(read);
let type: string | undefined;
try {
type = asRecord(JSON.parse(text)) === undefined ? undefined : nodePackageType(text);
} catch {
type = undefined;
}
if (type === undefined) {
throw new Error(`${path}.package has invalid package scope metadata.`);
}
return { type, manifest: join(directory, "package.json") };
}
if (directory === packageDirectory || dirname(directory) === directory) {
break;
}
}
return undefined;
}
export async function loadDriverPackage(
selection: ConfigurationRecord,
capability: "configuration" | "iam" | "compute" | "sandbox",
packageRoot: string,
allowFixtureTarball: boolean,
): Promise<LoadedDriverPackage | undefined> {
if (!Object.hasOwn(selection, "package")) {
return undefined;
}
const path = `drivers.${capability}`;
const packageName = selection.package;
if (!isNonEmptyString(packageName)) {
throw new Error(`${path}.package must be a nonempty string.`);
}
if (!PACKAGE_NAME.test(packageName)) {
throw new Error(`${path}.package must be one exact npm package name.`);
}
const ownerPath = resolve(packageRoot, "package.json");
let owner: ConfigurationRecord | undefined;
try {
owner = asRecord(JSON.parse(await readFile(ownerPath, "utf8")));
if (owner === undefined) {
throw new Error("controller package.json must be one object.");
}
} catch {
throw new Error(`${path}.package cannot read the controller package manifest.`);
}
const dependencies = owner.dependencies === undefined ? {} : asRecord(owner.dependencies);
if (dependencies === undefined) {
throw new Error("controller package.json dependencies must be one object.");
}
if (
!Object.hasOwn(dependencies, packageName) ||
typeof dependencies[packageName] !== "string" ||
dependencies[packageName].trim().length === 0
) {
throw new Error(`${path}.package must be a direct controller production dependency.`);
}
const dependencyVersion = dependencies[packageName];
let installedManifestPath: string;
try {
const ownerUrl = pathToFileURL(ownerPath);
const installed = findPackageJSON(packageName, ownerUrl);
if (installed === undefined) {
throw new Error("package metadata unavailable");
}
installedManifestPath = await realpath(installed);
} catch {
throw new Error(`${path}.package selects an unavailable installed Driver package.`);
}
let installedManifest: ConfigurationRecord | undefined;
try {
const text = withoutByteOrderMark(await readFile(installedManifestPath, "utf8"));
installedManifest = asRecord(JSON.parse(text));
if (installedManifest === undefined) {
throw new Error(`${path}.package manifest must be one object.`);
}
} catch {
throw new Error(`${path}.package has invalid installed package metadata.`);
}
if (installedManifest.name !== packageName) {
throw new Error(`${path}.package does not match the installed npm package name.`);
}
const installedVersion = installedManifest.version;
if (!isNonEmptyString(installedVersion)) {
throw new Error(`${path}.package installed version must be a nonempty string.`);
}
if (
dependencyVersion !== installedVersion &&
!(allowFixtureTarball && /^file:.+\.tgz$/.test(dependencyVersion))
) {
throw new Error(`${path}.package must be pinned to its exact installed production version.`);
}
const exported = rootExportTarget(
installedManifest.exports,
pathToFileURL(installedManifestPath),
path,
);
if (!(exported instanceof URL)) {
throw new Error(`${path}.package must declare an exported compiled ESM entry.`);
}
// Node then loads exactly the selected file: no extension, directory or main lookup.
if (/%2f|%5c/i.test(exported.pathname)) {
throw new Error(`${path}.package export target must not encode a path separator.`);
}
let targetPath: string;
try {
targetPath = fileURLToPath(exported);
} catch {
throw new Error(`${path}.package export target has malformed percent encoding.`);
}
const target = await stat(targetPath).catch(() => undefined);
if (target?.isDirectory() === true) {
throw new Error(`${path}.package export target must be a file, not a directory.`);
}
let entryPath: string;
try {
if (target?.isFile() !== true) {
throw new Error("The selected export target is not a file.");
}
entryPath = await realpath(targetPath);
} catch {
throw new Error(`${path}.package selects an unavailable compiled ESM entry.`);
}
const packageDirectory = dirname(installedManifestPath);
const contained = relative(packageDirectory, entryPath);
if (
contained === "" ||
contained === ".." ||
contained.startsWith(`..${sep}`) ||
isAbsolute(contained)
) {
throw new Error(`${path}.package entry escapes its installed package root.`);
}
// Node loads `.mjs` as ESM without reading a package scope, and `.js` by the nearest
// package.json scope, not the root's. A refusal names the file that decided the format.
const extension = extname(entryPath);
const packagePath = (file: string) =>
[packageName, ...relative(packageDirectory, file).split(sep)].join("/");
if (extension !== ".mjs") {
if (extension !== ".js") {
throw new Error(
`${path}.package must export precompiled JavaScript ESM: entry ${packagePath(entryPath)} is not a .mjs or .js file.`,
);
}
const scope = await entryPackageScope(entryPath, packageDirectory, path);
if (scope?.type !== "module") {
// No scope when the entry's real path sits under a node_modules directory inside the
// package: the walk stops there, as Node's does.
const reason =
scope === undefined
? `has no package.json scope inside ${packageName}`
: `takes its format from ${packagePath(scope.manifest)}, which does not set "type": "module"`;
throw new Error(
`${path}.package must export precompiled JavaScript ESM: entry ${packagePath(entryPath)} ${reason}.`,
);
}
}
const entryUrl = pathToFileURL(entryPath);
entryUrl.search = exported.search;
entryUrl.hash = exported.hash;
let imported: unknown;
try {
imported = await import(entryUrl.href);
} catch {
throw new Error(`${path}.package failed to load its compiled Driver module.`);
}
const module = asRecord(imported);
if (module === undefined) {
throw new Error(`${path}.package exports must be one object.`);
}
if (
typeof module.validateConfiguration !== "function" ||
typeof module.createDriver !== "function"
) {
throw new Error(`${path}.package must export Driver validation and a factory.`);
}
if (asRecord(module.configurationSchema) === undefined) {
throw new Error(`${path}.configurationSchema must be one object.`);
}
return Object.freeze({
module: module as unknown as ExternalDriverModule,
implementation: `${packageName}@${installedVersion}`,
});
}
export function createExternalDriver(
implementation: ExternalDriverModule,
selection: SelectedDriverConfiguration,
capability: "configuration" | "iam" | "compute" | "sandbox",
platformState?: NativeIAMStateStore,
): ConfigurationDriver | IAMDriver | ComputeDriver | SandboxDriver {
if (capability === "iam" && typeof platformState?.loadNativeIAMState !== "function") {
throw new Error("drivers.iam factory requires platform state.");
}
const created = asRecord(
implementation.createDriver({
id: selection.id,
implementation: selection.implementation,
configuration: selection.configuration,
...(platformState === undefined ? {} : { platformState }),
...(capability === "compute" ? { getOperationAbortSignal: currentComputeAbortSignal } : {}),
}),
);
if (created === undefined) {
throw new Error(`drivers.${capability} factory result must be one object.`);
}
if (
created.capability !== capability ||
created.id !== selection.id ||
created.implementation !== selection.implementation
) {
throw new Error(`drivers.${capability} factory returned an unselected Driver identity.`);
}
const methods =
capability === "configuration"
? ["create", "read", "update", "delete", "validate"]
: capability === "iam"
? ["lookupIdentity", "authorize"]
: capability === "compute"
? [
"ensureNamespace",
"deleteNamespace",
"prepareRevision",
"stopRevision",
"retireRevision",
]
: ["cleanup"];
if (methods.some((method) => typeof created[method] !== "function")) {
throw new Error(`drivers.${capability} factory returned an invalid Driver contract.`);
}
if (
capability === "sandbox" &&
((created.ensureNamespace !== undefined && typeof created.ensureNamespace !== "function") ||
(created.provisionHarness !== undefined && typeof created.provisionHarness !== "function"))
) {
throw new Error("drivers.sandbox factory returned invalid optional lifecycle hooks.");
}
if (
capability === "compute" &&
created.setLifecycleDrivers !== undefined &&
typeof created.setLifecycleDrivers !== "function"
) {
throw new Error("drivers.compute factory returned invalid lifecycle Driver wiring.");
}
return created as unknown as ConfigurationDriver | IAMDriver | ComputeDriver | SandboxDriver;
}
/** Load one package instance for the selected capabilities; the sandbox selection owns its configuration.
* A registration this refuses is closed before the refusal returns, so a refused startup can exit. */
export async function createExternalProtectedDriver(
implementation: ExternalDriverModule,
selection: SelectedDriverConfiguration,
// Composition that changes the installed authority must also change its durable identity.
repositoryAuthorityIdentity?: string,
): Promise<InstalledDriver> {
const { id, configuration } = selection;
const registration = asRecord(
implementation.createDriver({ id, configuration, implementation: selection.implementation }),
);
try {
if (!registration || !asRecord(registration.capabilities)) {
throw new Error(
"The selected package must return one Driver registration for Sandbox and Credential Proxy.",
);
}
const owner = new InstalledDriver(
repositoryAuthorityIdentity === undefined
? selection
: { ...selection, configuration: { sandbox: configuration, repositoryAuthorityIdentity } },
registration as unknown as DriverRegistration,
);
owner.require("sandbox");
owner.require("credential_proxy");
return owner;
} catch (error) {
try {
if (typeof registration?.close === "function") {
await registration.close();
}
} catch {
// The refusal above is the startup error; a failed close does not replace it.
}
throw error;
}
}