exec-buffer
Advanced tools
Comparing version
155
index.js
'use strict'; | ||
const fs = require('fs'); | ||
const execa = require('execa'); | ||
const pify = require('pify'); | ||
const tempfile = require('tempfile'); | ||
const fsP = pify(fs); | ||
const input = Symbol('inputPath'); | ||
const output = Symbol('outputPath'); | ||
var execFile = require('child_process').execFile; | ||
var fs = require('fs'); | ||
var rm = require('rimraf'); | ||
var tempfile = require('tempfile'); | ||
module.exports = opts => { | ||
opts = Object.assign({}, opts); | ||
/** | ||
* Run a buffer through a child process | ||
* | ||
* @api public | ||
*/ | ||
function ExecBuffer() { | ||
if (!(this instanceof ExecBuffer)) { | ||
return new ExecBuffer(); | ||
if (!Buffer.isBuffer(opts.input)) { | ||
return Promise.reject(new Error('Input is required')); | ||
} | ||
this.src(tempfile()); | ||
this.dest(tempfile()); | ||
} | ||
/** | ||
* Add a child process to use | ||
* | ||
* @param {String} bin | ||
* @param {Array} args | ||
* @api public | ||
*/ | ||
ExecBuffer.prototype.use = function (bin, args) { | ||
if (!arguments.length) { | ||
return this._use; | ||
if (typeof opts.bin !== 'string') { | ||
return Promise.reject(new Error('Binary is required')); | ||
} | ||
this._use = { | ||
args: args, | ||
bin: bin | ||
}; | ||
return this; | ||
}; | ||
/** | ||
* Get or set the temp source path | ||
* | ||
* @param {String} path | ||
* @api public | ||
*/ | ||
ExecBuffer.prototype.src = function (path) { | ||
if (!arguments.length) { | ||
return this._src; | ||
if (!Array.isArray(opts.args)) { | ||
return Promise.reject(new Error('Arguments are required')); | ||
} | ||
this._src = path; | ||
return this; | ||
}; | ||
const inputPath = tempfile(); | ||
const outputPath = tempfile(); | ||
/** | ||
* Get or set the temp destination path | ||
* | ||
* @param {String} path | ||
* @api public | ||
*/ | ||
opts.args = opts.args.map(x => x === input ? inputPath : x === output ? outputPath : x); | ||
ExecBuffer.prototype.dest = function (path) { | ||
if (!arguments.length) { | ||
return this._dest; | ||
} | ||
this._dest = path; | ||
return this; | ||
return fsP.writeFile(inputPath, opts.input) | ||
.then(() => execa(opts.bin, opts.args)) | ||
.then(() => fsP.readFile(outputPath)); | ||
}; | ||
/** | ||
* Run buffer through the child process | ||
* | ||
* @param {Buffer} buf | ||
* @param {Function} cb | ||
* @api public | ||
*/ | ||
ExecBuffer.prototype.run = function (buf, cb) { | ||
fs.writeFile(this.src(), buf, function (err) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
execFile(this.use().bin, this.use().args, function (err, stdout, stderr) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
fs.readFile(this.dest(), function (err, data) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
this.cleanup(function (err) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
cb(null, data, stderr); | ||
}); | ||
}.bind(this)); | ||
}.bind(this)); | ||
}.bind(this)); | ||
}; | ||
/** | ||
* Cleanup temporary files | ||
* | ||
* @param {Function} cb | ||
* @api private | ||
*/ | ||
ExecBuffer.prototype.cleanup = function (cb) { | ||
rm(this.src(), function (err) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
rm(this.dest(), function (err) { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
cb(); | ||
}); | ||
}.bind(this)); | ||
}; | ||
/** | ||
* Module exports | ||
*/ | ||
module.exports = ExecBuffer; | ||
module.exports.input = input; | ||
module.exports.output = output; |
{ | ||
"name": "exec-buffer", | ||
"version": "2.0.1", | ||
"version": "3.0.0", | ||
"description": "Run a buffer through a child process", | ||
@@ -13,6 +13,6 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "node test/test.js" | ||
"test": "xo && ava" | ||
}, | ||
@@ -27,9 +27,15 @@ "files": [ | ||
"dependencies": { | ||
"rimraf": "^2.2.6", | ||
"execa": "^0.3.0", | ||
"pify": "^2.3.0", | ||
"tempfile": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.0.4", | ||
"gifsicle": "^2.0.0" | ||
"ava": "*", | ||
"gifsicle": "^2.0.0", | ||
"is-gif": "^1.0.0", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
} |
# exec-buffer [](https://travis-ci.org/kevva/exec-buffer) | ||
> Run a Buffer through a child process | ||
> Run a buffer through a child process | ||
## Install | ||
```ba | ||
``` | ||
$ npm install --save exec-buffer | ||
``` | ||
## Usage | ||
```js | ||
var ExecBuffer = require('exec-buffer'); | ||
var fs = require('fs'); | ||
var gifsicle = require('gifsicle').path; | ||
const fs = require('fs'); | ||
const execBuffer = require('exec-buffer'); | ||
const gifsicle = require('gifsicle').path; | ||
var execBuffer = new ExecBuffer(); | ||
execBuffer.use(gifsicle, ['-o', execBuffer.dest(), execBuffer.src()]); | ||
execBuffer.run(fs.readFileSync('test.gif'), function (err, data) { | ||
if (err) { | ||
throw err; | ||
} | ||
console.log(data); | ||
//=> <Buffer 47 49 46 38 37 61 ...> | ||
}); | ||
execBuffer({ | ||
input: fs.readFileSync('test.gif'), | ||
bin: gifsicle, | ||
args: ['-o', execBuffer.output, execBuffer.input] | ||
}).then(data => { | ||
console.log(data); | ||
//=> <Buffer 47 49 46 38 37 61 ...> | ||
}); | ||
``` | ||
## API | ||
### new ExecBuffer() | ||
### execBuffer(options) | ||
Creates a new `ExecBuffer` instance. | ||
#### options | ||
### .use(bin, args) | ||
##### input | ||
#### bin | ||
Type: `buffer` | ||
Type: `String` | ||
The `buffer` to be ran through the child process. | ||
##### bin | ||
Type: `string` | ||
Path to the binary. | ||
#### args | ||
##### args | ||
Type: `Array` | ||
Type: `array` | ||
Arguments to run the binary with. | ||
### .src(path) | ||
### execBuffer.input | ||
#### path | ||
Returns a temporary path to where the input file will be written. | ||
Type: `String` | ||
### execBuffer.output | ||
Set or get the temporary source path. | ||
Returns a temporary path to where the output file will be written. | ||
### .dest(path) | ||
#### path | ||
Type: `String` | ||
Set or get the temporary destination path. | ||
### .run(buf, cb) | ||
#### buf | ||
Type: `Buffer` | ||
The `Buffer` to be ran through the child process. | ||
#### cb(err, data, stderr) | ||
Type: `Function` | ||
Returns a `Buffer` with the new `data` and any `stderr` output. | ||
## License | ||
MIT © [Kevin Mårtensson](https://github.com/kevva) |
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
0
-100%3790
-25.63%3
50%4
100%28
-76.47%67
-21.18%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed