nodejs

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 27, 2026 License: MIT Imports: 54 Imported by: 0

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

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

func (rt *Runtime) Close() error

Close releases host resources (open HTTP servers included); the interpreter stays usable.

func (*Runtime) ExitCode

func (rt *Runtime) ExitCode() int

func (*Runtime) Exited

func (rt *Runtime) Exited() bool

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

func (rt *Runtime) HTTPHandler() (http.Handler, bool)

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

func (rt *Runtime) RunScript(ctx context.Context, src string) (spidermonkey.Result, error)

RunScript evaluates src as a classic script (require available) and then runs the event loop to completion.

func (*Runtime) Wait

func (rt *Runtime) Wait(ctx context.Context) error

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.

func (*Runtime) Web

func (rt *Runtime) Web() *web.Web

Web returns the underlying compat/web installation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL