From 50873c7d45d2733244d5da8afef3d1872e657a60 Mon Sep 17 00:00:00 2001 From: popomore Date: Wed, 27 Apr 2016 17:24:51 +0800 Subject: [PATCH] feat: change .sign to standard async callback - subscribe error event - change the first argument of the callback as error --- index.js | 6 +++++- test/async_sign.tests.js | 9 ++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 9dfbca9..04c5482 100644 --- a/index.js +++ b/index.js @@ -132,7 +132,11 @@ JWT.sign = function(payload, secretOrPrivateKey, options, callback) { header: header, privateKey: secretOrPrivateKey, payload: JSON.stringify(payload) - }).on('done', callback); + }) + .on('error', callback) + .on('done', function(signature) { + callback(null, signature); + }); } else { return jws.sign({header: header, payload: payload, secret: secretOrPrivateKey, encoding: encoding}); } diff --git a/test/async_sign.tests.js b/test/async_sign.tests.js index 57495a9..93a2b14 100644 --- a/test/async_sign.tests.js +++ b/test/async_sign.tests.js @@ -9,12 +9,19 @@ describe('signing a token asynchronously', function() { var syncToken = jwt.sign({ foo: 'bar' }, secret, { algorithm: 'HS256' }); it('should return the same result as singing synchronously', function(done) { - jwt.sign({ foo: 'bar' }, secret, { algorithm: 'HS256' }, function (asyncToken) { + jwt.sign({ foo: 'bar' }, secret, { algorithm: 'HS256' }, function (err, asyncToken) { expect(asyncToken).to.be.a('string'); expect(asyncToken.split('.')).to.have.length(3); expect(asyncToken).to.equal(syncToken); done(); }); }); + + it('should throw error', function(done) { + jwt.sign({ foo: 'bar' }, secret, { algorithm: 'HS2561' }, function (err) { + expect(err).to.be.ok(); + done(); + }); + }); }); });