Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #9. Emit 'open' event in createReadStream and createWriteStream. #10

Merged
merged 1 commit into from
Feb 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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