Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-http-proxy

Package Overview
Dependencies
Maintainers
13
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-http-proxy - npm Package Compare versions

Comparing version 4.3.0 to 4.3.1

test/ws-prefix-rewrite.js

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',

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc