@e-mc/compress 0.9.9 → 0.9.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.
Files changed (5) hide show
  1. package/LICENSE +10 -10
  2. package/README.md +130 -130
  3. package/index.d.ts +4 -4
  4. package/index.js +12 -15
  5. package/package.json +32 -32
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,131 +1,131 @@
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.9.9/lib/index.d.ts)
13
-
14
- ```typescript
15
- import type { CompressLevel } from "./squared";
16
-
17
- import type { IModule, ModuleConstructor } from "./index";
18
- import type { BufferResult, CompressFormat, TryFileCompressor } from "./compress";
19
- import type { CompressModule, CompressSettings } from "./settings";
20
-
21
- import type { WriteStream } from "fs";
22
- import type { Readable } from "stream";
23
- import type { BrotliCompress, Gzip } from "zlib";
24
-
25
- interface ICompress extends IModule {
26
- module: CompressModule;
27
- level: Record<string, number>;
28
- compressors: Record<string, TryFileCompressor>;
29
- chunkSize?: number;
30
- init(...args: unknown[]): this;
31
- register(format: string, callback: TryFileCompressor): void;
32
- getLevel(value: string, fallback?: number): number | undefined;
33
- getReadable(file: string | URL | Buffer): Readable;
34
- createGzip(file: string | Buffer, options?: CompressLevel): Gzip;
35
- createBrotliCompress(file: string | Buffer, options?: CompressLevel): BrotliCompress;
36
- createWriteStreamAsGzip(file: string | Buffer, output: string, options?: CompressLevel): WriteStream;
37
- createWriteStreamAsBrotli(file: string | Buffer, output: string, options?: CompressLevel): WriteStream;
38
- writeGzip(file: string | Buffer, output: string, options?: CompressLevel): Promise<void>;
39
- writeBrotli(file: string | Buffer, output: string, options?: CompressLevel): Promise<void>;
40
- tryFile(file: string | Buffer, options: CompressFormat): Promise<BufferResult>;
41
- tryFile(file: string | Buffer, output: string, options?: CompressFormat): Promise<BufferResult>;
42
- tryImage(file: string, options: CompressFormat): Promise<BufferResult>;
43
- tryImage(file: string | Buffer, output: string, options?: CompressFormat): Promise<BufferResult>;
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
- tinify?: {
65
- api_key?: string;
66
- proxy?: string;
67
- };
68
- settings?: {
69
- broadcast_id?: string | string[];
70
- cache?: boolean;
71
- cache_expires?: number | string;
72
- gzip_level?: number;
73
- brotli_quality?: number;
74
- zopfli_iterations?: number;
75
- chunk_size?: number | string;
76
- };
77
- }
78
- ```
79
-
80
- ### Example usage
81
-
82
- ```javascript
83
- const Compress = require("@e-mc/compress");
84
-
85
- const instance = new Compress({
86
- gzip: {
87
- memLevel: 1,
88
- windowBits: 16
89
- },
90
- tinify: {
91
- api_key: "**********"
92
- },
93
- settings: {
94
- gzip_level: 9, // Lowest priority
95
- brotli_quality: 11,
96
- chunk_size: "16kb" // All compression types
97
- }
98
- });
99
- instance.init();
100
-
101
- const stream = instance.createWriteStreamAsGzip("/tmp/archive.tar", "/path/output/archive.tar.gz", { level: 5, chunkSize: 4 * 1024 }); // Override settings
102
- stream
103
- .on("finish", () => console.log("finish"))
104
- .on("error", err => console.error(err));
105
-
106
- const options = {
107
- plugin: "tinify",
108
- format: "png", // Optional with extension
109
- timeout: 60 * 1000, // 1m
110
- options: {
111
- apiKey: "**********" // Override settings
112
- }
113
- };
114
- instance.tryImage("/tmp/image.png", "/path/output/compressed.png", options)
115
- .then(data => {
116
- console.log(Buffer.byteLength(data));
117
- })
118
- .catch(err => console.error(err));
119
- ```
120
-
121
- ## References
122
-
123
- - https://www.unpkg.com/@e-mc/types@0.9.9/lib/squared.d.ts
124
- - https://www.unpkg.com/@e-mc/types@0.9.9/lib/compress.d.ts
125
- - https://www.unpkg.com/@e-mc/types@0.9.9/lib/settings.d.ts
126
-
127
- * https://www.npmjs.com/package/@types/node
128
-
129
- ## LICENSE
130
-
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.9.11/lib/index.d.ts)
13
+
14
+ ```typescript
15
+ import type { CompressLevel } from "./squared";
16
+
17
+ import type { IModule, ModuleConstructor } from "./index";
18
+ import type { BufferResult, CompressFormat, TryFileCompressor } from "./compress";
19
+ import type { CompressModule, CompressSettings } from "./settings";
20
+
21
+ import type { WriteStream } from "fs";
22
+ import type { Readable } from "stream";
23
+ import type { BrotliCompress, Gzip } from "zlib";
24
+
25
+ interface ICompress extends IModule {
26
+ module: CompressModule;
27
+ level: Record<string, number>;
28
+ compressors: Record<string, TryFileCompressor>;
29
+ chunkSize?: number;
30
+ init(...args: unknown[]): this;
31
+ register(format: string, callback: TryFileCompressor): void;
32
+ getLevel(value: string, fallback?: number): number | undefined;
33
+ getReadable(file: string | URL | Buffer): Readable;
34
+ createGzip(file: string | Buffer, options?: CompressLevel): Gzip;
35
+ createBrotliCompress(file: string | Buffer, options?: CompressLevel): BrotliCompress;
36
+ createWriteStreamAsGzip(file: string | Buffer, output: string, options?: CompressLevel): WriteStream;
37
+ createWriteStreamAsBrotli(file: string | Buffer, output: string, options?: CompressLevel): WriteStream;
38
+ writeGzip(file: string | Buffer, output: string, options?: CompressLevel): Promise<void>;
39
+ writeBrotli(file: string | Buffer, output: string, options?: CompressLevel): Promise<void>;
40
+ tryFile(file: string | Buffer, options: CompressFormat): Promise<BufferResult>;
41
+ tryFile(file: string | Buffer, output: string, options?: CompressFormat): Promise<BufferResult>;
42
+ tryImage(file: string, options: CompressFormat): Promise<BufferResult>;
43
+ tryImage(file: string | Buffer, output: string, options?: CompressFormat): Promise<BufferResult>;
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
+ tinify?: {
65
+ api_key?: string;
66
+ proxy?: string;
67
+ };
68
+ settings?: {
69
+ broadcast_id?: string | string[];
70
+ cache?: boolean;
71
+ cache_expires?: number | string;
72
+ gzip_level?: number;
73
+ brotli_quality?: number;
74
+ zopfli_iterations?: number;
75
+ chunk_size?: number | string;
76
+ };
77
+ }
78
+ ```
79
+
80
+ ### Example usage
81
+
82
+ ```javascript
83
+ const Compress = require("@e-mc/compress");
84
+
85
+ const instance = new Compress({
86
+ gzip: {
87
+ memLevel: 1,
88
+ windowBits: 16
89
+ },
90
+ tinify: {
91
+ api_key: "**********"
92
+ },
93
+ settings: {
94
+ gzip_level: 9, // Lowest priority
95
+ brotli_quality: 11,
96
+ chunk_size: "16kb" // All compression types
97
+ }
98
+ });
99
+ instance.init();
100
+
101
+ const stream = instance.createWriteStreamAsGzip("/tmp/archive.tar", "/path/output/archive.tar.gz", { level: 5, chunkSize: 4 * 1024 }); // Override settings
102
+ stream
103
+ .on("finish", () => console.log("finish"))
104
+ .on("error", err => console.error(err));
105
+
106
+ const options = {
107
+ plugin: "tinify",
108
+ format: "png", // Optional with extension
109
+ timeout: 60 * 1000, // 1m
110
+ options: {
111
+ apiKey: "**********" // Override settings
112
+ }
113
+ };
114
+ instance.tryImage("/tmp/image.png", "/path/output/compressed.png", options)
115
+ .then(data => {
116
+ console.log(Buffer.byteLength(data));
117
+ })
118
+ .catch(err => console.error(err));
119
+ ```
120
+
121
+ ## References
122
+
123
+ - https://www.unpkg.com/@e-mc/types@0.9.11/lib/squared.d.ts
124
+ - https://www.unpkg.com/@e-mc/types@0.9.11/lib/compress.d.ts
125
+ - https://www.unpkg.com/@e-mc/types@0.9.11/lib/settings.d.ts
126
+
127
+ * https://www.npmjs.com/package/@types/node
128
+
129
+ ## LICENSE
130
+
131
131
  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
@@ -27,7 +27,7 @@ function setCacheData(index) {
27
27
  if (CACHE_INIT[index]) {
28
28
  return true;
29
29
  }
30
- TEMP_DIR || (TEMP_DIR = this.getTempDir({ moduleDir: true, createDir: true, increment: 5 }));
30
+ TEMP_DIR ||= this.getTempDir({ moduleDir: true, createDir: true, increment: 5 });
31
31
  const settings = this.settings;
32
32
  const expires = (0, types_1.parseExpires)(settings.cache_expires || 0);
33
33
  if (TEMP_DIR) {
@@ -43,7 +43,6 @@ function setCacheData(index) {
43
43
  const srcDir = path.join(TEMP_DIR, ...paths);
44
44
  try {
45
45
  fs.readdirSync(srcDir, { withFileTypes: true }).forEach(item => {
46
- var _a, _b, _c;
47
46
  const filename = item.name;
48
47
  if (item.isFile()) {
49
48
  const pathname = path.join(srcDir, filename);
@@ -57,7 +56,7 @@ function setCacheData(index) {
57
56
  CACHE_FONTTO[filename] = paths[1];
58
57
  }
59
58
  else {
60
- ((_b = (CACHE_IMAGE[_a = paths[0]] || (CACHE_IMAGE[_a] = {})))[_c = paths[1]] || (_b[_c] = {}))[filename] = stat.ctime;
59
+ ((CACHE_IMAGE[paths[0]] ||= {})[paths[1]] ||= {})[filename] = stat.ctime;
61
60
  }
62
61
  return;
63
62
  }
@@ -152,7 +151,7 @@ class Compress extends module_1 {
152
151
  zopfli.numiterations = level;
153
152
  }
154
153
  else {
155
- zopfli.numiterations ?? (zopfli.numiterations = this.level.zopfli);
154
+ zopfli.numiterations ??= this.level.zopfli;
156
155
  }
157
156
  return this.getReadable(file).pipe(GZIP_ZOPFLI.createGzip(zopfli));
158
157
  }
@@ -164,13 +163,13 @@ class Compress extends module_1 {
164
163
  gzip.level = level;
165
164
  }
166
165
  else {
167
- gzip.level ?? (gzip.level = this.level.gz);
166
+ gzip.level ??= this.level.gz;
168
167
  }
169
168
  if (!isNaN(chunkSize = (0, types_1.alignSize)(chunkSize, 1))) {
170
169
  gzip.chunkSize = chunkSize;
171
170
  }
172
171
  else {
173
- gzip.chunkSize ?? (gzip.chunkSize = this.chunkSize);
172
+ gzip.chunkSize ??= this.chunkSize;
174
173
  }
175
174
  return this.getReadable(file).pipe(zlib.createGzip(gzip));
176
175
  }
@@ -187,7 +186,7 @@ class Compress extends module_1 {
187
186
  else {
188
187
  brotli = { params };
189
188
  }
190
- 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;
189
+ 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;
191
190
  try {
192
191
  params[zlib.constants.BROTLI_PARAM_SIZE_HINT] = typeof file === 'string' ? fs.statSync(file).size : Buffer.byteLength(file);
193
192
  }
@@ -197,7 +196,7 @@ class Compress extends module_1 {
197
196
  brotli.chunkSize = chunkSize;
198
197
  }
199
198
  else {
200
- brotli.chunkSize ?? (brotli.chunkSize = this.chunkSize);
199
+ brotli.chunkSize ??= this.chunkSize;
201
200
  }
202
201
  return this.getReadable(file).pipe(zlib.createBrotliCompress(brotli));
203
202
  }
@@ -227,7 +226,7 @@ class Compress extends module_1 {
227
226
  output = '';
228
227
  }
229
228
  else {
230
- options || (options = {});
229
+ options ||= {};
231
230
  }
232
231
  if (!(0, types_1.isString)(output)) {
233
232
  output = typeof file === 'string' ? file : '';
@@ -479,7 +478,7 @@ class Compress extends module_1 {
479
478
  output = '';
480
479
  }
481
480
  else {
482
- options || (options = {});
481
+ options ||= {};
483
482
  }
484
483
  const { plugin = "tinify", filename, startTime = process.hrtime(), timeout = 0, proxyUrl, sessionId, broadcastId } = options;
485
484
  if (!(0, types_1.isString)(output)) {
@@ -520,9 +519,8 @@ class Compress extends module_1 {
520
519
  const pathname = path.join(TEMP_DIR, plugin, hash);
521
520
  if (module_1.createDir(pathname)) {
522
521
  fs.writeFile(path.join(pathname, cacheKey), result, error => {
523
- var _a, _b;
524
522
  if (!error) {
525
- ((_a = (CACHE_IMAGE[plugin] || (CACHE_IMAGE[plugin] = {})))[_b = hash] || (_a[_b] = {}))[cacheKey] = new Date();
523
+ ((CACHE_IMAGE[plugin] ||= {})[hash] ||= {})[cacheKey] = new Date();
526
524
  }
527
525
  });
528
526
  }
@@ -574,7 +572,7 @@ class Compress extends module_1 {
574
572
  if (this.settings.cache && setCacheData.call(this, 1)) {
575
573
  let stored;
576
574
  try {
577
- stored = (CACHE_IMAGE[plugin] || (CACHE_IMAGE[plugin] = {}))[hash = module_1.asHash(data)];
575
+ stored = (CACHE_IMAGE[plugin] ||= {})[hash = module_1.asHash(data)];
578
576
  cacheKey = plugin + ':' + module_1.asHash(apiKey ? format + apiKey : module_1.asString(options.options), 'md5');
579
577
  const ctime = stored?.[cacheKey];
580
578
  if (ctime) {
@@ -631,8 +629,7 @@ class Compress extends module_1 {
631
629
  });
632
630
  }
633
631
  get settings() {
634
- var _a;
635
- return (_a = this.module).settings || (_a.settings = {});
632
+ return this.module.settings ||= {};
636
633
  }
637
634
  }
638
635
 
package/package.json CHANGED
@@ -1,32 +1,32 @@
1
- {
2
- "name": "@e-mc/compress",
3
- "version": "0.9.9",
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.9.9",
27
- "@e-mc/types": "0.9.9",
28
- "tinify": "^1.7.1",
29
- "wawoff2": "^2.0.1",
30
- "woff2sfnt-sfnt2woff": "^1.0.0"
31
- }
32
- }
1
+ {
2
+ "name": "@e-mc/compress",
3
+ "version": "0.9.11",
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.9.11",
27
+ "@e-mc/types": "0.9.11",
28
+ "tinify": "^1.7.1",
29
+ "wawoff2": "^2.0.1",
30
+ "woff2sfnt-sfnt2woff": "^1.0.0"
31
+ }
32
+ }