|
3 | 3 | *
|
4 | 4 | * MIT License
|
5 | 5 | *
|
6 |
| - * Copyright (c) 2016 - 1019 The Node Fetch Team |
| 6 | + * Copyright (c) 2016 - 2019 The Node Fetch Team |
7 | 7 | *
|
8 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
9 | 9 | * of this software and associated documentation files (the 'Software'), to deal
|
@@ -42,27 +42,21 @@ export function convertBody(buffer: Buffer, headers?: Headers): string {
|
42 | 42 | let charset: string
|
43 | 43 |
|
44 | 44 | // Header
|
45 |
| - if (contentType) { |
46 |
| - charset = getCharSet(contentType) |
47 |
| - } |
| 45 | + if (contentType) charset = getCharSet(contentType) |
48 | 46 |
|
49 | 47 | // No charset in content type, peek at response body for at most 1024 bytes
|
50 | 48 | const res = buffer.slice(0, 1024).toString()
|
51 | 49 |
|
52 | 50 | // HTML5, HTML4 and XML
|
53 |
| - if (!charset && res) { |
54 |
| - charset = getCharSet( |
55 |
| - $(res)("meta[charset]").attr("charset") || // HTML5 |
56 |
| - $(res)("meta[http-equiv][content]").attr("content") || // HTML4 |
57 |
| - $(res.replace(/<\?(.*)\?>/im, "<$1>"), { xmlMode: true }).root().find("xml").attr("encoding") // XML |
58 |
| - ) |
59 |
| - } |
| 51 | + if (!charset && res) charset = getCharSet( |
| 52 | + $(res)("meta[charset]").attr("charset") || // HTML5 |
| 53 | + $(res)("meta[http-equiv][content]").attr("content") || // HTML4 |
| 54 | + $(res.replace(/<\?(.*)\?>/im, "<$1>"), { xmlMode: true }).root().find("xml").attr("encoding") // XML |
| 55 | + ) |
60 | 56 |
|
61 | 57 | // Prevent decode issues when sites use incorrect encoding
|
62 | 58 | // ref: https://hsivonen.fi/encoding-menu/
|
63 |
| - if (charset && charset.toLowerCase() in ["gb2312", "gbk"]) { |
64 |
| - charset = "gb18030" |
65 |
| - } |
| 59 | + if (charset && charset.toLowerCase() in ["gb2312", "gbk"]) charset = "gb18030" |
66 | 60 |
|
67 | 61 | // Turn raw buffers into a single utf-8 buffer
|
68 | 62 | return convert(
|
|
0 commit comments