From f465a0f2a973a65743717e04f0b430e633f936f6 Mon Sep 17 00:00:00 2001 From: Merlijn Vos Date: Tue, 19 Nov 2024 09:24:46 +0100 Subject: [PATCH] @tus/server: send Tus-Version in OPTIONS (#675) * @tus/server: send Tus-Version in OPTIONS * Add changeset --- .changeset/brave-doors-ring.md | 5 +++++ packages/server/src/handlers/OptionsHandler.ts | 8 ++++---- packages/server/test/OptionsHandler.test.ts | 10 ++++++++-- 3 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 .changeset/brave-doors-ring.md diff --git a/.changeset/brave-doors-ring.md b/.changeset/brave-doors-ring.md new file mode 100644 index 00000000..8fbd044e --- /dev/null +++ b/.changeset/brave-doors-ring.md @@ -0,0 +1,5 @@ +--- +"@tus/server": patch +--- + +Send Tus-Version header in OPTIONS diff --git a/packages/server/src/handlers/OptionsHandler.ts b/packages/server/src/handlers/OptionsHandler.ts index 9bb19664..15b7d358 100644 --- a/packages/server/src/handlers/OptionsHandler.ts +++ b/packages/server/src/handlers/OptionsHandler.ts @@ -9,18 +9,18 @@ export class OptionsHandler extends BaseHandler { async send(req: http.IncomingMessage, res: http.ServerResponse) { const maxSize = await this.getConfiguredMaxSize(req, null) + res.setHeader('Tus-Version', '1.0.0') + if (this.store.extensions.length > 0) { + res.setHeader('Tus-Extension', this.store.extensions.join(',')) + } if (maxSize) { res.setHeader('Tus-Max-Size', maxSize) } const allowedHeaders = [...HEADERS, ...(this.options.allowedHeaders ?? [])] - res.setHeader('Access-Control-Allow-Methods', ALLOWED_METHODS) 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(',')) - } return this.write(res, 204) } diff --git a/packages/server/test/OptionsHandler.test.ts b/packages/server/test/OptionsHandler.test.ts index 85aa7b08..7fdae268 100644 --- a/packages/server/test/OptionsHandler.test.ts +++ b/packages/server/test/OptionsHandler.test.ts @@ -7,10 +7,14 @@ import httpMocks from 'node-mocks-http' import {OptionsHandler} from '../src/handlers/OptionsHandler' import {DataStore, ALLOWED_METHODS, ALLOWED_HEADERS, MAX_AGE} from '@tus/utils' -import {MemoryLocker} from '../src' +import {MemoryLocker, type ServerOptions} from '../src' describe('OptionsHandler', () => { - const options = {path: '/test/output', locker: new MemoryLocker()} + const options: ServerOptions = { + path: '/test/output', + locker: new MemoryLocker(), + maxSize: 1024, + } const store = new DataStore() const handler = new OptionsHandler(store, options) @@ -27,6 +31,8 @@ describe('OptionsHandler', () => { 'Access-Control-Allow-Methods': ALLOWED_METHODS, 'Access-Control-Allow-Headers': ALLOWED_HEADERS, 'Access-Control-Max-Age': MAX_AGE, + 'Tus-Version': '1.0.0', + 'Tus-Max-Size': 1024, } await handler.send(req, res) // eslint-disable-next-line guard-for-in