get the binary code from a process in node js in javascript

To get the binary code from a process in Node.js, you can use the spawn method of the child_process module to start a new process and then read the output of the process using a Buffer.

Here is an example code snippet:

index.tsx
const { spawn } = require('child_process');

const process = spawn('my-binary', ['arg1', 'arg2']);

let binary = Buffer.from([]);

process.stdout.on('data', (data) => {
  binary = Buffer.concat([binary, data]);
});

process.on('close', (code) => {
  console.log(`Binary code: ${binary.toString('hex')}`);
});
309 chars
14 lines

In this example, we first create a new process using the spawn method, passing the name of the binary file and any arguments it requires.

Then, we create an initial empty Buffer to hold the binary code and attach a listener to the stdout event of the process. Whenever new binary data is output by the process, the listener appends it to the existing Buffer using the Buffer.concat() method.

Finally, when the process exits, we log the binary code as a hexadecimal string using the Buffer.toString() method.

Note: This code assumes that the binary file outputs its binary code to stdout. If the binary outputs to a file, you would need to read the file contents using the fs module instead.

gistlibby LogSnag