1 | import type {Readable, Writable} from 'node:stream';
|
2 | import type {ReadableStream, WritableStream, TransformStream} from 'node:stream/web';
|
3 | import type {
|
4 | Not,
|
5 | And,
|
6 | Or,
|
7 | Unless,
|
8 | AndUnless,
|
9 | } from '../utils.js';
|
10 | import type {
|
11 | GeneratorTransform,
|
12 | GeneratorTransformFull,
|
13 | DuplexTransform,
|
14 | WebTransform,
|
15 | } from '../transform/normalize.js';
|
16 |
|
17 | type IsStandardStream<FdNumber extends string> = FdNumber extends keyof StandardStreams ? true : false;
|
18 |
|
19 | export type StandardStreams = readonly ['stdin', 'stdout', 'stderr'];
|
20 |
|
21 |
|
22 | export 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 |
|
33 | type 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 |
|
45 | type 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 |
|
60 | type IterableObject<IsArray extends boolean> = Iterable<unknown>
|
61 | & object
|
62 | & {readonly BYTES_PER_ELEMENT?: never}
|
63 | & AndUnless<IsArray, {readonly lastIndexOf?: never}>;
|
64 |
|
65 |
|
66 |
|
67 | type ProcessStdinFd = {readonly fd?: 0};
|
68 | type ProcessStdoutStderrFd = {readonly fd?: 1 | 2};
|
69 |
|
70 |
|
71 | export 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 |
|
82 | type 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 |
|
92 | type 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 |
|
101 | export 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 |
|
109 | export type StdinOption = StdinOptionCommon<false, false>;
|
110 |
|
111 | export type StdinSyncOption = StdinOptionCommon<true, false>;
|
112 |
|
113 |
|
114 | type 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 |
|
123 | export 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 |
|
131 | export type StdoutStderrOption = StdoutStderrOptionCommon<false, false>;
|
132 |
|
133 | export type StdoutStderrSyncOption = StdoutStderrOptionCommon<true, false>;
|
134 |
|
135 |
|
136 | type StdioExtraOptionCommon<IsSync extends boolean> =
|
137 | | StdinOptionCommon<IsSync, true>
|
138 | | StdoutStderrOptionCommon<IsSync, true>;
|
139 |
|
140 |
|
141 | type 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 |
|
150 | export type StdioSingleOptionItems<StdioOptionType> = StdioOptionType extends readonly StdioSingleOption[]
|
151 | ? StdioOptionType[number]
|
152 | : StdioOptionType;
|
153 |
|
154 |
|
155 | export type StdioOptionCommon<IsSync extends boolean = boolean> =
|
156 | | StdinOptionCommon<IsSync>
|
157 | | StdoutStderrOptionCommon<IsSync>;
|
158 |
|
159 |
|
160 | export 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 |
|
168 | export type StdioOptionsProperty<IsSync extends boolean = boolean> =
|
169 | | SimpleStdioOption<IsSync, false, false>
|
170 | | StdioOptionsArray<IsSync>;
|