Skip to content

Commit 7a376a2

Browse files
authored
Preserve text vs binary frames (#257)
fixes #255
1 parent bc71dc7 commit 7a376a2

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ function proxyWebSockets (source, target) {
4242
closeWebSocket(target, code, reason)
4343
}
4444

45-
source.on('message', data => waitConnection(target, () => target.send(data)))
45+
source.on('message', (data, binary) => waitConnection(target, () => target.send(data, { binary })))
4646
source.on('ping', data => waitConnection(target, () => target.ping(data)))
4747
source.on('pong', data => waitConnection(target, () => target.pong(data)))
4848
source.on('close', close)
4949
source.on('error', error => close(1011, error.message))
5050
source.on('unexpected-response', () => close(1011, 'unexpected response'))
5151

5252
// source WebSocket is already connected because it is created by ws server
53-
target.on('message', data => source.send(data))
53+
target.on('message', (data, binary) => source.send(data, { binary }))
5454
target.on('ping', data => source.ping(data))
5555
target.on('pong', data => source.pong(data))
5656
target.on('close', close)

test/websocket.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,21 @@ const cookieValue = 'foo=bar'
1111
const subprotocolValue = 'foo-subprotocol'
1212

1313
test('basic websocket proxy', async (t) => {
14-
t.plan(4)
14+
t.plan(7)
1515

1616
const origin = createServer()
1717
const wss = new WebSocket.Server({ server: origin })
1818
t.teardown(wss.close.bind(wss))
1919
t.teardown(origin.close.bind(origin))
2020

21+
const serverMessages = []
2122
wss.on('connection', (ws, request) => {
2223
t.equal(ws.protocol, subprotocolValue)
2324
t.equal(request.headers.cookie, cookieValue)
24-
ws.on('message', (message) => {
25-
t.equal(message.toString(), 'hello')
25+
ws.on('message', (message, binary) => {
26+
serverMessages.push([message.toString(), binary])
2627
// echo
27-
ws.send(message)
28+
ws.send(message, { binary })
2829
})
2930
})
3031

@@ -41,15 +42,22 @@ test('basic websocket proxy', async (t) => {
4142

4243
const options = { headers: { cookie: cookieValue } }
4344
const ws = new WebSocket(`ws://localhost:${server.server.address().port}`, [subprotocolValue], options)
44-
4545
await once(ws, 'open')
4646

47-
const stream = WebSocket.createWebSocketStream(ws)
47+
ws.send('hello', { binary: false })
48+
const [reply0, binary0] = await once(ws, 'message')
49+
t.equal(reply0.toString(), 'hello')
50+
t.equal(binary0, false)
4851

49-
stream.write('hello')
52+
ws.send(Buffer.from('fastify'), { binary: true })
53+
const [reply1, binary1] = await once(ws, 'message')
54+
t.equal(reply1.toString(), 'fastify')
55+
t.equal(binary1, true)
5056

51-
const [buf] = await once(stream, 'data')
52-
t.equal(buf.toString(), 'hello')
57+
t.strictSame(serverMessages, [
58+
['hello', false],
59+
['fastify', true]
60+
])
5361

5462
await Promise.all([
5563
once(ws, 'close'),

0 commit comments

Comments
 (0)