Skip to content
This repository was archived by the owner on Feb 12, 2019. It is now read-only.
Open
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
24 changes: 16 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ module.exports = function (file, opts) {
var bufferSize = opts && opts.bufferSize || 1024 * 64
var mode = opts && opts.mode || 438 // 0666
var flags = opts && opts.flags || 'r'
var fd = opts && opts.fd

function onError (err) {
stream.emit('error', err)
stream.destroy()
}

var stat, fd, position
var stat, position

if(!/rx?/.test(flags)) throw new Error("only flags 'r' and 'rx' are allowed")

Expand All @@ -27,11 +28,16 @@ module.exports = function (file, opts) {
position = stat.size
if(!--c) read()
})
fs.open(file, flags, mode, function (err, _fd) {
fd = _fd
if (fd) {
stream.emit('open')
if(!--c) read()
})
} else {
fs.open(file, flags, mode, function (err, _fd) {
fd = _fd
stream.emit('open')
if(!--c) read()
})
}

} else read()

Expand Down Expand Up @@ -65,10 +71,12 @@ module.exports = function (file, opts) {
stream.destroyed = true
stream.ended = true
function close () {
fs.close(fd, function (err) {
if(err) onError(err)
stream.emit('close')
})
if (typeof (opts && opts.fd) !== 'number') {
fs.close(fd, function (err) {
if(err) onError(err)
stream.emit('close')
})
}
}
if(!stream.started) return stream.emit('close')//allow for destroy on first tick.
if(!fd) stream.once('open', close) //destroy before file opens.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fs-reverse",
"version": "0.0.2",
"version": "0.0.3",
"description": "stream the lines of a file in reverse order!",
"main": "index.js",
"directories": {
Expand Down