From b0cb8630037dcde19fa7c7377b91c40d6921a7da Mon Sep 17 00:00:00 2001 From: boarwell <34891695+boarwell@users.noreply.github.com> Date: Sat, 2 Oct 2021 17:33:52 +0900 Subject: [PATCH] Fix encoding sniffing strategy --- lib/core/index.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/core/index.js b/lib/core/index.js index bfba7b15..920e55b9 100644 --- a/lib/core/index.js +++ b/lib/core/index.js @@ -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'); @@ -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; @@ -236,12 +237,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