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

Commit

Permalink
refactor: pass cidBase arg to core
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <[email protected]>
  • Loading branch information
Alan Shaw committed Sep 19, 2018
1 parent 76a1153 commit 2d4f8d4
Show file tree
Hide file tree
Showing 19 changed files with 322 additions and 111 deletions.
11 changes: 9 additions & 2 deletions src/cli/commands/bitswap/unwant.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'

const print = require('../../utils').print
const multibase = require('multibase')
const { print } = require('../../utils')
const { cidToString } = require('../../../utils/cid')

module.exports = {
command: 'unwant <key>',
Expand All @@ -12,14 +14,19 @@ module.exports = {
alias: 'k',
describe: 'Key to remove from your wantlist',
type: 'string'
},
'cid-base': {
describe: 'Number base to display CIDs in.',
type: 'string',
choices: multibase.names
}
},
handler (argv) {
argv.ipfs.bitswap.unwant(argv.key, (err) => {
if (err) {
throw err
}
print(`Key ${argv.key} removed from wantlist`)
print(`Key ${cidToString(argv.key, argv.cidBase)} removed from wantlist`)
})
}
}
4 changes: 1 addition & 3 deletions src/cli/commands/bitswap/wantlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ module.exports = {
if (err) {
throw err
}
cids.forEach((cid) => {
print(cid)
})
cids.forEach((cid) => print(cid))
})
}
}
4 changes: 2 additions & 2 deletions src/cli/commands/files/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const waterfall = require('async/waterfall')
const mh = require('multihashes')
const multibase = require('multibase')
const { print, isDaemonOn, createProgressBar } = require('../../utils')
const { cidToString } = require('../../../utils/cid')

function checkPath (inPath, recursive) {
// This function is to check for the following possible inputs
Expand Down Expand Up @@ -90,7 +89,7 @@ function addPipeline (index, addStream, list, argv) {
sortBy(added, 'path')
.reverse()
.map((file) => {
const log = [ 'added', cidToString(file.hash, argv.cidBase) ]
const log = [ 'added', file.hash ]

if (!quiet && file.path.length > 0) log.push(file.path)

Expand Down Expand Up @@ -199,6 +198,7 @@ module.exports = {
? argv.shardSplitThreshold
: Infinity,
cidVersion: argv.cidVersion,
cidBase: argv.cidBase,
rawLeaves: argv.rawLeaves,
onlyHash: argv.onlyHash,
hashAlg: argv.hash,
Expand Down
5 changes: 1 addition & 4 deletions src/cli/commands/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const multibase = require('multibase')
const { print, rightpad } = require('../utils')
const { cidToString } = require('../../utils/cid')

module.exports = {
command: 'ls <key>',
Expand Down Expand Up @@ -35,13 +34,11 @@ module.exports = {
},

handler (argv) {
argv.ipfs.ls(argv.key, { recursive: argv.recursive }, (err, links) => {
argv.ipfs.ls(argv.key, { recursive: argv.recursive, cidBase: argv.cidBase }, (err, links) => {
if (err) {
throw err
}

links = links.map(l => Object.assign(l, { hash: cidToString(l.hash, argv.cidBase) }))

if (argv.headers) {
links = [{hash: 'Hash', size: 'Size', name: 'Name'}].concat(links)
}
Expand Down
8 changes: 4 additions & 4 deletions src/cli/commands/pin/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const multibase = require('multibase')
const { print } = require('../../utils')
const { cidToString } = require('../../../utils/cid')

module.exports = {
command: 'add <ipfsPath...>',
Expand All @@ -24,12 +23,13 @@ module.exports = {
},

handler (argv) {
const recursive = argv.recursive
const { recursive, cidBase } = argv
const type = recursive ? 'recursive' : 'direct'
argv.ipfs.pin.add(argv.ipfsPath, { recursive: recursive }, (err, results) => {

argv.ipfs.pin.add(argv.ipfsPath, { recursive, cidBase }, (err, results) => {
if (err) { throw err }
results.forEach((res) => {
print(`pinned ${cidToString(res.hash, argv.cidBase)} ${type}ly`)
print(`pinned ${res.hash} ${type}ly`)
})
})
}
Expand Down
8 changes: 3 additions & 5 deletions src/cli/commands/pin/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const multibase = require('multibase')
const { print } = require('../../utils')
const { cidToString } = require('../../../utils/cid')

module.exports = {
// bracket syntax with '...' tells yargs to optionally accept a list
Expand Down Expand Up @@ -33,13 +32,12 @@ module.exports = {

handler: (argv) => {
const paths = argv.ipfsPath
const type = argv.type
const quiet = argv.quiet
const { type, quiet, cidBase } = argv

argv.ipfs.pin.ls(paths, { type }, (err, results) => {
argv.ipfs.pin.ls(paths, { type, cidBase }, (err, results) => {
if (err) { throw err }
results.forEach((res) => {
let line = cidToString(res.hash, argv.cidBase)
let line = res.hash
if (!quiet) {
line += ` ${res.type}`
}
Expand Down
5 changes: 3 additions & 2 deletions src/cli/commands/pin/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ module.exports = {
},

handler: (argv) => {
const recursive = argv.recursive
argv.ipfs.pin.rm(argv.ipfsPath, { recursive: recursive }, (err, results) => {
const { recursive, cidBase } = argv

argv.ipfs.pin.rm(argv.ipfsPath, { recursive, cidBase }, (err, results) => {
if (err) { throw err }
results.forEach((res) => {
print(`unpinned ${cidToString(res.hash, argv.cidBase)}`)
Expand Down
6 changes: 2 additions & 4 deletions src/cli/commands/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ module.exports = {
},

handler (argv) {
argv.ipfs.resolve(argv.name, {
recursive: argv.recursive,
cidBase: argv.cidBase
}, (err, res) => {
const { recursive, cidBase } = argv
argv.ipfs.resolve(argv.name, { recursive, cidBase }, (err, res) => {
if (err) throw err
print(res)
})
Expand Down
26 changes: 22 additions & 4 deletions src/core/components/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ const Big = require('big.js')
const CID = require('cids')
const PeerId = require('peer-id')
const errCode = require('err-code')
const multibase = require('multibase')
const { cidToString } = require('../../utils/cid')

function wantlistToCidArray (list) {
return Array.from(list).map((e) => e[1].cid)
function formatWantlist (list, cidBase) {
return Array.from(list).map((e) => cidToString(e[1].cid, cidBase))
}

module.exports = function bitswap (self) {
Expand All @@ -24,10 +26,18 @@ module.exports = function bitswap (self) {
options = {}
}

options = options || {}

if (!self.isOnline()) {
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
}

if (options.cidBase && !multibase.names.includes(options.cidBase)) {
return setImmediate(() => {
callback(errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE'))
})
}

let list
if (peerId) {
try {
Expand All @@ -43,7 +53,7 @@ module.exports = function bitswap (self) {
list = self._bitswap.getWantlist()
}

setImmediate(() => callback(null, wantlistToCidArray(list)))
setImmediate(() => callback(null, formatWantlist(list, options.cidBase)))
}),

stat: promisify((options, callback) => {
Expand All @@ -52,17 +62,25 @@ module.exports = function bitswap (self) {
options = {}
}

options = options || {}

if (!self.isOnline()) {
return setImmediate(() => callback(new Error(OFFLINE_ERROR)))
}

if (options.cidBase && !multibase.names.includes(options.cidBase)) {
return setImmediate(() => {
callback(errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE'))
})
}

const snapshot = self._bitswap.stat().snapshot

setImmediate(() => {
callback(null, {
provideBufLen: parseInt(snapshot.providesBufferLength.toString()),
blocksReceived: new Big(snapshot.blocksReceived),
wantlist: wantlistToCidArray(self._bitswap.getWantlist()),
wantlist: formatWantlist(self._bitswap.getWantlist(), options.cidBase),
peers: self._bitswap.peers().map((id) => id.toB58String()),
dupBlksReceived: new Big(snapshot.dupBlksReceived),
dupDataReceived: new Big(snapshot.dupDataReceived),
Expand Down
10 changes: 9 additions & 1 deletion src/core/components/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const waterfall = require('async/waterfall')
const setImmediate = require('async/setImmediate')
const promisify = require('promisify-es6')
const errCode = require('err-code')
const multibase = require('multibase')
const { cidToString } = require('../../utils/cid')

module.exports = function block (self) {
return {
Expand Down Expand Up @@ -102,6 +104,12 @@ module.exports = function block (self) {
return setImmediate(() => callback(errCode(err, 'ERR_INVALID_CID')))
}

if (options.cidBase && !multibase.names.includes(options.cidBase)) {
return setImmediate(() => {
callback(errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE'))
})
}

if (options.preload !== false) {
self._preload(cid)
}
Expand All @@ -111,7 +119,7 @@ module.exports = function block (self) {
return callback(err)
}
callback(null, {
key: cid,
key: cidToString(cid, options.cidBase),
size: block.data.length
})
})
Expand Down
33 changes: 21 additions & 12 deletions src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const OtherBuffer = require('buffer').Buffer
const CID = require('cids')
const toB58String = require('multihashes').toB58String
const errCode = require('err-code')
const multibase = require('multibase')
const parseChunkerString = require('../utils').parseChunkerString
const { cidToString } = require('../../utils/cid')

const WRAPPER = 'wrapper/'

Expand All @@ -32,11 +34,13 @@ function prepareFile (file, opts) {
cid = cid.toV1()
}

const cidStr = cidToString(cid, opts.cidBase)

return {
path: opts.wrapWithDirectory
? file.path.substring(WRAPPER.length)
: (file.path || cid.toBaseEncodedString()),
hash: cid,
: (file.path || cidStr),
hash: cidStr,
size: file.size
}
}
Expand Down Expand Up @@ -141,19 +145,28 @@ class AddHelper extends Duplex {
}

module.exports = function files (self) {
function _addPullStream (options = {}) {
function _addPullStream (options) {
options = options || {}

let chunkerOptions
try {
chunkerOptions = parseChunkerString(options.chunker)
} catch (err) {
return pull.map(() => { throw err })
}

const opts = Object.assign({}, {
shardSplitThreshold: self._options.EXPERIMENTAL.sharding
? 1000
: Infinity
}, options, chunkerOptions)

if (opts.cidBase && !multibase.names.includes(opts.cidBase)) {
return pull.map(() => {
throw errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE')
})
}

if (opts.hashAlg && opts.cidVersion !== 1) {
opts.cidVersion = 1
}
Expand Down Expand Up @@ -228,6 +241,10 @@ module.exports = function files (self) {
const maxDepth = recursive ? global.Infinity : pathDepth
options.maxDepth = options.maxDepth || maxDepth

if (options.cidBase && !multibase.names.includes(options.cidBase)) {
return pull.error(errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE'))
}

if (options.preload !== false) {
self._preload(pathComponents[0])
}
Expand All @@ -238,10 +255,7 @@ module.exports = function files (self) {
recursive ? node.depth >= pathDepth : node.depth === pathDepth
),
pull.map(node => {
// TODO: is this check required - is node.hash from exporter always a CID now?
if (!CID.isCID(node.hash)) {
node = Object.assign({}, node, { hash: new CID(node.hash) })
}
node = Object.assign({}, node, { hash: cidToString(node.hash, options.cidBase) })
delete node.content
return node
})
Expand Down Expand Up @@ -269,11 +283,6 @@ module.exports = function files (self) {
return callback(new Error('first arg must be a buffer, readable stream, pull stream, an object or array of objects'))
}

// CID v0 is for multihashes encoded with sha2-256
if (options.hashAlg && options.cidVersion !== 1) {
options.cidVersion = 1
}

pull(
pull.values([data]),
_addPullStream(options),
Expand Down
14 changes: 11 additions & 3 deletions src/core/components/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const CID = require('cids')
const mh = require('multihashes')
const Unixfs = require('ipfs-unixfs')
const errCode = require('err-code')
const multibase = require('multibase')
const { cidToString } = require('../../utils/cid')

function normalizeMultihash (multihash, enc) {
if (typeof multihash === 'string') {
Expand Down Expand Up @@ -292,6 +294,14 @@ module.exports = function object (self) {
options = {}
}

options = options || {}

if (options.cidBase && !multibase.names.includes(options.cidBase)) {
return setImmediate(() => {
callback(errCode(new Error('invalid multibase'), 'ERR_INVALID_MULTIBASE'))
})
}

self.object.get(multihash, options, (err, node) => {
if (err) {
return callback(err)
Expand All @@ -305,10 +315,8 @@ module.exports = function object (self) {
const blockSize = serialized.length
const linkLength = node.links.reduce((a, l) => a + l.size, 0)

const nodeJSON = node.toJSON()

callback(null, {
Hash: nodeJSON.multihash,
Hash: cidToString(node.multihash, options.cidBase),
NumLinks: node.links.length,
BlockSize: blockSize,
LinksSize: blockSize - node.data.length,
Expand Down
Loading

0 comments on commit 2d4f8d4

Please sign in to comment.