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

modernisation #485

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 2 additions & 8 deletions .github/workflows/publish_jsr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,10 @@ jobs:

- name: Set up Deno
uses: denoland/setup-deno@v1

- name: Check Format
run: deno fmt --check

- name: Convert to JSR package
run: deno run -A tools/convert_to_jsr.ts

- name: Format converted code
run: deno fmt

- name: Lint
run: deno lint

Expand All @@ -40,7 +34,7 @@ jobs:

- name: Build tests container
run: docker-compose build tests

- name: Run tests
run: docker-compose run tests

Expand Down
7 changes: 2 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM denoland/deno:alpine-1.40.3
FROM denoland/deno:alpine
WORKDIR /app

# Install wait utility
Expand All @@ -9,10 +9,7 @@ RUN chmod +x /wait
USER deno

# Cache external libraries
# Test deps caches all main dependencies as well
COPY tests/test_deps.ts tests/test_deps.ts
COPY deps.ts deps.ts
RUN deno cache tests/test_deps.ts
RUN deno cache deno.json

ADD . .
RUN deno cache mod.ts
2 changes: 1 addition & 1 deletion client/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Notice } from "../connection/message.ts";
import type { Notice } from "../connection/message.ts";

/**
* A connection error
Expand Down
4 changes: 2 additions & 2 deletions connection/auth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { crypto, hex } from "../deps.ts";
import { encodeHex } from "@std/encoding";

const encoder = new TextEncoder();

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

// AuthenticationMD5Password
Expand Down
60 changes: 27 additions & 33 deletions connection/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,10 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import {
bold,
BufReader,
BufWriter,
delay,
joinPath,
rgb24,
yellow,
} from "../deps.ts";
import { BufReader, BufWriter } from "@std/io";
import { delay } from "@std/async";
import { bold, rgb24, yellow } from "@std/fmt/colors";
import { join as joinPath } from "@std/path";
import { DeferredStack } from "../utils/deferred.ts";
import { getSocketName, readUInt32BE } from "../utils/utils.ts";
import { PacketWriter } from "./packet.ts";
Expand All @@ -54,7 +49,7 @@ import {
type QueryResult,
ResultType,
} from "../query/query.ts";
import { type ClientConfiguration } from "./connection_params.ts";
import type { ClientConfiguration } from "./connection_params.ts";
import * as scram from "./scram.ts";
import {
ConnectionError,
Expand Down Expand Up @@ -129,7 +124,7 @@ const encoder = new TextEncoder();
export class Connection {
#bufReader!: BufReader;
#bufWriter!: BufWriter;
#conn!: Deno.Conn;
#conn!: Deno.TcpConn | Deno.TlsConn;
connected = false;
#connection_params: ClientConfiguration;
#message_header = new Uint8Array(5);
Expand Down Expand Up @@ -295,7 +290,7 @@ export class Connection {
}

async #openTlsConnection(
connection: Deno.Conn,
connection: Deno.TcpConn,
options: { hostname: string; caCerts: string[] },
) {
this.#conn = await Deno.startTls(connection, options);
Expand Down Expand Up @@ -354,18 +349,19 @@ export class Connection {
// https://www.postgresql.org/docs/14/protocol-flow.html#id-1.10.5.7.11
if (accepts_tls) {
try {
await this.#openTlsConnection(this.#conn, {
await this.#openTlsConnection(this.#conn as Deno.TcpConn, {
hostname,
caCerts: caCertificates,
});
this.#tls = true;
} catch (e) {
if (!tls_enforced) {
console.error(
bold(yellow("TLS connection failed with message: ")) +
e.message +
"\n" +
bold("Defaulting to non-encrypted connection"),
`${
bold(yellow("TLS connection failed with message:"))
} ${e.message}\n${
bold("Defaulting to non-encrypted connection")
}`,
);
await this.#openConnection({ hostname, port, transport: "tcp" });
this.#tls = false;
Expand All @@ -384,7 +380,7 @@ export class Connection {
}

try {
let startup_response;
let startup_response: Message | undefined;
try {
startup_response = await this.#sendStartupMessage();
} catch (e) {
Expand All @@ -395,18 +391,16 @@ export class Connection {
throw new Error(
"The certificate used to secure the TLS connection is invalid.",
);
} else {
console.error(
bold(yellow("TLS connection failed with message: ")) +
e.message +
"\n" +
bold("Defaulting to non-encrypted connection"),
);
await this.#openConnection({ hostname, port, transport: "tcp" });
this.#tls = false;
this.#transport = "tcp";
startup_response = await this.#sendStartupMessage();
}
console.error(
`${
bold(yellow("TLS connection failed with message:"))
} ${e.message}\n${bold("Defaulting to non-encrypted connection")}`,
);
await this.#openConnection({ hostname, port, transport: "tcp" });
this.#tls = false;
this.#transport = "tcp";
startup_response = await this.#sendStartupMessage();
} else {
throw e;
}
Expand Down Expand Up @@ -783,16 +777,16 @@ export class Connection {
if (hasBinaryArgs) {
this.#packetWriter.addInt16(query.args.length);

query.args.forEach((arg) => {
for (const arg of query.args) {
this.#packetWriter.addInt16(arg instanceof Uint8Array ? 1 : 0);
});
}
} else {
this.#packetWriter.addInt16(0);
}

this.#packetWriter.addInt16(query.args.length);

query.args.forEach((arg) => {
for (const arg of query.args) {
if (arg === null || typeof arg === "undefined") {
this.#packetWriter.addInt32(-1);
} else if (arg instanceof Uint8Array) {
Expand All @@ -803,7 +797,7 @@ export class Connection {
this.#packetWriter.addInt32(byteLength);
this.#packetWriter.addString(arg);
}
});
}

this.#packetWriter.addInt16(0);
const buffer = this.#packetWriter.flush(0x42);
Expand Down
8 changes: 4 additions & 4 deletions connection/connection_params.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { parseConnectionUri } from "../utils/utils.ts";
import { ConnectionParamsError } from "../client/error.ts";
import { fromFileUrl, isAbsolute } from "../deps.ts";
import { OidType } from "../query/oid.ts";
import { DebugControls } from "../debug.ts";
import { ParseArrayFunction } from "../query/array_parser.ts";
import { fromFileUrl, isAbsolute } from "@std/path";
import type { OidType } from "../query/oid.ts";
import type { DebugControls } from "../debug.ts";
import type { ParseArrayFunction } from "../query/array_parser.ts";

/**
* The connection string must match the following URI structure. All parameters but database and user are optional
Expand Down
2 changes: 1 addition & 1 deletion connection/packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { copy } from "../deps.ts";
import { copy } from "@std/bytes";
import { readInt16BE, readInt32BE } from "../utils/utils.ts";

export class PacketReader {
Expand Down
10 changes: 5 additions & 5 deletions connection/scram.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { base64 } from "../deps.ts";
import { decodeBase64, encodeBase64 } from "@std/encoding";

/** Number of random bytes used to generate a nonce */
const defaultNonceSize = 16;
Expand Down Expand Up @@ -132,7 +132,7 @@ function escape(str: string): string {
}

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

function parseScramAttributes(message: string): Record<string, string> {
Expand Down Expand Up @@ -221,7 +221,7 @@ export class Client {
throw new Error(Reason.BadSalt);
}
try {
salt = base64.decodeBase64(attrs.s);
salt = 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.encodeBase64(
const proof = 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.encodeBase64(
const verifier = encodeBase64(
await computeScramSignature(
this.#auth_message,
this.#key_signatures.server,
Expand Down
17 changes: 15 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
{
"lock": false,
"name": "@bartlomieju/postgres",
"version": "0.19.3",
"exports": "./mod.ts"
"version": "0.20.0",
"exports": "./mod.ts",
"imports": {
"@std/assert": "jsr:@std/assert@^1.0.2",
"@std/async": "jsr:@std/async@^1.0.3",
"@std/bytes": "jsr:@std/bytes@^1.0.2",
"@std/datetime": "jsr:@std/datetime@^0.224.4",
"@std/encoding": "jsr:@std/encoding@^1.0.1",
"@std/fmt": "jsr:@std/fmt@^1.0.0",
"@std/io": "jsr:@std/io@^0.224.4",
"@std/path": "jsr:@std/path@^1.0.2"
},
"tasks": {
"test": "deno test --allow-net --allow-env --allow-read"
}
}
18 changes: 0 additions & 18 deletions deps.ts

This file was deleted.

25 changes: 13 additions & 12 deletions query/decode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Oid, OidType, OidTypes, OidValue } from "./oid.ts";
import { bold, yellow } from "../deps.ts";
import { bold, yellow } from "@std/fmt/colors";
import type { OidType, OidValue } from "./oid.ts";
import { Oid, OidTypes } from "./oid.ts";
import {
decodeBigint,
decodeBigintArray,
Expand Down Expand Up @@ -35,7 +36,7 @@ import {
decodeTid,
decodeTidArray,
} from "./decoders.ts";
import { ClientControls } from "../connection/connection_params.ts";
import type { ClientControls } from "../connection/connection_params.ts";
import { parseArray } from "./array_parser.ts";

export class Column {
Expand Down Expand Up @@ -198,10 +199,9 @@ function decodeText(value: string, typeOid: number) {
}
} catch (_e) {
console.error(
bold(yellow(`Error decoding type Oid ${typeOid} value`)) +
_e.message +
"\n" +
bold("Defaulting to null."),
`${
bold(yellow(`Error decoding type Oid ${typeOid} value`))
}${_e.message}\n${bold("Defaulting to null.")}`,
);
// If an error occurred during decoding, return null
return null;
Expand All @@ -224,9 +224,10 @@ export function decode(

if (decoderFunc) {
return decoderFunc(strValue, column.typeOid, parseArray);
} // if no custom decoder is found and the oid is for an array type, check if there is
}
// if no custom decoder is found and the oid is for an array type, check if there is
sachaw marked this conversation as resolved.
Show resolved Hide resolved
// a decoder for the base type and use that with the array parser
else if (oidType?.includes("_array")) {
if (oidType?.includes("_array")) {
const baseOidType = oidType.replace("_array", "") as OidType;
// check if the base type is in the Oid object
if (baseOidType in Oid) {
Expand All @@ -251,9 +252,9 @@ export function decode(
// else, default to 'auto' mode, which uses the typeOid to determine the decoding strategy
if (column.format === Format.BINARY) {
return decodeBinary();
} else if (column.format === Format.TEXT) {
}
if (column.format === Format.TEXT) {
return decodeText(strValue, column.typeOid);
} else {
throw new Error(`Unknown column format: ${column.format}`);
}
throw new Error(`Unknown column format: ${column.format}`);
}
Loading
Loading