| 364 | } |
| 365 | |
| 366 | parse(): Uint8Array { |
| 367 | // Don't parse again if not needed |
| 368 | if (this.parsed) return this.sections[0]; |
| 369 | |
| 370 | // Position within OP_REPLY at which documents start |
| 371 | // (See https://www.mongodb.com/docs/manual/reference/mongodb-wire-protocol/#wire-op-reply) |
| 372 | this.index = 20; |
| 373 | |
| 374 | // Read the message body |
| 375 | this.responseFlags = readInt32LE(this.data, 0); |
| 376 | this.cursorId = new BSON.Long(readInt32LE(this.data, 4), readInt32LE(this.data, 8)); |
| 377 | this.startingFrom = readInt32LE(this.data, 12); |
| 378 | this.numberReturned = readInt32LE(this.data, 16); |
| 379 | |
| 380 | if (this.numberReturned < 0 || this.numberReturned > 2 ** 32 - 1) { |
| 381 | throw new RangeError( |
| 382 | `OP_REPLY numberReturned is an invalid array length ${this.numberReturned}` |
| 383 | ); |
| 384 | } |
| 385 | |
| 386 | this.cursorNotFound = (this.responseFlags & CURSOR_NOT_FOUND) !== 0; |
| 387 | this.queryFailure = (this.responseFlags & QUERY_FAILURE) !== 0; |
| 388 | this.shardConfigStale = (this.responseFlags & SHARD_CONFIG_STALE) !== 0; |
| 389 | this.awaitCapable = (this.responseFlags & AWAIT_CAPABLE) !== 0; |
| 390 | |
| 391 | // Parse Body |
| 392 | for (let i = 0; i < this.numberReturned; i++) { |
| 393 | const bsonSize = |
| 394 | this.data[this.index] | |
| 395 | (this.data[this.index + 1] << 8) | |
| 396 | (this.data[this.index + 2] << 16) | |
| 397 | (this.data[this.index + 3] << 24); |
| 398 | |
| 399 | const section = this.data.subarray(this.index, this.index + bsonSize); |
| 400 | this.sections.push(section); |
| 401 | |
| 402 | // Adjust the index |
| 403 | this.index = this.index + bsonSize; |
| 404 | } |
| 405 | |
| 406 | // Set parsed |
| 407 | this.parsed = true; |
| 408 | |
| 409 | return this.sections[0]; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | // Msg Flags |