Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

feat: adds --cid-base argument to stringify cids in different bases #4

Merged
merged 1 commit into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/cli/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,26 @@ module.exports = {
default: false,
coerce: asBoolean,
describe: 'Use long listing format.'
},
cidBase: {
alias: 'cid-base',
default: 'base58btc',
describe: 'CID base to use.'
}
},

handler (argv) {
let {
path,
ipfs,
long
long,
cidBase
} = argv

argv.resolve(
ipfs.files.ls(path || FILE_SEPARATOR, {
long
long,
cidBase
})
.then(files => {
if (long) {
Expand Down
5 changes: 5 additions & 0 deletions src/cli/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ Type: <type>`,
default: false,
coerce: asBoolean,
describe: 'Compute the amount of the dag that is local, and if possible the total size'
},
cidBase: {
alias: 'cid-base',
default: 'base58btc',
describe: 'CID base to use.'
}
},

Expand Down
9 changes: 5 additions & 4 deletions src/core/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

const waterfall = require('async/waterfall')
const map = require('async/map')
const bs58 = require('bs58')
const UnixFs = require('ipfs-unixfs')
const {
traverseTo,
loadNode,
formatCid,
FILE_SEPARATOR,
FILE_TYPES
} = require('./utils')

const defaultOptions = {
long: false
long: false,
cidBase: 'base58btc'
}

module.exports = (ipfs) => {
Expand Down Expand Up @@ -47,7 +48,7 @@ module.exports = (ipfs) => {
done(null, {
name: link.name,
type: meta.type,
hash: bs58.encode(node.multihash),
hash: formatCid(node.multihash, options.cidBase),
size: meta.fileSize() || 0
})
}
Expand All @@ -57,7 +58,7 @@ module.exports = (ipfs) => {
cb(null, [{
name: result.name,
type: meta.type,
hash: bs58.encode(result.node.multihash),
hash: formatCid(result.node.multihash, options.cidBase),
size: meta.fileSize() || 0
}])
}
Expand Down
11 changes: 6 additions & 5 deletions src/core/stat.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
'use strict'

const unmarshal = require('ipfs-unixfs').unmarshal
const bs58 = require('bs58')
const {
traverseTo
traverseTo,
formatCid
} = require('./utils')
const waterfall = require('async/waterfall')
const log = require('debug')('ipfs:mfs:stat')

const defaultOptions = {
hash: false,
size: false,
withLocal: false
withLocal: false,
cidBase: 'base58btc'
}

module.exports = (ipfs) => {
Expand All @@ -32,7 +33,7 @@ module.exports = (ipfs) => {
({ node }, done) => {
if (options.hash) {
return done(null, {
hash: bs58.encode(node.multihash)
hash: formatCid(node.multihash, options.cidBase)
})
} else if (options.size) {
return done(null, {
Expand All @@ -49,7 +50,7 @@ module.exports = (ipfs) => {
}

done(null, {
hash: bs58.encode(node.multihash),
hash: formatCid(node.multihash, options.cidBase),
size: meta.fileSize() || 0,
cumulativeSize: node.size,
blocks: blocks,
Expand Down
15 changes: 15 additions & 0 deletions src/core/utils/format-cid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const CID = require('cids')

module.exports = (cid, base) => {
if (Buffer.isBuffer(cid)) {
cid = new CID(cid)
}

if (base === 'base58btc') {
return cid.toBaseEncodedString()
}

return cid.toV1().toBaseEncodedString(base)
}
1 change: 1 addition & 0 deletions src/core/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
createLock: require('./create-lock'),
createNode: require('./create-node'),
endPullStream: require('./end-pull-stream'),
formatCid: require('./format-cid'),
limitStreamBytes: require('./limit-stream-bytes'),
loadNode: require('./load-node'),
toSourcesAndDestination: require('./to-sources-and-destination'),
Expand Down
9 changes: 6 additions & 3 deletions src/http/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ const mfsLs = (api) => {
} = request.server.app
const {
arg,
long
long,
cidBase
} = request.query

return ipfs.files.ls(arg, {
long
long,
cidBase
})
.then(files => {
reply({
Expand All @@ -44,7 +46,8 @@ const mfsLs = (api) => {
},
query: Joi.object().keys({
arg: Joi.string().default('/'),
long: Joi.boolean().default(false)
long: Joi.boolean().default(false),
cidBase: Joi.string().default('base58btc')
})
.rename('l', 'long', {
override: true,
Expand Down
9 changes: 6 additions & 3 deletions src/http/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ const mfsStat = (api) => {
arg,
hash,
size,
withLocal
withLocal,
cidBase
} = request.query

return ipfs.files.stat(arg, {
hash,
size,
withLocal
withLocal,
cidBase
})
.then(stats => {
reply({
Expand Down Expand Up @@ -52,7 +54,8 @@ const mfsStat = (api) => {
arg: Joi.string().default('/'),
hash: Joi.boolean().default(false),
size: Joi.boolean().default(false),
withLocal: Joi.boolean().default(false)
withLocal: Joi.boolean().default(false),
cidBase: Joi.string().default('base58btc')
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ module.exports = {
createMfs,
bufferStream: require('./buffer-stream'),
collectLeafCids: require('./collect-leaf-cids'),
EMPTY_DIRECTORY_HASH: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
EMPTY_DIRECTORY_HASH: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn',
EMPTY_DIRECTORY_HASH_BASE32: 'bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354'
}
20 changes: 20 additions & 0 deletions test/ls.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,26 @@ describe('ls', function () {
})
})

it('lists a file with a base32 hash', () => {
const fileName = `small-file-${Math.random()}.txt`
const content = Buffer.from('Hello world')

return mfs.write(`/${fileName}`, content, {
create: true
})
.then(() => mfs.ls(`/${fileName}`, {
long: true,
cidBase: 'base32'
}))
.then(files => {
expect(files.length).to.equal(1)
expect(files[0].name).to.equal(fileName)
expect(files[0].type).to.equal(FILE_TYPES.file)
expect(files[0].size).to.equal(content.length)
expect(files[0].hash.startsWith('b')).to.equal(true)
})
})

it('fails to list non-existent file', () => {
return mfs.ls('/i-do-not-exist')
.then(() => {
Expand Down
36 changes: 35 additions & 1 deletion test/stat.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const loadFixture = require('aegir/fixtures')

const {
createMfs,
EMPTY_DIRECTORY_HASH
EMPTY_DIRECTORY_HASH,
EMPTY_DIRECTORY_HASH_BASE32
} = require('./helpers')

describe('stat', function () {
Expand Down Expand Up @@ -86,6 +87,20 @@ describe('stat', function () {
})
})

it('returns only a base32 hash', () => {
const path = `/directory-${Math.random()}`

return mfs.mkdir(path)
.then(() => mfs.stat(path, {
hash: true,
cidBase: 'base32'
}))
.then(stats => {
expect(Object.keys(stats).length).to.equal(1)
expect(stats.hash).to.equal(EMPTY_DIRECTORY_HASH_BASE32)
})
})

it('returns only the size', () => {
const path = `/directory-${Math.random()}`

Expand Down Expand Up @@ -134,4 +149,23 @@ describe('stat', function () {
expect(stats.type).to.equal('file')
})
})

it('stats a large file with base32', () => {
const filePath = '/stat/large-file.txt'

return mfs.write(filePath, largeFile, {
create: true,
parents: true
})
.then(() => mfs.stat(filePath, {
cidBase: 'base32'
}))
.then((stats) => {
expect(stats.hash.startsWith('b')).to.equal(true)
expect(stats.size).to.equal(largeFile.length)
expect(stats.cumulativeSize).to.equal(490800)
expect(stats.blocks).to.equal(2)
expect(stats.type).to.equal('file')
})
})
})