1 | "use strict";
|
2 | var __assign = (this && this.__assign) || function () {
|
3 | __assign = Object.assign || function(t) {
|
4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5 | s = arguments[i];
|
6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7 | t[p] = s[p];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | return __assign.apply(this, arguments);
|
12 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | exports.enable = exports.mongo330 = exports.mongo3 = exports.mongo2 = void 0;
|
15 |
|
16 |
|
17 | var diagnostic_channel_1 = require("diagnostic-channel");
|
18 | var mongodbPatchFunction = function (originalMongo) {
|
19 | var listener = originalMongo.instrument({
|
20 | operationIdGenerator: {
|
21 | next: function () {
|
22 | return diagnostic_channel_1.channel.bindToContext(function (cb) { return cb(); });
|
23 | }
|
24 | }
|
25 | });
|
26 | var eventMap = {};
|
27 | listener.on("started", function (event) {
|
28 | if (eventMap[event.requestId]) {
|
29 |
|
30 |
|
31 |
|
32 | return;
|
33 | }
|
34 | eventMap[event.requestId] = __assign(__assign({}, event), { time: new Date() });
|
35 | });
|
36 | listener.on("succeeded", function (event) {
|
37 | var startedData = eventMap[event.requestId];
|
38 | if (startedData) {
|
39 | delete eventMap[event.requestId];
|
40 | }
|
41 | if (typeof event.operationId === "function") {
|
42 | event.operationId(function () { return diagnostic_channel_1.channel.publish("mongodb", { startedData: startedData, event: event, succeeded: true }); });
|
43 | }
|
44 | else {
|
45 |
|
46 | diagnostic_channel_1.channel.publish("mongodb", { startedData: startedData, event: event, succeeded: true });
|
47 | }
|
48 | });
|
49 | listener.on("failed", function (event) {
|
50 | var startedData = eventMap[event.requestId];
|
51 | if (startedData) {
|
52 | delete eventMap[event.requestId];
|
53 | }
|
54 | if (typeof event.operationId === "function") {
|
55 | event.operationId(function () { return diagnostic_channel_1.channel.publish("mongodb", { startedData: startedData, event: event, succeeded: false }); });
|
56 | }
|
57 | else {
|
58 |
|
59 | diagnostic_channel_1.channel.publish("mongodb", { startedData: startedData, event: event, succeeded: false });
|
60 | }
|
61 | });
|
62 | return originalMongo;
|
63 | };
|
64 | var mongodb3PatchFunction = function (originalMongo) {
|
65 | var listener = originalMongo.instrument();
|
66 | var eventMap = {};
|
67 | var contextMap = {};
|
68 | listener.on("started", function (event) {
|
69 | if (eventMap[event.requestId]) {
|
70 |
|
71 |
|
72 |
|
73 | return;
|
74 | }
|
75 | contextMap[event.requestId] = diagnostic_channel_1.channel.bindToContext(function (cb) { return cb(); });
|
76 | eventMap[event.requestId] = __assign(__assign({}, event), { time: new Date() });
|
77 | });
|
78 | listener.on("succeeded", function (event) {
|
79 | var startedData = eventMap[event.requestId];
|
80 | if (startedData) {
|
81 | delete eventMap[event.requestId];
|
82 | }
|
83 | if (typeof event === "object" && typeof contextMap[event.requestId] === "function") {
|
84 | contextMap[event.requestId](function () { return diagnostic_channel_1.channel.publish("mongodb", { startedData: startedData, event: event, succeeded: true }); });
|
85 | delete contextMap[event.requestId];
|
86 | }
|
87 | });
|
88 | listener.on("failed", function (event) {
|
89 | var startedData = eventMap[event.requestId];
|
90 | if (startedData) {
|
91 | delete eventMap[event.requestId];
|
92 | }
|
93 | if (typeof event === "object" && typeof contextMap[event.requestId] === "function") {
|
94 | contextMap[event.requestId](function () { return diagnostic_channel_1.channel.publish("mongodb", { startedData: startedData, event: event, succeeded: false }); });
|
95 | delete contextMap[event.requestId];
|
96 | }
|
97 | });
|
98 | return originalMongo;
|
99 | };
|
100 |
|
101 |
|
102 | var mongodbcorePatchFunction = function (originalMongo) {
|
103 | var originalConnect = originalMongo.Server.prototype.connect;
|
104 | originalMongo.Server.prototype.connect = function contextPreservingConnect() {
|
105 | var ret = originalConnect.apply(this, arguments);
|
106 |
|
107 |
|
108 |
|
109 | var originalWrite = this.s.coreTopology.s.pool.write;
|
110 | this.s.coreTopology.s.pool.write = function contextPreservingWrite() {
|
111 | var cbidx = typeof arguments[1] === "function" ? 1 : 2;
|
112 | if (typeof arguments[cbidx] === "function") {
|
113 | arguments[cbidx] = diagnostic_channel_1.channel.bindToContext(arguments[cbidx]);
|
114 | }
|
115 | return originalWrite.apply(this, arguments);
|
116 | };
|
117 |
|
118 |
|
119 | var originalLogout = this.s.coreTopology.s.pool.logout;
|
120 | this.s.coreTopology.s.pool.logout = function contextPreservingLogout() {
|
121 | if (typeof arguments[1] === "function") {
|
122 | arguments[1] = diagnostic_channel_1.channel.bindToContext(arguments[1]);
|
123 | }
|
124 | return originalLogout.apply(this, arguments);
|
125 | };
|
126 | return ret;
|
127 | };
|
128 | return originalMongo;
|
129 | };
|
130 | var mongodb330PatchFunction = function (originalMongo) {
|
131 | mongodbcorePatchFunction(originalMongo);
|
132 | var listener = originalMongo.instrument();
|
133 | var eventMap = {};
|
134 | var contextMap = {};
|
135 | listener.on("started", function (event) {
|
136 | if (eventMap[event.requestId]) {
|
137 |
|
138 |
|
139 |
|
140 | return;
|
141 | }
|
142 | contextMap[event.requestId] = diagnostic_channel_1.channel.bindToContext(function (cb) { return cb(); });
|
143 | eventMap[event.requestId] = event;
|
144 | });
|
145 | listener.on("succeeded", function (event) {
|
146 | var startedData = eventMap[event.requestId];
|
147 | if (startedData) {
|
148 | delete eventMap[event.requestId];
|
149 | }
|
150 | if (typeof event === "object" && typeof contextMap[event.requestId] === "function") {
|
151 | contextMap[event.requestId](function () { return diagnostic_channel_1.channel.publish("mongodb", { startedData: startedData, event: event, succeeded: true }); });
|
152 | delete contextMap[event.requestId];
|
153 | }
|
154 | });
|
155 | listener.on("failed", function (event) {
|
156 | var startedData = eventMap[event.requestId];
|
157 | if (startedData) {
|
158 | delete eventMap[event.requestId];
|
159 | }
|
160 | if (typeof event === "object" && typeof contextMap[event.requestId] === "function") {
|
161 | contextMap[event.requestId](function () { return diagnostic_channel_1.channel.publish("mongodb", { startedData: startedData, event: event, succeeded: false }); });
|
162 | delete contextMap[event.requestId];
|
163 | }
|
164 | });
|
165 | return originalMongo;
|
166 | };
|
167 | exports.mongo2 = {
|
168 | versionSpecifier: ">= 2.0.0 <= 3.0.5",
|
169 | patch: mongodbPatchFunction
|
170 | };
|
171 | exports.mongo3 = {
|
172 | versionSpecifier: "> 3.0.5 < 3.3.0",
|
173 | patch: mongodb3PatchFunction
|
174 | };
|
175 | exports.mongo330 = {
|
176 | versionSpecifier: ">= 3.3.0 < 4.0.0",
|
177 | patch: mongodb330PatchFunction
|
178 | };
|
179 | function enable() {
|
180 | diagnostic_channel_1.channel.registerMonkeyPatch("mongodb", exports.mongo2);
|
181 | diagnostic_channel_1.channel.registerMonkeyPatch("mongodb", exports.mongo3);
|
182 | diagnostic_channel_1.channel.registerMonkeyPatch("mongodb", exports.mongo330);
|
183 | }
|
184 | exports.enable = enable;
|
185 |
|
\ | No newline at end of file |