-
Notifications
You must be signed in to change notification settings - Fork 132
/
index.js
58 lines (49 loc) · 1.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module.exports = require(__dirname + '/build/Release/imagemagick.node');
// Idea for stream conversions from
// http://codewinds.com/blog/2013-08-20-nodejs-transform-streams.html
var stream = require('stream');
var util = require('util');
function Convert(options) {
// allow use without new
if (!(this instanceof Convert)) {
return new Convert(options);
}
this._options = options;
this._bufs = [];
// init Transform
stream.Transform.call(this, options);
}
util.inherits(Convert, stream.Transform);
Convert.prototype._transform = function (chunk, enc, done) {
this._bufs.push(chunk);
done();
};
Convert.prototype._flush = function(done) {
this._options.srcData = Buffer.concat(this._bufs);
var self = this;
module.exports.convert(this._options,function(err,data){
if(err) {
return done(err);
}
self.push(data);
done();
});
}
module.exports.streams = { convert : Convert };
function promisify(func) {
return function(options) {
return new Promise((resolve, reject) => {
func(options, function(err, buff) {
if (err) {
return reject(err);
}
resolve(buff);
});
});
};
}
module.exports.promises = {
convert: promisify(module.exports.convert),
identify: promisify(module.exports.identify),
composite: promisify(module.exports.composite),
};