From 748a9099010adbf7d0a39dda249757047bee0111 Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Wed, 31 Jan 2024 12:01:19 +0100 Subject: [PATCH 1/3] forward original request --- packages/uploadthing/src/h3.ts | 1 + packages/uploadthing/src/internal/handler.ts | 2 +- packages/uploadthing/src/server.ts | 5 ++++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/uploadthing/src/h3.ts b/packages/uploadthing/src/h3.ts index 0f0576186c..e86135b9d2 100644 --- a/packages/uploadthing/src/h3.ts +++ b/packages/uploadthing/src/h3.ts @@ -55,6 +55,7 @@ export const createRouteHandler = ( const response = await requestHandler({ nativeRequest: toWebRequest(event), event, + originalRequest: undefined, }); if (response instanceof UploadThingError) { diff --git a/packages/uploadthing/src/internal/handler.ts b/packages/uploadthing/src/internal/handler.ts index b538d333e7..f5937ae185 100644 --- a/packages/uploadthing/src/internal/handler.ts +++ b/packages/uploadthing/src/internal/handler.ts @@ -136,7 +136,7 @@ export const buildRequestHandler = ( nativeRequest: Request; // Forward to middleware handler - originalRequest?: unknown; + originalRequest: unknown; res?: unknown; event?: unknown; }): Promise< diff --git a/packages/uploadthing/src/server.ts b/packages/uploadthing/src/server.ts index 2923eaec9a..03bb63c27b 100644 --- a/packages/uploadthing/src/server.ts +++ b/packages/uploadthing/src/server.ts @@ -48,7 +48,10 @@ export const INTERNAL_DO_NOT_USE_createRouteHandlerCore = < request: Request | { request: Request }, ): Promise => { const req = request instanceof Request ? request : request.request; - const response = await requestHandler({ nativeRequest: req }); + const response = await requestHandler({ + nativeRequest: req, + originalRequest: req, + }); if (response instanceof UploadThingError) { return new Response(JSON.stringify(formatError(response, opts.router)), { From 4f3846f9fad88b45874f98e301f2ffb0cf9a618b Mon Sep 17 00:00:00 2001 From: juliusmarminge Date: Wed, 31 Jan 2024 12:10:50 +0100 Subject: [PATCH 2/3] stricter types to prevent further regression --- packages/uploadthing/src/express.ts | 18 ++++++++++++------ packages/uploadthing/src/fastify.ts | 18 ++++++++++++------ packages/uploadthing/src/h3.ts | 14 ++++++++------ packages/uploadthing/src/internal/handler.ts | 18 +++++++++++++----- packages/uploadthing/src/next-legacy.ts | 18 ++++++++++++------ packages/uploadthing/src/server.ts | 15 +++++++++------ 6 files changed, 66 insertions(+), 35 deletions(-) diff --git a/packages/uploadthing/src/express.ts b/packages/uploadthing/src/express.ts index fa750ccc61..3b2bd016c3 100644 --- a/packages/uploadthing/src/express.ts +++ b/packages/uploadthing/src/express.ts @@ -24,13 +24,15 @@ import { createBuilder } from "./internal/upload-builder"; export type { FileRouter }; +type MiddlewareArgs = { + req: ExpressRequest; + res: ExpressResponse; + event: undefined; +}; + export const createUploadthing = ( opts?: CreateBuilderOptions, -) => - createBuilder< - { req: ExpressRequest; res: ExpressResponse; event: undefined }, - TErrorShape - >(opts); +) => createBuilder(opts); export const createRouteHandler = ( opts: RouterWithConfig, @@ -38,7 +40,10 @@ export const createRouteHandler = ( initLogger(opts.config?.logLevel); incompatibleNodeGuard(); - const requestHandler = buildRequestHandler(opts, "express"); + const requestHandler = buildRequestHandler( + opts, + "express", + ); const getBuildPerms = buildPermissionsInfoHandler(opts); const router = ExpressRouter(); @@ -72,6 +77,7 @@ export const createRouteHandler = ( nativeRequest: toWebRequest(req, url, bodyResult.data), originalRequest: req, res, + event: undefined, }); if (response instanceof UploadThingError) { diff --git a/packages/uploadthing/src/fastify.ts b/packages/uploadthing/src/fastify.ts index 55da311297..c9086eb614 100644 --- a/packages/uploadthing/src/fastify.ts +++ b/packages/uploadthing/src/fastify.ts @@ -24,13 +24,15 @@ import { createBuilder } from "./internal/upload-builder"; export type { FileRouter }; +type MiddlewareArgs = { + req: FastifyRequest; + res: FastifyReply; + event: undefined; +}; + export const createUploadthing = ( opts?: CreateBuilderOptions, -) => - createBuilder< - { req: FastifyRequest; res: FastifyReply; event: undefined }, - TErrorShape - >(opts); +) => createBuilder(opts); export const createRouteHandler = ( fastify: FastifyInstance, @@ -40,7 +42,10 @@ export const createRouteHandler = ( initLogger(opts.config?.logLevel); incompatibleNodeGuard(); - const requestHandler = buildRequestHandler(opts, "fastify"); + const requestHandler = buildRequestHandler( + opts, + "fastify", + ); const getBuildPerms = buildPermissionsInfoHandler(opts); const POST: RouteHandlerMethod = async (req, res) => { @@ -51,6 +56,7 @@ export const createRouteHandler = ( nativeRequest: toWebRequest(req, url), originalRequest: req, res, + event: undefined, }); if (response instanceof UploadThingError) { diff --git a/packages/uploadthing/src/h3.ts b/packages/uploadthing/src/h3.ts index e86135b9d2..bccef54932 100644 --- a/packages/uploadthing/src/h3.ts +++ b/packages/uploadthing/src/h3.ts @@ -25,13 +25,11 @@ import { createBuilder } from "./internal/upload-builder"; export type { FileRouter }; +type MiddlewareArgs = { req: undefined; res: undefined; event: H3Event }; + export const createUploadthing = ( opts?: CreateBuilderOptions, -) => - createBuilder< - { req: undefined; res: undefined; event: H3Event }, - TErrorShape - >(opts); +) => createBuilder(opts); export const createRouteHandler = ( opts: RouterWithConfig, @@ -39,7 +37,10 @@ export const createRouteHandler = ( initLogger(opts.config?.logLevel); incompatibleNodeGuard(); - const requestHandler = buildRequestHandler(opts, "h3"); + const requestHandler = buildRequestHandler( + opts, + "h3", + ); const getBuildPerms = buildPermissionsInfoHandler(opts); return defineEventHandler(async (event) => { @@ -56,6 +57,7 @@ export const createRouteHandler = ( nativeRequest: toWebRequest(event), event, originalRequest: undefined, + res: undefined, }); if (response instanceof UploadThingError) { diff --git a/packages/uploadthing/src/internal/handler.ts b/packages/uploadthing/src/internal/handler.ts index f5937ae185..c514e3fc63 100644 --- a/packages/uploadthing/src/internal/handler.ts +++ b/packages/uploadthing/src/internal/handler.ts @@ -26,7 +26,12 @@ import type { LogLevel } from "./logger"; import { logger } from "./logger"; import { getParseFn } from "./parser"; import { VALID_ACTION_TYPES } from "./types"; -import type { ActionType, FileRouter, UTEvents } from "./types"; +import type { + ActionType, + FileRouter, + MiddlewareFnArgs, + UTEvents, +} from "./types"; /** * Creates a wrapped fetch that will always forward a few headers to the server. @@ -128,7 +133,10 @@ export type UploadThingResponse = { chunkSize: number; }[]; -export const buildRequestHandler = ( +export const buildRequestHandler = < + TRouter extends FileRouter, + Args extends MiddlewareFnArgs, +>( opts: RouterWithConfig, adapter: string, ) => { @@ -136,9 +144,9 @@ export const buildRequestHandler = ( nativeRequest: Request; // Forward to middleware handler - originalRequest: unknown; - res?: unknown; - event?: unknown; + originalRequest: Args["req"]; + res: Args["res"]; + event: Args["event"]; }): Promise< | UploadThingError | { status: 200; body?: UploadThingResponse; cleanup?: Promise } diff --git a/packages/uploadthing/src/next-legacy.ts b/packages/uploadthing/src/next-legacy.ts index abd6c8fe20..9a8e77d935 100644 --- a/packages/uploadthing/src/next-legacy.ts +++ b/packages/uploadthing/src/next-legacy.ts @@ -21,13 +21,15 @@ import { createBuilder } from "./internal/upload-builder"; export type { FileRouter }; +type MiddlewareArgs = { + req: NextApiRequest; + res: NextApiResponse; + event: undefined; +}; + export const createUploadthing = ( opts?: CreateBuilderOptions, -) => - createBuilder< - { req: NextApiRequest; res: NextApiResponse; event: undefined }, - TErrorShape - >(opts); +) => createBuilder(opts); export const createRouteHandler = ( opts: RouterWithConfig, @@ -35,7 +37,10 @@ export const createRouteHandler = ( initLogger(opts.config?.logLevel); incompatibleNodeGuard(); - const requestHandler = buildRequestHandler(opts, "nextjs-pages"); + const requestHandler = buildRequestHandler( + opts, + "nextjs-pages", + ); const getBuildPerms = buildPermissionsInfoHandler(opts); return async (req: NextApiRequest, res: NextApiResponse) => { @@ -53,6 +58,7 @@ export const createRouteHandler = ( nativeRequest: toWebRequest(req, url), originalRequest: req, res, + event: undefined, }); res.setHeader("x-uploadthing-version", UPLOADTHING_VERSION); diff --git a/packages/uploadthing/src/server.ts b/packages/uploadthing/src/server.ts index 03bb63c27b..ddf7df5b70 100644 --- a/packages/uploadthing/src/server.ts +++ b/packages/uploadthing/src/server.ts @@ -18,13 +18,11 @@ export * from "./internal/types"; export { UTApi } from "./sdk"; export { UploadThingError }; +type MiddlewareArgs = { req: Request; res: undefined; event: undefined }; + export const createUploadthing = ( opts?: CreateBuilderOptions, -) => - createBuilder< - { req: Request; res: undefined; event: undefined }, - TErrorShape - >(opts); +) => createBuilder(opts); export interface ResponseWithCleanup extends Response { /** custom property where a Promise may be put that you can await in for example Cloudflare Workers */ @@ -41,7 +39,10 @@ export const INTERNAL_DO_NOT_USE_createRouteHandlerCore = < initLogger(opts.config?.logLevel); incompatibleNodeGuard(); - const requestHandler = buildRequestHandler(opts, adapter); + const requestHandler = buildRequestHandler( + opts, + adapter, + ); const getBuildPerms = buildPermissionsInfoHandler(opts); const POST = async ( @@ -51,6 +52,8 @@ export const INTERNAL_DO_NOT_USE_createRouteHandlerCore = < const response = await requestHandler({ nativeRequest: req, originalRequest: req, + event: undefined, + res: undefined, }); if (response instanceof UploadThingError) { From 7c06ec6c7a6162369a09d3e642f59b2c2ca04c8b Mon Sep 17 00:00:00 2001 From: "Mark R. Florkowski" Date: Wed, 31 Jan 2024 10:32:09 -0800 Subject: [PATCH 3/3] Create afraid-terms-live.md --- .changeset/afraid-terms-live.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/afraid-terms-live.md diff --git a/.changeset/afraid-terms-live.md b/.changeset/afraid-terms-live.md new file mode 100644 index 0000000000..2ff48471b8 --- /dev/null +++ b/.changeset/afraid-terms-live.md @@ -0,0 +1,5 @@ +--- +"uploadthing": patch +--- + +fix(server-handler): forward original request