[Pkg-javascript-commits] [node-keygrip] 55/68: create prototype getters for defaults and validation

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Fri Jun 27 22:13:28 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-keygrip.

commit 1f889746a9d2052e889d14df8a628fd3af9d1fc5
Author: Jonathan Ong <jonathanrichardong at gmail.com>
Date:   Sat May 17 15:41:32 2014 -0700

    create prototype getters for defaults and validation
---
 index.js | 38 ++++++++++++++++++++++++++++++++++++--
 test.js  | 14 ++++++++++++++
 2 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/index.js b/index.js
index 21aa5d9..995fb16 100644
--- a/index.js
+++ b/index.js
@@ -9,8 +9,42 @@ function Keygrip(keys, algorithm, encoding) {
   if (!(this instanceof Keygrip)) return new Keygrip(keys, algorithm, encoding)
 
   this.keys = keys
-  this.algorithm = algorithm || 'sha1'
-  this.encoding = encoding || 'base64'
+  if (algorithm) this.algorithm = algorithm
+  if (encoding) this.encoding = encoding
+}
+
+/**
+ * Allow setting `keygrip.algorithm = 'sha1'`
+ * or `keygrip.cipher = 'aes256'`
+ * with validation instead of always doing `keygrip([], alg, enc)`.
+ * This also allows for easier defaults.
+ */
+
+Keygrip.prototype = {
+  get algorithm() {
+    return this._algorithm
+  },
+
+  set algorithm(val) {
+    if (!~crypto.getHashes().indexOf(val))
+      throw new Error('unsupported hash algorithm: ' + val)
+    this._algorithm = val
+  },
+
+  get cipher() {
+    return this._cipher
+  },
+
+  set cipher(val) {
+    if (!~crypto.getCiphers().indexOf(val))
+      throw new Error('unsupported cipher: ' + val)
+    this._cipher = val
+  },
+
+  // defaults
+  _algorithm: 'sha1',
+  _cipher: 'aes256',
+  encoding: 'base64',
 }
 
 Keygrip.prototype.sign = function Keygrip$_sign(data, key) {
diff --git a/test.js b/test.js
index c660ce5..49db10b 100644
--- a/test.js
+++ b/test.js
@@ -12,6 +12,20 @@ describe('keygrip(keys)', function () {
       keys = new Keygrip(/* empty list */);
     }, /must be provided/);
   })
+
+  it('should throw when setting an invalid algorithm', function () {
+    var keys = new Keygrip(['a', 'b'])
+    assert.throws(function () {
+      keys.algorithm = 'asdf'
+    }, /unsupported/)
+  })
+
+  it('should throw when setting an invalid cipher', function () {
+    var keys = new Keygrip(['a', 'b'])
+    assert.throws(function () {
+      keys.cipher = 'asdf'
+    }, /unsupported/)
+  })
 })
 
 describe('keygrip([key])', function () {

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



More information about the Pkg-javascript-commits mailing list