@e-mc/compress 0.8.4 → 0.8.6
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 +1 -1
- package/README.md +4 -4
- package/index.js +31 -31
- package/package.json +3 -3
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
## Interface
|
|
11
11
|
|
|
12
|
-
- https://www.unpkg.com/@e-mc/types@0.8.
|
|
12
|
+
- https://www.unpkg.com/@e-mc/types@0.8.6/lib/index.d.ts
|
|
13
13
|
|
|
14
14
|
```typescript
|
|
15
15
|
import type { CompressLevel } from "./squared";
|
|
@@ -51,9 +51,9 @@ interface CompressConstructor extends ModuleConstructor {
|
|
|
51
51
|
|
|
52
52
|
## References
|
|
53
53
|
|
|
54
|
-
- https://www.unpkg.com/@e-mc/types@0.8.
|
|
55
|
-
- https://www.unpkg.com/@e-mc/types@0.8.
|
|
56
|
-
- https://www.unpkg.com/@e-mc/types@0.8.
|
|
54
|
+
- https://www.unpkg.com/@e-mc/types@0.8.6/lib/squared.d.ts
|
|
55
|
+
- https://www.unpkg.com/@e-mc/types@0.8.6/lib/compress.d.ts
|
|
56
|
+
- https://www.unpkg.com/@e-mc/types@0.8.6/lib/settings.d.ts
|
|
57
57
|
|
|
58
58
|
## LICENSE
|
|
59
59
|
|
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.6",
|
|
4
4
|
"description": "Compress constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -23,8 +23,8 @@
|
|
|
23
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.6",
|
|
27
|
+
"@e-mc/types": "0.8.6",
|
|
28
28
|
"tinify": "^1.7.1",
|
|
29
29
|
"wawoff2": "^2.0.1",
|
|
30
30
|
"woff2sfnt-sfnt2woff": "^1.0.0"
|