-
Notifications
You must be signed in to change notification settings - Fork 26k
/
worker-utils.ts
57 lines (49 loc) · 1.67 KB
/
worker-utils.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
import * as Log from '../../build/output/log'
import http from 'http'
export const getFreePort = async (): Promise<number> => {
return new Promise((resolve, reject) => {
const server = http.createServer(() => {})
server.listen(0, () => {
const address = server.address()
server.close()
if (address && typeof address === 'object') {
resolve(address.port)
} else {
reject(new Error('invalid address from server: ' + address?.toString()))
}
})
})
}
export const genRenderExecArgv = async (
isNodeDebugging: string | boolean | undefined,
type: 'pages' | 'app'
) => {
const execArgv = process.execArgv.filter((localArg) => {
return (
!localArg.startsWith('--inspect') && !localArg.startsWith('--inspect-brk')
)
})
if (isNodeDebugging) {
const debugPort = await getFreePort()
const isDebugging =
process.execArgv.some((localArg) => localArg.startsWith('--inspect')) ||
process.env.NODE_OPTIONS?.match?.(/--inspect(=\S+)?( |$)/)
const isDebuggingWithBrk =
process.execArgv.some((localArg) =>
localArg.startsWith('--inspect-brk')
) || process.env.NODE_OPTIONS?.match?.(/--inspect-brk(=\S+)?( |$)/)
if (isDebugging || isDebuggingWithBrk) {
Log.info(
`the --inspect${
isDebuggingWithBrk ? '-brk' : ''
} option was detected, the Next.js server${
type === 'pages' ? ' for pages' : type === 'app' ? ' for app' : ''
} should be inspected at port ${debugPort}.`
)
}
if (isDebugging || isDebuggingWithBrk) {
execArgv.push(`--inspect${isDebuggingWithBrk ? '-brk' : ''}=${debugPort}`)
}
}
return execArgv
}