src/vs/base/common/stream.ts/listenStream
export function listenStream<T>(stream: ReadableStreamEvents<T>, listener: IStreamListener<T>): IDisposable {
	let destroyed = false;

	stream.on('error', error => {
		if (!destroyed) {
			listener.onError(error);
		}
	});

	stream.on('end', () => {
		if (!destroyed) {
			listener.onEnd();
		}
	});

	// Adding the `data` listener will turn the stream
	// into flowing mode. As such it is important to
	// add this listener last (DO NOT CHANGE!)
	stream.on('data', data => {
		if (!destroyed) {
			listener.onData(data);
		}
	});

	return toDisposable(() => destroyed = true);
}