Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] next from trpc:next #70

Merged
merged 1 commit into from
Dec 29, 2024
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
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
Loading