@astx/runtime
v3.1.1
Published
Runtime library for a tiny JS AST based binary file format
Readme
@astx/runtime
Decodes and executes ASTX binary files. Part of the ASTX monorepo.
Installation
npm install @astx/runtimeAPI
loadFromBuffer(buffer, opts?): Promise<CompiledProgram>
Decodes an ASTX binary from a Uint8Array or Buffer.
import { loadFromBuffer, run } from "@astx/runtime";
const response = await fetch("/app.astx");
const bytes = new Uint8Array(await response.arrayBuffer());
const program = await loadFromBuffer(bytes);
run(program);Options (LoadBufferOptions):
| Option | Type | Default | Description |
|---|---|---|---|
| codec | AstxCodec | Node.js built-in zstd | Custom decompression implementation |
| dict | Uint8Array | — | Zstd dictionary (must match the one used when compiling) |
loadFromFile(filename, opts?): Promise<CompiledProgram>
Reads an .astx file from disk and decodes it. Node.js only.
import { loadFromFile, run } from "@astx/runtime";
const program = await loadFromFile("app.astx");
run(program, { mode: "vm" });run(program, opts?)
Executes a decoded CompiledProgram.
run(program, {
mode: "vm", // 'vm' (Node.js vm module) | 'eval' (direct eval)
inject: { // variables injected into the program's scope
__dirname: "/app",
__filename: "/app/index.astx",
},
});generateJSCode(program): string
Converts a decoded CompiledProgram back to JavaScript source. Useful for debugging — the output is not minified or human-readable.
import { loadFromFile, generateJSCode } from "@astx/runtime";
const program = await loadFromFile("app.astx");
console.log(generateJSCode(program));Browser support
All APIs are browser-compatible. The default codec uses node:zlib via a dynamic import — in a browser you must supply a custom AstxCodec:
import { decompress } from "fzstd"; // pure-JS Zstd decompressor
import { loadFromBuffer, run } from "@astx/runtime";
const bytes = new Uint8Array(await (await fetch("/app.astx")).arrayBuffer());
const program = await loadFromBuffer(bytes, {
codec: {
compress: async () => { throw new Error("not needed"); },
decompress: async (data) => decompress(data),
},
});
run(program);Known limitations
- Relative
require/importpaths – resolved relative to the working directory of the host process, not the.astxfile's original location. Use theinjectoption to set__dirname/__filenamewhen usingvmmode. - Dynamic
import()inside.astx– partially supported invmmode.
License
GPL-3.0 — see LICENSE.
