From 298c87e04a2d7e9c811829acd146381766b14b56 Mon Sep 17 00:00:00 2001 From: n4n5 <56606507+Its-Just-Nans@users.noreply.github.com> Date: Sun, 14 Nov 2021 16:03:57 +0100 Subject: [PATCH 1/2] [fix] transform to string --- lib/core/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/index.js b/lib/core/index.js index 920e55b9..a9251f50 100644 --- a/lib/core/index.js +++ b/lib/core/index.js @@ -243,7 +243,7 @@ module.exports = function createMiddleware(_dir, _options) { defaultEncoding: 'UTF-8' }); contentType += `; charset=${sniffedEncoding}`; - stream = Readable.from(bytes) + stream = Readable.from(bytes.toString()) } else { // Assume text types are utf8 contentType += '; charset=UTF-8'; From fc97638340c05ab0a9d19dc4b9eef300833334e1 Mon Sep 17 00:00:00 2001 From: n4n5 <56606507+Its-Just-Nans@users.noreply.github.com> Date: Sun, 14 Nov 2021 16:36:01 +0100 Subject: [PATCH 2/2] [fix] delete toString() --- lib/core/index.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/core/index.js b/lib/core/index.js index a9251f50..58f14443 100644 --- a/lib/core/index.js +++ b/lib/core/index.js @@ -5,7 +5,6 @@ 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'); @@ -235,7 +234,6 @@ module.exports = function createMiddleware(_dir, _options) { const lastModified = (new Date(stat.mtime)).toUTCString(); const etag = generateEtag(stat, weakEtags); let cacheControl = cache; - let stream = null; if (contentType && isTextFile(contentType)) { if (stat.size < buffer.constants.MAX_LENGTH) { const bytes = fs.readFileSync(file); @@ -243,7 +241,6 @@ module.exports = function createMiddleware(_dir, _options) { defaultEncoding: 'UTF-8' }); contentType += `; charset=${sniffedEncoding}`; - stream = Readable.from(bytes.toString()) } else { // Assume text types are utf8 contentType += '; charset=UTF-8'; @@ -329,10 +326,8 @@ module.exports = function createMiddleware(_dir, _options) { return; } - // stream may already have been assigned during encoding sniffing. - if (stream === null) { - stream = fs.createReadStream(file); - } + // create a clean stream using 'createReadStream' + const stream = fs.createReadStream(file); stream.pipe(res); stream.on('error', (err) => {