Skip to content

Commit a3166d3

Browse files
authored
chore: remove tspl from websocket (#4568)
1 parent 868c93e commit a3166d3

31 files changed

+342
-373
lines changed

test/websocket/client-received-masked-frame.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ const { test } = require('node:test')
44
const { once } = require('node:events')
55
const { WebSocketServer } = require('ws')
66
const { WebSocket } = require('../..')
7-
const { tspl } = require('@matteo.collina/tspl')
87
const { WebsocketFrameSend } = require('../../lib/web/websocket/frame')
98

109
test('Client fails the connection if receiving a masked frame', async (t) => {
11-
const assert = tspl(t, { plan: 2 })
10+
t.plan(2)
1211

1312
const body = Buffer.allocUnsafe(2)
1413
body.writeUInt16BE(1006, 0)
@@ -27,11 +26,11 @@ test('Client fails the connection if receiving a masked frame', async (t) => {
2726
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
2827

2928
ws.addEventListener('close', (e) => {
30-
assert.deepStrictEqual(e.code, 1006)
29+
t.assert.deepStrictEqual(e.code, 1006)
3130
})
3231

3332
ws.addEventListener('error', () => {
34-
assert.ok(true)
33+
t.assert.ok(true)
3534
})
3635

3736
t.after(() => {
@@ -40,6 +39,4 @@ test('Client fails the connection if receiving a masked frame', async (t) => {
4039
})
4140

4241
await once(ws, 'close')
43-
44-
await assert.completed
4542
})

test/websocket/close-invalid-status-code.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ const { test } = require('node:test')
44
const { once } = require('node:events')
55
const { WebSocketServer } = require('ws')
66
const { WebSocket } = require('../..')
7-
const { tspl } = require('@matteo.collina/tspl')
87

98
test('Client fails the connection if receiving a masked frame', async (t) => {
10-
const assert = tspl(t, { plan: 2 })
9+
t.plan(2)
1110

1211
const server = new WebSocketServer({ port: 0 })
1312

@@ -21,11 +20,11 @@ test('Client fails the connection if receiving a masked frame', async (t) => {
2120
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
2221

2322
ws.addEventListener('close', (e) => {
24-
assert.deepStrictEqual(e.code, 1006)
23+
t.assert.deepStrictEqual(e.code, 1006)
2524
})
2625

2726
ws.addEventListener('error', () => {
28-
assert.ok(true)
27+
t.assert.ok(true)
2928
})
3029

3130
t.after(() => {
@@ -34,6 +33,4 @@ test('Client fails the connection if receiving a masked frame', async (t) => {
3433
})
3534

3635
await once(ws, 'close')
37-
38-
await assert.completed
3936
})

test/websocket/close-invalid-utf-8.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@ const { test } = require('node:test')
44
const { once } = require('node:events')
55
const { WebSocketServer } = require('ws')
66
const { WebSocket } = require('../..')
7-
const { tspl } = require('@matteo.collina/tspl')
87

9-
test('Receiving a close frame with invalid utf-8', async (t) => {
10-
const assert = tspl(t, { plan: 2 })
8+
test('Receiving a close frame with invalid utf-8', (t, done) => {
9+
t.plan(2)
1110

1211
const server = new WebSocketServer({ port: 0 })
12+
t.after(() => {
13+
server.close()
14+
})
1315

1416
server.on('connection', (ws) => {
1517
ws.close(1000, Buffer.from([0xFF, 0xFE]))
1618

1719
ws.on('close', (code) => {
18-
assert.equal(code, 1007)
20+
t.assert.strictEqual(code, 1007)
21+
done()
1922
})
2023
})
2124

2225
const events = []
2326
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
27+
t.after(() => { ws.close() })
2428

2529
ws.addEventListener('close', (e) => {
2630
events.push({ type: 'close', code: e.code })
@@ -30,20 +34,13 @@ test('Receiving a close frame with invalid utf-8', async (t) => {
3034
events.push({ type: 'error' })
3135
})
3236

33-
t.after(() => {
34-
server.close()
35-
ws.close()
37+
once(ws, 'close').then(() => {
38+
// An error event should be propagated immediately, then we should receive
39+
// a close event with a 1006 code. The code is 1006, and not 1007 (as we send
40+
// the server) because the connection is closed before the server responds.
41+
t.assert.deepStrictEqual(events, [
42+
{ type: 'error' },
43+
{ type: 'close', code: 1006 }
44+
])
3645
})
37-
38-
await once(ws, 'close')
39-
40-
// An error event should be propagated immediately, then we should receive
41-
// a close event with a 1006 code. The code is 1006, and not 1007 (as we send
42-
// the server) because the connection is closed before the server responds.
43-
assert.deepStrictEqual(events, [
44-
{ type: 'error' },
45-
{ type: 'close', code: 1006 }
46-
])
47-
48-
await assert.completed
4946
})

test/websocket/close.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
const { tspl } = require('@matteo.collina/tspl')
44
const { describe, test, after } = require('node:test')
5-
const assert = require('node:assert')
65
const { WebSocketServer } = require('ws')
76
const { WebSocket } = require('../..')
87

98
describe('Close', () => {
10-
test('Close with code', () => {
9+
test('Close with code', (t) => {
1110
return new Promise((resolve) => {
1211
const server = new WebSocketServer({ port: 0 })
1312

1413
server.on('connection', (ws) => {
1514
ws.on('close', (code) => {
16-
assert.equal(code, 1000)
15+
t.assert.strictEqual(code, 1000)
1716
server.close()
1817
resolve()
1918
})
@@ -24,14 +23,14 @@ describe('Close', () => {
2423
})
2524
})
2625

27-
test('Close with code and reason', () => {
26+
test('Close with code and reason', (t) => {
2827
return new Promise((resolve) => {
2928
const server = new WebSocketServer({ port: 0 })
3029

3130
server.on('connection', (ws) => {
3231
ws.on('close', (code, reason) => {
33-
assert.equal(code, 1000)
34-
assert.deepStrictEqual(reason, Buffer.from('Goodbye'))
32+
t.assert.strictEqual(code, 1000)
33+
t.assert.deepStrictEqual(reason, Buffer.from('Goodbye'))
3534
server.close()
3635
resolve()
3736
})
@@ -42,22 +41,22 @@ describe('Close', () => {
4241
})
4342
})
4443

45-
test('Close with invalid code', () => {
44+
test('Close with invalid code', (t) => {
4645
const server = new WebSocketServer({ port: 0 })
4746

4847
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
4948

5049
return new Promise((resolve) => {
5150
ws.addEventListener('open', () => {
52-
assert.throws(
51+
t.assert.throws(
5352
() => ws.close(2999),
5453
{
5554
name: 'InvalidAccessError',
5655
constructor: DOMException
5756
}
5857
)
5958

60-
assert.throws(
59+
t.assert.throws(
6160
() => ws.close(5000),
6261
{
6362
name: 'InvalidAccessError',
@@ -72,14 +71,14 @@ describe('Close', () => {
7271
})
7372
})
7473

75-
test('Close with invalid reason', () => {
74+
test('Close with invalid reason', (t) => {
7675
const server = new WebSocketServer({ port: 0 })
7776

7877
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
7978

8079
return new Promise((resolve) => {
8180
ws.addEventListener('open', () => {
82-
assert.throws(
81+
t.assert.throws(
8382
() => ws.close(1000, 'a'.repeat(124)),
8483
{
8584
name: 'SyntaxError',
@@ -94,14 +93,14 @@ describe('Close', () => {
9493
})
9594
})
9695

97-
test('Close with no code or reason', () => {
96+
test('Close with no code or reason', (t) => {
9897
const server = new WebSocketServer({ port: 0 })
9998

10099
return new Promise((resolve) => {
101100
server.on('connection', (ws) => {
102101
ws.on('close', (code, reason) => {
103-
assert.equal(code, 1005)
104-
assert.deepStrictEqual(reason, Buffer.alloc(0))
102+
t.assert.strictEqual(code, 1005)
103+
t.assert.deepStrictEqual(reason, Buffer.alloc(0))
105104
server.close()
106105
resolve()
107106
})
@@ -112,14 +111,14 @@ describe('Close', () => {
112111
})
113112
})
114113

115-
test('Close with a 3000 status code', () => {
114+
test('Close with a 3000 status code', (t) => {
116115
const server = new WebSocketServer({ port: 0 })
117116

118117
return new Promise((resolve) => {
119118
server.on('connection', (ws) => {
120119
ws.on('close', (code, reason) => {
121-
assert.equal(code, 3000)
122-
assert.deepStrictEqual(reason, Buffer.alloc(0))
120+
t.assert.strictEqual(code, 3000)
121+
t.assert.deepStrictEqual(reason, Buffer.alloc(0))
123122
server.close()
124123
resolve()
125124
})

test/websocket/constructor.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,42 @@
11
'use strict'
22

33
const { test } = require('node:test')
4-
const assert = require('node:assert')
54
const { WebSocket } = require('../..')
65

7-
test('Constructor', () => {
8-
assert.throws(
6+
test('Constructor', (t) => {
7+
t.assert.throws(
98
() => new WebSocket('abc'),
109
{
1110
name: 'SyntaxError',
1211
constructor: DOMException
1312
}
1413
)
1514

16-
assert.throws(
15+
t.assert.throws(
1716
() => new WebSocket('wss://echo.websocket.events/#a'),
1817
{
1918
name: 'SyntaxError',
2019
constructor: DOMException
2120
}
2221
)
2322

24-
assert.throws(
23+
t.assert.throws(
2524
() => new WebSocket('wss://echo.websocket.events', ''),
2625
{
2726
name: 'SyntaxError',
2827
constructor: DOMException
2928
}
3029
)
3130

32-
assert.throws(
31+
t.assert.throws(
3332
() => new WebSocket('wss://echo.websocket.events', ['chat', 'chat']),
3433
{
3534
name: 'SyntaxError',
3635
constructor: DOMException
3736
}
3837
)
3938

40-
assert.throws(
39+
t.assert.throws(
4140
() => new WebSocket('wss://echo.websocket.events', ['<>@,;:\\"/[]?={}\t']),
4241
{
4342
name: 'SyntaxError',

test/websocket/continuation-frames.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
const { test } = require('node:test')
44
const { WebSocketServer } = require('ws')
55
const { WebSocket } = require('../..')
6-
const { tspl } = require('@matteo.collina/tspl')
76

8-
test('Receiving multiple continuation frames works as expected', async (t) => {
9-
const p = tspl(t, { plan: 1 })
7+
test('Receiving multiple continuation frames works as expected', (t, done) => {
8+
t.plan(1)
109

1110
const frames = [
1211
Buffer.from([0x01, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f]), // text frame "hello" (fragmented)
@@ -27,13 +26,14 @@ test('Receiving multiple continuation frames works as expected', async (t) => {
2726

2827
const ws = new WebSocket(`ws://localhost:${server.address().port}`)
2928

30-
ws.onerror = p.fail
31-
ws.onmessage = (e) => p.deepStrictEqual(e.data, 'hellohellohellohello')
29+
ws.onerror = t.assert.fail
30+
ws.onmessage = (e) => {
31+
t.assert.deepStrictEqual(e.data, 'hellohellohellohello')
32+
done()
33+
}
3234

3335
t.after(() => {
3436
server.close()
3537
ws.close()
3638
})
37-
38-
await p.completed
3939
})

test/websocket/custom-headers.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
'use strict'
22

33
const { test } = require('node:test')
4-
const assert = require('node:assert')
54
const { Agent, WebSocket } = require('../..')
65

7-
test('Setting custom headers', (t) => {
6+
test('Setting custom headers', (t, done) => {
87
const headers = {
98
'x-khafra-hello': 'hi',
109
Authorization: 'Bearer base64orsomethingitreallydoesntmatter'
1110
}
1211

13-
return new Promise((resolve, reject) => {
14-
class TestAgent extends Agent {
15-
dispatch (options) {
16-
assert.deepStrictEqual(options.headers['x-khafra-hello'], headers['x-khafra-hello'])
17-
assert.deepStrictEqual(options.headers.Authorization, headers.Authorization)
18-
resolve()
19-
return false
20-
}
12+
class TestAgent extends Agent {
13+
dispatch (options) {
14+
t.assert.deepStrictEqual(options.headers['x-khafra-hello'], headers['x-khafra-hello'])
15+
t.assert.deepStrictEqual(options.headers.Authorization, headers.Authorization)
16+
done()
17+
return false
2118
}
19+
}
2220

23-
const ws = new WebSocket('wss://echo.websocket.events', {
24-
headers,
25-
dispatcher: new TestAgent()
26-
})
27-
28-
ws.onclose = ws.onerror = ws.onmessage = reject
21+
const ws = new WebSocket('wss://echo.websocket.events', {
22+
headers,
23+
dispatcher: new TestAgent()
2924
})
25+
26+
ws.onclose = ws.onerror = ws.onmessage = t.assert.fail
3027
})

0 commit comments

Comments
 (0)