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

Commit

Permalink
feat: allow write and mkdir with different hash algs and cid versions
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: achingbrain <alex@achingbrain.net>
  • Loading branch information
achingbrain committed Nov 16, 2018
1 parent db77987 commit 0a12b3e
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 24 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"detect-webworker": "^1.0.0",
"dirty-chai": "^2.0.1",
"ipld": "~0.20.0",
"multihashes": "~0.4.14",
"pull-buffer-stream": "^1.0.0",
"pull-traverse": "^1.0.3"
},
Expand Down
6 changes: 3 additions & 3 deletions src/cli/mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = {
default: 0,
describe: 'Cid version to use. (experimental).'
},
hash: {
hashAlg: {
type: 'string',
describe: 'Hash function to use. Will set Cid version to 1 if used. (experimental).'
},
Expand All @@ -42,15 +42,15 @@ module.exports = {
ipfs,
parents,
cidVersion,
hash,
hashAlg,
flush
} = argv

argv.resolve(
ipfs.files.mkdir(path, {
parents,
cidVersion,
hash,
hashAlg,
flush
})
)
Expand Down
6 changes: 4 additions & 2 deletions src/core/mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {

const defaultOptions = {
parents: false,
hash: undefined,
hashAlg: undefined,
cidVersion: 0
}

Expand Down Expand Up @@ -61,7 +61,9 @@ module.exports = (context) => {
(cb) => traverseTo(context, path, {
parents: options.parents,
flush: options.flush,
createLastComponent: true
createLastComponent: true,
cidVersion: options.cidVersion,
hashAlg: options.hashAlg
}, cb),
(result, cb) => updateTree(context, result, cb),
(newRoot, next) => updateMfsRoot(context, newRoot.cid, next)
Expand Down
2 changes: 1 addition & 1 deletion src/core/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ const write = (context, existingNodeCid, existingNode, source, options, callback
}]),
importer(context.ipld, {
progress: options.progress,
hashAlg: options.hash,
hashAlg: options.hashAlg,
cidVersion: options.cidVersion,
strategy: options.strategy,
rawLeaves: options.rawLeaves,
Expand Down
18 changes: 18 additions & 0 deletions test/helpers/cid-at-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

const CID = require('cids')

module.exports = async (path, mfs) => {
const parts = path.split('/')
const fileName = parts.pop()
const directory = `/${parts.join('/')}`

return new CID(
(await mfs.ls(directory, {
long: true
}))
.filter(file => file.name === fileName)
.pop()
.hash
)
}
1 change: 1 addition & 0 deletions test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const createMfs = async () => {

module.exports = {
createMfs,
cidAtPath: require('./cid-at-path'),
collectLeafCids: require('./collect-leaf-cids'),
EMPTY_DIRECTORY_HASH: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn',
EMPTY_DIRECTORY_HASH_BASE32: 'bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354'
Expand Down
39 changes: 36 additions & 3 deletions test/mkdir.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const multihash = require('multihashes')
const {
createMfs
createMfs,
cidAtPath
} = require('./helpers')

describe('mkdir', function () {
Expand Down Expand Up @@ -105,11 +107,42 @@ describe('mkdir', function () {
})
})

it.skip('creates a nested directory with a different CID version to the parent', () => {
it('creates a nested directory with a different CID version to the parent', async () => {
const directory = `cid-versions-${Math.random()}`
const directoryPath = `/${directory}`
const subDirectory = `cid-versions-${Math.random()}`
const subDirectoryPath = `${directoryPath}/${subDirectory}`

await mfs.mkdir(directoryPath, {
cidVersion: 0
})

expect((await cidAtPath(directoryPath, mfs)).version).to.equal(0)

await mfs.mkdir(subDirectoryPath, {
cidVersion: 1
})

expect((await cidAtPath(subDirectoryPath, mfs)).version).to.equal(1)
})

it.skip('creates a nested directory with a different hash function to the parent', () => {
it('creates a nested directory with a different hash function to the parent', async () => {
const directory = `cid-versions-${Math.random()}`
const directoryPath = `/${directory}`
const subDirectory = `cid-versions-${Math.random()}`
const subDirectoryPath = `${directoryPath}/${subDirectory}`

await mfs.mkdir(directoryPath, {
cidVersion: 0
})

expect((await cidAtPath(directoryPath, mfs)).version).to.equal(0)

await mfs.mkdir(subDirectoryPath, {
cidVersion: 1,
hashAlg: 'sha2-512'
})

expect(multihash.decode((await cidAtPath(subDirectoryPath, mfs)).multihash).name).to.equal('sha2-512')
})
})
122 changes: 107 additions & 15 deletions test/write.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ const loadFixture = require('aegir/fixtures')
const isNode = require('detect-node')
const values = require('pull-stream/sources/values')
const bufferStream = require('pull-buffer-stream')
const multihash = require('multihashes')
const {
collectLeafCids,
createMfs
createMfs,
cidAtPath
} = require('./helpers')

let fs
Expand Down Expand Up @@ -489,26 +491,116 @@ describe('write', function () {
})
})

// https://github.com/ipld/js-ipld/issues/157
it.skip('writes a file with a different CID version to the parent', () => {
const directory = '/cid-versions'
const fileName = `${directory}/file.txt`
it('writes a file with a different CID version to the parent', async () => {
const directory = `cid-versions-${Math.random()}`
const directoryPath = `/${directory}`
const fileName = `file-${Math.random()}.txt`
const filePath = `${directoryPath}/${fileName}`
const expectedBytes = Buffer.from([0, 1, 2, 3])

return mfs.mkdir(directory, {
await mfs.mkdir(directoryPath, {
cidVersion: 0
})
.then(() => mfs.write(fileName, expectedBytes, {
create: true,
cidVersion: 1
}))
.then(() => mfs.read(fileName))
.then(actualBytes => {
expect(actualBytes).to.deep.equal(expectedBytes)
})

expect((await cidAtPath(directoryPath, mfs)).version).to.equal(0)

await mfs.write(filePath, expectedBytes, {
create: true,
cidVersion: 1
})

expect((await cidAtPath(filePath, mfs)).version).to.equal(1)

const actualBytes = await mfs.read(filePath)

expect(actualBytes).to.deep.equal(expectedBytes)
})

it('overwrites a file with a different CID version', async () => {
const directory = `cid-versions-${Math.random()}`
const directoryPath = `/${directory}`
const fileName = `file-${Math.random()}.txt`
const filePath = `${directoryPath}/${fileName}`
const expectedBytes = Buffer.from([0, 1, 2, 3])

await mfs.mkdir(directoryPath, {
cidVersion: 0
})

expect((await cidAtPath(directoryPath, mfs)).version).to.equal(0)

await mfs.write(filePath, Buffer.from([5, 6]), {
create: true,
cidVersion: 0
})

expect((await cidAtPath(filePath, mfs)).version).to.equal(0)

await mfs.write(filePath, expectedBytes, {
cidVersion: 1
})

expect((await cidAtPath(filePath, mfs)).version).to.equal(1)

const actualBytes = await mfs.read(filePath)

expect(actualBytes).to.deep.equal(expectedBytes)
})

it('partially overwrites a file with a different CID version', async () => {
const directory = `cid-versions-${Math.random()}`
const directoryPath = `/${directory}`
const fileName = `file-${Math.random()}.txt`
const filePath = `${directoryPath}/${fileName}`

await mfs.mkdir(directoryPath, {
cidVersion: 0
})

expect((await cidAtPath(directoryPath, mfs)).version).to.equal(0)

await mfs.write(filePath, Buffer.from([5, 6, 7, 8, 9, 10, 11]), {
create: true,
cidVersion: 0
})

expect((await cidAtPath(filePath, mfs)).version).to.equal(0)

await mfs.write(filePath, Buffer.from([0, 1, 2, 3]), {
cidVersion: 1,
offset: 1
})

expect((await cidAtPath(filePath, mfs)).version).to.equal(1)

const actualBytes = await mfs.read(filePath)

expect(actualBytes).to.deep.equal(Buffer.from([5, 0, 1, 2, 3, 10, 11]))
})

it.skip('writes a file with a different hash function to the parent', () => {
it('writes a file with a different hash function to the parent', async () => {
const directory = `cid-versions-${Math.random()}`
const directoryPath = `/${directory}`
const fileName = `file-${Math.random()}.txt`
const filePath = `${directoryPath}/${fileName}`
const expectedBytes = Buffer.from([0, 1, 2, 3])

await mfs.mkdir(directoryPath, {
cidVersion: 0
})

expect((await cidAtPath(directoryPath, mfs)).version).to.equal(0)

await mfs.write(filePath, expectedBytes, {
create: true,
cidVersion: 1,
hashAlg: 'sha2-512'
})

expect(multihash.decode((await cidAtPath(filePath, mfs)).multihash).name).to.equal('sha2-512')

const actualBytes = await mfs.read(filePath)

expect(actualBytes).to.deep.equal(expectedBytes)
})
})

0 comments on commit 0a12b3e

Please sign in to comment.