| 443 | } |
| 444 | |
| 445 | decodeText(bytes: Bytes): string { |
| 446 | if (bytes == null) return ''; |
| 447 | if (typeof bytes === 'string') return bytes; |
| 448 | |
| 449 | // Node: |
| 450 | if (typeof Buffer !== 'undefined') { |
| 451 | if (bytes instanceof Buffer) { |
| 452 | return bytes.toString(); |
| 453 | } |
| 454 | if (bytes instanceof Uint8Array) { |
| 455 | return Buffer.from(bytes).toString(); |
| 456 | } |
| 457 | |
| 458 | throw new Error( |
| 459 | `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.`, |
| 460 | ); |
| 461 | } |
| 462 | |
| 463 | // Browser |
| 464 | if (typeof TextDecoder !== 'undefined') { |
| 465 | if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) { |
| 466 | this.textDecoder ??= new TextDecoder('utf8'); |
| 467 | return this.textDecoder.decode(bytes); |
| 468 | } |
| 469 | |
| 470 | throw new Error( |
| 471 | `Unexpected: received non-Uint8Array/ArrayBuffer (${(bytes as any).constructor.name}) in a web platform. Please report this error.`, |
| 472 | ); |
| 473 | } |
| 474 | |
| 475 | throw new Error( |
| 476 | `Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.`, |
| 477 | ); |
| 478 | } |
| 479 | |
| 480 | flush(): string[] { |
| 481 | if (!this.buffer.length && !this.trailingCR) { |