|
| 1 | +/* eslint-env mocha */ |
| 2 | +'use strict' |
| 3 | + |
| 4 | +const chai = require('chai') |
| 5 | +chai.use(require('dirty-chai')) |
| 6 | +const expect = chai.expect |
| 7 | +const IPLD = require('ipld') |
| 8 | +const UnixFS = require('ipfs-unixfs') |
| 9 | +const pull = require('pull-stream') |
| 10 | +const CID = require('cids') |
| 11 | +const waterfall = require('async/waterfall') |
| 12 | +const parallel = require('async/parallel') |
| 13 | +const randomBytes = require('./helpers/random-bytes') |
| 14 | + |
| 15 | +const exporter = require('../src') |
| 16 | +const importer = require('ipfs-unixfs-importer') |
| 17 | + |
| 18 | +const SHARD_SPLIT_THRESHOLD = 1000 |
| 19 | + |
| 20 | +describe('exporter sharded', () => { |
| 21 | + let ipld |
| 22 | + |
| 23 | + before((done) => { |
| 24 | + IPLD.inMemory((err, resolver) => { |
| 25 | + expect(err).to.not.exist() |
| 26 | + |
| 27 | + ipld = resolver |
| 28 | + |
| 29 | + done() |
| 30 | + }) |
| 31 | + }) |
| 32 | + |
| 33 | + it('exports a sharded directory', (done) => { |
| 34 | + const files = {} |
| 35 | + let directory |
| 36 | + |
| 37 | + for (let i = 0; i < (SHARD_SPLIT_THRESHOLD + 1); i++) { |
| 38 | + files[`file-${Math.random()}.txt`] = { |
| 39 | + content: randomBytes(100) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + waterfall([ |
| 44 | + (cb) => pull( |
| 45 | + pull.values( |
| 46 | + Object.keys(files).map(path => ({ |
| 47 | + path, |
| 48 | + content: files[path].content |
| 49 | + })) |
| 50 | + ), |
| 51 | + importer(ipld, { |
| 52 | + wrap: true |
| 53 | + }), |
| 54 | + pull.collect(cb) |
| 55 | + ), |
| 56 | + (imported, cb) => { |
| 57 | + directory = new CID(imported.pop().multihash) |
| 58 | + |
| 59 | + // store the CIDs, we will validate them later |
| 60 | + imported.forEach(imported => { |
| 61 | + files[imported.path].cid = new CID(imported.multihash) |
| 62 | + }) |
| 63 | + |
| 64 | + ipld.get(directory, cb) |
| 65 | + }, |
| 66 | + ({ value, cid }, cb) => { |
| 67 | + const dir = UnixFS.unmarshal(value.data) |
| 68 | + |
| 69 | + expect(dir.type).to.equal('hamt-sharded-directory') |
| 70 | + |
| 71 | + pull( |
| 72 | + exporter(directory, ipld), |
| 73 | + pull.collect(cb) |
| 74 | + ) |
| 75 | + }, |
| 76 | + (exported, cb) => { |
| 77 | + const dir = exported.shift() |
| 78 | + |
| 79 | + expect(dir.hash).to.deep.equal(directory.buffer) |
| 80 | + |
| 81 | + parallel( |
| 82 | + exported.map(exported => (cb) => { |
| 83 | + pull( |
| 84 | + exported.content, |
| 85 | + pull.collect((err, bufs) => { |
| 86 | + if (err) { |
| 87 | + cb(err) |
| 88 | + } |
| 89 | + |
| 90 | + // validate the CID |
| 91 | + expect(files[exported.name].cid.buffer).to.deep.equal(exported.hash) |
| 92 | + |
| 93 | + // validate the exported file content |
| 94 | + expect(files[exported.name].content).to.deep.equal(bufs[0]) |
| 95 | + |
| 96 | + cb() |
| 97 | + }) |
| 98 | + ) |
| 99 | + }), |
| 100 | + cb |
| 101 | + ) |
| 102 | + } |
| 103 | + ], done) |
| 104 | + }) |
| 105 | +}) |
0 commit comments