bare-url 2.3.1 → 2.4.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/index.d.ts +2 -0
- package/index.js +18 -61
- package/lib/errors.d.ts +6 -4
- package/lib/errors.js +12 -17
- package/lib/url-search-params.d.ts +5 -3
- package/lib/url-search-params.js +20 -4
- package/package.json +2 -2
package/index.d.ts
CHANGED
|
@@ -25,6 +25,8 @@ declare class URL {
|
|
|
25
25
|
declare namespace URL {
|
|
26
26
|
export function isURL(value: unknown): value is URL
|
|
27
27
|
|
|
28
|
+
export function isURLSearchParams(value: unknown): value is URLSearchParams
|
|
29
|
+
|
|
28
30
|
export function parse(input: string, base?: string | URL): URL | null
|
|
29
31
|
|
|
30
32
|
export function canParse(input: string, base?: string | URL): boolean
|
package/index.js
CHANGED
|
@@ -7,7 +7,7 @@ const kind = Symbol.for('bare.url.kind')
|
|
|
7
7
|
|
|
8
8
|
const isWindows = Bare.platform === 'win32'
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
class URL {
|
|
11
11
|
static get [kind]() {
|
|
12
12
|
return 0 // Compatibility version
|
|
13
13
|
}
|
|
@@ -49,9 +49,7 @@ module.exports = exports = class URL {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
set protocol(value) {
|
|
52
|
-
this._update(
|
|
53
|
-
this._replace(value.replace(/:+$/, ''), 0, this._components[0])
|
|
54
|
-
)
|
|
52
|
+
this._update(this._replace(value.replace(/:+$/, ''), 0, this._components[0]))
|
|
55
53
|
}
|
|
56
54
|
|
|
57
55
|
// https://url.spec.whatwg.org/#dom-url-username
|
|
@@ -67,22 +65,13 @@ module.exports = exports = class URL {
|
|
|
67
65
|
|
|
68
66
|
if (this.username === '') value += '@'
|
|
69
67
|
|
|
70
|
-
this._update(
|
|
71
|
-
this._replace(
|
|
72
|
-
value,
|
|
73
|
-
this._components[0] + 3 /* :// */,
|
|
74
|
-
this._components[1]
|
|
75
|
-
)
|
|
76
|
-
)
|
|
68
|
+
this._update(this._replace(value, this._components[0] + 3 /* :// */, this._components[1]))
|
|
77
69
|
}
|
|
78
70
|
|
|
79
71
|
// https://url.spec.whatwg.org/#dom-url-password
|
|
80
72
|
|
|
81
73
|
get password() {
|
|
82
|
-
return this._href.slice(
|
|
83
|
-
this._components[1] + 1 /* : */,
|
|
84
|
-
this._components[2] - 1 /* @ */
|
|
85
|
-
)
|
|
74
|
+
return this._href.slice(this._components[1] + 1 /* : */, this._components[2] - 1 /* @ */)
|
|
86
75
|
}
|
|
87
76
|
|
|
88
77
|
set password(value) {
|
|
@@ -118,11 +107,7 @@ module.exports = exports = class URL {
|
|
|
118
107
|
}
|
|
119
108
|
|
|
120
109
|
this._update(
|
|
121
|
-
this._replace(
|
|
122
|
-
value,
|
|
123
|
-
this._components[2],
|
|
124
|
-
this._components[value.includes(':') ? 5 : 3]
|
|
125
|
-
)
|
|
110
|
+
this._replace(value, this._components[2], this._components[value.includes(':') ? 5 : 3])
|
|
126
111
|
)
|
|
127
112
|
}
|
|
128
113
|
|
|
@@ -176,29 +161,20 @@ module.exports = exports = class URL {
|
|
|
176
161
|
value = '/' + value
|
|
177
162
|
}
|
|
178
163
|
|
|
179
|
-
this._update(
|
|
180
|
-
this._replace(value, this._components[5], this._components[6] - 1 /* ? */)
|
|
181
|
-
)
|
|
164
|
+
this._update(this._replace(value, this._components[5], this._components[6] - 1 /* ? */))
|
|
182
165
|
}
|
|
183
166
|
|
|
184
167
|
// https://url.spec.whatwg.org/#dom-url-search
|
|
185
168
|
|
|
186
169
|
get search() {
|
|
187
|
-
return this._slice(
|
|
188
|
-
this._components[6] - 1 /* ? */,
|
|
189
|
-
this._components[7] - 1 /* # */
|
|
190
|
-
)
|
|
170
|
+
return this._slice(this._components[6] - 1 /* ? */, this._components[7] - 1 /* # */)
|
|
191
171
|
}
|
|
192
172
|
|
|
193
173
|
set search(value) {
|
|
194
174
|
if (value && value[0] !== '?') value = '?' + value
|
|
195
175
|
|
|
196
176
|
this._update(
|
|
197
|
-
this._replace(
|
|
198
|
-
value,
|
|
199
|
-
this._components[6] - 1 /* ? */,
|
|
200
|
-
this._components[7] - 1 /* # */
|
|
201
|
-
)
|
|
177
|
+
this._replace(value, this._components[6] - 1 /* ? */, this._components[7] - 1 /* # */)
|
|
202
178
|
)
|
|
203
179
|
|
|
204
180
|
this._params._parse(this.search)
|
|
@@ -267,7 +243,7 @@ module.exports = exports = class URL {
|
|
|
267
243
|
} catch (err) {
|
|
268
244
|
if (err instanceof TypeError) throw err
|
|
269
245
|
|
|
270
|
-
throw errors.INVALID_URL()
|
|
246
|
+
throw errors.INVALID_URL(`Invalid URL '${input}'`, input)
|
|
271
247
|
}
|
|
272
248
|
}
|
|
273
249
|
|
|
@@ -280,6 +256,8 @@ module.exports = exports = class URL {
|
|
|
280
256
|
}
|
|
281
257
|
}
|
|
282
258
|
|
|
259
|
+
module.exports = exports = URL
|
|
260
|
+
|
|
283
261
|
// https://url.spec.whatwg.org/#url-opaque-path
|
|
284
262
|
function hasOpaquePath(url) {
|
|
285
263
|
return url.pathname[0] !== '/'
|
|
@@ -290,8 +268,6 @@ function cannotHaveCredentialsOrPort(url) {
|
|
|
290
268
|
return url.hostname === '' || url.protocol === 'file:'
|
|
291
269
|
}
|
|
292
270
|
|
|
293
|
-
const URL = exports
|
|
294
|
-
|
|
295
271
|
exports.URL = URL
|
|
296
272
|
exports.URLSearchParams = URLSearchParams
|
|
297
273
|
|
|
@@ -300,11 +276,11 @@ exports.errors = errors
|
|
|
300
276
|
exports.isURL = function isURL(value) {
|
|
301
277
|
if (value instanceof URL) return true
|
|
302
278
|
|
|
303
|
-
return
|
|
304
|
-
typeof value === 'object' && value !== null && value[kind] === URL[kind]
|
|
305
|
-
)
|
|
279
|
+
return typeof value === 'object' && value !== null && value[kind] === URL[kind]
|
|
306
280
|
}
|
|
307
281
|
|
|
282
|
+
exports.isURLSearchParams = URLSearchParams.isURLSearchParams
|
|
283
|
+
|
|
308
284
|
// https://url.spec.whatwg.org/#dom-url-parse
|
|
309
285
|
exports.parse = function parse(input, base) {
|
|
310
286
|
const url = new URL(input, base, { throw: false })
|
|
@@ -333,15 +309,11 @@ exports.fileURLToPath = function fileURLToPath(url) {
|
|
|
333
309
|
}
|
|
334
310
|
} else {
|
|
335
311
|
if (url.hostname) {
|
|
336
|
-
throw errors.INVALID_FILE_URL_HOST(
|
|
337
|
-
"The file: URL host must be 'localhost' or empty"
|
|
338
|
-
)
|
|
312
|
+
throw errors.INVALID_FILE_URL_HOST("The file: URL host must be 'localhost' or empty")
|
|
339
313
|
}
|
|
340
314
|
|
|
341
315
|
if (/%2f/i.test(url.pathname)) {
|
|
342
|
-
throw errors.INVALID_FILE_URL_PATH(
|
|
343
|
-
'The file: URL path must not include encoded / characters'
|
|
344
|
-
)
|
|
316
|
+
throw errors.INVALID_FILE_URL_PATH('The file: URL path must not include encoded / characters')
|
|
345
317
|
}
|
|
346
318
|
}
|
|
347
319
|
|
|
@@ -352,11 +324,7 @@ exports.fileURLToPath = function fileURLToPath(url) {
|
|
|
352
324
|
|
|
353
325
|
const letter = pathname.charCodeAt(1) | 0x20
|
|
354
326
|
|
|
355
|
-
if (
|
|
356
|
-
letter < 0x61 /* a */ ||
|
|
357
|
-
letter > 0x7a /* z */ ||
|
|
358
|
-
pathname.charCodeAt(2) !== 0x3a /* : */
|
|
359
|
-
) {
|
|
327
|
+
if (letter < 0x61 /* a */ || letter > 0x7a /* z */ || pathname.charCodeAt(2) !== 0x3a /* : */) {
|
|
360
328
|
throw errors.INVALID_FILE_URL_PATH('The file: URL path must be absolute')
|
|
361
329
|
}
|
|
362
330
|
|
|
@@ -391,18 +359,7 @@ exports.pathToFileURL = function pathToFileURL(pathname) {
|
|
|
391
359
|
}
|
|
392
360
|
|
|
393
361
|
exports.format = function format(parts) {
|
|
394
|
-
const {
|
|
395
|
-
protocol,
|
|
396
|
-
auth,
|
|
397
|
-
host,
|
|
398
|
-
hostname,
|
|
399
|
-
port,
|
|
400
|
-
pathname,
|
|
401
|
-
search,
|
|
402
|
-
query,
|
|
403
|
-
hash,
|
|
404
|
-
slashes
|
|
405
|
-
} = parts
|
|
362
|
+
const { protocol, auth, host, hostname, port, pathname, search, query, hash, slashes } = parts
|
|
406
363
|
|
|
407
364
|
let result = ''
|
|
408
365
|
|
package/lib/errors.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
interface URLError extends Error {
|
|
2
|
+
readonly code: string
|
|
3
|
+
readonly input?: string
|
|
4
|
+
}
|
|
5
|
+
|
|
1
6
|
declare class URLError extends Error {
|
|
2
|
-
|
|
3
|
-
static INVALID_URL_SCHEME(msg?: string): URLError
|
|
4
|
-
static INVALID_FILE_URL_HOST(msg?: string): URLError
|
|
5
|
-
static INVALID_FILE_URL_PATH(msg?: string): URLError
|
|
7
|
+
private constructor()
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
export = URLError
|
package/lib/errors.js
CHANGED
|
@@ -1,38 +1,33 @@
|
|
|
1
1
|
module.exports = class URLError extends Error {
|
|
2
|
-
constructor(msg,
|
|
2
|
+
constructor(msg, fn = URLError, code = fn.name) {
|
|
3
3
|
super(`${code}: ${msg}`)
|
|
4
|
+
|
|
4
5
|
this.code = code
|
|
5
6
|
|
|
6
|
-
if (Error.captureStackTrace)
|
|
7
|
-
Error.captureStackTrace(this, fn)
|
|
8
|
-
}
|
|
7
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, fn)
|
|
9
8
|
}
|
|
10
9
|
|
|
11
10
|
get name() {
|
|
12
11
|
return 'URLError'
|
|
13
12
|
}
|
|
14
13
|
|
|
15
|
-
static INVALID_URL(msg
|
|
16
|
-
|
|
14
|
+
static INVALID_URL(msg, input) {
|
|
15
|
+
const err = new URLError(msg, URLError.INVALID_URL)
|
|
16
|
+
|
|
17
|
+
err.input = input
|
|
18
|
+
|
|
19
|
+
return err
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
static INVALID_URL_SCHEME(msg = 'Invalid URL') {
|
|
20
|
-
return new URLError(msg,
|
|
23
|
+
return new URLError(msg, URLError.INVALID_URL_SCHEME)
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
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
|
-
)
|
|
27
|
+
return new URLError(msg, URLError.INVALID_FILE_URL_HOST)
|
|
29
28
|
}
|
|
30
29
|
|
|
31
30
|
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
|
-
)
|
|
31
|
+
return new URLError(msg, URLError.INVALID_FILE_URL_PATH)
|
|
37
32
|
}
|
|
38
33
|
}
|
|
@@ -13,9 +13,11 @@ interface URLSearchParams extends Iterable<[name: string, value: string]> {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
declare class URLSearchParams {
|
|
16
|
-
constructor(
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
constructor(init: string | Record<string, string> | Iterable<[string, string]>)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare namespace URLSearchParams {
|
|
20
|
+
export function isURLSearchParams(value: unknown): value is URLSearchParams
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
export = URLSearchParams
|
package/lib/url-search-params.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
const kind = Symbol.for('bare.url.search-params.kind')
|
|
2
|
+
|
|
3
|
+
class URLSearchParams {
|
|
2
4
|
static _urls = new WeakMap()
|
|
3
5
|
|
|
6
|
+
static get [kind]() {
|
|
7
|
+
return 0 // Compatibility version
|
|
8
|
+
}
|
|
9
|
+
|
|
4
10
|
// https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams
|
|
5
11
|
constructor(init, url = null) {
|
|
6
12
|
this._params = new Map()
|
|
@@ -18,6 +24,10 @@ module.exports = class URLSearchParams {
|
|
|
18
24
|
}
|
|
19
25
|
}
|
|
20
26
|
|
|
27
|
+
get [kind]() {
|
|
28
|
+
return URLSearchParams[kind]
|
|
29
|
+
}
|
|
30
|
+
|
|
21
31
|
// https://url.spec.whatwg.org/#dom-urlsearchparams-size
|
|
22
32
|
get size() {
|
|
23
33
|
return this._params.length
|
|
@@ -142,9 +152,7 @@ module.exports = class URLSearchParams {
|
|
|
142
152
|
if (i === -1) i = sequence.length
|
|
143
153
|
|
|
144
154
|
const name = decodeURIComponent(sequence.substring(0, i))
|
|
145
|
-
const value = decodeURIComponent(
|
|
146
|
-
sequence.substring(i + 1, sequence.length)
|
|
147
|
-
)
|
|
155
|
+
const value = decodeURIComponent(sequence.substring(i + 1, sequence.length))
|
|
148
156
|
|
|
149
157
|
let list = this._params.get(name)
|
|
150
158
|
|
|
@@ -174,3 +182,11 @@ module.exports = class URLSearchParams {
|
|
|
174
182
|
return output
|
|
175
183
|
}
|
|
176
184
|
}
|
|
185
|
+
|
|
186
|
+
module.exports = exports = URLSearchParams
|
|
187
|
+
|
|
188
|
+
exports.isURLSearchParams = function isURLSearchParams(value) {
|
|
189
|
+
if (value instanceof URLSearchParams) return true
|
|
190
|
+
|
|
191
|
+
return typeof value === 'object' && value !== null && value[kind] === URLSearchParams[kind]
|
|
192
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-url",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "WHATWG URL implementation for JavaScript",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./package": "./package.json",
|
|
@@ -46,6 +46,6 @@
|
|
|
46
46
|
"cmake-bare": "^1.1.6",
|
|
47
47
|
"cmake-fetch": "^1.0.0",
|
|
48
48
|
"prettier": "^3.3.3",
|
|
49
|
-
"prettier-config-
|
|
49
|
+
"prettier-config-holepunch": "^2.0.0"
|
|
50
50
|
}
|
|
51
51
|
}
|