Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable to set mode of the compressed archive #65

Merged
merged 3 commits into from Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
});
});