What is exec-buffer?
The exec-buffer npm package is a utility for executing a buffer, typically used for processing images or other binary data. It provides a simple interface to run a binary and buffer the output, making it useful for tasks that involve manipulating binary data streams.
What are exec-buffer's main functionalities?
Execute a binary with input and output buffers
This feature allows you to execute a binary with input and output buffers. In this example, the input buffer is read from 'input.png', processed by the 'optipng' binary, and the output buffer is written to 'output.png'.
const execBuffer = require('exec-buffer');
const fs = require('fs');
const path = require('path');
const execa = require('execa');
(async () => {
const input = fs.readFileSync('input.png');
const output = await execBuffer({
input,
bin: 'optipng',
args: ['-out', execBuffer.output]
});
fs.writeFileSync('output.png', output);
})();
Other packages similar to exec-buffer
execa
Execa is a modern alternative to child_process for executing external binaries. It provides a more user-friendly API and better defaults. Unlike exec-buffer, execa does not specifically focus on buffering binary data but is more general-purpose for executing commands.
child_process
The child_process module is a core Node.js module that provides the ability to spawn child processes. It is more low-level compared to exec-buffer and requires more boilerplate code to handle buffers and streams.
buffer
The buffer module is a core Node.js module that provides a way to handle binary data directly. While it does not execute binaries, it is often used in conjunction with other modules like child_process to manage binary data streams.
exec-buffer 
Run a Buffer through a child process
Install
$ npm install --save exec-buffer
Usage
var ExecBuffer = require('exec-buffer');
var fs = require('fs');
var gifsicle = require('gifsicle').path;
var execBuffer = new ExecBuffer();
execBuffer
.use(gifsicle, ['-o', execBuffer.dest(), execBuffer.src()])
.run(fs.readFileSync('test.gif'), function (err, data) {
if (err) {
throw err;
}
console.log(data);
});
});
API
new ExecBuffer
Creates a new ExecBuffer
instance.
.use(bin, args)
Accepts a path to a binary and an Array
of arguments.
.src(path)
Set or get the temporary source path.
.dest(path)
Set or get the temporary destination path.
.run(buf, cb)
Run the Buffer
through the child process.
License
MIT © Kevin Mårtensson