Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .changeset/loose-cities-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@whatwg-node/promise-helpers': patch
---

- Name functions in `iterateAsync` for more readable traces
- `fakePromise` accepts `MaybePromise` as an input
5 changes: 5 additions & 0 deletions .changeset/quiet-corners-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@whatwg-node/node-fetch': patch
---

Use `Array.fromAsync` when possible to collect values
87 changes: 53 additions & 34 deletions packages/node-fetch/src/Body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Buffer } from 'node:buffer';
import { Readable } from 'node:stream';
import busboy from 'busboy';
import { handleMaybePromise, MaybePromise } from '@whatwg-node/promise-helpers';
import { hasArrayBufferMethod, hasBufferMethod, hasBytesMethod, PonyfillBlob } from './Blob.js';
import { PonyfillFile } from './File.js';
import { getStreamFromFormData, PonyfillFormData } from './FormData.js';
Expand Down Expand Up @@ -140,36 +141,44 @@ export class PonyfillBody<TJSON = any> implements Body {
return fakePromise(this._chunks);
}
if (this.bodyType === BodyInitType.AsyncIterable) {
if (Array.fromAsync) {
return handleMaybePromise(
() => Array.fromAsync(this.bodyInit as AsyncIterable<Uint8Array>),
chunks => {
this._chunks = chunks;
return this._chunks;
},
);
}
const iterator = (this.bodyInit as AsyncIterable<Uint8Array>)[Symbol.asyncIterator]();
const collectValue = (): Promise<Uint8Array[]> => {
return iterator.next().then(({ value, done }) => {
this._chunks ||= [];
if (value) {
this._chunks.push(value);
}
if (!done) {
return collectValue();
}
return this._chunks;
});
};
const collectValue = (): MaybePromise<Uint8Array[]> =>
handleMaybePromise(
() => iterator.next(),
({ value, done }) => {
this._chunks ||= [];
if (value) {
this._chunks.push(value);
}
if (!done) {
return collectValue();
}
return this._chunks;
},
);
return collectValue();
}
const _body = this.generateBody();
if (!_body) {
return fakePromise([]);
this._chunks = [];
return fakePromise(this._chunks);
}
this._chunks = [];
_body.readable.on('data', chunk => {
this._chunks!.push(chunk);
});
return new Promise<Uint8Array[]>((resolve, reject) => {
_body.readable.once('end', () => {
resolve(this._chunks!);
});
_body.readable.once('error', e => {
reject(e);
});
_body.readable.once('end', () => resolve(this._chunks!));
_body.readable.once('error', reject);
});
}

Expand All @@ -190,13 +199,18 @@ export class PonyfillBody<TJSON = any> implements Body {
});
return fakePromise(this._blob);
}
return this._collectChunksFromReadable().then(chunks => {
this._blob = new PonyfillBlob(chunks, {
type: this.contentType || '',
size: this.contentLength,
});
return this._blob;
});
return fakePromise(
handleMaybePromise(
() => this._collectChunksFromReadable(),
chunks => {
this._blob = new PonyfillBlob(chunks, {
type: this.contentType || '',
size: this.contentLength,
});
return this._blob;
},
),
);
}

_formData: PonyfillFormData | null = null;
Expand Down Expand Up @@ -299,14 +313,19 @@ export class PonyfillBody<TJSON = any> implements Body {
});
}
}
return this._collectChunksFromReadable().then(chunks => {
if (chunks.length === 1) {
this._buffer = chunks[0] as Buffer;
return this._buffer;
}
this._buffer = Buffer.concat(chunks);
return this._buffer;
});
return fakePromise(
handleMaybePromise(
() => this._collectChunksFromReadable(),
chunks => {
if (chunks.length === 1) {
this._buffer = chunks[0] as Buffer;
return this._buffer;
}
this._buffer = Buffer.concat(chunks);
return this._buffer;
},
),
);
}

bytes(): Promise<Uint8Array> {
Expand Down
8 changes: 5 additions & 3 deletions packages/promise-helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function handleMaybePromise<TInput, TOutput>(
}
}

export function fakePromise<T>(value: T): Promise<T>;
export function fakePromise<T>(value: T): Promise<Awaited<T>>;
export function fakePromise(value: void): Promise<void>;
export function fakePromise<T = void>(value: T): Promise<T> {
if (isPromise(value)) {
Expand Down Expand Up @@ -138,8 +138,10 @@ export function iterateAsync<TInput, TOutput>(
endedEarly = true;
}
return handleMaybePromise(
() => callback(value, endEarly, index++),
result => {
function handleCallback() {
return callback(value, endEarly, index++);
},
function handleCallbackResult(result) {
if (result) {
results?.push(result);
}
Expand Down
Loading