From af678aea0a7d6590ceefee197a960ed502a77fe9 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 14 Jan 2020 12:48:23 +0000 Subject: [PATCH] fix: many fixes --- package.json | 2 +- src/cli/commands/ls.js | 32 ++++++++++------------- src/core/utils.js | 2 +- src/http/api/resources/files-regular.js | 34 ++++++++++++------------- src/http/api/resources/swarm.js | 11 +++----- test/core/interface.spec.js | 9 ++----- test/http-api/interface.js | 9 ++----- 7 files changed, 39 insertions(+), 60 deletions(-) diff --git a/package.json b/package.json index 3254c76e02..8eda485625 100644 --- a/package.json +++ b/package.json @@ -139,7 +139,7 @@ "libp2p-delegated-content-routing": "^0.4.1", "libp2p-delegated-peer-routing": "^0.4.0", "libp2p-floodsub": "^0.20.0", - "libp2p-gossipsub": "^0.2.0", + "libp2p-gossipsub": "github:ChainSafe/gossipsub-js#fix/bind-is-not-needed", "libp2p-kad-dht": "^0.18.3", "libp2p-keychain": "^0.6.0", "libp2p-mdns": "^0.13.0", diff --git a/src/cli/commands/ls.js b/src/cli/commands/ls.js index d8abbd70a2..d760b0dd76 100644 --- a/src/cli/commands/ls.js +++ b/src/cli/commands/ls.js @@ -55,21 +55,21 @@ module.exports = { const ipfs = await getIpfs() let first = true - const widths = { - cid: 0, - size: 0, - mtime: 0, - mode: 0 + let maxWidths = [] + const getMaxWidths = (...args) => { + maxWidths = args.map((v, i) => Math.max(maxWidths[i] || 0, v.length)) + return maxWidths } const printLink = (mode, mtime, cid, size, name, depth = 0) => { + const widths = getMaxWidths(mode, mtime, cid, size, name) // todo: fix this by resolving https://github.com/ipfs/js-ipfs-unixfs-exporter/issues/24 const padding = Math.max(depth - pathParts.length, 0) print( - rightpad(mode, 11) + - rightpad(mtime || '-', widths.mtime + 1) + - rightpad(cid, widths.cid + 1) + - rightpad(size || '-', widths.size + 1) + + rightpad(mode, widths[0] + 1) + + rightpad(mtime, widths[1] + 1) + + rightpad(cid, widths[2] + 1) + + rightpad(size, widths[3] + 1) + ' '.repeat(padding) + name ) } @@ -78,25 +78,19 @@ module.exports = { const mode = formatMode(link.mode, link.type === 'dir') const mtime = formatMtime(link.mtime) const cid = cidToString(link.cid, { base: cidBase }) + const size = link.size ? String(link.size) : '-' const name = link.type === 'dir' ? `${link.name || ''}/` : link.name - widths.mode = Math.max(widths.mode, mode) - widths.mtime = Math.max(widths.mtime, mtime) - widths.cid = Math.max(widths.cid, cid.length) - widths.size = Math.max(widths.size, String(link.size).length) - if (first) { first = false if (headers) { - widths.mode = Math.max(widths.mode, 'Mode'.length) - widths.mtime = Math.max(widths.mtime, 'Mtime'.length) - widths.cid = Math.max(widths.cid, 'Hash'.length) - widths.size = Math.max(widths.size, 'Size'.length) + // Seed max widths for the first item + getMaxWidths(mode, mtime, cid, size, name) printLink('Mode', 'Mtime', 'Hash', 'Size', 'Name') } } - printLink(mode, mtime, cid, link.size, name, link.depth) + printLink(mode, mtime, cid, size, name, link.depth) } })()) } diff --git a/src/core/utils.js b/src/core/utils.js index 20318ee3ac..b3dd034637 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -158,7 +158,7 @@ const mapFile = (file, options) => { output.type = 'file' if (options.includeContent) { - output.content = file.content + output.content = file.content() } } diff --git a/src/http/api/resources/files-regular.js b/src/http/api/resources/files-regular.js index 3cb2a42938..3987059426 100644 --- a/src/http/api/resources/files-regular.js +++ b/src/http/api/resources/files-regular.js @@ -107,31 +107,29 @@ exports.get = { async handler (request, h) { const { ipfs } = request.server.app const { key } = request.pre.args - const pack = tar.pack() - - let filesArray - try { - filesArray = await all(ipfs.get(key)) - } catch (err) { - throw Boom.boomify(err, { message: 'Failed to get key' }) - } + const pack = tar.pack() pack.entry = promisify(pack.entry.bind(pack)) - Promise - .all(filesArray.map(async file => { - if (!file.content) { - return pack.entry({ name: file.path, type: 'directory' }) + const streamFiles = async () => { + try { + for await (const file of ipfs.get(key)) { + if (file.content) { + const content = await concat(file.content) + pack.entry({ name: file.path, size: file.size }, content.slice()) + } else { + pack.entry({ name: file.path, type: 'directory' }) + } } - const content = await concat(file.content) - return pack.entry({ name: file.path, size: file.size }, content.slice()) - })) - .then(() => pack.finalize()) - .catch(err => { + pack.finalize() + } catch (err) { log.error(err) pack.emit('error', err) pack.destroy() - }) + } + } + + streamFiles() // reply must be called right away so that tar-stream offloads its content // otherwise it will block in large files diff --git a/src/http/api/resources/swarm.js b/src/http/api/resources/swarm.js index 47fbccfcf1..101cee8ba5 100644 --- a/src/http/api/resources/swarm.js +++ b/src/http/api/resources/swarm.js @@ -55,14 +55,11 @@ exports.addrs = { const { ipfs } = request.server.app const peers = await ipfs.swarm.addrs() - const addrs = {} - peers.forEach((peer) => { - addrs[peer.id.toString()] = peer.multiaddrs.toArray() - .map((addr) => addr.toString()) - }) - return h.response({ - Addrs: addrs + Addrs: peers.reduce((addrs, peer) => { + addrs[peer.id.toString()] = peer.addrs.map(a => a.toString()) + return addrs + }, {}) }) } } diff --git a/test/core/interface.spec.js b/test/core/interface.spec.js index ea7b0402a3..d357b34a07 100644 --- a/test/core/interface.spec.js +++ b/test/core/interface.spec.js @@ -4,6 +4,7 @@ const tests = require('interface-ipfs-core') const merge = require('merge-options') const { createFactory } = require('ipfsd-ctl') +const { isNode } = require('ipfs-utils/src/env') const IPFS = require('../../src') /** @typedef { import("ipfsd-ctl").ControllerOptions } ControllerOptions */ @@ -33,13 +34,7 @@ describe('interface-ipfs-core tests', function () { const commonFactory = createFactory(commonOptions, overrides) tests.root(commonFactory, { - skip: isNode ? [{ - name: 'should ignore a directory from the file system', - reason: 'FIXME: unixfs importer returns an extra QmUNLLs dir first (seems to be fixed in 0.42)' - }] : [{ - name: 'should ignore a directory from the file system', - reason: 'FIXME: unixfs importer returns an extra QmUNLLs dir first (seems to be fixed in 0.42)' - }, { + skip: isNode ? null : [{ name: 'should add with mtime as hrtime', reason: 'Not designed to run in the browser' }] diff --git a/test/http-api/interface.js b/test/http-api/interface.js index 8f799bc92a..3ed25a6224 100644 --- a/test/http-api/interface.js +++ b/test/http-api/interface.js @@ -33,12 +33,7 @@ describe('interface-ipfs-core over ipfs-http-client tests', function () { } const commonFactory = createFactory(commonOptions, overrides) - tests.root(commonFactory, { - skip: [{ - name: 'should ignore a directory from the file system', - reason: 'FIXME: unixfs importer returns an extra QmUNLLs dir first (seems to be fixed in 0.42)' - }] - }) + tests.root(commonFactory) tests.bitswap(commonFactory) @@ -113,5 +108,5 @@ describe('interface-ipfs-core over ipfs-http-client tests', function () { tests.stats(commonFactory) - tests.swarm(commonFactory, { skip: true }) + tests.swarm(commonFactory) })