diff --git a/CHANGELOG.md b/CHANGELOG.md index a8f33d2..bd41bb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file automatically by Versionist. DO NOT EDIT THIS FILE MANUALLY! This project adheres to [Semantic Versioning](http://semver.org/). +## v4.1.1 - 2017-12-11 + +* Refactor read stream logic to improve clarity & error handling #32 [Tim Perry] +* Update Bluebird to v3 #32 [Tim Perry] + ## v4.1.0 - 2017-11-21 * Upgrade partitioninfo so we can read GPT partition tables too #31 [Tim Perry] diff --git a/build/imagefs.js b/build/imagefs.js index 3e6c641..7c350fd 100644 --- a/build/imagefs.js +++ b/build/imagefs.js @@ -104,18 +104,24 @@ read = function(disk, partition, path) { readStream = null; outputStream = new stream.PassThrough(); startReadStream = function() { - outputStream.removeListener('newListener', startReadStream); - return process.nextTick(function() { + var e; + try { readStream = fs_.createReadStream(path, { autoClose: false }); - readStream.on('error', function(err) { - return outputStream.emit('error', err); - }); - return readStream.pipe(outputStream); + } catch (_error) { + e = _error; + outputStream.emit('error', e); + return; + } + readStream.on('error', function(err) { + return outputStream.emit('error', err); }); + return readStream.pipe(outputStream); }; - outputStream.on('newListener', startReadStream); + outputStream.once('newListener', function() { + return process.nextTick(startReadStream); + }); return Promise.resolve(outputStream).disposer(function(stream) { outputStream.end(); if ((readStream != null) && readStream.closeAsync) { diff --git a/lib/imagefs.coffee b/lib/imagefs.coffee index ee97d73..eef65e3 100644 --- a/lib/imagefs.coffee +++ b/lib/imagefs.coffee @@ -90,18 +90,21 @@ read = (disk, partition, path) -> readStream = null outputStream = new stream.PassThrough() - # We don't start the stream until somebody else starts listening startReadStream = -> - outputStream.removeListener('newListener', startReadStream) - - # This is triggered _before_ the listener is added, so wait briefly - process.nextTick -> + try readStream = fs_.createReadStream(path, autoClose: false) - readStream.on 'error', (err) -> - outputStream.emit('error', err) - readStream.pipe(outputStream) + catch e + outputStream.emit('error', e) + return + + readStream.on 'error', (err) -> + outputStream.emit('error', err) - outputStream.on('newListener', startReadStream) + readStream.pipe(outputStream) + + # We don't start the stream until somebody else starts listening + # Delayed slightly, as this event fires _before_ the listener is added + outputStream.once('newListener', -> process.nextTick(startReadStream)) Promise.resolve(outputStream) .disposer (stream) -> diff --git a/package.json b/package.json index afcd4a8..a964b60 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "resin-image-fs", - "version": "4.1.0", + "version": "4.1.1", "description": "Resin.io image filesystem manipulation utilities", "main": "build/imagefs.js", "homepage": "https://github.com/resin-io/resin-image-fs", @@ -44,7 +44,7 @@ "wary": "^1.0.0" }, "dependencies": { - "bluebird": "^2.9.32", + "bluebird": "^3.5.1", "ext2fs": "^0.1.0", "fatfs": "^0.10.6", "file-disk": "^1.0.1",