Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use native Promise.withResolvers and bump deno std #407

Merged
merged 3 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020-2022 the deno_mongo authors
Copyright (c) 2020-2023 the deno_mongo authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 5 additions & 7 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export * from "https://deno.land/x/[email protected]/mod.js";
export { writeAll } from "https://deno.land/[email protected]/streams/write_all.ts";
export { crypto } from "https://deno.land/[email protected]/crypto/mod.ts";
export { BufReader } from "https://deno.land/[email protected]/io/mod.ts";
export { deferred } from "https://deno.land/[email protected]/async/deferred.ts";
export type { Deferred } from "https://deno.land/[email protected]/async/deferred.ts";
export * as b64 from "https://deno.land/[email protected]/encoding/base64.ts";
export * as hex from "https://deno.land/[email protected]/encoding/hex.ts";
export { writeAll } from "https://deno.land/[email protected]/streams/write_all.ts";
export { crypto } from "https://deno.land/[email protected]/crypto/mod.ts";
export { BufReader } from "https://deno.land/[email protected]/io/mod.ts";
export * as b64 from "https://deno.land/[email protected]/encoding/base64.ts";
export * as hex from "https://deno.land/[email protected]/encoding/hex.ts";
12 changes: 6 additions & 6 deletions src/auth/scram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function clientFirstMessageBare(username: string, nonce: Uint8Array) {
...enc.encode("n="),
...enc.encode(username),
...enc.encode(",r="),
...enc.encode(b64.encode(nonce)),
...enc.encode(b64.encodeBase64(nonce)),
],
);
}
Expand Down Expand Up @@ -160,7 +160,7 @@ export async function continueScramConversation(
const withoutProof = `c=biws,r=${rnonce}`;
const saltedPassword = await HI(
processedPassword,
b64.decode(salt),
b64.decodeBase64(salt),
iterations,
cryptoMethod,
);
Expand Down Expand Up @@ -193,7 +193,7 @@ export async function continueScramConversation(
);
if (
!compareDigest(
b64.decode(parsedResponse.v),
b64.decodeBase64(parsedResponse.v),
new Uint8Array(serverSignature),
)
) {
Expand Down Expand Up @@ -260,7 +260,7 @@ export async function passwordDigest(
"MD5",
enc.encode(`${username}:mongo:${password}`),
);
return dec.decode(hex.encode(new Uint8Array(result)));
return hex.encodeHex(new Uint8Array(result));
}

// XOR two buffers
Expand All @@ -275,7 +275,7 @@ export function xor(_a: ArrayBuffer, _b: ArrayBuffer) {
res[i] = a[i] ^ b[i];
}

return b64.encode(res);
return b64.encodeBase64(res);
}

export function H(method: CryptoMethod, text: BufferSource) {
Expand Down Expand Up @@ -333,7 +333,7 @@ export async function HI(
cryptoMethod: CryptoMethod,
): Promise<ArrayBuffer> {
// omit the work if already generated
const key = [data, b64.encode(salt), iterations].join(
const key = [data, b64.encodeBase64(salt), iterations].join(
"_",
);
if (_hiCache[key] !== undefined) {
Expand Down
27 changes: 15 additions & 12 deletions src/protocol/protocol.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
BufReader,
Deferred,
deferred,
Document,
writeAll,
} from "../../deps.ts";
import { BufReader, Document, writeAll } from "../../deps.ts";
import {
MongoDriverError,
MongoErrorInfo,
Expand All @@ -27,7 +21,12 @@ export class WireProtocol {
#socket: Socket;
#isPendingResponse = false;
#isPendingRequest = false;
#pendingResponses: Map<number, Deferred<Message>> = new Map();
#pendingResponses: Map<number, {
promise: Promise<Message>;
resolve: (value: Message | PromiseLike<Message>) => void;
// deno-lint-ignore no-explicit-any
reject: (reason?: any) => void;
}> = new Map();
#reader: BufReader;
#commandQueue: CommandTask[] = [];

Expand Down Expand Up @@ -62,10 +61,10 @@ export class WireProtocol {
this.#commandQueue.push(commandTask);
this.send();

const pendingMessage = deferred<Message>();
const pendingMessage = Promise.withResolvers<Message>();
this.#pendingResponses.set(requestId, pendingMessage);
this.receive();
const message = await pendingMessage;
const message = await pendingMessage.promise;

let documents: T[] = [];

Expand Down Expand Up @@ -108,12 +107,16 @@ export class WireProtocol {
this.#isPendingResponse = true;
while (this.#pendingResponses.size > 0) {
const headerBuffer = await this.#reader.readFull(new Uint8Array(16));
if (!headerBuffer) throw new MongoDriverError("Invalid response header");
if (!headerBuffer) {
throw new MongoDriverError("Invalid response header");
}
const header = parseHeader(headerBuffer);
const bodyBuffer = await this.#reader.readFull(
new Uint8Array(header.messageLength - 16),
);
if (!bodyBuffer) throw new MongoDriverError("Invalid response body");
if (!bodyBuffer) {
throw new MongoDriverError("Invalid response body");
}
const reply = deserializeMessage(header, bodyBuffer);
const pendingMessage = this.#pendingResponses.get(header.responseTo);
this.#pendingResponses.delete(header.responseTo);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/saslprep/load_code_points.ts

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions tests/cases/07_worker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { deferred } from "../../deps.ts";
import { assertEquals, describe, it } from "../test.deps.ts";

describe("worker", () => {
Expand All @@ -9,11 +8,11 @@ describe("worker", () => {
import.meta.resolve("./import_worker.ts"),
{ type: "module" },
);
const p = deferred<string>();
const p = Promise.withResolvers<string>();
importWorker.onmessage = (e) => p.resolve(e.data);
importWorker.postMessage("startWorker");

const result = await p;
const result = await p.promise;
importWorker.terminate();
assertEquals(result, "done");
},
Expand Down
6 changes: 3 additions & 3 deletions tests/test.deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export {
assertNotEquals,
assertRejects,
assertThrows,
} from "https://deno.land/std@0.202.0/assert/mod.ts";
export { equals as bytesEquals } from "https://deno.land/std@0.202.0/bytes/equals.ts";
} from "https://deno.land/std@0.209.0/assert/mod.ts";
export { equals as bytesEquals } from "https://deno.land/std@0.209.0/bytes/equals.ts";
export * as semver from "https://deno.land/x/[email protected]/mod.ts";
export {
afterAll,
Expand All @@ -14,4 +14,4 @@ export {
beforeEach,
describe,
it,
} from "https://deno.land/std@0.202.0/testing/bdd.ts";
} from "https://deno.land/std@0.209.0/testing/bdd.ts";
Loading