Skip to content

Commit 76787e1

Browse files
committed
Implement Readable instead of Stream.
1 parent a3492e3 commit 76787e1

File tree

4 files changed

+71
-47
lines changed

4 files changed

+71
-47
lines changed

lib/jpegstream.js

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
* Module dependencies.
1111
*/
1212

13-
var Stream = require('stream').Stream;
13+
var Readable = require('stream').Readable;
14+
var util = require('util');
1415

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

3233
var JPEGStream = module.exports = function JPEGStream(canvas, options, sync) {
33-
var self = this
34-
, method = sync
34+
if (!(this instanceof JPEGStream)) {
35+
throw new TypeError("Class constructors cannot be invoked without 'new'");
36+
}
37+
38+
Readable.call(this);
39+
40+
var self = this;
41+
var method = sync
3542
? 'streamJPEGSync'
3643
: 'streamJPEG';
3744
this.options = options;
3845
this.sync = sync;
3946
this.canvas = canvas;
40-
this.readable = true;
47+
4148
// TODO: implement async
4249
if ('streamJPEG' == method) method = 'streamJPEGSync';
43-
process.nextTick(function(){
44-
canvas[method](options.bufsize, options.quality, options.progressive, function(err, chunk){
45-
if (err) {
46-
self.emit('error', err);
47-
self.readable = false;
48-
} else if (chunk) {
49-
self.emit('data', chunk);
50-
} else {
51-
self.emit('end');
52-
self.readable = false;
53-
}
54-
});
55-
});
56-
};
5750

58-
/**
59-
* Inherit from `EventEmitter`.
60-
*/
51+
// For now we're not controlling the c++ code's data emission, so we only
52+
// call canvas.streamPNGSync once and let it emit data at will.
53+
var hasStarted = false;
54+
55+
self._read = function () {
56+
if (!hasStarted) {
57+
hasStarted = true;
58+
process.nextTick(function(){
59+
canvas[method](options.bufsize, options.quality, options.progressive, function(err, chunk){
60+
if (err) {
61+
self.emit('error', err);
62+
} else if (chunk) {
63+
self.push(chunk);
64+
} else {
65+
self.push(null);
66+
}
67+
});
68+
});
69+
}
70+
};
71+
};
6172

62-
JPEGStream.prototype.__proto__ = Stream.prototype;
73+
util.inherits(JPEGStream, Readable);

lib/pngstream.js

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
* Module dependencies.
1111
*/
1212

13-
var Stream = require('stream').Stream;
13+
var Readable = require('stream').Readable;
14+
var util = require('util');
1415

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

3233
var PNGStream = module.exports = function PNGStream(canvas, sync) {
33-
var self = this
34-
, method = sync
34+
if (!(this instanceof PNGStream)) {
35+
throw new TypeError("Class constructors cannot be invoked without 'new'");
36+
}
37+
38+
Readable.call(this);
39+
40+
var self = this;
41+
var method = sync
3542
? 'streamPNGSync'
3643
: 'streamPNG';
3744
this.sync = sync;
3845
this.canvas = canvas;
39-
this.readable = true;
46+
4047
// TODO: implement async
41-
if ('streamPNG' == method) method = 'streamPNGSync';
42-
process.nextTick(function(){
43-
canvas[method](function(err, chunk, len){
44-
if (err) {
45-
self.emit('error', err);
46-
self.readable = false;
47-
} else if (len) {
48-
self.emit('data', chunk, len);
49-
} else {
50-
self.emit('end');
51-
self.readable = false;
52-
}
53-
});
54-
});
55-
};
48+
if ('streamPNG' === method) method = 'streamPNGSync';
5649

57-
/**
58-
* Inherit from `EventEmitter`.
59-
*/
50+
// For now we're not controlling the c++ code's data emission, so we only
51+
// call canvas.streamPNGSync once and let it emit data at will.
52+
var hasStarted = false;
53+
self._read = function () {
54+
if (!hasStarted) {
55+
hasStarted = true;
56+
process.nextTick(function(){
57+
canvas[method](function(err, chunk, len){
58+
if (err) {
59+
self.emit('error', err);
60+
} else if (len) {
61+
self.push(chunk);
62+
} else {
63+
self.push(null);
64+
}
65+
});
66+
});
67+
}
68+
};
69+
};
6070

61-
PNGStream.prototype.__proto__ = Stream.prototype;
71+
util.inherits(PNGStream, Readable);

src/Canvas.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ NAN_METHOD(Canvas::StreamPNGSync) {
429429
Nan::Null()
430430
, Nan::Null()
431431
, Nan::New<Uint32>(0) };
432-
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), (v8::Local<v8::Function>)closure.fn, 1, argv);
432+
Nan::MakeCallback(Nan::GetCurrentContext()->Global(), (v8::Local<v8::Function>)closure.fn, 3, argv);
433433
}
434434
return;
435435
}

test/canvas.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
var Canvas = require('../')
66
, assert = require('assert')
77
, parseFont = Canvas.Context2d.parseFont
8-
, fs = require('fs');
8+
, fs = require('fs')
9+
, Readable = require('stream').Readable;
910

1011
console.log();
1112
console.log(' canvas: %s', Canvas.version);
@@ -749,6 +750,7 @@ describe('Canvas', function () {
749750
it('Canvas#createSyncPNGStream()', function (done) {
750751
var canvas = new Canvas(20, 20);
751752
var stream = canvas.createSyncPNGStream();
753+
assert(stream instanceof Readable);
752754
var firstChunk = true;
753755
stream.on('data', function(chunk){
754756
if (firstChunk) {
@@ -767,6 +769,7 @@ describe('Canvas', function () {
767769
it('Canvas#jpegStream()', function (done) {
768770
var canvas = new Canvas(640, 480);
769771
var stream = canvas.jpegStream();
772+
assert(stream instanceof Readable);
770773
var firstChunk = true;
771774
var bytes = 0;
772775
stream.on('data', function(chunk){

0 commit comments

Comments
 (0)