|
| 1 | +// ordinarily we'd open a PR against the multicodec module to get our |
| 2 | +// codec number added but since we're just testing we shim our new |
| 3 | +// codec into the base-table.json file - this has to be done |
| 4 | +// before requiring other modules as the int table will become read-only |
| 5 | +const codecName = 'dag-test' |
| 6 | +const codecNumber = 392091 |
| 7 | + |
| 8 | +const baseTable = require('multicodec/src/base-table.json') |
| 9 | +baseTable[codecName] = codecNumber |
| 10 | + |
| 11 | +// now require modules as usual |
| 12 | +const IPFSDaemon = require('ipfs-cli/src/daemon') |
| 13 | +const multihashing = require('multihashing-async') |
| 14 | +const multihash = multihashing.multihash |
| 15 | +const multicodec = require('multicodec') |
| 16 | +const CID = require('cids') |
| 17 | +const ipfsHttpClient = require('ipfs-http-client') |
| 18 | +const uint8ArrayToString = require('uint8arrays/to-string') |
| 19 | + |
| 20 | +async function main () { |
| 21 | + // see https://github.com/ipld/interface-ipld-format for the interface definition |
| 22 | + const format = { |
| 23 | + codec: codecNumber, |
| 24 | + defaultHashAlg: multicodec.SHA2_256, |
| 25 | + util: { |
| 26 | + serialize (data) { |
| 27 | + return Buffer.from(JSON.stringify(data)) |
| 28 | + }, |
| 29 | + deserialize (buf) { |
| 30 | + return JSON.parse(uint8ArrayToString(buf)) |
| 31 | + }, |
| 32 | + async cid (buf) { |
| 33 | + const multihash = await multihashing(buf, format.defaultHashAlg) |
| 34 | + |
| 35 | + return new CID(1, format.codec, multihash) |
| 36 | + } |
| 37 | + }, |
| 38 | + resolver: { |
| 39 | + resolve: (buf, path) => { |
| 40 | + return { |
| 41 | + value: format.util.deserialize(buf), |
| 42 | + remainderPath: path |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // start an IPFS Daemon |
| 49 | + const daemon = new IPFSDaemon({ |
| 50 | + ipld: { |
| 51 | + formats: [ |
| 52 | + format |
| 53 | + ] |
| 54 | + } |
| 55 | + }) |
| 56 | + await daemon.start() |
| 57 | + |
| 58 | + // in another process: |
| 59 | + const client = ipfsHttpClient({ |
| 60 | + url: `http://localhost:${daemon._httpApi._apiServers[0].info.port}`, |
| 61 | + ipld: { |
| 62 | + formats: [ |
| 63 | + format |
| 64 | + ] |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + const data = { |
| 69 | + hello: 'world' |
| 70 | + } |
| 71 | + |
| 72 | + const cid = await client.dag.put(data, { |
| 73 | + format: codecName, |
| 74 | + hashAlg: multihash.codes[format.defaultHashAlg] |
| 75 | + }) |
| 76 | + |
| 77 | + console.info(`Put ${JSON.stringify(data)} = CID(${cid})`) |
| 78 | + |
| 79 | + const { |
| 80 | + value |
| 81 | + } = await client.dag.get(cid) |
| 82 | + |
| 83 | + console.info(`Get CID(${cid}) = ${JSON.stringify(value)}`) |
| 84 | + |
| 85 | + await daemon.stop() |
| 86 | +} |
| 87 | + |
| 88 | +main() |
| 89 | + .catch(err => { |
| 90 | + console.error(err) |
| 91 | + process.exit(1) |
| 92 | + }) |
| 93 | + .then(() => { |
| 94 | + // https://github.com/libp2p/js-libp2p/issues/779 |
| 95 | + process.exit(0) |
| 96 | + }) |
0 commit comments