fix: Encoding error when opening Unicode query files

This commit is contained in:
Kenta Kishikawa 2024-01-31 00:01:02 +09:00
parent beb1a00874
commit a526797013
2 changed files with 9 additions and 2 deletions

View File

@ -31,6 +31,7 @@
"diff": "^5.0.0",
"diff2html": "^3.4.13",
"file-selector": "^0.2.4",
"iconv-lite": "^0.6.3",
"js-yaml": "^4.1.0",
"json-stable-stringify": "^1.0.1",
"localforage": "^1.9.0",

View File

@ -55,11 +55,16 @@ function getFileEncoding(filePath, fs) {
if (!e && buf[0] === 0xef && buf[1] === 0xbb && buf[2] === 0xbf) e = 'utf8';
if (!e && buf[0] === 0xfe && buf[1] === 0xff) e = 'utf16be';
if (!e && buf[0] === 0xff && buf[1] === 0xfe) e = 'utf16le';
if (!e) e = 'ascii';
if (!e) e = 'utf8';
return e;
}
function decodeFile(buf: Uint8Array, enc: string) {
const iconv = window.require('iconv-lite');
return iconv.decode(buf, enc);
}
function openElectronJsonLinesFile(filePath, parsed) {
openNewTab({
title: parsed.name,
@ -115,7 +120,8 @@ export function openElectronFileCore(filePath, extensions) {
if (nameLower.endsWith('.sql')) {
const encoding = getFileEncoding(filePath, fs);
const data = fs.readFileSync(filePath, { encoding });
const buf = fs.readFileSync(filePath);
const data = decodeFile(buf, encoding);
newQuery({
title: parsed.name,