oracledb 5.5.0 → 6.0.1

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 (107) hide show
  1. package/CHANGELOG.md +1 -1875
  2. package/LICENSE.txt +8 -222
  3. package/NOTICE.txt +1 -1
  4. package/README.md +38 -30
  5. package/SECURITY.md +38 -0
  6. package/build/Release/oracledb-6.0.1-darwin-x64.node +0 -0
  7. package/build/Release/oracledb-6.0.1-darwin-x64.node-buildinfo.txt +1 -0
  8. package/build/Release/oracledb-6.0.1-js-buildinfo.txt +1 -0
  9. package/build/Release/oracledb-6.0.1-linux-arm64.node +0 -0
  10. package/build/Release/oracledb-6.0.1-linux-arm64.node-buildinfo.txt +1 -0
  11. package/build/Release/oracledb-6.0.1-linux-x64.node +0 -0
  12. package/build/Release/oracledb-6.0.1-linux-x64.node-buildinfo.txt +1 -0
  13. package/build/Release/oracledb-6.0.1-win32-x64.node +0 -0
  14. package/build/Release/oracledb-6.0.1-win32-x64.node-buildinfo.txt +1 -0
  15. package/examples/README.md +113 -0
  16. package/examples/dbconfig.js +100 -0
  17. package/examples/example.js +156 -0
  18. package/index.js +26 -0
  19. package/lib/aqDeqOptions.js +148 -8
  20. package/lib/aqEnqOptions.js +59 -7
  21. package/lib/aqMessage.js +118 -7
  22. package/lib/aqQueue.js +243 -53
  23. package/lib/connection.js +1326 -465
  24. package/lib/constants.js +203 -0
  25. package/lib/dbObject.js +346 -32
  26. package/lib/errors.js +785 -0
  27. package/lib/impl/aqDeqOptions.js +197 -0
  28. package/lib/impl/aqEnqOptions.js +89 -0
  29. package/lib/impl/aqMessage.js +134 -0
  30. package/lib/impl/aqQueue.js +81 -0
  31. package/lib/impl/connection.js +529 -0
  32. package/lib/impl/dbObject.js +159 -0
  33. package/lib/impl/index.js +59 -0
  34. package/lib/impl/lob.js +85 -0
  35. package/lib/impl/pool.js +195 -0
  36. package/lib/impl/resultset.js +223 -0
  37. package/lib/impl/sodaCollection.js +174 -0
  38. package/lib/impl/sodaDatabase.js +81 -0
  39. package/lib/impl/sodaDocCursor.js +63 -0
  40. package/lib/impl/sodaDocument.js +98 -0
  41. package/lib/impl/sodaOperation.js +109 -0
  42. package/lib/lob.js +157 -68
  43. package/lib/oracledb.js +1047 -345
  44. package/lib/pool.js +654 -465
  45. package/lib/poolStatistics.js +24 -9
  46. package/lib/queryStream.js +16 -8
  47. package/lib/resultset.js +291 -183
  48. package/lib/settings.js +191 -0
  49. package/lib/sodaCollection.js +214 -201
  50. package/lib/sodaDatabase.js +108 -71
  51. package/lib/sodaDocCursor.js +44 -32
  52. package/lib/sodaDocument.js +76 -14
  53. package/lib/sodaOperation.js +131 -118
  54. package/lib/thin/connection.js +1043 -0
  55. package/lib/thin/dbObject.js +630 -0
  56. package/lib/thin/index.js +40 -0
  57. package/lib/thin/lob.js +189 -0
  58. package/lib/thin/pool.js +481 -0
  59. package/lib/thin/protocol/buffer.js +1043 -0
  60. package/lib/thin/protocol/capabilities.js +121 -0
  61. package/lib/thin/protocol/constants.js +765 -0
  62. package/lib/thin/protocol/encryptDecrypt.js +185 -0
  63. package/lib/thin/protocol/messages/auth.js +349 -0
  64. package/lib/thin/protocol/messages/base.js +476 -0
  65. package/lib/thin/protocol/messages/commit.js +56 -0
  66. package/lib/thin/protocol/messages/dataType.js +391 -0
  67. package/lib/thin/protocol/messages/execute.js +312 -0
  68. package/lib/thin/protocol/messages/fetch.js +60 -0
  69. package/lib/thin/protocol/messages/index.js +53 -0
  70. package/lib/thin/protocol/messages/lobOp.js +201 -0
  71. package/lib/thin/protocol/messages/logOff.js +53 -0
  72. package/lib/thin/protocol/messages/ping.js +56 -0
  73. package/lib/thin/protocol/messages/protocol.js +78 -0
  74. package/lib/thin/protocol/messages/rollback.js +56 -0
  75. package/lib/thin/protocol/messages/sessionRelease.js +60 -0
  76. package/lib/thin/protocol/messages/withData.js +812 -0
  77. package/lib/thin/protocol/oson.js +661 -0
  78. package/lib/thin/protocol/packet.js +519 -0
  79. package/lib/thin/protocol/protocol.js +202 -0
  80. package/lib/thin/protocol/utils.js +89 -0
  81. package/lib/thin/resultSet.js +122 -0
  82. package/lib/thin/sqlnet/connStrategy.js +175 -0
  83. package/lib/thin/sqlnet/constants.js +199 -0
  84. package/lib/thin/sqlnet/ezConnectResolver.js +488 -0
  85. package/lib/thin/sqlnet/navNodes.js +870 -0
  86. package/lib/thin/sqlnet/networkSession.js +621 -0
  87. package/lib/thin/sqlnet/ntTcp.js +527 -0
  88. package/lib/thin/sqlnet/nvStrToNvPair.js +783 -0
  89. package/lib/thin/sqlnet/packet.js +405 -0
  90. package/lib/thin/sqlnet/paramParser.js +206 -0
  91. package/lib/thin/sqlnet/sessionAtts.js +160 -0
  92. package/lib/thin/statement.js +300 -0
  93. package/lib/thin/util.js +289 -0
  94. package/lib/transformer.js +252 -0
  95. package/lib/types.js +357 -0
  96. package/lib/util.js +196 -110
  97. package/lib/version.js +36 -0
  98. package/package/README.md +33 -30
  99. package/package/install.js +89 -64
  100. package/package/prunebinaries.js +60 -12
  101. package/package.json +3 -3
  102. package/build/Release/oracledb-5.5.0-darwin-x64.node +0 -0
  103. package/build/Release/oracledb-5.5.0-darwin-x64.node-buildinfo.txt +0 -1
  104. package/build/Release/oracledb-5.5.0-linux-x64.node +0 -0
  105. package/build/Release/oracledb-5.5.0-linux-x64.node-buildinfo.txt +0 -1
  106. package/build/Release/oracledb-5.5.0-win32-x64.node +0 -0
  107. package/build/Release/oracledb-5.5.0-win32-x64.node-buildinfo.txt +0 -1
@@ -0,0 +1,1043 @@
1
+ // Copyright (c) 2022, 2023, Oracle and/or its affiliates.
2
+
3
+ //-----------------------------------------------------------------------------
4
+ //
5
+ // This software is dual-licensed to you under the Universal Permissive License
6
+ // (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
7
+ // 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
8
+ // either license.
9
+ //
10
+ // If you elect to accept the software under the Apache License, Version 2.0,
11
+ // the following applies:
12
+ //
13
+ // Licensed under the Apache License, Version 2.0 (the "License");
14
+ // you may not use this file except in compliance with the License.
15
+ // You may obtain a copy of the License at
16
+ //
17
+ // https://www.apache.org/licenses/LICENSE-2.0
18
+ //
19
+ // Unless required by applicable law or agreed to in writing, software
20
+ // distributed under the License is distributed on an "AS IS" BASIS,
21
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22
+ // See the License for the specific language governing permissions and
23
+ // limitations under the License.
24
+ //
25
+ //-----------------------------------------------------------------------------
26
+
27
+ 'use strict';
28
+
29
+ const { Buffer } = require('buffer');
30
+ const constants = require("./constants.js");
31
+ const errors = require("../../errors.js");
32
+ const settings = require("../../settings.js");
33
+ const types = require("../../types.js");
34
+
35
+ /**
36
+ * Base buffer class used for managing buffered data without unnecessary
37
+ * copying.
38
+ */
39
+ class BaseBuffer {
40
+
41
+ //---------------------------------------------------------------------------
42
+ // constructor()
43
+ //
44
+ // The initializer is either an integer specifying the size of the buffer, or
45
+ // an existing Buffer, which is used directly.
46
+ //---------------------------------------------------------------------------
47
+ constructor(initializer) {
48
+ if (typeof initializer === 'number') {
49
+ this.buf = Buffer.alloc(initializer);
50
+ this.size = 0;
51
+ this.maxSize = initializer;
52
+ } else {
53
+ this.buf = initializer;
54
+ this.size = this.maxSize = initializer.length;
55
+ }
56
+ this.pos = 0;
57
+ }
58
+
59
+ //---------------------------------------------------------------------------
60
+ // _grow()
61
+ //
62
+ // Called when the buffer needs to grow. The base function simply raises an
63
+ // error.
64
+ //---------------------------------------------------------------------------
65
+ _grow(numBytes) {
66
+ errors.throwErr(errors.ERR_BUFFER_LENGTH_INSUFFICIENT, this.numBytesLeft(),
67
+ numBytes);
68
+ }
69
+
70
+ //---------------------------------------------------------------------------
71
+ // _readBytesWithLength()
72
+ //
73
+ // Helper function that processes the number of bytes (if needed) and then
74
+ // acquires the specified number of bytes from the buffer. The base function
75
+ // simply uses the length as given.
76
+ //---------------------------------------------------------------------------
77
+ _readBytesWithLength(numBytes) {
78
+ return this.readBytes(numBytes);
79
+ }
80
+
81
+ //---------------------------------------------------------------------------
82
+ // _readInteger()
83
+ //
84
+ // Read an integer from the buffer of the specified maximum size and returns
85
+ // it. The signed flag indicates whether the value is allowed to be signed or
86
+ // not and the skip flag indicates whether the data should simply be skipped.
87
+ //---------------------------------------------------------------------------
88
+ _readInteger(maxSize, signed, skip) {
89
+ let isNegative = false;
90
+ let size = this.readUInt8();
91
+ if (size === 0) {
92
+ return 0;
93
+ } else if (size & 0x80) {
94
+ if (!signed) {
95
+ errors.throwErr(errors.ERR_UNEXPECTED_NEGATIVE_INTEGER);
96
+ }
97
+ isNegative = true;
98
+ size = size & 0x7f;
99
+ }
100
+ if (size > maxSize) {
101
+ errors.throwErr(errors.ERR_INTEGER_TOO_LARGE, size, maxSize);
102
+ }
103
+ if (skip) {
104
+ this.skipBytes(size);
105
+ } else {
106
+ const buf = this.readBytes(size);
107
+ const value = buf.readUIntBE(0, size);
108
+ return (isNegative) ? -value : value;
109
+ }
110
+ }
111
+
112
+ //---------------------------------------------------------------------------
113
+ // numBytesLeft()
114
+ //
115
+ // Returns the number of bytes that are remaining in the buffer.
116
+ //---------------------------------------------------------------------------
117
+ numBytesLeft() {
118
+ return this.size - this.pos;
119
+ }
120
+
121
+ //---------------------------------------------------------------------------
122
+ // parseBinaryDouble()
123
+ //
124
+ // Parses a binary double from the supplied buffer and returns a Number.
125
+ // It is assumed at this point that the size of the buffer is 8 bytes. A copy
126
+ // is made of the buffer in order to ensure that the original buffer is not
127
+ // modified. If it is and data spans multiple packets, incorrect data may be
128
+ // returned!
129
+ //---------------------------------------------------------------------------
130
+ parseBinaryDouble(buf) {
131
+ buf = Buffer.from(buf);
132
+ if (buf[0] & 0x80) {
133
+ buf[0] &= 0x7f;
134
+ } else {
135
+ // complement the bits for a negative number
136
+ buf[0] ^= 0xff;
137
+ buf[1] ^= 0xff;
138
+ buf[2] ^= 0xff;
139
+ buf[3] ^= 0xff;
140
+ buf[4] ^= 0xff;
141
+ buf[5] ^= 0xff;
142
+ buf[6] ^= 0xff;
143
+ buf[7] ^= 0xff;
144
+ }
145
+ return buf.readDoubleBE();
146
+ }
147
+
148
+ //---------------------------------------------------------------------------
149
+ // parseBinaryFloat()
150
+ //
151
+ // Parses a binary float from the supplied buffer and returns a Number. It
152
+ // is assumed at this point that the size of the buffer is 4 bytes. A copy is
153
+ // made of the buffer in order to ensure that the original buffer is not
154
+ // modified. If it is and data spans multiple packets, incorrect data may be
155
+ // returned!
156
+ //---------------------------------------------------------------------------
157
+ parseBinaryFloat(buf) {
158
+ buf = Buffer.from(buf);
159
+ if (buf[0] & 0x80) {
160
+ buf[0] &= 0x7f;
161
+ } else {
162
+ // complement the bits for a negative number
163
+ buf[0] ^= 0xff;
164
+ buf[1] ^= 0xff;
165
+ buf[2] ^= 0xff;
166
+ buf[3] ^= 0xff;
167
+ }
168
+ return buf.readFloatBE();
169
+ }
170
+
171
+ //---------------------------------------------------------------------------
172
+ // parseOracleDate()
173
+ //
174
+ // Parses an Oracle date from the supplied buffer and returns a Date. It is
175
+ // assumed at this point that the size of the buffer is either 7 bytes (date
176
+ // or compressed timestamp), 11 bytes (timestamp) or 13 bytes (timestamp with
177
+ // time zone). Time zone information is discarded because Node.js uses UTC
178
+ // timestamps and the server returns the data in that format, too. The Date
179
+ // type in Node.js doesn't support time zone information.
180
+ //---------------------------------------------------------------------------
181
+ parseOracleDate(buf, useLocalTime = true) {
182
+ let fseconds = 0;
183
+ if (buf.length >= 11) {
184
+ fseconds = Math.floor(buf.readUInt32BE(7) / (1000 * 1000));
185
+ }
186
+ const year = (buf[0] - 100) * 100 + buf[1] - 100;
187
+ return settings._makeDate(useLocalTime, year, buf[2], buf[3], buf[4] - 1,
188
+ buf[5] - 1, buf[6] - 1, fseconds, 0);
189
+ }
190
+
191
+ //---------------------------------------------------------------------------
192
+ // parseOracleNumber()
193
+ //
194
+ // Parses an Oracle number from the supplied buffer and returns a Number. It
195
+ // is assumed at this point that the buffer only contains the encoded numeric
196
+ // data.
197
+ //---------------------------------------------------------------------------
198
+ parseOracleNumber(buf) {
199
+
200
+ // the first byte is the exponent; positive numbers have the highest
201
+ // order bit set, whereas negative numbers have the highest order bit
202
+ // cleared and the bits inverted
203
+ let exponent = buf[0];
204
+ const isPositive = Boolean(exponent & 0x80);
205
+ if (!isPositive) {
206
+ exponent = (exponent ^ 0xFF);
207
+ }
208
+ exponent -= 193;
209
+ let decimalPointIndex = exponent * 2 + 2;
210
+
211
+ // a mantissa length of 0 implies a value of 0 (if positive) or a value
212
+ // of -1e126 (if negative)
213
+ if (buf.length === 1) {
214
+ if (isPositive) {
215
+ return "0";
216
+ }
217
+ return "-1e126";
218
+ }
219
+
220
+ // check for the trailing 102 byte for negative numbers and, if present,
221
+ // reduce the number of mantissa digits
222
+ let numBytes = buf.length;
223
+ if (!isPositive && buf[buf.length - 1] === 102) {
224
+ numBytes -= 1;
225
+ }
226
+
227
+ // process the mantissa bytes which are the remaining bytes; each
228
+ // mantissa byte is a base-100 digit
229
+ let base100Digit;
230
+ const digits = [];
231
+ for (let i = 1; i < numBytes; i++) {
232
+
233
+ // positive numbers have 1 added to them; negative numbers are
234
+ // subtracted from the value 101
235
+ if (isPositive) {
236
+ base100Digit = buf[i] - 1;
237
+ } else {
238
+ base100Digit = 101 - buf[i];
239
+ }
240
+
241
+ // process the first digit; leading zeroes are ignored
242
+ let digit = Math.floor(base100Digit / 10);
243
+ if (digit === 0 && i === 1) {
244
+ decimalPointIndex -= 1;
245
+ } else if (digit === 10) {
246
+ digits.push("1");
247
+ digits.push("0");
248
+ decimalPointIndex += 1;
249
+ } else if (digit !== 0 || i > 1) {
250
+ digits.push(digit.toString());
251
+ }
252
+
253
+ // process the second digit; trailing zeroes are ignored
254
+ digit = base100Digit % 10;
255
+ if (digit !== 0 || i < numBytes - 1) {
256
+ digits.push(digit.toString());
257
+ }
258
+ }
259
+
260
+ // create string of digits for transformation to JS value
261
+ const chars = [];
262
+
263
+ // if negative, include the sign
264
+ if (!isPositive) {
265
+ chars.push("-");
266
+ }
267
+
268
+ // if the decimal point index is 0 or less, add the decimal point and
269
+ // any leading zeroes that are needed
270
+ if (decimalPointIndex <= 0) {
271
+ chars.push(".");
272
+ if (decimalPointIndex < 0)
273
+ chars.push("0".repeat(-decimalPointIndex));
274
+ }
275
+
276
+ // add each of the digits
277
+ for (let i = 0; i < digits.length; i++) {
278
+ if (i > 0 && i === decimalPointIndex) {
279
+ chars.push(".");
280
+ }
281
+ chars.push(digits[i]);
282
+ }
283
+
284
+ // if the decimal point index exceeds the number of digits, add any
285
+ // trailing zeroes that are needed
286
+ if (decimalPointIndex > digits.length) {
287
+ for (let i = digits.length; i < decimalPointIndex; i++) {
288
+ chars.push("0");
289
+ }
290
+ }
291
+
292
+ // convert result to a Number
293
+ return chars.join("");
294
+ }
295
+
296
+ //---------------------------------------------------------------------------
297
+ // readBinaryDouble()
298
+ //
299
+ // Reads a binary double value from the buffer and returns a Number or a
300
+ // String, depending on the desired type.
301
+ //---------------------------------------------------------------------------
302
+ readBinaryDouble() {
303
+ const buf = this.readBytesWithLength();
304
+ if (!buf) {
305
+ return null;
306
+ }
307
+ return this.parseBinaryDouble(buf);
308
+ }
309
+
310
+ //---------------------------------------------------------------------------
311
+ // readBinaryFloat()
312
+ //
313
+ // Reads a binary float value from the buffer and returns a Number or a
314
+ // String, depending on the desired type.
315
+ //---------------------------------------------------------------------------
316
+ readBinaryFloat() {
317
+ const buf = this.readBytesWithLength();
318
+ if (!buf) {
319
+ return null;
320
+ }
321
+ return this.parseBinaryFloat(buf);
322
+ }
323
+
324
+ //---------------------------------------------------------------------------
325
+ // readBool()
326
+ //
327
+ // Reads a boolean value from the buffer and returns a Boolean.
328
+ //---------------------------------------------------------------------------
329
+ readBool() {
330
+ const buf = this.readBytesWithLength();
331
+ if (!buf) {
332
+ return null;
333
+ }
334
+ return (buf[buf.length - 1] === 1);
335
+ }
336
+
337
+ //---------------------------------------------------------------------------
338
+ // readBytes()
339
+ //
340
+ // Returns a Buffer containing the specified number of bytes. If an
341
+ // insufficient number of bytes are available an error is thrown.
342
+ //---------------------------------------------------------------------------
343
+ readBytes(numBytes) {
344
+ const numBytesLeft = this.numBytesLeft();
345
+ if (numBytes > numBytesLeft) {
346
+ errors.throwErr(errors.ERR_UNEXPECTED_END_OF_DATA, numBytes,
347
+ numBytesLeft);
348
+ }
349
+ const buf = this.buf.subarray(this.pos, this.pos + numBytes);
350
+ this.pos += numBytes;
351
+ return buf;
352
+ }
353
+
354
+ //---------------------------------------------------------------------------
355
+ // readBytesWithLength()
356
+ //
357
+ // Reads the length from the buffer and then returns a Buffer containing the
358
+ // specified number of bytes. If the length is 0 or the special null length
359
+ // indicator value, null is returned instead.
360
+ //---------------------------------------------------------------------------
361
+ readBytesWithLength() {
362
+ const numBytes = this.readUInt8();
363
+ if (numBytes === 0 || numBytes === constants.TNS_NULL_LENGTH_INDICATOR)
364
+ return null;
365
+ return this._readBytesWithLength(numBytes);
366
+ }
367
+
368
+ //---------------------------------------------------------------------------
369
+ // readDbObject()
370
+ //
371
+ // Reads a database object from the buffer and returns the implementation
372
+ // object (or null, if the object is atomically null).
373
+ //---------------------------------------------------------------------------
374
+ readDbObject() {
375
+ const obj = {};
376
+ let numBytes = this.readUB4();
377
+ if (numBytes > 0)
378
+ obj.toid = this.readBytesWithLength();
379
+ numBytes = this.readUB4();
380
+ if (numBytes > 0)
381
+ obj.oid = this.readBytesWithLength();
382
+ numBytes = this.readUB4();
383
+ if (numBytes > 0)
384
+ obj.snapshot = this.readBytesWithLength();
385
+ this.skipUB2(); // version
386
+ numBytes = this.readUB4();
387
+ this.skipUB2(); // flags
388
+ if (numBytes > 0)
389
+ obj.packedData = this.readBytesWithLength();
390
+ return obj;
391
+ }
392
+
393
+ //---------------------------------------------------------------------------
394
+ // readInt8()
395
+ //
396
+ // Reads a signed 8-bit integer from the buffer.
397
+ //---------------------------------------------------------------------------
398
+ readInt8() {
399
+ const buf = this.readBytes(1);
400
+ return buf.readInt8();
401
+ }
402
+
403
+ //---------------------------------------------------------------------------
404
+ // readOracleDate()
405
+ //
406
+ // Reads an Oracle date from the buffer and returns a Date or a String,
407
+ // depending on the desired type.
408
+ //---------------------------------------------------------------------------
409
+ readOracleDate(useLocalTime) {
410
+ const buf = this.readBytesWithLength();
411
+ if (!buf) {
412
+ return null;
413
+ }
414
+ return this.parseOracleDate(buf, useLocalTime);
415
+ }
416
+
417
+ //---------------------------------------------------------------------------
418
+ // readOracleNumber()
419
+ //
420
+ // Reads an Oracle number from the buffer and returns a Number or a String,
421
+ // depending on the desired type.
422
+ //---------------------------------------------------------------------------
423
+ readOracleNumber() {
424
+ const buf = this.readBytesWithLength();
425
+ if (!buf) {
426
+ return null;
427
+ }
428
+ return this.parseOracleNumber(buf);
429
+ }
430
+
431
+ //---------------------------------------------------------------------------
432
+ // readSB2()
433
+ //
434
+ // Reads a signed, variable length integer of up to 2 bytes in length.
435
+ //---------------------------------------------------------------------------
436
+ readSB2() {
437
+ return this._readInteger(2, true, false);
438
+ }
439
+
440
+ //---------------------------------------------------------------------------
441
+ // readSB4()
442
+ //
443
+ // Reads a signed, variable length integer of up to 4 bytes in length.
444
+ //---------------------------------------------------------------------------
445
+ readSB4() {
446
+ return this._readInteger(4, true, false);
447
+ }
448
+
449
+ //---------------------------------------------------------------------------
450
+ // readSB8()
451
+ //
452
+ // Reads a signed, variable length integer of up to 8 bytes in length.
453
+ //---------------------------------------------------------------------------
454
+ readSB8() {
455
+ return this._readInteger(8, true, false);
456
+ }
457
+
458
+ //---------------------------------------------------------------------------
459
+ // readStr()
460
+ //
461
+ // Reads a string from the buffer in the specified character set form.
462
+ //---------------------------------------------------------------------------
463
+ readStr(csfrm) {
464
+ const buf = this.readBytesWithLength();
465
+ if (!buf) {
466
+ return null;
467
+ }
468
+ if (csfrm === constants.TNS_CS_IMPLICIT)
469
+ return buf.toString();
470
+
471
+ // need a copy of the buffer since swap16() changes the buffer in place and
472
+ // it is possible that the buffer may need to be rescanned (for the case
473
+ // where insufficient packets are available during the initial scan)
474
+ return Buffer.from(buf).swap16().toString('utf16le');
475
+ }
476
+
477
+ //---------------------------------------------------------------------------
478
+ // readUB2()
479
+ //
480
+ // Reads an unsigned, variable length integer of up to 2 bytes in length.
481
+ //---------------------------------------------------------------------------
482
+ readUB2() {
483
+ return this._readInteger(2, false, false);
484
+ }
485
+
486
+ //---------------------------------------------------------------------------
487
+ // readUB4()
488
+ //
489
+ // Reads an unsigned, variable length integer of up to 4 bytes in length.
490
+ //---------------------------------------------------------------------------
491
+ readUB4() {
492
+ return this._readInteger(4, false, false);
493
+ }
494
+
495
+ //---------------------------------------------------------------------------
496
+ // readUB8()
497
+ //
498
+ // Reads an unsigned, variable length integer of up to 8 bytes in length.
499
+ //---------------------------------------------------------------------------
500
+ readUB8() {
501
+ return this._readInteger(8, false, false);
502
+ }
503
+
504
+ //---------------------------------------------------------------------------
505
+ // readUInt8()
506
+ //
507
+ // Reads an unsigned 8-bit integer from the buffer.
508
+ //---------------------------------------------------------------------------
509
+ readUInt8() {
510
+ const buf = this.readBytes(1);
511
+ return buf[0];
512
+ }
513
+
514
+ //---------------------------------------------------------------------------
515
+ // readUInt16BE()
516
+ //
517
+ // Reads an unsigned 16-bit integer from the buffer in big endian order.
518
+ //---------------------------------------------------------------------------
519
+ readUInt16BE() {
520
+ const buf = this.readBytes(2);
521
+ return buf.readUInt16BE();
522
+ }
523
+
524
+ //---------------------------------------------------------------------------
525
+ // readUInt16LE()
526
+ //
527
+ // Reads an unsigned 16-bit integer from the buffer in little endian order.
528
+ //---------------------------------------------------------------------------
529
+ readUInt16LE() {
530
+ const buf = this.readBytes(2);
531
+ return buf.readUInt16LE();
532
+ }
533
+
534
+ //---------------------------------------------------------------------------
535
+ // readUInt32BE()
536
+ //
537
+ // Reads an unsigned 32-bit integer from the buffer in big endian order.
538
+ //---------------------------------------------------------------------------
539
+ readUInt32BE() {
540
+ const buf = this.readBytes(4);
541
+ return buf.readUInt32BE();
542
+ }
543
+
544
+ //---------------------------------------------------------------------------
545
+ // reserveBytes()
546
+ //
547
+ // Reserves the specified number of bytes in the buffer. If not enough bytes
548
+ // remain in the buffer, the buffer is grown.
549
+ //---------------------------------------------------------------------------
550
+ reserveBytes(numBytes) {
551
+ if (numBytes > this.numBytesLeft())
552
+ this._grow(this.pos + numBytes);
553
+ const buf = this.buf.subarray(this.pos, this.pos + numBytes);
554
+ this.pos += numBytes;
555
+ return buf;
556
+ }
557
+
558
+ //---------------------------------------------------------------------------
559
+ // skipBytes()
560
+ //
561
+ // Skips the specified number of bytes in the buffer.
562
+ //---------------------------------------------------------------------------
563
+ skipBytes(numBytes) {
564
+ if (numBytes > this.numBytesLeft())
565
+ errors.throwErr(errors.ERR_UNEXPECTED_END_OF_DATA);
566
+ this.pos += numBytes;
567
+ }
568
+
569
+ //---------------------------------------------------------------------------
570
+ // skipSB4()
571
+ //
572
+ // Skips a signed, variable length integer of up to 4 bytes in length.
573
+ //---------------------------------------------------------------------------
574
+ skipSB4() {
575
+ return this._readInteger(4, true, true);
576
+ }
577
+
578
+ //---------------------------------------------------------------------------
579
+ // skipUB1()
580
+ //
581
+ // Skips a single byte integer in the buffer.
582
+ //---------------------------------------------------------------------------
583
+ skipUB1() {
584
+ this.skipBytes(1);
585
+ }
586
+
587
+ //---------------------------------------------------------------------------
588
+ // skipUB2()
589
+ //
590
+ // Skips an unsigned, variable length integer of up to 2 bytes in length.
591
+ //---------------------------------------------------------------------------
592
+ skipUB2() {
593
+ return this._readInteger(2, false, true);
594
+ }
595
+
596
+ //---------------------------------------------------------------------------
597
+ // skipUB4()
598
+ //
599
+ // Skips an unsigned, variable length integer of up to 4 bytes in length.
600
+ //---------------------------------------------------------------------------
601
+ skipUB4() {
602
+ return this._readInteger(4, false, true);
603
+ }
604
+
605
+ //---------------------------------------------------------------------------
606
+ // skipUB8()
607
+ //
608
+ // Skips an unsigned, variable length integer of up to 8 bytes in length.
609
+ //---------------------------------------------------------------------------
610
+ skipUB8() {
611
+ return this._readInteger(8, false, true);
612
+ }
613
+
614
+ //---------------------------------------------------------------------------
615
+ // writeBinaryDouble()
616
+ //
617
+ // Writes the number in binary double format to the buffer.
618
+ //---------------------------------------------------------------------------
619
+ writeBinaryDouble(n) {
620
+ this.writeUInt8(8);
621
+ const buf = this.reserveBytes(8);
622
+ buf.writeDoubleBE(n);
623
+ if ((buf[0] & 0x80) === 0) {
624
+ buf[0] |= 0x80;
625
+ } else {
626
+ // We complement the bits for a negative number
627
+ buf[0] ^= 0xff;
628
+ buf[1] ^= 0xff;
629
+ buf[2] ^= 0xff;
630
+ buf[3] ^= 0xff;
631
+ buf[4] ^= 0xff;
632
+ buf[5] ^= 0xff;
633
+ buf[6] ^= 0xff;
634
+ buf[7] ^= 0xff;
635
+ }
636
+ }
637
+
638
+ //---------------------------------------------------------------------------
639
+ // writeBinaryFloat()
640
+ //
641
+ // Writes the number in binary float format to the buffer.
642
+ //---------------------------------------------------------------------------
643
+ writeBinaryFloat(n) {
644
+ this.writeUInt8(4);
645
+ const buf = this.reserveBytes(4);
646
+ buf.writeFloatBE(n);
647
+ if ((buf[0] & 0x80) === 0) {
648
+ buf[0] |= 0x80;
649
+ } else {
650
+ // We complement the bits for a negative number
651
+ buf[0] ^= 0xff;
652
+ buf[1] ^= 0xff;
653
+ buf[2] ^= 0xff;
654
+ buf[3] ^= 0xff;
655
+ }
656
+ }
657
+
658
+ //---------------------------------------------------------------------------
659
+ // writeBytes()
660
+ //
661
+ // Writes the bytes in the supplied buffer to the buffer.
662
+ //---------------------------------------------------------------------------
663
+ writeBytes(value) {
664
+ let start = 0;
665
+ let valueLen = value.length;
666
+ while (valueLen > 0) {
667
+ const bytesLeft = this.numBytesLeft();
668
+ if (bytesLeft === 0) {
669
+ this._grow(this.pos + valueLen);
670
+ }
671
+ const bytesToWrite = Math.min(bytesLeft, valueLen);
672
+ value.copy(this.buf, this.pos, start, start + bytesToWrite);
673
+ this.pos += bytesToWrite;
674
+ start += bytesToWrite;
675
+ valueLen -= bytesToWrite;
676
+ }
677
+ }
678
+
679
+ //---------------------------------------------------------------------------
680
+ // writeBytesWithLength()
681
+ //
682
+ // Writes the bytes in the supplied buffer to the buffer, but first writes
683
+ // the length. If the length exceeds a fixed value, the value is written in
684
+ // chunks instead.
685
+ //---------------------------------------------------------------------------
686
+ writeBytesWithLength(value) {
687
+ let numBytes = value.length;
688
+ if (numBytes <= constants.TNS_MAX_SHORT_LENGTH) {
689
+ this.writeUInt8(numBytes);
690
+ if (numBytes > 0) {
691
+ this.writeBytes(value);
692
+ }
693
+ } else {
694
+ let start = 0;
695
+ this.writeUInt8(constants.TNS_LONG_LENGTH_INDICATOR);
696
+ while (numBytes > 0) {
697
+ const chunkLen = Math.min(numBytes, constants.BUFFER_CHUNK_SIZE);
698
+ this.writeUB4(chunkLen);
699
+ this.writeBytes(value.subarray(start, start + chunkLen));
700
+ numBytes -= chunkLen;
701
+ start += chunkLen;
702
+ }
703
+ this.writeUB4(0);
704
+ }
705
+ }
706
+
707
+ //---------------------------------------------------------------------------
708
+ // writeDbObject()
709
+ //
710
+ // Writes a database object to the buffer.
711
+ //---------------------------------------------------------------------------
712
+ writeDbObject(obj) {
713
+ this.writeUB4(obj.toid.length);
714
+ this.writeBytesWithLength(obj.toid);
715
+ if (obj.oid) {
716
+ this.writeUB4(obj.oid.length);
717
+ this.writeBytesWithLength(obj.oid);
718
+ } else {
719
+ this.writeUB4(0);
720
+ }
721
+ this.writeUB4(0); // snapshot
722
+ this.writeUB4(0); // version
723
+ const packedData = obj._getPackedData();
724
+ this.writeUB4(packedData.length);
725
+ this.writeUB4(obj.flags);
726
+ this.writeBytesWithLength(packedData);
727
+ }
728
+
729
+ //---------------------------------------------------------------------------
730
+ // writeOracleDate()
731
+ //
732
+ // Writes the date to the buffer using the given Oracle type. Note that if a
733
+ // timestamp with zero milliseconds is written, the type is automatically
734
+ // changed to DB_TYPE_DATE (except for DB_TYPE_TIMESTAMP_TZ which requires
735
+ // the full amount to be written).
736
+ //---------------------------------------------------------------------------
737
+ writeOracleDate(date, type, writeLength = true) {
738
+ let fsec;
739
+ let length = type._bufferSizeFactor;
740
+ if (length > 7) {
741
+ fsec = date.getUTCMilliseconds() * 1000 * 1000;
742
+ if (fsec === 0 && length <= 11)
743
+ length = 7;
744
+ }
745
+ if (writeLength) {
746
+ this.writeUInt8(length);
747
+ }
748
+ const ptr = this.reserveBytes(length);
749
+ if (type === types.DB_TYPE_DATE || type == types.DB_TYPE_TIMESTAMP) {
750
+ const year = date.getFullYear();
751
+ ptr[0] = Math.floor(year / 100) + 100;
752
+ ptr[1] = year % 100 + 100;
753
+ ptr[2] = date.getMonth() + 1;
754
+ ptr[3] = date.getDate();
755
+ ptr[4] = date.getHours() + 1;
756
+ ptr[5] = date.getMinutes() + 1;
757
+ ptr[6] = date.getSeconds() + 1;
758
+ } else {
759
+ const year = date.getUTCFullYear();
760
+ ptr[0] = Math.floor(year / 100) + 100;
761
+ ptr[1] = year % 100 + 100;
762
+ ptr[2] = date.getUTCMonth() + 1;
763
+ ptr[3] = date.getUTCDate();
764
+ ptr[4] = date.getUTCHours() + 1;
765
+ ptr[5] = date.getUTCMinutes() + 1;
766
+ ptr[6] = date.getUTCSeconds() + 1;
767
+ }
768
+ if (length > 7) {
769
+ ptr.writeInt32BE(fsec, 7);
770
+ if (length > 11) {
771
+ ptr[11] = constants.TZ_HOUR_OFFSET;
772
+ ptr[12] = constants.TZ_MINUTE_OFFSET;
773
+ }
774
+ }
775
+ }
776
+
777
+ //---------------------------------------------------------------------------
778
+ // writeOracleNumber()
779
+ //
780
+ // Writes the number (in string form) in Oracle Number format to the buffer.
781
+ //---------------------------------------------------------------------------
782
+ writeOracleNumber(value) {
783
+
784
+ // determine if number is negative
785
+ let isNegative = false;
786
+ if (value[0] === '-') {
787
+ isNegative = true;
788
+ value = value.substring(1);
789
+ }
790
+
791
+ // parse the exponent, if one is present
792
+ let exponent = 0;
793
+ const exponentPos = value.indexOf('e');
794
+ if (exponentPos > 0) {
795
+ exponent = Number(value.substring(exponentPos + 1));
796
+ value = value.substring(0, exponentPos);
797
+ }
798
+
799
+ // adjust the exponent and the value if there is a decimal point
800
+ const decimalPos = value.indexOf('.');
801
+ if (decimalPos > 0) {
802
+ exponent -= (value.length - decimalPos - 1);
803
+ value = value.substring(0, decimalPos) + value.substring(decimalPos + 1);
804
+ }
805
+
806
+ // strip any leading zeroes
807
+ if (value[0] === '0') {
808
+ value = value.replace(/^0+/, "");
809
+ }
810
+
811
+ // strip any trailing zeroes
812
+ if (value.length > 0 && value[value.length - 1] === '0') {
813
+ const trimmedValue = value.replace(/0+$/, "");
814
+ exponent += (value.length - trimmedValue.length);
815
+ value = trimmedValue;
816
+ }
817
+
818
+ // throw exception if number cannot be represented as an Oracle Number
819
+ if (value.length > constants.NUMBER_MAX_DIGITS || exponent > 126 ||
820
+ exponent < -129) {
821
+ errors.throwErr(errors.ERR_ORACLE_NUMBER_NO_REPR);
822
+ }
823
+
824
+ // if the exponent is odd, append a zero
825
+ if ((exponent > 0 && exponent % 2 === 1) ||
826
+ (exponent < 0 && exponent % 2 === -1)) {
827
+ exponent--;
828
+ value += "0";
829
+ }
830
+
831
+ // add a leading zero if the number of digits is odd
832
+ if (value.length % 2 === 1) {
833
+ value = "0" + value;
834
+ }
835
+
836
+ // write the encoded data to the wire
837
+ const appendSentinel =
838
+ (isNegative && value.length < constants.NUMBER_MAX_DIGITS);
839
+ const numPairs = value.length / 2;
840
+ let exponentOnWire = ((exponent + value.length) / 2) + 192;
841
+ if (isNegative) {
842
+ exponentOnWire = (exponentOnWire ^ 0xFF);
843
+ } else if (value.length === 0 && exponent === 0) {
844
+ exponentOnWire = 128;
845
+ }
846
+ const buf = this.reserveBytes(numPairs + 2 + appendSentinel);
847
+ buf[0] = numPairs + 1 + appendSentinel;
848
+ buf[1] = exponentOnWire;
849
+ for (let i = 0, pos = 2; i < value.length; i += 2, pos++) {
850
+ let base100Digit = Number(value.substring(i, i + 2));
851
+ if (isNegative) {
852
+ buf[pos] = 101 - base100Digit;
853
+ } else {
854
+ buf[pos] = base100Digit + 1;
855
+ }
856
+ }
857
+ if (appendSentinel) {
858
+ buf[buf.length - 1] = 102;
859
+ }
860
+
861
+ }
862
+
863
+ //---------------------------------------------------------------------------
864
+ // writeQLocator()
865
+ //
866
+ // Writes a QLocator. QLocators are always 40 bytes in length.
867
+ //---------------------------------------------------------------------------
868
+ writeQLocator(numBytes) {
869
+ this.writeUB4(40); // QLocator length
870
+ this.writeUInt8(40); // repeated length
871
+ this.writeUInt16BE(38); // internal length
872
+ this.writeUInt16BE(constants.TNS_LOB_QLOCATOR_VERSION);
873
+ this.writeUInt8(constants.TNS_LOB_LOC_FLAGS_VALUE_BASED |
874
+ constants.TNS_LOB_LOC_FLAGS_BLOB | constants.TNS_LOB_LOC_FLAGS_ABSTRACT);
875
+ this.writeUInt8(constants.TNS_LOB_LOC_FLAGS_INIT);
876
+ this.writeUInt16BE(0); // additional flags
877
+ this.writeUInt16BE(1); // byt1
878
+ this.writeUInt64BE(numBytes);
879
+ this.writeUInt16BE(0); // unused
880
+ this.writeUInt16BE(0); // csid
881
+ this.writeUInt16BE(0); // unused
882
+ this.writeUInt64BE(0); // unused
883
+ this.writeUInt64BE(0); // unused
884
+ }
885
+
886
+ //---------------------------------------------------------------------------
887
+ // writeStr()
888
+ //
889
+ // Writes the string to the buffer.
890
+ //---------------------------------------------------------------------------
891
+ writeStr(s) {
892
+ this.writeBytes(Buffer.from(s));
893
+ }
894
+
895
+ //---------------------------------------------------------------------------
896
+ // writeInt32BE()
897
+ //
898
+ // Writes a signed 32-bit integer to the buffer in big endian order.
899
+ //---------------------------------------------------------------------------
900
+ writeInt32BE(n) {
901
+ const buf = this.reserveBytes(4);
902
+ buf.writeInt32BE(n);
903
+ }
904
+
905
+ //---------------------------------------------------------------------------
906
+ // writeUB4()
907
+ //
908
+ // Writes an unsigned integer (up to 4 bytes in length) in variable length
909
+ // format to the buffer.
910
+ //---------------------------------------------------------------------------
911
+ writeUB4(value) {
912
+ if (value === 0) {
913
+ this.writeUInt8(0);
914
+ } else if (value <= 0xff) {
915
+ this.writeUInt8(1);
916
+ this.writeUInt8(value);
917
+ } else if (value <= 0xffff) {
918
+ this.writeUInt8(2);
919
+ this.writeUInt16BE(value);
920
+ } else {
921
+ this.writeUInt8(4);
922
+ this.writeUInt32BE(value);
923
+ }
924
+ }
925
+
926
+ //---------------------------------------------------------------------------
927
+ // writeUB8()
928
+ //
929
+ // Writes an unsigned integer (up to 8 bytes in length) in variable length
930
+ // format to the buffer.
931
+ //---------------------------------------------------------------------------
932
+ writeUB8(value) {
933
+ if (value === 0) {
934
+ this.writeUInt8(0);
935
+ } else if (value <= 0xff) {
936
+ this.writeUInt8(1);
937
+ this.writeUInt8(value);
938
+ } else if (value <= 0xffff) {
939
+ this.writeUInt8(2);
940
+ this.writeUInt16BE(value);
941
+ } else if (value <= 0xffffffff) {
942
+ this.writeUInt8(4);
943
+ this.writeUInt32BE(value);
944
+ } else {
945
+ this.writeUInt8(8);
946
+ this.writeUInt64BE(value);
947
+ }
948
+ }
949
+
950
+ //---------------------------------------------------------------------------
951
+ // writeUInt8()
952
+ //
953
+ // Writes an unsigned 8-bit integer to the buffer.
954
+ //---------------------------------------------------------------------------
955
+ writeUInt8(n) {
956
+ const buf = this.reserveBytes(1);
957
+ buf[0] = n;
958
+ }
959
+
960
+ //---------------------------------------------------------------------------
961
+ // writeUInt16BE()
962
+ //
963
+ // Writes an unsigned 16-bit integer to the buffer in big endian order.
964
+ //---------------------------------------------------------------------------
965
+ writeUInt16BE(n) {
966
+ const buf = this.reserveBytes(2);
967
+ buf.writeUInt16BE(n);
968
+ }
969
+
970
+ //---------------------------------------------------------------------------
971
+ // writeUInt32BE()
972
+ //
973
+ // Writes an unsigned 32-bit integer to the buffer in big endian order.
974
+ //---------------------------------------------------------------------------
975
+ writeUInt32BE(n) {
976
+ const buf = this.reserveBytes(4);
977
+ buf.writeUInt32BE(n);
978
+ }
979
+
980
+ //---------------------------------------------------------------------------
981
+ // writeUInt64BE()
982
+ //
983
+ // Writes an unsigned 64-bit integer to the buffer in big endian order. Since
984
+ // Node.js doesn't support anything above 32-bits without using BigInt, the
985
+ // higher order bits are simply written as 0.
986
+ //---------------------------------------------------------------------------
987
+ writeUInt64BE(n) {
988
+ const buf = this.reserveBytes(8);
989
+ buf.writeUInt32BE(0);
990
+ buf.writeUInt32BE(n, 4);
991
+ }
992
+
993
+ //---------------------------------------------------------------------------
994
+ // writeUInt16LE()
995
+ //
996
+ // Writes an unsigned 16-bit integer to the buffer in little endian order.
997
+ //---------------------------------------------------------------------------
998
+ writeUInt16LE(n) {
999
+ const buf = this.reserveBytes(2);
1000
+ buf.writeUInt16LE(n);
1001
+ }
1002
+
1003
+ }
1004
+
1005
+ class GrowableBuffer extends BaseBuffer {
1006
+
1007
+ //---------------------------------------------------------------------------
1008
+ // constructor()
1009
+ //
1010
+ // Initializes the buffer with an initial fixed chunk size.
1011
+ //---------------------------------------------------------------------------
1012
+ constructor(initializer) {
1013
+ if (initializer) {
1014
+ super(initializer);
1015
+ } else {
1016
+ super(constants.BUFFER_CHUNK_SIZE);
1017
+ this.size = this.maxSize;
1018
+ }
1019
+ }
1020
+
1021
+ //---------------------------------------------------------------------------
1022
+ // _grow()
1023
+ //
1024
+ // Called when the buffer needs to grow. Ensures that sufficient space is
1025
+ // allocated to include the requested number of bytes, rounded to the nearest
1026
+ // chunk size.
1027
+ //---------------------------------------------------------------------------
1028
+ _grow(numBytes) {
1029
+ const remainder = numBytes % constants.BUFFER_CHUNK_SIZE;
1030
+ if (remainder > 0) {
1031
+ numBytes += (constants.BUFFER_CHUNK_SIZE - remainder);
1032
+ }
1033
+ const buf = Buffer.alloc(numBytes);
1034
+ this.buf.copy(buf);
1035
+ this.buf = buf;
1036
+ this.maxSize = this.size = numBytes;
1037
+ }
1038
+ }
1039
+
1040
+ module.exports = {
1041
+ BaseBuffer,
1042
+ GrowableBuffer
1043
+ };