fast-uri 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +2 -0
- package/README.md +2 -2
- package/index.js +17 -30
- package/lib/utils.js +2 -2
- package/package.json +3 -4
- package/test/equal.test.js +55 -22
- package/types/index.d.ts +53 -0
- package/types/index.test-d.ts +17 -0
- package/index.d.ts +0 -27
- package/index.test-d.ts +0 -11
- package/types/.gitkeep +0 -0
package/.github/workflows/ci.yml
CHANGED
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
<div align="center">
|
|
4
4
|
|
|
5
|
-
[](https://github.com/fastify/fast-uri/actions/workflows/ci.yml)
|
|
6
6
|
[](https://standardjs.com/)
|
|
7
7
|
|
|
8
8
|
</div>
|
|
@@ -122,4 +122,4 @@ urijs: resolve x 225,759 ops/sec ±0.37% (95 runs sampled)
|
|
|
122
122
|
|
|
123
123
|
- [ ] Support MailTo
|
|
124
124
|
- [ ] Be 100% iso compatible with uri-js
|
|
125
|
-
- [ ] Add browser test stack
|
|
125
|
+
- [ ] Add browser test stack
|
package/index.js
CHANGED
|
@@ -179,7 +179,6 @@ function serialize (cmpts, opts) {
|
|
|
179
179
|
}
|
|
180
180
|
|
|
181
181
|
const URI_PARSE = /^(?:([^:/?#]+):)?(?:\/\/((?:([^/?#@]*)@)?(\[[^/?#\]]+\]|[^/?#:]*)(?::(\d*))?))?([^?#]*)(?:\?([^#]*))?(?:#((?:.|\n|\r)*))?/i
|
|
182
|
-
const NO_MATCH_IS_UNDEFINED = ('').match(/(){0}/)[1] === undefined
|
|
183
182
|
|
|
184
183
|
function parse (uri, opts) {
|
|
185
184
|
const options = Object.assign({}, opts)
|
|
@@ -198,34 +197,18 @@ function parse (uri, opts) {
|
|
|
198
197
|
const matches = uri.match(URI_PARSE)
|
|
199
198
|
|
|
200
199
|
if (matches) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
parsed.port = matches[5]
|
|
214
|
-
}
|
|
215
|
-
} else { // IE FIX for improper RegExp matching
|
|
216
|
-
// store each component
|
|
217
|
-
parsed.scheme = matches[1] || undefined
|
|
218
|
-
parsed.userinfo = (uri.indexOf('@') !== -1 ? matches[3] : undefined)
|
|
219
|
-
parsed.host = (uri.indexOf('//') !== -1 ? matches[4] : undefined)
|
|
220
|
-
parsed.port = parseInt(matches[5], 10)
|
|
221
|
-
parsed.path = matches[6] || ''
|
|
222
|
-
parsed.query = (uri.indexOf('?') !== -1 ? matches[7] : undefined)
|
|
223
|
-
parsed.fragment = (uri.indexOf('#') !== -1 ? matches[8] : undefined)
|
|
224
|
-
|
|
225
|
-
// fix port number
|
|
226
|
-
if (isNaN(parsed.port)) {
|
|
227
|
-
parsed.port = (uri.match(/\/\/(?:.|\n)*:(?:\/|\?|#|$)/) ? matches[4] : undefined)
|
|
228
|
-
}
|
|
200
|
+
// store each component
|
|
201
|
+
parsed.scheme = matches[1]
|
|
202
|
+
parsed.userinfo = matches[3]
|
|
203
|
+
parsed.host = matches[4]
|
|
204
|
+
parsed.port = parseInt(matches[5], 10)
|
|
205
|
+
parsed.path = matches[6] || ''
|
|
206
|
+
parsed.query = matches[7]
|
|
207
|
+
parsed.fragment = matches[8]
|
|
208
|
+
|
|
209
|
+
// fix port number
|
|
210
|
+
if (isNaN(parsed.port)) {
|
|
211
|
+
parsed.port = matches[5]
|
|
229
212
|
}
|
|
230
213
|
if (parsed.host) {
|
|
231
214
|
const ipv4result = normalizeIPv4(parsed.host)
|
|
@@ -295,7 +278,7 @@ function parse (uri, opts) {
|
|
|
295
278
|
return parsed
|
|
296
279
|
}
|
|
297
280
|
|
|
298
|
-
|
|
281
|
+
const fastUri = {
|
|
299
282
|
normalize,
|
|
300
283
|
resolve,
|
|
301
284
|
resolveComponents,
|
|
@@ -303,3 +286,7 @@ module.exports = {
|
|
|
303
286
|
serialize,
|
|
304
287
|
parse
|
|
305
288
|
}
|
|
289
|
+
|
|
290
|
+
module.exports = fastUri
|
|
291
|
+
module.exports.default = fastUri
|
|
292
|
+
module.exports.fastUri = fastUri
|
package/lib/utils.js
CHANGED
|
@@ -7,7 +7,7 @@ function normalizeIPv4 (host) {
|
|
|
7
7
|
const matches = host.match(/^(\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/) || []
|
|
8
8
|
const [address] = matches
|
|
9
9
|
if (address) {
|
|
10
|
-
return { host: stripLeadingZeros(address, '.'
|
|
10
|
+
return { host: stripLeadingZeros(address, '.'), isIPV4: true }
|
|
11
11
|
} else {
|
|
12
12
|
return { host, isIPV4: false }
|
|
13
13
|
}
|
|
@@ -92,7 +92,7 @@ function getIPV6 (input) {
|
|
|
92
92
|
|
|
93
93
|
function normalizeIPv6 (host, opts = {}) {
|
|
94
94
|
if (findToken(host, ':') < 2) { return { host, isIPV6: false } }
|
|
95
|
-
const ipv6 = getIPV6(host
|
|
95
|
+
const ipv6 = getIPV6(host)
|
|
96
96
|
|
|
97
97
|
if (!ipv6.error) {
|
|
98
98
|
let newHost = ipv6.address
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fast-uri",
|
|
3
3
|
"description": "Dependency free RFC 3986 URI toolbox",
|
|
4
|
-
"version": "2.
|
|
4
|
+
"version": "2.2.0",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "commonjs",
|
|
7
|
-
"types": "index.d.ts",
|
|
7
|
+
"types": "types/index.d.ts",
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"author": "Vincent Le Goff <vince.legoff@gmail.com> (https://github.com/zekth)",
|
|
10
10
|
"repository": {
|
|
@@ -31,8 +31,7 @@
|
|
|
31
31
|
"snazzy": "^9.0.0",
|
|
32
32
|
"standard": "^17.0.0",
|
|
33
33
|
"tap": "^16.0.0",
|
|
34
|
-
"tsd": "^0.
|
|
35
|
-
"typescript": "^4.4.4",
|
|
34
|
+
"tsd": "^0.24.1",
|
|
36
35
|
"uri-js": "^4.4.1"
|
|
37
36
|
},
|
|
38
37
|
"dependencies": {},
|
package/test/equal.test.js
CHANGED
|
@@ -4,12 +4,21 @@ const tap = require('tap')
|
|
|
4
4
|
const test = tap.test
|
|
5
5
|
const URI = require('../')
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
const fn = URI.equal
|
|
8
|
+
const runTest = (t, suite) => {
|
|
9
|
+
suite.forEach(s => {
|
|
10
|
+
const operator = s.result ? '==' : '!='
|
|
11
|
+
t.equal(fn(s.pair[0], s.pair[1]), s.result, `${s.pair[0]} ${operator} ${s.pair[1]}`)
|
|
12
|
+
t.equal(fn(s.pair[1], s.pair[0]), s.result, `${s.pair[1]} ${operator} ${s.pair[0]}`)
|
|
13
|
+
})
|
|
14
|
+
}
|
|
10
15
|
|
|
11
|
-
|
|
12
|
-
|
|
16
|
+
test('URI Equals', (t) => {
|
|
17
|
+
const suite = [
|
|
18
|
+
{ pair: ['example://a/b/c/%7Bfoo%7D', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d'], result: true }, // test from RFC 3986
|
|
19
|
+
{ pair: ['http://example.org/~user', 'http://example.org/%7euser'], result: true } // test from RFC 3987
|
|
20
|
+
]
|
|
21
|
+
runTest(t, suite)
|
|
13
22
|
t.end()
|
|
14
23
|
})
|
|
15
24
|
|
|
@@ -20,39 +29,55 @@ test('URI Equals', (t) => {
|
|
|
20
29
|
// })
|
|
21
30
|
|
|
22
31
|
test('HTTP Equals', (t) => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
const suite = [
|
|
33
|
+
// test from RFC 2616
|
|
34
|
+
{ pair: ['http://abc.com:80/~smith/home.html', 'http://abc.com/~smith/home.html'], result: true },
|
|
35
|
+
{ pair: [{ scheme: 'http', host: 'abc.com', port: 80, path: '/~smith/home.html' }, 'http://abc.com/~smith/home.html'], result: true },
|
|
36
|
+
{ pair: ['http://ABC.com/%7Esmith/home.html', 'http://abc.com/~smith/home.html'], result: true },
|
|
37
|
+
{ pair: ['http://ABC.com:/%7esmith/home.html', 'http://abc.com/~smith/home.html'], result: true },
|
|
38
|
+
{ pair: ['HTTP://ABC.COM', 'http://abc.com/'], result: true },
|
|
39
|
+
// test from RFC 3986
|
|
40
|
+
{ pair: ['http://example.com:/', 'http://example.com:80/'], result: true }
|
|
41
|
+
]
|
|
42
|
+
runTest(t, suite)
|
|
31
43
|
t.end()
|
|
32
44
|
})
|
|
33
45
|
|
|
34
46
|
test('HTTPS Equals', (t) => {
|
|
35
|
-
|
|
36
|
-
|
|
47
|
+
const suite = [
|
|
48
|
+
{ pair: ['https://example.com', 'https://example.com:443/'], result: true },
|
|
49
|
+
{ pair: ['https://example.com:/', 'https://example.com:443/'], result: true }
|
|
50
|
+
]
|
|
51
|
+
runTest(t, suite)
|
|
37
52
|
t.end()
|
|
38
53
|
})
|
|
39
54
|
|
|
40
55
|
test('URN Equals', (t) => {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
56
|
+
const suite = [
|
|
57
|
+
// test from RFC 2141
|
|
58
|
+
{ pair: ['urn:foo:a123,456', 'urn:foo:a123,456'], result: true },
|
|
59
|
+
{ pair: ['urn:foo:a123,456', 'URN:foo:a123,456'], result: true },
|
|
60
|
+
{ pair: ['urn:foo:a123,456', 'urn:FOO:a123,456'], result: true }
|
|
61
|
+
]
|
|
45
62
|
|
|
46
63
|
// Disabling for now as the whole equal logic might need
|
|
47
64
|
// to be refactored
|
|
48
65
|
// t.equal(URI.equal('urn:foo:a123,456', 'urn:foo:A123,456'), false)
|
|
49
66
|
// t.equal(URI.equal('urn:foo:a123%2C456', 'URN:FOO:a123%2c456'), true)
|
|
67
|
+
|
|
68
|
+
runTest(t, suite)
|
|
50
69
|
t.end()
|
|
51
70
|
})
|
|
71
|
+
|
|
52
72
|
test('UUID Equals', (t) => {
|
|
53
|
-
|
|
73
|
+
const suite = [
|
|
74
|
+
{ pair: ['URN:UUID:F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6', 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6'], result: true }
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
runTest(t, suite)
|
|
54
78
|
t.end()
|
|
55
79
|
})
|
|
80
|
+
|
|
56
81
|
// test('Mailto Equals', (t) => {
|
|
57
82
|
// // tests from RFC 6068
|
|
58
83
|
// t.equal(URI.equal('mailto:addr1@an.example,addr2@an.example', 'mailto:?to=addr1@an.example,addr2@an.example'), true)
|
|
@@ -61,11 +86,19 @@ test('UUID Equals', (t) => {
|
|
|
61
86
|
// })
|
|
62
87
|
|
|
63
88
|
test('WS Equal', (t) => {
|
|
64
|
-
|
|
89
|
+
const suite = [
|
|
90
|
+
{ pair: ['WS://ABC.COM:80/chat#one', 'ws://abc.com/chat'], result: true }
|
|
91
|
+
]
|
|
92
|
+
|
|
93
|
+
runTest(t, suite)
|
|
65
94
|
t.end()
|
|
66
95
|
})
|
|
67
96
|
|
|
68
97
|
test('WSS Equal', (t) => {
|
|
69
|
-
|
|
98
|
+
const suite = [
|
|
99
|
+
{ pair: ['WSS://ABC.COM:443/chat#one', 'wss://abc.com/chat'], result: true }
|
|
100
|
+
]
|
|
101
|
+
|
|
102
|
+
runTest(t, suite)
|
|
70
103
|
t.end()
|
|
71
104
|
})
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
type FastUri = typeof fastUri
|
|
2
|
+
|
|
3
|
+
declare namespace fastUri {
|
|
4
|
+
export interface URIComponent {
|
|
5
|
+
scheme?: string;
|
|
6
|
+
userinfo?: string;
|
|
7
|
+
host?: string;
|
|
8
|
+
port?: number | string;
|
|
9
|
+
path?: string;
|
|
10
|
+
query?: string;
|
|
11
|
+
fragment?: string;
|
|
12
|
+
reference?: string;
|
|
13
|
+
error?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface Options {
|
|
16
|
+
scheme?: string;
|
|
17
|
+
reference?: string;
|
|
18
|
+
unicodeSupport?: boolean;
|
|
19
|
+
domainHost?: boolean;
|
|
20
|
+
absolutePath?: boolean;
|
|
21
|
+
tolerant?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @deprecated Use Options instead
|
|
26
|
+
*/
|
|
27
|
+
export type options = Options
|
|
28
|
+
/**
|
|
29
|
+
* @deprecated Use URIComponent instead
|
|
30
|
+
*/
|
|
31
|
+
export type URIComponents = URIComponent
|
|
32
|
+
|
|
33
|
+
export function normalize(uri: string, opts?: Options): string;
|
|
34
|
+
export function normalize(uri: URIComponent, opts?: Options): URIComponent;
|
|
35
|
+
export function normalize(uri: any, opts?: Options): any;
|
|
36
|
+
|
|
37
|
+
export function resolve(baseURI: string, relativeURI: string, options?: Options): string
|
|
38
|
+
|
|
39
|
+
export function resolveComponent(base: URIComponent, relative: URIComponent, options?: Options, skipNormalization?: boolean): URIComponent
|
|
40
|
+
|
|
41
|
+
export function parse(uri: string, opts?: Options): URIComponent;
|
|
42
|
+
|
|
43
|
+
export function serialize(component: URIComponent, opts?: Options): string;
|
|
44
|
+
|
|
45
|
+
export function equal(uriA: string, uriB: string): boolean;
|
|
46
|
+
|
|
47
|
+
export function resolve(base: string, path: string): string;
|
|
48
|
+
|
|
49
|
+
export const fastUri: FastUri
|
|
50
|
+
export { fastUri as default }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export = fastUri
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import uri, { URIComponents, URIComponent, Options, options } from "..";
|
|
2
|
+
import { expectDeprecated, expectType } from "tsd";
|
|
3
|
+
|
|
4
|
+
const parsed = uri.parse("foo");
|
|
5
|
+
expectType<URIComponents>(parsed);
|
|
6
|
+
const parsed2 = uri.parse("foo", {
|
|
7
|
+
domainHost: true,
|
|
8
|
+
scheme: 'https',
|
|
9
|
+
unicodeSupport: false
|
|
10
|
+
})
|
|
11
|
+
expectType<URIComponents>(parsed2);
|
|
12
|
+
|
|
13
|
+
expectType<URIComponent>({} as URIComponents)
|
|
14
|
+
expectDeprecated({} as URIComponents)
|
|
15
|
+
|
|
16
|
+
expectType<Options>({} as options)
|
|
17
|
+
expectDeprecated({} as options)
|
package/index.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export interface URIComponents {
|
|
2
|
-
scheme?: string;
|
|
3
|
-
userinfo?: string;
|
|
4
|
-
host?: string;
|
|
5
|
-
port?: number | string;
|
|
6
|
-
path?: string;
|
|
7
|
-
query?: string;
|
|
8
|
-
fragment?: string;
|
|
9
|
-
reference?: string;
|
|
10
|
-
error?: string;
|
|
11
|
-
}
|
|
12
|
-
export interface options {
|
|
13
|
-
scheme?: string;
|
|
14
|
-
reference?: string;
|
|
15
|
-
unicodeSupport?: boolean;
|
|
16
|
-
domainHost?: boolean;
|
|
17
|
-
absolutePath?: boolean;
|
|
18
|
-
tolerant?:boolean;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function parse(uri: string, opts?: options): URIComponents;
|
|
22
|
-
|
|
23
|
-
export function serialize(component: URIComponents, opts?: options): string;
|
|
24
|
-
|
|
25
|
-
export function equal(uriA: string, uriB:string): boolean;
|
|
26
|
-
|
|
27
|
-
export function resolve(base:string, path:string): string;
|
package/index.test-d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import uri, { URIComponents } from ".";
|
|
2
|
-
import { expectType } from "tsd";
|
|
3
|
-
|
|
4
|
-
const parsed = uri.parse("foo");
|
|
5
|
-
expectType<URIComponents>(parsed);
|
|
6
|
-
const parsed2 = uri.parse("foo", {
|
|
7
|
-
domainHost: true,
|
|
8
|
-
scheme: 'https',
|
|
9
|
-
unicodeSupport: false
|
|
10
|
-
})
|
|
11
|
-
expectType<URIComponents>(parsed2);
|
package/types/.gitkeep
DELETED
|
File without changes
|