Skip to content

Commit 9731ae9

Browse files
committed
Add compression options to compressions
Closes #23
1 parent 33c0f46 commit 9731ae9

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

src/compress.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ import { readdir } from "node:fs/promises";
33
import { extname, resolve } from "node:path";
44
import { hrtime } from "node:process";
55
import { promises as stream } from "node:stream";
6+
import type { BrotliOptions, ZlibOptions, ZstdOptions } from "node:zlib";
67
import * as zlib from "node:zlib";
78

89
import type { AstroIntegrationLogger } from "astro";
910

10-
interface CompressionOptions {
11+
export type { BrotliOptions, ZlibOptions, ZstdOptions };
12+
13+
interface CompressionOptions<O extends object> {
1114
dir: string;
1215
extensions: Array<string>;
1316
batchSize: number;
14-
enabled: boolean | undefined;
17+
enabled: boolean | O | undefined;
1518
}
1619

1720
async function* walkDir(dir: string, extensions: Array<string>): AsyncGenerator<string> {
@@ -30,14 +33,14 @@ const filterFile = (file: string, extensions: Array<string>): boolean => {
3033
return extensions.some((ext) => extname(file) === ext);
3134
};
3235

33-
const compress = async <T extends NodeJS.WritableStream>(
36+
const compress = async <T extends NodeJS.WritableStream, O extends object>(
3437
name: string,
3538
compressedFileNames: string,
3639
compressor: () => T,
3740
logger: AstroIntegrationLogger,
38-
{ dir, extensions, batchSize, enabled }: CompressionOptions,
41+
{ dir, extensions, batchSize, enabled }: CompressionOptions<O>,
3942
): Promise<void> => {
40-
if (!enabled) {
43+
if (!enabled && typeof enabled !== "object") {
4144
logger.warn(`${name} compression disabled, skipping...`);
4245
return;
4346
}
@@ -68,32 +71,47 @@ export const gzip = async (
6871
dir: string,
6972
logger: AstroIntegrationLogger,
7073
extensions: Array<string>,
71-
enabled?: boolean,
74+
enabled?: boolean | ZlibOptions,
7275
batchSize = 10,
7376
): Promise<void> => {
74-
await compress("gzip", "gz", zlib.createGzip.bind({ level: 9 }), logger, { dir, extensions, enabled, batchSize });
77+
await compress("gzip", "gz", zlib.createGzip.bind({ level: 9 }), logger, {
78+
dir,
79+
extensions,
80+
enabled,
81+
batchSize,
82+
});
7583
};
7684

7785
export const brotli = async (
7886
dir: string,
7987
logger: AstroIntegrationLogger,
8088
extensions: Array<string>,
81-
enabled?: boolean,
89+
enabled?: boolean | BrotliOptions,
8290
batchSize = 10,
8391
): Promise<void> => {
84-
await compress("brotli", "br", zlib.createBrotliCompress, logger, { dir, extensions, enabled, batchSize });
92+
await compress("brotli", "br", zlib.createBrotliCompress, logger, {
93+
dir,
94+
extensions,
95+
enabled,
96+
batchSize,
97+
});
8598
};
8699

87100
export const zstd = async (
88101
dir: string,
89102
logger: AstroIntegrationLogger,
90103
extensions: Array<string>,
91-
enabled?: boolean,
104+
enabled?: boolean | ZstdOptions,
92105
batchSize = 10,
93106
): Promise<void> => {
94107
if (typeof zlib.createZstdCompress !== "function") {
95108
logger.warn("zstd compression is not supported in this Node.js version.");
96109
return;
97110
}
98-
await compress("zstd", "zst", zlib.createZstdCompress, logger, { dir, extensions, enabled, batchSize });
111+
await compress("zstd", "zst", zlib.createZstdCompress, logger, {
112+
dir,
113+
extensions,
114+
enabled,
115+
batchSize,
116+
});
99117
};

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { fileURLToPath } from "node:url";
22

33
import type { AstroIntegration } from "astro";
4-
4+
import type { BrotliOptions, ZlibOptions, ZstdOptions } from "./compress.js";
55
import { brotli, gzip, zstd } from "./compress.js";
66

77
const defaultFileExtensions = [".css", ".js", ".html", ".xml", ".cjs", ".mjs", ".svg", ".txt"];
88

99
interface Options {
1010
/** Enable gzip compression */
11-
gzip?: boolean;
11+
gzip?: boolean | ZlibOptions;
1212
/** Enable brotli compression */
13-
brotli?: boolean;
13+
brotli?: boolean | BrotliOptions;
1414
/** Enable zstd compression */
15-
zstd?: boolean;
15+
zstd?: boolean | ZstdOptions;
1616
/** Extensions to compress, must be in the format `.html`, `.css` etc */
1717
fileExtensions?: Array<string>;
1818
/** Number of files to batch process */

0 commit comments

Comments
 (0)