Pushing changes
This commit is contained in:
118
node_modules/streamroller/lib/BaseRollingFileStream.js
generated
vendored
Normal file
118
node_modules/streamroller/lib/BaseRollingFileStream.js
generated
vendored
Normal file
@ -0,0 +1,118 @@
|
||||
"use strict";
|
||||
var fs = require('fs')
|
||||
, zlib = require('zlib')
|
||||
, debug = require('debug')('streamroller:BaseRollingFileStream')
|
||||
, mkdirp = require('mkdirp')
|
||||
, path = require('path')
|
||||
, util = require('util')
|
||||
, stream = require('readable-stream');
|
||||
|
||||
module.exports = BaseRollingFileStream;
|
||||
|
||||
function BaseRollingFileStream(filename, options) {
|
||||
debug("In BaseRollingFileStream");
|
||||
this.filename = filename;
|
||||
this.options = options || {};
|
||||
this.options.encoding = this.options.encoding || 'utf8';
|
||||
this.options.mode = this.options.mode || parseInt('0644', 8);
|
||||
this.options.flags = this.options.flags || 'a';
|
||||
|
||||
this.currentSize = 0;
|
||||
|
||||
function currentFileSize(file) {
|
||||
var fileSize = 0;
|
||||
try {
|
||||
fileSize = fs.statSync(file).size;
|
||||
} catch (e) {
|
||||
// file does not exist
|
||||
}
|
||||
return fileSize;
|
||||
}
|
||||
|
||||
function throwErrorIfArgumentsAreNotValid() {
|
||||
if (!filename) {
|
||||
throw new Error("You must specify a filename");
|
||||
}
|
||||
}
|
||||
|
||||
throwErrorIfArgumentsAreNotValid();
|
||||
debug("Calling BaseRollingFileStream.super");
|
||||
BaseRollingFileStream.super_.call(this);
|
||||
this.openTheStream();
|
||||
this.currentSize = currentFileSize(this.filename);
|
||||
}
|
||||
util.inherits(BaseRollingFileStream, stream.Writable);
|
||||
|
||||
BaseRollingFileStream.prototype._write = function(chunk, encoding, callback) {
|
||||
var that = this;
|
||||
function writeTheChunk() {
|
||||
debug("writing the chunk to the underlying stream");
|
||||
that.currentSize += chunk.length;
|
||||
try {
|
||||
that.theStream.write(chunk, encoding, callback);
|
||||
}
|
||||
catch (err){
|
||||
debug(err);
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
debug("in _write");
|
||||
|
||||
if (this.shouldRoll()) {
|
||||
this.currentSize = 0;
|
||||
this.roll(this.filename, writeTheChunk);
|
||||
} else {
|
||||
writeTheChunk();
|
||||
}
|
||||
};
|
||||
|
||||
BaseRollingFileStream.prototype.openTheStream = function(cb) {
|
||||
debug("opening the underlying stream");
|
||||
var that = this;
|
||||
mkdirp.sync(path.dirname(this.filename));
|
||||
this.theStream = fs.createWriteStream(this.filename, this.options);
|
||||
this.theStream.on('error', function(err) {
|
||||
that.emit('error', err);
|
||||
});
|
||||
if (cb) {
|
||||
this.theStream.on("open", cb);
|
||||
}
|
||||
};
|
||||
|
||||
BaseRollingFileStream.prototype.closeTheStream = function(cb) {
|
||||
debug("closing the underlying stream");
|
||||
this.theStream.end(cb);
|
||||
};
|
||||
|
||||
BaseRollingFileStream.prototype.compress = function(filename, cb) {
|
||||
debug('Compressing ', filename, ' -> ', filename, '.gz');
|
||||
var gzip = zlib.createGzip();
|
||||
var inp = fs.createReadStream(filename);
|
||||
var out = fs.createWriteStream(filename+".gz");
|
||||
inp.pipe(gzip).pipe(out);
|
||||
|
||||
out.on('finish', function(err) {
|
||||
debug('Removing original ', filename);
|
||||
fs.unlink(filename, cb);
|
||||
});
|
||||
};
|
||||
|
||||
BaseRollingFileStream.prototype.shouldRoll = function() {
|
||||
return false; // default behaviour is never to roll
|
||||
};
|
||||
|
||||
BaseRollingFileStream.prototype.roll = function(filename, callback) {
|
||||
callback(); // default behaviour is not to do anything
|
||||
};
|
||||
|
||||
BaseRollingFileStream.prototype.end = function(chunk, encoding, callback) {
|
||||
var self = this;
|
||||
this.theStream.end(chunk, encoding, function(err) {
|
||||
stream.Writable.prototype.end.call(self, function() {
|
||||
if (callback) {
|
||||
callback(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
136
node_modules/streamroller/lib/DateRollingFileStream.js
generated
vendored
Normal file
136
node_modules/streamroller/lib/DateRollingFileStream.js
generated
vendored
Normal file
@ -0,0 +1,136 @@
|
||||
"use strict";
|
||||
var BaseRollingFileStream = require('./BaseRollingFileStream')
|
||||
, debug = require('debug')('streamroller:DateRollingFileStream')
|
||||
, format = require('date-format')
|
||||
, fs = require('fs')
|
||||
, path = require('path')
|
||||
, util = require('util');
|
||||
|
||||
module.exports = DateRollingFileStream;
|
||||
|
||||
function findTimestampFromFileIfExists(filename, now) {
|
||||
return fs.existsSync(filename) ? fs.statSync(filename).mtime : new Date(now());
|
||||
}
|
||||
|
||||
function DateRollingFileStream(filename, pattern, options, now) {
|
||||
debug("Now is ", now);
|
||||
if (pattern && typeof(pattern) === 'object') {
|
||||
now = options;
|
||||
options = pattern;
|
||||
pattern = null;
|
||||
}
|
||||
this.pattern = pattern || '.yyyy-MM-dd';
|
||||
this.now = now || Date.now;
|
||||
this.lastTimeWeWroteSomething = format.asString(
|
||||
this.pattern,
|
||||
findTimestampFromFileIfExists(filename, this.now)
|
||||
);
|
||||
|
||||
this.baseFilename = filename;
|
||||
this.alwaysIncludePattern = false;
|
||||
|
||||
if (options) {
|
||||
if (options.alwaysIncludePattern) {
|
||||
this.alwaysIncludePattern = true;
|
||||
filename = this.baseFilename + this.lastTimeWeWroteSomething;
|
||||
}
|
||||
delete options.alwaysIncludePattern;
|
||||
if (Object.keys(options).length === 0) {
|
||||
options = null;
|
||||
}
|
||||
}
|
||||
debug("this.now is ", this.now, ", now is ", now);
|
||||
|
||||
DateRollingFileStream.super_.call(this, filename, options);
|
||||
}
|
||||
util.inherits(DateRollingFileStream, BaseRollingFileStream);
|
||||
|
||||
DateRollingFileStream.prototype.shouldRoll = function () {
|
||||
var lastTime = this.lastTimeWeWroteSomething,
|
||||
thisTime = format.asString(this.pattern, new Date(this.now()));
|
||||
|
||||
debug("DateRollingFileStream.shouldRoll with now = ",
|
||||
this.now(), ", thisTime = ", thisTime, ", lastTime = ", lastTime);
|
||||
|
||||
this.lastTimeWeWroteSomething = thisTime;
|
||||
this.previousTime = lastTime;
|
||||
|
||||
return thisTime !== lastTime;
|
||||
};
|
||||
|
||||
DateRollingFileStream.prototype.roll = function (filename, callback) {
|
||||
var that = this;
|
||||
|
||||
debug("Starting roll");
|
||||
|
||||
if (this.alwaysIncludePattern) {
|
||||
this.filename = this.baseFilename + this.lastTimeWeWroteSomething;
|
||||
this.closeTheStream(
|
||||
this.compressIfNeeded.bind(this, filename,
|
||||
this.removeOldFilesIfNeeded.bind(this,
|
||||
this.openTheStream.bind(this, callback))));
|
||||
} else {
|
||||
var newFilename = this.baseFilename + this.previousTime;
|
||||
this.closeTheStream(
|
||||
deleteAnyExistingFile.bind(null,
|
||||
renameTheCurrentFile.bind(null,
|
||||
this.compressIfNeeded.bind(this, newFilename,
|
||||
this.removeOldFilesIfNeeded.bind(this,
|
||||
this.openTheStream.bind(this, callback))))));
|
||||
}
|
||||
|
||||
function deleteAnyExistingFile(cb) {
|
||||
//on windows, you can get a EEXIST error if you rename a file to an existing file
|
||||
//so, we'll try to delete the file we're renaming to first
|
||||
fs.unlink(newFilename, function (err) {
|
||||
|
||||
//ignore err: if we could not delete, it's most likely that it doesn't exist
|
||||
cb();
|
||||
});
|
||||
}
|
||||
|
||||
function renameTheCurrentFile(cb) {
|
||||
debug("Renaming the ", filename, " -> ", newFilename);
|
||||
fs.rename(filename, newFilename, cb);
|
||||
}
|
||||
};
|
||||
|
||||
DateRollingFileStream.prototype.compressIfNeeded = function (filename, cb) {
|
||||
debug("Checking if we need to compress the old file");
|
||||
if (this.options.compress) {
|
||||
this.compress(filename, cb);
|
||||
} else {
|
||||
cb();
|
||||
}
|
||||
};
|
||||
|
||||
DateRollingFileStream.prototype.removeOldFilesIfNeeded = function (cb) {
|
||||
debug("Checking if we need to delete old files");
|
||||
if (this.options.daysToKeep && this.options.daysToKeep > 0) {
|
||||
var oldestDate = new Date(this.now() - (this.options.daysToKeep * (24 * 60 * 60 * 1000)));
|
||||
debug("Will delete any log files modified before ", oldestDate.toString());
|
||||
|
||||
this.removeFilesOlderThan(oldestDate);
|
||||
}
|
||||
cb();
|
||||
};
|
||||
|
||||
DateRollingFileStream.prototype.removeFilesOlderThan = function (oldestDate) {
|
||||
|
||||
// Loop through any log files and delete any whose mtime is earlier than oldestDate
|
||||
var dirToScan = path.dirname(this.baseFilename);
|
||||
var fileToMatch = path.basename(this.baseFilename);
|
||||
var filesToCheck = fs.readdirSync(dirToScan).filter(function (file) {
|
||||
return file.indexOf(fileToMatch) > -1;
|
||||
});
|
||||
for (var i = 0; i < filesToCheck.length; i++) {
|
||||
var fileToCheck = path.join(dirToScan, filesToCheck[i]);
|
||||
var fileStats = fs.statSync(fileToCheck);
|
||||
if (fileStats.mtime < oldestDate) {
|
||||
debug("Deleting old log ", filesToCheck);
|
||||
fs.unlinkSync(fileToCheck);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
110
node_modules/streamroller/lib/RollingFileStream.js
generated
vendored
Normal file
110
node_modules/streamroller/lib/RollingFileStream.js
generated
vendored
Normal file
@ -0,0 +1,110 @@
|
||||
"use strict";
|
||||
var BaseRollingFileStream = require('./BaseRollingFileStream')
|
||||
, debug = require('debug')('streamroller:RollingFileStream')
|
||||
, util = require('util')
|
||||
, path = require('path')
|
||||
, child_process = require('child_process')
|
||||
, fs = require('fs');
|
||||
|
||||
module.exports = RollingFileStream;
|
||||
|
||||
function RollingFileStream (filename, size, backups, options) {
|
||||
//if you don't specify a size, this will behave like a normal file stream
|
||||
this.size = size || Number.MAX_SAFE_INTEGER;
|
||||
this.backups = backups || 1;
|
||||
|
||||
function throwErrorIfArgumentsAreNotValid() {
|
||||
if (!filename || size <= 0) {
|
||||
throw new Error("You must specify a filename and file size");
|
||||
}
|
||||
}
|
||||
|
||||
throwErrorIfArgumentsAreNotValid();
|
||||
|
||||
RollingFileStream.super_.call(this, filename, options);
|
||||
}
|
||||
util.inherits(RollingFileStream, BaseRollingFileStream);
|
||||
|
||||
RollingFileStream.prototype.shouldRoll = function() {
|
||||
debug("should roll with current size " + this.currentSize + " and max size " + this.size);
|
||||
return this.currentSize >= this.size;
|
||||
};
|
||||
|
||||
RollingFileStream.prototype.roll = function(filename, callback) {
|
||||
var that = this,
|
||||
nameMatcher = new RegExp('^' + path.basename(filename));
|
||||
|
||||
function justTheseFiles (item) {
|
||||
return nameMatcher.test(item);
|
||||
}
|
||||
|
||||
function index(filename_) {
|
||||
debug('Calculating index of '+filename_);
|
||||
return parseInt(filename_.substring((path.basename(filename) + '.').length), 10) || 0;
|
||||
}
|
||||
|
||||
function byIndex(a, b) {
|
||||
if (index(a) > index(b)) {
|
||||
return 1;
|
||||
} else if (index(a) < index(b) ) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
function increaseFileIndex (fileToRename, cb) {
|
||||
var idx = index(fileToRename);
|
||||
debug('Index of ' + fileToRename + ' is ' + idx);
|
||||
if (idx < that.backups) {
|
||||
|
||||
var ext = path.extname(fileToRename);
|
||||
var destination = filename + '.' + (idx+1);
|
||||
if (that.options.compress && /^gz$/.test(ext.substring(1))) {
|
||||
destination+=ext;
|
||||
}
|
||||
//on windows, you can get a EEXIST error if you rename a file to an existing file
|
||||
//so, we'll try to delete the file we're renaming to first
|
||||
fs.unlink(destination, function (err) {
|
||||
//ignore err: if we could not delete, it's most likely that it doesn't exist
|
||||
debug('Renaming ' + fileToRename + ' -> ' + destination);
|
||||
fs.rename(path.join(path.dirname(filename), fileToRename), destination, function(err) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
if (that.options.compress && ext!=".gz") {
|
||||
that.compress(destination, cb);
|
||||
} else {
|
||||
cb();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
cb();
|
||||
}
|
||||
}
|
||||
|
||||
function renameTheFiles(cb) {
|
||||
//roll the backups (rename file.n to file.n+1, where n <= numBackups)
|
||||
debug("Renaming the old files");
|
||||
fs.readdir(path.dirname(filename), function (err, files) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
var filesToProcess = files.filter(justTheseFiles).sort(byIndex);
|
||||
(function processOne(err) {
|
||||
var file = filesToProcess.pop();
|
||||
if (!file || err) { return cb(err); }
|
||||
increaseFileIndex(file, processOne);
|
||||
})();
|
||||
});
|
||||
}
|
||||
|
||||
debug("Rolling, rolling, rolling");
|
||||
this.closeTheStream(
|
||||
renameTheFiles.bind(null,
|
||||
this.openTheStream.bind(this,
|
||||
callback)));
|
||||
|
||||
};
|
3
node_modules/streamroller/lib/index.js
generated
vendored
Normal file
3
node_modules/streamroller/lib/index.js
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
exports.RollingFileStream = require('./RollingFileStream');
|
||||
exports.DateRollingFileStream = require('./DateRollingFileStream');
|
Reference in New Issue
Block a user