-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtypes.ts
More file actions
86 lines (66 loc) · 2.85 KB
/
types.ts
File metadata and controls
86 lines (66 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import type { IncomingMessage } from "node:http";
import type { Socket } from "node:net";
/** Handler for proxying HTTP requests to the worker. */
export type FetchHandler = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
/** Callback for receiving messages from the worker. */
export type RunnerMessageListener = (data: unknown) => void;
/** Raw Node.js upgrade request context. */
export interface NodeUpgradeContext {
req: IncomingMessage;
socket: Socket;
head: any;
}
/** Context passed to the upgrade handler for WebSocket upgrades. */
export interface UpgradeContext {
node: NodeUpgradeContext;
}
/** Handler for proxying WebSocket upgrade requests to the worker. */
export type UpgradeHandler = (context: UpgradeContext) => void;
/** Bidirectional RPC messaging interface between the runner and worker. */
export interface RunnerRPCHooks {
/** Send a message to the worker. */
sendMessage: (message: unknown) => void;
/** Register a listener for messages from the worker. */
onMessage: (listener: RunnerMessageListener) => void;
/** Remove a previously registered message listener. */
offMessage: (listener: RunnerMessageListener) => void;
}
/**
* Address reported by the worker once it is ready.
*
* Either a TCP `host`/`port` pair or a Unix `socketPath`.
*/
export type WorkerAddress =
| { host?: string; port: number; socketPath?: undefined }
| { host?: undefined; port?: undefined; socketPath: string };
/** Lifecycle hooks for observing runner state changes. */
export interface WorkerHooks {
/** Called when the worker closes, optionally with the cause. */
onClose?: (worker: EnvRunner, cause?: unknown) => void;
/** Called when the worker is ready and listening at the given address. */
onReady?: (worker: EnvRunner, address?: WorkerAddress) => void;
}
/** Options for the `rpc()` method. */
export interface RPCOptions {
/** Timeout in milliseconds before the RPC call rejects. Default: 3000. */
timeout?: number;
}
/** Core runner interface combining lifecycle hooks, RPC, and request proxying. */
export interface EnvRunner extends RunnerRPCHooks {
/** Whether the worker is ready to accept requests. */
readonly ready: boolean;
/** Whether the runner has been closed. */
readonly closed: boolean;
/** Proxy an HTTP request to the worker. */
fetch: FetchHandler;
/** Proxy a WebSocket upgrade request to the worker. */
upgrade?: UpgradeHandler;
/** Returns a promise that resolves when the runner becomes ready. */
waitForReady(timeout?: number): Promise<void>;
/** Send an RPC request and wait for the response. */
rpc<T = unknown>(name: string, data?: unknown, opts?: RPCOptions): Promise<T>;
/** Re-import the entry module without restarting the worker/process. */
reloadModule?(timeout?: number): Promise<void>;
/** Gracefully shut down the worker. */
close(): Promise<void>;
}