-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(websocket): handle ping/pong frames & fix fragmented frames (#1809)
* feat(websocket): handle ping/pong frames & fix bugs * fix: don't quit parsing on ping/pong frame * fix: parse fragmented frames * fix: remove hack in tests
- Loading branch information
Showing
8 changed files
with
247 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const { WebSocketServer } = require('ws') | ||
const { WebSocket } = require('../..') | ||
const diagnosticsChannel = require('diagnostics_channel') | ||
|
||
test('Fragmented frame with a ping frame in the middle of it', (t) => { | ||
t.plan(2) | ||
|
||
const server = new WebSocketServer({ port: 0 }) | ||
|
||
server.on('connection', (ws) => { | ||
const socket = ws._socket | ||
|
||
socket.write(Buffer.from([0x01, 0x03, 0x48, 0x65, 0x6c])) // Text frame "Hel" | ||
socket.write(Buffer.from([0x89, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f])) // ping "Hello" | ||
socket.write(Buffer.from([0x80, 0x02, 0x6c, 0x6f])) // Text frame "lo" | ||
}) | ||
|
||
t.teardown(() => { | ||
for (const client of server.clients) { | ||
client.close() | ||
} | ||
|
||
server.close() | ||
}) | ||
|
||
const ws = new WebSocket(`ws://localhost:${server.address().port}`) | ||
|
||
ws.addEventListener('message', ({ data }) => { | ||
t.same(data, 'Hello') | ||
|
||
ws.close() | ||
}) | ||
|
||
diagnosticsChannel.channel('undici:websocket:ping').subscribe( | ||
({ payload }) => t.same(payload, Buffer.from('Hello')) | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const { createServer } = require('http') | ||
const { WebSocketServer } = require('ws') | ||
const { WebSocket } = require('../..') | ||
|
||
test('WebSocket connecting to server that isn\'t a Websocket server', (t) => { | ||
t.plan(5) | ||
|
||
const server = createServer((req, res) => { | ||
t.equal(req.headers.connection, 'upgrade') | ||
t.equal(req.headers.upgrade, 'websocket') | ||
t.ok(req.headers['sec-websocket-key']) | ||
t.equal(req.headers['sec-websocket-version'], '13') | ||
|
||
res.end() | ||
server.unref() | ||
}).listen(0, () => { | ||
const ws = new WebSocket(`ws://localhost:${server.address().port}`) | ||
|
||
// Server isn't a websocket server | ||
ws.onmessage = ws.onopen = t.fail | ||
|
||
ws.addEventListener('error', t.pass) | ||
}) | ||
|
||
t.teardown(server.close.bind(server)) | ||
}) | ||
|
||
test('Open event is emitted', (t) => { | ||
t.plan(1) | ||
|
||
const server = new WebSocketServer({ port: 0 }) | ||
|
||
server.on('connection', (ws) => { | ||
ws.close(1000) | ||
}) | ||
|
||
t.teardown(server.close.bind(server)) | ||
|
||
const ws = new WebSocket(`ws://localhost:${server.address().port}`) | ||
|
||
ws.onmessage = ws.onerror = t.fail | ||
ws.addEventListener('open', t.pass) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const { WebSocketServer } = require('ws') | ||
const diagnosticsChannel = require('diagnostics_channel') | ||
const { WebSocket } = require('../..') | ||
|
||
test('Receives ping and parses body', (t) => { | ||
t.plan(1) | ||
|
||
const server = new WebSocketServer({ port: 0 }) | ||
|
||
server.on('connection', (ws) => { | ||
ws.ping('Hello, world') | ||
}) | ||
|
||
t.teardown(server.close.bind(server)) | ||
|
||
const ws = new WebSocket(`ws://localhost:${server.address().port}`) | ||
ws.onerror = ws.onmessage = t.fail | ||
|
||
diagnosticsChannel.channel('undici:websocket:ping').subscribe(({ payload }) => { | ||
t.same(payload, Buffer.from('Hello, world')) | ||
ws.close() | ||
}) | ||
}) | ||
|
||
test('Receives pong and parses body', (t) => { | ||
t.plan(1) | ||
|
||
const server = new WebSocketServer({ port: 0 }) | ||
|
||
server.on('connection', (ws) => { | ||
ws.pong('Pong') | ||
}) | ||
|
||
t.teardown(server.close.bind(server)) | ||
|
||
const ws = new WebSocket(`ws://localhost:${server.address().port}`) | ||
ws.onerror = ws.onmessage = t.fail | ||
|
||
diagnosticsChannel.channel('undici:websocket:pong').subscribe(({ payload }) => { | ||
t.same(payload, Buffer.from('Pong')) | ||
ws.close() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters