RollingFileWriteStream.js 13.8 KB
Newer Older
Kriengkrai Yothee's avatar
Kriengkrai Yothee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
const debug = require("debug")("streamroller:RollingFileWriteStream");
const _ = require("lodash");
const async = require("async");
const fs = require("fs-extra");
const zlib = require("zlib");
const path = require("path");
const newNow = require("./now");
const format = require("date-format");
const { Writable } = require("stream");

const FILENAME_SEP = ".";
const ZIP_EXT = ".gz";

const moveAndMaybeCompressFile = (
  sourceFilePath,
  targetFilePath,
  needCompress,
  done
) => {
  if (sourceFilePath === targetFilePath) {
    debug(
      `moveAndMaybeCompressFile: source and target are the same, not doing anything`
    );
    return done();
  }
  fs.access(sourceFilePath, fs.constants.W_OK | fs.constants.R_OK, e => {
    if (e) {
      debug(
        `moveAndMaybeCompressFile: source file path does not exist. not moving. sourceFilePath=${sourceFilePath}`
      );
      return done();
    }

    debug(
      `moveAndMaybeCompressFile: moving file from ${sourceFilePath} to ${targetFilePath} ${
        needCompress ? "with" : "without"
      } compress`
    );
    if (needCompress) {
      fs.createReadStream(sourceFilePath)
        .pipe(zlib.createGzip())
        .pipe(fs.createWriteStream(targetFilePath))
        .on("finish", () => {
          debug(
            `moveAndMaybeCompressFile: finished compressing ${targetFilePath}, deleting ${sourceFilePath}`
          );
          fs.unlink(sourceFilePath, done);
        });
    } else {
      debug(
        `moveAndMaybeCompressFile: deleting file=${targetFilePath}, renaming ${sourceFilePath} to ${targetFilePath}`
      );
      fs.unlink(targetFilePath, () => {
        fs.rename(sourceFilePath, targetFilePath, done);
      });
    }
  });
};

/**
 * RollingFileWriteStream is mainly used when writing to a file rolling by date or size.
 * RollingFileWriteStream inhebites from stream.Writable
 */
class RollingFileWriteStream extends Writable {
  /**
   * Create a RollingFileWriteStream
   * @constructor
   * @param {string} filePath - The file path to write.
   * @param {object} options - The extra options
   * @param {number} options.numToKeep - The max numbers of files to keep.
   * @param {number} options.maxSize - The maxSize one file can reach. Unit is Byte.
   *                                   This should be more than 1024. The default is Number.MAX_SAFE_INTEGER.
   * @param {string} options.mode - The mode of the files. The default is '0644'. Refer to stream.writable for more.
   * @param {string} options.flags - The default is 'a'. Refer to stream.flags for more.
   * @param {boolean} options.compress - Whether to compress backup files.
   * @param {boolean} options.keepFileExt - Whether to keep the file extension.
   * @param {string} options.pattern - The date string pattern in the file name.
   * @param {boolean} options.alwaysIncludePattern - Whether to add date to the name of the first file.
   */
  constructor(filePath, options) {
    debug(`creating RollingFileWriteStream. path=${filePath}`);
    super(options);
    this.options = this._parseOption(options);
    this.fileObject = path.parse(filePath);
    if (this.fileObject.dir === "") {
      this.fileObject = path.parse(path.join(process.cwd(), filePath));
    }
    this.justTheFile = this._formatFileName({ isHotFile: true });
    this.filename = path.join(this.fileObject.dir, this.justTheFile);
    this.state = {
      currentSize: 0
    };

    if (this.options.pattern) {
      this.state.currentDate = format(this.options.pattern, newNow());
    }

    if (this.options.flags === "a") {
      this._setExistingSizeAndDate();
    }

    debug(
      `create new file with no hot file. name=${
        this.justTheFile
      }, state=${JSON.stringify(this.state)}`
    );
    this._renewWriteStream();
  }

  _setExistingSizeAndDate() {
    try {
      const stats = fs.statSync(this.filename);
      this.state.currentSize = stats.size;
      if (this.options.pattern) {
        this.state.currentDate = format(this.options.pattern, stats.birthtime);
      }
    } catch (e) {
      //file does not exist, that's fine - move along
      return;
    }
  }

  _parseOption(rawOptions) {
    const defaultOptions = {
      maxSize: Number.MAX_SAFE_INTEGER,
      numToKeep: Number.MAX_SAFE_INTEGER,
      encoding: "utf8",
      mode: parseInt("0644", 8),
      flags: "a",
      compress: false,
      keepFileExt: false,
      alwaysIncludePattern: false
    };
    const options = _.defaults({}, rawOptions, defaultOptions);
    if (options.maxSize <= 0) {
      throw new Error(`options.maxSize (${options.maxSize}) should be > 0`);
    }
    if (options.numToKeep <= 0) {
      throw new Error(`options.numToKeep (${options.numToKeep}) should be > 0`);
    }
    debug(`creating stream with option=${JSON.stringify(options)}`);
    return options;
  }

  _shouldRoll(callback) {
    if (
      this.state.currentDate &&
      this.state.currentDate !== format(this.options.pattern, newNow())
    ) {
      debug(
        `_shouldRoll: rolling by date because ${
          this.state.currentDate
        } !== ${format(this.options.pattern, newNow())}`
      );
      this._roll({ isNextPeriod: true }, callback);
      return;
    }
    if (this.state.currentSize >= this.options.maxSize) {
      debug(
        `_shouldRoll: rolling by size because ${this.state.currentSize} >= ${this.options.maxSize}`
      );
      this._roll({ isNextPeriod: false }, callback);
      return;
    }
    callback();
  }

  _write(chunk, encoding, callback) {
    this._shouldRoll(() => {
      debug(
        `writing chunk. ` +
          `file=${this.currentFileStream.path} ` +
          `state=${JSON.stringify(this.state)} ` +
          `chunk=${chunk}`
      );
      this.currentFileStream.write(chunk, encoding, e => {
        this.state.currentSize += chunk.length;
        callback(e);
      });
    });
  }

  // Sorted from the oldest to the latest
  _getExistingFiles(cb) {
    fs.readdir(this.fileObject.dir, (e, files) => {
      debug(`_getExistingFiles: files=${files}`);
      const existingFileDetails = _.compact(
        _.map(files, n => {
          const parseResult = this._parseFileName(n);
          debug(`_getExistingFiles: parsed ${n} as `, parseResult);
          if (!parseResult) {
            return;
          }
          return _.assign({ fileName: n }, parseResult);
        })
      );
      cb(
        null,
        _.sortBy(
          existingFileDetails,
          n =>
            (n.date
              ? format.parse(this.options.pattern, n.date).valueOf()
              : newNow().valueOf()) - n.index
        )
      );
    });
  }

  // need file name instead of file abs path.
  _parseFileName(fileName) {
    let isCompressed = false;
    if (fileName.endsWith(ZIP_EXT)) {
      fileName = fileName.slice(0, -1 * ZIP_EXT.length);
      isCompressed = true;
    }
    let metaStr;
    if (this.options.keepFileExt) {
      const prefix = this.fileObject.name + FILENAME_SEP;
      const suffix = this.fileObject.ext;
      if (!fileName.startsWith(prefix) || !fileName.endsWith(suffix)) {
        return;
      }
      metaStr = fileName.slice(prefix.length, -1 * suffix.length);
      debug(
        `metaStr=${metaStr}, fileName=${fileName}, prefix=${prefix}, suffix=${suffix}`
      );
    } else {
      const prefix = this.fileObject.base;
      if (!fileName.startsWith(prefix)) {
        return;
      }
      metaStr = fileName.slice(prefix.length + 1);
      debug(`metaStr=${metaStr}, fileName=${fileName}, prefix=${prefix}`);
    }
    if (!metaStr) {
      return {
        index: 0,
        isCompressed
      };
    }
    if (this.options.pattern) {
      const items = _.split(metaStr, FILENAME_SEP);
      const indexStr = items[items.length - 1];
      debug("items: ", items, ", indexStr: ", indexStr);
      if (indexStr !== undefined && indexStr.match(/^\d+$/)) {
        const dateStr = metaStr.slice(0, -1 * (indexStr.length + 1));
        debug(`dateStr is ${dateStr}`);
        if (dateStr) {
          return {
            index: parseInt(indexStr, 10),
            date: dateStr,
            isCompressed
          };
        }
      }
      debug(`metaStr is ${metaStr}`);
      return {
        index: 0,
        date: metaStr,
        isCompressed
      };
    } else {
      if (metaStr.match(/^\d+$/)) {
        return {
          index: parseInt(metaStr, 10),
          isCompressed
        };
      }
    }
    return;
  }

  _formatFileName({ date, index, isHotFile }) {
    debug(
      `_formatFileName: date=${date}, index=${index}, isHotFile=${isHotFile}`
    );
    const dateStr =
      date ||
      _.get(this, "state.currentDate") ||
      format(this.options.pattern, newNow());
    const indexOpt = index || _.get(this, "state.currentIndex");
    const oriFileName = this.fileObject.base;
    if (isHotFile) {
      debug(
        `_formatFileName: includePattern? ${this.options.alwaysIncludePattern}, pattern: ${this.options.pattern}`
      );
      if (this.options.alwaysIncludePattern && this.options.pattern) {
        debug(
          `_formatFileName: is hot file, and include pattern, so: ${oriFileName +
            FILENAME_SEP +
            dateStr}`
        );
        return this.options.keepFileExt
          ? this.fileObject.name + FILENAME_SEP + dateStr + this.fileObject.ext
          : oriFileName + FILENAME_SEP + dateStr;
      }
      debug(`_formatFileName: is hot file so, filename: ${oriFileName}`);
      return oriFileName;
    }
    let fileNameExtraItems = [];
    if (this.options.pattern) {
      fileNameExtraItems.push(dateStr);
    }
    if (indexOpt && this.options.maxSize < Number.MAX_SAFE_INTEGER) {
      fileNameExtraItems.push(indexOpt);
    }
    let fileName;
    if (this.options.keepFileExt) {
      const baseFileName =
        this.fileObject.name +
        FILENAME_SEP +
        fileNameExtraItems.join(FILENAME_SEP);
      fileName = baseFileName + this.fileObject.ext;
    } else {
      fileName =
        oriFileName + FILENAME_SEP + fileNameExtraItems.join(FILENAME_SEP);
    }
    if (this.options.compress) {
      fileName += ZIP_EXT;
    }
    debug(`_formatFileName: ${fileName}`);
    return fileName;
  }

  _moveOldFiles(isNextPeriod, cb) {
    const currentFilePath = this.currentFileStream.path;
    debug(`numToKeep = ${this.options.numToKeep}`);
    const finishedRolling = () => {
      if (isNextPeriod) {
        this.state.currentSize = 0;
        this.state.currentDate = format(this.options.pattern, newNow());
        debug(`rolling for next period. state=${JSON.stringify(this.state)}`);
      } else {
        this.state.currentSize = 0;
        debug(
          `rolling during the same period. state=${JSON.stringify(this.state)}`
        );
      }
      this._renewWriteStream();
      // wait for the file to be open before cleaning up old ones,
      // otherwise the daysToKeep calculations can be off
      this.currentFileStream.write("", "utf8", () => this._clean(cb));
    };

    this._getExistingFiles((e, files) => {
      const filesToMove = [];
      const todaysFiles = this.state.currentDate
        ? files.filter(f => f.date === this.state.currentDate)
        : files;
      for (let i = todaysFiles.length; i >= 0; i--) {
        debug(`i = ${i}`);
        const sourceFilePath =
          i === 0
            ? currentFilePath
            : path.format({
                dir: this.fileObject.dir,
                base: this._formatFileName({
                  date: this.state.currentDate,
                  index: i
                })
              });
        const targetFilePath = path.format({
          dir: this.fileObject.dir,
          base: this._formatFileName({
            date: this.state.currentDate,
            index: i + 1
          })
        });
        filesToMove.push({ sourceFilePath, targetFilePath });
      }
      debug(`filesToMove = `, filesToMove);
      async.eachOfSeries(
        filesToMove,
        (files, idx, cb1) => {
          debug(
            `src=${files.sourceFilePath}, tgt=${
              files.sourceFilePath
            }, idx=${idx}, pos=${filesToMove.length - 1 - idx}`
          );
          moveAndMaybeCompressFile(
            files.sourceFilePath,
            files.targetFilePath,
            this.options.compress && filesToMove.length - 1 - idx === 0,
            cb1
          );
        },
        finishedRolling
      );
    });
  }

  _roll({ isNextPeriod }, cb) {
    debug(`rolling, isNextPeriod ? ${isNextPeriod}`);
    debug(`_roll: closing the current stream`);
    this.currentFileStream.end("", this.options.encoding, () => {
      this._moveOldFiles(isNextPeriod, cb);
    });
  }

  _renewWriteStream() {
    fs.ensureDirSync(this.fileObject.dir);
    this.justTheFile = this._formatFileName({
      date: this.state.currentDate,
      index: 0,
      isHotFile: true
    });
    const filePath = path.format({
      dir: this.fileObject.dir,
      base: this.justTheFile
    });
    const ops = _.pick(this.options, ["flags", "encoding", "mode"]);
    this.currentFileStream = fs.createWriteStream(filePath, ops);
    this.currentFileStream.on("error", e => {
      this.emit("error", e);
    });
  }

  _clean(cb) {
    this._getExistingFiles((e, existingFileDetails) => {
      debug(
        `numToKeep = ${this.options.numToKeep}, existingFiles = ${existingFileDetails.length}`
      );
      debug("existing files are: ", existingFileDetails);
      if (
        this.options.numToKeep > 0 &&
        existingFileDetails.length > this.options.numToKeep
      ) {
        const fileNamesToRemove = _.slice(
          existingFileDetails.map(f => f.fileName),
          0,
          existingFileDetails.length - this.options.numToKeep - 1
        );
        this._deleteFiles(fileNamesToRemove, cb);
        return;
      }
      cb();
    });
  }

  _deleteFiles(fileNames, done) {
    debug(`files to delete: ${fileNames}`);
    async.each(
      _.map(fileNames, f => path.format({ dir: this.fileObject.dir, base: f })),
      fs.unlink,
      done
    );
    return;
  }
}

module.exports = RollingFileWriteStream;