Buffer returned by child_process.execSync is incomplete
Asked Answered
M

1

12

I have the following script which executes a shell command:

#!/usr/bin/env node

const { execSync } = require('child_process');

try {
    const data = execSync(
        'yarn licenses generate-disclaimer --prod',
        { encoding: 'utf8', maxBuffer: 50 * 1024 * 1024 }
    );

    console.log(data.length);

    return true;
} catch (error) {
    console.error(`Failed to generate disclaimer: ${error.message}`);
    return false;
}

data is a Buffer containing stdout of the child process. As I understand, the way to convert it to a string is to use the .toString() method, but in my case the string is incomplete. The command I am trying to execute is supposed to produce ~500 KB of data, but buffer.length is 43741 (that's ~43 KB).

The problem is probably that yarn licenses output contains some special characters which result in the buffer being incomplete. If I replace the command with printf "%0.s-" {1..500000}, the buffer is complete.

I am using the latest node version (8.7.0).

Any thoughts/suggestions?


EDIT: Appending | tr "\0" "\n" to the command increases the buffer size to ~122 KB, so @YaroslavAdmin is definitely looking in the right direction. The result is still incomplete though. How do I make sure all special characters are escaped?

Marjoram answered 18/10, 2017 at 20:18 Comment(1)
I suspect that command prints some special character, which causes Node to strip remaining output. \0 maybe.Metasomatism
F
22

Add .toString() after execSync.

const data = execSync(
    'yarn licenses generate-disclaimer --prod',
    { encoding: 'utf8', maxBuffer: 50 * 1024 * 1024 }
).toString(); // <<<<<<<<<<<<
Ferdinandferdinanda answered 18/7, 2018 at 17:42 Comment(3)
Why use toString() when encoding is already specified ? @FerdinandferdinandaSkaggs
Because execSync returns a buffer, not a string.Bharat
In Node 16.2.0, if I provide encoding, execSync returns a string, so .toString is not needed.Duncan

© 2022 - 2024 — McMap. All rights reserved.