slice-file
Advanced tools
Comparing version 0.0.0 to 0.1.0
99
index.js
@@ -5,2 +5,3 @@ var fs = require('fs'); | ||
var inherits = require('inherits'); | ||
var split = require('split'); | ||
@@ -33,2 +34,4 @@ var nextTick = typeof setImmediate === 'function' | ||
this.bufsize = opts.bufsize || 4 * 1024; | ||
this.flags = opts.flags; | ||
this.mode = opts.mode; | ||
} | ||
@@ -41,3 +44,3 @@ | ||
if (self.fd === undefined) { | ||
return self.on('open', self._read.bind(self, start, end, cb)); | ||
return self.once('open', self._read.bind(self, start, end, cb)); | ||
} | ||
@@ -103,2 +106,13 @@ if (start === undefined) start = 0; | ||
FA.prototype._stat = function (cb) { | ||
var self = this; | ||
fs.stat(self.file, function (err, stat) { | ||
if (err) return cb && cb(err); | ||
self.stat = stat; | ||
self.emit('stat', stat); | ||
if (cb) cb(null, stat); | ||
}); | ||
}; | ||
FA.prototype._readReverse = function (start, end, cb) { | ||
@@ -112,8 +126,4 @@ var self = this; | ||
if (self.stat === undefined) { | ||
fs.stat(self.file, function (err, stat) { | ||
if (err) return cb(err); | ||
self.stat = stat; | ||
self.emit('stat', stat); | ||
}); | ||
return self.once('stat', function () { | ||
return self._stat(function (err) { | ||
if (err) cb(err); | ||
self._readReverse(start, end, cb) | ||
@@ -220,1 +230,76 @@ }); | ||
}; | ||
FA.prototype.follow = function (start, end) { | ||
var self = this; | ||
var tr = through(); | ||
tr.close = function () { | ||
this.closed = true; | ||
this.emit('close'); | ||
}; | ||
var slice = this.slice(start, end); | ||
var writing = false; | ||
var changed = false; | ||
slice.once('end', function () { | ||
if (tr.closed) return; | ||
var w = fs.watch(self.file, { fd: self.fd }); | ||
tr.once('close', function () { w.close() }); | ||
self.once('close', function () { w.close() }); | ||
if (!self.stat) self._stat(onstat); | ||
else onstat(null, self.stat); | ||
w.on('change', function (type) { | ||
if (type !== 'change') return; | ||
if (!writing) self._stat(onstat); | ||
changed = true; | ||
}); | ||
}); | ||
var lastStat = null; | ||
slice.pipe(tr, { end: false }); | ||
self.once('close', function () { tr.queue(null) }); | ||
tr.once('close', function () { tr.queue(null) }); | ||
return tr.pipe(split()) | ||
.pipe(through(function (line) { this.queue(line + '\n') })) | ||
; | ||
function onstat (err, stat) { | ||
if (err) return tr.emit('error', err); | ||
if (!lastStat) return lastStat = stat; | ||
if (stat.size < lastStat.size) { | ||
tr.emit('truncate', lastStat.size - stat.size); | ||
} | ||
else if (stat.size > lastStat.size) { | ||
writing = true; | ||
var stream = fs.createReadStream(self.file, { | ||
//fd: self.fd, | ||
start: lastStat.size, | ||
flags: self.flags, | ||
mode: self.mode, | ||
//autoClose: false, | ||
bufferSize: self.bufsize | ||
}); | ||
stream.on('error', function (err) { tr.emit('error', err) }); | ||
stream.on('end', function () { | ||
if (changed) self._stat(onstat); | ||
writing = false; | ||
changed = false; | ||
}); | ||
stream.pipe(tr, { end: false }); | ||
} | ||
lastStat = stat; | ||
} | ||
}; | ||
FA.prototype.close = function () { | ||
var self = this; | ||
if (self.fd === undefined) return self.once('open', self.close); | ||
fs.close(self.fd, function () { | ||
self.emit('close'); | ||
}); | ||
}; |
{ | ||
"name": "slice-file", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "stream file slices by line number indexes", | ||
@@ -8,3 +8,4 @@ "main": "index.js", | ||
"inherits": "~1.0.0", | ||
"through": "~2.2.7" | ||
"through": "~2.2.7", | ||
"split": "~0.2.1" | ||
}, | ||
@@ -11,0 +12,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20992
18
504
160
3
6
+ Addedsplit@~0.2.1
+ Addedsplit@0.2.10(transitive)