From 60a9776487b859418aa77f671f1961cddcc6d742 Mon Sep 17 00:00:00 2001 From: Vincent de Vreede Date: Wed, 7 May 2025 11:10:33 +0200 Subject: [PATCH 1/3] refactor(proxy): improve proxy factory implementation with lazy loading --- lib/proxy-factory.js | 56 +++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/lib/proxy-factory.js b/lib/proxy-factory.js index 924475d..b6aa9f0 100644 --- a/lib/proxy-factory.js +++ b/lib/proxy-factory.js @@ -1,29 +1,37 @@ 'use strict' -const fastProxy = require('fast-proxy-lite') +module.exports = (() => { + let fastProxyLite, lambdaProxyFactory, legacyProxyFactory -module.exports = ({ proxyType, opts, route }) => { - let proxy + return ({ proxyType, opts, route }) => { + const base = opts.targetOverride || route.target + const config = route.proxyConfig || {} - if (proxyType === 'http') { - proxy = fastProxy({ - base: opts.targetOverride || route.target, - ...route.proxyConfig - }).proxy - } else if (proxyType === 'lambda') { - proxy = require('http-lambda-proxy')({ - target: opts.targetOverride || route.target, - region: 'eu-central-1', - ...route.proxyConfig - }) - } else if (proxyType === 'http-legacy') { - proxy = require('fast-proxy')({ - base: opts.targetOverride || route.target, - ...route.proxyConfig - }).proxy - } else { - throw new Error(`Unsupported proxy type: ${proxyType}!`) - } + switch (proxyType) { + case 'http': + fastProxyLite = fastProxyLite || require('fast-proxy-lite') + return fastProxyLite({ + base, + ...config + }).proxy + + case 'lambda': + lambdaProxyFactory = lambdaProxyFactory || require('http-lambda-proxy') + return lambdaProxyFactory({ + target: base, + region: 'eu-central-1', + ...config + }) - return proxy -} + case 'http-legacy': + legacyProxyFactory = legacyProxyFactory || require('fast-proxy') + return legacyProxyFactory({ + base, + ...config + }).proxy + + default: + throw new Error(`Unsupported proxy type: ${proxyType}!`) + } + } +})() \ No newline at end of file From 11ff227e4d56a6c709463e0409201e96f2ff371a Mon Sep 17 00:00:00 2001 From: Vincent de Vreede Date: Wed, 7 May 2025 11:21:47 +0200 Subject: [PATCH 2/3] refactor(proxy): rename proxy factory variables for clarity --- lib/proxy-factory.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/proxy-factory.js b/lib/proxy-factory.js index b6aa9f0..3b4c00a 100644 --- a/lib/proxy-factory.js +++ b/lib/proxy-factory.js @@ -1,7 +1,7 @@ 'use strict' module.exports = (() => { - let fastProxyLite, lambdaProxyFactory, legacyProxyFactory + let fastProxyLite, httpLambdaProxy, fastProxyLegacy return ({ proxyType, opts, route }) => { const base = opts.targetOverride || route.target @@ -16,16 +16,16 @@ module.exports = (() => { }).proxy case 'lambda': - lambdaProxyFactory = lambdaProxyFactory || require('http-lambda-proxy') - return lambdaProxyFactory({ + httpLambdaProxy = httpLambdaProxy || require('http-lambda-proxy') + return httpLambdaProxy({ target: base, region: 'eu-central-1', ...config }) case 'http-legacy': - legacyProxyFactory = legacyProxyFactory || require('fast-proxy') - return legacyProxyFactory({ + fastProxyLegacy = fastProxyLegacy || require('fast-proxy') + return fastProxyLegacy({ base, ...config }).proxy From 0d7f3d1b19d975458719a990d153c8472323570a Mon Sep 17 00:00:00 2001 From: Vincent de Vreede Date: Thu, 8 May 2025 15:05:08 +0200 Subject: [PATCH 3/3] chore(format): run code formatter --- lib/proxy-factory.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/proxy-factory.js b/lib/proxy-factory.js index 3b4c00a..d673ae8 100644 --- a/lib/proxy-factory.js +++ b/lib/proxy-factory.js @@ -34,4 +34,4 @@ module.exports = (() => { throw new Error(`Unsupported proxy type: ${proxyType}!`) } } -})() \ No newline at end of file +})()