|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -const argCommand = require('../cmd-helpers').argCommand |
| 3 | +const DAGNode = require('ipfs-merkle-dag').DAGNode |
| 4 | +const DAGLink = require('ipfs-merkle-dag').DAGLink |
| 5 | +const promisify = require('promisify-es6') |
| 6 | +const bs58 = require('bs58') |
| 7 | +const bl = require('bl') |
4 | 8 |
|
5 | 9 | module.exports = (send) => {
|
6 |
| - return { |
7 |
| - get: argCommand(send, 'object/get'), |
8 |
| - put (file, encoding, cb) { |
9 |
| - if (typeof encoding === 'function') { |
10 |
| - return cb(null, new Error("Must specify an object encoding ('json' or 'protobuf')")) |
11 |
| - } |
12 |
| - return send('object/put', encoding, null, file, cb) |
13 |
| - }, |
14 |
| - data: argCommand(send, 'object/data'), |
15 |
| - links: argCommand(send, 'object/links'), |
16 |
| - stat: argCommand(send, 'object/stat'), |
17 |
| - new: argCommand(send, 'object/new'), |
| 10 | + const api = { |
| 11 | + get: promisify((multihash, options, callback) => { |
| 12 | + if (typeof options === 'function') { |
| 13 | + callback = options |
| 14 | + options = {} |
| 15 | + } |
| 16 | + if (!options) { |
| 17 | + options = {} |
| 18 | + } |
| 19 | + multihash = cleanMultihash(multihash, options) |
| 20 | + |
| 21 | + send('object/get', multihash, null, null, (err, result) => { |
| 22 | + if (err) { |
| 23 | + return callback(err) |
| 24 | + } |
| 25 | + |
| 26 | + const node = new DAGNode(result.Data, result.Links.map( |
| 27 | + (l) => { |
| 28 | + return new DAGLink(l.Name, l.Size, new Buffer(bs58.decode(l.Hash))) |
| 29 | + })) |
| 30 | + |
| 31 | + callback(null, node) |
| 32 | + }) |
| 33 | + }), |
| 34 | + put: promisify((obj, options, callback) => { |
| 35 | + if (typeof options === 'function') { |
| 36 | + callback = options |
| 37 | + options = {} |
| 38 | + } |
| 39 | + if (!options) { |
| 40 | + options = {} |
| 41 | + } |
| 42 | + |
| 43 | + let tmpObj = { |
| 44 | + Data: null, |
| 45 | + Links: [] |
| 46 | + } |
| 47 | + |
| 48 | + if (Buffer.isBuffer(obj)) { |
| 49 | + if (!options.enc) { |
| 50 | + tmpObj = { Data: obj.toString(), Links: [] } |
| 51 | + } |
| 52 | + } else if (obj.multihash) { |
| 53 | + tmpObj = { |
| 54 | + Data: obj.data.toString(), |
| 55 | + Links: obj.links.map((l) => { return l.toJSON() }) |
| 56 | + } |
| 57 | + } else if (typeof obj === 'object') { |
| 58 | + tmpObj.Data = obj.Data.toString() |
| 59 | + } else { |
| 60 | + return callback(new Error('obj not recognized')) |
| 61 | + } |
| 62 | + |
| 63 | + let buf |
| 64 | + if (Buffer.isBuffer(obj) && options.enc) { |
| 65 | + buf = obj |
| 66 | + } else { |
| 67 | + buf = new Buffer(JSON.stringify(tmpObj)) |
| 68 | + } |
| 69 | + const enc = options.enc || 'json' |
| 70 | + |
| 71 | + send('object/put', enc, null, buf, (err, result) => { |
| 72 | + if (err) { |
| 73 | + return callback(err) |
| 74 | + } |
| 75 | + |
| 76 | + if (Buffer.isBuffer(obj)) { |
| 77 | + if (!options.enc) { |
| 78 | + obj = { Data: obj, Links: [] } |
| 79 | + } else { |
| 80 | + obj = JSON.parse(obj.toString()) |
| 81 | + } |
| 82 | + } |
| 83 | + let node |
| 84 | + if (obj.multihash) { |
| 85 | + node = obj |
| 86 | + } else { |
| 87 | + node = new DAGNode(obj.Data, obj.Links) |
| 88 | + } |
| 89 | + |
| 90 | + if (node.toJSON().Hash !== result.Hash) { |
| 91 | + return callback(new Error('Stored object was different from constructed object')) |
| 92 | + } |
| 93 | + |
| 94 | + callback(null, node) |
| 95 | + }) |
| 96 | + }), |
| 97 | + data: promisify((multihash, options, callback) => { |
| 98 | + if (typeof options === 'function') { |
| 99 | + callback = options |
| 100 | + options = {} |
| 101 | + } |
| 102 | + if (!options) { |
| 103 | + options = {} |
| 104 | + } |
| 105 | + multihash = cleanMultihash(multihash, options) |
| 106 | + |
| 107 | + send('object/data', multihash, null, null, (err, result) => { |
| 108 | + if (err) { |
| 109 | + return callback(err) |
| 110 | + } |
| 111 | + |
| 112 | + result.pipe(bl(callback)) |
| 113 | + }) |
| 114 | + }), |
| 115 | + links: promisify((multihash, options, callback) => { |
| 116 | + if (typeof options === 'function') { |
| 117 | + callback = options |
| 118 | + options = {} |
| 119 | + } |
| 120 | + if (!options) { |
| 121 | + options = {} |
| 122 | + } |
| 123 | + multihash = cleanMultihash(multihash, options) |
| 124 | + |
| 125 | + send('object/links', multihash, null, null, (err, result) => { |
| 126 | + if (err) { |
| 127 | + return callback(err) |
| 128 | + } |
| 129 | + |
| 130 | + let links = [] |
| 131 | + |
| 132 | + if (result.Links) { |
| 133 | + links = result.Links.map((l) => { |
| 134 | + return new DAGLink(l.Name, l.Size, new Buffer(bs58.decode(l.Hash))) |
| 135 | + }) |
| 136 | + } |
| 137 | + callback(null, links) |
| 138 | + }) |
| 139 | + }), |
| 140 | + stat: promisify((multihash, options, callback) => { |
| 141 | + if (typeof options === 'function') { |
| 142 | + callback = options |
| 143 | + options = {} |
| 144 | + } |
| 145 | + if (!options) { |
| 146 | + options = {} |
| 147 | + } |
| 148 | + multihash = cleanMultihash(multihash, options) |
| 149 | + |
| 150 | + send('object/stat', multihash, null, null, callback) |
| 151 | + }), |
| 152 | + new: promisify((callback) => { |
| 153 | + send('object/new', null, null, null, (err, result) => { |
| 154 | + if (err) { |
| 155 | + return callback(err) |
| 156 | + } |
| 157 | + const node = new DAGNode() |
| 158 | + |
| 159 | + if (node.toJSON().Hash !== result.Hash) { |
| 160 | + return callback(new Error('Stored object was different from constructed object')) |
| 161 | + } |
| 162 | + |
| 163 | + callback(null, node) |
| 164 | + }) |
| 165 | + }), |
18 | 166 | patch: {
|
19 |
| - rmLink: (root, link, cb) => { |
20 |
| - return send('object/patch/rm-link', [root, link], null, null, cb) |
21 |
| - }, |
22 |
| - setData: (root, data, cb) => { |
23 |
| - return send('object/patch/set-data', [root], null, data, cb) |
24 |
| - }, |
25 |
| - appendData: (root, data, cb) => { |
26 |
| - return send('object/patch/append-data', [root], null, data, cb) |
27 |
| - }, |
28 |
| - addLink: (root, name, ref, cb) => { |
29 |
| - return send('object/patch/add-link', [root, name, ref], null, null, cb) |
| 167 | + addLink: promisify((multihash, dLink, options, callback) => { |
| 168 | + if (typeof options === 'function') { |
| 169 | + callback = options |
| 170 | + options = {} |
| 171 | + } |
| 172 | + if (!options) { |
| 173 | + options = {} |
| 174 | + } |
| 175 | + multihash = cleanMultihash(multihash, options) |
| 176 | + |
| 177 | + send('object/patch/add-link', [multihash, dLink.name, bs58.encode(dLink.hash).toString()], null, null, (err, result) => { |
| 178 | + if (err) { |
| 179 | + return callback(err) |
| 180 | + } |
| 181 | + api.get(result.Hash, { enc: 'base58' }, callback) |
| 182 | + }) |
| 183 | + }), |
| 184 | + rmLink: promisify((multihash, dLink, options, callback) => { |
| 185 | + if (typeof options === 'function') { |
| 186 | + callback = options |
| 187 | + options = {} |
| 188 | + } |
| 189 | + if (!options) { |
| 190 | + options = {} |
| 191 | + } |
| 192 | + multihash = cleanMultihash(multihash, options) |
| 193 | + |
| 194 | + send('object/patch/rm-link', [multihash, dLink.name], null, null, (err, result) => { |
| 195 | + if (err) { |
| 196 | + return callback(err) |
| 197 | + } |
| 198 | + api.get(result.Hash, { enc: 'base58' }, callback) |
| 199 | + }) |
| 200 | + }), |
| 201 | + setData: promisify((multihash, data, options, callback) => { |
| 202 | + if (typeof options === 'function') { |
| 203 | + callback = options |
| 204 | + options = {} |
| 205 | + } |
| 206 | + if (!options) { |
| 207 | + options = {} |
| 208 | + } |
| 209 | + multihash = cleanMultihash(multihash, options) |
| 210 | + |
| 211 | + send('object/patch/set-data', [multihash], null, data, (err, result) => { |
| 212 | + if (err) { |
| 213 | + return callback(err) |
| 214 | + } |
| 215 | + api.get(result.Hash, { enc: 'base58' }, callback) |
| 216 | + }) |
| 217 | + }), |
| 218 | + appendData: promisify((multihash, data, options, callback) => { |
| 219 | + if (typeof options === 'function') { |
| 220 | + callback = options |
| 221 | + options = {} |
| 222 | + } |
| 223 | + if (!options) { |
| 224 | + options = {} |
| 225 | + } |
| 226 | + multihash = cleanMultihash(multihash, options) |
| 227 | + |
| 228 | + send('object/patch/append-data', [multihash], null, data, (err, result) => { |
| 229 | + if (err) { |
| 230 | + return callback(err) |
| 231 | + } |
| 232 | + api.get(result.Hash, { enc: 'base58' }, callback) |
| 233 | + }) |
| 234 | + }) |
| 235 | + } |
| 236 | + } |
| 237 | + return api |
| 238 | +} |
| 239 | + |
| 240 | +function cleanMultihash (multihash, options) { |
| 241 | + if (Buffer.isBuffer(multihash)) { |
| 242 | + if (options.enc) { |
| 243 | + switch (options.enc) { |
| 244 | + case 'base58': { |
| 245 | + multihash = multihash.toString() |
| 246 | + break |
| 247 | + } |
| 248 | + default: throw new Error('invalid multihash') |
30 | 249 | }
|
| 250 | + } else { |
| 251 | + multihash = bs58.encode(multihash).toString() |
| 252 | + } |
| 253 | + } else if (typeof multihash === 'string') { |
| 254 | + if (options.enc) { |
| 255 | + // For the future, when we support more than one enc |
| 256 | + // switch (options.enc) { |
| 257 | + // case 'base58': // It is good |
| 258 | + // } |
| 259 | + } else { |
| 260 | + throw new Error('not valid multihash') |
31 | 261 | }
|
32 | 262 | }
|
| 263 | + return multihash |
33 | 264 | }
|
0 commit comments