Skip to content

Commit

Permalink
Add encoding sniffing
Browse files Browse the repository at this point in the history
  • Loading branch information
boarwell committed Oct 2, 2021
1 parent 02d6500 commit eccc8a2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
16 changes: 12 additions & 4 deletions lib/core/index.js
Expand Up @@ -12,6 +12,8 @@ const version = require('../../package.json').version;
const status = require('./status-handlers');
const generateEtag = require('./etag');
const optsParser = require('./opts');
const htmlEncodingSniffer = require("html-encoding-sniffer");
const { Readable } = require('stream');

let httpServerCore = null;

Expand Down Expand Up @@ -222,15 +224,18 @@ module.exports = function createMiddleware(_dir, _options) {
// and brotli special case.
const defaultType = opts.contentType || 'application/octet-stream';
let contentType = mime.lookup(file, defaultType);
let charSet;
const range = (req.headers && req.headers.range);
const lastModified = (new Date(stat.mtime)).toUTCString();
const etag = generateEtag(stat, weakEtags);
let cacheControl = cache;
let stream = null;
if (contentType && isTextFile(contentType)) {
// Assume text types are utf8
contentType += '; charset=UTF-8';
const htmlBytes = fs.readFileSync(file);
const sniffedEncoding = htmlEncodingSniffer(htmlBytes, {
defaultEncoding: 'UTF-8'
});
contentType += `; charset=${sniffedEncoding}`;
stream = Readable.from(htmlBytes)
}

if (file === gzippedFile) { // is .gz picked up
Expand Down Expand Up @@ -312,7 +317,10 @@ module.exports = function createMiddleware(_dir, _options) {
return;
}

stream = fs.createReadStream(file);
// streams may already have been assigned during encoding sniffing.
if (stream === null) {
stream = fs.createReadStream(file);
}

stream.pipe(res);
stream.on('error', (err) => {
Expand Down
68 changes: 63 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -89,6 +89,7 @@
"colors": "^1.4.0",
"corser": "^2.0.1",
"he": "^1.1.0",
"html-encoding-sniffer": "^3.0.0",
"http-proxy": "^1.18.0",
"mime": "^1.6.0",
"minimist": "^1.2.5",
Expand Down

0 comments on commit eccc8a2

Please sign in to comment.