Skip to content

@tus/server: preserve upload headers in Next.js Pages API routes - #914

Open
dani7514 wants to merge 3 commits into
tus:mainfrom
dani7514:fix/next-pages-upload-headers
Open

dani7514 wants to merge 3 commits into
tus:mainfrom
dani7514:fix/next-pages-upload-headers

Conversation

@dani7514

@dani7514 dani7514 commented Nov 27, 2026 •

Copy link
Copy Markdown

Closes #807.

With compression enabled in a Next.js Pages API route, creating a tus upload can return 201 Created with no Location header. The bundled compression middleware misreads the flat header array supplied by srvx, so the client loses the URL it needs to resume the upload.

For HTTP/1 Pages API responses with a wrapped writeHead, this change passes the headers to middleware as an object at the point srvx writes them. Repeated headers keep their separate values. Writing at that point also lets a download stream that fails before its first bytes return HTTP 500. Regression tests cover upload creation, early stream errors, and multiple Set-Cookie values.

Verification

  • Reproduced the missing header with @tus/server 2.4.7 and the Pages API resolvers from Next.js 15.5.4 and 16.4.6. With the patch, a client created an upload, sent the first chunk, read its offset with HEAD, and sent the remaining bytes. The stored file matched the original with compression both enabled and disabled.
  • All 175 active server tests passed on Node.js 20.19.0, 24.21.0, and 26.13.1. The suite has two existing skips. The TypeScript build and formatting and lint checks for the changed files passed.
  • Three broader checks failed on Windows with both this patch and the upstream baseline. The filesystem test could not create a directory symlink, and two upload-size end-to-end tests saw different connection-reset behavior than they expected.

Devin Review

@changeset-bot

changeset-bot Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 100d949

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@tus/server Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 2 potential issues.

Devin Review

Comment thread packages/server/src/test/Server.test.ts Outdated
Comment on lines +157 to +172
const pagesApp = () =>
http.createServer((req, res) => {
Object.assign(res, {revalidate: async () => {}})
const writeHead = res.writeHead.bind(res)
res.writeHead = (
status,
reason?,
headers?: http.OutgoingHttpHeaders | http.OutgoingHttpHeader[]
) => {
if (typeof reason !== 'string') return writeHead(status, reason)
// The compression bundled with Next.js Pages cannot read flat arrays.
return writeHead(status, reason, Array.isArray(headers) ? undefined : headers)
}
res.setHeader('X-App', 'pages')
void server.handle(req, res)
})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Compressed response completion goes untested

pagesApp leaves res.end untouched. With actual Next.js gzip, a buffered FastResponse reaches srvx’s res.end(callback), which the compressor reads as body data.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +185 to +195
it('preserves an early stream error through Pages middleware', async () => {
server.get('/failed-stream', () => {
const body = new Readable({
read() {
this.destroy(new Error('read failed'))
},
})
return new FastResponse(body, {headers: {'X-Success': 'true'}})
})
const response = await request(pagesApp()).get('/failed-stream').expect(500)
assert.equal(response.headers['x-success'], undefined)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Built-in downloads lack early error coverage

The FastResponse test defers headers until the Node stream is readable. GetHandler.send returns a native Response, so srvx sends success headers before reading the file stream.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

This branch is waiting to be deployed

1 waiting deployment
external-testing — 100d9492 Waiting Nov 28, 2026 by dani7514 via Tests #157
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@tus/server >= v2.1.0 seems now incompatible with Next.js pages API

1 participant