Skip to content

Commit

Permalink
Merge pull request #736 from boarwell/encoding-charset
Browse files Browse the repository at this point in the history
Add encoding charset sniffing
  • Loading branch information
thornjad committed Oct 19, 2021
2 parents 1e89681 + b0cb863 commit cf17c2f
Show file tree
Hide file tree
Showing 6 changed files with 525 additions and 21 deletions.
21 changes: 18 additions & 3 deletions lib/core/index.js
Expand Up @@ -5,13 +5,16 @@
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');
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');

let httpServerCore = null;

Expand Down Expand Up @@ -234,8 +237,17 @@ module.exports = function createMiddleware(_dir, _options) {
let cacheControl = cache;
let stream = null;
if (contentType && isTextFile(contentType)) {
// Assume text types are utf8
contentType += '; charset=UTF-8';
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 Expand Up @@ -317,7 +329,10 @@ module.exports = function createMiddleware(_dir, _options) {
return;
}

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

stream.pipe(res);
stream.on('error', (err) => {
Expand Down

0 comments on commit cf17c2f

Please sign in to comment.