@e-mc/compress 0.3.2 → 0.4.0
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/index.js +665 -621
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -1,624 +1,668 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const path = require("path");
|
|
4
|
-
const fs = require("fs");
|
|
5
|
-
const stream = require("stream");
|
|
6
|
-
const zlib = require("zlib");
|
|
7
|
-
const wawoff2 = require("wawoff2");
|
|
8
|
-
const { toSfnt, toWoff } = require('woff2sfnt-sfnt2woff');
|
|
9
|
-
const types_1 = require("../types");
|
|
10
|
-
const lib_v4_1 = require("../module/lib-v4");
|
|
11
|
-
const module_1 = require("../module");
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
let
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if (
|
|
116
|
-
this.level
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
if (
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
if (
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
if (
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
if (
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
}
|
|
621
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const fs = require("fs");
|
|
5
|
+
const stream = require("stream");
|
|
6
|
+
const zlib = require("zlib");
|
|
7
|
+
const wawoff2 = require("wawoff2");
|
|
8
|
+
const { toSfnt, toWoff } = require('woff2sfnt-sfnt2woff');
|
|
9
|
+
const types_1 = require("../types");
|
|
10
|
+
const lib_v4_1 = require("../module/lib-v4");
|
|
11
|
+
const module_1 = require("../module");
|
|
12
|
+
const CACHE_IMAGE = {};
|
|
13
|
+
const CACHE_FONT = {};
|
|
14
|
+
const CACHE_FONTFROM = {};
|
|
15
|
+
const CACHE_FONTTO = {};
|
|
16
|
+
const CACHE_INIT = [false, false];
|
|
17
|
+
const CACHE_TINIFY = {};
|
|
18
|
+
let SINGLETON_INSTANCE;
|
|
19
|
+
let TEMP_DIR = '';
|
|
20
|
+
const GZIP_ZOPFLI = (function () {
|
|
21
|
+
try {
|
|
22
|
+
return require('node-zopfli');
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
})();
|
|
28
|
+
function setCacheData(index) {
|
|
29
|
+
if (CACHE_INIT[index]) {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
TEMP_DIR || (TEMP_DIR = this.getTempDir({ moduleDir: true, createDir: true, increment: 5 }));
|
|
33
|
+
const settings = this.settings;
|
|
34
|
+
const expires = (0, types_1.parseExpires)(settings.cache_expires || 0);
|
|
35
|
+
if (TEMP_DIR) {
|
|
36
|
+
if (expires === 0) {
|
|
37
|
+
if (CACHE_INIT.every(value => !value)) {
|
|
38
|
+
module_1.default.removeDir(TEMP_DIR, true);
|
|
39
|
+
}
|
|
40
|
+
settings.cache_expires = 0;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
const current = Date.now();
|
|
44
|
+
(function recurse(paths) {
|
|
45
|
+
const srcDir = path.join(TEMP_DIR, ...paths);
|
|
46
|
+
try {
|
|
47
|
+
fs.readdirSync(srcDir, { withFileTypes: true }).forEach(item => {
|
|
48
|
+
var _a, _b, _c;
|
|
49
|
+
const filename = item.name;
|
|
50
|
+
if (item.isFile()) {
|
|
51
|
+
const pathname = path.join(srcDir, filename);
|
|
52
|
+
if (paths.length === 2) {
|
|
53
|
+
try {
|
|
54
|
+
const stat = fs.lstatSync(pathname);
|
|
55
|
+
if (stat.atimeMs + expires > current) {
|
|
56
|
+
if (index === 0 /* CACHE_TYPE.FONT */) {
|
|
57
|
+
CACHE_FONT[paths.concat(filename).join('_')] = [pathname, stat.ctime];
|
|
58
|
+
CACHE_FONTFROM[filename] = paths[0];
|
|
59
|
+
CACHE_FONTTO[filename] = paths[1];
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
((_b = (CACHE_IMAGE[_a = paths[0]] || (CACHE_IMAGE[_a] = {})))[_c = paths[1]] || (_b[_c] = {}))[filename] = stat.ctime;
|
|
63
|
+
}
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
removeFile(pathname);
|
|
71
|
+
}
|
|
72
|
+
else if (item.isDirectory()) {
|
|
73
|
+
recurse(paths.concat(filename));
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
}
|
|
79
|
+
})([]);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return CACHE_INIT[index] = true;
|
|
83
|
+
}
|
|
84
|
+
const removeFile = (pathname) => fs.unlink(pathname, () => { });
|
|
85
|
+
const checkChunkSize = (value) => typeof value === 'number' && value > 0 && value % 1024 === 0;
|
|
86
|
+
class Compress extends module_1.default {
|
|
87
|
+
static singleton() {
|
|
88
|
+
if (!SINGLETON_INSTANCE) {
|
|
89
|
+
SINGLETON_INSTANCE = new Compress();
|
|
90
|
+
Object.defineProperty(SINGLETON_INSTANCE, "_logEnabled", { value: false, enumerable: true, writable: false });
|
|
91
|
+
Object.defineProperty(SINGLETON_INSTANCE, "_logFlushed", { value: true, enumerable: true, writable: false });
|
|
92
|
+
Object.defineProperty(SINGLETON_INSTANCE, "host", { set(value) { }, get() { return null; } });
|
|
93
|
+
}
|
|
94
|
+
return SINGLETON_INSTANCE;
|
|
95
|
+
}
|
|
96
|
+
constructor(module = {}) {
|
|
97
|
+
super();
|
|
98
|
+
this.module = module;
|
|
99
|
+
this.level = {
|
|
100
|
+
gz: 9,
|
|
101
|
+
br: 11,
|
|
102
|
+
zopfli: 15
|
|
103
|
+
};
|
|
104
|
+
this.compressors = {};
|
|
105
|
+
this._moduleName = 'compress';
|
|
106
|
+
}
|
|
107
|
+
init(...args) {
|
|
108
|
+
let { gzip_level, brotli_quality, zopfli_iterations, chunk_size } = (0, types_1.isPlainObject)(args[0]) && args[0].settings || this.settings;
|
|
109
|
+
if (gzip_level !== undefined && (gzip_level = Math.floor(+gzip_level)) >= -1 && gzip_level <= 9) {
|
|
110
|
+
this.level.gz = Math.floor(gzip_level);
|
|
111
|
+
}
|
|
112
|
+
if (brotli_quality !== undefined && (brotli_quality = Math.floor(+brotli_quality)) >= 0 && brotli_quality <= 11) {
|
|
113
|
+
this.level.br = brotli_quality;
|
|
114
|
+
}
|
|
115
|
+
if (zopfli_iterations && (zopfli_iterations = Math.floor(+zopfli_iterations)) > 0) {
|
|
116
|
+
this.level.zopfli = zopfli_iterations;
|
|
117
|
+
}
|
|
118
|
+
if (chunk_size && checkChunkSize(chunk_size = +chunk_size)) {
|
|
119
|
+
this.chunkSize = chunk_size;
|
|
120
|
+
}
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
register(format, callback, level) {
|
|
124
|
+
this.compressors[format = format.toLowerCase()] = callback;
|
|
125
|
+
if (level !== undefined) {
|
|
126
|
+
this.level[format] = level;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
getLevel(value, fallback) {
|
|
130
|
+
const result = this.level[value = value.toLowerCase()] ?? this.level[value = path.extname(value).substring(1)];
|
|
131
|
+
if (!isNaN(result)) {
|
|
132
|
+
return result;
|
|
133
|
+
}
|
|
134
|
+
if (fallback !== undefined) {
|
|
135
|
+
return fallback;
|
|
136
|
+
}
|
|
137
|
+
switch (value) {
|
|
138
|
+
case 'gz':
|
|
139
|
+
return zlib.constants.Z_DEFAULT_LEVEL;
|
|
140
|
+
case 'br':
|
|
141
|
+
return zlib.constants.BROTLI_DEFAULT_QUALITY;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
getReadable(file) {
|
|
145
|
+
return file instanceof Buffer ? stream.Readable.from(file) : fs.createReadStream(file);
|
|
146
|
+
}
|
|
147
|
+
createGzip(file, options) {
|
|
148
|
+
let algorithm, level, chunkSize;
|
|
149
|
+
if (options) {
|
|
150
|
+
({ algorithm, level, chunkSize } = options);
|
|
151
|
+
}
|
|
152
|
+
if (algorithm === 'zopfli') {
|
|
153
|
+
if (GZIP_ZOPFLI) {
|
|
154
|
+
const zopfli = (0, types_1.isPlainObject)(this.module.zopfli) ? { ...this.module.zopfli } : {};
|
|
155
|
+
if (typeof level === 'number') {
|
|
156
|
+
zopfli.numiterations = level;
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
zopfli.numiterations ?? (zopfli.numiterations = this.level.zopfli);
|
|
160
|
+
}
|
|
161
|
+
return this.getReadable(file).pipe(GZIP_ZOPFLI.createGzip(zopfli));
|
|
162
|
+
}
|
|
163
|
+
level = undefined;
|
|
164
|
+
}
|
|
165
|
+
let gzip = this.module.gzip;
|
|
166
|
+
gzip = (0, types_1.isPlainObject)(gzip) ? { ...gzip } : {};
|
|
167
|
+
if (typeof level === 'number') {
|
|
168
|
+
gzip.level = level;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
gzip.level ?? (gzip.level = this.level.gz);
|
|
172
|
+
}
|
|
173
|
+
if (checkChunkSize(chunkSize)) {
|
|
174
|
+
gzip.chunkSize = chunkSize;
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
gzip.chunkSize ?? (gzip.chunkSize = this.chunkSize);
|
|
178
|
+
}
|
|
179
|
+
return this.getReadable(file).pipe(zlib.createGzip(gzip));
|
|
180
|
+
}
|
|
181
|
+
createBrotliCompress(file, options) {
|
|
182
|
+
let level, chunkSize, mimeType;
|
|
183
|
+
if (options) {
|
|
184
|
+
({ level, chunkSize, mimeType } = options);
|
|
185
|
+
}
|
|
186
|
+
const params = { [zlib.constants.BROTLI_PARAM_QUALITY]: level ?? this.level.br };
|
|
187
|
+
let brotli = this.module.brotli;
|
|
188
|
+
if ((0, types_1.isPlainObject)(brotli)) {
|
|
189
|
+
brotli = (0, types_1.isPlainObject)(brotli.params) ? { ...brotli, params: Object.assign(params, brotli.params) } : { ...brotli, params };
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
brotli = { params };
|
|
193
|
+
}
|
|
194
|
+
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;
|
|
195
|
+
params[zlib.constants.BROTLI_PARAM_SIZE_HINT] = (0, lib_v4_1.byteLength)(file);
|
|
196
|
+
if (checkChunkSize(chunkSize)) {
|
|
197
|
+
brotli.chunkSize = chunkSize;
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
brotli.chunkSize ?? (brotli.chunkSize = this.chunkSize);
|
|
201
|
+
}
|
|
202
|
+
return this.getReadable(file).pipe(zlib.createBrotliCompress(brotli));
|
|
203
|
+
}
|
|
204
|
+
createWriteStreamAsGzip(file, output, options) {
|
|
205
|
+
return this.createGzip(file, options).pipe(fs.createWriteStream(output));
|
|
206
|
+
}
|
|
207
|
+
createWriteStreamAsBrotli(file, output, options) {
|
|
208
|
+
return this.createBrotliCompress(file, options).pipe(fs.createWriteStream(output));
|
|
209
|
+
}
|
|
210
|
+
tryFile(file, output, config, callback) {
|
|
211
|
+
if (typeof config === 'function') {
|
|
212
|
+
callback = config;
|
|
213
|
+
config = undefined;
|
|
214
|
+
}
|
|
215
|
+
if ((0, types_1.isObject)(output)) {
|
|
216
|
+
config = output;
|
|
217
|
+
output = '';
|
|
218
|
+
}
|
|
219
|
+
if (!(0, types_1.isString)(output)) {
|
|
220
|
+
output = typeof file === 'string' ? file : '';
|
|
221
|
+
}
|
|
222
|
+
config || (config = {});
|
|
223
|
+
const { filename, startTime = process.hrtime(), timeout = 0, sessionId, broadcastId } = config;
|
|
224
|
+
let format = config.format;
|
|
225
|
+
if (!(0, types_1.isString)(format)) {
|
|
226
|
+
return Promise.reject((0, types_1.errorValue)('Missing option "format"', output || filename));
|
|
227
|
+
}
|
|
228
|
+
let hash = config.etag;
|
|
229
|
+
const cache = this.settings.cache ? !!(hash && (hash = module_1.default.asHash(hash)) || Buffer.isBuffer(file) && (hash = module_1.default.asHash(file))) : false;
|
|
230
|
+
return new Promise((resolve, reject) => {
|
|
231
|
+
let timer = null, aborted;
|
|
232
|
+
const endProcess = (err, data = null, ext, result, fromCache) => {
|
|
233
|
+
if (timer) {
|
|
234
|
+
clearTimeout(timer);
|
|
235
|
+
}
|
|
236
|
+
if (err) {
|
|
237
|
+
if (typeof callback === 'function') {
|
|
238
|
+
callback(err);
|
|
239
|
+
resolve(null);
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
reject(err);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
if (!fromCache) {
|
|
247
|
+
this.writeTimeProcess(ext || format, (0, types_1.isString)(result) ? path.basename(result) : filename || 'Completed', startTime, { type: 8 /* LOG_TYPE.COMPRESS */, sessionId, broadcastId });
|
|
248
|
+
}
|
|
249
|
+
if (callback) {
|
|
250
|
+
callback(null, result || data, ext);
|
|
251
|
+
}
|
|
252
|
+
resolve(data);
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
const startProcess = (compressing) => {
|
|
256
|
+
if (timeout > 0) {
|
|
257
|
+
timer = setTimeout(() => {
|
|
258
|
+
aborted = true;
|
|
259
|
+
endProcess((0, types_1.errorValue)("Timeout was exceeded" /* ERR_MESSAGE.TIMEOUT */, format));
|
|
260
|
+
}, timeout);
|
|
261
|
+
}
|
|
262
|
+
this.formatMessage(8 /* LOG_TYPE.COMPRESS */, format, [compressing ? 'Compressing file...' : 'Decompressing file...', filename || (output ? path.basename(output) : '')], (0, types_1.isString)(file) ? file : '', { titleColor: 'magenta', sessionId, broadcastId });
|
|
263
|
+
};
|
|
264
|
+
try {
|
|
265
|
+
const writeFont = (data, ext, from) => {
|
|
266
|
+
if (output) {
|
|
267
|
+
const pathname = (0, types_1.renameExt)(output, ext);
|
|
268
|
+
fs.writeFile(pathname, data, err => {
|
|
269
|
+
if (!err) {
|
|
270
|
+
endProcess(null, data, ext, pathname);
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
endProcess(err);
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
endProcess(null, data, ext);
|
|
279
|
+
}
|
|
280
|
+
if (cache) {
|
|
281
|
+
const pathname = path.join(from, ext);
|
|
282
|
+
let tempFont;
|
|
283
|
+
if (TEMP_DIR && module_1.default.createDir(tempFont = path.join(TEMP_DIR, pathname))) {
|
|
284
|
+
tempFont = path.join(tempFont, hash);
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
tempFont = this.getTempDir({ pathname, filename: hash, moduleDir: true, createDir: true });
|
|
288
|
+
}
|
|
289
|
+
fs.writeFile(tempFont, data, err => {
|
|
290
|
+
if (!err) {
|
|
291
|
+
CACHE_FONT[from + '_' + ext + '_' + hash] = [tempFont, new Date()];
|
|
292
|
+
CACHE_FONTTO[hash] = ext;
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
const hasFont = () => {
|
|
298
|
+
let from, to;
|
|
299
|
+
if (cache && (CACHE_INIT[0] || setCacheData.call(this, 0 /* CACHE_TYPE.FONT */)) && (from = CACHE_FONTFROM[hash]) && (to = CACHE_FONTTO[hash])) {
|
|
300
|
+
const cacheKey = from + `_${to}_` + hash;
|
|
301
|
+
const tempFont = CACHE_FONT[cacheKey];
|
|
302
|
+
if (tempFont) {
|
|
303
|
+
if (module_1.default.isPath(tempFont[0])) {
|
|
304
|
+
try {
|
|
305
|
+
const data = fs.readFileSync(tempFont[0]);
|
|
306
|
+
let pathname;
|
|
307
|
+
if (output) {
|
|
308
|
+
fs.writeFileSync(pathname = (0, types_1.renameExt)(output, to), data);
|
|
309
|
+
endProcess(null, data, to, pathname, true);
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
endProcess(null, data, to, '', true);
|
|
313
|
+
}
|
|
314
|
+
this.formatMessage(8 /* LOG_TYPE.COMPRESS */, from, [pathname ? path.basename(pathname) : `Completed -> font/${to}`, 'cache'], tempFont[1].toLocaleString(), { ...module_1.default.LOG_STYLE_NOTICE, hintBold: true, sessionId, broadcastId });
|
|
315
|
+
return true;
|
|
316
|
+
}
|
|
317
|
+
catch {
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
delete CACHE_FONT[cacheKey];
|
|
321
|
+
}
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
const errorResponse = (font) => endProcess((0, types_1.errorValue)('Invalid MIME', (font ? font.mime : "Unknown" /* ERR_MESSAGE.UNKNOWN */) + ': ' + (output || filename || "Unknown" /* ERR_MESSAGE.UNKNOWN */)));
|
|
326
|
+
const data = Buffer.isBuffer(file) ? file : fs.readFileSync(file);
|
|
327
|
+
switch (format = format.toLowerCase()) {
|
|
328
|
+
case 'gz':
|
|
329
|
+
case 'br': {
|
|
330
|
+
startProcess(true);
|
|
331
|
+
let transform, chunks;
|
|
332
|
+
if (output) {
|
|
333
|
+
transform = this[format === 'gz' ? 'createWriteStreamAsGzip' : 'createWriteStreamAsBrotli'](data, output, config);
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
chunks = [];
|
|
337
|
+
transform = this[format === 'gz' ? 'createGzip' : 'createBrotliCompress'](data, config).on('data', chunk => chunks.push(chunk));
|
|
338
|
+
}
|
|
339
|
+
transform
|
|
340
|
+
.on('finish', () => {
|
|
341
|
+
if (!aborted) {
|
|
342
|
+
endProcess(null, chunks && Buffer.concat(chunks), format, output);
|
|
343
|
+
}
|
|
344
|
+
})
|
|
345
|
+
.on('error', err => {
|
|
346
|
+
aborted = true;
|
|
347
|
+
endProcess(err);
|
|
348
|
+
});
|
|
349
|
+
break;
|
|
350
|
+
}
|
|
351
|
+
case 'ttf':
|
|
352
|
+
case 'otf': {
|
|
353
|
+
if (hasFont()) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
startProcess(false);
|
|
357
|
+
const checkResult = (result, from) => {
|
|
358
|
+
module_1.default.resolveMime(result).then(font => {
|
|
359
|
+
switch (font?.mime) {
|
|
360
|
+
case 'font/ttf':
|
|
361
|
+
case 'font/otf':
|
|
362
|
+
writeFont(result, font.ext, from);
|
|
363
|
+
break;
|
|
364
|
+
default:
|
|
365
|
+
errorResponse(font);
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
};
|
|
370
|
+
module_1.default.resolveMime(data).then(font => {
|
|
371
|
+
try {
|
|
372
|
+
switch (font?.mime) {
|
|
373
|
+
case 'font/woff':
|
|
374
|
+
if (cache) {
|
|
375
|
+
CACHE_FONTFROM[hash] = 'woff';
|
|
376
|
+
}
|
|
377
|
+
checkResult(toSfnt(data), 'woff');
|
|
378
|
+
break;
|
|
379
|
+
case 'font/woff2':
|
|
380
|
+
if (cache) {
|
|
381
|
+
CACHE_FONTFROM[hash] = 'woff2';
|
|
382
|
+
}
|
|
383
|
+
wawoff2.decompress(data).then(woff => checkResult(woff, 'woff2'));
|
|
384
|
+
break;
|
|
385
|
+
default:
|
|
386
|
+
errorResponse(font);
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
catch (err) {
|
|
391
|
+
endProcess(err);
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
break;
|
|
395
|
+
}
|
|
396
|
+
case 'woff':
|
|
397
|
+
case 'woff2': {
|
|
398
|
+
if (hasFont()) {
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
const checkResult = (result, mimeType, from) => {
|
|
402
|
+
module_1.default.resolveMime(result).then(font => {
|
|
403
|
+
if (font?.mime === mimeType) {
|
|
404
|
+
writeFont(result, font.ext, from);
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
errorResponse(font);
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
};
|
|
411
|
+
startProcess(true);
|
|
412
|
+
module_1.default.resolveMime(data).then(font => {
|
|
413
|
+
try {
|
|
414
|
+
switch (font?.mime) {
|
|
415
|
+
case 'font/ttf':
|
|
416
|
+
if (cache) {
|
|
417
|
+
CACHE_FONTFROM[hash] = 'ttf';
|
|
418
|
+
}
|
|
419
|
+
wawoff2.compress(data).then(result => checkResult(result, 'font/woff2', 'ttf'));
|
|
420
|
+
break;
|
|
421
|
+
case 'font/otf':
|
|
422
|
+
if (cache) {
|
|
423
|
+
CACHE_FONTFROM[hash] = 'otf';
|
|
424
|
+
}
|
|
425
|
+
checkResult(toWoff(data), 'font/woff', 'otf');
|
|
426
|
+
break;
|
|
427
|
+
default:
|
|
428
|
+
errorResponse(font);
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
catch (err) {
|
|
433
|
+
endProcess(err);
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
break;
|
|
437
|
+
}
|
|
438
|
+
default: {
|
|
439
|
+
const compressor = this.compressors[format]?.bind(this);
|
|
440
|
+
if (typeof compressor === 'function') {
|
|
441
|
+
startProcess(true);
|
|
442
|
+
if (compressor.toString().startsWith('async')) {
|
|
443
|
+
compressor.call(this, file, output, config)
|
|
444
|
+
.then(result => {
|
|
445
|
+
if (typeof result === 'string') {
|
|
446
|
+
endProcess(null, null, format, result);
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
endProcess(null, result, format);
|
|
450
|
+
}
|
|
451
|
+
})
|
|
452
|
+
.catch(err => endProcess(err));
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
compressor.call(this, file, output, config, endProcess);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
throw (0, types_1.errorValue)('Missing compression plugin', format);
|
|
460
|
+
}
|
|
461
|
+
break;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
catch (err) {
|
|
466
|
+
endProcess(err);
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
tryImage(file, output, config, callback) {
|
|
471
|
+
if (typeof config === 'function') {
|
|
472
|
+
callback = config;
|
|
473
|
+
config = undefined;
|
|
474
|
+
}
|
|
475
|
+
if ((0, types_1.isObject)(output)) {
|
|
476
|
+
config = output;
|
|
477
|
+
output = '';
|
|
478
|
+
}
|
|
479
|
+
else {
|
|
480
|
+
config || (config = {});
|
|
481
|
+
}
|
|
482
|
+
const { plugin = 'tinify', filename, startTime = process.hrtime(), timeout = 0, proxyUrl, options, sessionId, broadcastId } = config;
|
|
483
|
+
if (!(0, types_1.isString)(output)) {
|
|
484
|
+
output = typeof file === 'string' ? file : '';
|
|
485
|
+
}
|
|
486
|
+
const ext = output ? path.extname(output).substring(1) : config.format || "Unknown" /* ERR_MESSAGE.UNKNOWN */;
|
|
487
|
+
const format = config.format || ext;
|
|
488
|
+
return new Promise((resolve, reject) => {
|
|
489
|
+
let timer = null, hash, cacheKey, aborted, apiKey;
|
|
490
|
+
const failed = (err) => {
|
|
491
|
+
aborted = true;
|
|
492
|
+
if (timer) {
|
|
493
|
+
clearTimeout(timer);
|
|
494
|
+
}
|
|
495
|
+
if (typeof callback === 'function') {
|
|
496
|
+
callback(err, null);
|
|
497
|
+
resolve(null);
|
|
498
|
+
}
|
|
499
|
+
else {
|
|
500
|
+
reject(err);
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
const success = (result, ctime) => {
|
|
504
|
+
if (aborted) {
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
if (timer) {
|
|
508
|
+
clearTimeout(timer);
|
|
509
|
+
}
|
|
510
|
+
const complete = (err) => {
|
|
511
|
+
const value = output ? path.basename(output) : filename || (0, types_1.isString)(file) && path.basename(file);
|
|
512
|
+
const status = value ? plugin + ' -> ' + value : 'Completed';
|
|
513
|
+
if (ctime) {
|
|
514
|
+
this.formatMessage(8 /* LOG_TYPE.COMPRESS */, value ? path.extname(value).substring(1) : '', [status, 'cache'], ctime.toLocaleString(), { ...module_1.default.LOG_STYLE_NOTICE, hintBold: true, sessionId, broadcastId });
|
|
515
|
+
}
|
|
516
|
+
else {
|
|
517
|
+
this.writeTimeProcess(ext, status, startTime, { type: 8 /* LOG_TYPE.COMPRESS */, sessionId, broadcastId });
|
|
518
|
+
}
|
|
519
|
+
if (callback) {
|
|
520
|
+
callback(err, result);
|
|
521
|
+
}
|
|
522
|
+
resolve(result);
|
|
523
|
+
if (!ctime && hash && cacheKey && TEMP_DIR) {
|
|
524
|
+
const pathname = path.join(TEMP_DIR, plugin, hash);
|
|
525
|
+
if (module_1.default.createDir(pathname)) {
|
|
526
|
+
fs.writeFile(path.join(pathname, cacheKey), result, error => {
|
|
527
|
+
var _a, _b;
|
|
528
|
+
if (!error) {
|
|
529
|
+
((_a = (CACHE_IMAGE[plugin] || (CACHE_IMAGE[plugin] = {})))[_b = hash] || (_a[_b] = {}))[cacheKey] = new Date();
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
if (output) {
|
|
536
|
+
fs.writeFile(output, result, err => complete(err));
|
|
537
|
+
}
|
|
538
|
+
else {
|
|
539
|
+
complete(null);
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
const getFormat = () => output ? path.basename(output) : ext;
|
|
543
|
+
if (plugin === 'tinify') {
|
|
544
|
+
switch (format) {
|
|
545
|
+
case 'png':
|
|
546
|
+
case 'jpg':
|
|
547
|
+
case 'jpeg':
|
|
548
|
+
case 'webp':
|
|
549
|
+
apiKey = config.options?.apiKey || (module_1.default.enabled("process.env.apply" /* KEY_NAME.PROCESS_ENV_APPLY */) ? process.env.TINIFY_KEY : '');
|
|
550
|
+
break;
|
|
551
|
+
default:
|
|
552
|
+
failed((0, types_1.errorMessage)(plugin, 'Unsupported MIME', format));
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
if (!(0, types_1.isString)(apiKey)) {
|
|
556
|
+
failed((0, types_1.errorMessage)(plugin, 'Missing API key'));
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
switch (module_1.default.lookupMime(ext)) {
|
|
560
|
+
case 'image/jpeg':
|
|
561
|
+
case 'image/png':
|
|
562
|
+
case 'image/webp':
|
|
563
|
+
break;
|
|
564
|
+
default:
|
|
565
|
+
failed((0, types_1.errorMessage)(plugin, 'Unsupported format', getFormat()));
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
let data;
|
|
570
|
+
try {
|
|
571
|
+
data = Buffer.isBuffer(file) ? file : fs.readFileSync(file);
|
|
572
|
+
}
|
|
573
|
+
catch (err) {
|
|
574
|
+
failed(err);
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
if (this.settings.cache && setCacheData.call(this, 1 /* CACHE_TYPE.IMAGE */)) {
|
|
578
|
+
let stored;
|
|
579
|
+
try {
|
|
580
|
+
stored = (CACHE_IMAGE[plugin] || (CACHE_IMAGE[plugin] = {}))[hash = module_1.default.asHash(data)];
|
|
581
|
+
cacheKey = module_1.default.asHash(plugin + ':' + (apiKey ? format + apiKey : module_1.default.asString(options) || 'unknown'), 'md5');
|
|
582
|
+
const ctime = stored?.[cacheKey];
|
|
583
|
+
if (ctime) {
|
|
584
|
+
success(fs.readFileSync(path.join(TEMP_DIR, plugin, hash, cacheKey)), ctime);
|
|
585
|
+
return;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
catch {
|
|
589
|
+
if (stored && cacheKey) {
|
|
590
|
+
delete stored[cacheKey];
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
this.formatMessage(8 /* LOG_TYPE.COMPRESS */, ext, ['Compressing image...', plugin], filename || output, { titleColor: 'magenta', sessionId, broadcastId });
|
|
595
|
+
if (timeout > 0) {
|
|
596
|
+
timer = setTimeout(() => failed((0, types_1.errorMessage)(plugin, "Timeout was exceeded" /* ERR_MESSAGE.TIMEOUT */)), timeout);
|
|
597
|
+
}
|
|
598
|
+
if (apiKey) {
|
|
599
|
+
const apiProxy = proxyUrl && (typeof proxyUrl === 'function' ? proxyUrl("https://api.tinify.com" /* VALUES.TINIFY_API_ENDPOINT */) : proxyUrl) || this.module.tinify?.proxy || module_1.default.enabled("process.env.apply" /* KEY_NAME.PROCESS_ENV_APPLY */) && process.env.TINIFY_PROXY || '';
|
|
600
|
+
const key = apiKey + apiProxy;
|
|
601
|
+
let tinypng = CACHE_TINIFY[key];
|
|
602
|
+
const validate = () => {
|
|
603
|
+
tinypng = require('tinify');
|
|
604
|
+
tinypng.key = apiKey;
|
|
605
|
+
if (apiProxy) {
|
|
606
|
+
tinypng.proxy = apiProxy;
|
|
607
|
+
}
|
|
608
|
+
tinypng.validate(err => {
|
|
609
|
+
if (!err) {
|
|
610
|
+
fromBuffer();
|
|
611
|
+
CACHE_TINIFY[key] = tinypng;
|
|
612
|
+
}
|
|
613
|
+
else {
|
|
614
|
+
if (err instanceof tinypng.AccountError) {
|
|
615
|
+
delete CACHE_TINIFY[key];
|
|
616
|
+
}
|
|
617
|
+
failed(err);
|
|
618
|
+
}
|
|
619
|
+
});
|
|
620
|
+
};
|
|
621
|
+
const fromBuffer = (cache) => {
|
|
622
|
+
tinypng.fromBuffer(data).toBuffer((err, result) => {
|
|
623
|
+
if (!err && result) {
|
|
624
|
+
success(result);
|
|
625
|
+
}
|
|
626
|
+
else if (cache) {
|
|
627
|
+
delete CACHE_TINIFY[key];
|
|
628
|
+
validate();
|
|
629
|
+
}
|
|
630
|
+
else {
|
|
631
|
+
failed(err || new Error("Unknown" /* ERR_MESSAGE.UNKNOWN */));
|
|
632
|
+
}
|
|
633
|
+
});
|
|
634
|
+
};
|
|
635
|
+
if (!tinypng) {
|
|
636
|
+
validate();
|
|
637
|
+
}
|
|
638
|
+
else {
|
|
639
|
+
fromBuffer(true);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
else {
|
|
643
|
+
try {
|
|
644
|
+
const transform = require(plugin);
|
|
645
|
+
transform(options)(data).then(result => {
|
|
646
|
+
if (result !== data) {
|
|
647
|
+
success(result);
|
|
648
|
+
}
|
|
649
|
+
else {
|
|
650
|
+
failed((0, types_1.errorMessage)(plugin, 'Unsupported MIME', getFormat()));
|
|
651
|
+
}
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
catch (err) {
|
|
655
|
+
failed(err);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
get settings() {
|
|
661
|
+
var _a;
|
|
662
|
+
return (_a = this.module).settings || (_a.settings = {});
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
exports.default = Compress;
|
|
622
666
|
|
|
623
667
|
if (exports.default) {
|
|
624
668
|
module.exports = exports.default;
|