Skip to content

Commit

Permalink
Merge pull request #980 from zbjornson/no-sync-streams
Browse files Browse the repository at this point in the history
Remove sync streams
  • Loading branch information
LinusU authored Sep 2, 2017
2 parents 3370e64 + bc53059 commit 0019e3c
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 82 deletions.
2 changes: 0 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ stream.on('end', function(){
});
```

Currently _only_ sync streaming is supported, however we plan on supporting async streaming as well (of course :) ). Until then the `Canvas#toBuffer(callback)` alternative is async utilizing `eio_custom()`.

To encode indexed PNGs from canvases with `pixelFormat: 'A8'` or `'A1'`, provide an options object:

```js
Expand Down
42 changes: 0 additions & 42 deletions lib/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,6 @@ Canvas.prototype.createPNGStream = function(options){
return new PNGStream(this, false, options);
};

/**
* Create a synchronous `PNGStream` for `this` canvas.
*
* @param {Object} options
* @param {Uint8ClampedArray} options.palette Provide for indexed PNG encoding.
* entries should be R-G-B-A values.
* @param {Number} options.backgroundIndex Optional index of background color
* for indexed PNGs. Defaults to 0.
* @return {PNGStream}
* @api public
*/

Canvas.prototype.syncPNGStream =
Canvas.prototype.createSyncPNGStream = function(options){
return new PNGStream(this, true, options);
};

/**
* Create a `PDFStream` for `this` canvas.
*
Expand All @@ -93,18 +76,6 @@ Canvas.prototype.createPDFStream = function(){
return new PDFStream(this);
};

/**
* Create a synchronous `PDFStream` for `this` canvas.
*
* @return {PDFStream}
* @api public
*/

Canvas.prototype.syncPDFStream =
Canvas.prototype.createSyncPDFStream = function(){
return new PDFStream(this, true);
};

/**
* Create a `JPEGStream` for `this` canvas.
*
Expand All @@ -115,19 +86,6 @@ Canvas.prototype.createSyncPDFStream = function(){

Canvas.prototype.jpegStream =
Canvas.prototype.createJPEGStream = function(options){
return this.createSyncJPEGStream(options);
};

/**
* Create a synchronous `JPEGStream` for `this` canvas.
*
* @param {Object} options
* @return {JPEGStream}
* @api public
*/

Canvas.prototype.syncJPEGStream =
Canvas.prototype.createSyncJPEGStream = function(options){
options = options || {};
// Don't allow the buffer size to exceed the size of the canvas (#674)
var maxBufSize = this.width * this.height * 4;
Expand Down
13 changes: 2 additions & 11 deletions lib/jpegstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,19 @@ var util = require('util');
* stream.pipe(out);
*
* @param {Canvas} canvas
* @param {Boolean} sync
* @api public
*/

var JPEGStream = module.exports = function JPEGStream(canvas, options, sync) {
var JPEGStream = module.exports = function JPEGStream(canvas, options) {
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;

// TODO: implement async
if ('streamJPEG' == method) method = 'streamJPEGSync';
this.method = method;
};

util.inherits(JPEGStream, Readable);
Expand All @@ -63,7 +54,7 @@ JPEGStream.prototype._read = function _read() {
var bufsize = this.options.bufsize;
var quality = this.options.quality;
var progressive = this.options.progressive;
self.canvas[method](bufsize, quality, progressive, function(err, chunk){
self.canvas.streamJPEGSync(bufsize, quality, progressive, function(err, chunk){
if (err) {
self.emit('error', err);
} else if (chunk) {
Expand Down
14 changes: 2 additions & 12 deletions lib/pdfstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,17 @@ var util = require('util');
* stream.pipe(out);
*
* @param {Canvas} canvas
* @param {Boolean} sync
* @api public
*/

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

Readable.call(this);

var self = this
, method = sync
? 'streamPDFSync'
: 'streamPDF';
this.sync = sync;
this.canvas = canvas;

// TODO: implement async
if ('streamPDF' == method) method = 'streamPDFSync';
this.method = method;
};

util.inherits(PDFStream, Readable);
Expand All @@ -56,7 +46,7 @@ PDFStream.prototype._read = function _read() {
// call canvas.streamPDFSync once and let it emit data at will.
this._read = noop;
var self = this;
self.canvas[self.method](function(err, chunk, len){
self.canvas.streamPDFSync(function(err, chunk, len){
if (err) {
self.emit('error', err);
} else if (len) {
Expand Down
13 changes: 2 additions & 11 deletions lib/pngstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ var util = require('util');
* stream.pipe(out);
*
* @param {Canvas} canvas
* @param {Boolean} sync
* @param {Object} options
* @param {Uint8ClampedArray} options.palette Provide for indexed PNG encoding.
* entries should be R-G-B-A values.
Expand All @@ -35,24 +34,16 @@ var util = require('util');
* @api public
*/

var PNGStream = module.exports = function PNGStream(canvas, sync, options) {
var PNGStream = module.exports = function PNGStream(canvas, options) {
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.options = options || {};

// TODO: implement async
if ('streamPNG' === method) method = 'streamPNGSync';
this.method = method;
};

util.inherits(PNGStream, Readable);
Expand All @@ -64,7 +55,7 @@ PNGStream.prototype._read = function _read() {
// call canvas.streamPNGSync once and let it emit data at will.
this._read = noop;
var self = this;
self.canvas[self.method](function(err, chunk, len){
self.canvas.streamPNGSync(function(err, chunk, len){
if (err) {
self.emit('error', err);
} else if (len) {
Expand Down
8 changes: 4 additions & 4 deletions test/canvas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1152,9 +1152,9 @@ describe('Canvas', function () {
});
});

it('Canvas#createSyncPNGStream()', function (done) {
it('Canvas#createPNGStream()', function (done) {
var canvas = createCanvas(20, 20);
var stream = canvas.createSyncPNGStream();
var stream = canvas.createPNGStream();
assert(stream instanceof Readable);
var firstChunk = true;
stream.on('data', function(chunk){
Expand All @@ -1171,9 +1171,9 @@ describe('Canvas', function () {
});
});

it('Canvas#createSyncPDFStream()', function (done) {
it('Canvas#createPDFStream()', function (done) {
var canvas = createCanvas(20, 20, 'pdf');
var stream = canvas.createSyncPDFStream();
var stream = canvas.createPDFStream();
assert(stream instanceof Readable);
var firstChunk = true;
stream.on('data', function (chunk) {
Expand Down

0 comments on commit 0019e3c

Please sign in to comment.