Skip to content

Commit a45e929

Browse files
authored
Small improvements (#2182)
1 parent 9160316 commit a45e929

File tree

4 files changed

+69
-37
lines changed

4 files changed

+69
-37
lines changed

.changeset/loose-cities-grin.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@whatwg-node/promise-helpers': patch
3+
---
4+
5+
- Name functions in `iterateAsync` for more readable traces
6+
- `fakePromise` accepts `MaybePromise` as an input

.changeset/quiet-corners-tickle.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+
Use `Array.fromAsync` when possible to collect values

packages/node-fetch/src/Body.ts

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { Buffer } from 'node:buffer';
33
import { Readable } from 'node:stream';
44
import busboy from 'busboy';
5+
import { handleMaybePromise, MaybePromise } from '@whatwg-node/promise-helpers';
56
import { hasArrayBufferMethod, hasBufferMethod, hasBytesMethod, PonyfillBlob } from './Blob.js';
67
import { PonyfillFile } from './File.js';
78
import { getStreamFromFormData, PonyfillFormData } from './FormData.js';
@@ -140,36 +141,44 @@ export class PonyfillBody<TJSON = any> implements Body {
140141
return fakePromise(this._chunks);
141142
}
142143
if (this.bodyType === BodyInitType.AsyncIterable) {
144+
if (Array.fromAsync) {
145+
return handleMaybePromise(
146+
() => Array.fromAsync(this.bodyInit as AsyncIterable<Uint8Array>),
147+
chunks => {
148+
this._chunks = chunks;
149+
return this._chunks;
150+
},
151+
);
152+
}
143153
const iterator = (this.bodyInit as AsyncIterable<Uint8Array>)[Symbol.asyncIterator]();
144-
const collectValue = (): Promise<Uint8Array[]> => {
145-
return iterator.next().then(({ value, done }) => {
146-
this._chunks ||= [];
147-
if (value) {
148-
this._chunks.push(value);
149-
}
150-
if (!done) {
151-
return collectValue();
152-
}
153-
return this._chunks;
154-
});
155-
};
154+
const collectValue = (): MaybePromise<Uint8Array[]> =>
155+
handleMaybePromise(
156+
() => iterator.next(),
157+
({ value, done }) => {
158+
this._chunks ||= [];
159+
if (value) {
160+
this._chunks.push(value);
161+
}
162+
if (!done) {
163+
return collectValue();
164+
}
165+
return this._chunks;
166+
},
167+
);
156168
return collectValue();
157169
}
158170
const _body = this.generateBody();
159171
if (!_body) {
160-
return fakePromise([]);
172+
this._chunks = [];
173+
return fakePromise(this._chunks);
161174
}
162175
this._chunks = [];
163176
_body.readable.on('data', chunk => {
164177
this._chunks!.push(chunk);
165178
});
166179
return new Promise<Uint8Array[]>((resolve, reject) => {
167-
_body.readable.once('end', () => {
168-
resolve(this._chunks!);
169-
});
170-
_body.readable.once('error', e => {
171-
reject(e);
172-
});
180+
_body.readable.once('end', () => resolve(this._chunks!));
181+
_body.readable.once('error', reject);
173182
});
174183
}
175184

@@ -190,13 +199,18 @@ export class PonyfillBody<TJSON = any> implements Body {
190199
});
191200
return fakePromise(this._blob);
192201
}
193-
return this._collectChunksFromReadable().then(chunks => {
194-
this._blob = new PonyfillBlob(chunks, {
195-
type: this.contentType || '',
196-
size: this.contentLength,
197-
});
198-
return this._blob;
199-
});
202+
return fakePromise(
203+
handleMaybePromise(
204+
() => this._collectChunksFromReadable(),
205+
chunks => {
206+
this._blob = new PonyfillBlob(chunks, {
207+
type: this.contentType || '',
208+
size: this.contentLength,
209+
});
210+
return this._blob;
211+
},
212+
),
213+
);
200214
}
201215

202216
_formData: PonyfillFormData | null = null;
@@ -299,14 +313,19 @@ export class PonyfillBody<TJSON = any> implements Body {
299313
});
300314
}
301315
}
302-
return this._collectChunksFromReadable().then(chunks => {
303-
if (chunks.length === 1) {
304-
this._buffer = chunks[0] as Buffer;
305-
return this._buffer;
306-
}
307-
this._buffer = Buffer.concat(chunks);
308-
return this._buffer;
309-
});
316+
return fakePromise(
317+
handleMaybePromise(
318+
() => this._collectChunksFromReadable(),
319+
chunks => {
320+
if (chunks.length === 1) {
321+
this._buffer = chunks[0] as Buffer;
322+
return this._buffer;
323+
}
324+
this._buffer = Buffer.concat(chunks);
325+
return this._buffer;
326+
},
327+
),
328+
);
310329
}
311330

312331
bytes(): Promise<Uint8Array> {

packages/promise-helpers/src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function handleMaybePromise<TInput, TOutput>(
4545
}
4646
}
4747

48-
export function fakePromise<T>(value: T): Promise<T>;
48+
export function fakePromise<T>(value: T): Promise<Awaited<T>>;
4949
export function fakePromise(value: void): Promise<void>;
5050
export function fakePromise<T = void>(value: T): Promise<T> {
5151
if (isPromise(value)) {
@@ -138,8 +138,10 @@ export function iterateAsync<TInput, TOutput>(
138138
endedEarly = true;
139139
}
140140
return handleMaybePromise(
141-
() => callback(value, endEarly, index++),
142-
result => {
141+
function handleCallback() {
142+
return callback(value, endEarly, index++);
143+
},
144+
function handleCallbackResult(result) {
143145
if (result) {
144146
results?.push(result);
145147
}

0 commit comments

Comments
 (0)