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

Commit

Permalink
refactor: back out of core accepting cidBase
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
  • Loading branch information
Alan Shaw committed Nov 20, 2018
1 parent e804933 commit f29115d
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 130 deletions.
5 changes: 3 additions & 2 deletions src/cli/commands/bitswap/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

module.exports = {
command: 'stat',
Expand All @@ -17,12 +18,12 @@ module.exports = {
},

handler ({ ipfs, cidBase }) {
ipfs.bitswap.stat({ cidBase }, (err, stats) => {
ipfs.bitswap.stat((err, stats) => {
if (err) {
throw err
}

stats.wantlist = stats.wantlist || []
stats.wantlist = stats.wantlist.Keys.map(cid => cidToString(cid, cidBase))
stats.peers = stats.peers || []

print(`bitswap status
Expand Down
5 changes: 3 additions & 2 deletions src/cli/commands/bitswap/wantlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

module.exports = {
command: 'wantlist [peer]',
Expand All @@ -22,11 +23,11 @@ module.exports = {
},

handler ({ ipfs, peer, cidBase }) {
ipfs.bitswap.wantlist(peer, { cidBase }, (err, cids) => {
ipfs.bitswap.wantlist(peer, (err, cids) => {
if (err) {
throw err
}
cids.forEach((cid) => print(cid))
cids.forEach((cid) => print(cidToString(cid, cidBase)))
})
}
}
5 changes: 3 additions & 2 deletions src/cli/commands/block/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

module.exports = {
command: 'stat <key>',
Expand All @@ -17,12 +18,12 @@ module.exports = {
},

handler ({ ipfs, key, cidBase }) {
ipfs.block.stat(key, { cidBase }, (err, stats) => {
ipfs.block.stat(key, (err, stats) => {
if (err) {
throw err
}

print('Key: ' + stats.key)
print('Key: ' + cidToString(stats.key, cidBase))
print('Size: ' + stats.size)
})
}
Expand Down
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,6 +13,7 @@ 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 @@ -89,7 +90,7 @@ function addPipeline (index, addStream, list, argv) {
sortBy(added, 'path')
.reverse()
.map((file) => {
const log = [ 'added', file.hash ]
const log = [ 'added', cidToString(file.hash, argv.cidBase) ]

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

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

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

module.exports = {
command: 'stat <key>',
Expand All @@ -17,15 +18,15 @@ module.exports = {
},

handler ({ ipfs, key, cidBase }) {
ipfs.object.stat(key, { enc: 'base58', cidBase }, (err, stats) => {
ipfs.object.stat(key, { enc: 'base58' }, (err, stats) => {
if (err) {
throw err
}

delete stats.Hash // only for js-ipfs-api output

Object.keys(stats).forEach((key) => {
print(`${key}: ${stats[key]}`)
print(`${key}: ${cidToString(stats[key], cidBase)}`)
})
})
}
Expand Down
5 changes: 3 additions & 2 deletions src/cli/commands/pin/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

module.exports = {
command: 'add <ipfsPath...>',
Expand All @@ -25,10 +26,10 @@ module.exports = {
handler ({ ipfs, ipfsPath, recursive, cidBase }) {
const type = recursive ? 'recursive' : 'direct'

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

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,10 +34,10 @@ module.exports = {
handler: ({ ipfs, ipfsPath, type, quiet, cidBase }) => {
const paths = ipfsPath

ipfs.pin.ls(paths, { type, cidBase }, (err, results) => {
ipfs.pin.ls(paths, { type }, (err, results) => {
if (err) { throw err }
results.forEach((res) => {
let line = res.hash
let line = cidToString(res.hash, cidBase)
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 @@ -2,6 +2,7 @@

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

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

handler: ({ ipfs, ipfsPath, recursive, cidBase }) => {
ipfs.pin.rm(ipfsPath, { recursive, cidBase }, (err, results) => {
ipfs.pin.rm(ipfsPath, { recursive }, (err, results) => {
if (err) { throw err }
results.forEach((res) => {
print(`unpinned ${res.hash}`)
print(`unpinned ${cidToString(res.hash, cidBase)}`)
})
})
}
Expand Down
37 changes: 5 additions & 32 deletions src/core/components/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,23 @@ 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 formatWantlist (list, cidBase) {
return Array.from(list).map((e) => cidToString(e[1].cid, cidBase))
return Array.from(list).map((e) => ({ '/': e[1].cid.toBaseEncodedString() }))
}

module.exports = function bitswap (self) {
return {
wantlist: promisify((peerId, options, callback) => {
wantlist: promisify((peerId, callback) => {
if (typeof peerId === 'function') {
callback = peerId
options = {}
peerId = null
} else if (typeof options === 'function') {
callback = options
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 @@ -53,34 +39,21 @@ module.exports = function bitswap (self) {
list = self._bitswap.getWantlist()
}

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

stat: promisify((options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}

options = options || {}

stat: promisify((callback) => {
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: formatWantlist(self._bitswap.getWantlist(), options.cidBase),
wantlist: formatWantlist(self._bitswap.getWantlist()),
peers: self._bitswap.peers().map((id) => id.toB58String()),
dupBlksReceived: new Big(snapshot.dupBlksReceived),
dupDataReceived: new Big(snapshot.dupDataReceived),
Expand Down
10 changes: 1 addition & 9 deletions src/core/components/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ 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 @@ -118,12 +116,6 @@ 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 @@ -133,7 +125,7 @@ module.exports = function block (self) {
return callback(err)
}
callback(null, {
key: cidToString(cid, options.cidBase),
key: cid.toString(),
size: block.data.length
})
})
Expand Down
49 changes: 26 additions & 23 deletions src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ const pushable = require('pull-pushable')
const toStream = require('pull-stream-to-stream')
const toPull = require('stream-to-pull-stream')
const deferred = require('pull-defer')
const waterfall = require('async/waterfall')
const isStream = require('is-stream')
const isSource = require('is-pull-stream').isSource
const Duplex = require('readable-stream').Duplex
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/'

function noop () {}

function prepareFile (file, opts) {
function prepareFile (file, self, opts, callback) {
opts = opts || {}

let cid = new CID(file.multihash)
Expand All @@ -34,15 +33,28 @@ function prepareFile (file, opts) {
cid = cid.toV1()
}

const cidStr = cidToString(cid, opts.cidBase)
waterfall([
(cb) => opts.onlyHash
? cb(null, file)
: self.object.get(file.multihash, Object.assign({}, opts, { preload: false }), cb),
(node, cb) => {
const b58Hash = cid.toBaseEncodedString()

return {
path: opts.wrapWithDirectory
? file.path.substring(WRAPPER.length)
: (file.path || cidStr),
hash: cidStr,
size: file.size
}
let size = node.size

if (Buffer.isBuffer(node)) {
size = node.length
}

cb(null, {
path: opts.wrapWithDirectory
? file.path.substring(WRAPPER.length)
: (file.path || b58Hash),
hash: b58Hash,
size
})
}
], callback)
}

function normalizeContent (content, opts) {
Expand Down Expand Up @@ -161,12 +173,6 @@ module.exports = function files (self) {
: 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 All @@ -184,7 +190,7 @@ module.exports = function files (self) {
pull.map(content => normalizeContent(content, opts)),
pull.flatten(),
importer(self._ipld, opts),
pull.map(file => prepareFile(file, 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))
)
Expand Down Expand Up @@ -241,10 +247,6 @@ 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 @@ -255,7 +257,8 @@ module.exports = function files (self) {
recursive ? node.depth >= pathDepth : node.depth === pathDepth
),
pull.map(node => {
node = Object.assign({}, node, { hash: cidToString(node.hash, options.cidBase) })
const cid = new CID(node.hash)
node = Object.assign({}, node, { hash: cid.toBaseEncodedString() })
delete node.content
return node
})
Expand Down
Loading

0 comments on commit f29115d

Please sign in to comment.