jpegstream.js 874 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict'
  2. /*!
  3. * Canvas - JPEGStream
  4. * Copyright (c) 2010 LearnBoost <tj@learnboost.com>
  5. * MIT Licensed
  6. */
  7. const { Readable } = require('stream')
  8. function noop () {}
  9. class JPEGStream extends Readable {
  10. constructor (canvas, options) {
  11. super()
  12. if (canvas.streamJPEGSync === undefined) {
  13. throw new Error('node-canvas was built without JPEG support.')
  14. }
  15. this.options = options
  16. this.canvas = canvas
  17. }
  18. _read () {
  19. // For now we're not controlling the c++ code's data emission, so we only
  20. // call canvas.streamJPEGSync once and let it emit data at will.
  21. this._read = noop
  22. this.canvas.streamJPEGSync(this.options, (err, chunk) => {
  23. if (err) {
  24. this.emit('error', err)
  25. } else if (chunk) {
  26. this.push(chunk)
  27. } else {
  28. this.push(null)
  29. }
  30. })
  31. }
  32. };
  33. module.exports = JPEGStream