Skip to content

Commit 2f695e5

Browse files
refactor(proxy): improve proxy factory implementation with lazy loading (#97)
* refactor(proxy): improve proxy factory implementation with lazy loading * refactor(proxy): rename proxy factory variables for clarity * chore(format): run code formatter
1 parent 1d4172d commit 2f695e5

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

lib/proxy-factory.js

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
'use strict'
22

3-
const fastProxy = require('fast-proxy-lite')
3+
module.exports = (() => {
4+
let fastProxyLite, httpLambdaProxy, fastProxyLegacy
45

5-
module.exports = ({ proxyType, opts, route }) => {
6-
let proxy
6+
return ({ proxyType, opts, route }) => {
7+
const base = opts.targetOverride || route.target
8+
const config = route.proxyConfig || {}
79

8-
if (proxyType === 'http') {
9-
proxy = fastProxy({
10-
base: opts.targetOverride || route.target,
11-
...route.proxyConfig
12-
}).proxy
13-
} else if (proxyType === 'lambda') {
14-
proxy = require('http-lambda-proxy')({
15-
target: opts.targetOverride || route.target,
16-
region: 'eu-central-1',
17-
...route.proxyConfig
18-
})
19-
} else if (proxyType === 'http-legacy') {
20-
proxy = require('fast-proxy')({
21-
base: opts.targetOverride || route.target,
22-
...route.proxyConfig
23-
}).proxy
24-
} else {
25-
throw new Error(`Unsupported proxy type: ${proxyType}!`)
26-
}
10+
switch (proxyType) {
11+
case 'http':
12+
fastProxyLite = fastProxyLite || require('fast-proxy-lite')
13+
return fastProxyLite({
14+
base,
15+
...config
16+
}).proxy
17+
18+
case 'lambda':
19+
httpLambdaProxy = httpLambdaProxy || require('http-lambda-proxy')
20+
return httpLambdaProxy({
21+
target: base,
22+
region: 'eu-central-1',
23+
...config
24+
})
2725

28-
return proxy
29-
}
26+
case 'http-legacy':
27+
fastProxyLegacy = fastProxyLegacy || require('fast-proxy')
28+
return fastProxyLegacy({
29+
base,
30+
...config
31+
}).proxy
32+
33+
default:
34+
throw new Error(`Unsupported proxy type: ${proxyType}!`)
35+
}
36+
}
37+
})()

0 commit comments

Comments
 (0)