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

Commit

Permalink
use object tests from interface-ipfs-core
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed May 13, 2016
1 parent e8f5b64 commit 7852050
Show file tree
Hide file tree
Showing 3 changed files with 267 additions and 257 deletions.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
"jsnext:main": "src/index.js",
"dependencies": {
"babel-runtime": "^6.6.1",
"bs58": "^3.0.0",
"detect-node": "^2.0.3",
"flatmap": "0.0.3",
"glob": "^7.0.3",
"ipfs-merkle-dag": "^0.5.1",
"multiaddr": "^1.3.0",
"multipart-stream": "^2.0.1",
"ndjson": "^1.4.3",
"promisify-es6": "^1.0.1",
"qs": "^6.1.0",
"wreck": "^7.0.2"
},
Expand All @@ -23,9 +26,10 @@
"url": "https://github.com/ipfs/js-ipfs-api"
},
"devDependencies": {
"aegir": "^3.0.2",
"chai": "^3.5.0",
"aegir": "^3.0.1",
"gulp": "^3.9.1",
"interface-ipfs-core": "^0.1.2",
"ipfsd-ctl": "^0.13.0",
"pre-commit": "^1.1.2",
"raw-loader": "^0.5.1",
Expand Down Expand Up @@ -93,4 +97,4 @@
}
}
}
}
}
274 changes: 250 additions & 24 deletions src/api/object.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,259 @@
'use strict'

const argCommand = require('../cmd-helpers').argCommand
const DAGNode = require('ipfs-merkle-dag').DAGNode
const DAGLink = require('ipfs-merkle-dag').DAGLink
const promisify = require('promisify-es6')
const bs58 = require('bs58')
const bl = require('bl')

module.exports = (send) => {
return {
get: argCommand(send, 'object/get'),
put (file, encoding, cb) {
if (typeof encoding === 'function') {
return cb(null, new Error("Must specify an object encoding ('json' or 'protobuf')"))
}
return send('object/put', encoding, null, file, cb)
},
data: argCommand(send, 'object/data'),
links: argCommand(send, 'object/links'),
stat: argCommand(send, 'object/stat'),
new: argCommand(send, 'object/new'),
const api = {
get: promisify((multihash, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
if (!options) {
options = {}
}
multihash = cleanMultihash(multihash, options)

send('object/get', multihash, null, null, (err, result) => {
if (err) {
return callback(err)
}

const node = new DAGNode(result.Data, result.Links.map(
(l) => {
return new DAGLink(l.Name, l.Size, new Buffer(bs58.decode(l.Hash)))
}))

callback(null, node)
})
}),
put: promisify((obj, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
if (!options) {
options = {}
}

let tmpObj = {
Data: null,
Links: []
}

if (Buffer.isBuffer(obj)) {
if (!options.enc) {
tmpObj = { Data: obj.toString(), Links: [] }
}
} else if (obj.multihash) {
tmpObj = {
Data: obj.data.toString(),
Links: obj.links.map((l) => { return l.toJSON() })
}
} else if (typeof obj === 'object') {
tmpObj.Data = obj.Data.toString()
} else {
return callback(new Error('obj not recognized'))
}

let buf
if (Buffer.isBuffer(obj) && options.enc) {
buf = obj
} else {
buf = new Buffer(JSON.stringify(tmpObj))
}
const enc = options.enc || 'json'

send('object/put', enc, null, buf, (err, result) => {
if (err) {
return callback(err)
}

if (Buffer.isBuffer(obj)) {
if (!options.enc) {
obj = { Data: obj, Links: [] }
} else {
obj = JSON.parse(obj.toString())
}
}
let node
if (obj.multihash) {
node = obj
} else {
node = new DAGNode(obj.Data, obj.Links)
}

if (node.toJSON().Hash !== result.Hash) {
return callback(new Error('Stored object was different from constructed object'))
}

callback(null, node)
})
}),
data: promisify((multihash, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
if (!options) {
options = {}
}
multihash = cleanMultihash(multihash, options)

send('object/data', multihash, null, null, (err, result) => {
if (err) {
return callback(err)
}

result.pipe(bl(callback))
})
}),
links: promisify((multihash, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
if (!options) {
options = {}
}
multihash = cleanMultihash(multihash, options)

send('object/links', multihash, null, null, (err, result) => {
if (err) {
return callback(err)
}

let links = []

if (result.Links) {
links = result.Links.map((l) => {
return new DAGLink(l.Name, l.Size, new Buffer(bs58.decode(l.Hash)))
})
}
callback(null, links)
})
}),
stat: promisify((multihash, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
if (!options) {
options = {}
}
multihash = cleanMultihash(multihash, options)

send('object/stat', multihash, null, null, callback)
}),
new: promisify((callback) => {
send('object/new', null, null, null, (err, result) => {
if (err) {
return callback(err)
}
const node = new DAGNode()

if (node.toJSON().Hash !== result.Hash) {
return callback(new Error('Stored object was different from constructed object'))
}

callback(null, node)
})
}),
patch: {
rmLink: (root, link, cb) => {
return send('object/patch/rm-link', [root, link], null, null, cb)
},
setData: (root, data, cb) => {
return send('object/patch/set-data', [root], null, data, cb)
},
appendData: (root, data, cb) => {
return send('object/patch/append-data', [root], null, data, cb)
},
addLink: (root, name, ref, cb) => {
return send('object/patch/add-link', [root, name, ref], null, null, cb)
addLink: promisify((multihash, dLink, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
if (!options) {
options = {}
}
multihash = cleanMultihash(multihash, options)

send('object/patch/add-link', [multihash, dLink.name, bs58.encode(dLink.hash).toString()], null, null, (err, result) => {
if (err) {
return callback(err)
}
api.get(result.Hash, { enc: 'base58' }, callback)
})
}),
rmLink: promisify((multihash, dLink, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
if (!options) {
options = {}
}
multihash = cleanMultihash(multihash, options)

send('object/patch/rm-link', [multihash, dLink.name], null, null, (err, result) => {
if (err) {
return callback(err)
}
api.get(result.Hash, { enc: 'base58' }, callback)
})
}),
setData: promisify((multihash, data, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
if (!options) {
options = {}
}
multihash = cleanMultihash(multihash, options)

send('object/patch/set-data', [multihash], null, data, (err, result) => {
if (err) {
return callback(err)
}
api.get(result.Hash, { enc: 'base58' }, callback)
})
}),
appendData: promisify((multihash, data, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
if (!options) {
options = {}
}
multihash = cleanMultihash(multihash, options)

send('object/patch/append-data', [multihash], null, data, (err, result) => {
if (err) {
return callback(err)
}
api.get(result.Hash, { enc: 'base58' }, callback)
})
})
}
}
return api
}

function cleanMultihash (multihash, options) {
if (Buffer.isBuffer(multihash)) {
if (options.enc) {
switch (options.enc) {
case 'base58': multihash = multihash.toString()
}
} else {
multihash = bs58.encode(multihash).toString()
}
} else if (typeof multihash === 'string') {
if (options.enc) {
switch (options.enc) {
case 'base58': ; // It is good
}
} else {
throw new Error('not valid multihash')
}
}
return multihash
}
Loading

0 comments on commit 7852050

Please sign in to comment.