forked from BackendStack21/fast-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (107 loc) · 3.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict'
/* eslint-disable no-useless-call */
const defaultProxyFactory = require('./lib/proxy-factory')
const restana = require('restana')
const defaultProxyHandler = (req, res, url, proxy, proxyOpts) => proxy(req, res, url, proxyOpts)
const DEFAULT_METHODS = require('restana/libs/methods').filter(method => method !== 'all')
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
opts = Object.assign({
middlewares: [],
pathRegex: '/*'
}, opts)
const router = opts.server || restana(opts.restana)
// registering global middlewares
opts.middlewares.forEach(middleware => {
router.use(middleware)
})
// registering services.json
const services = opts.routes.map(route => ({
prefix: route.prefix,
docs: route.docs
}))
router.get('/services.json', (req, res) => {
send(res, 200, services)
})
// processing websocket routes
const wsRoutes = opts.routes.filter(route => route.proxyType === 'websocket')
if (wsRoutes.length) {
if (typeof router.getServer !== 'function') {
throw new Error(
'Unable to retrieve the HTTP server instance. ' +
'If you are not using restana, make sure to provide an "app.getServer()" alternative method!'
)
}
registerWebSocketRoutes({
routes: wsRoutes,
server: router.getServer()
})
}
// processing non-websocket routes
opts.routes.filter(route => route.proxyType !== 'websocket').forEach(route => {
if (undefined === route.prefixRewrite) {
route.prefixRewrite = ''
}
// retrieve proxy type
const { proxyType = 'http' } = route
if (!PROXY_TYPES.includes(proxyType)) {
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]
// populating required NOOPS
route.hooks = route.hooks || {}
route.hooks.onRequest = route.hooks.onRequest || onRequestNoOp
route.hooks.onResponse = route.hooks.onResponse || onResponse
// populating route middlewares
route.middlewares = route.middlewares || []
// populating pathRegex if missing
route.pathRegex = undefined === route.pathRegex ? opts.pathRegex : String(route.pathRegex)
// instantiate route proxy
const proxy = proxyFactory({ opts, route, proxyType })
// route proxy handler function
const proxyHandler = route.proxyHandler || defaultProxyHandler
// populating timeout config
route.timeout = route.timeout || opts.timeout
// registering route handlers
const methods = route.methods || DEFAULT_METHODS
const args = [
// path
route.prefix + route.pathRegex,
// route middlewares
...route.middlewares,
// route handler
handler(route, proxy, proxyHandler)
]
methods.forEach(method => {
method = method.toLowerCase()
if (router[method]) {
router[method].apply(router, args)
}
})
})
return router
}
const handler = (route, proxy, proxyHandler) => async (req, res, next) => {
try {
req.url = route.urlRewrite
? route.urlRewrite(req)
: req.url.replace(route.prefix, route.prefixRewrite)
const shouldAbortProxy = await route.hooks.onRequest(req, res)
if (!shouldAbortProxy) {
const proxyOpts = Object.assign({
request: {
timeout: req.timeout || route.timeout
},
queryString: route.disableQsOverwrite ? null : req.query
}, route.hooks)
proxyHandler(req, res, req.url, proxy, proxyOpts)
}
} catch (err) {
return next(err)
}
}
module.exports = gateway