Spaces:
Running
Running
Download fastloader.js from EncryptidSystems/EdgeAI: direct link, hf CLI and curl.
- Browser
- Download file 5.12 kB
-
https://huggingface.co/spaces/EncryptidSystems/EdgeAI/resolve/main/fastloader.js
- Command line
-
hf download hf://spaces/EncryptidSystems/EdgeAI/fastloader.js
-
curl -L -o fastloader.js https://huggingface.co/spaces/EncryptidSystems/EdgeAI/resolve/main/fastloader.js
5.12 kB
| // fastloader.js is the browser side of fastloader. It has two paths: | |
| // | |
| // compile / instantiate the page compiles the guest (ai.encryptid.systems) | |
| // connect the server keeps the instance (encryptid.systems) | |
| // | |
| // compile runs once per URL per page. It uses the streaming compiler when | |
| // the bytes are plain wasm. When they need peeling (opts.peel, such as | |
| // WASMJsLoader's guestCore), the peeled core is kept in Cache Storage | |
| // under the response's ETag. The next load skips peeling and compiles that | |
| // core with the streaming compiler too. | |
| // | |
| // import { guestCore } from "./loader.js"; | |
| // import { compile, connect } from "./fastloader.js"; | |
| // const mod = await compile("guest/cmdterm.wasm", { peel: guestCore }); | |
| // const api = connect(""); | |
| // await api.call("add", "add", [2, 3]); // "5" | |
| const STORE = "fastloader-v1"; | |
| const memo = new Map(); | |
| // compile returns a WebAssembly.Module for url. Concurrent and repeat calls | |
| // share one compile. opts.peel(Uint8Array) -> Uint8Array strips a wrapper. | |
| // opts.store names the Cache Storage bucket, or false for none. | |
| export function compile(url, opts = {}) { | |
| const key = new URL(url, baseURL()).href; | |
| let p = memo.get(key); | |
| if (!p) { | |
| p = compileOnce(key, opts); | |
| p.catch(() => memo.delete(key)); | |
| memo.set(key, p); | |
| } | |
| return p; | |
| } | |
| // instantiate compiles url (once) and instantiates it with imports. | |
| export async function instantiate(url, imports = {}, opts = {}) { | |
| const mod = await compile(url, opts); | |
| return WebAssembly.instantiate(mod, imports); | |
| } | |
| // preload starts compiles now so a later compile or instantiate is warm. | |
| export function preload(urls, opts = {}) { | |
| return Promise.all(urls.map((u) => compile(u, opts))); | |
| } | |
| // forget drops url from this page's memo and from the store. | |
| export async function forget(url, opts = {}) { | |
| const key = new URL(url, baseURL()).href; | |
| memo.delete(key); | |
| const store = await openStore(opts.store); | |
| if (store) await dropCores(store, key, ""); | |
| } | |
| async function compileOnce(url, opts) { | |
| const res = await fetch(url); | |
| if (!res.ok) throw new Error("fastloader: fetch " + url + " " + res.status); | |
| const tag = res.headers.get("etag") || res.headers.get("last-modified") || ""; | |
| const store = opts.peel && tag ? await openStore(opts.store) : null; | |
| const coreKey = store ? coreURL(url, tag) : ""; | |
| if (store) { | |
| const hit = await store.match(coreKey); | |
| if (hit) { | |
| cancel(res); | |
| return WebAssembly.compileStreaming(hit); | |
| } | |
| } | |
| // Plain wasm streams straight into the compiler. A wrapped guest can be | |
| // a valid module itself, so with peel the bytes are always peeled. | |
| if (!opts.peel && isWasm(res)) return WebAssembly.compileStreaming(res); | |
| const raw = new Uint8Array(await res.arrayBuffer()); | |
| const core = opts.peel ? opts.peel(raw) : raw; | |
| if (store) { | |
| // Stored after the compile starts; this load does not wait on it. | |
| // A quota error or private window just means no stored core. | |
| dropCores(store, url, coreKey) | |
| .then(() => store.put(coreKey, new Response(core, { headers: { "Content-Type": "application/wasm" } }))) | |
| .catch(() => {}); | |
| } | |
| return WebAssembly.compile(core); | |
| } | |
| // connect calls exports on an instance the fastloader server keeps open. | |
| // call sends args as strings and returns the export's result as text. | |
| // abi: "string" selects the packed string ABI (compileGML). | |
| export function connect(base = "") { | |
| const root = String(base).replace(/\/+$/, ""); | |
| return { | |
| async modules() { | |
| const res = await fetch(root + "/modules"); | |
| if (!res.ok) throw new Error("fastloader: modules " + res.status + " " + (await res.text())); | |
| return res.json(); | |
| }, | |
| async call(module, fn, args = [], opts = {}) { | |
| const q = opts.abi === "string" ? "?abi=string" : ""; | |
| const res = await fetch(root + "/" + encodeURIComponent(module) + "/" + encodeURIComponent(fn) + q, { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify(args.map(String)), | |
| }); | |
| const text = await res.text(); | |
| if (!res.ok) throw new Error("fastloader: " + module + "." + fn + ": " + text.trim()); | |
| return text; | |
| }, | |
| }; | |
| } | |
| function baseURL() { | |
| return typeof location !== "undefined" ? location.href : "http://localhost/"; | |
| } | |
| function isWasm(res) { | |
| return (res.headers.get("content-type") || "").split(";")[0].trim() === "application/wasm"; | |
| } | |
| function cancel(res) { | |
| if (res.body) res.body.cancel().catch(() => {}); | |
| } | |
| function coreURL(url, tag) { | |
| return url + (url.includes("?") ? "&" : "?") + "fastloader-core=" + encodeURIComponent(tag); | |
| } | |
| async function openStore(name) { | |
| if (name === false || typeof caches === "undefined") return null; | |
| try { | |
| return await caches.open(name || STORE); | |
| } catch (_) { | |
| return null; | |
| } | |
| } | |
| // dropCores deletes stored cores of url other than keep. | |
| async function dropCores(store, url, keep) { | |
| const prefix = coreURL(url, ""); | |
| for (const req of await store.keys()) { | |
| if (req.url.startsWith(prefix) && req.url !== keep) await store.delete(req); | |
| } | |
| } | |