Skip to content

fix: ignore a UTF-8 BOM at the start of the input in decode - #325

Open
SwastikTripathi wants to merge 1 commit into
npm:mainfrom
SwastikTripathi:fix/decode-bom
Open

SwastikTripathi wants to merge 1 commit into
npm:mainfrom
SwastikTripathi:fix/decode-bom

Conversation

@SwastikTripathi

Copy link
Copy Markdown

An ini file saved as "UTF-8 with BOM" (Notepad's default on older Windows, and an option in most editors) loses its first section header:

ini.decode('[section]\nkey=value\n')
// before: { '[section]': true, key: 'value' }
// after:  { section: { key: 'value' } }

fs.readFileSync(file, 'utf8') keeps the BOM, so the first line is [section], which doesn't match the ^\[ section pattern. It then falls through to the key branch, and unsafe() trims the BOM (JS trim() treats U+FEFF as whitespace), which is why it comes back as a key named [section]. Every key until the next header then ends up at the top level. Keys and comments on the first line are not affected for the same reason, only section headers.

decode now drops a leading U+FEFF before splitting lines. test/bom.js covers a BOM before a section, before CRLF sections, and before a top-level key; it fails without the change. npm test passes, with lint and 100% coverage.

@SwastikTripathi
SwastikTripathi requested a review from a team as a code owner November 11, 2026 03:24
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.

1 participant