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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/**
 * @license
 * Copyright 2023 Google Inc.
 * SPDX-License-Identifier: Apache-2.0
 */

import childProcess from 'node:child_process';
import {EventEmitter} from 'node:events';
import {accessSync} from 'node:fs';
import os from 'node:os';
import readline from 'node:readline';
import type Stream from 'node:stream';

import {
  type Browser,
  type BrowserPlatform,
  type ChromeReleaseChannel,
  executablePathByBrowser,
  resolveSystemExecutablePaths,
} from './browser-data/browser-data.js';
import {Cache} from './Cache.js';
import {debug} from './debug.js';
import {detectBrowserPlatform} from './detectPlatform.js';

const debugLaunch = debug('puppeteer:browsers:launcher');

/**
 * @public
 */
export interface ComputeExecutablePathOptions {
  /**
   * Root path to the storage directory.
   *
   * Can be set to `null` if the executable path should be relative
   * to the extracted download location. E.g. `./chrome-linux64/chrome`.
   */
  cacheDir: string | null;
  /**
   * Determines which platform the browser will be suited for.
   *
   * @defaultValue **Auto-detected.**
   */
  platform?: BrowserPlatform;
  /**
   * Determines which browser to launch.
   */
  browser: Browser;
  /**
   * Determines which buildId to download. BuildId should uniquely identify
   * binaries and they are used for caching.
   */
  buildId: string;
}

/**
 * @public
 */
export function computeExecutablePath(
  options: ComputeExecutablePathOptions,
): string {
  if (options.cacheDir === null) {
    options.platform ??= detectBrowserPlatform();
    if (options.platform === undefined) {
      throw new Error(
        `No platform specified. Couldn't auto-detect browser platform.`,
      );
    }
    return executablePathByBrowser[options.browser](
      options.platform,
      options.buildId,
    );
  }

  return new Cache(options.cacheDir).computeExecutablePath(options);
}

/**
 * @public
 */
export interface SystemOptions {
  /**
   * Determines which platform the browser will be suited for.
   *
   * @defaultValue **Auto-detected.**
   */
  platform?: BrowserPlatform;
  /**
   * Determines which browser to launch.
   */
  browser: Browser;
  /**
   * Release channel to look for on the system.
   */
  channel: ChromeReleaseChannel;
}

/**
 * Returns a path to a system-wide Chrome installation given a release channel
 * name by checking known installation locations (using
 * {@link https://pptr.dev/browsers-api/browsers.computesystemexecutablepath}).
 * If Chrome instance is not found at the expected path, an error is thrown.
 *
 * @public
 */
export function computeSystemExecutablePath(options: SystemOptions): string {
  options.platform ??= detectBrowserPlatform();
  if (!options.platform) {
    throw new Error(
      `Cannot download a binary for the provided platform: ${os.platform()} (${os.arch()})`,
    );
  }
  const paths = resolveSystemExecutablePaths(
    options.browser,
    options.platform,
    options.channel,
  );
  for (const path of paths) {
    try {
      accessSync(path);
      return path;
    } catch {}
  }
  throw new Error(
    `Could not find Google Chrome executable for channel '${options.channel}' at:${paths.map(
      path => {
        return `\n - ${path}`;
      },
    )}.`,
  );
}

/**
 * @public
 */
export interface LaunchOptions {
  /**
   * Absolute path to the browser's executable.
   */
  executablePath: string;
  /**
   * Configures stdio streams to open two additional streams for automation over
   * those streams instead of WebSocket.
   *
   * @defaultValue `false`.
   */
  pipe?: boolean;
  /**
   * If true, forwards the browser's process stdout and stderr to the Node's
   * process stdout and stderr.
   *
   * @defaultValue `false`.
   */
  dumpio?: boolean;
  /**
   * Additional arguments to pass to the executable when launching.
   */
  args?: string[];
  /**
   * Environment variables to set for the browser process.
   */
  env?: Record<string, string | undefined>;
  /**
   * Handles SIGINT in the Node process and tries to kill the browser process.
   *
   * @defaultValue `true`.
   */
  handleSIGINT?: boolean;
  /**
   * Handles SIGTERM in the Node process and tries to gracefully close the browser
   * process.
   *
   * @defaultValue `true`.
   */
  handleSIGTERM?: boolean;
  /**
   * Handles SIGHUP in the Node process and tries to gracefully close the browser process.
   *
   * @defaultValue `true`.
   */
  handleSIGHUP?: boolean;
  /**
   * Whether to spawn process in the {@link https://nodejs.org/api/child_process.html#optionsdetached | detached}
   * mode.
   *
   * @defaultValue `true` except on Windows.
   */
  detached?: boolean;
  /**
   * A callback to run after the browser process exits or before the process
   * will be closed via the {@link Process.close} call (including when handling
   * signals). The callback is only run once.
   */
  onExit?: () => Promise<void>;
  /**
   * If provided, the process will be killed when the signal is aborted.
   */
  signal?: AbortSignal;
}

/**
 * Launches a browser process according to {@link LaunchOptions}.
 *
 * @public
 */
export function launch(opts: LaunchOptions): Process {
  return new Process(opts);
}

/**
 * @public
 */
export const CDP_WEBSOCKET_ENDPOINT_REGEX =
  /^DevTools listening on (ws:\/\/.*)$/;

/**
 * @public
 */
export const WEBDRIVER_BIDI_WEBSOCKET_ENDPOINT_REGEX =
  /^WebDriver BiDi listening on (ws:\/\/.*)$/;

type EventHandler = (...args: any[]) => void;
const processListeners = new Map<string, EventHandler[]>();
const dispatchers = {
  exit: (...args: any[]) => {
    processListeners.get('exit')?.forEach(handler => {
      return handler(...args);
    });
  },
  SIGINT: (...args: any[]) => {
    processListeners.get('SIGINT')?.forEach(handler => {
      return handler(...args);
    });
  },
  SIGHUP: (...args: any[]) => {
    processListeners.get('SIGHUP')?.forEach(handler => {
      return handler(...args);
    });
  },
  SIGTERM: (...args: any[]) => {
    processListeners.get('SIGTERM')?.forEach(handler => {
      return handler(...args);
    });
  },
};

function subscribeToProcessEvent(
  event: 'exit' | 'SIGINT' | 'SIGHUP' | 'SIGTERM',
  handler: EventHandler,
): void {
  const listeners = processListeners.get(event) || [];
  if (listeners.length === 0) {
    process.on(event, dispatchers[event]);
  }
  listeners.push(handler);
  processListeners.set(event, listeners);
}

function unsubscribeFromProcessEvent(
  event: 'exit' | 'SIGINT' | 'SIGHUP' | 'SIGTERM',
  handler: EventHandler,
): void {
  const listeners = processListeners.get(event) || [];
  const existingListenerIdx = listeners.indexOf(handler);
  if (existingListenerIdx === -1) {
    return;
  }
  listeners.splice(existingListenerIdx, 1);
  processListeners.set(event, listeners);
  if (listeners.length === 0) {
    process.off(event, dispatchers[event]);
  }
}

/**
 * @public
 */
export class Process {
  #executablePath;
  #args: string[];
  #browserProcess: childProcess.ChildProcess;
  #exited = false;
  // The browser process can be closed externally or from the driver process. We
  // need to invoke the hooks only once though but we don't know how many times
  // we will be invoked.
  #hooksRan = false;
  #onExitHook = async () => {};
  #browserProcessExiting: Promise<void>;
  #logs: string[] = [];
  #maxLogLinesSize = 1000;
  #lineEmitter = new EventEmitter();
  #onAbort = (): void => {
    this.kill();
  };
  #signal?: AbortSignal;

  constructor(opts: LaunchOptions) {
    this.#executablePath = opts.executablePath;
    this.#args = opts.args ?? [];

    this.#signal = opts.signal;
    if (this.#signal?.aborted) {
      throw new Error(
        this.#signal.reason ? this.#signal.reason : 'Launch aborted',
      );
    }
    this.#signal?.addEventListener('abort', this.#onAbort, {once: true});

    opts.pipe ??= false;
    opts.dumpio ??= false;
    opts.handleSIGINT ??= true;
    opts.handleSIGTERM ??= true;
    opts.handleSIGHUP ??= true;
    // On non-windows platforms, `detached: true` makes child process a
    // leader of a new process group, making it possible to kill child
    // process tree with `.kill(-pid)` command. @see
    // https://nodejs.org/api/child_process.html#child_process_options_detached
    opts.detached ??= process.platform !== 'win32';
    const stdio = this.#configureStdio({
      pipe: opts.pipe,
    });

    const env = opts.env || {};

    debugLaunch(`Launching ${this.#executablePath} ${this.#args.join(' ')}`, {
      detached: opts.detached,
      env: Object.keys(env).reduce<Record<string, string | undefined>>(
        (res, key) => {
          if (key.toLowerCase().startsWith('puppeteer_')) {
            res[key] = env[key];
          }
          return res;
        },
        {},
      ),
      stdio,
    });

    this.#browserProcess = childProcess.spawn(
      this.#executablePath,
      this.#args,
      {
        detached: opts.detached,
        env,
        stdio,
      },
    );
    this.#recordStream(this.#browserProcess.stderr!);
    this.#recordStream(this.#browserProcess.stdout!);

    debugLaunch(`Launched ${this.#browserProcess.pid}`);
    if (opts.dumpio) {
      this.#browserProcess.stderr?.pipe(process.stderr);
      this.#browserProcess.stdout?.pipe(process.stdout);
    }
    subscribeToProcessEvent('exit', this.#onDriverProcessExit);
    if (opts.handleSIGINT) {
      subscribeToProcessEvent('SIGINT', this.#onDriverProcessSignal);
    }
    if (opts.handleSIGTERM) {
      subscribeToProcessEvent('SIGTERM', this.#onDriverProcessSignal);
    }
    if (opts.handleSIGHUP) {
      subscribeToProcessEvent('SIGHUP', this.#onDriverProcessSignal);
    }
    if (opts.onExit) {
      this.#onExitHook = opts.onExit;
    }
    this.#browserProcessExiting = new Promise((resolve, reject) => {
      this.#browserProcess.once('exit', async () => {
        debugLaunch(`Browser process ${this.#browserProcess.pid} onExit`);
        this.#clearListeners();
        this.#exited = true;
        try {
          await this.#runHooks();
        } catch (err) {
          reject(err);
          return;
        }
        resolve();
      });
    });
  }

  async #runHooks() {
    if (this.#hooksRan) {
      return;
    }
    this.#hooksRan = true;
    await this.#onExitHook();
  }

  get nodeProcess(): childProcess.ChildProcess {
    return this.#browserProcess;
  }

  #configureStdio(opts: {pipe: boolean}): Array<'ignore' | 'pipe'> {
    if (opts.pipe) {
      return ['pipe', 'pipe', 'pipe', 'pipe', 'pipe'];
    } else {
      return ['pipe', 'pipe', 'pipe'];
    }
  }

  #clearListeners(): void {
    unsubscribeFromProcessEvent('exit', this.#onDriverProcessExit);
    unsubscribeFromProcessEvent('SIGINT', this.#onDriverProcessSignal);
    unsubscribeFromProcessEvent('SIGTERM', this.#onDriverProcessSignal);
    unsubscribeFromProcessEvent('SIGHUP', this.#onDriverProcessSignal);
    this.#signal?.removeEventListener('abort', this.#onAbort);
  }

  #onDriverProcessExit = (_code: number) => {
    this.kill();
  };

  #onDriverProcessSignal = (signal: string): void => {
    switch (signal) {
      case 'SIGINT':
        this.kill();
        process.exit(130);
      case 'SIGTERM':
      case 'SIGHUP':
        void this.close();
        break;
    }
  };

  async close(): Promise<void> {
    await this.#runHooks();
    if (!this.#exited) {
      this.kill();
    }
    return await this.#browserProcessExiting;
  }

  hasClosed(): Promise<void> {
    return this.#browserProcessExiting;
  }

  kill(): void {
    debugLaunch(`Trying to kill ${this.#browserProcess.pid}`);
    // If the process failed to launch (for example if the browser executable path
    // is invalid), then the process does not get a pid assigned. A call to
    // `proc.kill` would error, as the `pid` to-be-killed can not be found.
    if (
      this.#browserProcess &&
      this.#browserProcess.pid &&
      pidExists(this.#browserProcess.pid)
    ) {
      try {
        debugLaunch(`Browser process ${this.#browserProcess.pid} exists`);
        if (process.platform === 'win32') {
          try {
            childProcess.execSync(
              `taskkill /pid ${this.#browserProcess.pid} /T /F`,
            );
          } catch (error) {
            debugLaunch(
              `Killing ${this.#browserProcess.pid} using taskkill failed`,
              error,
            );
            // taskkill can fail to kill the process e.g. due to missing permissions.
            // Let's kill the process via Node API. This delays killing of all child
            // processes of `this.proc` until the main Node.js process dies.
            this.#browserProcess.kill();
          }
        } else {
          // on linux the process group can be killed with the group id prefixed with
          // a minus sign. The process group id is the group leader's pid.
          const processGroupId = -this.#browserProcess.pid;

          try {
            process.kill(processGroupId, 'SIGKILL');
          } catch (error) {
            debugLaunch(
              `Killing ${this.#browserProcess.pid} using process.kill failed`,
              error,
            );
            // Killing the process group can fail due e.g. to missing permissions.
            // Let's kill the process via Node API. This delays killing of all child
            // processes of `this.proc` until the main Node.js process dies.
            this.#browserProcess.kill('SIGKILL');
          }
        }
      } catch (error) {
        throw new Error(
          `${PROCESS_ERROR_EXPLANATION}\nError cause: ${
            isErrorLike(error) ? error.stack : error
          }`,
        );
      }
    }
    this.#clearListeners();
  }

  #recordStream(stream: Stream.Readable): void {
    const rl = readline.createInterface(stream);
    const cleanup = (): void => {
      rl.off('line', onLine);
      rl.off('close', onClose);
      try {
        rl.close();
      } catch {}
    };
    const onLine = (line: string) => {
      if (line.trim() === '') {
        return;
      }
      this.#logs.push(line);
      const delta = this.#logs.length - this.#maxLogLinesSize;
      if (delta) {
        this.#logs.splice(0, delta);
      }
      this.#lineEmitter.emit('line', line);
    };
    const onClose = (): void => {
      cleanup();
    };
    rl.on('line', onLine);
    rl.on('close', onClose);
  }

  /**
   * Get recent logs (stderr + stdout) emitted by the browser.
   *
   * @public
   */
  getRecentLogs(): string[] {
    return [...this.#logs];
  }

  waitForLineOutput(regex: RegExp, timeout = 0): Promise<string> {
    return new Promise((resolve, reject) => {
      const onClose = (errorOrCode?: Error | number): void => {
        cleanup();
        reject(
          new Error(
            [
              `Failed to launch the browser process: ${
                errorOrCode instanceof Error
                  ? ` ${errorOrCode.message}`
                  : ` Code: ${errorOrCode}`
              }`,
              '',
              `stderr:`,
              this.getRecentLogs().join('\n'),
              '',
              'TROUBLESHOOTING: https://pptr.dev/troubleshooting',
              '',
            ].join('\n'),
          ),
        );
      };

      this.#browserProcess.on('exit', onClose);
      this.#browserProcess.on('error', onClose);
      const timeoutId =
        timeout > 0 ? setTimeout(onTimeout, timeout) : undefined;

      this.#lineEmitter.on('line', onLine);
      const cleanup = (): void => {
        clearTimeout(timeoutId);
        this.#lineEmitter.off('line', onLine);
        this.#browserProcess.off('exit', onClose);
        this.#browserProcess.off('error', onClose);
      };

      function onTimeout(): void {
        cleanup();
        reject(
          new TimeoutError(
            `Timed out after ${timeout} ms while waiting for the WS endpoint URL to appear in stdout!`,
          ),
        );
      }

      for (const line of this.#logs) {
        onLine(line);
      }

      function onLine(line: string): void {
        const match = line.match(regex);
        if (!match) {
          return;
        }
        cleanup();
        // The RegExp matches, so this will obviously exist.
        resolve(match[1]!);
      }
    });
  }
}

const PROCESS_ERROR_EXPLANATION = `Puppeteer was unable to kill the process which ran the browser binary.
This means that, on future Puppeteer launches, Puppeteer might not be able to launch the browser.
Please check your open processes and ensure that the browser processes that Puppeteer launched have been killed.
If you think this is a bug, please report it on the Puppeteer issue tracker.`;

/**
 * @internal
 */
function pidExists(pid: number): boolean {
  try {
    return process.kill(pid, 0);
  } catch (error) {
    if (isErrnoException(error)) {
      if (error.code && error.code === 'ESRCH') {
        return false;
      }
    }
    throw error;
  }
}

/**
 * @internal
 */
export interface ErrorLike extends Error {
  name: string;
  message: string;
}

/**
 * @internal
 */
export function isErrorLike(obj: unknown): obj is ErrorLike {
  return (
    typeof obj === 'object' && obj !== null && 'name' in obj && 'message' in obj
  );
}
/**
 * @internal
 */
export function isErrnoException(obj: unknown): obj is NodeJS.ErrnoException {
  return (
    isErrorLike(obj) &&
    ('errno' in obj || 'code' in obj || 'path' in obj || 'syscall' in obj)
  );
}

/**
 * @public
 */
export class TimeoutError extends Error {
  /**
   * @internal
   */
  constructor(message?: string) {
    super(message);
    this.name = this.constructor.name;
    Error.captureStackTrace(this, this.constructor);
  }
}