Skip to content

fix(node): normalize native content types when replying - #18655

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
clean99:contrib/node-reply-native-content-type-charset
Nov 23, 2026
Merged

kamilmysliwiec merged 1 commit into
nestjs:masterfrom
clean99:contrib/node-reply-native-content-type-charset

Conversation

@clean99

@clean99 clean99 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

A handler returning text after res.setHeader('Content-Type', 'text/html') sends that header without a charset. Wrapping res.send() changes it to text/html; charset=utf-8. Returned objects with a native application/json header have the same inconsistency. This is the follow-up identified in the review of #18527.

Issue Number: N/A

On commit 94df49d7898df2009fc6dd4900ed77847eb8aaab, run after npm ci --legacy-peer-deps and npm run build:

node --input-type=module <<'JS'
import { NodeAdapter } from '@nestjs/platform-node';
const adapter = new NodeAdapter();
adapter.get('/', (_req, res) => {
  res.setHeader('Content-Type', 'text/html');
  adapter.reply(res, '<p>zażółć</p>');
});
const response = await adapter.inject('/');
console.log(response.headers['content-type']);
JS

Before: text/html. After: text/html; charset=utf-8.

What is the new behavior?

writeBody() uses the existing text-header normalization shared by send() and json(). It reads the current header after serialization, preserving error-header corrections and headers set by toJSON(). Rendering uses the same writer and gets the same normalization.

Explicit charsets, raw extension-like values, MIME types without a default charset, and binary replies stay unchanged. Strings still default to text/plain.

Regression: npm test -- packages/platform-node/test/adapters/node-adapter.spec.ts packages/platform-node/test/adapters/node-request.spec.ts.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Full unit suite, CI lint, build, public types, changed-file formatting, and the HTTP-adapter/send-files integration suites pass. The integration run has three existing skipped tests and covers those two directories, not all integration tests.

No public API or dependency changes. This restores the existing header-normalization behavior, so no external documentation changes are needed.

Prepared by an automated contribution pipeline that I build and operate (https://github.com/clean99/software-factory); the tests and evidence above were produced by it.

@kamilmysliwiec
kamilmysliwiec merged commit 97e1254 into nestjs:master Nov 23, 2026
2 checks passed
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.

2 participants