* 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>
26 lines
831 B
JavaScript
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
|
|
}
|