11import http from 'node:http'
22import { EventEmitter } from 'node:events'
33
4+ import type { ServerRequest } from 'srvx/types'
5+ import { toNodeHandler } from 'srvx/node'
46import debug from 'debug'
57import { EVENTS , ERRORS , EXPOSED_HEADERS , REQUEST_METHODS , TUS_RESUMABLE } from '@tus/utils'
68import type { DataStore , Upload , CancellationContext } from '@tus/utils'
79
8- import { BaseHandler } from './handlers/BaseHandler.js'
910import { GetHandler } from './handlers/GetHandler.js'
1011import { HeadHandler } from './handlers/HeadHandler.js'
1112import { OptionsHandler } from './handlers/OptionsHandler.js'
@@ -15,7 +16,6 @@ import {DeleteHandler} from './handlers/DeleteHandler.js'
1516import { validateHeader } from './validators/HeaderValidator.js'
1617import type { ServerOptions , RouteHandler , WithOptional } from './types.js'
1718import { MemoryLocker } from './lockers/index.js'
18- import { getRequest , setResponse } from './web.js'
1919
2020type Handlers = {
2121 GET : InstanceType < typeof GetHandler >
@@ -27,10 +27,10 @@ type Handlers = {
2727}
2828
2929interface TusEvents {
30- [ EVENTS . POST_CREATE ] : ( req : Request , upload : Upload , url : string ) => void
31- [ EVENTS . POST_RECEIVE ] : ( req : Request , upload : Upload ) => void
32- [ EVENTS . POST_FINISH ] : ( req : Request , res : Response , upload : Upload ) => void
33- [ EVENTS . POST_TERMINATE ] : ( req : Request , res : Response , id : string ) => void
30+ [ EVENTS . POST_CREATE ] : ( req : ServerRequest , upload : Upload , url : string ) => void
31+ [ EVENTS . POST_RECEIVE ] : ( req : ServerRequest , upload : Upload ) => void
32+ [ EVENTS . POST_FINISH ] : ( req : ServerRequest , res : Response , upload : Upload ) => void
33+ [ EVENTS . POST_TERMINATE ] : ( req : ServerRequest , res : Response , id : string ) => void
3434}
3535
3636type on = EventEmitter [ 'on' ]
@@ -119,19 +119,11 @@ export class Server extends EventEmitter {
119119 }
120120
121121 get ( path : string , handler : RouteHandler ) {
122- this . handlers . GET . registerPath ( this . options . path + path , handler )
122+ this . handlers . GET . registerPath ( path , handler )
123123 }
124124
125125 async handle ( req : http . IncomingMessage , res : http . ServerResponse ) {
126- const { proto, host} = BaseHandler . extractHostAndProto (
127- // @ts -expect-error it's fine
128- new Headers ( req . headers ) ,
129- this . options . respectForwardedHeaders
130- )
131- const base = `${ proto } ://${ host } ${ this . options . path } `
132- const webReq = await getRequest ( { request : req , base} )
133- const webRes = await this . handler ( webReq )
134- return setResponse ( res , webRes )
126+ return toNodeHandler ( this . handler . bind ( this ) ) ( req , res )
135127 }
136128
137129 async handleWeb ( req : Request ) {
@@ -142,6 +134,19 @@ export class Server extends EventEmitter {
142134 const context = this . createContext ( )
143135 const headers = new Headers ( )
144136
137+ // @ts -expect-error temporary until https://github.com/unjs/srvx/issues/44 is fixed
138+ req . headers . get = ( key : string ) => {
139+ for ( const [ k , v ] of req . headers . entries ( ) ) {
140+ if ( k === key ) {
141+ if ( v === '' ) {
142+ return null
143+ }
144+ return v
145+ }
146+ }
147+ return null
148+ }
149+
145150 const onError = async ( error : {
146151 status_code ?: number
147152 body ?: string
@@ -164,7 +169,7 @@ export class Server extends EventEmitter {
164169 if ( req . method === 'GET' ) {
165170 const handler = this . handlers . GET
166171 const res = await handler . send ( req , context , headers ) . catch ( onError )
167- context . abort
172+ context . abort ( )
168173 return res
169174 }
170175
0 commit comments