Skip to content

Commit d7564aa

Browse files
committed
fix: do not set BOM seen if decode() errored
1 parent f6d3926 commit d7564aa

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

fallback/encoding.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ export class TextDecoder {
100100
}
101101
}
102102

103+
let seenBOM = false
103104
if (this.#canBOM) {
104105
const bom = this.#findBom(prefix ?? u)
105106
if (bom) {
106-
if (stream) this.#canBOM = false
107+
seenBOM = true
107108
if (prefix) {
108109
prefix = prefix.subarray(bom)
109110
} else {
@@ -117,7 +118,8 @@ export class TextDecoder {
117118
if (!this.#decode) this.#decode = unicodeDecoder(this.encoding, !this.fatal)
118119
try {
119120
const res = (prefix ? this.#decode(prefix) : '') + this.#decode(u) + suffix
120-
if (res.length > 0 && stream) this.#canBOM = false
121+
// "BOM seen" is set on the current decode call only if it did not error, in "serialize I/O queue" after decoding
122+
if (stream && (seenBOM || res.length > 0)) this.#canBOM = false
121123
return res
122124
} catch (err) {
123125
this.#chunk = null // reset unfinished chunk on errors

0 commit comments

Comments
 (0)