generated from Eomm/fastify-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
200 lines (173 loc) · 5.83 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
'use strict'
const fp = require('fastify-plugin')
const getSource = require('./lib/source-code')
const kTrackerMe = Symbol('fastify-overview.track-me')
const kStructure = Symbol('fastify-overview.structure')
const kSourceRegister = Symbol('fastify-overview.source.register')
const kSourceRoute = Symbol('fastify-overview.source.route')
const {
transformRoute,
getDecoratorNode,
getPluginNode,
getHookNode,
filterStructure
} = require('./lib/utils')
function fastifyOverview (fastify, options, next) {
const opts = Object.assign({
addSource: false
}, options)
const contextMap = new Map()
let structure
fastify.addHook('onRegister', function markInstance (instance) {
const parent = Object.getPrototypeOf(instance)
// this is the `avvio` instance
manInTheMiddle.call(this, instance, parent[kTrackerMe])
})
fastify.addHook('onRoute', function markRoute (routeOpts) {
const routeNode = Object.assign(transformRoute(routeOpts), opts.onRouteDefinition?.(routeOpts))
if (opts.addSource) {
routeNode.source = routeOpts.handler[kSourceRoute]
// the hooks added using the route options, does not have the `source` property
// so we can use the same as the route handler
const hooksKey = Object.keys(routeNode.hooks)
for (const hookKey of hooksKey) {
routeNode.hooks[hookKey].forEach(hookNode => {
hookNode.source = routeNode.source
})
}
}
this[kStructure].routes.push(routeNode)
})
fastify.addHook('onReady', function hook (done) {
const root = contextMap.get(rootToken)
structure = root
contextMap.clear()
done(null)
})
fastify.decorate('overview', function getOverview (opts) {
if (!structure) {
throw new Error('Fastify must be in ready status to access the overview')
}
if (opts?.hideEmpty || opts?.routesFilter) {
return filterStructure(structure, opts)
}
return structure
})
const rootToken = manInTheMiddle(fastify)
wrapFastify(fastify, opts)
if (opts.exposeRoute === true) {
const routeConfig = Object.assign(
{
method: 'GET',
exposeHeadRoute: false,
url: '/json-overview'
},
opts.exposeRouteOptions,
{ handler: getJsonOverview })
fastify.route(routeConfig)
}
next()
function manInTheMiddle (instance, parentId) {
const trackingToken = Math.random()
instance[kTrackerMe] = trackingToken
const trackStructure = getPluginNode(trackingToken, instance.pluginName)
if (opts.addSource && this) {
trackStructure.source = this._current.find(loadPipe => loadPipe.func[kSourceRegister] !== undefined).func[kSourceRegister]
}
contextMap.set(trackingToken, trackStructure)
instance[kStructure] = trackStructure
if (parentId) {
contextMap.get(parentId).children.push(trackStructure)
}
return trackingToken
}
}
/**
* this function is executed only once: when the plugin is registered.
* if it is executed more than once, the output structure will have duplicated
* entries.
* this is caused by the fact that the wrapDecorate will call wrapDecorate again and so on.
* Running the code only the first time relies on the Fastify prototype chain.
*
* The key here is to use the this[kStructure] property to get the right structure to update.
*/
function wrapFastify (instance, pluginOpts) {
// *** decorators
wrapDecorator(instance, 'decorate', pluginOpts)
wrapDecorator(instance, 'decorateRequest', pluginOpts)
wrapDecorator(instance, 'decorateReply', pluginOpts)
// *** register
const originalRegister = instance.register
instance.register = function wrapRegister (pluginFn, opts) {
if (pluginOpts.addSource) {
// this Symbol is processed by the `onRegister` hook if necessary
pluginFn[kSourceRegister] = getSource()[0]
}
return originalRegister.call(this, pluginFn, opts)
}
// *** routes
;[
'delete',
'get',
'head',
'patch',
'post',
'put',
'options',
'all'
].forEach(shortcut => {
const originalMethod = instance[shortcut]
instance[shortcut] = function wrapRoute (url, opts, handler) {
if (pluginOpts.addSource) {
// this Symbol is processed by the `onRoute` hook
getRouteHandler(url, opts, handler)[kSourceRoute] = getSource()[0]
}
return originalMethod.call(this, url, opts, handler)
}
})
const originalRoute = instance.route
instance.route = function wrapRoute (routeOpts) {
if (pluginOpts.addSource) {
// this Symbol is processed by the `onRoute` hook
routeOpts.handler[kSourceRoute] = getSource()[0]
}
return originalRoute.call(this, routeOpts)
}
// *** hooks
const originalHook = instance.addHook
instance.addHook = function wrapAddHook (name, hook) {
const hookNode = getHookNode(hook)
if (pluginOpts.addSource) {
hookNode.source = getSource()[0]
}
this[kStructure].hooks[name].push(hookNode)
return originalHook.call(this, name, hook)
}
}
function wrapDecorator (instance, type, { addSource, onDecorateDefinition }) {
const originalDecorate = instance[type]
instance[type] = function wrapDecorate (name, value) {
const decoratorNode = Object.assign(getDecoratorNode(name, value), onDecorateDefinition?.(type, name, value))
if (addSource) {
decoratorNode.source = getSource()[0]
}
this[kStructure].decorators[type].push(decoratorNode)
return originalDecorate.call(this, name, value)
}
}
function getRouteHandler (url, options, handler) {
if (!handler && typeof options === 'function') {
handler = options
}
return handler || (options && options.handler)
}
function getJsonOverview (request, reply) {
return this.overview()
}
const plugin = fp(fastifyOverview, {
name: 'fastify-overview',
fastify: '^4.23.x'
})
module.exports = plugin
module.exports.default = plugin
module.exports.fastifyOverview = plugin