Documentation
¶
Overview ¶
Package nodejs is the Node.js compatibility layer (docs/nodejs-compat-plan.md Phase 3): the node: core modules (path, events, util, buffer, fs, ...), process with Node's nextTick ordering, Buffer, CommonJS require with node_modules resolution, and ESM⇄CJS interop — installed explicitly:
js, _ := spidermonkey.New(spidermonkey.Config{FS: os.DirFS(appDir)})
rt, err := nodejs.Install(js) // installs compat/web too
rt.RunScript(ctx, `const _ = require("lodash"); ...`)
rt.Wait(ctx)
The standalone ESMLoader stays available for web-only embedders that just need pure-ESM npm imports without the Node runtime.
Index ¶
- func ESMLoader(cfg spidermonkey.Config, specifier, referrer string) (string, error)
- type Options
- type Runtime
- func (rt *Runtime) Close() error
- func (rt *Runtime) ExitCode() int
- func (rt *Runtime) Exited() bool
- func (rt *Runtime) HTTPHandler() (http.Handler, bool)
- func (rt *Runtime) RunModule(ctx context.Context, specifier, src string) (spidermonkey.ModuleResult, error)
- func (rt *Runtime) RunScript(ctx context.Context, src string) (spidermonkey.Result, error)
- func (rt *Runtime) Wait(ctx context.Context) error
- func (rt *Runtime) Web() *web.Web
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ESMLoader ¶
func ESMLoader(cfg spidermonkey.Config, specifier, referrer string) (string, error)
ESMLoader is a spidermonkey.ModuleLoader for PURE-ESM resolution over Config.FS: relative/absolute paths (with extension fallbacks) and bare npm specifiers via node_modules (exports maps, worker/browser/import conditions, node_modules walk-up). A bare or renamed specifier answers with a shim re-exporting the resolved file, so the real file registers under its own path and ITS relative imports resolve correctly.
CommonJS files are rejected here — that interop needs the full runtime (nodejs.Install, whose loader supersedes this one). Access control lives in Config.FS.
Types ¶
type Options ¶
type Options struct {
// Argv becomes process.argv. Empty means ["node", "main"].
Argv []string
}
Options configures Install.
type Runtime ¶
type Runtime struct {
// contains filtered or unexported fields
}
Runtime is one Node.js-compatible installation on one interpreter.
func Install ¶
func Install(js *spidermonkey.JS, opts ...Options) (*Runtime, error)
Install sets up the Node runtime on js (installing compat/web itself — do not call web.Install separately on the same interpreter).
func (*Runtime) Close ¶
Close releases host resources (open HTTP servers included); the interpreter stays usable.
func (*Runtime) Exited ¶
ExitCode reports the code passed to process.exit() (0 if it exited without an explicit code); Exited reports whether process.exit() was called at all.
func (*Runtime) HTTPHandler ¶
HTTPHandler returns an http.Handler for the single HTTP server the guest has started with http.createServer(...).listen(...), so an embedder can serve that Node app through its own server (or httptest) instead of reaching the guest's listener over a socket. It reports false until exactly one server is running (dispatch is independent of the guest's own listener).
func (*Runtime) RunModule ¶
func (rt *Runtime) RunModule(ctx context.Context, specifier, src string) (spidermonkey.ModuleResult, error)
RunModule evaluates src as an ES module registered under specifier and then runs the event loop to completion. A path-shaped specifier registers under its file:// URL (see entrySpecifier), which is what import.meta.url reports inside the module.
func (*Runtime) RunScript ¶
RunScript evaluates src as a classic script (require available) and then runs the event loop to completion.
func (*Runtime) Wait ¶
Wait runs the event loop until timers, immediates, nextTicks and pending ops are exhausted, or ctx is done. A process.exit() during the loop stops it and returns cleanly (Exited/ExitCode report the outcome). When the loop drains naturally it fires process 'beforeExit' (draining again if a handler scheduled work), and it fires 'exit' once on termination.