diff --git a/.gitignore b/.gitignore index af20d81..a5fd5c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ test/repo-just-for-test* - +test/test-repo-for-* # Logs logs *.log @@ -35,4 +35,4 @@ node_modules .node_repl_history lib -dist \ No newline at end of file +dist diff --git a/package.json b/package.json index 507b1cf..321ba91 100644 --- a/package.json +++ b/package.json @@ -42,8 +42,8 @@ "buffer-loader": "0.0.1", "chai": "^3.5.0", "fs-blob-store": "^5.2.1", - "idb-plus-blob-store": "^1.0.0", - "ipfs-repo": "^0.6.1", + "idb-plus-blob-store": "^1.1.1", + "ipfs-repo": "^0.6.6", "lodash": "^4.8.2", "ncp": "^2.0.0", "pre-commit": "^1.1.2", @@ -60,4 +60,4 @@ "Stephen Whitmore ", "dignifiedquire " ] -} \ No newline at end of file +} diff --git a/src/block-service.js b/src/block-service.js index 64d2b0b..a9c93a6 100644 --- a/src/block-service.js +++ b/src/block-service.js @@ -14,12 +14,32 @@ function BlockService (ipfsRepo, exchange) { let done = false ws.write(block.data) + ws.once('error', (err) => { done = true callback(err) }) + ws.once('finish', () => { - if (!done) callback() + if (!done) { + // Important to note: Writing to a stream + // isn't an atomic process, because streams can be + // piped, and the finish of one only represents that + // the data was buffered to the next one. + // This is something known and 'accepted' on the + // streams API, however, since we expose a callback + // interface on BlockService and a streams one, + // the users will expect for the callback to be fired + // when the final write was concluded. We add a + // timeout to ensure that. + // TODO: Create an elegant way to understand when + // the block was actually flushed to disk. This + // means changing how the blob-stores and repo are + // implemented. + // One option, is polling till we check it + // is written. + setTimeout(callback, 150) + } }) ws.end() } diff --git a/test/block-service-test.js b/test/block-service-test.js index 880816a..23431c0 100644 --- a/test/block-service-test.js +++ b/test/block-service-test.js @@ -29,7 +29,7 @@ module.exports = (repo) => { }) it('store and get a block, with custom extension', (done) => { - const b = new Block('A random data block', 'ext') + const b = new Block('A random data block 2', 'ext') bs.addBlock(b, (err) => { expect(err).to.not.exist bs.getBlock(b.key, 'ext', (err, block) => {