Skip to content

Commit 6e67fe6

Browse files
committed
Remove uri-js dependency
1 parent 1a49d33 commit 6e67fe6

File tree

7 files changed

+138
-31
lines changed

7 files changed

+138
-31
lines changed

core/package-lock.json

+5-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/package.json

-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
"url": "https://github.com/neo4j/neo4j-javascript-driver/issues"
2727
},
2828
"homepage": "https://github.com/neo4j/neo4j-javascript-driver#readme",
29-
"dependencies": {
30-
"uri-js": "^4.4.1"
31-
},
3229
"devDependencies": {
3330
"@types/jest": "^26.0.20",
3431
"esdoc": "^1.1.0",

core/src/internal/url-util.ts

+107-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
* limitations under the License.
1818
*/
1919

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

2322
const DEFAULT_BOLT_PORT = 7687
@@ -76,6 +75,17 @@ class Url {
7675
}
7776
}
7877

78+
interface ParsedUri {
79+
scheme?: string
80+
host?: string
81+
port?: number | string
82+
query?: string
83+
fragment?: string
84+
userInfo?: string
85+
authority?: string
86+
path?: string
87+
}
88+
7989
function parseDatabaseUrl(url: string) {
8090
assertString(url, 'URL')
8191

@@ -230,9 +240,104 @@ function defaultPortForScheme(scheme: string | null): number {
230240
}
231241
}
232242

243+
function uriJsParse(value: string) {
244+
// JS version of Python partition function
245+
function partition(s: string, delimiter: string): [string, string, string] {
246+
const i = s.indexOf(delimiter)
247+
if (i >= 0) return [s.substring(0, i), s[i], s.substring(i + 1)]
248+
else return [s, '', '']
249+
}
250+
251+
// JS version of Python rpartition function
252+
function rpartition(s: string, delimiter: string): [string, string, string] {
253+
const i = s.lastIndexOf(delimiter)
254+
if (i >= 0) return [s.substring(0, i), s[i], s.substring(i + 1)]
255+
else return ['', '', s]
256+
}
257+
258+
function between(
259+
s: string,
260+
ldelimiter: string,
261+
rdelimiter: string
262+
): [string, string] {
263+
const lpartition = partition(s, ldelimiter)
264+
const rpartition = partition(lpartition[2], rdelimiter)
265+
return [rpartition[0], rpartition[2]]
266+
}
267+
268+
// Parse an authority string into an object
269+
// with the following keys:
270+
// - userInfo (optional, might contain both user name and password)
271+
// - host
272+
// - port (optional, included only as a string)
273+
function parseAuthority(value: string): ParsedUri {
274+
let parsed: ParsedUri = {},
275+
parts: [string, string, string]
276+
277+
// Parse user info
278+
parts = rpartition(value, '@')
279+
if (parts[1] === '@') {
280+
parsed.userInfo = decodeURIComponent(parts[0])
281+
value = parts[2]
282+
}
283+
284+
// Parse host and port
285+
const [ipv6Host, rest] = between(value, `[`, `]`)
286+
if (ipv6Host !== '') {
287+
parsed.host = ipv6Host
288+
parts = partition(rest, ':')
289+
} else {
290+
parts = partition(value, ':')
291+
parsed.host = parts[0]
292+
}
293+
294+
if (parts[1] === ':') {
295+
parsed.port = parts[2]
296+
}
297+
298+
return parsed
299+
}
300+
301+
let parsed: ParsedUri = {},
302+
parts: string[]
303+
304+
// Parse scheme
305+
parts = partition(value, ':')
306+
if (parts[1] === ':') {
307+
parsed.scheme = decodeURIComponent(parts[0])
308+
value = parts[2]
309+
}
310+
311+
// Parse fragment
312+
parts = partition(value, '#')
313+
if (parts[1] === '#') {
314+
parsed.fragment = decodeURIComponent(parts[2])
315+
value = parts[0]
316+
}
317+
318+
// Parse query
319+
parts = partition(value, '?')
320+
if (parts[1] === '?') {
321+
parsed.query = parts[2]
322+
value = parts[0]
323+
}
324+
325+
// Parse authority and path
326+
if (value.startsWith('//')) {
327+
parts = partition(value.substr(2), '/')
328+
parsed = { ...parsed, ...parseAuthority(parts[0]) }
329+
parsed.path = parts[1] + parts[2]
330+
} else {
331+
parsed.path = value
332+
}
333+
334+
return parsed
335+
}
336+
233337
export {
234338
parseDatabaseUrl,
235339
defaultPortForScheme,
236340
formatIPv4Address,
237-
formatIPv6Address
341+
formatIPv6Address,
342+
Url
238343
}

test/internal/url-util.test.js renamed to core/test/internal/url-util.test.ts

+16-7
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@
1717
* limitations under the License.
1818
*/
1919

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

22-
const { urlUtil } = internal
22+
interface PartialUrl {
23+
readonly scheme?: string | null
24+
readonly host?: string
25+
readonly port?: number
26+
readonly hostAndPort?: string
27+
readonly query?: Object
28+
readonly ipv6?: boolean
29+
}
2330

2431
describe('#unit url-util', () => {
2532
it('should parse URL with just host name', () => {
@@ -785,7 +792,7 @@ describe('#unit url-util', () => {
785792
})
786793
})
787794

788-
function verifyUrl (urlString, expectedUrl) {
795+
function verifyUrl(urlString: string, expectedUrl: PartialUrl) {
789796
const url = parse(urlString)
790797
if (expectedUrl.scheme) {
791798
expect(url.scheme).toEqual(expectedUrl.scheme)
@@ -800,7 +807,9 @@ describe('#unit url-util', () => {
800807
if (expectedUrl.port) {
801808
expect(url.port).toEqual(expectedUrl.port)
802809
} else {
803-
expect(url.port).toEqual(urlUtil.defaultPortForScheme(expectedUrl.scheme))
810+
expect(url.port).toEqual(
811+
urlUtil.defaultPortForScheme(expectedUrl.scheme!!)
812+
)
804813
}
805814

806815
verifyHostAndPort(url, expectedUrl)
@@ -811,11 +820,11 @@ describe('#unit url-util', () => {
811820
}
812821
}
813822

814-
function verifyHostAndPort (url, expectedUrl) {
823+
function verifyHostAndPort(url: urlUtil.Url, expectedUrl: PartialUrl) {
815824
const port =
816825
expectedUrl.port === 0 || expectedUrl.port
817826
? expectedUrl.port
818-
: urlUtil.defaultPortForScheme(expectedUrl.scheme)
827+
: urlUtil.defaultPortForScheme(expectedUrl.scheme!!)
819828

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

827-
function parse (url) {
836+
function parse(url: any): urlUtil.Url {
828837
return urlUtil.parseDatabaseUrl(url)
829838
}
830839
})

neo4j-driver-lite/package-lock.json

+1-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@
111111
"neo4j-driver-bolt-connection": "file:bolt-connection",
112112
"neo4j-driver-core": "file:core",
113113
"rxjs": "^6.6.3",
114-
"text-encoding-utf-8": "^1.0.2",
115-
"uri-js": "^4.4.0"
114+
"text-encoding-utf-8": "^1.0.2"
116115
},
117116
"bundledDependencies": [
118117
"neo4j-driver-core",

0 commit comments

Comments
 (0)