Skip to content

Commit

Permalink
Fix readableStreamToReadable (#179)
Browse files Browse the repository at this point in the history
* Fix readableStreamToReadable

* Fix prettier
  • Loading branch information
ardatan committed Nov 11, 2022
1 parent d9ac31a commit 3297c87
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 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
3 changes: 2 additions & 1 deletion .prettierignore
@@ -1 +1,2 @@
dist
dist
CHANGELOG.md
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 3297c87

Please sign in to comment.