Skip to content
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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Install deno
uses: denolib/setup-deno@master
with:
deno-version: 1.4.0
deno-version: 1.6.0

- name: Check formatting
run: deno fmt --check
Expand Down
2 changes: 1 addition & 1 deletion array_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ArrayParser {
consumeDimensions(): void {
if (this.source[0] === "[") {
while (!this.isEof()) {
let char = this.nextCharacter();
const char = this.nextCharacter();
if (char.value === "=") break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ function decodeBytea(byteaStr: string): Uint8Array {
}

function decodeByteaHex(byteaStr: string): Uint8Array {
let bytesStr = byteaStr.slice(2);
let bytes = new Uint8Array(bytesStr.length / 2);
const bytesStr = byteaStr.slice(2);
const bytes = new Uint8Array(bytesStr.length / 2);
for (let i = 0, j = 0; i < bytesStr.length; i += 2, j++) {
bytes[j] = parseInt(bytesStr[i] + bytesStr[i + 1], HEX);
}
return bytes;
}

function decodeByteaEscape(byteaStr: string): Uint8Array {
let bytes = [];
const bytes = [];
let i = 0;
let k = 0;
while (i < byteaStr.length) {
Expand Down
10 changes: 5 additions & 5 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { BufReader, BufWriter } from "https://deno.land/std@0.69.0/io/bufio.ts";
export { copyBytes } from "https://deno.land/std@0.67.0/bytes/mod.ts";
export { deferred } from "https://deno.land/std@0.69.0/async/deferred.ts";
export type { Deferred } from "https://deno.land/std@0.69.0/async/deferred.ts";
export { createHash } from "https://deno.land/std@0.67.0/hash/mod.ts";
export { BufReader, BufWriter } from "https://deno.land/std@0.80.0/io/bufio.ts";
export { copy } from "https://deno.land/std@0.80.0/bytes/mod.ts";
export { deferred } from "https://deno.land/std@0.80.0/async/deferred.ts";
export type { Deferred } from "https://deno.land/std@0.80.0/async/deferred.ts";
export { createHash } from "https://deno.land/std@0.80.0/hash/mod.ts";
4 changes: 2 additions & 2 deletions encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function encodeDate(date: Date): string {

function escapeArrayElement(value: unknown): string {
// deno-lint-ignore no-explicit-any
let strValue = (value as any).toString();
const strValue = (value as any).toString();
const escapedValue = strValue.replace(/\\/g, "\\\\").replace(/"/g, '\\"');

return `"${escapedValue}"`;
Expand Down Expand Up @@ -73,7 +73,7 @@ function encodeArray(array: Array<unknown>): string {
}

function encodeBytes(value: Uint8Array): string {
let hex = Array.from(value)
const hex = Array.from(value)
.map((val) => (val < 10 ? `0${val.toString(16)}` : val.toString(16)))
.join("");
return `\\x${hex}`;
Expand Down
21 changes: 21 additions & 0 deletions oid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,25 @@ export const Oid = {
xid: 28,
cid: 29,
oidvector: 30,
// deno-lint-ignore camelcase
pg_ddl_command: 32,
// deno-lint-ignore camelcase
pg_type: 71,
// deno-lint-ignore camelcase
pg_attribute: 75,
// deno-lint-ignore camelcase
pg_proc: 81,
// deno-lint-ignore camelcase
pg_class: 83,
json: 114,
xml: 142,
_xml: 143,
// deno-lint-ignore camelcase
pg_node_tree: 194,
// deno-lint-ignore camelcase
json_array: 199,
smgr: 210,
// deno-lint-ignore camelcase
index_am_handler: 325,
point: 600,
lseg: 601,
Expand Down Expand Up @@ -91,6 +99,7 @@ export const Oid = {
interval: 1186,
_interval: 1187,
_numeric: 1231,
// deno-lint-ignore camelcase
pg_database: 1248,
_cstring: 1263,
timetz: 1266,
Expand Down Expand Up @@ -118,21 +127,30 @@ export const Oid = {
anyarray: 2277,
void: 2278,
trigger: 2279,
// deno-lint-ignore camelcase
language_handler: 2280,
internal: 2281,
opaque: 2282,
anyelement: 2283,
_record: 2287,
anynonarray: 2776,
// deno-lint-ignore camelcase
pg_authid: 2842,
// deno-lint-ignore camelcase
pg_auth_members: 2843,
// deno-lint-ignore camelcase
_txid_snapshot: 2949,
uuid: 2950,
_uuid: 2951,
// deno-lint-ignore camelcase
txid_snapshot: 2970,
// deno-lint-ignore camelcase
fdw_handler: 3115,
// deno-lint-ignore camelcase
pg_lsn: 3220,
// deno-lint-ignore camelcase
_pg_lsn: 3221,
// deno-lint-ignore camelcase
tsm_handler: 3310,
anyenum: 3500,
tsvector: 3614,
Expand All @@ -146,8 +164,10 @@ export const Oid = {
regdictionary: 3769,
_regdictionary: 3770,
jsonb: 3802,
// deno-lint-ignore camelcase
jsonb_array: 3807,
anyrange: 3831,
// deno-lint-ignore camelcase
event_trigger: 3838,
int4range: 3904,
_int4range: 3905,
Expand All @@ -161,6 +181,7 @@ export const Oid = {
_daterange: 3913,
int8range: 3926,
_int8range: 3927,
// deno-lint-ignore camelcase
pg_shseclabel: 4066,
regnamespace: 4089,
_regnamespace: 4090,
Expand Down
12 changes: 6 additions & 6 deletions packet_writer.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 { copyBytes } from "./deps.ts";
import { copy } from "./deps.ts";

export class PacketWriter {
private size: number;
Expand All @@ -49,7 +49,7 @@ export class PacketWriter {
// https://stackoverflow.com/questions/2269063/buffer-growth-strategy
const newSize = oldBuffer.length + (oldBuffer.length >> 1) + size;
this.buffer = new Uint8Array(newSize);
copyBytes(oldBuffer, this.buffer);
copy(oldBuffer, this.buffer);
}
}

Expand All @@ -76,7 +76,7 @@ export class PacketWriter {
} else {
const encodedStr = this.encoder.encode(string);
this._ensure(encodedStr.byteLength + 1); // +1 for null terminator
copyBytes(encodedStr, this.buffer, this.offset);
copy(encodedStr, this.buffer, this.offset);
this.offset += encodedStr.byteLength;
}

Expand All @@ -90,7 +90,7 @@ export class PacketWriter {
}

this._ensure(1);
copyBytes(this.encoder.encode(c), this.buffer, this.offset);
copy(this.encoder.encode(c), this.buffer, this.offset);
this.offset++;
return this;
}
Expand All @@ -99,14 +99,14 @@ export class PacketWriter {
string = string || "";
const encodedStr = this.encoder.encode(string);
this._ensure(encodedStr.byteLength);
copyBytes(encodedStr, this.buffer, this.offset);
copy(encodedStr, this.buffer, this.offset);
this.offset += encodedStr.byteLength;
return this;
}

add(otherBuffer: Uint8Array) {
this._ensure(otherBuffer.length);
copyBytes(otherBuffer, this.buffer, this.offset);
copy(otherBuffer, this.buffer, this.offset);
this.offset += otherBuffer.length;
return this;
}
Expand Down
3 changes: 1 addition & 2 deletions test_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export * from "./deps.ts";
export {
assert,
assertEquals,
assertStringContains,
assertThrows,
assertThrowsAsync,
} from "https://deno.land/std@0.67.0/testing/asserts.ts";
} from "https://deno.land/std@0.80.0/testing/asserts.ts";
4 changes: 2 additions & 2 deletions tests/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { test } = Deno;
import { Client, PostgresError } from "../mod.ts";
import { assert, assertStringContains } from "../test_deps.ts";
import { assert } from "../test_deps.ts";
import { TEST_CONNECTION_PARAMS } from "./constants.ts";

test("badAuthData", async function () {
Expand All @@ -15,7 +15,7 @@ test("badAuthData", async function () {
} catch (e) {
thrown = true;
assert(e instanceof PostgresError);
assertStringContains(e.message, "password authentication failed for user");
assert(e.message.includes("password authentication failed for user"));
} finally {
await client.end();
}
Expand Down
8 changes: 4 additions & 4 deletions tests/data_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,17 +269,17 @@ testClient(async function bpcharNestedArray() {
});

testClient(async function jsonArray() {
const json_array = await CLIENT.query(
const jsonArray = await CLIENT.query(
`SELECT ARRAY_AGG(A) FROM (
SELECT JSON_BUILD_OBJECT( 'X', '1' ) AS A
UNION ALL
SELECT JSON_BUILD_OBJECT( 'Y', '2' ) AS A
) A`,
);

assertEquals(json_array.rows[0][0], [{ X: "1" }, { Y: "2" }]);
assertEquals(jsonArray.rows[0][0], [{ X: "1" }, { Y: "2" }]);

const json_array_nested = await CLIENT.query(
const jsonArrayNested = await CLIENT.query(
`SELECT ARRAY[ARRAY[ARRAY_AGG(A), ARRAY_AGG(A)], ARRAY[ARRAY_AGG(A), ARRAY_AGG(A)]] FROM (
SELECT JSON_BUILD_OBJECT( 'X', '1' ) AS A
UNION ALL
Expand All @@ -288,7 +288,7 @@ testClient(async function jsonArray() {
);

assertEquals(
json_array_nested.rows[0][0],
jsonArrayNested.rows[0][0],
[
[
[{ X: "1" }, { Y: "2" }],
Expand Down
8 changes: 4 additions & 4 deletions tests/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ test("encodeObject", function () {
});

test("encodeUint8Array", function () {
const buf_1 = new Uint8Array([1, 2, 3]);
const buf_2 = new Uint8Array([2, 10, 500]);
const buf1 = new Uint8Array([1, 2, 3]);
const buf2 = new Uint8Array([2, 10, 500]);

assertEquals("\\x010203", encode(buf_1));
assertEquals("\\x02af4", encode(buf_2));
assertEquals("\\x010203", encode(buf1));
assertEquals("\\x02af4", encode(buf2));
});

test("encodeArray", function () {
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function getTestClient(
client: Client,
defSetupQueries?: Array<string>,
) {
return async function testClient(
return function testClient(
t: Deno.TestDefinition["fn"],
setupQueries?: Array<string>,
) {
Expand Down
14 changes: 7 additions & 7 deletions tests/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Pool } from "../pool.ts";
import { delay } from "../utils.ts";
import { DEFAULT_SETUP, TEST_CONNECTION_PARAMS } from "./constants.ts";

async function testPool(
function testPool(
t: (pool: Pool) => void | Promise<void>,
setupQueries?: Array<string> | null,
lazy?: boolean,
Expand Down Expand Up @@ -63,13 +63,13 @@ testPool(
await p;
assertEquals(POOL.available, 1);

const qs_thunks = [...Array(25)].map((_, i) =>
const qsThunks = [...Array(25)].map((_, i) =>
POOL.query("SELECT pg_sleep(0.1) is null, $1::text as id;", i)
);
const qs_promises = Promise.all(qs_thunks);
const qsPromises = Promise.all(qsThunks);
await delay(1);
assertEquals(POOL.available, 0);
const qs = await qs_promises;
const qs = await qsPromises;
assertEquals(POOL.available, 10);
assertEquals(POOL.size, 10);

Expand Down Expand Up @@ -101,13 +101,13 @@ testPool(async function manyQueries(POOL) {
await p;
assertEquals(POOL.available, 10);

const qs_thunks = [...Array(25)].map((_, i) =>
const qsThunks = [...Array(25)].map((_, i) =>
POOL.query("SELECT pg_sleep(0.1) is null, $1::text as id;", i)
);
const qs_promises = Promise.all(qs_thunks);
const qsPromises = Promise.all(qsThunks);
await delay(1);
assertEquals(POOL.available, 0);
const qs = await qs_promises;
const qs = await qsPromises;
assertEquals(POOL.available, 10);
assertEquals(POOL.size, 10);

Expand Down
10 changes: 6 additions & 4 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export interface DsnResult {
export function parseDsn(dsn: string): DsnResult {
//URL object won't parse the URL if it doesn't recognize the protocol
//This line replaces the protocol with http and then leaves it up to URL
const [protocol, stripped_url] = dsn.match(/(?:(?!:\/\/).)+/g) ?? ["", ""];
const url = new URL(`http:${stripped_url}`);
const [protocol, strippedUrl] = dsn.match(/(?:(?!:\/\/).)+/g) ?? ["", ""];
const url = new URL(`http:${strippedUrl}`);

return {
driver: protocol,
Expand All @@ -88,8 +88,10 @@ export function parseDsn(dsn: string): DsnResult {
};
}

export function delay<T>(ms: number, value?: T): Promise<T> {
return new Promise<T>((resolve, reject) => {
export function delay(ms: number): Promise<void>;
export function delay<T>(ms: number, value: T): Promise<T>;
export function delay(ms: number, value?: unknown) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(value);
}, ms);
Expand Down