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
7 changes: 7 additions & 0 deletions .changeset/mighty-worlds-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@whatwg-node/node-fetch': patch
'@whatwg-node/server': patch
'@whatwg-node/fetch': patch
---

Fixes the `TypeError: bodyInit.stream is not a function` error thrown when `@whatwg-node/server` is used with `node:http2` and attempts the incoming HTTP/2 request to parse with `Request.json`, `Request.text`, `Request.formData`, or `Request.blob` methods.
2 changes: 1 addition & 1 deletion packages/node-fetch/src/Body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ function isFormData(value: any): value is FormData {
}

function isBlob(value: any): value is Blob {
return value?.stream != null;
return value?.stream != null && typeof value.stream === 'function';
}

function isURLSearchParams(value: any): value is URLSearchParams {
Expand Down
46 changes: 34 additions & 12 deletions packages/server/test/http2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,22 @@ describeIf(!globalThis.Bun && !globalThis.Deno)('http2', () => {

runTestsForEachFetchImpl((_, { createServerAdapter }) => {
it('should support http2 and respond as expected', async () => {
let calledRequest: Request | undefined;
const handleRequest = jest.fn((_request: Request) => {
calledRequest = _request;
return new Response('Hey there!', {
status: 418,
headers: { 'x-is-this-http2': 'yes', 'content-type': 'text/plain;charset=UTF-8' },
});
const handleRequest = jest.fn(async (request: Request) => {
return Response.json(
{
body: await request.json(),
headers: Object.fromEntries(request.headers),
method: request.method,
url: request.url,
},
{
headers: {
'x-is-this-http2': 'yes',
'content-type': 'text/plain;charset=UTF-8',
},
status: 418,
},
);
});
const adapter = createServerAdapter(handleRequest);

Expand All @@ -49,11 +58,16 @@ describeIf(!globalThis.Bun && !globalThis.Deno)('http2', () => {
const req = client.request({
[constantsHttp2.HTTP2_HEADER_METHOD]: 'POST',
[constantsHttp2.HTTP2_HEADER_PATH]: '/hi',
[constantsHttp2.HTTP2_HEADER_CONTENT_TYPE]: 'application/json',
});

req.write(JSON.stringify({ hello: 'world' }));
req.end();

const receivedNodeRequest = await new Promise<{
headers: Record<string, string | string[] | undefined>;
data: string;
status?: number | undefined;
}>((resolve, reject) => {
req.once(
'response',
Expand All @@ -68,7 +82,8 @@ describeIf(!globalThis.Bun && !globalThis.Deno)('http2', () => {
req.on('end', () => {
resolve({
headers,
data,
data: JSON.parse(data),
status: headers[':status'],
});
});
},
Expand All @@ -77,18 +92,25 @@ describeIf(!globalThis.Bun && !globalThis.Deno)('http2', () => {
});

expect(receivedNodeRequest).toMatchObject({
data: 'Hey there!',
data: {
body: {
hello: 'world',
},
method: 'POST',
url: expect.stringMatching(/^http:\/\/localhost:\d+\/hi$/),
headers: {
'content-type': 'application/json',
},
},
headers: {
':status': 418,
'content-type': 'text/plain;charset=UTF-8',
'x-is-this-http2': 'yes',
},
status: 418,
});

await new Promise<void>(resolve => req.end(resolve));

expect(calledRequest?.method).toBe('POST');
expect(calledRequest?.url).toMatch(/^http:\/\/localhost:\d+\/hi$/);
});
});
});
Loading