From 313ba2b3900032ad3e214b314b2db769294e1248 Mon Sep 17 00:00:00 2001 From: Vincent de Vreede Date: Wed, 7 May 2025 09:42:09 +0200 Subject: [PATCH 1/4] fix(gateway): allow custom proxyFactory to use non-default proxyTypes - Skip proxyType validation when a custom proxyFactory is provided - Fallback to defaultProxyFactory only if custom one returns undefined - Default missing proxy hooks to NOOP functions --- index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 246450d..d0d7ffe 100644 --- a/index.js +++ b/index.js @@ -9,12 +9,13 @@ const defaultProxyHandler = (req, res, url, proxy, proxyOpts) => const DEFAULT_METHODS = require('restana/libs/methods').filter( (method) => method !== 'all' ) +const NOOP = (req, res) => {}; const send = require('@polka/send-type') const PROXY_TYPES = ['http', 'lambda'] const registerWebSocketRoutes = require('./lib/ws-proxy') const gateway = (opts) => { - const proxyFactory = opts.proxyFactory || defaultProxyFactory + const proxyFactory = (...args) => opts.proxyFactory?.(...args) ?? defaultProxyFactory(...args); opts = Object.assign( { @@ -67,16 +68,16 @@ const gateway = (opts) => { // retrieve proxy type const { proxyType = 'http' } = route - if (!PROXY_TYPES.includes(proxyType)) { + const isDefaultProxyType = PROXY_TYPES.includes(proxyType); + if (!opts.proxyFactory && !isDefaultProxyType) { throw new Error( 'Unsupported proxy type, expecting one of ' + PROXY_TYPES.toString() ) } // retrieve default hooks for proxy - const { onRequestNoOp, onResponse } = require('./lib/default-hooks')[ - proxyType - ] + const hooksForDefaultType = isDefaultProxyType ? require('./lib/default-hooks')[proxyType] : {}; + const { onRequestNoOp = NOOP, onResponse = NOOP } = hooksForDefaultType; // populating required NOOPS route.hooks = route.hooks || {} From d3cb1953f3bd450b0f2cb4a6637f73e28ebe730d Mon Sep 17 00:00:00 2001 From: Vincent de Vreede Date: Wed, 7 May 2025 10:33:15 +0200 Subject: [PATCH 2/4] fix(gateway): improve proxyFactory logic to handle undefined responses --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index d0d7ffe..90dc3ef 100644 --- a/index.js +++ b/index.js @@ -15,7 +15,7 @@ const PROXY_TYPES = ['http', 'lambda'] const registerWebSocketRoutes = require('./lib/ws-proxy') const gateway = (opts) => { - const proxyFactory = (...args) => opts.proxyFactory?.(...args) ?? defaultProxyFactory(...args); + const proxyFactory = opts.proxyFactory ? (...args) => ((r) => r === undefined ? defaultProxyFactory(...args) : r)(opts.proxyFactory(...args)) : defaultProxyFactory; opts = Object.assign( { From cb8bf82196b703217a88e654e0148ba810cc3347 Mon Sep 17 00:00:00 2001 From: Vincent de Vreede Date: Thu, 8 May 2025 15:06:07 +0200 Subject: [PATCH 3/4] chore(format): run code formatter --- index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 90dc3ef..2149378 100644 --- a/index.js +++ b/index.js @@ -9,13 +9,13 @@ const defaultProxyHandler = (req, res, url, proxy, proxyOpts) => const DEFAULT_METHODS = require('restana/libs/methods').filter( (method) => method !== 'all' ) -const NOOP = (req, res) => {}; +const NOOP = (req, res) => {} const send = require('@polka/send-type') const PROXY_TYPES = ['http', 'lambda'] const registerWebSocketRoutes = require('./lib/ws-proxy') const gateway = (opts) => { - const proxyFactory = opts.proxyFactory ? (...args) => ((r) => r === undefined ? defaultProxyFactory(...args) : r)(opts.proxyFactory(...args)) : defaultProxyFactory; + const proxyFactory = opts.proxyFactory ? (...args) => ((r) => r === undefined ? defaultProxyFactory(...args) : r)(opts.proxyFactory(...args)) : defaultProxyFactory opts = Object.assign( { @@ -68,7 +68,7 @@ const gateway = (opts) => { // retrieve proxy type const { proxyType = 'http' } = route - const isDefaultProxyType = PROXY_TYPES.includes(proxyType); + const isDefaultProxyType = PROXY_TYPES.includes(proxyType) if (!opts.proxyFactory && !isDefaultProxyType) { throw new Error( 'Unsupported proxy type, expecting one of ' + PROXY_TYPES.toString() @@ -76,8 +76,8 @@ const gateway = (opts) => { } // retrieve default hooks for proxy - const hooksForDefaultType = isDefaultProxyType ? require('./lib/default-hooks')[proxyType] : {}; - const { onRequestNoOp = NOOP, onResponse = NOOP } = hooksForDefaultType; + const hooksForDefaultType = isDefaultProxyType ? require('./lib/default-hooks')[proxyType] : {} + const { onRequestNoOp = NOOP, onResponse = NOOP } = hooksForDefaultType // populating required NOOPS route.hooks = route.hooks || {} From bc6d5a497a269d27a63ffefe253a2f62e8b45c99 Mon Sep 17 00:00:00 2001 From: Vincent de Vreede Date: Thu, 8 May 2025 15:26:01 +0200 Subject: [PATCH 4/4] docs: enhance README with detailed proxyFactory behavior explanation --- docs/README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 994807d..b1c9207 100644 --- a/docs/README.md +++ b/docs/README.md @@ -131,8 +131,12 @@ module.exports.handler = serverless(service) timeout: 0, // Optional "target" value that overrides the routes "target" config value. Feature intended for testing purposes. targetOverride: "https://yourdev.api-gateway.com", - // Optional "Proxy Factory" implementation, allows the integration of custom proxying strategies. - // Default value: require('fast-proxy-lite/lib/proxy-factory') + // Optional "Proxy Factory" implementation, allows integration of custom proxying strategies. + // Behavior: + // - If it returns any value (e.g. a custom proxy), that value will be used directly. + // - If it returns `undefined` (or does not return anything), the default factory from `fast-gateway` will be used as a fallback. + // - If it returns `null`, no proxy will be used and the default factory will be skipped entirely. + // Default: the built-in proxy factory from `fast-gateway` proxyFactory: ({ proxyType, opts, route }) => {...} // HTTP proxy