Code coverage report for parser-sync.js

# All files parser-sync.js

93.33% Statements 42/45
78.57% Branches 11/14
100% Functions 8/8
93.33% Lines 42/45

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

    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x

    1x
    176x

    13x

    165x

    5x

    73x

    12x

    147x

    176x

    492x

    176x

    176x

    176x
    176x

    176x
    13x

    163x
    163x

    163x
    36x

    127x
    127x
    127x

    162x

    162x

    162x
    162x

    162x
    162x

    162x

    162x
    162x

    162x

     "use strict";

    let hasSyncZlib = true;
    let zlib = require("zlib");
    let inflateSync = require("./sync-inflate");
    Iif (!zlib.deflateSync) {
      hasSyncZlib = false;
    }
    let SyncReader = require("./sync-reader");
    let FilterSync = require("./filter-parse-sync");
    let Parser = require("./parser");
    let bitmapper = require("./bitmapper");
    let formatNormaliser = require("./format-normaliser");

    module.exports = function (buffer, options) {
      Iif (!hasSyncZlib) {
        throw new Error(
          "To use the sync capability of this library in old node versions, please pin pngjs to v2.3.0"
        );
      }

      let err;
      function handleError(_err_) {
        err = _err_;
      }

      let metaData;
      function handleMetaData(_metaData_) {
        metaData = _metaData_;
      }

      function handleTransColor(transColor) {
        metaData.transColor = transColor;
      }

      function handlePalette(palette) {
        metaData.palette = palette;
      }

      function handleSimpleTransparency() {
        metaData.alpha = true;
      }

      let gamma;
      function handleGamma(_gamma_) {
        gamma = _gamma_;
      }

      let inflateDataList = [];
      function handleInflateData(inflatedData) {
        inflateDataList.push(inflatedData);
      }

      let reader = new SyncReader(buffer);

      let parser = new Parser(options, {
        read: reader.read.bind(reader),
        error: handleError,
        metadata: handleMetaData,
        gamma: handleGamma,
        palette: handlePalette,
        transColor: handleTransColor,
        inflateData: handleInflateData,
        simpleTransparency: handleSimpleTransparency,
      });

      parser.start();
      reader.process();

      if (err) {
        throw err;
      }

      //join together the inflate datas
      let inflateData = Buffer.concat(inflateDataList);
      inflateDataList.length = 0;

      let inflatedData;
      if (metaData.interlace) {
        inflatedData = zlib.inflateSync(inflateData);
      } else {
        let rowSize =
          ((metaData.width * metaData.bpp * metaData.depth + 7) >> 3) + 1;
        let imageSize = rowSize * metaData.height;
        inflatedData = inflateSync(inflateData, {
          chunkSize: imageSize,
          maxLength: imageSize,
        });
      }
      inflateData = null;

      Iif (!inflatedData || !inflatedData.length) {
        throw new Error("bad png - invalid inflate data response");
      }

      let unfilteredData = FilterSync.process(inflatedData, metaData);
      inflateData = null;

      let bitmapData = bitmapper.dataToBitMap(unfilteredData, metaData);
      unfilteredData = null;

      let normalisedBitmapData = formatNormaliser(bitmapData, metaData);

      metaData.data = normalisedBitmapData;
      metaData.gamma = gamma || 0;

      return metaData;
    };