newrelic 9.4.0 → 9.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,254 +0,0 @@
1
- /*
2
- * Copyright 2020 New Relic Corporation. All rights reserved.
3
- * SPDX-License-Identifier: Apache-2.0
4
- */
5
-
6
- 'use strict'
7
-
8
- const shared = require('./hapi/shared')
9
-
10
- module.exports = function initialize(agent, hapi, moduleName, shim) {
11
- if (!agent || !hapi || !shim) {
12
- shim &&
13
- shim.logger.debug(
14
- 'Hapi instrumentation function called with incorrect arguments, not instrumenting.'
15
- )
16
- return false
17
- }
18
-
19
- shim.setFramework(shim.HAPI)
20
-
21
- shim.setErrorPredicate(function hapiErrorPredicate(err) {
22
- return err instanceof Error
23
- })
24
-
25
- if (hapi.createServer) {
26
- wrapCreateServer(shim, hapi)
27
- } else if (hapi.Server) {
28
- // Server.connection was removed in v17
29
- if (!hapi.Server.prototype.connection) {
30
- return require('./hapi/hapi')(agent, hapi, moduleName, shim)
31
- }
32
- }
33
- }
34
-
35
- function wrapServer(shim, Server) {
36
- // wrap server.handler function that registers a new handler type
37
- // the second argument is expected to be a function that generates the handler function
38
- shim.wrap(Server.prototype, 'handler', function wrapHandler(shim, original) {
39
- return function wrappedHandler() {
40
- const args = shim.argsToArray.apply(shim, arguments)
41
-
42
- const handlerGenerator = args[1]
43
- if (typeof handlerGenerator === 'function') {
44
- args[1] = wrapGenerator(handlerGenerator)
45
- }
46
-
47
- return original.apply(this, args)
48
-
49
- function wrapGenerator(generator) {
50
- function wrappedGenerator() {
51
- const generatorArgs = shim.argsToArray.apply(shim, arguments)
52
- const handler = generator.apply(this, generatorArgs)
53
- if (typeof handler === 'function') {
54
- const route = generatorArgs[0]
55
- return wrapRouteHandler(shim, handler, route && route.path)
56
- }
57
- return handler
58
- }
59
-
60
- wrappedGenerator.defaults = generator.defaults
61
-
62
- return wrappedGenerator
63
- }
64
- }
65
- })
66
-
67
- shim.wrap(Server.prototype, 'route', function wrapRoute(shim, original) {
68
- return function wrappedRoute() {
69
- const args = shim.argsToArray.apply(shim, arguments)
70
-
71
- // first argument is expected to be the route configuration object
72
- if (!shim.isObject(args[0])) {
73
- return original.apply(this, args)
74
- }
75
-
76
- // If route is created via a plugin, pull prefix if it exists
77
- const prefix =
78
- (this.realm &&
79
- this.realm.modifiers &&
80
- this.realm.modifiers.route &&
81
- this.realm.modifiers.route.prefix) ||
82
- ''
83
-
84
- _wrapRoute(shim, args[0])
85
-
86
- return original.apply(this, args)
87
-
88
- function _wrapRoute(shim, route) {
89
- const routePath = prefix + route.path
90
- // handler function could be on the route object, or on a nested config object
91
- if (shim.isArray(route)) {
92
- for (let i = 0; i < route.length; ++i) {
93
- _wrapRoute(shim, route[i])
94
- }
95
- return
96
- } else if (route.config) {
97
- if (route.config.pre) {
98
- // config objects can also contain multiple OTHER handlers in a `pre` array
99
- route.config.pre = wrapPreHandlers(shim, route.config.pre, routePath)
100
- }
101
- if (route.config.handler) {
102
- wrapRouteHandler(shim, route.config, routePath)
103
- return
104
- }
105
- }
106
- wrapRouteHandler(shim, route, routePath)
107
- }
108
- }
109
- })
110
-
111
- shim.wrap(Server.prototype, 'ext', function wrapExt(shim, original) {
112
- return function wrappedExt(event, method) {
113
- const args = shim.argsToArray.apply(shim, arguments)
114
-
115
- if (shim.isArray(event)) {
116
- for (let i = 0; i < event.length; i++) {
117
- event[i].method = wrapMiddleware(shim, event[i].method, event[i].type)
118
- }
119
- } else if (shim.isObject(event)) {
120
- event.method = wrapMiddleware(shim, event.method, event.type)
121
- } else if (shim.isString(event)) {
122
- args[1] = wrapMiddleware(shim, method, event)
123
- } else {
124
- shim.logger.debug('Unsupported event type %j', event)
125
- return
126
- }
127
-
128
- return original.apply(this, args)
129
- }
130
- })
131
- }
132
-
133
- function wrapCreateServer(shim, hapi) {
134
- shim.wrap(hapi, 'createServer', function getWrapper(shim, createServer) {
135
- return function createServerWrapper() {
136
- const server = createServer.apply(this, arguments)
137
- wrapServer(shim, server.constructor)
138
- shim.unwrap(hapi, 'createServer')
139
- return server
140
- }
141
- })
142
- }
143
-
144
- function wrapPreHandlers(shim, container, path) {
145
- if (shim.isArray(container)) {
146
- for (let i = 0; i < container.length; ++i) {
147
- container[i] = wrapPreHandlers(shim, container[i], path)
148
- }
149
- return container
150
- } else if (shim.isFunction(container)) {
151
- return _wrapPreHandler(container)
152
- } else if (container.method && shim.isFunction(container.method)) {
153
- return shim.wrap(container, 'method', function wrapHandler(shim, handler) {
154
- return _wrapPreHandler(handler)
155
- })
156
- }
157
- // The 'pre' option also allows strings pointing to methods registered via
158
- // server.method() (ie: 'methodName(args)'). For the most part, these should
159
- // be simple utility functions, but may be something we want to wrap in the future.
160
- return container
161
-
162
- function _wrapPreHandler(handler) {
163
- return shim.recordMiddleware(
164
- wrapHapiHandler(shim, handler),
165
- buildMiddlewareSpec(shim, path, true)
166
- )
167
- }
168
- }
169
-
170
- function wrapRouteHandler(shim, container, path) {
171
- if (shim.isFunction(container)) {
172
- return _wrapRouteHandler(container)
173
- } else if (container.handler && shim.isFunction(container.handler)) {
174
- return shim.wrap(container, 'handler', function wrapHandler(shim, handler) {
175
- return _wrapRouteHandler(handler)
176
- })
177
- }
178
-
179
- function _wrapRouteHandler(handler) {
180
- return shim.recordMiddleware(wrapHapiHandler(shim, handler), buildMiddlewareSpec(shim, path))
181
- }
182
- }
183
-
184
- function buildMiddlewareSpec(shim, path, isPreHandler) {
185
- return {
186
- route: path,
187
- req: function getReq(shim, fn, fnName, args) {
188
- const request = args[0]
189
- if (request && request.raw) {
190
- return request.raw.req
191
- }
192
- },
193
- next: function wrapNext(shim, fn, fnName, args, wrap) {
194
- const reply = args[1]
195
- if (!shim.isFunction(reply)) {
196
- return
197
- }
198
- wrapReply(wrap, reply, isPreHandler)
199
- },
200
- params: function getParams(shim, fn, fnName, args) {
201
- const req = args[0]
202
- return req && req.params
203
- }
204
- }
205
- }
206
-
207
- function wrapHapiHandler(shim, handler) {
208
- return shim.wrap(handler, function wrapHandler(shim, original) {
209
- return function wrapped() {
210
- const reply = arguments[1]
211
- if (reply) {
212
- shim.recordRender(reply, 'view')
213
- }
214
- return original.apply(this, arguments)
215
- }
216
- })
217
- }
218
-
219
- function wrapMiddleware(shim, middleware, event) {
220
- if (!shared.ROUTE_EVENTS[event]) {
221
- return middleware
222
- }
223
-
224
- const spec = {
225
- route: event,
226
- type: event === 'onPreResponse' ? shim.ERRORWARE : shim.MIDDLEWARE,
227
- next: function wrapNext(shim, fn, fnName, args, wrap) {
228
- const reply = args[1]
229
- if (!reply || !shim.isFunction(reply.continue)) {
230
- return
231
- }
232
- wrap(reply, 'continue')
233
- },
234
- req: function getReq(shim, fn, fnName, args) {
235
- const request = args[0]
236
- if (request && request.raw) {
237
- return request.raw.req
238
- }
239
- }
240
- }
241
-
242
- return shim.recordMiddleware(middleware, spec)
243
- }
244
-
245
- function wrapReply(wrap, reply, isPreHandler) {
246
- const isFinal = !isPreHandler
247
- // The only reply method that is actually used by pre-handlers is `response`. All
248
- // other methods still exist, but don't function as they do in normal route handlers.
249
- // Since they can still be referenced by users, they still need to be wrapped.
250
- wrap(reply, 'continue', isFinal)
251
- wrap(reply, 'redirect', isFinal)
252
- wrap(reply, 'close', isFinal)
253
- wrap(reply, 'response', isFinal)
254
- }