Skip to content

Commit

Permalink
Fix readableStreamToReadable
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Nov 11, 2022
1 parent a1e4437 commit 1e7b5ec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-points-drop.md
@@ -0,0 +1,5 @@
---
'@whatwg-node/fetch': patch
---

Fix destroy method for ReadableStream to Readable conversion
27 changes: 16 additions & 11 deletions packages/fetch/dist/readableStreamToReadable.js
@@ -1,20 +1,25 @@
const streams = require('stream');

module.exports = function readableStreamToReadable(readableStream) {
return streams.Readable.from({
[Symbol.asyncIterator]() {
const reader = readableStream.getReader();
return {
next() {
return reader.read();
},
async return() {
const reader = readableStream.getReader();
return new streams.Readable({
read() {
reader.read().then(({ done, value }) => {
if (done) {
this.push(null);
} else {
this.push(value);
}
})
},
async destroy() {
try {
reader.cancel();
reader.releaseLock();
await readableStream.cancel();
return { done: true };
}
} catch (error) {
console.log(error);
}
}
});
})
}

0 comments on commit 1e7b5ec

Please sign in to comment.