123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.CommaAndColonSeparatedRecord = exports.redactConnectionString = void 0;
- const whatwg_url_1 = require("whatwg-url");
- const redact_1 = require("./redact");
- Object.defineProperty(exports, "redactConnectionString", { enumerable: true, get: function () { return redact_1.redactConnectionString; } });
- const DUMMY_HOSTNAME = '__this_is_a_placeholder__';
- function connectionStringHasValidScheme(connectionString) {
- return (connectionString.startsWith('mongodb://') ||
- connectionString.startsWith('mongodb+srv://'));
- }
- const HOSTS_REGEX = new RegExp(String.raw `^(?<protocol>mongodb(?:\+srv|)):\/\/(?:(?<username>[^:]*)(?::(?<password>[^@]*))?@)?(?<hosts>(?!:)[^\/?@]*)(?<rest>.*)`);
- class CaseInsensitiveMap extends Map {
- delete(name) {
- return super.delete(this._normalizeKey(name));
- }
- get(name) {
- return super.get(this._normalizeKey(name));
- }
- has(name) {
- return super.has(this._normalizeKey(name));
- }
- set(name, value) {
- return super.set(this._normalizeKey(name), value);
- }
- _normalizeKey(name) {
- name = `${name}`;
- for (const key of this.keys()) {
- if (key.toLowerCase() === name.toLowerCase()) {
- name = key;
- break;
- }
- }
- return name;
- }
- }
- function caseInsenstiveURLSearchParams(Ctor) {
- return class CaseInsenstiveURLSearchParams extends Ctor {
- append(name, value) {
- return super.append(this._normalizeKey(name), value);
- }
- delete(name) {
- return super.delete(this._normalizeKey(name));
- }
- get(name) {
- return super.get(this._normalizeKey(name));
- }
- getAll(name) {
- return super.getAll(this._normalizeKey(name));
- }
- has(name) {
- return super.has(this._normalizeKey(name));
- }
- set(name, value) {
- return super.set(this._normalizeKey(name), value);
- }
- keys() {
- return super.keys();
- }
- values() {
- return super.values();
- }
- entries() {
- return super.entries();
- }
- [Symbol.iterator]() {
- return super[Symbol.iterator]();
- }
- _normalizeKey(name) {
- return CaseInsensitiveMap.prototype._normalizeKey.call(this, name);
- }
- };
- }
- class URLWithoutHost extends whatwg_url_1.URL {
- }
- class MongoParseError extends Error {
- get name() {
- return 'MongoParseError';
- }
- }
- class ConnectionString extends URLWithoutHost {
- constructor(uri) {
- var _a;
- if (!connectionStringHasValidScheme(uri)) {
- throw new MongoParseError('Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"');
- }
- const match = uri.match(HOSTS_REGEX);
- if (!match) {
- throw new MongoParseError(`Invalid connection string "${uri}"`);
- }
- const { protocol, username, password, hosts, rest } = (_a = match.groups) !== null && _a !== void 0 ? _a : {};
- if (!protocol || !hosts) {
- throw new MongoParseError(`Protocol and host list are required in "${uri}"`);
- }
- try {
- decodeURIComponent(username !== null && username !== void 0 ? username : '');
- decodeURIComponent(password !== null && password !== void 0 ? password : '');
- }
- catch (err) {
- throw new MongoParseError(err.message);
- }
- const illegalCharacters = new RegExp(String.raw `[:/?#\[\]@]`, 'gi');
- if (username === null || username === void 0 ? void 0 : username.match(illegalCharacters)) {
- throw new MongoParseError(`Username contains unescaped characters ${username}`);
- }
- if (!username || !password) {
- const uriWithoutProtocol = uri.replace(`${protocol}://`, '');
- if (uriWithoutProtocol.startsWith('@') || uriWithoutProtocol.startsWith(':')) {
- throw new MongoParseError('URI contained empty userinfo section');
- }
- }
- if (password === null || password === void 0 ? void 0 : password.match(illegalCharacters)) {
- throw new MongoParseError('Password contains unescaped characters');
- }
- let authString = '';
- if (typeof username === 'string')
- authString += username;
- if (typeof password === 'string')
- authString += `:${password}`;
- if (authString)
- authString += '@';
- super(`${protocol.toLowerCase()}://${authString}${DUMMY_HOSTNAME}${rest}`);
- this._hosts = hosts.split(',');
- if (this.isSRV && this.hosts.length !== 1) {
- throw new MongoParseError('mongodb+srv URI cannot have multiple service names');
- }
- if (this.isSRV && this.hosts.some(host => host.includes(':'))) {
- throw new MongoParseError('mongodb+srv URI cannot have port number');
- }
- if (!this.pathname) {
- this.pathname = '/';
- }
- Object.setPrototypeOf(this.searchParams, caseInsenstiveURLSearchParams(this.searchParams.constructor).prototype);
- }
- get host() { return DUMMY_HOSTNAME; }
- set host(_ignored) { throw new Error('No single host for connection string'); }
- get hostname() { return DUMMY_HOSTNAME; }
- set hostname(_ignored) { throw new Error('No single host for connection string'); }
- get port() { return ''; }
- set port(_ignored) { throw new Error('No single host for connection string'); }
- get href() { return this.toString(); }
- set href(_ignored) { throw new Error('Cannot set href for connection strings'); }
- get isSRV() {
- return this.protocol.includes('srv');
- }
- get hosts() {
- return this._hosts;
- }
- set hosts(list) {
- this._hosts = list;
- }
- toString() {
- return super.toString().replace(DUMMY_HOSTNAME, this.hosts.join(','));
- }
- clone() {
- return new ConnectionString(this.toString());
- }
- redact(options) {
- return (0, redact_1.redactValidConnectionString)(this, options);
- }
- typedSearchParams() {
- const sametype = false && new (caseInsenstiveURLSearchParams(whatwg_url_1.URLSearchParams))();
- return this.searchParams;
- }
- [Symbol.for('nodejs.util.inspect.custom')]() {
- const { href, origin, protocol, username, password, hosts, pathname, search, searchParams, hash } = this;
- return { href, origin, protocol, username, password, hosts, pathname, search, searchParams, hash };
- }
- }
- exports.default = ConnectionString;
- class CommaAndColonSeparatedRecord extends CaseInsensitiveMap {
- constructor(from) {
- super();
- for (const entry of (from !== null && from !== void 0 ? from : '').split(',')) {
- if (!entry)
- continue;
- const colonIndex = entry.indexOf(':');
- if (colonIndex === -1) {
- this.set(entry, '');
- }
- else {
- this.set(entry.slice(0, colonIndex), entry.slice(colonIndex + 1));
- }
- }
- }
- toString() {
- return [...this].map(entry => entry.join(':')).join(',');
- }
- }
- exports.CommaAndColonSeparatedRecord = CommaAndColonSeparatedRecord;
- //# sourceMappingURL=index.js.map
|