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

@ -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"
},
"devDependencies": {
"aegir": "^17.0.1",
"aegir": "^17.1.1",
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"chai-string": "^1.5.0",

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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