Skip to content

Make read function sync #797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ReadableWebToNodeStream extends Readable {
* https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultReader
*/
private reader: ReadableStreamDefaultReader<Uint8Array>;
private pendingRead: Promise<{ done: boolean; value?: Uint8Array }> | undefined;
private pendingRead: Promise<void> | undefined;

/**
*
Expand All @@ -33,23 +33,27 @@ export class ReadableWebToNodeStream extends Readable {
* the implementation should begin pushing that data into the read queue
* https://nodejs.org/api/stream.html#stream_readable_read_size_1
*/
public async _read(): Promise<void> {
public _read(): void {
// Should start pushing data into the queue
// Read data from the underlying Web-API-readable-stream
if (this.released) {
this.push(null); // Signal EOF
return;
}
this.pendingRead = this.reader.read();
const data = await this.pendingRead;
// clear the promise before pushing new data to the queue and allow sequential calls to _read()
this.pendingRead = undefined;
if (data.done || this.released) {
this.push(null); // Signal EOF
} else if (data.value) {
this.bytesRead += data.value.length;
this.push(data.value); // Push new data to the queue
}
this.pendingRead = this.reader
.read()
.then((data) => {
delete this.pendingRead;
if (data.done || this.released) {
this.push(null); // Signal EOF
} else {
this.bytesRead += data.value.length;
this.push(data.value); // Push new data to the queue
}
})
.catch((err) => {
this.destroy(err);
});
}

/**
Expand Down
Loading