-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathutil.ts
More file actions
510 lines (451 loc) · 17.2 KB
/
util.ts
File metadata and controls
510 lines (451 loc) · 17.2 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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ChildProcess, SpawnOptions, spawn } from 'child_process';
import { createHash } from 'crypto';
import { existsSync, readFileSync, readdirSync } from 'fs';
import { HttpProxyAgent } from 'http-proxy-agent';
import * as https from 'https';
import { HttpsProxyAgent } from 'https-proxy-agent';
import * as path from 'path';
import { URL } from 'url';
import { DownloadOptions, DownloadPlatform, defaultCachePath, downloadAndUnzipVSCode } from './download';
import * as request from './request';
import { TestOptions } from './runTest';
import { ProgressReporter } from './progress';
export let systemDefaultPlatform: DownloadPlatform;
const isPlatformWindows = (platform: string) => platform.includes('win32');
const isPlatformDarwin = (platform: string) => platform.includes('darwin');
const isPlatformLinux = (platform: string) => platform.includes('linux');
export const isPlatformServer = (platform: string) => platform.includes('server');
export const isPlatformCLI = (platform: string) => platform.includes('cli-');
// Extract the architecture component from a download platform string. The
// legacy `darwin` / `server-darwin` names have no arch suffix and refer to x64.
const getPlatformArch = (platform: string): NodeJS.Architecture | undefined => {
if (platform.includes('arm64')) return 'arm64';
if (platform.includes('armhf')) return 'arm';
if (platform.includes('x64')) return 'x64';
if (isPlatformDarwin(platform)) return 'x64';
return undefined;
};
export const isRunnableOnHost = (platform: string): boolean => {
const osMatches =
process.platform === 'win32'
? isPlatformWindows(platform)
: process.platform === 'darwin'
? isPlatformDarwin(platform)
: isPlatformLinux(platform);
return osMatches && getPlatformArch(platform) === process.arch;
};
switch (process.platform) {
case 'darwin':
systemDefaultPlatform = process.arch === 'arm64' ? 'darwin-arm64' : 'darwin';
break;
case 'win32':
systemDefaultPlatform = process.arch === 'arm64' ? 'win32-arm64-archive' : 'win32-x64-archive';
break;
default:
systemDefaultPlatform =
process.arch === 'arm64' ? 'linux-arm64' : process.arch === 'arm' ? 'linux-armhf' : 'linux-x64';
}
const UNRELEASED_SUFFIX = '-unreleased';
export class Version {
public static parse(version: string): Version {
const unreleased = version.endsWith(UNRELEASED_SUFFIX);
if (unreleased) {
version = version.slice(0, -UNRELEASED_SUFFIX.length);
}
return new Version(version, !unreleased);
}
constructor(
public readonly id: string,
public readonly isReleased = true,
) {}
public get isCommit() {
return /^[0-9a-f]{40}$/.test(this.id);
}
public get isInsiders() {
return this.id === 'insiders' || this.id.endsWith('-insider');
}
public get isStable() {
return this.id === 'stable' || /^[0-9]+\.[0-9]+\.[0-9]$/.test(this.id);
}
public toString() {
return this.id + (this.isReleased ? '' : UNRELEASED_SUFFIX);
}
}
export function getVSCodeDownloadUrl(version: Version, platform: string) {
if (version.id === 'insiders') {
return `https://update.code.visualstudio.com/latest/${platform}/insider?released=${version.isReleased}`;
} else if (version.isInsiders) {
return `https://update.code.visualstudio.com/${version.id}/${platform}/insider?released=${version.isReleased}`;
} else if (version.isStable) {
return `https://update.code.visualstudio.com/${version.id}/${platform}/stable?released=${version.isReleased}`;
} else {
// insiders commit hash
return `https://update.code.visualstudio.com/commit:${version.id}/${platform}/insider`;
}
}
let PROXY_AGENT: HttpProxyAgent<string> | undefined = undefined;
let HTTPS_PROXY_AGENT: HttpsProxyAgent<string> | undefined = undefined;
if (process.env.npm_config_proxy) {
PROXY_AGENT = new HttpProxyAgent(process.env.npm_config_proxy);
HTTPS_PROXY_AGENT = new HttpsProxyAgent(process.env.npm_config_proxy);
}
if (process.env.npm_config_https_proxy) {
HTTPS_PROXY_AGENT = new HttpsProxyAgent(process.env.npm_config_https_proxy);
}
export function urlToOptions(url: string): https.RequestOptions {
const parsed = new URL(url);
const options: https.RequestOptions = {};
if (PROXY_AGENT && parsed.protocol.startsWith('http:')) {
options.agent = PROXY_AGENT;
}
if (HTTPS_PROXY_AGENT && parsed.protocol.startsWith('https:')) {
options.agent = HTTPS_PROXY_AGENT;
}
return options;
}
export function downloadDirToExecutablePath(dir: string, platform: DownloadPlatform) {
if (isPlatformServer(platform)) {
return isPlatformWindows(platform)
? path.resolve(dir, 'bin', 'code-server.cmd')
: path.resolve(dir, 'bin', 'code-server');
} else if (isPlatformCLI(platform)) {
return isPlatformWindows(platform) ? path.resolve(dir, 'code.exe') : path.resolve(dir, 'code');
} else {
if (isPlatformWindows(platform)) {
return path.resolve(dir, 'Code.exe');
} else if (isPlatformDarwin(platform)) {
return path.resolve(dir, 'Visual Studio Code.app/Contents/MacOS/Electron');
} else {
return path.resolve(dir, 'code');
}
}
}
export function insidersDownloadDirToExecutablePath(dir: string, platform: DownloadPlatform) {
if (isPlatformServer(platform)) {
return isPlatformWindows(platform)
? path.resolve(dir, 'bin', 'code-server-insiders.cmd')
: path.resolve(dir, 'bin', 'code-server-insiders');
} else if (isPlatformCLI(platform)) {
return isPlatformWindows(platform) ? path.resolve(dir, 'code-insiders.exe') : path.resolve(dir, 'code-insiders');
} else {
if (isPlatformWindows(platform)) {
return path.resolve(dir, 'Code - Insiders.exe');
} else if (isPlatformDarwin(platform)) {
return path.resolve(dir, 'Visual Studio Code - Insiders.app/Contents/MacOS/Electron');
} else {
return path.resolve(dir, 'code-insiders');
}
}
}
export function insidersDownloadDirMetadata(
dir: string,
platform: DownloadPlatform,
reporter: ProgressReporter,
latestHash: string,
) {
let productJsonPath;
if (isPlatformServer(platform)) {
productJsonPath = path.resolve(dir, 'product.json');
} else if (isPlatformWindows(platform)) {
// https://github.com/microsoft/vscode/issues/293013
// https://github.com/microsoft/vscode/issues/279329#issuecomment-3580527758
// Recent Windows archives nest the application under a versioned folder
// named after the first 10 characters of the commit hash, e.g.
// `<dir>/39d5031f21/resources/app/product.json`. The installed build's
// commit may differ from the server's latest, so we cannot assume the
// folder is named after `latestHash`; locate the product.json instead.
productJsonPath = findWindowsProductJsonPath(dir, latestHash);
} else if (isPlatformDarwin(platform)) {
productJsonPath = path.resolve(dir, 'Visual Studio Code - Insiders.app/Contents/Resources/app/product.json');
} else {
productJsonPath = path.resolve(dir, 'resources/app/product.json');
}
try {
const productJson = JSON.parse(readFileSync(productJsonPath, 'utf-8'));
return {
version: productJson.commit,
date: new Date(productJson.date),
};
} catch (e) {
reporter.error(`Error reading product.json (${e}) will download again`);
return {
version: 'unknown',
date: new Date(0),
};
}
}
/**
* Resolves the path to `product.json` inside a downloaded Windows VS Code
* archive, accounting for the versioned-resources folder introduced in
* https://github.com/microsoft/vscode/issues/249239.
*/
function findWindowsProductJsonPath(dir: string, latestHash: string) {
const relativeProductJsonPath = path.join('resources', 'app', 'product.json');
// Preferred: the versioned folder named after the latest commit hash. When
// the installed build is up to date this is an exact match.
if (latestHash) {
const hinted = path.resolve(dir, latestHash.slice(0, 10), relativeProductJsonPath);
if (existsSync(hinted)) {
return hinted;
}
}
// Otherwise the install may be outdated and the folder named after a
// different commit; scan for the versioned folder so we can still read the
// currently-installed metadata (e.g. to report the old version being replaced).
try {
for (const entry of readdirSync(dir)) {
if (/^[0-9a-f]{10}$/.test(entry)) {
const candidate = path.resolve(dir, entry, relativeProductJsonPath);
if (existsSync(candidate)) {
return candidate;
}
}
}
} catch {
// ignore, fall through to the legacy layout
}
// Legacy flat layout, where the app lives directly under the archive root.
return path.resolve(dir, relativeProductJsonPath);
}
export interface IUpdateMetadata {
url: string;
name: string;
version: string;
productVersion: string;
hash: string;
timestamp: number;
sha256hash: string;
supportsFastUpdate: boolean;
}
export async function getInsidersVersionMetadata(platform: string, version: string, released: boolean) {
const remoteUrl = `https://update.code.visualstudio.com/api/versions/${version}/${platform}/insider?released=${released}`;
return await request.getJSON<IUpdateMetadata>(remoteUrl, 30_000);
}
export async function getLatestInsidersMetadata(platform: string, released: boolean) {
const remoteUrl = `https://update.code.visualstudio.com/api/update/${platform}/insider/latest?released=${released}`;
return await request.getJSON<IUpdateMetadata>(remoteUrl, 30_000);
}
/**
* Resolve the VS Code cli path from executable path returned from `downloadAndUnzipVSCode`.
* Usually you will want {@link resolveCliArgsFromVSCodeExecutablePath} instead.
*/
export function resolveCliPathFromVSCodeExecutablePath(
vscodeExecutablePath: string,
platform: DownloadPlatform = systemDefaultPlatform,
) {
if (platform === 'win32-archive') {
throw new Error('Windows 32-bit is no longer supported');
}
if (isPlatformServer(platform) || isPlatformCLI(platform)) {
// no separate CLI
return vscodeExecutablePath;
}
if (isPlatformWindows(platform)) {
if (vscodeExecutablePath.endsWith('Code - Insiders.exe')) {
return path.resolve(vscodeExecutablePath, '../bin/code-insiders.cmd');
} else {
return path.resolve(vscodeExecutablePath, '../bin/code.cmd');
}
} else if (isPlatformDarwin(platform)) {
return path.resolve(vscodeExecutablePath, '../../../Contents/Resources/app/bin/code');
} else {
if (vscodeExecutablePath.endsWith('code-insiders')) {
return path.resolve(vscodeExecutablePath, '../bin/code-insiders');
} else {
return path.resolve(vscodeExecutablePath, '../bin/code');
}
}
}
/**
* Resolve the VS Code cli arguments from executable path returned from `downloadAndUnzipVSCode`.
* You can use this path to spawn processes for extension management. For example:
*
* ```ts
* const cp = require('child_process');
* const { downloadAndUnzipVSCode, resolveCliArgsFromVSCodeExecutablePath } = require('@vscode/test-electron')
* const vscodeExecutablePath = await downloadAndUnzipVSCode('1.36.0');
* const [cli, ...args] = resolveCliArgsFromVSCodeExecutablePath(vscodeExecutablePath);
*
* cp.spawnSync(cli, [...args, '--install-extension', '<EXTENSION-ID-OR-PATH-TO-VSIX>'], {
* encoding: 'utf-8',
* stdio: 'inherit'
* shell: process.platform === 'win32',
* });
* ```
*
* @param vscodeExecutablePath The `vscodeExecutablePath` from `downloadAndUnzipVSCode`.
*/
export function resolveCliArgsFromVSCodeExecutablePath(
vscodeExecutablePath: string,
options?: Pick<TestOptions, 'reuseMachineInstall' | 'platform'>,
) {
const args = [
resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath, options?.platform ?? systemDefaultPlatform),
];
if (!options?.reuseMachineInstall) {
args.push(...getProfileArguments(args));
}
return args;
}
export interface RunVSCodeCommandOptions extends Partial<DownloadOptions> {
/**
* Additional options to pass to `child_process.spawn`
*/
spawn?: SpawnOptions;
/**
* Whether VS Code should be launched using default settings and extensions
* installed on this machine. If `false`, then separate directories will be
* used inside the `.vscode-test` folder within the project.
*
* Defaults to `false`.
*/
reuseMachineInstall?: boolean;
}
/** Adds the extensions and user data dir to the arguments for the VS Code CLI */
export function getProfileArguments(args: readonly string[]) {
const out: string[] = [];
if (!hasArg('extensions-dir', args)) {
out.push(`--extensions-dir=${path.join(defaultCachePath, 'extensions')}`);
}
if (!hasArg('user-data-dir', args)) {
out.push(`--user-data-dir=${path.join(defaultCachePath, 'user-data')}`);
}
return out;
}
export function hasArg(argName: string, argList: readonly string[]) {
return argList.some((a) => a === `--${argName}` || a.startsWith(`--${argName}=`));
}
export class VSCodeCommandError extends Error {
constructor(
args: string[],
public readonly exitCode: number | null,
public readonly stderr: string,
public stdout: string,
) {
super(`'code ${args.join(' ')}' failed with exit code ${exitCode}:\n\n${stderr}\n\n${stdout}`);
}
}
/**
* Runs a VS Code command, and returns its output.
*
* @throws a {@link VSCodeCommandError} if the command fails
*/
export async function runVSCodeCommand(_args: readonly string[], options: RunVSCodeCommandOptions = {}) {
const args = _args.slice();
let executable = await downloadAndUnzipVSCode(options);
let shell = false;
if (!options.reuseMachineInstall) {
args.push(...getProfileArguments(args));
}
// Unless the user is manually running tests or extension development, then resolve to the CLI script
if (!hasArg('extensionTestsPath', args) && !hasArg('extensionDevelopmentPath', args)) {
executable = resolveCliPathFromVSCodeExecutablePath(executable, options?.platform ?? systemDefaultPlatform);
shell = process.platform === 'win32'; // CVE-2024-27980
}
return new Promise<{ stdout: string; stderr: string }>((resolve, reject) => {
let stdout = '';
let stderr = '';
const child = spawn(shell ? `"${executable}"` : executable, args, {
stdio: 'pipe',
shell,
windowsHide: true,
...options.spawn,
});
child.stdout?.setEncoding('utf-8').on('data', (data) => (stdout += data));
child.stderr?.setEncoding('utf-8').on('data', (data) => (stderr += data));
child.on('error', reject);
child.on('exit', (code) => {
if (code !== 0) {
reject(new VSCodeCommandError(args, code, stderr, stdout));
} else {
resolve({ stdout, stderr });
}
});
});
}
/** Predicates whether arg is undefined or null */
export function isDefined<T>(arg: T | undefined | null): arg is T {
return arg != null;
}
/**
* Validates the stream data matches the given length and checksum, if any.
*
* Note: md5 is not ideal, but it's what we get from the CDN, and for the
* purposes of self-reported content verification is sufficient.
*/
export function validateStream(readable: NodeJS.ReadableStream, length: number, sha256?: string) {
let actualLen = 0;
const checksum = sha256 ? createHash('sha256') : undefined;
return new Promise<void>((resolve, reject) => {
readable.on('data', (chunk) => {
checksum?.update(chunk);
actualLen += chunk.length;
});
readable.on('error', reject);
readable.on('end', () => {
if (actualLen !== length) {
return reject(new Error(`Downloaded stream length ${actualLen} does not match expected length ${length}`));
}
const digest = checksum?.digest('hex');
if (digest && digest !== sha256) {
return reject(new Error(`Downloaded file checksum ${digest} does not match expected checksum ${sha256}`));
}
resolve();
});
});
}
/** Gets a Buffer from a Node.js stream */
export function streamToBuffer(readable: NodeJS.ReadableStream) {
return new Promise<Buffer>((resolve, reject) => {
const chunks: Buffer[] = [];
readable.on('data', (chunk) => chunks.push(chunk));
readable.on('error', reject);
readable.on('end', () => resolve(Buffer.concat(chunks)));
});
}
/** Gets whether child is a subdirectory of the parent */
export function isSubdirectory(parent: string, child: string) {
const relative = path.relative(parent, child);
return !relative.startsWith('..') && !path.isAbsolute(relative);
}
/**
* Wraps a function so that it's called once, and never again, memoizing
* the result unless it rejects.
*/
export function onceWithoutRejections<T, Args extends unknown[]>(fn: (...args: Args) => Promise<T>) {
let value: Promise<T> | undefined;
return (...args: Args) => {
if (!value) {
value = fn(...args).catch((err) => {
value = undefined;
throw err;
});
}
return value;
};
}
export function killTree(processId: number, force: boolean) {
let cp: ChildProcess;
if (process.platform === 'win32') {
const windir = process.env['WINDIR'] || 'C:\\Windows';
// when killing a process in Windows its child processes are *not* killed but become root processes.
// Therefore we use TASKKILL.EXE
cp = spawn(
path.join(windir, 'System32', 'taskkill.exe'),
[...(force ? ['/F'] : []), '/T', '/PID', processId.toString()],
{ stdio: 'inherit' },
);
} else {
// on linux and OS X we kill all direct and indirect child processes as well
cp = spawn('sh', [path.resolve(__dirname, '../killTree.sh'), processId.toString(), force ? '9' : '15'], {
stdio: 'inherit',
});
}
return new Promise<void>((resolve, reject) => {
cp.on('error', reject).on('exit', resolve);
});
}