Skip to content

Commit 3b0ce14

Browse files
committed
Meta tweaks
Signed-off-by: Richie Bendall <[email protected]>
1 parent a4efa37 commit 3b0ce14

File tree

9 files changed

+77
-73
lines changed

9 files changed

+77
-73
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Build directories
22
dist/
3-
docs/
43

54
# Lock files
65
package-lock.json

.travis.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,3 @@ deploy:
1919
api_key: $npm_token
2020
on:
2121
tags: true
22-
23-
- provider: pages
24-
skip_cleanup: true
25-
github_commit: "chore: Published documentation [skip ci]"
26-
github_token: $github_token
27-
committer_from_gh: true
28-
keep_history: true
29-
local_dir: docs
30-
on:
31-
tags: true

README.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
1-
# Fetch Charset Detection
1+
# fetch-charset-detection [![Travis CI Build Status](https://img.shields.io/travis/com/Richienb/fetch-charset-detection/master.svg?style=for-the-badge)](https://travis-ci.com/Richienb/fetch-charset-detection)
22

3-
> Charset detection and conversion, originally from `node-fetch`.
4-
5-
[![Build Status](https://travis-ci.com/node-fetch/fetch-charset-detection.svg?branch=master)](https://travis-ci.com/node-fetch/fetch-charset-detection)
3+
Detect the encoding of a buffer and stringify it. Originally from [node-fetch](https://github.com/node-fetch/node-fetch)
64

75
## Install
86

9-
```
7+
```sh
108
npm install fetch-charset-detection
119
```
1210

1311
## Usage
1412

1513
```js
16-
const convertBody = require("fetch-charset-detection");
14+
const convertBody = require("fetch-charset-detection")
1715

18-
convertBody(data, headers);
16+
convertBody(content)
1917
```
2018

2119
## API
2220

23-
Refer to the [documentation](https://richienb.github.io/fetch-charset-detection).
21+
### convertBody(content, headers?)
22+
23+
#### content
24+
25+
Type: `Buffer`
26+
27+
The content to stringify.
28+
29+
#### headers
30+
31+
Type: [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers)
32+
33+
The HTTP headers provided with the content.

package.json

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,65 @@
11
{
22
"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-
],
113
"version": "0.0.0",
12-
"main": "dist/index.js",
4+
"description": "Detect the encoding of a buffer and stringify it.",
5+
"repository": "https://github.com/Richienb/fetch-charset-detection.git",
6+
"author": "Richie Bendall <[email protected]>",
7+
"license": "MIT",
8+
"main": "index.js",
139
"files": [
14-
"dist"
10+
"index.js",
11+
"index.d.ts"
1512
],
1613
"engines": {
1714
"node": ">=10"
1815
},
19-
"repository": "https://github.com/Richienb/fetch-charset-detection.git",
20-
"author": "Richie Bendall <[email protected]>",
21-
"license": "MIT",
2216
"scripts": {
23-
"docs": "typedoc",
24-
"build": "tsc && typedoc",
17+
"build": "tsc",
2518
"dev": "tsc --watch",
2619
"lint": "xo",
2720
"test": "xo && ava"
2821
},
22+
"keywords": [
23+
"content-type",
24+
"headers",
25+
"http",
26+
"meta",
27+
"parse",
28+
"buffer",
29+
"node-fetch"
30+
],
2931
"dependencies": {
3032
"cheerio": "^1.0.0-rc.3",
3133
"content-type": "^1.0.4",
32-
"iconv-lite": "^0.6.2",
33-
"nice-try": "^2.0.1"
34+
"iconv-lite": "^0.6.2"
3435
},
3536
"devDependencies": {
3637
"@richienb/tsconfig": "^0.1.1",
37-
"@richienb/typedoc": "^0.1.1",
3838
"@types/cheerio": "^0.22.21",
3939
"@types/content-type": "^1.1.3",
4040
"@types/nice-try": "^2.0.0",
41+
"@typescript-eslint/eslint-plugin": "^4.1.1",
42+
"@typescript-eslint/parser": "^4.1.1",
4143
"ava": "^3.12.1",
4244
"eslint-config-richienb": "^0.4.2",
43-
"node-fetch": "^2.6.0",
45+
"eslint-config-xo-typescript": "^0.32.0",
46+
"node-fetch": "^2.6.1",
4447
"ts-node": "^9.0.0",
45-
"typedoc": "^0.19.0",
46-
"typescript": "^4.0.2",
47-
"xo": "^0.33.0"
48+
"xo": "^0.33.1"
4849
},
4950
"xo": {
50-
"extends": "richienb"
51+
"extends": "richienb",
52+
"rules": {
53+
"node/no-missing-import": 0
54+
},
55+
"overrides": [
56+
{
57+
"files": "test.ts",
58+
"rules": {
59+
"@typescript-eslint/no-unsafe-call": 0
60+
}
61+
}
62+
]
5163
},
5264
"ava": {
5365
"extensions": [

source/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import getCharset from "./utils/get-charset"
2-
import {decode} from "iconv-lite"
2+
import { decode } from "iconv-lite"
33

44
/**
5-
* Detect buffer encoding and convert to target encoding
6-
* ref: http://www.w3.org/TR/2011/WD-html5-20110113/parsing.html#determining-the-character-encoding
7-
*
8-
* @param content The content to convert.
9-
* @param headers HTTP Headers provided with a request.
5+
Detect the encoding of a buffer and stringify it.
6+
@param content The content to stringify.
7+
@param headers The HTTP headers provided with the content.
108
*/
119
function convertBody(content: Buffer, headers?: Headers): string {
1210
// Turn raw buffers into a single utf-8 buffer
1311
return decode(
1412
content,
15-
getCharset(content, headers),
13+
getCharset(content, headers)
1614
)
1715
}
1816

source/utils/get-charset.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import {load} from "cheerio"
1+
import { load } from "cheerio"
22
import parseContentType from "./parse-content-type"
33

44
/**
5-
Get the charset of content.
6-
@param content The content to convert.
5+
Get the charset of `content`.
6+
@param content The content to stringify.
77
@param headers HTTP Headers provided with the request.
88
*/
99
function getCharset(content: Buffer, headers?: Headers) {
10+
// See http://www.w3.org/TR/2011/WD-html5-20110113/parsing.html#determining-the-character-encoding
1011
// Resulting charset
1112
let charset: string
1213

@@ -26,7 +27,7 @@ function getCharset(content: Buffer, headers?: Headers) {
2627
charset = parseContentType(
2728
$("meta[charset]").attr("charset") || // HTML5
2829
$("meta[http-equiv][content]").attr("content") || // HTML4
29-
load(data.replace(/<\?(.*)\?>/im, "<$1>"), {xmlMode: true}).root().find("xml").attr("encoding"), // XML
30+
load(data.replace(/<\?(.*)\?>/im, "<$1>"), { xmlMode: true }).root().find("xml").attr("encoding") // XML
3031
)
3132

3233
// Prevent decode issues when sites use incorrect encoding

source/utils/parse-content-type.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import {parse} from "content-type"
2-
import niceTry from "nice-try"
1+
import { parse } from "content-type"
32

43
/**
54
Get the character set from a Content-Type header.
65
@param contentType The Content-Type HTTP header.
76
*/
87
function parseContentType(contentType: string) {
9-
return niceTry(() => parse(contentType))?.parameters?.charset ?? contentType
8+
try {
9+
return parse(contentType).parameters.charset
10+
} catch (_) {
11+
return contentType
12+
}
1013
}
1114

1215
export = parseContentType

test.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,40 @@
11
import test from "ava"
22
import { Headers } from "node-fetch"
33
import { encode } from "iconv-lite"
4-
import convertBody from "./src"
4+
import convertBody from "./source"
55

6-
test("should support encoding decode, xml dtd detect", (t) => {
6+
test("should support encoding decode, xml dtd detect", t => {
77
const text = "<?xml version=\"1.0\" encoding=\"EUC-JP\"?><title>日本語</title>"
88
t.is(convertBody(encode(text, "EUC-JP"), new Headers({ "Content-Type": "text/xml" })), text)
99
})
1010

11-
test("should support encoding decode, content-type detect", (t) => {
11+
test("should support encoding decode, content-type detect", t => {
1212
const text = "<div>日本語</div>"
1313
t.is(convertBody(encode(text, "Shift_JIS"), new Headers({ "Content-Type": "text/html; charset=Shift-JIS" })), text)
1414
})
1515

16-
test("should support encoding decode, html5 detect", (t) => {
16+
test("should support encoding decode, html5 detect", t => {
1717
const text = "<meta charset=\"gbk\"><div>中文</div>"
1818
t.is(convertBody(encode(text, "gbk"), new Headers({ "Content-Type": "text/html" })), text)
1919
})
2020

21-
test("should support encoding decode, html4 detect", (t) => {
21+
test("should support encoding decode, html4 detect", t => {
2222
const text = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"><div>中文</div>"
2323
t.is(convertBody(encode(text, "gb2312"), new Headers({ "Content-Type": "text/html" })), text)
2424
})
2525

26-
test("should default to utf8 encoding", (t) => {
27-
const text = "中文"
28-
t.is(convertBody(text), text)
29-
})
30-
31-
test("should support uncommon content-type order, end with qs", (t) => {
26+
test("should support uncommon content-type order, end with qs", t => {
3227
const text = "中文"
3328
t.is(convertBody(encode(text, "gbk"), new Headers({ "Content-Type": "text/plain; charset=gbk; qs=1" })), text)
3429
})
3530

36-
test("should support chunked encoding, html4 detect", (t) => {
31+
test("should support chunked encoding, html4 detect", t => {
3732
const text = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Shift_JIS\" /><div>日本語</div>"
3833
const padding = "a".repeat(10)
3934
t.is(convertBody(encode(padding + text, "Shift_JIS"), new Headers({ "Content-Type": "text/html", "Transfer-Encoding": "chunked" })), padding + text)
4035
})
4136

42-
test("should only do encoding detection up to 1024 bytes", (t) => {
37+
test("should only do encoding detection up to 1024 bytes", t => {
4338
const text = "中文"
4439
const padding = "a".repeat(1200)
4540
t.not(convertBody(encode(padding + text, "gbk"), new Headers({ "Content-Type": "text/html", "Transfer-Encoding": "chunked" })), text)

typedoc.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)