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