diff --git a/package.json b/package.json index 49140ed422..f51fa89c86 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "homepage": "https://github.com/ipfs/js-ipfs#readme", "devDependencies": { - "aegir": "^8.0.1", + "aegir": "^8.1.2", "buffer-loader": "0.0.1", "chai": "^3.5.0", "execa": "^0.5.0", @@ -48,7 +48,7 @@ "form-data": "^2.0.0", "fs-pull-blob-store": "^0.4.1", "gulp": "^3.9.1", - "interface-ipfs-core": "^0.15.0", + "interface-ipfs-core": "^0.16.6", "left-pad": "^1.1.1", "lodash": "^4.15.0", "ncp": "^2.0.0", @@ -70,14 +70,15 @@ "glob": "^7.0.6", "hapi": "^15.0.3", "idb-pull-blob-store": "^0.5.1", - "ipfs-api": "^9.0.0", + "ipfs-api": "^10.0.0", "ipfs-bitswap": "^0.7.0", "ipfs-block": "^0.4.0", "ipfs-block-service": "^0.6.0", "ipfs-multipart": "^0.1.0", "ipfs-repo": "^0.10.0", "ipfs-unixfs": "^0.1.4", - "ipfs-unixfs-engine": "^0.11.3", + "ipfs-unixfs-engine": "^0.12.0", + "ipld-resolver": "^0.1.1", "isstream": "^0.1.2", "joi": "^9.0.4", "libp2p-ipfs": "^0.14.1", @@ -97,7 +98,7 @@ "peer-id": "^0.7.0", "peer-info": "^0.7.1", "promisify-es6": "^1.0.1", - "pull-file": "^1.1ed.0", + "pull-file": "^1.0.0", "pull-paramap": "^1.1.6", "pull-pushable": "^2.0.1", "pull-sort": "^1.0.0", diff --git a/src/core/components/files.js b/src/core/components/files.js index d6bbf880f3..7cfdc02632 100644 --- a/src/core/components/files.js +++ b/src/core/components/files.js @@ -32,7 +32,7 @@ module.exports = function files (self) { add: promisify((data, callback) => { if (!callback || typeof callback !== 'function') { - callback = function noop () {} + callback = noop } pull( @@ -97,17 +97,22 @@ module.exports = function files (self) { } } -function prepareFile (self, file, cb) { +function prepareFile (self, file, callback) { const bs58mh = multihashes.toB58String(file.multihash) self.object.get(file.multihash, (err, node) => { if (err) { - return cb(err) + return callback(err) } - cb(null, { - path: file.path || bs58mh, - hash: bs58mh, - size: node.size() + node.size((err, size) => { + if (err) { + return callback(err) + } + callback(null, { + path: file.path || bs58mh, + hash: bs58mh, + size: size + }) }) }) } @@ -147,3 +152,5 @@ function normalizeContent (content) { return data }) } + +function noop () {} diff --git a/src/core/components/object.js b/src/core/components/object.js index 902fdc2e4f..503bff8739 100644 --- a/src/core/components/object.js +++ b/src/core/components/object.js @@ -22,18 +22,19 @@ function normalizeMultihash (multihash, enc) { } } -function parseBuffer (buf, encoding) { +function parseBuffer (buf, encoding, callback) { switch (encoding) { case 'json': - return parseJSONBuffer(buf) + return parseJSONBuffer(buf, callback) case 'protobuf': - return parseProtoBuffer(buf) + return parseProtoBuffer(buf, callback) default: - throw new Error(`unkown encoding: ${encoding}`) + callback(new Error(`unkown encoding: ${encoding}`)) } } -function parseJSONBuffer (buf) { +function parseJSONBuffer (buf, callback) { + let node try { const parsed = JSON.parse(buf.toString()) const links = (parsed.Links || []).map((link) => { @@ -43,21 +44,22 @@ function parseJSONBuffer (buf) { new Buffer(bs58.decode(link.Hash)) ) }) - return new DAGNode(new Buffer(parsed.Data), links) + node = new DAGNode(new Buffer(parsed.Data), links) } catch (err) { - throw new Error('failed to parse JSON: ' + err) + return callback(new Error('failed to parse JSON: ' + err)) } + callback(null, node) } -function parseProtoBuffer (buf) { - return dagPB.util.deserialize(buf) +function parseProtoBuffer (buf, callback) { + dagPB.util.deserialize(buf, callback) } module.exports = function object (self) { function editAndSave (edit) { - return (multihash, options, cb) => { + return (multihash, options, callback) => { if (typeof options === 'function') { - cb = options + callback = options options = {} } @@ -68,35 +70,45 @@ module.exports = function object (self) { (node, cb) => { node = edit(node) - self._ipldResolver.put({ - node: node, - cid: new CID(node.multihash()) - }, (err) => { - cb(err, node) + node.multihash((err, multihash) => { + if (err) { + return cb(err) + } + self._ipldResolver.put({ + node: node, + cid: new CID(multihash) + }, (err) => { + cb(err, node) + }) }) } - ], cb) + ], callback) } } return { - new: promisify((cb) => { + new: promisify((callback) => { const node = new DAGNode() - self._ipldResolver.put({ - node: node, - cid: new CID(node.multihash()) - }, function (err) { + node.multihash((err, multihash) => { if (err) { - return cb(err) + return callback(err) } + self._ipldResolver.put({ + node: node, + cid: new CID(multihash) + }, function (err) { + if (err) { + return callback(err) + } - cb(null, node) + callback(null, node) + }) }) }), - put: promisify((obj, options, cb) => { + put: promisify((obj, options, callback) => { if (typeof options === 'function') { - cb = options + callback = options options = {} } @@ -105,11 +117,14 @@ module.exports = function object (self) { if (Buffer.isBuffer(obj)) { if (encoding) { - try { - node = parseBuffer(obj, encoding) - } catch (err) { - return cb(err) - } + parseBuffer(obj, encoding, (err, _node) => { + if (err) { + return callback(err) + } + node = _node + next() + }) + return } else { node = new DAGNode(obj) } @@ -119,24 +134,33 @@ module.exports = function object (self) { } else if (typeof obj === 'object') { node = new DAGNode(obj.Data, obj.Links) } else { - return cb(new Error('obj not recognized')) + return callback(new Error('obj not recognized')) } - self._ipldResolver.put({ - node: node, - cid: new CID(node.multihash()) - }, (err, block) => { - if (err) { - return cb(err) - } + next() - self.object.get(node.multihash(), cb) - }) + function next () { + node.multihash((err, multihash) => { + if (err) { + return callback(err) + } + self._ipldResolver.put({ + node: node, + cid: new CID(multihash) + }, (err, block) => { + if (err) { + return callback(err) + } + + self.object.get(multihash, callback) + }) + }) + } }), - get: promisify((multihash, options, cb) => { + get: promisify((multihash, options, callback) => { if (typeof options === 'function') { - cb = options + callback = options options = {} } @@ -145,75 +169,87 @@ module.exports = function object (self) { try { mh = normalizeMultihash(multihash, options.enc) } catch (err) { - return cb(err) + return callback(err) } const cid = new CID(mh) - self._ipldResolver.get(cid, cb) + self._ipldResolver.get(cid, callback) }), - data: promisify((multihash, options, cb) => { + data: promisify((multihash, options, callback) => { if (typeof options === 'function') { - cb = options + callback = options options = {} } self.object.get(multihash, options, (err, node) => { if (err) { - return cb(err) + return callback(err) } - cb(null, node.data) + callback(null, node.data) }) }), - links: promisify((multihash, options, cb) => { + links: promisify((multihash, options, callback) => { if (typeof options === 'function') { - cb = options + callback = options options = {} } self.object.get(multihash, options, (err, node) => { if (err) { - return cb(err) + return callback(err) } - cb(null, node.links) + callback(null, node.links) }) }), - stat: promisify((multihash, options, cb) => { + stat: promisify((multihash, options, callback) => { if (typeof options === 'function') { - cb = options + callback = options options = {} } self.object.get(multihash, options, (err, node) => { if (err) { - return cb(err) + return callback(err) } - const blockSize = dagPB.util.serialize(node).length - const linkLength = node.links.reduce((a, l) => a + l.size, 0) + dagPB.util.serialize(node, (err, serialized) => { + if (err) { + return callback(err) + } + + const blockSize = serialized.length + const linkLength = node.links.reduce((a, l) => a + l.size, 0) - cb(null, { - Hash: node.toJSON().Hash, - NumLinks: node.links.length, - BlockSize: blockSize, - LinksSize: blockSize - node.data.length, - DataSize: node.data.length, - CumulativeSize: blockSize + linkLength + node.toJSON((err, nodeJSON) => { + if (err) { + return callback(err) + } + + callback(null, { + Hash: nodeJSON.Hash, + NumLinks: node.links.length, + BlockSize: blockSize, + LinksSize: blockSize - node.data.length, + DataSize: node.data.length, + CumulativeSize: blockSize + linkLength + }) + }) }) }) }), patch: promisify({ - addLink (multihash, link, options, cb) { + addLink (multihash, link, options, callback) { editAndSave((node) => { node.addRawLink(link) return node - })(multihash, options, cb) + })(multihash, options, callback) }, - rmLink (multihash, linkRef, options, cb) { + rmLink (multihash, linkRef, options, callback) { editAndSave((node) => { node.links = node.links.filter((link) => { if (typeof linkRef === 'string') { @@ -231,21 +267,21 @@ module.exports = function object (self) { return !link.hash.equals(linkRef.hash) }) return node - })(multihash, options, cb) + })(multihash, options, callback) }, - appendData (multihash, data, options, cb) { + appendData (multihash, data, options, callback) { editAndSave((node) => { node.data = Buffer.concat([node.data, data]) return node - })(multihash, options, cb) + })(multihash, options, callback) }, - setData (multihash, data, options, cb) { + setData (multihash, data, options, callback) { editAndSave((node) => { node.data = data return node - })(multihash, options, cb) + })(multihash, options, callback) } }) } diff --git a/src/http-api/resources/object.js b/src/http-api/resources/object.js index d3454e13e8..c8399ba910 100644 --- a/src/http-api/resources/object.js +++ b/src/http-api/resources/object.js @@ -2,12 +2,12 @@ const bs58 = require('bs58') const multipart = require('ipfs-multipart') -const mDAG = require('ipfs-merkle-dag') -const DAGLink = mDAG.DAGLink +const dagPB = require('ipld-dag-pb') +const DAGLink = dagPB.DAGLink +const DAGNode = dagPB.DAGNode const debug = require('debug') const log = debug('http-api:object') log.error = debug('http-api:object:error') -const DAGNode = mDAG.DAGNode exports = module.exports