UNPKG

4.77 kBTypeScriptView Raw
1import type {Options} from './arguments/options.js';
2
3// Message when the `serialization` option is `'advanced'`
4type AdvancedMessage =
5 | string
6 | number
7 | boolean
8 | null
9 | object;
10
11// Message when the `serialization` option is `'json'`
12type JsonMessage =
13 | string
14 | number
15 | boolean
16 | null
17 | readonly JsonMessage[]
18 | {readonly [key: string | number]: JsonMessage};
19
20/**
21Type of messages exchanged between a process and its subprocess using `sendMessage()`, `getOneMessage()` and `getEachMessage()`.
22
23This requires the `ipc` option to be `true`. The type of `message` depends on the `serialization` option.
24*/
25export type Message<
26 Serialization extends Options['serialization'] = Options['serialization'],
27> = Serialization extends 'json' ? JsonMessage : AdvancedMessage;
28
29/**
30Options to `sendMessage()` and `subprocess.sendMessage()`
31*/
32type SendMessageOptions = {
33 /**
34 Throw when the other process is not receiving or listening to messages.
35
36 @default false
37 */
38 readonly strict?: boolean;
39};
40
41// IPC methods in subprocess
42/**
43Send a `message` to the parent process.
44
45This requires the `ipc` option to be `true`. The type of `message` depends on the `serialization` option.
46*/
47export function sendMessage(message: Message, sendMessageOptions?: SendMessageOptions): Promise<void>;
48
49/**
50Options to `getOneMessage()` and `subprocess.getOneMessage()`
51*/
52type GetOneMessageOptions<
53 Serialization extends Options['serialization'],
54> = {
55 /**
56 Ignore any `message` that returns `false`.
57 */
58 readonly filter?: (message: Message<Serialization>) => boolean;
59
60 /**
61 Keep the subprocess alive while `getOneMessage()` is waiting.
62
63 @default true
64 */
65 readonly reference?: boolean;
66};
67
68/**
69Receive a single `message` from the parent process.
70
71This requires the `ipc` option to be `true`. The type of `message` depends on the `serialization` option.
72*/
73export function getOneMessage(getOneMessageOptions?: GetOneMessageOptions<Options['serialization']>): Promise<Message>;
74
75/**
76Options to `getEachMessage()` and `subprocess.getEachMessage()`
77*/
78type GetEachMessageOptions = {
79 /**
80 Keep the subprocess alive while `getEachMessage()` is waiting.
81
82 @default true
83 */
84 readonly reference?: boolean;
85};
86
87/**
88Iterate over each `message` from the parent process.
89
90This requires the `ipc` option to be `true`. The type of `message` depends on the `serialization` option.
91*/
92export function getEachMessage(getEachMessageOptions?: GetEachMessageOptions): AsyncIterableIterator<Message>;
93
94/**
95Retrieves the [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) shared by the `cancelSignal` option.
96
97This can only be called inside a subprocess. This requires the `gracefulCancel` option to be `true`.
98*/
99export function getCancelSignal(): Promise<AbortSignal>;
100
101// IPC methods in the subprocess
102export type IpcMethods<
103 IpcEnabled extends boolean,
104 Serialization extends Options['serialization'],
105> = IpcEnabled extends true
106 ? {
107 /**
108 Send a `message` to the subprocess.
109
110 This requires the `ipc` option to be `true`. The type of `message` depends on the `serialization` option.
111 */
112 sendMessage(message: Message<Serialization>, sendMessageOptions?: SendMessageOptions): Promise<void>;
113
114 /**
115 Receive a single `message` from the subprocess.
116
117 This requires the `ipc` option to be `true`. The type of `message` depends on the `serialization` option.
118 */
119 getOneMessage(getOneMessageOptions?: GetOneMessageOptions<Serialization>): Promise<Message<Serialization>>;
120
121 /**
122 Iterate over each `message` from the subprocess.
123
124 This requires the `ipc` option to be `true`. The type of `message` depends on the `serialization` option.
125 */
126 getEachMessage(getEachMessageOptions?: GetEachMessageOptions): AsyncIterableIterator<Message<Serialization>>;
127 }
128 // Those methods only work if the `ipc` option is `true`.
129 // At runtime, they are actually defined, in order to provide with a nice error message.
130 // At type check time, they are typed as `undefined` to prevent calling them.
131 : {
132 sendMessage: undefined;
133 getOneMessage: undefined;
134 getEachMessage: undefined;
135 };
136
137// Whether IPC is enabled, based on the `ipc`, `ipcInput` and `gracefulCancel` options
138export type HasIpc<OptionsType extends Options> = HasIpcOption<
139OptionsType['ipc'],
140'ipcInput' extends keyof OptionsType ? OptionsType['ipcInput'] : undefined,
141'gracefulCancel' extends keyof OptionsType ? OptionsType['gracefulCancel'] : undefined
142>;
143
144type HasIpcOption<
145 IpcOption extends Options['ipc'],
146 IpcInputOption extends Options['ipcInput'],
147 GracefulCancelOption extends Options['gracefulCancel'],
148> = IpcOption extends true
149 ? true
150 : IpcOption extends false
151 ? false
152 : IpcInputOption extends undefined
153 ? GracefulCancelOption extends true
154 ? true
155 : false
156 : true;