Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
perf: reduce bundle size (#1959)
Browse files Browse the repository at this point in the history
* fix: remove sortby

* fix: swap lodash flatten for just-flatten-it

* fix: remove lodash isString and swap url for iso-url

* fix: use dlv and merge-options

* fix: more dlv, merge-options and remove lodash values

* fix: more libs swaping

* chore: clean up package.json
  • Loading branch information
hugomrdias authored and alanshaw committed Mar 26, 2019
1 parent 66d6c42 commit a3b6235
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 38 deletions.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
"stream-to-promise": "^2.2.0"
},
"dependencies": {
"@nodeutils/defaults-deep": "^1.1.0",
"async": "^2.6.1",
"bignumber.js": "^8.0.2",
"binary-querystring": "~0.1.2",
Expand All @@ -92,6 +91,7 @@
"datastore-core": "~0.6.0",
"datastore-pubsub": "~0.1.1",
"debug": "^4.1.0",
"dlv": "^1.1.2",
"err-code": "^1.1.2",
"file-type": "^10.2.0",
"fnv1a": "^1.0.1",
Expand All @@ -100,7 +100,6 @@
"glob": "^7.1.3",
"hapi": "^18.0.0",
"hapi-pino": "^5.2.0",
"hoek": "^6.1.2",
"human-to-milliseconds": "^1.0.0",
"interface-datastore": "~0.6.0",
"ipfs-bitswap": "~0.23.0",
Expand All @@ -124,9 +123,12 @@
"is-ipfs": "~0.6.0",
"is-pull-stream": "~0.0.0",
"is-stream": "^1.1.0",
"iso-url": "~0.4.6",
"joi": "^14.3.0",
"joi-browser": "^13.4.0",
"joi-multiaddr": "^4.0.0",
"just-flatten-it": "^2.1.0",
"just-safe-set": "^2.1.0",
"libp2p": "~0.25.0-rc.5",
"libp2p-bootstrap": "~0.9.3",
"libp2p-crypto": "~0.16.0",
Expand All @@ -142,6 +144,7 @@
"libp2p-websockets": "~0.12.2",
"lodash": "^4.17.11",
"mafmt": "^6.0.2",
"merge-options": "^1.0.1",
"mime-types": "^2.1.21",
"mkdirp": "~0.5.1",
"multiaddr": "^6.0.0",
Expand All @@ -151,7 +154,6 @@
"multihashes": "~0.4.14",
"multihashing-async": "~0.5.1",
"node-fetch": "^2.3.0",
"once": "^1.4.0",
"peer-book": "~0.9.0",
"peer-id": "~0.12.0",
"peer-info": "~0.15.0",
Expand All @@ -167,7 +169,6 @@
"pull-sort": "^1.0.1",
"pull-stream": "^3.6.9",
"pull-stream-to-stream": "^1.3.4",
"pump": "^3.0.0",
"readable-stream": "^3.1.1",
"receptacle": "^1.3.2",
"stream-to-pull-stream": "^1.7.3",
Expand Down
12 changes: 10 additions & 2 deletions src/cli/commands/add.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const sortBy = require('lodash/sortBy')
const pull = require('pull-stream')
const promisify = require('promisify-es6')
const getFolderSize = promisify(require('get-folder-size'))
Expand Down Expand Up @@ -38,7 +37,16 @@ function addPipeline (source, addStream, options) {
return resolve()
}

sortBy(added, 'path')
added
.sort((a, b) => {
if (a.path > b.path) {
return 1
}
if (a.path < b.path) {
return -1
}
return 0
})
.reverse()
.map((file) => {
const log = options.quiet ? [] : ['added']
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/dag.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const CID = require('cids')
const pull = require('pull-stream')
const mapAsync = require('async/map')
const setImmediate = require('async/setImmediate')
const flattenDeep = require('lodash/flattenDeep')
const flattenDeep = require('just-flatten-it')
const errCode = require('err-code')

module.exports = function dag (self) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/files-regular/add-from-url.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { URL } = require('url')
const { URL } = require('iso-url')
const fetch = require('../../runtime/fetch-nodejs')

module.exports = (self) => {
Expand Down
3 changes: 1 addition & 2 deletions src/core/components/files-regular/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const pull = require('pull-stream')
const sort = require('pull-sort')
const isStream = require('is-stream')
const isSource = require('is-pull-stream').isSource
const isString = require('lodash/isString')

module.exports = function (self) {
const add = promisify((data, options, callback) => {
Expand All @@ -24,7 +23,7 @@ module.exports = function (self) {
// path is optional if content is present
if (obj.content) return isBufferOrStream(obj.content)
// path must be a non-empty string if no content
return Boolean(obj.path) && isString(obj.path)
return Boolean(obj.path) && typeof obj.path === 'string'
}
// An input atom: a buffer, stream or content object
const isInput = obj => isBufferOrStream(obj) || isContentObject(obj)
Expand Down
4 changes: 2 additions & 2 deletions src/core/components/init.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

const peerId = require('peer-id')
const mergeOptions = require('merge-options')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const promisify = require('promisify-es6')
const defaultsDeep = require('@nodeutils/defaults-deep')
const defaultConfig = require('../runtime/config-nodejs.js')
const Keychain = require('libp2p-keychain')
const {
Expand Down Expand Up @@ -59,7 +59,7 @@ module.exports = function init (self) {
opts.bits = Number(opts.bits) || 2048
opts.log = opts.log || function () {}

const config = defaultsDeep(self._options.config, defaultConfig())
const config = mergeOptions(defaultConfig(), self._options.config)
let privateKey

waterfall([
Expand Down
6 changes: 3 additions & 3 deletions src/core/components/libp2p.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const get = require('lodash/get')
const defaultsDeep = require('@nodeutils/defaults-deep')
const get = require('dlv')
const mergeOptions = require('merge-options')
const ipnsUtils = require('../ipns/routing/utils')

module.exports = function libp2p (self, config) {
Expand Down Expand Up @@ -107,7 +107,7 @@ function defaultBundle ({ datastore, peerInfo, peerBook, options, config }) {
})
}

const libp2pOptions = defaultsDeep(get(options, 'libp2p', {}), libp2pDefaults)
const libp2pOptions = mergeOptions(libp2pDefaults, get(options, 'libp2p', {}))

// Required inline to reduce startup time
// Note: libp2p-nodejs gets replaced by libp2p-browser when webpacked/browserified
Expand Down
4 changes: 2 additions & 2 deletions src/core/components/pre-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
const waterfall = require('async/waterfall')
const Keychain = require('libp2p-keychain')
const defaultsDeep = require('@nodeutils/defaults-deep')
const mergeOptions = require('merge-options')
const NoKeychain = require('./no-keychain')
/*
* Load stuff from Repo into memory
Expand All @@ -22,7 +22,7 @@ module.exports = function preStart (self) {
return cb(null, config)
}

config = defaultsDeep(self._options.config, config)
config = mergeOptions(config, self._options.config)

self.config.replace(config, (err) => {
if (err) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const series = require('async/series')
const Bitswap = require('ipfs-bitswap')
const get = require('lodash/get')
const get = require('dlv')
const setImmediate = require('async/setImmediate')
const promisify = require('promisify-es6')
const { TieredDatastore } = require('datastore-core')
Expand Down
5 changes: 2 additions & 3 deletions src/core/components/swarm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const promisify = require('promisify-es6')
const values = require('lodash/values')

const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR

Expand All @@ -25,7 +24,7 @@ module.exports = function swarm (self) {

const peers = []

values(self._peerInfoBook.getAll()).forEach((peer) => {
Object.values(self._peerInfoBook.getAll()).forEach((peer) => {
const connectedAddr = peer.isConnected()

if (!connectedAddr) { return }
Expand All @@ -50,7 +49,7 @@ module.exports = function swarm (self) {
return callback(new Error(OFFLINE_ERROR))
}

const peers = values(self._peerInfoBook.getAll())
const peers = Object.values(self._peerInfoBook.getAll())

callback(null, peers)
}),
Expand Down
4 changes: 2 additions & 2 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const multibase = require('multibase')
const multicodec = require('multicodec')
const CID = require('cids')
const debug = require('debug')
const defaultsDeep = require('@nodeutils/defaults-deep')
const mergeOptions = require('merge-options')
const EventEmitter = require('events')

const config = require('./config')
Expand Down Expand Up @@ -78,7 +78,7 @@ class IPFS extends EventEmitter {

options = config.validate(options || {})

this._options = defaultsDeep(options, defaults)
this._options = mergeOptions(defaults, options)

if (options.init === false) {
this._options.init = false
Expand Down
4 changes: 2 additions & 2 deletions src/core/runtime/libp2p-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const SECIO = require('libp2p-secio')
const Bootstrap = require('libp2p-bootstrap')
const KadDHT = require('libp2p-kad-dht')
const libp2p = require('libp2p')
const defaultsDeep = require('@nodeutils/defaults-deep')
const mergeOptions = require('merge-options')
const multiaddr = require('multiaddr')

class Node extends libp2p {
Expand Down Expand Up @@ -62,7 +62,7 @@ class Node extends libp2p {
}
}

super(defaultsDeep(_options, defaults))
super(mergeOptions(defaults, _options))
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/runtime/libp2p-nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const KadDHT = require('libp2p-kad-dht')
const Multiplex = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const libp2p = require('libp2p')
const defaultsDeep = require('@nodeutils/defaults-deep')
const mergeOptions = require('merge-options')
const multiaddr = require('multiaddr')

class Node extends libp2p {
Expand Down Expand Up @@ -65,7 +65,7 @@ class Node extends libp2p {
}
}

super(defaultsDeep(_options, defaults))
super(mergeOptions(defaults, _options))
}
}

Expand Down
13 changes: 8 additions & 5 deletions src/http/api/resources/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict'

const debug = require('debug')
const get = require('lodash/get')
const set = require('lodash/set')
const get = require('dlv')
const set = require('just-safe-set')
const log = debug('ipfs:http-api:config')
log.error = debug('ipfs:http-api:config:error')
const multipart = require('ipfs-multipart')
Expand Down Expand Up @@ -74,11 +74,14 @@ exports.getOrSet = {
}
} else {
// Set the new value of a given key
const updatedConfig = set(originalConfig, key, value)
const result = set(originalConfig, key, value)
if (!result) {
throw Boom.badRequest('Failed to set config value')
}
try {
await ipfs.config.replace(updatedConfig)
await ipfs.config.replace(originalConfig)
} catch (err) {
throw Boom.boomify(err, { message: 'Failed to get config value' })
throw Boom.boomify(err, { message: 'Failed to replace config value' })
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/http/api/resources/pin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

const mapValues = require('lodash/mapValues')
const keyBy = require('lodash/keyBy')
const multibase = require('multibase')
const Joi = require('joi')
const Boom = require('boom')
Expand Down Expand Up @@ -63,10 +61,11 @@ exports.ls = {
}

return h.response({
Keys: mapValues(
keyBy(result, obj => cidToString(obj.hash, { base: request.query['cid-base'] })),
obj => ({ Type: obj.type })
)
Keys: result.reduce((acc, v) => {
const prop = cidToString(v.hash, { base: request.query['cid-base'] })
acc[prop] = { Type: v.type }
return acc
}, {})
})
}
}
Expand Down

0 comments on commit a3b6235

Please sign in to comment.