From 9f1d2cf276cf2fa70f5a44808819dad8a7b2801b Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Fri, 23 Oct 2020 22:46:39 +0200 Subject: [PATCH] Also allow a string in monkeypatched `end` calls --- operations/development-server.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/operations/development-server.js b/operations/development-server.js index 0ef2c39..2fefed3 100644 --- a/operations/development-server.js +++ b/operations/development-server.js @@ -17,7 +17,7 @@ function monkeyPatchEnd(res) { let originalChunk = args[0]; let prefix; - if (originalChunk == null || Buffer.isBuffer(originalChunk)) { + if (originalChunk == null || typeof originalChunk === "string" || Buffer.isBuffer(originalChunk)) { if (res.getHeader("content-type").startsWith("text/html")) { let contentLength = res.getHeader("content-length"); @@ -31,18 +31,22 @@ function monkeyPatchEnd(res) { // Reset the `end` method back to the original method; we don't need to get in the way anymore res.end = end; + + let originalChunkAsBuffer = (typeof originalChunk === "string") + ? Buffer.from(originalChunk) + : originalChunk; if (prefix != null) { - let chunk = (originalChunk == null) + let chunk = (originalChunkAsBuffer == null) ? reloadClientTag - : Buffer.concat([ originalChunk, reloadClientTag ]); + : Buffer.concat([ originalChunkAsBuffer, reloadClientTag ]); end(chunk, ... args.slice(1)); } else { end(... args); } } else { - throw new Error(`Expected a buffer in 'end', but got something else: ${util.inspect(args[0])}`); + throw new Error(`Expected a buffer or string in 'end', but got something else: ${util.inspect(args[0])}`); } }; }