fastify-http-proxy
Advanced tools
Comparing version 4.3.0 to 4.3.1
55
index.js
@@ -56,18 +56,4 @@ 'use strict' | ||
function createWebSocketUrl (options, request) { | ||
const source = new URL(request.url, 'http://127.0.0.1') | ||
const target = new URL( | ||
options.rewritePrefix || options.prefix || source.pathname, | ||
options.upstream | ||
) | ||
target.search = source.search | ||
return target | ||
} | ||
function setupWebSocketProxy (fastify, options) { | ||
function setupWebSocketProxy (fastify, options, rewritePrefix) { | ||
const server = new WebSocket.Server({ | ||
path: options.prefix, | ||
server: fastify.server, | ||
@@ -97,4 +83,10 @@ ...options.wsServerOptions | ||
server.on('connection', (source, request) => { | ||
const url = createWebSocketUrl(options, request) | ||
if (fastify.prefix && !request.url.startsWith(fastify.prefix)) { | ||
fastify.log.debug({ url: request.url }, 'not matching prefix') | ||
source.close() | ||
return | ||
} | ||
const url = createWebSocketUrl(request) | ||
const target = new WebSocket(url, options.wsClientOptions) | ||
@@ -105,4 +97,31 @@ | ||
}) | ||
function createWebSocketUrl (request) { | ||
const source = new URL(request.url, 'http://127.0.0.1') | ||
const target = new URL( | ||
source.pathname.replace(fastify.prefix, rewritePrefix), | ||
options.upstream | ||
) | ||
target.search = source.search | ||
return target | ||
} | ||
} | ||
function generateRewritePrefix (prefix, opts) { | ||
if (!prefix) { | ||
return '' | ||
} | ||
let rewritePrefix = opts.rewritePrefix || new URL(opts.upstream).pathname | ||
if (!prefix.endsWith('/') && rewritePrefix.endsWith('/')) { | ||
rewritePrefix = rewritePrefix.slice(0, -1) | ||
} | ||
return rewritePrefix | ||
} | ||
async function httpProxy (fastify, opts) { | ||
@@ -114,3 +133,3 @@ if (!opts.upstream) { | ||
const preHandler = opts.preHandler || opts.beforeHandler | ||
const rewritePrefix = opts.rewritePrefix || '' | ||
const rewritePrefix = generateRewritePrefix(fastify.prefix, opts) | ||
@@ -171,3 +190,3 @@ const fromOpts = Object.assign({}, opts) | ||
if (opts.websocket) { | ||
setupWebSocketProxy(fastify, opts) | ||
setupWebSocketProxy(fastify, opts, rewritePrefix) | ||
} | ||
@@ -174,0 +193,0 @@ } |
{ | ||
"name": "fastify-http-proxy", | ||
"version": "4.3.0", | ||
"version": "4.3.1", | ||
"description": "proxy http requests, for Fastify", | ||
@@ -36,2 +36,3 @@ "main": "index.js", | ||
"fastify": "^3.0.0", | ||
"fastify-websocket": "^3.0.0", | ||
"got": "^11.5.1", | ||
@@ -51,3 +52,3 @@ "http-errors": "^1.8.0", | ||
"dependencies": { | ||
"fastify-reply-from": "^4.0.0", | ||
"fastify-reply-from": "^4.0.2", | ||
"ws": "^7.4.1" | ||
@@ -54,0 +55,0 @@ }, |
@@ -182,3 +182,3 @@ 'use strict' | ||
foo: 'bar', | ||
url: '/*', | ||
url: '/', | ||
method: [ | ||
@@ -185,0 +185,0 @@ 'DELETE', |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36626
20
889
21
Updatedfastify-reply-from@^4.0.2