-
Notifications
You must be signed in to change notification settings - Fork 25
/
mongodb-core.pub.ts
executable file
·44 lines (38 loc) · 1.85 KB
/
mongodb-core.pub.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
import {channel, IModulePatcher, PatchFunction} from "diagnostic-channel";
const mongodbcorePatchFunction: PatchFunction = function(originalMongoCore) {
const originalConnect = originalMongoCore.Server.prototype.connect;
originalMongoCore.Server.prototype.connect = function contextPreservingConnect() {
const ret = originalConnect.apply(this, arguments);
// Messages sent to mongo progress through a pool
// This can result in context getting mixed between different responses
// so we wrap the callbacks to restore appropriate state
const originalWrite = this.s.pool.write;
this.s.pool.write = function contextPreservingWrite() {
const cbidx = typeof arguments[1] === "function" ? 1 : 2;
if (typeof arguments[cbidx] === "function" ) {
arguments[cbidx] = channel.bindToContext(arguments[cbidx]);
}
return originalWrite.apply(this, arguments);
};
// Logout is a special case, it doesn't call the write function but instead
// directly calls into connection.write
const originalLogout = this.s.pool.logout;
this.s.pool.logout = function contextPreservingLogout() {
if (typeof arguments[1] === "function") {
arguments[1] = channel.bindToContext(arguments[1]);
}
return originalLogout.apply(this, arguments);
};
return ret;
};
return originalMongoCore;
};
export const mongoCore: IModulePatcher = {
versionSpecifier: ">= 2.0.0 < 4.0.0",
patch: mongodbcorePatchFunction,
};
export function enable() {
channel.registerMonkeyPatch("mongodb-core", mongoCore);
}