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

deps(std): bump dependencies and prefer their lighter submodules #422

Merged
merged 11 commits into from
Feb 5, 2024
5 changes: 1 addition & 4 deletions connection/auth.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { crypto, hex } from "../deps.ts";

const encoder = new TextEncoder();
const decoder = new TextDecoder();

async function md5(bytes: Uint8Array): Promise<string> {
return decoder.decode(
hex.encode(new Uint8Array(await crypto.subtle.digest("MD5", bytes))),
);
return hex.encodeHex(await crypto.subtle.digest("MD5", bytes));
}

// AuthenticationMD5Password
Expand Down
8 changes: 4 additions & 4 deletions connection/scram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ function escape(str: string): string {
}

function generateRandomNonce(size: number): string {
return base64.encode(crypto.getRandomValues(new Uint8Array(size)));
return base64.encodeBase64(crypto.getRandomValues(new Uint8Array(size)));
}

function parseScramAttributes(message: string): Record<string, string> {
Expand Down Expand Up @@ -223,7 +223,7 @@ export class Client {
throw new Error(Reason.BadSalt);
}
try {
salt = base64.decode(attrs.s);
salt = base64.decodeBase64(attrs.s);
} catch {
throw new Error(Reason.BadSalt);
}
Expand Down Expand Up @@ -261,7 +261,7 @@ export class Client {

this.#auth_message += "," + responseWithoutProof;

const proof = base64.encode(
const proof = base64.encodeBase64(
computeScramProof(
await computeScramSignature(
this.#auth_message,
Expand Down Expand Up @@ -294,7 +294,7 @@ export class Client {
throw new Error(attrs.e ?? Reason.Rejected);
}

const verifier = base64.encode(
const verifier = base64.encodeBase64(
await computeScramSignature(
this.#auth_message,
this.#key_signatures.server,
Expand Down
26 changes: 10 additions & 16 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
export * as base64 from "https://deno.land/std@0.160.0/encoding/base64.ts";
export * as hex from "https://deno.land/std@0.160.0/encoding/hex.ts";
export * as date from "https://deno.land/std@0.160.0/datetime/mod.ts";
export {
BufReader,
BufWriter,
} from "https://deno.land/std@0.160.0/io/buffer.ts";
export { copy } from "https://deno.land/std@0.160.0/bytes/mod.ts";
export { crypto } from "https://deno.land/std@0.160.0/crypto/mod.ts";
export {
type Deferred,
deferred,
delay,
} from "https://deno.land/std@0.160.0/async/mod.ts";
export { bold, yellow } from "https://deno.land/std@0.160.0/fmt/colors.ts";
export * as base64 from "https://deno.land/std@0.214.0/encoding/base64.ts";
export * as hex from "https://deno.land/std@0.214.0/encoding/hex.ts";
export { parse as parseDate } from "https://deno.land/std@0.214.0/datetime/parse.ts";
export { BufReader } from "https://deno.land/std@0.214.0/io/buf_reader.ts";
export { BufWriter } from "https://deno.land/std@0.214.0/io/buf_writer.ts";
export { copy } from "https://deno.land/std@0.214.0/bytes/copy.ts";
export { crypto } from "https://deno.land/std@0.214.0/crypto/crypto.ts";
export { delay } from "https://deno.land/std@0.214.0/async/delay.ts";
export { bold, yellow } from "https://deno.land/std@0.214.0/fmt/colors.ts";
export {
fromFileUrl,
isAbsolute,
join as joinPath,
} from "https://deno.land/std@0.160.0/path/mod.ts";
} from "https://deno.land/std@0.214.0/path/mod.ts";
4 changes: 2 additions & 2 deletions query/decoders.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { date } from "../deps.ts";
import { parseDate } from "../deps.ts";
import { parseArray } from "./array_parser.ts";
import type {
Box,
Expand Down Expand Up @@ -127,7 +127,7 @@ export function decodeDate(dateStr: string): Date | number {
return Number(-Infinity);
}

return date.parse(dateStr, "yyyy-MM-dd");
return parseDate(dateStr, "yyyy-MM-dd");
}

export function decodeDateArray(value: string) {
Expand Down
31 changes: 15 additions & 16 deletions tests/connection_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {
assertEquals,
assertRejects,
deferred,
copyStream,
joinPath,
streams,
} from "./test_deps.ts";
import {
getClearConfiguration,
Expand Down Expand Up @@ -38,8 +37,8 @@ function createProxy(
aborted = true;
});
await Promise.all([
streams.copy(conn, outbound),
streams.copy(outbound, conn),
copyStream(conn, outbound),
copyStream(outbound, conn),
]).catch(() => {});

if (!aborted) {
Expand Down Expand Up @@ -374,15 +373,15 @@ Deno.test("Closes connection on bad TLS availability verification", async functi
);

// Await for server initialization
const initialized = deferred();
const initialized = Promise.withResolvers();
server.onmessage = ({ data }) => {
if (data !== "initialized") {
initialized.reject(`Unexpected message "${data}" received from worker`);
}
initialized.resolve();
initialized.resolve(null);
};
server.postMessage("initialize");
await initialized;
await initialized.promise;

const client = new Client({
database: "none",
Expand Down Expand Up @@ -413,17 +412,17 @@ Deno.test("Closes connection on bad TLS availability verification", async functi
await client.end();
}

const closed = deferred();
const closed = Promise.withResolvers();
server.onmessage = ({ data }) => {
if (data !== "closed") {
closed.reject(
`Unexpected message "${data}" received from worker`,
);
}
closed.resolve();
closed.resolve(null);
};
server.postMessage("close");
await closed;
await closed.promise;
server.terminate();

assertEquals(bad_tls_availability_message, true);
Expand All @@ -438,15 +437,15 @@ async function mockReconnection(attempts: number) {
);

// Await for server initialization
const initialized = deferred();
const initialized = Promise.withResolvers();
server.onmessage = ({ data }) => {
if (data !== "initialized") {
initialized.reject(`Unexpected message "${data}" received from worker`);
}
initialized.resolve();
initialized.resolve(null);
};
server.postMessage("initialize");
await initialized;
await initialized.promise;

const client = new Client({
connection: {
Expand Down Expand Up @@ -483,17 +482,17 @@ async function mockReconnection(attempts: number) {
await client.end();
}

const closed = deferred();
const closed = Promise.withResolvers();
server.onmessage = ({ data }) => {
if (data !== "closed") {
closed.reject(
`Unexpected message "${data}" received from worker`,
);
}
closed.resolve();
closed.resolve(null);
};
server.postMessage("close");
await closed;
await closed.promise;
server.terminate();

// If reconnections are set to zero, it will attempt to connect at least once, but won't
Expand Down
14 changes: 7 additions & 7 deletions tests/data_types_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals, base64, date } from "./test_deps.ts";
import { assertEquals, base64, formatDate, parseDate } from "./test_deps.ts";
import { getMainConfiguration } from "./config.ts";
import { generateSimpleClientTest } from "./helpers.ts";
import type {
Expand Down Expand Up @@ -34,7 +34,7 @@ function generateRandomPoint(max_value = 100): Point {

const CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
function randomBase64(): string {
return base64.encode(
return base64.encodeBase64(
Array.from(
{ length: Math.ceil(Math.random() * 256) },
() => CHARS[Math.floor(Math.random() * CHARS.length)],
Expand Down Expand Up @@ -671,7 +671,7 @@ Deno.test(
`SELECT decode('${base64_string}','base64')`,
);

assertEquals(result.rows[0][0], base64.decode(base64_string));
assertEquals(result.rows[0][0], base64.decodeBase64(base64_string));
}),
);

Expand All @@ -691,7 +691,7 @@ Deno.test(

assertEquals(
result.rows[0][0],
strings.map(base64.decode),
strings.map(base64.decodeBase64),
);
}),
);
Expand Down Expand Up @@ -931,7 +931,7 @@ Deno.test(
);

assertEquals(result.rows[0], [
date.parse(date_text, "yyyy-MM-dd"),
parseDate(date_text, "yyyy-MM-dd"),
Infinity,
]);
}),
Expand All @@ -941,7 +941,7 @@ Deno.test(
"date array",
testClient(async (client) => {
await client.queryArray(`SET SESSION TIMEZONE TO '${timezone}'`);
const dates = ["2020-01-01", date.format(new Date(), "yyyy-MM-dd")];
const dates = ["2020-01-01", formatDate(new Date(), "yyyy-MM-dd")];

const { rows: result } = await client.queryArray<[[Date, Date]]>(
"SELECT ARRAY[$1::DATE, $2]",
Expand All @@ -950,7 +950,7 @@ Deno.test(

assertEquals(
result[0][0],
dates.map((d) => date.parse(d, "yyyy-MM-dd")),
dates.map((d) => parseDate(d, "yyyy-MM-dd")),
);
}),
);
Expand Down
5 changes: 3 additions & 2 deletions tests/test_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export {
assertObjectMatch,
assertRejects,
assertThrows,
} from "https://deno.land/std@0.160.0/testing/asserts.ts";
export * as streams from "https://deno.land/std@0.160.0/streams/conversion.ts";
} from "https://deno.land/std@0.214.0/assert/mod.ts";
export { format as formatDate } from "https://deno.land/std@0.214.0/datetime/format.ts";
export { copy as copyStream } from "https://deno.land/std@0.214.0/io/copy.ts";
10 changes: 5 additions & 5 deletions utils/deferred.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Deferred, deferred } from "../deps.ts";
export type Deferred<T> = ReturnType<typeof Promise.withResolvers<T>>;

export class DeferredStack<T> {
#elements: Array<T>;
Expand Down Expand Up @@ -30,9 +30,9 @@ export class DeferredStack<T> {
this.#size++;
return await this.#creator();
}
const d = deferred<T>();
const d = Promise.withResolvers<T>();
this.#queue.push(d);
return await d;
return await d.promise;
}

push(value: T): void {
Expand Down Expand Up @@ -112,9 +112,9 @@ export class DeferredAccessStack<T> {
} else {
// If there are not elements left in the stack, it will await the call until
// at least one is restored and then return it
const d = deferred<T>();
const d = Promise.withResolvers<T>();
this.#queue.push(d);
element = await d;
element = await d.promise;
}

if (!await this.#checkElementInitialization(element)) {
Expand Down
Loading