Skip to content

Commit 2b9eca2

Browse files
committed
Fix splitting BSON objects
1 parent 2731c4f commit 2b9eca2

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

packages/common/src/utils/stream_transform.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export function extractBsonObjects(source: SimpleAsyncIterator<Uint8Array>): Sim
212212
// We're in the middle of reading a BSON document.
213213
const bytesToRead = Math.min(availableInData, remainingLength);
214214
const copySource = new Uint8Array(chunk.buffer, chunk.byteOffset + i, bytesToRead);
215-
objectBody.set(copySource, 4 + copySource.length - remainingLength);
215+
objectBody.set(copySource, objectBody.length - remainingLength);
216216
i += bytesToRead;
217217
remainingLength -= bytesToRead;
218218

@@ -242,10 +242,7 @@ export function extractBsonObjects(source: SimpleAsyncIterator<Uint8Array>): Sim
242242
}
243243

244244
objectBody = new Uint8Array(length);
245-
for (let j = 0; j < 4; j++) {
246-
objectBody[j] = lengthBuffer.getUint8(j);
247-
lengthBuffer.setUint8(j, 0);
248-
}
245+
new DataView(objectBody.buffer).setInt32(0, length, true);
249246
}
250247
}
251248
}

packages/common/tests/utils/stream_transform.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ describe('bson objects', () => {
117117
expect(await stream.next()).toStrictEqual(doneResult);
118118
});
119119

120+
test('splits bson objects (2)', async () => {
121+
async function* source() {
122+
yield new Uint8Array([5, 0, 0, 0, 1]);
123+
124+
yield new Uint8Array([6, 0, 0, 0, 2]);
125+
yield new Uint8Array([3]);
126+
}
127+
128+
const stream = extractBsonObjects(source());
129+
const a = (await stream.next()).value;
130+
const b = (await stream.next()).value;
131+
expect(a).toHaveLength(5);
132+
expect(b).toHaveLength(6);
133+
expect(Array.from(a)).toEqual([5, 0, 0, 0, 1]);
134+
expect(Array.from(b)).toEqual([6, 0, 0, 0, 2, 3]);
135+
expect(await stream.next()).toStrictEqual(doneResult);
136+
});
137+
120138
test('invalid bson size', async () => {
121139
async function* source() {
122140
yield new Uint8Array([3, 0, 0, 0]);

0 commit comments

Comments
 (0)