postgrejs 2.12.0 → 2.15.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.
Files changed (62) hide show
  1. package/CHANGELOG.md +215 -191
  2. package/README.md +25 -25
  3. package/cjs/connection/connection.js +10 -5
  4. package/cjs/connection/cursor.js +2 -4
  5. package/cjs/connection/intl-connection.js +19 -12
  6. package/cjs/connection/pool.js +12 -6
  7. package/cjs/connection/prepared-statement.js +6 -3
  8. package/cjs/data-types/bytea-type.js +2 -4
  9. package/cjs/data-types/numeric-type.js +2 -2
  10. package/cjs/data-types/time-type.js +2 -1
  11. package/cjs/data-types/timestamp-type.js +2 -1
  12. package/cjs/data-types/timestamptz-type.js +2 -1
  13. package/cjs/index.js +26 -39
  14. package/cjs/protocol/frontend.js +8 -5
  15. package/cjs/protocol/pg-socket.js +33 -31
  16. package/cjs/protocol/sasl.js +14 -14
  17. package/cjs/protocol/smart-buffer.js +2 -24
  18. package/cjs/util/array-calculatedim.js +1 -2
  19. package/cjs/util/bigint-methods.js +2 -3
  20. package/cjs/util/config-from-env.js +1 -2
  21. package/cjs/util/connection-config.js +8 -11
  22. package/cjs/util/convert-row-to-object.js +1 -2
  23. package/cjs/util/decode-binaryarray.js +1 -2
  24. package/cjs/util/encode-binaryarray.js +1 -2
  25. package/cjs/util/escape-literal.js +1 -2
  26. package/cjs/util/fast-parseint.js +1 -2
  27. package/cjs/util/get-parsers.js +1 -2
  28. package/cjs/util/parse-array.js +1 -2
  29. package/cjs/util/parse-datetime.js +1 -2
  30. package/cjs/util/parse-row.js +1 -2
  31. package/cjs/util/parse-time.js +2 -2
  32. package/cjs/util/stringify-arrayliteral.js +1 -2
  33. package/cjs/util/stringify-for-sql.js +2 -3
  34. package/cjs/util/wrap-row-description.js +1 -2
  35. package/esm/connection/connection.js +10 -5
  36. package/esm/connection/intl-connection.js +17 -10
  37. package/esm/connection/pool.js +12 -6
  38. package/esm/connection/prepared-statement.js +6 -3
  39. package/esm/data-types/time-type.js +2 -1
  40. package/esm/data-types/timestamp-type.js +2 -1
  41. package/esm/data-types/timestamptz-type.js +2 -1
  42. package/esm/index.js +5 -5
  43. package/esm/protocol/frontend.js +8 -5
  44. package/esm/protocol/pg-socket.js +28 -24
  45. package/esm/protocol/sasl.js +12 -10
  46. package/esm/util/connection-config.js +4 -4
  47. package/package.json +5 -5
  48. package/typings/connection/connection.d.ts +0 -1
  49. package/typings/connection/cursor.d.ts +0 -1
  50. package/typings/connection/prepared-statement.d.ts +0 -1
  51. package/typings/index.d.ts +5 -5
  52. package/typings/interfaces/database-connection-params.d.ts +1 -1
  53. package/typings/protocol/backend.d.ts +0 -1
  54. package/typings/protocol/buffer-reader.d.ts +0 -1
  55. package/typings/protocol/frontend.d.ts +0 -1
  56. package/typings/protocol/pg-socket.d.ts +0 -1
  57. package/typings/protocol/protocol.d.ts +0 -1
  58. package/typings/protocol/smart-buffer.d.ts +0 -1
  59. package/typings/safe-event-emitter.d.ts +0 -1
  60. package/typings/types.d.ts +0 -1
  61. package/typings/util/bigint-methods.d.ts +0 -1
  62. package/typings/util/decode-binaryarray.d.ts +0 -1
@@ -16,8 +16,9 @@ export const TimestamptzType = {
16
16
  return fetchAsString ? '-infinity' : -Infinity;
17
17
  // Shift from 2000 to 1970
18
18
  let d = new Date((lo + hi * timeMul) / 1000 + timeShift);
19
- if (fetchAsString || !options.utcDates)
19
+ if (fetchAsString || !options.utcDates) {
20
20
  d = new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds());
21
+ }
21
22
  return fetchAsString ? dateToTimestamptzString(d) : d;
22
23
  },
23
24
  encodeBinary(buf, v, options) {
package/esm/index.js CHANGED
@@ -1,11 +1,10 @@
1
- export * from './constants.js';
2
- export * from './data-type-map.js';
3
- export * from './types.js';
4
1
  export * from './connection/bind-param.js';
5
2
  export * from './connection/connection.js';
6
3
  export * from './connection/cursor.js';
7
4
  export * from './connection/pool.js';
8
5
  export * from './connection/prepared-statement.js';
6
+ export * from './constants.js';
7
+ export * from './data-type-map.js';
9
8
  export * from './interfaces/command-result.js';
10
9
  export * from './interfaces/data-mapping-options.js';
11
10
  export * from './interfaces/data-type.js';
@@ -16,10 +15,11 @@ export * from './interfaces/query-result.js';
16
15
  export * from './interfaces/script-execute-options.js';
17
16
  export * from './interfaces/script-result.js';
18
17
  export * from './interfaces/statement-prepare-options.js';
18
+ export * from './protocol/database-error.js';
19
+ export * from './protocol/smart-buffer.js';
20
+ export * from './types.js';
19
21
  export * from './util/connection-config.js';
20
22
  export * from './util/escape-literal.js';
21
23
  export * from './util/parse-datetime.js';
22
24
  export * from './util/stringify-arrayliteral.js';
23
25
  export * from './util/stringify-for-sql.js';
24
- export * from './protocol/smart-buffer.js';
25
- export * from './protocol/database-error.js';
@@ -158,8 +158,9 @@ export class Frontend {
158
158
  return setLengthAndFlush(io, 1);
159
159
  }
160
160
  getDescribeMessage(args) {
161
- if (args.name && args.name.length > 63)
162
- throw new Error(args.type === 'P' ? 'Portal' : 'Statement' + 'name length must be lower than 63');
161
+ if (args.name && args.name.length > 63) {
162
+ throw new Error(args.type === 'P' ? 'Portal' : 'Statement name length must be lower than 63');
163
+ }
163
164
  const io = this._io
164
165
  .start()
165
166
  .writeInt8(Protocol.FrontendMessageCode.Describe)
@@ -169,8 +170,9 @@ export class Frontend {
169
170
  return setLengthAndFlush(io, 1);
170
171
  }
171
172
  getExecuteMessage(args) {
172
- if (args.fetchCount && (args.fetchCount < 0 || args.fetchCount > 4294967295))
173
+ if (args.fetchCount && (args.fetchCount < 0 || args.fetchCount > 4294967295)) {
173
174
  throw new Error('fetchCount can be between 0 and 4294967295');
175
+ }
174
176
  const io = this._io
175
177
  .start()
176
178
  .writeInt8(Protocol.FrontendMessageCode.Execute)
@@ -180,8 +182,9 @@ export class Frontend {
180
182
  return setLengthAndFlush(io, 1);
181
183
  }
182
184
  getCloseMessage(args) {
183
- if (args.name && args.name.length > 63)
184
- throw new Error(args.type === 'P' ? 'Portal' : 'Statement' + 'name length must be lower than 63');
185
+ if (args.name && args.name.length > 63) {
186
+ throw new Error(args.type === 'P' ? 'Portal' : 'Statement name length must be lower than 63');
187
+ }
185
188
  const io = this._io
186
189
  .start()
187
190
  .writeInt8(Protocol.FrontendMessageCode.Close)
@@ -53,30 +53,31 @@ export class PgSocket extends SafeEventEmitter {
53
53
  socket.setTimeout(0);
54
54
  if (this.options.keepAlive || this.options.keepAlive == null)
55
55
  socket.setKeepAlive(true);
56
- if (options.ssl) {
57
- socket.write(this._frontend.getSSLRequestMessage());
58
- socket.once('data', x => {
59
- this._removeListeners();
60
- if (x.toString() === 'S') {
61
- const tslOptions = { ...options.ssl, socket };
62
- if (options.host && net.isIP(options.host) === 0)
63
- tslOptions.servername = options.host;
64
- const tlsSocket = (this._socket = tls.connect(tslOptions));
65
- tlsSocket.once('error', errorHandler);
66
- tlsSocket.once('secureConnect', () => {
67
- this._removeListeners();
68
- this._handleConnect();
69
- });
70
- return;
71
- }
72
- if (x.toString() === 'N')
56
+ socket.write(this._frontend.getSSLRequestMessage());
57
+ socket.once('data', x => {
58
+ this._removeListeners();
59
+ if (x.toString() === 'S') {
60
+ const tslOptions = { ...options.ssl, socket };
61
+ if (options.host && net.isIP(options.host) === 0)
62
+ tslOptions.servername = options.host;
63
+ const tlsSocket = (this._socket = tls.connect(tslOptions));
64
+ tlsSocket.once('error', errorHandler);
65
+ tlsSocket.once('secureConnect', () => {
66
+ this._removeListeners();
67
+ this._handleConnect();
68
+ });
69
+ return;
70
+ }
71
+ if (x.toString() === 'N') {
72
+ if (options.requireSSL) {
73
73
  return errorHandler(new Error('Server does not support SSL connections'));
74
- return errorHandler(new Error('There was an error establishing an SSL connection'));
75
- });
76
- }
77
- else {
78
- this._handleConnect();
79
- }
74
+ }
75
+ this._removeListeners();
76
+ this._handleConnect();
77
+ return;
78
+ }
79
+ return errorHandler(new Error('There was an error establishing an SSL connection'));
80
+ });
80
81
  };
81
82
  socket.setNoDelay(true);
82
83
  socket.setTimeout(options.connectTimeoutMs || 30000, () => errorHandler(new Error('Connection timed out')));
@@ -283,8 +284,9 @@ export class PgSocket extends SafeEventEmitter {
283
284
  });
284
285
  break;
285
286
  case Protocol.AuthenticationMessageKind.SASL: {
286
- if (!msg.mechanisms.includes('SCRAM-SHA-256'))
287
+ if (!msg.mechanisms.includes('SCRAM-SHA-256')) {
287
288
  throw new Error('SASL: Only mechanism SCRAM-SHA-256 is currently supported');
289
+ }
288
290
  const saslSession = (this._saslSession = SASL.createSession(this.options.user || '', 'SCRAM-SHA-256'));
289
291
  this._send(this._frontend.getSASLMessage(saslSession));
290
292
  break;
@@ -308,6 +310,8 @@ export class PgSocket extends SafeEventEmitter {
308
310
  this._saslSession = undefined;
309
311
  break;
310
312
  }
313
+ default:
314
+ break;
311
315
  }
312
316
  }
313
317
  _handleParameterStatus(msg) {
@@ -33,6 +33,8 @@ export var SASL;
33
33
  case 'i':
34
34
  iteration = parseInt(i.substring(2), 10);
35
35
  break;
36
+ default:
37
+ break;
36
38
  }
37
39
  }
38
40
  if (!nonce)
@@ -69,25 +71,25 @@ export var SASL;
69
71
  throw new Error('SASL: Server signature does not match');
70
72
  }
71
73
  SASL.finalizeSession = finalizeSession;
72
- function firstMessageBare(username, nonce) {
74
+ const firstMessageBare = function (username, nonce) {
73
75
  return `n=${username},r=${nonce}`;
74
- }
76
+ };
75
77
  /**
76
78
  * Hi() is, essentially, PBKDF2 [RFC2898] with HMAC() as the
77
79
  * pseudorandom function (PRF) and with dkLen == output length of
78
80
  * HMAC() == output length of H()
79
81
  */
80
- function hi(text, salt, iterations) {
82
+ const hi = function (text, salt, iterations) {
81
83
  return crypto.pbkdf2Sync(text, Buffer.from(salt, 'base64'), iterations, 32, 'sha256');
82
- }
84
+ };
83
85
  const encode64 = str => Buffer.from(str).toString('base64');
84
- function hmac(key, msg) {
86
+ const hmac = function (key, msg) {
85
87
  return crypto.createHmac('sha256', key).update(msg).digest();
86
- }
87
- function hash(data) {
88
+ };
89
+ const hash = function (data) {
88
90
  return crypto.createHash('sha256').update(data).digest();
89
- }
90
- function xor(a, b) {
91
+ };
92
+ const xor = function (a, b) {
91
93
  a = Buffer.isBuffer(a) ? a : Buffer.from(a);
92
94
  b = Buffer.isBuffer(b) ? b : Buffer.from(b);
93
95
  if (a.length !== b.length)
@@ -98,5 +100,5 @@ export var SASL;
98
100
  out[i] = a[i] ^ b[i];
99
101
  }
100
102
  return out;
101
- }
103
+ };
102
104
  })(SASL || (SASL = {}));
@@ -23,9 +23,7 @@ export function parseConnectionString(str) {
23
23
  if (!str.includes('://'))
24
24
  str = 'postgres://' + str;
25
25
  const parsed = new URL(str);
26
- const getFirst = (v) => {
27
- return typeof v === 'string' ? v : Array.isArray(v) ? v[0] : '';
28
- };
26
+ const getFirst = (v) => (typeof v === 'string' ? v : Array.isArray(v) ? v[0] : '');
29
27
  const cfg = {};
30
28
  cfg.host = decodeURI(parsed.hostname || '');
31
29
  if (parsed.port)
@@ -47,11 +45,13 @@ export function parseConnectionString(str) {
47
45
  cfg.database = decodeURI(getFirst(parsed.searchParams.get('db')));
48
46
  if (parsed.searchParams.get('schema'))
49
47
  cfg.schema = decodeURI(getFirst(parsed.searchParams.get('schema')));
50
- if (parsed.searchParams.get('application_name'))
48
+ if (parsed.searchParams.get('application_name')) {
51
49
  cfg.applicationName = decodeURI(getFirst(parsed.searchParams.get('application_name')));
50
+ }
52
51
  if (parsed.username)
53
52
  cfg.user = parsed.username;
54
53
  if (parsed.password)
55
54
  cfg.password = parsed.password;
55
+ cfg.requireSSL = parsed.searchParams.get('sslmode') === 'require';
56
56
  return cfg;
57
57
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "postgrejs",
3
- "description": "Enterprise level PostgreSQL client for JavaScript",
4
- "version": "2.12.0",
3
+ "description": "Professional PostgreSQL client NodeJS",
4
+ "version": "2.15.0",
5
5
  "author": "Panates",
6
6
  "contributors": [
7
7
  "Eray Hanoglu <e.hanoglu@panates.com>"
@@ -9,7 +9,7 @@
9
9
  "license": "MIT",
10
10
  "repository": {
11
11
  "type": "git",
12
- "url": "https://github.com/panates/postgresql-client.git"
12
+ "url": "https://github.com/panates/postgrejs.git"
13
13
  },
14
14
  "type": "module",
15
15
  "main": "./cjs/index.js",
@@ -19,7 +19,7 @@
19
19
  "doublylinked": "^2.5.4",
20
20
  "lightning-pool": "^4.2.2",
21
21
  "postgres-bytea": "^3.0.0",
22
- "power-tasks": "^1.7.3",
22
+ "power-tasks": "^1.7.4",
23
23
  "putil-merge": "^3.12.1",
24
24
  "putil-promisify": "^1.10.1",
25
25
  "putil-varhelpers": "^1.6.5"
@@ -51,4 +51,4 @@
51
51
  "ts",
52
52
  "esm"
53
53
  ]
54
- }
54
+ }
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { ConnectionState } from '../constants.js';
3
2
  import { ConnectionConfiguration } from '../interfaces/database-connection-params.js';
4
3
  import { QueryOptions } from '../interfaces/query-options.js';
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { FieldInfo } from '../interfaces/field-info.js';
3
2
  import { QueryOptions } from '../interfaces/query-options.js';
4
3
  import { SafeEventEmitter } from '../safe-event-emitter.js';
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { QueryOptions } from '../interfaces/query-options.js';
3
2
  import { QueryResult } from '../interfaces/query-result.js';
4
3
  import { StatementPrepareOptions } from '../interfaces/statement-prepare-options.js';
@@ -1,11 +1,10 @@
1
- export * from './constants.js';
2
- export * from './data-type-map.js';
3
- export * from './types.js';
4
1
  export * from './connection/bind-param.js';
5
2
  export * from './connection/connection.js';
6
3
  export * from './connection/cursor.js';
7
4
  export * from './connection/pool.js';
8
5
  export * from './connection/prepared-statement.js';
6
+ export * from './constants.js';
7
+ export * from './data-type-map.js';
9
8
  export * from './interfaces/command-result.js';
10
9
  export * from './interfaces/data-mapping-options.js';
11
10
  export * from './interfaces/data-type.js';
@@ -16,10 +15,11 @@ export * from './interfaces/query-result.js';
16
15
  export * from './interfaces/script-execute-options.js';
17
16
  export * from './interfaces/script-result.js';
18
17
  export * from './interfaces/statement-prepare-options.js';
18
+ export * from './protocol/database-error.js';
19
+ export * from './protocol/smart-buffer.js';
20
+ export * from './types.js';
19
21
  export * from './util/connection-config.js';
20
22
  export * from './util/escape-literal.js';
21
23
  export * from './util/parse-datetime.js';
22
24
  export * from './util/stringify-arrayliteral.js';
23
25
  export * from './util/stringify-for-sql.js';
24
- export * from './protocol/smart-buffer.js';
25
- export * from './protocol/database-error.js';
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import type { PoolConfiguration as LPoolConfiguration } from 'lightning-pool';
3
2
  import type { ConnectionOptions as TlsConnectionOptions } from 'tls';
4
3
  import type { SmartBufferConfig } from '../protocol/smart-buffer.js';
@@ -10,6 +9,7 @@ export interface DatabaseConnectionParams {
10
9
  password?: string | (() => string | Promise<string>);
11
10
  database?: string;
12
11
  applicationName?: string;
12
+ requireSSL?: boolean;
13
13
  ssl?: TlsConnectionOptions;
14
14
  timezone?: string;
15
15
  schema?: string;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { Protocol } from './protocol.js';
3
2
  declare type ParseCallback = (code: Protocol.BackendMessageCode, data?: any) => void;
4
3
  export declare class Backend {
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  export declare class BufferReader {
3
2
  buffer: Buffer;
4
3
  offset: number;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { DataTypeMap } from '../data-type-map.js';
3
2
  import { QueryOptions } from '../interfaces/query-options.js';
4
3
  import { Maybe, OID } from '../types.js';
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { ConnectionState } from '../constants.js';
3
2
  import { ConnectionConfiguration } from '../interfaces/database-connection-params.js';
4
3
  import { SafeEventEmitter } from '../safe-event-emitter.js';
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { Nullable } from '../types.js';
3
2
  export declare namespace Protocol {
4
3
  const VERSION_MAJOR = 3;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { BufferReader } from './buffer-reader.js';
3
2
  export interface SmartBufferConfig {
4
3
  pageSize?: number;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { EventEmitter } from 'events';
3
2
  export declare class SafeEventEmitter extends EventEmitter {
4
3
  emit(event: string | symbol, ...args: any[]): boolean;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import type { DataMappingOptions } from './interfaces/data-mapping-options.js';
3
2
  import type { SmartBuffer } from './protocol/smart-buffer';
4
3
  export type OID = number;
@@ -1,3 +1,2 @@
1
- /// <reference types="node" />
2
1
  export declare function readBigInt64BE(buf: Buffer, offset?: number): bigint;
3
2
  export declare function writeBigUInt64BE(buf: Buffer, value: BigInt, offset?: number): number;
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { DataMappingOptions } from '../interfaces/data-mapping-options.js';
3
2
  import type { DecodeBinaryFunction, Nullable } from '../types.js';
4
3
  export declare function decodeBinaryArray<T = any>(buf: Buffer, decoder: DecodeBinaryFunction, options?: DataMappingOptions): Nullable<T[]>;