command.js
5.89 KB
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
'use strict';
const Query = require('../connection/commands').Query;
const Msg = require('../connection/msg').Msg;
const MongoError = require('../error').MongoError;
const getReadPreference = require('./shared').getReadPreference;
const isSharded = require('./shared').isSharded;
const databaseNamespace = require('./shared').databaseNamespace;
const isTransactionCommand = require('../transactions').isTransactionCommand;
const applySession = require('../sessions').applySession;
const MongoNetworkError = require('../error').MongoNetworkError;
const maxWireVersion = require('../utils').maxWireVersion;
function isClientEncryptionEnabled(server) {
const wireVersion = maxWireVersion(server);
return wireVersion && server.autoEncrypter;
}
function command(server, ns, cmd, options, callback) {
if (typeof options === 'function') (callback = options), (options = {});
options = options || {};
if (cmd == null) {
return callback(new MongoError(`command ${JSON.stringify(cmd)} does not return a cursor`));
}
if (!isClientEncryptionEnabled(server)) {
_command(server, ns, cmd, options, callback);
return;
}
const wireVersion = maxWireVersion(server);
if (typeof wireVersion !== 'number' || wireVersion < 8) {
callback(new MongoError('Auto-encryption requires a minimum MongoDB version of 4.2'));
return;
}
_cryptCommand(server, ns, cmd, options, callback);
}
function _command(server, ns, cmd, options, callback) {
const bson = server.s.bson;
const pool = server.s.pool;
const readPreference = getReadPreference(cmd, options);
const shouldUseOpMsg = supportsOpMsg(server);
const session = options.session;
const serverClusterTime = server.clusterTime;
let clusterTime = serverClusterTime;
let finalCmd = Object.assign({}, cmd);
const serverApi = options.serverApi;
if (serverApi) {
finalCmd.apiVersion = serverApi.version || serverApi;
if (serverApi.strict != null) {
finalCmd.apiStrict = serverApi.strict;
}
if (serverApi.deprecationErrors != null) {
finalCmd.apiDeprecationErrors = serverApi.deprecationErrors;
}
}
if (hasSessionSupport(server) && session) {
const sessionClusterTime = session.clusterTime;
if (
serverClusterTime &&
serverClusterTime.clusterTime &&
sessionClusterTime &&
sessionClusterTime.clusterTime &&
sessionClusterTime.clusterTime.greaterThan(serverClusterTime.clusterTime)
) {
clusterTime = sessionClusterTime;
}
// We need to unpin any read or write commands that happen outside of a pinned
// transaction, so we check if we have a pinned transaction that is no longer
// active, and unpin for all except start or commit.
if (
!session.transaction.isActive &&
session.transaction.isPinned &&
!finalCmd.startTransaction &&
!finalCmd.commitTransaction
) {
session.transaction.unpinServer();
}
const err = applySession(session, finalCmd, options);
if (err) {
return callback(err);
}
}
if (clusterTime) {
// if we have a known cluster time, gossip it
finalCmd.$clusterTime = clusterTime;
}
if (isSharded(server) && !shouldUseOpMsg && readPreference && readPreference.mode !== 'primary') {
finalCmd = {
$query: finalCmd,
$readPreference: readPreference.toJSON()
};
}
const commandOptions = Object.assign(
{
command: true,
numberToSkip: 0,
numberToReturn: -1,
checkKeys: false
},
options
);
// This value is not overridable
commandOptions.slaveOk = readPreference.slaveOk();
const cmdNs = `${databaseNamespace(ns)}.$cmd`;
const message = shouldUseOpMsg
? new Msg(bson, cmdNs, finalCmd, commandOptions)
: new Query(bson, cmdNs, finalCmd, commandOptions);
const inTransaction = session && (session.inTransaction() || isTransactionCommand(finalCmd));
const commandResponseHandler = inTransaction
? function(err) {
// We need to add a TransientTransactionError errorLabel, as stated in the transaction spec.
if (
err &&
err instanceof MongoNetworkError &&
!err.hasErrorLabel('TransientTransactionError')
) {
err.addErrorLabel('TransientTransactionError');
}
if (
!cmd.commitTransaction &&
err &&
err instanceof MongoError &&
err.hasErrorLabel('TransientTransactionError')
) {
session.transaction.unpinServer();
}
return callback.apply(null, arguments);
}
: callback;
try {
pool.write(message, commandOptions, commandResponseHandler);
} catch (err) {
commandResponseHandler(err);
}
}
function hasSessionSupport(topology) {
if (topology == null) return false;
if (topology.description) {
return topology.description.maxWireVersion >= 6;
}
return topology.ismaster == null ? false : topology.ismaster.maxWireVersion >= 6;
}
function supportsOpMsg(topologyOrServer) {
const description = topologyOrServer.ismaster
? topologyOrServer.ismaster
: topologyOrServer.description;
if (description == null) {
return false;
}
return description.maxWireVersion >= 6 && description.__nodejs_mock_server__ == null;
}
function _cryptCommand(server, ns, cmd, options, callback) {
const autoEncrypter = server.autoEncrypter;
function commandResponseHandler(err, response) {
if (err || response == null) {
callback(err, response);
return;
}
autoEncrypter.decrypt(response.result, options, (err, decrypted) => {
if (err) {
callback(err, null);
return;
}
response.result = decrypted;
response.message.documents = [decrypted];
callback(null, response);
});
}
autoEncrypter.encrypt(ns, cmd, options, (err, encrypted) => {
if (err) {
callback(err, null);
return;
}
_command(server, ns, encrypted, options, commandResponseHandler);
});
}
module.exports = command;