66 LEGACY_HELLO_COMMAND ,
77 LEGACY_HELLO_COMMAND_CAMEL_CASE
88} from '../constants' ;
9- import { calculateDurationInMs , deepCopy } from '../utils' ;
9+ import { calculateDurationInMs } from '../utils' ;
1010import {
1111 DocumentSequence ,
1212 OpMsgRequest ,
@@ -125,7 +125,7 @@ export class CommandSucceededEvent {
125125 this . requestId = command . requestId ;
126126 this . commandName = commandName ;
127127 this . duration = calculateDurationInMs ( started ) ;
128- this . reply = maybeRedact ( commandName , cmd , extractReply ( command , reply ) ) ;
128+ this . reply = maybeRedact ( commandName , cmd , extractReply ( reply ) ) ;
129129 this . serverConnectionId = serverConnectionId ;
130130 }
131131
@@ -214,7 +214,6 @@ const HELLO_COMMANDS = new Set(['hello', LEGACY_HELLO_COMMAND, LEGACY_HELLO_COMM
214214
215215// helper methods
216216const extractCommandName = ( commandDoc : Document ) => Object . keys ( commandDoc ) [ 0 ] ;
217- const namespace = ( command : OpQueryRequest ) => command . ns ;
218217const collectionName = ( command : OpQueryRequest ) => command . ns . split ( '.' ) [ 1 ] ;
219218const maybeRedact = ( commandName : string , commandDoc : Document , result : Error | Document ) =>
220219 SENSITIVE_COMMANDS . has ( commandName ) ||
@@ -242,19 +241,10 @@ const LEGACY_FIND_OPTIONS_MAP = {
242241 returnFieldSelector : 'projection'
243242} as const ;
244243
245- const OP_QUERY_KEYS = [
246- 'tailable' ,
247- 'oplogReplay' ,
248- 'noCursorTimeout' ,
249- 'awaitData' ,
250- 'partial' ,
251- 'exhaust'
252- ] as const ;
253-
254244/** Extract the actual command from the query, possibly up-converting if it's a legacy format */
255245function extractCommand ( command : WriteProtocolMessageType ) : Document {
256246 if ( command instanceof OpMsgRequest ) {
257- const cmd = deepCopy ( command . command ) ;
247+ const cmd = { ... command . command } ;
258248 // For OP_MSG with payload type 1 we need to pull the documents
259249 // array out of the document sequence for monitoring.
260250 if ( cmd . ops instanceof DocumentSequence ) {
@@ -276,72 +266,37 @@ function extractCommand(command: WriteProtocolMessageType): Document {
276266 result = { find : collectionName ( command ) } ;
277267 Object . keys ( LEGACY_FIND_QUERY_MAP ) . forEach ( key => {
278268 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 ] } ;
280270 }
281271 } ) ;
282272 }
283273
284274 Object . keys ( LEGACY_FIND_OPTIONS_MAP ) . forEach ( key => {
285275 const legacyKey = key as keyof typeof LEGACY_FIND_OPTIONS_MAP ;
286276 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 ] ;
294278 }
295279 } ) ;
296280
297- if ( command . pre32Limit != null ) {
298- result . limit = command . pre32Limit ;
299- }
300-
301- if ( command . query . $explain ) {
302- return { explain : result } ;
303- }
304281 return result ;
305282 }
306283
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 } ;
309286 if ( command . query ) {
310- for ( const k in command . query ) {
311- clonedQuery [ k ] = deepCopy ( command . query [ k ] ) ;
312- }
287+ clonedQuery = { ...command . query } ;
313288 clonedCommand . query = clonedQuery ;
314289 }
315290
316- for ( const k in command ) {
317- if ( k === 'query' ) continue ;
318- clonedCommand [ k ] = deepCopy ( ( command as unknown as Record < string , unknown > ) [ k ] ) ;
319- }
320291 return command . query ? clonedQuery : clonedCommand ;
321292}
322293
323- function extractReply ( command : WriteProtocolMessageType , reply ?: Document ) {
294+ function extractReply ( reply ?: Document ) {
324295 if ( ! reply ) {
325296 return reply ;
326297 }
327298
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 ;
345300}
346301
347302function extractConnectionDetails ( connection : Connection ) {
0 commit comments