generated from Richienb/typescript-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
No longer accepts a string as the first argument. Signed-off-by: Richie Bendall <[email protected]>
- Loading branch information
Showing
11 changed files
with
106 additions
and
4,534 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import getCharset from "./utils/get-charset" | ||
import {decode} from "iconv-lite" | ||
|
||
/** | ||
* Detect buffer encoding and convert to target encoding | ||
* ref: http://www.w3.org/TR/2011/WD-html5-20110113/parsing.html#determining-the-character-encoding | ||
* | ||
* @param content The content to convert. | ||
* @param headers HTTP Headers provided with a request. | ||
*/ | ||
function convertBody(content: Buffer, headers?: Headers): string { | ||
// Turn raw buffers into a single utf-8 buffer | ||
return decode( | ||
content, | ||
getCharset(content, headers), | ||
) | ||
} | ||
|
||
export = convertBody |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {load} from "cheerio" | ||
import parseContentType from "./parse-content-type" | ||
|
||
/** | ||
Get the charset of content. | ||
@param content The content to convert. | ||
@param headers HTTP Headers provided with the request. | ||
*/ | ||
function getCharset(content: Buffer, headers?: Headers) { | ||
// Resulting charset | ||
let charset: string | ||
|
||
// Try to extract content-type header | ||
const contentType = headers?.get("content-type") | ||
if (contentType) { | ||
charset = parseContentType(contentType) | ||
} | ||
|
||
// No charset in content type, peek at response body for at most 1024 bytes | ||
const data = content.slice(0, 1024).toString() | ||
|
||
// HTML5, HTML4 and XML | ||
if (!charset && data) { | ||
const $ = load(data) | ||
|
||
charset = parseContentType( | ||
$("meta[charset]").attr("charset") || // HTML5 | ||
$("meta[http-equiv][content]").attr("content") || // HTML4 | ||
load(data.replace(/<\?(.*)\?>/im, "<$1>"), {xmlMode: true}).root().find("xml").attr("encoding"), // XML | ||
) | ||
|
||
// Prevent decode issues when sites use incorrect encoding | ||
// ref: https://hsivonen.fi/encoding-menu/ | ||
if (charset && ["gb2312", "gbk"].includes(charset.toLowerCase())) { | ||
charset = "gb18030" | ||
} | ||
} | ||
|
||
return charset || "utf-8" | ||
} | ||
|
||
export = getCharset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {parse} from "content-type" | ||
import niceTry from "nice-try" | ||
|
||
/** | ||
Get the character set from a Content-Type header. | ||
@param contentType The Content-Type HTTP header. | ||
*/ | ||
function parseContentType(contentType: string) { | ||
return niceTry(() => parse(contentType))?.parameters?.charset ?? contentType | ||
} | ||
|
||
export = parseContentType |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"esModuleInterop": true, | ||
"sourceMap": true, | ||
"declaration": true, | ||
"outDir": "dist/", | ||
"target": "es5" | ||
}, | ||
"include": [ | ||
"src/**/*" | ||
] | ||
"extends": "@richienb/tsconfig", | ||
"compilerOptions": { | ||
"outDir": "dist/" | ||
}, | ||
"include": [ | ||
"source" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
{ | ||
"out": "./docs", | ||
"mode": "file", | ||
"target": "ES6", | ||
"ignoreCompilerErrors": true | ||
"extends": "node_modules/@richienb/typedoc", | ||
"out": "./docs" | ||
} |
Oops, something went wrong.