Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
feat: dag.put
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Apr 5, 2018
1 parent 41d32e3 commit 9463d3a
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"glob": "^7.1.2",
"ipfs-block": "~0.6.1",
"ipfs-unixfs": "~0.1.14",
"ipld-dag-cbor": "^0.12.0",
"ipld-dag-pb": "~0.13.1",
"is-ipfs": "^0.3.2",
"is-stream": "^1.1.0",
Expand Down
79 changes: 79 additions & 0 deletions src/dag/dag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict'

const dagPB = require('ipld-dag-pb')
const dagCBOR = require('ipld-dag-cbor')
const promisify = require('promisify-es6')
const CID = require('cids')
const multihash = require('multihashes')

function noop () {}

module.exports = (send) => {
const api = {
put: promisify((dagNode, options, callback) => {
if (typeof options === 'function') {
return setImmediate(() => callback(new Error('no options were passed')))
}

callback = callback || noop

let hashAlg = options.hashAlg || 'sha2-256'
let format
let inputEnc

if (options.cid && CID.isCID(options.cid)) {
format = options.cid.codec
hashAlg = multihash.decode(options.cid.multihash).name
prepare()
} else if (options.format) {
format = options.format
prepare()
} else {
callback(new Error('Invalid arguments'))
}

function prepare () {
if (format === 'dag-cbor') {
// TODO change this once
// https://github.com/ipfs/go-ipfs/issues/3771 is finished
format = 'cbor'

inputEnc = 'cbor'
dagCBOR.util.serialize(dagNode, finalize)
}
if (format === 'dag-pb') {
// TODO change this once
// https://github.com/ipfs/go-ipfs/issues/3771 is finished
format = 'protobuf'

inputEnc = 'protobuf'
dagPB.util.serialize(dagNode, finalize)
}
}

function finalize (err, serialized) {
if (err) { return callback(err) }

send({
path: 'dag/put',
qs: {
hashAlg: hashAlg, // not implemented in go yet https://github.com/ipfs/go-ipfs/issues/3771
format: format,
inputenc: inputEnc
},
files: serialized
}, (err, result) => {
if (err) {
return callback(err)
}
// TODO handle the result
})
}
}),
get: promisify((cid, path, options, callback) => {
// TODO
})
}

return api
}
34 changes: 34 additions & 0 deletions test/interface/dag.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-env mocha */

'use strict'

const test = require('interface-ipfs-core')
const parallel = require('async/parallel')

const IPFSApi = require('../../src')

const DaemonFactory = require('ipfsd-ctl')
const df = DaemonFactory.create()

const nodes = []
const common = {
setup: function (callback) {
callback(null, {
spawnNode: (cb) => {
df.spawn((err, _ipfsd) => {
if (err) {
return cb(err)
}

nodes.push(_ipfsd)
cb(null, IPFSApi(_ipfsd.apiAddr))
})
}
})
},
teardown: function (callback) {
parallel(nodes.map((node) => (cb) => node.stop(cb)), callback)
}
}

test.dag(common)

0 comments on commit 9463d3a

Please sign in to comment.