Skip to content

Commit

Permalink
update mongodb uri
Browse files Browse the repository at this point in the history
  • Loading branch information
renzholy committed Nov 17, 2022
1 parent bab143f commit 2d0cd75
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 14 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"deepmerge": "4.2.2",
"lodash-es": "4.17.21",
"markdown-table": "3.0.2",
"mongodb-uri": "0.9.7",
"next": "13.0.3",
"pretty-ms": "8.0.0",
"react": "18.2.0",
Expand All @@ -45,7 +44,6 @@
"devDependencies": {
"@types/bytes": "3.1.1",
"@types/lodash-es": "4.17.6",
"@types/mongodb-uri": "0.9.1",
"@types/node": "18.11.9",
"@types/react": "18.0.25",
"@types/react-dom": "18.0.9",
Expand Down
2 changes: 1 addition & 1 deletion src/components/connection-edit-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
DirectionalHint,
} from '@fluentui/react'
import { useMemo, useCallback, useState, useEffect } from 'react'
import mongodbUri from 'mongodb-uri'
import { compact, uniqBy } from 'lodash-es'
import useSWR from 'swr'
import { runCommand } from 'utils/fetcher'
Expand All @@ -19,6 +18,7 @@ import { useConnection, useConnections } from 'hooks/use-connections'
import usePromise from 'hooks/use-promise'
import useRouterQuery from 'hooks/use-router-query'
import PromiseButton from './pure/promise-button'
import { mongodbUri } from 'utils/mongo-uri'

function ConnectionItem(props: { connection: string; disabled?: boolean }) {
const uri = useMemo(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mongodbUri from 'mongodb-uri'
import { mongodbUri } from './mongo-uri'

export function generateConnectionWithDirectHost(
host: string,
Expand Down
151 changes: 151 additions & 0 deletions src/utils/mongo-uri.ts
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({})
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 2d0cd75

Please sign in to comment.