WXL
5 天以前 871522ed7e06fd9c62a87c178d7f5c88d7853a20
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
declare module "node:stream/promises" {
    import { Abortable } from "node:events";
    import {
        FinishedOptions as _FinishedOptions,
        PipelineDestination,
        PipelineSource,
        PipelineTransform,
    } from "node:stream";
    import { ReadableStream, WritableStream } from "node:stream/web";
    interface FinishedOptions extends _FinishedOptions {
        /**
         * If true, removes the listeners registered by this function before the promise is fulfilled.
         * @default false
         */
        cleanup?: boolean | undefined;
    }
    /**
     * ```js
     * import { finished } from 'node:stream/promises';
     * import { createReadStream } from 'node:fs';
     *
     * const rs = createReadStream('archive.tar');
     *
     * async function run() {
     *   await finished(rs);
     *   console.log('Stream is done reading.');
     * }
     *
     * run().catch(console.error);
     * rs.resume(); // Drain the stream.
     * ```
     *
     * The `finished` API also provides a [callback version](https://nodejs.org/docs/latest-v25.x/api/stream.html#streamfinishedstream-options-callback).
     *
     * `stream.finished()` leaves dangling event listeners (in particular
     * `'error'`, `'end'`, `'finish'` and `'close'`) after the returned promise is
     * resolved or rejected. The reason for this is so that unexpected `'error'`
     * events (due to incorrect stream implementations) do not cause unexpected
     * crashes. If this is unwanted behavior then `options.cleanup` should be set to
     * `true`:
     *
     * ```js
     * await finished(rs, { cleanup: true });
     * ```
     * @since v15.0.0
     * @returns Fulfills when the stream is no longer readable or writable.
     */
    function finished(
        stream: NodeJS.ReadableStream | NodeJS.WritableStream | ReadableStream | WritableStream,
        options?: FinishedOptions,
    ): Promise<void>;
    interface PipelineOptions extends Abortable {
        end?: boolean | undefined;
    }
    type PipelineResult<S extends PipelineDestination<any, any>> = S extends (...args: any[]) => PromiseLike<infer R>
        ? Promise<R>
        : Promise<void>;
    /**
     * ```js
     * import { pipeline } from 'node:stream/promises';
     * import { createReadStream, createWriteStream } from 'node:fs';
     * import { createGzip } from 'node:zlib';
     *
     * await pipeline(
     *   createReadStream('archive.tar'),
     *   createGzip(),
     *   createWriteStream('archive.tar.gz'),
     * );
     * console.log('Pipeline succeeded.');
     * ```
     *
     * To use an `AbortSignal`, pass it inside an options object, as the last argument.
     * When the signal is aborted, `destroy` will be called on the underlying pipeline,
     * with an `AbortError`.
     *
     * ```js
     * import { pipeline } from 'node:stream/promises';
     * import { createReadStream, createWriteStream } from 'node:fs';
     * import { createGzip } from 'node:zlib';
     *
     * const ac = new AbortController();
     * const { signal } = ac;
     * setImmediate(() => ac.abort());
     * try {
     *   await pipeline(
     *     createReadStream('archive.tar'),
     *     createGzip(),
     *     createWriteStream('archive.tar.gz'),
     *     { signal },
     *   );
     * } catch (err) {
     *   console.error(err); // AbortError
     * }
     * ```
     *
     * The `pipeline` API also supports async generators:
     *
     * ```js
     * import { pipeline } from 'node:stream/promises';
     * import { createReadStream, createWriteStream } from 'node:fs';
     *
     * await pipeline(
     *   createReadStream('lowercase.txt'),
     *   async function* (source, { signal }) {
     *     source.setEncoding('utf8');  // Work with strings rather than `Buffer`s.
     *     for await (const chunk of source) {
     *       yield await processChunk(chunk, { signal });
     *     }
     *   },
     *   createWriteStream('uppercase.txt'),
     * );
     * console.log('Pipeline succeeded.');
     * ```
     *
     * Remember to handle the `signal` argument passed into the async generator.
     * Especially in the case where the async generator is the source for the
     * pipeline (i.e. first argument) or the pipeline will never complete.
     *
     * ```js
     * import { pipeline } from 'node:stream/promises';
     * import fs from 'node:fs';
     * await pipeline(
     *   async function* ({ signal }) {
     *     await someLongRunningfn({ signal });
     *     yield 'asd';
     *   },
     *   fs.createWriteStream('uppercase.txt'),
     * );
     * console.log('Pipeline succeeded.');
     * ```
     *
     * The `pipeline` API provides [callback version](https://nodejs.org/docs/latest-v25.x/api/stream.html#streampipelinesource-transforms-destination-callback):
     * @since v15.0.0
     * @returns Fulfills when the pipeline is complete.
     */
    function pipeline<A extends PipelineSource<any>, B extends PipelineDestination<A, any>>(
        source: A,
        destination: B,
        options?: PipelineOptions,
    ): PipelineResult<B>;
    function pipeline<
        A extends PipelineSource<any>,
        T1 extends PipelineTransform<A, any>,
        B extends PipelineDestination<T1, any>,
    >(
        source: A,
        transform1: T1,
        destination: B,
        options?: PipelineOptions,
    ): PipelineResult<B>;
    function pipeline<
        A extends PipelineSource<any>,
        T1 extends PipelineTransform<A, any>,
        T2 extends PipelineTransform<T1, any>,
        B extends PipelineDestination<T2, any>,
    >(
        source: A,
        transform1: T1,
        transform2: T2,
        destination: B,
        options?: PipelineOptions,
    ): PipelineResult<B>;
    function pipeline<
        A extends PipelineSource<any>,
        T1 extends PipelineTransform<A, any>,
        T2 extends PipelineTransform<T1, any>,
        T3 extends PipelineTransform<T2, any>,
        B extends PipelineDestination<T3, any>,
    >(
        source: A,
        transform1: T1,
        transform2: T2,
        transform3: T3,
        destination: B,
        options?: PipelineOptions,
    ): PipelineResult<B>;
    function pipeline<
        A extends PipelineSource<any>,
        T1 extends PipelineTransform<A, any>,
        T2 extends PipelineTransform<T1, any>,
        T3 extends PipelineTransform<T2, any>,
        T4 extends PipelineTransform<T3, any>,
        B extends PipelineDestination<T4, any>,
    >(
        source: A,
        transform1: T1,
        transform2: T2,
        transform3: T3,
        transform4: T4,
        destination: B,
        options?: PipelineOptions,
    ): PipelineResult<B>;
    function pipeline(
        streams: readonly [PipelineSource<any>, ...PipelineTransform<any, any>[], PipelineDestination<any, any>],
        options?: PipelineOptions,
    ): Promise<void>;
    function pipeline(
        ...streams: [PipelineSource<any>, ...PipelineTransform<any, any>[], PipelineDestination<any, any>]
    ): Promise<void>;
    function pipeline(
        ...streams: [
            PipelineSource<any>,
            ...PipelineTransform<any, any>[],
            PipelineDestination<any, any>,
            options: PipelineOptions,
        ]
    ): Promise<void>;
}
declare module "stream/promises" {
    export * from "node:stream/promises";
}