Skip to content

Commit bff7299

Browse files
committed
refactor: Update deps, rework docs and only export as commonjs
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
1 parent f7564c4 commit bff7299

File tree

7 files changed

+610
-1394
lines changed

7 files changed

+610
-1394
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Richie Bendall and 2016 - 1019 The Node Fetch Team
3+
Copyright (c) 2019 Richie Bendall
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ Charset detection and conversion, originally from `node-fetch`.
44

55
[![NPM](https://nodei.co/npm/fetch-charset-detection.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/fetch-charset-detection)
66

7-
## Importing
7+
## Install
88

9-
From your NodeJS application:
9+
```
10+
npm install fetch-charset-detection
11+
```
12+
13+
## Usage
1014

1115
```js
1216
const convertBody = require("fetch-charset-detection");
17+
18+
convertBody(data, headers);
1319
```
1420

1521
## API

package.json

+72-72
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,74 @@
11
{
2-
"name": "fetch-charset-detection",
3-
"description": "Charset detection and conversion utilities, originally from node-fetch.",
4-
"keywords": [
5-
"content-type",
6-
"headers",
7-
"http",
8-
"meta",
9-
"node-fetch"
10-
],
11-
"version": "0.0.0",
12-
"main": "dist/index.js",
13-
"files": [
14-
"src/**/*",
15-
"dist/**/*"
16-
],
17-
"repository": "https://github.com/Richienb/fetch-charset-detection.git",
18-
"author": "Richie Bendall <richiebendall@gmail.com>",
19-
"license": "MIT",
20-
"scripts": {
21-
"docs": "typedoc",
22-
"build": "tsc && yarn docs",
23-
"dev": "yarn build --watch",
24-
"lint": "xo",
25-
"test": "yarn lint && ava"
26-
},
27-
"dependencies": {
28-
"@sindresorhus/is": "^1.2.0",
29-
"cheerio": "^1.0.0-rc.3",
30-
"content-type": "^1.0.4",
31-
"iconv-lite": "^0.5.0",
32-
"nice-try": "^2.0.0"
33-
},
34-
"devDependencies": {
35-
"@types/cheerio": "^0.22.13",
36-
"@types/content-type": "^1.1.3",
37-
"ava": "^2.4.0",
38-
"eslint-config-richienb": "^0.3.0",
39-
"node-fetch": "^2.6.0",
40-
"ts-node": "^8.5.4",
41-
"typedoc": "^0.16.4",
42-
"typescript": "^3.7.4",
43-
"xo": "^0.25.3"
44-
},
45-
"resolutions": {
46-
"eslint": "^6.8.0"
47-
},
48-
"xo": {
49-
"extends": "richienb/ts",
50-
"overrides": [
51-
{
52-
"files": "test.js",
53-
"rules": {
54-
"import/default": 0,
55-
"node/no-missing-import": 0,
56-
"import/no-unresolved": 0,
57-
"node/no-unsupported-features/node-builtins": 0
58-
}
59-
}
60-
]
61-
},
62-
"ava": {
63-
"compileEnhancements": false,
64-
"extensions": [
65-
"ts"
66-
],
67-
"require": [
68-
"ts-node/register"
69-
]
70-
},
71-
"engines": {
72-
"node": ">=0.12"
73-
}
2+
"name": "fetch-charset-detection",
3+
"description": "Charset detection and conversion utilities, originally from node-fetch.",
4+
"keywords": [
5+
"content-type",
6+
"headers",
7+
"http",
8+
"meta",
9+
"node-fetch"
10+
],
11+
"version": "0.0.0",
12+
"main": "dist/index.js",
13+
"files": [
14+
"src/**/*",
15+
"dist/**/*"
16+
],
17+
"engines": {
18+
"node": ">=10"
19+
},
20+
"repository": "https://github.com/Richienb/fetch-charset-detection.git",
21+
"author": "Richie Bendall <richiebendall@gmail.com>",
22+
"license": "MIT",
23+
"scripts": {
24+
"docs": "typedoc",
25+
"build": "tsc && yarn docs",
26+
"dev": "yarn build --watch",
27+
"lint": "xo",
28+
"test": "yarn lint && ava"
29+
},
30+
"dependencies": {
31+
"@sindresorhus/is": "^2.0.0",
32+
"cheerio": "^1.0.0-rc.3",
33+
"content-type": "^1.0.4",
34+
"iconv-lite": "^0.5.0",
35+
"nice-try": "^2.0.0"
36+
},
37+
"devDependencies": {
38+
"@types/cheerio": "^0.22.13",
39+
"@types/content-type": "^1.1.3",
40+
"@types/nice-try": "^2.0.0",
41+
"ava": "^3.3.0",
42+
"eslint-config-richienb": "^0.3.0",
43+
"node-fetch": "^2.6.0",
44+
"ts-node": "^8.5.4",
45+
"typedoc": "^0.16.4",
46+
"typescript": "^3.7.4",
47+
"xo": "^0.26.0"
48+
},
49+
"resolutions": {
50+
"eslint": "^6.8.0"
51+
},
52+
"xo": {
53+
"extends": "richienb/ts",
54+
"overrides": [
55+
{
56+
"files": "test.js",
57+
"rules": {
58+
"import/default": 0,
59+
"node/no-missing-import": 0,
60+
"import/no-unresolved": 0,
61+
"node/no-unsupported-features/node-builtins": 0
62+
}
63+
}
64+
]
65+
},
66+
"ava": {
67+
"extensions": [
68+
"ts"
69+
],
70+
"require": [
71+
"ts-node/register"
72+
]
73+
}
7474
}

src/index.ts

+35-61
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,3 @@
1-
/**
2-
* @license
3-
*
4-
* MIT License
5-
*
6-
* Copyright (c) 2019 Richie Bendall and 2016 - 2019 The Node Fetch Team
7-
*
8-
* Permission is hereby granted, free of charge, to any person obtaining a copy
9-
* of this software and associated documentation files (the 'Software'), to deal
10-
* in the Software without restriction, including without limitation the rights
11-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12-
* copies of the Software, and to permit persons to whom the Software is
13-
* furnished to do so, subject to the following conditions:
14-
*
15-
* The above copyright notice and this permission notice shall be included in all
16-
* copies or substantial portions of the Software.
17-
*
18-
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24-
* SOFTWARE.
25-
*/
26-
271
import getCharset from "./utils/get-charset"
282
import { decode } from "iconv-lite"
293
import { load as $ } from "cheerio"
@@ -36,40 +10,40 @@ import is from "@sindresorhus/is"
3610
* @param content The content to convert.
3711
* @param headers HTTP Headers provided with a request.
3812
*/
39-
export default function convertBody(content: Buffer | string, headers?: Headers): string {
40-
// Try to extract content-type header
41-
const contentType = !is.nullOrUndefined(headers) ? headers.get("content-type") : null
42-
43-
// Resulting charset
44-
let charset: string
45-
46-
// Convert to buffer
47-
if (is.string(content)) content = Buffer.from(content)
48-
49-
// Header
50-
if (contentType) charset = getCharset(contentType)
51-
52-
// No charset in content type, peek at response body for at most 1024 bytes
53-
const res = content.slice(0, 1024).toString()
54-
55-
// HTML5, HTML4 and XML
56-
if (!charset && res) {
57-
charset = getCharset(
58-
$(res)("meta[charset]").attr("charset") || // HTML5
59-
$(res)("meta[http-equiv][content]").attr("content") || // HTML4
60-
$(res.replace(/<\?(.*)\?>/im, "<$1>"), { xmlMode: true }).root().find("xml").attr("encoding"), // XML
61-
)
62-
}
63-
64-
// Prevent decode issues when sites use incorrect encoding
65-
// ref: https://hsivonen.fi/encoding-menu/
66-
if (charset && ["gb2312", "gbk"].includes(charset.toLowerCase())) charset = "gb18030"
67-
68-
// Turn raw buffers into a single utf-8 buffer
69-
return decode(
70-
content,
71-
charset || "utf-8",
72-
)
13+
function convertBody(content: Buffer | string, headers?: Headers): string {
14+
// Try to extract content-type header
15+
const contentType = !is.nullOrUndefined(headers) ? headers.get("content-type") : null
16+
17+
// Resulting charset
18+
let charset: string
19+
20+
// Convert to buffer
21+
if (is.string(content)) content = Buffer.from(content)
22+
23+
// Header
24+
if (contentType) charset = getCharset(contentType)
25+
26+
// No charset in content type, peek at response body for at most 1024 bytes
27+
const res = content.slice(0, 1024).toString()
28+
29+
// HTML5, HTML4 and XML
30+
if (!charset && res) {
31+
charset = getCharset(
32+
$(res)("meta[charset]").attr("charset") || // HTML5
33+
$(res)("meta[http-equiv][content]").attr("content") || // HTML4
34+
$(res.replace(/<\?(.*)\?>/im, "<$1>"), { xmlMode: true }).root().find("xml").attr("encoding"), // XML
35+
)
36+
}
37+
38+
// Prevent decode issues when sites use incorrect encoding
39+
// ref: https://hsivonen.fi/encoding-menu/
40+
if (charset && ["gb2312", "gbk"].includes(charset.toLowerCase())) charset = "gb18030"
41+
42+
// Turn raw buffers into a single utf-8 buffer
43+
return decode(
44+
content,
45+
charset || "utf-8",
46+
)
7347
}
7448

75-
module.exports = convertBody
49+
export = convertBody

src/utils/get-charset.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ import niceTry from "nice-try"
55
/**
66
* Get the character set from a Content-Type header.
77
* @param contentType The Content-Type HTTP header.
8-
* @private
98
*/
10-
export default function getCharset(contentType: string): string | null {
11-
if (is.nullOrUndefined(contentType)) return null
9+
export = (contentType: string): string | null => {
10+
if (is.nullOrUndefined(contentType)) return null
1211

13-
const parsed = niceTry(() => parseContentType(contentType))
14-
if (!is.nullOrUndefined(parsed)) return parsed.parameters.charset
15-
else return contentType
12+
const parsed = niceTry(() => parseContentType(contentType))
13+
if (!is.nullOrUndefined(parsed)) return parsed.parameters.charset
14+
else return contentType
1615
}

test.js renamed to test.ts

File renamed without changes.

0 commit comments

Comments
 (0)