Skip to content

Commit cd5394c

Browse files
committedFeb 6, 2025
Remove h3, write conversion ourselves
1 parent 41cb5d3 commit cd5394c

14 files changed

+351
-274
lines changed
 

‎biome.json

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
"recommended": true,
1313
"style": {
1414
"noParameterAssign": "off"
15+
},
16+
"performance": {
17+
"noDelete": "off"
1518
}
1619
}
1720
},

‎package-lock.json

+19-126
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/server/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
"dependencies": {
2222
"@tus/utils": "^0.5.1",
2323
"debug": "^4.3.4",
24-
"h3": "^1.14.0",
25-
"lodash.throttle": "^4.1.1"
24+
"lodash.throttle": "^4.1.1",
25+
"set-cookie-parser": "^2.7.1"
2626
},
2727
"devDependencies": {
2828
"@types/debug": "^4.1.12",
2929
"@types/lodash.throttle": "^4.1.9",
3030
"@types/mocha": "^10.0.6",
3131
"@types/node": "^22.10.1",
32+
"@types/set-cookie-parser": "^2.4.10",
3233
"@types/sinon": "^17.0.3",
3334
"@types/supertest": "^2.0.16",
3435
"mocha": "^11.0.1",

‎packages/server/src/handlers/BaseHandler.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ export class BaseHandler extends EventEmitter {
3838

3939
if (this.options.generateUrl) {
4040
// user-defined generateUrl function
41-
const {proto, host} = this.extractHostAndProto(req)
41+
const {proto, host} = BaseHandler.extractHostAndProto(
42+
req.headers,
43+
this.options.respectForwardedHeaders
44+
)
4245

4346
return this.options.generateUrl(req, {
4447
proto,
@@ -53,7 +56,10 @@ export class BaseHandler extends EventEmitter {
5356
return `${path}/${id}`
5457
}
5558

56-
const {proto, host} = this.extractHostAndProto(req)
59+
const {proto, host} = BaseHandler.extractHostAndProto(
60+
req.headers,
61+
this.options.respectForwardedHeaders
62+
)
5763

5864
return `${proto}://${host}${path}/${id}`
5965
}
@@ -73,19 +79,19 @@ export class BaseHandler extends EventEmitter {
7379
return decodeURIComponent(match[1])
7480
}
7581

76-
protected extractHostAndProto(req: Request) {
82+
static extractHostAndProto(headers: Headers, respectForwardedHeaders?: boolean) {
7783
let proto: string | undefined
7884
let host: string | undefined
7985

80-
if (this.options.respectForwardedHeaders) {
81-
const forwarded = req.headers.get('forwarded')
86+
if (respectForwardedHeaders) {
87+
const forwarded = headers.get('forwarded')
8288
if (forwarded) {
8389
host ??= reForwardedHost.exec(forwarded)?.[1]
8490
proto ??= reForwardedProto.exec(forwarded)?.[1]
8591
}
8692

87-
const forwardHost = req.headers.get('x-forwarded-host')
88-
const forwardProto = req.headers.get('x-forwarded-proto')
93+
const forwardHost = headers.get('x-forwarded-host')
94+
const forwardProto = headers.get('x-forwarded-proto')
8995

9096
// @ts-expect-error we can pass undefined
9197
if (['http', 'https'].includes(forwardProto)) {
@@ -95,10 +101,10 @@ export class BaseHandler extends EventEmitter {
95101
host ??= forwardHost as string
96102
}
97103

98-
host ??= req.headers.get('host') || new URL(req.url).host
104+
host ??= headers.get('host') as string
99105
proto ??= 'http'
100106

101-
return {host: host as string, proto}
107+
return {host, proto}
102108
}
103109

104110
protected async getLocker(req: Request) {

‎packages/server/src/handlers/DeleteHandler.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {BaseHandler} from './BaseHandler'
22
import {ERRORS, EVENTS, type CancellationContext} from '@tus/utils'
33

44
export class DeleteHandler extends BaseHandler {
5-
async send(req: Request, context: CancellationContext) {
5+
async send(req: Request, context: CancellationContext, headers = new Headers()) {
66
const id = this.getFileIdFromRequest(req)
77
if (!id) {
88
throw ERRORS.FILE_NOT_FOUND
@@ -25,7 +25,7 @@ export class DeleteHandler extends BaseHandler {
2525
} finally {
2626
await lock.unlock()
2727
}
28-
const writtenRes = this.write(204)
28+
const writtenRes = this.write(204, headers)
2929
this.emit(EVENTS.POST_TERMINATE, req, writtenRes, id)
3030
return writtenRes
3131
}

‎packages/server/src/handlers/GetHandler.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ export class GetHandler extends BaseHandler {
5757
/**
5858
* Read data from the DataStore and send the stream.
5959
*/
60-
async send(req: Request, context: CancellationContext): Promise<Response> {
60+
async send(
61+
req: Request,
62+
context: CancellationContext,
63+
headers = new Headers()
64+
): Promise<Response> {
6165
const path = new URL(req.url).pathname
6266
const handler = this.paths.get(path)
6367

@@ -90,11 +94,9 @@ export class GetHandler extends BaseHandler {
9094
// @ts-expect-error exists if supported
9195
const file_stream = await this.store.read(id)
9296
await lock.unlock()
93-
const headers = {
94-
'Content-Length': stats.offset.toString(),
95-
'Content-Type': contentType,
96-
'Content-Disposition': contentDisposition,
97-
}
97+
headers.set('Content-Length', stats.offset.toString())
98+
headers.set('Content-Type', contentType)
99+
headers.set('Content-Disposition', contentDisposition)
98100
return new Response(file_stream, {headers, status: 200})
99101
}
100102

0 commit comments

Comments
 (0)
Failed to load comments.