From 701e0ada4828394264e2060484011075e8c4b544 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Thu, 1 Oct 2026 04:21:30 +0000 Subject: [PATCH 1/2] fix: map UTF-8-SIG so BOM documents do not crash after jschardet 4 jschardet 4 reports chardet labels such as UTF-8-SIG that iconv-lite does not recognize. Decoding then threw on common UTF-8-with-BOM input when no charset was declared in headers or a meta tag. --- index.js | 25 ++++++++++++++++++------- test/index.js | 16 ++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 0056649..34d1987 100644 --- a/index.js +++ b/index.js @@ -5,9 +5,20 @@ const isBuffer = require('is-buffer') const iconv = require('iconv-lite') const charset = require('charset') +// jschardet 4 reports chardet labels; some (notably UTF-8-SIG) are not iconv-lite names. +const ICONV_ALIASES = { + 'utf-8-sig': 'utf-8' +} + const inferredEncoding = content => { - const charset = jschardet.detect(content) - return charset && charset.encoding + const detected = jschardet.detect(content) + return detected && detected.encoding +} + +const resolveEncoding = (encoding, fallback) => { + if (!encoding) return fallback + const mapped = ICONV_ALIASES[encoding.toLowerCase()] || encoding + return iconv.encodingExists(mapped) ? mapped : fallback } module.exports = targetEncoding => { @@ -16,13 +27,13 @@ module.exports = targetEncoding => { } const getEncoding = (content, contentType) => - charset({ 'content-type': contentType }, content) || - inferredEncoding(content) || - targetEncoding + resolveEncoding( + charset({ 'content-type': contentType }, content) || inferredEncoding(content), + targetEncoding + ) return (buffer, contentType) => { if (!isBuffer(buffer)) throw new TypeError('content should be a buffer.') - const encoding = getEncoding(buffer, contentType) - return iconv.decode(buffer, encoding) + return iconv.decode(buffer, getEncoding(buffer, contentType)) } } diff --git a/test/index.js b/test/index.js index 4a8c3bf..6e7120f 100644 --- a/test/index.js +++ b/test/index.js @@ -31,3 +31,19 @@ test('inferred', t => { const output = toUTF8(buffer, 'text/html') t.true(output.includes('次常用國字標準字體表')) }) + +test('UTF-8 with BOM and no charset hint', t => { + const buffer = Buffer.concat([ + Buffer.from([0xef, 0xbb, 0xbf]), + Buffer.from('日本語') + ]) + const output = toUTF8(buffer, 'text/html') + t.true(output.includes('日本語')) + t.false(output.startsWith('\uFEFF')) +}) + +test('falls back when the detected encoding is unknown to iconv', t => { + const buffer = Buffer.from('hello') + const output = toUTF8(buffer, 'text/html; charset=iso-2022-jp') + t.true(output.includes('hello')) +}) From 15978f48971c0428271b40d8d7fa6b076cb69055 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Thu, 1 Oct 2026 18:13:24 +0200 Subject: [PATCH 2/2] fix: throw when iconv cannot decode a charset A missing iconv codec was decoded as the target encoding, including an explicit Content-Type charset. UTF-8-SIG still maps to utf-8. Co-authored-by: Cursor --- index.js | 16 +++++++--------- test/index.js | 6 +++--- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 34d1987..4178a63 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ const isBuffer = require('is-buffer') const iconv = require('iconv-lite') const charset = require('charset') -// jschardet 4 reports chardet labels; some (notably UTF-8-SIG) are not iconv-lite names. +// jschardet 4 names UTF-8 with a BOM `UTF-8-SIG`. That is UTF-8; iconv-lite has no such codec. const ICONV_ALIASES = { 'utf-8-sig': 'utf-8' } @@ -15,10 +15,9 @@ const inferredEncoding = content => { return detected && detected.encoding } -const resolveEncoding = (encoding, fallback) => { - if (!encoding) return fallback - const mapped = ICONV_ALIASES[encoding.toLowerCase()] || encoding - return iconv.encodingExists(mapped) ? mapped : fallback +const toIconvName = encoding => { + if (!encoding) return encoding + return ICONV_ALIASES[encoding.toLowerCase()] || encoding } module.exports = targetEncoding => { @@ -27,10 +26,9 @@ module.exports = targetEncoding => { } const getEncoding = (content, contentType) => - resolveEncoding( - charset({ 'content-type': contentType }, content) || inferredEncoding(content), - targetEncoding - ) + toIconvName(charset({ 'content-type': contentType }, content)) || + toIconvName(inferredEncoding(content)) || + targetEncoding return (buffer, contentType) => { if (!isBuffer(buffer)) throw new TypeError('content should be a buffer.') diff --git a/test/index.js b/test/index.js index 6e7120f..f94fa02 100644 --- a/test/index.js +++ b/test/index.js @@ -42,8 +42,8 @@ test('UTF-8 with BOM and no charset hint', t => { t.false(output.startsWith('\uFEFF')) }) -test('falls back when the detected encoding is unknown to iconv', t => { +test('throws when the declared charset is unknown to iconv', t => { const buffer = Buffer.from('hello') - const output = toUTF8(buffer, 'text/html; charset=iso-2022-jp') - t.true(output.includes('hello')) + const error = t.throws(() => toUTF8(buffer, 'text/html; charset=iso-2022-jp')) + t.true(error.message.includes('iso-2022-jp')) })