From 7eeed87b101a6a68560f350502f230e38bd276b6 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 10 Jan 2019 11:16:03 +0000 Subject: [PATCH] fix: reduce bundle size (#28) --- .gitignore | 1 + appveyor.yml | 29 ----------------------------- package.json | 14 ++++++++------ src/cms.js | 19 ++++++++++++------- src/keychain.js | 18 ++++++++---------- src/util.js | 3 ++- test/browser.js | 6 +++--- test/node.js | 6 +++--- 8 files changed, 37 insertions(+), 59 deletions(-) delete mode 100644 appveyor.yml diff --git a/.gitignore b/.gitignore index 1c73b37..b64f085 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,4 @@ test/test-data/go-ipfs-repo/LOG.old # while testing npm5 package-lock.json +yarn.lock \ No newline at end of file diff --git a/appveyor.yml b/appveyor.yml deleted file mode 100644 index 046bf91..0000000 --- a/appveyor.yml +++ /dev/null @@ -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 diff --git a/package.json b/package.json index 9cd9fed..3b97528 100644 --- a/package.json +++ b/package.json @@ -44,21 +44,23 @@ "dependencies": { "async": "^2.6.1", "interface-datastore": "~0.6.0", - "libp2p-crypto": "~0.15.0", - "lodash": "^4.17.11", - "pull-stream": "^3.6.9", + "libp2p-crypto": "~0.16.0", + "merge-options": "^1.0.1", + "node-forge": "~0.7.6", + "pull-stream": "^3.6.8", "sanitize-filename": "^1.6.1" }, "devDependencies": { - "aegir": "^18.0.2", + "aegir": "^18.0.3", "chai": "^4.2.0", - "chai-string": "^1.5.0", + "chai-string": "^1.4.0", "datastore-fs": "~0.7.0", "datastore-level": "~0.10.0", "dirty-chai": "^2.0.1", "level-js": "^4.0.0", "mocha": "^5.2.0", - "peer-id": "~0.12.1", + "multihashes": "~0.4.14", + "peer-id": "~0.12.2", "rimraf": "^2.6.3" }, "contributors": [ diff --git a/src/cms.js b/src/cms.js index 937063c..90d7d85 100644 --- a/src/cms.js +++ b/src/cms.js @@ -1,7 +1,12 @@ 'use strict' -const async = require('async') -const forge = require('node-forge') +const setImmediate = require('async/setImmediate') +const series = require('async/series') +const detect = require('async/detect') +const waterfall = require('async/waterfall') +require('node-forge/lib/pkcs7') +require('node-forge/lib/pbe') +const forge = require('node-forge/lib/forge') const util = require('./util') /** @@ -39,13 +44,13 @@ class CMS { */ encrypt (name, plain, callback) { const self = this - const done = (err, result) => async.setImmediate(() => callback(err, result)) + const done = (err, result) => setImmediate(() => callback(err, result)) if (!Buffer.isBuffer(plain)) { return done(new Error('Plain data must be a Buffer')) } - async.series([ + series([ (cb) => self.keychain.findKeyByName(name, cb), (cb) => self.keychain._getPrivateKey(name, cb) ], (err, results) => { @@ -85,7 +90,7 @@ class CMS { * @returns {undefined} */ decrypt (cmsData, callback) { - const done = (err, result) => async.setImmediate(() => callback(err, result)) + const done = (err, result) => setImmediate(() => callback(err, result)) if (!Buffer.isBuffer(cmsData)) { return done(new Error('CMS data is required')) @@ -112,7 +117,7 @@ class CMS { keyId: r.issuer.find(a => a.shortName === 'CN').value } }) - async.detect( + detect( recipients, (r, cb) => self.keychain.findKeyById(r.keyId, (err, info) => cb(null, !err && info)), (err, r) => { @@ -124,7 +129,7 @@ class CMS { return done(err) } - async.waterfall([ + waterfall([ (cb) => self.keychain.findKeyById(r.keyId, cb), (key, cb) => self.keychain._getPrivateKey(key.name, cb) ], (err, pem) => { diff --git a/src/keychain.js b/src/keychain.js index a9619fd..cecc320 100644 --- a/src/keychain.js +++ b/src/keychain.js @@ -2,12 +2,11 @@ 'use strict' const sanitize = require('sanitize-filename') -const deepmerge = require('lodash/merge') +const mergeOptions = require('merge-options') const crypto = require('libp2p-crypto') const DS = require('interface-datastore') -const pull = require('pull-stream') -const isString = require('lodash/isString') -const isSafeInteger = require('lodash/isSafeInteger') +const collect = require('pull-stream/sinks/collect') +const pull = require('pull-stream/pull') const CMS = require('./cms') const keyPrefix = '/pkcs8/' @@ -32,7 +31,7 @@ const defaultOptions = { function validateKeyName (name) { if (!name) return false - if (!isString(name)) return false + if (typeof name !== 'string') return false return name === sanitize(name.trim()) } @@ -107,8 +106,7 @@ class Keychain { } this.store = store - const opts = {} - deepmerge(opts, defaultOptions, options) + const opts = mergeOptions(defaultOptions, options) // Enforce NIST SP 800-132 if (!opts.passPhrase || opts.passPhrase.length < 20) { @@ -186,11 +184,11 @@ class Keychain { return _error(callback, `Invalid key name '${name}'`) } - if (!isString(type)) { + if (typeof type !== 'string') { return _error(callback, `Invalid key type '${type}'`) } - if (!isSafeInteger(size)) { + if (!Number.isSafeInteger(size)) { return _error(callback, `Invalid key size '${size}'`) } @@ -246,7 +244,7 @@ class Keychain { } pull( self.store.query(query), - pull.collect((err, res) => { + collect((err, res) => { if (err) return _error(callback, err) const info = res.map(r => JSON.parse(r.value)) diff --git a/src/util.js b/src/util.js index 9aa248f..bc61c5b 100644 --- a/src/util.js +++ b/src/util.js @@ -1,6 +1,7 @@ 'use strict' -const forge = require('node-forge') +require('node-forge/lib/x509') +const forge = require('node-forge/lib/forge') const pki = forge.pki exports = module.exports diff --git a/test/browser.js b/test/browser.js index 374ce6d..0a37bed 100644 --- a/test/browser.js +++ b/test/browser.js @@ -1,7 +1,7 @@ /* eslint-env mocha */ 'use strict' -const async = require('async') +const series = require('async/series') const LevelStore = require('datastore-level') describe('browser', () => { @@ -9,14 +9,14 @@ describe('browser', () => { const datastore2 = new LevelStore('test-keystore-2', { db: require('level-js') }) before((done) => { - async.series([ + series([ (cb) => datastore1.open(cb), (cb) => datastore2.open(cb) ], done) }) after((done) => { - async.series([ + series([ (cb) => datastore1.close(cb), (cb) => datastore2.close(cb) ], done) diff --git a/test/node.js b/test/node.js index 6ca293e..e11d074 100644 --- a/test/node.js +++ b/test/node.js @@ -4,7 +4,7 @@ const os = require('os') const path = require('path') const rimraf = require('rimraf') -const async = require('async') +const series = require('async/series') const FsStore = require('datastore-fs') describe('node', () => { @@ -14,14 +14,14 @@ describe('node', () => { const datastore2 = new FsStore(store2) before((done) => { - async.series([ + series([ (cb) => datastore1.open(cb), (cb) => datastore2.open(cb) ], done) }) after((done) => { - async.series([ + series([ (cb) => datastore1.close(cb), (cb) => datastore2.close(cb), (cb) => rimraf(store1, cb),