| 1415 | // `force` parses a pending token without waiting for its end, so that an |
| 1416 | // error found before invalid input is reported however the input is chunked |
| 1417 | feed(chunk: string, force = false): void { |
| 1418 | if (chunk.length === 0 && !force) return |
| 1419 | if (!this.started) { |
| 1420 | this.started = true |
| 1421 | if (chunk.charCodeAt(0) === 0xFEFF) { |
| 1422 | this.bom = true |
| 1423 | chunk = chunk.slice(1) |
| 1424 | } |
| 1425 | } |
| 1426 | if (this.bytes) { |
| 1427 | const nonAscii = chunk.search(/[\u0080-\uFFFF]/) |
| 1428 | if (nonAscii !== -1) { |
| 1429 | // The prefix may contain the declaration that selects US-ASCII. |
| 1430 | this.feed(chunk.slice(0, nonAscii), true) |
| 1431 | if (this.ascii) throw this.error("Syntax", "invalid US-ASCII byte", this.received) |
| 1432 | chunk = chunk.slice(nonAscii) |
| 1433 | } |
| 1434 | } |
| 1435 | const invalid = this.bytes ? chunk.search(invalidDecodedChar) : searchInvalid(chunk) |
| 1436 | if (invalid !== -1) { |
| 1437 | // Parse up to the invalid character first, so that earlier errors are |
| 1438 | // reported first however the input is chunked |
| 1439 | this.consume(chunk.slice(0, invalid), true) |
| 1440 | const code = chunk.codePointAt(invalid)!.toString(16).toUpperCase().padStart(4, "0") |
| 1441 | throw this.error("Syntax", `invalid character U+${code}`, this.received) |
| 1442 | } |
| 1443 | this.consume(chunk, force) |
| 1444 | } |
| 1445 | |
| 1446 | end(): void { |
| 1447 | if (this.pending !== undefined) { |