12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.matchesParentDomain = exports.parseUnsignedInteger = exports.parseInteger = exports.compareObjectId = exports.getMongoDBClientEncryption = exports.commandSupportsReadConcern = exports.shuffle = exports.supportsRetryableWrites = exports.enumToString = exports.emitWarningOnce = exports.emitWarning = exports.MONGODB_WARNING_CODE = exports.DEFAULT_PK_FACTORY = exports.HostAddress = exports.BufferPool = exports.List = exports.deepCopy = exports.isRecord = exports.setDifference = exports.isHello = exports.isSuperset = exports.resolveOptions = exports.hasAtomicOperators = exports.calculateDurationInMs = exports.now = exports.makeStateMachine = exports.errorStrictEqual = exports.arrayStrictEqual = exports.eachAsync = exports.maxWireVersion = exports.uuidV4 = exports.databaseNamespace = exports.maybeCallback = exports.makeCounter = exports.MongoDBCollectionNamespace = exports.MongoDBNamespace = exports.ns = exports.getTopology = exports.decorateWithExplain = exports.decorateWithReadConcern = exports.decorateWithCollation = exports.isPromiseLike = exports.applyRetryableWrites = exports.filterOptions = exports.mergeOptions = exports.isObject = exports.normalizeHintField = exports.checkCollectionName = exports.hostMatchesWildcards = exports.ByteUtils = void 0;
- exports.request = void 0;
- const crypto = require("crypto");
- const http = require("http");
- const url = require("url");
- const url_1 = require("url");
- const bson_1 = require("./bson");
- const constants_1 = require("./cmap/wire_protocol/constants");
- const constants_2 = require("./constants");
- const error_1 = require("./error");
- const read_concern_1 = require("./read_concern");
- const read_preference_1 = require("./read_preference");
- const common_1 = require("./sdam/common");
- const write_concern_1 = require("./write_concern");
- exports.ByteUtils = {
- toLocalBufferType(buffer) {
- return Buffer.isBuffer(buffer)
- ? buffer
- : Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength);
- },
- equals(seqA, seqB) {
- return exports.ByteUtils.toLocalBufferType(seqA).equals(seqB);
- },
- compare(seqA, seqB) {
- return exports.ByteUtils.toLocalBufferType(seqA).compare(seqB);
- },
- toBase64(uint8array) {
- return exports.ByteUtils.toLocalBufferType(uint8array).toString('base64');
- }
- };
- function hostMatchesWildcards(host, wildcards) {
- for (const wildcard of wildcards) {
- if (host === wildcard ||
- (wildcard.startsWith('*.') && host?.endsWith(wildcard.substring(2, wildcard.length))) ||
- (wildcard.startsWith('*/') && host?.endsWith(wildcard.substring(2, wildcard.length)))) {
- return true;
- }
- }
- return false;
- }
- exports.hostMatchesWildcards = hostMatchesWildcards;
- function checkCollectionName(collectionName) {
- if ('string' !== typeof collectionName) {
- throw new error_1.MongoInvalidArgumentError('Collection name must be a String');
- }
- if (!collectionName || collectionName.indexOf('..') !== -1) {
- throw new error_1.MongoInvalidArgumentError('Collection names cannot be empty');
- }
- if (collectionName.indexOf('$') !== -1 &&
- collectionName.match(/((^\$cmd)|(oplog\.\$main))/) == null) {
-
- throw new error_1.MongoInvalidArgumentError("Collection names must not contain '$'");
- }
- if (collectionName.match(/^\.|\.$/) != null) {
-
- throw new error_1.MongoInvalidArgumentError("Collection names must not start or end with '.'");
- }
-
- if (collectionName.indexOf('\x00') !== -1) {
-
- throw new error_1.MongoInvalidArgumentError('Collection names cannot contain a null character');
- }
- }
- exports.checkCollectionName = checkCollectionName;
- function normalizeHintField(hint) {
- let finalHint = undefined;
- if (typeof hint === 'string') {
- finalHint = hint;
- }
- else if (Array.isArray(hint)) {
- finalHint = {};
- hint.forEach(param => {
- finalHint[param] = 1;
- });
- }
- else if (hint != null && typeof hint === 'object') {
- finalHint = {};
- for (const name in hint) {
- finalHint[name] = hint[name];
- }
- }
- return finalHint;
- }
- exports.normalizeHintField = normalizeHintField;
- const TO_STRING = (object) => Object.prototype.toString.call(object);
- function isObject(arg) {
- return '[object Object]' === TO_STRING(arg);
- }
- exports.isObject = isObject;
- function mergeOptions(target, source) {
- return { ...target, ...source };
- }
- exports.mergeOptions = mergeOptions;
- function filterOptions(options, names) {
- const filterOptions = {};
- for (const name in options) {
- if (names.includes(name)) {
- filterOptions[name] = options[name];
- }
- }
-
- return filterOptions;
- }
- exports.filterOptions = filterOptions;
- function applyRetryableWrites(target, db) {
- if (db && db.s.options?.retryWrites) {
- target.retryWrites = true;
- }
- return target;
- }
- exports.applyRetryableWrites = applyRetryableWrites;
- function isPromiseLike(value) {
- return !!value && typeof value.then === 'function';
- }
- exports.isPromiseLike = isPromiseLike;
- function decorateWithCollation(command, target, options) {
- const capabilities = getTopology(target).capabilities;
- if (options.collation && typeof options.collation === 'object') {
- if (capabilities && capabilities.commandsTakeCollation) {
- command.collation = options.collation;
- }
- else {
- throw new error_1.MongoCompatibilityError(`Current topology does not support collation`);
- }
- }
- }
- exports.decorateWithCollation = decorateWithCollation;
- function decorateWithReadConcern(command, coll, options) {
- if (options && options.session && options.session.inTransaction()) {
- return;
- }
- const readConcern = Object.assign({}, command.readConcern || {});
- if (coll.s.readConcern) {
- Object.assign(readConcern, coll.s.readConcern);
- }
- if (Object.keys(readConcern).length > 0) {
- Object.assign(command, { readConcern: readConcern });
- }
- }
- exports.decorateWithReadConcern = decorateWithReadConcern;
- function decorateWithExplain(command, explain) {
- if (command.explain) {
- return command;
- }
- return { explain: command, verbosity: explain.verbosity };
- }
- exports.decorateWithExplain = decorateWithExplain;
- function getTopology(provider) {
-
- if ('topology' in provider && provider.topology) {
- return provider.topology;
- }
- else if ('client' in provider && provider.client.topology) {
- return provider.client.topology;
- }
- throw new error_1.MongoNotConnectedError('MongoClient must be connected to perform this operation');
- }
- exports.getTopology = getTopology;
- function ns(ns) {
- return MongoDBNamespace.fromString(ns);
- }
- exports.ns = ns;
- class MongoDBNamespace {
-
- constructor(db, collection) {
- this.db = db;
- this.collection = collection;
- this.collection = collection === '' ? undefined : collection;
- }
- toString() {
- return this.collection ? `${this.db}.${this.collection}` : this.db;
- }
- withCollection(collection) {
- return new MongoDBCollectionNamespace(this.db, collection);
- }
- static fromString(namespace) {
- if (typeof namespace !== 'string' || namespace === '') {
-
- throw new error_1.MongoRuntimeError(`Cannot parse namespace from "${namespace}"`);
- }
- const [db, ...collectionParts] = namespace.split('.');
- const collection = collectionParts.join('.');
- return new MongoDBNamespace(db, collection === '' ? undefined : collection);
- }
- }
- exports.MongoDBNamespace = MongoDBNamespace;
- class MongoDBCollectionNamespace extends MongoDBNamespace {
- constructor(db, collection) {
- super(db, collection);
- this.collection = collection;
- }
- }
- exports.MongoDBCollectionNamespace = MongoDBCollectionNamespace;
- function* makeCounter(seed = 0) {
- let count = seed;
- while (true) {
- const newCount = count;
- count += 1;
- yield newCount;
- }
- }
- exports.makeCounter = makeCounter;
- function maybeCallback(promiseFn, callback) {
- const promise = promiseFn();
- if (callback == null) {
- return promise;
- }
- promise.then(result => callback(undefined, result), error => callback(error));
- return;
- }
- exports.maybeCallback = maybeCallback;
- function databaseNamespace(ns) {
- return ns.split('.')[0];
- }
- exports.databaseNamespace = databaseNamespace;
- function uuidV4() {
- const result = crypto.randomBytes(16);
- result[6] = (result[6] & 0x0f) | 0x40;
- result[8] = (result[8] & 0x3f) | 0x80;
- return result;
- }
- exports.uuidV4 = uuidV4;
- function maxWireVersion(topologyOrServer) {
- if (topologyOrServer) {
- if (topologyOrServer.loadBalanced) {
-
-
-
-
- return constants_1.MAX_SUPPORTED_WIRE_VERSION;
- }
- if (topologyOrServer.hello) {
- return topologyOrServer.hello.maxWireVersion;
- }
- if ('lastHello' in topologyOrServer && typeof topologyOrServer.lastHello === 'function') {
- const lastHello = topologyOrServer.lastHello();
- if (lastHello) {
- return lastHello.maxWireVersion;
- }
- }
- if (topologyOrServer.description &&
- 'maxWireVersion' in topologyOrServer.description &&
- topologyOrServer.description.maxWireVersion != null) {
- return topologyOrServer.description.maxWireVersion;
- }
- }
- return 0;
- }
- exports.maxWireVersion = maxWireVersion;
- function eachAsync(arr, eachFn, callback) {
- arr = arr || [];
- let idx = 0;
- let awaiting = 0;
- for (idx = 0; idx < arr.length; ++idx) {
- awaiting++;
- eachFn(arr[idx], eachCallback);
- }
- if (awaiting === 0) {
- callback();
- return;
- }
- function eachCallback(err) {
- awaiting--;
- if (err) {
- callback(err);
- return;
- }
- if (idx === arr.length && awaiting <= 0) {
- callback();
- }
- }
- }
- exports.eachAsync = eachAsync;
- function arrayStrictEqual(arr, arr2) {
- if (!Array.isArray(arr) || !Array.isArray(arr2)) {
- return false;
- }
- return arr.length === arr2.length && arr.every((elt, idx) => elt === arr2[idx]);
- }
- exports.arrayStrictEqual = arrayStrictEqual;
- function errorStrictEqual(lhs, rhs) {
- if (lhs === rhs) {
- return true;
- }
- if (!lhs || !rhs) {
- return lhs === rhs;
- }
- if ((lhs == null && rhs != null) || (lhs != null && rhs == null)) {
- return false;
- }
- if (lhs.constructor.name !== rhs.constructor.name) {
- return false;
- }
- if (lhs.message !== rhs.message) {
- return false;
- }
- return true;
- }
- exports.errorStrictEqual = errorStrictEqual;
- function makeStateMachine(stateTable) {
- return function stateTransition(target, newState) {
- const legalStates = stateTable[target.s.state];
- if (legalStates && legalStates.indexOf(newState) < 0) {
- throw new error_1.MongoRuntimeError(`illegal state transition from [${target.s.state}] => [${newState}], allowed: [${legalStates}]`);
- }
- target.emit('stateChanged', target.s.state, newState);
- target.s.state = newState;
- };
- }
- exports.makeStateMachine = makeStateMachine;
- function now() {
- const hrtime = process.hrtime();
- return Math.floor(hrtime[0] * 1000 + hrtime[1] / 1000000);
- }
- exports.now = now;
- function calculateDurationInMs(started) {
- if (typeof started !== 'number') {
- throw new error_1.MongoInvalidArgumentError('Numeric value required to calculate duration');
- }
- const elapsed = now() - started;
- return elapsed < 0 ? 0 : elapsed;
- }
- exports.calculateDurationInMs = calculateDurationInMs;
- function hasAtomicOperators(doc) {
- if (Array.isArray(doc)) {
- for (const document of doc) {
- if (hasAtomicOperators(document)) {
- return true;
- }
- }
- return false;
- }
- const keys = Object.keys(doc);
- return keys.length > 0 && keys[0][0] === '$';
- }
- exports.hasAtomicOperators = hasAtomicOperators;
- function resolveOptions(parent, options) {
- const result = Object.assign({}, options, (0, bson_1.resolveBSONOptions)(options, parent));
-
- const session = options?.session;
- if (!session?.inTransaction()) {
- const readConcern = read_concern_1.ReadConcern.fromOptions(options) ?? parent?.readConcern;
- if (readConcern) {
- result.readConcern = readConcern;
- }
- const writeConcern = write_concern_1.WriteConcern.fromOptions(options) ?? parent?.writeConcern;
- if (writeConcern) {
- result.writeConcern = writeConcern;
- }
- }
- const readPreference = read_preference_1.ReadPreference.fromOptions(options) ?? parent?.readPreference;
- if (readPreference) {
- result.readPreference = readPreference;
- }
- return result;
- }
- exports.resolveOptions = resolveOptions;
- function isSuperset(set, subset) {
- set = Array.isArray(set) ? new Set(set) : set;
- subset = Array.isArray(subset) ? new Set(subset) : subset;
- for (const elem of subset) {
- if (!set.has(elem)) {
- return false;
- }
- }
- return true;
- }
- exports.isSuperset = isSuperset;
- function isHello(doc) {
- return doc[constants_2.LEGACY_HELLO_COMMAND] || doc.hello ? true : false;
- }
- exports.isHello = isHello;
- function setDifference(setA, setB) {
- const difference = new Set(setA);
- for (const elem of setB) {
- difference.delete(elem);
- }
- return difference;
- }
- exports.setDifference = setDifference;
- const HAS_OWN = (object, prop) => Object.prototype.hasOwnProperty.call(object, prop);
- function isRecord(value, requiredKeys = undefined) {
- if (!isObject(value)) {
- return false;
- }
- const ctor = value.constructor;
- if (ctor && ctor.prototype) {
- if (!isObject(ctor.prototype)) {
- return false;
- }
-
- if (!HAS_OWN(ctor.prototype, 'isPrototypeOf')) {
- return false;
- }
- }
- if (requiredKeys) {
- const keys = Object.keys(value);
- return isSuperset(keys, requiredKeys);
- }
- return true;
- }
- exports.isRecord = isRecord;
- function deepCopy(value) {
- if (value == null) {
- return value;
- }
- else if (Array.isArray(value)) {
- return value.map(item => deepCopy(item));
- }
- else if (isRecord(value)) {
- const res = {};
- for (const key in value) {
- res[key] = deepCopy(value[key]);
- }
- return res;
- }
- const ctor = value.constructor;
- if (ctor) {
- switch (ctor.name.toLowerCase()) {
- case 'date':
- return new ctor(Number(value));
- case 'map':
- return new Map(value);
- case 'set':
- return new Set(value);
- case 'buffer':
- return Buffer.from(value);
- }
- }
- return value;
- }
- exports.deepCopy = deepCopy;
- class List {
- get length() {
- return this.count;
- }
- get [Symbol.toStringTag]() {
- return 'List';
- }
- constructor() {
- this.count = 0;
-
-
-
- this.head = {
- next: null,
- prev: null,
- value: null
- };
- this.head.next = this.head;
- this.head.prev = this.head;
- }
- toArray() {
- return Array.from(this);
- }
- toString() {
- return `head <=> ${this.toArray().join(' <=> ')} <=> head`;
- }
- *[Symbol.iterator]() {
- for (const node of this.nodes()) {
- yield node.value;
- }
- }
- *nodes() {
- let ptr = this.head.next;
- while (ptr !== this.head) {
-
- const { next } = ptr;
- yield ptr;
- ptr = next;
- }
- }
-
- push(value) {
- this.count += 1;
- const newNode = {
- next: this.head,
- prev: this.head.prev,
- value
- };
- this.head.prev.next = newNode;
- this.head.prev = newNode;
- }
-
- pushMany(iterable) {
- for (const value of iterable) {
- this.push(value);
- }
- }
-
- unshift(value) {
- this.count += 1;
- const newNode = {
- next: this.head.next,
- prev: this.head,
- value
- };
- this.head.next.prev = newNode;
- this.head.next = newNode;
- }
- remove(node) {
- if (node === this.head || this.length === 0) {
- return null;
- }
- this.count -= 1;
- const prevNode = node.prev;
- const nextNode = node.next;
- prevNode.next = nextNode;
- nextNode.prev = prevNode;
- return node.value;
- }
-
- shift() {
- return this.remove(this.head.next);
- }
-
- pop() {
- return this.remove(this.head.prev);
- }
-
- prune(filter) {
- for (const node of this.nodes()) {
- if (filter(node.value)) {
- this.remove(node);
- }
- }
- }
- clear() {
- this.count = 0;
- this.head.next = this.head;
- this.head.prev = this.head;
- }
-
- first() {
-
- return this.head.next.value;
- }
-
- last() {
-
- return this.head.prev.value;
- }
- }
- exports.List = List;
- class BufferPool {
- constructor() {
- this.buffers = new List();
- this.totalByteLength = 0;
- }
- get length() {
- return this.totalByteLength;
- }
-
- append(buffer) {
- this.buffers.push(buffer);
- this.totalByteLength += buffer.length;
- }
-
- getInt32() {
- if (this.totalByteLength < 4) {
- return null;
- }
- const firstBuffer = this.buffers.first();
- if (firstBuffer != null && firstBuffer.byteLength >= 4) {
- return firstBuffer.readInt32LE(0);
- }
-
-
- const top4Bytes = this.read(4);
- const value = top4Bytes.readInt32LE(0);
-
- this.totalByteLength += 4;
- this.buffers.unshift(top4Bytes);
- return value;
- }
-
- read(size) {
- if (typeof size !== 'number' || size < 0) {
- throw new error_1.MongoInvalidArgumentError('Argument "size" must be a non-negative number');
- }
-
- if (size > this.totalByteLength) {
- return Buffer.alloc(0);
- }
-
-
- const result = Buffer.allocUnsafe(size);
- for (let bytesRead = 0; bytesRead < size;) {
- const buffer = this.buffers.shift();
- if (buffer == null) {
- break;
- }
- const bytesRemaining = size - bytesRead;
- const bytesReadable = Math.min(bytesRemaining, buffer.byteLength);
- const bytes = buffer.subarray(0, bytesReadable);
- result.set(bytes, bytesRead);
- bytesRead += bytesReadable;
- this.totalByteLength -= bytesReadable;
- if (bytesReadable < buffer.byteLength) {
- this.buffers.unshift(buffer.subarray(bytesReadable));
- }
- }
- return result;
- }
- }
- exports.BufferPool = BufferPool;
- class HostAddress {
- constructor(hostString) {
- this.host = undefined;
- this.port = undefined;
- this.socketPath = undefined;
- this.isIPv6 = false;
- const escapedHost = hostString.split(' ').join('%20');
- if (escapedHost.endsWith('.sock')) {
-
- this.socketPath = decodeURIComponent(escapedHost);
- return;
- }
- const urlString = `iLoveJS://${escapedHost}`;
- let url;
- try {
- url = new url_1.URL(urlString);
- }
- catch (urlError) {
- const runtimeError = new error_1.MongoRuntimeError(`Unable to parse ${escapedHost} with URL`);
- runtimeError.cause = urlError;
- throw runtimeError;
- }
- const hostname = url.hostname;
- const port = url.port;
- let normalized = decodeURIComponent(hostname).toLowerCase();
- if (normalized.startsWith('[') && normalized.endsWith(']')) {
- this.isIPv6 = true;
- normalized = normalized.substring(1, hostname.length - 1);
- }
- this.host = normalized.toLowerCase();
- if (typeof port === 'number') {
- this.port = port;
- }
- else if (typeof port === 'string' && port !== '') {
- this.port = Number.parseInt(port, 10);
- }
- else {
- this.port = 27017;
- }
- if (this.port === 0) {
- throw new error_1.MongoParseError('Invalid port (zero) with hostname');
- }
- Object.freeze(this);
- }
- [Symbol.for('nodejs.util.inspect.custom')]() {
- return this.inspect();
- }
- inspect() {
- return `new HostAddress('${this.toString()}')`;
- }
- toString() {
- if (typeof this.host === 'string') {
- if (this.isIPv6) {
- return `[${this.host}]:${this.port}`;
- }
- return `${this.host}:${this.port}`;
- }
- return `${this.socketPath}`;
- }
- static fromString(s) {
- return new HostAddress(s);
- }
- static fromHostPort(host, port) {
- if (host.includes(':')) {
- host = `[${host}]`;
- }
- return HostAddress.fromString(`${host}:${port}`);
- }
- static fromSrvRecord({ name, port }) {
- return HostAddress.fromHostPort(name, port);
- }
- toHostPort() {
- if (this.socketPath) {
- return { host: this.socketPath, port: 0 };
- }
- const host = this.host ?? '';
- const port = this.port ?? 0;
- return { host, port };
- }
- }
- exports.HostAddress = HostAddress;
- exports.DEFAULT_PK_FACTORY = {
-
- createPk() {
- return new bson_1.ObjectId();
- }
- };
- exports.MONGODB_WARNING_CODE = 'MONGODB DRIVER';
- function emitWarning(message) {
- return process.emitWarning(message, { code: exports.MONGODB_WARNING_CODE });
- }
- exports.emitWarning = emitWarning;
- const emittedWarnings = new Set();
- function emitWarningOnce(message) {
- if (!emittedWarnings.has(message)) {
- emittedWarnings.add(message);
- return emitWarning(message);
- }
- }
- exports.emitWarningOnce = emitWarningOnce;
- function enumToString(en) {
- return Object.values(en).join(', ');
- }
- exports.enumToString = enumToString;
- function supportsRetryableWrites(server) {
- if (!server) {
- return false;
- }
- if (server.loadBalanced) {
-
- return true;
- }
- if (server.description.logicalSessionTimeoutMinutes != null) {
-
- if (server.description.type !== common_1.ServerType.Standalone) {
-
- return true;
- }
- }
- return false;
- }
- exports.supportsRetryableWrites = supportsRetryableWrites;
- function shuffle(sequence, limit = 0) {
- const items = Array.from(sequence);
- if (limit > items.length) {
- throw new error_1.MongoRuntimeError('Limit must be less than the number of items');
- }
- let remainingItemsToShuffle = items.length;
- const lowerBound = limit % items.length === 0 ? 1 : items.length - limit;
- while (remainingItemsToShuffle > lowerBound) {
-
- const randomIndex = Math.floor(Math.random() * remainingItemsToShuffle);
- remainingItemsToShuffle -= 1;
-
- const swapHold = items[remainingItemsToShuffle];
- items[remainingItemsToShuffle] = items[randomIndex];
- items[randomIndex] = swapHold;
- }
- return limit % items.length === 0 ? items : items.slice(lowerBound);
- }
- exports.shuffle = shuffle;
- function commandSupportsReadConcern(command, options) {
- if (command.aggregate || command.count || command.distinct || command.find || command.geoNear) {
- return true;
- }
- if (command.mapReduce &&
- options &&
- options.out &&
- (options.out.inline === 1 || options.out === 'inline')) {
- return true;
- }
- return false;
- }
- exports.commandSupportsReadConcern = commandSupportsReadConcern;
- function getMongoDBClientEncryption() {
- let mongodbClientEncryption = null;
-
-
- if (typeof process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE === 'string' &&
- process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE.length > 0) {
- try {
-
-
-
- mongodbClientEncryption = require(process.env.MONGODB_CLIENT_ENCRYPTION_OVERRIDE);
- }
- catch {
-
- }
- }
- else {
- try {
-
-
-
- mongodbClientEncryption = require('mongodb-client-encryption');
- }
- catch {
-
- }
- }
- return mongodbClientEncryption;
- }
- exports.getMongoDBClientEncryption = getMongoDBClientEncryption;
- function compareObjectId(oid1, oid2) {
- if (oid1 == null && oid2 == null) {
- return 0;
- }
- if (oid1 == null) {
- return -1;
- }
- if (oid2 == null) {
- return 1;
- }
- return exports.ByteUtils.compare(oid1.id, oid2.id);
- }
- exports.compareObjectId = compareObjectId;
- function parseInteger(value) {
- if (typeof value === 'number')
- return Math.trunc(value);
- const parsedValue = Number.parseInt(String(value), 10);
- return Number.isNaN(parsedValue) ? null : parsedValue;
- }
- exports.parseInteger = parseInteger;
- function parseUnsignedInteger(value) {
- const parsedInt = parseInteger(value);
- return parsedInt != null && parsedInt >= 0 ? parsedInt : null;
- }
- exports.parseUnsignedInteger = parseUnsignedInteger;
- function matchesParentDomain(address, srvHost) {
-
- const normalizedAddress = address.endsWith('.') ? address.slice(0, address.length - 1) : address;
- const normalizedSrvHost = srvHost.endsWith('.') ? srvHost.slice(0, srvHost.length - 1) : srvHost;
- const allCharacterBeforeFirstDot = /^.*?\./;
-
-
-
-
- const addressDomain = `.${normalizedAddress.replace(allCharacterBeforeFirstDot, '')}`;
- const srvHostDomain = `.${normalizedSrvHost.replace(allCharacterBeforeFirstDot, '')}`;
- return addressDomain.endsWith(srvHostDomain);
- }
- exports.matchesParentDomain = matchesParentDomain;
- async function request(uri, options = {}) {
- return new Promise((resolve, reject) => {
- const requestOptions = {
- method: 'GET',
- timeout: 10000,
- json: true,
- ...url.parse(uri),
- ...options
- };
- const req = http.request(requestOptions, res => {
- res.setEncoding('utf8');
- let data = '';
- res.on('data', d => {
- data += d;
- });
- res.once('end', () => {
- if (options.json === false) {
- resolve(data);
- return;
- }
- try {
- const parsed = JSON.parse(data);
- resolve(parsed);
- }
- catch {
-
- reject(new error_1.MongoRuntimeError(`Invalid JSON response: "${data}"`));
- }
- });
- });
- req.once('timeout', () => req.destroy(new error_1.MongoNetworkTimeoutError(`Network request to ${uri} timed out after ${options.timeout} ms`)));
- req.once('error', error => reject(error));
- req.end();
- });
- }
- exports.request = request;
|