27
27
import getCharset from "./utils/get-charset"
28
28
import { decode } from "iconv-lite"
29
29
import { load as $ } from "cheerio"
30
- import _ from "lodash "
30
+ import is from "@sindresorhus/is "
31
31
32
32
/**
33
33
* Detect buffer encoding and convert to target encoding
@@ -38,32 +38,32 @@ import _ from "lodash"
38
38
*/
39
39
export default function convertBody ( content : Buffer | string , headers ?: Headers ) : string {
40
40
// Try to extract content-type header
41
- const contentType = ! _ . isNil ( headers ) ? headers . get ( "content-type" ) : null
41
+ const contentType = ! is . nullOrUndefined ( headers ) ? headers . get ( "content-type" ) : null
42
42
43
43
// Resulting charset
44
44
let charset : string
45
45
46
46
// Convert to buffer
47
- if ( _ . isString ( content ) ) content = Buffer . from ( content )
47
+ if ( is . string ( content ) ) content = Buffer . from ( content )
48
48
49
49
// Header
50
50
if ( contentType ) charset = getCharset ( contentType )
51
51
52
52
// No charset in content type, peek at response body for at most 1024 bytes
53
- const res = _ . toString ( content . slice ( 0 , 1024 ) )
53
+ const res = content . slice ( 0 , 1024 ) . toString ( )
54
54
55
55
// HTML5, HTML4 and XML
56
56
if ( ! charset && res ) {
57
57
charset = getCharset (
58
58
$ ( res ) ( "meta[charset]" ) . attr ( "charset" ) || // HTML5
59
59
$ ( res ) ( "meta[http-equiv][content]" ) . attr ( "content" ) || // HTML4
60
- $ ( _ . replace ( res , / < \? ( .* ) \? > / im, "<$1>" ) , { xmlMode : true } ) . root ( ) . find ( "xml" ) . attr ( "encoding" ) , // XML
60
+ $ ( res . replace ( / < \? ( .* ) \? > / im, "<$1>" ) , { xmlMode : true } ) . root ( ) . find ( "xml" ) . attr ( "encoding" ) , // XML
61
61
)
62
62
}
63
63
64
64
// Prevent decode issues when sites use incorrect encoding
65
65
// ref: https://hsivonen.fi/encoding-menu/
66
- if ( charset && _ . includes ( [ "gb2312" , "gbk" ] , _ . lowerCase ( charset ) ) ) charset = "gb18030"
66
+ if ( charset && [ "gb2312" , "gbk" ] . includes ( charset . toLowerCase ( ) ) ) charset = "gb18030"
67
67
68
68
// Turn raw buffers into a single utf-8 buffer
69
69
return decode (
0 commit comments