-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
153 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
type Host = { | ||
host: string | ||
port?: number | undefined | ||
} | ||
|
||
type ParserOptions = { | ||
scheme?: string | ||
} | ||
|
||
type UriObject = { | ||
scheme?: string | ||
hosts: Host[] | ||
|
||
username?: string | undefined | ||
password?: string | undefined | ||
database?: string | undefined | ||
options?: any | ||
} | ||
|
||
class MongodbUriParser { | ||
scheme?: string | ||
|
||
constructor(options: ParserOptions) { | ||
if (options && options.scheme) { | ||
this.scheme = options.scheme | ||
} | ||
} | ||
|
||
parse(uri: string): UriObject { | ||
const uriObject: UriObject = { hosts: [] } | ||
|
||
let i = uri.indexOf('://') | ||
if (i < 0) { | ||
throw new Error('No scheme found in URI ' + uri) | ||
} | ||
uriObject.scheme = uri.substring(0, i) | ||
if (this.scheme && this.scheme !== uriObject.scheme) { | ||
throw new Error('URI must begin with ' + this.scheme + '://') | ||
} | ||
let rest = uri.substring(i + 3) | ||
|
||
i = rest.indexOf('@') | ||
if (i >= 0) { | ||
const credentials = rest.substring(0, i) | ||
rest = rest.substring(i + 1) | ||
i = credentials.indexOf(':') | ||
if (i >= 0) { | ||
uriObject.username = decodeURIComponent(credentials.substring(0, i)) | ||
uriObject.password = decodeURIComponent(credentials.substring(i + 1)) | ||
} else { | ||
uriObject.username = decodeURIComponent(credentials) | ||
} | ||
} | ||
|
||
i = rest.indexOf('?') | ||
if (i >= 0) { | ||
const options = rest.substring(i + 1) | ||
rest = rest.substring(0, i) | ||
uriObject.options = {} | ||
options.split('&').forEach((o) => { | ||
const iEquals = o.indexOf('=') | ||
uriObject.options[decodeURIComponent(o.substring(0, iEquals))] = | ||
decodeURIComponent(o.substring(iEquals + 1)) | ||
}) | ||
} | ||
|
||
i = rest.indexOf('/') | ||
if (i >= 0) { | ||
// Make sure the database name isn't the empty string | ||
if (i < rest.length - 1) { | ||
uriObject.database = decodeURIComponent(rest.substring(i + 1)) | ||
} | ||
rest = rest.substring(0, i) | ||
} | ||
|
||
this._parseAddress(rest, uriObject) | ||
|
||
return uriObject as UriObject | ||
} | ||
|
||
_parseAddress(address: string, uriObject: UriObject) { | ||
uriObject.hosts = [] | ||
address.split(',').forEach((h) => { | ||
const i = h.indexOf(':') | ||
if (i >= 0) { | ||
uriObject.hosts.push({ | ||
host: decodeURIComponent(h.substring(0, i)), | ||
port: parseInt(h.substring(i + 1)), | ||
}) | ||
} else { | ||
uriObject.hosts.push({ host: decodeURIComponent(h) }) | ||
} | ||
}) | ||
} | ||
|
||
format(uriObject: UriObject) { | ||
if (!uriObject) { | ||
return (this.scheme || 'mongodb') + '://localhost' | ||
} | ||
|
||
if (this.scheme && uriObject.scheme && this.scheme !== uriObject.scheme) { | ||
throw new Error('Scheme not supported: ' + uriObject.scheme) | ||
} | ||
let uri = (this.scheme || uriObject.scheme || 'mongodb') + '://' | ||
|
||
if (uriObject.username) { | ||
uri += encodeURIComponent(uriObject.username) | ||
// While it's not to the official spec, we allow empty passwords | ||
if (uriObject.password) { | ||
uri += ':' + encodeURIComponent(uriObject.password) | ||
} | ||
uri += '@' | ||
} | ||
|
||
uri += this._formatAddress(uriObject) | ||
|
||
// While it's not to the official spec, we only put a slash if there's a database, independent of whether there are options | ||
if (uriObject.database) { | ||
uri += '/' + encodeURIComponent(uriObject.database) | ||
} | ||
|
||
if (uriObject.options) { | ||
Object.keys(uriObject.options).forEach((k, i) => { | ||
uri = uri + (i === 0 ? '?' : '&') | ||
uri = | ||
uri + | ||
encodeURIComponent(k) + | ||
'=' + | ||
encodeURIComponent(uriObject.options[k]) | ||
}) | ||
} | ||
|
||
return uri | ||
} | ||
|
||
_formatAddress(uriObject: UriObject) { | ||
let address = '' | ||
uriObject.hosts.forEach((h, i) => { | ||
if (i > 0) { | ||
address += ',' | ||
} | ||
address += encodeURIComponent(h.host) | ||
if (h.port) { | ||
address += ':' + encodeURIComponent(h.port) | ||
} | ||
}) | ||
return address | ||
} | ||
} | ||
|
||
export const mongodbUri = new MongodbUriParser({}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1099,11 +1099,6 @@ | |
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.189.tgz#975ff8c38da5ae58b751127b19ad5e44b5b7f6d2" | ||
integrity sha512-kb9/98N6X8gyME9Cf7YaqIMvYGnBSWqEci6tiettE6iJWH1XdJz/PO8LB0GtLCG7x8dU3KWhZT+lA1a35127tA== | ||
|
||
"@types/[email protected]": | ||
version "0.9.1" | ||
resolved "https://registry.yarnpkg.com/@types/mongodb-uri/-/mongodb-uri-0.9.1.tgz#7860f479e1f85d5e37e7f335f61ee9b65f564d01" | ||
integrity sha512-Rxyje0yj8zI8GX4qKtroVpidJglBLZBjZG9QLizHdymZmvmVZdOkseje7Qrk9Kpbjopis9ZAvMnkEArQYV0ZaA== | ||
|
||
"@types/node@*", "@types/[email protected]": | ||
version "18.11.9" | ||
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.11.9.tgz#02d013de7058cea16d36168ef2fc653464cfbad4" | ||
|
@@ -2295,11 +2290,6 @@ mongodb-connection-string-url@^2.5.4: | |
"@types/whatwg-url" "^8.2.1" | ||
whatwg-url "^11.0.0" | ||
|
||
[email protected]: | ||
version "0.9.7" | ||
resolved "https://registry.yarnpkg.com/mongodb-uri/-/mongodb-uri-0.9.7.tgz#0f771ad16f483ae65f4287969428e9fbc4aa6181" | ||
integrity sha512-s6BdnqNoEYfViPJgkH85X5Nw5NpzxN8hoflKLweNa7vBxt2V7kaS06d74pAtqDxde8fn4r9h4dNdLiFGoNV0KA== | ||
|
||
[email protected]: | ||
version "4.11.0" | ||
resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-4.11.0.tgz#d28fdc7509f24d0d274f456529441fa3e570415c" | ||
|