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

Commit

Permalink
feat: CBOR TAG
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Jan 31, 2017
1 parent 48f0294 commit a349c9e
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 17 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
"borc": "^2.0.2",
"bs58": "^4.0.0",
"cids": "^0.4.0",
"is-circular": "^1.0.1",
"lodash.clonedeep": "^4.5.0",
"lodash.defaults": "^4.2.0",
"lodash.includes": "^4.3.0",
"multihashes": "^0.3.2",
"multihashing-async": "^0.4.0",
"traverse": "^0.6.6"
Expand All @@ -59,4 +63,4 @@
"npmcdn-to-unpkg-bot <npmcdn-to-unpkg-bot@users.noreply.github.com>",
"wanderer <mjbecze@gmail.com>"
]
}
}
96 changes: 80 additions & 16 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,103 @@ const multihashing = require('multihashing-async')
const CID = require('cids')
const waterfall = require('async/waterfall')
const setImmediate = require('async/setImmediate')
const cloneDeep = require('lodash.cloneDeep')
const includes = require('lodash.includes')
const defaults = require('lodash.defaults')
const isCircular = require('is-circular')

const resolver = require('./resolver')

function tagCID (cid) {
// https://github.com/ipfs/go-ipfs/issues/3570#issuecomment-273931692
const CID_CBOR_TAG = 42

return new cbor.Tagged(CID_CBOR_TAG, cid)
}

function replaceCIDbyTAG (dagNode) {
if (isCircular(dagNode)) {
throw new Error('The object passes has circular references')
}

const copy = cloneDeep(dagNode)

function transform (obj) {
const keys = Object.keys(obj)

// Recursive transform
keys.forEach((key) => {
if (typeof obj[key] === 'object') {
obj[key] = transform(obj[key])
}
})

if (includes(keys, '/')) {
let cid = obj['/']

// Multiaddr encoding
// if (typeof link === 'string' && isMultiaddr(link)) {
// link = new Multiaddr(link).buffer
// }

delete obj['/'] // Remove the /
return tagCID(cid)
}

return obj
}

return transform(copy)
}

function replaceTAGbyCID (dagNode) {
function transform (obj) {
Object.keys(obj).forEach((key) => {
const val = obj[key]

if (val instanceof cbor.Tagged) {
if (typeof val.value === 'string') {
obj[key] = { '/': val.value }
} else {
obj[key] = defaults({ '/': val.value[0] }, transform(val.value[1]))
}
} else if (typeof val === 'object') {
obj[key] = transform(val)
}
})

return obj
}

return transform(dagNode)
}

exports = module.exports

exports.serialize = (dagNode, callback) => {
let serialized

try {
serialized = cbor.encode(dagNode)
const dagNodeTagged = replaceCIDbyTAG(dagNode)
serialized = cbor.encode(dagNodeTagged)
} catch (err) {
// return is important, otherwise in case of error the execution would continue
return setImmediate(() => {
callback(err)
})
return setImmediate(() => callback(err))
}
setImmediate(() => {
callback(null, serialized)
})
setImmediate(() => callback(null, serialized))
}

exports.deserialize = (data, callback) => {
let res
let deserialized

try {
res = cbor.decodeFirst(data)
deserialized = cbor.decodeFirst(data)
} catch (err) {
return setImmediate(() => {
callback(err)
})
return setImmediate(() => callback(err))
}

setImmediate(() => {
callback(null, res)
})
const dagNode = replaceTAGbyCID(deserialized)

setImmediate(() => callback(null, dagNode))
}

exports.cid = (dagNode, callback) => {
Expand Down

0 comments on commit a349c9e

Please sign in to comment.