Skip to content

Remove uri-js dependency #694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
"url": "https://github.com/neo4j/neo4j-javascript-driver/issues"
},
"homepage": "https://github.com/neo4j/neo4j-javascript-driver#readme",
"dependencies": {
"uri-js": "^4.4.1"
},
"devDependencies": {
"@types/jest": "^26.0.20",
"esdoc": "^1.1.0",
Expand Down
109 changes: 107 additions & 2 deletions core/src/internal/url-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* limitations under the License.
*/

import { parse as uriJsParse } from 'uri-js'
import { assertString } from './util'

const DEFAULT_BOLT_PORT = 7687
Expand Down Expand Up @@ -76,6 +75,17 @@ class Url {
}
}

interface ParsedUri {
scheme?: string
host?: string
port?: number | string
query?: string
fragment?: string
userInfo?: string
authority?: string
path?: string
}

function parseDatabaseUrl(url: string) {
assertString(url, 'URL')

Expand Down Expand Up @@ -230,9 +240,104 @@ function defaultPortForScheme(scheme: string | null): number {
}
}

function uriJsParse(value: string) {
// JS version of Python partition function
function partition(s: string, delimiter: string): [string, string, string] {
const i = s.indexOf(delimiter)
if (i >= 0) return [s.substring(0, i), s[i], s.substring(i + 1)]
else return [s, '', '']
}

// JS version of Python rpartition function
function rpartition(s: string, delimiter: string): [string, string, string] {
const i = s.lastIndexOf(delimiter)
if (i >= 0) return [s.substring(0, i), s[i], s.substring(i + 1)]
else return ['', '', s]
}

function between(
s: string,
ldelimiter: string,
rdelimiter: string
): [string, string] {
const lpartition = partition(s, ldelimiter)
const rpartition = partition(lpartition[2], rdelimiter)
return [rpartition[0], rpartition[2]]
}

// Parse an authority string into an object
// with the following keys:
// - userInfo (optional, might contain both user name and password)
// - host
// - port (optional, included only as a string)
function parseAuthority(value: string): ParsedUri {
let parsed: ParsedUri = {},
parts: [string, string, string]

// Parse user info
parts = rpartition(value, '@')
if (parts[1] === '@') {
parsed.userInfo = decodeURIComponent(parts[0])
value = parts[2]
}

// Parse host and port
const [ipv6Host, rest] = between(value, `[`, `]`)
if (ipv6Host !== '') {
parsed.host = ipv6Host
parts = partition(rest, ':')
} else {
parts = partition(value, ':')
parsed.host = parts[0]
}

if (parts[1] === ':') {
parsed.port = parts[2]
}

return parsed
}

let parsed: ParsedUri = {},
parts: string[]

// Parse scheme
parts = partition(value, ':')
if (parts[1] === ':') {
parsed.scheme = decodeURIComponent(parts[0])
value = parts[2]
}

// Parse fragment
parts = partition(value, '#')
if (parts[1] === '#') {
parsed.fragment = decodeURIComponent(parts[2])
value = parts[0]
}

// Parse query
parts = partition(value, '?')
if (parts[1] === '?') {
parsed.query = parts[2]
value = parts[0]
}

// Parse authority and path
if (value.startsWith('//')) {
parts = partition(value.substr(2), '/')
parsed = { ...parsed, ...parseAuthority(parts[0]) }
parsed.path = parts[1] + parts[2]
} else {
parsed.path = value
}

return parsed
}

export {
parseDatabaseUrl,
defaultPortForScheme,
formatIPv4Address,
formatIPv6Address
formatIPv6Address,
Url
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@
* limitations under the License.
*/

import { internal } from 'neo4j-driver-core'
import * as urlUtil from '../../src/internal/url-util'

const { urlUtil } = internal
interface PartialUrl {
readonly scheme?: string | null
readonly host?: string
readonly port?: number
readonly hostAndPort?: string
readonly query?: Object
readonly ipv6?: boolean
}

describe('#unit url-util', () => {
it('should parse URL with just host name', () => {
Expand Down Expand Up @@ -785,7 +792,7 @@ describe('#unit url-util', () => {
})
})

function verifyUrl (urlString, expectedUrl) {
function verifyUrl(urlString: string, expectedUrl: PartialUrl) {
const url = parse(urlString)
if (expectedUrl.scheme) {
expect(url.scheme).toEqual(expectedUrl.scheme)
Expand All @@ -800,7 +807,9 @@ describe('#unit url-util', () => {
if (expectedUrl.port) {
expect(url.port).toEqual(expectedUrl.port)
} else {
expect(url.port).toEqual(urlUtil.defaultPortForScheme(expectedUrl.scheme))
expect(url.port).toEqual(
urlUtil.defaultPortForScheme(expectedUrl.scheme!!)
)
}

verifyHostAndPort(url, expectedUrl)
Expand All @@ -811,11 +820,11 @@ describe('#unit url-util', () => {
}
}

function verifyHostAndPort (url, expectedUrl) {
function verifyHostAndPort(url: urlUtil.Url, expectedUrl: PartialUrl) {
const port =
expectedUrl.port === 0 || expectedUrl.port
? expectedUrl.port
: urlUtil.defaultPortForScheme(expectedUrl.scheme)
: urlUtil.defaultPortForScheme(expectedUrl.scheme!!)

if (expectedUrl.ipv6) {
expect(url.hostAndPort).toEqual(`[${expectedUrl.host}]:${port}`)
Expand All @@ -824,7 +833,7 @@ describe('#unit url-util', () => {
}
}

function parse (url) {
function parse(url: any): urlUtil.Url {
return urlUtil.parseDatabaseUrl(url)
}
})
6 changes: 1 addition & 5 deletions neo4j-driver-lite/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@
"neo4j-driver-bolt-connection": "file:bolt-connection",
"neo4j-driver-core": "file:core",
"rxjs": "^6.6.3",
"text-encoding-utf-8": "^1.0.2",
"uri-js": "^4.4.0"
"text-encoding-utf-8": "^1.0.2"
},
"bundledDependencies": [
"neo4j-driver-core",
Expand Down