Skip to content

Commit 227b24a

Browse files
committed
Ensure to resolve base64 entities
1 parent 0806424 commit 227b24a

4 files changed

Lines changed: 81 additions & 9 deletions

File tree

index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const jschardet = require('jschardet')
44
const isBuffer = require('is-buffer')
55
const iconv = require('iconv-lite')
66
const charset = require('charset')
7+
const he = require('he')
78

89
const REGEX_CHARSET = /charset=["]*([^>"\s]+)/i
910

@@ -13,7 +14,9 @@ const inferredEncoding = content => {
1314
}
1415

1516
module.exports = targetEncoding => {
16-
if (!targetEncoding) throw new TypeError('Need to provide a target encoding.')
17+
if (!iconv.encodingExists(targetEncoding)) {
18+
throw new TypeError(`Target encoding '${targetEncoding}' not supported.`)
19+
}
1720

1821
const getEncoding = (content, contentType) =>
1922
charset({ 'content-type': contentType }, content) ||
@@ -23,6 +26,13 @@ module.exports = targetEncoding => {
2326
return (buffer, contentType) => {
2427
if (!isBuffer(buffer)) throw new TypeError('content should be a buffer.')
2528
const encoding = getEncoding(buffer, contentType)
26-
return iconv.decode(buffer, encoding).replace(REGEX_CHARSET, targetEncoding)
29+
30+
const str = iconv
31+
.decode(buffer, encoding)
32+
.replace(REGEX_CHARSET, targetEncoding)
33+
.toString()
34+
35+
// Ensure to resolve Base64 entities
36+
return he.decode(str)
2737
}
2838
}

package.json

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
],
2727
"dependencies": {
2828
"charset": "~1.0.1",
29+
"he": "~1.2.0",
2930
"iconv-lite": "~0.4.19",
3031
"is-buffer": "~2.0.0",
3132
"jschardet": "~1.6.0",
@@ -52,20 +53,33 @@
5253
"coveralls": "nyc report --reporter=text-lcov | coveralls",
5354
"dev": "nodemon --exec \"npm start\" -e \"js\"",
5455
"lint": "standard-markdown README.md && standard",
55-
"precommit": "lint-staged",
56-
"precommit-lint": "prettier-standard",
5756
"prelint": "npm run pretty",
5857
"pretest": "npm run lint",
5958
"pretty": "prettier-standard {core,test,bin}/**/*.js",
6059
"start": "node index.js",
6160
"test": "nyc mocha"
6261
},
62+
"husky": {
63+
"hooks": {
64+
"pre-commit": "lint-staged"
65+
}
66+
},
6367
"license": "MIT",
6468
"lint-staged": {
65-
"*.js": [
66-
"precommit-lint",
67-
"git add"
68-
]
69+
"linters": {
70+
"package.json": [
71+
"finepack",
72+
"git add"
73+
],
74+
"*.js": [
75+
"prettier-standard",
76+
"git add"
77+
],
78+
"*.md": [
79+
"standard-markdown",
80+
"git add"
81+
]
82+
}
6983
},
7084
"standard": {
7185
"env": [

test/fixtures/base64.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<html>
2+
3+
<head></head>
4+
5+
<body>
6+
<pre style="word-wrap: break-word; white-space: pre-wrap;">&lt;!DOCTYPE html&gt;
7+
&lt;html lang="en"&gt;
8+
&lt;head&gt;
9+
&lt;meta charset="UTF-8"&gt;
10+
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
11+
&lt;meta http-equiv="X-UA-Compatible" content="ie=edge"&gt;
12+
&lt;title&gt;Document&lt;/title&gt;
13+
&lt;/head&gt;
14+
&lt;body&gt;
15+
&lt;a href="https://httpbin-org.herokuapp.com/redirect/3"&gt;&lt;/a&gt;
16+
&lt;/body&gt;
17+
&lt;/html&gt;</pre>
18+
</body>
19+
20+
</html>

test/index.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { expect } = require('chai')
44
const path = require('path')
55
const fs = require('fs')
66

7-
const toUTF8 = require('../index')('utf-8')
7+
const toUTF8 = require('..')('utf-8')
88

99
describe('Encoding Converter', function () {
1010
it('properly decodes Shift-JIS html documents', function () {
@@ -43,6 +43,34 @@ describe('Encoding Converter', function () {
4343
'<meta http-equiv="Content-Type" content="text/html;utf-8" />'
4444
)
4545
})
46+
47+
it.only('Decode Base64', function () {
48+
const buffer = loadExample('base64.html')
49+
const output = toUTF8(buffer)
50+
expect(output.trim()).to.deep.equal(
51+
`
52+
<html>
53+
54+
<head></head>
55+
56+
<body>
57+
<pre style="word-wrap: break-word; white-space: pre-wrap;"><!DOCTYPE html>
58+
<html lang="en">
59+
<head>
60+
<meta utf-8">
61+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
62+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
63+
<title>Document</title>
64+
</head>
65+
<body>
66+
<a href="https://httpbin-org.herokuapp.com/redirect/3"></a>
67+
</body>
68+
</html></pre>
69+
</body>
70+
71+
</html>`.trim()
72+
)
73+
})
4674
})
4775

4876
function loadExample (name) {

0 commit comments

Comments
 (0)