UNPKG

4.15 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.enable = exports.mysql = void 0;
4// Copyright (c) Microsoft Corporation. All rights reserved.
5// Licensed under the MIT license. See LICENSE file in the project root for details.
6var diagnostic_channel_1 = require("diagnostic-channel");
7var path = require("path");
8var mysqlPatchFunction = function (originalMysql, originalMysqlPath) {
9 // The `name` passed in here is for debugging purposes,
10 // to help distinguish which object is being patched.
11 var patchObjectFunction = function (obj, name) {
12 return function (func, cbWrapper) {
13 var originalFunc = obj[func];
14 if (originalFunc) {
15 obj[func] = function mysqlContextPreserver() {
16 // Find the callback, if there is one
17 var cbidx = arguments.length - 1;
18 for (var i = arguments.length - 1; i >= 0; --i) {
19 if (typeof arguments[i] === "function") {
20 cbidx = i;
21 break;
22 }
23 else if (typeof arguments[i] !== "undefined") {
24 break;
25 }
26 }
27 var cb = arguments[cbidx];
28 var resultContainer = { result: null, startTime: null, startDate: null };
29 if (typeof cb === "function") {
30 // Preserve context on the callback.
31 // If this is one of the functions that we want to track,
32 // then wrap the callback with the tracking wrapper
33 if (cbWrapper) {
34 resultContainer.startTime = process.hrtime();
35 resultContainer.startDate = new Date();
36 arguments[cbidx] = diagnostic_channel_1.channel.bindToContext(cbWrapper(resultContainer, cb));
37 }
38 else {
39 arguments[cbidx] = diagnostic_channel_1.channel.bindToContext(cb);
40 }
41 }
42 var result = originalFunc.apply(this, arguments);
43 resultContainer.result = result;
44 return result;
45 };
46 }
47 };
48 };
49 var patchClassMemberFunction = function (classObject, name) {
50 return patchObjectFunction(classObject.prototype, name + ".prototype");
51 };
52 var connectionCallbackFunctions = [
53 "connect", "changeUser",
54 "ping", "statistics", "end",
55 ];
56 var connectionClass = require(path.dirname(originalMysqlPath) + "/lib/Connection");
57 connectionCallbackFunctions.forEach(function (value) { return patchClassMemberFunction(connectionClass, "Connection")(value); });
58 // Connection.createQuery is a static method
59 patchObjectFunction(connectionClass, "Connection")("createQuery", function (resultContainer, cb) {
60 return function (err) {
61 var hrDuration = process.hrtime(resultContainer.startTime);
62 /* tslint:disable-next-line:no-bitwise */
63 var duration = (hrDuration[0] * 1e3 + hrDuration[1] / 1e6) | 0;
64 diagnostic_channel_1.channel.publish("mysql", { query: resultContainer.result, callbackArgs: arguments, err: err, duration: duration, time: resultContainer.startDate });
65 cb.apply(this, arguments);
66 };
67 });
68 var poolCallbackFunctions = [
69 "_enqueueCallback",
70 ];
71 var poolClass = require(path.dirname(originalMysqlPath) + "/lib/Pool");
72 poolCallbackFunctions.forEach(function (value) { return patchClassMemberFunction(poolClass, "Pool")(value); });
73 return originalMysql;
74};
75exports.mysql = {
76 versionSpecifier: ">= 2.0.0 < 3.0.0",
77 patch: mysqlPatchFunction,
78};
79function enable() {
80 diagnostic_channel_1.channel.registerMonkeyPatch("mysql", exports.mysql);
81}
82exports.enable = enable;
83//# sourceMappingURL=mysql.pub.js.map
\No newline at end of file