Skip to content

Commit

Permalink
Merge pull request #65 from rnd-debug/feat/compress-with-rights
Browse files Browse the repository at this point in the history
feat: enable to set mode of the compressed archive
  • Loading branch information
lamweili committed Jan 10, 2022
2 parents 2f8f1bd + 488dc14 commit 545c3b6
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/RollingFileWriteStream.js
Expand Up @@ -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
moveAndCompressOptions
);
}

Expand Down
21 changes: 17 additions & 4 deletions lib/moveAndMaybeCompressFile.js
Expand Up @@ -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`
Expand All @@ -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}`
Expand Down
52 changes: 50 additions & 2 deletions test/moveAndMaybeCompressFile-test.js
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 => {
Expand All @@ -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();
});
});

0 comments on commit 545c3b6

Please sign in to comment.