Skip to content

Commit

Permalink
Fix encoding sniffing strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
boarwell committed Oct 2, 2021
1 parent 7d576c9 commit 4392921
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/core/index.js
Expand Up @@ -5,6 +5,8 @@
const path = require('path');
const fs = require('fs');
const url = require('url');
const { Readable } = require('stream');
const buffer = require('buffer');
const mime = require('mime');
const urlJoin = require('url-join');
const showDir = require('./show-dir');
Expand All @@ -13,7 +15,6 @@ 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 @@ -230,12 +231,17 @@ module.exports = function createMiddleware(_dir, _options) {
let cacheControl = cache;
let stream = null;
if (contentType && isTextFile(contentType)) {
const htmlBytes = fs.readFileSync(file);
const sniffedEncoding = htmlEncodingSniffer(htmlBytes, {
defaultEncoding: 'UTF-8'
});
contentType += `; charset=${sniffedEncoding}`;
stream = Readable.from(htmlBytes)
if (stat.size < buffer.constants.MAX_LENGTH) {
const bytes = fs.readFileSync(file);
const sniffedEncoding = htmlEncodingSniffer(bytes, {
defaultEncoding: 'UTF-8'
});
contentType += `; charset=${sniffedEncoding}`;
stream = Readable.from(bytes)
} else {
// Assume text types are utf8
contentType += '; charset=UTF-8';
}
}

if (file === gzippedFile) { // is .gz picked up
Expand Down

0 comments on commit 4392921

Please sign in to comment.