This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #267 from ipfs/use-interface-ipfs-core
use object tests from interface-ipfs-core
- Loading branch information
Showing
3 changed files
with
272 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { | ||
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() | ||
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 | ||
} |
Oops, something went wrong.