UNPKG

cwebp

Version:

node.js wrapper for cwebp and dwebp binaries from WebP image processing utility

69 lines (63 loc) 2.07 kB
// Generated by CoffeeScript 1.12.7 var Buffer, PassThrough, Stream, bindCallback, ref, streamToBuffer; streamToBuffer = require('raw-body'); Buffer = require('buffer').Buffer; ref = require('stream'), Stream = ref.Stream, PassThrough = ref.PassThrough; bindCallback = function(promise, next) { if (typeof next === 'function') { promise.then(function(result) { return process.nextTick(next, null, result); }, function(err) { return process.nextTick(next, err); }); } return promise; }; module.exports = { _write: function(source, outname) { var args, res, stdin, stdout; outname || (outname = '-'); stdin = typeof source !== 'string'; stdout = outname === '-'; args = [].concat(this.args(), ['-o', outname, '--', (stdin ? '-' : source)]); if (!stdin) { return this._spawn(args, stdin, stdout); } else if (Buffer.isBuffer(source)) { res = this._spawn(args, stdin, stdout); res.stdin.end(source); return res; } else if (source instanceof Stream) { res = this._spawn(args, stdin, stdout); source.pipe(res.stdin); return res; } else { return { promise: Promise.reject(new Error('Mailformed source')) }; } }, write: function(outname, next) { var promise; promise = outname ? (this._write(this.source, outname)).promise : Promise.reject(new Error('outname in not specified')); return bindCallback(promise, next); }, toBuffer: function(next) { return bindCallback(streamToBuffer(this.stream()), next); }, stream: function() { var outstream, promise, res; outstream = new PassThrough(); res = this._write(this.source, '-'); promise = res.stdout ? (res.stdout.pipe(outstream, { end: false }), res.promise) : res.promise.then(function() { return Promise.reject(new Error('Failed to pipe stdout')); }); promise.then(function() { return outstream.end(); })["catch"](function(err) { return outstream.emit('error', err); }); return outstream; } };