pngstream.js 940 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict'
  2. /*!
  3. * Canvas - PNGStream
  4. * Copyright (c) 2010 LearnBoost <tj@learnboost.com>
  5. * MIT Licensed
  6. */
  7. const { Readable } = require('stream')
  8. function noop () {}
  9. class PNGStream extends Readable {
  10. constructor (canvas, options) {
  11. super()
  12. if (options &&
  13. options.palette instanceof Uint8ClampedArray &&
  14. options.palette.length % 4 !== 0) {
  15. throw new Error('Palette length must be a multiple of 4.')
  16. }
  17. this.canvas = canvas
  18. this.options = options || {}
  19. }
  20. _read () {
  21. // For now we're not controlling the c++ code's data emission, so we only
  22. // call canvas.streamPNGSync once and let it emit data at will.
  23. this._read = noop
  24. this.canvas.streamPNGSync((err, chunk, len) => {
  25. if (err) {
  26. this.emit('error', err)
  27. } else if (len) {
  28. this.push(chunk)
  29. } else {
  30. this.push(null)
  31. }
  32. }, this.options)
  33. }
  34. }
  35. module.exports = PNGStream