-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlink.js
77 lines (67 loc) · 1.51 KB
/
link.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
const unixresolve = require('unix-path-resolve')
exports.stringify = function stringify (o) {
const p = o.protocol ? o.protocol + '://' + o.transform : ''
if (!o.resolve && !o.dirname && !o.filename) return p
if (o.resolve) return p + o.dirname + '/~' + o.resolve
return p + o.filename
}
exports.parse = function parse (l) {
const extra = l.lastIndexOf('?')
let refresh = false
if (extra > -1) {
refresh = /[&?]refresh($|=)/.test(l.slice(extra))
l = l.slice(0, extra)
}
l = decodeURI(l)
let protocol = null
let transform = null
if (l.startsWith('~')) {
return {
protocol,
transform,
resolve: l.slice(1),
dirname: '/',
filename: null,
refresh
}
}
if (!l.startsWith('/')) {
const i = l.indexOf(':')
if (i === -1) throw new Error('Invalid identifier')
protocol = l.slice(0, i)
l = l.slice(i).replace(/^:\/?\/?/, '')
const j = l.indexOf('/')
if (j === -1) {
transform = l
return {
protocol,
transform,
resolve: null,
dirname: null,
filename: null,
refresh
}
}
transform = l.slice(0, j)
l = l.slice(j)
}
const i = l.indexOf('/~')
if (i > -1) {
return {
protocol,
transform,
resolve: l.slice(i + 2),
dirname: i === 0 ? '/' : unixresolve(l.slice(0, i)),
filename: null,
refresh
}
}
return {
protocol,
transform,
resolve: null,
dirname: null,
filename: unixresolve(l),
refresh
}
}