1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.enable = exports.mysql = void 0;
|
4 |
|
5 |
|
6 | var diagnostic_channel_1 = require("diagnostic-channel");
|
7 | var path = require("path");
|
8 | var mysqlPatchFunction = function (originalMysql, originalMysqlPath) {
|
9 |
|
10 |
|
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 |
|
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 |
|
31 |
|
32 |
|
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 |
|
59 | patchObjectFunction(connectionClass, "Connection")("createQuery", function (resultContainer, cb) {
|
60 | return function (err) {
|
61 | var hrDuration = process.hrtime(resultContainer.startTime);
|
62 |
|
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 | };
|
75 | exports.mysql = {
|
76 | versionSpecifier: ">= 2.0.0 < 3.0.0",
|
77 | patch: mysqlPatchFunction
|
78 | };
|
79 | function enable() {
|
80 | diagnostic_channel_1.channel.registerMonkeyPatch("mysql", exports.mysql);
|
81 | }
|
82 | exports.enable = enable;
|
83 |
|
\ | No newline at end of file |