Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@tus/server: allow passing custom headers #519

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ Return a relative URL as the `Location` header to the client (`boolean`).

Allow `Forwarded`, `X-Forwarded-Proto`, and `X-Forwarded-Host` headers to override the `Location` header returned by the server (`boolean`).

#### `options.allowedHeaders`

Additional headers sent in `Access-Control-Allow-Headers` (`string[]`).

#### `options.namingFunction`

Control how you want to name files (`(req) => string`)
Expand Down
6 changes: 4 additions & 2 deletions packages/server/src/handlers/OptionsHandler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {BaseHandler} from './BaseHandler'
import {ALLOWED_METHODS, ALLOWED_HEADERS, MAX_AGE} from '../constants'
import {ALLOWED_METHODS, MAX_AGE, HEADERS} from '../constants'

import type http from 'node:http'

// A successful response indicated by the 204 No Content status MUST contain
// the Tus-Version header. It MAY include the Tus-Extension and Tus-Max-Size headers.
export class OptionsHandler extends BaseHandler {
async send(_: http.IncomingMessage, res: http.ServerResponse) {
const allowedHeaders = [...HEADERS, ...(this.options.allowedHeaders ?? [])]

res.setHeader('Access-Control-Allow-Methods', ALLOWED_METHODS)
res.setHeader('Access-Control-Allow-Headers', ALLOWED_HEADERS)
res.setHeader('Access-Control-Allow-Headers', allowedHeaders.join(', '))
res.setHeader('Access-Control-Max-Age', MAX_AGE)
if (this.store.extensions.length > 0) {
res.setHeader('Tus-Extension', this.store.extensions.join(','))
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export type ServerOptions = {
// Allow `Forwarded`, `X-Forwarded-Proto`, and `X-Forwarded-Host` headers
// to override the `Location` header returned by the server.
respectForwardedHeaders?: boolean
// adds custom headers sent in `Access-Control-Allow-Headers`.
allowedHeaders?: string[]
// Control how you want to name files.
// It is important to make these unique to prevent data loss. Only use it if you really need to.
// Default uses `crypto.randomBytes(16).toString('hex')`.
Expand Down
13 changes: 13 additions & 0 deletions packages/server/test/Server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ describe('Server', () => {
})
})

it('OPTIONS should return returns custom headers in Access-Control-Allow-Headers', (done) => {
server.options.allowedHeaders = ['Custom-Header']

request(listener)
.options('/')
.expect(204, '', (err, res) => {
res.headers.should.have.property('access-control-allow-headers')
res.headers['access-control-allow-headers'].should.containEql('Custom-Header')
server.options.allowedHeaders = []
done(err)
})
})

it('HEAD should 404 non files', (done) => {
request(listener)
.head('/')
Expand Down