Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | 416x 416x 416x 416x 6058044x 6058044x 6058044x 6058044x 6058044x 855823x 855823x 6058044x 6058044x 6058044x 6058044x 6058044x 6058044x 6058044x 6058044x 416x 5720734x 5720734x 5720734x 5720734x 5720734x 5720734x 5720734x 5720734x 5720734x 5720734x 5720734x 5720734x 416x 346290x 346290x 346290x 346290x 346290x 346290x 346290x 346290x 346290x 346290x 346290x 346290x 416x 416x 12125068x 416x 416x 12125068x 416x 416x 12125068x 8394604x 8394604x 5929x 8394604x 5929x 8394604x 3730464x 16x 12x 4x 4x 44x 4x 16x 48x 48x 24x 16x 3730448x 3730448x 3730448x 3730444x 3730444x 3730448x 5720734x 600341x 5120393x 12125068x 12125068x 12125068x | import { type Document, type ObjectId } from '../bson'; import { COMMAND_FAILED, COMMAND_STARTED, COMMAND_SUCCEEDED, LEGACY_HELLO_COMMAND, LEGACY_HELLO_COMMAND_CAMEL_CASE } from '../constants'; import { calculateDurationInMs } from '../utils'; import { DocumentSequence, OpMsgRequest, type OpQueryRequest, type WriteProtocolMessageType } from './commands'; import type { Connection } from './connection'; /** * An event indicating the start of a given command * @public * @category Event */ export class CommandStartedEvent { commandObj?: Document; requestId: number; databaseName: string; commandName: string; command: Document; address: string; /** Driver generated connection id */ connectionId?: string | number; /** * Server generated connection id * Distinct from the connection id and is returned by the hello or legacy hello response as "connectionId" * from the server on 4.2+. */ serverConnectionId: bigint | null; serviceId?: ObjectId; /** @internal */ name = COMMAND_STARTED; /** * Create a started event * * @internal * @param pool - the pool that originated the command * @param command - the command */ constructor( connection: Connection, command: WriteProtocolMessageType, serverConnectionId: bigint | null ) { const cmd = extractCommand(command); const commandName = extractCommandName(cmd); const { address, connectionId, serviceId } = extractConnectionDetails(connection); // TODO: remove in major revision, this is not spec behavior if (SENSITIVE_COMMANDS.has(commandName)) { this.commandObj = {}; this.commandObj[commandName] = true; } this.address = address; this.connectionId = connectionId; this.serviceId = serviceId; this.requestId = command.requestId; this.databaseName = command.databaseName; this.commandName = commandName; this.command = maybeRedact(commandName, cmd, cmd); this.serverConnectionId = serverConnectionId; } /* @internal */ get hasServiceId(): boolean { return !!this.serviceId; } } /** * An event indicating the success of a given command * @public * @category Event */ export class CommandSucceededEvent { address: string; /** Driver generated connection id */ connectionId?: string | number; /** * Server generated connection id * Distinct from the connection id and is returned by the hello or legacy hello response as "connectionId" from the server on 4.2+. */ serverConnectionId: bigint | null; requestId: number; duration: number; commandName: string; reply: unknown; serviceId?: ObjectId; /** @internal */ name = COMMAND_SUCCEEDED; /** * Create a succeeded event * * @internal * @param pool - the pool that originated the command * @param command - the command * @param reply - the reply for this command from the server * @param started - a high resolution tuple timestamp of when the command was first sent, to calculate duration */ constructor( connection: Connection, command: WriteProtocolMessageType, reply: Document | undefined, started: number, serverConnectionId: bigint | null ) { const cmd = extractCommand(command); const commandName = extractCommandName(cmd); const { address, connectionId, serviceId } = extractConnectionDetails(connection); this.address = address; this.connectionId = connectionId; this.serviceId = serviceId; this.requestId = command.requestId; this.commandName = commandName; this.duration = calculateDurationInMs(started); this.reply = maybeRedact(commandName, cmd, extractReply(reply)); this.serverConnectionId = serverConnectionId; } /* @internal */ get hasServiceId(): boolean { return !!this.serviceId; } } /** * An event indicating the failure of a given command * @public * @category Event */ export class CommandFailedEvent { address: string; /** Driver generated connection id */ connectionId?: string | number; /** * Server generated connection id * Distinct from the connection id and is returned by the hello or legacy hello response as "connectionId" from the server on 4.2+. */ serverConnectionId: bigint | null; requestId: number; duration: number; commandName: string; failure: Error; serviceId?: ObjectId; /** @internal */ name = COMMAND_FAILED; /** * Create a failure event * * @internal * @param pool - the pool that originated the command * @param command - the command * @param error - the generated error or a server error response * @param started - a high resolution tuple timestamp of when the command was first sent, to calculate duration */ constructor( connection: Connection, command: WriteProtocolMessageType, error: Error | Document, started: number, serverConnectionId: bigint | null ) { const cmd = extractCommand(command); const commandName = extractCommandName(cmd); const { address, connectionId, serviceId } = extractConnectionDetails(connection); this.address = address; this.connectionId = connectionId; this.serviceId = serviceId; this.requestId = command.requestId; this.commandName = commandName; this.duration = calculateDurationInMs(started); this.failure = maybeRedact(commandName, cmd, error) as Error; this.serverConnectionId = serverConnectionId; } /* @internal */ get hasServiceId(): boolean { return !!this.serviceId; } } /** * Commands that we want to redact because of the sensitive nature of their contents * @internal */ export const SENSITIVE_COMMANDS = new Set([ 'authenticate', 'saslStart', 'saslContinue', 'getnonce', 'createUser', 'updateUser', 'copydbgetnonce', 'copydbsaslstart', 'copydb' ]); const HELLO_COMMANDS = new Set(['hello', LEGACY_HELLO_COMMAND, LEGACY_HELLO_COMMAND_CAMEL_CASE]); // helper methods const extractCommandName = (commandDoc: Document) => Object.keys(commandDoc)[0]; const collectionName = (command: OpQueryRequest) => command.ns.split('.')[1]; const maybeRedact = (commandName: string, commandDoc: Document, result: Error | Document) => SENSITIVE_COMMANDS.has(commandName) || (HELLO_COMMANDS.has(commandName) && commandDoc.speculativeAuthenticate) ? {} : result; const LEGACY_FIND_QUERY_MAP: { [key: string]: string } = { $query: 'filter', $orderby: 'sort', $hint: 'hint', $comment: 'comment', $maxScan: 'maxScan', $max: 'max', $min: 'min', $returnKey: 'returnKey', $showDiskLoc: 'showRecordId', $maxTimeMS: 'maxTimeMS', $snapshot: 'snapshot' }; const LEGACY_FIND_OPTIONS_MAP = { numberToSkip: 'skip', numberToReturn: 'batchSize', returnFieldSelector: 'projection' } as const; /** Extract the actual command from the query, possibly up-converting if it's a legacy format */ function extractCommand(command: WriteProtocolMessageType): Document { if (command instanceof OpMsgRequest) { const cmd = { ...command.command }; // For OP_MSG with payload type 1 we need to pull the documents // array out of the document sequence for monitoring. if (cmd.ops instanceof DocumentSequence) { cmd.ops = cmd.ops.documents; } if (cmd.nsInfo instanceof DocumentSequence) { cmd.nsInfo = cmd.nsInfo.documents; } return cmd; } if (command.query?.$query) { let result: Document; if (command.ns === 'admin.$cmd') { // up-convert legacy command result = Object.assign({}, command.query.$query); } else { // up-convert legacy find command result = { find: collectionName(command) }; Object.keys(LEGACY_FIND_QUERY_MAP).forEach(key => { if (command.query[key] != null) { result[LEGACY_FIND_QUERY_MAP[key]] = { ...command.query[key] }; } }); } Object.keys(LEGACY_FIND_OPTIONS_MAP).forEach(key => { const legacyKey = key as keyof typeof LEGACY_FIND_OPTIONS_MAP; if (command[legacyKey] != null) { result[LEGACY_FIND_OPTIONS_MAP[legacyKey]] = command[legacyKey]; } }); return result; } let clonedQuery: Record<string, unknown> = {}; const clonedCommand: Record<string, unknown> = { ...command }; if (command.query) { clonedQuery = { ...command.query }; clonedCommand.query = clonedQuery; } return command.query ? clonedQuery : clonedCommand; } function extractReply(reply?: Document) { if (!reply) { return reply; } return reply.result ? reply.result : reply; } function extractConnectionDetails(connection: Connection) { let connectionId; Eif ('id' in connection) { connectionId = connection.id; } return { address: connection.address, serviceId: connection.serviceId, connectionId }; } |