This repository has been archived by the owner on Jul 14, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support
compressFile
option (#69)
* allows to `gzip` compress the output file * since the internal architecture does not allow to pass the output directly through an compressing output stream: * the result is written in an uncompressed manner as before, * the file is renamed by appending `.temp` to it, * and then stream processed with a pipeline of `read stream` -> `gzip stream` -> `write stream` into the final output file implements issue #13 with a streaming approach not risking to blow up the memory
- Loading branch information
1 parent
d0bdd6a
commit 06b7cb8
Showing
6 changed files
with
94 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as fs from 'fs'; | ||
import * as zlib from 'zlib'; | ||
|
||
function compressFile(filename: string): Promise<void> { | ||
const tempFilename = `${filename}.temp`; | ||
|
||
fs.renameSync(filename, tempFilename); | ||
|
||
const deleteFile = (file: string): void => { | ||
try { | ||
fs.unlinkSync(file); | ||
} catch (_err) { | ||
/* istanbul ignore next */ | ||
} | ||
}; | ||
|
||
try { | ||
const read = fs.createReadStream(tempFilename); | ||
const zip = zlib.createGzip(); | ||
const write = fs.createWriteStream(filename); | ||
read.pipe(zip).pipe(write); | ||
|
||
return new Promise((resolve, reject) => { | ||
write.on( | ||
'error', | ||
/* istanbul ignore next */ err => { | ||
// close the write stream and propagate the error | ||
write.end(); | ||
reject(err); | ||
}, | ||
); | ||
write.on('finish', () => { | ||
resolve(); | ||
}); | ||
}); | ||
} catch (err) /* istanbul ignore next */ { | ||
// in case of an error: remove the output file and propagate the error | ||
deleteFile(filename); | ||
throw err; | ||
} finally { | ||
// in any case: remove the temp file | ||
deleteFile(tempFilename); | ||
} | ||
} | ||
|
||
export { compressFile }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters