├── .editorconfig ├── .gitattributes ├── .gitignore ├── .npmrc ├── .travis.yml ├── fixture.gif ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_size = 2 12 | indent_style = space 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '8' 5 | - '6' 6 | - '4' 7 | -------------------------------------------------------------------------------- /fixture.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kevva/exec-buffer/dba833f9d66fc5f5dad37818def25cc297f6b14b/fixture.gif -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const fs = require('fs'); 3 | const execa = require('execa'); 4 | const pFinally = require('p-finally'); 5 | const pify = require('pify'); 6 | const rimraf = require('rimraf'); 7 | const tempfile = require('tempfile'); 8 | 9 | const fsP = pify(fs); 10 | const rmP = pify(rimraf); 11 | const input = Symbol('inputPath'); 12 | const output = Symbol('outputPath'); 13 | 14 | module.exports = opts => { 15 | opts = Object.assign({}, opts); 16 | 17 | if (!Buffer.isBuffer(opts.input)) { 18 | return Promise.reject(new Error('Input is required')); 19 | } 20 | 21 | if (typeof opts.bin !== 'string') { 22 | return Promise.reject(new Error('Binary is required')); 23 | } 24 | 25 | if (!Array.isArray(opts.args)) { 26 | return Promise.reject(new Error('Arguments are required')); 27 | } 28 | 29 | const inputPath = opts.inputPath || tempfile(); 30 | const outputPath = opts.outputPath || tempfile(); 31 | 32 | opts.args = opts.args.map(x => x === input ? inputPath : x === output ? outputPath : x); 33 | 34 | const promise = fsP.writeFile(inputPath, opts.input) 35 | .then(() => execa(opts.bin, opts.args)) 36 | .then(() => fsP.readFile(outputPath)); 37 | 38 | return pFinally(promise, () => Promise.all([ 39 | rmP(inputPath), 40 | rmP(outputPath) 41 | ])); 42 | }; 43 | 44 | module.exports.input = input; 45 | module.exports.output = output; 46 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Kevin Mårtensson (github.com/kevva) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "exec-buffer", 3 | "version": "3.2.0", 4 | "description": "Run a buffer through a child process", 5 | "license": "MIT", 6 | "repository": "kevva/exec-buffer", 7 | "author": { 8 | "name": "Kevin Mårtensson", 9 | "email": "kevinmartensson@gmail.com", 10 | "url": "https://github.com/kevva" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "test": "xo && ava" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "buffer", 23 | "exec" 24 | ], 25 | "dependencies": { 26 | "execa": "^0.7.0", 27 | "p-finally": "^1.0.0", 28 | "pify": "^3.0.0", 29 | "rimraf": "^2.5.4", 30 | "tempfile": "^2.0.0" 31 | }, 32 | "devDependencies": { 33 | "ava": "*", 34 | "gifsicle": "^3.0.4", 35 | "is-gif": "^1.0.0", 36 | "path-exists": "^3.0.0", 37 | "xo": "*" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # exec-buffer [![Build Status](http://img.shields.io/travis/kevva/exec-buffer.svg?style=flat)](https://travis-ci.org/kevva/exec-buffer) 2 | 3 | > Run a Buffer through a child process 4 | 5 | 6 | ## Install 7 | 8 | ``` 9 | $ npm install exec-buffer 10 | ``` 11 | 12 | 13 | ## Usage 14 | 15 | ```js 16 | const fs = require('fs'); 17 | const execBuffer = require('exec-buffer'); 18 | const gifsicle = require('gifsicle').path; 19 | 20 | execBuffer({ 21 | input: fs.readFileSync('test.gif'), 22 | bin: gifsicle, 23 | args: ['-o', execBuffer.output, execBuffer.input] 24 | }).then(data => { 25 | console.log(data); 26 | //=> 27 | }); 28 | ``` 29 | 30 | 31 | ## API 32 | 33 | ### execBuffer(options) 34 | 35 | #### options 36 | 37 | Type: `Object` 38 | 39 | ##### input 40 | 41 | Type: `Buffer` 42 | 43 | The `Buffer` to be ran through the child process. 44 | 45 | ##### bin 46 | 47 | Type: `string` 48 | 49 | Path to the binary. 50 | 51 | ##### args 52 | 53 | Type: `Array` 54 | 55 | Arguments to run the binary with. 56 | 57 | ##### inputPath 58 | 59 | Type: `string`
60 | Default: `tempfile()` 61 | 62 | Where `input` will be written to. In most cases you don't need to set this. 63 | 64 | ##### outputPath 65 | 66 | Type: `string`
67 | Default: `tempfile()` 68 | 69 | Where output file will be written to. In most cases you don't need to set this. 70 | 71 | ### execBuffer.input 72 | 73 | Returns a temporary path to where the input file will be written. 74 | 75 | ### execBuffer.output 76 | 77 | Returns a temporary path to where the output file will be written. 78 | 79 | 80 | ## License 81 | 82 | MIT © [Kevin Mårtensson](https://github.com/kevva) 83 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import fs from 'fs'; 2 | import path from 'path'; 3 | import gifsicle from 'gifsicle'; 4 | import isGif from 'is-gif'; 5 | import pathExists from 'path-exists'; 6 | import pify from 'pify'; 7 | import test from 'ava'; 8 | import m from './'; 9 | 10 | test('set temporary directories', t => { 11 | const {input, output} = m; 12 | t.truthy(input); 13 | t.truthy(output); 14 | }); 15 | 16 | test('return a optimized buffer', async t => { 17 | const buf = await pify(fs.readFile)(path.join(__dirname, 'fixture.gif')); 18 | const data = await m({ 19 | input: buf, 20 | bin: gifsicle, 21 | args: ['-o', m.output, m.input] 22 | }); 23 | 24 | t.true(data.length < buf.length); 25 | t.true(isGif(data)); 26 | }); 27 | 28 | test('remove temporary files', async t => { 29 | const buf = await pify(fs.readFile)(path.join(__dirname, 'fixture.gif')); 30 | const err = await t.throws(m({ 31 | input: buf, 32 | bin: 'foobarunicorn', 33 | args: [m.output, m.input] 34 | })); 35 | 36 | t.is(err.code, 'ENOENT'); 37 | t.false(await pathExists(err.spawnargs[0])); 38 | t.false(await pathExists(err.spawnargs[1])); 39 | }); 40 | --------------------------------------------------------------------------------