NodeJsHostOptions Class

Options used to start a NodeJsHost.

public sealed class NodeJsHostOptions

Remarks

The options are copied when a NodeJsHost or a NodeJsHostPool is started, so changing them afterwards has no effect on it.

Constructors

NameDescription
NodeJsHostOptions()

NodeJsHostOptions()

NodeJsHostOptions.cs
public NodeJsHostOptions()

No documentation is available.

Properties

NameDescription
EnvironmentVariablesGets the environment variables set for the Node.js process. A null value removes the variable.
MaxConcurrentCallsGets or sets the maximum number of calls that run at the same time in a Node.js process. Other calls wait until a call completes. When null, the number of calls is not limited.
NodeArgumentsGets the additional arguments passed to node, such as --max-old-space-size=4096.
NodeExecutablePathGets or sets the path of the node executable. When null, node is searched in the PATH.
StandardErrorReceivedGets or sets a callback invoked for each line written by the Node.js process to its standard error (e.g. console.error). When null, the output is discarded, except the last lines, which are included in the message of the exception thrown when the process exits. Exceptions thrown by the callback are ignored.
StandardOutputReceivedGets or sets a callback invoked for each line written by the Node.js process to its standard output (e.g. console.log). When null, the output is discarded. Exceptions thrown by the callback are ignored.
StartupTimeoutGets or sets the maximum time to wait for the Node.js process to start, or Timeout.InfiniteTimeSpan to wait indefinitely.
UnresponsiveTimeoutGets or sets the maximum time a canceled call can block the event loop of the Node.js process (e.g. with an infinite loop, or with promises that never let the event loop run other callbacks) before the process is killed. When null, the process is never killed.
WorkerThreadsGets or sets the number of worker threads (node:worker_threads) that run the calls, so CPU-bound calls run in parallel in a single Node.js process. When null, the calls run on the main thread of the process.
WorkingDirectoryGets or sets the working directory of the Node.js process. Module specifiers (relative paths and npm packages) are resolved from this directory. When null, the current directory is used.

EnvironmentVariables

NodeJsHostOptions.cs

Gets the environment variables set for the Node.js process. A null value removes the variable.

public IDictionary<string, string?> EnvironmentVariables { get; }

MaxConcurrentCalls

NodeJsHostOptions.cs

Gets or sets the maximum number of calls that run at the same time in a Node.js process. Other calls wait until a call completes. When null, the number of calls is not limited.

public int? MaxConcurrentCalls { get; set; }

Property value

int?

Remarks

Limiting the number of calls bounds the memory and the work queued in the Node.js process. With NodeJsHostPool, calls wait in the pool and run on the first process that becomes available, which balances calls of different durations; 1 is a good value for CPU-bound code.

With NodeJsHostOptions.WorkerThreads, the limit applies to the calls of all the worker threads of the process, and each call that can run is sent to the worker thread with the fewest calls in progress. Use the number of worker threads for CPU-bound code, so a call runs on a worker thread that becomes available instead of waiting for a long call.

A canceled call no longer counts, even if its JavaScript code is still running. A call made using NodeJsHostPool.RunAsync counts as one call of the pool, and each call it makes on the host counts as one call of the host.

Gets the additional arguments passed to node, such as --max-old-space-size=4096.

public IList<string> NodeArguments { get; }

Property value

IList<string>

NodeExecutablePath

NodeJsHostOptions.cs

Gets or sets the path of the node executable. When null, node is searched in the PATH.

public string? NodeExecutablePath { get; set; }

Property value

string?

StandardErrorReceived

NodeJsHostOptions.cs

Gets or sets a callback invoked for each line written by the Node.js process to its standard error (e.g. console.error). When null, the output is discarded, except the last lines, which are included in the message of the exception thrown when the process exits. Exceptions thrown by the callback are ignored.

public Action<string>? StandardErrorReceived { get; set; }

Property value

Action<string>?

StandardOutputReceived

NodeJsHostOptions.cs

Gets or sets a callback invoked for each line written by the Node.js process to its standard output (e.g. console.log). When null, the output is discarded. Exceptions thrown by the callback are ignored.

public Action<string>? StandardOutputReceived { get; set; }

Property value

Action<string>?

StartupTimeout

NodeJsHostOptions.cs

Gets or sets the maximum time to wait for the Node.js process to start, or Timeout.InfiniteTimeSpan to wait indefinitely.

public TimeSpan StartupTimeout { get; set; }

Property value

TimeSpan

UnresponsiveTimeout

NodeJsHostOptions.cs

Gets or sets the maximum time a canceled call can block the event loop of the Node.js process (e.g. with an infinite loop, or with promises that never let the event loop run other callbacks) before the process is killed. When null, the process is never killed.

public TimeSpan? UnresponsiveTimeout { get; set; }

Property value

TimeSpan?

Remarks

Canceling a call does not stop its JavaScript code, and synchronous code blocks all the other calls of the process. Killing the process is the only way to stop it. The calls in progress then fail with a NodeJsException whose NodeJsException.ExitCode is null, and NodeJsHostPool replaces the process the next time a host is selected.

The process is killed only when the code blocking its event loop belongs to a canceled call, including code run by the promises, timeouts, and I/O operations it started. Code run by long-lived objects (intervals, sockets, servers, event listeners on objects the call did not create...) and the code that loads a module imported by NodeJsHost.InvokeAsync or require belong to no call, as they can run code for several calls. Calls that are not canceled are never stopped, even when they keep the event loop busy for longer. While canceled calls are running, the process is checked at least every second, so a canceled call that blocks the event loop after awaiting is also stopped. Choose a value larger than the longest synchronous step of a canceled call that must not be stopped.

To know which call runs, the process tracks the asynchronous context of each call using node:async_hooks, which slows down code that awaits many promises, and a watchdog thread answers the host while the event loop is blocked.

With NodeJsHostOptions.WorkerThreads, only the worker thread blocked by a canceled call is terminated and restarted: the calls in progress on this worker thread fail, the other worker threads are not affected, and the process keeps running. The main thread of the process, which does not run calls, answers the host.

Gets or sets the number of worker threads (node:worker_threads) that run the calls, so CPU-bound calls run in parallel in a single Node.js process. When null, the calls run on the main thread of the process.

public int? WorkerThreads { get; set; }

Property value

int?

Remarks

Each call runs on one worker thread: the one with the fewest calls in progress, or, when the call uses a JSReference, the one that keeps the referenced value. Worker threads do not share state: each one has its own globalThis, loads its own modules, and keeps its own referenced values, so consecutive calls may run on different worker threads and not see the same values. A call cannot use references kept by different worker threads.

A worker thread that exits (e.g. calling process.exit()) is restarted: the calls in progress on it fail with a NodeJsException whose NodeJsException.ExitCode is null, and its referenced values are lost. Some APIs behave differently in a worker thread: process.exit() only stops the worker thread, process.chdir() is not supported, process.env is a copy, and native addons must support worker threads.

WorkingDirectory

NodeJsHostOptions.cs

Gets or sets the working directory of the Node.js process. Module specifiers (relative paths and npm packages) are resolved from this directory. When null, the current directory is used.

public string? WorkingDirectory { get; set; }

Property value

string?