Socket
Socket
Sign inDemoInstall

file-timestamp-stream

Package Overview
Dependencies
2
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.6.0 to 1.0.0

src/file-timestamp-stream.ts

8

CHANGELOG.md
# Changelog
## v1.0.0 2018-09-07
* Rewritten in Typescript.
* Node >= 6 is required.
* New syntax of import in plain Javascript.
* `newFilename` function has `FileTimestampStream` object as its first argument.
* Removed `options` property.
## v0.6.0 2018-07-07

@@ -4,0 +12,0 @@

49

lib/file-timestamp-stream.d.ts
/// <reference types="node" />
import fs from 'fs'
import { Writable } from 'stream'
export interface FileTimestampStreamOptions {
flags?: string
fs?: typeof fs
newFilename?: (path: string) => string
path?: string
import fs from 'fs';
import { Writable, WritableOptions } from 'stream';
export interface FileTimestampStreamOptions extends WritableOptions {
flags?: string | null;
fs?: typeof fs;
newFilename?: (fileTimestampStream: FileTimestampStream) => string;
path?: string;
}
export class FileTimestampStream extends Writable {
readonly options: FileTimestampStreamOptions
readonly flags: string
readonly fs: typeof fs
readonly newFilename: () => string
readonly path: string
currentFilename?: string
stream?: Writable
constructor (options?: FileTimestampStreamOptions)
export declare class FileTimestampStream extends Writable {
readonly flags: string;
readonly fs: typeof fs;
readonly path: string;
currentFilename?: string;
stream?: Writable;
newFilename: (fileTimestampStream: FileTimestampStream) => string;
private streamErrorHandler;
constructor(options?: FileTimestampStreamOptions);
_rotate(): void;
_write(chunk: any, encoding: string, callback: (error?: Error | null) => void): void;
_writev(chunks: Array<{
chunk: any;
encoding: string;
}>, callback: (error?: Error | null) => void): void;
_final(callback: (error?: Error | null) => void): void;
_destroy(error: Error | null, callback: (error: Error | null) => void): void;
}
export default FileTimestampStream
export default FileTimestampStream;

@@ -1,127 +0,86 @@

'use strict'
const fs = require('fs')
const Writable = require('stream').Writable
const strftime = require('ultra-strftime')
function defaultNewFilename (path) {
return strftime(path, new Date())
}
/**
* @interface FileTimestampStreamOptions
* @property {string} [flags]
* @property {object} [fs]
* @property {function} [newFilename]
* @property {string} [path]
*/
/**
* @class
* @param {FileTimestampStreamOptions} options
*/
class FileTimestampStream extends Writable {
constructor (options) {
super(options)
this.options = options || {}
this.newFilename = this.options.newFilename || defaultNewFilename
this.flags = this.options.flags != null ? this.options.flags : 'a'
this.fs = this.options.fs || fs
this.path = this.options.path || 'out.log'
this._streamErrorHandler = (err) => {
this.emit('error', err)
"use strict";
/// <reference types="node" />
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs_1 = tslib_1.__importDefault(require("fs"));
const stream_1 = require("stream");
const ultra_strftime_1 = tslib_1.__importDefault(require("ultra-strftime"));
class FileTimestampStream extends stream_1.Writable {
constructor(options = {}) {
super(options);
this.newFilename = options.newFilename || defaultNewFilename;
this.flags = options.flags || 'a';
this.fs = options.fs || fs_1.default;
this.path = options.path || 'out.log';
this.streamErrorHandler = (err) => {
this.emit('error', err);
};
}
}
_rotate () {
const newFilename = this.newFilename(this.path)
if (newFilename !== this.currentFilename) {
if (this.stream) {
this.stream.end()
this.stream.removeListener('error', this._streamErrorHandler)
}
this.stream = this.fs.createWriteStream(newFilename, {
flags: this.flags
})
this.stream.on('error', this._streamErrorHandler)
this.currentFilename = newFilename
_rotate() {
const newFilename = this.newFilename(this);
if (newFilename !== this.currentFilename) {
if (this.stream) {
this.stream.end();
this.stream.removeListener('error', this.streamErrorHandler);
}
this.stream = this.fs.createWriteStream(newFilename, {
flags: this.flags
});
this.stream.on('error', this.streamErrorHandler);
this.currentFilename = newFilename;
}
}
}
/**
* @param {any} chunk
* @param {string} encoding
* @param {function} callback
*/
_write (chunk, encoding, callback) {
try {
this._rotate()
this.stream.write(chunk, encoding, callback)
} catch (e) {
callback(e)
_write(chunk, encoding, callback) {
try {
this._rotate();
this.stream.write(chunk, encoding, callback);
}
catch (e) {
callback(e);
}
}
}
/**
* @param {object[]} chunks
* @param {function} callback
*/
_writev (chunks, callback) {
let corked = false
try {
this._rotate()
corked = true
this.stream.cork()
for (const chunk of chunks) {
this.stream.write(chunk.chunk, chunk.encoding)
}
process.nextTick(() => this.stream.uncork())
callback()
} catch (e) {
if (corked) {
process.nextTick(() => this.stream.uncork())
}
callback(e)
_writev(chunks, callback) {
let corked = false;
try {
this._rotate();
corked = true;
this.stream.cork();
for (const chunk of chunks) {
this.stream.write(chunk.chunk, chunk.encoding);
}
process.nextTick(() => this.stream.uncork());
callback();
}
catch (e) {
if (corked) {
process.nextTick(() => this.stream.uncork());
}
callback(e);
}
}
}
/**
* @param {function} callback
*/
_final (callback) {
if (this.stream) {
this.stream.end(callback)
} else {
callback()
_final(callback) {
if (this.stream) {
this.stream.end(callback);
}
else {
callback();
}
}
}
/**
* @param {Error|null} err
* @param {function} callback
*/
_destroy (err, callback) {
if (this.stream) {
this.stream.destroy()
this.stream.removeListener('error', this._streamErrorHandler)
delete this._streamErrorHandler
delete this.stream
_destroy(error, callback) {
if (this.stream) {
this.stream.destroy();
this.stream.removeListener('error', this.streamErrorHandler);
delete this.streamErrorHandler;
delete this.stream;
}
this.newFilename = (_fileTimestampStream) => {
throw new Error('write after destroy');
};
callback(error);
}
this.newFilename = () => {
throw new Error('write after destroy')
}
callback(err)
}
}
FileTimestampStream.FileTimestampStream = FileTimestampStream
module.exports = FileTimestampStream
exports.FileTimestampStream = FileTimestampStream;
function defaultNewFilename(fileTimestampStream) {
return ultra_strftime_1.default(fileTimestampStream.path, new Date());
}
exports.default = FileTimestampStream;
{
"name": "file-timestamp-stream",
"version": "0.6.0",
"version": "1.0.0",
"description": "Writing stream with file rotating based on timestamp",

@@ -23,45 +23,47 @@ "main": "lib/file-timestamp-stream.js",

"engines": {
"node": ">=4.0.0"
"node": ">=6.0.0"
},
"dependencies": {
"tslib": "^1.9.3",
"ultra-strftime": "^1.0.2"
},
"devDependencies": {
"@types/node": "^10.5.2",
"@types/chai": "^4.1.4",
"@types/mocha": "^5.2.5",
"@types/node": "^10.9.4",
"@types/ultra-strftime": "^1.0.0",
"chai": "^4.1.2",
"eslint": "^5.0.1",
"eslint-config-standard": "^12.0.0-alpha.0",
"eslint-plugin-import": "^2.13.0",
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-promise": "^3.8.0",
"eslint-plugin-standard": "^3.1.0",
"markdownlint-cli": "^0.10.0",
"standard": "^11.0.1",
"tap": "^12.0.1",
"tap-given": "^0.6.0",
"tslint": "^5.10.0",
"tslint-config-standard": "^7.1.0",
"typescript": "^2.9.2"
"eslint": "^5.5.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-node": "^7.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"markdownlint-cli": "^0.13.0",
"mocha": "^5.2.0",
"nyc": "^13.0.1",
"ts-node": "^7.0.1",
"tslint": "^5.11.0",
"tslint-config-standard": "^8.0.1",
"typescript": "^3.0.3"
},
"scripts": {
"pretest": "eslint . && tsc --noEmit --pretty && tslint -t stylish -p . && echo markdownlint *.md",
"test": "npm run test:api",
"test:api": "tap test/*.js",
"test:coverage": "npm test -- --coverage"
"build": "tsc --pretty",
"clean": "rimraf lib",
"postpublish": "git tag v$npm_package_version -a -m \"Release $npm_package_version\" && git push --tags",
"prepublishOnly": "npm run build",
"pretest": "npm run build && eslint . && tslint -t stylish -p . && markdownlint \"*.md\"",
"test": "npm run test:spec",
"test:spec": "npm run ts-mocha -- \"test/*.ts\"",
"test:coverage": "nyc --reporter json npm run test:spec && nyc report",
"ts-mocha": "mocha --use_strict --throw-deprecation --require source-map-support/register --require ts-node/register --timeout 90000"
},
"standard": {
"globals": [
"After",
"And",
"Feature",
"Given",
"Scenario",
"Then",
"When"
"nyc": {
"extension": [
".ts"
],
"exclude": [
"**/*.d.ts"
]
},
"nyc": {
"exclude": []
}
}
# file-timestamp-stream
<!-- markdownlint-disable MD013 -->
[![Build Status](https://secure.travis-ci.org/dex4er/js-file-timestamp-stream.svg)](http://travis-ci.org/dex4er/js-file-timestamp-stream) [![Coverage Status](https://coveralls.io/repos/github/dex4er/js-file-timestamp-stream/badge.svg)](https://coveralls.io/github/dex4er/js-file-timestamp-stream) [![npm](https://img.shields.io/npm/v/file-timestamp-stream.svg)](https://www.npmjs.com/package/file-timestamp-stream)
<!-- markdownlint-enable MD013 -->

@@ -9,2 +11,6 @@ This module creates

## Requirements
This module requires ES6 with Node >= 6.
## Installation

@@ -27,3 +33,3 @@

```js
const FileTimestampStream = require('file-timestamp-stream')
const { FileTimestampStream } = require('file-timestamp-stream')
```

@@ -37,5 +43,17 @@

Transpiling this module with own settings in `tsconfig.json`:
```json
{
"compilerOptions": {
"paths": {
"file-timestamp-stream": ["node_modules/file-timestamp-stream/src/file-timestamp-stream"]
}
}
}
```
### Options
* `newFilename` is a custom function with path as an only argument which
* `newFilename` is a custom function with this object as an only argument which
returns new filename (default: returns new filename based on path and current

@@ -73,5 +91,5 @@ time)

function newFilename (path) {
const filename = strftime(path)
if (filename !== stream.currentFilename) counter++
function newFilename (fileTimestampStream) {
const filename = strftime(fileTimestampStream.path)
if (filename !== fileTimestampStream.currentFilename) counter++
return filename

@@ -78,0 +96,0 @@ }

{
"compilerOptions": {
"declaration": true,
"esModuleInterop": true,
"importHelpers": true,
"lib": [
"es6"
],
"module": "commonjs",
"noImplicitAny": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "./lib",
"target": "es6",
"strict": true,
"target": "ES2017"
}
"typeRoots": [
"node_modules/@types"
]
},
"exclude": [
"./examples/**/*",
"./lib/**/*",
"./test/**/*"
]
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc