(res, onDone, needBinary)
| 467 | } |
| 468 | |
| 469 | function toUtf8 (res, onDone, needBinary) { |
| 470 | const contentType = (res.headers['content-type'] || '').split(';'); |
| 471 | let encoding = null; |
| 472 | for (let i = 0; i < contentType.length; i++) { |
| 473 | const keyValue = contentType[i].split('='); |
| 474 | if (keyValue.length === 2 && keyValue[0].toLowerCase() === 'charset') { |
| 475 | encoding = keyValue[1].toLowerCase(); |
| 476 | } |
| 477 | } |
| 478 | let data = []; |
| 479 | res.setEncoding('binary'); |
| 480 | res.on('data', (chunk) => { |
| 481 | data.push(Buffer.from(chunk, 'binary')); |
| 482 | }); |
| 483 | res.on('end', () => { |
| 484 | const binary = Buffer.concat(data); |
| 485 | if (needBinary) { |
| 486 | onDone(contentType, binary); |
| 487 | } else { |
| 488 | if (encoding && encoding !== 'utf-8') { |
| 489 | onDone(contentType, iconv.encode(iconv.decode(binary, encoding), 'utf-8')); |
| 490 | } else { |
| 491 | onDone(contentType, binary.toString('utf-8')); |
| 492 | } |
| 493 | } |
| 494 | }); |
| 495 | } |
| 496 | |
| 497 | function getProperty (data, variableName) { |
| 498 | variableName = variableName.replace(/\./gi, '\\.'); |
no test coverage detected