UNPKG

5.47 kBTypeScriptView Raw
1import type {Readable, Writable} from 'node:stream';
2import type {ReadableStream, WritableStream, TransformStream} from 'node:stream/web';
3import type {
4 Not,
5 And,
6 Or,
7 Unless,
8 AndUnless,
9} from '../utils.js';
10import type {
11 GeneratorTransform,
12 GeneratorTransformFull,
13 DuplexTransform,
14 WebTransform,
15} from '../transform/normalize.js';
16
17type IsStandardStream<FdNumber extends string> = FdNumber extends keyof StandardStreams ? true : false;
18
19export type StandardStreams = readonly ['stdin', 'stdout', 'stderr'];
20
21// When `options.stdin|stdout|stderr|stdio` is set to one of those values, no stream is created
22export type NoStreamStdioOption<FdNumber extends string> =
23 | 'ignore'
24 | 'inherit'
25 | 'ipc'
26 | number
27 | Readable
28 | Writable
29 | Unless<IsStandardStream<FdNumber>, undefined>
30 | readonly [NoStreamStdioOption<FdNumber>];
31
32// `options.stdio` when it is not an array
33type SimpleStdioOption<
34 IsSync extends boolean,
35 IsExtra extends boolean,
36 IsArray extends boolean,
37> =
38 | undefined
39 | 'pipe'
40 | Unless<And<And<Not<IsSync>, IsArray>, IsExtra>, 'inherit'>
41 | Unless<IsArray, 'ignore'>
42 | Unless<IsSync, 'overlapped'>;
43
44// Values available in both `options.stdin|stdio` and `options.stdout|stderr|stdio`
45type CommonStdioOption<
46 IsSync extends boolean,
47 IsExtra extends boolean,
48 IsArray extends boolean,
49> =
50 | SimpleStdioOption<IsSync, IsExtra, IsArray>
51 | URL
52 | {readonly file: string; readonly append?: boolean}
53 | GeneratorTransform<IsSync>
54 | GeneratorTransformFull<IsSync>
55 | Unless<And<Not<IsSync>, IsArray>, 3 | 4 | 5 | 6 | 7 | 8 | 9>
56 | Unless<Or<IsSync, IsArray>, 'ipc'>
57 | Unless<IsSync, DuplexTransform | WebTransform | TransformStream>;
58
59// Synchronous iterables excluding strings, Uint8Arrays and Arrays
60type IterableObject<IsArray extends boolean> = Iterable<unknown>
61& object
62& {readonly BYTES_PER_ELEMENT?: never}
63& AndUnless<IsArray, {readonly lastIndexOf?: never}>;
64
65// `process.stdin|stdout|stderr` are `Duplex` with a `fd` property.
66// This ensures they can only be passed to `stdin`/`stdout`/`stderr`, based on their direction.
67type ProcessStdinFd = {readonly fd?: 0};
68type ProcessStdoutStderrFd = {readonly fd?: 1 | 2};
69
70// Values available only in `options.stdin|stdio`
71export type InputStdioOption<
72 IsSync extends boolean = boolean,
73 IsExtra extends boolean = boolean,
74 IsArray extends boolean = boolean,
75> =
76 | 0
77 | Unless<And<IsSync, IsExtra>, Uint8Array | IterableObject<IsArray>>
78 | Unless<And<IsSync, IsArray>, Readable & ProcessStdinFd>
79 | Unless<IsSync, (AsyncIterable<unknown> & ProcessStdinFd) | ReadableStream>;
80
81// Values available only in `options.stdout|stderr|stdio`
82type OutputStdioOption<
83 IsSync extends boolean,
84 IsArray extends boolean,
85> =
86 | 1
87 | 2
88 | Unless<And<IsSync, IsArray>, Writable & ProcessStdoutStderrFd>
89 | Unless<IsSync, WritableStream>;
90
91// `options.stdin` array items
92type StdinSingleOption<
93 IsSync extends boolean,
94 IsExtra extends boolean,
95 IsArray extends boolean,
96> =
97 | CommonStdioOption<IsSync, IsExtra, IsArray>
98 | InputStdioOption<IsSync, IsExtra, IsArray>;
99
100// `options.stdin`
101export type StdinOptionCommon<
102 IsSync extends boolean = boolean,
103 IsExtra extends boolean = boolean,
104> =
105 | StdinSingleOption<IsSync, IsExtra, false>
106 | ReadonlyArray<StdinSingleOption<IsSync, IsExtra, true>>;
107
108// `options.stdin`, async
109export type StdinOption = StdinOptionCommon<false, false>;
110// `options.stdin`, sync
111export type StdinSyncOption = StdinOptionCommon<true, false>;
112
113// `options.stdout|stderr` array items
114type StdoutStderrSingleOption<
115 IsSync extends boolean,
116 IsExtra extends boolean,
117 IsArray extends boolean,
118> =
119 | CommonStdioOption<IsSync, IsExtra, IsArray>
120 | OutputStdioOption<IsSync, IsArray>;
121
122// `options.stdout|stderr`
123export type StdoutStderrOptionCommon<
124 IsSync extends boolean = boolean,
125 IsExtra extends boolean = boolean,
126> =
127 | StdoutStderrSingleOption<IsSync, IsExtra, false>
128 | ReadonlyArray<StdoutStderrSingleOption<IsSync, IsExtra, true>>;
129
130// `options.stdout|stderr`, async
131export type StdoutStderrOption = StdoutStderrOptionCommon<false, false>;
132// `options.stdout|stderr`, sync
133export type StdoutStderrSyncOption = StdoutStderrOptionCommon<true, false>;
134
135// `options.stdio[3+]`
136type StdioExtraOptionCommon<IsSync extends boolean> =
137 | StdinOptionCommon<IsSync, true>
138 | StdoutStderrOptionCommon<IsSync, true>;
139
140// `options.stdin|stdout|stderr|stdio` array items
141type StdioSingleOption<
142 IsSync extends boolean = boolean,
143 IsExtra extends boolean = boolean,
144 IsArray extends boolean = boolean,
145> =
146 | StdinSingleOption<IsSync, IsExtra, IsArray>
147 | StdoutStderrSingleOption<IsSync, IsExtra, IsArray>;
148
149// Get `options.stdin|stdout|stderr|stdio` items if it is an array, else keep as is
150export type StdioSingleOptionItems<StdioOptionType> = StdioOptionType extends readonly StdioSingleOption[]
151 ? StdioOptionType[number]
152 : StdioOptionType;
153
154// `options.stdin|stdout|stderr|stdio`
155export type StdioOptionCommon<IsSync extends boolean = boolean> =
156 | StdinOptionCommon<IsSync>
157 | StdoutStderrOptionCommon<IsSync>;
158
159// `options.stdio` when it is an array
160export type StdioOptionsArray<IsSync extends boolean = boolean> = readonly [
161 StdinOptionCommon<IsSync, false>,
162 StdoutStderrOptionCommon<IsSync, false>,
163 StdoutStderrOptionCommon<IsSync, false>,
164 ...ReadonlyArray<StdioExtraOptionCommon<IsSync>>,
165];
166
167// `options.stdio`
168export type StdioOptionsProperty<IsSync extends boolean = boolean> =
169 | SimpleStdioOption<IsSync, false, false>
170 | StdioOptionsArray<IsSync>;