-
Notifications
You must be signed in to change notification settings - Fork 296
use object tests from interface-ipfs-core #267
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,264 @@ | ||
'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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about layouts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried all the other layouts through the CLI and only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I made it fully work in js-ipfs :/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you like to propose that we shim that feature in, although go-ipfs not supporting? All is possible with code 🎤 :D |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are you using a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
case 'base58': { | ||
multihash = multihash.toString() | ||
break | ||
} | ||
default: throw new Error('invalid multihash') | ||
} | ||
} else { | ||
multihash = bs58.encode(multihash).toString() | ||
} | ||
} else if (typeof multihash === 'string') { | ||
if (options.enc) { | ||
// For the future, when we support more than one enc | ||
// switch (options.enc) { | ||
// case 'base58': // It is good | ||
// } | ||
} else { | ||
throw new Error('not valid multihash') | ||
} | ||
} | ||
return multihash | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to support the variety of ways
ipfs.object.put
can be called, I had to break the elegant promise injection that was there before. @dignifiedquire wanna share some wisdom on how we can add promises again, in the most elegant way possible?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's probably best to just wrap the function using sth like this: https://github.com/manuel-di-iorio/promisify-es6