Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
feat: skip nextTick in nodeify (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire authored and daviddias committed Aug 17, 2017
1 parent 1b6a070 commit f20267b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@
"browserify-aes": "^1.0.6",
"keypair": "^1.0.1",
"libp2p-crypto-secp256k1": "~0.2.1",
"nodeify": "^1.0.1",
"multihashing-async": "~0.4.6",
"pem-jwk": "^1.5.1",
"protocol-buffers": "^3.2.1",
"rsa-pem-to-jwk": "^1.1.3",
"safe-buffer": "^5.1.1",
"tweetnacl": "^1.0.0",
"webcrypto-shim": "github:dignifiedquire/webcrypto-shim#master",
"multihashing-async": "~0.4.6"
"webcrypto-shim": "github:dignifiedquire/webcrypto-shim#master"
},
"devDependencies": {
"aegir": "^11.0.2",
Expand Down Expand Up @@ -79,4 +78,4 @@
"greenkeeper[bot] <greenkeeper[bot]@users.noreply.github.com>",
"nikuda <[email protected]>"
]
}
}
10 changes: 7 additions & 3 deletions src/hmac/index-browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const nodeify = require('nodeify')
const nodeify = require('../nodeify')
const Buffer = require('safe-buffer').Buffer

const crypto = require('../webcrypto.js')()
Expand All @@ -12,6 +12,11 @@ const hashTypes = {
SHA512: 'SHA-512'
}

const sign = (key, data, cb) => {
nodeify(crypto.subtle.sign({name: 'HMAC'}, key, data)
.then((raw) => Buffer.from(raw)), cb)
}

exports.create = function (hashType, secret, callback) {
const hash = hashTypes[hashType]

Expand All @@ -27,8 +32,7 @@ exports.create = function (hashType, secret, callback) {
).then((key) => {
return {
digest (data, cb) {
nodeify(crypto.subtle.sign({name: 'HMAC'}, key, data)
.then((raw) => Buffer.from(raw)), cb)
sign(key, data, cb)
},
length: lengths[hashType]
}
Expand Down
2 changes: 1 addition & 1 deletion src/keys/ecdh-browser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const webcrypto = require('../webcrypto.js')()
const nodeify = require('nodeify')
const nodeify = require('../nodeify')
const BN = require('asn1.js').bignum
const Buffer = require('safe-buffer').Buffer

Expand Down
2 changes: 1 addition & 1 deletion src/keys/rsa-browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const nodeify = require('nodeify')
const nodeify = require('../nodeify')
const Buffer = require('safe-buffer').Buffer

const webcrypto = require('../webcrypto.js')()
Expand Down
11 changes: 11 additions & 0 deletions src/nodeify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

// Based on npmjs.com/nodeify but without additional `nextTick` calls
// to keep the overhead low
module.exports = function nodeify (promise, cb) {
return promise.then((res) => {
cb(null, res)
}, (err) => {
cb(err)
})
}

0 comments on commit f20267b

Please sign in to comment.