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

feat(dag): basics (put, get) #520

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"eslint-plugin-react": "^6.9.0",
"gulp": "^3.9.1",
"hapi": "^16.1.0",
"interface-ipfs-core": "^0.23.5",
"interface-ipfs-core": "^0.23.6",
"ipfsd-ctl": "^0.18.2",
"pre-commit": "^1.2.2",
"socket.io": "^1.7.2",
Expand Down Expand Up @@ -124,4 +124,4 @@
"url": "https://github.com/ipfs/js-ipfs-api/issues"
},
"homepage": "https://github.com/ipfs/js-ipfs-api"
}
}
69 changes: 69 additions & 0 deletions src/api/dag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict'

const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const DAGLink = dagPB.DAGLink
const promisify = require('promisify-es6')
const bs58 = require('bs58')
const streamToValue = require('../stream-to-value')
const cleanMultihash = require('../clean-multihash')
const LRU = require('lru-cache')

const lruOptions = {
max: 128
}

const Unixfs = require('ipfs-unixfs')
const cache = LRU(lruOptions)

module.exports = (send) => {
const api = {
get: promisify((cid, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}

options = options || {}

const node = cache.get(cid)

if (node) { return callback(null, node) }

send({
path: 'dag/get',
args: cid
}, (err, result) => {
if (err) {
return callback(err)
}

// TODO we need to get the raw bytes
})
}),

put: promisify((dagNode, multicodec, hashAlg, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}

options = options || {}

send({
path: 'dag/put',
qs: {
format: multicodec,
inputenc: enc
},
files: buf
}, (err, result) => {
if (err) { return callback(err) }

// TODO should get the CID back
})
})
}

return api
}
5 changes: 3 additions & 2 deletions src/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function requireCommands () {
mount: require('./api/mount'),
name: require('./api/name'),
object: require('./api/object'),
dag: require('./api/dag'),
pin: require('./api/pin'),
ping: require('./api/ping'),
refs: require('./api/refs'),
Expand All @@ -31,7 +32,7 @@ function requireCommands () {

// TODO: crowding the 'files' namespace temporarily for interface-ipfs-core
// compatibility, until 'files vs mfs' naming decision is resolved.
cmds.files = function (send) {
cmds.files = (send) => {
const files = require('./api/files')(send)
files.add = require('./api/add')(send)
files.createAddStream = require('./api/create-add-stream.js')(send)
Expand All @@ -41,7 +42,7 @@ function requireCommands () {
return files
}

cmds.util = function (send) {
cmds.util = (send) => {
const util = {
addFromFs: require('./api/util/fs-add')(send),
addFromStream: require('./api/add')(send),
Expand Down
20 changes: 20 additions & 0 deletions test/interface/dag.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-env mocha */

'use strict'

const test = require('interface-ipfs-core')
const FactoryClient = require('../ipfs-factory/client')

let fc

const common = {
setup: function (callback) {
fc = new FactoryClient()
callback(null, fc)
},
teardown: function (callback) {
fc.dismantle(callback)
}
}

test.dag(common)