@pi-r/jimp 0.2.2 → 0.2.9
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 +43 -40
- package/package.json +5 -5
- package/util.js +4 -4
package/LICENSE
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
Copyright
|
|
1
|
+
Copyright 2020-24 Code Geass
|
|
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 = '';
|
|
@@ -86,7 +92,7 @@ async function transformCommand(localFile, handler, command, outputType, outputA
|
|
|
86
92
|
return handler.rotate();
|
|
87
93
|
default:
|
|
88
94
|
return handler.rotate(localFile, (err, result) => {
|
|
89
|
-
if (!err && handler.host) {
|
|
95
|
+
if (!err && handler.host?.moduleName === 'filemanager') {
|
|
90
96
|
try {
|
|
91
97
|
handler.host.add(result, parent);
|
|
92
98
|
}
|
|
@@ -188,10 +194,10 @@ function getCacheData() {
|
|
|
188
194
|
}
|
|
189
195
|
function formatMessage(value, startTime, failed, cTimeMs) {
|
|
190
196
|
if (cTimeMs) {
|
|
191
|
-
this.formatMessage(2048
|
|
197
|
+
this.formatMessage(2048, "jimp", [value, 'cache'], new Date(cTimeMs).toLocaleString(), { ...Image.LOG_STYLE_NOTICE, hintBold: true });
|
|
192
198
|
}
|
|
193
199
|
else if (startTime) {
|
|
194
|
-
this.writeTimeProcess("jimp"
|
|
200
|
+
this.writeTimeProcess("jimp", value, startTime, { type: 2048, failed });
|
|
195
201
|
}
|
|
196
202
|
}
|
|
197
203
|
function getTempPath(ext) {
|
|
@@ -231,7 +237,7 @@ class JimpHandler {
|
|
|
231
237
|
const output = leading + value + ext;
|
|
232
238
|
tasks.push(img.writeAsync(output)
|
|
233
239
|
.then(() => this.finalize(output, callback))
|
|
234
|
-
.catch(err => this.instance.writeFail(["Unable to rotate image"
|
|
240
|
+
.catch(err => this.instance.writeFail(["Unable to rotate image", "jimp"], err, 2048)));
|
|
235
241
|
}
|
|
236
242
|
}
|
|
237
243
|
if (deg) {
|
|
@@ -254,7 +260,7 @@ class JimpHandler {
|
|
|
254
260
|
try {
|
|
255
261
|
const alias = getMethodName(name);
|
|
256
262
|
if (!alias) {
|
|
257
|
-
throw (0, types_1.errorValue)("Invalid method name"
|
|
263
|
+
throw (0, types_1.errorValue)("Invalid method name", name);
|
|
258
264
|
}
|
|
259
265
|
if (alias === 'composite') {
|
|
260
266
|
const [src, x, y, opts] = args;
|
|
@@ -262,7 +268,7 @@ class JimpHandler {
|
|
|
262
268
|
handler.composite(await jimp.read(src), x, y, opts);
|
|
263
269
|
}
|
|
264
270
|
else {
|
|
265
|
-
throw (0, types_1.errorValue)("Invalid parameters"
|
|
271
|
+
throw (0, types_1.errorValue)("Invalid parameters", alias);
|
|
266
272
|
}
|
|
267
273
|
}
|
|
268
274
|
else {
|
|
@@ -270,7 +276,7 @@ class JimpHandler {
|
|
|
270
276
|
}
|
|
271
277
|
}
|
|
272
278
|
catch (err) {
|
|
273
|
-
this.instance.writeFail(["Unknown"
|
|
279
|
+
this.instance.writeFail(["Unknown", "jimp" + ': ' + name], err, 2048);
|
|
274
280
|
}
|
|
275
281
|
}
|
|
276
282
|
}
|
|
@@ -401,7 +407,7 @@ class JimpHandler {
|
|
|
401
407
|
try {
|
|
402
408
|
child_process.execFile((0, util_1.getWebP_bin)('cwebp', webp.path), args, { shell: true, signal: this.instance.signal }, err => {
|
|
403
409
|
if (err) {
|
|
404
|
-
this.instance.writeFail(["Unable to convert file"
|
|
410
|
+
this.instance.writeFail(["Unable to convert file", path.basename(filename)], err, 2048);
|
|
405
411
|
}
|
|
406
412
|
else if (webp !== output) {
|
|
407
413
|
const tempFile = output;
|
|
@@ -416,7 +422,7 @@ class JimpHandler {
|
|
|
416
422
|
});
|
|
417
423
|
}
|
|
418
424
|
catch (err) {
|
|
419
|
-
this.instance.checkPackage(err, 'cwebp-bin@6.1.2', "Unknown"
|
|
425
|
+
this.instance.checkPackage(err, 'cwebp-bin@6.1.2', "Unknown", { type: 2048, passThrough: !!callback });
|
|
420
426
|
if (callback) {
|
|
421
427
|
callback(err, '');
|
|
422
428
|
}
|
|
@@ -447,7 +453,7 @@ class JimpHandler {
|
|
|
447
453
|
resolve(fs.readFileSync(result));
|
|
448
454
|
}
|
|
449
455
|
catch (err) {
|
|
450
|
-
this.instance.writeFail(["Unable to read file"
|
|
456
|
+
this.instance.writeFail(["Unable to read file", path.basename(result)], err, 32);
|
|
451
457
|
resolve(null);
|
|
452
458
|
}
|
|
453
459
|
queueMicrotask(() => fs.unlink(result, err => !err && this.instance.emit('file:delete', result)));
|
|
@@ -513,7 +519,7 @@ class JimpHandler {
|
|
|
513
519
|
callback(err, '');
|
|
514
520
|
}
|
|
515
521
|
else {
|
|
516
|
-
this.instance.writeFail(["Unable to write file"
|
|
522
|
+
this.instance.writeFail(["Unable to write file", path.basename(output)], err, 2048);
|
|
517
523
|
}
|
|
518
524
|
});
|
|
519
525
|
}
|
|
@@ -527,11 +533,10 @@ class JimpHandler {
|
|
|
527
533
|
return this.instance.rotateData?.values.length || 0;
|
|
528
534
|
}
|
|
529
535
|
}
|
|
530
|
-
// @ts-ignore
|
|
531
536
|
class Jimp extends Image {
|
|
532
537
|
constructor() {
|
|
533
538
|
super(...arguments);
|
|
534
|
-
this._moduleName = "jimp"
|
|
539
|
+
this._moduleName = "jimp";
|
|
535
540
|
this._threadable = true;
|
|
536
541
|
}
|
|
537
542
|
static async transform(file, command, options = {}) {
|
|
@@ -582,7 +587,7 @@ class Jimp extends Image {
|
|
|
582
587
|
return Promise.resolve(buffer);
|
|
583
588
|
}
|
|
584
589
|
}
|
|
585
|
-
instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp"
|
|
590
|
+
instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp", ["Transforming image...", filename], command);
|
|
586
591
|
Image.initCpuUsage(instance);
|
|
587
592
|
return performCommand(null, instance, file, command, outputType, finalAs)
|
|
588
593
|
.then(async (handler) => {
|
|
@@ -619,26 +624,26 @@ class Jimp extends Image {
|
|
|
619
624
|
const localUri = host.getLocalUri(data);
|
|
620
625
|
const mimeType = host.getMimeType(data);
|
|
621
626
|
if (!localUri || !util_1.MIME_INPUT.has(mimeType)) {
|
|
622
|
-
reject((0, types_1.errorValue)("Unknown"
|
|
627
|
+
reject((0, types_1.errorValue)("Unknown", !localUri ? 'URI' : 'MIME'));
|
|
623
628
|
return;
|
|
624
629
|
}
|
|
625
630
|
if (!this.canRead(localUri, { ownPermissionOnly: true })) {
|
|
626
|
-
reject((0, types_1.errorValue)("Not permitted to read file"
|
|
631
|
+
reject((0, types_1.errorValue)("Not permitted to read file", localUri));
|
|
627
632
|
return;
|
|
628
633
|
}
|
|
629
634
|
const [outputType, saveAs, finalAs] = (0, util_1.parseFormat)(command = command.trim(), mimeType, true);
|
|
630
635
|
if (!outputType) {
|
|
631
|
-
reject((0, types_1.errorValue)("Invalid format"
|
|
636
|
+
reject((0, types_1.errorValue)("Invalid format", /^\w+/.exec(command)?.[0] || "Unknown"));
|
|
632
637
|
return;
|
|
633
638
|
}
|
|
634
639
|
const replace = command.indexOf('@') !== -1;
|
|
635
640
|
const output = host.addCopy(data.getObject({ command, outputType }), saveAs, replace);
|
|
636
641
|
if (!output) {
|
|
637
|
-
reject((0, types_1.errorValue)("Not able to copy file"
|
|
642
|
+
reject((0, types_1.errorValue)("Not able to copy file", outputType));
|
|
638
643
|
return;
|
|
639
644
|
}
|
|
640
645
|
if (!this.canWrite(output, { ownPermissionOnly: true })) {
|
|
641
|
-
reject((0, types_1.errorValue)("Not permitted to write file"
|
|
646
|
+
reject((0, types_1.errorValue)("Not permitted to write file", output));
|
|
642
647
|
return;
|
|
643
648
|
}
|
|
644
649
|
const startTime = process.hrtime();
|
|
@@ -698,7 +703,7 @@ class Jimp extends Image {
|
|
|
698
703
|
removeFile(bmpFile);
|
|
699
704
|
}
|
|
700
705
|
const errorResponse = (err) => {
|
|
701
|
-
this.writeFail(["Unable to finalize image"
|
|
706
|
+
this.writeFail(["Unable to finalize image", "jimp"], err, { type: 2048, startTime });
|
|
702
707
|
resolve();
|
|
703
708
|
};
|
|
704
709
|
if (outputType === jimp.MIME_GIF) {
|
|
@@ -720,18 +725,18 @@ class Jimp extends Image {
|
|
|
720
725
|
finalize(result);
|
|
721
726
|
}
|
|
722
727
|
else {
|
|
723
|
-
errorResponse(err || new Error("Unknown"
|
|
728
|
+
errorResponse(err || new Error("Unknown"));
|
|
724
729
|
}
|
|
725
730
|
});
|
|
726
731
|
}
|
|
727
732
|
})
|
|
728
733
|
.catch(err => {
|
|
729
|
-
this.writeFail(["Unable to read buffer"
|
|
734
|
+
this.writeFail(["Unable to read buffer", path.basename(localUri)], err, { type: 2048, startTime });
|
|
730
735
|
resolve();
|
|
731
736
|
});
|
|
732
737
|
};
|
|
733
738
|
const errorResponse = (err) => {
|
|
734
|
-
this.writeFail(["Unable to convert file"
|
|
739
|
+
this.writeFail(["Unable to convert file", path.basename(localUri)], err, { type: 2048, startTime });
|
|
735
740
|
resolve();
|
|
736
741
|
};
|
|
737
742
|
const rotateAnim = (cmd) => {
|
|
@@ -741,9 +746,9 @@ class Jimp extends Image {
|
|
|
741
746
|
}
|
|
742
747
|
};
|
|
743
748
|
const hasTransform = (cmd) => !!(cmd.rotate || cmd.opacity >= 0 && cmd.opacity < 1 || cmd.resize || cmd.crop || cmd.method);
|
|
744
|
-
const startMessage = () => host.formatMessage(2048
|
|
749
|
+
const startMessage = () => host.formatMessage(2048, "jimp", ["Transforming image...", path.basename(localUri)], command);
|
|
745
750
|
if (mimeType === jimp.MIME_GIF) {
|
|
746
|
-
if (outputType === jimp.MIME_GIF || outputType === "image/webp"
|
|
751
|
+
if (outputType === jimp.MIME_GIF || outputType === "image/webp") {
|
|
747
752
|
const cmd = this.parseCommand(command);
|
|
748
753
|
const transformWebP = (target, modified) => {
|
|
749
754
|
var _a;
|
|
@@ -800,7 +805,7 @@ class Jimp extends Image {
|
|
|
800
805
|
});
|
|
801
806
|
}
|
|
802
807
|
catch (err) {
|
|
803
|
-
if (this.checkPackage(err, 'gif2webp-bin@3', 2048
|
|
808
|
+
if (this.checkPackage(err, 'gif2webp-bin@3', 2048)) {
|
|
804
809
|
resolve();
|
|
805
810
|
}
|
|
806
811
|
else {
|
|
@@ -853,18 +858,16 @@ class Jimp extends Image {
|
|
|
853
858
|
transformBuffer();
|
|
854
859
|
}
|
|
855
860
|
}
|
|
856
|
-
else if (mimeType === "image/webp"
|
|
861
|
+
else if (mimeType === "image/webp") {
|
|
857
862
|
const tryWebpMux = async () => {
|
|
858
|
-
let loaded;
|
|
859
863
|
try {
|
|
860
|
-
const webp = new (require('node-webpmux').Image
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
+
const webp = new (WEBPMUX || (WEBPMUX = require('node-webpmux'))).Image();
|
|
865
|
+
if (!WEBPMUX_INIT) {
|
|
866
|
+
await webp.initLib();
|
|
867
|
+
WEBPMUX_INIT = true;
|
|
864
868
|
}
|
|
865
|
-
await webp.initLib();
|
|
866
869
|
await webp.load(host.getBuffer(file));
|
|
867
|
-
if (!(webp.hasAnim && (outputType === "image/webp"
|
|
870
|
+
if (!(webp.hasAnim && (outputType === "image/webp" || outputType === jimp.MIME_GIF))) {
|
|
868
871
|
transformBuffer(bmp.encode({ width: webp.width, height: webp.height, data: Image.toABGR(await (!webp.hasAnim ? webp.getImageData() : webp.getFrameData(0))) }).data);
|
|
869
872
|
return true;
|
|
870
873
|
}
|
|
@@ -926,10 +929,10 @@ class Jimp extends Image {
|
|
|
926
929
|
return true;
|
|
927
930
|
}
|
|
928
931
|
catch (err) {
|
|
929
|
-
if (
|
|
930
|
-
this.writeFail(["Unknown"
|
|
932
|
+
if (WEBPMUX_INIT) {
|
|
933
|
+
this.writeFail(["Unknown", 'node-webpmux'], err, { type: 2048, startTime });
|
|
931
934
|
}
|
|
932
|
-
else if (this.checkPackage(err, 'node-webpmux@3', 2048
|
|
935
|
+
else if (this.checkPackage(err, 'node-webpmux@3', 2048)) {
|
|
933
936
|
resolve();
|
|
934
937
|
return true;
|
|
935
938
|
}
|
|
@@ -951,7 +954,7 @@ class Jimp extends Image {
|
|
|
951
954
|
}
|
|
952
955
|
catch (err) {
|
|
953
956
|
if (!await tryWebpMux()) {
|
|
954
|
-
if (this.checkPackage(err, 'dwebp-bin@1', 2048
|
|
957
|
+
if (this.checkPackage(err, 'dwebp-bin@1', 2048)) {
|
|
955
958
|
resolve();
|
|
956
959
|
}
|
|
957
960
|
else {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/jimp",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
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.5.
|
|
24
|
-
"@e-mc/types": "^0.5.
|
|
23
|
+
"@e-mc/image": "^0.5.5",
|
|
24
|
+
"@e-mc/types": "^0.5.5",
|
|
25
25
|
"bmp-js": "^0.1.0",
|
|
26
26
|
"gifwrap": "^0.9.4",
|
|
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 {
|