-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathurls.js
More file actions
54 lines (48 loc) · 1.66 KB
/
urls.js
File metadata and controls
54 lines (48 loc) · 1.66 KB
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
import { parse } from 'url'
import { join } from 'path'
import { readFileSync, existsSync } from 'fs'
const SCHEME_REGEX = /[a-z]+:\/\//i
// 1 2 3 4
const VERSION_REGEX = /^(hyper:\/\/)?([^/]+)(\+[^/]+)(.*)$/i
export function parseHyperUrl (str, parseQS) {
// prepend the scheme if it's missing and if the url has 64 characters (and therefore is not an alias)
if (!SCHEME_REGEX.test(str) && str.length === 64) {
str = 'hyper://' + str
}else{
str = join(os.homedir(),".hyperdrive",str)
if (existsSync(str)){
str = readFileSync(str)
} else {
throw new Error("Invalid url or alias")
}
}
var parsed, version = null, match = VERSION_REGEX.exec(str)
if (match) {
// run typical parse with version segment removed
parsed = parse((match[1] || '') + (match[2] || '') + (match[4] || ''), parseQS)
version = match[3].slice(1)
} else {
parsed = parse(str, parseQS)
}
parsed.href = str // overwrite href to include actual original
if (!parsed.query && parsed.searchParams) {
parsed.query = Object.fromEntries(parsed.searchParams) // to match node
}
parsed.version = version // add version segment
if (!parsed.origin) parsed.origin = `hyper://${parsed.hostname}/`
return parsed
}
export function urlToKey (url) {
return Buffer.from(/([0-9a-f]{64})/i.exec(url)[1], 'hex')
}
export function fromURLToKeyStr (url) {
try {
return /([0-9a-f]{64})/i.exec(url)[1]
} catch (e) {
throw new Error(`Invalid hyper:// URL, ${url}`)
}
}
export function fromPathToHyperbeeKeyList (path) {
var parts = path.split('/').filter(Boolean)
return parts.map(part => decodeURIComponent(part))
}