@pi-r/jimp 0.5.0 → 0.5.2
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 +1 -1
- package/README.md +4 -2
- package/index.js +50 -39
- package/package.json +5 -5
- package/util.js +4 -4
package/LICENSE
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright 2023
|
|
1
|
+
Copyright 2023 Wit Studio
|
|
2
2
|
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
4
|
|
package/README.md
CHANGED
package/index.js
CHANGED
|
@@ -8,7 +8,14 @@ const gifwrap = require("gifwrap");
|
|
|
8
8
|
const bmp = require("bmp-js");
|
|
9
9
|
const types_1 = require("@e-mc/types");
|
|
10
10
|
const Image = require('@e-mc/image');
|
|
11
|
-
|
|
11
|
+
let WEBPMUX = null, WEBPMUX_INIT = false;
|
|
12
|
+
try {
|
|
13
|
+
WEBPMUX = require('node-webpmux');
|
|
14
|
+
new WEBPMUX.Image().initLib().then(() => WEBPMUX_INIT = true);
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
}
|
|
18
|
+
const util_1 = require("@pi-r/jimp/util");
|
|
12
19
|
const CACHE_TRANSFORM = {};
|
|
13
20
|
let CACHE_INIT = false;
|
|
14
21
|
let TEMP_DIR = '';
|
|
@@ -113,9 +120,9 @@ async function transformCommand(localFile, handler, command, outputType, outputA
|
|
|
113
120
|
return handler.rotate();
|
|
114
121
|
default:
|
|
115
122
|
return handler.rotate(localFile, (err, result) => {
|
|
116
|
-
if (!err) {
|
|
123
|
+
if (!err && handler.host?.moduleName === 'filemanager') {
|
|
117
124
|
try {
|
|
118
|
-
handler.host
|
|
125
|
+
handler.host.add(result, parent);
|
|
119
126
|
}
|
|
120
127
|
catch {
|
|
121
128
|
}
|
|
@@ -215,10 +222,10 @@ function getCacheData() {
|
|
|
215
222
|
}
|
|
216
223
|
function formatMessage(value, startTime, failed, cTimeMs) {
|
|
217
224
|
if (cTimeMs) {
|
|
218
|
-
this.formatMessage(2048
|
|
225
|
+
this.formatMessage(2048, "jimp", [value, 'cache'], new Date(cTimeMs).toLocaleString(), { ...Image.LOG_STYLE_NOTICE, hintBold: true });
|
|
219
226
|
}
|
|
220
227
|
else if (startTime) {
|
|
221
|
-
this.writeTimeProcess("jimp"
|
|
228
|
+
this.writeTimeProcess("jimp", value, startTime, { type: 2048, failed });
|
|
222
229
|
}
|
|
223
230
|
}
|
|
224
231
|
function getTempPath(ext) {
|
|
@@ -258,7 +265,7 @@ class JimpHandler {
|
|
|
258
265
|
const output = leading + value + ext;
|
|
259
266
|
tasks.push(img.writeAsync(output)
|
|
260
267
|
.then(() => this.finalize(output, callback))
|
|
261
|
-
.catch(err => this.instance.writeFail(["Unable to rotate image"
|
|
268
|
+
.catch(err => this.instance.writeFail(["Unable to rotate image", "jimp"], err, 2048)));
|
|
262
269
|
}
|
|
263
270
|
}
|
|
264
271
|
if (deg) {
|
|
@@ -281,7 +288,7 @@ class JimpHandler {
|
|
|
281
288
|
try {
|
|
282
289
|
const alias = getMethodName(name);
|
|
283
290
|
if (!alias) {
|
|
284
|
-
throw (0, types_1.errorValue)("Invalid method name"
|
|
291
|
+
throw (0, types_1.errorValue)("Invalid method name", name);
|
|
285
292
|
}
|
|
286
293
|
if (alias === 'composite') {
|
|
287
294
|
const [src, x, y, opts] = args;
|
|
@@ -289,7 +296,7 @@ class JimpHandler {
|
|
|
289
296
|
handler.composite(await jimp.read(src), x, y, opts);
|
|
290
297
|
}
|
|
291
298
|
else {
|
|
292
|
-
throw (0, types_1.errorValue)("Invalid parameters"
|
|
299
|
+
throw (0, types_1.errorValue)("Invalid parameters", alias);
|
|
293
300
|
}
|
|
294
301
|
}
|
|
295
302
|
else {
|
|
@@ -297,7 +304,7 @@ class JimpHandler {
|
|
|
297
304
|
}
|
|
298
305
|
}
|
|
299
306
|
catch (err) {
|
|
300
|
-
this.instance.writeFail(["Unknown"
|
|
307
|
+
this.instance.writeFail(["Unknown", "jimp" + ': ' + name], err, 2048);
|
|
301
308
|
}
|
|
302
309
|
}
|
|
303
310
|
}
|
|
@@ -428,7 +435,7 @@ class JimpHandler {
|
|
|
428
435
|
try {
|
|
429
436
|
child_process.execFile((0, util_1.getWebP_bin)('cwebp', webp.path), args, { shell: true, signal: this.instance.signal, ...execOptions(settings) }, err => {
|
|
430
437
|
if (err) {
|
|
431
|
-
this.instance.writeFail(["Unable to convert file"
|
|
438
|
+
this.instance.writeFail(["Unable to convert file", path.basename(filename)], err, 2048);
|
|
432
439
|
}
|
|
433
440
|
else if (webp !== output) {
|
|
434
441
|
const tempFile = output;
|
|
@@ -443,7 +450,7 @@ class JimpHandler {
|
|
|
443
450
|
});
|
|
444
451
|
}
|
|
445
452
|
catch (err) {
|
|
446
|
-
this.instance.checkPackage(err, 'cwebp-bin@6.1.2', "Unknown"
|
|
453
|
+
this.instance.checkPackage(err, 'cwebp-bin@6.1.2', "Unknown", { type: 2048, passThrough: !!callback });
|
|
447
454
|
if (callback) {
|
|
448
455
|
callback(err, '');
|
|
449
456
|
}
|
|
@@ -474,7 +481,7 @@ class JimpHandler {
|
|
|
474
481
|
resolve(fs.readFileSync(result));
|
|
475
482
|
}
|
|
476
483
|
catch (err) {
|
|
477
|
-
this.instance.writeFail(["Unable to read file"
|
|
484
|
+
this.instance.writeFail(["Unable to read file", path.basename(result)], err, 32);
|
|
478
485
|
resolve(null);
|
|
479
486
|
}
|
|
480
487
|
queueMicrotask(() => fs.unlink(result, err => !err && this.instance.emit('file:delete', result)));
|
|
@@ -540,7 +547,7 @@ class JimpHandler {
|
|
|
540
547
|
callback(err, '');
|
|
541
548
|
}
|
|
542
549
|
else {
|
|
543
|
-
this.instance.writeFail(["Unable to write file"
|
|
550
|
+
this.instance.writeFail(["Unable to write file", path.basename(output)], err, 2048);
|
|
544
551
|
}
|
|
545
552
|
});
|
|
546
553
|
}
|
|
@@ -554,11 +561,10 @@ class JimpHandler {
|
|
|
554
561
|
return this.instance.rotateData?.values.length || 0;
|
|
555
562
|
}
|
|
556
563
|
}
|
|
557
|
-
// @ts-ignore
|
|
558
564
|
class Jimp extends Image {
|
|
559
565
|
constructor() {
|
|
560
566
|
super(...arguments);
|
|
561
|
-
this._moduleName = "jimp"
|
|
567
|
+
this._moduleName = "jimp";
|
|
562
568
|
this._threadable = true;
|
|
563
569
|
}
|
|
564
570
|
static async transform(file, command, options = {}) {
|
|
@@ -611,7 +617,7 @@ class Jimp extends Image {
|
|
|
611
617
|
return Promise.resolve(buffer);
|
|
612
618
|
}
|
|
613
619
|
}
|
|
614
|
-
instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp"
|
|
620
|
+
instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp", ["Transforming image...", filename], command);
|
|
615
621
|
Image.initCpuUsage(instance);
|
|
616
622
|
return performCommand(null, instance, file, command, outputType, finalAs)
|
|
617
623
|
.then(async (handler) => {
|
|
@@ -645,26 +651,26 @@ class Jimp extends Image {
|
|
|
645
651
|
const localUri = host.getLocalUri(data);
|
|
646
652
|
const mimeType = host.getMimeType(data);
|
|
647
653
|
if (!localUri || !util_1.MIME_INPUT.has(mimeType)) {
|
|
648
|
-
reject((0, types_1.errorValue)("Unknown"
|
|
654
|
+
reject((0, types_1.errorValue)("Unknown", !localUri ? 'URI' : 'MIME'));
|
|
649
655
|
return;
|
|
650
656
|
}
|
|
651
657
|
if (!this.canRead(localUri, { ownPermissionOnly: true })) {
|
|
652
|
-
reject((0, types_1.errorValue)("Not permitted to read file"
|
|
658
|
+
reject((0, types_1.errorValue)("Not permitted to read file", localUri));
|
|
653
659
|
return;
|
|
654
660
|
}
|
|
655
661
|
const [outputType, saveAs, finalAs] = (0, util_1.parseFormat)(command = command.trim(), mimeType, true);
|
|
656
662
|
if (!outputType) {
|
|
657
|
-
reject((0, types_1.errorValue)("Invalid format"
|
|
663
|
+
reject((0, types_1.errorValue)("Invalid format", /^\w+/.exec(command)?.[0] || "Unknown"));
|
|
658
664
|
return;
|
|
659
665
|
}
|
|
660
666
|
const replace = command.indexOf('@') !== -1;
|
|
661
667
|
const output = host.addCopy(data.getObject({ command, outputType }), saveAs, replace);
|
|
662
668
|
if (!output) {
|
|
663
|
-
reject((0, types_1.errorValue)("Not able to copy file"
|
|
669
|
+
reject((0, types_1.errorValue)("Not able to copy file", outputType));
|
|
664
670
|
return;
|
|
665
671
|
}
|
|
666
672
|
if (!this.canWrite(output, { ownPermissionOnly: true })) {
|
|
667
|
-
reject((0, types_1.errorValue)("Not permitted to write file"
|
|
673
|
+
reject((0, types_1.errorValue)("Not permitted to write file", output));
|
|
668
674
|
return;
|
|
669
675
|
}
|
|
670
676
|
const startTime = process.hrtime();
|
|
@@ -725,7 +731,7 @@ class Jimp extends Image {
|
|
|
725
731
|
removeFile(bmpFile);
|
|
726
732
|
}
|
|
727
733
|
const errorResponse = (err) => {
|
|
728
|
-
this.writeFail(["Unable to finalize image"
|
|
734
|
+
this.writeFail(["Unable to finalize image", "jimp"], err, { type: 2048, startTime });
|
|
729
735
|
resolve();
|
|
730
736
|
};
|
|
731
737
|
if (outputType === jimp.MIME_GIF) {
|
|
@@ -747,18 +753,18 @@ class Jimp extends Image {
|
|
|
747
753
|
finalize(result);
|
|
748
754
|
}
|
|
749
755
|
else {
|
|
750
|
-
errorResponse(err || new Error("Unknown"
|
|
756
|
+
errorResponse(err || new Error("Unknown"));
|
|
751
757
|
}
|
|
752
758
|
});
|
|
753
759
|
}
|
|
754
760
|
})
|
|
755
761
|
.catch(err => {
|
|
756
|
-
this.writeFail(["Unable to read buffer"
|
|
762
|
+
this.writeFail(["Unable to read buffer", path.basename(localUri)], err, { type: 2048, startTime });
|
|
757
763
|
resolve();
|
|
758
764
|
});
|
|
759
765
|
};
|
|
760
766
|
const errorResponse = (err) => {
|
|
761
|
-
this.writeFail(["Unable to convert file"
|
|
767
|
+
this.writeFail(["Unable to convert file", path.basename(localUri)], err, { type: 2048, startTime });
|
|
762
768
|
resolve();
|
|
763
769
|
};
|
|
764
770
|
const rotateAnim = (cmd) => {
|
|
@@ -768,9 +774,9 @@ class Jimp extends Image {
|
|
|
768
774
|
}
|
|
769
775
|
};
|
|
770
776
|
const hasTransform = (cmd) => !!(cmd.rotate || cmd.opacity >= 0 && cmd.opacity < 1 || cmd.resize || cmd.crop || cmd.method);
|
|
771
|
-
const startMessage = () => host.formatMessage(2048
|
|
777
|
+
const startMessage = () => host.formatMessage(2048, "jimp", ["Transforming image...", path.basename(localUri)], command);
|
|
772
778
|
if (mimeType === jimp.MIME_GIF) {
|
|
773
|
-
if (outputType === jimp.MIME_GIF || outputType === "image/webp"
|
|
779
|
+
if (outputType === jimp.MIME_GIF || outputType === "image/webp") {
|
|
774
780
|
const cmd = this.parseCommand(command);
|
|
775
781
|
const transformWebP = (target, modified) => {
|
|
776
782
|
var _a;
|
|
@@ -828,7 +834,7 @@ class Jimp extends Image {
|
|
|
828
834
|
});
|
|
829
835
|
}
|
|
830
836
|
catch (err) {
|
|
831
|
-
if (this.checkPackage(err, 'gif2webp-bin@3', 2048
|
|
837
|
+
if (this.checkPackage(err, 'gif2webp-bin@3', 2048)) {
|
|
832
838
|
resolve();
|
|
833
839
|
}
|
|
834
840
|
else {
|
|
@@ -881,15 +887,16 @@ class Jimp extends Image {
|
|
|
881
887
|
transformBuffer();
|
|
882
888
|
}
|
|
883
889
|
}
|
|
884
|
-
else if (mimeType === "image/webp"
|
|
890
|
+
else if (mimeType === "image/webp") {
|
|
885
891
|
const tryWebpMux = async () => {
|
|
886
|
-
let loaded;
|
|
887
892
|
try {
|
|
888
|
-
const webp = new (require('node-webpmux').Image
|
|
889
|
-
|
|
890
|
-
|
|
893
|
+
const webp = new (WEBPMUX || (WEBPMUX = require('node-webpmux'))).Image();
|
|
894
|
+
if (!WEBPMUX_INIT) {
|
|
895
|
+
await webp.initLib();
|
|
896
|
+
WEBPMUX_INIT = true;
|
|
897
|
+
}
|
|
891
898
|
await webp.load(host.getBuffer(file));
|
|
892
|
-
if (!(webp.hasAnim && (outputType === "image/webp"
|
|
899
|
+
if (!(webp.hasAnim && (outputType === "image/webp" || outputType === jimp.MIME_GIF))) {
|
|
893
900
|
transformBuffer(bmp.encode({ width: webp.width, height: webp.height, data: Image.toABGR(await (!webp.hasAnim ? webp.getImageData() : webp.getFrameData(0))) }).data);
|
|
894
901
|
return true;
|
|
895
902
|
}
|
|
@@ -951,10 +958,10 @@ class Jimp extends Image {
|
|
|
951
958
|
return true;
|
|
952
959
|
}
|
|
953
960
|
catch (err) {
|
|
954
|
-
if (
|
|
955
|
-
this.writeFail(["Unknown"
|
|
961
|
+
if (WEBPMUX_INIT) {
|
|
962
|
+
this.writeFail(["Unknown", 'node-webpmux'], err, { type: 2048, startTime });
|
|
956
963
|
}
|
|
957
|
-
else if (this.checkPackage(err, 'node-webpmux@3', 2048
|
|
964
|
+
else if (this.checkPackage(err, 'node-webpmux@3', 2048)) {
|
|
958
965
|
resolve();
|
|
959
966
|
return true;
|
|
960
967
|
}
|
|
@@ -977,7 +984,7 @@ class Jimp extends Image {
|
|
|
977
984
|
}
|
|
978
985
|
catch (err) {
|
|
979
986
|
if (!await tryWebpMux()) {
|
|
980
|
-
if (this.checkPackage(err, 'dwebp-bin@1', 2048
|
|
987
|
+
if (this.checkPackage(err, 'dwebp-bin@1', 2048)) {
|
|
981
988
|
resolve();
|
|
982
989
|
}
|
|
983
990
|
else {
|
|
@@ -996,5 +1003,9 @@ class Jimp extends Image {
|
|
|
996
1003
|
return (_a = this.module).settings || (_a.settings = {});
|
|
997
1004
|
}
|
|
998
1005
|
}
|
|
1006
|
+
exports.default = Jimp;
|
|
999
1007
|
|
|
1000
|
-
|
|
1008
|
+
if (exports.default) {
|
|
1009
|
+
module.exports = exports.default;
|
|
1010
|
+
module.exports.default = exports.default;
|
|
1011
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/jimp",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "Jimp image constructor for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"publishConfig": {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "https://github.com/anpham6/pi-r.git",
|
|
11
|
+
"url": "git+https://github.com/anpham6/pi-r.git",
|
|
12
12
|
"directory": "src/module/jimp"
|
|
13
13
|
},
|
|
14
14
|
"keywords": [
|
|
@@ -20,10 +20,10 @@
|
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/image": "^0.7.
|
|
24
|
-
"@e-mc/types": "^0.7.
|
|
23
|
+
"@e-mc/image": "^0.7.2",
|
|
24
|
+
"@e-mc/types": "^0.7.2",
|
|
25
25
|
"bmp-js": "^0.1.0",
|
|
26
26
|
"gifwrap": "^0.10.1",
|
|
27
|
-
"jimp": "^0.22.
|
|
27
|
+
"jimp": "^0.22.12"
|
|
28
28
|
}
|
|
29
29
|
}
|
package/util.js
CHANGED
|
@@ -6,8 +6,8 @@ const fs = require("fs");
|
|
|
6
6
|
const jimp = require("jimp");
|
|
7
7
|
const types_1 = require("@e-mc/types");
|
|
8
8
|
const Image = require("@e-mc/image");
|
|
9
|
-
exports.MIME_INPUT = new Set([jimp.MIME_PNG, jimp.MIME_JPEG, jimp.MIME_BMP, jimp.MIME_GIF, jimp.MIME_TIFF, "image/webp"
|
|
10
|
-
exports.MIME_OUTPUT = new Set([jimp.MIME_PNG, jimp.MIME_JPEG, jimp.MIME_BMP, jimp.MIME_GIF, "image/webp"
|
|
9
|
+
exports.MIME_INPUT = new Set([jimp.MIME_PNG, jimp.MIME_JPEG, jimp.MIME_BMP, jimp.MIME_GIF, jimp.MIME_TIFF, "image/webp"]);
|
|
10
|
+
exports.MIME_OUTPUT = new Set([jimp.MIME_PNG, jimp.MIME_JPEG, jimp.MIME_BMP, jimp.MIME_GIF, "image/webp"]);
|
|
11
11
|
function parseFormat(command, mimeType, gif) {
|
|
12
12
|
command = command.toLowerCase();
|
|
13
13
|
for (let mime of exports.MIME_OUTPUT) {
|
|
@@ -20,10 +20,10 @@ function parseFormat(command, mimeType, gif) {
|
|
|
20
20
|
saveAs = 'jpg';
|
|
21
21
|
break;
|
|
22
22
|
case 'webp':
|
|
23
|
-
if (mimeType === "image/webp"
|
|
23
|
+
if (mimeType === "image/webp") {
|
|
24
24
|
try {
|
|
25
25
|
require('node-webpmux');
|
|
26
|
-
mime = "image/webp"
|
|
26
|
+
mime = "image/webp";
|
|
27
27
|
break;
|
|
28
28
|
}
|
|
29
29
|
catch {
|