6
6
LEGACY_HELLO_COMMAND ,
7
7
LEGACY_HELLO_COMMAND_CAMEL_CASE
8
8
} from '../constants' ;
9
- import { calculateDurationInMs , deepCopy } from '../utils' ;
9
+ import { calculateDurationInMs } from '../utils' ;
10
10
import {
11
11
DocumentSequence ,
12
12
OpMsgRequest ,
@@ -125,7 +125,7 @@ export class CommandSucceededEvent {
125
125
this . requestId = command . requestId ;
126
126
this . commandName = commandName ;
127
127
this . duration = calculateDurationInMs ( started ) ;
128
- this . reply = maybeRedact ( commandName , cmd , extractReply ( command , reply ) ) ;
128
+ this . reply = maybeRedact ( commandName , cmd , extractReply ( reply ) ) ;
129
129
this . serverConnectionId = serverConnectionId ;
130
130
}
131
131
@@ -214,7 +214,6 @@ const HELLO_COMMANDS = new Set(['hello', LEGACY_HELLO_COMMAND, LEGACY_HELLO_COMM
214
214
215
215
// helper methods
216
216
const extractCommandName = ( commandDoc : Document ) => Object . keys ( commandDoc ) [ 0 ] ;
217
- const namespace = ( command : OpQueryRequest ) => command . ns ;
218
217
const collectionName = ( command : OpQueryRequest ) => command . ns . split ( '.' ) [ 1 ] ;
219
218
const maybeRedact = ( commandName : string , commandDoc : Document , result : Error | Document ) =>
220
219
SENSITIVE_COMMANDS . has ( commandName ) ||
@@ -242,19 +241,10 @@ const LEGACY_FIND_OPTIONS_MAP = {
242
241
returnFieldSelector : 'projection'
243
242
} as const ;
244
243
245
- const OP_QUERY_KEYS = [
246
- 'tailable' ,
247
- 'oplogReplay' ,
248
- 'noCursorTimeout' ,
249
- 'awaitData' ,
250
- 'partial' ,
251
- 'exhaust'
252
- ] as const ;
253
-
254
244
/** Extract the actual command from the query, possibly up-converting if it's a legacy format */
255
245
function extractCommand ( command : WriteProtocolMessageType ) : Document {
256
246
if ( command instanceof OpMsgRequest ) {
257
- const cmd = deepCopy ( command . command ) ;
247
+ const cmd = { ... command . command } ;
258
248
// For OP_MSG with payload type 1 we need to pull the documents
259
249
// array out of the document sequence for monitoring.
260
250
if ( cmd . ops instanceof DocumentSequence ) {
@@ -276,72 +266,37 @@ function extractCommand(command: WriteProtocolMessageType): Document {
276
266
result = { find : collectionName ( command ) } ;
277
267
Object . keys ( LEGACY_FIND_QUERY_MAP ) . forEach ( key => {
278
268
if ( command . query [ key ] != null ) {
279
- result [ LEGACY_FIND_QUERY_MAP [ key ] ] = deepCopy ( command . query [ key ] ) ;
269
+ result [ LEGACY_FIND_QUERY_MAP [ key ] ] = { ... command . query [ key ] } ;
280
270
}
281
271
} ) ;
282
272
}
283
273
284
274
Object . keys ( LEGACY_FIND_OPTIONS_MAP ) . forEach ( key => {
285
275
const legacyKey = key as keyof typeof LEGACY_FIND_OPTIONS_MAP ;
286
276
if ( command [ legacyKey ] != null ) {
287
- result [ LEGACY_FIND_OPTIONS_MAP [ legacyKey ] ] = deepCopy ( command [ legacyKey ] ) ;
288
- }
289
- } ) ;
290
-
291
- OP_QUERY_KEYS . forEach ( key => {
292
- if ( command [ key ] ) {
293
- result [ key ] = command [ key ] ;
277
+ result [ LEGACY_FIND_OPTIONS_MAP [ legacyKey ] ] = command [ legacyKey ] ;
294
278
}
295
279
} ) ;
296
280
297
- if ( command . pre32Limit != null ) {
298
- result . limit = command . pre32Limit ;
299
- }
300
-
301
- if ( command . query . $explain ) {
302
- return { explain : result } ;
303
- }
304
281
return result ;
305
282
}
306
283
307
- const clonedQuery : Record < string , unknown > = { } ;
308
- const clonedCommand : Record < string , unknown > = { } ;
284
+ let clonedQuery : Record < string , unknown > = { } ;
285
+ const clonedCommand : Record < string , unknown > = { ... command } ;
309
286
if ( command . query ) {
310
- for ( const k in command . query ) {
311
- clonedQuery [ k ] = deepCopy ( command . query [ k ] ) ;
312
- }
287
+ clonedQuery = { ...command . query } ;
313
288
clonedCommand . query = clonedQuery ;
314
289
}
315
290
316
- for ( const k in command ) {
317
- if ( k === 'query' ) continue ;
318
- clonedCommand [ k ] = deepCopy ( ( command as unknown as Record < string , unknown > ) [ k ] ) ;
319
- }
320
291
return command . query ? clonedQuery : clonedCommand ;
321
292
}
322
293
323
- function extractReply ( command : WriteProtocolMessageType , reply ?: Document ) {
294
+ function extractReply ( reply ?: Document ) {
324
295
if ( ! reply ) {
325
296
return reply ;
326
297
}
327
298
328
- if ( command instanceof OpMsgRequest ) {
329
- return deepCopy ( reply . result ? reply . result : reply ) ;
330
- }
331
-
332
- // is this a legacy find command?
333
- if ( command . query && command . query . $query != null ) {
334
- return {
335
- ok : 1 ,
336
- cursor : {
337
- id : deepCopy ( reply . cursorId ) ,
338
- ns : namespace ( command ) ,
339
- firstBatch : deepCopy ( reply . documents )
340
- }
341
- } ;
342
- }
343
-
344
- return deepCopy ( reply . result ? reply . result : reply ) ;
299
+ return reply . result ? reply . result : reply ;
345
300
}
346
301
347
302
function extractConnectionDetails ( connection : Connection ) {
0 commit comments