Skip to content

Commit

Permalink
fix: check if the file already starts with a UTF-8 BOM (#8551)
Browse files Browse the repository at this point in the history
fix #8512

If the file is already in UTF-8 BOM format, there's no need to convert
it again. If it's converted again, it will result in garbled characters.

---------

Co-authored-by: beyondkmp <beyondkmkp@gmail.com>
  • Loading branch information
beyondkmp and beyondkmp authored Oct 2, 2024
1 parent 7488456 commit 57cebf4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-spoons-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-builder-lib": patch
---

Check if the file already starts with a UTF-8 BOM
21 changes: 16 additions & 5 deletions packages/app-builder-lib/src/targets/nsis/nsisLicense.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { log } from "builder-util"
import { lcid } from "../../util/langs"
import { getLicenseFiles, getNotLocalizedLicenseFile } from "../../util/license"
import * as path from "path"
Expand All @@ -9,17 +10,27 @@ import * as fs from "fs"

function convertFileToUtf8WithBOMSync(filePath: string): boolean {
try {
const UTF8_BOM_HEADER = Buffer.from([0xef, 0xbb, 0xbf])
const data = fs.readFileSync(filePath)
// UTF-8 BOM is EF BB BF
const BOM = Buffer.from([0xef, 0xbb, 0xbf])
const dataWithBOM = Buffer.concat([BOM, data])

// Check if the file already starts with a UTF-8 BOM
log.debug({ file: log.filePath(filePath) }, "checking file for BOM header")
if (data.length >= UTF8_BOM_HEADER.length && data.subarray(0, UTF8_BOM_HEADER.length).equals(UTF8_BOM_HEADER)) {
log.debug({ file: log.filePath(filePath) }, "file is already in BOM format, skipping conversion.")
return true
}

// If not, add the BOM
const dataWithBOM = Buffer.concat([UTF8_BOM_HEADER, data])
fs.writeFileSync(filePath, dataWithBOM)
log.debug({ file: log.filePath(filePath) }, "file successfully converted to UTF-8 with BOM")
return true
} catch (err) {
console.error("Failed to convert file to UTF-8 with BOM: ", err)
} catch (err: any) {
log.error({ file: log.filePath(filePath), message: err.message ?? err.stack }, "unable to convert file to UTF-8 with BOM")
return false
}
}

export async function computeLicensePage(packager: WinPackager, options: NsisOptions, scriptGenerator: NsisScriptGenerator, languages: Array<string>): Promise<void> {
const license = await getNotLocalizedLicenseFile(options.license, packager)
if (license != null) {
Expand Down

0 comments on commit 57cebf4

Please sign in to comment.