-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.ts
50 lines (38 loc) · 1.25 KB
/
lib.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
const VERSION: string = "v0.2.1";
// NOTE: 2 debug/test local libs set env var DENO_PLUGINS to a directory
// containg a shared lib for your os
const PLUGINS: string = Deno.env().DENO_PLUGINS?.trim()?.replace(/\/$/, "") ||
".deno_plugins";
const PLUGIN_NAME: string = (Deno.build.os === "win" ? "" : "lib") +
"deno_libhydrogen" +
(Deno.build.os === "win"
? ".dll"
: Deno.build.os === "mac" ? ".dylib" : ".so");
const REMOTE: string =
`https://github.com/chiefbiiko/deno-libhydrogen/releases/download/${VERSION}/${PLUGIN_NAME}`;
const LOCAL: string = `${PLUGINS}/${PLUGIN_NAME}`;
function exists(file: string): boolean {
try {
Deno.statSync(file);
return true;
} catch (err) {
if (err.name === "NotFound") {
return false;
} else {
throw err;
}
}
}
async function maybe_fetch(): Promise<void> {
if (!exists(LOCAL)) {
const response: Response = await fetch(REMOTE);
if (!response.ok) {
throw Error(`unable to locate plugin @ ${REMOTE}`);
}
const arr_buf: ArrayBuffer = await response.arrayBuffer();
await Deno.mkdir(PLUGINS, { recursive: true });
await Deno.writeFile(LOCAL, new Uint8Array(arr_buf));
}
}
await maybe_fetch();
export const plugin: Deno.Plugin = Deno.openPlugin(LOCAL);