-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
126 lines (99 loc) · 3.25 KB
/
index.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
import {
type InstalledWebBrowser,
chromeWebBrowserTypes,
getInstalledWebBrowsers
} from '@cityssm/web-browser-info'
import Debug from 'debug'
import {
type Browser,
type LaunchOptions,
launch as puppeteerLaunch
} from 'puppeteer'
import { DEBUG_NAMESPACE } from './debug.config.js'
const debug = Debug(`${DEBUG_NAMESPACE}:index`)
let installedWebBrowsers: InstalledWebBrowser[] = []
async function loadFallbackBrowsers(): Promise<InstalledWebBrowser[]> {
if (installedWebBrowsers.length === 0) {
const tempInstalledWebBrowsers: InstalledWebBrowser[] = []
/*
* Load Chrome first
*/
const fallbackChromeBrowsers = await getInstalledWebBrowsers(
chromeWebBrowserTypes,
110
)
tempInstalledWebBrowsers.push(...fallbackChromeBrowsers)
/*
* Load Firefox
*/
const fallbackFirefoxBrowsers = await getInstalledWebBrowsers('firefox')
tempInstalledWebBrowsers.push(...fallbackFirefoxBrowsers)
installedWebBrowsers = tempInstalledWebBrowsers
if (tempInstalledWebBrowsers.length === 0) {
throw new Error('No fallback system browsers available.')
} else {
debug('Available fallback browsers:')
debug(tempInstalledWebBrowsers)
}
}
return installedWebBrowsers
}
/**
* Launches a Puppeteer browser instance.
* Automatically falls back to a system browser if no browser is available in the Puppeteer cache.
* @param options - Optional launch parameters
* @returns - A Puppeteer browser instance.
*/
export default async function launch(
options: LaunchOptions = {}
): Promise<Browser> {
const puppeteerOptions: LaunchOptions = {
timeout: 60_000,
headless: true,
...options
}
try {
debug(`Attempting to launch browser: ${JSON.stringify(puppeteerOptions)}`)
const browser = await puppeteerLaunch(puppeteerOptions)
// eslint-disable-next-line sonarjs/different-types-comparison, @typescript-eslint/no-unnecessary-condition
if (browser === undefined) {
throw new Error('Puppeteer browser is undefined')
}
debug('Launched puppeteer browser')
return browser
} catch (error) {
debug('Switching to fallback browsers')
const fallbackOptions = await loadFallbackBrowsers()
for (const fallback of fallbackOptions) {
if (
(options.browser === 'firefox' && fallback.type !== 'firefox') ||
(options.browser === 'chrome' &&
!(chromeWebBrowserTypes as string[]).includes(fallback.type))
) {
continue
}
try {
const fallbackPuppeteerOptions: LaunchOptions = {
...puppeteerOptions,
browser: fallback.type === 'firefox' ? 'firefox' : 'chrome',
executablePath: fallback.command
}
debug(
`Attempting to launch fallback browser: ${JSON.stringify(
fallbackPuppeteerOptions
)}`
)
const browser = await puppeteerLaunch(fallbackPuppeteerOptions)
debug('Launched fallback browser')
debug(fallback)
return browser
} catch {
debug(`Error launching browser: ${fallback.command}`)
// ignore, try the next one
}
}
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw error
}
}
export * as puppeteer from 'puppeteer'