-
Notifications
You must be signed in to change notification settings - Fork 700
Expand file tree
/
Copy pathextension.ts
More file actions
564 lines (495 loc) · 22.1 KB
/
Copy pathextension.ts
File metadata and controls
564 lines (495 loc) · 22.1 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
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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as os from 'node:os';
import * as path from 'node:path';
import * as vscode from 'vscode';
import type {
TunnelStatus,
IHandshake,
ILaunchOptionsAllowlist,
ILaunchOptionsValidationResult
} from '@rushstack/playwright-browser-tunnel';
import { PlaywrightTunnel } from '@rushstack/playwright-browser-tunnel/lib/PlaywrightBrowserTunnel';
import {
EXTENSION_INSTALLED_FILENAME,
getNormalizedErrorString
} from '@rushstack/playwright-browser-tunnel/lib/utilities';
import { LaunchOptionsValidator } from '@rushstack/playwright-browser-tunnel/lib/LaunchOptionsValidator';
import { Terminal, type ITerminal, type ITerminalProvider } from '@rushstack/terminal';
import { runWorkspaceCommandAsync } from '@rushstack/vscode-shared/lib/runWorkspaceCommandAsync';
import { VScodeOutputChannelTerminalProvider } from '@rushstack/vscode-shared/lib/VScodeOutputChannelTerminalProvider';
import packageJson from '../package.json';
const EXTENSION_DISPLAY_NAME: string = 'Playwright Local Browser Server';
const COMMAND_SHOW_LOG: string = 'playwright-local-browser-server.showLog';
const COMMAND_SHOW_SETTINGS: string = 'playwright-local-browser-server.showSettings';
const COMMAND_START_TUNNEL: string = 'playwright-local-browser-server.start';
const COMMAND_STOP_TUNNEL: string = 'playwright-local-browser-server.stop';
const COMMAND_RESTART_TUNNEL: string = 'playwright-local-browser-server.restart';
const COMMAND_SHOW_MENU: string = 'playwright-local-browser-server.showMenu';
const COMMAND_MANAGE_ALLOWLIST: string = 'playwright-local-browser-server.manageAllowlist';
const VSCODE_COMMAND_WORKSPACE_OPEN_SETTINGS: string = 'workbench.action.openSettings';
const EXTENSION_ID: string = `${packageJson.publisher}.${packageJson.name}`;
const VSCODE_SETTINGS_EXTENSION_FILTER: string = `@ext:${EXTENSION_ID}`;
async function writeExtensionInstalledFileAsync(terminal: ITerminal): Promise<void> {
try {
// If on a remote environment, write a file to os.tempdir() using workspace fs
let fileUri: vscode.Uri;
let tempDir: string;
if (vscode.env.remoteName) {
const markerPrefix: string = '<<<TEMPDIR_START>>>';
const markerSuffix: string = '<<<TEMPDIR_END>>>';
const output: string = await runWorkspaceCommandAsync({
terminalOptions: { name: 'playwright-local-browser-server', hideFromUser: true },
commandLine: `node -p "'${markerPrefix}' + require('node:os').tmpdir() + '${markerSuffix}'"`,
terminal
});
const startIndex: number = output.indexOf(markerPrefix);
const endIndex: number = output.indexOf(markerSuffix);
if (startIndex !== -1 && endIndex !== -1 && endIndex > startIndex) {
tempDir = output.substring(startIndex + markerPrefix.length, endIndex).trim();
} else {
throw new Error('Failed to parse temp directory from command output');
}
// For remote environments, use the vscode-remote scheme
// The workspace folder should have the correct scheme already
const workspaceFolder: vscode.WorkspaceFolder | undefined = vscode.workspace.workspaceFolders?.[0];
if (workspaceFolder) {
// Use the same scheme as the workspace folder (e.g., 'vscode-remote')
fileUri = vscode.Uri.from({
scheme: workspaceFolder.uri.scheme,
authority: workspaceFolder.uri.authority,
path: `${tempDir}/${EXTENSION_INSTALLED_FILENAME}`
});
} else {
// Fallback if no workspace folder
fileUri = vscode.Uri.parse(
`vscode-remote://${vscode.env.remoteName}${path.posix.join(tempDir, EXTENSION_INSTALLED_FILENAME)}`
);
}
terminal.writeLine(`Using temp directory: ${tempDir}`);
terminal.writeLine(`Writing to URI: ${fileUri.toString()}`);
// TODO: Can we have this be a JSON file which the test fixture writes OS-designated port number to
// so that the browser-tunnel can pick it up here? For now this file just serves as a marker
// that the extension is installed on codespaces/vs code remotes so that the test fixture verifies.
await vscode.workspace.fs.writeFile(
fileUri,
Buffer.from('This is a test file created by the Playwright Local Browser Server extension.\n', 'utf8')
);
terminal.writeLine(`Test file written to temp directory.`);
}
} catch (error) {
terminal.writeError(`Error writing extension installed file: ${error}`);
return;
}
}
export async function activate(context: vscode.ExtensionContext): Promise<void> {
// Setup Logging Terminal
const outputChannel: vscode.OutputChannel = vscode.window.createOutputChannel(EXTENSION_DISPLAY_NAME);
outputChannel.appendLine(`${EXTENSION_DISPLAY_NAME} Extension output channel initialized.`);
// Create terminal adapter for PlaywrightTunnel
const terminalProvider: ITerminalProvider = new VScodeOutputChannelTerminalProvider(outputChannel, {
debugEnabled: true,
verboseEnabled: true
});
const terminal: ITerminal = new Terminal(terminalProvider);
await writeExtensionInstalledFileAsync(terminal);
// Create status bar item
const statusBarItem: vscode.StatusBarItem = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Right,
100
);
statusBarItem.command = COMMAND_SHOW_MENU;
let currentStatus: TunnelStatus = 'stopped';
function updateStatusBar(status: TunnelStatus): void {
currentStatus = status;
switch (status) {
case 'stopped':
statusBarItem.text = '$(debug-stop) Playwright Tunnel';
statusBarItem.tooltip = 'Playwright Tunnel: Stopped - Click for options';
statusBarItem.backgroundColor = undefined;
break;
case 'waiting-for-connection':
statusBarItem.text = '$(radio-tower) Playwright Tunnel';
statusBarItem.tooltip = 'Playwright Tunnel: Waiting for connection...';
statusBarItem.backgroundColor = new vscode.ThemeColor('statusBarItem.warningBackground');
break;
case 'setting-up-browser-server':
statusBarItem.text = '$(loading~spin) Playwright Tunnel';
statusBarItem.tooltip = 'Playwright Tunnel: Setting up browser server...';
statusBarItem.backgroundColor = new vscode.ThemeColor('statusBarItem.warningBackground');
break;
case 'browser-server-running':
statusBarItem.text = '$(check) Playwright Tunnel';
statusBarItem.tooltip = 'Playwright Tunnel: Running';
statusBarItem.backgroundColor = new vscode.ThemeColor('statusBarItem.prominentBackground');
break;
case 'error':
statusBarItem.text = '$(error) Playwright Tunnel';
statusBarItem.tooltip = 'Playwright Tunnel: Error - Click for options';
statusBarItem.backgroundColor = new vscode.ThemeColor('statusBarItem.errorBackground');
break;
}
}
// Initialize status bar
updateStatusBar('stopped');
statusBarItem.show();
// Tunnel instance
let tunnel: PlaywrightTunnel | undefined;
function getTmpPath(): string {
return path.join(os.tmpdir(), 'playwright-browser-tunnel');
}
function handleShowLog(): void {
outputChannel.show();
}
async function handleShowSettings(): Promise<void> {
await vscode.commands.executeCommand(
VSCODE_COMMAND_WORKSPACE_OPEN_SETTINGS,
VSCODE_SETTINGS_EXTENSION_FILTER
);
}
async function handleManageAllowlist(): Promise<void> {
try {
const allowlist: ILaunchOptionsAllowlist = await LaunchOptionsValidator.readAllowlistAsync();
const allowlistPath: string = LaunchOptionsValidator.getAllowlistFilePath();
interface IAllowlistQuickPickItem extends vscode.QuickPickItem {
action: 'add' | 'remove' | 'clear' | 'view';
}
const items: IAllowlistQuickPickItem[] = [
{
label: '$(add) Add Option to Allowlist',
description: 'Allow a specific launch option',
action: 'add'
},
{
label: '$(remove) Remove Option from Allowlist',
description: 'Revoke permission for a launch option',
action: 'remove'
},
{
label: '$(clear-all) Clear All Allowlist',
description: 'Remove all allowed options',
action: 'clear'
},
{
label: '$(eye) View Current Allowlist',
description: `Currently ${allowlist.allowedOptions.length} option(s) allowed`,
action: 'view'
}
];
const selected: IAllowlistQuickPickItem | undefined = await vscode.window.showQuickPick(items, {
placeHolder: 'Manage Launch Options Allowlist'
});
if (!selected) {
return;
}
switch (selected.action) {
case 'add': {
const optionToAdd: string | undefined = await vscode.window.showInputBox({
placeHolder: 'e.g., args, executablePath, channel',
prompt:
'Enter the name of a Playwright launch option to allow. ' +
'All options are denied by default for security.',
ignoreFocusOut: true
});
if (optionToAdd && optionToAdd.trim()) {
const trimmedOption: string = optionToAdd.trim();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await LaunchOptionsValidator.addToAllowlistAsync(trimmedOption as any);
outputChannel.appendLine(`Added '${trimmedOption}' to allowlist.`);
void vscode.window.showInformationMessage(
`'${trimmedOption}' has been added to the allowlist at:\n${allowlistPath}`
);
}
break;
}
case 'remove': {
if (allowlist.allowedOptions.length === 0) {
void vscode.window.showInformationMessage('Allowlist is empty. Nothing to remove.');
return;
}
const optionToRemove: string | undefined = await vscode.window.showQuickPick(
allowlist.allowedOptions,
{
placeHolder: 'Select a launch option to remove from allowlist',
ignoreFocusOut: true
}
);
if (optionToRemove) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await LaunchOptionsValidator.removeFromAllowlistAsync(optionToRemove as any);
outputChannel.appendLine(`Removed '${optionToRemove}' from allowlist.`);
void vscode.window.showInformationMessage(
`'${optionToRemove}' has been removed from the allowlist.`
);
}
break;
}
case 'clear': {
const confirmation: string | undefined = await vscode.window.showInformationMessage(
'Are you sure you want to clear all allowed launch options?',
{ modal: true },
'Yes, Clear All',
'Cancel'
);
if (confirmation === 'Yes, Clear All') {
await LaunchOptionsValidator.clearAllowlistAsync();
outputChannel.appendLine('Cleared all options from allowlist.');
void vscode.window.showInformationMessage('Allowlist has been cleared.');
}
break;
}
case 'view': {
// just open the allowlist file in an editor (separate editor preferred because this will always be a local file)
await vscode.window.showTextDocument(vscode.Uri.file(allowlistPath));
break;
}
}
} catch (error) {
const errorMessage: string = getNormalizedErrorString(error);
outputChannel.appendLine(`Failed to manage allowlist: ${errorMessage}`);
void vscode.window.showErrorMessage(`Failed to manage allowlist: ${errorMessage}`);
}
}
async function handleStartTunnelAsync(isAutoStart: boolean = false): Promise<void> {
// Store current tunnel reference to avoid race conditions
const existingTunnel: PlaywrightTunnel | undefined = tunnel;
if (existingTunnel && currentStatus !== 'stopped' && currentStatus !== 'error') {
outputChannel.appendLine('Tunnel is already running or starting.');
void vscode.window.showInformationMessage('Playwright tunnel is already running.');
return;
}
const config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration(
'playwright-local-browser-server'
);
const shouldAutoStart: boolean = config.get<boolean>('autoStart', false);
const tunnelPort: number = config.get<number>('tunnelPort', 56767);
// If this is a manual start and autoStart is not enabled, prompt the user
if (!isAutoStart && !shouldAutoStart) {
const response: string | undefined = await vscode.window.showInformationMessage(
'Would you like to automatically start the Playwright tunnel when this extension activates?',
'Always Auto-Start',
'Just This Once',
'Cancel'
);
if (response === 'Cancel') {
outputChannel.appendLine('Tunnel start cancelled by user.');
return;
}
if (response === 'Always Auto-Start') {
await config.update('autoStart', true, vscode.ConfigurationTarget.Global);
outputChannel.appendLine('Auto-start preference saved: enabled.');
void vscode.window.showInformationMessage(
'Playwright tunnel will now start automatically. You can change this in settings.'
);
}
}
// If this is an auto-start but the setting is disabled, don't start
if (isAutoStart && !shouldAutoStart) {
outputChannel.appendLine('Auto-start is disabled. Tunnel not started.');
return;
}
try {
const tmpPath: string = getTmpPath();
outputChannel.appendLine(`Starting Playwright tunnel on port ${tunnelPort}`);
outputChannel.appendLine(`Using temp path: ${tmpPath}`);
const newTunnel: PlaywrightTunnel = new PlaywrightTunnel({
mode: 'poll-connection',
wsEndpoint: `ws://127.0.0.1:${tunnelPort}`,
terminal,
playwrightInstallPath: tmpPath,
onStatusChange: (status: TunnelStatus) => {
outputChannel.appendLine(`Tunnel status changed: ${status}`);
updateStatusBar(status);
},
onBeforeLaunch: async (handshake: IHandshake) => {
// Validate launch options against the allowlist
const validationResult: ILaunchOptionsValidationResult =
await LaunchOptionsValidator.validateLaunchOptionsAsync(handshake.launchOptions);
// Filter out 'headless' from denied options since we handle it separately
const deniedKeys: string[] = validationResult.deniedOptions.filter((key) => key !== 'headless');
// If there are denied options, always prompt to give user the chance to add to allowlist
if (deniedKeys.length > 0) {
let message: string = `Playwright is requesting to launch ${handshake.browserName}.\n\n`;
// Create a copy without the headless property for display
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { headless, ...displayOptions } = handshake.launchOptions;
if (Object.keys(displayOptions).length > 0) {
message += `Launch options: ${JSON.stringify(displayOptions, null, 2)}\n\n`;
}
message += `The following options are not in your allowlist and will be filtered:\n`;
message += deniedKeys.map((key) => ` - ${key}`).join('\n');
message += '\n\nWould you like to add these options to your allowlist?';
const response: string | undefined = await vscode.window.showWarningMessage(
message,
{ modal: true },
'Add to Allowlist',
'Continue Without',
'Configure Allowlist'
);
switch (response) {
case 'Add to Allowlist': {
// Add all denied options to the allowlist
for (const option of deniedKeys) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
await LaunchOptionsValidator.addToAllowlistAsync(option as any);
outputChannel.appendLine(`Added '${option}' to allowlist.`);
}
void vscode.window.showInformationMessage(
`Added ${deniedKeys.length} option(s) to allowlist: ${deniedKeys.join(', ')}`
);
outputChannel.appendLine('User added denied options to allowlist. Browser launch approved.');
return true;
}
case 'Continue Without': {
outputChannel.appendLine(
`User approved browser launch. Denied options will be filtered: ${deniedKeys.join(', ')}`
);
return true;
}
case 'Configure Allowlist': {
outputChannel.appendLine('User chose to configure allowlist and cancel browser launch.');
await handleManageAllowlist();
return false;
}
// "Cancel" or closed dialog
default: {
outputChannel.appendLine('User canceled browser launch.');
return false;
}
}
}
return true;
}
});
// Start the tunnel (don't await - it runs continuously)
void newTunnel.startAsync().catch((error: Error) => {
outputChannel.appendLine(`Tunnel error: ${getNormalizedErrorString(error)}`);
updateStatusBar('error');
void vscode.window.showErrorMessage(`Playwright tunnel error: ${getNormalizedErrorString(error)}`);
});
// Assign to the module-level variable after starting
// Disabling this since we are capturing the initial state and not reading
// from `tunnel` after any await.
// eslint-disable-next-line require-atomic-updates
tunnel = newTunnel;
outputChannel.appendLine('Tunnel start initiated.');
} catch (error) {
const errorMessage: string = getNormalizedErrorString(error);
outputChannel.appendLine(`Failed to start tunnel: ${errorMessage}`);
updateStatusBar('error');
void vscode.window.showErrorMessage(`Failed to start Playwright tunnel: ${errorMessage}`);
}
}
async function handleStopTunnelAsync(): Promise<void> {
const currentTunnel: PlaywrightTunnel | undefined = tunnel;
if (!currentTunnel) {
outputChannel.appendLine('No tunnel instance to stop.');
void vscode.window.showInformationMessage('Playwright tunnel is not running.');
return;
}
// Clear the reference before awaiting to avoid race condition
tunnel = undefined;
try {
outputChannel.appendLine('Stopping Playwright tunnel...');
await currentTunnel.stopAsync();
updateStatusBar('stopped');
outputChannel.appendLine('Tunnel stopped.');
void vscode.window.showInformationMessage('Playwright tunnel stopped.');
} catch (error) {
const errorMessage: string = getNormalizedErrorString(error);
outputChannel.appendLine(`Failed to stop tunnel: ${errorMessage}`);
void vscode.window.showErrorMessage(`Failed to stop Playwright tunnel: ${errorMessage}`);
}
}
async function handleRestartTunnelAsync(): Promise<void> {
outputChannel.appendLine('Restarting Playwright tunnel...');
await handleStopTunnelAsync();
await handleStartTunnelAsync();
}
async function handleShowMenu(): Promise<void> {
interface IQuickPickItem extends vscode.QuickPickItem {
action: 'start' | 'stop' | 'restart' | 'showLog' | 'manageAllowlist';
}
const items: IQuickPickItem[] = [
{
label: '$(play) Start Tunnel',
description: 'Start the Playwright browser tunnel',
action: 'start'
},
{
label: '$(debug-stop) Stop Tunnel',
description: 'Stop the Playwright browser tunnel',
action: 'stop'
},
{
label: '$(debug-restart) Restart Tunnel',
description: 'Stop and start the Playwright browser tunnel',
action: 'restart'
},
{
label: '$(shield) Manage Allowlist',
description: 'Configure allowed launch options',
action: 'manageAllowlist'
},
{
label: '$(output) Show Logs',
description: 'Show the Playwright tunnel output log',
action: 'showLog'
}
];
const selected: IQuickPickItem | undefined = await vscode.window.showQuickPick(items, {
placeHolder: `Playwright Tunnel (${currentStatus})`
});
if (selected) {
switch (selected.action) {
case 'start':
await handleStartTunnelAsync();
break;
case 'stop':
await handleStopTunnelAsync();
break;
case 'restart':
await handleRestartTunnelAsync();
break;
case 'manageAllowlist':
await handleManageAllowlist();
break;
case 'showLog':
handleShowLog();
break;
}
}
}
context.subscriptions.push(
outputChannel,
statusBarItem,
vscode.commands.registerCommand(COMMAND_SHOW_LOG, handleShowLog),
vscode.commands.registerCommand(COMMAND_SHOW_SETTINGS, handleShowSettings),
vscode.commands.registerCommand(COMMAND_START_TUNNEL, handleStartTunnelAsync),
vscode.commands.registerCommand(COMMAND_STOP_TUNNEL, handleStopTunnelAsync),
vscode.commands.registerCommand(COMMAND_RESTART_TUNNEL, handleRestartTunnelAsync),
vscode.commands.registerCommand(COMMAND_SHOW_MENU, handleShowMenu),
vscode.commands.registerCommand(COMMAND_MANAGE_ALLOWLIST, handleManageAllowlist),
// Cleanup tunnel on deactivate
{
dispose: () => {
const currentTunnel: PlaywrightTunnel | undefined = tunnel;
if (currentTunnel) {
outputChannel.appendLine('Extension deactivating, stopping tunnel...');
void currentTunnel.stopAsync().then(() => {
tunnel = undefined;
});
}
}
}
);
// Auto-start the tunnel on activation if configured
const config: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration(
'playwright-local-browser-server'
);
const autoStart: boolean = config.get<boolean>('autoStart', false);
if (autoStart) {
void handleStartTunnelAsync(true);
}
}
export function deactivate(): void {}