* Creates a `TextDecoder` instance using the charset extracted from the `Content-Type` header, * falling back to the default (`utf-8`) if no valid charset is specified or supported.
(contentType: string)
| 481 | * falling back to the default (`utf-8`) if no valid charset is specified or supported. |
| 482 | */ |
| 483 | function getTextDecoder(contentType: string): TextDecoder { |
| 484 | const match = contentType.match(CHARSET_REGEX); |
| 485 | if (match !== null) { |
| 486 | try { |
| 487 | return new TextDecoder(match[1]); |
| 488 | } catch { |
| 489 | // Catching `RangeError` thrown by `new TextDecoder(...)` when the encoding label is invalid or |
| 490 | // unsupported. There is no synchronous inspection API on `TextDecoder` to verify whether an |
| 491 | // encoding is supported without executing the constructor, so catching the error is required |
| 492 | // before falling back to default UTF-8. |
| 493 | } |
| 494 | } |
| 495 | return new TextDecoder(); |
| 496 | } |