@e-mc/compress 0.9.1 → 0.9.3

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 (3) hide show
  1. package/README.md +4 -4
  2. package/index.js +25 -48
  3. package/package.json +3 -3
package/README.md CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## Interface
11
11
 
12
- * [View Source](https://www.unpkg.com/@e-mc/types@0.9.1/lib/index.d.ts)
12
+ * [View Source](https://www.unpkg.com/@e-mc/types@0.9.3/lib/index.d.ts)
13
13
 
14
14
  ```typescript
15
15
  import type { CompressLevel } from "./squared";
@@ -120,9 +120,9 @@ instance.tryImage("/tmp/image.png", "/path/output/compressed.png", options)
120
120
 
121
121
  ## References
122
122
 
123
- - https://www.unpkg.com/@e-mc/types@0.9.1/lib/squared.d.ts
124
- - https://www.unpkg.com/@e-mc/types@0.9.1/lib/compress.d.ts
125
- - https://www.unpkg.com/@e-mc/types@0.9.1/lib/settings.d.ts
123
+ - https://www.unpkg.com/@e-mc/types@0.9.3/lib/squared.d.ts
124
+ - https://www.unpkg.com/@e-mc/types@0.9.3/lib/compress.d.ts
125
+ - https://www.unpkg.com/@e-mc/types@0.9.3/lib/settings.d.ts
126
126
 
127
127
  * https://www.npmjs.com/package/@types/node
128
128
 
package/index.js CHANGED
@@ -4,6 +4,7 @@ const fs = require("fs");
4
4
  const stream = require("stream");
5
5
  const zlib = require("zlib");
6
6
  const wawoff2 = require("wawoff2");
7
+ const tinify = require("tinify");
7
8
  const { toSfnt, toWoff } = require('woff2sfnt-sfnt2woff');
8
9
  const types_1 = require("@e-mc/types");
9
10
  const module_1 = require("@e-mc/module");
@@ -12,7 +13,6 @@ const CACHE_FONT = {};
12
13
  const CACHE_FONTFROM = {};
13
14
  const CACHE_FONTTO = {};
14
15
  const CACHE_INIT = [false, false];
15
- const CACHE_TINIFY = {};
16
16
  let SINGLETON_INSTANCE;
17
17
  let TEMP_DIR = '';
18
18
  const GZIP_ZOPFLI = (function () {
@@ -80,7 +80,6 @@ function setCacheData(index) {
80
80
  return CACHE_INIT[index] = true;
81
81
  }
82
82
  const removeFile = (pathname) => fs.unlink(pathname, () => { });
83
- const checkChunkSize = (value) => typeof value === 'number' && value > 0 && value % 1024 === 0;
84
83
  class Compress extends module_1 {
85
84
  static singleton() {
86
85
  if (!SINGLETON_INSTANCE) {
@@ -113,7 +112,7 @@ class Compress extends module_1 {
113
112
  if (zopfli_iterations && (zopfli_iterations = Math.floor(+zopfli_iterations)) > 0) {
114
113
  this.level.zopfli = zopfli_iterations;
115
114
  }
116
- if (chunk_size && ((0, types_1.isString)(chunk_size) && checkChunkSize(chunk_size = (0, types_1.formatSize)(chunk_size)) || checkChunkSize(chunk_size = +chunk_size))) {
115
+ if (chunk_size && ((0, types_1.isString)(chunk_size) && (0, types_1.alignSize)(chunk_size = (0, types_1.formatSize)(chunk_size), 1) || (0, types_1.alignSize)(chunk_size = +chunk_size, 1))) {
117
116
  this.chunkSize = chunk_size;
118
117
  }
119
118
  return this;
@@ -168,7 +167,7 @@ class Compress extends module_1 {
168
167
  else {
169
168
  gzip.level ?? (gzip.level = this.level.gz);
170
169
  }
171
- if (checkChunkSize(chunkSize)) {
170
+ if (!isNaN(chunkSize = (0, types_1.alignSize)(chunkSize, 1))) {
172
171
  gzip.chunkSize = chunkSize;
173
172
  }
174
173
  else {
@@ -195,7 +194,7 @@ class Compress extends module_1 {
195
194
  }
196
195
  catch {
197
196
  }
198
- if (checkChunkSize(chunkSize)) {
197
+ if (!isNaN(chunkSize = (0, types_1.alignSize)(chunkSize, 1))) {
199
198
  brotli.chunkSize = chunkSize;
200
199
  }
201
200
  else {
@@ -228,10 +227,12 @@ class Compress extends module_1 {
228
227
  options = output;
229
228
  output = '';
230
229
  }
230
+ else {
231
+ options || (options = {});
232
+ }
231
233
  if (!(0, types_1.isString)(output)) {
232
234
  output = typeof file === 'string' ? file : '';
233
235
  }
234
- options || (options = {});
235
236
  const { filename, startTime = process.hrtime(), timeout = 0, sessionId, broadcastId } = options;
236
237
  let format = options.format;
237
238
  if (!format) {
@@ -518,7 +519,6 @@ class Compress extends module_1 {
518
519
  else {
519
520
  this.writeTimeProcess(ext, status, startTime, { type: 8, sessionId, broadcastId });
520
521
  }
521
- resolve(result);
522
522
  if (!ctime && hash && cacheKey && TEMP_DIR) {
523
523
  const pathname = path.join(TEMP_DIR, plugin, hash);
524
524
  if (module_1.createDir(pathname)) {
@@ -530,6 +530,7 @@ class Compress extends module_1 {
530
530
  });
531
531
  }
532
532
  }
533
+ resolve(result);
533
534
  };
534
535
  if (output) {
535
536
  fs.writeFile(output, result, err => complete(err));
@@ -577,7 +578,7 @@ class Compress extends module_1 {
577
578
  let stored;
578
579
  try {
579
580
  stored = (CACHE_IMAGE[plugin] || (CACHE_IMAGE[plugin] = {}))[hash = module_1.asHash(data)];
580
- cacheKey = module_1.asHash(plugin + ':' + (apiKey ? format + apiKey : module_1.asString(options) || 'unknown'), 'md5');
581
+ cacheKey = plugin + ':' + module_1.asHash(apiKey ? format + apiKey : module_1.asString(options.options), 'md5');
581
582
  const ctime = stored?.[cacheKey];
582
583
  if (ctime) {
583
584
  success(fs.readFileSync(path.join(TEMP_DIR, plugin, hash, cacheKey)), ctime);
@@ -596,47 +597,23 @@ class Compress extends module_1 {
596
597
  }
597
598
  if (apiKey) {
598
599
  const apiProxy = proxyUrl && (typeof proxyUrl === 'function' ? proxyUrl("https://api.tinify.com") : proxyUrl) || this.module.tinify?.proxy || module_1.enabled("process.env.apply") && process.env.TINIFY_PROXY || '';
599
- const key = apiKey + apiProxy;
600
- let tinypng = CACHE_TINIFY[key];
601
- const validate = () => {
602
- tinypng = require('tinify');
603
- tinypng.key = apiKey;
604
- if (apiProxy) {
605
- tinypng.proxy = apiProxy;
606
- }
607
- tinypng.validate(err => {
608
- if (!err) {
609
- fromBuffer();
610
- CACHE_TINIFY[key] = tinypng;
611
- }
612
- else {
613
- if (err instanceof tinypng.AccountError) {
614
- delete CACHE_TINIFY[key];
600
+ tinify.key = apiKey;
601
+ tinify.proxy = apiProxy;
602
+ tinify.validate(err => {
603
+ if (!err) {
604
+ tinify.fromBuffer(data).toBuffer((error, result) => {
605
+ if (!error && result) {
606
+ success(result);
615
607
  }
616
- failed(err);
617
- }
618
- });
619
- };
620
- const fromBuffer = (cache) => {
621
- tinypng.fromBuffer(data).toBuffer((err, result) => {
622
- if (!err && result) {
623
- success(result);
624
- }
625
- else if (cache) {
626
- delete CACHE_TINIFY[key];
627
- validate();
628
- }
629
- else {
630
- failed(err || new Error("Unknown"));
631
- }
632
- });
633
- };
634
- if (!tinypng) {
635
- validate();
636
- }
637
- else {
638
- fromBuffer(true);
639
- }
608
+ else {
609
+ failed(error || new Error("Unknown"));
610
+ }
611
+ });
612
+ }
613
+ else {
614
+ failed(err);
615
+ }
616
+ });
640
617
  }
641
618
  else {
642
619
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/compress",
3
- "version": "0.9.1",
3
+ "version": "0.9.3",
4
4
  "description": "Compress constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -23,8 +23,8 @@
23
23
  "license": "BSD 3-Clause",
24
24
  "homepage": "https://github.com/anpham6/e-mc#readme",
25
25
  "dependencies": {
26
- "@e-mc/module": "0.9.1",
27
- "@e-mc/types": "0.9.1",
26
+ "@e-mc/module": "0.9.3",
27
+ "@e-mc/types": "0.9.3",
28
28
  "tinify": "^1.7.1",
29
29
  "wawoff2": "^2.0.1",
30
30
  "woff2sfnt-sfnt2woff": "^1.0.0"