(raw: EncodedMessage)
| 394 | } |
| 395 | |
| 396 | async function decodeRawMessage(raw: EncodedMessage): Promise<{ json: any, buffer?: Uint8Array }> { |
| 397 | if (typeof raw === 'string') { |
| 398 | return { json: JSON.parse(raw) } |
| 399 | } |
| 400 | |
| 401 | const buffer = raw instanceof Uint8Array ? raw : new Uint8Array(raw) |
| 402 | |
| 403 | const delimiterIndex = buffer.indexOf(JSON_AND_BINARY_DELIMITER) |
| 404 | |
| 405 | if (delimiterIndex === -1) { |
| 406 | const jsonPart = new TextDecoder().decode(buffer) |
| 407 | return { json: JSON.parse(jsonPart) } |
| 408 | } |
| 409 | |
| 410 | const jsonPart = new TextDecoder().decode(buffer.subarray(0, delimiterIndex)) |
| 411 | const bufferPart = buffer.subarray(delimiterIndex + 1) |
| 412 | |
| 413 | return { |
| 414 | json: JSON.parse(jsonPart), |
| 415 | buffer: bufferPart, |
| 416 | } |
| 417 | } |
no test coverage detected