-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathresolve-browser-runtime-bundle.mjs
More file actions
132 lines (122 loc) · 3.95 KB
/
Copy pathresolve-browser-runtime-bundle.mjs
File metadata and controls
132 lines (122 loc) · 3.95 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
#!/usr/bin/env node
import fs from "node:fs";
import path from "node:path";
function fail(message) {
process.stderr.write(`invalid browser runtime bundle: ${message}\n`);
process.exit(1);
}
const [manifestArgument, rootArgument, outputMode] = process.argv.slice(2);
if (!manifestArgument || !rootArgument)
fail("usage: resolve-browser-runtime-bundle <manifest> <root> [--report]");
let rootPath;
let manifestPath;
let bundlePath;
let manifest;
try {
rootPath = fs.realpathSync(rootArgument);
manifestPath = fs.realpathSync(manifestArgument);
bundlePath = fs.realpathSync(path.dirname(manifestPath));
manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
} catch (error) {
fail(`cannot read manifest: ${error.message}`);
}
if (
bundlePath !== rootPath &&
!bundlePath.startsWith(`${rootPath}${path.sep}`)
) {
fail("manifest escapes bundle root");
}
if (
!manifest ||
typeof manifest !== "object" ||
typeof manifest.name !== "string" ||
typeof manifest.version !== "string" ||
!Array.isArray(manifest.components)
) {
fail("name, version, and components are required");
}
const componentIds = new Set();
const extensionPaths = [];
const loadedComponents = [];
for (const component of manifest.components) {
if (
!component ||
!["extension", "script", "opencli_plugin"].includes(component.kind) ||
typeof component.id !== "string" ||
typeof component.version !== "string" ||
typeof component.path !== "string"
) {
fail("component has an invalid kind, id, version, or path");
}
if (componentIds.has(component.id))
fail(`duplicate component ${component.id}`);
componentIds.add(component.id);
const unresolvedPath = path.resolve(bundlePath, component.path);
if (!unresolvedPath.startsWith(`${bundlePath}${path.sep}`)) {
fail(`component ${component.id} escapes bundle directory`);
}
if (!fs.existsSync(unresolvedPath)) {
if (component.required !== false)
fail(
`required component ${component.id}@${component.version} is missing`,
);
continue;
}
const componentPath = fs.realpathSync(unresolvedPath);
if (!componentPath.startsWith(`${bundlePath}${path.sep}`)) {
fail(`component ${component.id} resolves outside bundle directory`);
}
const componentManifestPath = path.join(componentPath, "manifest.json");
if (!fs.existsSync(componentManifestPath))
fail(`component ${component.id} has no manifest.json`);
let componentManifest;
try {
componentManifest = JSON.parse(
fs.readFileSync(componentManifestPath, "utf8"),
);
} catch (error) {
fail(
`component ${component.id} has an invalid manifest.json: ${error.message}`,
);
}
if (componentManifest.version !== component.version) {
fail(
`component ${component.id} expected version ${component.version}, found ${componentManifest.version ?? "unknown"}`,
);
}
loadedComponents.push({
kind: component.kind,
id: component.id,
version: component.version,
healthy: true,
diagnostic: null,
});
if (component.kind === "extension") extensionPaths.push(componentPath);
}
for (const capability of manifest.capabilities ?? []) {
if (
!capability ||
!componentIds.has(capability.component_id) ||
typeof capability.name !== "string" ||
typeof capability.action !== "string"
) {
fail("capability references an unknown component or action");
}
}
if (outputMode === "--report") {
const loadedIds = new Set(loadedComponents.map((component) => component.id));
process.stdout.write(
JSON.stringify({
loaded_bundle_name: manifest.name,
loaded_bundle_version: manifest.version,
loaded_components: loadedComponents,
capabilities: (manifest.capabilities ?? [])
.filter((capability) => loadedIds.has(capability.component_id))
.map((capability) => capability.name),
self_check: { ok: true, phase: "launcher-resolver" },
restart_required: false,
}),
);
} else {
process.stdout.write(extensionPaths.join("\n"));
}