Skip to content

Remove sync streams #980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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