vite-plugin-singlefile-compression 2.0.10 → 2.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,9 +4,9 @@ declare const compressors: {
4
4
  deflate(buf: zlib.InputType): Buffer;
5
5
  gzip(buf: zlib.InputType): Buffer;
6
6
  brotli: typeof zlib.brotliCompressSync;
7
- zstd: typeof zlib.zstdCompressSync;
7
+ zstd: (buf: zlib.InputType) => NonSharedBuffer;
8
8
  };
9
- export type compressor = ((buf: zlib.InputType) => Buffer);
9
+ export type compressor = ((buf: zlib.InputType) => (Buffer | Uint8Array));
10
10
  export type compressFormat = keyof typeof compressors;
11
- export declare function compress(format: compressFormat, buf: zlib.InputType, useBase128: boolean, compressor: compressor): string;
11
+ export declare function compress(format: compressFormat, buf: zlib.InputType, useBase128: boolean, compressor: compressor | undefined): any;
12
12
  export {};
package/dist/compress.js CHANGED
@@ -1,20 +1,29 @@
1
1
  import base128 from "base128-ascii";
2
2
  import zlib from 'zlib';
3
- const zlibDefaultOptions = {
4
- level: zlib.constants.Z_BEST_COMPRESSION,
5
- };
6
3
  const compressors = {
7
4
  "deflate-raw"(buf) {
8
- return zlib.deflateRawSync(buf, zlibDefaultOptions);
5
+ return zlib.deflateRawSync(buf, {
6
+ level: zlib.constants.Z_BEST_COMPRESSION,
7
+ });
9
8
  },
10
9
  deflate(buf) {
11
- return zlib.deflateSync(buf, zlibDefaultOptions);
10
+ return zlib.deflateSync(buf, {
11
+ level: zlib.constants.Z_BEST_COMPRESSION,
12
+ });
12
13
  },
13
14
  gzip(buf) {
14
- return zlib.gzipSync(buf, zlibDefaultOptions);
15
+ return zlib.gzipSync(buf, {
16
+ level: zlib.constants.Z_BEST_COMPRESSION,
17
+ });
15
18
  },
16
19
  brotli: zlib.brotliCompressSync,
17
- zstd: zlib.zstdCompressSync,
20
+ zstd: zlib.zstdCompressSync && function (buf) {
21
+ return zlib.zstdCompressSync(buf, {
22
+ params: {
23
+ [zlib.constants.ZSTD_c_compressionLevel]: 22
24
+ }
25
+ });
26
+ },
18
27
  };
19
28
  function switchCompressor(format) {
20
29
  if (Object.prototype.hasOwnProperty.call(compressors, format)) {
@@ -25,15 +34,21 @@ function switchCompressor(format) {
25
34
  }
26
35
  let funcName = format + 'CompressSync';
27
36
  if (Object.prototype.hasOwnProperty.call(zlib, funcName)) {
28
- const f = zlib[format + 'CompressSync'];
37
+ const f = zlib[funcName];
29
38
  if (typeof f == 'function')
30
39
  return f;
31
40
  }
32
41
  throw Error(`Could not get compressor: Unknown compress format '${format}', please set your compressor function.`);
33
42
  }
34
43
  export function compress(format, buf, useBase128, compressor) {
35
- const outBuf = compressor ? compressor(buf) : switchCompressor(format)(buf);
36
- return useBase128
37
- ? base128.encode(outBuf).toJSTemplateLiterals()
38
- : outBuf.toString('base64');
44
+ if (typeof compressor != 'function')
45
+ compressor = switchCompressor(format);
46
+ const outBuf = compressor(buf);
47
+ if (useBase128)
48
+ return base128.encode(outBuf).toJSTemplateLiterals();
49
+ if (outBuf instanceof Buffer)
50
+ return outBuf.toString('base64');
51
+ if (typeof outBuf.toBase64 == 'function') // Uint8Array Node25
52
+ return outBuf.toBase64();
53
+ return Buffer.from(outBuf).toString('base64');
39
54
  }
@@ -1,6 +1,6 @@
1
1
  import { compressFormat, compressor } from './compress.js';
2
2
  export declare const template: {
3
- base(script: string, format: compressFormat, useBase128: boolean, compressor: compressor): string;
3
+ base(script: string, format: compressFormat, useBase128: boolean, compressor: compressor | undefined): string;
4
4
  assets(assetsJSON: string): string;
5
5
  css(cssSource: string): string;
6
6
  icon(dataURL: string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-singlefile-compression",
3
- "version": "2.0.10",
3
+ "version": "2.0.11",
4
4
  "author": "bddjr",
5
5
  "license": "MIT",
6
6
  "description": "Compress all assets and embeds them into dist/index.html, making it convenient to share as a single HTML file.",