From 87dff04511605aa3d3b2379a627116f8f9d7eff2 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 15 Mar 2019 09:57:35 +0000 Subject: [PATCH] perf: reduce bundle size (#915) --- .aegir.js | 1 + .travis.yml | 11 ++----- README.md | 7 ++-- package.json | 49 +++++++++++++--------------- src/config/replace.js | 13 ++++++-- src/files-mfs/ls-readable-stream.js | 2 +- src/files-mfs/stat.js | 4 +-- src/files-regular/add-from-url.js | 6 ++-- src/files-regular/add.js | 3 +- src/files-regular/get-pull-stream.js | 5 +-- src/files-regular/ls-pull-stream.js | 4 +-- src/pin/ls.js | 3 +- src/stats/bw-pull-stream.js | 5 +-- src/utils/multipart.js | 6 ++-- src/utils/request.js | 12 ------- src/utils/send-files-stream.js | 2 +- src/utils/send-request.js | 4 +-- test/files-mfs.spec.js | 14 ++++---- test/ping.spec.js | 2 +- 19 files changed, 72 insertions(+), 81 deletions(-) delete mode 100644 src/utils/request.js diff --git a/.aegir.js b/.aegir.js index 51aedf024..6effabad0 100644 --- a/.aegir.js +++ b/.aegir.js @@ -5,6 +5,7 @@ const createServer = require('ipfsd-ctl').createServer const server = createServer() module.exports = { + bundlesize: { maxSize: '231kB' }, webpack: { resolve: { mainFields: ['browser', 'main'] diff --git a/.travis.yml b/.travis.yml index 12b8e9494..912ef7ac7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,6 +23,7 @@ jobs: - stage: check script: + - npx aegir build --bundlesize - npx aegir commitlint --travis - npx aegir dep-check - npm run lint @@ -31,19 +32,13 @@ jobs: name: chrome addons: chrome: stable - script: npx aegir test -t browser + script: npx aegir test -t browser -t webworker - stage: test name: firefox addons: firefox: latest - script: npx aegir test -t browser -- --browsers FirefoxHeadless - - - stage: test - name: webworker - addons: - chrome: stable - script: npx aegir test -t webworker + script: npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless notifications: email: false diff --git a/README.md b/README.md index 817ccdb78..a1ef73530 100644 --- a/README.md +++ b/README.md @@ -9,18 +9,19 @@ - +

- + +
- +

diff --git a/package.json b/package.json index 0e58febec..2a4449e0a 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,7 @@ "browser": { "glob": false, "fs": false, - "stream": "readable-stream", - "http": "stream-http" + "stream": "readable-stream" }, "scripts": { "test": "aegir test", @@ -20,17 +19,15 @@ "release": "aegir release ", "release-minor": "aegir release --type minor ", "release-major": "aegir release --type major ", - "coverage": "aegir coverage --timeout 100000", - "coverage-publish": "aegir coverage --provider coveralls --timeout 100000", - "dep-check": "npx dependency-check package.json './test/**/*.js' './src/**/*.js'" + "coverage": "npx nyc -r html npm run test:node -- --bail" }, "dependencies": { "async": "^2.6.1", "bignumber.js": "^8.0.2", - "bl": "^2.1.2", + "bl": "^3.0.0", "bs58": "^4.0.1", "cids": "~0.5.5", - "concat-stream": "^2.0.0", + "concat-stream": "hugomrdias/concat-stream#feat/smaller", "debug": "^4.1.0", "detect-node": "^2.0.4", "end-of-stream": "^1.4.1", @@ -38,35 +35,34 @@ "flatmap": "0.0.3", "glob": "^7.1.3", "ipfs-block": "~0.8.0", - "ipfs-unixfs": "~0.1.16", - "ipld-dag-cbor": "~0.13.0", - "ipld-dag-pb": "~0.15.0", - "is-ipfs": "~0.4.7", + "ipld-dag-cbor": "~0.13.1", + "ipld-dag-pb": "~0.15.3", + "is-ipfs": "~0.6.0", "is-pull-stream": "0.0.0", "is-stream": "^1.1.0", - "libp2p-crypto": "~0.16.0", - "lodash": "^4.17.11", + "iso-stream-http": "~0.1.1", + "iso-url": "~0.4.6", + "just-kebab-case": "^1.1.0", + "just-map-keys": "^1.1.0", "lru-cache": "^5.1.1", - "multiaddr": "^6.0.0", + "multiaddr": "^6.0.6", "multibase": "~0.6.0", "multicodec": "~0.5.0", "multihashes": "~0.4.14", - "ndjson": "^1.5.0", + "ndjson": "hugomrdias/ndjson#feat/readable-stream3", "once": "^1.4.0", - "peer-id": "~0.12.1", - "peer-info": "~0.15.0", + "peer-id": "~0.12.2", + "peer-info": "~0.15.1", "promisify-es6": "^1.0.3", "pull-defer": "~0.2.3", - "pull-pushable": "^2.2.0", - "pull-stream-to-stream": "^1.3.4", + "pull-stream": "^3.6.9", + "pull-to-stream": "~0.1.0", "pump": "^3.0.0", "qs": "^6.5.2", - "readable-stream": "^3.0.6", - "stream-http": "^3.0.0", + "readable-stream": "^3.1.1", "stream-to-pull-stream": "^1.7.2", - "streamifier": "~0.1.1", - "tar-stream": "^1.6.2", - "through2": "^3.0.0" + "tar-stream": "^2.0.1", + "through2": "^3.0.1" }, "engines": { "node": ">=10.0.0", @@ -77,7 +73,7 @@ "url": "https://github.com/ipfs/js-ipfs-http-client" }, "devDependencies": { - "aegir": "^18.0.2", + "aegir": "^18.2.1", "browser-process-platform": "~0.1.1", "chai": "^4.2.0", "cross-env": "^5.2.0", @@ -87,7 +83,6 @@ "interface-ipfs-core": "~0.99.0", "ipfsd-ctl": "~0.42.0", "nock": "^10.0.2", - "pull-stream": "^3.6.9", "stream-equal": "^1.1.1" }, "keywords": [ @@ -182,4 +177,4 @@ "url": "https://github.com/ipfs/js-ipfs-http-client/issues" }, "homepage": "https://github.com/ipfs/js-ipfs-http-client" -} \ No newline at end of file +} diff --git a/src/config/replace.js b/src/config/replace.js index 24f9f0b00..971c61ee0 100644 --- a/src/config/replace.js +++ b/src/config/replace.js @@ -1,14 +1,23 @@ 'use strict' -const streamifier = require('streamifier') +const { Readable } = require('readable-stream') const promisify = require('promisify-es6') const SendOneFile = require('../utils/send-one-file') +function toStream (input) { + return new Readable({ + read () { + this.push(input) + this.push(null) + } + }) +} + module.exports = (send) => { const sendOneFile = SendOneFile(send, 'config/replace') return promisify((config, callback) => { if (typeof config === 'object') { - config = streamifier.createReadStream(Buffer.from(JSON.stringify(config))) + config = toStream(Buffer.from(JSON.stringify(config))) } sendOneFile(config, {}, callback) diff --git a/src/files-mfs/ls-readable-stream.js b/src/files-mfs/ls-readable-stream.js index 2e0ea1a50..404893e70 100644 --- a/src/files-mfs/ls-readable-stream.js +++ b/src/files-mfs/ls-readable-stream.js @@ -3,7 +3,7 @@ const { Transform, PassThrough -} = require('stream') +} = require('readable-stream') const pump = require('pump') const ndjson = require('ndjson') const isStream = require('is-stream') diff --git a/src/files-mfs/stat.js b/src/files-mfs/stat.js index a205fea8e..7274e32a1 100644 --- a/src/files-mfs/stat.js +++ b/src/files-mfs/stat.js @@ -1,8 +1,8 @@ 'use strict' const promisify = require('promisify-es6') -const mapKeys = require('lodash/mapKeys') -const kebabCase = require('lodash/kebabCase') +const mapKeys = require('just-map-keys') +const kebabCase = require('just-kebab-case') const transform = function (data, callback) { callback(null, { diff --git a/src/files-regular/add-from-url.js b/src/files-regular/add-from-url.js index e926a7c81..f1065f2de 100644 --- a/src/files-regular/add-from-url.js +++ b/src/files-regular/add-from-url.js @@ -1,8 +1,8 @@ 'use strict' const promisify = require('promisify-es6') -const { URL } = require('url') -const request = require('../utils/request') +const { URL } = require('iso-url') +const { getRequest } = require('iso-stream-http') const SendOneFile = require('../utils/send-one-file-multiple-results') const FileResultStreamConverter = require('../utils/file-result-stream-converter') @@ -37,7 +37,7 @@ const validUrl = (url) => typeof url === 'string' && url.startsWith('http') const requestWithRedirect = (url, opts, sendOneFile, callback) => { const parsedUrl = new URL(url) - const req = request(parsedUrl.protocol)(url, (res) => { + const req = getRequest(parsedUrl, (res) => { if (res.statusCode >= 400) { return callback(new Error(`Failed to download with ${res.statusCode}`)) } diff --git a/src/files-regular/add.js b/src/files-regular/add.js index 97fe3a5e5..c5506b48b 100644 --- a/src/files-regular/add.js +++ b/src/files-regular/add.js @@ -4,7 +4,6 @@ const promisify = require('promisify-es6') const ConcatStream = require('concat-stream') const once = require('once') const isStream = require('is-stream') -const isString = require('lodash/isString') const isSource = require('is-pull-stream').isSource const FileResultStreamConverter = require('../utils/file-result-stream-converter') const SendFilesStream = require('../utils/send-files-stream') @@ -33,7 +32,7 @@ module.exports = (send) => { // 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) diff --git a/src/files-regular/get-pull-stream.js b/src/files-regular/get-pull-stream.js index c7d8aa7a4..4b2ede770 100644 --- a/src/files-regular/get-pull-stream.js +++ b/src/files-regular/get-pull-stream.js @@ -3,7 +3,8 @@ const cleanCID = require('../utils/clean-cid') const TarStreamToObjects = require('../utils/tar-stream-to-objects') const v = require('is-ipfs') -const pull = require('pull-stream') +const pull = require('pull-stream/pull') +const map = require('pull-stream/throughs/map') const toPull = require('stream-to-pull-stream') const deferred = require('pull-defer') @@ -30,7 +31,7 @@ module.exports = (send) => { p.resolve( pull( toPull.source(stream), - pull.map(file => { + map(file => { const { path, content } = file return content ? { path, content: toPull.source(content) } : file }) diff --git a/src/files-regular/ls-pull-stream.js b/src/files-regular/ls-pull-stream.js index c82d54f62..d1e238df0 100644 --- a/src/files-regular/ls-pull-stream.js +++ b/src/files-regular/ls-pull-stream.js @@ -1,7 +1,7 @@ 'use strict' const moduleConfig = require('../utils/module-config') -const pull = require('pull-stream') +const values = require('pull-stream/sources/values') const deferred = require('pull-defer') const IsIpfs = require('is-ipfs') const cleanCID = require('../utils/clean-cid') @@ -54,7 +54,7 @@ module.exports = (arg) => { type: typeOf(link) })) - p.resolve(pull.values(result)) + p.resolve(values(result)) }) return p diff --git a/src/pin/ls.js b/src/pin/ls.js index 6348dfe89..c8ad26a4b 100644 --- a/src/pin/ls.js +++ b/src/pin/ls.js @@ -1,7 +1,6 @@ 'use strict' const promisify = require('promisify-es6') -const keys = require('lodash/keys') module.exports = (send) => { return promisify((hash, opts, callback) => { @@ -26,7 +25,7 @@ module.exports = (send) => { if (err) { return callback(err) } - callback(null, keys(res.Keys).map(hash => ( + callback(null, Object.keys(res.Keys).map(hash => ( { hash, type: res.Keys[hash].Type } ))) }) diff --git a/src/stats/bw-pull-stream.js b/src/stats/bw-pull-stream.js index f302a3a5f..26a4eb311 100644 --- a/src/stats/bw-pull-stream.js +++ b/src/stats/bw-pull-stream.js @@ -1,7 +1,8 @@ 'use strict' const toPull = require('stream-to-pull-stream') -const pull = require('pull-stream') +const map = require('pull-stream/throughs/map') +const pull = require('pull-stream/pull') const transformChunk = require('./bw-util') const deferred = require('pull-defer') @@ -21,7 +22,7 @@ module.exports = (send) => { p.resolve(pull( toPull.source(stream), - pull.map(transformChunk) + map(transformChunk) )) }) diff --git a/src/utils/multipart.js b/src/utils/multipart.js index a7096eefd..22006c435 100644 --- a/src/utils/multipart.js +++ b/src/utils/multipart.js @@ -1,9 +1,9 @@ 'use strict' -const Transform = require('stream').Transform +const Transform = require('readable-stream').Transform const isNode = require('detect-node') const isSource = require('is-pull-stream').isSource -const toStream = require('pull-stream-to-stream') +const toStream = require('pull-to-stream') const PADDING = '--' const NEW_LINE = '\r\n' @@ -78,7 +78,7 @@ class Multipart extends Transform { } if (isSource(content)) { - content = toStream.source(content) + content = toStream.readable(content) } // From now on we assume content is a stream diff --git a/src/utils/request.js b/src/utils/request.js deleted file mode 100644 index d81edff7c..000000000 --- a/src/utils/request.js +++ /dev/null @@ -1,12 +0,0 @@ -'use strict' - -const http = require('http') -const https = require('https') - -module.exports = (protocol) => { - if (protocol.indexOf('https') === 0) { - return https.request - } - - return http.request -} diff --git a/src/utils/send-files-stream.js b/src/utils/send-files-stream.js index aff2cdcc9..c2450d39e 100644 --- a/src/utils/send-files-stream.js +++ b/src/utils/send-files-stream.js @@ -1,6 +1,6 @@ 'use strict' -const Duplex = require('stream').Duplex +const { Duplex } = require('readable-stream') const eachSeries = require('async/eachSeries') const isStream = require('is-stream') const once = require('once') diff --git a/src/utils/send-request.js b/src/utils/send-request.js index 16351bb39..219327adf 100644 --- a/src/utils/send-request.js +++ b/src/utils/send-request.js @@ -6,9 +6,9 @@ const isNode = require('detect-node') const ndjson = require('ndjson') const pump = require('pump') const once = require('once') +const { getRequest } = require('iso-stream-http') const streamToValue = require('./stream-to-value') const streamToJsonValue = require('./stream-to-json-value') -const request = require('./request') const log = require('debug')('ipfs-http-client:request') // -- Internal @@ -171,7 +171,7 @@ function requestAPI (config, options, callback) { protocol: `${config.protocol}:` } - const req = request(config.protocol)(reqOptions, onRes(options.buffer, callback)) + const req = getRequest(reqOptions, onRes(options.buffer, callback)) req.on('error', (err) => { callback(err) diff --git a/test/files-mfs.spec.js b/test/files-mfs.spec.js index b550105a6..f88e37ba9 100644 --- a/test/files-mfs.spec.js +++ b/test/files-mfs.spec.js @@ -9,7 +9,9 @@ chai.use(dirtyChai) const loadFixture = require('aegir/fixtures') const mh = require('multihashes') const CID = require('cids') -const pull = require('pull-stream') +const values = require('pull-stream/sources/values') +const pull = require('pull-stream/pull') +const collect = require('pull-stream/sinks/collect') const ipfsClient = require('../src') const f = require('./utils/factory') @@ -280,9 +282,9 @@ describe('.files (the MFS API part)', function () { const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm' pull( - pull.values([{ content: pull.values([Buffer.from('test')]) }]), + values([{ content: values([Buffer.from('test')]) }]), ipfs.addPullStream(), - pull.collect((err, res) => { + collect((err, res) => { expect(err).to.not.exist() expect(res).to.have.length(1) @@ -295,7 +297,7 @@ describe('.files (the MFS API part)', function () { it('.add with pull stream (callback)', (done) => { const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm' - ipfs.add(pull.values([Buffer.from('test')]), (err, res) => { + ipfs.add(values([Buffer.from('test')]), (err, res) => { expect(err).to.not.exist() expect(res).to.have.length(1) @@ -307,7 +309,7 @@ describe('.files (the MFS API part)', function () { it('.add with pull stream (promise)', () => { const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm' - return ipfs.add(pull.values([Buffer.from('test')])) + return ipfs.add(values([Buffer.from('test')])) .then((res) => { expect(res).to.have.length(1) expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 }) @@ -317,7 +319,7 @@ describe('.files (the MFS API part)', function () { it('.add with array of objects with pull stream content', () => { const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm' - return ipfs.add([{ content: pull.values([Buffer.from('test')]) }]) + return ipfs.add([{ content: values([Buffer.from('test')]) }]) .then((res) => { expect(res).to.have.length(1) expect(res[0]).to.eql({ path: expectedCid, hash: expectedCid, size: 12 }) diff --git a/test/ping.spec.js b/test/ping.spec.js index c8a856e13..5f6ecd3ea 100644 --- a/test/ping.spec.js +++ b/test/ping.spec.js @@ -3,7 +3,7 @@ const chai = require('chai') const dirtyChai = require('dirty-chai') -const pull = require('pull-stream') +const pull = require('pull-stream/pull') const collect = require('pull-stream/sinks/collect') const expect = chai.expect chai.use(dirtyChai)