[Pkg-javascript-commits] [node-hash-base] 01/04: Initial commit

Bastien Roucariès rouca at moszumanska.debian.org
Thu May 4 10:21:22 UTC 2017


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

rouca pushed a commit to annotated tag v1.0.1
in repository node-hash-base.

commit e315bd2c5c9e98ecac9a45bb5af1c38d87c3bc47
Author: Kirill Fomichev <fanatid at ya.ru>
Date:   Sun Apr 3 22:13:00 2016 +0300

    Initial commit
---
 .gitignore    |   5 ++
 .travis.yml   |  17 ++++++
 README.md     |  19 +++++++
 index.js      |  61 +++++++++++++++++++++
 package.json  |  37 +++++++++++++
 test/index.js | 169 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 308 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8afa17e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+.nyc_output
+coverage
+node_modules
+
+npm-debug.log
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..85b53b8
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,17 @@
+sudo: false
+language: node_js
+node_js:
+  - "0.10"
+  - "0.11"
+  - "0.12"
+  - "4"
+  - "5"
+env:
+  matrix:
+    - TEST_SUITE=unit
+matrix:
+  include:
+    - node_js: "4"
+      env: TEST_SUITE=lint
+script: npm run-script $TEST_SUITE
+
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..63cf332
--- /dev/null
+++ b/README.md
@@ -0,0 +1,19 @@
+# hash-base
+
+[![NPM Package](https://img.shields.io/npm/v/hash-base.svg?style=flat-square)](https://www.npmjs.org/package/hash-base)
+[![Build Status](https://img.shields.io/travis/crypto-browserify/hash-base.svg?branch=master&style=flat-square)](https://travis-ci.org/crypto-browserify/hash-base)
+[![Dependency status](https://img.shields.io/david/crypto-browserify/hash-base.svg?style=flat-square)](https://david-dm.org/crypto-browserify/hash-base#info=dependencies)
+
+[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
+
+Abstract base class to inherit from if you want to create streams implementing the same API as node crypto streams.
+
+Requires you to implement 2 methods:
+
+  - `_update(data)` takes a buffer and should return nothing
+
+  - `_digest()` takes no arguments and should return a buffer
+
+## LICENSE
+
+MIT
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..673cff3
--- /dev/null
+++ b/index.js
@@ -0,0 +1,61 @@
+'use strict'
+var Transform = require('stream').Transform
+var inherits = require('inherits')
+
+function HashBase () {
+  Transform.call(this)
+}
+
+inherits(HashBase, Transform)
+
+HashBase.DEFAULT_ENCODING = 'buffer'
+
+HashBase.prototype.update = function (data, encoding) {
+  if (!Buffer.isBuffer(data)) {
+    if (encoding === undefined) encoding = HashBase.DEFAULT_ENCODING
+    if (encoding === 'buffer') encoding = 'binary'
+    data = new Buffer(data, encoding)
+  }
+
+  this._update(data)
+}
+
+HashBase.prototype.digest = function (encoding) {
+  var digest = this._digest()
+  if (encoding === undefined) encoding = HashBase.DEFAULT_ENCODING
+  if (encoding !== 'buffer') digest = digest.toString(encoding)
+  return digest
+}
+
+HashBase.prototype._update = function (data) {
+  throw new Error('_update is not implemented')
+}
+
+HashBase.prototype._digest = function () {
+  throw new Error('_digest is not implemented')
+}
+
+HashBase.prototype._transform = function (chunk, encoding, callback) {
+  var error = null
+  try {
+    if (encoding !== 'buffer') chunk = new Buffer(chunk, encoding)
+    this._update(chunk)
+  } catch (err) {
+    error = err
+  }
+
+  callback(error)
+}
+
+HashBase.prototype._flush = function (callback) {
+  var error = null
+  try {
+    this.push(this._digest())
+  } catch (err) {
+    error = err
+  }
+
+  callback(error)
+}
+
+module.exports = HashBase
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..5c2873b
--- /dev/null
+++ b/package.json
@@ -0,0 +1,37 @@
+{
+  "name": "hash-base",
+  "version": "1.0.0",
+  "description": "abstract base class for hash-streams",
+  "keywords": [
+    "hash",
+    "stream"
+  ],
+  "homepage": "https://github.com/crypto-browserify/hash-base",
+  "bugs": {
+    "url": "https://github.com/crypto-browserify/hash-base/issues"
+  },
+  "license": "MIT",
+  "author": "Kirill Fomichev <fanatid at ya.ru> (https://github.com/fanatid)",
+  "files": [
+    "index.js"
+  ],
+  "main": "index.js",
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/crypto-browserify/hash-base.git"
+  },
+  "scripts": {
+    "coverage": "nyc node test/*.js",
+    "lint": "standard",
+    "test": "npm run lint && npm run unit",
+    "unit": "node test/*.js"
+  },
+  "dependencies": {
+    "inherits": "^2.0.1"
+  },
+  "devDependencies": {
+    "nyc": "^6.1.1",
+    "standard": "^6.0.8",
+    "tape": "^4.2.0"
+  }
+}
diff --git a/test/index.js b/test/index.js
new file mode 100644
index 0000000..2d8c16d
--- /dev/null
+++ b/test/index.js
@@ -0,0 +1,169 @@
+'use strict'
+var test = require('tape').test
+var HashBase = require('../')
+
+var DEFAULT_ENCODING = HashBase.DEFAULT_ENCODING
+
+function beforeEach (t) {
+  var _test = t.test
+  t.test = function (name, cb) {
+    _test(name, function (t) {
+      HashBase.DEFAULT_ENCODING = DEFAULT_ENCODING
+      t.base = new HashBase()
+      cb(t)
+    })
+  }
+}
+
+test('update', function (t) {
+  beforeEach(t)
+
+  t.test('should pass buffer to _update', function (t) {
+    t.plan(1)
+    var buffer = new Buffer(42)
+    t.base._update = function (data) { t.true(buffer === data) }
+    t.base.update(buffer)
+    t.end()
+  })
+
+  t.test('should use DEFAULT_ENCODING', function (t) {
+    HashBase.DEFAULT_ENCODING = 'utf-8'
+    t.base._update = function (data) { t.same(data, new Buffer('ZЪ', 'utf-8')) }
+    t.base.update('ZЪ')
+    t.end()
+  })
+
+  t.test('should decode string as binary by default', function (t) {
+    t.base._update = function (data) { t.same(data, new Buffer('ZЪ', 'binary')) }
+    t.base.update('ZЪ')
+    t.end()
+  })
+
+  t.test('should decode string with custom encoding', function (t) {
+    t.base._update = function (data) { t.same(data, new Buffer('ZЪ', 'utf-8')) }
+    t.base.update('ZЪ', 'utf-8')
+    t.end()
+  })
+
+  t.end()
+})
+
+test('decode', function (t) {
+  beforeEach(t)
+
+  t.test('should return buffer from _digest by default', function (t) {
+    t.plan(2)
+    var buffer = new Buffer(42)
+    t.base._digest = function () {
+      t.pass()
+      return buffer
+    }
+    t.same(t.base.digest(), buffer)
+    t.end()
+  })
+
+  t.test('should use DEFAULT_ENCODING', function (t) {
+    HashBase.DEFAULT_ENCODING = 'utf-8'
+    t.base._digest = function () { return new Buffer('ZЪ', 'utf-8') }
+    t.same(t.base.digest(), 'ZЪ')
+    t.end()
+  })
+
+  t.test('should return buffer by default', function (t) {
+    t.base._digest = function () { return new Buffer('ZЪ', 'utf-8') }
+    t.same(t.base.digest(), new Buffer('ZЪ', 'utf-8'))
+    t.end()
+  })
+
+  t.test('should encode result with custom encoding', function (t) {
+    t.base._digest = function () { return new Buffer('ZЪ', 'utf-8') }
+    t.same(t.base.digest('utf-8'), 'ZЪ')
+    t.end()
+  })
+
+  t.end()
+})
+
+test('_update', function (t) {
+  beforeEach(t)
+
+  t.test('should throw error by default', function (t) {
+    t.throws(function () {
+      t.base._update()
+    }, /^Error: _update is not implemented$/)
+    t.end()
+  })
+
+  t.end()
+})
+
+test('_digest', function (t) {
+  beforeEach(t)
+
+  t.test('_digest should throw error by default', function (t) {
+    t.throws(function () {
+      t.base._digest()
+    }, /^Error: _digest is not implemented$/)
+    t.end()
+  })
+
+  t.end()
+})
+
+test('_transform', function (t) {
+  beforeEach(t)
+
+  t.test('should use _update', function (t) {
+    t.plan(2)
+    var buffer = new Buffer(42)
+    t.base._update = function (data) { t.true(data === buffer) }
+    t.base._transform(buffer, 'buffer', function (err) {
+      t.same(err, null)
+    })
+    t.end()
+  })
+
+  t.test('should decode string with custom encoding', function (t) {
+    t.plan(2)
+    t.base._update = function (data) { t.same(data, new Buffer('ZЪ', 'utf-8')) }
+    t.base._transform('ZЪ', 'utf-8', function (err) {
+      t.same(err, null)
+    })
+    t.end()
+  })
+
+  t.test('should handle error in _update', function (t) {
+    t.plan(1)
+    var err = new Error('hey')
+    t.base._update = function () { throw err }
+    t.base._transform(new Buffer(42), 'buffer', function (_err) {
+      t.true(_err === err)
+    })
+    t.end()
+  })
+
+  t.end()
+})
+
+test('_flush', function (t) {
+  beforeEach(t)
+
+  t.test('should use _digest', function (t) {
+    t.plan(2)
+    var buffer = new Buffer(42)
+    t.base._digest = function () { return buffer }
+    t.base.push = function (data) { t.true(data === buffer) }
+    t.base._flush(function (err) { t.same(err, null) })
+    t.end()
+  })
+
+  t.test('should handle errors in _digest', function (t) {
+    t.plan(1)
+    var err = new Error('hey')
+    t.base._digest = function () { throw err }
+    t.base._flush(function (_err) { t.true(_err === err) })
+    t.end()
+  })
+
+  t.end()
+})

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-hash-base.git



More information about the Pkg-javascript-commits mailing list