@ragestudio/scylla-odm 0.22.2 → 0.22.3

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 (153) hide show
  1. package/batch/index.d.ts +3 -3
  2. package/batch/index.d.ts.map +1 -1
  3. package/client.d.ts +6 -5
  4. package/client.d.ts.map +1 -1
  5. package/client.js +7 -7
  6. package/client.js.map +1 -1
  7. package/cql_gen/create_table.d.ts +1 -1
  8. package/cql_gen/create_table.d.ts.map +1 -1
  9. package/document/index.d.ts +3 -3
  10. package/document/index.d.ts.map +1 -1
  11. package/driver/LICENSE.txt +177 -0
  12. package/driver/NOTICE.txt +67 -0
  13. package/driver/auth/index.d.ts +37 -0
  14. package/driver/auth/index.js +37 -0
  15. package/driver/auth/no-auth-provider.js +73 -0
  16. package/driver/auth/plain-text-auth-provider.js +81 -0
  17. package/driver/auth/provider.js +77 -0
  18. package/driver/client-options.js +442 -0
  19. package/driver/client.js +1267 -0
  20. package/driver/concurrent/index.d.ts +49 -0
  21. package/driver/concurrent/index.js +366 -0
  22. package/driver/connection.js +1034 -0
  23. package/driver/control-connection.js +1282 -0
  24. package/driver/encoder.js +2316 -0
  25. package/driver/errors.js +223 -0
  26. package/driver/execution-options.js +612 -0
  27. package/driver/execution-profile.js +274 -0
  28. package/driver/host-connection-pool.js +587 -0
  29. package/driver/host.js +699 -0
  30. package/driver/index.d.ts +387 -0
  31. package/driver/index.js +81 -0
  32. package/driver/mapping/cache.js +214 -0
  33. package/driver/mapping/doc-info-adapter.js +171 -0
  34. package/driver/mapping/index.d.ts +219 -0
  35. package/driver/mapping/index.js +57 -0
  36. package/driver/mapping/mapper.js +225 -0
  37. package/driver/mapping/mapping-handler.js +641 -0
  38. package/driver/mapping/model-batch-item.js +215 -0
  39. package/driver/mapping/model-batch-mapper.js +141 -0
  40. package/driver/mapping/model-mapper.js +315 -0
  41. package/driver/mapping/model-mapping-info.js +225 -0
  42. package/driver/mapping/object-selector.js +417 -0
  43. package/driver/mapping/q.js +156 -0
  44. package/driver/mapping/query-generator.js +556 -0
  45. package/driver/mapping/result-mapper.js +123 -0
  46. package/driver/mapping/result.js +139 -0
  47. package/driver/mapping/table-mappings.js +133 -0
  48. package/driver/mapping/tree.js +160 -0
  49. package/driver/metadata/aggregate.js +79 -0
  50. package/driver/metadata/client-state.js +119 -0
  51. package/driver/metadata/data-collection.js +182 -0
  52. package/driver/metadata/event-debouncer.js +174 -0
  53. package/driver/metadata/index.d.ts +276 -0
  54. package/driver/metadata/index.js +1156 -0
  55. package/driver/metadata/materialized-view.js +49 -0
  56. package/driver/metadata/schema-function.js +98 -0
  57. package/driver/metadata/schema-index.js +166 -0
  58. package/driver/metadata/schema-parser.js +1399 -0
  59. package/driver/metadata/table-metadata.js +77 -0
  60. package/driver/operation-state.js +206 -0
  61. package/driver/policies/address-resolution.js +145 -0
  62. package/driver/policies/index.d.ts +241 -0
  63. package/driver/policies/index.js +110 -0
  64. package/driver/policies/load-balancing.js +970 -0
  65. package/driver/policies/reconnection.js +166 -0
  66. package/driver/policies/retry.js +326 -0
  67. package/driver/policies/speculative-execution.js +150 -0
  68. package/driver/policies/timestamp-generation.js +176 -0
  69. package/driver/prepare-handler.js +347 -0
  70. package/driver/promise-utils.js +191 -0
  71. package/driver/readers.js +624 -0
  72. package/driver/request-execution.js +644 -0
  73. package/driver/request-handler.js +332 -0
  74. package/driver/requests.js +618 -0
  75. package/driver/stream-id-stack.js +209 -0
  76. package/driver/streams.js +745 -0
  77. package/driver/token.js +325 -0
  78. package/driver/tokenizer.js +631 -0
  79. package/driver/types/big-decimal.js +282 -0
  80. package/driver/types/duration.js +576 -0
  81. package/driver/types/index.d.ts +486 -0
  82. package/driver/types/index.js +733 -0
  83. package/driver/types/inet-address.js +262 -0
  84. package/driver/types/integer.js +818 -0
  85. package/driver/types/local-date.js +280 -0
  86. package/driver/types/local-time.js +299 -0
  87. package/driver/types/mutable-long.js +385 -0
  88. package/driver/types/protocol-version.js +391 -0
  89. package/driver/types/result-set.js +287 -0
  90. package/driver/types/result-stream.js +164 -0
  91. package/driver/types/row.js +85 -0
  92. package/driver/types/time-uuid.js +414 -0
  93. package/driver/types/tuple.js +103 -0
  94. package/driver/types/uuid.js +160 -0
  95. package/driver/types/vector.js +130 -0
  96. package/driver/types/version-number.js +153 -0
  97. package/driver/utils.js +1485 -0
  98. package/driver/writers.js +350 -0
  99. package/global.d.ts +1 -1
  100. package/global.d.ts.map +1 -1
  101. package/index.d.ts +6 -6
  102. package/index.d.ts.map +1 -1
  103. package/index.js +6 -6
  104. package/index.js.map +1 -1
  105. package/migrate/index.d.ts +1 -1
  106. package/migrate/index.d.ts.map +1 -1
  107. package/migrate/index.js +1 -1
  108. package/migrate/index.js.map +1 -1
  109. package/model/index.d.ts +6 -6
  110. package/model/index.d.ts.map +1 -1
  111. package/model/index.js +10 -10
  112. package/model/index.js.map +1 -1
  113. package/operations/countAll.d.ts +1 -1
  114. package/operations/countAll.d.ts.map +1 -1
  115. package/operations/delete.d.ts +3 -4
  116. package/operations/delete.d.ts.map +1 -1
  117. package/operations/delete.js +1 -1
  118. package/operations/delete.js.map +1 -1
  119. package/operations/find.d.ts +2 -2
  120. package/operations/find.d.ts.map +1 -1
  121. package/operations/find.js +1 -1
  122. package/operations/find.js.map +1 -1
  123. package/operations/findOne.d.ts +2 -2
  124. package/operations/findOne.d.ts.map +1 -1
  125. package/operations/findOne.js +1 -1
  126. package/operations/findOne.js.map +1 -1
  127. package/operations/insert.d.ts +3 -3
  128. package/operations/insert.d.ts.map +1 -1
  129. package/operations/insert.js +2 -2
  130. package/operations/insert.js.map +1 -1
  131. package/operations/sync.d.ts +1 -1
  132. package/operations/sync.d.ts.map +1 -1
  133. package/operations/sync.js +1 -1
  134. package/operations/sync.js.map +1 -1
  135. package/operations/tableExists.d.ts +1 -1
  136. package/operations/tableExists.d.ts.map +1 -1
  137. package/operations/update.d.ts +3 -3
  138. package/operations/update.d.ts.map +1 -1
  139. package/operations/update.js +2 -2
  140. package/operations/update.js.map +1 -1
  141. package/package.json +4 -12
  142. package/schema/index.d.ts +1 -1
  143. package/schema/index.d.ts.map +1 -1
  144. package/types.d.ts +4 -4
  145. package/types.d.ts.map +1 -1
  146. package/utils/queryParser.d.ts +1 -1
  147. package/utils/queryParser.d.ts.map +1 -1
  148. package/utils/queryParser.js +1 -1
  149. package/utils/queryParser.js.map +1 -1
  150. package/utils/typeChecker.d.ts +1 -1
  151. package/utils/typeChecker.d.ts.map +1 -1
  152. package/utils/typeChecker.js +1 -1
  153. package/utils/typeChecker.js.map +1 -1
@@ -0,0 +1,1485 @@
1
+ /*
2
+ * Licensed to the Apache Software Foundation (ASF) under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. The ASF licenses this file
6
+ * to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ import Long from "long"
20
+ import util from "util"
21
+ import net from "net"
22
+ import { EventEmitter } from "events"
23
+
24
+ import errors from "./errors.js"
25
+ import promiseUtils from "./promise-utils.js"
26
+
27
+ /**
28
+ * Max int that can be accurately represented with 64-bit Number (2^53)
29
+ * @type {number}
30
+ * @const
31
+ */
32
+ const maxInt = 9007199254740992
33
+
34
+ const maxInt32 = 0x7fffffff
35
+
36
+ const emptyObject = Object.freeze({})
37
+
38
+ const emptyArray = Object.freeze([])
39
+
40
+ function noop() {}
41
+
42
+ /**
43
+ * Forward-compatible allocation of buffer, filled with zeros.
44
+ * @type {Function}
45
+ */
46
+ const allocBuffer = Buffer.alloc || allocBufferFillDeprecated
47
+
48
+ /**
49
+ * Forward-compatible unsafe allocation of buffer.
50
+ * @type {Function}
51
+ */
52
+ const allocBufferUnsafe = Buffer.allocUnsafe || allocBufferDeprecated
53
+
54
+ /**
55
+ * Forward-compatible allocation of buffer to contain a string.
56
+ * @type {Function}
57
+ */
58
+ const allocBufferFromString =
59
+ (Int8Array.from !== Buffer.from && Buffer.from) ||
60
+ allocBufferFromStringDeprecated
61
+
62
+ /**
63
+ * Forward-compatible allocation of buffer from an array of bytes
64
+ * @type {Function}
65
+ */
66
+ const allocBufferFromArray =
67
+ (Int8Array.from !== Buffer.from && Buffer.from) ||
68
+ allocBufferFromArrayDeprecated
69
+
70
+ function allocBufferDeprecated(size) {
71
+ // eslint-disable-next-line
72
+ return new Buffer(size)
73
+ }
74
+
75
+ function allocBufferFillDeprecated(size) {
76
+ const b = allocBufferDeprecated(size)
77
+ b.fill(0)
78
+ return b
79
+ }
80
+
81
+ function allocBufferFromStringDeprecated(text, encoding) {
82
+ if (typeof text !== "string") {
83
+ throw new TypeError("Expected string, obtained " + util.inspect(text))
84
+ }
85
+ // eslint-disable-next-line
86
+ return new Buffer(text, encoding)
87
+ }
88
+
89
+ function allocBufferFromArrayDeprecated(arr) {
90
+ if (!Array.isArray(arr)) {
91
+ throw new TypeError("Expected Array, obtained " + util.inspect(arr))
92
+ }
93
+ // eslint-disable-next-line
94
+ return new Buffer(arr)
95
+ }
96
+
97
+ /**
98
+ * @returns {Function} Returns a wrapper function that invokes the underlying callback only once.
99
+ * @param {Function} callback
100
+ */
101
+ function callbackOnce(callback) {
102
+ let cb = callback
103
+
104
+ return function wrapperCallback(err, result) {
105
+ cb(err, result)
106
+ cb = noop
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Creates a copy of a buffer
112
+ */
113
+ function copyBuffer(buf) {
114
+ const targetBuffer = allocBufferUnsafe(buf.length)
115
+ buf.copy(targetBuffer)
116
+ return targetBuffer
117
+ }
118
+
119
+ /**
120
+ * Appends the original stack trace to the error after a tick of the event loop
121
+ */
122
+ function fixStack(stackTrace, error) {
123
+ if (stackTrace) {
124
+ error.stack +=
125
+ "\n (event loop)\n" +
126
+ stackTrace.substr(stackTrace.indexOf("\n") + 1)
127
+ }
128
+ return error
129
+ }
130
+
131
+ /**
132
+ * Uses the logEmitter to emit log events
133
+ * @param {String} type
134
+ * @param {String} info
135
+ * @param [furtherInfo]
136
+ */
137
+ function log(type, info, furtherInfo, options) {
138
+ if (!this.logEmitter) {
139
+ const effectiveOptions = options || this.options
140
+ if (!effectiveOptions || !effectiveOptions.logEmitter) {
141
+ throw new Error("Log emitter not defined")
142
+ }
143
+ this.logEmitter = effectiveOptions.logEmitter
144
+ }
145
+ this.logEmitter("log", type, this.constructor.name, info, furtherInfo || "")
146
+ }
147
+
148
+ /**
149
+ * Gets the sum of the length of the items of an array
150
+ */
151
+ function totalLength(arr) {
152
+ if (arr.length === 1) {
153
+ return arr[0].length
154
+ }
155
+ let total = 0
156
+ arr.forEach(function (item) {
157
+ let length = item.length
158
+ length = length ? length : 0
159
+ total += length
160
+ })
161
+ return total
162
+ }
163
+
164
+ /**
165
+ * Merge the contents of two or more objects together into the first object. Similar to jQuery.extend / Object.assign.
166
+ * The main difference between this method is that declared properties with an <code>undefined</code> value are not set
167
+ * to the target.
168
+ */
169
+ function extend(target) {
170
+ const sources = Array.prototype.slice.call(arguments, 1)
171
+ sources.forEach(function (source) {
172
+ if (!source) {
173
+ return
174
+ }
175
+ const keys = Object.keys(source)
176
+ for (let i = 0; i < keys.length; i++) {
177
+ const key = keys[i]
178
+ const value = source[key]
179
+ if (value === undefined) {
180
+ continue
181
+ }
182
+ target[key] = value
183
+ }
184
+ })
185
+ return target
186
+ }
187
+
188
+ /**
189
+ * Returns a new object with the property names set to lowercase.
190
+ */
191
+ function toLowerCaseProperties(obj) {
192
+ const keys = Object.keys(obj)
193
+ const result = {}
194
+ for (let i = 0; i < keys.length; i++) {
195
+ const k = keys[i]
196
+ result[k.toLowerCase()] = obj[k]
197
+ }
198
+ return result
199
+ }
200
+
201
+ /**
202
+ * Extends the target by the most inner props of sources
203
+ * @param {Object} target
204
+ * @returns {Object}
205
+ */
206
+ function deepExtend(target) {
207
+ const sources = Array.prototype.slice.call(arguments, 1)
208
+ sources.forEach(function (source) {
209
+ for (const prop in source) {
210
+ // eslint-disable-next-line no-prototype-builtins
211
+ if (!source.hasOwnProperty(prop)) {
212
+ continue
213
+ }
214
+ const targetProp = target[prop]
215
+ const targetType = typeof targetProp
216
+ //target prop is
217
+ // a native single type
218
+ // or not existent
219
+ // or is not an anonymous object (not class instance)
220
+ if (
221
+ !targetProp ||
222
+ targetType === "number" ||
223
+ targetType === "string" ||
224
+ Array.isArray(targetProp) ||
225
+ targetProp instanceof Date ||
226
+ targetProp.constructor.name !== "Object"
227
+ ) {
228
+ target[prop] = source[prop]
229
+ } else {
230
+ //inner extend
231
+ target[prop] = deepExtend({}, targetProp, source[prop])
232
+ }
233
+ }
234
+ })
235
+ return target
236
+ }
237
+
238
+ function propCompare(propName) {
239
+ return function (a, b) {
240
+ if (a[propName] > b[propName]) {
241
+ return 1
242
+ }
243
+ if (a[propName] < b[propName]) {
244
+ return -1
245
+ }
246
+ return 0
247
+ }
248
+ }
249
+
250
+ function funcCompare(name, argArray) {
251
+ return function (a, b) {
252
+ if (typeof a[name] === "undefined") {
253
+ return 0
254
+ }
255
+ const valA = a[name].apply(a, argArray)
256
+ const valB = b[name].apply(b, argArray)
257
+ if (valA > valB) {
258
+ return 1
259
+ }
260
+ if (valA < valB) {
261
+ return -1
262
+ }
263
+ return 0
264
+ }
265
+ }
266
+ /**
267
+ * Uses the iterator protocol to go through the items of the Array
268
+ * @param {Array} arr
269
+ * @returns {Iterator}
270
+ */
271
+ function arrayIterator(arr) {
272
+ return arr[Symbol.iterator]()
273
+ }
274
+
275
+ /**
276
+ * Convert the iterator values into an array
277
+ * @param iterator
278
+ * @returns {Array}
279
+ */
280
+ function iteratorToArray(iterator) {
281
+ const values = []
282
+ let item = iterator.next()
283
+ while (!item.done) {
284
+ values.push(item.value)
285
+ item = iterator.next()
286
+ }
287
+ return values
288
+ }
289
+
290
+ /**
291
+ * Searches the specified Array for the provided key using the binary
292
+ * search algorithm. The Array must be sorted.
293
+ * @param {Array} arr
294
+ * @param key
295
+ * @param {function} compareFunc
296
+ * @returns {number} The position of the key in the Array, if it is found.
297
+ * If it is not found, it returns a negative number which is the bitwise complement of the index of the first element that is larger than key.
298
+ */
299
+ function binarySearch(arr, key, compareFunc) {
300
+ let low = 0
301
+ let high = arr.length - 1
302
+
303
+ while (low <= high) {
304
+ const mid = (low + high) >>> 1
305
+ const midVal = arr[mid]
306
+ const cmp = compareFunc(midVal, key)
307
+ if (cmp < 0) {
308
+ low = mid + 1
309
+ } else if (cmp > 0) {
310
+ high = mid - 1
311
+ } else {
312
+ //The key was found in the Array
313
+ return mid
314
+ }
315
+ }
316
+ // key not found
317
+ return ~low
318
+ }
319
+
320
+ /**
321
+ * Inserts the value in the position determined by its natural order determined by the compare func
322
+ * @param {Array} arr
323
+ * @param item
324
+ * @param {function} compareFunc
325
+ */
326
+ function insertSorted(arr, item, compareFunc) {
327
+ if (arr.length === 0) {
328
+ return arr.push(item)
329
+ }
330
+ let position = binarySearch(arr, item, compareFunc)
331
+ if (position < 0) {
332
+ position = ~position
333
+ }
334
+ arr.splice(position, 0, item)
335
+ }
336
+
337
+ /**
338
+ * Validates the provided parameter is of type function.
339
+ * @param {Function} fn The instance to validate.
340
+ * @param {String} [name] Name of the function to use in the error message. Defaults to 'callback'.
341
+ * @returns {Function}
342
+ */
343
+ function validateFn(fn, name) {
344
+ if (typeof fn !== "function") {
345
+ throw new errors.ArgumentError(
346
+ util.format("%s is not a function", name || "callback"),
347
+ )
348
+ }
349
+ return fn
350
+ }
351
+
352
+ /**
353
+ * Adapts the parameters based on the prepared metadata.
354
+ * If the params are passed as an associative array (Object),
355
+ * it adapts the object into an array with the same order as columns
356
+ * @param {Array|Object} params
357
+ * @param {Array} columns
358
+ * @returns {Array} Returns an array of parameters.
359
+ * @throws {Error} In case a parameter with a specific name is not defined
360
+ */
361
+ function adaptNamedParamsPrepared(params, columns) {
362
+ if (!params || Array.isArray(params) || !columns || columns.length === 0) {
363
+ // params is an array or there aren't parameters
364
+ return params
365
+ }
366
+ const paramsArray = new Array(columns.length)
367
+ params = toLowerCaseProperties(params)
368
+ const keys = {}
369
+ for (let i = 0; i < columns.length; i++) {
370
+ const name = columns[i].name
371
+ // eslint-disable-next-line no-prototype-builtins
372
+ if (!params.hasOwnProperty(name)) {
373
+ throw new errors.ArgumentError(
374
+ util.format('Parameter "%s" not defined', name),
375
+ )
376
+ }
377
+ paramsArray[i] = params[name]
378
+ keys[name] = i
379
+ }
380
+ return paramsArray
381
+ }
382
+
383
+ /**
384
+ * Adapts the associative-array of parameters and hints for simple statements
385
+ * into Arrays based on the (arbitrary) position of the keys.
386
+ * @param {Array|Object} params
387
+ * @param {ExecutionOptions} execOptions
388
+ * @returns {{ params: Array<{name, value}>, namedParameters: boolean, keyIndexes: object }} Returns an array of
389
+ * parameters and the keys as an associative array.
390
+ */
391
+ function adaptNamedParamsWithHints(params, execOptions) {
392
+ if (!params || Array.isArray(params)) {
393
+ //The parameters is an Array or there isn't parameter
394
+ return { params: params, namedParameters: false, keyIndexes: null }
395
+ }
396
+
397
+ const keys = Object.keys(params)
398
+ const paramsArray = new Array(keys.length)
399
+ const hints = new Array(keys.length)
400
+ const userHints = execOptions.getHints() || emptyObject
401
+ const keyIndexes = {}
402
+
403
+ for (let i = 0; i < keys.length; i++) {
404
+ const key = keys[i]
405
+ // As lower cased identifiers
406
+ paramsArray[i] = { name: key.toLowerCase(), value: params[key] }
407
+ hints[i] = userHints[key]
408
+ keyIndexes[key] = i
409
+ }
410
+
411
+ execOptions.setHints(hints)
412
+
413
+ return { params: paramsArray, namedParameters: true, keyIndexes }
414
+ }
415
+
416
+ /**
417
+ * Returns a string with a value repeated n times
418
+ * @param {String} val
419
+ * @param {Number} times
420
+ * @returns {String}
421
+ */
422
+ function stringRepeat(val, times) {
423
+ if (!times || times < 0) {
424
+ return null
425
+ }
426
+ if (times === 1) {
427
+ return val
428
+ }
429
+ return new Array(times + 1).join(val)
430
+ }
431
+
432
+ /**
433
+ * Returns an array containing the values of the Object, similar to Object.values().
434
+ * If obj is null or undefined, it will return an empty array.
435
+ * @param {Object} obj
436
+ * @returns {Array}
437
+ */
438
+ function objectValues(obj) {
439
+ if (!obj) {
440
+ return emptyArray
441
+ }
442
+ const keys = Object.keys(obj)
443
+ const values = new Array(keys.length)
444
+ for (let i = 0; i < keys.length; i++) {
445
+ values[i] = obj[keys[i]]
446
+ }
447
+ return values
448
+ }
449
+
450
+ /**
451
+ * Wraps the callback-based method. When no originalCallback is not defined, it returns a Promise.
452
+ * @param {ClientOptions} options
453
+ * @param {Function} originalCallback
454
+ * @param {Function} handler
455
+ * @returns {Promise|undefined}
456
+ */
457
+ function promiseWrapper(options, originalCallback, handler) {
458
+ if (typeof originalCallback === "function") {
459
+ // Callback-based invocation
460
+ handler.call(this, originalCallback)
461
+ return undefined
462
+ }
463
+ const factory = options.promiseFactory || defaultPromiseFactory
464
+ const self = this
465
+ return factory(function handlerWrapper(callback) {
466
+ handler.call(self, callback)
467
+ })
468
+ }
469
+
470
+ /**
471
+ * @param {Function} handler
472
+ * @returns {Promise}
473
+ */
474
+ function defaultPromiseFactory(handler) {
475
+ return new Promise(function executor(resolve, reject) {
476
+ handler(function handlerCallback(err, result) {
477
+ if (err) {
478
+ return reject(err)
479
+ }
480
+ resolve(result)
481
+ })
482
+ })
483
+ }
484
+
485
+ /**
486
+ * Returns the first not undefined param
487
+ */
488
+ function ifUndefined(v1, v2) {
489
+ return v1 !== undefined ? v1 : v2
490
+ }
491
+
492
+ /**
493
+ * Returns the first not undefined param
494
+ */
495
+ function ifUndefined3(v1, v2, v3) {
496
+ if (v1 !== undefined) {
497
+ return v1
498
+ }
499
+ return v2 !== undefined ? v2 : v3
500
+ }
501
+
502
+ /**
503
+ * Shuffles an Array in-place.
504
+ * @param {Array} arr
505
+ * @returns {Array}
506
+ * @private
507
+ */
508
+ function shuffleArray(arr) {
509
+ // Fisher–Yates algorithm
510
+ for (let i = arr.length - 1; i > 0; i--) {
511
+ // Math.random() has an extremely short permutation cycle length but we don't care about collisions
512
+ const j = Math.floor(Math.random() * (i + 1))
513
+ const temp = arr[i]
514
+ arr[i] = arr[j]
515
+ arr[j] = temp
516
+ }
517
+
518
+ return arr
519
+ }
520
+
521
+ // Classes
522
+
523
+ /**
524
+ * Represents a unique set of values.
525
+ * @constructor
526
+ */
527
+ function HashSet() {
528
+ this.length = 0
529
+ this.items = {}
530
+ }
531
+
532
+ /**
533
+ * Adds a new item to the set.
534
+ * @param {Object} key
535
+ * @returns {boolean} Returns true if it was added to the set; false if the key is already present.
536
+ */
537
+ HashSet.prototype.add = function (key) {
538
+ if (this.contains(key)) {
539
+ return false
540
+ }
541
+ this.items[key] = true
542
+ this.length++
543
+ return true
544
+ }
545
+
546
+ /**
547
+ * @returns {boolean} Returns true if the key is present in the set.
548
+ */
549
+ HashSet.prototype.contains = function (key) {
550
+ return this.length > 0 && this.items[key] === true
551
+ }
552
+
553
+ /**
554
+ * Removes the item from set.
555
+ * @param key
556
+ * @return {boolean} Returns true if the key existed and was removed, otherwise it returns false.
557
+ */
558
+ HashSet.prototype.remove = function (key) {
559
+ if (!this.contains(key)) {
560
+ return false
561
+ }
562
+ delete this.items[key]
563
+ this.length--
564
+ }
565
+
566
+ /**
567
+ * Returns an array containing the set items.
568
+ * @returns {Array}
569
+ */
570
+ HashSet.prototype.toArray = function () {
571
+ return Object.keys(this.items)
572
+ }
573
+
574
+ /**
575
+ * Utility class that resolves host names into addresses.
576
+ */
577
+ class AddressResolver {
578
+ /**
579
+ * Creates a new instance of the resolver.
580
+ * @param {Object} options
581
+ * @param {String} options.nameOrIp
582
+ * @param {Object} [options.dns]
583
+ */
584
+ constructor(options) {
585
+ if (!options || !options.nameOrIp || !options.dns) {
586
+ throw new Error(
587
+ "nameOrIp and dns lib must be provided as part of the options",
588
+ )
589
+ }
590
+
591
+ this._resolve4 = util.promisify(options.dns.resolve4)
592
+ this._nameOrIp = options.nameOrIp
593
+ this._isIp = net.isIP(options.nameOrIp)
594
+ this._index = 0
595
+ this._addresses = null
596
+ this._refreshing = null
597
+ }
598
+
599
+ /**
600
+ * Resolves the addresses for the host name.
601
+ */
602
+ async init() {
603
+ if (this._isIp) {
604
+ return
605
+ }
606
+
607
+ await this._resolve()
608
+ }
609
+
610
+ /**
611
+ * Tries to resolve the addresses for the host name.
612
+ */
613
+ async refresh() {
614
+ if (this._isIp) {
615
+ return
616
+ }
617
+
618
+ if (this._refreshing) {
619
+ return await promiseUtils.fromEvent(this._refreshing, "finished")
620
+ }
621
+
622
+ this._refreshing = new EventEmitter().setMaxListeners(0)
623
+
624
+ try {
625
+ await this._resolve()
626
+ } catch (err) {
627
+ // Ignore the possible resolution error
628
+ }
629
+
630
+ this._refreshing.emit("finished")
631
+ this._refreshing = null
632
+ }
633
+
634
+ async _resolve() {
635
+ const arr = await this._resolve4(this._nameOrIp)
636
+
637
+ if (!arr || arr.length === 0) {
638
+ throw new Error(`${this._nameOrIp} could not be resolved`)
639
+ }
640
+
641
+ this._addresses = arr
642
+ }
643
+
644
+ /**
645
+ * Returns resolved ips in a round-robin fashion.
646
+ */
647
+ getIp() {
648
+ if (this._isIp) {
649
+ return this._nameOrIp
650
+ }
651
+
652
+ const item = this._addresses[this._index % this._addresses.length]
653
+ this._index = this._index !== maxInt32 ? this._index + 1 : 0
654
+
655
+ return item
656
+ }
657
+ }
658
+
659
+ /**
660
+ * @param {Array} arr
661
+ * @param {Function} fn
662
+ * @param {Function} [callback]
663
+ */
664
+ function each(arr, fn, callback) {
665
+ if (!Array.isArray(arr)) {
666
+ throw new TypeError("First parameter is not an Array")
667
+ }
668
+ callback = callback || noop
669
+ const length = arr.length
670
+ if (length === 0) {
671
+ return callback()
672
+ }
673
+ let completed = 0
674
+ for (let i = 0; i < length; i++) {
675
+ fn(arr[i], next)
676
+ }
677
+ function next(err) {
678
+ if (err) {
679
+ const cb = callback
680
+ callback = noop
681
+ cb(err)
682
+ return
683
+ }
684
+ if (++completed !== length) {
685
+ return
686
+ }
687
+ callback()
688
+ }
689
+ }
690
+
691
+ /**
692
+ * @param {Array} arr
693
+ * @param {Function} fn
694
+ * @param {Function} [callback]
695
+ */
696
+ function eachSeries(arr, fn, callback) {
697
+ if (!Array.isArray(arr)) {
698
+ throw new TypeError("First parameter is not an Array")
699
+ }
700
+ callback = callback || noop
701
+ const length = arr.length
702
+ if (length === 0) {
703
+ return callback()
704
+ }
705
+ let sync
706
+ let index = 1
707
+ fn(arr[0], next)
708
+ if (sync === undefined) {
709
+ sync = false
710
+ }
711
+
712
+ function next(err) {
713
+ if (err) {
714
+ return callback(err)
715
+ }
716
+ if (index >= length) {
717
+ return callback()
718
+ }
719
+ if (sync === undefined) {
720
+ sync = true
721
+ }
722
+ if (sync) {
723
+ return process.nextTick(function () {
724
+ fn(arr[index++], next)
725
+ })
726
+ }
727
+ fn(arr[index++], next)
728
+ }
729
+ }
730
+
731
+ /**
732
+ * @param {Array} arr
733
+ * @param {Function} fn
734
+ * @param {Function} [callback]
735
+ */
736
+ function forEachOf(arr, fn, callback) {
737
+ return mapEach(arr, fn, true, callback)
738
+ }
739
+
740
+ /**
741
+ * @param {Array} arr
742
+ * @param {Function} fn
743
+ * @param {Function} [callback]
744
+ */
745
+ function map(arr, fn, callback) {
746
+ return mapEach(arr, fn, false, callback)
747
+ }
748
+
749
+ function mapEach(arr, fn, useIndex, callback) {
750
+ if (!Array.isArray(arr)) {
751
+ throw new TypeError("First parameter must be an Array")
752
+ }
753
+ callback = callback || noop
754
+ const length = arr.length
755
+ if (length === 0) {
756
+ return callback(null, [])
757
+ }
758
+ const result = new Array(length)
759
+ let completed = 0
760
+ const invoke = useIndex ? invokeWithIndex : invokeWithoutIndex
761
+ for (let i = 0; i < length; i++) {
762
+ invoke(i)
763
+ }
764
+
765
+ function invokeWithoutIndex(i) {
766
+ fn(arr[i], function mapItemCallback(err, transformed) {
767
+ result[i] = transformed
768
+ next(err)
769
+ })
770
+ }
771
+
772
+ function invokeWithIndex(i) {
773
+ fn(arr[i], i, function mapItemCallback(err, transformed) {
774
+ result[i] = transformed
775
+ next(err)
776
+ })
777
+ }
778
+
779
+ function next(err) {
780
+ if (err) {
781
+ const cb = callback
782
+ callback = noop
783
+ cb(err)
784
+ return
785
+ }
786
+ if (++completed !== length) {
787
+ return
788
+ }
789
+ callback(null, result)
790
+ }
791
+ }
792
+
793
+ /**
794
+ * @param {Array} arr
795
+ * @param {Function} fn
796
+ * @param {Function} [callback]
797
+ */
798
+ function mapSeries(arr, fn, callback) {
799
+ if (!Array.isArray(arr)) {
800
+ throw new TypeError("First parameter must be an Array")
801
+ }
802
+ callback = callback || noop
803
+ const length = arr.length
804
+ if (length === 0) {
805
+ return callback(null, [])
806
+ }
807
+ const result = new Array(length)
808
+ let index = 0
809
+ let sync
810
+ invoke(0)
811
+ if (sync === undefined) {
812
+ sync = false
813
+ }
814
+
815
+ function invoke(i) {
816
+ fn(arr[i], function mapItemCallback(err, transformed) {
817
+ result[i] = transformed
818
+ next(err)
819
+ })
820
+ }
821
+
822
+ function next(err) {
823
+ if (err) {
824
+ return callback(err)
825
+ }
826
+ if (++index === length) {
827
+ return callback(null, result)
828
+ }
829
+ if (sync === undefined) {
830
+ sync = true
831
+ }
832
+ const i = index
833
+ if (sync) {
834
+ return process.nextTick(function () {
835
+ invoke(i)
836
+ })
837
+ }
838
+ invoke(index)
839
+ }
840
+ }
841
+
842
+ /**
843
+ * @param {Array.<Function>} arr
844
+ * @param {Function} [callback]
845
+ */
846
+ function parallel(arr, callback) {
847
+ if (!Array.isArray(arr)) {
848
+ throw new TypeError("First parameter must be an Array")
849
+ }
850
+ callback = callback || noop
851
+ const length = arr.length
852
+ let completed = 0
853
+ for (let i = 0; i < length; i++) {
854
+ arr[i](next)
855
+ }
856
+ function next(err) {
857
+ if (err) {
858
+ const cb = callback
859
+ callback = noop
860
+ return cb(err)
861
+ }
862
+ if (++completed !== length) {
863
+ return
864
+ }
865
+ callback()
866
+ }
867
+ }
868
+
869
+ /**
870
+ * Similar to async.series(), but instead accumulating the result in an Array, it callbacks with the result of the last
871
+ * function in the array.
872
+ * @param {Array.<Function>} arr
873
+ * @param {Function} [callback]
874
+ */
875
+ function series(arr, callback) {
876
+ if (!Array.isArray(arr)) {
877
+ throw new TypeError("First parameter must be an Array")
878
+ }
879
+ callback = callback || noop
880
+ let index = 0
881
+ let sync
882
+ next()
883
+ function next(err, result) {
884
+ if (err) {
885
+ return callback(err)
886
+ }
887
+ if (index === arr.length) {
888
+ return callback(null, result)
889
+ }
890
+ if (sync) {
891
+ return process.nextTick(function () {
892
+ sync = true
893
+ arr[index++](next)
894
+ sync = false
895
+ })
896
+ }
897
+ sync = true
898
+ arr[index++](next)
899
+ sync = false
900
+ }
901
+ }
902
+
903
+ /**
904
+ * @param {Number} count
905
+ * @param {Function} iteratorFunc
906
+ * @param {Function} [callback]
907
+ */
908
+ function times(count, iteratorFunc, callback) {
909
+ callback = callback || noop
910
+ count = +count
911
+ if (isNaN(count) || count === 0) {
912
+ return callback()
913
+ }
914
+ let completed = 0
915
+ for (let i = 0; i < count; i++) {
916
+ iteratorFunc(i, next)
917
+ }
918
+ function next(err) {
919
+ if (err) {
920
+ const cb = callback
921
+ callback = noop
922
+ return cb(err)
923
+ }
924
+ if (++completed !== count) {
925
+ return
926
+ }
927
+ callback()
928
+ }
929
+ }
930
+
931
+ /**
932
+ * @param {Number} count
933
+ * @param {Number} limit
934
+ * @param {Function} iteratorFunc
935
+ * @param {Function} [callback]
936
+ */
937
+ function timesLimit(count, limit, iteratorFunc, callback) {
938
+ let sync = undefined
939
+ callback = callback || noop
940
+ limit = Math.min(limit, count)
941
+ let index = limit - 1
942
+ let i
943
+ let completed = 0
944
+ for (i = 0; i < limit; i++) {
945
+ iteratorFunc(i, next)
946
+ }
947
+ i = -1
948
+ function next(err) {
949
+ if (err) {
950
+ const cb = callback
951
+ callback = noop
952
+ cb(err)
953
+ return
954
+ }
955
+ if (++completed === count) {
956
+ return callback()
957
+ }
958
+ index++
959
+ if (index >= count) {
960
+ return
961
+ }
962
+ if (sync === undefined) {
963
+ sync = i >= 0
964
+ }
965
+ if (sync) {
966
+ const captureIndex = index
967
+ return process.nextTick(function () {
968
+ iteratorFunc(captureIndex, next)
969
+ })
970
+ }
971
+ iteratorFunc(index, next)
972
+ }
973
+ }
974
+
975
+ /**
976
+ * @param {Number} count
977
+ * @param {Function} iteratorFunction
978
+ * @param {Function} callback
979
+ */
980
+ function timesSeries(count, iteratorFunction, callback) {
981
+ count = +count
982
+ if (isNaN(count) || count < 1) {
983
+ return callback()
984
+ }
985
+ let index = 1
986
+ let sync
987
+ iteratorFunction(0, next)
988
+ if (sync === undefined) {
989
+ sync = false
990
+ }
991
+ function next(err) {
992
+ if (err) {
993
+ return callback(err)
994
+ }
995
+ if (index === count) {
996
+ return callback()
997
+ }
998
+ if (sync === undefined) {
999
+ sync = true
1000
+ }
1001
+ const i = index++
1002
+ if (sync) {
1003
+ //Prevent "Maximum call stack size exceeded"
1004
+ return process.nextTick(function () {
1005
+ iteratorFunction(i, next)
1006
+ })
1007
+ }
1008
+ //do a sync call as the callback is going to call on a future tick
1009
+ iteratorFunction(i, next)
1010
+ }
1011
+ }
1012
+
1013
+ /**
1014
+ * @param {Function} condition
1015
+ * @param {Function} fn
1016
+ * @param {Function} callback
1017
+ */
1018
+ function whilst(condition, fn, callback) {
1019
+ let sync = 0
1020
+ next()
1021
+ function next(err) {
1022
+ if (err) {
1023
+ return callback(err)
1024
+ }
1025
+ if (!condition()) {
1026
+ return callback()
1027
+ }
1028
+ if (sync === 0) {
1029
+ sync = 1
1030
+ fn(function (err) {
1031
+ if (sync === 1) {
1032
+ //sync function
1033
+ sync = 4
1034
+ }
1035
+ next(err)
1036
+ })
1037
+ if (sync === 1) {
1038
+ //async function
1039
+ sync = 2
1040
+ }
1041
+ return
1042
+ }
1043
+ if (sync === 4) {
1044
+ //Prevent "Maximum call stack size exceeded"
1045
+ return process.nextTick(function () {
1046
+ fn(next)
1047
+ })
1048
+ }
1049
+ //do a sync call as the callback is going to call on a future tick
1050
+ fn(next)
1051
+ }
1052
+ }
1053
+
1054
+ /**
1055
+ * Contains the methods for reading and writing vints into binary format.
1056
+ * Exposes only 2 internal methods, the rest are hidden.
1057
+ */
1058
+ const VIntCoding = (function () {
1059
+ /** @param {Long} n */
1060
+ function encodeZigZag64(n) {
1061
+ // (n << 1) ^ (n >> 63);
1062
+ return n.toUnsigned().shiftLeft(1).xor(n.shiftRight(63))
1063
+ }
1064
+
1065
+ /** @param {Long} n */
1066
+ function decodeZigZag64(n) {
1067
+ // (n >>> 1) ^ -(n & 1);
1068
+ return n.shiftRightUnsigned(1).xor(n.and(Long.ONE).negate())
1069
+ }
1070
+
1071
+ /**
1072
+ * @param {Long} value
1073
+ * @param {Buffer} buffer
1074
+ * @returns {Number}
1075
+ */
1076
+ function writeVInt(value, buffer) {
1077
+ return writeUnsignedVInt(encodeZigZag64(value), buffer)
1078
+ }
1079
+
1080
+ /**
1081
+ * @param {Long} value
1082
+ * @param {Buffer} buffer
1083
+ * @returns {number}
1084
+ */
1085
+ function writeUnsignedVInt(value, buffer) {
1086
+ const size = computeUnsignedVIntSize(value)
1087
+ if (size === 1) {
1088
+ buffer[0] = value.getLowBits()
1089
+ return 1
1090
+ }
1091
+ encodeVInt(value, size, buffer)
1092
+ return size
1093
+ }
1094
+
1095
+ /**
1096
+ * @param {Long} value
1097
+ * @returns {number}
1098
+ */
1099
+ function computeUnsignedVIntSize(value) {
1100
+ const magnitude = numberOfLeadingZeros(value.or(Long.ONE))
1101
+ return (639 - magnitude * 9) >> 6
1102
+ }
1103
+
1104
+ /**
1105
+ * @param {Long} value
1106
+ * @param {Number} size
1107
+ * @param {Buffer} buffer
1108
+ */
1109
+ function encodeVInt(value, size, buffer) {
1110
+ const extraBytes = size - 1
1111
+ let intValue = value.getLowBits()
1112
+ let i
1113
+ let intBytes = 4
1114
+ for (i = extraBytes; i >= 0 && intBytes-- > 0; i--) {
1115
+ buffer[i] = 0xff & intValue
1116
+ intValue >>= 8
1117
+ }
1118
+ intValue = value.getHighBits()
1119
+ for (; i >= 0; i--) {
1120
+ buffer[i] = 0xff & intValue
1121
+ intValue >>= 8
1122
+ }
1123
+ buffer[0] |= encodeExtraBytesToRead(extraBytes)
1124
+ }
1125
+ /**
1126
+ * Returns the number of zero bits preceding the highest-order one-bit in the binary representation of the value.
1127
+ * @param {Long} value
1128
+ * @returns {Number}
1129
+ */
1130
+ function numberOfLeadingZeros(value) {
1131
+ if (value.equals(Long.ZERO)) {
1132
+ return 64
1133
+ }
1134
+ let n = 1
1135
+ let x = value.getHighBits()
1136
+ if (x === 0) {
1137
+ n += 32
1138
+ x = value.getLowBits()
1139
+ }
1140
+ if (x >>> 16 === 0) {
1141
+ n += 16
1142
+ x <<= 16
1143
+ }
1144
+ if (x >>> 24 === 0) {
1145
+ n += 8
1146
+ x <<= 8
1147
+ }
1148
+ if (x >>> 28 === 0) {
1149
+ n += 4
1150
+ x <<= 4
1151
+ }
1152
+ if (x >>> 30 === 0) {
1153
+ n += 2
1154
+ x <<= 2
1155
+ }
1156
+ n -= x >>> 31
1157
+ return n
1158
+ }
1159
+
1160
+ function encodeExtraBytesToRead(extraBytesToRead) {
1161
+ return ~(0xff >> extraBytesToRead)
1162
+ }
1163
+
1164
+ /**
1165
+ * @param {Buffer} buffer
1166
+ * @param {{value: number}} offset
1167
+ * @returns {Long}
1168
+ */
1169
+ function readVInt(buffer, offset) {
1170
+ return decodeZigZag64(readUnsignedVInt(buffer, offset))
1171
+ }
1172
+
1173
+ /**
1174
+ * uvint_unpack
1175
+ * @param {Buffer} input
1176
+ * @param {{ value: number}} offset
1177
+ * @returns {Long}
1178
+ */
1179
+ function readUnsignedVInt(input, offset) {
1180
+ const firstByte = input[offset.value++]
1181
+ if ((firstByte & 0x80) === 0) {
1182
+ return Long.fromInt(firstByte)
1183
+ }
1184
+ const sByteInt = fromSignedByteToInt(firstByte)
1185
+ const size = numberOfExtraBytesToRead(sByteInt)
1186
+ let result = Long.fromInt(sByteInt & firstByteValueMask(size))
1187
+ for (let ii = 0; ii < size; ii++) {
1188
+ const b = Long.fromInt(input[offset.value++])
1189
+ // (result << 8) | b
1190
+ result = result.shiftLeft(8).or(b)
1191
+ }
1192
+ return result
1193
+ }
1194
+
1195
+ function fromSignedByteToInt(value) {
1196
+ if (value > 0x7f) {
1197
+ return value - 0x0100
1198
+ }
1199
+ return value
1200
+ }
1201
+
1202
+ function numberOfLeadingZerosInt32(i) {
1203
+ if (i === 0) {
1204
+ return 32
1205
+ }
1206
+ let n = 1
1207
+ if (i >>> 16 === 0) {
1208
+ n += 16
1209
+ i <<= 16
1210
+ }
1211
+ if (i >>> 24 === 0) {
1212
+ n += 8
1213
+ i <<= 8
1214
+ }
1215
+ if (i >>> 28 === 0) {
1216
+ n += 4
1217
+ i <<= 4
1218
+ }
1219
+ if (i >>> 30 === 0) {
1220
+ n += 2
1221
+ i <<= 2
1222
+ }
1223
+ n -= i >>> 31
1224
+ return n
1225
+ }
1226
+
1227
+ /**
1228
+ * @param {Number} firstByte
1229
+ * @returns {Number}
1230
+ */
1231
+ function numberOfExtraBytesToRead(firstByte) {
1232
+ // Instead of counting 1s of the byte, we negate and count 0 of the byte
1233
+ return numberOfLeadingZerosInt32(~firstByte) - 24
1234
+ }
1235
+
1236
+ /**
1237
+ * @param {Number} extraBytesToRead
1238
+ * @returns {Number}
1239
+ */
1240
+ function firstByteValueMask(extraBytesToRead) {
1241
+ return 0xff >> extraBytesToRead
1242
+ }
1243
+
1244
+ /**
1245
+ * @param {Number} value
1246
+ * @param {Buffer} output
1247
+ * @returns {void}
1248
+ */
1249
+ // eslint-disable-next-line no-unused-vars
1250
+ function writeUnsignedVInt32(value, output) {
1251
+ writeUnsignedVInt(Long.fromNumber(value), output)
1252
+ }
1253
+
1254
+ /**
1255
+ * Read up to a 32-bit integer back, using the unsigned (no zigzag) encoding.
1256
+ *
1257
+ * <p>Note this method is the same as {@link #readUnsignedVInt(DataInput)}, except that we do
1258
+ * *not* block if there are not enough bytes in the buffer to reconstruct the value.
1259
+ *
1260
+ * @param {Buffer} input
1261
+ * @param {Number} readerIndex
1262
+ * @returns {Number}
1263
+ * @throws VIntOutOfRangeException If the vint doesn't fit into a 32-bit integer
1264
+ */
1265
+ // eslint-disable-next-line no-unused-vars
1266
+ function getUnsignedVInt32(input, readerIndex) {
1267
+ return checkedCast(getUnsignedVInt(input, readerIndex, input.length))
1268
+ }
1269
+
1270
+ /**
1271
+ *
1272
+ * @param {Buffer} input
1273
+ * @param {Number} readerIndex
1274
+ * @param {Number} readerLimit
1275
+ * @returns {Long}
1276
+ */
1277
+ function getUnsignedVInt(input, readerIndex, readerLimit) {
1278
+ if (readerIndex < 0) {
1279
+ throw new errors.ArgumentError(
1280
+ "Reader index should be non-negative, but was " + readerIndex,
1281
+ )
1282
+ }
1283
+
1284
+ if (readerIndex >= readerLimit) {
1285
+ return Long.fromNumber(-1)
1286
+ }
1287
+
1288
+ const firstByte = /** @type {Number} */ (input.at(readerIndex++))
1289
+
1290
+ // Bail out early if this is one byte, necessary or it fails later
1291
+ if (firstByte >= 0) {
1292
+ return Long.fromNumber(firstByte)
1293
+ }
1294
+
1295
+ const size = numberOfExtraBytesToRead(firstByte)
1296
+ if (readerIndex + size > readerLimit) {
1297
+ return Long.fromNumber(-1)
1298
+ }
1299
+
1300
+ const retval = Long.fromNumber(firstByte & firstByteValueMask(size))
1301
+ for (let ii = 0; ii < size; ii++) {
1302
+ const b = /** @type {Number} */ (input.at(readerIndex++))
1303
+ retval.shiftLeft(8)
1304
+ retval.or(b & 0xff)
1305
+ }
1306
+
1307
+ return retval
1308
+ }
1309
+
1310
+ /**
1311
+ *
1312
+ * @param {Long} value
1313
+ * @returns {Number}
1314
+ */
1315
+ function checkedCast(value) {
1316
+ const result = value.toInt()
1317
+ if (value.notEquals(result)) {
1318
+ throw new errors.VIntOutOfRangeException(value)
1319
+ }
1320
+ return result
1321
+ }
1322
+
1323
+ /**
1324
+ *
1325
+ * @param {Buffer} bytes
1326
+ * @returns {[number, number]} [size, bytes read]
1327
+ */
1328
+ function uvintUnpack(bytes) {
1329
+ const firstByte = bytes[0]
1330
+
1331
+ if ((firstByte & 128) === 0) {
1332
+ return [firstByte, 1]
1333
+ }
1334
+
1335
+ const numExtraBytes = 8 - (~firstByte & 0xff).toString(2).length
1336
+ let rv = firstByte & (0xff >> numExtraBytes)
1337
+
1338
+ for (let idx = 1; idx <= numExtraBytes; idx++) {
1339
+ const newByte = bytes[idx]
1340
+ rv <<= 8
1341
+ rv |= newByte & 0xff
1342
+ }
1343
+
1344
+ return [rv, numExtraBytes + 1]
1345
+ }
1346
+
1347
+ /**
1348
+ *
1349
+ * @param {Number} val
1350
+ * @returns {Buffer}
1351
+ */
1352
+ function uvintPack(val) {
1353
+ const rv = []
1354
+ if (val < 128) {
1355
+ rv.push(val)
1356
+ } else {
1357
+ let v = val
1358
+ let numExtraBytes = 0
1359
+ let numBits = v.toString(2).length
1360
+ let reservedBits = numExtraBytes + 1
1361
+
1362
+ while (numBits > 8 - reservedBits) {
1363
+ numExtraBytes += 1
1364
+ numBits -= 8
1365
+ reservedBits = Math.min(numExtraBytes + 1, 8)
1366
+ rv.push(v & 0xff)
1367
+ v >>= 8
1368
+ }
1369
+
1370
+ if (numExtraBytes > 8) {
1371
+ throw new Error(
1372
+ `Value ${val} is too big and cannot be encoded as vint`,
1373
+ )
1374
+ }
1375
+
1376
+ const n = 8 - numExtraBytes
1377
+ v |= (0xff >> n) << n
1378
+ rv.push(Math.abs(v))
1379
+ }
1380
+
1381
+ rv.reverse()
1382
+ return Buffer.from(rv)
1383
+ }
1384
+
1385
+ return {
1386
+ readVInt: readVInt,
1387
+ writeVInt: writeVInt,
1388
+ uvintPack: uvintPack,
1389
+ uvintUnpack: uvintUnpack,
1390
+ }
1391
+ })()
1392
+
1393
+ export {
1394
+ adaptNamedParamsPrepared,
1395
+ adaptNamedParamsWithHints,
1396
+ AddressResolver,
1397
+ allocBuffer,
1398
+ allocBufferUnsafe,
1399
+ allocBufferFromArray,
1400
+ allocBufferFromString,
1401
+ arrayIterator,
1402
+ binarySearch,
1403
+ callbackOnce,
1404
+ copyBuffer,
1405
+ deepExtend,
1406
+ each,
1407
+ eachSeries,
1408
+ emptyArray,
1409
+ emptyObject,
1410
+ extend,
1411
+ fixStack,
1412
+ forEachOf,
1413
+ funcCompare,
1414
+ HashSet,
1415
+ ifUndefined,
1416
+ ifUndefined3,
1417
+ insertSorted,
1418
+ iteratorToArray,
1419
+ log,
1420
+ map,
1421
+ mapSeries,
1422
+ maxInt,
1423
+ noop,
1424
+ objectValues,
1425
+ parallel,
1426
+ promiseWrapper,
1427
+ propCompare,
1428
+ series,
1429
+ shuffleArray,
1430
+ stringRepeat,
1431
+ times,
1432
+ timesLimit,
1433
+ timesSeries,
1434
+ totalLength,
1435
+ validateFn,
1436
+ VIntCoding,
1437
+ whilst,
1438
+ }
1439
+
1440
+ export default {
1441
+ adaptNamedParamsPrepared,
1442
+ adaptNamedParamsWithHints,
1443
+ AddressResolver,
1444
+ allocBuffer,
1445
+ allocBufferUnsafe,
1446
+ allocBufferFromArray,
1447
+ allocBufferFromString,
1448
+ arrayIterator,
1449
+ binarySearch,
1450
+ callbackOnce,
1451
+ copyBuffer,
1452
+ deepExtend,
1453
+ each,
1454
+ eachSeries,
1455
+ emptyArray,
1456
+ emptyObject,
1457
+ extend,
1458
+ fixStack,
1459
+ forEachOf,
1460
+ funcCompare,
1461
+ HashSet,
1462
+ ifUndefined,
1463
+ ifUndefined3,
1464
+ insertSorted,
1465
+ iteratorToArray,
1466
+ log,
1467
+ map,
1468
+ mapSeries,
1469
+ maxInt,
1470
+ noop,
1471
+ objectValues,
1472
+ parallel,
1473
+ promiseWrapper,
1474
+ propCompare,
1475
+ series,
1476
+ shuffleArray,
1477
+ stringRepeat,
1478
+ times,
1479
+ timesLimit,
1480
+ timesSeries,
1481
+ totalLength,
1482
+ validateFn,
1483
+ VIntCoding,
1484
+ whilst,
1485
+ }