node-web-stream-adapters
Version:
Node adapters for web streams
39 lines • 1.18 kB
JavaScript
import { Readable } from "stream";
/**
* See https://github.com/comunica/readable-from-web.js/blob/main/lib/ReadableFromWeb.ts
* MIT License
*/
class ReadableFromWeb extends Readable {
reader;
readerClosed;
constructor(stream, options) {
super(options);
this.reader = stream.getReader();
this.readerClosed = false;
this.reader.closed.then(() => {
this.readerClosed = true;
}).catch((error) => {
this.readerClosed = true;
this.destroy(error);
});
}
_destroy(error, callback) {
if (!this.readerClosed) {
this.reader.cancel(error).then().finally(() => {
this.readerClosed = true;
this.reader.releaseLock();
});
}
callback && callback(error);
}
_read() {
this.reader;
this.reader.read()
.then(chunk => this.push(chunk.done ? null : chunk.value))
.catch((error) => this.destroy(error));
}
}
export function webStreamToReadable(stream, options) {
return new ReadableFromWeb(stream, options);
}
//# sourceMappingURL=webStreamToReadable.js.map