Skip to content

Commit

Permalink
Implement Readable instead of Stream.
Browse files Browse the repository at this point in the history
  • Loading branch information
zbjornson committed May 10, 2016
1 parent a3492e3 commit 4cacbe8
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 51 deletions.
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
language: node_js
node_js:
- "0.8"
- "0.10"
- "0.12"
- "iojs-v1.8.4"
- "iojs-v2.5.0"
- "iojs-v3.3.0"
- "4"
- "5"
matrix:
allow_failures:
- node_js: "5"
- "6"
addons:
apt:
sources:
Expand Down
55 changes: 33 additions & 22 deletions lib/jpegstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* Module dependencies.
*/

var Stream = require('stream').Stream;
var Readable = require('stream').Readable;
var util = require('util');

/**
* Initialize a `JPEGStream` with the given `canvas`.
Expand All @@ -30,33 +31,43 @@ var Stream = require('stream').Stream;
*/

var JPEGStream = module.exports = function JPEGStream(canvas, options, sync) {
var self = this
, method = sync
if (!(this instanceof JPEGStream)) {
throw new TypeError("Class constructors cannot be invoked without 'new'");
}

Readable.call(this);

var self = this;
var method = sync
? 'streamJPEGSync'
: 'streamJPEG';
this.options = options;
this.sync = sync;
this.canvas = canvas;
this.readable = true;

// TODO: implement async
if ('streamJPEG' == method) method = 'streamJPEGSync';
process.nextTick(function(){
canvas[method](options.bufsize, options.quality, options.progressive, function(err, chunk){
if (err) {
self.emit('error', err);
self.readable = false;
} else if (chunk) {
self.emit('data', chunk);
} else {
self.emit('end');
self.readable = false;
}
});
});
};

/**
* Inherit from `EventEmitter`.
*/
// For now we're not controlling the c++ code's data emission, so we only
// call canvas.streamPNGSync once and let it emit data at will.
var hasStarted = false;

self._read = function () {
if (!hasStarted) {
hasStarted = true;
process.nextTick(function(){
canvas[method](options.bufsize, options.quality, options.progressive, function(err, chunk){
if (err) {
self.emit('error', err);
} else if (chunk) {
self.push(chunk);
} else {
self.push(null);
}
});
});
}
};
};

JPEGStream.prototype.__proto__ = Stream.prototype;
util.inherits(JPEGStream, Readable);
56 changes: 33 additions & 23 deletions lib/pngstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* Module dependencies.
*/

var Stream = require('stream').Stream;
var Readable = require('stream').Readable;
var util = require('util');

/**
* Initialize a `PNGStream` with the given `canvas`.
Expand All @@ -30,32 +31,41 @@ var Stream = require('stream').Stream;
*/

var PNGStream = module.exports = function PNGStream(canvas, sync) {
var self = this
, method = sync
if (!(this instanceof PNGStream)) {
throw new TypeError("Class constructors cannot be invoked without 'new'");
}

Readable.call(this);

var self = this;
var method = sync
? 'streamPNGSync'
: 'streamPNG';
this.sync = sync;
this.canvas = canvas;
this.readable = true;

// TODO: implement async
if ('streamPNG' == method) method = 'streamPNGSync';
process.nextTick(function(){
canvas[method](function(err, chunk, len){
if (err) {
self.emit('error', err);
self.readable = false;
} else if (len) {
self.emit('data', chunk, len);
} else {
self.emit('end');
self.readable = false;
}
});
});
};
if ('streamPNG' === method) method = 'streamPNGSync';

/**
* Inherit from `EventEmitter`.
*/
// For now we're not controlling the c++ code's data emission, so we only
// call canvas.streamPNGSync once and let it emit data at will.
var hasStarted = false;
self._read = function () {
if (!hasStarted) {
hasStarted = true;
process.nextTick(function(){
canvas[method](function(err, chunk, len){
if (err) {
self.emit('error', err);
} else if (len) {
self.push(chunk);
} else {
self.push(null);
}
});
});
}
};
};

PNGStream.prototype.__proto__ = Stream.prototype;
util.inherits(PNGStream, Readable);
2 changes: 1 addition & 1 deletion src/Canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ NAN_METHOD(Canvas::StreamPNGSync) {
Nan::Null()
, Nan::Null()
, Nan::New<Uint32>(0) };
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), (v8::Local<v8::Function>)closure.fn, 1, argv);
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), (v8::Local<v8::Function>)closure.fn, 3, argv);
}
return;
}
Expand Down
5 changes: 4 additions & 1 deletion test/canvas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
var Canvas = require('../')
, assert = require('assert')
, parseFont = Canvas.Context2d.parseFont
, fs = require('fs');
, fs = require('fs')
, Readable = require('stream').Readable;

console.log();
console.log(' canvas: %s', Canvas.version);
Expand Down Expand Up @@ -749,6 +750,7 @@ describe('Canvas', function () {
it('Canvas#createSyncPNGStream()', function (done) {
var canvas = new Canvas(20, 20);
var stream = canvas.createSyncPNGStream();
assert(stream instanceof Readable);
var firstChunk = true;
stream.on('data', function(chunk){
if (firstChunk) {
Expand All @@ -767,6 +769,7 @@ describe('Canvas', function () {
it('Canvas#jpegStream()', function (done) {
var canvas = new Canvas(640, 480);
var stream = canvas.jpegStream();
assert(stream instanceof Readable);
var firstChunk = true;
var bytes = 0;
stream.on('data', function(chunk){
Expand Down

0 comments on commit 4cacbe8

Please sign in to comment.