{"size": 1315, "ext": "js", "max_stars_repo_path": "node_modules/crc32-stream/lib/deflate-crc32-stream.js", "max_stars_repo_name": "ai-man-123/MDyoobotz", "max_stars_repo_stars_event_min_datetime": "2016-12-01T16:21:20.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-28T15:08:20.000Z", "max_issues_repo_path": "node_modules/crc32-stream/lib/deflate-crc32-stream.js", "max_issues_repo_name": "ai-man-123/MDyoobotz", "max_issues_repo_issues_event_min_datetime": "2016-12-03T10:53:14.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-29T19:55:34.000Z", "max_forks_repo_path": "node_modules/crc32-stream/lib/deflate-crc32-stream.js", "max_forks_repo_name": "ai-man-123/MDyoobotz", "max_forks_repo_forks_event_min_datetime": "2016-12-01T20:30:33.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-28T17:01:34.000Z"}
{"max_stars_count": 761, "max_issues_count": 324, "max_forks_count": 364, "avg_line_length": 20.873015873, "max_line_length": 75, "alphanum_fraction": 0.6114068441}
/**
 * node-crc32-stream
 *
 * Copyright (c) 2014 Chris Talkington, contributors.
 * Licensed under the MIT license.
 * https://github.com/archiverjs/node-crc32-stream/blob/master/LICENSE-MIT
 */

'use strict';

const {DeflateRaw} = require('zlib');

const {crc32} = require('crc');

class DeflateCRC32Stream extends DeflateRaw {
  constructor(options) {
    super(options);

    this.checksum = Buffer.allocUnsafe(4);
    this.checksum.writeInt32BE(0, 0);

    this.rawSize = 0;
    this.compressedSize = 0;
  }

  push(chunk, encoding) {
    if (chunk) {
      this.compressedSize += chunk.length;
    }

    return super.push(chunk, encoding);
  }

  write(chunk, enc, cb) {
    if (chunk) {
      this.checksum = crc32(chunk, this.checksum);
      this.rawSize += chunk.length;
    }

    return super.write(chunk, enc, cb);
  }

  digest(encoding) {
    const checksum = Buffer.allocUnsafe(4);
    checksum.writeUInt32BE(this.checksum >>> 0, 0);
    return encoding ? checksum.toString(encoding) : checksum;
  }

  hex() {
    return this.digest('hex').toUpperCase();
  }

  size(compressed = false) {
    if (compressed) {
      return this.compressedSize;
    } else {
      return this.rawSize;
    }
  }
}

module.exports = DeflateCRC32Stream;
