Skip to content

Commit eff419f

Browse files
feat: Use modern deno streams (#410)
* modern streams!! Signed-off-by: Jersey <[email protected]> * forgot the hashtag Signed-off-by: Jersey <[email protected]> * fix the issue Signed-off-by: Jersey <[email protected]> * try to prevent negatives here Signed-off-by: Jersey <[email protected]> --------- Signed-off-by: Jersey <[email protected]>
1 parent 749d804 commit eff419f

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

deps.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@ export {
1919
export { crypto as stdCrypto } from "jsr:@std/crypto@^0.220.1/crypto";
2020
export { decodeBase64, encodeBase64 } from "jsr:@std/encoding@^0.220.1/base64";
2121
export { encodeHex } from "jsr:@std/encoding@^0.220.1/hex";
22-
export { BufReader, writeAll } from "jsr:@std/io@^0.220.1";

src/protocol/protocol.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { BufReader, writeAll } from "../../deps.ts";
21
import {
32
MongoDriverError,
43
MongoErrorInfo,
@@ -9,7 +8,6 @@ import { handshake } from "./handshake.ts";
98
import { parseHeader } from "./header.ts";
109
import { deserializeMessage, Message, serializeMessage } from "./message.ts";
1110

12-
type Socket = Deno.Reader & Deno.Writer;
1311
interface CommandTask {
1412
requestId: number;
1513
db: string;
@@ -19,7 +17,7 @@ interface CommandTask {
1917
let nextRequestId = 0;
2018

2119
export class WireProtocol {
22-
#socket: Socket;
20+
#conn: Deno.Conn;
2321
#isPendingResponse = false;
2422
#isPendingRequest = false;
2523
#pendingResponses: Map<number, {
@@ -28,12 +26,10 @@ export class WireProtocol {
2826
// deno-lint-ignore no-explicit-any
2927
reject: (reason?: any) => void;
3028
}> = new Map();
31-
#reader: BufReader;
3229
#commandQueue: CommandTask[] = [];
3330

34-
constructor(socket: Socket) {
35-
this.#socket = socket;
36-
this.#reader = new BufReader(this.#socket);
31+
constructor(socket: Deno.Conn) {
32+
this.#conn = socket;
3733
}
3834

3935
async connect() {
@@ -98,7 +94,9 @@ export class WireProtocol {
9894
],
9995
});
10096

101-
await writeAll(this.#socket, buffer);
97+
const w = this.#conn.writable.getWriter();
98+
await w.write(buffer);
99+
w.releaseLock();
102100
}
103101
this.#isPendingRequest = false;
104102
}
@@ -107,14 +105,14 @@ export class WireProtocol {
107105
if (this.#isPendingResponse) return;
108106
this.#isPendingResponse = true;
109107
while (this.#pendingResponses.size > 0) {
110-
const headerBuffer = await this.#reader.readFull(new Uint8Array(16));
108+
const headerBuffer = await this.read_socket(16);
111109
if (!headerBuffer) {
112110
throw new MongoDriverError("Invalid response header");
113111
}
114112
const header = parseHeader(headerBuffer);
115-
const bodyBuffer = await this.#reader.readFull(
116-
new Uint8Array(header.messageLength - 16),
117-
);
113+
let bodyBytes = header.messageLength - 16;
114+
if (bodyBytes < 0) bodyBytes = 0;
115+
const bodyBuffer = await this.read_socket(header.messageLength - 16);
118116
if (!bodyBuffer) {
119117
throw new MongoDriverError("Invalid response body");
120118
}
@@ -125,4 +123,13 @@ export class WireProtocol {
125123
}
126124
this.#isPendingResponse = false;
127125
}
126+
127+
private async read_socket(
128+
b: number,
129+
): Promise<Uint8Array | undefined> {
130+
const reader = this.#conn.readable.getReader({ mode: "byob" });
131+
const { value } = await reader.read(new Uint8Array(b));
132+
reader.releaseLock();
133+
return value;
134+
}
128135
}

0 commit comments

Comments
 (0)