CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
index.js213 linesDownload Raw Back to lib
1"use strict";2Object.defineProperty(exports, "__esModule", { value: true });3exports.CommaAndColonSeparatedRecord = exports.ConnectionString = exports.redactConnectionString = void 0;4const whatwg_url_1 = require("whatwg-url");5const redact_1 = require("./redact");6Object.defineProperty(exports, "redactConnectionString", { enumerable: true, get: function () { return redact_1.redactConnectionString; } });7const DUMMY_HOSTNAME = '__this_is_a_placeholder__';8function connectionStringHasValidScheme(connectionString) {9    return (connectionString.startsWith('mongodb://') ||10        connectionString.startsWith('mongodb+srv://'));11}12const HOSTS_REGEX = /^(?<protocol>[^/]+):\/\/(?:(?<username>[^:@]*)(?::(?<password>[^@]*))?@)?(?<hosts>(?!:)[^/?@]*)(?<rest>.*)/;13class CaseInsensitiveMap extends Map {14    delete(name) {15        return super.delete(this._normalizeKey(name));16    }17    get(name) {18        return super.get(this._normalizeKey(name));19    }20    has(name) {21        return super.has(this._normalizeKey(name));22    }23    set(name, value) {24        return super.set(this._normalizeKey(name), value);25    }26    _normalizeKey(name) {27        name = `${name}`;28        for (const key of this.keys()) {29            if (key.toLowerCase() === name.toLowerCase()) {30                name = key;31                break;32            }33        }34        return name;35    }36}37function caseInsenstiveURLSearchParams(Ctor) {38    return class CaseInsenstiveURLSearchParams extends Ctor {39        append(name, value) {40            return super.append(this._normalizeKey(name), value);41        }42        delete(name) {43            return super.delete(this._normalizeKey(name));44        }45        get(name) {46            return super.get(this._normalizeKey(name));47        }48        getAll(name) {49            return super.getAll(this._normalizeKey(name));50        }51        has(name) {52            return super.has(this._normalizeKey(name));53        }54        set(name, value) {55            return super.set(this._normalizeKey(name), value);56        }57        keys() {58            return super.keys();59        }60        values() {61            return super.values();62        }63        entries() {64            return super.entries();65        }66        [Symbol.iterator]() {67            return super[Symbol.iterator]();68        }69        _normalizeKey(name) {70            return CaseInsensitiveMap.prototype._normalizeKey.call(this, name);71        }72    };73}74class URLWithoutHost extends whatwg_url_1.URL {75}76class MongoParseError extends Error {77    get name() {78        return 'MongoParseError';79    }80}81class ConnectionString extends URLWithoutHost {82    constructor(uri, options = {}) {83        var _a;84        const { looseValidation } = options;85        if (!looseValidation && !connectionStringHasValidScheme(uri)) {86            throw new MongoParseError('Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"');87        }88        const match = uri.match(HOSTS_REGEX);89        if (!match) {90            throw new MongoParseError(`Invalid connection string "${uri}"`);91        }92        const { protocol, username, password, hosts, rest } = (_a = match.groups) !== null && _a !== void 0 ? _a : {};93        if (!looseValidation) {94            if (!protocol || !hosts) {95                throw new MongoParseError(`Protocol and host list are required in "${uri}"`);96            }97            try {98                decodeURIComponent(username !== null && username !== void 0 ? username : '');99                decodeURIComponent(password !== null && password !== void 0 ? password : '');100            }101            catch (err) {102                throw new MongoParseError(err.message);103            }104            const illegalCharacters = /[:/?#[\]@]/gi;105            if (username === null || username === void 0 ? void 0 : username.match(illegalCharacters)) {106                throw new MongoParseError(`Username contains unescaped characters ${username}`);107            }108            if (!username || !password) {109                const uriWithoutProtocol = uri.replace(`${protocol}://`, '');110                if (uriWithoutProtocol.startsWith('@') || uriWithoutProtocol.startsWith(':')) {111                    throw new MongoParseError('URI contained empty userinfo section');112                }113            }114            if (password === null || password === void 0 ? void 0 : password.match(illegalCharacters)) {115                throw new MongoParseError('Password contains unescaped characters');116            }117        }118        let authString = '';119        if (typeof username === 'string')120            authString += username;121        if (typeof password === 'string')122            authString += `:${password}`;123        if (authString)124            authString += '@';125        try {126            super(`${protocol.toLowerCase()}://${authString}${DUMMY_HOSTNAME}${rest}`);127        }128        catch (err) {129            if (looseValidation) {130                new ConnectionString(uri, {131                    ...options,132                    looseValidation: false133                });134            }135            if (typeof err.message === 'string') {136                err.message = err.message.replace(DUMMY_HOSTNAME, hosts);137            }138            throw err;139        }140        this._hosts = hosts.split(',');141        if (!looseValidation) {142            if (this.isSRV && this.hosts.length !== 1) {143                throw new MongoParseError('mongodb+srv URI cannot have multiple service names');144            }145            if (this.isSRV && this.hosts.some(host => host.includes(':'))) {146                throw new MongoParseError('mongodb+srv URI cannot have port number');147            }148        }149        if (!this.pathname) {150            this.pathname = '/';151        }152        Object.setPrototypeOf(this.searchParams, caseInsenstiveURLSearchParams(this.searchParams.constructor).prototype);153    }154    get host() { return DUMMY_HOSTNAME; }155    set host(_ignored) { throw new Error('No single host for connection string'); }156    get hostname() { return DUMMY_HOSTNAME; }157    set hostname(_ignored) { throw new Error('No single host for connection string'); }158    get port() { return ''; }159    set port(_ignored) { throw new Error('No single host for connection string'); }160    get href() { return this.toString(); }161    set href(_ignored) { throw new Error('Cannot set href for connection strings'); }162    get isSRV() {163        return this.protocol.includes('srv');164    }165    get hosts() {166        return this._hosts;167    }168    set hosts(list) {169        this._hosts = list;170    }171    toString() {172        return super.toString().replace(DUMMY_HOSTNAME, this.hosts.join(','));173    }174    clone() {175        return new ConnectionString(this.toString(), {176            looseValidation: true177        });178    }179    redact(options) {180        return (0, redact_1.redactValidConnectionString)(this, options);181    }182    typedSearchParams() {183        const sametype = false && new (caseInsenstiveURLSearchParams(whatwg_url_1.URLSearchParams))();184        return this.searchParams;185    }186    [Symbol.for('nodejs.util.inspect.custom')]() {187        const { href, origin, protocol, username, password, hosts, pathname, search, searchParams, hash } = this;188        return { href, origin, protocol, username, password, hosts, pathname, search, searchParams, hash };189    }190}191exports.ConnectionString = ConnectionString;192class CommaAndColonSeparatedRecord extends CaseInsensitiveMap {193    constructor(from) {194        super();195        for (const entry of (from !== null && from !== void 0 ? from : '').split(',')) {196            if (!entry)197                continue;198            const colonIndex = entry.indexOf(':');199            if (colonIndex === -1) {200                this.set(entry, '');201            }202            else {203                this.set(entry.slice(0, colonIndex), entry.slice(colonIndex + 1));204            }205        }206    }207    toString() {208        return [...this].map(entry => entry.join(':')).join(',');209    }210}211exports.CommaAndColonSeparatedRecord = CommaAndColonSeparatedRecord;212exports.default = ConnectionString;213//# sourceMappingURL=index.js.map