@pi-r/jimp 0.6.2 → 0.6.6

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 CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2023 An Pham
1
+ Copyright 2024 An Pham
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
@@ -1,7 +1,10 @@
1
- ### @pi-r/jimp
1
+ # @pi-r/jimp
2
2
 
3
- https://e-mc.readthedocs.io/en/latest/image/index.html
3
+ ## Documentation
4
4
 
5
- ### LICENSE
5
+ - [E-mc](https://e-mc.readthedocs.io/en/latest/image/index.html)
6
+ - [squared](https://squared.readthedocs.io)
7
+
8
+ ## LICENSE
6
9
 
7
10
  MIT
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
- const util_1 = require("./util");
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?.add(result, parent);
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 /* LOG_TYPE.IMAGE */, "jimp" /* STRINGS.MODULE_NAME */, [value, 'cache'], new Date(cTimeMs).toLocaleString(), { ...Image.LOG_STYLE_NOTICE, hintBold: true });
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" /* STRINGS.MODULE_NAME */, value, startTime, { type: 2048 /* LOG_TYPE.IMAGE */, failed });
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" /* ERR_IMAGE.ROTATE */, "jimp" /* STRINGS.MODULE_NAME */], err, 2048 /* LOG_TYPE.IMAGE */)));
267
+ .catch(err => this.instance.writeFail(["Unable to rotate image", "jimp"], err, 2048)));
262
268
  }
263
269
  }
264
270
  if (deg) {
@@ -281,7 +287,7 @@ class JimpHandler {
281
287
  try {
282
288
  const alias = getMethodName(name);
283
289
  if (!alias) {
284
- throw (0, types_1.errorValue)("Invalid method name" /* ERR_IMAGE.METHOD_NAME */, name);
290
+ throw (0, types_1.errorValue)("Invalid method name", name);
285
291
  }
286
292
  if (alias === 'composite') {
287
293
  const [src, x, y, opts] = args;
@@ -289,7 +295,7 @@ class JimpHandler {
289
295
  handler.composite(await jimp.read(src), x, y, opts);
290
296
  }
291
297
  else {
292
- throw (0, types_1.errorValue)("Invalid parameters" /* ERR_MESSAGE.PARAMETERS */, alias);
298
+ throw (0, types_1.errorValue)("Invalid parameters", alias);
293
299
  }
294
300
  }
295
301
  else {
@@ -297,7 +303,7 @@ class JimpHandler {
297
303
  }
298
304
  }
299
305
  catch (err) {
300
- this.instance.writeFail(["Unknown" /* ERR_MESSAGE.UNKNOWN */, "jimp" /* STRINGS.MODULE_NAME */ + ': ' + name], err, 2048 /* LOG_TYPE.IMAGE */);
306
+ this.instance.writeFail(["Unknown", "jimp" + ': ' + name], err, 2048);
301
307
  }
302
308
  }
303
309
  }
@@ -428,7 +434,7 @@ class JimpHandler {
428
434
  try {
429
435
  child_process.execFile((0, util_1.getWebP_bin)('cwebp', webp.path), args, { shell: true, signal: this.instance.signal, ...execOptions(settings) }, err => {
430
436
  if (err) {
431
- this.instance.writeFail(["Unable to convert file" /* ERR_MESSAGE.CONVERT_FILE */, path.basename(filename)], err, 2048 /* LOG_TYPE.IMAGE */);
437
+ this.instance.writeFail(["Unable to convert file", path.basename(filename)], err, 2048);
432
438
  }
433
439
  else if (webp !== output) {
434
440
  const tempFile = output;
@@ -443,7 +449,7 @@ class JimpHandler {
443
449
  });
444
450
  }
445
451
  catch (err) {
446
- this.instance.checkPackage(err, 'cwebp-bin@6.1.2', "Unknown" /* ERR_MESSAGE.UNKNOWN */, { type: 2048 /* LOG_TYPE.IMAGE */, passThrough: !!callback });
452
+ this.instance.checkPackage(err, 'cwebp-bin@6.1.2', "Unknown", { type: 2048, passThrough: !!callback });
447
453
  if (callback) {
448
454
  callback(err, '');
449
455
  }
@@ -474,7 +480,7 @@ class JimpHandler {
474
480
  resolve(fs.readFileSync(result));
475
481
  }
476
482
  catch (err) {
477
- this.instance.writeFail(["Unable to read file" /* ERR_MESSAGE.READ_FILE */, path.basename(result)], err, 32 /* LOG_TYPE.FILE */);
483
+ this.instance.writeFail(["Unable to read file", path.basename(result)], err, 32);
478
484
  resolve(null);
479
485
  }
480
486
  queueMicrotask(() => fs.unlink(result, err => !err && this.instance.emit('file:delete', result)));
@@ -540,7 +546,7 @@ class JimpHandler {
540
546
  callback(err, '');
541
547
  }
542
548
  else {
543
- this.instance.writeFail(["Unable to write file" /* ERR_MESSAGE.WRITE_FILE */, path.basename(output)], err, 2048 /* LOG_TYPE.IMAGE */);
549
+ this.instance.writeFail(["Unable to write file", path.basename(output)], err, 2048);
544
550
  }
545
551
  });
546
552
  }
@@ -554,11 +560,10 @@ class JimpHandler {
554
560
  return this.instance.rotateData?.values.length || 0;
555
561
  }
556
562
  }
557
- // @ts-ignore
558
563
  class Jimp extends Image {
559
564
  constructor() {
560
565
  super(...arguments);
561
- this._moduleName = "jimp" /* STRINGS.MODULE_NAME */;
566
+ this._moduleName = "jimp";
562
567
  this._threadable = true;
563
568
  }
564
569
  static async transform(file, command, options = {}) {
@@ -611,7 +616,7 @@ class Jimp extends Image {
611
616
  return buffer;
612
617
  }
613
618
  }
614
- instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp" /* STRINGS.MODULE_NAME */, ["Transforming image..." /* STRINGS.TRANSFORM */, filename], command);
619
+ instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp", ["Transforming image...", filename], command);
615
620
  Image.initCpuUsage(instance);
616
621
  return performCommand(null, instance, file, command, outputType, finalAs)
617
622
  .then(async (handler) => {
@@ -645,26 +650,26 @@ class Jimp extends Image {
645
650
  const localUri = host.getLocalUri(data);
646
651
  const mimeType = host.getMimeType(data);
647
652
  if (!localUri || !util_1.MIME_INPUT.has(mimeType)) {
648
- reject((0, types_1.errorValue)("Unknown" /* ERR_MESSAGE.UNKNOWN */, !localUri ? 'URI' : 'MIME'));
653
+ reject((0, types_1.errorValue)("Unknown", !localUri ? 'URI' : 'MIME'));
649
654
  return;
650
655
  }
651
656
  if (!this.canRead(localUri, { ownPermissionOnly: true })) {
652
- reject((0, types_1.errorValue)("Not permitted to read file" /* ERR_MESSAGE.UNSUPPORTED_READ */, localUri));
657
+ reject((0, types_1.errorValue)("Not permitted to read file", localUri));
653
658
  return;
654
659
  }
655
660
  const [outputType, saveAs, finalAs] = (0, util_1.parseFormat)(command = command.trim(), mimeType, true);
656
661
  if (!outputType) {
657
- reject((0, types_1.errorValue)("Invalid format" /* ERR_MESSAGE.FORMAT */, /^\w+/.exec(command)?.[0] || "Unknown" /* ERR_MESSAGE.UNKNOWN */));
662
+ reject((0, types_1.errorValue)("Invalid format", /^\w+/.exec(command)?.[0] || "Unknown"));
658
663
  return;
659
664
  }
660
665
  const replace = command.includes('@');
661
666
  const output = host.addCopy(data.getObject({ command, outputType }), saveAs, replace);
662
667
  if (!output) {
663
- reject((0, types_1.errorValue)("Not able to copy file" /* ERR_MESSAGE.NOT_COPYABLE */, outputType));
668
+ reject((0, types_1.errorValue)("Not able to copy file", outputType));
664
669
  return;
665
670
  }
666
671
  if (!this.canWrite(output, { ownPermissionOnly: true })) {
667
- reject((0, types_1.errorValue)("Not permitted to write file" /* ERR_MESSAGE.UNSUPPORTED_WRITE */, output));
672
+ reject((0, types_1.errorValue)("Not permitted to write file", output));
668
673
  return;
669
674
  }
670
675
  const startTime = process.hrtime();
@@ -725,7 +730,7 @@ class Jimp extends Image {
725
730
  removeFile(bmpFile);
726
731
  }
727
732
  const errorResponse = (err) => {
728
- this.writeFail(["Unable to finalize image" /* ERR_IMAGE.FINALIZE */, "jimp" /* STRINGS.MODULE_NAME */], err, { type: 2048 /* LOG_TYPE.IMAGE */, startTime });
733
+ this.writeFail(["Unable to finalize image", "jimp"], err, { type: 2048, startTime });
729
734
  resolve();
730
735
  };
731
736
  if (outputType === jimp.MIME_GIF) {
@@ -747,18 +752,18 @@ class Jimp extends Image {
747
752
  finalize(result);
748
753
  }
749
754
  else {
750
- errorResponse(err || new Error("Unknown" /* ERR_MESSAGE.UNKNOWN */));
755
+ errorResponse(err || new Error("Unknown"));
751
756
  }
752
757
  });
753
758
  }
754
759
  })
755
760
  .catch(err => {
756
- this.writeFail(["Unable to read buffer" /* ERR_MESSAGE.READ_BUFFER */, path.basename(localUri)], err, { type: 2048 /* LOG_TYPE.IMAGE */, startTime });
761
+ this.writeFail(["Unable to read buffer", path.basename(localUri)], err, { type: 2048, startTime });
757
762
  resolve();
758
763
  });
759
764
  };
760
765
  const errorResponse = (err) => {
761
- this.writeFail(["Unable to convert file" /* ERR_MESSAGE.CONVERT_FILE */, path.basename(localUri)], err, { type: 2048 /* LOG_TYPE.IMAGE */, startTime });
766
+ this.writeFail(["Unable to convert file", path.basename(localUri)], err, { type: 2048, startTime });
762
767
  resolve();
763
768
  };
764
769
  const rotateAnim = (cmd) => {
@@ -768,9 +773,9 @@ class Jimp extends Image {
768
773
  }
769
774
  };
770
775
  const hasTransform = (cmd) => !!(cmd.rotate || cmd.opacity >= 0 && cmd.opacity < 1 || cmd.resize || cmd.crop || cmd.method);
771
- const startMessage = () => host.formatMessage(2048 /* LOG_TYPE.IMAGE */, "jimp" /* STRINGS.MODULE_NAME */, ["Transforming image..." /* STRINGS.TRANSFORM */, path.basename(localUri)], command);
776
+ const startMessage = () => host.formatMessage(2048, "jimp", ["Transforming image...", path.basename(localUri)], command);
772
777
  if (mimeType === jimp.MIME_GIF) {
773
- if (outputType === jimp.MIME_GIF || outputType === "image/webp" /* STRINGS.MIME_WEBP */) {
778
+ if (outputType === jimp.MIME_GIF || outputType === "image/webp") {
774
779
  const cmd = this.parseCommand(command);
775
780
  const transformWebP = (target, modified) => {
776
781
  var _a;
@@ -828,7 +833,7 @@ class Jimp extends Image {
828
833
  });
829
834
  }
830
835
  catch (err) {
831
- if (this.checkPackage(err, 'gif2webp-bin@3', 2048 /* LOG_TYPE.IMAGE */)) {
836
+ if (this.checkPackage(err, 'gif2webp-bin@3', 2048)) {
832
837
  resolve();
833
838
  }
834
839
  else {
@@ -881,15 +886,16 @@ class Jimp extends Image {
881
886
  transformBuffer();
882
887
  }
883
888
  }
884
- else if (mimeType === "image/webp" /* STRINGS.MIME_WEBP */) {
889
+ else if (mimeType === "image/webp") {
885
890
  const tryWebpMux = async () => {
886
- let loaded;
887
891
  try {
888
- const webp = new (require('node-webpmux').Image)();
889
- loaded = true;
890
- await webp.initLib();
892
+ const webp = new (WEBPMUX || (WEBPMUX = require('node-webpmux'))).Image();
893
+ if (!WEBPMUX_INIT) {
894
+ await webp.initLib();
895
+ WEBPMUX_INIT = true;
896
+ }
891
897
  await webp.load(host.getBuffer(file));
892
- if (!(webp.hasAnim && (outputType === "image/webp" /* STRINGS.MIME_WEBP */ || outputType === jimp.MIME_GIF))) {
898
+ if (!(webp.hasAnim && (outputType === "image/webp" || outputType === jimp.MIME_GIF))) {
893
899
  transformBuffer(bmp.encode({ width: webp.width, height: webp.height, data: Image.toABGR(await (!webp.hasAnim ? webp.getImageData() : webp.getFrameData(0))) }).data);
894
900
  return true;
895
901
  }
@@ -951,10 +957,10 @@ class Jimp extends Image {
951
957
  return true;
952
958
  }
953
959
  catch (err) {
954
- if (loaded) {
955
- this.writeFail(["Unknown" /* ERR_MESSAGE.UNKNOWN */, 'node-webpmux'], err, { type: 2048 /* LOG_TYPE.IMAGE */, startTime });
960
+ if (WEBPMUX_INIT) {
961
+ this.writeFail(["Unknown", 'node-webpmux'], err, { type: 2048, startTime });
956
962
  }
957
- else if (this.checkPackage(err, 'node-webpmux@3', 2048 /* LOG_TYPE.IMAGE */)) {
963
+ else if (this.checkPackage(err, 'node-webpmux@3', 2048)) {
958
964
  resolve();
959
965
  return true;
960
966
  }
@@ -977,7 +983,7 @@ class Jimp extends Image {
977
983
  }
978
984
  catch (err) {
979
985
  if (!await tryWebpMux()) {
980
- if (this.checkPackage(err, 'dwebp-bin@1', 2048 /* LOG_TYPE.IMAGE */)) {
986
+ if (this.checkPackage(err, 'dwebp-bin@1', 2048)) {
981
987
  resolve();
982
988
  }
983
989
  else {
@@ -996,5 +1002,4 @@ class Jimp extends Image {
996
1002
  return (_a = this.module).settings || (_a.settings = {});
997
1003
  }
998
1004
  }
999
-
1000
1005
  module.exports = Jimp;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/jimp",
3
- "version": "0.6.2",
3
+ "version": "0.6.6",
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.8.2",
24
- "@e-mc/types": "^0.8.2",
23
+ "@e-mc/image": "^0.8.7",
24
+ "@e-mc/types": "^0.8.7",
25
25
  "bmp-js": "^0.1.0",
26
26
  "gifwrap": "^0.10.1",
27
- "jimp": "^0.22.10"
27
+ "jimp": "^0.22.12"
28
28
  }
29
29
  }
package/util.js CHANGED
@@ -1,13 +1,12 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
2
  exports.showOutputType = exports.showInputType = exports.getWebP_bin = exports.normalizePath = exports.renameExt = exports.parseFormat = exports.MIME_OUTPUT = exports.MIME_INPUT = void 0;
4
3
  const path = require("path");
5
4
  const fs = require("fs");
6
5
  const jimp = require("jimp");
7
6
  const types_1 = require("@e-mc/types");
8
7
  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" /* STRINGS.MIME_WEBP */]);
10
- exports.MIME_OUTPUT = new Set([jimp.MIME_PNG, jimp.MIME_JPEG, jimp.MIME_BMP, jimp.MIME_GIF, "image/webp" /* STRINGS.MIME_WEBP */]);
8
+ exports.MIME_INPUT = new Set([jimp.MIME_PNG, jimp.MIME_JPEG, jimp.MIME_BMP, jimp.MIME_GIF, jimp.MIME_TIFF, "image/webp"]);
9
+ exports.MIME_OUTPUT = new Set([jimp.MIME_PNG, jimp.MIME_JPEG, jimp.MIME_BMP, jimp.MIME_GIF, "image/webp"]);
11
10
  function parseFormat(command, mimeType, gif) {
12
11
  command = command.toLowerCase();
13
12
  for (let mime of exports.MIME_OUTPUT) {
@@ -20,10 +19,10 @@ function parseFormat(command, mimeType, gif) {
20
19
  saveAs = 'jpg';
21
20
  break;
22
21
  case 'webp':
23
- if (mimeType === "image/webp" /* STRINGS.MIME_WEBP */) {
22
+ if (mimeType === "image/webp") {
24
23
  try {
25
24
  require('node-webpmux');
26
- mime = "image/webp" /* STRINGS.MIME_WEBP */;
25
+ mime = "image/webp";
27
26
  break;
28
27
  }
29
28
  catch {