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

Awesome IPLD endeavour #25

Merged
merged 11 commits into from
Oct 26, 2016
17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "ipld",
"name": "ipld-dag-cbor",
"version": "0.6.0",
"description": "JavaScript implementation of the IPLD (InterpPlanetary Linked Data)",
"main": "lib/index.js",
Expand Down Expand Up @@ -39,22 +39,21 @@
"dependencies": {
"babel-runtime": "^6.6.1",
"bs58": "^3.0.0",
"cbor": "^1.0.4",
"lodash.clonedeep": "^4.3.2",
"lodash.defaults": "^4.0.1",
"lodash.includes": "^4.1.3",
"multiaddr": "^2.0.0",
"cbor": "^2.0.1",
"cids": "^0.2.0",
"multihashes": "^0.2.2",
"multihashing": "^0.2.1",
"nofilter": "0.0.2"
"traverse": "^0.6.6"
},
"devDependencies": {
"aegir": "^3.0.4",
"aegir": "8.1.2",
"async": "^2.1.2",
"chai": "^3.5.0",
"ipfs-block": "^0.4.0",
"pre-commit": "^1.1.3"
},
"contributors": [
"David Dias <daviddias.p@gmail.com>",
"dignifiedquire <dignifiedquire@gmail.com>"
]
}
}
105 changes: 0 additions & 105 deletions src/cbor.js

This file was deleted.

12 changes: 2 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
'use strict'

const cbor = require('./cbor')
const multihash = require('./multihash')

exports = module.exports

exports.LINK_TAG = cbor.LINK_TAG
exports.LINK_SYMBOL = cbor.LINK_SYMBOL
exports.marshal = cbor.marshal
exports.unmarshal = cbor.unmarshal
exports.multihash = multihash
exports.util = require('./util.js')
exports.resolver = require('./resolver.js')
14 changes: 0 additions & 14 deletions src/multihash.js

This file was deleted.

117 changes: 117 additions & 0 deletions src/resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
'use strict'

const util = require('./util')
const traverse = require('traverse')

exports = module.exports

exports.multicodec = 'dag-cbor'

/*
* resolve: receives a path and a block and returns the value on path,
* throw if not possible. `block` is an IPFS Block instance (contains data + key)
*/
exports.resolve = (block, path, callback) => {
if (typeof path === 'function') {
callback = path
path = undefined
}

util.deserialize(block.data, (err, node) => {
if (err) {
return callback(err)
}

// root

if (!path || path === '/') {
return callback(null, {
value: node,
remainderPath: ''
})
}

// within scope

// const tree = exports.tree(block)
const parts = path.split('/')
const val = traverse(node).get(parts)

if (val) {
return callback(null, {
value: val,
remainderPath: ''
})
}

// out of scope
let value
let len = parts.length

for (let i = 0; i < len; i++) {
const partialPath = parts.shift()

if (Array.isArray(node) && !Buffer.isBuffer(node)) {
value = node[Number(partialPath)]
} if (node[partialPath]) {
value = node[partialPath]
} else {
// can't traverse more
if (!value) {
return callback(new Error('path not available at root'))
} else {
parts.unshift(partialPath)
return callback(null, {
value: value,
remainderPath: parts.join('/')
})
}
}
node = value
}
})
}

/*
* tree: returns a flattened array with paths: values of the project. options
* are option (i.e. nestness)
*/
exports.tree = (block, options, callback) => {
if (typeof options === 'function') {
callback = options
options = undefined
}

if (!options) {
options = {}
}

util.deserialize(block.data, (err, node) => {
if (err) {
return callback(err)
}

callback(null, flattenObject(node))
})
}

function flattenObject (obj, delimiter) {
if (!delimiter) {
delimiter = '/'
}

if (Object.keys(obj).length === 0) {
return []
}

return traverse(obj).reduce(function (acc, x) {
if (this.isLeaf) {
acc.push({
path: this.path.join(delimiter),
value: x
})
}

return acc
}, [])
}
26 changes: 26 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

const cbor = require('cbor')
const multihashing = require('multihashing')
const CID = require('cids')
const resolver = require('./resolver')

exports = module.exports

exports.serialize = (dagNode, callback) => {
callback(null, cbor.encode(dagNode))
}

exports.deserialize = (data, callback) => {
cbor.decodeFirst(data, callback)
}

exports.cid = (dagNode, callback) => {
exports.serialize(dagNode, (err, serialized) => {
if (err) {
return callback(err)
}
const mh = multihashing(serialized, 'sha2-256')
callback(null, new CID(1, resolver.multicodec, mh))
})
}
Loading