Skip to content

Commit

Permalink
Some config modules produce immutable options objects
Browse files Browse the repository at this point in the history
for example: npm install config --save

Creating a naive copy of the options object as I can't guarantee no code will try to change this in future.

fixes #8
  • Loading branch information
Jim Tupper committed Aug 9, 2016
1 parent 604d6b3 commit 44fc6a7
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,54 @@ var PeriodTrigger = require('./lib/periodtrigger');
var InitialPeriodTrigger = require('./lib/initialperiodtrigger');
var ThresholdTrigger = require('./lib/thresholdtrigger');
var TriggerAdapter = require('./lib/triggeradapter');
var _ = require('lodash');

var path = require('path');

var existingFilesStreams = {};


function RotatingFileStreamFactory(options) {
if (typeof (options.path) !== 'string') {
// options_in might be readonly, copy so that we can modify as needed
var options_copy = _.extend({}, options);

if (typeof (options_copy.path) !== 'string') {
throw new Error('Must provide a string for path');
}

options.path = path.resolve(options.path);
options_copy.path = path.resolve(options_copy.path);

var rfs = existingFilesStreams[options.path];
var rfs = existingFilesStreams[options_copy.path];

if (!rfs) {
rfs = RotatingFileStream(options);
rfs = RotatingFileStream(options_copy);

existingFilesStreams[options.path] = rfs;
existingFilesStreams[options_copy.path] = rfs;

rfs.once('shutdown', function () {
existingFilesStreams[options.path] = null;
existingFilesStreams[options_copy.path] = null;
});

if (options.period) {
var periodTrigger = PeriodTrigger(options);
if (options_copy.period) {
var periodTrigger = PeriodTrigger(options_copy);
TriggerAdapter(periodTrigger, rfs);
}

if (options.period && options.rotateExisting) {
var initialPeriodTrigger = InitialPeriodTrigger(options);
if (options_copy.period && options_copy.rotateExisting) {
var initialPeriodTrigger = InitialPeriodTrigger(options_copy);
TriggerAdapter(initialPeriodTrigger, rfs);
}

if (options.threshold) {
var thresholdTrigger = ThresholdTrigger(options);
if (options_copy.threshold) {
var thresholdTrigger = ThresholdTrigger(options_copy);
TriggerAdapter(thresholdTrigger, rfs);
}

rfs.initialise();

} else if (options.shared !== true ||
existingFilesStreams[options.path].shared !== true) {
} else if (options_copy.shared !== true ||
existingFilesStreams[options_copy.path].shared !== true) {
throw new Error('You should not create multiple rotating file ' +
'streams against the same file: ' + options.path);
'streams against the same file: ' + options_copy.path);
}

return rfs;
Expand Down

0 comments on commit 44fc6a7

Please sign in to comment.