Also allow a string in monkeypatched `end` calls

master
Sven Slootweg 3 years ago
parent 4fffcf8c38
commit 9f1d2cf276

@ -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])}`);
}
};
}

Loading…
Cancel
Save