Skip to content

Commit

Permalink
Build cjs + deno
Browse files Browse the repository at this point in the history
  • Loading branch information
porsager committed Apr 19, 2022
1 parent 13950af commit cbc6a75
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 38 deletions.
25 changes: 12 additions & 13 deletions cjs/src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
, connectTimer = timer(connectTimedOut, options.connect_timeout)

let socket = null
, cancelMessage
, result = new Result()
, incoming = Buffer.alloc(0)
, needsTypes = options.fetch_types
Expand Down Expand Up @@ -139,16 +140,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
}

async function cancel({ pid, secret }, resolve, reject) {
socket || (socket = await createSocket())
if (!socket)
return

socket.removeAllListeners()
socket = net.Socket()
socket.on('connect', () => socket.write(b().i32(16).i32(80877102).i32(pid).i32(secret).end(16)))
socket.once('error', reject)
socket.once('close', resolve)
connect()
try {
cancelMessage = b().i32(16).i32(80877102).i32(pid).i32(secret).end(16)
await connect()
socket.once('error', reject)
socket.once('close', resolve)
} catch (error) {
reject(error)
}
}

function execute(q) {
Expand Down Expand Up @@ -421,7 +420,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
if (socket) {
socket.removeListener('data', data)
socket.removeListener('connect', connected)
socket.readyState !== 'closed' && socket.end(b().X().end())
socket.readyState === 'open' && socket.end(b().X().end())
}
ended && (ended(), ending = ended = null)
}
Expand Down Expand Up @@ -955,7 +954,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
}

function StartupMessage() {
return b().inc(4).i16(3).z(2).str(
return cancelMessage || b().inc(4).i16(3).z(2).str(
Object.entries(Object.assign({
user,
database,
Expand Down Expand Up @@ -1012,7 +1011,7 @@ function timer(fn, seconds) {
},
start() {
timer && clearTimeout(timer)
timer = setTimeout(done, seconds * 1000, arguments).unref()
timer = setTimeout(done, seconds * 1000, arguments)
}
}

Expand Down
6 changes: 3 additions & 3 deletions cjs/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1774,16 +1774,16 @@ t('Execute', async() => {

t('Cancel running query', async() => {
const query = sql`select pg_sleep(2)`
setTimeout(() => query.cancel(), 50)
setTimeout(() => query.cancel(), 200)
const error = await query.catch(x => x)
return ['57014', error.code]
})

t('Cancel piped query', async() => {
await sql`select 1`
const last = sql`select pg_sleep(0.05)`.execute()
const last = sql`select pg_sleep(0.2)`.execute()
const query = sql`select pg_sleep(2) as dig`
setTimeout(() => query.cancel(), 10)
setTimeout(() => query.cancel(), 100)
const error = await query.catch(x => x)
await last
return ['57014', error.code]
Expand Down
4 changes: 2 additions & 2 deletions deno/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const xs = await sql`
### Query parameters

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
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.

```js
const name = 'Mur'
Expand Down Expand Up @@ -521,7 +521,7 @@ Do note that you can often achieve the same result using [`WITH` queries (Common
Like - `postgres('connectionURL', { transformation: {...} })`
### Parameters
* `to`: The function to transform the outgoing query column name to, i.e ``SELECT ${ sql('aName') }` to `SELECT a_name` when using `postgres.toCamel`.
* `to`: The function to transform the outgoing query column name to, i.e `SELECT ${ sql('aName') }` to `SELECT a_name` when using `postgres.toCamel`.
* `from`: The function to transform the incoming query result column name to, see example below.
> Both parameters are optional, if not provided, the default transformation function will be used.
Expand Down
25 changes: 12 additions & 13 deletions deno/src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
, connectTimer = timer(connectTimedOut, options.connect_timeout)

let socket = null
, cancelMessage
, result = new Result()
, incoming = Buffer.alloc(0)
, needsTypes = options.fetch_types
Expand Down Expand Up @@ -143,16 +144,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
}

async function cancel({ pid, secret }, resolve, reject) {
socket || (socket = await createSocket())
if (!socket)
return

socket.removeAllListeners()
socket = net.Socket()
socket.on('connect', () => socket.write(b().i32(16).i32(80877102).i32(pid).i32(secret).end(16)))
socket.once('error', reject)
socket.once('close', resolve)
connect()
try {
cancelMessage = b().i32(16).i32(80877102).i32(pid).i32(secret).end(16)
await connect()
socket.once('error', reject)
socket.once('close', resolve)
} catch (error) {
reject(error)
}
}

function execute(q) {
Expand Down Expand Up @@ -425,7 +424,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
if (socket) {
socket.removeListener('data', data)
socket.removeListener('connect', connected)
socket.readyState !== 'closed' && socket.end(b().X().end())
socket.readyState === 'open' && socket.end(b().X().end())
}
ended && (ended(), ending = ended = null)
}
Expand Down Expand Up @@ -959,7 +958,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
}

function StartupMessage() {
return b().inc(4).i16(3).z(2).str(
return cancelMessage || b().inc(4).i16(3).z(2).str(
Object.entries(Object.assign({
user,
database,
Expand Down Expand Up @@ -1016,7 +1015,7 @@ function timer(fn, seconds) {
},
start() {
timer && clearTimeout(timer)
timer = (window.timer = setTimeout(done, seconds * 1000, arguments), Deno.unrefTimer(window.timer), window.timer)
timer = setTimeout(done, seconds * 1000, arguments)
}
}

Expand Down
6 changes: 3 additions & 3 deletions deno/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1776,16 +1776,16 @@ t('Execute', async() => {

t('Cancel running query', async() => {
const query = sql`select pg_sleep(2)`
setTimeout(() => query.cancel(), 50)
setTimeout(() => query.cancel(), 200)
const error = await query.catch(x => x)
return ['57014', error.code]
})

t('Cancel piped query', async() => {
await sql`select 1`
const last = sql`select pg_sleep(0.05)`.execute()
const last = sql`select pg_sleep(0.2)`.execute()
const query = sql`select pg_sleep(2) as dig`
setTimeout(() => query.cancel(), 10)
setTimeout(() => query.cancel(), 100)
const error = await query.catch(x => x)
await last
return ['57014', error.code]
Expand Down
8 changes: 4 additions & 4 deletions deno/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,12 @@ declare namespace postgres {
writable(options?: {
highWaterMark?: number,
start?: number
}): Promise<Writable>;
}): Promise<import('node:stream').Writable>;
readable(options?: {
highWaterMark?: number,
start?: number,
end?: number
}): Promise<Readable>;
}): Promise<import('node:stream').Readable>;

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

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

execute(): this;
cancel(): void;
Expand Down

0 comments on commit cbc6a75

Please sign in to comment.