set encoding when reading from streams

using .toString() works most of the time but can lead to utf8 characters being
broken across chunk boundaries.

https://nodejs.org/api/stream.html#stream_readable_setencoding_encoding
This commit is contained in:
Brian Gough
2020-05-07 10:30:14 +01:00
parent 7254a025ae
commit 5b5fd2f5df
6 changed files with 7 additions and 4 deletions

View File

@@ -334,7 +334,7 @@ module.exports = CompileManager = {
proc.on('error', callback)
let stderr = ''
proc.stderr.on('data', chunk => (stderr += chunk.toString()))
proc.stderr.setEncoding('utf8').on('data', chunk => (stderr += chunk))
return proc.on('close', function(code) {
if (code === 0) {

View File

@@ -46,7 +46,7 @@ module.exports = CommandRunner = {
const proc = spawn(command[0], command.slice(1), { cwd: directory, env })
let stdout = ''
proc.stdout.on('data', data => (stdout += data))
proc.stdout.setEncoding('utf8').on('data', data => (stdout += data))
proc.on('error', function(err) {
logger.err(

View File

@@ -87,7 +87,7 @@ module.exports = OutputFileFinder = {
const proc = spawn('find', args)
let stdout = ''
proc.stdout.on('data', chunk => (stdout += chunk.toString()))
proc.stdout.setEncoding('utf8').on('data', chunk => (stdout += chunk))
proc.on('error', callback)
return proc.on('close', function(code) {
if (code !== 0) {

View File

@@ -78,7 +78,7 @@ module.exports = OutputFileOptimiser = {
const timer = new Metrics.Timer('qpdf')
const proc = spawn('qpdf', args)
let stdout = ''
proc.stdout.on('data', chunk => (stdout += chunk.toString()))
proc.stdout.setEncoding('utf8').on('data', chunk => (stdout += chunk))
callback = _.once(callback) // avoid double call back for error and close event
proc.on('error', function(err) {
logger.warn({ err, args }, 'qpdf failed')