Spaces:
Paused
Paused
File size: 8,159 Bytes
d54c8f8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | "use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommaAndColonSeparatedRecord = exports.ConnectionString = 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 = /^(?<protocol>[^/]+):\/\/(?:(?<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, options = {}) {
var _a;
const { looseValidation } = options;
if (!looseValidation && !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 (!looseValidation) {
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 = /[:/?#[\]@]/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 += '@';
try {
super(`${protocol.toLowerCase()}://${authString}${DUMMY_HOSTNAME}${rest}`);
}
catch (err) {
if (looseValidation) {
new ConnectionString(uri, {
...options,
looseValidation: false
});
}
if (typeof err.message === 'string') {
err.message = err.message.replace(DUMMY_HOSTNAME, hosts);
}
throw err;
}
this._hosts = hosts.split(',');
if (!looseValidation) {
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(), {
looseValidation: true
});
}
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.ConnectionString = 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;
exports.default = ConnectionString;
//# sourceMappingURL=index.js.map |