From 5e5034b7afa743077043aa3a029094418e0a2c75 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Mon, 20 Apr 2020 08:22:27 +1000 Subject: [PATCH 1/3] feat: enable to set mode of the compressed archive --- lib/RollingFileWriteStream.js | 10 ++++-- lib/moveAndMaybeCompressFile.js | 21 ++++++++--- package.json | 2 +- test/moveAndMaybeCompressFile-test.js | 52 +++++++++++++++++++++++++-- 4 files changed, 75 insertions(+), 10 deletions(-) diff --git a/lib/RollingFileWriteStream.js b/lib/RollingFileWriteStream.js index 34e60e4..88c3a10 100644 --- a/lib/RollingFileWriteStream.js +++ b/lib/RollingFileWriteStream.js @@ -177,10 +177,14 @@ class RollingFileWriteStream extends Writable { index: i + 1 }); + const moveAndCompressOptions = { + compress: this.options.compress && i === 0, + mode: this.options.mode + } await moveAndMaybeCompressFile( - sourceFilePath, - targetFilePath, - this.options.compress && i === 0 + sourceFilePath, + targetFilePath, + moveAndCompressOptions ); } diff --git a/lib/moveAndMaybeCompressFile.js b/lib/moveAndMaybeCompressFile.js index b9b6068..3da4ca3 100644 --- a/lib/moveAndMaybeCompressFile.js +++ b/lib/moveAndMaybeCompressFile.js @@ -2,11 +2,24 @@ const debug = require('debug')('streamroller:moveAndMaybeCompressFile'); const fs = require('fs-extra'); const zlib = require('zlib'); +const _parseOption = function(rawOptions){ + const defaultOptions = { + mode: parseInt("0644", 8), + compress: false, + }; + const options = Object.assign({}, defaultOptions, rawOptions); + debug( + `_parseOption: moveAndMaybeCompressFile called with option=${JSON.stringify(options)}` + ); + return options; +} + const moveAndMaybeCompressFile = async ( sourceFilePath, targetFilePath, - needCompress + options ) => { + options = _parseOption(options); if (sourceFilePath === targetFilePath) { debug( `moveAndMaybeCompressFile: source and target are the same, not doing anything` @@ -17,14 +30,14 @@ const moveAndMaybeCompressFile = async ( debug( `moveAndMaybeCompressFile: moving file from ${sourceFilePath} to ${targetFilePath} ${ - needCompress ? "with" : "without" + options.compress ? "with" : "without" } compress` ); - if (needCompress) { + if (options.compress) { await new Promise((resolve, reject) => { fs.createReadStream(sourceFilePath) .pipe(zlib.createGzip()) - .pipe(fs.createWriteStream(targetFilePath)) + .pipe(fs.createWriteStream(targetFilePath, {mode: options.mode})) .on("finish", () => { debug( `moveAndMaybeCompressFile: finished compressing ${targetFilePath}, deleting ${sourceFilePath}` diff --git a/package.json b/package.json index a0f6420..38345f4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "streamroller", - "version": "2.2.3", + "version": "2.2.4", "description": "file streams that roll over when size limits, or dates are reached", "main": "lib/index.js", "directories": { diff --git a/test/moveAndMaybeCompressFile-test.js b/test/moveAndMaybeCompressFile-test.js index acbe84b..775dc17 100644 --- a/test/moveAndMaybeCompressFile-test.js +++ b/test/moveAndMaybeCompressFile-test.js @@ -34,7 +34,8 @@ describe('moveAndMaybeCompressFile', () => { const source = path.join(TEST_DIR, 'test.log'); const destination = path.join(TEST_DIR, 'moved-test.log.gz'); await fs.outputFile(source, 'This is the test file.'); - await moveAndMaybeCompressFile(source, destination, true); + const moveAndCompressOptions = {compress: true} + await moveAndMaybeCompressFile(source, destination, moveAndCompressOptions); const zippedContents = await fs.readFile(destination); const contents = await new Promise(resolve => { @@ -101,7 +102,8 @@ describe('moveAndMaybeCompressFile', () => { const source = path.join(TEST_DIR, 'test.log'); const destination = path.join(TEST_DIR, 'moved-test.log.gz'); await fs.outputFile(source, 'This is the test file.'); - await moveWithMock(source, destination, true); + const options = {compress: true}; + await moveWithMock(source, destination, options); const zippedContents = await fs.readFile(destination); const contents = await new Promise(resolve => { @@ -115,4 +117,50 @@ describe('moveAndMaybeCompressFile', () => { (await fs.readFile(source, 'utf8')).should.be.empty() }); + + it('should compress the source file at the new destination with 0o775 rights', async () => { + const source = path.join(TEST_DIR, 'test.log'); + const destination = path.join(TEST_DIR, 'moved-test.log.gz'); + await fs.outputFile(source, 'This is the test file.'); + const moveAndCompressOptions = {compress: true, mode:0o775} + await moveAndMaybeCompressFile(source, destination, moveAndCompressOptions); + + const destinationStats = await fs.stat(destination); + const destMode = (destinationStats.mode & 0o775).toString(8); + destMode.should.equal('775'); + + const zippedContents = await fs.readFile(destination); + const contents = await new Promise(resolve => { + zlib.gunzip(zippedContents, (e, data) => { + resolve(data.toString()); + }); + }); + contents.should.equal('This is the test file.'); + + const exists = await fs.pathExists(source); + exists.should.be.false(); + }); + + it('should compress the source file at the new destination with 0o400 rights', async () => { + const source = path.join(TEST_DIR, 'test.log'); + const destination = path.join(TEST_DIR, 'moved-test.log.gz'); + await fs.outputFile(source, 'This is the test file.'); + const moveAndCompressOptions = {compress: true, mode:0o400} + await moveAndMaybeCompressFile(source, destination, moveAndCompressOptions); + + const destinationStats = await fs.stat(destination); + const destMode = (destinationStats.mode & 0o400).toString(8); + destMode.should.equal('400'); + + const zippedContents = await fs.readFile(destination); + const contents = await new Promise(resolve => { + zlib.gunzip(zippedContents, (e, data) => { + resolve(data.toString()); + }); + }); + contents.should.equal('This is the test file.'); + + const exists = await fs.pathExists(source); + exists.should.be.false(); + }); }); From 5e66d2382bf70674384fb11aa0e72831597e97b7 Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Thu, 6 Jan 2022 14:59:06 +0800 Subject: [PATCH 2/3] Fixed lint indentation --- lib/RollingFileWriteStream.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/RollingFileWriteStream.js b/lib/RollingFileWriteStream.js index 88c3a10..c672464 100644 --- a/lib/RollingFileWriteStream.js +++ b/lib/RollingFileWriteStream.js @@ -182,9 +182,9 @@ class RollingFileWriteStream extends Writable { mode: this.options.mode } await moveAndMaybeCompressFile( - sourceFilePath, - targetFilePath, - moveAndCompressOptions + sourceFilePath, + targetFilePath, + moveAndCompressOptions ); } From 488dc14f173724327896e02b8c73722583b24236 Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Thu, 6 Jan 2022 15:02:18 +0800 Subject: [PATCH 3/3] Reverted version bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 38345f4..a0f6420 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "streamroller", - "version": "2.2.4", + "version": "2.2.3", "description": "file streams that roll over when size limits, or dates are reached", "main": "lib/index.js", "directories": {