-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
373 lines (286 loc) · 7.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* global Bare */
const path = require('bare-path')
const binding = require('./binding')
const errors = require('./lib/errors')
const isWindows = Bare.platform === 'win32'
module.exports = exports = class URL {
static {
binding.tag(this)
}
constructor(input, base, opts = {}) {
if (arguments.length === 0) throw errors.INVALID_URL()
input = `${input}`
if (base !== undefined) base = `${base}`
this._components = new Uint32Array(8)
this._parse(input, base, opts.throw !== false)
}
// https://url.spec.whatwg.org/#dom-url-href
get href() {
return this._href
}
set href(value) {
this._update(value)
}
// https://url.spec.whatwg.org/#dom-url-protocol
get protocol() {
return this._slice(0, this._components[0]) + ':'
}
set protocol(value) {
this._update(
this._replace(value.replace(/:+$/, ''), 0, this._components[0])
)
}
// https://url.spec.whatwg.org/#dom-url-username
get username() {
return this._slice(this._components[0] + 3 /* :// */, this._components[1])
}
set username(value) {
if (cannotHaveCredentialsOrPort(this)) {
return
}
if (this.username === '') value += '@'
this._update(
this._replace(
value,
this._components[0] + 3 /* :// */,
this._components[1]
)
)
}
// https://url.spec.whatwg.org/#dom-url-password
get password() {
return this._href.slice(
this._components[1] + 1 /* : */,
this._components[2] - 1 /* @ */
)
}
set password(value) {
if (cannotHaveCredentialsOrPort(this)) {
return
}
let start = this._components[1] + 1 /* : */
let end = this._components[2] - 1 /* @ */
if (this.password === '') {
value = ':' + value
start--
}
if (this.username === '') {
value += '@'
end++
}
this._update(this._replace(value, start, end))
}
// https://url.spec.whatwg.org/#dom-url-host
get host() {
return this._slice(this._components[2], this._components[5])
}
set host(value) {
if (hasOpaquePath(this)) {
return
}
this._update(
this._replace(
value,
this._components[2],
this._components[value.includes(':') ? 5 : 3]
)
)
}
// https://url.spec.whatwg.org/#dom-url-hostname
get hostname() {
return this._slice(this._components[2], this._components[3])
}
set hostname(value) {
if (hasOpaquePath(this)) {
return
}
this._update(this._replace(value, this._components[2], this._components[3]))
}
// https://url.spec.whatwg.org/#dom-url-port
get port() {
return this._slice(this._components[3] + 1 /* : */, this._components[5])
}
set port(value) {
if (cannotHaveCredentialsOrPort(this)) {
return
}
let start = this._components[3] + 1 /* : */
if (this.port === '') {
value = ':' + value
start--
}
this._update(this._replace(value, start, this._components[5]))
}
// https://url.spec.whatwg.org/#dom-url-pathname
get pathname() {
return this._slice(this._components[5], this._components[6] - 1 /* ? */)
}
set pathname(value) {
if (hasOpaquePath(this)) {
return
}
if (value[0] !== '/' && value[0] !== '\\') {
value = '/' + value
}
this._update(
this._replace(value, this._components[5], this._components[6] - 1 /* ? */)
)
}
// https://url.spec.whatwg.org/#dom-url-search
get search() {
return this._slice(
this._components[6] - 1 /* ? */,
this._components[7] - 1 /* # */
)
}
set search(value) {
if (value && value[0] !== '?') value = '?' + value
this._update(
this._replace(
value,
this._components[6] - 1 /* ? */,
this._components[7] - 1 /* # */
)
)
}
// https://url.spec.whatwg.org/#dom-url-hash
get hash() {
return this._slice(this._components[7] - 1 /* # */)
}
set hash(value) {
if (value && value[0] !== '#') value = '#' + value
this._update(this._replace(value, this._components[7] - 1 /* # */))
}
toString() {
return this._href
}
toJSON() {
return this._href
}
[Symbol.for('bare.inspect')]() {
return {
__proto__: { constructor: URL },
href: this.href,
protocol: this.protocol,
username: this.username,
password: this.password,
host: this.host,
hostname: this.hostname,
port: this.port,
pathname: this.pathname,
search: this.search,
hash: this.hash
}
}
_slice(start, end = this._href.length) {
return this._href.slice(start, end)
}
_replace(replacement, start, end = this._href.length) {
return this._slice(0, start) + replacement + this._slice(end)
}
_parse(href, base, shouldThrow) {
try {
this._href = binding.parse(
String(href),
base ? String(base) : null,
this._components,
shouldThrow
)
} catch (err) {
if (err instanceof TypeError) throw err
throw errors.INVALID_URL()
}
}
_update(href) {
try {
this._parse(href, null, true)
} catch (err) {
if (err instanceof TypeError) throw err
}
}
}
// https://url.spec.whatwg.org/#url-opaque-path
function hasOpaquePath(url) {
return url.pathname[0] !== '/'
}
// https://url.spec.whatwg.org/#cannot-have-a-username-password-port
function cannotHaveCredentialsOrPort(url) {
return url.hostname === '' || url.protocol === 'file:'
}
const URL = exports
exports.URL = URL // For Node.js compatibility
exports.isURL = function isURL(value) {
if (typeof value !== 'object' || value === null) return false
let constructor = value.constructor
while (typeof constructor === 'function') {
if (binding.isTagged(constructor)) return true
constructor = Reflect.getPrototypeOf(constructor)
}
return false
}
exports.parse = function parse(input, base) {
const url = new URL(input, base, { throw: false })
return url.href ? url : null
}
exports.canParse = function canParse(input, base) {
return binding.canParse(String(input), base ? String(base) : null)
}
exports.fileURLToPath = function fileURLToPath(url) {
if (typeof url === 'string') {
url = new URL(url)
}
if (url.protocol !== 'file:') {
throw errors.INVALID_URL_SCHEME('The URL must use the file: protocol')
}
if (isWindows) {
if (/%2f|%5c/i.test(url.pathname)) {
throw errors.INVALID_FILE_URL_PATH(
'The file: URL path must not include encoded \\ or / characters'
)
}
} else {
if (url.hostname) {
throw errors.INVALID_FILE_URL_HOST(
"The file: URL host must be 'localhost' or empty"
)
}
if (/%2f/i.test(url.pathname)) {
throw errors.INVALID_FILE_URL_PATH(
'The file: URL path must not include encoded / characters'
)
}
}
const pathname = path.normalize(decodeURIComponent(url.pathname))
if (isWindows) {
if (url.hostname) return '\\\\' + url.hostname + pathname
const letter = pathname.charCodeAt(1) | 0x20
if (
letter < 0x61 /* a */ ||
letter > 0x7a /* z */ ||
pathname.charCodeAt(2) !== 0x3a /* : */
) {
throw errors.INVALID_FILE_URL_PATH('The file: URL path must be absolute')
}
return pathname.slice(1)
}
return pathname
}
exports.pathToFileURL = function pathToFileURL(pathname) {
let resolved = path.resolve(pathname)
if (pathname[pathname.length - 1] === '/') {
resolved += '/'
} else if (isWindows && pathname[pathname.length - 1] === '\\') {
resolved += '\\'
}
resolved = resolved
.replaceAll('%', '%25') // Must be first
.replaceAll('#', '%23')
.replaceAll('?', '%3f')
.replaceAll('\n', '%0a')
.replaceAll('\r', '%0d')
.replaceAll('\t', '%09')
if (!isWindows) {
resolved = resolved.replaceAll('\\', '%5c')
}
return new URL('file:' + resolved)
}