-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
72 additions
and
13 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
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 |
---|---|---|
@@ -1,16 +1,43 @@ | ||
import { readFile, writeFile } from 'node:fs/promises'; | ||
import { promisify } from 'node:util'; | ||
import { gzip as gz } from 'node:zlib'; | ||
import { gunzip as gunzipCB, gzip as gz } from 'node:zlib'; | ||
|
||
const gzip = promisify(gz); | ||
const gunzip = promisify(gunzipCB); | ||
|
||
export async function compressFile(file: string): Promise<string> { | ||
export enum OSFlags { | ||
FAT = 0, | ||
Unix = 3, | ||
HPFS = 6, // cspell:ignore hpfs | ||
MACOS = 7, | ||
NTFS = 11, | ||
} | ||
|
||
// https://docs.fileformat.com/compression/gz/#:~:text=A%20GZ%20file%20is%20a,compression%20formats%20on%20UNIX%20systems. | ||
|
||
const OSSystemIDOffset = 9; | ||
|
||
export async function compressFile(file: string, os?: OSFlags): Promise<string> { | ||
if (file.endsWith('.gz')) return file; | ||
|
||
const targetFile = file + '.gz'; | ||
|
||
const buf = await readFile(file); | ||
const zBuf = await gzip(buf); | ||
const zBuf = await compress(await readFile(file), os); | ||
await writeFile(targetFile, zBuf); | ||
return targetFile; | ||
} | ||
|
||
export async function compress(buf: string | Uint8Array | Buffer, os?: OSFlags): Promise<Uint8Array> { | ||
const zBuf = await gzip(buf); | ||
const osFlag = os ?? zBuf[OSSystemIDOffset]; | ||
zBuf[OSSystemIDOffset] = osFlag; | ||
return zBuf; | ||
} | ||
export async function decompress(buf: Uint8Array | Buffer, encoding?: undefined): Promise<Uint8Array>; | ||
export async function decompress(buf: Uint8Array | Buffer, encoding: 'utf8'): Promise<string>; | ||
export async function decompress(buf: Uint8Array | Buffer, encoding: 'utf8' | undefined): Promise<string | Uint8Array>; | ||
export async function decompress(buf: Uint8Array | Buffer, encoding?: 'utf8'): Promise<string | Uint8Array> { | ||
const dBuf = gunzip(buf); | ||
if (!encoding) return dBuf; | ||
return (await dBuf).toString(encoding); | ||
} |
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { globP } from '../util/globP.js'; | ||
import type { OSFlags } from './compressFiles.js'; | ||
import { compressFile } from './compressFiles.js'; | ||
|
||
// cspell:ignore nodir | ||
|
||
export async function gzip(globs: string[]): Promise<void> { | ||
export async function gzip(globs: string[], os?: OSFlags): Promise<void> { | ||
const files = await globP(globs, { nodir: true }); | ||
for (const fileName of files) { | ||
await compressFile(fileName); | ||
await compressFile(fileName, os); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { compressFile } from './compressFiles.js'; | ||
export { compressFile, OSFlags } from './compressFiles.js'; | ||
export { gzip } from './gzip.js'; |