Skip to content

Commit 2dc030e

Browse files
committed
Make read function sync
1 parent a50dfe0 commit 2dc030e

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

lib/index.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class ReadableWebToNodeStream extends Readable {
1616
* https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader
1717
*/
1818
private reader: ReadableStreamDefaultReader<Uint8Array>;
19-
private pendingRead: Promise<{ done: boolean; value?: Uint8Array }> | undefined;
19+
private pendingRead: Promise<void> | undefined;
2020

2121
/**
2222
*
@@ -33,23 +33,27 @@ export class ReadableWebToNodeStream extends Readable {
3333
* the implementation should begin pushing that data into the read queue
3434
* https://nodejs.org/api/stream.html#stream_readable_read_size_1
3535
*/
36-
public async _read(): Promise<void> {
36+
public _read(): void {
3737
// Should start pushing data into the queue
3838
// Read data from the underlying Web-API-readable-stream
3939
if (this.released) {
4040
this.push(null); // Signal EOF
4141
return;
4242
}
43-
this.pendingRead = this.reader.read();
44-
const data = await this.pendingRead;
45-
// clear the promise before pushing new data to the queue and allow sequential calls to _read()
46-
this.pendingRead = undefined;
47-
if (data.done || this.released) {
48-
this.push(null); // Signal EOF
49-
} else if (data.value) {
50-
this.bytesRead += data.value.length;
51-
this.push(data.value); // Push new data to the queue
52-
}
43+
this.pendingRead = this.reader
44+
.read()
45+
.then((data) => {
46+
delete this.pendingRead;
47+
if (data.done || this.released) {
48+
this.push(null); // Signal EOF
49+
} else {
50+
this.bytesRead += data.value.length;
51+
this.push(data.value); // Push new data to the queue
52+
}
53+
})
54+
.catch((err) => {
55+
this.destroy(err);
56+
});
5357
}
5458

5559
/**

0 commit comments

Comments
 (0)