Cotton code
26 lines · 1.1 KB · TypeScriptRaw
/** * The Worker's rules module in Node: the same checked-in WASM * (apps/business/src/rules/wasm/, built by `pnpm rules:wasm` from * crates/cotton-rules), loaded from its file, so a script on the Mac runs * the rule the Worker and the phone run, never a copy of it. One door, as * the Worker's apps/business/src/rules/index.ts: a rule's name and its * input, its answer, or its refusal thrown in the rule's words. */import { readFileSync } from "node:fs";import { join } from "node:path";import { call, initSync } from "../../apps/business/src/rules/wasm/cotton_rules.js";import { ROOT } from "./stamp";let ready = false;/** Run `name` over `input`: its answer; a refusal throws in the rule's words. */export function rule<T>(name: string, input: unknown): T {  if (!ready) {    initSync({ module: readFileSync(join(ROOT, "apps/business/src/rules/wasm/cotton_rules_bg.wasm")) });    ready = true;  }  const out = JSON.parse(call(name, JSON.stringify(input))) as { ok: T } | { error: string };  if ("error" in out) throw new Error(`${name}: ${out.error}`);  return out.ok;}