/* One template page (template.html) — external module so the page can ship a
* strict CSP, same as index.html.
*
* The engine is created but not started. createTemplate() calls startPyrel()
* on the first Run, so reading the notebook costs one 40 KB document rather
* than ~100 MB of WebAssembly.
*/
import { createTemplate } from './template.js';
import { createEngine, engineModeFromLocation } from './engine/engine.js';
import { showBootError } from './boot-error.js';
try {
const slug = new URLSearchParams(location.search).get('t');
if (!slug) throw new Error('no template named — open this page from templates.html');
// Slug comes from the URL and is used to build a fetch path, so it is checked
// against the shape the extractor emits rather than trusted.
if (!/^[a-z0-9][a-z0-9_-]*$/i.test(slug)) throw new Error(`not a template name: ${slug}`);
const res = await fetch(`data/templates/${slug}.json`);
if (!res.ok) throw new Error(`no template "${slug}" — ${res.status} ${res.statusText}`);
const doc = await res.json();
document.title = `${doc.title} — Templates — Event Horizon`;
/* wasm by default, not `auto`.
*
* `auto` prefers wasm but probes the sidecar on the way, which on a static
* host is a request that cannot succeed — a 502 in the console of a page that
* is working fine. And the fallback is pointless here: templates need
* prepareTemplate, which only the browser engine has, so falling through to
* the sidecar can only turn a slow start into "the server engine cannot run
* templates". ?engine=server still reaches it, and still says that.
*
* It is also the honest default for what this page claims. "Runs right here,
* in this tab" should not quietly be served by a process somewhere else. */
const engine = createEngine({
mode: new URLSearchParams(location.search).get('engine') ? engineModeFromLocation() : 'wasm',
onProgress: (text) => {
const p = document.querySelector('[data-el="progress"]');
if (p) p.textContent = text;
},
});
window.template = createTemplate({ doc, engine });
} catch (e) {
showBootError(e);
}