1
0
mirror of synced 2025-12-22 03:16:52 -05:00
Files
docs/lib/prefix-stream-write.js
James M. Greene 6e20ed7927 Implement app clustering (#17752)
* Install throng for easy cluster management
* Extract the Express app construction into its own file
* Switch server.js to use app clustering for deployed environments
* Worker count is based on the lesser of process.env.WEB_CONCURRENCY and the count of CPUs
* Reading clustered output is difficult, let's prefix the std{out,err} streams

Co-authored-by: Jason Etcovitch <jasonetco@github.com>
2021-03-19 20:07:46 +00:00

26 lines
831 B
JavaScript

module.exports = function prefixStreamWrite (writableStream, prefix) {
const oldWrite = writableStream.write
function newWrite (...args) {
const [chunk, encoding] = args
// Prepend the prefix if the chunk is either a string or a Buffer.
// Otherwise, just leave it alone to be safe.
if (typeof chunk === 'string') {
// Only prepend the prefix is the `encoding` is safe or not provided.
// If it's a function, it is third arg `callback` provided as optional second
if (!encoding || encoding === 'utf8' || typeof encoding === 'function') {
args[0] = prefix + chunk
}
} else if (Buffer.isBuffer(chunk)) {
args[0] = Buffer.concat([Buffer.from(prefix), chunk])
}
return oldWrite.apply(this, args)
}
writableStream.write = newWrite
return writableStream
}