Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"fs": pach
"fs-js": pach
---

Change to reject if unsupported encodings in readTextFileLines
26 changes: 26 additions & 0 deletions plugins/fs/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
Loading