@e-mc/compress 0.9.2 → 0.9.4
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/README.md +4 -4
- package/index.js +67 -92
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
|
|
10
10
|
## Interface
|
|
11
11
|
|
|
12
|
-
* [View Source](https://www.unpkg.com/@e-mc/types@0.9.
|
|
12
|
+
* [View Source](https://www.unpkg.com/@e-mc/types@0.9.4/lib/index.d.ts)
|
|
13
13
|
|
|
14
14
|
```typescript
|
|
15
15
|
import type { CompressLevel } from "./squared";
|
|
@@ -120,9 +120,9 @@ instance.tryImage("/tmp/image.png", "/path/output/compressed.png", options)
|
|
|
120
120
|
|
|
121
121
|
## References
|
|
122
122
|
|
|
123
|
-
- https://www.unpkg.com/@e-mc/types@0.9.
|
|
124
|
-
- https://www.unpkg.com/@e-mc/types@0.9.
|
|
125
|
-
- https://www.unpkg.com/@e-mc/types@0.9.
|
|
123
|
+
- https://www.unpkg.com/@e-mc/types@0.9.4/lib/squared.d.ts
|
|
124
|
+
- https://www.unpkg.com/@e-mc/types@0.9.4/lib/compress.d.ts
|
|
125
|
+
- https://www.unpkg.com/@e-mc/types@0.9.4/lib/settings.d.ts
|
|
126
126
|
|
|
127
127
|
* https://www.npmjs.com/package/@types/node
|
|
128
128
|
|
package/index.js
CHANGED
|
@@ -4,6 +4,7 @@ const fs = require("fs");
|
|
|
4
4
|
const stream = require("stream");
|
|
5
5
|
const zlib = require("zlib");
|
|
6
6
|
const wawoff2 = require("wawoff2");
|
|
7
|
+
const tinify = require("tinify");
|
|
7
8
|
const { toSfnt, toWoff } = require('woff2sfnt-sfnt2woff');
|
|
8
9
|
const types_1 = require("@e-mc/types");
|
|
9
10
|
const module_1 = require("@e-mc/module");
|
|
@@ -12,7 +13,6 @@ const CACHE_FONT = {};
|
|
|
12
13
|
const CACHE_FONTFROM = {};
|
|
13
14
|
const CACHE_FONTTO = {};
|
|
14
15
|
const CACHE_INIT = [false, false];
|
|
15
|
-
const CACHE_TINIFY = {};
|
|
16
16
|
let SINGLETON_INSTANCE;
|
|
17
17
|
let TEMP_DIR = '';
|
|
18
18
|
const GZIP_ZOPFLI = (function () {
|
|
@@ -65,7 +65,7 @@ function setCacheData(index) {
|
|
|
65
65
|
catch {
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
|
-
|
|
68
|
+
fs.unlink(pathname, () => { });
|
|
69
69
|
}
|
|
70
70
|
else if (item.isDirectory()) {
|
|
71
71
|
recurse(paths.concat(filename));
|
|
@@ -79,7 +79,6 @@ function setCacheData(index) {
|
|
|
79
79
|
}
|
|
80
80
|
return CACHE_INIT[index] = true;
|
|
81
81
|
}
|
|
82
|
-
const removeFile = (pathname) => fs.unlink(pathname, () => { });
|
|
83
82
|
class Compress extends module_1 {
|
|
84
83
|
static singleton() {
|
|
85
84
|
if (!SINGLETON_INSTANCE) {
|
|
@@ -119,7 +118,7 @@ class Compress extends module_1 {
|
|
|
119
118
|
}
|
|
120
119
|
register(format, callback, level) {
|
|
121
120
|
this.compressors[format = format.toLowerCase()] = callback;
|
|
122
|
-
if (level
|
|
121
|
+
if (typeof level === 'number') {
|
|
123
122
|
this.level[format] = level;
|
|
124
123
|
}
|
|
125
124
|
}
|
|
@@ -128,7 +127,7 @@ class Compress extends module_1 {
|
|
|
128
127
|
if (!isNaN(result)) {
|
|
129
128
|
return result;
|
|
130
129
|
}
|
|
131
|
-
if (fallback
|
|
130
|
+
if (typeof fallback === 'number') {
|
|
132
131
|
return fallback;
|
|
133
132
|
}
|
|
134
133
|
switch (value) {
|
|
@@ -227,10 +226,12 @@ class Compress extends module_1 {
|
|
|
227
226
|
options = output;
|
|
228
227
|
output = '';
|
|
229
228
|
}
|
|
229
|
+
else {
|
|
230
|
+
options || (options = {});
|
|
231
|
+
}
|
|
230
232
|
if (!(0, types_1.isString)(output)) {
|
|
231
233
|
output = typeof file === 'string' ? file : '';
|
|
232
234
|
}
|
|
233
|
-
options || (options = {});
|
|
234
235
|
const { filename, startTime = process.hrtime(), timeout = 0, sessionId, broadcastId } = options;
|
|
235
236
|
let format = options.format;
|
|
236
237
|
if (!format) {
|
|
@@ -374,29 +375,28 @@ class Compress extends module_1 {
|
|
|
374
375
|
}
|
|
375
376
|
});
|
|
376
377
|
};
|
|
377
|
-
module_1.resolveMime(data)
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
catch (err) {
|
|
398
|
-
endProcess(err);
|
|
378
|
+
module_1.resolveMime(data)
|
|
379
|
+
.then(font => {
|
|
380
|
+
switch (font?.mime) {
|
|
381
|
+
case "font/woff":
|
|
382
|
+
if (cache) {
|
|
383
|
+
CACHE_FONTFROM[hash] = 'woff';
|
|
384
|
+
}
|
|
385
|
+
checkResult(toSfnt(data), 'woff');
|
|
386
|
+
break;
|
|
387
|
+
case "font/woff2":
|
|
388
|
+
if (cache) {
|
|
389
|
+
CACHE_FONTFROM[hash] = 'woff2';
|
|
390
|
+
}
|
|
391
|
+
wawoff2.decompress(data).then(woff => checkResult(woff, 'woff2'));
|
|
392
|
+
break;
|
|
393
|
+
default:
|
|
394
|
+
errorResponse(font);
|
|
395
|
+
break;
|
|
399
396
|
}
|
|
397
|
+
})
|
|
398
|
+
.catch((err) => {
|
|
399
|
+
endProcess(err);
|
|
400
400
|
});
|
|
401
401
|
break;
|
|
402
402
|
}
|
|
@@ -416,29 +416,28 @@ class Compress extends module_1 {
|
|
|
416
416
|
});
|
|
417
417
|
};
|
|
418
418
|
startProcess(true);
|
|
419
|
-
module_1.resolveMime(data)
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
catch (err) {
|
|
440
|
-
endProcess(err);
|
|
419
|
+
module_1.resolveMime(data)
|
|
420
|
+
.then(font => {
|
|
421
|
+
switch (font?.mime) {
|
|
422
|
+
case "font/ttf":
|
|
423
|
+
if (cache) {
|
|
424
|
+
CACHE_FONTFROM[hash] = 'ttf';
|
|
425
|
+
}
|
|
426
|
+
wawoff2.compress(data).then(result => checkResult(result, "font/woff2", 'ttf'));
|
|
427
|
+
break;
|
|
428
|
+
case "font/otf":
|
|
429
|
+
if (cache) {
|
|
430
|
+
CACHE_FONTFROM[hash] = 'otf';
|
|
431
|
+
}
|
|
432
|
+
checkResult(toWoff(data), "font/woff", 'otf');
|
|
433
|
+
break;
|
|
434
|
+
default:
|
|
435
|
+
errorResponse(font);
|
|
436
|
+
break;
|
|
441
437
|
}
|
|
438
|
+
})
|
|
439
|
+
.catch((err) => {
|
|
440
|
+
endProcess(err);
|
|
442
441
|
});
|
|
443
442
|
break;
|
|
444
443
|
}
|
|
@@ -517,7 +516,6 @@ class Compress extends module_1 {
|
|
|
517
516
|
else {
|
|
518
517
|
this.writeTimeProcess(ext, status, startTime, { type: 8, sessionId, broadcastId });
|
|
519
518
|
}
|
|
520
|
-
resolve(result);
|
|
521
519
|
if (!ctime && hash && cacheKey && TEMP_DIR) {
|
|
522
520
|
const pathname = path.join(TEMP_DIR, plugin, hash);
|
|
523
521
|
if (module_1.createDir(pathname)) {
|
|
@@ -529,6 +527,7 @@ class Compress extends module_1 {
|
|
|
529
527
|
});
|
|
530
528
|
}
|
|
531
529
|
}
|
|
530
|
+
resolve(result);
|
|
532
531
|
};
|
|
533
532
|
if (output) {
|
|
534
533
|
fs.writeFile(output, result, err => complete(err));
|
|
@@ -576,7 +575,7 @@ class Compress extends module_1 {
|
|
|
576
575
|
let stored;
|
|
577
576
|
try {
|
|
578
577
|
stored = (CACHE_IMAGE[plugin] || (CACHE_IMAGE[plugin] = {}))[hash = module_1.asHash(data)];
|
|
579
|
-
cacheKey =
|
|
578
|
+
cacheKey = plugin + ':' + module_1.asHash(apiKey ? format + apiKey : module_1.asString(options.options), 'md5');
|
|
580
579
|
const ctime = stored?.[cacheKey];
|
|
581
580
|
if (ctime) {
|
|
582
581
|
success(fs.readFileSync(path.join(TEMP_DIR, plugin, hash, cacheKey)), ctime);
|
|
@@ -595,47 +594,23 @@ class Compress extends module_1 {
|
|
|
595
594
|
}
|
|
596
595
|
if (apiKey) {
|
|
597
596
|
const apiProxy = proxyUrl && (typeof proxyUrl === 'function' ? proxyUrl("https://api.tinify.com") : proxyUrl) || this.module.tinify?.proxy || module_1.enabled("process.env.apply") && process.env.TINIFY_PROXY || '';
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
}
|
|
606
|
-
tinypng.validate(err => {
|
|
607
|
-
if (!err) {
|
|
608
|
-
fromBuffer();
|
|
609
|
-
CACHE_TINIFY[key] = tinypng;
|
|
610
|
-
}
|
|
611
|
-
else {
|
|
612
|
-
if (err instanceof tinypng.AccountError) {
|
|
613
|
-
delete CACHE_TINIFY[key];
|
|
597
|
+
tinify.key = apiKey;
|
|
598
|
+
tinify.proxy = apiProxy;
|
|
599
|
+
tinify.validate(err => {
|
|
600
|
+
if (!err) {
|
|
601
|
+
tinify.fromBuffer(data).toBuffer((error, result) => {
|
|
602
|
+
if (!error && result) {
|
|
603
|
+
success(result);
|
|
614
604
|
}
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
else if (cache) {
|
|
625
|
-
delete CACHE_TINIFY[key];
|
|
626
|
-
validate();
|
|
627
|
-
}
|
|
628
|
-
else {
|
|
629
|
-
failed(err || new Error("Unknown"));
|
|
630
|
-
}
|
|
631
|
-
});
|
|
632
|
-
};
|
|
633
|
-
if (!tinypng) {
|
|
634
|
-
validate();
|
|
635
|
-
}
|
|
636
|
-
else {
|
|
637
|
-
fromBuffer(true);
|
|
638
|
-
}
|
|
605
|
+
else {
|
|
606
|
+
failed(error || new Error("Unknown"));
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
else {
|
|
611
|
+
failed(err);
|
|
612
|
+
}
|
|
613
|
+
});
|
|
639
614
|
}
|
|
640
615
|
else {
|
|
641
616
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/compress",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.4",
|
|
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.9.
|
|
27
|
-
"@e-mc/types": "0.9.
|
|
26
|
+
"@e-mc/module": "0.9.4",
|
|
27
|
+
"@e-mc/types": "0.9.4",
|
|
28
28
|
"tinify": "^1.7.1",
|
|
29
29
|
"wawoff2": "^2.0.1",
|
|
30
30
|
"woff2sfnt-sfnt2woff": "^1.0.0"
|