postgrejs 3.6.0 → 3.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +64 -6
- package/README.md +9 -79
- package/connection/intl-connection.d.ts +5 -3
- package/connection/intl-connection.js +47 -17
- package/connection/pool.d.ts +22 -0
- package/connection/pool.js +45 -3
- package/connection/prepared-statement.js +2 -2
- package/constants.d.ts +60 -0
- package/constants.js +119 -1
- package/data-type-map.js +82 -0
- package/data-types/bit-type.d.ts +5 -0
- package/data-types/bit-type.js +91 -0
- package/data-types/box-type.js +9 -19
- package/data-types/circle-type.js +6 -16
- package/data-types/classes/geometric.d.ts +105 -0
- package/data-types/classes/geometric.js +197 -0
- package/data-types/classes/interval.d.ts +73 -0
- package/data-types/classes/interval.js +181 -0
- package/data-types/classes/numeric.d.ts +37 -0
- package/data-types/classes/numeric.js +47 -0
- package/data-types/classes/range.d.ts +74 -0
- package/data-types/classes/range.js +111 -0
- package/data-types/classes/type-oid.d.ts +34 -0
- package/data-types/classes/type-oid.js +45 -0
- package/data-types/inet-type.d.ts +5 -0
- package/data-types/inet-type.js +286 -0
- package/data-types/interval-type.d.ts +5 -0
- package/data-types/interval-type.js +79 -0
- package/data-types/jsonpath-type.d.ts +3 -0
- package/data-types/jsonpath-type.js +53 -0
- package/data-types/line-type.d.ts +3 -0
- package/data-types/line-type.js +41 -0
- package/data-types/lseg-type.js +9 -19
- package/data-types/macaddr-type.d.ts +5 -0
- package/data-types/macaddr-type.js +86 -0
- package/data-types/numeric-type.js +98 -69
- package/data-types/path-type.d.ts +5 -0
- package/data-types/path-type.js +139 -0
- package/data-types/pg-lsn-type.d.ts +3 -0
- package/data-types/pg-lsn-type.js +58 -0
- package/data-types/point-type.js +8 -13
- package/data-types/range-type.d.ts +23 -0
- package/data-types/range-type.js +342 -0
- package/data-types/snapshot-type.d.ts +5 -0
- package/data-types/snapshot-type.js +96 -0
- package/data-types/tid-type.d.ts +3 -0
- package/data-types/tid-type.js +50 -0
- package/data-types/timetz-type.d.ts +3 -0
- package/data-types/timetz-type.js +184 -0
- package/data-types/tsvector-type.d.ts +5 -0
- package/data-types/tsvector-type.js +216 -0
- package/data-types/xid-type.d.ts +7 -0
- package/data-types/xid-type.js +110 -0
- package/index.d.ts +5 -0
- package/index.js +5 -0
- package/interfaces/data-mapping-options.d.ts +26 -0
- package/interfaces/data-type.d.ts +8 -9
- package/package.json +1 -1
- package/protocol/connection-lost-error.d.ts +42 -0
- package/protocol/connection-lost-error.js +56 -0
- package/protocol/pg-socket.d.ts +7 -0
- package/protocol/pg-socket.js +41 -3
- package/util/parse-array.js +17 -2
- package/util/resolve-column-formats.d.ts +2 -1
- package/util/resolve-column-formats.js +35 -8
- package/util/stringify-for-sql.js +37 -1
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { DataTypeOIDs } from '../constants.js';
|
|
2
|
+
/**
|
|
3
|
+
* `tsvector` and `tsquery` decode to the strings PostgreSQL prints -
|
|
4
|
+
* `'cat':1,3 'dog':2` and `'a' & !'b'` - and encode by handing a string
|
|
5
|
+
* straight back to the server. They share this file because they share
|
|
6
|
+
* that decision and half their quoting.
|
|
7
|
+
*
|
|
8
|
+
* **Why the text form.** It is the canonical spelling of both types: what
|
|
9
|
+
* the server accepts back, what the documentation is written in, and what
|
|
10
|
+
* someone inspecting an indexed document or a stored search wants to
|
|
11
|
+
* read. A structure of lexemes and positions, or a query tree, would be a
|
|
12
|
+
* second representation to convert to and from for values that are
|
|
13
|
+
* produced by `to_tsvector`/`to_tsquery` and consumed by a match
|
|
14
|
+
* operator, rather than walked. `pg` leaves both as strings.
|
|
15
|
+
*
|
|
16
|
+
* **Why no encodeBinary.** The server's own input parser is what defines
|
|
17
|
+
* these values. A `tsvector` literal is sorted and deduplicated on the
|
|
18
|
+
* way in, by a byte ordering and a position-merging rule that belong to
|
|
19
|
+
* the server; a `tsquery` literal is a small expression language, with
|
|
20
|
+
* operators that have been added across releases. Re-implementing either
|
|
21
|
+
* here would mean `'b a'::tsvector` or `'a<->b'::tsquery` quietly meaning
|
|
22
|
+
* one thing as a literal and another as a parameter, depending only on
|
|
23
|
+
* which column format the value travelled in. Handing the string over
|
|
24
|
+
* instead makes them the same by construction - the cost is that these
|
|
25
|
+
* two types cannot appear in a binary `COPY`, which `copyFrom` reports
|
|
26
|
+
* by name before it writes a row.
|
|
27
|
+
*
|
|
28
|
+
* Decoding is a different matter and is done here: the tree has already
|
|
29
|
+
* been built by the server, so reading it back is not parsing a grammar.
|
|
30
|
+
*/
|
|
31
|
+
/** The four weights, in the order the top two bits of a position hold. */
|
|
32
|
+
const VECTOR_WEIGHTS = ['', 'C', 'B', 'A'];
|
|
33
|
+
/** The weights a query value carries, as a bit each. */
|
|
34
|
+
const QUERY_WEIGHTS = [
|
|
35
|
+
[8, 'A'],
|
|
36
|
+
[4, 'B'],
|
|
37
|
+
[2, 'C'],
|
|
38
|
+
[1, 'D'],
|
|
39
|
+
];
|
|
40
|
+
const POSITION_MASK = 0x3fff;
|
|
41
|
+
const QI_VAL = 1;
|
|
42
|
+
const OP_NOT = 1;
|
|
43
|
+
const OP_AND = 2;
|
|
44
|
+
const OP_OR = 3;
|
|
45
|
+
const OP_PHRASE = 4;
|
|
46
|
+
/**
|
|
47
|
+
* How tightly each operator binds, which is what decides where the
|
|
48
|
+
* server puts parentheses - and so where this has to put them too.
|
|
49
|
+
*/
|
|
50
|
+
const PRIORITY = {
|
|
51
|
+
[OP_NOT]: 4,
|
|
52
|
+
[OP_PHRASE]: 3,
|
|
53
|
+
[OP_AND]: 2,
|
|
54
|
+
[OP_OR]: 1,
|
|
55
|
+
};
|
|
56
|
+
/** A lexeme is printed quoted, with `'` doubled and `\` escaped. */
|
|
57
|
+
function quote(lexeme) {
|
|
58
|
+
let out = "'";
|
|
59
|
+
const l = lexeme.length;
|
|
60
|
+
let i;
|
|
61
|
+
let c;
|
|
62
|
+
for (i = 0; i < l; i++) {
|
|
63
|
+
c = lexeme[i];
|
|
64
|
+
if (c === "'" || c === '\\')
|
|
65
|
+
out += c;
|
|
66
|
+
out += c;
|
|
67
|
+
}
|
|
68
|
+
return out + "'";
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* An int32 lexeme count, then for each: the lexeme as a NUL-terminated
|
|
72
|
+
* string, an int16 count of positions, and that many int16s holding the
|
|
73
|
+
* weight in the top two bits and the position in the low fourteen. A
|
|
74
|
+
* weight of D is the default and is not printed.
|
|
75
|
+
*/
|
|
76
|
+
function decodeVector(v, offset) {
|
|
77
|
+
const count = v.readInt32BE(offset);
|
|
78
|
+
let p = offset + 4;
|
|
79
|
+
let out = '';
|
|
80
|
+
let i;
|
|
81
|
+
let j;
|
|
82
|
+
let end;
|
|
83
|
+
let positions;
|
|
84
|
+
let entry;
|
|
85
|
+
for (i = 0; i < count; i++) {
|
|
86
|
+
end = v.indexOf(0, p);
|
|
87
|
+
if (i)
|
|
88
|
+
out += ' ';
|
|
89
|
+
out += quote(v.toString('utf8', p, end));
|
|
90
|
+
p = end + 1;
|
|
91
|
+
positions = v.readInt16BE(p);
|
|
92
|
+
p += 2;
|
|
93
|
+
for (j = 0; j < positions; j++) {
|
|
94
|
+
entry = v.readUInt16BE(p);
|
|
95
|
+
p += 2;
|
|
96
|
+
out +=
|
|
97
|
+
(j ? ',' : ':') + (entry & POSITION_MASK) + VECTOR_WEIGHTS[entry >> 14];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* An int32 node count, then the nodes themselves: an operator before its
|
|
104
|
+
* operands, and its right operand before its left. A value node carries
|
|
105
|
+
* a weight bitmask, a prefix flag and a NUL-terminated lexeme; a phrase
|
|
106
|
+
* operator carries an int16 distance.
|
|
107
|
+
*/
|
|
108
|
+
function readQueryNode(v, pos) {
|
|
109
|
+
const type = v[pos.p++];
|
|
110
|
+
if (type === QI_VAL) {
|
|
111
|
+
const weights = v[pos.p++];
|
|
112
|
+
const prefix = !!v[pos.p++];
|
|
113
|
+
const end = v.indexOf(0, pos.p);
|
|
114
|
+
const lexeme = v.toString('utf8', pos.p, end);
|
|
115
|
+
pos.p = end + 1;
|
|
116
|
+
return { lexeme, weights, prefix };
|
|
117
|
+
}
|
|
118
|
+
const oper = v[pos.p++];
|
|
119
|
+
if (oper === OP_NOT)
|
|
120
|
+
return { oper, right: readQueryNode(v, pos) };
|
|
121
|
+
let distance = 0;
|
|
122
|
+
if (oper === OP_PHRASE) {
|
|
123
|
+
distance = v.readInt16BE(pos.p);
|
|
124
|
+
pos.p += 2;
|
|
125
|
+
}
|
|
126
|
+
// Right before left, which is the order they were written in.
|
|
127
|
+
const right = readQueryNode(v, pos);
|
|
128
|
+
const left = readQueryNode(v, pos);
|
|
129
|
+
return { oper, distance, left, right };
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Prints the tree the way the server prints it, parentheses included.
|
|
133
|
+
* An operand is wrapped when it binds more loosely than what it sits
|
|
134
|
+
* under; the extra `rightPhraseOp` case is what keeps
|
|
135
|
+
* `'a' <-> ( 'b' <-> 'c' )` distinct from `'a' <-> 'b' <-> 'c'`, which
|
|
136
|
+
* are different queries.
|
|
137
|
+
*/
|
|
138
|
+
function printQuery(node, parentPriority, rightPhraseOp) {
|
|
139
|
+
if (node.oper === undefined) {
|
|
140
|
+
let out = quote(node.lexeme);
|
|
141
|
+
if (node.weights || node.prefix) {
|
|
142
|
+
// The star comes before the letters - `'d':*AB`, not `'d':AB*`.
|
|
143
|
+
out += ':';
|
|
144
|
+
if (node.prefix)
|
|
145
|
+
out += '*';
|
|
146
|
+
let w;
|
|
147
|
+
for (w of QUERY_WEIGHTS)
|
|
148
|
+
if (node.weights & w[0])
|
|
149
|
+
out += w[1];
|
|
150
|
+
}
|
|
151
|
+
return out;
|
|
152
|
+
}
|
|
153
|
+
const priority = PRIORITY[node.oper];
|
|
154
|
+
if (node.oper === OP_NOT)
|
|
155
|
+
return '!' + printQuery(node.right, priority, false);
|
|
156
|
+
const op = node.oper === OP_AND
|
|
157
|
+
? '&'
|
|
158
|
+
: node.oper === OP_OR
|
|
159
|
+
? '|'
|
|
160
|
+
: node.distance === 1
|
|
161
|
+
? '<->'
|
|
162
|
+
: '<' + node.distance + '>';
|
|
163
|
+
const out = printQuery(node.left, priority, false) +
|
|
164
|
+
' ' +
|
|
165
|
+
op +
|
|
166
|
+
' ' +
|
|
167
|
+
printQuery(node.right, priority, node.oper === OP_PHRASE);
|
|
168
|
+
return priority < parentPriority ||
|
|
169
|
+
(rightPhraseOp && priority === parentPriority)
|
|
170
|
+
? '( ' + out + ' )'
|
|
171
|
+
: out;
|
|
172
|
+
}
|
|
173
|
+
export const TsVectorType = {
|
|
174
|
+
name: 'tsvector',
|
|
175
|
+
oid: DataTypeOIDs.tsvector,
|
|
176
|
+
jsType: 'string',
|
|
177
|
+
// The text form is ordinary-looking text - see inet-type.ts for why
|
|
178
|
+
// none of these string-shaped types join inference.
|
|
179
|
+
inferrable: false,
|
|
180
|
+
encodeText(v) {
|
|
181
|
+
return '' + v;
|
|
182
|
+
},
|
|
183
|
+
decodeBinary(v, offset = 0) {
|
|
184
|
+
return decodeVector(v, offset);
|
|
185
|
+
},
|
|
186
|
+
decodeText(v) {
|
|
187
|
+
return v;
|
|
188
|
+
},
|
|
189
|
+
isType(v) {
|
|
190
|
+
return typeof v === 'string';
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
export const ArrayTsVectorType = {
|
|
194
|
+
...TsVectorType,
|
|
195
|
+
name: '_tsvector',
|
|
196
|
+
oid: DataTypeOIDs._tsvector,
|
|
197
|
+
elementsOID: DataTypeOIDs.tsvector,
|
|
198
|
+
};
|
|
199
|
+
export const TsQueryType = {
|
|
200
|
+
...TsVectorType,
|
|
201
|
+
name: 'tsquery',
|
|
202
|
+
oid: DataTypeOIDs.tsquery,
|
|
203
|
+
decodeBinary(v, offset = 0) {
|
|
204
|
+
const count = v.readInt32BE(offset);
|
|
205
|
+
// An empty query prints as nothing, and has no root to read.
|
|
206
|
+
if (!count)
|
|
207
|
+
return '';
|
|
208
|
+
return printQuery(readQueryNode(v, { p: offset + 4 }), 0, false);
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
export const ArrayTsQueryType = {
|
|
212
|
+
...TsQueryType,
|
|
213
|
+
name: '_tsquery',
|
|
214
|
+
oid: DataTypeOIDs._tsquery,
|
|
215
|
+
elementsOID: DataTypeOIDs.tsquery,
|
|
216
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { DataType } from '../interfaces/data-type.js';
|
|
2
|
+
export declare const XidType: DataType;
|
|
3
|
+
export declare const ArrayXidType: DataType;
|
|
4
|
+
export declare const CidType: DataType;
|
|
5
|
+
export declare const ArrayCidType: DataType;
|
|
6
|
+
export declare const Xid8Type: DataType;
|
|
7
|
+
export declare const ArrayXid8Type: DataType;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { DataTypeOIDs } from '../constants.js';
|
|
2
|
+
import { fastParseInt, fastParseIntBuffer } from '../util/fast-parseint.js';
|
|
3
|
+
/**
|
|
4
|
+
* `xid`, `xid8` and `cid` - the transaction and command counters behind
|
|
5
|
+
* the `xmin`, `xmax`, `cmin` and `cmax` system columns, which an
|
|
6
|
+
* application selects when it wants a row's identity for optimistic
|
|
7
|
+
* locking. They are counters, so they decode to numbers.
|
|
8
|
+
*
|
|
9
|
+
* None of the three joins inference. A JavaScript number says nothing
|
|
10
|
+
* about which of these it is, and claiming integers here would take them
|
|
11
|
+
* from `int4` and `int8`, where they almost always belong - name the
|
|
12
|
+
* type to use one.
|
|
13
|
+
*/
|
|
14
|
+
const BIG32 = BigInt(32);
|
|
15
|
+
/**
|
|
16
|
+
* A 64-bit value is only exactly a JavaScript number while its high half
|
|
17
|
+
* is at most this: 0x1fffff * 2^32 + 0xffffffff is exactly
|
|
18
|
+
* Number.MAX_SAFE_INTEGER.
|
|
19
|
+
*/
|
|
20
|
+
const MAX_SAFE_HIGH = 0x1fffff;
|
|
21
|
+
/** Both 64-bit types here are unsigned, so the halves are read apart. */
|
|
22
|
+
function readUInt64(v, offset) {
|
|
23
|
+
const hi = v.readUInt32BE(offset);
|
|
24
|
+
const lo = v.readUInt32BE(offset + 4);
|
|
25
|
+
if (hi <= MAX_SAFE_HIGH)
|
|
26
|
+
return hi * 4294967296 + lo;
|
|
27
|
+
return (BigInt(hi) << BIG32) | BigInt(lo);
|
|
28
|
+
}
|
|
29
|
+
function writeUInt64(buf, v) {
|
|
30
|
+
const n = typeof v === 'bigint' ? v : BigInt(fastParseInt(v));
|
|
31
|
+
buf.writeUInt32BE(Number(n >> BIG32));
|
|
32
|
+
buf.writeUInt32BE(Number(n & BigInt(0xffffffff)));
|
|
33
|
+
}
|
|
34
|
+
function createUInt32Type(name, oid) {
|
|
35
|
+
return {
|
|
36
|
+
name,
|
|
37
|
+
oid,
|
|
38
|
+
jsType: 'number',
|
|
39
|
+
inferrable: false,
|
|
40
|
+
encodeText(v) {
|
|
41
|
+
return '' + v;
|
|
42
|
+
},
|
|
43
|
+
encodeBinary(buf, v) {
|
|
44
|
+
buf.writeUInt32BE(fastParseInt(v));
|
|
45
|
+
},
|
|
46
|
+
decodeBinary(v, offset = 0) {
|
|
47
|
+
return v.readUInt32BE(offset);
|
|
48
|
+
},
|
|
49
|
+
decodeText: fastParseInt,
|
|
50
|
+
decodeTextBuffer: fastParseIntBuffer,
|
|
51
|
+
isType(v) {
|
|
52
|
+
return (typeof v === 'number' &&
|
|
53
|
+
Number.isInteger(v) &&
|
|
54
|
+
v >= 0 &&
|
|
55
|
+
v <= 4294967295);
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
export const XidType = createUInt32Type('xid', DataTypeOIDs.xid);
|
|
60
|
+
export const ArrayXidType = {
|
|
61
|
+
...XidType,
|
|
62
|
+
name: '_xid',
|
|
63
|
+
oid: DataTypeOIDs._xid,
|
|
64
|
+
elementsOID: DataTypeOIDs.xid,
|
|
65
|
+
};
|
|
66
|
+
export const CidType = createUInt32Type('cid', DataTypeOIDs.cid);
|
|
67
|
+
export const ArrayCidType = {
|
|
68
|
+
...CidType,
|
|
69
|
+
name: '_cid',
|
|
70
|
+
oid: DataTypeOIDs._cid,
|
|
71
|
+
elementsOID: DataTypeOIDs.cid,
|
|
72
|
+
};
|
|
73
|
+
export const Xid8Type = {
|
|
74
|
+
name: 'xid8',
|
|
75
|
+
oid: DataTypeOIDs.xid8,
|
|
76
|
+
// A number while the value fits one exactly, a BigInt after that -
|
|
77
|
+
// the same contract int8 has.
|
|
78
|
+
jsType: 'BigInt',
|
|
79
|
+
inferrable: false,
|
|
80
|
+
encodeText(v) {
|
|
81
|
+
return '' + v;
|
|
82
|
+
},
|
|
83
|
+
encodeBinary: writeUInt64,
|
|
84
|
+
decodeBinary(v, offset = 0) {
|
|
85
|
+
return readUInt64(v, offset);
|
|
86
|
+
},
|
|
87
|
+
decodeText(s) {
|
|
88
|
+
// A full transaction id never has a sign, so the digit count alone
|
|
89
|
+
// decides whether it can be a number.
|
|
90
|
+
if (s.length <= 15)
|
|
91
|
+
return Number(s);
|
|
92
|
+
const v = BigInt(s);
|
|
93
|
+
return v <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(v) : v;
|
|
94
|
+
},
|
|
95
|
+
decodeTextBuffer(buf, offset, len) {
|
|
96
|
+
if (len <= 15)
|
|
97
|
+
return fastParseIntBuffer(buf, offset, len);
|
|
98
|
+
return Xid8Type.decodeText(buf.toString('latin1', offset, offset + len), {});
|
|
99
|
+
},
|
|
100
|
+
isType(v) {
|
|
101
|
+
return (typeof v === 'bigint' ||
|
|
102
|
+
(typeof v === 'number' && Number.isInteger(v) && v >= 0));
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
export const ArrayXid8Type = {
|
|
106
|
+
...Xid8Type,
|
|
107
|
+
name: '_xid8',
|
|
108
|
+
oid: DataTypeOIDs._xid8,
|
|
109
|
+
elementsOID: DataTypeOIDs.xid8,
|
|
110
|
+
};
|
package/index.d.ts
CHANGED
|
@@ -8,6 +8,10 @@ export * from './connection/pool.js';
|
|
|
8
8
|
export * from './connection/prepared-statement.js';
|
|
9
9
|
export * from './constants.js';
|
|
10
10
|
export * from './data-type-map.js';
|
|
11
|
+
export * from './data-types/classes/geometric.js';
|
|
12
|
+
export * from './data-types/classes/interval.js';
|
|
13
|
+
export * from './data-types/classes/numeric.js';
|
|
14
|
+
export * from './data-types/classes/range.js';
|
|
11
15
|
export * from './interfaces/batch-result.js';
|
|
12
16
|
export * from './interfaces/command-result.js';
|
|
13
17
|
export * from './interfaces/copy-from-rows-options.js';
|
|
@@ -23,6 +27,7 @@ export * from './interfaces/query-result.js';
|
|
|
23
27
|
export * from './interfaces/script-execute-options.js';
|
|
24
28
|
export * from './interfaces/script-result.js';
|
|
25
29
|
export * from './interfaces/statement-prepare-options.js';
|
|
30
|
+
export * from './protocol/connection-lost-error.js';
|
|
26
31
|
export * from './protocol/database-error.js';
|
|
27
32
|
export * from './protocol/smart-buffer.js';
|
|
28
33
|
export * from './types.js';
|
package/index.js
CHANGED
|
@@ -8,6 +8,10 @@ export * from './connection/pool.js';
|
|
|
8
8
|
export * from './connection/prepared-statement.js';
|
|
9
9
|
export * from './constants.js';
|
|
10
10
|
export * from './data-type-map.js';
|
|
11
|
+
export * from './data-types/classes/geometric.js';
|
|
12
|
+
export * from './data-types/classes/interval.js';
|
|
13
|
+
export * from './data-types/classes/numeric.js';
|
|
14
|
+
export * from './data-types/classes/range.js';
|
|
11
15
|
export * from './interfaces/batch-result.js';
|
|
12
16
|
export * from './interfaces/command-result.js';
|
|
13
17
|
export * from './interfaces/copy-from-rows-options.js';
|
|
@@ -23,6 +27,7 @@ export * from './interfaces/query-result.js';
|
|
|
23
27
|
export * from './interfaces/script-execute-options.js';
|
|
24
28
|
export * from './interfaces/script-result.js';
|
|
25
29
|
export * from './interfaces/statement-prepare-options.js';
|
|
30
|
+
export * from './protocol/connection-lost-error.js';
|
|
26
31
|
export * from './protocol/database-error.js';
|
|
27
32
|
export * from './protocol/smart-buffer.js';
|
|
28
33
|
export * from './types.js';
|
|
@@ -22,4 +22,30 @@ export interface DataMappingOptions {
|
|
|
22
22
|
* that comes back.
|
|
23
23
|
*/
|
|
24
24
|
fetchAsString?: OID[];
|
|
25
|
+
/**
|
|
26
|
+
* Ask the server for text on any column this client has no way to
|
|
27
|
+
* decode, instead of taking bytes it cannot read.
|
|
28
|
+
*
|
|
29
|
+
* Result columns are requested in binary, and there is a decoder only
|
|
30
|
+
* for the types registered in the type map - so an enum, a composite,
|
|
31
|
+
* an extension type, and 54 of PostgreSQL's own built-ins (`interval`,
|
|
32
|
+
* `inet`, the range family, `tsvector`, `money`, ...) arrive as a raw
|
|
33
|
+
* `Buffer` that nothing can interpret. With this on they arrive as the
|
|
34
|
+
* string PostgreSQL would have printed, which is what `pg` gives for
|
|
35
|
+
* the same column.
|
|
36
|
+
*
|
|
37
|
+
* Off by default, and it is not free: the column types have to be known
|
|
38
|
+
* before the Bind that asks for them, so a query that has not been
|
|
39
|
+
* prepared yet is prepared on first sight (one extra round trip per
|
|
40
|
+
* distinct statement per connection, measured at 233µs -> 447µs on
|
|
41
|
+
* loopback), and one run with `prepare: false` asks for the whole row
|
|
42
|
+
* as text.
|
|
43
|
+
*
|
|
44
|
+
* A registered type is untouched, so nothing that decodes today starts
|
|
45
|
+
* arriving as a string. An array whose own OID is unregistered comes
|
|
46
|
+
* back as the array literal rather than a JS array - there is no way to
|
|
47
|
+
* know it is an array without reading the catalog, and `pg` answers the
|
|
48
|
+
* same way. `fields[i].dataTypeName` stays empty for the same reason.
|
|
49
|
+
*/
|
|
50
|
+
unknownTypesAsString?: boolean;
|
|
25
51
|
}
|
|
@@ -26,15 +26,14 @@ export interface DataType {
|
|
|
26
26
|
encodeText?: EncodeTextFunction;
|
|
27
27
|
encodeCalculateDim?: EncodeCalculateDimFunction;
|
|
28
28
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
29
|
+
/**
|
|
30
|
+
* The shape `box` and `lseg` share.
|
|
31
|
+
*
|
|
32
|
+
* @deprecated Both types now decode to a class of their own - `Box` and
|
|
33
|
+
* `LineSegment` - which is what tells them apart; they were
|
|
34
|
+
* indistinguishable while both were only this. Kept because a plain
|
|
35
|
+
* object of these four numbers is still accepted as a parameter.
|
|
36
|
+
*/
|
|
38
37
|
export interface Rectangle {
|
|
39
38
|
x1: number;
|
|
40
39
|
y1: number;
|
package/package.json
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A connection whose socket closed without anyone asking it to - the
|
|
3
|
+
* backend terminated by an administrator, a failover, a network fault.
|
|
4
|
+
*
|
|
5
|
+
* Reported on `Connection`'s `'close'` event as its reason, for a pooled
|
|
6
|
+
* connection on `Pool`'s `'destroy'` (as the second argument) and
|
|
7
|
+
* `'error'`, and as the rejection of whatever query was in flight when it
|
|
8
|
+
* happened - one object for all of them, so a caller that needs both can
|
|
9
|
+
* pair them by identity. `Pool` emits `'error'` for this and for a connection that
|
|
10
|
+
* could not be created in the first place, and those want different
|
|
11
|
+
* handling - this class is how a listener tells them apart:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* pool.on('error', err => {
|
|
15
|
+
* if (err.code === '08006') log.warn('pooled connection lost', err.processID);
|
|
16
|
+
* else log.error('could not open a connection', err);
|
|
17
|
+
* });
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* `code` is there so that branching does not have to mean `instanceof`,
|
|
21
|
+
* which fails across duplicated copies of the package and is awkward from
|
|
22
|
+
* JavaScript. It reads the same way `DatabaseError.code` does - a
|
|
23
|
+
* SQLSTATE - and `08006 connection_failure` is the one this is. The server
|
|
24
|
+
* never sends it: it cannot report that its own connection went away, so
|
|
25
|
+
* there is no ambiguity about where a code of this value came from.
|
|
26
|
+
*
|
|
27
|
+
* The message deliberately says nothing else: it matches what other
|
|
28
|
+
* drivers report for the same event, so code that branches on it keeps
|
|
29
|
+
* working. What happened to *this* connection is on the object instead -
|
|
30
|
+
* `processID` names the backend, and `cause` carries the socket error
|
|
31
|
+
* when there was one (a clean FIN leaves none).
|
|
32
|
+
*/
|
|
33
|
+
export declare class ConnectionLostError extends Error {
|
|
34
|
+
/**
|
|
35
|
+
* SQLSTATE 08006, `connection_failure`. Synthesized here rather than
|
|
36
|
+
* received - see the class doc.
|
|
37
|
+
*/
|
|
38
|
+
readonly code: string;
|
|
39
|
+
/** The backend's process id, as it was before the connection reset. */
|
|
40
|
+
readonly processID?: number;
|
|
41
|
+
constructor(processID?: number, cause?: unknown);
|
|
42
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A connection whose socket closed without anyone asking it to - the
|
|
3
|
+
* backend terminated by an administrator, a failover, a network fault.
|
|
4
|
+
*
|
|
5
|
+
* Reported on `Connection`'s `'close'` event as its reason, for a pooled
|
|
6
|
+
* connection on `Pool`'s `'destroy'` (as the second argument) and
|
|
7
|
+
* `'error'`, and as the rejection of whatever query was in flight when it
|
|
8
|
+
* happened - one object for all of them, so a caller that needs both can
|
|
9
|
+
* pair them by identity. `Pool` emits `'error'` for this and for a connection that
|
|
10
|
+
* could not be created in the first place, and those want different
|
|
11
|
+
* handling - this class is how a listener tells them apart:
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* pool.on('error', err => {
|
|
15
|
+
* if (err.code === '08006') log.warn('pooled connection lost', err.processID);
|
|
16
|
+
* else log.error('could not open a connection', err);
|
|
17
|
+
* });
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* `code` is there so that branching does not have to mean `instanceof`,
|
|
21
|
+
* which fails across duplicated copies of the package and is awkward from
|
|
22
|
+
* JavaScript. It reads the same way `DatabaseError.code` does - a
|
|
23
|
+
* SQLSTATE - and `08006 connection_failure` is the one this is. The server
|
|
24
|
+
* never sends it: it cannot report that its own connection went away, so
|
|
25
|
+
* there is no ambiguity about where a code of this value came from.
|
|
26
|
+
*
|
|
27
|
+
* The message deliberately says nothing else: it matches what other
|
|
28
|
+
* drivers report for the same event, so code that branches on it keeps
|
|
29
|
+
* working. What happened to *this* connection is on the object instead -
|
|
30
|
+
* `processID` names the backend, and `cause` carries the socket error
|
|
31
|
+
* when there was one (a clean FIN leaves none).
|
|
32
|
+
*/
|
|
33
|
+
export class ConnectionLostError extends Error {
|
|
34
|
+
/**
|
|
35
|
+
* SQLSTATE 08006, `connection_failure`. Synthesized here rather than
|
|
36
|
+
* received - see the class doc.
|
|
37
|
+
*/
|
|
38
|
+
code = '08006';
|
|
39
|
+
/** The backend's process id, as it was before the connection reset. */
|
|
40
|
+
processID;
|
|
41
|
+
constructor(processID, cause) {
|
|
42
|
+
super('Connection terminated unexpectedly');
|
|
43
|
+
this.name = 'ConnectionLostError';
|
|
44
|
+
this.processID = processID;
|
|
45
|
+
// Assigned rather than passed to the constructor, as abortError()
|
|
46
|
+
// does - same reason, the option is newer than this package's floor.
|
|
47
|
+
if (cause !== undefined) {
|
|
48
|
+
try {
|
|
49
|
+
this.cause = cause;
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
/* c8 ignore next */
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
package/protocol/pg-socket.d.ts
CHANGED
|
@@ -13,6 +13,12 @@ export interface SocketError extends Error {
|
|
|
13
13
|
export declare class PgSocket extends SafeEventEmitter {
|
|
14
14
|
options: ConnectionConfiguration;
|
|
15
15
|
private _state;
|
|
16
|
+
/**
|
|
17
|
+
* The socket-level error that is about to end this connection, kept so
|
|
18
|
+
* the 'close' that follows can say what happened instead of arriving
|
|
19
|
+
* bare. Cleared by _reset().
|
|
20
|
+
*/
|
|
21
|
+
private _closeReason?;
|
|
16
22
|
private _socket?;
|
|
17
23
|
private _backend;
|
|
18
24
|
private _frontend;
|
|
@@ -282,6 +288,7 @@ export declare class PgSocket extends SafeEventEmitter {
|
|
|
282
288
|
/** Accepts this server, or drops it and moves to the next candidate. */
|
|
283
289
|
protected _applySessionAttrs(wanted: string, standby: boolean, readOnly: boolean): void;
|
|
284
290
|
protected _handleClose(): void;
|
|
291
|
+
private _buildCloseReason;
|
|
285
292
|
protected _handleError(err: unknown): void;
|
|
286
293
|
/** Rejects and clears every still-open capture, e.g. on close/error. */
|
|
287
294
|
private _failPendingCaptures;
|
package/protocol/pg-socket.js
CHANGED
|
@@ -8,6 +8,7 @@ import { ConnectionState } from '../constants.js';
|
|
|
8
8
|
import { SafeEventEmitter } from '../safe-event-emitter.js';
|
|
9
9
|
import { Backend } from './backend.js';
|
|
10
10
|
import { signatureHashOfCertificate } from './cert-signature.js';
|
|
11
|
+
import { ConnectionLostError } from './connection-lost-error.js';
|
|
11
12
|
import { DatabaseError } from './database-error.js';
|
|
12
13
|
import { Frontend } from './frontend.js';
|
|
13
14
|
import { Protocol } from './protocol.js';
|
|
@@ -17,6 +18,12 @@ const COMMAND_RESULT_PATTERN = /^([^\d]+)(?: (\d+)(?: (\d+))?)?$/;
|
|
|
17
18
|
export class PgSocket extends SafeEventEmitter {
|
|
18
19
|
options;
|
|
19
20
|
_state = ConnectionState.CLOSED;
|
|
21
|
+
/**
|
|
22
|
+
* The socket-level error that is about to end this connection, kept so
|
|
23
|
+
* the 'close' that follows can say what happened instead of arriving
|
|
24
|
+
* bare. Cleared by _reset().
|
|
25
|
+
*/
|
|
26
|
+
_closeReason;
|
|
20
27
|
_socket;
|
|
21
28
|
_backend = new Backend();
|
|
22
29
|
_frontend;
|
|
@@ -532,6 +539,7 @@ export class PgSocket extends SafeEventEmitter {
|
|
|
532
539
|
this._socket.removeAllListeners('close');
|
|
533
540
|
}
|
|
534
541
|
_reset() {
|
|
542
|
+
this._closeReason = undefined;
|
|
535
543
|
this._backend.reset();
|
|
536
544
|
this._sessionParameters = {};
|
|
537
545
|
this._processID = undefined;
|
|
@@ -627,14 +635,44 @@ export class PgSocket extends SafeEventEmitter {
|
|
|
627
635
|
this._connectToHost();
|
|
628
636
|
}
|
|
629
637
|
_handleClose() {
|
|
630
|
-
|
|
638
|
+
// close() is the only thing that sets CLOSING, so anything else here
|
|
639
|
+
// is the far end going away on its own - an admin terminating the
|
|
640
|
+
// backend, a failover, a network fault. Built before _reset() clears
|
|
641
|
+
// the process id, which is the one detail that makes the report
|
|
642
|
+
// actionable, and carrying the socket error as its cause when there
|
|
643
|
+
// was one (a bare FIN leaves none).
|
|
644
|
+
const reason = this._state === ConnectionState.CLOSING
|
|
645
|
+
? undefined
|
|
646
|
+
: this._buildCloseReason();
|
|
647
|
+
// Whatever was in flight is rejected with the same object the 'close'
|
|
648
|
+
// event carries, so `err.code === '08006'` works in a caller's catch
|
|
649
|
+
// exactly as it does in a Pool listener - the generic message it used
|
|
650
|
+
// to get named no cause and carried no code at all. A close that was
|
|
651
|
+
// asked for keeps that message: nothing went wrong, the caller simply
|
|
652
|
+
// raced the shutdown.
|
|
653
|
+
this._failPendingCaptures(reason ?? new Error('Connection closed'));
|
|
631
654
|
this._reset();
|
|
632
655
|
this._socket = undefined;
|
|
633
656
|
this._state = ConnectionState.CLOSED;
|
|
634
|
-
this.emit('close');
|
|
657
|
+
this.emit('close', reason);
|
|
658
|
+
}
|
|
659
|
+
_buildCloseReason() {
|
|
660
|
+
return new ConnectionLostError(this._processID, this._closeReason);
|
|
635
661
|
}
|
|
636
662
|
_handleError(err) {
|
|
637
|
-
|
|
663
|
+
// Kept for the 'close' that follows: the socket reports the cause
|
|
664
|
+
// here and the fact of closing separately, and only the pair together
|
|
665
|
+
// says what actually happened.
|
|
666
|
+
this._closeReason = err instanceof Error ? err : new Error(String(err));
|
|
667
|
+
// An error on a live connection is that connection going away - Node
|
|
668
|
+
// always follows 'error' with 'close' - so the caller gets the same
|
|
669
|
+
// ConnectionLostError the close will carry, with this as its cause,
|
|
670
|
+
// rather than a raw ECONNRESET whose `code` is a Node errno where a
|
|
671
|
+
// SQLSTATE is expected. Before READY there is no connection to have
|
|
672
|
+
// lost, and the error itself is the useful thing.
|
|
673
|
+
this._failPendingCaptures(this._state === ConnectionState.READY
|
|
674
|
+
? this._buildCloseReason()
|
|
675
|
+
: this._closeReason);
|
|
638
676
|
if (this._state !== ConnectionState.READY) {
|
|
639
677
|
this._socket?.end();
|
|
640
678
|
}
|
package/util/parse-array.js
CHANGED
|
@@ -21,13 +21,28 @@ export function parsePostgresArray(s, opts) {
|
|
|
21
21
|
continue;
|
|
22
22
|
}
|
|
23
23
|
if (c === '}' || c === sep) {
|
|
24
|
-
|
|
24
|
+
// An element is present when it had content OR when it was
|
|
25
|
+
// quoted: `""` is a real empty string, and testing the token
|
|
26
|
+
// alone dropped it - `{"",b}` came back as one element, with
|
|
27
|
+
// every later index shifted.
|
|
28
|
+
//
|
|
29
|
+
// An unquoted empty token is still skipped. PostgreSQL never
|
|
30
|
+
// emits one (a null prints as the bare word NULL, an empty
|
|
31
|
+
// string always as `""`), so `{a,,b}` is not output this has to
|
|
32
|
+
// read - and skipping it is what keeps the separator that
|
|
33
|
+
// follows a nested array from pushing an element of its own.
|
|
34
|
+
if (token || exactlyValue) {
|
|
25
35
|
if (token === 'NULL' && !exactlyValue)
|
|
26
36
|
arr.push(null);
|
|
27
37
|
else
|
|
28
38
|
arr.push(transform ? transform(token) : token);
|
|
29
|
-
exactlyValue = false;
|
|
30
39
|
}
|
|
40
|
+
// Reset per element rather than only when one was pushed.
|
|
41
|
+
// Leaving it set carried "this one was quoted" into the next
|
|
42
|
+
// element - and that flag is what tells a real NULL apart from
|
|
43
|
+
// the string "NULL", so a null following an empty string
|
|
44
|
+
// decoded as four characters of text.
|
|
45
|
+
exactlyValue = false;
|
|
31
46
|
token = '';
|
|
32
47
|
if (c === '}')
|
|
33
48
|
return;
|