@trebired/code-server-kit 0.1.0 → 0.2.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.
- package/CHANGELOG.md +12 -0
- package/README.md +251 -66
- package/dist/errors.d.ts +19 -4
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +37 -7
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/launch.d.ts +2 -3
- package/dist/launch.d.ts.map +1 -1
- package/dist/launch.js +15 -223
- package/dist/launch.js.map +1 -1
- package/dist/plan.d.ts +15 -0
- package/dist/plan.d.ts.map +1 -0
- package/dist/plan.js +355 -0
- package/dist/plan.js.map +1 -0
- package/dist/profile.d.ts +8 -0
- package/dist/profile.d.ts.map +1 -0
- package/dist/profile.js +107 -0
- package/dist/profile.js.map +1 -0
- package/dist/proxy.d.ts +6 -0
- package/dist/proxy.d.ts.map +1 -0
- package/dist/proxy.js +92 -0
- package/dist/proxy.js.map +1 -0
- package/dist/readiness.d.ts.map +1 -1
- package/dist/readiness.js +42 -4
- package/dist/readiness.js.map +1 -1
- package/dist/resolve.d.ts.map +1 -1
- package/dist/resolve.js +3 -1
- package/dist/resolve.js.map +1 -1
- package/dist/spec.d.ts +7 -0
- package/dist/spec.d.ts.map +1 -0
- package/dist/spec.js +64 -0
- package/dist/spec.js.map +1 -0
- package/dist/types.d.ts +101 -3
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/launch.js
CHANGED
|
@@ -1,67 +1,16 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
-
import
|
|
3
|
-
import net from "node:net";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import { CodeServerBinaryNotFoundError, CodeServerPortAllocationError, } from "./errors.js";
|
|
6
|
-
import { resolveCodeServerInstallation } from "./resolve.js";
|
|
7
|
-
const DEFAULT_BIND_HOST = "127.0.0.1";
|
|
8
|
-
const DIRECT_LAUNCH_ACCESS = fs.constants.X_OK;
|
|
9
|
-
async function createCodeServerLaunch(options) {
|
|
10
|
-
const installation = options.installation ?? resolveCodeServerInstallation({
|
|
11
|
-
resolveFrom: options.resolveFrom,
|
|
12
|
-
});
|
|
13
|
-
const launchMode = normalizeLaunchMode(options.launchMode, installation);
|
|
14
|
-
const { extensionsDir, userDataDir } = resolveLaunchDirectories(options);
|
|
15
|
-
const binding = await resolveLaunchBinding(options);
|
|
16
|
-
const workspacePath = options.workspacePath ? path.resolve(options.workspacePath) : null;
|
|
17
|
-
const trustedOrigins = normalizeTrustedOrigins(options.trustedOrigins);
|
|
18
|
-
const cliArgs = buildCodeServerArgs({
|
|
19
|
-
bindAddr: binding.bindAddr,
|
|
20
|
-
extensionsDir,
|
|
21
|
-
trustedOrigins,
|
|
22
|
-
userDataDir,
|
|
23
|
-
workspacePath,
|
|
24
|
-
});
|
|
25
|
-
if (launchMode === "direct") {
|
|
26
|
-
assertDirectLaunchAvailable(installation.entryPoint);
|
|
27
|
-
}
|
|
28
|
-
return launchMode === "node"
|
|
29
|
-
? {
|
|
30
|
-
args: [installation.entryPoint, ...cliArgs],
|
|
31
|
-
bindAddr: binding.bindAddr,
|
|
32
|
-
codeServerPackageRoot: installation.packageRoot,
|
|
33
|
-
command: normalizeNodeCommand(options.nodeCommand),
|
|
34
|
-
entryPoint: installation.entryPoint,
|
|
35
|
-
extensionsDir,
|
|
36
|
-
host: binding.host,
|
|
37
|
-
launchMode,
|
|
38
|
-
port: binding.port,
|
|
39
|
-
supportRoot: installation.supportRoot,
|
|
40
|
-
userDataDir,
|
|
41
|
-
workspacePath,
|
|
42
|
-
}
|
|
43
|
-
: {
|
|
44
|
-
args: cliArgs,
|
|
45
|
-
bindAddr: binding.bindAddr,
|
|
46
|
-
codeServerPackageRoot: installation.packageRoot,
|
|
47
|
-
command: installation.entryPoint,
|
|
48
|
-
entryPoint: installation.entryPoint,
|
|
49
|
-
extensionsDir,
|
|
50
|
-
host: binding.host,
|
|
51
|
-
launchMode,
|
|
52
|
-
port: binding.port,
|
|
53
|
-
supportRoot: installation.supportRoot,
|
|
54
|
-
userDataDir,
|
|
55
|
-
workspacePath,
|
|
56
|
-
};
|
|
57
|
-
}
|
|
2
|
+
import { CodeServerBinaryNotFoundError } from "./errors.js";
|
|
58
3
|
async function launchCodeServerProcess(options) {
|
|
59
4
|
const plan = options.plan;
|
|
60
5
|
const stdoutChunks = [];
|
|
61
6
|
const stderrChunks = [];
|
|
62
7
|
const child = spawn(plan.command, plan.args, {
|
|
63
|
-
cwd: options.cwd,
|
|
64
|
-
env:
|
|
8
|
+
cwd: options.cwd ?? plan.cwd,
|
|
9
|
+
env: {
|
|
10
|
+
...process.env,
|
|
11
|
+
...plan.env,
|
|
12
|
+
...(options.env ?? {}),
|
|
13
|
+
},
|
|
65
14
|
stdio: ["ignore", "pipe", "pipe"],
|
|
66
15
|
});
|
|
67
16
|
child.stdout?.on("data", (chunk) => {
|
|
@@ -92,6 +41,12 @@ async function launchCodeServerProcess(options) {
|
|
|
92
41
|
child,
|
|
93
42
|
codeServerPackageRoot: plan.codeServerPackageRoot,
|
|
94
43
|
command: plan.command,
|
|
44
|
+
cwd: options.cwd ?? plan.cwd,
|
|
45
|
+
env: {
|
|
46
|
+
...process.env,
|
|
47
|
+
...plan.env,
|
|
48
|
+
...(options.env ?? {}),
|
|
49
|
+
},
|
|
95
50
|
exit,
|
|
96
51
|
extensionsDir: plan.extensionsDir,
|
|
97
52
|
getStderr() {
|
|
@@ -106,176 +61,13 @@ async function launchCodeServerProcess(options) {
|
|
|
106
61
|
},
|
|
107
62
|
launchMode: plan.launchMode,
|
|
108
63
|
pid: child.pid,
|
|
64
|
+
plan,
|
|
109
65
|
port: plan.port,
|
|
110
66
|
supportRoot: plan.supportRoot,
|
|
111
67
|
userDataDir: plan.userDataDir,
|
|
112
68
|
workspacePath: plan.workspacePath,
|
|
113
69
|
};
|
|
114
70
|
}
|
|
115
|
-
function buildCodeServerArgs(options) {
|
|
116
|
-
const args = [
|
|
117
|
-
"--auth",
|
|
118
|
-
"none",
|
|
119
|
-
"--bind-addr",
|
|
120
|
-
options.bindAddr,
|
|
121
|
-
"--disable-telemetry",
|
|
122
|
-
"--disable-update-check",
|
|
123
|
-
"--disable-workspace-trust",
|
|
124
|
-
"--disable-getting-started-override",
|
|
125
|
-
"--user-data-dir",
|
|
126
|
-
options.userDataDir,
|
|
127
|
-
"--extensions-dir",
|
|
128
|
-
options.extensionsDir,
|
|
129
|
-
];
|
|
130
|
-
for (const origin of options.trustedOrigins) {
|
|
131
|
-
args.push("--trusted-origins", origin);
|
|
132
|
-
}
|
|
133
|
-
if (options.workspacePath) {
|
|
134
|
-
args.push(options.workspacePath);
|
|
135
|
-
}
|
|
136
|
-
return args;
|
|
137
|
-
}
|
|
138
|
-
function resolveLaunchDirectories(options) {
|
|
139
|
-
const dataRoot = options.dataRoot ? path.resolve(options.dataRoot) : null;
|
|
140
|
-
const userDataDir = options.userDataDir
|
|
141
|
-
? path.resolve(options.userDataDir)
|
|
142
|
-
: dataRoot
|
|
143
|
-
? path.join(dataRoot, "user-data")
|
|
144
|
-
: null;
|
|
145
|
-
const extensionsDir = options.extensionsDir
|
|
146
|
-
? path.resolve(options.extensionsDir)
|
|
147
|
-
: dataRoot
|
|
148
|
-
? path.join(dataRoot, "extensions")
|
|
149
|
-
: null;
|
|
150
|
-
if (!userDataDir || !extensionsDir) {
|
|
151
|
-
throw new TypeError("createCodeServerLaunch requires userDataDir and extensionsDir, or a shared dataRoot.");
|
|
152
|
-
}
|
|
153
|
-
return {
|
|
154
|
-
extensionsDir,
|
|
155
|
-
userDataDir,
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
async function resolveLaunchBinding(options) {
|
|
159
|
-
if (options.bindAddr && (options.host || options.port !== undefined)) {
|
|
160
|
-
throw new TypeError("Pass either bindAddr or host/port to createCodeServerLaunch, not both.");
|
|
161
|
-
}
|
|
162
|
-
if (options.bindAddr) {
|
|
163
|
-
const parsed = parseBindAddr(options.bindAddr);
|
|
164
|
-
const port = parsed.port === 0 ? await allocatePort(parsed.host) : parsed.port;
|
|
165
|
-
return {
|
|
166
|
-
bindAddr: formatBindAddr(parsed.host, port),
|
|
167
|
-
host: parsed.host,
|
|
168
|
-
port,
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
const host = normalizeHost(options.host);
|
|
172
|
-
const port = options.port == null || options.port === 0
|
|
173
|
-
? await allocatePort(host)
|
|
174
|
-
: normalizePort(options.port);
|
|
175
|
-
return {
|
|
176
|
-
bindAddr: formatBindAddr(host, port),
|
|
177
|
-
host,
|
|
178
|
-
port,
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
function normalizeLaunchMode(requested, installation) {
|
|
182
|
-
if (requested === "direct" || requested === "node") {
|
|
183
|
-
return requested;
|
|
184
|
-
}
|
|
185
|
-
return installation.entryKind === "executable" ? "direct" : "node";
|
|
186
|
-
}
|
|
187
|
-
function normalizeTrustedOrigins(value) {
|
|
188
|
-
const normalized = [];
|
|
189
|
-
for (const origin of value ?? []) {
|
|
190
|
-
if (typeof origin !== "string")
|
|
191
|
-
continue;
|
|
192
|
-
const trimmed = origin.trim();
|
|
193
|
-
if (!trimmed || normalized.includes(trimmed))
|
|
194
|
-
continue;
|
|
195
|
-
normalized.push(trimmed);
|
|
196
|
-
}
|
|
197
|
-
return normalized;
|
|
198
|
-
}
|
|
199
|
-
function normalizeNodeCommand(value) {
|
|
200
|
-
const normalized = typeof value === "string" ? value.trim() : "";
|
|
201
|
-
return normalized || process.execPath;
|
|
202
|
-
}
|
|
203
|
-
function normalizeHost(value) {
|
|
204
|
-
const normalized = typeof value === "string" ? value.trim() : "";
|
|
205
|
-
return normalized || DEFAULT_BIND_HOST;
|
|
206
|
-
}
|
|
207
|
-
function normalizePort(value) {
|
|
208
|
-
if (!Number.isInteger(value) || value < 0 || value > 65535) {
|
|
209
|
-
throw new TypeError("Port must be an integer between 0 and 65535.");
|
|
210
|
-
}
|
|
211
|
-
return value;
|
|
212
|
-
}
|
|
213
|
-
function parseBindAddr(bindAddr) {
|
|
214
|
-
const normalized = bindAddr.trim();
|
|
215
|
-
const ipv6Match = /^\[(.+)\]:(\d+)$/.exec(normalized);
|
|
216
|
-
if (ipv6Match) {
|
|
217
|
-
return {
|
|
218
|
-
host: ipv6Match[1],
|
|
219
|
-
port: normalizePort(Number(ipv6Match[2])),
|
|
220
|
-
};
|
|
221
|
-
}
|
|
222
|
-
const lastColonIndex = normalized.lastIndexOf(":");
|
|
223
|
-
if (lastColonIndex <= 0) {
|
|
224
|
-
throw new TypeError("bindAddr must use host:port or [host]:port format.");
|
|
225
|
-
}
|
|
226
|
-
return {
|
|
227
|
-
host: normalized.slice(0, lastColonIndex),
|
|
228
|
-
port: normalizePort(Number(normalized.slice(lastColonIndex + 1))),
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
function formatBindAddr(host, port) {
|
|
232
|
-
return host.includes(":")
|
|
233
|
-
? `[${host}]:${port}`
|
|
234
|
-
: `${host}:${port}`;
|
|
235
|
-
}
|
|
236
|
-
async function allocatePort(host) {
|
|
237
|
-
return await new Promise((resolve, reject) => {
|
|
238
|
-
const server = net.createServer();
|
|
239
|
-
server.once("error", (error) => {
|
|
240
|
-
reject(new CodeServerPortAllocationError("Could not allocate a code-server TCP port.", {
|
|
241
|
-
cause: error instanceof Error ? error.message : String(error),
|
|
242
|
-
host,
|
|
243
|
-
}));
|
|
244
|
-
});
|
|
245
|
-
server.listen(0, host, () => {
|
|
246
|
-
const address = server.address();
|
|
247
|
-
if (!address || typeof address === "string") {
|
|
248
|
-
server.close();
|
|
249
|
-
reject(new CodeServerPortAllocationError("Could not determine the allocated code-server TCP port.", {
|
|
250
|
-
host,
|
|
251
|
-
}));
|
|
252
|
-
return;
|
|
253
|
-
}
|
|
254
|
-
server.close((error) => {
|
|
255
|
-
if (error) {
|
|
256
|
-
reject(new CodeServerPortAllocationError("Could not release the allocated code-server TCP port.", {
|
|
257
|
-
cause: error.message,
|
|
258
|
-
host,
|
|
259
|
-
port: address.port,
|
|
260
|
-
}));
|
|
261
|
-
return;
|
|
262
|
-
}
|
|
263
|
-
resolve(address.port);
|
|
264
|
-
});
|
|
265
|
-
});
|
|
266
|
-
});
|
|
267
|
-
}
|
|
268
|
-
function assertDirectLaunchAvailable(entryPoint) {
|
|
269
|
-
try {
|
|
270
|
-
fs.accessSync(entryPoint, DIRECT_LAUNCH_ACCESS);
|
|
271
|
-
}
|
|
272
|
-
catch (error) {
|
|
273
|
-
throw new CodeServerBinaryNotFoundError("Resolved code-server entrypoint is not directly executable.", {
|
|
274
|
-
cause: error instanceof Error ? error.message : String(error),
|
|
275
|
-
entryPoint,
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
71
|
function wrapSpawnError(error, plan) {
|
|
280
72
|
const errorCode = typeof error === "object" && error && "code" in error
|
|
281
73
|
? String(error.code)
|
|
@@ -288,5 +80,5 @@ function wrapSpawnError(error, plan) {
|
|
|
288
80
|
}
|
|
289
81
|
return error instanceof Error ? error : new Error(String(error));
|
|
290
82
|
}
|
|
291
|
-
export {
|
|
83
|
+
export { launchCodeServerProcess };
|
|
292
84
|
//# sourceMappingURL=launch.js.map
|
package/dist/launch.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"launch.js","sourceRoot":"","sources":["../src/launch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"launch.js","sourceRoot":"","sources":["../src/launch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAE3C,OAAO,EAAE,6BAA6B,EAAE,MAAM,aAAa,CAAC;AAQ5D,KAAK,UAAU,uBAAuB,CAAC,OAAuC;IAC5E,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI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I,CAAC,WAAW;QAC7B,WAAW,EAAE,IAAI,CAAC,WAAW;QAC7B,aAAa,EAAE,IAAI,CAAC,aAAa;KAClC,CAAC;AACJ,CAAC;AAED,SAAS,cAAc,CAAC,KAAc,EAAE,IAA0B;IAChE,MAAM,SAAS,GAAG,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,IAAI,MAAM,IAAI,KAAK;QACrE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC;QACpB,CAAC,CAAC,IAAI,CAAC;IAET,IAAI,SAAS,KAAK,QAAQ,EAAE,CAAC;QAC3B,OAAO,IAAI,6BAA6B,CAAC,oDAAoD,EAAE;YAC7F,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,OAAO,EAAE,IAAI,CAAC,OAAO;SACtB,CAAC,CAAC;IACL,CAAC;IAED,OAAO,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;AACnE,CAAC;AAED,OAAO,EAAE,uBAAuB,EAAE,CAAC"}
|
package/dist/plan.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { CodeServerLaunchOptions, CodeServerLaunchPlan, CodeServerLaunchSpec, CreateCodeServerLaunchPlanOptions } from "./types.js";
|
|
2
|
+
declare function createCodeServerLaunchPlan(options: CreateCodeServerLaunchPlanOptions): Promise<CodeServerLaunchPlan>;
|
|
3
|
+
declare function createCodeServerLaunch(options: CodeServerLaunchOptions): Promise<CodeServerLaunchPlan>;
|
|
4
|
+
declare function buildCodeServerArgs(options: {
|
|
5
|
+
bindAddr: string;
|
|
6
|
+
extensionsDir: string;
|
|
7
|
+
trustedOrigins: string[];
|
|
8
|
+
userDataDir: string;
|
|
9
|
+
workspacePath: string | null;
|
|
10
|
+
}): string[];
|
|
11
|
+
declare function buildCodeServerLaunchSpec(plan: CodeServerLaunchPlan): CodeServerLaunchSpec;
|
|
12
|
+
declare function normalizeTrustedOrigins(value?: string[]): string[];
|
|
13
|
+
declare function allocatePort(host: string): Promise<number>;
|
|
14
|
+
export { allocatePort, buildCodeServerArgs, buildCodeServerLaunchSpec, createCodeServerLaunch, createCodeServerLaunchPlan, normalizeTrustedOrigins, };
|
|
15
|
+
//# sourceMappingURL=plan.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plan.d.ts","sourceRoot":"","sources":["../src/plan.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAEV,uBAAuB,EACvB,oBAAoB,EACpB,oBAAoB,EAGpB,iCAAiC,EAClC,MAAM,YAAY,CAAC;AAKpB,iBAAe,0BAA0B,CAAC,OAAO,EAAE,iCAAiC,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAgGnH;AAED,iBAAe,sBAAsB,CAAC,OAAO,EAAE,uBAAuB,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAErG;AAED,iBAAS,mBAAmB,CAAC,OAAO,EAAE;IACpC,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B,GAAG,MAAM,EAAE,CAyBX;AAED,iBAAS,yBAAyB,CAAC,IAAI,EAAE,oBAAoB,GAAG,oBAAoB,CAgCnF;AAED,iBAAS,uBAAuB,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CA6B3D;AA+HD,iBAAe,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAmCzD;AAoDD,OAAO,EACL,YAAY,EACZ,mBAAmB,EACnB,yBAAyB,EACzB,sBAAsB,EACtB,0BAA0B,EAC1B,uBAAuB,GACxB,CAAC"}
|
package/dist/plan.js
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import net from "node:net";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { CodeServerBinaryNotFoundError, CodeServerInvalidConfigurationError, CodeServerLaunchPlanningError, CodeServerPortAllocationError, } from "./errors.js";
|
|
5
|
+
import { resolveCodeServerInstallation } from "./resolve.js";
|
|
6
|
+
const DEFAULT_BIND_HOST = "127.0.0.1";
|
|
7
|
+
const DIRECT_LAUNCH_ACCESS = fs.constants.X_OK;
|
|
8
|
+
async function createCodeServerLaunchPlan(options) {
|
|
9
|
+
try {
|
|
10
|
+
const installation = options.installation ?? resolveCodeServerInstallation({
|
|
11
|
+
resolveFrom: options.resolveFrom,
|
|
12
|
+
});
|
|
13
|
+
const launchMode = normalizeLaunchMode(options.launchMode, installation);
|
|
14
|
+
const { extensionsDir, userDataDir } = resolveLaunchDirectories(options);
|
|
15
|
+
const binding = await resolveLaunchBinding(options);
|
|
16
|
+
const workspacePath = options.workspacePath ? path.resolve(options.workspacePath) : null;
|
|
17
|
+
const trustedOrigins = normalizeTrustedOrigins(options.trustedOrigins);
|
|
18
|
+
const cwd = path.resolve(options.cwd ?? installation.packageRoot);
|
|
19
|
+
const env = {
|
|
20
|
+
...(options.env ?? {}),
|
|
21
|
+
};
|
|
22
|
+
const cliArgs = buildCodeServerArgs({
|
|
23
|
+
bindAddr: binding.bindAddr,
|
|
24
|
+
extensionsDir,
|
|
25
|
+
trustedOrigins,
|
|
26
|
+
userDataDir,
|
|
27
|
+
workspacePath,
|
|
28
|
+
});
|
|
29
|
+
if (launchMode === "direct") {
|
|
30
|
+
assertDirectLaunchAvailable(installation.entryPoint);
|
|
31
|
+
}
|
|
32
|
+
const supportBindings = buildSupportBindings(installation);
|
|
33
|
+
const recommendedReadablePaths = uniquePaths([
|
|
34
|
+
installation.packageRoot,
|
|
35
|
+
installation.entryPoint,
|
|
36
|
+
installation.supportRoot,
|
|
37
|
+
workspacePath,
|
|
38
|
+
]);
|
|
39
|
+
const recommendedWritablePaths = uniquePaths([
|
|
40
|
+
userDataDir,
|
|
41
|
+
extensionsDir,
|
|
42
|
+
]);
|
|
43
|
+
return launchMode === "node"
|
|
44
|
+
? {
|
|
45
|
+
args: [installation.entryPoint, ...cliArgs],
|
|
46
|
+
bindAddr: binding.bindAddr,
|
|
47
|
+
codeServerPackageRoot: installation.packageRoot,
|
|
48
|
+
command: normalizeNodeCommand(options.nodeCommand),
|
|
49
|
+
cwd,
|
|
50
|
+
entryKind: installation.entryKind,
|
|
51
|
+
entryPoint: installation.entryPoint,
|
|
52
|
+
env,
|
|
53
|
+
extensionsDir,
|
|
54
|
+
host: binding.host,
|
|
55
|
+
installation,
|
|
56
|
+
launchMode,
|
|
57
|
+
port: binding.port,
|
|
58
|
+
recommendedReadablePaths,
|
|
59
|
+
recommendedWritablePaths,
|
|
60
|
+
supportBindings,
|
|
61
|
+
supportRoot: installation.supportRoot,
|
|
62
|
+
trustedOrigins,
|
|
63
|
+
userDataDir,
|
|
64
|
+
workspacePath,
|
|
65
|
+
}
|
|
66
|
+
: {
|
|
67
|
+
args: cliArgs,
|
|
68
|
+
bindAddr: binding.bindAddr,
|
|
69
|
+
codeServerPackageRoot: installation.packageRoot,
|
|
70
|
+
command: installation.entryPoint,
|
|
71
|
+
cwd,
|
|
72
|
+
entryKind: installation.entryKind,
|
|
73
|
+
entryPoint: installation.entryPoint,
|
|
74
|
+
env,
|
|
75
|
+
extensionsDir,
|
|
76
|
+
host: binding.host,
|
|
77
|
+
installation,
|
|
78
|
+
launchMode,
|
|
79
|
+
port: binding.port,
|
|
80
|
+
recommendedReadablePaths,
|
|
81
|
+
recommendedWritablePaths,
|
|
82
|
+
supportBindings,
|
|
83
|
+
supportRoot: installation.supportRoot,
|
|
84
|
+
trustedOrigins,
|
|
85
|
+
userDataDir,
|
|
86
|
+
workspacePath,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
if (error instanceof CodeServerLaunchPlanningError || error instanceof CodeServerInvalidConfigurationError) {
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
if (error instanceof Error && "code" in error) {
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
throw new CodeServerLaunchPlanningError("Could not create a code-server launch plan.", {
|
|
97
|
+
cause: error instanceof Error ? error.message : String(error),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
async function createCodeServerLaunch(options) {
|
|
102
|
+
return await createCodeServerLaunchPlan(options);
|
|
103
|
+
}
|
|
104
|
+
function buildCodeServerArgs(options) {
|
|
105
|
+
const args = [
|
|
106
|
+
"--auth",
|
|
107
|
+
"none",
|
|
108
|
+
"--bind-addr",
|
|
109
|
+
options.bindAddr,
|
|
110
|
+
"--disable-telemetry",
|
|
111
|
+
"--disable-update-check",
|
|
112
|
+
"--disable-workspace-trust",
|
|
113
|
+
"--disable-getting-started-override",
|
|
114
|
+
"--user-data-dir",
|
|
115
|
+
options.userDataDir,
|
|
116
|
+
"--extensions-dir",
|
|
117
|
+
options.extensionsDir,
|
|
118
|
+
];
|
|
119
|
+
for (const origin of options.trustedOrigins) {
|
|
120
|
+
args.push("--trusted-origins", origin);
|
|
121
|
+
}
|
|
122
|
+
if (options.workspacePath) {
|
|
123
|
+
args.push(options.workspacePath);
|
|
124
|
+
}
|
|
125
|
+
return args;
|
|
126
|
+
}
|
|
127
|
+
function buildCodeServerLaunchSpec(plan) {
|
|
128
|
+
const bindings = uniqueBindings([
|
|
129
|
+
{
|
|
130
|
+
access: "read",
|
|
131
|
+
hostPath: plan.installation.packageRoot,
|
|
132
|
+
mountPath: plan.installation.packageRoot,
|
|
133
|
+
reason: "code-server package root",
|
|
134
|
+
},
|
|
135
|
+
...plan.supportBindings,
|
|
136
|
+
...plan.recommendedWritablePaths.map((value) => ({
|
|
137
|
+
access: "write",
|
|
138
|
+
hostPath: value,
|
|
139
|
+
mountPath: value,
|
|
140
|
+
reason: value === plan.userDataDir
|
|
141
|
+
? "code-server user data"
|
|
142
|
+
: value === plan.extensionsDir
|
|
143
|
+
? "code-server extensions"
|
|
144
|
+
: "code-server writable path",
|
|
145
|
+
})),
|
|
146
|
+
]);
|
|
147
|
+
return {
|
|
148
|
+
args: [...plan.args],
|
|
149
|
+
bindings,
|
|
150
|
+
command: plan.command,
|
|
151
|
+
cwd: plan.cwd,
|
|
152
|
+
env: {
|
|
153
|
+
...plan.env,
|
|
154
|
+
},
|
|
155
|
+
readablePaths: [...plan.recommendedReadablePaths],
|
|
156
|
+
writablePaths: [...plan.recommendedWritablePaths],
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
function normalizeTrustedOrigins(value) {
|
|
160
|
+
const normalized = [];
|
|
161
|
+
for (const raw of value ?? []) {
|
|
162
|
+
if (typeof raw !== "string")
|
|
163
|
+
continue;
|
|
164
|
+
const trimmed = raw.trim();
|
|
165
|
+
if (!trimmed)
|
|
166
|
+
continue;
|
|
167
|
+
let origin;
|
|
168
|
+
try {
|
|
169
|
+
origin = new URL(trimmed).origin;
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
throw new CodeServerInvalidConfigurationError("trustedOrigins entries must be absolute origins.", {
|
|
173
|
+
value: trimmed,
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
if (origin === "null") {
|
|
177
|
+
throw new CodeServerInvalidConfigurationError("trustedOrigins entries must resolve to normal HTTP or HTTPS origins.", {
|
|
178
|
+
value: trimmed,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
if (!normalized.includes(origin)) {
|
|
182
|
+
normalized.push(origin);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return normalized;
|
|
186
|
+
}
|
|
187
|
+
function resolveLaunchDirectories(options) {
|
|
188
|
+
const dataRoot = options.dataRoot ? path.resolve(options.dataRoot) : null;
|
|
189
|
+
const userDataDir = options.userDataDir
|
|
190
|
+
? path.resolve(options.userDataDir)
|
|
191
|
+
: dataRoot
|
|
192
|
+
? path.join(dataRoot, "user-data")
|
|
193
|
+
: null;
|
|
194
|
+
const extensionsDir = options.extensionsDir
|
|
195
|
+
? path.resolve(options.extensionsDir)
|
|
196
|
+
: dataRoot
|
|
197
|
+
? path.join(dataRoot, "extensions")
|
|
198
|
+
: null;
|
|
199
|
+
if (!userDataDir || !extensionsDir) {
|
|
200
|
+
throw new CodeServerInvalidConfigurationError("createCodeServerLaunchPlan requires userDataDir and extensionsDir, or a shared dataRoot.");
|
|
201
|
+
}
|
|
202
|
+
return {
|
|
203
|
+
extensionsDir,
|
|
204
|
+
userDataDir,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
async function resolveLaunchBinding(options) {
|
|
208
|
+
if (options.bindAddr && (options.host || options.port !== undefined)) {
|
|
209
|
+
throw new CodeServerInvalidConfigurationError("Pass either bindAddr or host/port to createCodeServerLaunchPlan, not both.");
|
|
210
|
+
}
|
|
211
|
+
if (options.bindAddr) {
|
|
212
|
+
const parsed = parseBindAddr(options.bindAddr);
|
|
213
|
+
const port = parsed.port === 0 ? await allocatePort(parsed.host) : parsed.port;
|
|
214
|
+
return {
|
|
215
|
+
bindAddr: formatBindAddr(parsed.host, port),
|
|
216
|
+
host: parsed.host,
|
|
217
|
+
port,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
const host = normalizeHost(options.host);
|
|
221
|
+
const port = options.port == null || options.port === 0
|
|
222
|
+
? await allocatePort(host)
|
|
223
|
+
: normalizePort(options.port);
|
|
224
|
+
return {
|
|
225
|
+
bindAddr: formatBindAddr(host, port),
|
|
226
|
+
host,
|
|
227
|
+
port,
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
function normalizeLaunchMode(requested, installation) {
|
|
231
|
+
if (requested === "direct" || requested === "node") {
|
|
232
|
+
return requested;
|
|
233
|
+
}
|
|
234
|
+
return installation.entryKind === "executable" ? "direct" : "node";
|
|
235
|
+
}
|
|
236
|
+
function normalizeNodeCommand(value) {
|
|
237
|
+
const normalized = typeof value === "string" ? value.trim() : "";
|
|
238
|
+
return normalized || process.execPath;
|
|
239
|
+
}
|
|
240
|
+
function normalizeHost(value) {
|
|
241
|
+
const normalized = typeof value === "string" ? value.trim() : "";
|
|
242
|
+
return normalized || DEFAULT_BIND_HOST;
|
|
243
|
+
}
|
|
244
|
+
function normalizePort(value) {
|
|
245
|
+
if (!Number.isInteger(value) || value < 0 || value > 65535) {
|
|
246
|
+
throw new CodeServerInvalidConfigurationError("Port must be an integer between 0 and 65535.", {
|
|
247
|
+
value,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
return value;
|
|
251
|
+
}
|
|
252
|
+
function parseBindAddr(bindAddr) {
|
|
253
|
+
const normalized = bindAddr.trim();
|
|
254
|
+
const ipv6Match = /^\[(.+)\]:(\d+)$/.exec(normalized);
|
|
255
|
+
if (ipv6Match) {
|
|
256
|
+
return {
|
|
257
|
+
host: ipv6Match[1],
|
|
258
|
+
port: normalizePort(Number(ipv6Match[2])),
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
const lastColonIndex = normalized.lastIndexOf(":");
|
|
262
|
+
if (lastColonIndex <= 0) {
|
|
263
|
+
throw new CodeServerInvalidConfigurationError("bindAddr must use host:port or [host]:port format.", {
|
|
264
|
+
bindAddr,
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
return {
|
|
268
|
+
host: normalized.slice(0, lastColonIndex),
|
|
269
|
+
port: normalizePort(Number(normalized.slice(lastColonIndex + 1))),
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
function formatBindAddr(host, port) {
|
|
273
|
+
return host.includes(":")
|
|
274
|
+
? `[${host}]:${port}`
|
|
275
|
+
: `${host}:${port}`;
|
|
276
|
+
}
|
|
277
|
+
async function allocatePort(host) {
|
|
278
|
+
return await new Promise((resolve, reject) => {
|
|
279
|
+
const server = net.createServer();
|
|
280
|
+
server.once("error", (error) => {
|
|
281
|
+
reject(new CodeServerPortAllocationError("Could not allocate a code-server TCP port.", {
|
|
282
|
+
cause: error instanceof Error ? error.message : String(error),
|
|
283
|
+
host,
|
|
284
|
+
}));
|
|
285
|
+
});
|
|
286
|
+
server.listen(0, host, () => {
|
|
287
|
+
const address = server.address();
|
|
288
|
+
if (!address || typeof address === "string") {
|
|
289
|
+
server.close();
|
|
290
|
+
reject(new CodeServerPortAllocationError("Could not determine the allocated code-server TCP port.", {
|
|
291
|
+
host,
|
|
292
|
+
}));
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
server.close((error) => {
|
|
296
|
+
if (error) {
|
|
297
|
+
reject(new CodeServerPortAllocationError("Could not release the allocated code-server TCP port.", {
|
|
298
|
+
cause: error.message,
|
|
299
|
+
host,
|
|
300
|
+
port: address.port,
|
|
301
|
+
}));
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
resolve(address.port);
|
|
305
|
+
});
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
function assertDirectLaunchAvailable(entryPoint) {
|
|
310
|
+
try {
|
|
311
|
+
fs.accessSync(entryPoint, DIRECT_LAUNCH_ACCESS);
|
|
312
|
+
}
|
|
313
|
+
catch (error) {
|
|
314
|
+
throw new CodeServerBinaryNotFoundError("Resolved code-server entrypoint is not directly executable.", {
|
|
315
|
+
cause: error instanceof Error ? error.message : String(error),
|
|
316
|
+
entryPoint,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
function buildSupportBindings(installation) {
|
|
321
|
+
if (!installation.supportRoot)
|
|
322
|
+
return [];
|
|
323
|
+
return [{
|
|
324
|
+
access: "read",
|
|
325
|
+
hostPath: installation.supportRoot,
|
|
326
|
+
mountPath: installation.supportRoot,
|
|
327
|
+
reason: "code-server support root",
|
|
328
|
+
}];
|
|
329
|
+
}
|
|
330
|
+
function uniquePaths(values) {
|
|
331
|
+
const normalized = [];
|
|
332
|
+
for (const value of values) {
|
|
333
|
+
if (typeof value !== "string")
|
|
334
|
+
continue;
|
|
335
|
+
const nextValue = path.resolve(value);
|
|
336
|
+
if (!normalized.includes(nextValue)) {
|
|
337
|
+
normalized.push(nextValue);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return normalized;
|
|
341
|
+
}
|
|
342
|
+
function uniqueBindings(values) {
|
|
343
|
+
const bindings = [];
|
|
344
|
+
const seen = new Set();
|
|
345
|
+
for (const value of values) {
|
|
346
|
+
const key = `${value.access}:${value.hostPath}:${value.mountPath}`;
|
|
347
|
+
if (seen.has(key))
|
|
348
|
+
continue;
|
|
349
|
+
seen.add(key);
|
|
350
|
+
bindings.push(value);
|
|
351
|
+
}
|
|
352
|
+
return bindings;
|
|
353
|
+
}
|
|
354
|
+
export { allocatePort, buildCodeServerArgs, buildCodeServerLaunchSpec, createCodeServerLaunch, createCodeServerLaunchPlan, normalizeTrustedOrigins, };
|
|
355
|
+
//# sourceMappingURL=plan.js.map
|