@pi-r/tinify 0.9.3 → 0.10.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 +47 -19
- package/package.json +3 -3
package/index.js
CHANGED
|
@@ -2,30 +2,58 @@
|
|
|
2
2
|
const tinify = require("tinify");
|
|
3
3
|
const Compress = require("@e-mc/compress");
|
|
4
4
|
const types_1 = require("@e-mc/types");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
const CACHE_APIKEY = {};
|
|
6
|
+
const withinRange = (value = 0) => value > Date.now();
|
|
7
|
+
function compress(options = {}) {
|
|
8
|
+
let apiKey = options.apiKey, proxy = options.proxy, expires = options.expires;
|
|
9
|
+
if (!apiKey && (0, types_1.isPlainObject)(this?.module)) {
|
|
10
|
+
const settings = this.module.tinify;
|
|
11
|
+
if (settings) {
|
|
12
|
+
apiKey ||= settings.api_key;
|
|
13
|
+
proxy ??= settings.proxy;
|
|
14
|
+
expires ??= settings.expires;
|
|
15
|
+
}
|
|
16
|
+
apiKey ||= this.module.settings?.tinify_api_key;
|
|
17
|
+
}
|
|
18
|
+
if (Compress.enabled("process.env.apply")) {
|
|
19
|
+
apiKey ||= process.env.TINIFY_KEY;
|
|
20
|
+
proxy ??= process.env.TINIFY_PROXY;
|
|
21
|
+
}
|
|
9
22
|
return async (data) => {
|
|
10
23
|
return new Promise((resolve, reject) => {
|
|
11
24
|
if (apiKey) {
|
|
12
25
|
tinify.key = apiKey;
|
|
13
|
-
tinify.proxy = proxy;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
26
|
+
tinify.proxy = proxy ||= '';
|
|
27
|
+
const cacheKey = apiKey + proxy;
|
|
28
|
+
const sendData = () => {
|
|
29
|
+
tinify.fromBuffer(data).toBuffer((err, result) => {
|
|
30
|
+
if (result) {
|
|
31
|
+
resolve(result);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
reject(err || new Error("Unknown"));
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
if (withinRange(CACHE_APIKEY[cacheKey])) {
|
|
39
|
+
sendData();
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
tinify.validate(err => {
|
|
43
|
+
if (!err) {
|
|
44
|
+
if (expires) {
|
|
45
|
+
const offset = (0, types_1.parseExpires)(expires);
|
|
46
|
+
if (offset > 0) {
|
|
47
|
+
CACHE_APIKEY[cacheKey] = Date.now() + offset;
|
|
48
|
+
}
|
|
19
49
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
});
|
|
50
|
+
sendData();
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
reject(err);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
29
57
|
}
|
|
30
58
|
else {
|
|
31
59
|
reject((0, types_1.errorMessage)('tinify', "Missing API key"));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/tinify",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "TinyPNG compress function for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"publishConfig": {
|
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@e-mc/compress": "^0.
|
|
23
|
-
"@e-mc/types": "^0.
|
|
22
|
+
"@e-mc/compress": "^0.12.0",
|
|
23
|
+
"@e-mc/types": "^0.12.0",
|
|
24
24
|
"tinify": "^1.8.0"
|
|
25
25
|
}
|
|
26
26
|
}
|