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

Commit

Permalink
fix: many fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alanshaw committed Jan 14, 2020
1 parent fadff95 commit af678ae
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 13 additions & 19 deletions src/cli/commands/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand All @@ -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)
}
})())
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const mapFile = (file, options) => {
output.type = 'file'

if (options.includeContent) {
output.content = file.content
output.content = file.content()
}
}

Expand Down
34 changes: 16 additions & 18 deletions src/http/api/resources/files-regular.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 4 additions & 7 deletions src/http/api/resources/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}, {})
})
}
}
Expand Down
9 changes: 2 additions & 7 deletions test/core/interface.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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'
}]
Expand Down
9 changes: 2 additions & 7 deletions test/http-api/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

0 comments on commit af678ae

Please sign in to comment.