Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffer returned by child_process.execSync is incomplete

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?

like image 901
Andrii Chernenko Avatar asked Oct 18 '17 20:10

Andrii Chernenko


People also ask

What is Child_process spawn?

child_process.exec() : spawns a shell and runs a command within that shell, passing the stdout and stderr to a callback function when complete.

What is a Child_process module in node JS?

Usually, Node. js allows single-threaded, non-blocking performance but running a single thread in a CPU cannot handle increasing workload hence the child_process module can be used to spawn child processes. The child processes communicate with each other using a built-in messaging system.

How do you spawn in Javascript?

The spawn function launches a command in a new process and we can use it to pass that command any arguments. For example, here's code to spawn a new process that will execute the pwd command. const { spawn } = require('child_process'); const child = spawn('pwd');

How do I run an exec in node JS?

The exec() function in Node. js creates a new shell process and executes a command in that shell. The output of the command is kept in a buffer in memory, which you can accept via a callback function passed into exec() .


1 Answers

Add .toString() after execSync.

const data = execSync(
    'yarn licenses generate-disclaimer --prod',
    { encoding: 'utf8', maxBuffer: 50 * 1024 * 1024 }
).toString(); // <<<<<<<<<<<<
like image 93
junvar Avatar answered Sep 21 '22 14:09

junvar