Skip to content

Commit

Permalink
Merge 227b24a into 0806424
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Nov 17, 2018
2 parents 0806424 + 227b24a commit 55b6a91
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const jschardet = require('jschardet')
const isBuffer = require('is-buffer')
const iconv = require('iconv-lite')
const charset = require('charset')
const he = require('he')

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

Expand All @@ -13,7 +14,9 @@ const inferredEncoding = content => {
}

module.exports = targetEncoding => {
if (!targetEncoding) throw new TypeError('Need to provide a target encoding.')
if (!iconv.encodingExists(targetEncoding)) {
throw new TypeError(`Target encoding '${targetEncoding}' not supported.`)
}

const getEncoding = (content, contentType) =>
charset({ 'content-type': contentType }, content) ||
Expand All @@ -23,6 +26,13 @@ module.exports = targetEncoding => {
return (buffer, contentType) => {
if (!isBuffer(buffer)) throw new TypeError('content should be a buffer.')
const encoding = getEncoding(buffer, contentType)
return iconv.decode(buffer, encoding).replace(REGEX_CHARSET, targetEncoding)

const str = iconv
.decode(buffer, encoding)
.replace(REGEX_CHARSET, targetEncoding)
.toString()

// Ensure to resolve Base64 entities
return he.decode(str)
}
}
26 changes: 20 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
],
"dependencies": {
"charset": "~1.0.1",
"he": "~1.2.0",
"iconv-lite": "~0.4.19",
"is-buffer": "~2.0.0",
"jschardet": "~1.6.0",
Expand All @@ -52,20 +53,33 @@
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"dev": "nodemon --exec \"npm start\" -e \"js\"",
"lint": "standard-markdown README.md && standard",
"precommit": "lint-staged",
"precommit-lint": "prettier-standard",
"prelint": "npm run pretty",
"pretest": "npm run lint",
"pretty": "prettier-standard {core,test,bin}/**/*.js",
"start": "node index.js",
"test": "nyc mocha"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"license": "MIT",
"lint-staged": {
"*.js": [
"precommit-lint",
"git add"
]
"linters": {
"package.json": [
"finepack",
"git add"
],
"*.js": [
"prettier-standard",
"git add"
],
"*.md": [
"standard-markdown",
"git add"
]
}
},
"standard": {
"env": [
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/base64.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<html>

<head></head>

<body>
<pre style="word-wrap: break-word; white-space: pre-wrap;">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="UTF-8"&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
&lt;meta http-equiv="X-UA-Compatible" content="ie=edge"&gt;
&lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;a href="https://httpbin-org.herokuapp.com/redirect/3"&gt;&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</body>

</html>
30 changes: 29 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { expect } = require('chai')
const path = require('path')
const fs = require('fs')

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

describe('Encoding Converter', function () {
it('properly decodes Shift-JIS html documents', function () {
Expand Down Expand Up @@ -43,6 +43,34 @@ describe('Encoding Converter', function () {
'<meta http-equiv="Content-Type" content="text/html;utf-8" />'
)
})

it.only('Decode Base64', function () {
const buffer = loadExample('base64.html')
const output = toUTF8(buffer)
expect(output.trim()).to.deep.equal(
`
<html>
<head></head>
<body>
<pre style="word-wrap: break-word; white-space: pre-wrap;"><!DOCTYPE html>
<html lang="en">
<head>
<meta utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<a href="https://httpbin-org.herokuapp.com/redirect/3"></a>
</body>
</html></pre>
</body>
</html>`.trim()
)
})
})

function loadExample (name) {
Expand Down

0 comments on commit 55b6a91

Please sign in to comment.