@e-mc/compress 0.0.1
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 +11 -0
- package/README.md +5 -0
- package/index.d.ts +5 -0
- package/index.js +626 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Copyright 2023 An Pham
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
+
|
|
11
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,626 @@
|
|
|
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;
|
|
622
|
+
|
|
623
|
+
if (exports.default) {
|
|
624
|
+
module.exports = exports.default;
|
|
625
|
+
module.exports.default = exports.default;
|
|
626
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@e-mc/compress",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Compress constructor for e-mc.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/anpham6/e-mc.git",
|
|
13
|
+
"directory": "src/compress"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"squared",
|
|
17
|
+
"squared-functions"
|
|
18
|
+
],
|
|
19
|
+
"author": {
|
|
20
|
+
"name": "An Pham",
|
|
21
|
+
"email": "anpham6@gmail.com"
|
|
22
|
+
},
|
|
23
|
+
"license": "BSD 3-Clause",
|
|
24
|
+
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@e-mc/module": "0.0.1",
|
|
27
|
+
"@e-mc/types": "0.0.1",
|
|
28
|
+
"tinify": "^1.7.1",
|
|
29
|
+
"wawoff2": "^2.0.1",
|
|
30
|
+
"woff2sfnt-sfnt2woff": "^1.0.0"
|
|
31
|
+
}
|
|
32
|
+
}
|