[Pkg-javascript-commits] [node-deflate-crc32-stream] 01/02: Imported Upstream version 0.1.1

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Sat Jun 28 23:02:55 UTC 2014


This is an automated email from the git hooks/post-receive script.

andrewrk-guest pushed a commit to branch master
in repository node-deflate-crc32-stream.

commit 8b4c36f6809d9eb7b3a2ce80870b1a316f780916
Author: Andrew Kelley <superjoe30 at gmail.com>
Date:   Sat Jun 28 22:57:26 2014 +0000

    Imported Upstream version 0.1.1
---
 .gitignore                  |  3 ++
 .travis.yml                 |  4 ++
 CONTRIBUTING.md             | 14 +++++++
 LICENSE-MIT                 | 22 +++++++++++
 README.md                   | 61 +++++++++++++++++++++++++++++
 lib/deflate-crc32-stream.js | 69 +++++++++++++++++++++++++++++++++
 package.json                | 45 ++++++++++++++++++++++
 test/deflate.js             | 40 +++++++++++++++++++
 test/helpers/index.js       | 93 +++++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 351 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c766b8b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+npm-debug.log
+node_modules/
+tmp/
\ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..2ca91f2
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+  - "0.10"
+  - "0.8"
\ No newline at end of file
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 0000000..1fa92d4
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1,14 @@
+## Contributing
+
+#### Code Style Guide
+
+* code should be indented with 2 spaces
+* single quotes should be used where feasible
+* commas should be followed by a single space (function params, etc)
+* variable declaration should include `var`, [no multiple declarations](http://benalman.com/news/2012/05/multiple-var-statements-javascript/)
+
+#### Tests
+
+* tests should be added to the nodeunit configs in `test/`
+* tests can be run with `npm test`
+* see existing tests for guidance
\ No newline at end of file
diff --git a/LICENSE-MIT b/LICENSE-MIT
new file mode 100644
index 0000000..56420a6
--- /dev/null
+++ b/LICENSE-MIT
@@ -0,0 +1,22 @@
+Copyright (c) 2014 Chris Talkington, contributors.
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..740675b
--- /dev/null
+++ b/README.md
@@ -0,0 +1,61 @@
+# deflate-crc32-stream v0.1.0 [![Build Status](https://travis-ci.org/ctalkington/node-deflate-crc32-stream.svg?branch=master)](https://travis-ci.org/ctalkington/node-deflate-crc32-stream)
+
+deflate-crc32-stream is a streaming deflater with CRC32 checksumer. It uses [buffer-crc32](https://www.npmjs.org/package/buffer-crc32) behind the scenes to reliably handle binary data and fancy character sets. Data comes through compressed with [zlib.DeflateRaw](http://nodejs.org/api/zlib.html#zlib_class_zlib_deflateraw).
+
+[![NPM](https://nodei.co/npm/deflate-crc32-stream.png)](https://nodei.co/npm/deflate-crc32-stream/)
+
+### Install
+
+```bash
+npm install deflate-crc32-stream --save
+```
+
+You can also use `npm install https://github.com/ctalkington/node-deflate-crc32-stream/archive/master.tar.gz` to test upcoming versions.
+
+### Usage
+
+```js
+var CRC32Stream = require('deflate-crc32-stream');
+
+var source = fs.createReadStream('file.txt');
+var deflate = new DeflateCRC32Stream();
+
+deflate.on('end', function(err) {
+  // do something with deflate.digest() here
+});
+
+// either pipe it
+source.pipe(deflate);
+
+// or write it
+deflate.write('string');
+deflate.end();
+```
+
+### Instance API
+
+Inherits [zlib.DeflateRaw](http://nodejs.org/api/zlib.html#zlib_class_zlib_deflateraw) methods.
+
+#### digest()
+
+Returns the checksum digest in unsigned form.
+
+#### hex()
+
+Returns the hexadecimal representation of the checksum digest. (ie E81722F0)
+
+#### size(compressed)
+
+Returns the raw uncompressed size/length of passed-through data.
+
+If `compressed` is `true`, it returns compressed length instead.
+
+### Instance Options
+
+Inherits [zlib.DeflateRaw](http://nodejs.org/api/zlib.html#zlib_class_zlib_deflateraw) options.
+
+## Things of Interest
+
+- [Changelog](https://github.com/ctalkington/node-deflate-crc32-stream/releases)
+- [Contributing](https://github.com/ctalkington/node-deflate-crc32-stream/blob/master/CONTRIBUTING.md)
+- [MIT License](https://github.com/ctalkington/node-deflate-crc32-stream/blob/master/LICENSE-MIT)
\ No newline at end of file
diff --git a/lib/deflate-crc32-stream.js b/lib/deflate-crc32-stream.js
new file mode 100644
index 0000000..d467dad
--- /dev/null
+++ b/lib/deflate-crc32-stream.js
@@ -0,0 +1,69 @@
+/**
+ * node-deflate-crc32-stream
+ *
+ * Copyright (c) 2014 Chris Talkington, contributors.
+ * Licensed under the MIT license.
+ * https://github.com/ctalkington/node-deflate-crc32-stream/blob/master/LICENSE-MIT
+ */
+var zlib = require('zlib');
+var inherits = require('util').inherits;
+
+var crc32 = require('buffer-crc32');
+
+function DeflateCRC32Stream(options) {
+  zlib.DeflateRaw.call(this, options);
+
+  this.checksum = new Buffer(4);
+  this.checksum.writeInt32BE(0, 0);
+
+  this.rawSize = 0;
+  this.compressedSize = 0;
+
+  // BC v0.8
+  if (typeof zlib.DeflateRaw.prototype.push !== 'function') {
+    this.on('data', function(chunk) {
+      if (chunk) {
+        this.compressedSize += chunk.length;
+      }
+    });
+  }
+}
+
+inherits(DeflateCRC32Stream, zlib.DeflateRaw);
+
+DeflateCRC32Stream.prototype.push = function(chunk, encoding) {
+  if (chunk) {
+    this.compressedSize += chunk.length;
+  }
+
+  return zlib.DeflateRaw.prototype.push.call(this, chunk, encoding);
+};
+
+DeflateCRC32Stream.prototype.write = function(chunk, cb) {
+  if (chunk) {
+    this.checksum = crc32(chunk, this.checksum);
+    this.rawSize += chunk.length;
+  }
+
+  return zlib.DeflateRaw.prototype.write.call(this, chunk, cb);
+};
+
+DeflateCRC32Stream.prototype.digest = function() {
+  return crc32.unsigned(0, this.checksum);
+};
+
+DeflateCRC32Stream.prototype.hex = function() {
+  return this.digest().toString(16).toUpperCase();
+};
+
+DeflateCRC32Stream.prototype.size = function(compressed) {
+  compressed = compressed || false;
+
+  if (compressed) {
+    return this.compressedSize;
+  } else {
+    return this.rawSize;
+  }
+};
+
+module.exports = DeflateCRC32Stream;
\ No newline at end of file
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..57e1781
--- /dev/null
+++ b/package.json
@@ -0,0 +1,45 @@
+{
+  "name": "deflate-crc32-stream",
+  "version": "0.1.1",
+  "description": "a streaming deflater with CRC32 checksumer",
+  "homepage": "https://github.com/ctalkington/node-deflate-crc32-stream",
+  "author": {
+    "name": "Chris Talkington",
+    "url": "http://christalkington.com/"
+  },
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/ctalkington/node-deflate-crc32-stream.git"
+  },
+  "bugs": {
+    "url": "https://github.com/ctalkington/node-deflate-crc32-stream/issues"
+  },
+  "licenses": [
+    {
+      "type": "MIT",
+      "url": "https://github.com/ctalkington/node-deflate-crc32-stream/blob/master/LICENSE-MIT"
+    }
+  ],
+  "main": "lib/deflate-crc32-stream.js",
+  "engines": {
+    "node": ">= 0.8.0"
+  },
+  "scripts": {
+    "test": "mocha --reporter dot"
+  },
+  "dependencies": {
+    "buffer-crc32": "~0.2.1"
+  },
+  "devDependencies": {
+    "chai": "~1.8.1",
+    "mocha": "~1.16.0",
+    "readable-stream": "~1.0.24"
+  },
+  "keywords": [
+    "deflate",
+    "crc32-stream",
+    "crc32",
+    "stream",
+    "checksum"
+  ]
+}
\ No newline at end of file
diff --git a/test/deflate.js b/test/deflate.js
new file mode 100644
index 0000000..70f7c26
--- /dev/null
+++ b/test/deflate.js
@@ -0,0 +1,40 @@
+/*global before,describe,it */
+var assert = require('chai').assert;
+
+var helpers = require('./helpers');
+var BinaryStream = helpers.BinaryStream;
+var DeadEndStream = helpers.DeadEndStream;
+
+var DeflateCRC32Stream = require('../lib/deflate-crc32-stream.js');
+
+describe('DeflateCRC32Stream', function() {
+  it('should checksum data while passing through data', function(done) {
+    var binary = new BinaryStream(1024 * 16);
+    var checksum = new DeflateCRC32Stream();
+    var deadend = new DeadEndStream();
+
+    checksum.on('end', function() {
+      assert.equal(checksum.digest(), 3893830384);
+      assert.equal(checksum.hex(), 'E81722F0');
+      assert.equal(checksum.size(), 16384);
+      assert.equal(checksum.size(true), 402);
+      done();
+    });
+
+    checksum.pipe(deadend);
+    binary.pipe(checksum);
+  });
+
+  it('should gracefully handle having no data chunks passed to it', function(done) {
+    var checksum = new DeflateCRC32Stream();
+    var deadend = new DeadEndStream();
+
+    checksum.on('end', function() {
+      assert.equal(checksum.digest(), 0);
+      done();
+    });
+
+    checksum.pipe(deadend);
+    checksum.end();
+  });
+});
\ No newline at end of file
diff --git a/test/helpers/index.js b/test/helpers/index.js
new file mode 100644
index 0000000..84f4ded
--- /dev/null
+++ b/test/helpers/index.js
@@ -0,0 +1,93 @@
+var crypto = require('crypto');
+var fs = require('fs');
+var inherits = require('util').inherits;
+
+var Stream = require('stream').Stream;
+var Readable = require('readable-stream').Readable;
+var Writable = require('readable-stream').Writable;
+
+function adjustDateByOffset(d, offset) {
+  d = (d instanceof Date) ? d : new Date();
+
+  if (offset >= 1) {
+    d.setMinutes(d.getMinutes() - offset);
+  } else {
+    d.setMinutes(d.getMinutes() + Math.abs(offset));
+  }
+
+  return d;
+}
+
+module.exports.adjustDateByOffset = adjustDateByOffset;
+
+function binaryBuffer(n) {
+  var buffer = new Buffer(n);
+
+  for (var i = 0; i < n; i++) {
+    buffer.writeUInt8(i&255, i);
+  }
+
+  return buffer;
+}
+
+module.exports.binaryBuffer = binaryBuffer;
+
+function BinaryStream(size, options) {
+  Readable.call(this, options);
+
+  var buf = new Buffer(size);
+
+  for (var i = 0; i < size; i++) {
+    buf.writeUInt8(i&255, i);
+  }
+
+  this.push(buf);
+  this.push(null);
+}
+
+inherits(BinaryStream, Readable);
+
+BinaryStream.prototype._read = function(size) {};
+
+module.exports.BinaryStream = BinaryStream;
+
+function DeadEndStream(options) {
+  Writable.call(this, options);
+}
+
+inherits(DeadEndStream, Writable);
+
+DeadEndStream.prototype._write = function(chuck, encoding, callback) {
+  callback();
+};
+
+module.exports.DeadEndStream = DeadEndStream;
+
+function fileBuffer(filepath) {
+  return fs.readFileSync(filepath);
+}
+
+module.exports.fileBuffer = fileBuffer;
+
+function WriteHashStream(path, options) {
+  fs.WriteStream.call(this, path, options);
+
+  this.hash = crypto.createHash('sha1');
+  this.digest = null;
+
+  this.on('close', function() {
+    this.digest = this.hash.digest('hex');
+  });
+}
+
+inherits(WriteHashStream, fs.WriteStream);
+
+WriteHashStream.prototype.write = function(chunk) {
+  if (chunk) {
+    this.hash.update(chunk);
+  }
+
+  return fs.WriteStream.prototype.write.call(this, chunk);
+};
+
+module.exports.WriteHashStream = WriteHashStream;
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-deflate-crc32-stream.git



More information about the Pkg-javascript-commits mailing list