Skip to content

Commit

Permalink
Merge pull request #10 from eugirdor/emit-open-event
Browse files Browse the repository at this point in the history
Fix #9. Emit 'open' event in createReadStream and createWriteStream.
  • Loading branch information
davidkudera committed Feb 5, 2015
2 parents 0989b26 + 52ca5d1 commit 7f9e16d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ $ npm test

## Changelog

* 1.1.3
+ Bug with createReadStream and createWriteStream not emitting 'open' event

* 1.1.2
+ Bug with createWriteStream sending improper 'finish' event

Expand Down
5 changes: 5 additions & 0 deletions src/fs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,9 @@ class fs
if options.fd == null
options.fd = @openSync(path, options.flags, options.mode)

process.nextTick ->
rs.emit('open', options.fd)

size = @fstatSync(options.fd).size

buffer = new Buffer(size)
Expand Down Expand Up @@ -1112,6 +1115,8 @@ class fs

try
fd = @openSync(path, options.flags, options.mode)
process.nextTick ->
ws.emit('open', fd)
catch err
process.nextTick ->
ws.emit('error', err)
Expand Down
29 changes: 29 additions & 0 deletions test/src/fs.posix.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,20 @@ describe 'fs.posix', ->

describe '#createReadStream()', ->

it 'should emit an open event with the file descriptor when it opens a file', (done) ->
fs.writeFileSync('/var/www/index.php', '')
rs = fs.createReadStream('/var/www/index.php')
rs.on 'open', (fd) ->
expect(fd).to.be.a('number')
done()

it 'should not emit an open event if file does not exist', (done) ->
rs = fs.createReadStream('/var/www/index.php')
rs.on 'open', (fd) ->
expect().fail()
rs.on 'error', (err) ->
done()

it 'should emit an error event if file does not exist', (done) ->
rs = fs.createReadStream('/var/www/index.php')
rs.on 'error', (err) ->
Expand Down Expand Up @@ -1117,6 +1131,21 @@ describe 'fs.posix', ->

describe '#createWriteStream()', ->

it 'should emit an open event with the file descriptor when it opens a file', (done) ->
fs.writeFileSync('/var/www/index.php', '')
ws = fs.createWriteStream('/var/www/index.php')
ws.on 'open', (fd) ->
expect(fd).to.be.a('number')
done()

it 'should not emit an open event if creating write stream fails', (done) ->
fs.writeFileSync('/var/www/index.php', '')
ws = fs.createWriteStream('/var/www/index.php', {flags: 'wx'})
ws.on 'open', (fd) ->
expect().fail()
ws.on 'error', (err) ->
done()

it 'should emit an error event if mode is wx and file already exists', (done) ->
fs.writeFileSync('/var/www/index.php', '')
ws = fs.createWriteStream('/var/www/index.php', {flags: 'wx'})
Expand Down
29 changes: 29 additions & 0 deletions test/src/fs.windows.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,20 @@ describe 'fs.windows', ->

describe '#createReadStream()', ->

it 'should emit an open event with the file descriptor when it opens a file', (done) ->
fs.writeFileSync('c:\\xampp\\htdocs\\index.php', '')
rs = fs.createReadStream('c:\\xampp\\htdocs\\index.php')
rs.on 'open', (fd) ->
expect(fd).to.be.a('number')
done()

it 'should not emit an open event if file does not exist', (done) ->
rs = fs.createReadStream('c:\\xampp\\htdocs\\index.php')
rs.on 'open', (fd) ->
expect().fail()
rs.on 'error', (err) ->
done()

it 'should emit an error event if file does not exist', (done) ->
rs = fs.createReadStream('c:\\xampp\\htdocs\\index.php')
rs.on 'error', (err) ->
Expand Down Expand Up @@ -1147,6 +1161,21 @@ describe 'fs.windows', ->

describe '#createWriteStream()', ->

it 'should emit an open event with the file descriptor when it opens a file', (done) ->
fs.writeFileSync('c:\\xampp\\htdocs\\index.php', '')
ws = fs.createWriteStream('/var/www/index.php')
ws.on 'open', (fd) ->
expect(fd).to.be.a('number')
done()

it 'should not emit an open event if creating write stream fails', (done) ->
fs.writeFileSync('c:\\xampp\\htdocs\\index.php', '')
ws = fs.createWriteStream('c:\\xampp\\htdocs\\index.php', {flags: 'wx'})
ws.on 'open', (fd) ->
expect().fail()
ws.on 'error', (err) ->
done()

it 'should emit an error event if mode is wx and file already exists', (done) ->
fs.writeFileSync('c:\\xampp\\htdocs\\index.php', '')
ws = fs.createWriteStream('c:\\xampp\\htdocs\\index.php', {flags: 'wx'})
Expand Down

0 comments on commit 7f9e16d

Please sign in to comment.