-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve an error message when process failed due to the wrong en…
…coding (#437)
- Loading branch information
Showing
4 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { isMismatchEncoding } from "../encoding"; | ||
import path from "path"; | ||
|
||
describe("isMismatchEncoding", () => { | ||
it("should detect the mismatch encoding correctly", async () => { | ||
const inputSJISFile = path.join(__dirname, "./fixtures/input_sjis.csv"); | ||
expect(await isMismatchEncoding(inputSJISFile, "sjis")).toBe(false); | ||
expect(await isMismatchEncoding(inputSJISFile, "utf8")).toBe(true); | ||
|
||
const inputUTF8File = path.join(__dirname, "./fixtures/input_utf8.csv"); | ||
expect(await isMismatchEncoding(inputUTF8File, "sjis")).toBe(true); | ||
expect(await isMismatchEncoding(inputUTF8File, "utf8")).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import fs from "fs"; | ||
import iconv from "iconv-lite"; | ||
import readline from "readline"; | ||
import { extractFileFormat } from "./file"; | ||
import type { SupportedImportEncoding } from "./file"; | ||
import { Transform } from "stream"; | ||
|
||
export const isMismatchEncoding = async ( | ||
filePath: string, | ||
encoding: SupportedImportEncoding, | ||
): Promise<boolean> => { | ||
const format = extractFileFormat(filePath); | ||
switch (format) { | ||
case "csv": | ||
return isMismatchEncodingOfCsvFile(filePath, encoding); | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
const isMismatchEncodingOfCsvFile: ( | ||
filePath: string, | ||
encoding: SupportedImportEncoding, | ||
) => Promise<boolean> = async (filePath, encoding) => { | ||
const decodedFirstLine = await getDecodedFirstLine(filePath, encoding); | ||
return containsUntranslatableChars(decodedFirstLine); | ||
}; | ||
|
||
const getDecodedFirstLine: ( | ||
filePath: string, | ||
encoding: SupportedImportEncoding, | ||
) => Promise<string> = async (filePath, encoding) => { | ||
const stream = fs.createReadStream(filePath); | ||
const decodedStream = stream.pipe( | ||
Transform.from(iconv.decodeStream(encoding)), | ||
); | ||
stream.on("error", (e) => { | ||
decodedStream.destroy(e); | ||
}); | ||
|
||
const reader = readline.createInterface({ | ||
input: decodedStream, | ||
}); | ||
|
||
const { value: firstRow } = await reader[Symbol.asyncIterator]().next(); | ||
reader.close(); | ||
|
||
return firstRow; | ||
}; | ||
|
||
const containsUntranslatableChars: (content: string) => boolean = (content) => { | ||
const untranslatableChars = ["�", "?"]; | ||
return untranslatableChars.some((char) => content.includes(char)); | ||
}; |