Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
test: let the testing begin
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Oct 3, 2016
1 parent edee5fa commit 90ff073
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 149 deletions.
141 changes: 139 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,141 @@
'use strict'

exports.IPLDService = require('./ipld-service')
exports.resolve = require('./resolve')
// const isIPFS = require('is-ipfs')
const Block = require('ipfs-block')
// const ipld = require('ipld-dag-cbor')
const pull = require('pull-stream')
const traverse = require('pull-traverse')
// const mh = require('multihashes')
const dagPB = require('ipld-dag-pb')

const utils = require('./utils')

module.exports = class IPLDResolver {
constructor (blockService) {
// TODO instead of throwing, just create an in-memory
// block-service, so it is nice for demos
if (!blockService) {
throw new Error('IPLDService requires a BlockService instance')
}

this.bs = blockService
this.resolvers = {}
this.support(dagPB.resolver.multicodec, dagPB.DAGNode, dagPB.resolver)
}

// Adds support for an IPLD format
// default ones are dag-pb and dag-cbor
support (multicodec, type, resolver) {
this.resolvers[multicodec] = {
resolver: resolver,
Type: type
}
}

resolve (cid, path) {
// TODO
}

put (node, callback) {
callback = callback || noop
pull(
pull.values([node]),
this.putStream(callback)
)
}

putStream (callback) {
callback = callback || noop

return pull(
pull.map((node) => {
return {
block: new Block(node.serialize()),
cid: node.cid()
}
}),
this.bs.putStream(),
pull.onEnd(callback)
)
}

get (cid, callback) {
pull(
this.getStream(cid),
pull.collect((err, res) => {
if (err) {
return callback(err)
}
callback(null, res[0])
})
)
}

getStream (cid) {
return pull(
this.bs.getStream(cid),
pull.map((block) => {
if (this.resolvers[cid.codec]) {
const node = new this.resolvers[cid.codec].Type()
node.deserialize(block.data)
return node
} else { // multicodec unknown, send back raw data
return block.data
}
})
)
}

getRecursive (cid, callback) {
pull(
this.getRecursiveStream(cid),
pull.collect(callback)
)
}

getRecursiveStream (cid) {
return pull(
this.getStream(cid),
pull.map((node) => {
traverse.widthFirst(node, (node) => {
return pull(
pull.values(utils.getKeys(node)),
pull.map((link) => this.getStream(link)),
pull.flatten()
)
})
}),
pull.flatten()
)
}

remove (cids, callback) {
this.bs.delete(cids, callback)
}
}

function noop () {}

/*
function normalizeKey (key) {
let res
const isMhash = isIPFS.multihash(key)
const isPath = isIPFS.path(key)
if (!isMhash && !isPath) {
return null
}
if (isMhash) {
res = key
} else if (isPath) {
res = key.replace('/ipfs/', '')
}
if (typeof res === 'string') {
return mh.fromB58String(res)
}
return res
}
*/
137 changes: 0 additions & 137 deletions src/ipld-resolver.js

This file was deleted.

File renamed without changes.
9 changes: 4 additions & 5 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ const IPFSRepo = require('ipfs-repo')
const pull = require('pull-stream')
const repoContext = require.context('buffer!./example-repo', true)

const tests = require('./ipld-tests')

const idb = window.indexedDB ||
window.mozIndexedDB ||
window.webkitIndexedDB ||
Expand All @@ -18,7 +16,7 @@ const idb = window.indexedDB ||
idb.deleteDatabase('ipfs')
idb.deleteDatabase('ipfs/blocks')

describe('ipfs merkle dag browser tests', function () {
describe('Browser', function () {
before(function (done) {
this.timeout(10000)

Expand Down Expand Up @@ -49,6 +47,7 @@ describe('ipfs merkle dag browser tests', function () {
}, done)
})

const repo = new IPFSRepo('ipfs', {stores: Store})
tests(repo)
const repo = new IPFSRepo('ipfs', { stores: Store })

require('./test-ipld-dag-pb')(repo)
})
8 changes: 4 additions & 4 deletions test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ const expect = require('chai').expect
const IPFSRepo = require('ipfs-repo')
const Store = require('fs-pull-blob-store')

const tests = require('./ipld-tests')

describe('node test blocks', () => {
describe('Node.js', () => {
const repoExample = process.cwd() + '/test/example-repo'
const repoTests = process.cwd() + '/test/repo-just-for-test' + Date.now()

Expand All @@ -30,5 +28,7 @@ describe('node test blocks', () => {

const repo = new IPFSRepo(repoTests, {stores: Store})

tests(repo)
require('./test-ipld-dag-pb')(repo)
// require('./test-ipld-dag-cbor')(repo)
// require('./test-ipld-eth-block')(repo)
})
2 changes: 1 addition & 1 deletion test/ipld-tests.js → test/old-ipld-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = (repo) => {
const bs = new BlockService(repo)
const ipldService = new IPLDService(bs)

describe('IPLDService', () => {
describe('IPLD Resolver (old tests)', () => {
it('throws when not passed a repo', () => {
expect(() => new IPLDService()).to.throw(/requires a BlockService/)
})
Expand Down
8 changes: 8 additions & 0 deletions test/test-ipld-all-together-now.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* eslint-env mocha */
'use strict'

/*
* Test different types of data structures living together
* &
* Test data made of mixed data structures!
*/
2 changes: 2 additions & 0 deletions test/test-ipld-dag-cbor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* eslint-env mocha */
'use strict'
Loading

0 comments on commit 90ff073

Please sign in to comment.