Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 38 additions & 1 deletion js/src/block/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const multihash = require('multihashes')
const CID = require('cids')
const auto = require('async/auto')
const crypto = require('crypto')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the bytes need to be so random that it requires to add the crypto module (and respective browser polyfill?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, and it's probably faster to just use Date.now() or Math.random(). I've removed all instances of crypto. There's one test that generates lots of random data to ensure it spans multiple dag nodes and I've changed that test to use randombytes so we don't pull in the whole of crypto in the browser.

const { getDescribe, getIt, expect } = require('../utils/mocha')

module.exports = (createCommon, options) => {
Expand Down Expand Up @@ -70,6 +71,42 @@ module.exports = (createCommon, options) => {
})
})

// TODO it.skip('Promises support', (done) => {})
it('should get a block added as CIDv0 with a CIDv1', done => {
const input = crypto.randomBytes(32)

ipfs.block.put(input, { version: 0 }, (err, res) => {
expect(err).to.not.exist()

const cidv0 = res.cid
expect(cidv0.version).to.equal(0)

const cidv1 = cidv0.toV1()

ipfs.block.get(cidv1, (err, output) => {
expect(err).to.not.exist()
expect(output.data).to.eql(input)
done()
})
})
})

it('should get a block added as CIDv1 with a CIDv0', done => {
const input = crypto.randomBytes(32)

ipfs.block.put(input, { version: 1 }, (err, res) => {
expect(err).to.not.exist()

const cidv1 = res.cid
expect(cidv1.version).to.equal(1)

const cidv0 = cidv1.toV0()

ipfs.block.get(cidv0, (err, output) => {
expect(err).to.not.exist()
expect(output.data).to.eql(input)
done()
})
})
})
})
}
43 changes: 43 additions & 0 deletions js/src/dag/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const { series, eachSeries } = require('async')
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const dagCBOR = require('ipld-dag-cbor')
const crypto = require('crypto')
const Unixfs = require('ipfs-unixfs')
const CID = require('cids')
const { spawnNodeWithId } = require('../utils/spawn')
const { getDescribe, getIt, expect } = require('../utils/mocha')

Expand Down Expand Up @@ -211,5 +214,45 @@ module.exports = (createCommon, options) => {
done()
})
})

it('should get a node added as CIDv0 with a CIDv1', done => {
const input = crypto.randomBytes(32)

dagPB.DAGNode.create(input, (err, node) => {
expect(err).to.not.exist()

ipfs.dag.put(node, { format: 'dag-pb', hashAlg: 'sha2-256' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(0)

const cidv1 = cid.toV1()

ipfs.dag.get(cidv1, (err, output) => {
expect(err).to.not.exist()
expect(output.value.data).to.eql(input)
done()
})
})
})
})

it('should get a node added as CIDv1 with a CIDv0', done => {
const input = crypto.randomBytes(32)

ipfs.add(input, { cidVersion: 1, rawLeaves: false }, (err, res) => {
expect(err).to.not.exist()

const cidv1 = new CID(res[0].hash)
expect(cidv1.version).to.equal(1)

const cidv0 = cidv1.toV0()

ipfs.dag.get(cidv0, (err, output) => {
expect(err).to.not.exist()
expect(Unixfs.unmarshal(output.value.data).data).to.eql(input)
done()
})
})
})
})
}
39 changes: 39 additions & 0 deletions js/src/files-regular/cat.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { fixtures } = require('./utils')
const bs58 = require('bs58')
const parallel = require('async/parallel')
const CID = require('cids')
const crypto = require('crypto')
const { getDescribe, getIt, expect } = require('../utils/mocha')

module.exports = (createCommon, options) => {
Expand Down Expand Up @@ -76,6 +77,44 @@ module.exports = (createCommon, options) => {
})
})

it('should cat a file added as CIDv0 with a CIDv1', done => {
const input = crypto.randomBytes(32)

ipfs.add(input, { cidVersion: 0 }, (err, res) => {
expect(err).to.not.exist()

const cidv0 = new CID(res[0].hash)
expect(cidv0.version).to.equal(0)

const cidv1 = cidv0.toV1()

ipfs.cat(cidv1, (err, output) => {
expect(err).to.not.exist()
expect(output).to.eql(input)
done()
})
})
})

it('should cat a file added as CIDv1 with a CIDv0', done => {
const input = crypto.randomBytes(32)

ipfs.add(input, { cidVersion: 1, rawLeaves: false }, (err, res) => {
expect(err).to.not.exist()

const cidv1 = new CID(res[0].hash)
expect(cidv1.version).to.equal(1)

const cidv0 = cidv1.toV0()

ipfs.cat(cidv0, (err, output) => {
expect(err).to.not.exist()
expect(output).to.eql(input)
done()
})
})
})

it('should cat a BIG file', (done) => {
ipfs.cat(fixtures.bigFile.cid, (err, data) => {
expect(err).to.not.exist()
Expand Down
40 changes: 40 additions & 0 deletions js/src/files-regular/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const { fixtures } = require('./utils')
const bs58 = require('bs58')
const parallel = require('async/parallel')
const series = require('async/series')
const crypto = require('crypto')
const CID = require('cids')
const { getDescribe, getIt, expect } = require('../utils/mocha')

module.exports = (createCommon, options) => {
Expand Down Expand Up @@ -73,6 +75,44 @@ module.exports = (createCommon, options) => {
})
})

it('should get a file added as CIDv0 with a CIDv1', done => {
const input = crypto.randomBytes(32)

ipfs.add(input, { cidVersion: 0 }, (err, res) => {
expect(err).to.not.exist()

const cidv0 = new CID(res[0].hash)
expect(cidv0.version).to.equal(0)

const cidv1 = cidv0.toV1()

ipfs.get(cidv1, (err, output) => {
expect(err).to.not.exist()
expect(output[0].content).to.eql(input)
done()
})
})
})

it('should get a file added as CIDv1 with a CIDv0', done => {
const input = crypto.randomBytes(32)

ipfs.add(input, { cidVersion: 1, rawLeaves: false }, (err, res) => {
expect(err).to.not.exist()

const cidv1 = new CID(res[0].hash)
expect(cidv1.version).to.equal(1)

const cidv0 = cidv1.toV0()

ipfs.get(cidv0, (err, output) => {
expect(err).to.not.exist()
expect(output[0].content).to.eql(input)
done()
})
})
})

it('should get a BIG file', (done) => {
ipfs.get(fixtures.bigFile.cid, (err, files) => {
expect(err).to.not.exist()
Expand Down
58 changes: 58 additions & 0 deletions js/src/files-regular/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

const { fixtures } = require('./utils')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const crypto = require('crypto')
const CID = require('cids')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
Expand Down Expand Up @@ -104,6 +106,62 @@ module.exports = (createCommon, options) => {
})
})

it('should ls files added as CIDv0 with a CIDv1', done => {
const randomName = () => crypto.randomBytes(6).toString('hex')
const dir = randomName()

const input = [
{ path: `${dir}/${randomName()}`, content: crypto.randomBytes(32) },
{ path: `${dir}/${randomName()}`, content: crypto.randomBytes(32) }
]

ipfs.add(input, { cidVersion: 0 }, (err, res) => {
expect(err).to.not.exist()

const cidv0 = new CID(res[res.length - 1].hash)
expect(cidv0.version).to.equal(0)

const cidv1 = cidv0.toV1()

ipfs.ls(cidv1, (err, output) => {
expect(err).to.not.exist()
expect(output.length).to.equal(input.length)
output.forEach(({ hash }) => {
expect(res.find(file => file.hash === hash)).to.exist()
})
done()
})
})
})

it('should ls files added as CIDv1 with a CIDv0', done => {
const randomName = () => crypto.randomBytes(6).toString('hex')
const dir = randomName()

const input = [
{ path: `${dir}/${randomName()}`, content: crypto.randomBytes(32) },
{ path: `${dir}/${randomName()}`, content: crypto.randomBytes(32) }
]

ipfs.add(input, { cidVersion: 1, rawLeaves: false }, (err, res) => {
expect(err).to.not.exist()

const cidv1 = new CID(res[res.length - 1].hash)
expect(cidv1.version).to.equal(1)

const cidv0 = cidv1.toV1()

ipfs.ls(cidv0, (err, output) => {
expect(err).to.not.exist()
expect(output.length).to.equal(input.length)
output.forEach(({ hash }) => {
expect(res.find(file => file.hash === hash)).to.exist()
})
done()
})
})
})

it('should correctly handle a non existing hash', (done) => {
ipfs.ls('surelynotavalidhashheh?', (err, res) => {
expect(err).to.exist()
Expand Down