postgrejs 2.12.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 +814 -0
- package/LICENSE +21 -0
- package/README.md +200 -0
- package/cjs/connection/bind-param.js +10 -0
- package/cjs/connection/connection.js +279 -0
- package/cjs/connection/cursor.js +93 -0
- package/cjs/connection/intl-connection.js +243 -0
- package/cjs/connection/pool.js +226 -0
- package/cjs/connection/portal.js +148 -0
- package/cjs/connection/prepared-statement.js +217 -0
- package/cjs/constants.js +250 -0
- package/cjs/data-type-map.js +119 -0
- package/cjs/data-types/bool-type.js +27 -0
- package/cjs/data-types/box-type.js +52 -0
- package/cjs/data-types/bytea-type.js +29 -0
- package/cjs/data-types/char-type.js +27 -0
- package/cjs/data-types/circle-type.js +48 -0
- package/cjs/data-types/date-type.js +67 -0
- package/cjs/data-types/float4-type.js +25 -0
- package/cjs/data-types/float8-type.js +25 -0
- package/cjs/data-types/int2-type.js +26 -0
- package/cjs/data-types/int2-vector-type.js +38 -0
- package/cjs/data-types/int4-type.js +26 -0
- package/cjs/data-types/int8-type.js +31 -0
- package/cjs/data-types/json-type.js +38 -0
- package/cjs/data-types/jsonb-type.js +42 -0
- package/cjs/data-types/lseg-type.js +52 -0
- package/cjs/data-types/numeric-type.js +125 -0
- package/cjs/data-types/oid-type.js +32 -0
- package/cjs/data-types/point-type.js +39 -0
- package/cjs/data-types/time-type.js +54 -0
- package/cjs/data-types/timestamp-type.js +81 -0
- package/cjs/data-types/timestamptz-type.js +73 -0
- package/cjs/data-types/uuid-type.js +39 -0
- package/cjs/data-types/varchar-type.js +27 -0
- package/cjs/index.js +41 -0
- package/cjs/interfaces/command-result.js +2 -0
- package/cjs/interfaces/data-mapping-options.js +2 -0
- package/cjs/interfaces/data-type.js +2 -0
- package/cjs/interfaces/database-connection-params.js +2 -0
- package/cjs/interfaces/field-info.js +2 -0
- package/cjs/interfaces/query-options.js +2 -0
- package/cjs/interfaces/query-result.js +2 -0
- package/cjs/interfaces/script-execute-options.js +2 -0
- package/cjs/interfaces/script-result.js +2 -0
- package/cjs/interfaces/statement-prepare-options.js +2 -0
- package/cjs/package.json +3 -0
- package/cjs/protocol/backend.js +256 -0
- package/cjs/protocol/buffer-reader.js +78 -0
- package/cjs/protocol/database-error.js +17 -0
- package/cjs/protocol/frontend.js +218 -0
- package/cjs/protocol/pg-socket.js +345 -0
- package/cjs/protocol/protocol.js +71 -0
- package/cjs/protocol/sasl.js +108 -0
- package/cjs/protocol/smart-buffer.js +195 -0
- package/cjs/safe-event-emitter.js +17 -0
- package/cjs/types.js +2 -0
- package/cjs/util/array-calculatedim.js +19 -0
- package/cjs/util/bigint-methods.js +42 -0
- package/cjs/util/config-from-env.js +30 -0
- package/cjs/util/connection-config.js +65 -0
- package/cjs/util/convert-row-to-object.js +13 -0
- package/cjs/util/decode-binaryarray.js +39 -0
- package/cjs/util/encode-binaryarray.js +45 -0
- package/cjs/util/escape-literal.js +27 -0
- package/cjs/util/fast-parseint.js +32 -0
- package/cjs/util/get-parsers.js +42 -0
- package/cjs/util/parse-array.js +61 -0
- package/cjs/util/parse-datetime.js +38 -0
- package/cjs/util/parse-row.js +11 -0
- package/cjs/util/parse-time.js +29 -0
- package/cjs/util/stringify-arrayliteral.js +38 -0
- package/cjs/util/stringify-for-sql.js +30 -0
- package/cjs/util/wrap-row-description.js +35 -0
- package/esm/connection/bind-param.js +6 -0
- package/esm/connection/connection.js +275 -0
- package/esm/connection/cursor.js +86 -0
- package/esm/connection/intl-connection.js +238 -0
- package/esm/connection/pool.js +222 -0
- package/esm/connection/portal.js +144 -0
- package/esm/connection/prepared-statement.js +213 -0
- package/esm/constants.js +247 -0
- package/esm/data-type-map.js +115 -0
- package/esm/data-types/bool-type.js +24 -0
- package/esm/data-types/box-type.js +49 -0
- package/esm/data-types/bytea-type.js +23 -0
- package/esm/data-types/char-type.js +24 -0
- package/esm/data-types/circle-type.js +45 -0
- package/esm/data-types/date-type.js +64 -0
- package/esm/data-types/float4-type.js +22 -0
- package/esm/data-types/float8-type.js +22 -0
- package/esm/data-types/int2-type.js +23 -0
- package/esm/data-types/int2-vector-type.js +35 -0
- package/esm/data-types/int4-type.js +23 -0
- package/esm/data-types/int8-type.js +28 -0
- package/esm/data-types/json-type.js +35 -0
- package/esm/data-types/jsonb-type.js +39 -0
- package/esm/data-types/lseg-type.js +49 -0
- package/esm/data-types/numeric-type.js +121 -0
- package/esm/data-types/oid-type.js +29 -0
- package/esm/data-types/point-type.js +36 -0
- package/esm/data-types/time-type.js +51 -0
- package/esm/data-types/timestamp-type.js +78 -0
- package/esm/data-types/timestamptz-type.js +70 -0
- package/esm/data-types/uuid-type.js +36 -0
- package/esm/data-types/varchar-type.js +24 -0
- package/esm/index.js +25 -0
- package/esm/interfaces/command-result.js +1 -0
- package/esm/interfaces/data-mapping-options.js +1 -0
- package/esm/interfaces/data-type.js +1 -0
- package/esm/interfaces/database-connection-params.js +1 -0
- package/esm/interfaces/field-info.js +1 -0
- package/esm/interfaces/query-options.js +1 -0
- package/esm/interfaces/query-result.js +1 -0
- package/esm/interfaces/script-execute-options.js +1 -0
- package/esm/interfaces/script-result.js +1 -0
- package/esm/interfaces/statement-prepare-options.js +1 -0
- package/esm/protocol/backend.js +252 -0
- package/esm/protocol/buffer-reader.js +74 -0
- package/esm/protocol/database-error.js +13 -0
- package/esm/protocol/frontend.js +214 -0
- package/esm/protocol/pg-socket.js +338 -0
- package/esm/protocol/protocol.js +68 -0
- package/esm/protocol/sasl.js +102 -0
- package/esm/protocol/smart-buffer.js +168 -0
- package/esm/safe-event-emitter.js +13 -0
- package/esm/types.js +1 -0
- package/esm/util/array-calculatedim.js +15 -0
- package/esm/util/bigint-methods.js +37 -0
- package/esm/util/config-from-env.js +26 -0
- package/esm/util/connection-config.js +57 -0
- package/esm/util/convert-row-to-object.js +9 -0
- package/esm/util/decode-binaryarray.js +35 -0
- package/esm/util/encode-binaryarray.js +41 -0
- package/esm/util/escape-literal.js +23 -0
- package/esm/util/fast-parseint.js +28 -0
- package/esm/util/get-parsers.js +38 -0
- package/esm/util/parse-array.js +57 -0
- package/esm/util/parse-datetime.js +34 -0
- package/esm/util/parse-row.js +7 -0
- package/esm/util/parse-time.js +25 -0
- package/esm/util/stringify-arrayliteral.js +34 -0
- package/esm/util/stringify-for-sql.js +25 -0
- package/esm/util/wrap-row-description.js +31 -0
- package/package.json +54 -0
- package/typings/connection/bind-param.d.ts +6 -0
- package/typings/connection/connection.d.ts +110 -0
- package/typings/connection/cursor.d.ts +25 -0
- package/typings/connection/intl-connection.d.ts +40 -0
- package/typings/connection/pool.d.ts +52 -0
- package/typings/connection/portal.d.ts +23 -0
- package/typings/connection/prepared-statement.d.ts +27 -0
- package/typings/constants.d.ts +130 -0
- package/typings/data-type-map.d.ts +11 -0
- package/typings/data-types/bool-type.d.ts +3 -0
- package/typings/data-types/box-type.d.ts +3 -0
- package/typings/data-types/bytea-type.d.ts +3 -0
- package/typings/data-types/char-type.d.ts +3 -0
- package/typings/data-types/circle-type.d.ts +3 -0
- package/typings/data-types/date-type.d.ts +3 -0
- package/typings/data-types/float4-type.d.ts +3 -0
- package/typings/data-types/float8-type.d.ts +3 -0
- package/typings/data-types/int2-type.d.ts +3 -0
- package/typings/data-types/int2-vector-type.d.ts +3 -0
- package/typings/data-types/int4-type.d.ts +3 -0
- package/typings/data-types/int8-type.d.ts +3 -0
- package/typings/data-types/json-type.d.ts +3 -0
- package/typings/data-types/jsonb-type.d.ts +3 -0
- package/typings/data-types/lseg-type.d.ts +3 -0
- package/typings/data-types/numeric-type.d.ts +12 -0
- package/typings/data-types/oid-type.d.ts +4 -0
- package/typings/data-types/point-type.d.ts +3 -0
- package/typings/data-types/time-type.d.ts +3 -0
- package/typings/data-types/timestamp-type.d.ts +3 -0
- package/typings/data-types/timestamptz-type.d.ts +3 -0
- package/typings/data-types/uuid-type.d.ts +3 -0
- package/typings/data-types/varchar-type.d.ts +3 -0
- package/typings/index.d.ts +25 -0
- package/typings/interfaces/command-result.d.ts +28 -0
- package/typings/interfaces/data-mapping-options.d.ts +9 -0
- package/typings/interfaces/data-type.d.ts +31 -0
- package/typings/interfaces/database-connection-params.d.ts +38 -0
- package/typings/interfaces/field-info.d.ts +46 -0
- package/typings/interfaces/query-options.d.ts +46 -0
- package/typings/interfaces/query-result.d.ts +8 -0
- package/typings/interfaces/script-execute-options.d.ts +26 -0
- package/typings/interfaces/script-result.d.ts +15 -0
- package/typings/interfaces/statement-prepare-options.d.ts +13 -0
- package/typings/protocol/backend.d.ts +9 -0
- package/typings/protocol/buffer-reader.d.ts +18 -0
- package/typings/protocol/database-error.d.ts +20 -0
- package/typings/protocol/frontend.d.ts +59 -0
- package/typings/protocol/pg-socket.d.ts +51 -0
- package/typings/protocol/protocol.d.ts +180 -0
- package/typings/protocol/sasl.d.ts +13 -0
- package/typings/protocol/smart-buffer.d.ts +39 -0
- package/typings/safe-event-emitter.d.ts +5 -0
- package/typings/types.d.ts +15 -0
- package/typings/util/array-calculatedim.d.ts +1 -0
- package/typings/util/bigint-methods.d.ts +3 -0
- package/typings/util/config-from-env.d.ts +2 -0
- package/typings/util/connection-config.d.ts +3 -0
- package/typings/util/convert-row-to-object.d.ts +2 -0
- package/typings/util/decode-binaryarray.d.ts +4 -0
- package/typings/util/encode-binaryarray.d.ts +5 -0
- package/typings/util/escape-literal.d.ts +1 -0
- package/typings/util/fast-parseint.d.ts +1 -0
- package/typings/util/get-parsers.d.ts +4 -0
- package/typings/util/parse-array.d.ts +5 -0
- package/typings/util/parse-datetime.d.ts +1 -0
- package/typings/util/parse-row.d.ts +3 -0
- package/typings/util/parse-time.d.ts +3 -0
- package/typings/util/stringify-arrayliteral.d.ts +3 -0
- package/typings/util/stringify-for-sql.d.ts +4 -0
- package/typings/util/wrap-row-description.d.ts +5 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Protocol = void 0;
|
|
4
|
+
var Protocol;
|
|
5
|
+
(function (Protocol) {
|
|
6
|
+
Protocol.VERSION_MAJOR = 3;
|
|
7
|
+
Protocol.VERSION_MINOR = 0;
|
|
8
|
+
// https://www.postgresql.org/docs/9.3/protocol-message-formats.html
|
|
9
|
+
let BackendMessageCode;
|
|
10
|
+
(function (BackendMessageCode) {
|
|
11
|
+
BackendMessageCode[BackendMessageCode["Authentication"] = 82] = "Authentication";
|
|
12
|
+
BackendMessageCode[BackendMessageCode["BackendKeyData"] = 75] = "BackendKeyData";
|
|
13
|
+
BackendMessageCode[BackendMessageCode["BindComplete"] = 50] = "BindComplete";
|
|
14
|
+
BackendMessageCode[BackendMessageCode["CloseComplete"] = 51] = "CloseComplete";
|
|
15
|
+
BackendMessageCode[BackendMessageCode["CommandComplete"] = 67] = "CommandComplete";
|
|
16
|
+
BackendMessageCode[BackendMessageCode["CopyData"] = 100] = "CopyData";
|
|
17
|
+
BackendMessageCode[BackendMessageCode["CopyDone"] = 99] = "CopyDone";
|
|
18
|
+
BackendMessageCode[BackendMessageCode["CopyInResponse"] = 103] = "CopyInResponse";
|
|
19
|
+
BackendMessageCode[BackendMessageCode["CopyOutResponse"] = 72] = "CopyOutResponse";
|
|
20
|
+
BackendMessageCode[BackendMessageCode["CopyBothResponse"] = 87] = "CopyBothResponse";
|
|
21
|
+
BackendMessageCode[BackendMessageCode["DataRow"] = 68] = "DataRow";
|
|
22
|
+
BackendMessageCode[BackendMessageCode["EmptyQueryResponse"] = 73] = "EmptyQueryResponse";
|
|
23
|
+
BackendMessageCode[BackendMessageCode["ErrorResponse"] = 69] = "ErrorResponse";
|
|
24
|
+
BackendMessageCode[BackendMessageCode["FunctionCallResponse"] = 86] = "FunctionCallResponse";
|
|
25
|
+
BackendMessageCode[BackendMessageCode["NegotiateProtocolVersion"] = 118] = "NegotiateProtocolVersion";
|
|
26
|
+
BackendMessageCode[BackendMessageCode["NoData"] = 110] = "NoData";
|
|
27
|
+
BackendMessageCode[BackendMessageCode["NoticeResponse"] = 78] = "NoticeResponse";
|
|
28
|
+
BackendMessageCode[BackendMessageCode["NotificationResponse"] = 65] = "NotificationResponse";
|
|
29
|
+
BackendMessageCode[BackendMessageCode["ParameterDescription"] = 116] = "ParameterDescription";
|
|
30
|
+
BackendMessageCode[BackendMessageCode["ParameterStatus"] = 83] = "ParameterStatus";
|
|
31
|
+
BackendMessageCode[BackendMessageCode["ParseComplete"] = 49] = "ParseComplete";
|
|
32
|
+
BackendMessageCode[BackendMessageCode["PortalSuspended"] = 115] = "PortalSuspended";
|
|
33
|
+
BackendMessageCode[BackendMessageCode["ReadyForQuery"] = 90] = "ReadyForQuery";
|
|
34
|
+
BackendMessageCode[BackendMessageCode["RowDescription"] = 84] = "RowDescription";
|
|
35
|
+
})(BackendMessageCode = Protocol.BackendMessageCode || (Protocol.BackendMessageCode = {}));
|
|
36
|
+
let FrontendMessageCode;
|
|
37
|
+
(function (FrontendMessageCode) {
|
|
38
|
+
FrontendMessageCode[FrontendMessageCode["Bind"] = 66] = "Bind";
|
|
39
|
+
FrontendMessageCode[FrontendMessageCode["Close"] = 67] = "Close";
|
|
40
|
+
FrontendMessageCode[FrontendMessageCode["CopyData"] = 100] = "CopyData";
|
|
41
|
+
FrontendMessageCode[FrontendMessageCode["CopyDone"] = 99] = "CopyDone";
|
|
42
|
+
FrontendMessageCode[FrontendMessageCode["CopyFail"] = 102] = "CopyFail";
|
|
43
|
+
FrontendMessageCode[FrontendMessageCode["Describe"] = 68] = "Describe";
|
|
44
|
+
FrontendMessageCode[FrontendMessageCode["Execute"] = 69] = "Execute";
|
|
45
|
+
FrontendMessageCode[FrontendMessageCode["Flush"] = 72] = "Flush";
|
|
46
|
+
FrontendMessageCode[FrontendMessageCode["FunctionCall"] = 70] = "FunctionCall";
|
|
47
|
+
FrontendMessageCode[FrontendMessageCode["Parse"] = 80] = "Parse";
|
|
48
|
+
FrontendMessageCode[FrontendMessageCode["PasswordMessage"] = 112] = "PasswordMessage";
|
|
49
|
+
FrontendMessageCode[FrontendMessageCode["Query"] = 81] = "Query";
|
|
50
|
+
FrontendMessageCode[FrontendMessageCode["Sync"] = 83] = "Sync";
|
|
51
|
+
FrontendMessageCode[FrontendMessageCode["Terminate"] = 88] = "Terminate";
|
|
52
|
+
})(FrontendMessageCode = Protocol.FrontendMessageCode || (Protocol.FrontendMessageCode = {}));
|
|
53
|
+
let AuthenticationMessageKind;
|
|
54
|
+
(function (AuthenticationMessageKind) {
|
|
55
|
+
AuthenticationMessageKind["KerberosV5"] = "KerberosV5";
|
|
56
|
+
AuthenticationMessageKind["CleartextPassword"] = "CleartextPassword";
|
|
57
|
+
AuthenticationMessageKind["MD5Password"] = "MD5Password";
|
|
58
|
+
AuthenticationMessageKind["SCMCredential"] = "SCMCredential";
|
|
59
|
+
AuthenticationMessageKind["GSS"] = "GSS";
|
|
60
|
+
AuthenticationMessageKind["SSPI"] = "SSPI";
|
|
61
|
+
AuthenticationMessageKind["GSSContinue"] = "GSSContinue";
|
|
62
|
+
AuthenticationMessageKind["SASL"] = "SASL";
|
|
63
|
+
AuthenticationMessageKind["SASLContinue"] = "SASLContinue";
|
|
64
|
+
AuthenticationMessageKind["SASLFinal"] = "SASLFinal";
|
|
65
|
+
})(AuthenticationMessageKind = Protocol.AuthenticationMessageKind || (Protocol.AuthenticationMessageKind = {}));
|
|
66
|
+
let DataFormat;
|
|
67
|
+
(function (DataFormat) {
|
|
68
|
+
DataFormat[DataFormat["text"] = 0] = "text";
|
|
69
|
+
DataFormat[DataFormat["binary"] = 1] = "binary";
|
|
70
|
+
})(DataFormat = Protocol.DataFormat || (Protocol.DataFormat = {}));
|
|
71
|
+
})(Protocol || (exports.Protocol = Protocol = {}));
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SASL = void 0;
|
|
7
|
+
/* eslint-disable no-bitwise */
|
|
8
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
9
|
+
var SASL;
|
|
10
|
+
(function (SASL) {
|
|
11
|
+
const CLIENT_KEY = 'Client Key';
|
|
12
|
+
const SERVER_KEY = 'Server Key';
|
|
13
|
+
const GS2_HEADER = 'n,,';
|
|
14
|
+
function createSession(username, mechanism) {
|
|
15
|
+
const nonce = crypto_1.default.randomBytes(18).toString('base64');
|
|
16
|
+
const clientFirstMessage = `${GS2_HEADER}${firstMessageBare(username, nonce)}`;
|
|
17
|
+
return {
|
|
18
|
+
username,
|
|
19
|
+
mechanism,
|
|
20
|
+
nonce,
|
|
21
|
+
clientFirstMessage,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
SASL.createSession = createSession;
|
|
25
|
+
function continueSession(session, password, data) {
|
|
26
|
+
const s = data.toString();
|
|
27
|
+
const items = s.split(',');
|
|
28
|
+
let nonce = '';
|
|
29
|
+
let salt = '';
|
|
30
|
+
let iteration = 0;
|
|
31
|
+
for (const i of items) {
|
|
32
|
+
switch (i[0]) {
|
|
33
|
+
case 'r':
|
|
34
|
+
nonce = i.substring(2);
|
|
35
|
+
break;
|
|
36
|
+
case 's':
|
|
37
|
+
salt = i.substring(2);
|
|
38
|
+
break;
|
|
39
|
+
case 'i':
|
|
40
|
+
iteration = parseInt(i.substring(2), 10);
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (!nonce)
|
|
45
|
+
throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: nonce missing');
|
|
46
|
+
if (!salt)
|
|
47
|
+
throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: salt missing');
|
|
48
|
+
if (!iteration)
|
|
49
|
+
throw new Error('SASL: SCRAM-SERVER-FIRST-MESSAGE: iteration missing');
|
|
50
|
+
if (!nonce.startsWith(session.nonce))
|
|
51
|
+
throw new Error('SASL: Server nonce does not start with client nonce');
|
|
52
|
+
const serverFirstMessage = `r=${nonce},s=${salt},i=${iteration}`;
|
|
53
|
+
const clientFinalMessageWithoutProof = `c=${encode64(GS2_HEADER)},r=${nonce}`;
|
|
54
|
+
const authMessage = `${firstMessageBare(session.username, session.nonce)},${serverFirstMessage},${clientFinalMessageWithoutProof}`;
|
|
55
|
+
const saltPass = hi(password, salt, iteration);
|
|
56
|
+
const clientKey = hmac(saltPass, CLIENT_KEY);
|
|
57
|
+
const storedKey = hash(clientKey);
|
|
58
|
+
const clientSignature = hmac(storedKey, authMessage);
|
|
59
|
+
const clientProofBytes = xor(clientKey, clientSignature);
|
|
60
|
+
const clientProof = clientProofBytes.toString('base64');
|
|
61
|
+
const serverKey = hmac(saltPass, SERVER_KEY);
|
|
62
|
+
const serverSignatureBytes = hmac(serverKey, authMessage);
|
|
63
|
+
session.serverSignature = serverSignatureBytes.toString('base64');
|
|
64
|
+
session.clientFinalMessage = clientFinalMessageWithoutProof + ',p=' + clientProof;
|
|
65
|
+
}
|
|
66
|
+
SASL.continueSession = continueSession;
|
|
67
|
+
function finalizeSession(session, data) {
|
|
68
|
+
let serverSignature = '';
|
|
69
|
+
const arr = data.split(',');
|
|
70
|
+
for (const s of arr) {
|
|
71
|
+
if (s[0] === 'v')
|
|
72
|
+
serverSignature = s.substr(2);
|
|
73
|
+
}
|
|
74
|
+
if (serverSignature !== session.serverSignature)
|
|
75
|
+
throw new Error('SASL: Server signature does not match');
|
|
76
|
+
}
|
|
77
|
+
SASL.finalizeSession = finalizeSession;
|
|
78
|
+
function firstMessageBare(username, nonce) {
|
|
79
|
+
return `n=${username},r=${nonce}`;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Hi() is, essentially, PBKDF2 [RFC2898] with HMAC() as the
|
|
83
|
+
* pseudorandom function (PRF) and with dkLen == output length of
|
|
84
|
+
* HMAC() == output length of H()
|
|
85
|
+
*/
|
|
86
|
+
function hi(text, salt, iterations) {
|
|
87
|
+
return crypto_1.default.pbkdf2Sync(text, Buffer.from(salt, 'base64'), iterations, 32, 'sha256');
|
|
88
|
+
}
|
|
89
|
+
const encode64 = str => Buffer.from(str).toString('base64');
|
|
90
|
+
function hmac(key, msg) {
|
|
91
|
+
return crypto_1.default.createHmac('sha256', key).update(msg).digest();
|
|
92
|
+
}
|
|
93
|
+
function hash(data) {
|
|
94
|
+
return crypto_1.default.createHash('sha256').update(data).digest();
|
|
95
|
+
}
|
|
96
|
+
function xor(a, b) {
|
|
97
|
+
a = Buffer.isBuffer(a) ? a : Buffer.from(a);
|
|
98
|
+
b = Buffer.isBuffer(b) ? b : Buffer.from(b);
|
|
99
|
+
if (a.length !== b.length)
|
|
100
|
+
throw new Error('Buffers must be of the same length');
|
|
101
|
+
const l = a.length;
|
|
102
|
+
const out = Buffer.allocUnsafe(l);
|
|
103
|
+
for (let i = 0; i < l; i++) {
|
|
104
|
+
out[i] = a[i] ^ b[i];
|
|
105
|
+
}
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
})(SASL || (exports.SASL = SASL = {}));
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.SmartBuffer = void 0;
|
|
27
|
+
const os = __importStar(require("os"));
|
|
28
|
+
const bigint_methods_js_1 = require("../util/bigint-methods.js");
|
|
29
|
+
const buffer_reader_js_1 = require("./buffer-reader.js");
|
|
30
|
+
class SmartBuffer extends buffer_reader_js_1.BufferReader {
|
|
31
|
+
constructor(cfg) {
|
|
32
|
+
// @ts-ignore
|
|
33
|
+
super(Buffer.allocUnsafe((cfg?.pageSize ? parseInt(cfg.pageSize, 10) : 0) || SmartBuffer.DEFAULT_PAGE_SIZE));
|
|
34
|
+
this._lastHouseKeep = 0;
|
|
35
|
+
this._stMaxPages = 1;
|
|
36
|
+
this._length = 0;
|
|
37
|
+
this._houseKeepInterval = cfg?.houseKeepInterval || 5000;
|
|
38
|
+
this.pageSize = this.buffer.length;
|
|
39
|
+
this.maxSize = cfg?.maxLength || SmartBuffer.DEFAULT_MAX_SIZE;
|
|
40
|
+
this._length = 0;
|
|
41
|
+
}
|
|
42
|
+
get capacity() {
|
|
43
|
+
return this.buffer.length;
|
|
44
|
+
}
|
|
45
|
+
get length() {
|
|
46
|
+
return this._length;
|
|
47
|
+
}
|
|
48
|
+
start() {
|
|
49
|
+
this.offset = 0;
|
|
50
|
+
this._length = 0;
|
|
51
|
+
if (this._houseKeepTimer) {
|
|
52
|
+
clearTimeout(this._houseKeepTimer);
|
|
53
|
+
this._houseKeepTimer = undefined;
|
|
54
|
+
}
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
flush() {
|
|
58
|
+
if (this._houseKeepTimer)
|
|
59
|
+
clearTimeout(this._houseKeepTimer);
|
|
60
|
+
const length = this.length;
|
|
61
|
+
this._length = 0;
|
|
62
|
+
const out = this.buffer.slice(0, length);
|
|
63
|
+
const pages = length ? Math.ceil(length / this.pageSize) : 1;
|
|
64
|
+
this._stMaxPages = Math.max(this._stMaxPages, pages);
|
|
65
|
+
if (this._lastHouseKeep < Date.now() + this._houseKeepInterval)
|
|
66
|
+
this._houseKeep();
|
|
67
|
+
this._houseKeepTimer = setTimeout(() => {
|
|
68
|
+
this._houseKeepTimer = undefined;
|
|
69
|
+
this._houseKeep();
|
|
70
|
+
}, this._houseKeepInterval).unref();
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
growSize(len) {
|
|
74
|
+
const endOffset = this.offset + len;
|
|
75
|
+
if (this.capacity < endOffset) {
|
|
76
|
+
if (endOffset > this.maxSize)
|
|
77
|
+
throw new Error('Buffer limit exceeded.');
|
|
78
|
+
const newSize = Math.ceil(endOffset / this.pageSize) * this.pageSize;
|
|
79
|
+
const newBuffer = Buffer.allocUnsafe(newSize);
|
|
80
|
+
this.buffer.copy(newBuffer);
|
|
81
|
+
this.buffer = newBuffer;
|
|
82
|
+
}
|
|
83
|
+
this._length = Math.max(this.length, endOffset);
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
fill(value = 0, len = 1) {
|
|
87
|
+
this.growSize(len);
|
|
88
|
+
this.buffer.fill(value, this.offset, this.offset + len);
|
|
89
|
+
this.offset += len;
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
writeCString(str, encoding) {
|
|
93
|
+
const len = str ? Buffer.byteLength(str, encoding) : 0;
|
|
94
|
+
this.growSize(len + 1);
|
|
95
|
+
if (str) {
|
|
96
|
+
this.buffer.write(str, this.offset, encoding);
|
|
97
|
+
this.offset += len;
|
|
98
|
+
}
|
|
99
|
+
this.writeUInt8(0);
|
|
100
|
+
return this;
|
|
101
|
+
}
|
|
102
|
+
writeLString(str, encoding) {
|
|
103
|
+
const len = str ? Buffer.byteLength(str, encoding) : 0;
|
|
104
|
+
this.growSize(len + 4);
|
|
105
|
+
this.writeInt32BE(str == null ? -1 : len);
|
|
106
|
+
if (str) {
|
|
107
|
+
if (encoding)
|
|
108
|
+
this.offset += this.buffer.write(str, this.offset, encoding);
|
|
109
|
+
else
|
|
110
|
+
this.offset += this.buffer.write(str, this.offset);
|
|
111
|
+
}
|
|
112
|
+
return this;
|
|
113
|
+
}
|
|
114
|
+
writeString(str, encoding) {
|
|
115
|
+
if (str) {
|
|
116
|
+
const len = Buffer.byteLength(str, encoding);
|
|
117
|
+
this.growSize(len);
|
|
118
|
+
this.offset += this.buffer.write(str, this.offset, encoding);
|
|
119
|
+
}
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
writeInt8(n) {
|
|
123
|
+
this.growSize(1);
|
|
124
|
+
this.buffer.writeInt8(n, this.offset);
|
|
125
|
+
this.offset++;
|
|
126
|
+
return this;
|
|
127
|
+
}
|
|
128
|
+
writeUInt8(n) {
|
|
129
|
+
this.growSize(1);
|
|
130
|
+
this.buffer.writeUInt8(n, this.offset);
|
|
131
|
+
this.offset++;
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
writeUInt16BE(n) {
|
|
135
|
+
this.growSize(2);
|
|
136
|
+
this.buffer.writeUInt16BE(n, this.offset);
|
|
137
|
+
this.offset += 2;
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
writeUInt32BE(n) {
|
|
141
|
+
this.growSize(4);
|
|
142
|
+
this.buffer.writeUInt32BE(n, this.offset);
|
|
143
|
+
this.offset += 4;
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
writeInt16BE(n) {
|
|
147
|
+
this.growSize(2);
|
|
148
|
+
this.buffer.writeInt16BE(n, this.offset);
|
|
149
|
+
this.offset += 2;
|
|
150
|
+
return this;
|
|
151
|
+
}
|
|
152
|
+
writeInt32BE(n) {
|
|
153
|
+
this.growSize(4);
|
|
154
|
+
this.buffer.writeInt32BE(n, this.offset);
|
|
155
|
+
this.offset += 4;
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
158
|
+
writeBigInt64BE(n) {
|
|
159
|
+
n = typeof n === 'bigint' ? n : BigInt(n);
|
|
160
|
+
this.growSize(8);
|
|
161
|
+
if (typeof this.buffer.writeBigInt64BE === 'function')
|
|
162
|
+
this.buffer.writeBigInt64BE(n, this.offset);
|
|
163
|
+
else
|
|
164
|
+
(0, bigint_methods_js_1.writeBigUInt64BE)(this.buffer, n, this.offset);
|
|
165
|
+
this.offset += 8;
|
|
166
|
+
return this;
|
|
167
|
+
}
|
|
168
|
+
writeFloatBE(n) {
|
|
169
|
+
this.growSize(4);
|
|
170
|
+
this.buffer.writeFloatBE(n, this.offset);
|
|
171
|
+
this.offset += 4;
|
|
172
|
+
return this;
|
|
173
|
+
}
|
|
174
|
+
writeDoubleBE(n) {
|
|
175
|
+
this.growSize(8);
|
|
176
|
+
this.buffer.writeDoubleBE(n, this.offset);
|
|
177
|
+
this.offset += 8;
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
180
|
+
writeBuffer(buffer) {
|
|
181
|
+
this.growSize(buffer.length);
|
|
182
|
+
buffer.copy(this.buffer, this.offset, 0, buffer.length);
|
|
183
|
+
this.offset += buffer.length;
|
|
184
|
+
return this;
|
|
185
|
+
}
|
|
186
|
+
_houseKeep() {
|
|
187
|
+
const needSize = this._stMaxPages * this.pageSize;
|
|
188
|
+
if (this.buffer.length > needSize)
|
|
189
|
+
this.buffer = Buffer.allocUnsafe(needSize);
|
|
190
|
+
this._stMaxPages = this.length ? Math.ceil(this.length / this.pageSize) : 1;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
exports.SmartBuffer = SmartBuffer;
|
|
194
|
+
SmartBuffer.DEFAULT_PAGE_SIZE = 4096;
|
|
195
|
+
SmartBuffer.DEFAULT_MAX_SIZE = Math.min(Math.floor(os.totalmem() / 2), 1024 * 1024 * 1024 * 2);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SafeEventEmitter = void 0;
|
|
4
|
+
const events_1 = require("events");
|
|
5
|
+
class SafeEventEmitter extends events_1.EventEmitter {
|
|
6
|
+
emit(event, ...args) {
|
|
7
|
+
try {
|
|
8
|
+
if (event === 'error' && !this.listenerCount('error'))
|
|
9
|
+
return false;
|
|
10
|
+
return super.emit(event, ...args);
|
|
11
|
+
}
|
|
12
|
+
catch (ignored) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.SafeEventEmitter = SafeEventEmitter;
|
package/cjs/types.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.arrayCalculateDim = void 0;
|
|
4
|
+
function arrayCalculateDim(arr) {
|
|
5
|
+
if (!arr || arr.length === 0)
|
|
6
|
+
return [0];
|
|
7
|
+
const dim = [arr.length];
|
|
8
|
+
const iterate = (a, level) => {
|
|
9
|
+
for (let i = 0; i < a.length; i++) {
|
|
10
|
+
if (Array.isArray(a[i])) {
|
|
11
|
+
dim[level] = Math.max(dim[level] || 0, a[i].length);
|
|
12
|
+
iterate(a[i], level + 1);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
iterate(arr, 1);
|
|
17
|
+
return dim;
|
|
18
|
+
}
|
|
19
|
+
exports.arrayCalculateDim = arrayCalculateDim;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.writeBigUInt64BE = exports.readBigInt64BE = void 0;
|
|
4
|
+
/* eslint-disable no-bitwise */
|
|
5
|
+
const big0 = BigInt(0);
|
|
6
|
+
const beAnd = BigInt('0xffffffff');
|
|
7
|
+
const big32 = BigInt(32);
|
|
8
|
+
// https://github.com/nodejs/node/blob/v13.9.0/lib/internal/buffer.js
|
|
9
|
+
function readBigInt64BE(buf, offset = 0) {
|
|
10
|
+
const first = buf[offset];
|
|
11
|
+
const last = buf[offset + 7];
|
|
12
|
+
if (first === undefined || last === undefined)
|
|
13
|
+
return big0;
|
|
14
|
+
const val = (first << 24) + // Overflow
|
|
15
|
+
buf[++offset] * 2 ** 16 +
|
|
16
|
+
buf[++offset] * 2 ** 8 +
|
|
17
|
+
buf[++offset];
|
|
18
|
+
return ((BigInt(val) << big32) + BigInt(buf[++offset] * 2 ** 24 + buf[++offset] * 2 ** 16 + buf[++offset] * 2 ** 8 + last));
|
|
19
|
+
}
|
|
20
|
+
exports.readBigInt64BE = readBigInt64BE;
|
|
21
|
+
function writeBigUInt64BE(buf, value, offset = 0) {
|
|
22
|
+
// @ts-ignore
|
|
23
|
+
let lo = Number(value & beAnd);
|
|
24
|
+
buf[offset + 7] = lo;
|
|
25
|
+
lo = lo >> 8;
|
|
26
|
+
buf[offset + 6] = lo;
|
|
27
|
+
lo = lo >> 8;
|
|
28
|
+
buf[offset + 5] = lo;
|
|
29
|
+
lo = lo >> 8;
|
|
30
|
+
buf[offset + 4] = lo;
|
|
31
|
+
// @ts-ignore
|
|
32
|
+
let hi = Number((value >> big32) & beAnd);
|
|
33
|
+
buf[offset + 3] = hi;
|
|
34
|
+
hi = hi >> 8;
|
|
35
|
+
buf[offset + 2] = hi;
|
|
36
|
+
hi = hi >> 8;
|
|
37
|
+
buf[offset + 1] = hi;
|
|
38
|
+
hi = hi >> 8;
|
|
39
|
+
buf[offset] = hi;
|
|
40
|
+
return offset + 8;
|
|
41
|
+
}
|
|
42
|
+
exports.writeBigUInt64BE = writeBigUInt64BE;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.configFromEnv = void 0;
|
|
4
|
+
const putil_varhelpers_1 = require("putil-varhelpers");
|
|
5
|
+
function configFromEnv() {
|
|
6
|
+
const env = process.env;
|
|
7
|
+
const result = {};
|
|
8
|
+
result.host = env.PGHOST || env.PGHOSTADDR;
|
|
9
|
+
if (env.PGPORT)
|
|
10
|
+
result.port = (0, putil_varhelpers_1.toIntDef)(env.PGPORT, 5432);
|
|
11
|
+
if (env.PGDATABASE)
|
|
12
|
+
result.database = env.PGDATABASE;
|
|
13
|
+
if (env.PGUSER)
|
|
14
|
+
result.user = env.PGUSER;
|
|
15
|
+
if (env.PGPASSWORD)
|
|
16
|
+
result.password = env.PGPASSWORD;
|
|
17
|
+
if (env.PGAPPNAME)
|
|
18
|
+
result.applicationName = env.PGAPPNAME;
|
|
19
|
+
if (env.PGTZ)
|
|
20
|
+
result.timezone = env.PGTZ;
|
|
21
|
+
if (env.PGSCHEMA)
|
|
22
|
+
result.schema = env.PGSCHEMA;
|
|
23
|
+
if (env.PGCONNECT_TIMEOUT)
|
|
24
|
+
result.connectTimeoutMs = (0, putil_varhelpers_1.toIntDef)(env.PGCONNECT_TIMEOUT, 30000);
|
|
25
|
+
if (env.PGMAX_BUFFER_SIZE) {
|
|
26
|
+
result.buffer = { maxLength: (0, putil_varhelpers_1.toInt)(env.PGMAX_BUFFER_SIZE) };
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
exports.configFromEnv = configFromEnv;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.parseConnectionString = exports.getConnectionConfig = void 0;
|
|
7
|
+
const putil_merge_1 = __importDefault(require("putil-merge"));
|
|
8
|
+
const config_from_env_js_1 = require("./config-from-env.js");
|
|
9
|
+
function getConnectionConfig(config) {
|
|
10
|
+
const cfg = (0, config_from_env_js_1.configFromEnv)();
|
|
11
|
+
if (typeof config === 'string') {
|
|
12
|
+
(0, putil_merge_1.default)(cfg, parseConnectionString(config));
|
|
13
|
+
}
|
|
14
|
+
else if (typeof config === 'object') {
|
|
15
|
+
(0, putil_merge_1.default)(cfg, config);
|
|
16
|
+
}
|
|
17
|
+
if (cfg.host) {
|
|
18
|
+
const x = parseConnectionString('' + cfg.host);
|
|
19
|
+
(0, putil_merge_1.default)(cfg, x);
|
|
20
|
+
}
|
|
21
|
+
cfg.user = cfg.user || 'postgres';
|
|
22
|
+
cfg.database = cfg.database || 'postgres';
|
|
23
|
+
cfg.host = cfg.host || '127.0.0.1';
|
|
24
|
+
return cfg;
|
|
25
|
+
}
|
|
26
|
+
exports.getConnectionConfig = getConnectionConfig;
|
|
27
|
+
function parseConnectionString(str) {
|
|
28
|
+
if (str.startsWith('/'))
|
|
29
|
+
str = 'socket:/' + str;
|
|
30
|
+
if (!str.includes('://'))
|
|
31
|
+
str = 'postgres://' + str;
|
|
32
|
+
const parsed = new URL(str);
|
|
33
|
+
const getFirst = (v) => {
|
|
34
|
+
return typeof v === 'string' ? v : Array.isArray(v) ? v[0] : '';
|
|
35
|
+
};
|
|
36
|
+
const cfg = {};
|
|
37
|
+
cfg.host = decodeURI(parsed.hostname || '');
|
|
38
|
+
if (parsed.port)
|
|
39
|
+
cfg.port = parseInt(parsed.port, 10);
|
|
40
|
+
if (parsed.protocol === 'socket:' || parsed.protocol === 'unix:') {
|
|
41
|
+
if (!cfg.host.startsWith('/'))
|
|
42
|
+
cfg.host = '/' + cfg.host;
|
|
43
|
+
cfg.host += decodeURI(parsed.pathname || '');
|
|
44
|
+
if (parsed.searchParams.get('db'))
|
|
45
|
+
cfg.database = decodeURI(getFirst(parsed.searchParams.get('db')));
|
|
46
|
+
}
|
|
47
|
+
else if (parsed.protocol === 'pg:' || parsed.protocol === 'postgres:') {
|
|
48
|
+
if (parsed.pathname)
|
|
49
|
+
cfg.database = decodeURI(parsed.pathname.substring(1));
|
|
50
|
+
}
|
|
51
|
+
if (parsed.searchParams.get('host'))
|
|
52
|
+
cfg.host = decodeURI(getFirst(parsed.searchParams.get('host')));
|
|
53
|
+
if (parsed.searchParams.get('db'))
|
|
54
|
+
cfg.database = decodeURI(getFirst(parsed.searchParams.get('db')));
|
|
55
|
+
if (parsed.searchParams.get('schema'))
|
|
56
|
+
cfg.schema = decodeURI(getFirst(parsed.searchParams.get('schema')));
|
|
57
|
+
if (parsed.searchParams.get('application_name'))
|
|
58
|
+
cfg.applicationName = decodeURI(getFirst(parsed.searchParams.get('application_name')));
|
|
59
|
+
if (parsed.username)
|
|
60
|
+
cfg.user = parsed.username;
|
|
61
|
+
if (parsed.password)
|
|
62
|
+
cfg.password = parsed.password;
|
|
63
|
+
return cfg;
|
|
64
|
+
}
|
|
65
|
+
exports.parseConnectionString = parseConnectionString;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertRowToObject = void 0;
|
|
4
|
+
function convertRowToObject(fields, row) {
|
|
5
|
+
const out = {};
|
|
6
|
+
const l = row.length;
|
|
7
|
+
let i;
|
|
8
|
+
for (i = 0; i < l; i++) {
|
|
9
|
+
out[fields[i].fieldName] = row[i];
|
|
10
|
+
}
|
|
11
|
+
return out;
|
|
12
|
+
}
|
|
13
|
+
exports.convertRowToObject = convertRowToObject;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decodeBinaryArray = void 0;
|
|
4
|
+
const buffer_reader_js_1 = require("../protocol/buffer-reader.js");
|
|
5
|
+
function decodeBinaryArray(buf, decoder, options = {}) {
|
|
6
|
+
if (!buf.length)
|
|
7
|
+
return null;
|
|
8
|
+
const io = new buffer_reader_js_1.BufferReader(buf);
|
|
9
|
+
const ndims = io.readInt32BE();
|
|
10
|
+
io.readInt32BE(); // hasNulls
|
|
11
|
+
const elementOID = io.readInt32BE(); // element oid
|
|
12
|
+
if (ndims === 0)
|
|
13
|
+
return [];
|
|
14
|
+
const dims = [];
|
|
15
|
+
const readDim = (level) => {
|
|
16
|
+
const elemCount = dims[level];
|
|
17
|
+
const target = new Array(elemCount);
|
|
18
|
+
for (let i = 0; i < elemCount; i++) {
|
|
19
|
+
if (level < dims.length - 1) {
|
|
20
|
+
target[i] = readDim(level + 1);
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const len = io.readInt32BE();
|
|
24
|
+
if (len === -1)
|
|
25
|
+
target[i] = null;
|
|
26
|
+
else {
|
|
27
|
+
const b = io.readBuffer(len);
|
|
28
|
+
target[i] = decoder(b, { ...options, elementOID });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return target;
|
|
32
|
+
};
|
|
33
|
+
for (let d = 0; d < ndims; d++) {
|
|
34
|
+
dims[d] = io.readInt32BE();
|
|
35
|
+
io.readInt32BE(); // LBound
|
|
36
|
+
}
|
|
37
|
+
return readDim(0);
|
|
38
|
+
}
|
|
39
|
+
exports.decodeBinaryArray = decodeBinaryArray;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encodeBinaryArray = void 0;
|
|
4
|
+
const constants_js_1 = require("../constants.js");
|
|
5
|
+
const array_calculatedim_js_1 = require("./array-calculatedim.js");
|
|
6
|
+
function encodeBinaryArray(io, value, itemOid, options, encode, encodeCalculateDimFn) {
|
|
7
|
+
encodeCalculateDimFn = encodeCalculateDimFn || array_calculatedim_js_1.arrayCalculateDim;
|
|
8
|
+
itemOid = itemOid || constants_js_1.DataTypeOIDs.varchar;
|
|
9
|
+
const dim = encodeCalculateDimFn(value);
|
|
10
|
+
const ndims = dim.length;
|
|
11
|
+
const zeroOffset = io.offset;
|
|
12
|
+
io.writeInt32BE(ndims) // Number of dimensions
|
|
13
|
+
.writeInt32BE(0) // reserved for has-null flag
|
|
14
|
+
.writeInt32BE(itemOid);
|
|
15
|
+
for (let d = 0; d < ndims; d++) {
|
|
16
|
+
io.writeInt32BE(dim[d]); // Number of items in dimension
|
|
17
|
+
io.writeInt32BE(0); // LBound always 0.
|
|
18
|
+
}
|
|
19
|
+
let hasNull = false;
|
|
20
|
+
let pos;
|
|
21
|
+
const writeDim = (arr, level) => {
|
|
22
|
+
const elemCount = dim[level];
|
|
23
|
+
for (let i = 0; i < elemCount; i++) {
|
|
24
|
+
if (level < dim.length - 1) {
|
|
25
|
+
writeDim(arr && arr[i], level + 1);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
// if value is null
|
|
29
|
+
if (!arr || arr[i] == null) {
|
|
30
|
+
hasNull = true;
|
|
31
|
+
io.writeInt32BE(-1);
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
io.writeInt32BE(0); // reserved for data len
|
|
35
|
+
pos = io.offset;
|
|
36
|
+
encode(io, arr[i], options);
|
|
37
|
+
// Update item data size
|
|
38
|
+
io.buffer.writeInt32BE(io.length - pos, pos - 4);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
writeDim(value, 0);
|
|
42
|
+
if (hasNull)
|
|
43
|
+
io.buffer.writeInt32BE(1, zeroOffset + 4);
|
|
44
|
+
}
|
|
45
|
+
exports.encodeBinaryArray = encodeBinaryArray;
|