# All files parser-async.js

88.75% Statements 71/80
72.73% Branches 16/22
84.62% Functions 11/13
88.75% Lines 71/80

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166

    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    353x

    353x

    353x
    353x

    353x

    1x

    1x
    14x

    14x

    14x

    14x
    1x

    14x
    2x

    2x

    14x

    1x
    741x
    174x
    36x

    36x
    36x

    36x

    138x

    138x
    138x

    138x
    138x

    138x
    138x

    138x

    138x
    138x
    487x

    487x

    487x

    487x

    138x

    741x

    1x
    176x
    176x

    176x

    1x
    7x

    1x
    77x

    1x
    15x

    1x

    174x

    1x
    173x

    173x
    1x

    172x

    1x
    173x

    173x
    173x

    173x
    173x

    173x

     "use strict";

    let util = require("util");
    let zlib = require("zlib");
    let ChunkStream = require("./chunkstream");
    let FilterAsync = require("./filter-parse-async");
    let Parser = require("./parser");
    let bitmapper = require("./bitmapper");
    let formatNormaliser = require("./format-normaliser");

    let ParserAsync = (module.exports = function (options) {
      ChunkStream.call(this);

      this._parser = new Parser(options, {
        read: this.read.bind(this),
        error: this._handleError.bind(this),
        metadata: this._handleMetaData.bind(this),
        gamma: this.emit.bind(this, "gamma"),
        palette: this._handlePalette.bind(this),
        transColor: this._handleTransColor.bind(this),
        finished: this._finished.bind(this),
        inflateData: this._inflateData.bind(this),
        simpleTransparency: this._simpleTransparency.bind(this),
        headersFinished: this._headersFinished.bind(this),
      });
      this._options = options;
      this.writable = true;

      this._parser.start();
    });
    util.inherits(ParserAsync, ChunkStream);

    ParserAsync.prototype._handleError = function (err) {
      this.emit("error", err);

      this.writable = false;

      this.destroy();

      if (this._inflate && this._inflate.destroy) {
        this._inflate.destroy();
      }

      if (this._filter) {
        this._filter.destroy();
        // For backward compatibility with Node 7 and below.
        // Suppress errors due to _inflate calling write() even after
        // it's destroy()'ed.
        this._filter.on("error", function () {});
      }

      this.errord = true;
    };

    ParserAsync.prototype._inflateData = function (data) {
      if (!this._inflate) {
        if (this._bitmapInfo.interlace) {
          this._inflate = zlib.createInflate();

          this._inflate.on("error", this.emit.bind(this, "error"));
          this._filter.on("complete", this._complete.bind(this));

          this._inflate.pipe(this._filter);
        } else {
          let rowSize =
            ((this._bitmapInfo.width *
              this._bitmapInfo.bpp *
              this._bitmapInfo.depth +
              7) >>
              3) +
            1;
          let imageSize = rowSize * this._bitmapInfo.height;
          let chunkSize = Math.max(imageSize, zlib.Z_MIN_CHUNK);

          this._inflate = zlib.createInflate({ chunkSize: chunkSize });
          let leftToInflate = imageSize;

          let emitError = this.emit.bind(this, "error");
          this._inflate.on("error", function (err) {
            if (!leftToInflate) {
              return;
            }

            emitError(err);
          });
          this._filter.on("complete", this._complete.bind(this));

          let filterWrite = this._filter.write.bind(this._filter);
          this._inflate.on("data", function (chunk) {
            Iif (!leftToInflate) {
              return;
            }

            Iif (chunk.length > leftToInflate) {
              chunk = chunk.slice(0, leftToInflate);
            }

            leftToInflate -= chunk.length;

            filterWrite(chunk);
          });

          this._inflate.on("end", this._filter.end.bind(this._filter));
        }
      }
      this._inflate.write(data);
    };

    ParserAsync.prototype._handleMetaData = function (metaData) {
      this._metaData = metaData;
      this._bitmapInfo = Object.create(metaData);

      this._filter = new FilterAsync(this._bitmapInfo);
    };

    ParserAsync.prototype._handleTransColor = function (transColor) {
      this._bitmapInfo.transColor = transColor;
    };

    ParserAsync.prototype._handlePalette = function (palette) {
      this._bitmapInfo.palette = palette;
    };

    ParserAsync.prototype._simpleTransparency = function () {
      this._metaData.alpha = true;
    };

    ParserAsync.prototype._headersFinished = function () {
      // Up until this point, we don't know if we have a tRNS chunk (alpha)
      // so we can't emit metadata any earlier
      this.emit("metadata", this._metaData);
    };

    ParserAsync.prototype._finished = function () {
      Iif (this.errord) {
        return;
      }

      if (!this._inflate) {
        this.emit("error", "No Inflate block");
      } else {
        // no more data to inflate
        this._inflate.end();
      }
    };

    ParserAsync.prototype._complete = function (filteredData) {
      Iif (this.errord) {
        return;
      }

      let normalisedBitmapData;

      try {
        let bitmapData = bitmapper.dataToBitMap(filteredData, this._bitmapInfo);

        normalisedBitmapData = formatNormaliser(bitmapData, this._bitmapInfo);
        bitmapData = null;
      } catch (ex) {
        this._handleError(ex);
        return;
      }

      this.emit("parsed", normalisedBitmapData);
    };