bare-url 2.0.9 → 2.1.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/global.js +1 -0
- package/index.js +94 -49
- package/lib/errors.js +16 -8
- package/package.json +10 -4
- package/prebuilds/android-arm/bare-url.bare +0 -0
- package/prebuilds/android-arm64/bare-url.bare +0 -0
- package/prebuilds/android-ia32/bare-url.bare +0 -0
- package/prebuilds/android-x64/bare-url.bare +0 -0
- package/prebuilds/darwin-arm64/bare-url.bare +0 -0
- package/prebuilds/darwin-x64/bare-url.bare +0 -0
- package/prebuilds/ios-arm64/bare-url.bare +0 -0
- package/prebuilds/ios-arm64-simulator/bare-url.bare +0 -0
- package/prebuilds/ios-x64-simulator/bare-url.bare +0 -0
- package/prebuilds/linux-arm64/bare-url.bare +0 -0
- package/prebuilds/linux-x64/bare-url.bare +0 -0
- package/prebuilds/win32-arm64/bare-url.bare +0 -0
- package/prebuilds/win32-x64/bare-url.bare +0 -0
package/global.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
global.URL = require('.')
|
package/index.js
CHANGED
|
@@ -5,12 +5,12 @@ const errors = require('./lib/errors')
|
|
|
5
5
|
|
|
6
6
|
const isWindows = Bare.platform === 'win32'
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
module.exports = exports = class URL {
|
|
9
9
|
static {
|
|
10
10
|
binding.tag(this)
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
constructor
|
|
13
|
+
constructor(input, base, opts = {}) {
|
|
14
14
|
if (arguments.length === 0) throw errors.INVALID_URL()
|
|
15
15
|
|
|
16
16
|
input = `${input}`
|
|
@@ -24,47 +24,58 @@ const URL = module.exports = exports = class URL {
|
|
|
24
24
|
|
|
25
25
|
// https://url.spec.whatwg.org/#dom-url-href
|
|
26
26
|
|
|
27
|
-
get href
|
|
27
|
+
get href() {
|
|
28
28
|
return this._href
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
set href
|
|
31
|
+
set href(value) {
|
|
32
32
|
this._update(value)
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
// https://url.spec.whatwg.org/#dom-url-protocol
|
|
36
36
|
|
|
37
|
-
get protocol
|
|
37
|
+
get protocol() {
|
|
38
38
|
return this._slice(0, this._components[0]) + ':'
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
set protocol
|
|
42
|
-
this._update(
|
|
41
|
+
set protocol(value) {
|
|
42
|
+
this._update(
|
|
43
|
+
this._replace(value.replace(/:+$/, ''), 0, this._components[0])
|
|
44
|
+
)
|
|
43
45
|
}
|
|
44
46
|
|
|
45
47
|
// https://url.spec.whatwg.org/#dom-url-username
|
|
46
48
|
|
|
47
|
-
get username
|
|
49
|
+
get username() {
|
|
48
50
|
return this._slice(this._components[0] + 3 /* :// */, this._components[1])
|
|
49
51
|
}
|
|
50
52
|
|
|
51
|
-
set username
|
|
53
|
+
set username(value) {
|
|
52
54
|
if (cannotHaveCredentialsOrPort(this)) {
|
|
53
55
|
return
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
if (this.username === '') value += '@'
|
|
57
59
|
|
|
58
|
-
this._update(
|
|
60
|
+
this._update(
|
|
61
|
+
this._replace(
|
|
62
|
+
value,
|
|
63
|
+
this._components[0] + 3 /* :// */,
|
|
64
|
+
this._components[1]
|
|
65
|
+
)
|
|
66
|
+
)
|
|
59
67
|
}
|
|
60
68
|
|
|
61
69
|
// https://url.spec.whatwg.org/#dom-url-password
|
|
62
70
|
|
|
63
|
-
get password
|
|
64
|
-
return this._href.slice(
|
|
71
|
+
get password() {
|
|
72
|
+
return this._href.slice(
|
|
73
|
+
this._components[1] + 1 /* : */,
|
|
74
|
+
this._components[2] - 1 /* @ */
|
|
75
|
+
)
|
|
65
76
|
}
|
|
66
77
|
|
|
67
|
-
set password
|
|
78
|
+
set password(value) {
|
|
68
79
|
if (cannotHaveCredentialsOrPort(this)) {
|
|
69
80
|
return
|
|
70
81
|
}
|
|
@@ -87,25 +98,31 @@ const URL = module.exports = exports = class URL {
|
|
|
87
98
|
|
|
88
99
|
// https://url.spec.whatwg.org/#dom-url-host
|
|
89
100
|
|
|
90
|
-
get host
|
|
101
|
+
get host() {
|
|
91
102
|
return this._slice(this._components[2], this._components[5])
|
|
92
103
|
}
|
|
93
104
|
|
|
94
|
-
set host
|
|
105
|
+
set host(value) {
|
|
95
106
|
if (hasOpaquePath(this)) {
|
|
96
107
|
return
|
|
97
108
|
}
|
|
98
109
|
|
|
99
|
-
this._update(
|
|
110
|
+
this._update(
|
|
111
|
+
this._replace(
|
|
112
|
+
value,
|
|
113
|
+
this._components[2],
|
|
114
|
+
this._components[value.includes(':') ? 5 : 3]
|
|
115
|
+
)
|
|
116
|
+
)
|
|
100
117
|
}
|
|
101
118
|
|
|
102
119
|
// https://url.spec.whatwg.org/#dom-url-hostname
|
|
103
120
|
|
|
104
|
-
get hostname
|
|
121
|
+
get hostname() {
|
|
105
122
|
return this._slice(this._components[2], this._components[3])
|
|
106
123
|
}
|
|
107
124
|
|
|
108
|
-
set hostname
|
|
125
|
+
set hostname(value) {
|
|
109
126
|
if (hasOpaquePath(this)) {
|
|
110
127
|
return
|
|
111
128
|
}
|
|
@@ -115,11 +132,11 @@ const URL = module.exports = exports = class URL {
|
|
|
115
132
|
|
|
116
133
|
// https://url.spec.whatwg.org/#dom-url-port
|
|
117
134
|
|
|
118
|
-
get port
|
|
135
|
+
get port() {
|
|
119
136
|
return this._slice(this._components[3] + 1 /* : */, this._components[5])
|
|
120
137
|
}
|
|
121
138
|
|
|
122
|
-
set port
|
|
139
|
+
set port(value) {
|
|
123
140
|
if (cannotHaveCredentialsOrPort(this)) {
|
|
124
141
|
return
|
|
125
142
|
}
|
|
@@ -136,11 +153,11 @@ const URL = module.exports = exports = class URL {
|
|
|
136
153
|
|
|
137
154
|
// https://url.spec.whatwg.org/#dom-url-pathname
|
|
138
155
|
|
|
139
|
-
get pathname
|
|
156
|
+
get pathname() {
|
|
140
157
|
return this._slice(this._components[5], this._components[6] - 1 /* ? */)
|
|
141
158
|
}
|
|
142
159
|
|
|
143
|
-
set pathname
|
|
160
|
+
set pathname(value) {
|
|
144
161
|
if (hasOpaquePath(this)) {
|
|
145
162
|
return
|
|
146
163
|
}
|
|
@@ -149,42 +166,53 @@ const URL = module.exports = exports = class URL {
|
|
|
149
166
|
value = '/' + value
|
|
150
167
|
}
|
|
151
168
|
|
|
152
|
-
this._update(
|
|
169
|
+
this._update(
|
|
170
|
+
this._replace(value, this._components[5], this._components[6] - 1 /* ? */)
|
|
171
|
+
)
|
|
153
172
|
}
|
|
154
173
|
|
|
155
174
|
// https://url.spec.whatwg.org/#dom-url-search
|
|
156
175
|
|
|
157
|
-
get search
|
|
158
|
-
return this._slice(
|
|
176
|
+
get search() {
|
|
177
|
+
return this._slice(
|
|
178
|
+
this._components[6] - 1 /* ? */,
|
|
179
|
+
this._components[7] - 1 /* # */
|
|
180
|
+
)
|
|
159
181
|
}
|
|
160
182
|
|
|
161
|
-
set search
|
|
183
|
+
set search(value) {
|
|
162
184
|
if (value && value[0] !== '?') value = '?' + value
|
|
163
185
|
|
|
164
|
-
this._update(
|
|
186
|
+
this._update(
|
|
187
|
+
this._replace(
|
|
188
|
+
value,
|
|
189
|
+
this._components[6] - 1 /* ? */,
|
|
190
|
+
this._components[7] - 1 /* # */
|
|
191
|
+
)
|
|
192
|
+
)
|
|
165
193
|
}
|
|
166
194
|
|
|
167
195
|
// https://url.spec.whatwg.org/#dom-url-hash
|
|
168
196
|
|
|
169
|
-
get hash
|
|
197
|
+
get hash() {
|
|
170
198
|
return this._slice(this._components[7] - 1 /* # */)
|
|
171
199
|
}
|
|
172
200
|
|
|
173
|
-
set hash
|
|
201
|
+
set hash(value) {
|
|
174
202
|
if (value && value[0] !== '#') value = '#' + value
|
|
175
203
|
|
|
176
204
|
this._update(this._replace(value, this._components[7] - 1 /* # */))
|
|
177
205
|
}
|
|
178
206
|
|
|
179
|
-
toString
|
|
207
|
+
toString() {
|
|
180
208
|
return this._href
|
|
181
209
|
}
|
|
182
210
|
|
|
183
|
-
toJSON
|
|
211
|
+
toJSON() {
|
|
184
212
|
return this._href
|
|
185
213
|
}
|
|
186
214
|
|
|
187
|
-
[Symbol.for('bare.inspect')]
|
|
215
|
+
[Symbol.for('bare.inspect')]() {
|
|
188
216
|
return {
|
|
189
217
|
__proto__: { constructor: URL },
|
|
190
218
|
|
|
@@ -201,17 +229,22 @@ const URL = module.exports = exports = class URL {
|
|
|
201
229
|
}
|
|
202
230
|
}
|
|
203
231
|
|
|
204
|
-
_slice
|
|
232
|
+
_slice(start, end = this._href.length) {
|
|
205
233
|
return this._href.slice(start, end)
|
|
206
234
|
}
|
|
207
235
|
|
|
208
|
-
_replace
|
|
236
|
+
_replace(replacement, start, end = this._href.length) {
|
|
209
237
|
return this._slice(0, start) + replacement + this._slice(end)
|
|
210
238
|
}
|
|
211
239
|
|
|
212
|
-
_parse
|
|
240
|
+
_parse(href, base, shouldThrow) {
|
|
213
241
|
try {
|
|
214
|
-
this._href = binding.parse(
|
|
242
|
+
this._href = binding.parse(
|
|
243
|
+
String(href),
|
|
244
|
+
base ? String(base) : null,
|
|
245
|
+
this._components,
|
|
246
|
+
shouldThrow
|
|
247
|
+
)
|
|
215
248
|
} catch (err) {
|
|
216
249
|
if (err instanceof TypeError) throw err
|
|
217
250
|
|
|
@@ -219,7 +252,7 @@ const URL = module.exports = exports = class URL {
|
|
|
219
252
|
}
|
|
220
253
|
}
|
|
221
254
|
|
|
222
|
-
_update
|
|
255
|
+
_update(href) {
|
|
223
256
|
try {
|
|
224
257
|
this._parse(href, null, true)
|
|
225
258
|
} catch (err) {
|
|
@@ -229,18 +262,20 @@ const URL = module.exports = exports = class URL {
|
|
|
229
262
|
}
|
|
230
263
|
|
|
231
264
|
// https://url.spec.whatwg.org/#url-opaque-path
|
|
232
|
-
function hasOpaquePath
|
|
265
|
+
function hasOpaquePath(url) {
|
|
233
266
|
return url.pathname[0] !== '/'
|
|
234
267
|
}
|
|
235
268
|
|
|
236
269
|
// https://url.spec.whatwg.org/#cannot-have-a-username-password-port
|
|
237
|
-
function cannotHaveCredentialsOrPort
|
|
270
|
+
function cannotHaveCredentialsOrPort(url) {
|
|
238
271
|
return url.hostname === '' || url.protocol === 'file:'
|
|
239
272
|
}
|
|
240
273
|
|
|
241
|
-
|
|
274
|
+
const URL = exports
|
|
242
275
|
|
|
243
|
-
exports.
|
|
276
|
+
exports.URL = URL // For Node.js compatibility
|
|
277
|
+
|
|
278
|
+
exports.isURL = function isURL(value) {
|
|
244
279
|
if (typeof value !== 'object' || value === null) return false
|
|
245
280
|
|
|
246
281
|
let constructor = value.constructor
|
|
@@ -254,16 +289,16 @@ exports.isURL = function isURL (value) {
|
|
|
254
289
|
return false
|
|
255
290
|
}
|
|
256
291
|
|
|
257
|
-
exports.parse = function parse
|
|
292
|
+
exports.parse = function parse(input, base) {
|
|
258
293
|
const url = new URL(input, base, { throw: false })
|
|
259
294
|
return url.href ? url : null
|
|
260
295
|
}
|
|
261
296
|
|
|
262
|
-
exports.canParse = function canParse
|
|
297
|
+
exports.canParse = function canParse(input, base) {
|
|
263
298
|
return binding.canParse(String(input), base ? String(base) : null)
|
|
264
299
|
}
|
|
265
300
|
|
|
266
|
-
exports.fileURLToPath = function fileURLToPath
|
|
301
|
+
exports.fileURLToPath = function fileURLToPath(url) {
|
|
267
302
|
if (typeof url === 'string') {
|
|
268
303
|
url = new URL(url)
|
|
269
304
|
}
|
|
@@ -274,15 +309,21 @@ exports.fileURLToPath = function fileURLToPath (url) {
|
|
|
274
309
|
|
|
275
310
|
if (isWindows) {
|
|
276
311
|
if (/%2f|%5c/i.test(url.pathname)) {
|
|
277
|
-
throw errors.INVALID_FILE_URL_PATH(
|
|
312
|
+
throw errors.INVALID_FILE_URL_PATH(
|
|
313
|
+
'The file: URL path must not include encoded \\ or / characters'
|
|
314
|
+
)
|
|
278
315
|
}
|
|
279
316
|
} else {
|
|
280
317
|
if (url.hostname) {
|
|
281
|
-
throw errors.INVALID_FILE_URL_HOST(
|
|
318
|
+
throw errors.INVALID_FILE_URL_HOST(
|
|
319
|
+
"The file: URL host must be 'localhost' or empty"
|
|
320
|
+
)
|
|
282
321
|
}
|
|
283
322
|
|
|
284
323
|
if (/%2f/i.test(url.pathname)) {
|
|
285
|
-
throw errors.INVALID_FILE_URL_PATH(
|
|
324
|
+
throw errors.INVALID_FILE_URL_PATH(
|
|
325
|
+
'The file: URL path must not include encoded / characters'
|
|
326
|
+
)
|
|
286
327
|
}
|
|
287
328
|
}
|
|
288
329
|
|
|
@@ -293,7 +334,11 @@ exports.fileURLToPath = function fileURLToPath (url) {
|
|
|
293
334
|
|
|
294
335
|
const letter = pathname.charCodeAt(1) | 0x20
|
|
295
336
|
|
|
296
|
-
if (
|
|
337
|
+
if (
|
|
338
|
+
letter < 0x61 /* a */ ||
|
|
339
|
+
letter > 0x7a /* z */ ||
|
|
340
|
+
pathname.charCodeAt(2) !== 0x3a /* : */
|
|
341
|
+
) {
|
|
297
342
|
throw errors.INVALID_FILE_URL_PATH('The file: URL path must be absolute')
|
|
298
343
|
}
|
|
299
344
|
|
|
@@ -303,7 +348,7 @@ exports.fileURLToPath = function fileURLToPath (url) {
|
|
|
303
348
|
return pathname
|
|
304
349
|
}
|
|
305
350
|
|
|
306
|
-
exports.pathToFileURL = function pathToFileURL
|
|
351
|
+
exports.pathToFileURL = function pathToFileURL(pathname) {
|
|
307
352
|
let resolved = path.resolve(pathname)
|
|
308
353
|
|
|
309
354
|
if (pathname[pathname.length - 1] === '/') {
|
package/lib/errors.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
module.exports = class URLError extends Error {
|
|
2
|
-
constructor
|
|
2
|
+
constructor(msg, code, fn = URLError) {
|
|
3
3
|
super(`${code}: ${msg}`)
|
|
4
4
|
this.code = code
|
|
5
5
|
|
|
@@ -8,23 +8,31 @@ module.exports = class URLError extends Error {
|
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
get name
|
|
11
|
+
get name() {
|
|
12
12
|
return 'URLError'
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
static INVALID_URL
|
|
15
|
+
static INVALID_URL(msg = 'Invalid URL') {
|
|
16
16
|
return new URLError(msg, 'INVALID_URL', URLError.INVALID_URL)
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
static INVALID_URL_SCHEME
|
|
19
|
+
static INVALID_URL_SCHEME(msg = 'Invalid URL') {
|
|
20
20
|
return new URLError(msg, 'INVALID_URL_SCHEME', URLError.INVALID_URL_SCHEME)
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
static INVALID_FILE_URL_HOST
|
|
24
|
-
return new URLError(
|
|
23
|
+
static INVALID_FILE_URL_HOST(msg = 'Invalid file: URL host') {
|
|
24
|
+
return new URLError(
|
|
25
|
+
msg,
|
|
26
|
+
'INVALID_FILE_URL_HOST',
|
|
27
|
+
URLError.INVALID_FILE_URL_HOST
|
|
28
|
+
)
|
|
25
29
|
}
|
|
26
30
|
|
|
27
|
-
static INVALID_FILE_URL_PATH
|
|
28
|
-
return new URLError(
|
|
31
|
+
static INVALID_FILE_URL_PATH(msg = 'Invalid file: URL path') {
|
|
32
|
+
return new URLError(
|
|
33
|
+
msg,
|
|
34
|
+
'INVALID_FILE_URL_PATH',
|
|
35
|
+
URLError.INVALID_FILE_URL_PATH
|
|
36
|
+
)
|
|
29
37
|
}
|
|
30
38
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-url",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "WHATWG URL implementation for JavaScript",
|
|
5
|
-
"
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./index.js",
|
|
7
|
+
"./package": "./package.json",
|
|
8
|
+
"./global": "./global.js"
|
|
9
|
+
},
|
|
6
10
|
"files": [
|
|
7
11
|
"index.js",
|
|
12
|
+
"global.js",
|
|
8
13
|
"binding.c",
|
|
9
14
|
"binding.js",
|
|
10
15
|
"CMakeLists.txt",
|
|
@@ -13,7 +18,7 @@
|
|
|
13
18
|
],
|
|
14
19
|
"addon": true,
|
|
15
20
|
"scripts": {
|
|
16
|
-
"test": "
|
|
21
|
+
"test": "prettier . --check && bare test.js"
|
|
17
22
|
},
|
|
18
23
|
"repository": {
|
|
19
24
|
"type": "git",
|
|
@@ -32,6 +37,7 @@
|
|
|
32
37
|
"brittle": "^3.3.2",
|
|
33
38
|
"cmake-bare": "^1.1.6",
|
|
34
39
|
"cmake-fetch": "^1.0.0",
|
|
35
|
-
"
|
|
40
|
+
"prettier": "^3.3.3",
|
|
41
|
+
"prettier-config-standard": "^7.0.0"
|
|
36
42
|
}
|
|
37
43
|
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|