-
Notifications
You must be signed in to change notification settings - Fork 549
/
debug.js
123 lines (111 loc) · 3.07 KB
/
debug.js
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
'use strict'
const { test } = require('node:test')
const { spawn } = require('node:child_process')
const { join } = require('node:path')
const { tspl } = require('@matteo.collina/tspl')
// eslint-disable-next-line no-control-regex
const removeEscapeColorsRE = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g
test('debug#websocket', { skip: !process.versions.icu }, async t => {
const assert = tspl(t, { plan: 8 })
const child = spawn(
process.execPath,
[join(__dirname, '../fixtures/websocket.js')],
{
env: {
NODE_DEBUG: 'websocket'
}
}
)
const chunks = []
const assertions = [
/(WEBSOCKET [0-9]+:) (connecting to)/,
/(WEBSOCKET [0-9]+:) (connected to)/,
/(WEBSOCKET [0-9]+:) (sending request)/,
/(WEBSOCKET [0-9]+:) (connection opened)/,
/(WEBSOCKET [0-9]+:) (closed connection to)/,
/^$/
]
child.stderr.setEncoding('utf8')
child.stderr.on('data', chunk => {
chunks.push(chunk)
})
child.stderr.on('end', () => {
const lines = extractLines(chunks)
assert.strictEqual(lines.length, assertions.length)
for (let i = 1; i < lines.length; i++) {
assert.match(lines[i], assertions[i])
}
})
await assert.completed
})
test('debug#fetch', async t => {
const assert = tspl(t, { plan: 7 })
const child = spawn(
process.execPath,
[join(__dirname, '../fixtures/fetch.js')],
{
env: Object.assign({}, process.env, { NODE_DEBUG: 'fetch' })
}
)
const chunks = []
const assertions = [
/(FETCH [0-9]+:) (connecting to)/,
/(FETCH [0-9]+:) (connected to)/,
/(FETCH [0-9]+:) (sending request)/,
/(FETCH [0-9]+:) (received response)/,
/(FETCH [0-9]+:) (trailers received)/,
/^$/
]
child.stderr.setEncoding('utf8')
child.stderr.on('data', chunk => {
chunks.push(chunk)
})
child.stderr.on('end', () => {
const lines = extractLines(chunks)
assert.strictEqual(lines.length, assertions.length)
for (let i = 0; i < lines.length; i++) {
assert.match(lines[i], assertions[i])
}
})
await assert.completed
})
test('debug#undici', async t => {
// Due to Node.js webpage redirect
const assert = tspl(t, { plan: 7 })
const child = spawn(
process.execPath,
[join(__dirname, '../fixtures/undici.js')],
{
env: {
NODE_DEBUG: 'undici'
}
}
)
const chunks = []
const assertions = [
/(UNDICI [0-9]+:) (connecting to)/,
/(UNDICI [0-9]+:) (connected to)/,
/(UNDICI [0-9]+:) (sending request)/,
/(UNDICI [0-9]+:) (received response)/,
/(UNDICI [0-9]+:) (trailers received)/,
/^$/
]
child.stderr.setEncoding('utf8')
child.stderr.on('data', chunk => {
chunks.push(chunk)
})
child.stderr.on('end', () => {
const lines = extractLines(chunks)
assert.strictEqual(lines.length, assertions.length)
for (let i = 0; i < lines.length; i++) {
assert.match(lines[i], assertions[i])
}
})
await assert.completed
})
function extractLines (chunks) {
return chunks
.join('')
.split('\n')
.map(v => v.replace(removeEscapeColorsRE, ''))
}