Skip to content

Commit f049db4

Browse files
committed
Build deno + cjs
1 parent 75c376d commit f049db4

File tree

10 files changed

+46
-26
lines changed

10 files changed

+46
-26
lines changed

cjs/src/connection.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const errorFields = {
5151
function Connection(options, { onopen = noop, onend = noop, ondrain = noop, onclose = noop } = {}) {
5252
const {
5353
ssl,
54+
max,
5455
user,
5556
host,
5657
port,
@@ -169,7 +170,7 @@ function Connection(options, { onopen = noop, onend = noop, ondrain = noop, oncl
169170

170171
function toBuffer(q) {
171172
if (q.parameters.length >= 65534)
172-
throw Errors.generic({ message: 'Max number of parameters (65534) exceeded', code: 'MAX_PARAMETERS_EXCEEDED' })
173+
throw Errors.generic('MAX_PARAMETERS_EXCEEDED', 'Max number of parameters (65534) exceeded')
173174

174175
return q.options.simple
175176
? b().Q().str(q.strings[0] + b.N).end()
@@ -557,6 +558,9 @@ function Connection(options, { onopen = noop, onend = noop, ondrain = noop, oncl
557558

558559
final && (final(), final = null)
559560

561+
if (result.command === 'BEGIN' && max !== 1 && !connection.reserved)
562+
return errored(Errors.generic('UNSAFE_TRANSACTION', 'Only use sql.begin or max: 1'))
563+
560564
if (query.options.simple)
561565
return
562566

@@ -679,10 +683,7 @@ function Connection(options, { onopen = noop, onend = noop, ondrain = noop, oncl
679683
if (x.toString('utf8', 9).split(b.N, 1)[0].slice(2) === serverSignature)
680684
return
681685
/* c8 ignore next 5 */
682-
errored(Errors.generic({
683-
message: 'The server did not return the correct signature',
684-
code: 'SASL_SIGNATURE_MISMATCH'
685-
}))
686+
errored(Errors.generic('SASL_SIGNATURE_MISMATCH', 'The server did not return the correct signature'))
686687
socket.destroy()
687688
}
688689

cjs/src/errors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ function postgres(x) {
3333
return error
3434
}
3535

36-
function generic(x) {
37-
const error = Object.assign(new Error(x.message), x)
36+
function generic(code, message) {
37+
const error = Object.assign(new Error(code + ': ' + message), { code })
3838
Error.captureStackTrace(error, generic)
3939
return error
4040
}

cjs/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function Postgres(a, b) {
114114
return query
115115
}
116116

117-
function file(path, args = [], options = { cache: true }) {
117+
function file(path, args = [], options = {}) {
118118
arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])
119119
const query = new Query([], args, (query) => {
120120
fs.readFile(path, 'utf8', (err, string) => {
@@ -359,7 +359,7 @@ function Postgres(a, b) {
359359
: (
360360
queries.remove(query),
361361
query.cancelled = true,
362-
query.reject(Errors.generic({ code: '57014', message: 'canceling statement due to user request' })),
362+
query.reject(Errors.generic('57014', 'canceling statement due to user request')),
363363
resolve()
364364
)
365365
})

cjs/src/types.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const Builder = module.exports.Builder = class Builder extends NotTagged {
8383
module.exports.handleValue = handleValue;function handleValue(x, parameters, types) {
8484
const value = x instanceof Parameter ? x.value : x
8585
if (value === undefined)
86-
throw Errors.generic({ code: 'UNDEFINED_VALUE', message: 'Undefined values are not allowed' })
86+
throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')
8787

8888
return '$' + (types.push(
8989
x instanceof Parameter
@@ -155,7 +155,7 @@ const builders = Object.entries({
155155
}).map(([x, fn]) => ([new RegExp('(^|[\\s(])' + x, 'i'), fn]))
156156

157157
function notTagged() {
158-
throw Errors.generic({ message: 'Query not called as a tagged template literal', code: 'NOT_TAGGED_CALL' })
158+
throw Errors.generic('NOT_TAGGED_CALL', 'Query not called as a tagged template literal')
159159
}
160160

161161
const serializers = module.exports.serializers = defaultHandlers.serializers

cjs/tests/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ t('null for int', async() => {
160160
return [1, (await sql`insert into test values(${ null })`).count, await sql`drop table test`]
161161
})
162162

163+
t('Throws on illegal transactions', async() => {
164+
const sql = postgres({ ...options, max: 2, fetch_types: false })
165+
const error = await sql`begin`.catch(e => e)
166+
return [
167+
error.code,
168+
'UNSAFE_TRANSACTION'
169+
]
170+
})
171+
163172
t('Transaction throws', async() => {
164173
await sql`create table test (a int)`
165174
return ['22P02', await sql.begin(async sql => {
@@ -942,15 +951,15 @@ t('dynamic select args', async() => {
942951

943952
t('dynamic values single row', async() => {
944953
const [{ b }] = await sql`
945-
select * from (values ${ sql(['a', 'b', 'c']) }) AS x(a, b, c)
954+
select * from (values ${ sql(['a', 'b', 'c']) }) as x(a, b, c)
946955
`
947956

948957
return ['b', b]
949958
})
950959

951960
t('dynamic values multi row', async() => {
952961
const [, { b }] = await sql`
953-
select * from (values ${ sql([['a', 'b', 'c'], ['a', 'b', 'c']]) }) AS x(a, b, c)
962+
select * from (values ${ sql([['a', 'b', 'c'], ['a', 'b', 'c']]) }) as x(a, b, c)
954963
`
955964

956965
return ['b', b]

deno/src/connection.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const errorFields = {
5454
function Connection(options, { onopen = noop, onend = noop, ondrain = noop, onclose = noop } = {}) {
5555
const {
5656
ssl,
57+
max,
5758
user,
5859
host,
5960
port,
@@ -172,7 +173,7 @@ function Connection(options, { onopen = noop, onend = noop, ondrain = noop, oncl
172173

173174
function toBuffer(q) {
174175
if (q.parameters.length >= 65534)
175-
throw Errors.generic({ message: 'Max number of parameters (65534) exceeded', code: 'MAX_PARAMETERS_EXCEEDED' })
176+
throw Errors.generic('MAX_PARAMETERS_EXCEEDED', 'Max number of parameters (65534) exceeded')
176177

177178
return q.options.simple
178179
? b().Q().str(q.strings[0] + b.N).end()
@@ -560,6 +561,9 @@ function Connection(options, { onopen = noop, onend = noop, ondrain = noop, oncl
560561

561562
final && (final(), final = null)
562563

564+
if (result.command === 'BEGIN' && max !== 1 && !connection.reserved)
565+
return errored(Errors.generic('UNSAFE_TRANSACTION', 'Only use sql.begin or max: 1'))
566+
563567
if (query.options.simple)
564568
return
565569

@@ -682,10 +686,7 @@ function Connection(options, { onopen = noop, onend = noop, ondrain = noop, oncl
682686
if (x.toString('utf8', 9).split(b.N, 1)[0].slice(2) === serverSignature)
683687
return
684688
/* c8 ignore next 5 */
685-
errored(Errors.generic({
686-
message: 'The server did not return the correct signature',
687-
code: 'SASL_SIGNATURE_MISMATCH'
688-
}))
689+
errored(Errors.generic('SASL_SIGNATURE_MISMATCH', 'The server did not return the correct signature'))
689690
socket.destroy()
690691
}
691692

deno/src/errors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ function postgres(x) {
3333
return error
3434
}
3535

36-
function generic(x) {
37-
const error = Object.assign(new Error(x.message), x)
36+
function generic(code, message) {
37+
const error = Object.assign(new Error(code + ': ' + message), { code })
3838
Error.captureStackTrace(error, generic)
3939
return error
4040
}

deno/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function Postgres(a, b) {
115115
return query
116116
}
117117

118-
function file(path, args = [], options = { cache: true }) {
118+
function file(path, args = [], options = {}) {
119119
arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])
120120
const query = new Query([], args, (query) => {
121121
fs.readFile(path, 'utf8', (err, string) => {
@@ -360,7 +360,7 @@ function Postgres(a, b) {
360360
: (
361361
queries.remove(query),
362362
query.cancelled = true,
363-
query.reject(Errors.generic({ code: '57014', message: 'canceling statement due to user request' })),
363+
query.reject(Errors.generic('57014', 'canceling statement due to user request')),
364364
resolve()
365365
)
366366
})

deno/src/types.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class Builder extends NotTagged {
8484
export function handleValue(x, parameters, types) {
8585
const value = x instanceof Parameter ? x.value : x
8686
if (value === undefined)
87-
throw Errors.generic({ code: 'UNDEFINED_VALUE', message: 'Undefined values are not allowed' })
87+
throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')
8888

8989
return '$' + (types.push(
9090
x instanceof Parameter
@@ -156,7 +156,7 @@ const builders = Object.entries({
156156
}).map(([x, fn]) => ([new RegExp('(^|[\\s(])' + x, 'i'), fn]))
157157

158158
function notTagged() {
159-
throw Errors.generic({ message: 'Query not called as a tagged template literal', code: 'NOT_TAGGED_CALL' })
159+
throw Errors.generic('NOT_TAGGED_CALL', 'Query not called as a tagged template literal')
160160
}
161161

162162
export const serializers = defaultHandlers.serializers

deno/tests/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ t('null for int', async() => {
161161
return [1, (await sql`insert into test values(${ null })`).count, await sql`drop table test`]
162162
})
163163

164+
t('Throws on illegal transactions', async() => {
165+
const sql = postgres({ ...options, max: 2, fetch_types: false })
166+
const error = await sql`begin`.catch(e => e)
167+
return [
168+
error.code,
169+
'UNSAFE_TRANSACTION'
170+
]
171+
})
172+
164173
t('Transaction throws', async() => {
165174
await sql`create table test (a int)`
166175
return ['22P02', await sql.begin(async sql => {
@@ -943,15 +952,15 @@ t('dynamic select args', async() => {
943952

944953
t('dynamic values single row', async() => {
945954
const [{ b }] = await sql`
946-
select * from (values ${ sql(['a', 'b', 'c']) }) AS x(a, b, c)
955+
select * from (values ${ sql(['a', 'b', 'c']) }) as x(a, b, c)
947956
`
948957

949958
return ['b', b]
950959
})
951960

952961
t('dynamic values multi row', async() => {
953962
const [, { b }] = await sql`
954-
select * from (values ${ sql([['a', 'b', 'c'], ['a', 'b', 'c']]) }) AS x(a, b, c)
963+
select * from (values ${ sql([['a', 'b', 'c'], ['a', 'b', 'c']]) }) as x(a, b, c)
955964
`
956965

957966
return ['b', b]

0 commit comments

Comments
 (0)