* 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)
| 517 | * falling back to the default (`utf-8`) if no valid charset is specified or supported. |
| 518 | */ |
| 519 | function getTextDecoder(contentType: string): TextDecoder { |
| 520 | const match = contentType.match(CHARSET_REGEX); |
| 521 | if (match !== null) { |
| 522 | try { |
| 523 | return new TextDecoder(match[1]); |
| 524 | } catch { |
| 525 | // Catching `RangeError` thrown by `new TextDecoder(...)` when the encoding label is invalid or |
| 526 | // unsupported. There is no synchronous inspection API on `TextDecoder` to verify whether an |
| 527 | // encoding is supported without executing the constructor, so catching the error is required |
| 528 | // before falling back to default UTF-8. |
| 529 | } |
| 530 | } |
| 531 | return new TextDecoder(); |
| 532 | } |