-
Notifications
You must be signed in to change notification settings - Fork 58
/
run.ts
640 lines (509 loc) · 17.4 KB
/
run.ts
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
import * as vscode from "vscode";
import { Context } from "./context";
import type { CommandDescriptor, Commands } from "../commands";
import { parseRegExpWithReplacement } from "../utils/regexp";
/**
* Runs the given string of JavaScript code.
*/
export function run(string: string, context?: object): Thenable<unknown>;
/**
* Runs the given strings of JavaScript code.
*/
export function run(strings: readonly string[], context?: object): Thenable<unknown[]>;
export function run(strings: string | readonly string[], context: object = {}) {
const isSingleStringArgument = typeof strings === "string";
if (isSingleStringArgument) {
strings = [strings as string];
}
const functions: ((...args: any[]) => Thenable<unknown>)[] = [];
for (const code of strings) {
functions.push(compileFunction(code, Object.keys(context)));
}
const parameterValues = runParameterValues();
if (isSingleStringArgument) {
return Context.WithoutActiveEditor.wrap(
functions[0](...parameterValues, ...Object.values(context)),
);
}
const promises: Thenable<unknown>[] = [],
contextValues = Object.values(context);
for (const func of functions) {
promises.push(func(...parameterValues, ...contextValues));
}
return Context.WithoutActiveEditor.wrap(Promise.all(promises));
}
const cachedParameterNames = [] as string[],
cachedParameters = [] as unknown[];
let globalsObject: Record<string, any> = {};
function ensureCacheIsPopulated() {
if (cachedParameterNames.length > 0) {
return;
}
for (const name in globalsObject) {
cachedParameterNames.push(name);
cachedParameters.push(globalsObject[name]);
}
cachedParameterNames.push("vscode");
cachedParameters.push(vscode);
}
/**
* Sets the globals available within {@link run} expressions.
*/
export function setRunGlobals(globals: object) {
cachedParameterNames.length = 0;
cachedParameters.length = 0;
globalsObject = globals;
}
/**
* Returns the parameter names given to dynamically run functions.
*/
export function runParameterNames() {
ensureCacheIsPopulated();
return cachedParameterNames as readonly string[];
}
/**
* Returns the parameter values given to dynamically run functions.
*/
export function runParameterValues() {
ensureCacheIsPopulated();
return cachedParameters as readonly unknown[];
}
let canRunArbitraryCode = true;
/**
* Disables usage of the {@link compileFunction} and {@link run} functions,
* preventing the execution of arbitrary user inputs.
*
* For security purposes, execution cannot be re-enabled after calling this
* function.
*/
export function disableRunFunction() {
canRunArbitraryCode = false;
}
/**
* Returns whether {@link compileFunction} and {@link run} can be used.
*/
export function runIsEnabled() {
return canRunArbitraryCode;
}
interface CompiledFunction {
(...args: any[]): Thenable<unknown>;
}
const AsyncFunction: new (...names: string[]) => CompiledFunction =
async function () {}.constructor as any,
functionCache = new Map<string, CachedFunction>();
type CachedFunction = [funct: CompiledFunction, lastAccessTimestamp: number];
/**
* A few common inputs.
*/
const safeExpressions = [
/^(return +)?(\$\$?|[in]|\d+|count) *([=!]==?|[<>]=?|&{1,2}|\|{1,2}|[-+*/^]) *(\$\$?|[in]|\d+|count)$/,
/^(return +)?`\${await register\(["']\w+["'], *[i0-9]\)}` !== ["']false["']$/,
];
/**
* Compiles the given JavaScript code into a function.
*/
export function compileFunction(code: string, additionalParameterNames: readonly string[] = []) {
if (!canRunArbitraryCode && !safeExpressions.some((re) => re.test(code))) {
throw new Error("execution of arbitrary code is disabled");
}
const cacheId = additionalParameterNames.join(";") + code,
cached = functionCache.get(cacheId);
if (cached !== undefined) {
cached[1] = Date.now();
return cached[0];
}
let func: CompiledFunction;
try {
// Wrap code in block to allow shadowing of parameters.
func = new AsyncFunction(...runParameterNames(), ...additionalParameterNames, `{\n${code}\n}`);
} catch (e) {
throw new Error(`cannot parse function body: ${code}: ${e}`);
}
functionCache.set(cacheId, [func, Date.now()]);
return func;
}
/**
* Removes all functions that were not used in the last n milliseconds from
* the cache.
*/
export function clearCompiledFunctionsCache(olderThanMs: number): void;
/**
* Removes all functions that were not used in the last 5 minutes from the
* cache.
*/
export function clearCompiledFunctionsCache(): void;
export function clearCompiledFunctionsCache(olderThanMs = 1000 * 60 * 5) {
if (olderThanMs === 0) {
return functionCache.clear();
}
const olderThan = Date.now() - olderThanMs,
toDelete = [] as string[];
for (const [code, value] of functionCache) {
if (value[1] < olderThan) {
toDelete.push(code);
}
}
for (const code of toDelete) {
functionCache.delete(code);
}
}
interface ArgumentAssignment {
baseValue: Record<string, any>;
include?: readonly string[];
exclude?: ReadonlySet<string>;
}
type ArgumentAssignments = readonly ArgumentAssignment[];
function assignArgument(
assignment: ArgumentAssignment,
argument: Record<string, any>,
): Record<string, any> {
const ownedArgument = Object.assign({}, assignment.baseValue);
if ("include" in assignment) {
for (const propName of assignment.include!) {
const propValue = argument[propName];
if (propValue !== undefined) {
ownedArgument[propName] = propValue;
}
}
} else if ("exclude" in assignment) {
const excluded = assignment.exclude!;
for (const propName in argument) {
if (excluded.has(propName)) {
continue;
}
ownedArgument[propName] = argument[propName];
}
}
return ownedArgument;
}
function buildAssignment(argument: unknown): ArgumentAssignment {
if (typeof argument !== "object" || argument === null) {
return { baseValue: {} };
}
const { $include, $exclude, ...baseValue } = argument as Record<string, any>,
assignment: ArgumentAssignment = { baseValue };
if (Array.isArray($include)) {
assignment.include = $include;
} else if (Array.isArray($exclude)) {
assignment.exclude = new Set<string>($exclude);
}
return assignment;
}
function assignArguments(
assignment: ArgumentAssignments,
argument: Record<string, any>,
): readonly Record<string, any>[] {
return assignment.map((assignment) => assignArgument(assignment, argument));
}
function buildAssignments(args: unknown): ArgumentAssignments {
if (!Array.isArray(args)) {
if (typeof args === "object") {
return [buildAssignment(args)];
}
return [];
}
return args.map(buildAssignment);
}
/**
* Builds a function which can be called with an `argument` object, which will
* execute the list of given commands.
*/
export function buildCommands(
commands: readonly command.Any[],
extension: { readonly commands: Commands } = Context.WithoutActiveEditor.current.extension,
) {
const batches = [] as (
[CommandDescriptor, ArgumentAssignment][] | [string, ArgumentAssignments])[],
currentBatch = [] as [CommandDescriptor, ArgumentAssignment][];
// Build and validate commands.
for (let i = 0, len = commands.length; i < len; i++) {
let commandName: string,
commandArguments: unknown;
const command = commands[i];
if (typeof command === "string") {
commandName = command;
commandArguments = undefined;
} else if (Array.isArray(command)) {
commandName = command[0];
commandArguments = command.slice(1);
if (typeof commandName !== "string") {
throw new Error("the first element of a command tuple must be a command name");
}
} else if (typeof command === "object" && command !== null) {
commandName = (command as command.Command).command;
commandArguments = (command as command.Command).args;
if (typeof commandName !== "string") {
throw new Error("the \"command\" property of a command object must be a command name");
}
} else {
throw new Error(
"commands must be command names, {command: string, args: any} objects or arrays",
);
}
if (commandName.startsWith(".")) {
commandName = `dance${commandName}`;
}
if (commandName.startsWith("dance.")) {
const descriptor = extension.commands[commandName];
if (descriptor === undefined) {
throw new Error(`command ${JSON.stringify(commandName)} does not exist`);
}
const argument = Array.isArray(commandArguments) ? commandArguments[0] : commandArguments,
assignment = buildAssignment(argument);
currentBatch.push([descriptor, assignment]);
} else {
if (currentBatch.length > 0) {
batches.push(currentBatch.splice(0));
}
batches.push([commandName, buildAssignments(commandArguments)]);
}
}
if (currentBatch.length > 0) {
batches.push(currentBatch);
}
return async (argument: Record<string, any>, context = Context.WithoutActiveEditor.current) => {
// Execute all commands.
const results: unknown[] = [],
ownedArguments: any[] = [];
for (const batch of batches) {
if (typeof batch[0] === "string") {
const ownedArgument = assignArguments(batch[1] as ArgumentAssignments, argument);
results.push(await vscode.commands.executeCommand(batch[0], ...ownedArgument));
ownedArguments.push(ownedArgument);
} else {
const context = Context.WithoutActiveEditor.current;
let { currentCount, currentRegister } = context.extension;
for (const [descriptor, assignment] of batch as [CommandDescriptor, ArgumentAssignment][]) {
const ownedArgument = assignArgument(assignment, argument);
if (currentCount !== context.extension.currentCount) {
currentCount = ownedArgument["count"] = context.extension.currentCount;
context.extension.currentCount = 0;
}
if (currentRegister !== context.extension.currentRegister) {
currentRegister = ownedArgument["register"] = context.extension.currentRegister;
context.extension.currentRegister = undefined;
}
if (ownedArgument["try"]) {
delete ownedArgument["try"];
try {
results.push(await descriptor.handler(context, ownedArgument));
} catch {
results.push(undefined);
}
} else {
results.push(await descriptor.handler(context, ownedArgument));
}
ownedArguments.push(ownedArgument);
}
}
}
if (context.shouldRecord()) {
const recorder = context.extension.recorder;
let i = 0;
for (const batch of batches) {
if (typeof batch[0] === "string") {
const ownedArgument = ownedArguments[i++];
recorder.recordExternalCommand(batch[0], ownedArgument);
} else {
for (const [descriptor] of batch as [CommandDescriptor, never][]) {
const ownedArgument = ownedArguments[i++];
if (ownedArgument["record"] === false) {
continue;
}
recorder.recordCommand(descriptor, ownedArgument);
}
}
}
}
return results;
};
}
/**
* Runs the VS Code command with the given identifier and optional arguments.
*/
export async function command(commandName: string, ...args: readonly any[]): Promise<any> {
return (await commands([commandName, ...args]))[0];
}
/**
* Runs the VS Code commands with the given identifiers and optional arguments.
*/
export async function commands(...commands: readonly command.Any[]): Promise<any[]> {
return await buildCommands(commands)({});
}
export declare namespace command {
/**
* A tuple given to {@link command}.
*/
export type Tuple = readonly [commandId: string, ...args: any[]];
/**
* An object given to {@link command}.
*/
export interface Command {
readonly command: string;
readonly args?: any;
}
export type Any = string | Tuple | Command;
}
let canExecuteArbitraryCommands = true;
const commonSpawnOptions = { stdio: "pipe", windowsHide: true } as const;
/**
* Executes a shell command.
*/
export function execute(
command: string,
input?: string,
options: { cwd?: string; env?: Record<string, string> } = {},
cancellationToken = Context.WithoutActiveEditor.current.cancellationToken,
) {
const {
cwd = function () {
const currentFileUri = Context.currentOrUndefined?.document.uri;
if (currentFileUri?.scheme === "file" || currentFileUri?.scheme === "vscode-userdata") {
return vscode.Uri.joinPath(currentFileUri, "..").fsPath;
}
const workspaceFolder = vscode.workspace.workspaceFolders?.[0]?.uri;
if (workspaceFolder?.scheme === "file") {
return workspaceFolder.fsPath;
}
return undefined;
}(),
env: givenEnv = function () {
if (vscode.env.remoteName !== undefined) {
return {};
}
const env = { ...process.env };
if (cwd !== undefined) {
env["PWD"] = cwd;
}
return env;
}(),
} = options;
if (process.platform as string === "web") {
return Context.WithoutActiveEditor.wrap(
Promise.reject(new Error("execution of arbitrary commands is not supported on the web")),
);
}
if (!canExecuteArbitraryCommands) {
return Context.WithoutActiveEditor.wrap(
Promise.reject(new Error("execution of arbitrary commands is disabled")),
);
}
const promise = import("child_process").then((cp) =>
new Promise<string>((resolve, reject) => {
const automationProfile = getAutomationProfile(),
shell = typeof automationProfile?.path === "string" ? automationProfile.path : "",
args = Array.isArray(automationProfile?.args) ? automationProfile!.args : [],
env = {
...(typeof automationProfile?.env === "object" ? automationProfile.env : {}),
...givenEnv,
},
child = shell.length === 0
? cp.spawn(command, { ...commonSpawnOptions, shell: true, env, cwd: env["PWD"] })
: cp.spawn(shell,
[...args, command],
{ ...commonSpawnOptions, shell: false, env, cwd: env["PWD"] });
let stdout = "",
stderr = "";
const disposable = cancellationToken.onCancellationRequested(() => {
child.kill("SIGINT");
});
child.stdout.on("data", (chunk: Buffer) => (stdout += chunk.toString("utf-8")));
child.stderr.on("data", (chunk: Buffer) => (stderr += chunk.toString("utf-8")));
child.stdin.end(input, "utf-8");
child.once("error", (err) => {
disposable.dispose();
reject(err);
});
child.once("exit", (code) => {
disposable.dispose();
code === 0
? resolve(stdout.trimRight())
: reject(new Error(`Command exited with error ${code}: ${
stderr.length > 0 ? stderr.trimRight() : "<No error output>"
}`));
});
}),
);
return Context.WithoutActiveEditor.wrap(promise);
}
/**
* Disables usage of the {@link execute} function, preventing the execution of
* arbitrary user commands.
*
* For security purposes, execution cannot be re-enabled after calling this
* function.
*/
export function disableExecuteFunction() {
canExecuteArbitraryCommands = false;
}
interface AutomationProfile {
readonly path?: string;
readonly args?: readonly string[];
readonly env?: Record<string, string>;
}
function getAutomationProfile() {
let os: string;
switch (process.platform) {
case "cygwin":
case "linux":
os = "linux";
break;
case "darwin":
os = "osx";
break;
case "win32":
os = "windows";
break;
default:
return undefined;
}
return vscode.workspace.getConfiguration("terminal.integrated.automationProfile")
.get<AutomationProfile | null>(os);
}
/**
* Runs the given string of JavaScript code on the given input, except in two
* cases:
* 1. If the string is a complete RegExp expression, instead its match will be
* returned.
* 2. If the string starts with "#", instead a command will be run.
*/
export function switchRun(string: string, context: { $: string } & Record<string, any>) {
if (string.length === 0) {
// An empty expression is just `undefined`.
return Context.WithoutActiveEditor.wrap(Promise.resolve());
}
if (string[0] === "/") {
// RegExp replace or match.
const [regexp, replacement] = parseRegExpWithReplacement(string);
if (replacement === undefined) {
return Context.WithoutActiveEditor.wrap(Promise.resolve(regexp.exec(context.$)));
}
return Context.WithoutActiveEditor.wrap(Promise.resolve(context.$.replace(regexp, replacement)));
}
if (string[0] === "#") {
// Shell command.
return execute(string.slice(1), context.$);
}
// JavaScript expression.
return run("return " + string, context);
}
/**
* Validates the given input string for use in {@link switchRun}. If it is
* invalid, an exception will be thrown.
*/
export function validateForSwitchRun(string: string) {
if (string.trim().length === 0) {
throw new Error("the given string cannot be empty");
}
if (string[0] === "/") {
parseRegExpWithReplacement(string);
return;
}
if (string[0] === "#") {
if (string.slice(1).trim().length === 0) {
throw new Error("the given shell command cannot be empty");
}
return;
}
compileFunction("return " + string);
}