From 05c10eea1e7aa0966d335a5b552b3524fe274cd8 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 2 May 2019 11:45:04 +0100 Subject: [PATCH 01/11] feat: add support for File DOM API to files-regular --- package.json | 2 ++ .../files-regular/add-pull-stream.js | 10 ++++++-- src/core/components/files-regular/add.js | 23 ++++--------------- src/core/components/files-regular/utils.js | 16 +++++++++---- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 49af52c46a..4c146ef62f 100644 --- a/package.json +++ b/package.json @@ -133,8 +133,10 @@ "is-pull-stream": "~0.0.0", "is-stream": "^2.0.0", "iso-url": "~0.4.6", + "ipfs-utils": "~0.0.3", "just-flatten-it": "^2.1.0", "just-safe-set": "^2.1.0", + "kind-of": "^6.0.2", "libp2p": "~0.25.0", "libp2p-bootstrap": "~0.9.3", "libp2p-crypto": "~0.16.0", diff --git a/src/core/components/files-regular/add-pull-stream.js b/src/core/components/files-regular/add-pull-stream.js index d69d2c8258..78d93ebfe1 100644 --- a/src/core/components/files-regular/add-pull-stream.js +++ b/src/core/components/files-regular/add-pull-stream.js @@ -1,13 +1,16 @@ 'use strict' const importer = require('ipfs-unixfs-importer') +const kindOf = require('kind-of') +const CID = require('cids') const pull = require('pull-stream') const toPull = require('stream-to-pull-stream') const waterfall = require('async/waterfall') const isStream = require('is-stream') -const isSource = require('is-pull-stream').isSource -const CID = require('cids') +const { isSource } = require('is-pull-stream') const { parseChunkerString } = require('./utils') +const streamFromFileReader = require('ipfs-utils/src/streams/stream-from-filereader') +const { supportsFileReader } = require('ipfs-utils/src/supports') const WRAPPER = 'wrapper/' @@ -52,6 +55,9 @@ function normalizeContent (content, opts) { } return content.map((data) => { + if (supportsFileReader && kindOf(content) === 'file') { + data = { path: '', content: toPull.source(streamFromFileReader(content)) } + } // Buffer input if (Buffer.isBuffer(data)) { data = { path: '', content: pull.values([data]) } diff --git a/src/core/components/files-regular/add.js b/src/core/components/files-regular/add.js index 174cf8d51a..d254159b7f 100644 --- a/src/core/components/files-regular/add.js +++ b/src/core/components/files-regular/add.js @@ -3,8 +3,8 @@ const promisify = require('promisify-es6') const pull = require('pull-stream') const sort = require('pull-sort') -const isStream = require('is-stream') const isSource = require('is-pull-stream').isSource +const validateAddInput = require('ipfs-utils/src/files/add-input-validation') module.exports = function (self) { const add = promisify((data, options, callback) => { @@ -15,23 +15,10 @@ module.exports = function (self) { options = options || {} - // Buffer, pull stream or Node.js stream - const isBufferOrStream = obj => Buffer.isBuffer(obj) || isStream.readable(obj) || isSource(obj) - // An object like { content?, path? }, where content isBufferOrStream and path isString - const isContentObject = obj => { - if (typeof obj !== 'object') return false - // 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) && typeof obj.path === 'string' - } - // An input atom: a buffer, stream or content object - const isInput = obj => isBufferOrStream(obj) || isContentObject(obj) - // All is ok if data isInput or data is an array of isInput - const ok = isInput(data) || (Array.isArray(data) && data.every(isInput)) - - if (!ok) { - return callback(new Error('invalid input: expected buffer, readable stream, pull stream, object or array of objects')) + try { + validateAddInput(data) + } catch (err) { + return callback(err) } pull( diff --git a/src/core/components/files-regular/utils.js b/src/core/components/files-regular/utils.js index 8b1241ae76..abda5f6d85 100644 --- a/src/core/components/files-regular/utils.js +++ b/src/core/components/files-regular/utils.js @@ -1,8 +1,9 @@ 'use strict' const CID = require('cids') +const { Buffer } = require('buffer') -exports.normalizePath = (path) => { +const normalizePath = (path) => { if (Buffer.isBuffer(path)) { return new CID(path).toString() } @@ -30,7 +31,7 @@ exports.normalizePath = (path) => { * * @return {Object} Chunker options for DAGBuilder */ -exports.parseChunkerString = (chunker) => { +const parseChunkerString = (chunker) => { if (!chunker) { return { chunker: 'fixed' @@ -67,7 +68,7 @@ exports.parseChunkerString = (chunker) => { * * @return {Object} rabin chunker options */ -function parseRabinString (chunker) { +const parseRabinString = (chunker) => { const options = {} const parts = chunker.split('-') switch (parts.length) { @@ -89,7 +90,7 @@ function parseRabinString (chunker) { return options } -function parseChunkSize (str, name) { +const parseChunkSize = (str, name) => { let size = parseInt(str) if (isNaN(size)) { throw new Error(`Chunker parameter ${name} must be an integer`) @@ -97,3 +98,10 @@ function parseChunkSize (str, name) { return size } + +module.exports = { + normalizePath, + parseChunkSize, + parseRabinString, + parseChunkerString +} From cbff6a8b8c2622ec9b562b0b0b79ad6a882fa6ec Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 2 May 2019 11:55:49 +0100 Subject: [PATCH 02/11] chore: use correct interface tests --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4c146ef62f..ee39fea358 100644 --- a/package.json +++ b/package.json @@ -108,7 +108,7 @@ "glob": "^7.1.3", "hapi-pino": "^6.0.0", "human-to-milliseconds": "^1.0.0", - "interface-datastore": "~0.6.0", + "interface-datastore": "ipfs/interface-js-ipfs-core#support-file-dom-api", "ipfs-bitswap": "~0.23.0", "ipfs-block": "~0.8.0", "ipfs-block-service": "~0.15.1", From 3860b81253692f87ca5f441c4b2da9ed53cfbd23 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 2 May 2019 12:00:39 +0100 Subject: [PATCH 03/11] chore: use correct interface tests --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ee39fea358..7a818878e3 100644 --- a/package.json +++ b/package.json @@ -108,7 +108,7 @@ "glob": "^7.1.3", "hapi-pino": "^6.0.0", "human-to-milliseconds": "^1.0.0", - "interface-datastore": "ipfs/interface-js-ipfs-core#support-file-dom-api", + "interface-datastore": "ipfs/interface-js-ipfs-core#feat/support-file-dom-api", "ipfs-bitswap": "~0.23.0", "ipfs-block": "~0.8.0", "ipfs-block-service": "~0.15.1", From b1a5bda9eacdb70098a4bb7149fe64b380c2241e Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 2 May 2019 15:22:19 +0100 Subject: [PATCH 04/11] chore: fix package declaration cause npm is dumb --- src/core/components/files-regular/add-pull-stream.js | 4 ++-- src/core/components/files-regular/add.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/components/files-regular/add-pull-stream.js b/src/core/components/files-regular/add-pull-stream.js index 78d93ebfe1..182527827c 100644 --- a/src/core/components/files-regular/add-pull-stream.js +++ b/src/core/components/files-regular/add-pull-stream.js @@ -9,8 +9,8 @@ const waterfall = require('async/waterfall') const isStream = require('is-stream') const { isSource } = require('is-pull-stream') const { parseChunkerString } = require('./utils') -const streamFromFileReader = require('ipfs-utils/src/streams/stream-from-filereader') -const { supportsFileReader } = require('ipfs-utils/src/supports') +const streamFromFileReader = require('js-ipfs-utils/src/streams/stream-from-filereader') +const { supportsFileReader } = require('js-ipfs-utils/src/supports') const WRAPPER = 'wrapper/' diff --git a/src/core/components/files-regular/add.js b/src/core/components/files-regular/add.js index d254159b7f..69407fd15e 100644 --- a/src/core/components/files-regular/add.js +++ b/src/core/components/files-regular/add.js @@ -4,7 +4,7 @@ const promisify = require('promisify-es6') const pull = require('pull-stream') const sort = require('pull-sort') const isSource = require('is-pull-stream').isSource -const validateAddInput = require('ipfs-utils/src/files/add-input-validation') +const validateAddInput = require('js-ipfs-utils/src/files/add-input-validation') module.exports = function (self) { const add = promisify((data, options, callback) => { From e10560ab86ba99cb4c245757495658967ab81724 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 2 May 2019 15:42:30 +0100 Subject: [PATCH 05/11] chore: fix deps --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index 7a818878e3..000f53f2e4 100644 --- a/package.json +++ b/package.json @@ -72,12 +72,12 @@ "execa": "^1.0.0", "form-data": "^2.3.3", "hat": "0.0.3", - "interface-ipfs-core": "~0.99.1", "ipfsd-ctl": "~0.42.0", "libp2p-websocket-star": "~0.10.2", "ncp": "^2.0.0", "qs": "^6.5.2", "rimraf": "^2.6.2", + "interface-ipfs-core": "ipfs/interface-js-ipfs-core#feat/support-file-dom-api", "sinon": "^7.3.1", "stream-to-promise": "^2.2.0" }, @@ -108,7 +108,6 @@ "glob": "^7.1.3", "hapi-pino": "^6.0.0", "human-to-milliseconds": "^1.0.0", - "interface-datastore": "ipfs/interface-js-ipfs-core#feat/support-file-dom-api", "ipfs-bitswap": "~0.23.0", "ipfs-block": "~0.8.0", "ipfs-block-service": "~0.15.1", From d89cc879f1fa402b9a6f1e625832ff6c5f865030 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 2 May 2019 16:02:10 +0100 Subject: [PATCH 06/11] chore: fix deps --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 000f53f2e4..83109ce544 100644 --- a/package.json +++ b/package.json @@ -108,6 +108,7 @@ "glob": "^7.1.3", "hapi-pino": "^6.0.0", "human-to-milliseconds": "^1.0.0", + "interface-datastore": "^0.7.0", "ipfs-bitswap": "~0.23.0", "ipfs-block": "~0.8.0", "ipfs-block-service": "~0.15.1", From 930168953432d10702f896d3b1f4815b87b00ba4 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 2 May 2019 16:09:12 +0100 Subject: [PATCH 07/11] chore: fix deps --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 83109ce544..e1986b520f 100644 --- a/package.json +++ b/package.json @@ -108,7 +108,7 @@ "glob": "^7.1.3", "hapi-pino": "^6.0.0", "human-to-milliseconds": "^1.0.0", - "interface-datastore": "^0.7.0", + "interface-datastore": "~0.7.0", "ipfs-bitswap": "~0.23.0", "ipfs-block": "~0.8.0", "ipfs-block-service": "~0.15.1", From 6dd3f183cce9687763e006a55e6e3d42eaa29b97 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 3 May 2019 10:48:34 +0100 Subject: [PATCH 08/11] fix: use correct input --- .../files-regular/add-pull-stream.js | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/core/components/files-regular/add-pull-stream.js b/src/core/components/files-regular/add-pull-stream.js index 182527827c..1cc1190e70 100644 --- a/src/core/components/files-regular/add-pull-stream.js +++ b/src/core/components/files-regular/add-pull-stream.js @@ -3,7 +3,11 @@ const importer = require('ipfs-unixfs-importer') const kindOf = require('kind-of') const CID = require('cids') -const pull = require('pull-stream') +const pullValues = require('pull-stream/sources/values') +const pullMap = require('pull-stream/throughs/map') +const pullAsyncMap = require('pull-stream/throughs/async-map') +const pullFlatten = require('pull-stream/throughs/flatten') +const pull = require('pull-stream/pull') const toPull = require('stream-to-pull-stream') const waterfall = require('async/waterfall') const isStream = require('is-stream') @@ -55,12 +59,12 @@ function normalizeContent (content, opts) { } return content.map((data) => { - if (supportsFileReader && kindOf(content) === 'file') { - data = { path: '', content: toPull.source(streamFromFileReader(content)) } + if (supportsFileReader && kindOf(data) === 'file') { + data = { path: '', content: toPull.source(streamFromFileReader(data)) } } // Buffer input if (Buffer.isBuffer(data)) { - data = { path: '', content: pull.values([data]) } + data = { path: '', content: pullValues([data]) } } // Readable stream input @@ -74,7 +78,7 @@ function normalizeContent (content, opts) { if (data && data.content && typeof data.content !== 'function') { if (Buffer.isBuffer(data.content)) { - data.content = pull.values([data.content]) + data.content = pullValues([data.content]) } if (isStream.readable(data.content)) { @@ -130,7 +134,7 @@ module.exports = function (self) { try { chunkerOptions = parseChunkerString(options.chunker) } catch (err) { - return pull.map(() => { throw err }) + return pullMap(() => { throw err }) } const opts = Object.assign({}, { shardSplitThreshold: self._options.EXPERIMENTAL.sharding @@ -153,12 +157,12 @@ module.exports = function (self) { opts.progress = progress return pull( - pull.map(content => normalizeContent(content, opts)), - pull.flatten(), + pullMap(content => normalizeContent(content, opts)), + pullFlatten(), importer(self._ipld, opts), - pull.asyncMap((file, cb) => prepareFile(file, self, opts, cb)), - pull.map(file => preloadFile(file, self, opts)), - pull.asyncMap((file, cb) => pinFile(file, self, opts, cb)) + pullAsyncMap((file, cb) => prepareFile(file, self, opts, cb)), + pullMap(file => preloadFile(file, self, opts)), + pullAsyncMap((file, cb) => pinFile(file, self, opts, cb)) ) } } From 6d76cb504df067661abb132f1aa9c7315b3d6a68 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 3 May 2019 11:20:19 +0100 Subject: [PATCH 09/11] chore: add correct http-client --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e1986b520f..b70aee6995 100644 --- a/package.json +++ b/package.json @@ -112,7 +112,7 @@ "ipfs-bitswap": "~0.23.0", "ipfs-block": "~0.8.0", "ipfs-block-service": "~0.15.1", - "ipfs-http-client": "^31.0.0", + "ipfs-http-client": "ipfs/js-ipfs-http-client#feat/support-file-dom-api", "ipfs-http-response": "~0.2.1", "ipfs-mfs": "~0.10.2", "ipfs-multipart": "~0.1.0", From 6e262d21ea841a6d640fb8a82add59a9527ede06 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 17 May 2019 01:27:36 +0200 Subject: [PATCH 10/11] fix: use ipfs-utils --- package.json | 4 ++-- src/core/components/files-regular/add-pull-stream.js | 4 ++-- src/core/components/files-regular/add.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index b70aee6995..3798179526 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "ncp": "^2.0.0", "qs": "^6.5.2", "rimraf": "^2.6.2", - "interface-ipfs-core": "ipfs/interface-js-ipfs-core#feat/support-file-dom-api", + "interface-ipfs-core": "~0.102.0", "sinon": "^7.3.1", "stream-to-promise": "^2.2.0" }, @@ -112,7 +112,7 @@ "ipfs-bitswap": "~0.23.0", "ipfs-block": "~0.8.0", "ipfs-block-service": "~0.15.1", - "ipfs-http-client": "ipfs/js-ipfs-http-client#feat/support-file-dom-api", + "ipfs-http-client": "~31.1.0", "ipfs-http-response": "~0.2.1", "ipfs-mfs": "~0.10.2", "ipfs-multipart": "~0.1.0", diff --git a/src/core/components/files-regular/add-pull-stream.js b/src/core/components/files-regular/add-pull-stream.js index 1cc1190e70..9937b08852 100644 --- a/src/core/components/files-regular/add-pull-stream.js +++ b/src/core/components/files-regular/add-pull-stream.js @@ -13,8 +13,8 @@ const waterfall = require('async/waterfall') const isStream = require('is-stream') const { isSource } = require('is-pull-stream') const { parseChunkerString } = require('./utils') -const streamFromFileReader = require('js-ipfs-utils/src/streams/stream-from-filereader') -const { supportsFileReader } = require('js-ipfs-utils/src/supports') +const streamFromFileReader = require('ipfs-utils/src/streams/stream-from-filereader') +const { supportsFileReader } = require('ipfs-utils/src/supports') const WRAPPER = 'wrapper/' diff --git a/src/core/components/files-regular/add.js b/src/core/components/files-regular/add.js index 69407fd15e..d254159b7f 100644 --- a/src/core/components/files-regular/add.js +++ b/src/core/components/files-regular/add.js @@ -4,7 +4,7 @@ const promisify = require('promisify-es6') const pull = require('pull-stream') const sort = require('pull-sort') const isSource = require('is-pull-stream').isSource -const validateAddInput = require('js-ipfs-utils/src/files/add-input-validation') +const validateAddInput = require('ipfs-utils/src/files/add-input-validation') module.exports = function (self) { const add = promisify((data, options, callback) => { From fe84aab052cf05031d856c66badb866f4f22d507 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 17 May 2019 01:34:49 +0200 Subject: [PATCH 11/11] chore: fix semver lint --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3798179526..99a0a39edd 100644 --- a/package.json +++ b/package.json @@ -112,7 +112,7 @@ "ipfs-bitswap": "~0.23.0", "ipfs-block": "~0.8.0", "ipfs-block-service": "~0.15.1", - "ipfs-http-client": "~31.1.0", + "ipfs-http-client": "^31.1.0", "ipfs-http-response": "~0.2.1", "ipfs-mfs": "~0.10.2", "ipfs-multipart": "~0.1.0",