GenServer-based pool of JavaScript runtimes for concurrent workloads.
V8 isolates are single-threaded, so the pool round-robins requests across N runtimes to achieve parallelism.
Usage
# In your supervision tree
children = [
{Denox.Pool, name: :js_pool, size: 4, permissions: :all}
]
# Then use the pool
{:ok, result} = Denox.Pool.eval(:js_pool, "1 + 2")
{:ok, result} = Denox.Pool.eval_ts(:js_pool, "const x: number = 42; x")Pre-loading shared code
Use load_npm/2 to load a bundled JS file into all pool runtimes at once.
This is the recommended way to share npm packages across a pool:
Denox.Npm.bundle!("npm:zod@3.22", "priv/bundles/zod.js")
:ok = Denox.Pool.load_npm(:js_pool, "priv/bundles/zod.js")Or use exec/2 to initialise shared globals:
:ok = Denox.Pool.exec(:js_pool, "globalThis.helper = (x) => x * 2")Note that each runtime in the pool has independent state, so mutations made
via exec/2 are distributed round-robin and may not reach all runtimes.
Use load_npm/2 (which broadcasts to all runtimes) for shared initialisation.
Summary
Functions
Call a named JavaScript function with arguments.
Call a named async JavaScript function with arguments, returning a Task.
Call a named async JavaScript function and decode the JSON result, returning a Task.
Call a named JavaScript function and decode the JSON result.
Returns a child specification for the pool, using the :name option as the child id.
Evaluate JavaScript code using the next runtime in the pool.
Evaluate JavaScript code asynchronously, returning a Task.
Evaluate JavaScript asynchronously and decode JSON result, returning a Task.
Evaluate and decode JSON result.
Read and evaluate a JavaScript or TypeScript file.
Read and evaluate a JavaScript or TypeScript file asynchronously, returning a Task.
Read and evaluate a JavaScript or TypeScript file asynchronously and decode the JSON result, returning a Task.
Read and evaluate a JavaScript or TypeScript file and decode the JSON result.
Load and evaluate an ES module file. Supports .ts/.js with import/export.
Evaluate TypeScript code using the next runtime in the pool.
Evaluate TypeScript code asynchronously, returning a Task.
Evaluate TypeScript asynchronously and decode JSON result, returning a Task.
Evaluate TypeScript and decode JSON result.
Execute JavaScript code, ignoring the return value.
Execute TypeScript code, ignoring the return value.
Load a bundled JS file into all runtimes in the pool.
Return the pool size.
Start a pool of runtimes.
Types
@type pool() :: GenServer.server()
Functions
Call a named JavaScript function with arguments.
Call a named async JavaScript function with arguments, returning a Task.
Call a named async JavaScript function and decode the JSON result, returning a Task.
Call a named JavaScript function and decode the JSON result.
@spec child_spec(keyword()) :: Supervisor.child_spec()
Returns a child specification for the pool, using the :name option as the child id.
This allows multiple pools to coexist in the same supervisor without id conflicts.
Evaluate JavaScript code using the next runtime in the pool.
Evaluate JavaScript code asynchronously, returning a Task.
Evaluate JavaScript asynchronously and decode JSON result, returning a Task.
Evaluate and decode JSON result.
Read and evaluate a JavaScript or TypeScript file.
Read and evaluate a JavaScript or TypeScript file asynchronously, returning a Task.
Read and evaluate a JavaScript or TypeScript file asynchronously and decode the JSON result, returning a Task.
Read and evaluate a JavaScript or TypeScript file and decode the JSON result.
Load and evaluate an ES module file. Supports .ts/.js with import/export.
Evaluate TypeScript code using the next runtime in the pool.
Evaluate TypeScript code asynchronously, returning a Task.
Evaluate TypeScript asynchronously and decode JSON result, returning a Task.
Evaluate TypeScript and decode JSON result.
Execute JavaScript code, ignoring the return value.
Returns :ok on success. Returns {:error, message} if the JavaScript
throws or if evaluation fails — callers must still handle the error.
Execute TypeScript code, ignoring the return value.
Returns :ok on success. Returns {:error, message} if the code
throws or if evaluation fails — callers must still handle the error.
Load a bundled JS file into all runtimes in the pool.
@spec size(pool()) :: non_neg_integer()
Return the pool size.
@spec start_link(keyword()) :: GenServer.on_start()
Start a pool of runtimes.
Options:
:name- registered name for the pool (required):size- number of runtimes (default:System.schedulers_online()):permissions- permission mode for all runtimes (:all,:none, or keyword list):sandbox- (deprecated) usepermissions: :noneinstead:base_dir- base directory for module resolution:cache_dir- cache directory for remote modules:import_map- map of bare specifiers to resolved URLs/paths:callback_pid- PID that handles JS→Elixir callbacks:snapshot- V8 snapshot binary for faster cold start (applied to all runtimes)