/
browser.script.ts
98 lines (85 loc) · 3.04 KB
/
browser.script.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { DefaultRubyVM } from "./browser.js";
export const main = async (
pkg: { name: string; version: string },
options?: Parameters<typeof DefaultRubyVM>[1],
) => {
const response = fetch(
`https://cdn.jsdelivr.net/npm/${pkg.name}@${pkg.version}/dist/ruby+stdlib.wasm`,
);
const module = await compileWebAssemblyModule(response);
const { vm } = await DefaultRubyVM(module, options);
vm.printVersion();
globalThis.rubyVM = vm;
// Wait for the text/ruby script tag to be read.
// It may take some time to read ruby+stdlib.wasm
// and DOMContentLoaded has already been fired.
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", () =>
runRubyScriptsInHtml(vm),
);
} else {
runRubyScriptsInHtml(vm);
}
};
const runRubyScriptsInHtml = async (vm) => {
const tags = document.querySelectorAll('script[type="text/ruby"]');
// Get Ruby scripts in parallel.
const promisingRubyScripts = Array.from(tags).map((tag) =>
loadScriptAsync(tag),
);
// Run Ruby scripts sequentially.
for await (const script of promisingRubyScripts) {
if (script) {
const { scriptContent, evalStyle } = script;
switch (evalStyle) {
case "async":
vm.evalAsync(scriptContent);
break;
case "sync":
vm.eval(scriptContent);
break;
}
}
}
};
const deriveEvalStyle = (tag: Element): "async" | "sync" => {
const rawEvalStyle = tag.getAttribute("data-eval") || "sync";
if (rawEvalStyle !== "async" && rawEvalStyle !== "sync") {
console.warn(
`data-eval attribute of script tag must be "async" or "sync". ${rawEvalStyle} is ignored and "sync" is used instead.`,
);
return "sync";
}
return rawEvalStyle;
};
const loadScriptAsync = async (
tag: Element,
): Promise<{ scriptContent: string; evalStyle: "async" | "sync" } | null> => {
const evalStyle = deriveEvalStyle(tag);
// Inline comments can be written with the src attribute of the script tag.
// The presence of the src attribute is checked before the presence of the inline.
// see: https://html.spec.whatwg.org/multipage/scripting.html#inline-documentation-for-external-scripts
if (tag.hasAttribute("src")) {
const url = tag.getAttribute("src");
const response = await fetch(url);
if (response.ok) {
return { scriptContent: await response.text(), evalStyle };
}
return Promise.resolve(null);
}
return Promise.resolve({ scriptContent: tag.innerHTML, evalStyle });
};
// WebAssembly.compileStreaming is a relatively new API.
// For example, it is not available in iOS Safari 14,
// so check whether WebAssembly.compileStreaming is available and
// fall back to the existing implementation using WebAssembly.compile if not.
const compileWebAssemblyModule = async function (
response: Promise<Response>,
): Promise<WebAssembly.Module> {
if (!WebAssembly.compileStreaming) {
const buffer = await (await response).arrayBuffer();
return WebAssembly.compile(buffer);
} else {
return WebAssembly.compileStreaming(response);
}
};