-
Notifications
You must be signed in to change notification settings - Fork 18
/
util.ts
62 lines (49 loc) · 1.8 KB
/
util.ts
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
export const assert = (value, msg) => value || ERR(msg)
export const NOOP = () => {}
export const ERR = (...msgs): any => {
throw new Error(msgs.join(' '))
}
export const TODO = () => ERR('TODO')
export const UNSUPPORTED = () => ERR('UNSUPPORTED')
export const UNREACHABLE = () => ERR('UNREACHABLE')
export const last = arr => arr[arr.length - 1]
export const LITTLE_ENDIAN = (() => {
let b = new ArrayBuffer(2)
new DataView(b).setInt16(0, 256, true)
return new Int16Array(b)[0] === 256
})()
export const camelCase = name => name.replace(/\-[a-zA-Z]/g, match => match.slice(1).toUpperCase())
export const kebabCase = name => name.replace(/[A-Z]/g, match => '-' + match.toLowerCase())
export const pascalCase = name => ((name = camelCase(name)), name[0].toUpperCase() + name.slice(1))
export const PLATFORM = globalThis.Deno?.build.os ?? (await import('os')).platform().replace(/win32/, 'windows')
export const Worker =
globalThis.Worker ??
class Worker extends (await import('worker_threads')).Worker {
addEventListener(ev, listener) {
this.on(ev, data => listener({ data }))
}
}
// @ts-expect-error
export const fetch = globalThis.fetch ?? (await import('node-fetch')).default
export const readTextFile =
globalThis.Deno?.readTextFile ??
(async path => {
const fs = await import('fs/promises')
return fs.readFile(path, 'utf-8')
})
export async function readURL(url) {
url = new URL(url)
if (url.protocol === 'data:') {
return TODO()
}
if (url.protocol === 'file:') {
let path: string
if (PLATFORM === 'windows') path = url.href.replace('file:///', '') // ~~Windows dirty fix~~ Maybe better
else path = url.pathname
return readTextFile(path)
}
if (url.protocol.match(/^https?:$/)) {
return fetch(url).then(res => res.text())
}
return UNSUPPORTED()
}