index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CommaAndColonSeparatedRecord = exports.redactConnectionString = void 0;
  4. const whatwg_url_1 = require("whatwg-url");
  5. const redact_1 = require("./redact");
  6. Object.defineProperty(exports, "redactConnectionString", { enumerable: true, get: function () { return redact_1.redactConnectionString; } });
  7. const DUMMY_HOSTNAME = '__this_is_a_placeholder__';
  8. function connectionStringHasValidScheme(connectionString) {
  9. return (connectionString.startsWith('mongodb://') ||
  10. connectionString.startsWith('mongodb+srv://'));
  11. }
  12. const HOSTS_REGEX = new RegExp(String.raw `^(?<protocol>mongodb(?:\+srv|)):\/\/(?:(?<username>[^:]*)(?::(?<password>[^@]*))?@)?(?<hosts>(?!:)[^\/?@]*)(?<rest>.*)`);
  13. class 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. }
  37. function 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. }
  74. class URLWithoutHost extends whatwg_url_1.URL {
  75. }
  76. class MongoParseError extends Error {
  77. get name() {
  78. return 'MongoParseError';
  79. }
  80. }
  81. class ConnectionString extends URLWithoutHost {
  82. constructor(uri) {
  83. var _a;
  84. if (!connectionStringHasValidScheme(uri)) {
  85. throw new MongoParseError('Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"');
  86. }
  87. const match = uri.match(HOSTS_REGEX);
  88. if (!match) {
  89. throw new MongoParseError(`Invalid connection string "${uri}"`);
  90. }
  91. const { protocol, username, password, hosts, rest } = (_a = match.groups) !== null && _a !== void 0 ? _a : {};
  92. if (!protocol || !hosts) {
  93. throw new MongoParseError(`Protocol and host list are required in "${uri}"`);
  94. }
  95. try {
  96. decodeURIComponent(username !== null && username !== void 0 ? username : '');
  97. decodeURIComponent(password !== null && password !== void 0 ? password : '');
  98. }
  99. catch (err) {
  100. throw new MongoParseError(err.message);
  101. }
  102. const illegalCharacters = new RegExp(String.raw `[:/?#\[\]@]`, 'gi');
  103. if (username === null || username === void 0 ? void 0 : username.match(illegalCharacters)) {
  104. throw new MongoParseError(`Username contains unescaped characters ${username}`);
  105. }
  106. if (!username || !password) {
  107. const uriWithoutProtocol = uri.replace(`${protocol}://`, '');
  108. if (uriWithoutProtocol.startsWith('@') || uriWithoutProtocol.startsWith(':')) {
  109. throw new MongoParseError('URI contained empty userinfo section');
  110. }
  111. }
  112. if (password === null || password === void 0 ? void 0 : password.match(illegalCharacters)) {
  113. throw new MongoParseError('Password contains unescaped characters');
  114. }
  115. let authString = '';
  116. if (typeof username === 'string')
  117. authString += username;
  118. if (typeof password === 'string')
  119. authString += `:${password}`;
  120. if (authString)
  121. authString += '@';
  122. super(`${protocol.toLowerCase()}://${authString}${DUMMY_HOSTNAME}${rest}`);
  123. this._hosts = hosts.split(',');
  124. if (this.isSRV && this.hosts.length !== 1) {
  125. throw new MongoParseError('mongodb+srv URI cannot have multiple service names');
  126. }
  127. if (this.isSRV && this.hosts.some(host => host.includes(':'))) {
  128. throw new MongoParseError('mongodb+srv URI cannot have port number');
  129. }
  130. if (!this.pathname) {
  131. this.pathname = '/';
  132. }
  133. Object.setPrototypeOf(this.searchParams, caseInsenstiveURLSearchParams(this.searchParams.constructor).prototype);
  134. }
  135. get host() { return DUMMY_HOSTNAME; }
  136. set host(_ignored) { throw new Error('No single host for connection string'); }
  137. get hostname() { return DUMMY_HOSTNAME; }
  138. set hostname(_ignored) { throw new Error('No single host for connection string'); }
  139. get port() { return ''; }
  140. set port(_ignored) { throw new Error('No single host for connection string'); }
  141. get href() { return this.toString(); }
  142. set href(_ignored) { throw new Error('Cannot set href for connection strings'); }
  143. get isSRV() {
  144. return this.protocol.includes('srv');
  145. }
  146. get hosts() {
  147. return this._hosts;
  148. }
  149. set hosts(list) {
  150. this._hosts = list;
  151. }
  152. toString() {
  153. return super.toString().replace(DUMMY_HOSTNAME, this.hosts.join(','));
  154. }
  155. clone() {
  156. return new ConnectionString(this.toString());
  157. }
  158. redact(options) {
  159. return (0, redact_1.redactValidConnectionString)(this, options);
  160. }
  161. typedSearchParams() {
  162. const sametype = false && new (caseInsenstiveURLSearchParams(whatwg_url_1.URLSearchParams))();
  163. return this.searchParams;
  164. }
  165. [Symbol.for('nodejs.util.inspect.custom')]() {
  166. const { href, origin, protocol, username, password, hosts, pathname, search, searchParams, hash } = this;
  167. return { href, origin, protocol, username, password, hosts, pathname, search, searchParams, hash };
  168. }
  169. }
  170. exports.default = ConnectionString;
  171. class CommaAndColonSeparatedRecord extends CaseInsensitiveMap {
  172. constructor(from) {
  173. super();
  174. for (const entry of (from !== null && from !== void 0 ? from : '').split(',')) {
  175. if (!entry)
  176. continue;
  177. const colonIndex = entry.indexOf(':');
  178. if (colonIndex === -1) {
  179. this.set(entry, '');
  180. }
  181. else {
  182. this.set(entry.slice(0, colonIndex), entry.slice(colonIndex + 1));
  183. }
  184. }
  185. }
  186. toString() {
  187. return [...this].map(entry => entry.join(':')).join(',');
  188. }
  189. }
  190. exports.CommaAndColonSeparatedRecord = CommaAndColonSeparatedRecord;
  191. //# sourceMappingURL=index.js.map