UNPKG

13.7 kBTypeScriptView Raw
1declare module 'stream/web' {
2 // stub module, pending copy&paste from .d.ts or manual impl
3 // copy from lib.dom.d.ts
4 interface ReadableWritablePair<R = any, W = any> {
5 readable: ReadableStream<R>;
6 /**
7 * Provides a convenient, chainable way of piping this readable stream
8 * through a transform stream (or any other { writable, readable }
9 * pair). It simply pipes the stream into the writable side of the
10 * supplied pair, and returns the readable side for further use.
11 *
12 * Piping a stream will lock it for the duration of the pipe, preventing
13 * any other consumer from acquiring a reader.
14 */
15 writable: WritableStream<W>;
16 }
17 interface StreamPipeOptions {
18 preventAbort?: boolean;
19 preventCancel?: boolean;
20 /**
21 * Pipes this readable stream to a given writable stream destination.
22 * The way in which the piping process behaves under various error
23 * conditions can be customized with a number of passed options. It
24 * returns a promise that fulfills when the piping process completes
25 * successfully, or rejects if any errors were encountered.
26 *
27 * Piping a stream will lock it for the duration of the pipe, preventing
28 * any other consumer from acquiring a reader.
29 *
30 * Errors and closures of the source and destination streams propagate
31 * as follows:
32 *
33 * An error in this source readable stream will abort destination,
34 * unless preventAbort is truthy. The returned promise will be rejected
35 * with the source's error, or with any error that occurs during
36 * aborting the destination.
37 *
38 * An error in destination will cancel this source readable stream,
39 * unless preventCancel is truthy. The returned promise will be rejected
40 * with the destination's error, or with any error that occurs during
41 * canceling the source.
42 *
43 * When this source readable stream closes, destination will be closed,
44 * unless preventClose is truthy. The returned promise will be fulfilled
45 * once this process completes, unless an error is encountered while
46 * closing the destination, in which case it will be rejected with that
47 * error.
48 *
49 * If destination starts out closed or closing, this source readable
50 * stream will be canceled, unless preventCancel is true. The returned
51 * promise will be rejected with an error indicating piping to a closed
52 * stream failed, or with any error that occurs during canceling the
53 * source.
54 *
55 * The signal option can be set to an AbortSignal to allow aborting an
56 * ongoing pipe operation via the corresponding AbortController. In this
57 * case, this source readable stream will be canceled, and destination
58 * aborted, unless the respective options preventCancel or preventAbort
59 * are set.
60 */
61 preventClose?: boolean;
62 signal?: AbortSignal;
63 }
64 interface ReadableStreamGenericReader {
65 readonly closed: Promise<undefined>;
66 cancel(reason?: any): Promise<void>;
67 }
68 interface ReadableStreamDefaultReadValueResult<T> {
69 done: false;
70 value: T;
71 }
72 interface ReadableStreamDefaultReadDoneResult {
73 done: true;
74 value?: undefined;
75 }
76 type ReadableStreamController<T> = ReadableStreamDefaultController<T>;
77 type ReadableStreamDefaultReadResult<T> = ReadableStreamDefaultReadValueResult<T> | ReadableStreamDefaultReadDoneResult;
78 interface ReadableByteStreamControllerCallback {
79 (controller: ReadableByteStreamController): void | PromiseLike<void>;
80 }
81 interface UnderlyingSinkAbortCallback {
82 (reason?: any): void | PromiseLike<void>;
83 }
84 interface UnderlyingSinkCloseCallback {
85 (): void | PromiseLike<void>;
86 }
87 interface UnderlyingSinkStartCallback {
88 (controller: WritableStreamDefaultController): any;
89 }
90 interface UnderlyingSinkWriteCallback<W> {
91 (chunk: W, controller: WritableStreamDefaultController): void | PromiseLike<void>;
92 }
93 interface UnderlyingSourceCancelCallback {
94 (reason?: any): void | PromiseLike<void>;
95 }
96 interface UnderlyingSourcePullCallback<R> {
97 (controller: ReadableStreamController<R>): void | PromiseLike<void>;
98 }
99 interface UnderlyingSourceStartCallback<R> {
100 (controller: ReadableStreamController<R>): any;
101 }
102 interface TransformerFlushCallback<O> {
103 (controller: TransformStreamDefaultController<O>): void | PromiseLike<void>;
104 }
105 interface TransformerStartCallback<O> {
106 (controller: TransformStreamDefaultController<O>): any;
107 }
108 interface TransformerTransformCallback<I, O> {
109 (chunk: I, controller: TransformStreamDefaultController<O>): void | PromiseLike<void>;
110 }
111 interface UnderlyingByteSource {
112 autoAllocateChunkSize?: number;
113 cancel?: ReadableStreamErrorCallback;
114 pull?: ReadableByteStreamControllerCallback;
115 start?: ReadableByteStreamControllerCallback;
116 type: 'bytes';
117 }
118 interface UnderlyingSource<R = any> {
119 cancel?: UnderlyingSourceCancelCallback;
120 pull?: UnderlyingSourcePullCallback<R>;
121 start?: UnderlyingSourceStartCallback<R>;
122 type?: undefined;
123 }
124 interface UnderlyingSink<W = any> {
125 abort?: UnderlyingSinkAbortCallback;
126 close?: UnderlyingSinkCloseCallback;
127 start?: UnderlyingSinkStartCallback;
128 type?: undefined;
129 write?: UnderlyingSinkWriteCallback<W>;
130 }
131 interface ReadableStreamErrorCallback {
132 (reason: any): void | PromiseLike<void>;
133 }
134 /** This Streams API interface represents a readable stream of byte data. */
135 interface ReadableStream<R = any> {
136 readonly locked: boolean;
137 cancel(reason?: any): Promise<void>;
138 getReader(): ReadableStreamDefaultReader<R>;
139 pipeThrough<T>(transform: ReadableWritablePair<T, R>, options?: StreamPipeOptions): ReadableStream<T>;
140 pipeTo(destination: WritableStream<R>, options?: StreamPipeOptions): Promise<void>;
141 tee(): [ReadableStream<R>, ReadableStream<R>];
142 [Symbol.asyncIterator](options?: { preventCancel?: boolean }): AsyncIterableIterator<R>;
143 }
144 const ReadableStream: {
145 prototype: ReadableStream;
146 new (underlyingSource: UnderlyingByteSource, strategy?: QueuingStrategy<Uint8Array>): ReadableStream<Uint8Array>;
147 new <R = any>(underlyingSource?: UnderlyingSource<R>, strategy?: QueuingStrategy<R>): ReadableStream<R>;
148 };
149 interface ReadableStreamDefaultReader<R = any> extends ReadableStreamGenericReader {
150 read(): Promise<ReadableStreamDefaultReadResult<R>>;
151 releaseLock(): void;
152 }
153 const ReadableStreamDefaultReader: {
154 prototype: ReadableStreamDefaultReader;
155 new <R = any>(stream: ReadableStream<R>): ReadableStreamDefaultReader<R>;
156 };
157 const ReadableStreamBYOBReader: any;
158 const ReadableStreamBYOBRequest: any;
159 interface ReadableByteStreamController {
160 readonly byobRequest: undefined;
161 readonly desiredSize: number | null;
162 close(): void;
163 enqueue(chunk: ArrayBufferView): void;
164 error(error?: any): void;
165 }
166 const ReadableByteStreamController: {
167 prototype: ReadableByteStreamController;
168 new (): ReadableByteStreamController;
169 };
170 interface ReadableStreamDefaultController<R = any> {
171 readonly desiredSize: number | null;
172 close(): void;
173 enqueue(chunk?: R): void;
174 error(e?: any): void;
175 }
176 const ReadableStreamDefaultController: {
177 prototype: ReadableStreamDefaultController;
178 new (): ReadableStreamDefaultController;
179 };
180 interface Transformer<I = any, O = any> {
181 flush?: TransformerFlushCallback<O>;
182 readableType?: undefined;
183 start?: TransformerStartCallback<O>;
184 transform?: TransformerTransformCallback<I, O>;
185 writableType?: undefined;
186 }
187 interface TransformStream<I = any, O = any> {
188 readonly readable: ReadableStream<O>;
189 readonly writable: WritableStream<I>;
190 }
191 const TransformStream: {
192 prototype: TransformStream;
193 new <I = any, O = any>(transformer?: Transformer<I, O>, writableStrategy?: QueuingStrategy<I>, readableStrategy?: QueuingStrategy<O>): TransformStream<I, O>;
194 };
195 interface TransformStreamDefaultController<O = any> {
196 readonly desiredSize: number | null;
197 enqueue(chunk?: O): void;
198 error(reason?: any): void;
199 terminate(): void;
200 }
201 const TransformStreamDefaultController: {
202 prototype: TransformStreamDefaultController;
203 new (): TransformStreamDefaultController;
204 };
205 /**
206 * This Streams API interface provides a standard abstraction for writing
207 * streaming data to a destination, known as a sink. This object comes with
208 * built-in back pressure and queuing.
209 */
210 interface WritableStream<W = any> {
211 readonly locked: boolean;
212 abort(reason?: any): Promise<void>;
213 close(): Promise<void>;
214 getWriter(): WritableStreamDefaultWriter<W>;
215 }
216 const WritableStream: {
217 prototype: WritableStream;
218 new <W = any>(underlyingSink?: UnderlyingSink<W>, strategy?: QueuingStrategy<W>): WritableStream<W>;
219 };
220 /**
221 * This Streams API interface is the object returned by
222 * WritableStream.getWriter() and once created locks the < writer to the
223 * WritableStream ensuring that no other streams can write to the underlying
224 * sink.
225 */
226 interface WritableStreamDefaultWriter<W = any> {
227 readonly closed: Promise<undefined>;
228 readonly desiredSize: number | null;
229 readonly ready: Promise<undefined>;
230 abort(reason?: any): Promise<void>;
231 close(): Promise<void>;
232 releaseLock(): void;
233 write(chunk?: W): Promise<void>;
234 }
235 const WritableStreamDefaultWriter: {
236 prototype: WritableStreamDefaultWriter;
237 new <W = any>(stream: WritableStream<W>): WritableStreamDefaultWriter<W>;
238 };
239 /**
240 * This Streams API interface represents a controller allowing control of a
241 * WritableStream's state. When constructing a WritableStream, the
242 * underlying sink is given a corresponding WritableStreamDefaultController
243 * instance to manipulate.
244 */
245 interface WritableStreamDefaultController {
246 error(e?: any): void;
247 }
248 const WritableStreamDefaultController: {
249 prototype: WritableStreamDefaultController;
250 new (): WritableStreamDefaultController;
251 };
252 interface QueuingStrategy<T = any> {
253 highWaterMark?: number;
254 size?: QueuingStrategySize<T>;
255 }
256 interface QueuingStrategySize<T = any> {
257 (chunk?: T): number;
258 }
259 interface QueuingStrategyInit {
260 /**
261 * Creates a new ByteLengthQueuingStrategy with the provided high water
262 * mark.
263 *
264 * Note that the provided high water mark will not be validated ahead of
265 * time. Instead, if it is negative, NaN, or not a number, the resulting
266 * ByteLengthQueuingStrategy will cause the corresponding stream
267 * constructor to throw.
268 */
269 highWaterMark: number;
270 }
271 /**
272 * This Streams API interface provides a built-in byte length queuing
273 * strategy that can be used when constructing streams.
274 */
275 interface ByteLengthQueuingStrategy extends QueuingStrategy<ArrayBufferView> {
276 readonly highWaterMark: number;
277 readonly size: QueuingStrategySize<ArrayBufferView>;
278 }
279 const ByteLengthQueuingStrategy: {
280 prototype: ByteLengthQueuingStrategy;
281 new (init: QueuingStrategyInit): ByteLengthQueuingStrategy;
282 };
283 /**
284 * This Streams API interface provides a built-in byte length queuing
285 * strategy that can be used when constructing streams.
286 */
287 interface CountQueuingStrategy extends QueuingStrategy {
288 readonly highWaterMark: number;
289 readonly size: QueuingStrategySize;
290 }
291 const CountQueuingStrategy: {
292 prototype: CountQueuingStrategy;
293 new (init: QueuingStrategyInit): CountQueuingStrategy;
294 };
295 interface TextEncoderStream {
296 /** Returns "utf-8". */
297 readonly encoding: 'utf-8';
298 readonly readable: ReadableStream<Uint8Array>;
299 readonly writable: WritableStream<string>;
300 readonly [Symbol.toStringTag]: string;
301 }
302 const TextEncoderStream: {
303 prototype: TextEncoderStream;
304 new (): TextEncoderStream;
305 };
306 interface TextDecoderOptions {
307 fatal?: boolean;
308 ignoreBOM?: boolean;
309 }
310 type BufferSource = ArrayBufferView | ArrayBuffer;
311 interface TextDecoderStream {
312 /** Returns encoding's name, lower cased. */
313 readonly encoding: string;
314 /** Returns `true` if error mode is "fatal", and `false` otherwise. */
315 readonly fatal: boolean;
316 /** Returns `true` if ignore BOM flag is set, and `false` otherwise. */
317 readonly ignoreBOM: boolean;
318 readonly readable: ReadableStream<string>;
319 readonly writable: WritableStream<BufferSource>;
320 readonly [Symbol.toStringTag]: string;
321 }
322 const TextDecoderStream: {
323 prototype: TextDecoderStream;
324 new (label?: string, options?: TextDecoderOptions): TextDecoderStream;
325 };
326}
327declare module 'node:stream/web' {
328 export * from 'stream/web';
329}