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

[WIP] refactor: use CIDv1 and encode with base32 by default #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@
},
"dependencies": {
"async": "^2.6.1",
"cids": "~0.5.5",
"cids": "github:ipld/js-cid#refactor/cidv1base32-default",
"debug": "^4.1.0",
"filereader-stream": "^2.0.0",
"hamt-sharding": "~0.0.2",
"interface-datastore": "~0.6.0",
"ipfs-multipart": "~0.1.0",
"ipfs-unixfs": "~0.1.16",
"ipfs-unixfs-exporter": "~0.35.5",
"ipfs-unixfs-importer": "~0.38.0",
"ipfs-unixfs-importer": "github:ipfs/js-ipfs-unixfs-importer#refactor/cidv1b32-default",
"ipld-dag-pb": "~0.15.0",
"is-pull-stream": "~0.0.0",
"is-stream": "^1.1.0",
Expand Down
4 changes: 2 additions & 2 deletions src/core/mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const {
const defaultOptions = {
parents: false,
hashAlg: 'sha2-256',
cidVersion: 0,
cidVersion: 1,
shardSplitThreshold: 1000,
format: 'dag-pb',
flush: true
Expand All @@ -37,7 +37,7 @@ module.exports = (context) => {
options = Object.assign({}, defaultOptions, options)

options.parents = options.p || options.parents
options.cidVersion = options.cidVersion || 0
options.cidVersion = options.cidVersion == null ? 1 : options.cidVersion

if (!path) {
return callback(new Error('no path given to Mkdir'))
Expand Down
2 changes: 1 addition & 1 deletion src/core/rm.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {

const defaultOptions = {
recursive: false,
cidVersion: 0,
cidVersion: 1,
hashAlg: 'sha2-256',
format: 'dag-pb'
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/utils/add-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const defaultOptions = {
name: '',
size: undefined,
flush: true,
cidVersion: 0,
cidVersion: 1,
hashAlg: 'sha2-256',
codec: 'dag-pb',
shardSplitThreshold: 1000
Expand Down Expand Up @@ -111,7 +111,7 @@ const addToDirectory = (context, options, callback) => {
(parent, done) => {
// Persist the new parent DAGNode
context.ipld.put(parent, {
version: options.cidVersion,
version: options.cidVersion == null ? 1 : options.cidVersion,
format: options.codec,
hashAlg: options.hashAlg,
hashOnly: !options.flush
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils/create-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const createNode = (context, type, options, callback) => {
waterfall([
(done) => DAGNode.create(new UnixFS(type).marshal(), [], done),
(node, done) => context.ipld.put(node, {
version: options.cidVersion,
version: options.cidVersion == null ? 1 : options.cidVersion,
format: options.format,
hashAlg: options.hashAlg
}, (err, cid) => done(err, {
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils/hamt-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const updateHamtDirectory = (context, links, bucket, options, callback) => {
(parent, done) => {
// Persist the new parent DAGNode
context.ipld.put(parent, {
version: options.cidVersion,
version: options.cidVersion == null ? 1 : options.cidVersion,
format: options.codec,
hashAlg: options.hashAlg,
hashOnly: !options.flush
Expand Down
6 changes: 3 additions & 3 deletions src/core/utils/remove-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const defaultOptions = {
parentCid: undefined,
name: '',
flush: true,
cidVersion: 0,
cidVersion: 1,
hashAlg: 'sha2-256',
codec: 'dag-pb',
shardSplitThreshold: 1000
Expand Down Expand Up @@ -70,7 +70,7 @@ const removeFromDirectory = (context, options, callback) => {
(cb) => DAGNode.rmLink(options.parent, options.name, cb),
(newParentNode, cb) => {
context.ipld.put(newParentNode, {
version: options.cidVersion,
version: options.cidVersion == null ? 1 : options.cidVersion,
format: options.codec,
hashAlg: options.hashAlg
}, (error, cid) => cb(error, {
Expand Down Expand Up @@ -131,7 +131,7 @@ const updateShard = (context, positions, child, options, callback) => {
(done) => DAGNode.rmLink(node, link.name, done),
(node, done) => {
context.ipld.put(node, {
version: options.cidVersion,
version: options.cidVersion == null ? 1 : options.cidVersion,
format: options.codec,
hashAlg: options.hashAlg,
hashOnly: !options.flush
Expand Down
4 changes: 2 additions & 2 deletions src/core/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const defaultOptions = {
truncate: false, // whether to truncate the file first
rawLeaves: false,
reduceSingleLeafToSelf: false,
cidVersion: 0,
cidVersion: 1,
hashAlg: 'sha2-256',
format: 'dag-pb',
parents: false, // whether to create intermediate directories if they do not exist
Expand Down Expand Up @@ -74,7 +74,7 @@ module.exports = function mfsWrite (context) {
options.length = Infinity
}

options.cidVersion = options.cidVersion || 0
options.cidVersion = options.cidVersion == null ? 1 : options.cidVersion

waterfall([
(done) => {
Expand Down
2 changes: 1 addition & 1 deletion src/http/mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const mfsMkdir = {
cidVersion: Joi.number().integer().valid([
0,
1
]).default(0),
]).default(1),
flush: Joi.boolean().default(true)
})
.rename('p', 'parents', {
Expand Down
2 changes: 1 addition & 1 deletion src/http/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const mfsWrite = {
cidVersion: Joi.number().integer().valid([
0,
1
]).default(0),
]).default(1),
hashAlg: Joi.string().valid([
'sha2-256'
]).default('sha2-256'),
Expand Down
3 changes: 2 additions & 1 deletion test/helpers/create-shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const createShard = (ipld, files, shardSplitThreshold = 10) => {
importer(ipld, {
shardSplitThreshold,
reduceSingleLeafToSelf: false, // same as go-ipfs-mfs implementation, differs from `ipfs add`(!)
leafType: 'raw' // same as go-ipfs-mfs implementation, differs from `ipfs add`(!)
leafType: 'raw', // same as go-ipfs-mfs implementation, differs from `ipfs add`(!)
rawLeaves: false
}),
collect((err, files) => {
if (err) {
Expand Down
4 changes: 2 additions & 2 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ module.exports = {
createTwoShards: require('./create-two-shards'),
findTreeWithDepth: require('./find-tree-with-depth'),
printTree: require('./print-tree'),
EMPTY_DIRECTORY_HASH: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn',
EMPTY_DIRECTORY_HASH_BASE32: 'bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354'
EMPTY_DIRECTORY_HASH: 'bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354',
EMPTY_DIRECTORY_HASH_BASE64URL: 'uAXASIFmUhDkGXylhnvQSgMu5Mr5SxW2ZxZZrZeAREjnwmLvv'
}
20 changes: 10 additions & 10 deletions test/stat.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {
createMfs,
createShardedDirectory,
EMPTY_DIRECTORY_HASH,
EMPTY_DIRECTORY_HASH_BASE32
EMPTY_DIRECTORY_HASH_BASE64URL
} = require('./helpers')

describe('stat', () => {
Expand Down Expand Up @@ -75,18 +75,18 @@ describe('stat', () => {
expect(stats.hash).to.equal(EMPTY_DIRECTORY_HASH)
})

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

await mfs.mkdir(path)

const stats = await mfs.stat(path, {
hash: true,
cidBase: 'base32'
cidBase: 'base64url'
})

expect(Object.keys(stats).length).to.equal(1)
expect(stats.hash).to.equal(EMPTY_DIRECTORY_HASH_BASE32)
expect(stats.hash).to.equal(EMPTY_DIRECTORY_HASH_BASE64URL)
})

it('returns only the size', async () => {
Expand Down Expand Up @@ -116,7 +116,7 @@ describe('stat', () => {

const stats = await mfs.stat(filePath)
expect(stats.size).to.equal(smallFile.length)
expect(stats.cumulativeSize).to.equal(71)
expect(stats.cumulativeSize).to.equal(73)
expect(stats.blocks).to.equal(1)
expect(stats.type).to.equal('file')
})
Expand All @@ -131,12 +131,12 @@ describe('stat', () => {

const stats = await mfs.stat(filePath)
expect(stats.size).to.equal(largeFile.length)
expect(stats.cumulativeSize).to.equal(490800)
expect(stats.cumulativeSize).to.equal(490804)
expect(stats.blocks).to.equal(2)
expect(stats.type).to.equal('file')
})

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

await mfs.write(filePath, largeFile, {
Expand All @@ -145,11 +145,11 @@ describe('stat', () => {
})

const stats = await mfs.stat(filePath, {
cidBase: 'base32'
cidBase: 'base64url'
})
expect(stats.hash.startsWith('b')).to.equal(true)
expect(stats.hash.startsWith('u')).to.equal(true)
expect(stats.size).to.equal(largeFile.length)
expect(stats.cumulativeSize).to.equal(490800)
expect(stats.cumulativeSize).to.equal(490804)
expect(stats.blocks).to.equal(2)
expect(stats.type).to.equal('file')
})
Expand Down