(bytes, charset = 'utf-8')
| 205 | } |
| 206 | |
| 207 | function decodeBytesToString(bytes, charset = 'utf-8') { |
| 208 | const normalizedCharset = String(charset || 'utf-8').trim().toLowerCase(); |
| 209 | const candidates = [normalizedCharset]; |
| 210 | if (normalizedCharset === 'utf8') { |
| 211 | candidates.unshift('utf-8'); |
| 212 | } |
| 213 | if (normalizedCharset === 'gb2312' || normalizedCharset === 'gbk') { |
| 214 | candidates.unshift('gb18030'); |
| 215 | } |
| 216 | |
| 217 | for (const candidate of candidates) { |
| 218 | try { |
| 219 | if (typeof TextDecoder !== 'undefined') { |
| 220 | return new TextDecoder(candidate, { fatal: false }).decode(bytes); |
| 221 | } |
| 222 | } catch { |
| 223 | // ignore and try fallback |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | if (typeof Buffer !== 'undefined') { |
| 228 | return Buffer.from(bytes).toString('utf8'); |
| 229 | } |
| 230 | |
| 231 | let result = ''; |
| 232 | for (const byte of bytes) { |
| 233 | result += String.fromCharCode(byte); |
| 234 | } |
| 235 | return result; |
| 236 | } |
| 237 | |
| 238 | function getCharsetFromContentType(contentType = '') { |
| 239 | const match = String(contentType || '').match(/charset="?([^";]+)"?/i); |
no outgoing calls
no test coverage detected