-
Notifications
You must be signed in to change notification settings - Fork 240
/
InstrumentHooks.ts
280 lines (241 loc) · 10 KB
/
InstrumentHooks.ts
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { strShimFunction, strShimPrototype } from "@microsoft/applicationinsights-shims";
import { getInst, objHasOwnProperty } from "@nevware21/ts-utils";
import {
IInstrumentCallDetails, IInstrumentHook, IInstrumentHooks, IInstrumentHooksCallbacks, InstrumentorHooksCallback
} from "../JavaScriptSDK.Interfaces/IInstrumentHooks";
import { _getObjProto } from "./HelperFuncs";
const aiInstrumentHooks = "_aiHooks";
const enum CallbackType {
Request = 0,
Response = 1,
HookError = 2,
FunctionError = 3
}
const cbNames = [
"req", "rsp", "hkErr", "fnErr"
];
/** @ignore */
function _arrLoop<T>(arr:T[], fn:(value:T, idx:number) => boolean|number|void) {
if (arr) {
for (let lp = 0; lp < arr.length; lp++) {
if (fn(arr[lp], lp)) {
break;
}
}
}
}
/** @ignore */
function _doCallbacks(hooks:IInstrumentHook[], callDetails: IInstrumentCallDetails, cbArgs:any[], hookCtx:any[], type:CallbackType): void {
if (type >= CallbackType.Request && type <= CallbackType.HookError) {
_arrLoop(hooks, (hook, idx) => {
let cbks = hook.cbks;
let cb:InstrumentorHooksCallback = cbks[cbNames[type]];
if (cb) {
// Set the specific hook context implementation using a lazy creation pattern
callDetails.ctx = () => {
let ctx = hookCtx[idx] = (hookCtx[idx] || {});
return ctx;
};
try {
cb.apply(callDetails.inst, cbArgs);
} catch (err) {
let orgEx = callDetails.err;
try {
// Report Hook error via the callback
let hookErrorCb:InstrumentorHooksCallback = cbks[cbNames[CallbackType.HookError]];
if (hookErrorCb) {
callDetails.err = err;
hookErrorCb.apply(callDetails.inst, cbArgs);
}
} catch(e) {
// Not much we can do here -- swallowing the exception to avoid crashing the hosting app
} finally {
// restore the original exception (if any)
callDetails.err = orgEx;
}
}
}
});
}
}
/** @ignore */
function _createFunctionHook(aiHook:IInstrumentHooks) {
// Define a temporary method that queues-up a the real method call
return function (this: any) {
let funcThis = this;
// Capture the original arguments passed to the method
let orgArgs = arguments as any;
let hooks = aiHook.h;
let funcArgs: IInstrumentCallDetails = {
name: aiHook.n,
inst: funcThis,
ctx: null,
set: _replaceArg
};
let hookCtx: any[] = [];
let cbArgs = _createArgs([funcArgs], orgArgs);
funcArgs.evt = getInst("event");
function _createArgs(target:any[], theArgs:any[]): any[] {
_arrLoop((theArgs as any), (arg) => {
target.push(arg);
});
return target;
}
function _replaceArg(idx:number, value:any) {
orgArgs = _createArgs([], orgArgs);
orgArgs[idx] = value;
cbArgs = _createArgs([funcArgs], orgArgs);
}
// Call the pre-request hooks
_doCallbacks(hooks, funcArgs, cbArgs, hookCtx, CallbackType.Request);
// Call the original function was called
let theFunc = aiHook.f;
if (theFunc) {
try {
funcArgs.rslt = theFunc.apply(funcThis, orgArgs);
} catch (err) {
// Report the request callback
funcArgs.err = err;
_doCallbacks(hooks, funcArgs, cbArgs, hookCtx, CallbackType.FunctionError);
// rethrow the original exception so anyone listening for it can catch the exception
throw err;
}
}
// Call the post-request hooks
_doCallbacks(hooks, funcArgs, cbArgs, hookCtx, CallbackType.Response);
return funcArgs.rslt;
};
}
/** @ignore */
function _getOwner(target:any, name:string, checkPrototype: boolean, checkParentProto: boolean): any {
let owner = null;
if (target) {
if (objHasOwnProperty(target, name)) {
owner = target;
} else if (checkPrototype) {
owner = _getOwner(_getObjProto(target), name, checkParentProto, false);
}
}
return owner;
}
/**
* Intercept the named prototype functions for the target class / object
* @param target - The target object
* @param funcName - The function name
* @param callbacks - The callbacks to configure and call whenever the function is called
*/
export function InstrumentProto(target:any, funcName:string, callbacks: IInstrumentHooksCallbacks): IInstrumentHook {
if (target) {
return InstrumentFunc(target[strShimPrototype], funcName, callbacks, false);
}
return null;
}
/**
* Intercept the named prototype functions for the target class / object
* @param target - The target object
* @param funcNames - The function names to intercept and call
* @param callbacks - The callbacks to configure and call whenever the function is called
*/
export function InstrumentProtos(target:any, funcNames:string[], callbacks: IInstrumentHooksCallbacks): IInstrumentHook[] {
if (target) {
return InstrumentFuncs(target[strShimPrototype], funcNames, callbacks, false);
}
return null;
}
function _createInstrumentHook(owner: any, funcName: string, fn: any, callbacks: IInstrumentHooksCallbacks) {
let aiHook: IInstrumentHooks = fn && fn[aiInstrumentHooks];
if (!aiHook) {
// Only hook the function once
aiHook = {
i: 0,
n: funcName,
f: fn,
h: []
};
// Override (hook) the original function
let newFunc = _createFunctionHook(aiHook);
newFunc[aiInstrumentHooks] = aiHook; // Tag and store the function hooks
owner[funcName] = newFunc;
}
const theHook: IInstrumentHook = {
// tslint:disable:object-literal-shorthand
id: aiHook.i,
cbks: callbacks,
rm: function () {
// DO NOT Use () => { shorthand for the function as the this gets replaced
// with the outer this and not the this for theHook instance.
let id = this.id;
_arrLoop(aiHook.h, (hook, idx) => {
if (hook.id === id) {
aiHook.h.splice(idx, 1);
return 1;
}
});
}
// tslint:enable:object-literal-shorthand
};
aiHook.i++;
aiHook.h.push(theHook);
return theHook;
}
/**
* Intercept the named prototype functions for the target class / object
* @param target - The target object
* @param funcName - The function name
* @param callbacks - The callbacks to configure and call whenever the function is called
* @param checkPrototype - If the function doesn't exist on the target should it attempt to hook the prototype function
* @param checkParentProto - If the function doesn't exist on the target or it's prototype should it attempt to hook the parent's prototype
*/
export function InstrumentFunc(target:any, funcName:string, callbacks: IInstrumentHooksCallbacks, checkPrototype: boolean = true, checkParentProto?: boolean): IInstrumentHook {
if (target && funcName && callbacks) {
let owner = _getOwner(target, funcName, checkPrototype, checkParentProto);
if (owner) {
let fn = owner[funcName]
if (typeof fn === strShimFunction) {
return _createInstrumentHook(owner, funcName, fn, callbacks);
}
}
}
return null;
}
/**
* Intercept the named functions for the target class / object
* @param target - The target object
* @param funcNames - The function names to intercept and call
* @param callbacks - The callbacks to configure and call whenever the function is called
* @param checkPrototype - If the function doesn't exist on the target should it attempt to hook the prototype function
* @param checkParentProto - If the function doesn't exist on the target or it's prototype should it attempt to hook the parent's prototype
*/
export function InstrumentFuncs(target:any, funcNames:string[], callbacks: IInstrumentHooksCallbacks, checkPrototype:boolean = true, checkParentProto?: boolean): IInstrumentHook[] {
let hooks: IInstrumentHook[] = null;
_arrLoop(funcNames, (funcName) => {
let hook = InstrumentFunc(target, funcName, callbacks, checkPrototype, checkParentProto);
if (hook) {
if (!hooks) {
hooks = [];
}
hooks.push(hook);
}
});
return hooks;
}
/**
* Add an instrumentation hook to the provided named "event" for the target class / object, this doesn't check whether the
* named "event" is in fact a function and just assigns the instrumentation hook to the target[evtName]
* @param target - The target object
* @param evtName - The name of the event
* @param callbacks - The callbacks to configure and call whenever the function is called
* @param checkPrototype - If the function doesn't exist on the target should it attempt to hook the prototype function
* @param checkParentProto - If the function doesn't exist on the target or it's prototype should it attempt to hook the parent's prototype
*/
export function InstrumentEvent(target: any, evtName: string, callbacks: IInstrumentHooksCallbacks, checkPrototype?: boolean, checkParentProto?: boolean): IInstrumentHook {
if (target && evtName && callbacks) {
let owner = _getOwner(target, evtName, checkPrototype, checkParentProto) || target;
if (owner) {
return _createInstrumentHook(owner, evtName, owner[evtName], callbacks);
}
}
return null;
}