@fastify/http-proxy
Advanced tools
Comparing version 8.2.3 to 8.3.0
14
index.js
@@ -111,3 +111,2 @@ 'use strict' | ||
const source = new URL(request.url, 'ws://127.0.0.1') | ||
for (const { prefix, rewritePrefix, upstream, wsClientOptions } of this.prefixList) { | ||
@@ -166,3 +165,14 @@ if (source.pathname.startsWith(prefix)) { | ||
wsProxy.addUpstream(fastify.prefix, rewritePrefix, options.upstream, options.wsClientOptions) | ||
if (options.upstream !== '') { | ||
wsProxy.addUpstream(fastify.prefix, rewritePrefix, options.upstream, options.wsClientOptions) | ||
} else if (typeof options.replyOptions.getUpstream === 'function') { | ||
wsProxy.findUpstream = function (request) { | ||
const source = new URL(request.url, 'ws://127.0.0.1') | ||
const upstream = options.replyOptions.getUpstream(request, '') | ||
const target = new URL(source.pathname, upstream) | ||
target.protocol = upstream.indexOf('http:') === 0 ? 'ws:' : 'wss' | ||
target.search = source.search | ||
return { target, wsClientOptions: options.wsClientOptions } | ||
} | ||
} | ||
} | ||
@@ -169,0 +179,0 @@ |
{ | ||
"name": "@fastify/http-proxy", | ||
"version": "8.2.3", | ||
"version": "8.3.0", | ||
"description": "proxy http requests, for Fastify", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"lint": "standard | snazzy", | ||
"lint:fix": "standard --fix | snazzy", | ||
"lint:typescript": "npm run lint:fix - --parser @typescript-eslint/parser --plugin typescript \"test/types/*.ts\"", | ||
"test": "npm run lint && tap \"test/*.js\" && npm run typescript", | ||
"typescript": "tsd" | ||
}, | ||
"repository": { | ||
@@ -63,3 +56,10 @@ "type": "git", | ||
"access": "public" | ||
}, | ||
"scripts": { | ||
"lint": "standard | snazzy", | ||
"lint:fix": "standard --fix | snazzy", | ||
"lint:typescript": "npm run lint:fix - --parser @typescript-eslint/parser --plugin typescript \"test/types/*.ts\"", | ||
"test": "npm run lint && tap \"test/*.js\" && npm run typescript", | ||
"typescript": "tsd" | ||
} | ||
} | ||
} |
@@ -148,3 +148,3 @@ # @fastify/http-proxy | ||
An object accessible within the `preHandler` via `reply.context.config`. | ||
See [Config](https://www.fastify.io/docs/v2.1.x/Routes/#config) in the Fastify | ||
See [Config](https://www.fastify.io/docs/v4.8.x/Reference/Routes/#config) in the Fastify | ||
documentation for information on this option. Note: this is merged with other | ||
@@ -151,0 +151,0 @@ configuration passed to the route. |
@@ -69,2 +69,5 @@ 'use strict' | ||
upstream: '', | ||
getWebSocketUpstream () { | ||
t.fail('should never be called') | ||
}, | ||
replyOptions: { | ||
@@ -71,0 +74,0 @@ getUpstream: function (original, base) { |
@@ -128,1 +128,61 @@ 'use strict' | ||
}) | ||
test('getWebSocketStream', async (t) => { | ||
t.plan(7) | ||
const origin = createServer() | ||
const wss = new WebSocket.Server({ server: origin }) | ||
t.teardown(wss.close.bind(wss)) | ||
t.teardown(origin.close.bind(origin)) | ||
const serverMessages = [] | ||
wss.on('connection', (ws, request) => { | ||
t.equal(ws.protocol, subprotocolValue) | ||
t.equal(request.headers.cookie, cookieValue) | ||
ws.on('message', (message, binary) => { | ||
serverMessages.push([message.toString(), binary]) | ||
// echo | ||
ws.send(message, { binary }) | ||
}) | ||
}) | ||
await promisify(origin.listen.bind(origin))({ port: 0 }) | ||
const server = Fastify() | ||
server.register(proxy, { | ||
upstream: '', | ||
replyOptions: { | ||
getUpstream: function (original, base) { | ||
return `http://localhost:${origin.address().port}` | ||
} | ||
}, | ||
websocket: true | ||
}) | ||
await server.listen({ port: 0 }) | ||
t.teardown(server.close.bind(server)) | ||
const options = { headers: { cookie: cookieValue } } | ||
const ws = new WebSocket(`ws://localhost:${server.server.address().port}`, [subprotocolValue], options) | ||
await once(ws, 'open') | ||
ws.send('hello', { binary: false }) | ||
const [reply0, binary0] = await once(ws, 'message') | ||
t.equal(reply0.toString(), 'hello') | ||
t.equal(binary0, false) | ||
ws.send(Buffer.from('fastify'), { binary: true }) | ||
const [reply1, binary1] = await once(ws, 'message') | ||
t.equal(reply1.toString(), 'fastify') | ||
t.equal(binary1, true) | ||
t.strictSame(serverMessages, [ | ||
['hello', false], | ||
['fastify', true] | ||
]) | ||
await Promise.all([ | ||
once(ws, 'close'), | ||
server.close() | ||
]) | ||
}) |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
58279
1525
5