Repository navigation
Conversation
The Node HTTP adapter only removed a leading UTF-8 byte order mark when responseEncoding was unset or exactly 'utf8'. The README lists 'utf-8', 'UTF-8' and 'UTF8' as equivalent options, and Buffer#toString decodes them the same way, but with those spellings U+FEFF stayed at the start of text and data: URL responses. A JSON response with a BOM was then returned unparsed, or rejected with ERR_BAD_RESPONSE when responseType is 'json' and transitional.silentJSONParsing is false. The fetch adapter already strips the BOM whatever responseEncoding is. Match the encoding with /^utf-?8$/i, or an unset encoding, at both places that strip the BOM. Other encodings and arraybuffer or stream responses are unchanged.
Confidence Score: 5/5
[Medium impact] The PR appears safe to merge. SummaryThe Node HTTP adapter now strips a leading BOM for case-insensitive
Reviews (5) · Last reviewed commit: "Merge v1.x into fix-http-utf8-bom-aliase..." · Reviewed by Greptile |
There was a problem hiding this comment.
All reported issues were addressed across 3 files
Reply to a comment to ask cubic a question or push back. It learns from your replies.
View guided diff | Re-trigger cubic
atishay-kasliwal
left a comment
There was a problem hiding this comment.
Served a BOM-prefixed JSON and text body to the http adapter. With responseEncoding utf-8, UTF8 and UTF-8, the JSON now parses, and text and data URLs come back without the BOM, as utf8 and the default already did; master returned the BOM-prefixed string. latin1 and utf16le are unchanged.
Summary
The Node
httpadapter removes a leading UTF-8 byte order mark from buffered text and JSON responses, but only whenresponseEncodingis unset or exactly'utf8'. The README documentsutf-8,UTF-8andUTF8as equivalent, andBuffer#toStringdecodes them the same way, yet with those spellingsU+FEFFstays at the start of the data. A BOM-prefixed JSON response then comes back as an unparsed string, or rejects withERR_BAD_RESPONSEwhenresponseType: 'json'is used withtransitional.silentJSONParsing: false. The fetch adapter already strips the BOM regardless ofresponseEncoding.This adds one helper,
isUTF8Encoding, that matches an unset encoding or/^utf-?8$/i, and uses it at both BOM sites (buffered responses anddata:URLs).Linked issue
No issue. Related: #11477 proposed the same change and was closed by its author without review.
Changes
lib/adapters/http.js:isUTF8Encodinghelper used at the twostripBOMsites.tests/unit/adapters/responseEncodingBom.test.js: new tests forutf8,utf-8,UTF-8andUTF8on buffered text, JSON (including strict parsing) anddata:URL responses, plus negative cases that must keep the BOM (other encodings,arraybuffer,stream).PRE_RELEASE_CHANGELOG.md: Bug Fixes entry.Validation
npm ci --ignore-scripts.npx vitest run --project unit tests/unit/adapters/responseEncodingBom.test.js: 16 of 24 tests fail on the baselib/adapters/http.js(for exampleSyntaxError: Unexpected token '' ... is not valid JSON) and all 24 pass with this change (24 now includes a stream-response case).npm run test:vitest:unit(whole unit project, one worker): 78 files, 1823 tests pass.npm run lint: passes.npm run build: passes.prettier --checkon the changed files: clean.lib/adapters/http.jsis replaced with a null module in browser builds.Compatibility and documentation
index.d.ts,index.d.ctsandPRE_RELEASE_DOCS.mdneed no update (README.mdalready lists these spellings).arraybuffer/streamresponses are unchanged.Checklist
PRE_RELEASE_CHANGELOG.mdentry added or updated (or AGENTS.md's automated release-preparation exception explained)This PR was prepared with AI assistance (Claude). I ran the new tests against both versions of the adapter and ran the full unit suite myself. I cannot apply labels as an outside contributor, so a maintainer needs to apply
ai-assisted.