(message: Uint8Array)
| 183 | * This method does not parse the response's BSON. |
| 184 | */ |
| 185 | export async function decompressResponse(message: Uint8Array): Promise<OpMsgResponse | OpReply> { |
| 186 | const messageHeader: MessageHeader = { |
| 187 | length: readInt32LE(message, 0), |
| 188 | requestId: readInt32LE(message, 4), |
| 189 | responseTo: readInt32LE(message, 8), |
| 190 | opCode: readInt32LE(message, 12) |
| 191 | }; |
| 192 | |
| 193 | if (messageHeader.opCode !== OP_COMPRESSED) { |
| 194 | const ResponseType = messageHeader.opCode === OP_MSG ? OpMsgResponse : OpReply; |
| 195 | const messageBody = message.subarray(MESSAGE_HEADER_SIZE); |
| 196 | return new ResponseType(message, messageHeader, messageBody); |
| 197 | } |
| 198 | |
| 199 | const header: MessageHeader = { |
| 200 | ...messageHeader, |
| 201 | fromCompressed: true, |
| 202 | opCode: readInt32LE(message, MESSAGE_HEADER_SIZE), |
| 203 | length: readInt32LE(message, MESSAGE_HEADER_SIZE + 4) |
| 204 | }; |
| 205 | const compressorID = message[MESSAGE_HEADER_SIZE + 8]; |
| 206 | const compressedBuffer = message.slice(MESSAGE_HEADER_SIZE + 9); |
| 207 | |
| 208 | // recalculate based on wrapped opcode |
| 209 | const ResponseType = header.opCode === OP_MSG ? OpMsgResponse : OpReply; |
| 210 | const messageBody = await decompress(compressorID, compressedBuffer); |
| 211 | if (messageBody.length !== header.length) { |
| 212 | throw new MongoDecompressionError('Message body and message header must be the same length'); |
| 213 | } |
| 214 | return new ResponseType(message, header, messageBody); |
| 215 | } |
no test coverage detected