Skip to content

Commit

Permalink
fix(server): handle runtimes where req.socket isn't available (trpc…
Browse files Browse the repository at this point in the history
  • Loading branch information
KATT authored Dec 29, 2024
1 parent 1032f48 commit 29a2f0d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/server/src/adapters/fastify/fastifyRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import {
// @trpc/server/node-http
import {
incomingMessageToRequest,
type IncomingMessageWithBody,
type NodeHTTPCreateContextOption,
type UniversalIncomingMessage,
} from '../node-http';

export type FastifyHandlerOptions<
Expand Down Expand Up @@ -54,7 +54,7 @@ export async function fastifyRequestHandler<
});
};

const incomingMessage = opts.req.raw as IncomingMessageWithBody;
const incomingMessage: UniversalIncomingMessage = opts.req.raw;

// monkey-path body to the IncomingMessage
if ('body' in opts.req) {
Expand Down
28 changes: 17 additions & 11 deletions packages/server/src/adapters/node-http/incomingMessageToRequest.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import type * as http from 'http';
import { TRPCError } from '../../@trpc/server';

export interface IncomingMessageWithBody extends http.IncomingMessage {
export interface UniversalIncomingMessage
extends Omit<http.IncomingMessage, 'socket'> {
/**
* Many adapters will add a `body` property to the incoming message and pre-parse the body
*/
body?: unknown;
/**
* Socket is not always available in all deployments, so we need to make it optional
* @see https://github.com/trpc/trpc/issues/6341
*/
socket?: http.IncomingMessage['socket'];
}

function createBody(
req: http.IncomingMessage,
req: UniversalIncomingMessage,
opts: {
/**
* Max body size in bytes. If the body is larger than this, the request will be aborted
Expand All @@ -19,16 +25,16 @@ function createBody(
): RequestInit['body'] {
// Some adapters will pre-parse the body and add it to the request object
if ('body' in req) {
if (req.body === undefined) {
// If body property exists but is undefined, return undefined
return undefined;
}
// If the body is already a string, return it directly
if (typeof req.body === 'string') {
return req.body;
}
// If body exists but isn't a string, stringify it as JSON
else if (req.body !== undefined) {
return JSON.stringify(req.body);
}
// If body property exists but is undefined, return undefined
return undefined;
return JSON.stringify(req.body);
}
let size = 0;
let hasClosed = false;
Expand Down Expand Up @@ -71,7 +77,7 @@ function createBody(
},
});
}
export function createURL(req: http.IncomingMessage): URL {
export function createURL(req: UniversalIncomingMessage): URL {
try {
const protocol =
req.socket && 'encrypted' in req.socket && req.socket.encrypted
Expand Down Expand Up @@ -117,7 +123,7 @@ function createHeaders(incoming: http.IncomingHttpHeaders): Headers {
* Convert an [`IncomingMessage`](https://nodejs.org/api/http.html#class-httpincomingmessage) to a [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request)
*/
export function incomingMessageToRequest(
req: http.IncomingMessage,
req: UniversalIncomingMessage,
res: http.ServerResponse,
opts: {
/**
Expand All @@ -130,14 +136,14 @@ export function incomingMessageToRequest(

const onAbort = () => {
res.off('close', onAbort);
req.socket.off('end', onAbort);
req.socket?.off('end', onAbort);

// abort the request
ac.abort();
};

res.once('close', onAbort);
req.socket.once('end', onAbort);
req.socket?.once('end', onAbort);

// Get host from either regular header or HTTP/2 pseudo-header
const url = createURL(req);
Expand Down

0 comments on commit 29a2f0d

Please sign in to comment.