Skip to content

Commit 71f6213

Browse files
jsumnersgurgunday
andauthored
Merge next into master (#352)
* Update for Fastify v5 (#347) * update * remove coverage check * update ci * Update package.json Signed-off-by: James Sumners <[email protected]> --------- Signed-off-by: James Sumners <[email protected]> Co-authored-by: Gürgün Dayıoğlu <[email protected]>
1 parent 27197c9 commit 71f6213

File tree

8 files changed

+123
-113
lines changed

8 files changed

+123
-113
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ on:
1717

1818
jobs:
1919
test:
20-
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
20+
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v4.1.0
2121
with:
2222
license-check: true

.taprc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
ts: false
2-
jsx: false
3-
flow: false
4-
coverage: true
5-
check-coverage: true
1+
files:
2+
- test/**/*.js

index.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const kWsHead = Symbol('wsHead')
1313
const kWsUpgradeListener = Symbol('wsUpgradeListener')
1414

1515
function liftErrorCode (code) {
16-
/* istanbul ignore next */
16+
/* c8 ignore start */
1717
if (typeof code !== 'number') {
1818
// Sometimes "close" event emits with a non-numeric value
1919
return 1011
@@ -23,6 +23,7 @@ function liftErrorCode (code) {
2323
} else {
2424
return code
2525
}
26+
/* c8 ignore stop */
2627
}
2728

2829
function closeWebSocket (socket, code, reason) {
@@ -52,27 +53,27 @@ function proxyWebSockets (source, target) {
5253
}
5354

5455
source.on('message', (data, binary) => waitConnection(target, () => target.send(data, { binary })))
55-
/* istanbul ignore next */
56+
/* c8 ignore start */
5657
source.on('ping', data => waitConnection(target, () => target.ping(data)))
57-
/* istanbul ignore next */
5858
source.on('pong', data => waitConnection(target, () => target.pong(data)))
59+
/* c8 ignore stop */
5960
source.on('close', close)
60-
/* istanbul ignore next */
61+
/* c8 ignore start */
6162
source.on('error', error => close(1011, error.message))
62-
/* istanbul ignore next */
6363
source.on('unexpected-response', () => close(1011, 'unexpected response'))
64+
/* c8 ignore stop */
6465

6566
// source WebSocket is already connected because it is created by ws server
6667
target.on('message', (data, binary) => source.send(data, { binary }))
67-
/* istanbul ignore next */
68+
/* c8 ignore start */
6869
target.on('ping', data => source.ping(data))
69-
/* istanbul ignore next */
70+
/* c8 ignore stop */
7071
target.on('pong', data => source.pong(data))
7172
target.on('close', close)
72-
/* istanbul ignore next */
73+
/* c8 ignore start */
7374
target.on('error', error => close(1011, error.message))
74-
/* istanbul ignore next */
7575
target.on('unexpected-response', () => close(1011, 'unexpected response'))
76+
/* c8 ignore stop */
7677
}
7778

7879
function handleUpgrade (fastify, rawRequest, socket, head) {
@@ -129,7 +130,6 @@ class WebSocketProxy {
129130
fastify.server.close = function (done) {
130131
wss.close(() => {
131132
oldClose.call(this, (err) => {
132-
/* istanbul ignore next */
133133
done && done(err)
134134
})
135135
})
@@ -139,11 +139,11 @@ class WebSocketProxy {
139139
}
140140
}
141141

142-
/* istanbul ignore next */
142+
/* c8 ignore start */
143143
wss.on('error', (err) => {
144-
/* istanbul ignore next */
145144
this.logger.error(err)
146145
})
146+
/* c8 ignore stop */
147147

148148
this.wss = wss
149149
this.prefixList = []
@@ -167,7 +167,7 @@ class WebSocketProxy {
167167

168168
const upstream = this.getUpstream(request, '')
169169
const target = new URL(dest, upstream)
170-
/* istanbul ignore next */
170+
/* c8 ignore next */
171171
target.protocol = upstream.indexOf('http:') === 0 ? 'ws:' : 'wss'
172172
target.search = search
173173
return target
@@ -333,10 +333,9 @@ async function fastifyHttpProxy (fastify, opts) {
333333
reply.hijack()
334334
try {
335335
wsProxy.handleUpgrade(request, dest || '/', noop)
336-
} catch (err) {
337-
/* istanbul ignore next */
336+
} /* c8 ignore start */ catch (err) {
338337
request.log.warn({ err }, 'websocket proxy error')
339-
}
338+
} /* c8 ignore stop */
340339
return
341340
}
342341
reply.from(dest || '/', replyOpts)

package.json

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,34 @@
2828
},
2929
"homepage": "https://github.com/fastify/fastify-http-proxy#readme",
3030
"devDependencies": {
31-
"@fastify/pre-commit": "^2.0.2",
32-
"@fastify/websocket": "^9.0.0",
33-
"@types/node": "^20.1.0",
34-
"@types/ws": "^8.2.2",
35-
"@typescript-eslint/eslint-plugin": "^7.1.0",
36-
"@typescript-eslint/parser": "^7.1.0",
37-
"express": "^4.17.2",
31+
"@fastify/pre-commit": "^2.1.0",
32+
"@fastify/websocket": "^10.0.1",
33+
"@types/node": "^20.12.7",
34+
"@types/ws": "^8.5.10",
35+
"@typescript-eslint/eslint-plugin": "^7.6.0",
36+
"@typescript-eslint/parser": "^7.6.0",
37+
"express": "^4.19.2",
3838
"express-http-proxy": "^2.0.0",
3939
"fast-proxy": "^2.1.0",
40-
"fastify": "^4.0.0-rc.2",
41-
"got": "^11.8.3",
40+
"fastify": "^4.26.2",
41+
"got": "^11.8.6",
4242
"http-errors": "^2.0.0",
4343
"http-proxy": "^1.18.1",
44-
"simple-get": "^4.0.0",
44+
"simple-get": "^4.0.1",
4545
"snazzy": "^9.0.0",
46-
"socket.io": "^4.4.1",
47-
"socket.io-client": "^4.4.1",
48-
"standard": "^17.0.0",
49-
"tap": "^16.0.0",
46+
"socket.io": "^4.7.5",
47+
"socket.io-client": "^4.7.5",
48+
"standard": "^17.1.0",
49+
"tap": "^18.7.2",
5050
"tsd": "^0.31.0",
51-
"typescript": "^5.0.2",
51+
"typescript": "^5.4.5",
5252
"why-is-node-running": "^2.2.2"
5353
},
5454
"dependencies": {
55-
"@fastify/reply-from": "^9.0.0",
55+
"@fastify/reply-from": "^10.0.0-pre.fv5.1",
5656
"fast-querystring": "^1.1.2",
57-
"fastify-plugin": "^4.5.0",
58-
"ws": "^8.4.2"
57+
"fastify-plugin": "^4.5.1",
58+
"ws": "^8.16.0"
5959
},
6060
"tsd": {
6161
"directory": "test/types"

test/websocket-pathname.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const { test } = require('tap')
2+
const Fastify = require('fastify')
3+
const proxy = require('..')
4+
const WebSocket = require('ws')
5+
const { createServer } = require('node:http')
6+
const { promisify } = require('node:util')
7+
const { once } = require('node:events')
8+
9+
// TODO: this test is flaky, probably because of promise resolution
10+
test('keep proxy websocket pathname', async (t) => {
11+
t.plan(5)
12+
13+
const origin = createServer()
14+
const wss = new WebSocket.Server({ server: origin })
15+
16+
t.teardown(wss.close.bind(wss))
17+
t.teardown(origin.close.bind(origin))
18+
19+
const serverMessages = []
20+
wss.on('connection', (ws, request) => {
21+
ws.on('message', (message, binary) => {
22+
// Also need save request.url for check from what url the message is coming.
23+
serverMessages.push([message.toString(), binary, request.headers.host.split(':', 1)[0], request.url])
24+
ws.send(message, { binary })
25+
})
26+
})
27+
28+
await promisify(origin.listen.bind(origin))({ port: 0, host: '127.0.0.1' })
29+
// Host for wsUpstream and for later check.
30+
const host = '127.0.0.1'
31+
// Path for wsUpstream and for later check.
32+
const path = '/keep/path'
33+
const server = Fastify()
34+
server.register(proxy, {
35+
upstream: `ws://127.0.0.1:${origin.address().port}`,
36+
// Start proxy with different upstream, without path
37+
wsUpstream: `ws://${host}:${origin.address().port}`,
38+
websocket: true
39+
})
40+
41+
await server.listen({ port: 0, host: '127.0.0.1' })
42+
t.teardown(server.close.bind(server))
43+
44+
// Start websocket with different upstream for connect, added path.
45+
const ws = new WebSocket(`ws://${host}:${server.server.address().port}${path}`)
46+
await once(ws, 'open')
47+
48+
const data = [{ message: 'hello', binary: false }, { message: 'fastify', binary: true, isBuffer: true }]
49+
const dataLength = data.length
50+
let dataIndex = 0
51+
52+
for (; dataIndex < dataLength; dataIndex++) {
53+
const { message: msg, binary, isBuffer } = data[dataIndex]
54+
const message = isBuffer
55+
? Buffer.from(msg)
56+
: msg
57+
58+
ws.send(message, { binary })
59+
60+
const [reply, binaryAnswer] = await once(ws, 'message')
61+
62+
t.equal(reply.toString(), msg)
63+
t.equal(binaryAnswer, binary)
64+
}
65+
// Also check "path", must be the same.
66+
t.strictSame(serverMessages, [
67+
['hello', false, host, path],
68+
['fastify', true, host, path]
69+
])
70+
71+
await Promise.all([
72+
once(ws, 'close'),
73+
server.close()
74+
])
75+
})

test/websocket.js

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -642,70 +642,3 @@ test('multiple websocket upstreams with distinct server options', async (t) => {
642642
server.close()
643643
])
644644
})
645-
646-
test('keep proxy websocket pathname', async (t) => {
647-
t.plan(5)
648-
649-
const origin = createServer()
650-
const wss = new WebSocket.Server({ server: origin })
651-
652-
t.teardown(wss.close.bind(wss))
653-
t.teardown(origin.close.bind(origin))
654-
655-
const serverMessages = []
656-
wss.on('connection', (ws, request) => {
657-
ws.on('message', (message, binary) => {
658-
// Also need save request.url for check from what url the message is coming.
659-
serverMessages.push([message.toString(), binary, request.headers.host.split(':', 1)[0], request.url])
660-
ws.send(message, { binary })
661-
})
662-
})
663-
664-
await promisify(origin.listen.bind(origin))({ port: 0, host: '127.0.0.1' })
665-
// Host for wsUpstream and for later check.
666-
const host = '127.0.0.1'
667-
// Path for wsUpstream and for later check.
668-
const path = '/keep/path'
669-
const server = Fastify()
670-
server.register(proxy, {
671-
upstream: `ws://127.0.0.1:${origin.address().port}`,
672-
// Start proxy with different upstream, without path
673-
wsUpstream: `ws://${host}:${origin.address().port}`,
674-
websocket: true
675-
})
676-
677-
await server.listen({ port: 0, host: '127.0.0.1' })
678-
t.teardown(server.close.bind(server))
679-
680-
// Start websocket with different upstream for connect, added path.
681-
const ws = new WebSocket(`ws://${host}:${server.server.address().port}${path}`)
682-
await once(ws, 'open')
683-
684-
const data = [{ message: 'hello', binary: false }, { message: 'fastify', binary: true, isBuffer: true }]
685-
const dataLength = data.length
686-
let dataIndex = 0
687-
688-
for (; dataIndex < dataLength; dataIndex++) {
689-
const { message: msg, binary, isBuffer } = data[dataIndex]
690-
const message = isBuffer
691-
? Buffer.from(msg)
692-
: msg
693-
694-
ws.send(message, { binary })
695-
696-
const [reply, binaryAnswer] = await once(ws, 'message')
697-
698-
t.equal(reply.toString(), msg)
699-
t.equal(binaryAnswer, binary)
700-
}
701-
// Also check "path", must be the same.
702-
t.strictSame(serverMessages, [
703-
['hello', false, host, path],
704-
['fastify', true, host, path]
705-
])
706-
707-
await Promise.all([
708-
once(ws, 'close'),
709-
server.close()
710-
])
711-
})

test/ws-prefix-rewrite-core.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,12 @@ async function handleProxy (info, { backendPath, proxyOptions, wrapperOptions },
7676
handler: (req, reply) => {
7777
reply.send(req.url)
7878
},
79-
wsHandler: (conn, req) => {
80-
conn.write(req.url)
81-
conn.end()
79+
wsHandler: (socket, req) => {
80+
socket.send(req.url)
81+
82+
socket.once('message', chunk => {
83+
socket.close()
84+
})
8285
}
8386
})
8487

test/ws-prefix-rewrite.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ async function handleProxy (info, { backendPath, proxyOptions, wrapperOptions },
7474
handler: (req, reply) => {
7575
reply.send(req.url)
7676
},
77-
wsHandler: (conn, req) => {
78-
conn.write(req.url)
79-
conn.end()
77+
wsHandler: (socket, req) => {
78+
socket.send(req.url)
79+
80+
socket.once('message', chunk => {
81+
socket.close()
82+
})
8083
}
8184
})
8285

0 commit comments

Comments
 (0)