@pi-r/jimp 0.3.1 → 0.3.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 CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2023 An Pham
1
+ Copyright 2023 Studio Trigger
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,5 +1,7 @@
1
- ### @pi-r/jimp
1
+ # @pi-r/jimp
2
2
 
3
- ### LICENSE
3
+ PEP 402 - Forever Aina
4
+
5
+ ## LICENSE
4
6
 
5
7
  MIT
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
- const util_1 = require("./util");
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?.add(result, parent);
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 /* LOG_TYPE.IMAGE */, "jimp" /* STRINGS.MODULE_NAME */, [value, 'cache'], new Date(cTimeMs).toLocaleString(), { ...Image.LOG_STYLE_NOTICE, hintBold: true });
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" /* STRINGS.MODULE_NAME */, value, startTime, { type: 2048 /* LOG_TYPE.IMAGE */, failed });
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" /* ERR_IMAGE.ROTATE */, "jimp" /* STRINGS.MODULE_NAME */], err, 2048 /* LOG_TYPE.IMAGE */)));
268
+ .catch(err => this.instance.writeFail(["Unable to rotate image", "jimp"], err, 2048)));
262
269
  }
263
270
  }
264
271
  if (deg) {
@@ -283,7 +290,7 @@ class JimpHandler {
283
290
  try {
284
291
  const alias = getMethodName(name);
285
292
  if (!alias) {
286
- throw (0, types_1.errorValue)("Invalid method name" /* ERR_IMAGE.METHOD_NAME */, name);
293
+ throw (0, types_1.errorValue)("Invalid method name", name);
287
294
  }
288
295
  if (alias === 'composite') {
289
296
  const [src, x, y, opts] = args;
@@ -291,7 +298,7 @@ class JimpHandler {
291
298
  handler.composite(await jimp.read(src), x, y, opts);
292
299
  }
293
300
  else {
294
- throw (0, types_1.errorValue)("Invalid parameters" /* ERR_MESSAGE.PARAMETERS */, alias);
301
+ throw (0, types_1.errorValue)("Invalid parameters", alias);
295
302
  }
296
303
  }
297
304
  else {
@@ -299,7 +306,7 @@ class JimpHandler {
299
306
  }
300
307
  }
301
308
  catch (err) {
302
- this.instance.writeFail(["Unknown" /* ERR_MESSAGE.UNKNOWN */, "jimp" /* STRINGS.MODULE_NAME */ + ': ' + name], err, 2048 /* LOG_TYPE.IMAGE */);
309
+ this.instance.writeFail(["Unknown", "jimp" + ': ' + name], err, 2048);
303
310
  }
304
311
  }
305
312
  }
@@ -430,7 +437,7 @@ class JimpHandler {
430
437
  try {
431
438
  child_process.execFile((0, util_1.getWebP_bin)('cwebp', webp.path), args, { shell: true, signal: this.instance.signal, ...execOptions(settings) }, err => {
432
439
  if (err) {
433
- this.instance.writeFail(["Unable to convert file" /* ERR_MESSAGE.CONVERT_FILE */, path.basename(filename)], err, 2048 /* LOG_TYPE.IMAGE */);
440
+ this.instance.writeFail(["Unable to convert file", path.basename(filename)], err, 2048);
434
441
  }
435
442
  else if (webp !== output) {
436
443
  const tempFile = output;
@@ -445,7 +452,7 @@ class JimpHandler {
445
452
  });
446
453
  }
447
454
  catch (err) {
448
- this.instance.checkPackage(err, 'cwebp-bin@6.1.2', "Unknown" /* ERR_MESSAGE.UNKNOWN */, { type: 2048 /* LOG_TYPE.IMAGE */, passThrough: !!callback });
455
+ this.instance.checkPackage(err, 'cwebp-bin@6.1.2', "Unknown", { type: 2048, passThrough: !!callback });
449
456
  if (callback) {
450
457
  callback(err, '');
451
458
  }
@@ -476,7 +483,7 @@ class JimpHandler {
476
483
  resolve(fs.readFileSync(result));
477
484
  }
478
485
  catch (err) {
479
- this.instance.writeFail(["Unable to read file" /* ERR_MESSAGE.READ_FILE */, path.basename(result)], err, 32 /* LOG_TYPE.FILE */);
486
+ this.instance.writeFail(["Unable to read file", path.basename(result)], err, 32);
480
487
  resolve(null);
481
488
  }
482
489
  queueMicrotask(() => fs.unlink(result, err => !err && this.instance.emit('file:delete', result)));
@@ -542,7 +549,7 @@ class JimpHandler {
542
549
  callback(err, '');
543
550
  }
544
551
  else {
545
- this.instance.writeFail(["Unable to write file" /* ERR_MESSAGE.WRITE_FILE */, path.basename(output)], err, 2048 /* LOG_TYPE.IMAGE */);
552
+ this.instance.writeFail(["Unable to write file", path.basename(output)], err, 2048);
546
553
  }
547
554
  });
548
555
  }
@@ -556,11 +563,10 @@ class JimpHandler {
556
563
  return this.instance.rotateData?.values.length || 0;
557
564
  }
558
565
  }
559
- // @ts-ignore
560
566
  class Jimp extends Image {
561
567
  constructor() {
562
568
  super(...arguments);
563
- this._moduleName = "jimp" /* STRINGS.MODULE_NAME */;
569
+ this._moduleName = "jimp";
564
570
  this._threadable = true;
565
571
  }
566
572
  static async transform(file, command, options = {}) {
@@ -613,7 +619,7 @@ class Jimp extends Image {
613
619
  return Promise.resolve(buffer);
614
620
  }
615
621
  }
616
- instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp" /* STRINGS.MODULE_NAME */, ["Transforming image..." /* STRINGS.TRANSFORM */, filename], command);
622
+ instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp", ["Transforming image...", filename], command);
617
623
  Image.initCpuUsage(instance);
618
624
  return performCommand(null, instance, file, command, outputType, finalAs)
619
625
  .then(async (handler) => {
@@ -647,26 +653,26 @@ class Jimp extends Image {
647
653
  const localUri = host.getLocalUri(data);
648
654
  const mimeType = host.getMimeType(data);
649
655
  if (!localUri || !util_1.MIME_INPUT.has(mimeType)) {
650
- reject((0, types_1.errorValue)("Unknown" /* ERR_MESSAGE.UNKNOWN */, !localUri ? 'URI' : 'MIME'));
656
+ reject((0, types_1.errorValue)("Unknown", !localUri ? 'URI' : 'MIME'));
651
657
  return;
652
658
  }
653
659
  if (!this.canRead(localUri, { ownPermissionOnly: true })) {
654
- reject((0, types_1.errorValue)("Not permitted to read file" /* ERR_MESSAGE.UNSUPPORTED_READ */, localUri));
660
+ reject((0, types_1.errorValue)("Not permitted to read file", localUri));
655
661
  return;
656
662
  }
657
663
  const [outputType, saveAs, finalAs] = (0, util_1.parseFormat)(command = command.trim(), mimeType, true);
658
664
  if (!outputType) {
659
- reject((0, types_1.errorValue)("Invalid format" /* ERR_MESSAGE.FORMAT */, /^\w+/.exec(command)?.[0] || "Unknown" /* ERR_MESSAGE.UNKNOWN */));
665
+ reject((0, types_1.errorValue)("Invalid format", /^\w+/.exec(command)?.[0] || "Unknown"));
660
666
  return;
661
667
  }
662
668
  const replace = command.indexOf('@') !== -1;
663
669
  const output = host.addCopy(data.getObject({ command, outputType }), saveAs, replace);
664
670
  if (!output) {
665
- reject((0, types_1.errorValue)("Not able to copy file" /* ERR_MESSAGE.NOT_COPYABLE */, outputType));
671
+ reject((0, types_1.errorValue)("Not able to copy file", outputType));
666
672
  return;
667
673
  }
668
674
  if (!this.canWrite(output, { ownPermissionOnly: true })) {
669
- reject((0, types_1.errorValue)("Not permitted to write file" /* ERR_MESSAGE.UNSUPPORTED_WRITE */, output));
675
+ reject((0, types_1.errorValue)("Not permitted to write file", output));
670
676
  return;
671
677
  }
672
678
  const startTime = process.hrtime();
@@ -727,7 +733,7 @@ class Jimp extends Image {
727
733
  removeFile(bmpFile);
728
734
  }
729
735
  const errorResponse = (err) => {
730
- this.writeFail(["Unable to finalize image" /* ERR_IMAGE.FINALIZE */, "jimp" /* STRINGS.MODULE_NAME */], err, { type: 2048 /* LOG_TYPE.IMAGE */, startTime });
736
+ this.writeFail(["Unable to finalize image", "jimp"], err, { type: 2048, startTime });
731
737
  resolve();
732
738
  };
733
739
  if (outputType === jimp.MIME_GIF) {
@@ -749,18 +755,18 @@ class Jimp extends Image {
749
755
  finalize(result);
750
756
  }
751
757
  else {
752
- errorResponse(err || new Error("Unknown" /* ERR_MESSAGE.UNKNOWN */));
758
+ errorResponse(err || new Error("Unknown"));
753
759
  }
754
760
  });
755
761
  }
756
762
  })
757
763
  .catch(err => {
758
- this.writeFail(["Unable to read buffer" /* ERR_MESSAGE.READ_BUFFER */, path.basename(localUri)], err, { type: 2048 /* LOG_TYPE.IMAGE */, startTime });
764
+ this.writeFail(["Unable to read buffer", path.basename(localUri)], err, { type: 2048, startTime });
759
765
  resolve();
760
766
  });
761
767
  };
762
768
  const errorResponse = (err) => {
763
- this.writeFail(["Unable to convert file" /* ERR_MESSAGE.CONVERT_FILE */, path.basename(localUri)], err, { type: 2048 /* LOG_TYPE.IMAGE */, startTime });
769
+ this.writeFail(["Unable to convert file", path.basename(localUri)], err, { type: 2048, startTime });
764
770
  resolve();
765
771
  };
766
772
  const rotateAnim = (cmd) => {
@@ -770,9 +776,9 @@ class Jimp extends Image {
770
776
  }
771
777
  };
772
778
  const hasTransform = (cmd) => !!(cmd.rotate || cmd.opacity >= 0 && cmd.opacity < 1 || cmd.resize || cmd.crop || cmd.method);
773
- const startMessage = () => host.formatMessage(2048 /* LOG_TYPE.IMAGE */, "jimp" /* STRINGS.MODULE_NAME */, ["Transforming image..." /* STRINGS.TRANSFORM */, path.basename(localUri)], command);
779
+ const startMessage = () => host.formatMessage(2048, "jimp", ["Transforming image...", path.basename(localUri)], command);
774
780
  if (mimeType === jimp.MIME_GIF) {
775
- if (outputType === jimp.MIME_GIF || outputType === "image/webp" /* STRINGS.MIME_WEBP */) {
781
+ if (outputType === jimp.MIME_GIF || outputType === "image/webp") {
776
782
  const cmd = this.parseCommand(command);
777
783
  const transformWebP = (target, modified) => {
778
784
  var _a;
@@ -830,7 +836,7 @@ class Jimp extends Image {
830
836
  });
831
837
  }
832
838
  catch (err) {
833
- if (this.checkPackage(err, 'gif2webp-bin@3', 2048 /* LOG_TYPE.IMAGE */)) {
839
+ if (this.checkPackage(err, 'gif2webp-bin@3', 2048)) {
834
840
  resolve();
835
841
  }
836
842
  else {
@@ -883,18 +889,16 @@ class Jimp extends Image {
883
889
  transformBuffer();
884
890
  }
885
891
  }
886
- else if (mimeType === "image/webp" /* STRINGS.MIME_WEBP */) {
892
+ else if (mimeType === "image/webp") {
887
893
  const tryWebpMux = async () => {
888
- let loaded;
889
894
  try {
890
- const webp = new (require('node-webpmux').Image)();
891
- loaded = true;
892
- if (Image.supported(18, 1)) {
893
- this.formatMessage(2048 /* LOG_TYPE.IMAGE */, 'WARN', 'node-webpmux is not compatible with global fetch', 'NodeJS: --no-experimental-fetch', { ...Image.LOG_STYLE_WARN });
895
+ const webp = new (WEBPMUX || (WEBPMUX = require('node-webpmux'))).Image();
896
+ if (!WEBPMUX_INIT) {
897
+ await webp.initLib();
898
+ WEBPMUX_INIT = true;
894
899
  }
895
- await webp.initLib();
896
900
  await webp.load(host.getBuffer(file));
897
- if (!(webp.hasAnim && (outputType === "image/webp" /* STRINGS.MIME_WEBP */ || outputType === jimp.MIME_GIF))) {
901
+ if (!(webp.hasAnim && (outputType === "image/webp" || outputType === jimp.MIME_GIF))) {
898
902
  transformBuffer(bmp.encode({ width: webp.width, height: webp.height, data: Image.toABGR(await (!webp.hasAnim ? webp.getImageData() : webp.getFrameData(0))) }).data);
899
903
  return true;
900
904
  }
@@ -956,10 +960,10 @@ class Jimp extends Image {
956
960
  return true;
957
961
  }
958
962
  catch (err) {
959
- if (loaded) {
960
- this.writeFail(["Unknown" /* ERR_MESSAGE.UNKNOWN */, 'node-webpmux'], err, { type: 2048 /* LOG_TYPE.IMAGE */, startTime });
963
+ if (WEBPMUX_INIT) {
964
+ this.writeFail(["Unknown", 'node-webpmux'], err, { type: 2048, startTime });
961
965
  }
962
- else if (this.checkPackage(err, 'node-webpmux@3', 2048 /* LOG_TYPE.IMAGE */)) {
966
+ else if (this.checkPackage(err, 'node-webpmux@3', 2048)) {
963
967
  resolve();
964
968
  return true;
965
969
  }
@@ -982,7 +986,7 @@ class Jimp extends Image {
982
986
  }
983
987
  catch (err) {
984
988
  if (!await tryWebpMux()) {
985
- if (this.checkPackage(err, 'dwebp-bin@1', 2048 /* LOG_TYPE.IMAGE */)) {
989
+ if (this.checkPackage(err, 'dwebp-bin@1', 2048)) {
986
990
  resolve();
987
991
  }
988
992
  else {
@@ -1001,5 +1005,9 @@ class Jimp extends Image {
1001
1005
  return (_a = this.module).settings || (_a.settings = {});
1002
1006
  }
1003
1007
  }
1008
+ exports.default = Jimp;
1004
1009
 
1005
- module.exports = Jimp;
1010
+ if (exports.default) {
1011
+ module.exports = exports.default;
1012
+ module.exports.default = exports.default;
1013
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/jimp",
3
- "version": "0.3.1",
3
+ "version": "0.3.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.6.0",
24
- "@e-mc/types": "^0.6.0",
23
+ "@e-mc/image": "^0.6.1",
24
+ "@e-mc/types": "^0.6.1",
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
@@ -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" /* 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 */]);
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" /* STRINGS.MIME_WEBP */) {
23
+ if (mimeType === "image/webp") {
24
24
  try {
25
25
  require('node-webpmux');
26
- mime = "image/webp" /* STRINGS.MIME_WEBP */;
26
+ mime = "image/webp";
27
27
  break;
28
28
  }
29
29
  catch {