1
>
/*---------------------------------------------------------------------------------------------
stream.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import { CancellationToken } from './cancellation.js';
7
>
import { onUnexpectedError } from './errors.js';
8
>
import { DisposableStore, toDisposable } from './lifecycle.js';
9
>
10
>
/**
11
>
* The payload that flows in readable stream events.
12
>
*/
13
>
export type ReadableStreamEventPayload<T> = T | Error | 'end';
14
>
15
>
export interface ReadableStreamEvents<T> {
16
>
17
>
/**
18
>
* The 'data' event is emitted whenever the stream is
19
>
* relinquishing ownership of a chunk of data to a consumer.
20
>
*
21
>
* NOTE: PLEASE UNDERSTAND THAT ADDING A DATA LISTENER CAN
22
>
* TURN THE STREAM INTO FLOWING MODE. IT IS THEREFOR THE
23
>
* LAST LISTENER THAT SHOULD BE ADDED AND NOT THE FIRST
24
>
*
25
>
* Use `listenStream` as a helper method to listen to
26
>
* stream events in the right order.
27
>
*/
28
>
on(event: 'data', callback: (data: T) => void): void;
29
>
30
>
/**
31
>
* Emitted when any error occurs.
32
>
*/
33
>
on(event: 'error', callback: (err: Error) => void): void;
34
>
35
>
/**
36
>
* The 'end' event is emitted when there is no more data
37
>
* to be consumed from the stream. The 'end' event will
38
>
* not be emitted unless the data is completely consumed.
39
>
*/
40
>
on(event: 'end', callback: () => void): void;
41
>
}
42
>
43
>
/**
44
>
* A interface that emulates the API shape of a node.js readable
45
>
* stream for use in native and web environments.
46
>
*/
47
>
export interface ReadableStream<T> extends ReadableStreamEvents<T> {
48
>
49
>
/**
50
>
* Stops emitting any events until resume() is called.
51
>
*/
52
>
pause(): void;
53
>
54
>
/**
55
>
* Starts emitting events again after pause() was called.
56
>
*/
57
>
resume(): void;
58
>
59
>
/**
60
>
* Destroys the stream and stops emitting any event.
61
>
*/
62
>
destroy(): void;
63
>
64
>
/**
65
>
* Allows to remove a listener that was previously added.
66
>
*/
67
>
removeListener(event: string, callback: Function): void;
68
>
}
69
>
70
>
/**
71
>
* A interface that emulates the API shape of a node.js readable
72
>
* for use in native and web environments.
73
>
*/
74
>
export interface Readable<T> {
75
>
76
>
/**
77
>
* Read data from the underlying source. Will return
78
>
* null to indicate that no more data can be read.
79
>
*/
80
>
read(): T | null;
81
>
}
82
>
83
>
export function isReadable<T>(obj: unknown): obj is Readable<T> {
84
const candidate = obj as Readable<T> | undefined;
85
if (!candidate) {