UNPKG

2.35 kBTypeScriptView Raw
1import Stagehand from './stagehand';
2import { RemoteType, HandlerType, MethodsOnly } from './utils/types';
3declare const STAGEHAND_INSTANCE = "--stagehand-instance";
4/**
5 * The remote implementation of a type `T`. It only includes `T`s methods, and the return values
6 * of those methods are all made async if they weren't already.
7 */
8export declare type Remote<T> = RemoteType<MethodsOnly<T>> & {
9 [STAGEHAND_INSTANCE]: Stagehand;
10};
11/**
12 * The necessarily implementation type of an intended remote interface `T`. All methods in
13 * `Implementation<T>` are allowed to return promises even if the corresponding method in `T`
14 * does not, but `Implementation<T>` also requires that all incoming callbacks be async, since
15 * they correspond to a remote type themselves.
16 */
17export declare type Implementation<T> = HandlerType<T>;
18/**
19 * The minimal interface needed for stagehand communication. The `adapters` directory contains
20 * utilities for converting common types (e.g. Node's `ChildProcess` or the Web's `MessagePort`)
21 * into `MessageEndpoint`s.
22 */
23export interface MessageEndpoint {
24 onMessage(callback: (message: unknown) => void): void;
25 sendMessage(message: unknown): void;
26 disconnect(): void;
27}
28/**
29 * Given a message endpoint and a backing implementation object, listens for a connection on the
30 * given endpoint and responds to commands that come in over it. This function will typically be
31 * called in whatever secondary thread/process will be servicing commands from the primary.
32 */
33export declare function launch<T>(endpoint: MessageEndpoint, implementation: Implementation<T>): Promise<Stagehand>;
34/**
35 * Given a message endpoint (and typically an explicit type parameter indicating the interface of
36 * the remote implementation), returns a promise that will resolve when successfully connected to
37 * the implementation on the other side of that endpoint.
38 *
39 * The resulting object will have methods defined on it that correspond to those of the backing
40 * implementation, returning a promise for the eventual result of that method.
41 */
42export declare function connect<T>(endpoint: MessageEndpoint): Promise<Remote<T>>;
43/**
44 * Given a remote object (as returned from `connect`), disconnects from the source and closes the connection.
45 */
46export declare function disconnect<T>(remote: Remote<T>): void;
47export {};