-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
214 lines (195 loc) · 6.14 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
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
209
210
211
212
213
214
import {hostname as osHostname} from 'node:os'
import express from 'express'
import compression from 'compression'
import hsts from 'hsts'
import pino from 'pino'
import createCors from 'cors'
import onHeaders from 'on-headers'
import {getAllRoutes as getRoutes} from './routes/index.js'
import {routeUriTemplate} from './lib/route-uri-template.js'
import {formatLinkHeader as linkHeader} from './lib/link-header.js'
import {setOpenapiLink, serveOpenapiSpec} from './lib/openapi-spec.js'
const REQ_START_TIME = Symbol.for('request-start-time')
const defaultConfig = {
hostname: osHostname(),
cors: true,
etags: 'weak',
csp: `default-src 'none'`,
handleErrors: true,
openapiSpec: false,
aboutPage: true,
logging: false,
healthCheck: null,
mapRouteParsers: (route, parsers) => parsers,
mapRouteOpenapiPaths: (route, openapiPaths) => openapiPaths,
addHafasOpts: () => {},
modifyRoutes: routes => routes,
}
const assertNonEmptyString = (cfg, key) => {
if ('string' !== typeof cfg[key]) {
throw new Error(`config.${key} must be a string`)
}
if (!cfg[key]) throw new Error(`config.${key} must not be empty`)
}
const assertBoolean = (cfg, key) => {
if ('boolean' !== typeof cfg[key]) {
throw new Error(`config.${key} must be a boolean`)
}
}
const createHafasRestApi = async (hafas, config, attachMiddleware) => {
config = Object.assign({}, defaultConfig, config)
// mandatory
assertNonEmptyString(config, 'hostname')
assertNonEmptyString(config, 'name')
// optional
if ('cors' in config) assertBoolean(config, 'cors')
if ('handleErrors' in config) assertBoolean(config, 'handleErrors')
if ('logging' in config) assertBoolean(config, 'logging')
if (config.healthCheck !== null && 'function' !== typeof config.healthCheck) {
throw new Error('cfg.healthCheck must be a function')
}
if ('version' in config) assertNonEmptyString(config, 'version')
if ('homepage' in config) assertNonEmptyString(config, 'homepage')
if ('aboutPage' in config) assertBoolean(config, 'aboutPage')
if ('description' in config) assertNonEmptyString(config, 'description')
if ('docsLink' in config) assertNonEmptyString(config, 'docsLink')
if ('function' !== typeof config.mapRouteParsers) {
throw new Error('cfg.mapRouteParsers must be a function')
}
if ('function' !== typeof config.mapRouteOpenapiPaths) {
throw new Error('cfg.mapRouteOpenapiPaths must be a function')
}
const api = express()
api.locals.config = config
api.locals.logger = pino({
redact: {
paths: [
'err.request', 'err.response',
],
remove: true,
},
})
if (config.cors) {
const cors = createCors({
exposedHeaders: '*',
maxAge: 24 * 60 * 60, // 1 day
})
api.options('*', cors)
api.use(cors)
}
api.set('etag', config.etags)
if (config.logging) {
const {
createLoggingMiddleware: createLogging,
} = await import('./logging.js')
api.use(createLogging(api.locals.logger))
}
api.use(compression())
api.use(hsts({
maxAge: 10 * 24 * 60 * 60
}))
api.use((req, res, next) => {
res.setLinkHeader = (linkSpec) => {
const link = linkHeader(res.getHeader('Link'), linkSpec)
res.setHeader('Link', link)
}
req.searchWithNewParams = (newParams) => {
const u = new URL(req.url, 'http://example.org')
for (const [name, val] of Object.entries(newParams)) {
if (val === null) u.searchParams.delete(name)
else u.searchParams.set(name, val)
}
return u.search
}
res.allowCachingFor = (sec) => {
if (!Number.isInteger(sec)) {
throw new Error('sec is invalid')
}
// Allow clients to use the cache when re-fetching fails
// for another `sec` seconds after expiry.
res.setHeader('cache-control', `public, max-age: ${sec}, s-maxage: ${sec}, stale-if-error=${sec}`)
// Allow CDNs to cache for another `sec` seconds while
// they're re-fetching the latest copy.
res.setHeader('surrogate-control', `stale-while-revalidate=${sec}`)
}
res.serverTiming = Object.create(null)
res[REQ_START_TIME] = process.hrtime()
onHeaders(res, () => {
const t = Object.entries(res.serverTiming)
const dt = process.hrtime(res[REQ_START_TIME])
t.push(['total', Math.round(dt[0] * 1e3 + dt[1] / 1e6)])
const h = t.map(([name, dur]) => name + ';dur=' + dur).join(', ')
res.setHeader('server-timing', h)
})
if (!res.headersSent) {
// https://helmetjs.github.io/docs/dont-sniff-mimetype/
res.setHeader('X-Content-Type-Options', 'nosniff')
res.setHeader('content-security-policy', config.csp)
res.setHeader('X-Powered-By', [
config.name, config.version, config.homepage
].filter(str => !!str).join(' '))
if (config.version) res.setHeader('X-API-Version', config.version)
if (config.openapiSpec) setOpenapiLink(res)
}
next()
})
if (attachMiddleware) attachMiddleware(api)
if (config.healthCheck) {
api.get('/health', (req, res, next) => {
res.setHeader('cache-control', 'no-store')
res.setHeader('expires', '0')
try {
config.healthCheck()
.then((isHealthy) => {
if (isHealthy === true) {
res.status(200)
res.json({ok: true})
} else {
res.status(502)
res.json({ok: false})
}
}, next)
} catch (err) {
next(err)
}
})
}
if (config.aboutPage) {
const {
createAboutPageRoute: aboutPage,
} = await import('./about-page.js')
api.get('/', aboutPage(config.name, config.description, config.docsLink))
}
if (config.docsAsMarkdown) {
const {
createDocsRoute: docs,
} = await import('./docs.js')
api.get('/docs', docs(config))
}
const _routes = await getRoutes(hafas, config)
const routes = config.modifyRoutes(_routes, hafas, config)
api.routes = routes
for (const [path, route] of Object.entries(routes)) {
api.get(path, route)
}
if (config.openapiSpec) serveOpenapiSpec(api)
const rootLinks = {}
for (const [path, route] of Object.entries(routes)) {
rootLinks[route.name + 'Url'] = routeUriTemplate(path, route)
}
api.get('/', (req, res, next) => {
if (!req.accepts('json')) return next()
if (res.headersSent) return next()
res.json(rootLinks)
})
if (config.handleErrors) {
const {
createErrorHandler: handleErrors,
} = await import('./handle-errors.js')
api.use(handleErrors(api.locals.logger))
}
return api
}
export {
createHafasRestApi,
}