Skip to content

Commit b69157b

Browse files
authored
Avoid .toArray and use Readable's native listeners directly (#2578)
1 parent 14100e3 commit b69157b

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

.changeset/forty-dodos-spend.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@whatwg-node/node-fetch': patch
3+
---
4+
5+
Avoid `.toArray` and use `Readable`'s native listeners directly

packages/node-fetch/src/Body.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,15 @@ export class PonyfillBody<TJSON = any> implements Body {
179179
this._chunks = [];
180180
return fakePromise(this._chunks);
181181
}
182-
return _body.readable.toArray().then(chunks => {
183-
this._chunks = chunks;
184-
return this._chunks;
182+
const chunks: Uint8Array[] = [];
183+
return new Promise<Uint8Array[]>((resolve, reject) => {
184+
_body.readable.on('data', chunk => {
185+
chunks.push(chunk);
186+
});
187+
_body.readable.once('error', reject);
188+
_body.readable.once('end', () => {
189+
resolve((this._chunks = chunks));
190+
});
185191
});
186192
}
187193

0 commit comments

Comments
 (0)