Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/auth/pbkdf2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export async function pbkdf2(
return crypto.subtle.deriveBits(
{
name: "PBKDF2",
salt: salt,
iterations: iterations,
salt: salt as BufferSource,
iterations,
hash: {
name: algo,
},
Expand Down
6 changes: 5 additions & 1 deletion src/protocol/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export function setHeader(
}

export function parseHeader(buffer: Uint8Array): MessageHeader {
const view = new DataView(buffer.buffer);
const view = new DataView(
buffer.buffer,
buffer.byteOffset,
buffer.byteLength,
);
return {
messageLength: view.getUint32(0, true),
requestId: view.getUint32(4, true),
Expand Down
33 changes: 21 additions & 12 deletions src/protocol/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ function serializeSections(

view.setUint8(0, 1);
view.setUint32(1, section1.byteLength - 1, true);
let pos = 4;
let pos = 5;

// write identifuer
section1.set(identifier, pos);
pos += identifier.byteLength;

// write documents
for (const doc of docs) {
section1.set(doc, pos);
pos += doc.byteLength;
Expand Down Expand Up @@ -103,29 +108,29 @@ export function deserializeMessage(
header: MessageHeader,
buffer: Uint8Array,
): Message {
const view = new DataView(buffer.buffer);
const view = new DataView(
buffer.buffer,
buffer.byteOffset,
buffer.byteLength,
);

const flags = view.getInt32(0);
const flags = view.getInt32(0, true);
const sections: Section[] = [];

let pos = 4;
while (pos < view.byteLength) {
while (pos < buffer.byteLength) {
const kind = view.getInt8(pos);
pos++;
if (kind === 0) {
const docLen = view.getInt32(pos, true);
const document = deserialize(
new Uint8Array(view.buffer.slice(pos, pos + docLen)),
);
const document = deserialize(buffer.slice(pos, pos + docLen));
pos += docLen;
sections.push({ document });
} else if (kind === 1) {
const len = view.getInt32(pos, true);
const sectionBody = new Uint8Array(
view.buffer.slice(pos + 4, pos + len - 4),
);
const sectionBody = buffer.slice(pos + 4, pos + len);
const identifierEndPos = sectionBody.findIndex((byte) => byte === 0);
const identifier = decoder.decode(buffer.slice(0, identifierEndPos));
const identifier = decoder.decode(sectionBody.slice(0, identifierEndPos));
const docsBuffer = sectionBody.slice(identifierEndPos + 1);
const documents = parseDocuments(docsBuffer);
pos += len;
Expand All @@ -146,7 +151,11 @@ export function deserializeMessage(
function parseDocuments(buffer: Uint8Array): Document[] {
let pos = 0;
const docs = [];
const view = new DataView(buffer.buffer);
const view = new DataView(
buffer.buffer,
buffer.byteOffset,
buffer.byteLength,
);
while (pos < buffer.byteLength) {
const docLen = view.getInt32(pos, true);
const doc = deserialize(buffer.slice(pos, pos + docLen));
Expand Down
16 changes: 14 additions & 2 deletions src/protocol/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,20 @@ export class WireProtocol {
b: number,
): Promise<Uint8Array | undefined> {
const reader = this.#conn.readable.getReader({ mode: "byob" });
const { value } = await reader.read(new Uint8Array(b));
const result = new Uint8Array(b);
let offset = 0;

while (offset < b) {
const { value, done } = await reader.read(new Uint8Array(b - offset));
if (done || !value) {
reader.releaseLock();
return undefined;
}
result.set(value, offset);
offset += value.byteLength;
}

reader.releaseLock();
return value;
return result;
}
}
2 changes: 1 addition & 1 deletion tests/cases/03_crud.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { assertInstanceOf } from "jsr:@std/assert@^0.220.1/assert_instance_of";
import type { Database, MongoClient, ObjectId } from "../../mod.ts";
import {
MongoInvalidArgumentError,
Expand All @@ -10,6 +9,7 @@ import {
afterEach,
assert,
assertEquals,
assertInstanceOf,
assertRejects,
beforeEach,
describe,
Expand Down
1 change: 1 addition & 0 deletions tests/deps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export {
assert,
assertEquals,
assertInstanceOf,
assertNotEquals,
assertRejects,
assertThrows,
Expand Down