From 356f4dc528132c4ff733d06269a09bc9ae791080 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 31 Jul 2023 12:53:36 +0200 Subject: [PATCH 1/3] refactor(app): extract handler returned response handling --- src/app.ts | 113 ++++++++++++++++++++++++++++------------------------- 1 file changed, 60 insertions(+), 53 deletions(-) diff --git a/src/app.ts b/src/app.ts index 6cf832f8..57fe4b42 100644 --- a/src/app.ts +++ b/src/app.ts @@ -101,6 +101,7 @@ export function use( export function createAppEventHandler(stack: Stack, options: AppOptions) { const spacing = options.debug ? 2 : undefined; + return eventHandler(async (event) => { // Keep original incoming url accessable event.node.req.originalUrl = @@ -135,62 +136,17 @@ export function createAppEventHandler(stack: Stack, options: AppOptions) { // 4. Handle request const val = await layer.handler(event); - // Already handled - if (event.handled) { + // 5. Try to handle return value + if ( + val !== undefined && + (await handleHandlerEvent(event, val, spacing)) !== false + ) { return; } - // Empty Content - if (val === null) { - event.node.res.statusCode = 204; - return send(event); - } - - if (val) { - // Web Response - if (isWebResponse(val)) { - return sendWebResponse(event, val); - } - - // Stream - if (isStream(val)) { - return sendStream(event, val); - } - - // Buffer - if (val.buffer) { - return send(event, val); - } - - // Blob - if (val.arrayBuffer && typeof val.arrayBuffer === "function") { - return send( - event, - Buffer.from(await (val as Blob).arrayBuffer()), - val.type - ); - } - - // Error - if (val instanceof Error) { - throw createError(val); - } - } - - const valType = typeof val; - - // HTML String - if (valType === "string") { - return send(event, val, MIMES.html); - } - - // JSON Response - if ( - valType === "object" || - valType === "boolean" || - valType === "number" - ) { - return send(event, JSON.stringify(val, undefined, spacing), MIMES.json); + // Already handled + if (event.handled) { + return; } } if (!event.handled) { @@ -222,3 +178,54 @@ function normalizeLayer(input: InputLayer) { handler, } as Layer; } + +function handleHandlerEvent(event: H3Event, val: any, jsonSpace?: number) { + // Empty Content + if (val === null) { + event.node.res.statusCode = 204; + return send(event); + } + + if (val) { + // Web Response + if (isWebResponse(val)) { + return sendWebResponse(event, val); + } + + // Stream + if (isStream(val)) { + return sendStream(event, val); + } + + // Buffer + if (val.buffer) { + return send(event, val); + } + + // Blob + if (val.arrayBuffer && typeof val.arrayBuffer === "function") { + return (val as Blob).arrayBuffer().then((arrayBuffer) => { + return send(event, Buffer.from(arrayBuffer), val.type); + }); + } + + // Error + if (val instanceof Error) { + throw createError(val); + } + } + + const valType = typeof val; + + // HTML String + if (valType === "string") { + return send(event, val, MIMES.html); + } + + // JSON Response + if (valType === "object" || valType === "boolean" || valType === "number") { + return send(event, JSON.stringify(val, undefined, jsonSpace), MIMES.json); + } + + return false; +} From 03464826bcfa0d8dc317a58220de33379596e937 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 1 Aug 2023 12:23:55 +0200 Subject: [PATCH 2/3] reduce additional await --- src/app.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/app.ts b/src/app.ts index 57fe4b42..c61641a2 100644 --- a/src/app.ts +++ b/src/app.ts @@ -137,11 +137,10 @@ export function createAppEventHandler(stack: Stack, options: AppOptions) { const val = await layer.handler(event); // 5. Try to handle return value - if ( - val !== undefined && - (await handleHandlerEvent(event, val, spacing)) !== false - ) { - return; + const handledVal = + val !== undefined && handleHandlerEvent(event, val, spacing); + if (handledVal !== false) { + return handledVal; } // Already handled From f40445f8cddab4a248ecb4516dfcdc158b502fe3 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 1 Aug 2023 12:26:24 +0200 Subject: [PATCH 3/3] rename util --- src/app.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app.ts b/src/app.ts index c61641a2..420cadab 100644 --- a/src/app.ts +++ b/src/app.ts @@ -138,7 +138,7 @@ export function createAppEventHandler(stack: Stack, options: AppOptions) { // 5. Try to handle return value const handledVal = - val !== undefined && handleHandlerEvent(event, val, spacing); + val !== undefined && handleHandlerResponse(event, val, spacing); if (handledVal !== false) { return handledVal; } @@ -178,7 +178,7 @@ function normalizeLayer(input: InputLayer) { } as Layer; } -function handleHandlerEvent(event: H3Event, val: any, jsonSpace?: number) { +function handleHandlerResponse(event: H3Event, val: any, jsonSpace?: number) { // Empty Content if (val === null) { event.node.res.statusCode = 204;