From 388c1ae4a5e0a939e5973505855233143ad460eb Mon Sep 17 00:00:00 2001 From: OkaYu Date: Thu, 12 Feb 2026 07:13:10 +0900 Subject: [PATCH] fix(fs): change to reject if unsupported encodings in readTextFileLines --- ...upported-encodings-in-readTextFileLines.md | 6 +++++ plugins/fs/guest-js/index.ts | 26 +++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 .changes/change-to-reject-if-unsupported-encodings-in-readTextFileLines.md diff --git a/.changes/change-to-reject-if-unsupported-encodings-in-readTextFileLines.md b/.changes/change-to-reject-if-unsupported-encodings-in-readTextFileLines.md new file mode 100644 index 0000000000..901c618198 --- /dev/null +++ b/.changes/change-to-reject-if-unsupported-encodings-in-readTextFileLines.md @@ -0,0 +1,6 @@ +--- +"fs": pach +"fs-js": pach +--- + +Change to reject if unsupported encodings in readTextFileLines diff --git a/plugins/fs/guest-js/index.ts b/plugins/fs/guest-js/index.ts index 64e53fa313..e6c1682881 100644 --- a/plugins/fs/guest-js/index.ts +++ b/plugins/fs/guest-js/index.ts @@ -782,6 +782,28 @@ async function readTextFile( return new TextDecoder(options?.encoding ?? 'utf-8').decode(bytes) } +/** + * Asserts that the given encoding is ASCII-compatible for LF (0x0A) and CR (0x0D). + * + * @throws if the encoding is UTF-16, "replacement", or otherwise invalid. + */ +function assertLfCrAsciiCompatibleEncoding(encodingLabel?: string): void { + // Normalize the encoding label. + // If it is invalid, a RangeError is thrown here. + const encoding = (new TextDecoder(encodingLabel)).encoding + + // Reject encodings defined in the Web Encoding Standard + // that do not represent LF and CR as 0x0A and 0x0D. + // https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings + if ( + encoding === "utf-16le" || + encoding === "utf-16be" || + encoding === "replacement" + ) { + throw new Error(`Unsupported encoding label: ${encodingLabel}`) + } +} + /** * Returns an async {@linkcode AsyncIterableIterator} over the lines of a file, decoded using the specified encoding (default: UTF-8). * @example @@ -804,6 +826,10 @@ async function readTextFileLines( if (path instanceof URL && path.protocol !== 'file:') { throw new TypeError('Must be a file URL.') } + if (options?.encoding != null) { + // https://github.com/tauri-apps/plugins-workspace/pull/3244#issuecomment-3869568792 + assertLfCrAsciiCompatibleEncoding(options.encoding) + } const pathStr = path instanceof URL ? path.toString() : path