ITADN
hon-sh/goqjs
hon-sh/goqjs · 文件
文件最后提交记录最后更新时间
README.md

hon

hon is a Go module that embeds a QuickJS-backed JavaScript runtime in-process. Go owns the host (scheduling, networking, app logic); QuickJS runs the event loop (js_std_loop). You can pool multiple runtimes and inject host APIs (console, fetch, HTTP helpers, or your own) via stdlib / Inject*.

Good fit: embedding JS inside a Go service — React SSR, plugins, or per-request scripts — especially workloads that mix lots of await I/O with moderate render/CPU work.

Not a fit: replacing Node/Bun as a general JS platform, or expecting one isolate to crush heavy CPU-bound jobs (scale those with -c N pools, or keep the heavy work in Go).

  • Module: github.com/hon-go/hon
  • CLIs: gohon, gohon-serve (the bare name hon is reserved for a Zig + QuickJS sibling)

See docs/seed-1.md (engine) and docs/seed-2.md (host / stdlib).

Setup

QuickJS 2026-06-04 is vendored under third_party/quickjs/.

How it works

  • C/QuickJS owns the event loop (js_std_loop).
  • Core boot (in Go) exposes timers + invoke — not console / fetch / HTTP.
  • stdlib.Install injects host APIs before the first Run; later injection panics.
  • pool.New builds N workers; serve wraps user run(req,res) and binds each request by id.

Examples

CLI (gohon)

go run ./cmd/gohon -f examples/sleep.js 3 5 6
go run ./cmd/gohon -c 2 -f examples/fib.js 32 33 34 35
make test

-c N starts an N-wide Runtime pool. Exactly one of -e / -f supplies the JS function expression for run. Remaining args each start one concurrent pool.Run(arg).

cmd/gohon installs stdlib console and a convenience sleep.

examplerole
examples/sleep.jsasync timer multiplexing
examples/fib.jsCPU-bound; use -c 2
examples/fact.jsBigInt factorial — CPU-bound

HTTP (gohon-serve)

go run ./cmd/gohon-serve -f examples/serve-hello.js
go run ./cmd/gohon-serve -c 2 -f examples/serve-sleep.js -addr :8080
curl -s localhost:8080/hi
curl -s 'localhost:8080/fib?n=20'   # with serve-fib.js

JS entry is async function(req, res) { ... }. Go locates the response via req_id (not raw fds).

examplerole
examples/serve-hello.jsmethod/path echo
examples/serve-sleep.jsasync sleep then chunked write
examples/serve-fib.jsCPU fib from ?n=

HN SSR (Vite + React)

Minimal Hacker News SSR demo lives in a separate repo: hon-go/hn-ssr.