Denox.Run.Base behaviour (denox v0.7.0)

Copy Markdown View Source

Shared GenServer dispatch logic for Run modules.

This module provides a __using__ macro that injects GenServer boilerplate, a common public API, and stdout dispatch logic for both the NIF-backed Denox.Run and the CLI-backed Denox.CLI.Run.

Public API (injected into using modules)

Lifecycle: start_link/1, stop/1

I/O: send/2, recv/2, send_and_recv/3

Pub/Sub: subscribe/1, unsubscribe/1

Introspection: alive?/1, os_pid/1

Convenience (one-shot):

  • capture/1 — start, collect all lines, stop; returns {:ok, [String.t()]}
  • stream/1 — start + lazy Stream (auto-stops on halt)
  • stream_from/2 — lazy Stream from an existing PID (caller manages lifecycle)
  • with_runtime/2 — start + user function + guaranteed stop via after

Implementing a Backend

Modules that use Denox.Run.Base, backend: :my_backend must implement four callbacks:

See Denox.Run (NIF backend) and Denox.CLI.Run (subprocess backend) for concrete implementations.

Summary

Functions

Resolve a specifier to a form suitable for the Deno runtime.

Callbacks

alive_backend?(backend_state)

@callback alive_backend?(backend_state :: term()) :: boolean()

init_backend(keyword)

@callback init_backend(keyword()) :: {:ok, backend_state :: term()} | {:error, term()}

send_backend(backend_state, t)

@callback send_backend(backend_state :: term(), String.t()) :: :ok | {:error, term()}

stop_backend(backend_state)

@callback stop_backend(backend_state :: term()) :: :ok

Functions

resolve_specifier(spec)

@spec resolve_specifier(String.t()) :: String.t()

Resolve a specifier to a form suitable for the Deno runtime.

Rules

  • Specifiers already prefixed with npm:, jsr:, http://, https://, or file:// are passed through unchanged.
  • Scoped package names starting with @ are prefixed with npm:.
  • Everything else is returned as-is (treated as a file path by the backend).

Examples

iex> Denox.Run.Base.resolve_specifier("npm:cowsay")
"npm:cowsay"

iex> Denox.Run.Base.resolve_specifier("@scope/pkg")
"npm:@scope/pkg"

iex> Denox.Run.Base.resolve_specifier("server.ts")
"server.ts"