forked from b-fuze/deno-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deno-dom-native.ts
65 lines (51 loc) · 1.71 KB
/
deno-dom-native.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
63
64
import {
prepare,
PerpareOptions,
} from "https://raw.githubusercontent.com/b-fuze/deno-plugin-prepare/master/mod.ts";
import { register } from "./src/parser.ts";
const nativeEnv = "DENO_DOM_PLUGIN";
let denoNativePluginPath: string | undefined;
// Try to read the environment
try {
denoNativePluginPath = Deno.env.get(nativeEnv);
} catch {}
if (denoNativePluginPath) {
// Open native plugin and register the native `parse` function
Deno.openPlugin(denoNativePluginPath);
} else {
const releaseUrl = "https://github.com/b-fuze/deno-dom/releases/download/v0.1.2-alpha2";
const pluginOptions: PerpareOptions = {
name: "test_plugin",
// Whether to output log. Optional, default is true
printLog: false,
// Whether to use locally cached files. Optional, default is true
// checkCache: true,
// Support "http://", "https://", "file://"
urls: {
darwin: releaseUrl + "/libplugin.dylib",
windows: releaseUrl + "/plugin.dll",
linux: releaseUrl + "/libplugin.so",
},
};
await prepare(pluginOptions);
}
type pluginId = number;
interface DenoCore {
ops(): {
[opName: string]: number;
};
dispatch(plugin: pluginId, ...data: Uint8Array[]): Uint8Array;
};
const core = <DenoCore> (<any> <unknown> Deno).core;
const { denoDomParseSync, denoDomParseFragSync } = core.ops();
const encoder = new TextEncoder();
const decoder = new TextDecoder();
function parse(html: string): string {
return decoder.decode(core.dispatch(denoDomParseSync, encoder.encode(html)));
}
function parseFrag(html: string): string {
return decoder.decode(core.dispatch(denoDomParseFragSync, encoder.encode(html)));
}
// Register parse function
register(parse, parseFrag);
export * from "./src/api.ts";