-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
77 lines (62 loc) · 2.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
'use strict'
const fp = require('fastify-plugin')
const kIgnore = Symbol('fastify-405:ignoreOnRoute')
const HTTP_METHODS = ['GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'PATCH', 'TRACE']
function fourOhfive (fastify, opts, next) {
const options = Object.assign({
regexp: /.*/,
allow: ['GET', 'POST']
}, opts)
if (!(options.regexp instanceof RegExp)) {
next(new Error('Options.regexp must be a regular expression'))
return
}
if (!Array.isArray(options.allow) || !options.allow.reduce((acc, m) => acc && HTTP_METHODS.includes(m), true)) {
next(new Error(`Options.allow must be an array with only these values: ${HTTP_METHODS}`))
return
}
const unAllow = HTTP_METHODS.filter(_ => !options.allow.includes(_))
const preAllow = options.allow.join(', ')
const registered = new Map()
fastify.addHook('onRoute', function support405 (routeOptions) {
if ((routeOptions.config || {}).ignore === kIgnore) {
return
}
fastify.log.trace('Evaluating [%o] [%s] url', routeOptions.method, routeOptions.url)
const is405Url = options.regexp.test(routeOptions.url)
if (!is405Url) {
fastify.log.debug('Reject url [%s] not match the regexp [%s]', routeOptions.url, options.regexp)
return
}
let method
if (typeof routeOptions.method === 'string') {
method = routeOptions.method
} else {
method = routeOptions.method[0]
}
const isAllowed = options.allow.includes(method)
const isAlreadyRegistered = registered.has(routeOptions.url)
if (!isAllowed || isAlreadyRegistered) {
fastify.log.trace('Url [%s] already registered for 405 methods %o', routeOptions.url, unAllow)
return
}
registered.set(routeOptions.url, true)
fastify.log.debug('Adding 405 routes for [%s] with methods %o', routeOptions.url, unAllow)
fastify.route({
method: unAllow,
path: routeOptions.routePath,
config: { ignore: kIgnore },
handler: (req, reply) => {
reply
.code(405)
.header('allow', preAllow)
.send()
}
})
})
next()
}
module.exports = fp(fourOhfive, {
fastify: '^5',
name: 'fastify-405'
})