UNPKG

@deepkit/bson

Version:

Deepkit BSON parser

88 lines 4.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BsonStreamReader = void 0; const core_1 = require("@deepkit/core"); const model_js_1 = require("./model.js"); function readUint32LE(buffer, offset = 0) { return buffer[offset] + (buffer[offset + 1] * 2 ** 8) + (buffer[offset + 2] * 2 ** 16) + (buffer[offset + 3] * 2 ** 24); } readUint32LE.__type = ['buffer', 'offset', () => 0, 'readUint32LE', 'PW2!\'2">#\'/$']; /** * Reads BSON messages from a stream and emits them as Uint8Array. */ class BsonStreamReader { constructor(onMessage) { this.onMessage = onMessage; this.currentMessageSize = 0; } emptyBuffer() { return this.currentMessage === undefined; } feed(data, bytes) { if (!data.byteLength) return; if (!bytes) bytes = data.byteLength; if (!this.currentMessage) { if (data.byteLength < 4) { //not enough data to read the header. Wait for next onData this.currentMessage = data; this.currentMessageSize = 0; return; } this.currentMessage = data.byteLength === bytes ? data : data.subarray(0, bytes); this.currentMessageSize = readUint32LE(data); } else { this.currentMessage = (0, core_1.bufferConcat)([this.currentMessage, data.byteLength === bytes ? data : data.subarray(0, bytes)]); if (!this.currentMessageSize) { if (this.currentMessage.byteLength < 4) { //not enough data to read the header. Wait for next onData return; } this.currentMessageSize = readUint32LE(this.currentMessage); } if (this.currentMessage.byteLength < this.currentMessageSize) { //not enough data to read the header. Wait for next onData return; } } let currentSize = this.currentMessageSize; let currentBuffer = this.currentMessage; while (currentBuffer) { if (currentSize > currentBuffer.byteLength) { this.currentMessage = currentBuffer; this.currentMessageSize = currentSize; //message not completely loaded, wait for next onData return; } if (currentSize === currentBuffer.byteLength) { //current buffer is exactly the message length this.currentMessageSize = 0; this.currentMessage = undefined; this.onMessage(currentBuffer); return; } if (currentSize < currentBuffer.byteLength) { //we have more messages in this buffer. read what is necessary and hop to next loop iteration const message = currentBuffer.subarray(0, currentSize); this.onMessage(message); currentBuffer = currentBuffer.subarray(currentSize); if (currentBuffer.byteLength < 4) { //not enough data to read the header. Wait for next onData this.currentMessage = currentBuffer; this.currentMessageSize = 0; return; } const nextCurrentSize = readUint32LE(currentBuffer); if (nextCurrentSize <= 0) throw new model_js_1.BSONError('message size wrong'); currentSize = nextCurrentSize; //buffer and size has been set. consume this message in the next loop iteration } } } } exports.BsonStreamReader = BsonStreamReader; BsonStreamReader.__type = ['currentMessage', 'currentMessageSize', function () { return 0; }, 'response', '', 'onMessage', 'constructor', 'emptyBuffer', 'data', 'bytes', 'feed', 'BsonStreamReader', 'W3!8<\'3"<>#PPW2$$/%2&<9"0\'P)0(PW2)\'2*8"0+5w,']; //# sourceMappingURL=stream.js.map