feat: nextTick instead of setImmediate, and fix sync in async (#136)

* fix: avoid sync callback in async function

* chore: fix linting

* chore: remove non jenkins ci

* refactor: use nextTick over setImmediate

* refactor: async/nextTick for better browser support
This commit is contained in:
Jacob Heun 2019-01-03 08:13:07 -08:00 committed by David Dias
parent 857d2bd902
commit c54ea206f0
15 changed files with 38 additions and 110 deletions

View File

@ -1,32 +0,0 @@
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
sudo: false
language: node_js
matrix:
include:
- node_js: 6
env: CXX=g++-4.8
- node_js: 8
env: CXX=g++-4.8
# - node_js: stable
# env: CXX=g++-4.8
script:
- npm run lint
- npm run test
- npm run coverage
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
after_success:
- npm run coverage-publish
addons:
firefox: 'latest'
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8

View File

@ -1,29 +0,0 @@
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
version: "{build}"
environment:
matrix:
- nodejs_version: "6"
- nodejs_version: "8"
matrix:
fast_finish: true
install:
# Install Node.js
- ps: Install-Product node $env:nodejs_version
# Upgrade npm
- npm install -g npm
# Output our current versions for debugging
- node --version
- npm --version
# Install our package dependencies
- npm install
test_script:
- npm run test:node
build: off

View File

@ -25,4 +25,4 @@ curves.forEach((curve) => {
suite suite
.on('cycle', (event) => console.log(String(event.target))) .on('cycle', (event) => console.log(String(event.target)))
.run({async: true}) .run({ async: true })

View File

@ -25,7 +25,7 @@ async.waterfall([
suite suite
.on('cycle', (event) => console.log(String(event.target))) .on('cycle', (event) => console.log(String(event.target)))
.run({async: true}) .run({ async: true })
}) })
function setup (cipher, hash, secret) { function setup (cipher, hash, secret) {

View File

@ -40,4 +40,4 @@ suite.add('sign and verify', (d) => {
suite suite
.on('cycle', (event) => console.log(String(event.target))) .on('cycle', (event) => console.log(String(event.target)))
.run({async: true}) .run({ async: true })

View File

@ -1,15 +0,0 @@
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
machine:
node:
version: stable
dependencies:
pre:
- google-chrome --version
- curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
- sudo dpkg -i google-chrome.deb || true
- sudo apt-get update
- sudo apt-get install -f
- sudo apt-get install --only-upgrade lsb-base
- sudo dpkg -i google-chrome.deb
- google-chrome --version

View File

@ -46,7 +46,7 @@
"webcrypto-shim": "github:dignifiedquire/webcrypto-shim#master" "webcrypto-shim": "github:dignifiedquire/webcrypto-shim#master"
}, },
"devDependencies": { "devDependencies": {
"aegir": "^17.0.1", "aegir": "^17.1.1",
"benchmark": "^2.1.4", "benchmark": "^2.1.4",
"chai": "^4.2.0", "chai": "^4.2.0",
"chai-string": "^1.5.0", "chai-string": "^1.5.0",

View File

@ -1,10 +1,10 @@
'use strict' 'use strict'
const asm = require('asmcrypto.js') const asm = require('asmcrypto.js')
const setImmediate = require('async/setImmediate') const nextTick = require('async/nextTick')
exports.create = function (key, iv, callback) { exports.create = function (key, iv, callback) {
const done = (err, res) => setImmediate(() => callback(err, res)) const done = (err, res) => nextTick(() => callback(err, res))
if (key.length !== 16 && key.length !== 32) { if (key.length !== 16 && key.length !== 32) {
return done(new Error('Invalid key length')) return done(new Error('Invalid key length'))
@ -21,7 +21,7 @@ exports.create = function (key, iv, callback) {
const res = { const res = {
encrypt (data, cb) { encrypt (data, cb) {
const done = (err, res) => setImmediate(() => cb(err, res)) const done = (err, res) => nextTick(() => cb(err, res))
let res let res
try { try {
@ -36,7 +36,7 @@ exports.create = function (key, iv, callback) {
}, },
decrypt (data, cb) { decrypt (data, cb) {
const done = (err, res) => setImmediate(() => cb(err, res)) const done = (err, res) => nextTick(() => cb(err, res))
let res let res
try { try {

View File

@ -12,7 +12,7 @@ const hashTypes = {
} }
const sign = (key, data, cb) => { const sign = (key, data, cb) => {
nodeify(crypto.subtle.sign({name: 'HMAC'}, key, data) nodeify(crypto.subtle.sign({ name: 'HMAC' }, key, data)
.then((raw) => Buffer.from(raw)), cb) .then((raw) => Buffer.from(raw)), cb)
} }
@ -24,7 +24,7 @@ exports.create = function (hashType, secret, callback) {
secret, secret,
{ {
name: 'HMAC', name: 'HMAC',
hash: {name: hash} hash: { name: hash }
}, },
false, false,
['sign'] ['sign']

View File

@ -2,6 +2,7 @@
const crypto = require('crypto') const crypto = require('crypto')
const lengths = require('./lengths') const lengths = require('./lengths')
const nextTick = require('async/nextTick')
exports.create = function (hash, secret, callback) { exports.create = function (hash, secret, callback) {
const res = { const res = {
@ -10,7 +11,9 @@ exports.create = function (hash, secret, callback) {
hmac.update(data) hmac.update(data)
cb(null, hmac.digest()) nextTick(() => {
cb(null, hmac.digest())
})
}, },
length: lengths[hash] length: lengths[hash]
} }

View File

@ -1,7 +1,7 @@
'use strict' 'use strict'
const crypto = require('crypto') const crypto = require('crypto')
const setImmediate = require('async/setImmediate') const nextTick = require('async/nextTick')
const curves = { const curves = {
'P-256': 'prime256v1', 'P-256': 'prime256v1',
@ -16,7 +16,7 @@ exports.generateEphmeralKeyPair = function (curve, callback) {
const ecdh = crypto.createECDH(curves[curve]) const ecdh = crypto.createECDH(curves[curve])
ecdh.generateKeys() ecdh.generateKeys()
setImmediate(() => callback(null, { nextTick(() => callback(null, {
key: ecdh.getPublicKey(), key: ecdh.getPublicKey(),
genSharedKey (theirPub, forcePrivate, cb) { genSharedKey (theirPub, forcePrivate, cb) {
if (typeof forcePrivate === 'function') { if (typeof forcePrivate === 'function') {
@ -35,7 +35,7 @@ exports.generateEphmeralKeyPair = function (curve, callback) {
return cb(err) return cb(err)
} }
setImmediate(() => cb(null, secret)) nextTick(() => cb(null, secret))
} }
})) }))
} }

View File

@ -1,13 +1,13 @@
'use strict' 'use strict'
const nacl = require('tweetnacl') const nacl = require('tweetnacl')
const setImmediate = require('async/setImmediate') const nextTick = require('async/nextTick')
exports.publicKeyLength = nacl.sign.publicKeyLength exports.publicKeyLength = nacl.sign.publicKeyLength
exports.privateKeyLength = nacl.sign.secretKeyLength exports.privateKeyLength = nacl.sign.secretKeyLength
exports.generateKey = function (callback) { exports.generateKey = function (callback) {
setImmediate(() => { nextTick(() => {
let result let result
try { try {
result = nacl.sign.keyPair() result = nacl.sign.keyPair()
@ -20,7 +20,7 @@ exports.generateKey = function (callback) {
// seed should be a 32 byte uint8array // seed should be a 32 byte uint8array
exports.generateKeyFromSeed = function (seed, callback) { exports.generateKeyFromSeed = function (seed, callback) {
setImmediate(() => { nextTick(() => {
let result let result
try { try {
result = nacl.sign.keyPair.fromSeed(seed) result = nacl.sign.keyPair.fromSeed(seed)
@ -32,13 +32,13 @@ exports.generateKeyFromSeed = function (seed, callback) {
} }
exports.hashAndSign = function (key, msg, callback) { exports.hashAndSign = function (key, msg, callback) {
setImmediate(() => { nextTick(() => {
callback(null, Buffer.from(nacl.sign.detached(msg, key))) callback(null, Buffer.from(nacl.sign.detached(msg, key)))
}) })
} }
exports.hashAndVerify = function (key, sig, msg, callback) { exports.hashAndVerify = function (key, sig, msg, callback) {
setImmediate(() => { nextTick(() => {
let result let result
try { try {
result = nacl.sign.detached.verify(msg, sig, key) result = nacl.sign.detached.verify(msg, sig, key)

View File

@ -12,7 +12,7 @@ exports.generateKey = function (bits, callback) {
name: 'RSASSA-PKCS1-v1_5', name: 'RSASSA-PKCS1-v1_5',
modulusLength: bits, modulusLength: bits,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]), publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: {name: 'SHA-256'} hash: { name: 'SHA-256' }
}, },
true, true,
['sign', 'verify'] ['sign', 'verify']
@ -31,7 +31,7 @@ exports.unmarshalPrivateKey = function (key, callback) {
key, key,
{ {
name: 'RSASSA-PKCS1-v1_5', name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'} hash: { name: 'SHA-256' }
}, },
true, true,
['sign'] ['sign']
@ -59,13 +59,13 @@ exports.hashAndSign = function (key, msg, callback) {
key, key,
{ {
name: 'RSASSA-PKCS1-v1_5', name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'} hash: { name: 'SHA-256' }
}, },
false, false,
['sign'] ['sign']
).then((privateKey) => { ).then((privateKey) => {
return webcrypto.subtle.sign( return webcrypto.subtle.sign(
{name: 'RSASSA-PKCS1-v1_5'}, { name: 'RSASSA-PKCS1-v1_5' },
privateKey, privateKey,
Uint8Array.from(msg) Uint8Array.from(msg)
) )
@ -78,13 +78,13 @@ exports.hashAndVerify = function (key, sig, msg, callback) {
key, key,
{ {
name: 'RSASSA-PKCS1-v1_5', name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'} hash: { name: 'SHA-256' }
}, },
false, false,
['verify'] ['verify']
).then((publicKey) => { ).then((publicKey) => {
return webcrypto.subtle.verify( return webcrypto.subtle.verify(
{name: 'RSASSA-PKCS1-v1_5'}, { name: 'RSASSA-PKCS1-v1_5' },
publicKey, publicKey,
sig, sig,
msg msg
@ -109,7 +109,7 @@ function derivePublicFromPrivate (jwKey) {
}, },
{ {
name: 'RSASSA-PKCS1-v1_5', name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'} hash: { name: 'SHA-256' }
}, },
true, true,
['verify'] ['verify']

View File

@ -3,11 +3,11 @@
const multihashing = require('multihashing-async') const multihashing = require('multihashing-async')
const protobuf = require('protons') const protobuf = require('protons')
const bs58 = require('bs58') const bs58 = require('bs58')
const nextTick = require('async/nextTick')
const crypto = require('./rsa') const crypto = require('./rsa')
const pbm = protobuf(require('./keys.proto')) const pbm = protobuf(require('./keys.proto'))
const forge = require('node-forge') const forge = require('node-forge')
const setImmediate = require('async/setImmediate')
class RsaPublicKey { class RsaPublicKey {
constructor (key) { constructor (key) {
@ -129,7 +129,7 @@ class RsaPrivateKey {
ensure(callback) ensure(callback)
setImmediate(() => { nextTick(() => {
let err = null let err = null
let pem = null let pem = null
try { try {

View File

@ -1,6 +1,8 @@
'use strict' 'use strict'
const crypto = require('crypto') const crypto = require('crypto')
const nextTick = require('async/nextTick')
let keypair let keypair
try { try {
if (process.env.LP2P_FORCE_CRYPTO_LIB === 'keypair') { if (process.env.LP2P_FORCE_CRYPTO_LIB === 'keypair') {
@ -8,7 +10,7 @@ try {
} }
const ursa = require('ursa-optional') // throws if not compiled const ursa = require('ursa-optional') // throws if not compiled
keypair = ({bits}) => { keypair = ({ bits }) => {
const key = ursa.generatePrivateKey(bits) const key = ursa.generatePrivateKey(bits)
return { return {
private: key.toPrivatePem(), private: key.toPrivatePem(),
@ -22,14 +24,13 @@ try {
keypair = require('keypair') keypair = require('keypair')
} }
const setImmediate = require('async/setImmediate')
const pemToJwk = require('pem-jwk').pem2jwk const pemToJwk = require('pem-jwk').pem2jwk
const jwkToPem = require('pem-jwk').jwk2pem const jwkToPem = require('pem-jwk').jwk2pem
exports.utils = require('./rsa-utils') exports.utils = require('./rsa-utils')
exports.generateKey = function (bits, callback) { exports.generateKey = function (bits, callback) {
setImmediate(() => { nextTick(() => {
let result let result
try { try {
const key = keypair({ bits: bits }) const key = keypair({ bits: bits })
@ -47,7 +48,7 @@ exports.generateKey = function (bits, callback) {
// Takes a jwk key // Takes a jwk key
exports.unmarshalPrivateKey = function (key, callback) { exports.unmarshalPrivateKey = function (key, callback) {
setImmediate(() => { nextTick(() => {
if (!key) { if (!key) {
return callback(new Error('Key is invalid')) return callback(new Error('Key is invalid'))
} }
@ -67,7 +68,7 @@ exports.getRandomValues = function (arr) {
} }
exports.hashAndSign = function (key, msg, callback) { exports.hashAndSign = function (key, msg, callback) {
setImmediate(() => { nextTick(() => {
let result let result
try { try {
const sign = crypto.createSign('RSA-SHA256') const sign = crypto.createSign('RSA-SHA256')
@ -83,7 +84,7 @@ exports.hashAndSign = function (key, msg, callback) {
} }
exports.hashAndVerify = function (key, sig, msg, callback) { exports.hashAndVerify = function (key, sig, msg, callback) {
setImmediate(() => { nextTick(() => {
let result let result
try { try {
const verify = crypto.createVerify('RSA-SHA256') const verify = crypto.createVerify('RSA-SHA256')