11import { withoutTrailingSlash } from 'ufo'
2- import type { IncomingMessage , ServerResponse } from './types/node'
32import { lazyHandle , promisifyHandle } from './handle'
4- import type { Handle , LazyHandle , Middleware , PHandle } from './handle '
3+ import { toEventHandler , createEvent } from './event '
54import { createError , sendError } from './error'
65import { send , sendStream , isStream , MIMES } from './utils'
6+ import type { IncomingMessage , ServerResponse } from './types/node'
7+ import type { Handle , LazyHandle , Middleware , PHandle } from './handle'
8+ import type { H3EventHandler } from './event'
79
810export interface Layer {
911 route : string
1012 match ?: Matcher
11- handle : Handle
13+ handler : H3EventHandler
1214}
1315
1416export type Stack = Layer [ ]
@@ -92,6 +94,8 @@ export function use (
9294export function createHandle ( stack : Stack , options : AppOptions ) : PHandle {
9395 const spacing = options . debug ? 2 : undefined
9496 return async function handle ( req : IncomingMessage , res : ServerResponse ) {
97+ const event = createEvent ( req , res )
98+
9599 // @ts -ignore express/connect compatibility
96100 req . originalUrl = req . originalUrl || req . url || '/'
97101 const reqUrl = req . url || '/'
@@ -107,7 +111,7 @@ export function createHandle (stack: Stack, options: AppOptions): PHandle {
107111 if ( layer . match && ! layer . match ( req . url as string , req ) ) {
108112 continue
109113 }
110- const val = await layer . handle ( req , res )
114+ const val = await layer . handler ( event )
111115 if ( res . writableEnded ) {
112116 return
113117 }
@@ -117,7 +121,7 @@ export function createHandle (stack: Stack, options: AppOptions): PHandle {
117121 } else if ( isStream ( val ) ) {
118122 return sendStream ( res , val )
119123 } else if ( type === 'object' || type === 'boolean' || type === 'number' /* IS_JSON */ ) {
120- if ( val && val . buffer ) {
124+ if ( val && ( val as Buffer ) . buffer ) {
121125 return send ( res , val )
122126 } else if ( val instanceof Error ) {
123127 throw createError ( val )
@@ -132,15 +136,18 @@ export function createHandle (stack: Stack, options: AppOptions): PHandle {
132136 }
133137}
134138
135- function normalizeLayer ( layer : InputLayer ) {
136- if ( layer . promisify === undefined ) {
137- layer . promisify = layer . handle . length > 2 /* req, res, next */
139+ function normalizeLayer ( input : InputLayer ) {
140+ if ( input . promisify === undefined ) {
141+ input . promisify = input . handle . length > 2 /* req, res, next */
138142 }
143+
144+ const handle = input . lazy
145+ ? lazyHandle ( input . handle as LazyHandle , input . promisify )
146+ : ( input . promisify ? promisifyHandle ( input . handle ) : input . handle )
147+
139148 return {
140- route : withoutTrailingSlash ( layer . route ) ,
141- match : layer . match ,
142- handle : layer . lazy
143- ? lazyHandle ( layer . handle as LazyHandle , layer . promisify )
144- : ( layer . promisify ? promisifyHandle ( layer . handle ) : layer . handle )
149+ route : withoutTrailingSlash ( input . route ) ,
150+ match : input . match ,
151+ handler : toEventHandler ( handle )
145152 }
146153}
0 commit comments