Skip to content

procxy

Procxy - A TypeScript library for transparent process-based proxy of class instances.

Overview

Procxy enables you to run class instances in isolated child processes while interacting with them as if they were local objects. All method calls become async and are transparently forwarded over IPC.

Key Features

  • 🎯 Type-Safe: Full TypeScript support with IntelliSense
  • Fast: <10ms overhead per method call
  • 🔄 Event Support: Transparent EventEmitter forwarding
  • 🛡️ Error Handling: Complete error propagation with stack traces
  • 🧹 Lifecycle: Automatic cleanup with disposable protocol support
  • ⚙️ Configurable: Timeouts, retries, custom env/cwd

Quick Start

typescript
import { procxy } from 'procxy';

class Calculator {
  add(a: number, b: number) { return a + b; }
}

// Create remote instance
const calc = await procxy(Calculator, './calculator.js');

// Call methods (now async)
const result = await calc.add(5, 3); // 8

// Clean up
await calc.$terminate();
typescript
// Automatic cleanup with await using
await using calc = await procxy(Calculator, './calculator.js');
const result = await calc.add(5, 3);
// Automatically terminated when scope exits

Configuration

Type AliasDescription
ProcxyOptionsConfiguration for the procxy() function.
SerializationModeSerialization mode for IPC messages exchanged between parent and child processes.

Core

NameDescription
ProcxyThe proxy type returned by procxy() — a transparent async mirror of a remote class instance.
procxyMain function to create a process-based proxy for a class instance.

Errors

ClassDescription
ChildCrashedErrorThrown when the child process exits or is killed while the proxy is active.
ModuleResolutionErrorThrown when procxy cannot determine the file path of the class's module.
OptionsValidationErrorThrown when a value in ProcxyOptions fails validation before the child process is spawned.
ProcxyErrorBase class for all errors thrown by Procxy.
SerializationErrorThrown when a constructor argument, method argument, or return value cannot be serialized across the IPC boundary.
TimeoutErrorThrown when a proxied method call or the INIT handshake exceeds the configured timeout.

Other

NameDescription
ErrorInfoError information serialized in Response messages. Preserves error message, stack trace, name, and optional code.
EventMessageEvent message sent from child to parent for EventEmitter events. Forwards events emitted in child to listeners in parent.
HandleAckHandle acknowledgment sent from child to parent after handle is received.
HandleMessageHandle transmission message sent from parent to child. Notifies child that a handle (socket, server, file descriptor) is being sent. The actual handle is passed separately via Node.js child.send(message, handle).
InitMessageInitialization message sent from parent to child on startup. Contains module path, class name, constructor arguments, and serialization mode.
RequestMethod invocation request sent from parent to child. Includes unique ID for request/response correlation.
ResponseMethod invocation response sent from child to parent. Either contains return value (RESULT) or error information (ERROR).
ChildToParentMessageUnion type of all IPC messages sent from child to parent.
ParentToChildMessageUnion type of all IPC messages sent from parent to child.

Runtime Utilities

FunctionDescription
isAdvancedModeRuntime utilities for working with Procxy instances.
isHandleSupportedRuntime utilities for working with Procxy instances.
isProcxyRuntime utilities for working with Procxy instances.

Serialization

NameDescription
V8SerializableUnion of all types that survive Node.js V8 structured-clone serialization across an IPC boundary.
sanitizeForV8Strip non-V8-serializable properties from a value, returning a deep-cloned plain version.
sanitizeForV8ArrayApply sanitizeForV8 to each element of an array, returning a new sanitized array.

Type Utilities

Type AliasDescription
ChangeProcxyModeProduce a new Procxy type identical to P except with a different serialization mode.
GetProcxyLifecycleMethodsExtract the lifecycle method and property names from a Procxy type.
GetProcxyMethodsExtract the union of user-defined method names available on a Procxy type.
GetProcxyModeExtract the serialization mode from a Procxy type.
HasHandleSupportConditional type that resolves to true when P has $sendHandle support.
IsProcxyConditional type that resolves to true when P is a Procxy type.
IsProcxyIsomorphicConditional type that resolves to true when T <-> Procxy<T> form a consistent isomorphism.
ProcxyIsomorphismDescribes the bidirectional type mapping between T and Procxy<T>.
ToggleProcxyHandlesProduce a new Procxy type identical to P except with a different SupportHandles flag.
UnwrapProcxyExtract the original type T from Procxy<T, Mode, SupportHandles>.
VerifyIsomorphismCompile-time assertion that T round-trips through Procxy<T> without loss.

Types

Type AliasDescription
IsProcxiableConditional type that resolves to true when T can cross the IPC boundary in the given mode.
MaybeProxyA value that is either the original type T or a Procxy<T> proxy for it.
PassableHandleUnion of OS-level handle types that can be transferred to the child process via $sendHandle.
ProcxiableThe serializable type constraint for a given IPC mode.
ProcxifyExtract only the serializable, non-method properties from a type — the "data shape" of a class.
SerializableConstructorArgsConstrain constructor argument types to be serializable under the given mode.

Released under the MIT License.