@e-mc/compress 0.10.1 → 0.10.3

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.
Files changed (5) hide show
  1. package/LICENSE +10 -10
  2. package/README.md +126 -126
  3. package/index.d.ts +4 -4
  4. package/index.js +13 -16
  5. package/package.json +31 -31
package/LICENSE CHANGED
@@ -1,11 +1,11 @@
1
- Copyright 2024 An Pham
2
-
3
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
-
5
- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
-
7
- 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
-
9
- 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
-
1
+ Copyright 2024 An Pham
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
11
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -1,127 +1,127 @@
1
- # @e-mc/compress
2
-
3
- * NodeJS 16
4
- * ES2020
5
-
6
- ## General Usage
7
-
8
- * [Read the Docs](https://e-mc.readthedocs.io)
9
-
10
- ## Interface
11
-
12
- * [View Source](https://www.unpkg.com/@e-mc/types@0.10.1/lib/index.d.ts)
13
-
14
- ```typescript
15
- import type { IModule, ModuleConstructor } from "./index";
16
- import type { BufferResult, CompressFormat, CompressLevel, ReadableOptions, TryFileCompressor } from "./compress";
17
- import type { CompressModule, CompressSettings } from "./settings";
18
-
19
- import type { WriteStream } from "fs";
20
- import type { Readable } from "stream";
21
- import type { BrotliCompress, Gzip } from "zlib";
22
-
23
- interface ICompress extends IModule {
24
- module: CompressModule;
25
- level: Record<string, number>;
26
- compressors: Record<string, TryFileCompressor>;
27
- chunkSize?: number;
28
- init(...args: unknown[]): this;
29
- register(format: string, callback: TryFileCompressor): void;
30
- getLevel(value: string, fallback?: number): number | undefined;
31
- getReadable(file: string | URL | Buffer, options?: ReadableOptions): Readable;
32
- createGzip(file: string | Buffer, options?: CompressLevel): Gzip;
33
- createBrotliCompress(file: string | Buffer, options?: CompressLevel): BrotliCompress;
34
- createWriteStreamAsGzip(file: string | Buffer, output: string, options?: CompressLevel): WriteStream;
35
- createWriteStreamAsBrotli(file: string | Buffer, output: string, options?: CompressLevel): WriteStream;
36
- writeGzip(file: string | Buffer, output: string, options?: CompressLevel): Promise<void>;
37
- writeBrotli(file: string | Buffer, output: string, options?: CompressLevel): Promise<void>;
38
- tryFile(file: string | Buffer, options: CompressFormat): Promise<BufferResult>;
39
- tryFile(file: string | Buffer, output: string, options?: CompressFormat): Promise<BufferResult>;
40
- tryImage(file: string, options: CompressFormat): Promise<BufferResult>;
41
- tryImage(file: string | Buffer, output: string, options?: CompressFormat): Promise<BufferResult>;
42
- set chunkSize(value: number | string | undefined): void;
43
- get chunkSize(): number | undefined;
44
- get settings(): CompressSettings;
45
- }
46
-
47
- interface CompressConstructor extends ModuleConstructor {
48
- singleton(): ICompress;
49
- readonly prototype: ICompress;
50
- new(module?: CompressModule): ICompress;
51
- }
52
- ```
53
-
54
- ## Settings
55
-
56
- ```typescript
57
- import type { BrotliOptions, ZlibOptions } from "zlib";
58
- import type { Options as ZopfliOptions } from "node-zopfli";
59
-
60
- interface CompressModule {
61
- gzip?: ZlibOptions;
62
- brotli?: BrotliOptions;
63
- zopfli?: ZopfliOptions;
64
- settings?: {
65
- broadcast_id?: string | string[];
66
- cache?: boolean;
67
- cache_expires?: number | string;
68
- gzip_level?: number;
69
- brotli_quality?: number;
70
- zopfli_iterations?: number;
71
- chunk_size?: number | string;
72
- };
73
- }
74
- ```
75
-
76
- ### Example usage
77
-
78
- ```javascript
79
- const Compress = require("@e-mc/compress");
80
-
81
- const instance = new Compress({
82
- gzip: {
83
- memLevel: 1,
84
- windowBits: 16
85
- },
86
- tinify: {
87
- api_key: "**********"
88
- },
89
- settings: {
90
- gzip_level: 9, // Lowest priority
91
- brotli_quality: 11,
92
- chunk_size: "16kb" // All compression types
93
- }
94
- });
95
- instance.init();
96
-
97
- const stream = instance.createWriteStreamAsGzip("/tmp/archive.tar", "/path/output/archive.tar.gz", { level: 5, chunkSize: 4 * 1024 }); // Override settings
98
- stream
99
- .on("finish", () => console.log("finish"))
100
- .on("error", err => console.error(err));
101
-
102
- const options = {
103
- plugin: "@pi-r/tinify",
104
- format: "png", // Recommended
105
- timeout: 60 * 1000, // 1m
106
- options: {
107
- apiKey: "**********" // Override settings
108
- }
109
- };
110
- instance.tryImage("/tmp/image.png", "/path/output/compressed.png", options)
111
- .then(data => {
112
- console.log(Buffer.byteLength(data));
113
- })
114
- .catch(err => console.error(err));
115
- ```
116
-
117
- ## References
118
-
119
- - https://www.unpkg.com/@e-mc/types@0.10.1/lib/squared.d.ts
120
- - https://www.unpkg.com/@e-mc/types@0.10.1/lib/compress.d.ts
121
- - https://www.unpkg.com/@e-mc/types@0.10.1/lib/settings.d.ts
122
-
123
- * https://www.npmjs.com/package/@types/node
124
-
125
- ## LICENSE
126
-
1
+ # @e-mc/compress
2
+
3
+ * NodeJS 16
4
+ * ES2020
5
+
6
+ ## General Usage
7
+
8
+ * [Read the Docs](https://e-mc.readthedocs.io)
9
+
10
+ ## Interface
11
+
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.10.3/lib/index.d.ts)
13
+
14
+ ```typescript
15
+ import type { IModule, ModuleConstructor } from "./index";
16
+ import type { BufferResult, CompressFormat, CompressLevel, ReadableOptions, TryFileCompressor } from "./compress";
17
+ import type { CompressModule, CompressSettings } from "./settings";
18
+
19
+ import type { WriteStream } from "fs";
20
+ import type { Readable } from "stream";
21
+ import type { BrotliCompress, Gzip } from "zlib";
22
+
23
+ interface ICompress extends IModule {
24
+ module: CompressModule;
25
+ level: Record<string, number>;
26
+ compressors: Record<string, TryFileCompressor>;
27
+ chunkSize?: number;
28
+ init(...args: unknown[]): this;
29
+ register(format: string, callback: TryFileCompressor): void;
30
+ getLevel(value: string, fallback?: number): number | undefined;
31
+ getReadable(file: string | URL | Buffer, options?: ReadableOptions): Readable;
32
+ createGzip(file: string | Buffer, options?: CompressLevel): Gzip;
33
+ createBrotliCompress(file: string | Buffer, options?: CompressLevel): BrotliCompress;
34
+ createWriteStreamAsGzip(file: string | Buffer, output: string, options?: CompressLevel): WriteStream;
35
+ createWriteStreamAsBrotli(file: string | Buffer, output: string, options?: CompressLevel): WriteStream;
36
+ writeGzip(file: string | Buffer, output: string, options?: CompressLevel): Promise<void>;
37
+ writeBrotli(file: string | Buffer, output: string, options?: CompressLevel): Promise<void>;
38
+ tryFile(file: string | Buffer, options: CompressFormat): Promise<BufferResult>;
39
+ tryFile(file: string | Buffer, output: string, options?: CompressFormat): Promise<BufferResult>;
40
+ tryImage(file: string, options: CompressFormat): Promise<BufferResult>;
41
+ tryImage(file: string | Buffer, output: string, options?: CompressFormat): Promise<BufferResult>;
42
+ set chunkSize(value: number | string | undefined): void;
43
+ get chunkSize(): number | undefined;
44
+ get settings(): CompressSettings;
45
+ }
46
+
47
+ interface CompressConstructor extends ModuleConstructor {
48
+ singleton(): ICompress;
49
+ readonly prototype: ICompress;
50
+ new(module?: CompressModule): ICompress;
51
+ }
52
+ ```
53
+
54
+ ## Settings
55
+
56
+ ```typescript
57
+ import type { BrotliOptions, ZlibOptions } from "zlib";
58
+ import type { Options as ZopfliOptions } from "node-zopfli";
59
+
60
+ interface CompressModule {
61
+ gzip?: ZlibOptions;
62
+ brotli?: BrotliOptions;
63
+ zopfli?: ZopfliOptions;
64
+ settings?: {
65
+ broadcast_id?: string | string[];
66
+ cache?: boolean;
67
+ cache_expires?: number | string;
68
+ gzip_level?: number;
69
+ brotli_quality?: number;
70
+ zopfli_iterations?: number;
71
+ chunk_size?: number | string;
72
+ };
73
+ }
74
+ ```
75
+
76
+ ### Example usage
77
+
78
+ ```javascript
79
+ const Compress = require("@e-mc/compress");
80
+
81
+ const instance = new Compress({
82
+ gzip: {
83
+ memLevel: 1,
84
+ windowBits: 16
85
+ },
86
+ tinify: {
87
+ api_key: "**********"
88
+ },
89
+ settings: {
90
+ gzip_level: 9, // Lowest priority
91
+ brotli_quality: 11,
92
+ chunk_size: "16kb" // All compression types
93
+ }
94
+ });
95
+ instance.init();
96
+
97
+ const stream = instance.createWriteStreamAsGzip("/tmp/archive.tar", "/path/output/archive.tar.gz", { level: 5, chunkSize: 4 * 1024 }); // Override settings
98
+ stream
99
+ .on("finish", () => console.log("finish"))
100
+ .on("error", err => console.error(err));
101
+
102
+ const options = {
103
+ plugin: "@pi-r/tinify",
104
+ format: "png", // Recommended
105
+ timeout: 60 * 1000, // 1m
106
+ options: {
107
+ apiKey: "**********" // Override settings
108
+ }
109
+ };
110
+ instance.tryImage("/tmp/image.png", "/path/output/compressed.png", options)
111
+ .then(data => {
112
+ console.log(Buffer.byteLength(data));
113
+ })
114
+ .catch(err => console.error(err));
115
+ ```
116
+
117
+ ## References
118
+
119
+ - https://www.unpkg.com/@e-mc/types@0.10.3/lib/squared.d.ts
120
+ - https://www.unpkg.com/@e-mc/types@0.10.3/lib/compress.d.ts
121
+ - https://www.unpkg.com/@e-mc/types@0.10.3/lib/settings.d.ts
122
+
123
+ * https://www.npmjs.com/package/@types/node
124
+
125
+ ## LICENSE
126
+
127
127
  BSD 3-Clause
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { CompressConstructor } from '../types/lib';
2
-
3
- declare const Compress: CompressConstructor;
4
-
1
+ import type { CompressConstructor } from '@e-mc/types/lib';
2
+
3
+ declare const Compress: CompressConstructor;
4
+
5
5
  export = Compress;
package/index.js CHANGED
@@ -5,8 +5,8 @@ const fs = require("fs");
5
5
  const stream = require("stream");
6
6
  const zlib = require("zlib");
7
7
  const wawoff2 = require("wawoff2");
8
- const module_1 = require("@e-mc/module");
9
8
  const types_1 = require("@e-mc/types");
9
+ const module_1 = require("@e-mc/module");
10
10
  const { toSfnt, toWoff } = require('woff2sfnt-sfnt2woff');
11
11
  const kChunkSize = Symbol('chunkSize');
12
12
  const CACHE_IMAGE = {};
@@ -28,7 +28,7 @@ function setCacheData(instance, index) {
28
28
  if (CACHE_INIT[index]) {
29
29
  return true;
30
30
  }
31
- TEMP_DIR || (TEMP_DIR = instance.getTempDir({ moduleDir: true, createDir: true, increment: 5 }));
31
+ TEMP_DIR ||= instance.getTempDir({ moduleDir: true, createDir: true, increment: 5 });
32
32
  const settings = instance.settings;
33
33
  const expires = (0, types_1.parseExpires)(settings.cache_expires || 0);
34
34
  if (TEMP_DIR) {
@@ -44,7 +44,6 @@ function setCacheData(instance, index) {
44
44
  const srcDir = path.join(TEMP_DIR, ...paths);
45
45
  try {
46
46
  fs.readdirSync(srcDir, { withFileTypes: true }).forEach(item => {
47
- var _b, _c, _d;
48
47
  const filename = item.name;
49
48
  if (item.isFile()) {
50
49
  const pathname = path.join(srcDir, filename);
@@ -58,7 +57,7 @@ function setCacheData(instance, index) {
58
57
  CACHE_FONTTO[filename] = paths[1];
59
58
  }
60
59
  else {
61
- ((_c = (CACHE_IMAGE[_b = paths[0]] || (CACHE_IMAGE[_b] = {})))[_d = paths[1]] || (_c[_d] = {}))[filename] = stat.ctime;
60
+ ((CACHE_IMAGE[paths[0]] ||= {})[paths[1]] ||= {})[filename] = stat.ctime;
62
61
  }
63
62
  return;
64
63
  }
@@ -179,7 +178,7 @@ class Compress extends module_1 {
179
178
  zopfli.numiterations = level;
180
179
  }
181
180
  else {
182
- zopfli.numiterations ?? (zopfli.numiterations = this.level.zopfli);
181
+ zopfli.numiterations ??= this.level.zopfli;
183
182
  }
184
183
  return this.getReadable(file, options).pipe(GZIP_ZOPFLI.createGzip(zopfli));
185
184
  }
@@ -191,13 +190,13 @@ class Compress extends module_1 {
191
190
  gzip.level = level;
192
191
  }
193
192
  else {
194
- gzip.level ?? (gzip.level = this.level.gz);
193
+ gzip.level ??= this.level.gz;
195
194
  }
196
195
  if (!isNaN(chunkSize = (0, types_1.alignSize)(chunkSize, 1))) {
197
196
  gzip.chunkSize = chunkSize;
198
197
  }
199
198
  else {
200
- gzip.chunkSize ?? (gzip.chunkSize = this.chunkSize);
199
+ gzip.chunkSize ??= this.chunkSize;
201
200
  }
202
201
  return this.getReadable(file, options).pipe(zlib.createGzip(gzip));
203
202
  }
@@ -214,7 +213,7 @@ class Compress extends module_1 {
214
213
  else {
215
214
  brotli = { params };
216
215
  }
217
- params[zlib.constants.BROTLI_PARAM_MODE] = (mimeType || (mimeType = '')).includes('text/') ? zlib.constants.BROTLI_MODE_TEXT : mimeType.includes('font/') ? zlib.constants.BROTLI_MODE_FONT : zlib.constants.BROTLI_MODE_GENERIC;
216
+ params[zlib.constants.BROTLI_PARAM_MODE] = (mimeType ||= '').includes('text/') ? zlib.constants.BROTLI_MODE_TEXT : mimeType.includes('font/') ? zlib.constants.BROTLI_MODE_FONT : zlib.constants.BROTLI_MODE_GENERIC;
218
217
  try {
219
218
  params[zlib.constants.BROTLI_PARAM_SIZE_HINT] = typeof file === 'string' ? fs.statSync(file).size : Buffer.byteLength(file);
220
219
  }
@@ -224,7 +223,7 @@ class Compress extends module_1 {
224
223
  brotli.chunkSize = chunkSize;
225
224
  }
226
225
  else {
227
- brotli.chunkSize ?? (brotli.chunkSize = this.chunkSize);
226
+ brotli.chunkSize ??= this.chunkSize;
228
227
  }
229
228
  return this.getReadable(file, options).pipe(zlib.createBrotliCompress(brotli));
230
229
  }
@@ -256,7 +255,7 @@ class Compress extends module_1 {
256
255
  if (!(0, types_1.isString)(output)) {
257
256
  output = typeof file === 'string' ? file : '';
258
257
  }
259
- options || (options = {});
258
+ options ||= {};
260
259
  const { filename, startTime = process.hrtime(), timeout = 0, sessionId, broadcastId } = options;
261
260
  let format = options.format, hash = options.etag;
262
261
  if (!format) {
@@ -508,7 +507,7 @@ class Compress extends module_1 {
508
507
  output = '';
509
508
  }
510
509
  else {
511
- options || (options = {});
510
+ options ||= {};
512
511
  }
513
512
  const { filename, startTime = process.hrtime(), timeout = 0, sessionId, broadcastId } = options;
514
513
  let plugin = options.plugin;
@@ -570,9 +569,8 @@ class Compress extends module_1 {
570
569
  const pathname = path.join(TEMP_DIR, sanitizePath(plugin), hash);
571
570
  if (module_1.createDir(pathname)) {
572
571
  fs.writeFile(path.join(pathname, cacheKey), result, error => {
573
- var _b, _c;
574
572
  if (!error) {
575
- ((_b = (CACHE_IMAGE[plugin] || (CACHE_IMAGE[plugin] = {})))[_c = hash] || (_b[_c] = {}))[cacheKey] = new Date();
573
+ ((CACHE_IMAGE[plugin] ||= {})[hash] ||= {})[cacheKey] = new Date();
576
574
  }
577
575
  });
578
576
  }
@@ -599,7 +597,7 @@ class Compress extends module_1 {
599
597
  let stored;
600
598
  try {
601
599
  hash = module_1.asHash(data);
602
- stored = (CACHE_IMAGE[plugin] || (CACHE_IMAGE[plugin] = {}))[hash];
600
+ stored = (CACHE_IMAGE[plugin] ||= {})[hash];
603
601
  cacheKey = plugin + ((0, types_1.isPlainObject)(options.options) ? ':' + module_1.asHash(module_1.asString(options.options), 'md5') : '');
604
602
  const ctime = stored?.[cacheKey];
605
603
  if (ctime) {
@@ -650,8 +648,7 @@ class Compress extends module_1 {
650
648
  return this[kChunkSize];
651
649
  }
652
650
  get settings() {
653
- var _b;
654
- return (_b = this.module).settings || (_b.settings = {});
651
+ return this.module.settings ||= {};
655
652
  }
656
653
  }
657
654
  _a = kChunkSize;
package/package.json CHANGED
@@ -1,31 +1,31 @@
1
- {
2
- "name": "@e-mc/compress",
3
- "version": "0.10.1",
4
- "description": "Compress constructor for E-mc.",
5
- "main": "index.js",
6
- "types": "index.d.ts",
7
- "publishConfig": {
8
- "access": "public"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "git+https://github.com/anpham6/e-mc.git",
13
- "directory": "src/compress"
14
- },
15
- "keywords": [
16
- "squared",
17
- "squared-functions"
18
- ],
19
- "author": {
20
- "name": "An Pham",
21
- "email": "anpham6@gmail.com"
22
- },
23
- "license": "BSD 3-Clause",
24
- "homepage": "https://github.com/anpham6/e-mc#readme",
25
- "dependencies": {
26
- "@e-mc/module": "0.10.1",
27
- "@e-mc/types": "0.10.1",
28
- "wawoff2": "^2.0.1",
29
- "woff2sfnt-sfnt2woff": "^1.0.0"
30
- }
31
- }
1
+ {
2
+ "name": "@e-mc/compress",
3
+ "version": "0.10.3",
4
+ "description": "Compress constructor for E-mc.",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/anpham6/e-mc.git",
13
+ "directory": "src/compress"
14
+ },
15
+ "keywords": [
16
+ "squared",
17
+ "squared-functions"
18
+ ],
19
+ "author": {
20
+ "name": "An Pham",
21
+ "email": "anpham6@gmail.com"
22
+ },
23
+ "license": "BSD-3-Clause",
24
+ "homepage": "https://github.com/anpham6/e-mc#readme",
25
+ "dependencies": {
26
+ "@e-mc/module": "0.10.3",
27
+ "@e-mc/types": "0.10.3",
28
+ "wawoff2": "^2.0.1",
29
+ "woff2sfnt-sfnt2woff": "^1.0.0"
30
+ }
31
+ }