@e-mc/compress 0.8.5 → 0.8.7
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.
- package/LICENSE +8 -4
- package/README.md +55 -2
- package/index.js +31 -31
- package/package.json +4 -4
package/LICENSE
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
Copyright 2024
|
|
1
|
+
Copyright 2024 An Pham
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
6
|
|
|
7
|
-
|
|
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
|
+
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,7 +1,60 @@
|
|
|
1
1
|
# @e-mc/compress
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
* NodeJS 14
|
|
4
|
+
* ES2020
|
|
5
|
+
|
|
6
|
+
## General Usage
|
|
7
|
+
|
|
8
|
+
* [Read the Docs](https://e-mc.readthedocs.io)
|
|
9
|
+
|
|
10
|
+
## Interface
|
|
11
|
+
|
|
12
|
+
- https://www.unpkg.com/@e-mc/types@0.8.7/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, TryFileResult, TryImageResult } 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
|
+
tryFile(file: string | Buffer, config: CompressFormat, callback?: TryFileResult): Promise<BufferResult>;
|
|
39
|
+
tryFile(file: string | Buffer, output: string, config: CompressFormat, callback?: TryFileResult): Promise<BufferResult>;
|
|
40
|
+
tryImage(file: string, config: CompressFormat, callback?: TryImageResult): Promise<BufferResult>;
|
|
41
|
+
tryImage(file: string | Buffer, output: string, config: CompressFormat, callback?: TryImageResult): Promise<BufferResult>;
|
|
42
|
+
get settings(): CompressSettings;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface CompressConstructor extends ModuleConstructor {
|
|
46
|
+
singleton(): ICompress;
|
|
47
|
+
readonly prototype: ICompress;
|
|
48
|
+
new(module?: CompressModule): ICompress;
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## References
|
|
53
|
+
|
|
54
|
+
- https://www.unpkg.com/@e-mc/types@0.8.7/lib/squared.d.ts
|
|
55
|
+
- https://www.unpkg.com/@e-mc/types@0.8.7/lib/compress.d.ts
|
|
56
|
+
- https://www.unpkg.com/@e-mc/types@0.8.7/lib/settings.d.ts
|
|
4
57
|
|
|
5
58
|
## LICENSE
|
|
6
59
|
|
|
7
|
-
|
|
60
|
+
BSD 3-Clause
|
package/index.js
CHANGED
|
@@ -6,9 +6,9 @@ const stream = require("stream");
|
|
|
6
6
|
const zlib = require("zlib");
|
|
7
7
|
const wawoff2 = require("wawoff2");
|
|
8
8
|
const { toSfnt, toWoff } = require('woff2sfnt-sfnt2woff');
|
|
9
|
-
const types_1 = require("
|
|
10
|
-
const lib_v4_1 = require("
|
|
11
|
-
const module_1 = require("
|
|
9
|
+
const types_1 = require("@e-mc/types");
|
|
10
|
+
const lib_v4_1 = require("@e-mc/module/lib-v4");
|
|
11
|
+
const module_1 = require("@e-mc/module");
|
|
12
12
|
const CACHE_IMAGE = {};
|
|
13
13
|
const CACHE_FONT = {};
|
|
14
14
|
const CACHE_FONTFROM = {};
|
|
@@ -53,7 +53,7 @@ function setCacheData(index) {
|
|
|
53
53
|
try {
|
|
54
54
|
const stat = fs.lstatSync(pathname);
|
|
55
55
|
if (stat.atimeMs + expires > current) {
|
|
56
|
-
if (index === 0
|
|
56
|
+
if (index === 0) {
|
|
57
57
|
CACHE_FONT[paths.concat(filename).join('_')] = [pathname, stat.ctime];
|
|
58
58
|
CACHE_FONTFROM[filename] = paths[0];
|
|
59
59
|
CACHE_FONTTO[filename] = paths[1];
|
|
@@ -244,7 +244,7 @@ class Compress extends module_1.default {
|
|
|
244
244
|
}
|
|
245
245
|
else {
|
|
246
246
|
if (!fromCache) {
|
|
247
|
-
this.writeTimeProcess(ext || format, (0, types_1.isString)(result) ? path.basename(result) : filename || "Completed"
|
|
247
|
+
this.writeTimeProcess(ext || format, (0, types_1.isString)(result) ? path.basename(result) : filename || "Completed", startTime, { type: 8, sessionId, broadcastId });
|
|
248
248
|
}
|
|
249
249
|
if (callback) {
|
|
250
250
|
callback(null, result || data, ext);
|
|
@@ -256,10 +256,10 @@ class Compress extends module_1.default {
|
|
|
256
256
|
if (timeout > 0) {
|
|
257
257
|
timer = setTimeout(() => {
|
|
258
258
|
aborted = true;
|
|
259
|
-
endProcess((0, types_1.errorValue)("Timeout was exceeded"
|
|
259
|
+
endProcess((0, types_1.errorValue)("Timeout was exceeded", format));
|
|
260
260
|
}, timeout);
|
|
261
261
|
}
|
|
262
|
-
this.formatMessage(8
|
|
262
|
+
this.formatMessage(8, format, [compressing ? 'Compressing file...' : 'Decompressing file...', filename || (output ? path.basename(output) : '')], (0, types_1.isString)(file) ? file : '', { titleColor: 'magenta', sessionId, broadcastId });
|
|
263
263
|
};
|
|
264
264
|
try {
|
|
265
265
|
const writeFont = (data, ext, from) => {
|
|
@@ -296,7 +296,7 @@ class Compress extends module_1.default {
|
|
|
296
296
|
};
|
|
297
297
|
const hasFont = () => {
|
|
298
298
|
let from, to;
|
|
299
|
-
if (cache && (CACHE_INIT[0] || setCacheData.call(this, 0
|
|
299
|
+
if (cache && (CACHE_INIT[0] || setCacheData.call(this, 0)) && (from = CACHE_FONTFROM[hash]) && (to = CACHE_FONTTO[hash])) {
|
|
300
300
|
const cacheKey = from + `_${to}_` + hash;
|
|
301
301
|
const tempFont = CACHE_FONT[cacheKey];
|
|
302
302
|
if (tempFont) {
|
|
@@ -311,7 +311,7 @@ class Compress extends module_1.default {
|
|
|
311
311
|
else {
|
|
312
312
|
endProcess(null, data, to, '', true);
|
|
313
313
|
}
|
|
314
|
-
this.formatMessage(8
|
|
314
|
+
this.formatMessage(8, from, [pathname ? path.basename(pathname) : "Completed" + ` -> font/${to}`, 'cache'], tempFont[1].toLocaleString(), { ...module_1.default.LOG_STYLE_NOTICE, hintBold: true, sessionId, broadcastId });
|
|
315
315
|
return true;
|
|
316
316
|
}
|
|
317
317
|
catch {
|
|
@@ -322,7 +322,7 @@ class Compress extends module_1.default {
|
|
|
322
322
|
return false;
|
|
323
323
|
}
|
|
324
324
|
};
|
|
325
|
-
const errorResponse = (font) => endProcess((0, types_1.errorValue)('Invalid MIME', (font ? font.mime : "Unknown"
|
|
325
|
+
const errorResponse = (font) => endProcess((0, types_1.errorValue)('Invalid MIME', (font ? font.mime : "Unknown") + ': ' + (output || filename || "Unknown")));
|
|
326
326
|
const data = Buffer.isBuffer(file) ? file : fs.readFileSync(file);
|
|
327
327
|
switch (format = format.toLowerCase()) {
|
|
328
328
|
case 'gz':
|
|
@@ -357,8 +357,8 @@ class Compress extends module_1.default {
|
|
|
357
357
|
const checkResult = (result, from) => {
|
|
358
358
|
module_1.default.resolveMime(result).then(font => {
|
|
359
359
|
switch (font?.mime) {
|
|
360
|
-
case "font/ttf"
|
|
361
|
-
case "font/otf"
|
|
360
|
+
case "font/ttf":
|
|
361
|
+
case "font/otf":
|
|
362
362
|
writeFont(result, font.ext, from);
|
|
363
363
|
break;
|
|
364
364
|
default:
|
|
@@ -370,13 +370,13 @@ class Compress extends module_1.default {
|
|
|
370
370
|
module_1.default.resolveMime(data).then(font => {
|
|
371
371
|
try {
|
|
372
372
|
switch (font?.mime) {
|
|
373
|
-
case "font/woff"
|
|
373
|
+
case "font/woff":
|
|
374
374
|
if (cache) {
|
|
375
375
|
CACHE_FONTFROM[hash] = 'woff';
|
|
376
376
|
}
|
|
377
377
|
checkResult(toSfnt(data), 'woff');
|
|
378
378
|
break;
|
|
379
|
-
case "font/woff2"
|
|
379
|
+
case "font/woff2":
|
|
380
380
|
if (cache) {
|
|
381
381
|
CACHE_FONTFROM[hash] = 'woff2';
|
|
382
382
|
}
|
|
@@ -412,17 +412,17 @@ class Compress extends module_1.default {
|
|
|
412
412
|
module_1.default.resolveMime(data).then(font => {
|
|
413
413
|
try {
|
|
414
414
|
switch (font?.mime) {
|
|
415
|
-
case "font/ttf"
|
|
415
|
+
case "font/ttf":
|
|
416
416
|
if (cache) {
|
|
417
417
|
CACHE_FONTFROM[hash] = 'ttf';
|
|
418
418
|
}
|
|
419
|
-
wawoff2.compress(data).then(result => checkResult(result, "font/woff2"
|
|
419
|
+
wawoff2.compress(data).then(result => checkResult(result, "font/woff2", 'ttf'));
|
|
420
420
|
break;
|
|
421
|
-
case "font/otf"
|
|
421
|
+
case "font/otf":
|
|
422
422
|
if (cache) {
|
|
423
423
|
CACHE_FONTFROM[hash] = 'otf';
|
|
424
424
|
}
|
|
425
|
-
checkResult(toWoff(data), "font/woff"
|
|
425
|
+
checkResult(toWoff(data), "font/woff", 'otf');
|
|
426
426
|
break;
|
|
427
427
|
default:
|
|
428
428
|
errorResponse(font);
|
|
@@ -483,7 +483,7 @@ class Compress extends module_1.default {
|
|
|
483
483
|
if (!(0, types_1.isString)(output)) {
|
|
484
484
|
output = typeof file === 'string' ? file : '';
|
|
485
485
|
}
|
|
486
|
-
const ext = output ? path.extname(output).substring(1) : config.format || "Unknown"
|
|
486
|
+
const ext = output ? path.extname(output).substring(1) : config.format || "Unknown";
|
|
487
487
|
const format = config.format || ext;
|
|
488
488
|
return new Promise((resolve, reject) => {
|
|
489
489
|
let timer = null, hash, cacheKey, aborted, apiKey;
|
|
@@ -509,12 +509,12 @@ class Compress extends module_1.default {
|
|
|
509
509
|
}
|
|
510
510
|
const complete = (err) => {
|
|
511
511
|
const value = output ? path.basename(output) : filename || (0, types_1.isString)(file) && path.basename(file);
|
|
512
|
-
const status = value ? plugin + ' -> ' + value : "Completed"
|
|
512
|
+
const status = value ? plugin + ' -> ' + value : "Completed";
|
|
513
513
|
if (ctime) {
|
|
514
|
-
this.formatMessage(8
|
|
514
|
+
this.formatMessage(8, value ? path.extname(value).substring(1) : '', [status, 'cache'], ctime.toLocaleString(), { ...module_1.default.LOG_STYLE_NOTICE, hintBold: true, sessionId, broadcastId });
|
|
515
515
|
}
|
|
516
516
|
else {
|
|
517
|
-
this.writeTimeProcess(ext, status, startTime, { type: 8
|
|
517
|
+
this.writeTimeProcess(ext, status, startTime, { type: 8, sessionId, broadcastId });
|
|
518
518
|
}
|
|
519
519
|
if (callback) {
|
|
520
520
|
callback(err, result);
|
|
@@ -546,7 +546,7 @@ class Compress extends module_1.default {
|
|
|
546
546
|
case 'jpg':
|
|
547
547
|
case 'jpeg':
|
|
548
548
|
case 'webp':
|
|
549
|
-
apiKey = config.options?.apiKey || (module_1.default.enabled("process.env.apply"
|
|
549
|
+
apiKey = config.options?.apiKey || (module_1.default.enabled("process.env.apply") ? process.env.TINIFY_KEY : '');
|
|
550
550
|
break;
|
|
551
551
|
default:
|
|
552
552
|
failed((0, types_1.errorMessage)(plugin, 'Unsupported MIME', format));
|
|
@@ -557,9 +557,9 @@ class Compress extends module_1.default {
|
|
|
557
557
|
return;
|
|
558
558
|
}
|
|
559
559
|
switch (module_1.default.lookupMime(ext)) {
|
|
560
|
-
case "image/jpeg"
|
|
561
|
-
case "image/png"
|
|
562
|
-
case "image/webp"
|
|
560
|
+
case "image/jpeg":
|
|
561
|
+
case "image/png":
|
|
562
|
+
case "image/webp":
|
|
563
563
|
break;
|
|
564
564
|
default:
|
|
565
565
|
failed((0, types_1.errorMessage)(plugin, 'Unsupported format', getFormat()));
|
|
@@ -574,7 +574,7 @@ class Compress extends module_1.default {
|
|
|
574
574
|
failed(err);
|
|
575
575
|
return;
|
|
576
576
|
}
|
|
577
|
-
if (this.settings.cache && setCacheData.call(this, 1
|
|
577
|
+
if (this.settings.cache && setCacheData.call(this, 1)) {
|
|
578
578
|
let stored;
|
|
579
579
|
try {
|
|
580
580
|
stored = (CACHE_IMAGE[plugin] || (CACHE_IMAGE[plugin] = {}))[hash = module_1.default.asHash(data)];
|
|
@@ -591,12 +591,12 @@ class Compress extends module_1.default {
|
|
|
591
591
|
}
|
|
592
592
|
}
|
|
593
593
|
}
|
|
594
|
-
this.formatMessage(8
|
|
594
|
+
this.formatMessage(8, ext, ['Compressing image...', plugin], filename || output, { titleColor: 'magenta', sessionId, broadcastId });
|
|
595
595
|
if (timeout > 0) {
|
|
596
|
-
timer = setTimeout(() => failed((0, types_1.errorMessage)(plugin, "Timeout was exceeded"
|
|
596
|
+
timer = setTimeout(() => failed((0, types_1.errorMessage)(plugin, "Timeout was exceeded")), timeout);
|
|
597
597
|
}
|
|
598
598
|
if (apiKey) {
|
|
599
|
-
const apiProxy = proxyUrl && (typeof proxyUrl === 'function' ? proxyUrl("https://api.tinify.com"
|
|
599
|
+
const apiProxy = proxyUrl && (typeof proxyUrl === 'function' ? proxyUrl("https://api.tinify.com") : proxyUrl) || this.module.tinify?.proxy || module_1.default.enabled("process.env.apply") && process.env.TINIFY_PROXY || '';
|
|
600
600
|
const key = apiKey + apiProxy;
|
|
601
601
|
let tinypng = CACHE_TINIFY[key];
|
|
602
602
|
const validate = () => {
|
|
@@ -628,7 +628,7 @@ class Compress extends module_1.default {
|
|
|
628
628
|
validate();
|
|
629
629
|
}
|
|
630
630
|
else {
|
|
631
|
-
failed(err || new Error("Unknown"
|
|
631
|
+
failed(err || new Error("Unknown"));
|
|
632
632
|
}
|
|
633
633
|
});
|
|
634
634
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/compress",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.7",
|
|
4
4
|
"description": "Compress constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
"name": "An Pham",
|
|
21
21
|
"email": "anpham6@gmail.com"
|
|
22
22
|
},
|
|
23
|
-
"license": "
|
|
23
|
+
"license": "BSD 3-Clause",
|
|
24
24
|
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@e-mc/module": "0.8.
|
|
27
|
-
"@e-mc/types": "0.8.
|
|
26
|
+
"@e-mc/module": "0.8.7",
|
|
27
|
+
"@e-mc/types": "0.8.7",
|
|
28
28
|
"tinify": "^1.7.1",
|
|
29
29
|
"wawoff2": "^2.0.1",
|
|
30
30
|
"woff2sfnt-sfnt2woff": "^1.0.0"
|