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

Commit

Permalink
fix: reduce bundle size (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias authored and vasco-santos committed Jan 10, 2019
1 parent 4b895cf commit 7eeed87
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ test/test-data/go-ipfs-repo/LOG.old

# while testing npm5
package-lock.json
yarn.lock
29 changes: 0 additions & 29 deletions appveyor.yml

This file was deleted.

14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
19 changes: 12 additions & 7 deletions src/cms.js
Original file line number Diff line number Diff line change
@@ -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')

/**
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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'))
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand Down
18 changes: 8 additions & 10 deletions src/keychain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
Expand All @@ -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())
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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}'`)
}

Expand Down Expand Up @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 3 additions & 3 deletions test/browser.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/* eslint-env mocha */
'use strict'

const async = require('async')
const series = require('async/series')
const LevelStore = require('datastore-level')

describe('browser', () => {
const datastore1 = new LevelStore('test-keystore-1', { db: require('level-js') })
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)
Expand Down
6 changes: 3 additions & 3 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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),
Expand Down

0 comments on commit 7eeed87

Please sign in to comment.