Skip to content

Commit 26147ba

Browse files
committed
supporting timeout and proxyHandler configs
1 parent 2f5000a commit 26147ba

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fastProxy = require('fast-proxy')
22
const restana = require('restana')
33
const pump = require('pump')
44
const toArray = require('stream-to-array')
5+
const defaultProxyHandler = (req, res, url, proxy, proxyOpts) => proxy(req, res, url, proxyOpts)
56

67
const gateway = (opts) => {
78
opts = Object.assign({
@@ -48,19 +49,31 @@ const gateway = (opts) => {
4849
...(opts.fastProxy)
4950
})
5051

52+
// route proxy handler function
53+
const proxyHandler = route.proxyHandler || defaultProxyHandler
54+
55+
// populating timeout config
56+
route.timeout = route.timeout || opts.timeout
57+
5158
// registering route handler
5259
const methods = route.methods || ['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS']
53-
server.route(methods, route.prefix + route.pathRegex, handler(route, proxy), null, route.middlewares)
60+
server.route(methods, route.prefix + route.pathRegex, handler(route, proxy, proxyHandler), null, route.middlewares)
5461
})
5562

5663
return server
5764
}
5865

59-
const handler = (route, proxy) => async (req, res) => {
66+
const handler = (route, proxy, proxyHandler) => async (req, res) => {
6067
req.url = req.url.replace(route.prefix, route.prefixRewrite)
6168
const shouldAbortProxy = await route.hooks.onRequest(req, res)
6269
if (!shouldAbortProxy) {
63-
proxy(req, res, req.url, Object.assign({}, route.hooks))
70+
const proxyOpts = Object.assign({
71+
request: {
72+
timeout: req.timeout || route.timeout
73+
}
74+
}, route.hooks)
75+
76+
proxyHandler(req, res, req.url, proxy, proxyOpts)
6477
}
6578
}
6679

0 commit comments

Comments
 (0)