Skip to content

Commit cbc6a75

Browse files
committed
Build cjs + deno
1 parent 13950af commit cbc6a75

File tree

6 files changed

+36
-38
lines changed

6 files changed

+36
-38
lines changed

cjs/src/connection.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
7575
, connectTimer = timer(connectTimedOut, options.connect_timeout)
7676

7777
let socket = null
78+
, cancelMessage
7879
, result = new Result()
7980
, incoming = Buffer.alloc(0)
8081
, needsTypes = options.fetch_types
@@ -139,16 +140,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
139140
}
140141

141142
async function cancel({ pid, secret }, resolve, reject) {
142-
socket || (socket = await createSocket())
143-
if (!socket)
144-
return
145-
146-
socket.removeAllListeners()
147-
socket = net.Socket()
148-
socket.on('connect', () => socket.write(b().i32(16).i32(80877102).i32(pid).i32(secret).end(16)))
149-
socket.once('error', reject)
150-
socket.once('close', resolve)
151-
connect()
143+
try {
144+
cancelMessage = b().i32(16).i32(80877102).i32(pid).i32(secret).end(16)
145+
await connect()
146+
socket.once('error', reject)
147+
socket.once('close', resolve)
148+
} catch (error) {
149+
reject(error)
150+
}
152151
}
153152

154153
function execute(q) {
@@ -421,7 +420,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
421420
if (socket) {
422421
socket.removeListener('data', data)
423422
socket.removeListener('connect', connected)
424-
socket.readyState !== 'closed' && socket.end(b().X().end())
423+
socket.readyState === 'open' && socket.end(b().X().end())
425424
}
426425
ended && (ended(), ending = ended = null)
427426
}
@@ -955,7 +954,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
955954
}
956955

957956
function StartupMessage() {
958-
return b().inc(4).i16(3).z(2).str(
957+
return cancelMessage || b().inc(4).i16(3).z(2).str(
959958
Object.entries(Object.assign({
960959
user,
961960
database,
@@ -1012,7 +1011,7 @@ function timer(fn, seconds) {
10121011
},
10131012
start() {
10141013
timer && clearTimeout(timer)
1015-
timer = setTimeout(done, seconds * 1000, arguments).unref()
1014+
timer = setTimeout(done, seconds * 1000, arguments)
10161015
}
10171016
}
10181017

cjs/tests/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1774,16 +1774,16 @@ t('Execute', async() => {
17741774

17751775
t('Cancel running query', async() => {
17761776
const query = sql`select pg_sleep(2)`
1777-
setTimeout(() => query.cancel(), 50)
1777+
setTimeout(() => query.cancel(), 200)
17781778
const error = await query.catch(x => x)
17791779
return ['57014', error.code]
17801780
})
17811781

17821782
t('Cancel piped query', async() => {
17831783
await sql`select 1`
1784-
const last = sql`select pg_sleep(0.05)`.execute()
1784+
const last = sql`select pg_sleep(0.2)`.execute()
17851785
const query = sql`select pg_sleep(2) as dig`
1786-
setTimeout(() => query.cancel(), 10)
1786+
setTimeout(() => query.cancel(), 100)
17871787
const error = await query.catch(x => x)
17881788
await last
17891789
return ['57014', error.code]

deno/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ const xs = await sql`
126126
127127
### Query parameters
128128

129-
Parameters are automatically extracted and handled by the database so that SQL injection isn't possible. No special handling is necessary, simply use tagged template literals as usual. **Dynamic queries and query building can be seen in the [next section]()**. // todo
129+
Parameters are automatically extracted and handled by the database so that SQL injection isn't possible. No special handling is necessary, simply use tagged template literals as usual.
130130

131131
```js
132132
const name = 'Mur'
@@ -521,7 +521,7 @@ Do note that you can often achieve the same result using [`WITH` queries (Common
521521
Like - `postgres('connectionURL', { transformation: {...} })`
522522
523523
### Parameters
524-
* `to`: The function to transform the outgoing query column name to, i.e ``SELECT ${ sql('aName') }` to `SELECT a_name` when using `postgres.toCamel`.
524+
* `to`: The function to transform the outgoing query column name to, i.e `SELECT ${ sql('aName') }` to `SELECT a_name` when using `postgres.toCamel`.
525525
* `from`: The function to transform the incoming query result column name to, see example below.
526526
527527
> Both parameters are optional, if not provided, the default transformation function will be used.

deno/src/connection.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
7979
, connectTimer = timer(connectTimedOut, options.connect_timeout)
8080

8181
let socket = null
82+
, cancelMessage
8283
, result = new Result()
8384
, incoming = Buffer.alloc(0)
8485
, needsTypes = options.fetch_types
@@ -143,16 +144,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
143144
}
144145

145146
async function cancel({ pid, secret }, resolve, reject) {
146-
socket || (socket = await createSocket())
147-
if (!socket)
148-
return
149-
150-
socket.removeAllListeners()
151-
socket = net.Socket()
152-
socket.on('connect', () => socket.write(b().i32(16).i32(80877102).i32(pid).i32(secret).end(16)))
153-
socket.once('error', reject)
154-
socket.once('close', resolve)
155-
connect()
147+
try {
148+
cancelMessage = b().i32(16).i32(80877102).i32(pid).i32(secret).end(16)
149+
await connect()
150+
socket.once('error', reject)
151+
socket.once('close', resolve)
152+
} catch (error) {
153+
reject(error)
154+
}
156155
}
157156

158157
function execute(q) {
@@ -425,7 +424,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
425424
if (socket) {
426425
socket.removeListener('data', data)
427426
socket.removeListener('connect', connected)
428-
socket.readyState !== 'closed' && socket.end(b().X().end())
427+
socket.readyState === 'open' && socket.end(b().X().end())
429428
}
430429
ended && (ended(), ending = ended = null)
431430
}
@@ -959,7 +958,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
959958
}
960959

961960
function StartupMessage() {
962-
return b().inc(4).i16(3).z(2).str(
961+
return cancelMessage || b().inc(4).i16(3).z(2).str(
963962
Object.entries(Object.assign({
964963
user,
965964
database,
@@ -1016,7 +1015,7 @@ function timer(fn, seconds) {
10161015
},
10171016
start() {
10181017
timer && clearTimeout(timer)
1019-
timer = (window.timer = setTimeout(done, seconds * 1000, arguments), Deno.unrefTimer(window.timer), window.timer)
1018+
timer = setTimeout(done, seconds * 1000, arguments)
10201019
}
10211020
}
10221021

deno/tests/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1776,16 +1776,16 @@ t('Execute', async() => {
17761776

17771777
t('Cancel running query', async() => {
17781778
const query = sql`select pg_sleep(2)`
1779-
setTimeout(() => query.cancel(), 50)
1779+
setTimeout(() => query.cancel(), 200)
17801780
const error = await query.catch(x => x)
17811781
return ['57014', error.code]
17821782
})
17831783

17841784
t('Cancel piped query', async() => {
17851785
await sql`select 1`
1786-
const last = sql`select pg_sleep(0.05)`.execute()
1786+
const last = sql`select pg_sleep(0.2)`.execute()
17871787
const query = sql`select pg_sleep(2) as dig`
1788-
setTimeout(() => query.cancel(), 10)
1788+
setTimeout(() => query.cancel(), 100)
17891789
const error = await query.catch(x => x)
17901790
await last
17911791
return ['57014', error.code]

deno/types/index.d.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -433,12 +433,12 @@ declare namespace postgres {
433433
writable(options?: {
434434
highWaterMark?: number,
435435
start?: number
436-
}): Promise<Writable>;
436+
}): Promise<import('node:stream').Writable>;
437437
readable(options?: {
438438
highWaterMark?: number,
439439
start?: number,
440440
end?: number
441-
}): Promise<Readable>;
441+
}): Promise<import('node:stream').Readable>;
442442

443443
close(): Promise<void>;
444444
tell(): Promise<void>;
@@ -518,8 +518,8 @@ declare namespace postgres {
518518
type RowList<T extends readonly any[]> = T & Iterable<NonNullable<T[number]>> & ResultQueryMeta<T['length'], keyof T[number]>;
519519

520520
interface PendingQueryModifiers<TRow extends readonly any[]> {
521-
readable(): Readable;
522-
writable(): Writable;
521+
readable(): import('node:stream').Readable;
522+
writable(): import('node:stream').Writable;
523523

524524
execute(): this;
525525
cancel(): void;

0 commit comments

Comments
 (0)