parser-async.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. "use strict";
  2. let util = require("util");
  3. let zlib = require("zlib");
  4. let ChunkStream = require("./chunkstream");
  5. let FilterAsync = require("./filter-parse-async");
  6. let Parser = require("./parser");
  7. let bitmapper = require("./bitmapper");
  8. let formatNormaliser = require("./format-normaliser");
  9. let ParserAsync = (module.exports = function (options) {
  10. ChunkStream.call(this);
  11. this._parser = new Parser(options, {
  12. read: this.read.bind(this),
  13. error: this._handleError.bind(this),
  14. metadata: this._handleMetaData.bind(this),
  15. gamma: this.emit.bind(this, "gamma"),
  16. palette: this._handlePalette.bind(this),
  17. transColor: this._handleTransColor.bind(this),
  18. finished: this._finished.bind(this),
  19. inflateData: this._inflateData.bind(this),
  20. simpleTransparency: this._simpleTransparency.bind(this),
  21. headersFinished: this._headersFinished.bind(this),
  22. });
  23. this._options = options;
  24. this.writable = true;
  25. this._parser.start();
  26. });
  27. util.inherits(ParserAsync, ChunkStream);
  28. ParserAsync.prototype._handleError = function (err) {
  29. this.emit("error", err);
  30. this.writable = false;
  31. this.destroy();
  32. if (this._inflate && this._inflate.destroy) {
  33. this._inflate.destroy();
  34. }
  35. if (this._filter) {
  36. this._filter.destroy();
  37. // For backward compatibility with Node 7 and below.
  38. // Suppress errors due to _inflate calling write() even after
  39. // it's destroy()'ed.
  40. this._filter.on("error", function () {});
  41. }
  42. this.errord = true;
  43. };
  44. ParserAsync.prototype._inflateData = function (data) {
  45. if (!this._inflate) {
  46. if (this._bitmapInfo.interlace) {
  47. this._inflate = zlib.createInflate();
  48. this._inflate.on("error", this.emit.bind(this, "error"));
  49. this._filter.on("complete", this._complete.bind(this));
  50. this._inflate.pipe(this._filter);
  51. } else {
  52. let rowSize =
  53. ((this._bitmapInfo.width *
  54. this._bitmapInfo.bpp *
  55. this._bitmapInfo.depth +
  56. 7) >>
  57. 3) +
  58. 1;
  59. let imageSize = rowSize * this._bitmapInfo.height;
  60. let chunkSize = Math.max(imageSize, zlib.Z_MIN_CHUNK);
  61. this._inflate = zlib.createInflate({ chunkSize: chunkSize });
  62. let leftToInflate = imageSize;
  63. let emitError = this.emit.bind(this, "error");
  64. this._inflate.on("error", function (err) {
  65. if (!leftToInflate) {
  66. return;
  67. }
  68. emitError(err);
  69. });
  70. this._filter.on("complete", this._complete.bind(this));
  71. let filterWrite = this._filter.write.bind(this._filter);
  72. this._inflate.on("data", function (chunk) {
  73. if (!leftToInflate) {
  74. return;
  75. }
  76. if (chunk.length > leftToInflate) {
  77. chunk = chunk.slice(0, leftToInflate);
  78. }
  79. leftToInflate -= chunk.length;
  80. filterWrite(chunk);
  81. });
  82. this._inflate.on("end", this._filter.end.bind(this._filter));
  83. }
  84. }
  85. this._inflate.write(data);
  86. };
  87. ParserAsync.prototype._handleMetaData = function (metaData) {
  88. this._metaData = metaData;
  89. this._bitmapInfo = Object.create(metaData);
  90. this._filter = new FilterAsync(this._bitmapInfo);
  91. };
  92. ParserAsync.prototype._handleTransColor = function (transColor) {
  93. this._bitmapInfo.transColor = transColor;
  94. };
  95. ParserAsync.prototype._handlePalette = function (palette) {
  96. this._bitmapInfo.palette = palette;
  97. };
  98. ParserAsync.prototype._simpleTransparency = function () {
  99. this._metaData.alpha = true;
  100. };
  101. ParserAsync.prototype._headersFinished = function () {
  102. // Up until this point, we don't know if we have a tRNS chunk (alpha)
  103. // so we can't emit metadata any earlier
  104. this.emit("metadata", this._metaData);
  105. };
  106. ParserAsync.prototype._finished = function () {
  107. if (this.errord) {
  108. return;
  109. }
  110. if (!this._inflate) {
  111. this.emit("error", "No Inflate block");
  112. } else {
  113. // no more data to inflate
  114. this._inflate.end();
  115. }
  116. };
  117. ParserAsync.prototype._complete = function (filteredData) {
  118. if (this.errord) {
  119. return;
  120. }
  121. let normalisedBitmapData;
  122. try {
  123. let bitmapData = bitmapper.dataToBitMap(filteredData, this._bitmapInfo);
  124. normalisedBitmapData = formatNormaliser(bitmapData, this._bitmapInfo);
  125. bitmapData = null;
  126. } catch (ex) {
  127. this._handleError(ex);
  128. return;
  129. }
  130. this.emit("parsed", normalisedBitmapData);
  131. };