fast-uri 0.0.1

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/lib/utils.js ADDED
@@ -0,0 +1,236 @@
1
+ 'use strict'
2
+
3
+ const { HEX } = require('./scopedChars')
4
+
5
+ function normalizeIPv4 (host) {
6
+ if (findToken(host, '.') < 3) { return { host, isIPV4: false } }
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
+ const [address] = matches
9
+ if (address) {
10
+ return { host: stripLeadingZeros(address, '.', false), isIPV4: true }
11
+ } else {
12
+ return { host, isIPV4: false }
13
+ }
14
+ }
15
+
16
+ function stringToHexStripped (input) {
17
+ let acc = ''
18
+ let strip = true
19
+ for (const c of input) {
20
+ if (c !== '0' && strip === true) strip = false
21
+ if (HEX[c] === undefined) return undefined
22
+ if (!strip) acc += c
23
+ }
24
+ return acc
25
+ }
26
+
27
+ function getIPV6 (input) {
28
+ let tokenCount = 0
29
+ const output = { error: false, address: '', zone: '' }
30
+ const address = []
31
+ const buffer = []
32
+ let isZone = false
33
+ let endipv6Encountered = false
34
+ let endIpv6 = false
35
+
36
+ function consume () {
37
+ if (buffer.length) {
38
+ if (isZone === false) {
39
+ const hex = stringToHexStripped(buffer.join(''))
40
+ if (hex !== undefined) {
41
+ address.push(hex)
42
+ } else {
43
+ output.error = true
44
+ return false
45
+ }
46
+ }
47
+ buffer.length = 0
48
+ }
49
+ return true
50
+ }
51
+
52
+ for (let i = 0; i < input.length; i++) {
53
+ const cursor = input[i]
54
+ if (cursor === '[' || cursor === ']') { continue }
55
+ if (cursor === ':') {
56
+ if (endipv6Encountered === true) {
57
+ endIpv6 = true
58
+ }
59
+ if (!consume()) { break }
60
+ tokenCount++
61
+ address.push(':')
62
+ if (tokenCount > 7) {
63
+ // not valid
64
+ output.error = true
65
+ break
66
+ }
67
+ if (i - 1 >= 0 && input[i - 1] === ':') {
68
+ endipv6Encountered = true
69
+ }
70
+ continue
71
+ } else if (cursor === '%') {
72
+ if (!consume()) { break }
73
+ // switch to zone detection
74
+ isZone = true
75
+ } else {
76
+ buffer.push(cursor)
77
+ continue
78
+ }
79
+ }
80
+ if (buffer.length) {
81
+ if (isZone) {
82
+ output.zone = buffer.join('')
83
+ } else if (endIpv6) {
84
+ address.push(buffer.join(''))
85
+ } else {
86
+ address.push(stringToHexStripped(buffer.join('')))
87
+ }
88
+ }
89
+ output.address = address.join('')
90
+ return output
91
+ }
92
+
93
+ function normalizeIPv6 (host, opts = {}) {
94
+ if (findToken(host, ':') < 2) { return { host, isIPV6: false } }
95
+ const ipv6 = getIPV6(host, opts.isIPV4)
96
+
97
+ if (!ipv6.error) {
98
+ let newHost = ipv6.address
99
+ let escapedHost = ipv6.address
100
+ if (ipv6.zone) {
101
+ newHost += '%' + ipv6.zone
102
+ escapedHost += '%25' + ipv6.zone
103
+ }
104
+ return { host: newHost, escapedHost: escapedHost, isIPV6: true }
105
+ } else {
106
+ return { host, isIPV6: false }
107
+ }
108
+ }
109
+
110
+ function stripLeadingZeros (str, token) {
111
+ let out = ''
112
+ let skip = true
113
+ const l = str.length
114
+ for (let i = 0; i < l; i++) {
115
+ const c = str[i]
116
+ if (c === '0' && skip) {
117
+ if ((i + 1 <= l && str[i + 1] === token) || i + 1 === l) {
118
+ out += c
119
+ skip = false
120
+ }
121
+ } else {
122
+ if (c === token) {
123
+ skip = true
124
+ } else {
125
+ skip = false
126
+ }
127
+ out += c
128
+ }
129
+ }
130
+ return out
131
+ }
132
+
133
+ function findToken (str, token) {
134
+ let ind = 0
135
+ for (let i = 0; i < str.length; i++) {
136
+ if (str[i] === token) ind++
137
+ }
138
+ return ind
139
+ }
140
+
141
+ const RDS1 = /^\.\.?\//
142
+ const RDS2 = /^\/\.(\/|$)/
143
+ const RDS3 = /^\/\.\.(\/|$)/
144
+ const RDS5 = /^\/?(?:.|\n)*?(?=\/|$)/
145
+
146
+ function removeDotSegments (input) {
147
+ const output = []
148
+
149
+ while (input.length) {
150
+ if (input.match(RDS1)) {
151
+ input = input.replace(RDS1, '')
152
+ } else if (input.match(RDS2)) {
153
+ input = input.replace(RDS2, '/')
154
+ } else if (input.match(RDS3)) {
155
+ input = input.replace(RDS3, '/')
156
+ output.pop()
157
+ } else if (input === '.' || input === '..') {
158
+ input = ''
159
+ } else {
160
+ const im = input.match(RDS5)
161
+ if (im) {
162
+ const s = im[0]
163
+ input = input.slice(s.length)
164
+ output.push(s)
165
+ } else {
166
+ throw new Error('Unexpected dot segment condition')
167
+ }
168
+ }
169
+ }
170
+ return output.join('')
171
+ }
172
+
173
+ function normalizeComponentEncoding (components, esc) {
174
+ const func = esc !== true ? escape : unescape
175
+ if (components.scheme !== undefined) {
176
+ components.scheme = func(components.scheme)
177
+ }
178
+ if (components.userinfo !== undefined) {
179
+ components.userinfo = func(components.userinfo)
180
+ }
181
+ if (components.host !== undefined) {
182
+ components.host = func(components.host)
183
+ }
184
+ if (components.path !== undefined) {
185
+ components.path = func(components.path)
186
+ }
187
+ if (components.query !== undefined) {
188
+ components.query = func(components.query)
189
+ }
190
+ if (components.fragment !== undefined) {
191
+ components.fragment = func(components.fragment)
192
+ }
193
+ return components
194
+ }
195
+
196
+ function recomposeAuthority (components, options) {
197
+ const uriTokens = []
198
+
199
+ if (components.userinfo !== undefined) {
200
+ uriTokens.push(components.userinfo)
201
+ uriTokens.push('@')
202
+ }
203
+
204
+ if (components.host !== undefined) {
205
+ let host = unescape(components.host)
206
+ const ipV4res = normalizeIPv4(host)
207
+
208
+ if (ipV4res.isIPV4) {
209
+ host = ipV4res.host
210
+ } else {
211
+ const ipV6res = normalizeIPv6(ipV4res.host, { isIPV4: false })
212
+ if (ipV6res.isIPV6 === true) {
213
+ host = `[${ipV6res.escapedHost}]`
214
+ } else {
215
+ host = components.host
216
+ }
217
+ }
218
+ uriTokens.push(host)
219
+ }
220
+
221
+ if (typeof components.port === 'number' || typeof components.port === 'string') {
222
+ uriTokens.push(':')
223
+ uriTokens.push(String(components.port))
224
+ }
225
+
226
+ return uriTokens.length ? uriTokens.join('') : undefined
227
+ };
228
+
229
+ module.exports = {
230
+ recomposeAuthority,
231
+ normalizeComponentEncoding,
232
+ removeDotSegments,
233
+ normalizeIPv4,
234
+ normalizeIPv6,
235
+ stringToHexStripped
236
+ }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "fast-uri",
3
+ "version": "0.0.1",
4
+ "main": "index.js",
5
+ "license": "MIT",
6
+ "author": "Vincent Le Goff <vince.legoff@gmail.com> (https://github.com/zekth)",
7
+ "scripts": {
8
+ "bench": "node benchmark.js",
9
+ "lint:fix": "standard --fix",
10
+ "test:lint": "standard | snazzy",
11
+ "test:typescript": "tsd",
12
+ "test:unit": "tap -J test/*.test.js",
13
+ "test:unit:dev": "tap -J test/*.test.js --coverage-report=html",
14
+ "test": "npm run test:lint && npm run test:unit && npm run test:typescript",
15
+ "test:ci": "npm run test:lint && npm run test:unit -- --cov --coverage-report=lcovonly && npm run test:typescript"
16
+ },
17
+ "devDependencies": {
18
+ "benchmark": "^2.1.4",
19
+ "coveralls": "^3.1.1",
20
+ "snazzy": "^9.0.0",
21
+ "standard": "^16.0.4",
22
+ "tap": "^15.0.10",
23
+ "tsd": "^0.19.0",
24
+ "typescript": "^4.4.4",
25
+ "uri-js": "^4.4.1"
26
+ },
27
+ "dependencies": {},
28
+ "tap": {
29
+ "check-coverage": false
30
+ }
31
+ }
package/test/.gitkeep ADDED
File without changes
@@ -0,0 +1,121 @@
1
+ 'use strict'
2
+
3
+ const tap = require('tap')
4
+ const test = tap.test
5
+ const fastifyURI = require('../')
6
+ const urijs = require('uri-js')
7
+
8
+ test('compatibility Parse', (t) => {
9
+ const toParse = [
10
+ '//www.g.com/error\n/bleh/bleh',
11
+ 'https://fastify.org',
12
+ '//10.10.10.10',
13
+ '//10.10.000.10',
14
+ '//[2001:db8::7%en0]',
15
+ '//[2001:dbZ::1]:80',
16
+ '//[2001:db8::1]:80',
17
+ '//[2001:db8::001]:80',
18
+ 'uri://user:pass@example.com:123/one/two.three?q1=a1&q2=a2#body',
19
+ 'http://user:pass@example.com:123/one/space in.url?q1=a1&q2=a2#body',
20
+ '//[::ffff:129.144.52.38]',
21
+ 'uri://10.10.10.10.example.com/en/process',
22
+ '//[2606:2800:220:1:248:1893:25c8:1946]/test',
23
+ 'ws://example.com/chat',
24
+ 'ws://example.com/foo?bar=baz',
25
+ 'wss://example.com/?bar=baz',
26
+ 'wss://example.com/chat',
27
+ 'wss://example.com/foo?bar=baz',
28
+ 'wss://example.com/?bar=baz',
29
+ 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6',
30
+ 'urn:uuid:notauuid-7dec-11d0-a765-00a0c91e6bf6',
31
+ 'urn:example:%D0%B0123,z456',
32
+ '//[2606:2800:220:1:248:1893:25c8:1946:43209]',
33
+ 'http://foo.bar',
34
+ 'http://'
35
+ // 'mailto:chris@example.com'-203845,
36
+ // 'mailto:infobot@example.com?subject=current-issue',
37
+ // 'mailto:infobot@example.com?body=send%20current-issue',
38
+ // 'mailto:infobot@example.com?body=send%20current-issue%0D%0Asend%20index',
39
+ // 'mailto:list@example.org?In-Reply-To=%3C3469A91.D10AF4C@example.com%3E',
40
+ // 'mailto:majordomo@example.com?body=subscribe%20bamboo-l',
41
+ // 'mailto:joe@example.com?cc=bob@example.com&body=hello',
42
+ // 'mailto:gorby%25kremvax@example.com',
43
+ // 'mailto:unlikely%3Faddress@example.com?blat=foop',
44
+ // 'mailto:Mike%26family@example.org',
45
+ // 'mailto:%22not%40me%22@example.org',
46
+ // 'mailto:%22oh%5C%5Cno%22@example.org',
47
+ // 'mailto:%22%5C%5C%5C%22it\'s%5C%20ugly%5C%5C%5C%22%22@example.org',
48
+ // 'mailto:user@example.org?subject=caf%C3%A9',
49
+ // 'mailto:user@example.org?subject=%3D%3Futf-8%3FQ%3Fcaf%3DC3%3DA9%3F%3D',
50
+ // 'mailto:user@example.org?subject=%3D%3Fiso-8859-1%3FQ%3Fcaf%3DE9%3F%3D',
51
+ // 'mailto:user@example.org?subject=caf%C3%A9&body=caf%C3%A9',
52
+ // 'mailto:user@%E7%B4%8D%E8%B1%86.example.org?subject=Test&body=NATTO'
53
+ ]
54
+ toParse.forEach((x) => {
55
+ t.same(fastifyURI.parse(x), urijs.parse(x), 'Compatibility parse: ' + x)
56
+ })
57
+ t.end()
58
+ })
59
+
60
+ test('compatibility serialize', (t) => {
61
+ const toSerialize = [
62
+ { host: '10.10.10.10.example.com' },
63
+ { host: '2001:db8::7' },
64
+ { host: '::ffff:129.144.52.38' },
65
+ { host: '2606:2800:220:1:248:1893:25c8:1946' },
66
+ { host: '10.10.10.10.example.com' },
67
+ { host: '10.10.10.10' },
68
+ { path: '?query' },
69
+ { path: 'foo:bar' },
70
+ { path: '//path' },
71
+ {
72
+ scheme: 'uri',
73
+ host: 'example.com',
74
+ port: '9000'
75
+ },
76
+ {
77
+ scheme: 'uri',
78
+ userinfo: 'foo:bar',
79
+ host: 'example.com',
80
+ port: 1,
81
+ path: 'path',
82
+ query: 'query',
83
+ fragment: 'fragment'
84
+ },
85
+ {
86
+ scheme: '',
87
+ userinfo: '',
88
+ host: '',
89
+ port: 0,
90
+ path: '',
91
+ query: '',
92
+ fragment: ''
93
+ },
94
+ {
95
+ scheme: undefined,
96
+ userinfo: undefined,
97
+ host: undefined,
98
+ port: undefined,
99
+ path: undefined,
100
+ query: undefined,
101
+ fragment: undefined
102
+ },
103
+ { host: 'fe80::a%en1' },
104
+ { host: 'fe80::a%25en1' },
105
+ {
106
+ scheme: 'ws',
107
+ host: 'example.com',
108
+ resourceName: '/foo?bar',
109
+ secure: true
110
+ }
111
+ ]
112
+ toSerialize.forEach((x) => {
113
+ const r = JSON.stringify(x)
114
+ t.same(
115
+ fastifyURI.serialize(x),
116
+ urijs.serialize(x),
117
+ 'Compatibility serialize: ' + JSON.stringify(r)
118
+ )
119
+ })
120
+ t.end()
121
+ })
@@ -0,0 +1,71 @@
1
+ 'use strict'
2
+
3
+ const tap = require('tap')
4
+ const test = tap.test
5
+ const URI = require('../')
6
+
7
+ test('URI Equals', (t) => {
8
+ // test from RFC 3986
9
+ t.equal(URI.equal('example://a/b/c/%7Bfoo%7D', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d'), true)
10
+
11
+ // test from RFC 3987
12
+ t.equal(URI.equal('http://example.org/~user', 'http://example.org/%7euser'), true)
13
+ t.end()
14
+ })
15
+
16
+ // test('IRI Equals', (t) => {
17
+ // // example from RFC 3987
18
+ // t.equal(URI.equal('example://a/b/c/%7Bfoo%7D/ros\xE9', 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d/ros%C3%A9', IRI_OPTION), true)
19
+ // t.end()
20
+ // })
21
+
22
+ test('HTTP Equals', (t) => {
23
+ // test from RFC 2616
24
+ t.equal(URI.equal('http://abc.com:80/~smith/home.html', 'http://abc.com/~smith/home.html'), true)
25
+ t.equal(URI.equal({ scheme: 'http', host: 'abc.com', port: 80, path: '/~smith/home.html' }, 'http://abc.com/~smith/home.html'), true)
26
+ t.equal(URI.equal('http://ABC.com/%7Esmith/home.html', 'http://abc.com/~smith/home.html'), true)
27
+ t.equal(URI.equal('http://ABC.com:/%7esmith/home.html', 'http://abc.com/~smith/home.html'), true)
28
+ t.equal(URI.equal('HTTP://ABC.COM', 'http://abc.com/'), true)
29
+ // test from RFC 3986
30
+ t.equal(URI.equal('http://example.com:/', 'http://example.com:80/'), true)
31
+ t.end()
32
+ })
33
+
34
+ test('HTTPS Equals', (t) => {
35
+ t.equal(URI.equal('https://example.com', 'https://example.com:443/'), true)
36
+ t.equal(URI.equal('https://example.com:/', 'https://example.com:443/'), true)
37
+ t.end()
38
+ })
39
+
40
+ test('URN Equals', (t) => {
41
+ // test from RFC 2141
42
+ t.equal(URI.equal('urn:foo:a123,456', 'urn:foo:a123,456'), true)
43
+ t.equal(URI.equal('urn:foo:a123,456', 'URN:foo:a123,456'), true)
44
+ t.equal(URI.equal('urn:foo:a123,456', 'urn:FOO:a123,456'), true)
45
+
46
+ // Disabling for now as the whole equal logic might need
47
+ // to be refactored
48
+ // t.equal(URI.equal('urn:foo:a123,456', 'urn:foo:A123,456'), false)
49
+ // t.equal(URI.equal('urn:foo:a123%2C456', 'URN:FOO:a123%2c456'), true)
50
+ t.end()
51
+ })
52
+ test('UUID Equals', (t) => {
53
+ t.equal(URI.equal('URN:UUID:F81D4FAE-7DEC-11D0-A765-00A0C91E6BF6', 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6'), true)
54
+ t.end()
55
+ })
56
+ // test('Mailto Equals', (t) => {
57
+ // // tests from RFC 6068
58
+ // t.equal(URI.equal('mailto:addr1@an.example,addr2@an.example', 'mailto:?to=addr1@an.example,addr2@an.example'), true)
59
+ // t.equal(URI.equal('mailto:?to=addr1@an.example,addr2@an.example', 'mailto:addr1@an.example?to=addr2@an.example'), true)
60
+ // t.end()
61
+ // })
62
+
63
+ test('WS Equal', (t) => {
64
+ t.equal(URI.equal('WS://ABC.COM:80/chat#one', 'ws://abc.com/chat'), true)
65
+ t.end()
66
+ })
67
+
68
+ test('WSS Equal', (t) => {
69
+ t.equal(URI.equal('WSS://ABC.COM:443/chat#one', 'wss://abc.com/chat'), true)
70
+ t.end()
71
+ })