@@ -3,15 +3,18 @@ import { readdir } from "node:fs/promises";
33import { extname , resolve } from "node:path" ;
44import { hrtime } from "node:process" ;
55import { promises as stream } from "node:stream" ;
6+ import type { BrotliOptions , ZlibOptions , ZstdOptions } from "node:zlib" ;
67import * as zlib from "node:zlib" ;
78
89import 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
1720async 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
7785export 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
87100export 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} ;
0 commit comments