| 428 | } |
| 429 | |
| 430 | decodeText(bytes: Bytes): string { |
| 431 | if (bytes == null) return ''; |
| 432 | if (typeof bytes === 'string') return bytes; |
| 433 | |
| 434 | // Node: |
| 435 | if (typeof Buffer !== 'undefined') { |
| 436 | if (bytes instanceof Buffer) { |
| 437 | return bytes.toString(); |
| 438 | } |
| 439 | if (bytes instanceof Uint8Array) { |
| 440 | return Buffer.from(bytes).toString(); |
| 441 | } |
| 442 | |
| 443 | throw new Error( |
| 444 | `Unexpected: received non-Uint8Array (${bytes.constructor.name}) stream chunk in an environment with a global "Buffer" defined, which this library assumes to be Node. Please report this error.` |
| 445 | ); |
| 446 | } |
| 447 | |
| 448 | // Browser |
| 449 | if (typeof TextDecoder !== 'undefined') { |
| 450 | if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) { |
| 451 | this.textDecoder ??= new TextDecoder('utf8'); |
| 452 | return this.textDecoder.decode(bytes); |
| 453 | } |
| 454 | |
| 455 | throw new Error( |
| 456 | `Unexpected: received non-Uint8Array/ArrayBuffer (${ |
| 457 | (bytes as any).constructor.name |
| 458 | }) in a web platform. Please report this error.` |
| 459 | ); |
| 460 | } |
| 461 | |
| 462 | throw new Error( |
| 463 | `Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.` |
| 464 | ); |
| 465 | } |
| 466 | |
| 467 | flush(): string[] { |
| 468 | if (!this.buffer.length && !this.trailingCR) { |