-
Notifications
You must be signed in to change notification settings - Fork 116
/
router.js
208 lines (179 loc) · 5.95 KB
/
router.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict'
const router = module.exports = { }
const _ = {
assign: require('lodash.assign'),
omit: require('lodash.omit')
}
const express = require('express')
let app
let server
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const jsonApi = require('./jsonApi.js')
const debug = require('./debugging.js')
const responseHelper = require('./responseHelper.js')
const url = require('url')
const metrics = require('./metrics.js')
const graphQl = require('./graphQl/index.js')
router.applyMiddleware = () => {
app = app || jsonApi._apiConfig.router || express()
app.use((req, res, next) => {
res.set({
'Content-Type': 'application/vnd.api+json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PATCH, DELETE, OPTIONS',
'Access-Control-Allow-Headers': req.headers['access-control-request-headers'] || '',
'Cache-Control': 'private, must-revalidate, max-age=0',
'Expires': 'Thu, 01 Jan 1970 00:00:00'
})
if (req.method === 'OPTIONS') {
return res.status(204).end()
}
return next()
})
app.use((req, res, next) => {
if (!req.headers['content-type'] && !req.headers.accept) return next()
if (req.headers['content-type']) {
// 415 Unsupported Media Type
if (req.headers['content-type'].match(/^application\/vnd\.api\+json;.+$/)) {
return res.status(415).end(`HTTP 415 Unsupported Media Type - [${req.headers['content-type']}]`)
}
// Convert "application/vnd.api+json" content type to "application/json".
// This enables the express body parser to correctly parse the JSON payload.
if (req.headers['content-type'].match(/^application\/vnd\.api\+json$/)) {
req.headers['content-type'] = 'application/json'
}
}
if (req.headers.accept) {
// 406 Not Acceptable
let matchingTypes = req.headers.accept.split(/, ?/)
matchingTypes = matchingTypes.filter(mediaType => // Accept application/*, */vnd.api+json, */* and the correct JSON:API type.
mediaType.match(/^(\*|application)\/(\*|json|vnd\.api\+json)$/) || mediaType.match(/\*\/\*/))
if (matchingTypes.length === 0) {
return res.status(406).end()
}
}
return next()
})
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cookieParser())
if (!jsonApi._apiConfig.router) {
app.disable('x-powered-by')
app.disable('etag')
}
graphQl.with(app)
let requestId = 0
app.route('*').all((req, res, next) => {
debug.requestCounter(requestId++, req.method, req.url)
if (requestId > 1000) requestId = 0
next()
})
}
router.listen = port => {
if (!server) {
if (jsonApi._apiConfig.protocol === 'https') {
server = require('https').createServer(jsonApi._apiConfig.tls || {}, app)
} else {
server = require('http').createServer(app)
}
server.listen(port)
}
}
router.close = () => {
if (server) {
server.close()
server = null
}
}
router._routes = { }
router.bindRoute = (config, callback) => {
const path = jsonApi._apiConfig.base + config.path
const verb = config.verb.toLowerCase()
const routeHandler = (req, res, extras) => {
let request = router._getParams(req)
request = _.assign(request, extras)
const resourceConfig = jsonApi._resources[request.params.type]
request.resourceConfig = resourceConfig
res._request = request
res._startDate = new Date()
router.authenticate(request, res, () => callback(request, resourceConfig, res))
}
router._routes[verb] = router._routes[verb] || { }
router._routes[verb][config.path] = routeHandler
app[verb](path, routeHandler)
}
router.authenticate = (request, res, callback) => {
if (!router._authFunction) return callback()
router._authFunction(request, err => {
if (!err) return callback()
const errorWrapper = {
status: '401',
code: 'UNAUTHORIZED',
title: 'Authentication Failed',
detail: err || 'You are not authorised to access this resource.'
}
const payload = responseHelper.generateError(request, errorWrapper)
res.status(401).end(Buffer.from(JSON.stringify(payload)))
})
}
router.authenticateWith = authFunction => {
router._authFunction = authFunction
}
router.bind404 = callback => {
app.use((req, res) => {
const request = router._getParams(req)
return callback(request, res)
})
}
router.bindErrorHandler = callback => {
app.use((error, req, res, next) => {
const request = router._getParams(req)
return callback(request, res, error, next)
})
}
router._getParams = req => {
let urlParts = req.url.split(jsonApi._apiConfig.base)
urlParts.shift()
urlParts = urlParts.join(jsonApi._apiConfig.base).split('?')
const headersToRemove = [
'host', 'connection', 'accept-encoding', 'accept-language', 'content-length'
]
let combined
let reqUrl = req.url
if (jsonApi._apiConfig.urlPrefixAlias) {
combined = jsonApi._apiConfig.urlPrefixAlias.replace(/\/$/, '')
reqUrl = reqUrl.replace(jsonApi._apiConfig.base, '/')
} else {
combined = url.format({
protocol: jsonApi._apiConfig.protocol,
hostname: jsonApi._apiConfig.hostname,
port: jsonApi._apiConfig.port
})
}
combined += reqUrl
return {
params: _.assign(req.params, req.body, req.query),
headers: req.headers,
safeHeaders: _.omit(req.headers, headersToRemove),
cookies: req.cookies,
originalUrl: req.originalUrl,
route: {
verb: req.method,
host: req.headers.host,
base: jsonApi._apiConfig.base,
path: urlParts.shift() || '',
query: urlParts.shift() || '',
combined
}
}
}
router.sendResponse = (res, payload, httpCode) => {
const timeDiff = (new Date()) - res._startDate
metrics.processResponse(res._request, httpCode, payload, timeDiff)
res.status(httpCode).end(Buffer.from(JSON.stringify(payload)))
}
router.getExpressServer = () => {
app = app || jsonApi._apiConfig.router || express()
return app
}