@pi-r/jimp 0.3.1 → 0.3.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.
- package/LICENSE +1 -1
- package/README.md +4 -2
- package/index.js +44 -41
- package/package.json +5 -5
- package/util.js +4 -4
package/LICENSE
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright 2023
|
|
1
|
+
Copyright 2023 Lisa Mishima
|
|
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
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
2
|
const path = require("path");
|
|
4
3
|
const fs = require("fs");
|
|
5
4
|
const child_process = require("child_process");
|
|
@@ -8,7 +7,14 @@ const gifwrap = require("gifwrap");
|
|
|
8
7
|
const bmp = require("bmp-js");
|
|
9
8
|
const types_1 = require("@e-mc/types");
|
|
10
9
|
const Image = require('@e-mc/image');
|
|
11
|
-
|
|
10
|
+
let WEBPMUX = null, WEBPMUX_INIT = false;
|
|
11
|
+
try {
|
|
12
|
+
WEBPMUX = require('node-webpmux');
|
|
13
|
+
new WEBPMUX.Image().initLib().then(() => WEBPMUX_INIT = true);
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
}
|
|
17
|
+
const util_1 = require("@pi-r/jimp/util");
|
|
12
18
|
const CACHE_TRANSFORM = {};
|
|
13
19
|
let CACHE_INIT = false;
|
|
14
20
|
let TEMP_DIR = '';
|
|
@@ -113,9 +119,9 @@ async function transformCommand(localFile, handler, command, outputType, outputA
|
|
|
113
119
|
return handler.rotate();
|
|
114
120
|
default:
|
|
115
121
|
return handler.rotate(localFile, (err, result) => {
|
|
116
|
-
if (!err) {
|
|
122
|
+
if (!err && handler.host?.moduleName === 'filemanager') {
|
|
117
123
|
try {
|
|
118
|
-
handler.host
|
|
124
|
+
handler.host.add(result, parent);
|
|
119
125
|
}
|
|
120
126
|
catch {
|
|
121
127
|
}
|
|
@@ -215,10 +221,10 @@ function getCacheData() {
|
|
|
215
221
|
}
|
|
216
222
|
function formatMessage(value, startTime, failed, cTimeMs) {
|
|
217
223
|
if (cTimeMs) {
|
|
218
|
-
this.formatMessage(2048
|
|
224
|
+
this.formatMessage(2048, "jimp", [value, 'cache'], new Date(cTimeMs).toLocaleString(), { ...Image.LOG_STYLE_NOTICE, hintBold: true });
|
|
219
225
|
}
|
|
220
226
|
else if (startTime) {
|
|
221
|
-
this.writeTimeProcess("jimp"
|
|
227
|
+
this.writeTimeProcess("jimp", value, startTime, { type: 2048, failed });
|
|
222
228
|
}
|
|
223
229
|
}
|
|
224
230
|
function getTempPath(ext) {
|
|
@@ -258,7 +264,7 @@ class JimpHandler {
|
|
|
258
264
|
const output = leading + value + ext;
|
|
259
265
|
tasks.push(img.writeAsync(output)
|
|
260
266
|
.then(() => this.finalize(output, callback))
|
|
261
|
-
.catch(err => this.instance.writeFail(["Unable to rotate image"
|
|
267
|
+
.catch(err => this.instance.writeFail(["Unable to rotate image", "jimp"], err, 2048)));
|
|
262
268
|
}
|
|
263
269
|
}
|
|
264
270
|
if (deg) {
|
|
@@ -283,7 +289,7 @@ class JimpHandler {
|
|
|
283
289
|
try {
|
|
284
290
|
const alias = getMethodName(name);
|
|
285
291
|
if (!alias) {
|
|
286
|
-
throw (0, types_1.errorValue)("Invalid method name"
|
|
292
|
+
throw (0, types_1.errorValue)("Invalid method name", name);
|
|
287
293
|
}
|
|
288
294
|
if (alias === 'composite') {
|
|
289
295
|
const [src, x, y, opts] = args;
|
|
@@ -291,7 +297,7 @@ class JimpHandler {
|
|
|
291
297
|
handler.composite(await jimp.read(src), x, y, opts);
|
|
292
298
|
}
|
|
293
299
|
else {
|
|
294
|
-
throw (0, types_1.errorValue)("Invalid parameters"
|
|
300
|
+
throw (0, types_1.errorValue)("Invalid parameters", alias);
|
|
295
301
|
}
|
|
296
302
|
}
|
|
297
303
|
else {
|
|
@@ -299,7 +305,7 @@ class JimpHandler {
|
|
|
299
305
|
}
|
|
300
306
|
}
|
|
301
307
|
catch (err) {
|
|
302
|
-
this.instance.writeFail(["Unknown"
|
|
308
|
+
this.instance.writeFail(["Unknown", "jimp" + ': ' + name], err, 2048);
|
|
303
309
|
}
|
|
304
310
|
}
|
|
305
311
|
}
|
|
@@ -430,7 +436,7 @@ class JimpHandler {
|
|
|
430
436
|
try {
|
|
431
437
|
child_process.execFile((0, util_1.getWebP_bin)('cwebp', webp.path), args, { shell: true, signal: this.instance.signal, ...execOptions(settings) }, err => {
|
|
432
438
|
if (err) {
|
|
433
|
-
this.instance.writeFail(["Unable to convert file"
|
|
439
|
+
this.instance.writeFail(["Unable to convert file", path.basename(filename)], err, 2048);
|
|
434
440
|
}
|
|
435
441
|
else if (webp !== output) {
|
|
436
442
|
const tempFile = output;
|
|
@@ -445,7 +451,7 @@ class JimpHandler {
|
|
|
445
451
|
});
|
|
446
452
|
}
|
|
447
453
|
catch (err) {
|
|
448
|
-
this.instance.checkPackage(err, 'cwebp-bin@6.1.2', "Unknown"
|
|
454
|
+
this.instance.checkPackage(err, 'cwebp-bin@6.1.2', "Unknown", { type: 2048, passThrough: !!callback });
|
|
449
455
|
if (callback) {
|
|
450
456
|
callback(err, '');
|
|
451
457
|
}
|
|
@@ -476,7 +482,7 @@ class JimpHandler {
|
|
|
476
482
|
resolve(fs.readFileSync(result));
|
|
477
483
|
}
|
|
478
484
|
catch (err) {
|
|
479
|
-
this.instance.writeFail(["Unable to read file"
|
|
485
|
+
this.instance.writeFail(["Unable to read file", path.basename(result)], err, 32);
|
|
480
486
|
resolve(null);
|
|
481
487
|
}
|
|
482
488
|
queueMicrotask(() => fs.unlink(result, err => !err && this.instance.emit('file:delete', result)));
|
|
@@ -542,7 +548,7 @@ class JimpHandler {
|
|
|
542
548
|
callback(err, '');
|
|
543
549
|
}
|
|
544
550
|
else {
|
|
545
|
-
this.instance.writeFail(["Unable to write file"
|
|
551
|
+
this.instance.writeFail(["Unable to write file", path.basename(output)], err, 2048);
|
|
546
552
|
}
|
|
547
553
|
});
|
|
548
554
|
}
|
|
@@ -556,11 +562,10 @@ class JimpHandler {
|
|
|
556
562
|
return this.instance.rotateData?.values.length || 0;
|
|
557
563
|
}
|
|
558
564
|
}
|
|
559
|
-
// @ts-ignore
|
|
560
565
|
class Jimp extends Image {
|
|
561
566
|
constructor() {
|
|
562
567
|
super(...arguments);
|
|
563
|
-
this._moduleName = "jimp"
|
|
568
|
+
this._moduleName = "jimp";
|
|
564
569
|
this._threadable = true;
|
|
565
570
|
}
|
|
566
571
|
static async transform(file, command, options = {}) {
|
|
@@ -613,7 +618,7 @@ class Jimp extends Image {
|
|
|
613
618
|
return Promise.resolve(buffer);
|
|
614
619
|
}
|
|
615
620
|
}
|
|
616
|
-
instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp"
|
|
621
|
+
instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp", ["Transforming image...", filename], command);
|
|
617
622
|
Image.initCpuUsage(instance);
|
|
618
623
|
return performCommand(null, instance, file, command, outputType, finalAs)
|
|
619
624
|
.then(async (handler) => {
|
|
@@ -647,26 +652,26 @@ class Jimp extends Image {
|
|
|
647
652
|
const localUri = host.getLocalUri(data);
|
|
648
653
|
const mimeType = host.getMimeType(data);
|
|
649
654
|
if (!localUri || !util_1.MIME_INPUT.has(mimeType)) {
|
|
650
|
-
reject((0, types_1.errorValue)("Unknown"
|
|
655
|
+
reject((0, types_1.errorValue)("Unknown", !localUri ? 'URI' : 'MIME'));
|
|
651
656
|
return;
|
|
652
657
|
}
|
|
653
658
|
if (!this.canRead(localUri, { ownPermissionOnly: true })) {
|
|
654
|
-
reject((0, types_1.errorValue)("Not permitted to read file"
|
|
659
|
+
reject((0, types_1.errorValue)("Not permitted to read file", localUri));
|
|
655
660
|
return;
|
|
656
661
|
}
|
|
657
662
|
const [outputType, saveAs, finalAs] = (0, util_1.parseFormat)(command = command.trim(), mimeType, true);
|
|
658
663
|
if (!outputType) {
|
|
659
|
-
reject((0, types_1.errorValue)("Invalid format"
|
|
664
|
+
reject((0, types_1.errorValue)("Invalid format", /^\w+/.exec(command)?.[0] || "Unknown"));
|
|
660
665
|
return;
|
|
661
666
|
}
|
|
662
667
|
const replace = command.indexOf('@') !== -1;
|
|
663
668
|
const output = host.addCopy(data.getObject({ command, outputType }), saveAs, replace);
|
|
664
669
|
if (!output) {
|
|
665
|
-
reject((0, types_1.errorValue)("Not able to copy file"
|
|
670
|
+
reject((0, types_1.errorValue)("Not able to copy file", outputType));
|
|
666
671
|
return;
|
|
667
672
|
}
|
|
668
673
|
if (!this.canWrite(output, { ownPermissionOnly: true })) {
|
|
669
|
-
reject((0, types_1.errorValue)("Not permitted to write file"
|
|
674
|
+
reject((0, types_1.errorValue)("Not permitted to write file", output));
|
|
670
675
|
return;
|
|
671
676
|
}
|
|
672
677
|
const startTime = process.hrtime();
|
|
@@ -727,7 +732,7 @@ class Jimp extends Image {
|
|
|
727
732
|
removeFile(bmpFile);
|
|
728
733
|
}
|
|
729
734
|
const errorResponse = (err) => {
|
|
730
|
-
this.writeFail(["Unable to finalize image"
|
|
735
|
+
this.writeFail(["Unable to finalize image", "jimp"], err, { type: 2048, startTime });
|
|
731
736
|
resolve();
|
|
732
737
|
};
|
|
733
738
|
if (outputType === jimp.MIME_GIF) {
|
|
@@ -749,18 +754,18 @@ class Jimp extends Image {
|
|
|
749
754
|
finalize(result);
|
|
750
755
|
}
|
|
751
756
|
else {
|
|
752
|
-
errorResponse(err || new Error("Unknown"
|
|
757
|
+
errorResponse(err || new Error("Unknown"));
|
|
753
758
|
}
|
|
754
759
|
});
|
|
755
760
|
}
|
|
756
761
|
})
|
|
757
762
|
.catch(err => {
|
|
758
|
-
this.writeFail(["Unable to read buffer"
|
|
763
|
+
this.writeFail(["Unable to read buffer", path.basename(localUri)], err, { type: 2048, startTime });
|
|
759
764
|
resolve();
|
|
760
765
|
});
|
|
761
766
|
};
|
|
762
767
|
const errorResponse = (err) => {
|
|
763
|
-
this.writeFail(["Unable to convert file"
|
|
768
|
+
this.writeFail(["Unable to convert file", path.basename(localUri)], err, { type: 2048, startTime });
|
|
764
769
|
resolve();
|
|
765
770
|
};
|
|
766
771
|
const rotateAnim = (cmd) => {
|
|
@@ -770,9 +775,9 @@ class Jimp extends Image {
|
|
|
770
775
|
}
|
|
771
776
|
};
|
|
772
777
|
const hasTransform = (cmd) => !!(cmd.rotate || cmd.opacity >= 0 && cmd.opacity < 1 || cmd.resize || cmd.crop || cmd.method);
|
|
773
|
-
const startMessage = () => host.formatMessage(2048
|
|
778
|
+
const startMessage = () => host.formatMessage(2048, "jimp", ["Transforming image...", path.basename(localUri)], command);
|
|
774
779
|
if (mimeType === jimp.MIME_GIF) {
|
|
775
|
-
if (outputType === jimp.MIME_GIF || outputType === "image/webp"
|
|
780
|
+
if (outputType === jimp.MIME_GIF || outputType === "image/webp") {
|
|
776
781
|
const cmd = this.parseCommand(command);
|
|
777
782
|
const transformWebP = (target, modified) => {
|
|
778
783
|
var _a;
|
|
@@ -830,7 +835,7 @@ class Jimp extends Image {
|
|
|
830
835
|
});
|
|
831
836
|
}
|
|
832
837
|
catch (err) {
|
|
833
|
-
if (this.checkPackage(err, 'gif2webp-bin@3', 2048
|
|
838
|
+
if (this.checkPackage(err, 'gif2webp-bin@3', 2048)) {
|
|
834
839
|
resolve();
|
|
835
840
|
}
|
|
836
841
|
else {
|
|
@@ -883,18 +888,16 @@ class Jimp extends Image {
|
|
|
883
888
|
transformBuffer();
|
|
884
889
|
}
|
|
885
890
|
}
|
|
886
|
-
else if (mimeType === "image/webp"
|
|
891
|
+
else if (mimeType === "image/webp") {
|
|
887
892
|
const tryWebpMux = async () => {
|
|
888
|
-
let loaded;
|
|
889
893
|
try {
|
|
890
|
-
const webp = new (require('node-webpmux').Image
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
+
const webp = new (WEBPMUX || (WEBPMUX = require('node-webpmux'))).Image();
|
|
895
|
+
if (!WEBPMUX_INIT) {
|
|
896
|
+
await webp.initLib();
|
|
897
|
+
WEBPMUX_INIT = true;
|
|
894
898
|
}
|
|
895
|
-
await webp.initLib();
|
|
896
899
|
await webp.load(host.getBuffer(file));
|
|
897
|
-
if (!(webp.hasAnim && (outputType === "image/webp"
|
|
900
|
+
if (!(webp.hasAnim && (outputType === "image/webp" || outputType === jimp.MIME_GIF))) {
|
|
898
901
|
transformBuffer(bmp.encode({ width: webp.width, height: webp.height, data: Image.toABGR(await (!webp.hasAnim ? webp.getImageData() : webp.getFrameData(0))) }).data);
|
|
899
902
|
return true;
|
|
900
903
|
}
|
|
@@ -956,10 +959,10 @@ class Jimp extends Image {
|
|
|
956
959
|
return true;
|
|
957
960
|
}
|
|
958
961
|
catch (err) {
|
|
959
|
-
if (
|
|
960
|
-
this.writeFail(["Unknown"
|
|
962
|
+
if (WEBPMUX_INIT) {
|
|
963
|
+
this.writeFail(["Unknown", 'node-webpmux'], err, { type: 2048, startTime });
|
|
961
964
|
}
|
|
962
|
-
else if (this.checkPackage(err, 'node-webpmux@3', 2048
|
|
965
|
+
else if (this.checkPackage(err, 'node-webpmux@3', 2048)) {
|
|
963
966
|
resolve();
|
|
964
967
|
return true;
|
|
965
968
|
}
|
|
@@ -982,7 +985,7 @@ class Jimp extends Image {
|
|
|
982
985
|
}
|
|
983
986
|
catch (err) {
|
|
984
987
|
if (!await tryWebpMux()) {
|
|
985
|
-
if (this.checkPackage(err, 'dwebp-bin@1', 2048
|
|
988
|
+
if (this.checkPackage(err, 'dwebp-bin@1', 2048)) {
|
|
986
989
|
resolve();
|
|
987
990
|
}
|
|
988
991
|
else {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/jimp",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
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.6.
|
|
24
|
-
"@e-mc/types": "^0.6.
|
|
23
|
+
"@e-mc/image": "^0.6.2",
|
|
24
|
+
"@e-mc/types": "^0.6.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 {
|