|
1 | 1 | 'use strict' |
2 | 2 |
|
| 3 | +const Block = require('ipfs-block') |
| 4 | +const multihash = require('multihashes') |
| 5 | + |
3 | 6 | module.exports = function block (self) { |
4 | 7 | return { |
5 | | - get: (multihash, callback) => { |
6 | | - self._blockS.getBlock(multihash, callback) |
| 8 | + get: (hash, callback) => { |
| 9 | + hash = cleanHash(hash) |
| 10 | + |
| 11 | + self._blockS.getBlock(hash, callback) |
7 | 12 | }, |
8 | 13 | put: (block, callback) => { |
9 | | - self._blockS.addBlock(block, callback) |
| 14 | + if (Array.isArray(block)) { |
| 15 | + return callback(new Error('Array is not supported')) |
| 16 | + } |
| 17 | + if (Buffer.isBuffer(block)) { |
| 18 | + block = new Block(block) |
| 19 | + } |
| 20 | + |
| 21 | + self._blockS.addBlock(block, (err) => { |
| 22 | + callback(err, block) |
| 23 | + }) |
10 | 24 | }, |
11 | | - del: (multihash, callback) => { |
12 | | - self._blockS.deleteBlock(multihash, callback) |
| 25 | + del: (hash, callback) => { |
| 26 | + hash = cleanHash(hash) |
| 27 | + self._blockS.deleteBlock(hash, callback) |
13 | 28 | }, |
14 | | - stat: (multihash, callback) => { |
15 | | - self._blockS.getBlock(multihash, (err, block) => { |
| 29 | + stat: (hash, callback) => { |
| 30 | + hash = cleanHash(hash) |
| 31 | + |
| 32 | + self._blockS.getBlock(hash, (err, block) => { |
16 | 33 | if (err) { |
17 | 34 | return callback(err) |
18 | 35 | } |
19 | 36 | callback(null, { |
20 | | - Key: multihash, |
21 | | - Size: block.data.length |
| 37 | + key: hash, |
| 38 | + size: block.data.length |
22 | 39 | }) |
23 | 40 | }) |
24 | 41 | } |
25 | 42 | } |
26 | 43 | } |
| 44 | + |
| 45 | +function cleanHash (hash) { |
| 46 | + if (typeof hash === 'string') { |
| 47 | + return multihash.fromB58String(hash) |
| 48 | + } |
| 49 | + return hash |
| 50 | +} |
0 commit comments