@pi-r/jimp 0.3.0 → 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 = '';
@@ -24,13 +31,16 @@ const METHOD_ALIAS = {
24
31
  composite: 'cp',
25
32
  mask: 'ma',
26
33
  convolute: 'cl',
34
+ convolution: 'cl',
27
35
  flip: 'fl',
28
36
  mirror: 'mi',
29
37
  rotate: 'ro',
30
38
  brightness: 'br',
31
39
  contrast: 'cn',
32
40
  dither565: 'dt',
41
+ dither16: 'dt',
33
42
  greyscale: 'gr',
43
+ grayscale: 'gr',
34
44
  invert: 'in',
35
45
  normalize: 'no',
36
46
  fade: 'fa',
@@ -43,7 +53,19 @@ const METHOD_ALIAS = {
43
53
  sepia: 'se',
44
54
  pixelate: 'px',
45
55
  displace: 'dp',
46
- color: 'co'
56
+ color: 'co',
57
+ colour: 'co',
58
+ backgroundQuiet: 'bq',
59
+ circle: 'ci',
60
+ colorType: 'ce',
61
+ cropQuiet: 'cq',
62
+ deflateLevel: 'dl',
63
+ deflateStrategy: 'ds',
64
+ fishEye: 'fe',
65
+ filterType: 'ft',
66
+ rgba: 'rg',
67
+ shadow: 'sh',
68
+ threshold: 'th'
47
69
  };
48
70
  function getMethodName(value) {
49
71
  const name = value.toLowerCase();
@@ -98,9 +120,9 @@ async function transformCommand(localFile, handler, command, outputType, outputA
98
120
  return handler.rotate();
99
121
  default:
100
122
  return handler.rotate(localFile, (err, result) => {
101
- if (!err) {
123
+ if (!err && handler.host?.moduleName === 'filemanager') {
102
124
  try {
103
- handler.host?.add(result, parent);
125
+ handler.host.add(result, parent);
104
126
  }
105
127
  catch {
106
128
  }
@@ -200,10 +222,10 @@ function getCacheData() {
200
222
  }
201
223
  function formatMessage(value, startTime, failed, cTimeMs) {
202
224
  if (cTimeMs) {
203
- 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 });
204
226
  }
205
227
  else if (startTime) {
206
- this.writeTimeProcess("jimp" /* STRINGS.MODULE_NAME */, value, startTime, { type: 2048 /* LOG_TYPE.IMAGE */, failed });
228
+ this.writeTimeProcess("jimp", value, startTime, { type: 2048, failed });
207
229
  }
208
230
  }
209
231
  function getTempPath(ext) {
@@ -243,7 +265,7 @@ class JimpHandler {
243
265
  const output = leading + value + ext;
244
266
  tasks.push(img.writeAsync(output)
245
267
  .then(() => this.finalize(output, callback))
246
- .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)));
247
269
  }
248
270
  }
249
271
  if (deg) {
@@ -268,7 +290,7 @@ class JimpHandler {
268
290
  try {
269
291
  const alias = getMethodName(name);
270
292
  if (!alias) {
271
- throw (0, types_1.errorValue)("Invalid method name" /* ERR_IMAGE.METHOD_NAME */, name);
293
+ throw (0, types_1.errorValue)("Invalid method name", name);
272
294
  }
273
295
  if (alias === 'composite') {
274
296
  const [src, x, y, opts] = args;
@@ -276,7 +298,7 @@ class JimpHandler {
276
298
  handler.composite(await jimp.read(src), x, y, opts);
277
299
  }
278
300
  else {
279
- throw (0, types_1.errorValue)("Invalid parameters" /* ERR_MESSAGE.PARAMETERS */, alias);
301
+ throw (0, types_1.errorValue)("Invalid parameters", alias);
280
302
  }
281
303
  }
282
304
  else {
@@ -284,7 +306,7 @@ class JimpHandler {
284
306
  }
285
307
  }
286
308
  catch (err) {
287
- 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);
288
310
  }
289
311
  }
290
312
  }
@@ -415,7 +437,7 @@ class JimpHandler {
415
437
  try {
416
438
  child_process.execFile((0, util_1.getWebP_bin)('cwebp', webp.path), args, { shell: true, signal: this.instance.signal, ...execOptions(settings) }, err => {
417
439
  if (err) {
418
- 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);
419
441
  }
420
442
  else if (webp !== output) {
421
443
  const tempFile = output;
@@ -430,7 +452,7 @@ class JimpHandler {
430
452
  });
431
453
  }
432
454
  catch (err) {
433
- 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 });
434
456
  if (callback) {
435
457
  callback(err, '');
436
458
  }
@@ -461,7 +483,7 @@ class JimpHandler {
461
483
  resolve(fs.readFileSync(result));
462
484
  }
463
485
  catch (err) {
464
- 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);
465
487
  resolve(null);
466
488
  }
467
489
  queueMicrotask(() => fs.unlink(result, err => !err && this.instance.emit('file:delete', result)));
@@ -527,7 +549,7 @@ class JimpHandler {
527
549
  callback(err, '');
528
550
  }
529
551
  else {
530
- 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);
531
553
  }
532
554
  });
533
555
  }
@@ -541,11 +563,10 @@ class JimpHandler {
541
563
  return this.instance.rotateData?.values.length || 0;
542
564
  }
543
565
  }
544
- // @ts-ignore
545
566
  class Jimp extends Image {
546
567
  constructor() {
547
568
  super(...arguments);
548
- this._moduleName = "jimp" /* STRINGS.MODULE_NAME */;
569
+ this._moduleName = "jimp";
549
570
  this._threadable = true;
550
571
  }
551
572
  static async transform(file, command, options = {}) {
@@ -598,7 +619,7 @@ class Jimp extends Image {
598
619
  return Promise.resolve(buffer);
599
620
  }
600
621
  }
601
- 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);
602
623
  Image.initCpuUsage(instance);
603
624
  return performCommand(null, instance, file, command, outputType, finalAs)
604
625
  .then(async (handler) => {
@@ -632,26 +653,26 @@ class Jimp extends Image {
632
653
  const localUri = host.getLocalUri(data);
633
654
  const mimeType = host.getMimeType(data);
634
655
  if (!localUri || !util_1.MIME_INPUT.has(mimeType)) {
635
- reject((0, types_1.errorValue)("Unknown" /* ERR_MESSAGE.UNKNOWN */, !localUri ? 'URI' : 'MIME'));
656
+ reject((0, types_1.errorValue)("Unknown", !localUri ? 'URI' : 'MIME'));
636
657
  return;
637
658
  }
638
659
  if (!this.canRead(localUri, { ownPermissionOnly: true })) {
639
- 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));
640
661
  return;
641
662
  }
642
663
  const [outputType, saveAs, finalAs] = (0, util_1.parseFormat)(command = command.trim(), mimeType, true);
643
664
  if (!outputType) {
644
- 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"));
645
666
  return;
646
667
  }
647
668
  const replace = command.indexOf('@') !== -1;
648
669
  const output = host.addCopy(data.getObject({ command, outputType }), saveAs, replace);
649
670
  if (!output) {
650
- 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));
651
672
  return;
652
673
  }
653
674
  if (!this.canWrite(output, { ownPermissionOnly: true })) {
654
- 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));
655
676
  return;
656
677
  }
657
678
  const startTime = process.hrtime();
@@ -712,7 +733,7 @@ class Jimp extends Image {
712
733
  removeFile(bmpFile);
713
734
  }
714
735
  const errorResponse = (err) => {
715
- 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 });
716
737
  resolve();
717
738
  };
718
739
  if (outputType === jimp.MIME_GIF) {
@@ -734,18 +755,18 @@ class Jimp extends Image {
734
755
  finalize(result);
735
756
  }
736
757
  else {
737
- errorResponse(err || new Error("Unknown" /* ERR_MESSAGE.UNKNOWN */));
758
+ errorResponse(err || new Error("Unknown"));
738
759
  }
739
760
  });
740
761
  }
741
762
  })
742
763
  .catch(err => {
743
- 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 });
744
765
  resolve();
745
766
  });
746
767
  };
747
768
  const errorResponse = (err) => {
748
- 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 });
749
770
  resolve();
750
771
  };
751
772
  const rotateAnim = (cmd) => {
@@ -755,9 +776,9 @@ class Jimp extends Image {
755
776
  }
756
777
  };
757
778
  const hasTransform = (cmd) => !!(cmd.rotate || cmd.opacity >= 0 && cmd.opacity < 1 || cmd.resize || cmd.crop || cmd.method);
758
- 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);
759
780
  if (mimeType === jimp.MIME_GIF) {
760
- if (outputType === jimp.MIME_GIF || outputType === "image/webp" /* STRINGS.MIME_WEBP */) {
781
+ if (outputType === jimp.MIME_GIF || outputType === "image/webp") {
761
782
  const cmd = this.parseCommand(command);
762
783
  const transformWebP = (target, modified) => {
763
784
  var _a;
@@ -815,7 +836,7 @@ class Jimp extends Image {
815
836
  });
816
837
  }
817
838
  catch (err) {
818
- if (this.checkPackage(err, 'gif2webp-bin@3', 2048 /* LOG_TYPE.IMAGE */)) {
839
+ if (this.checkPackage(err, 'gif2webp-bin@3', 2048)) {
819
840
  resolve();
820
841
  }
821
842
  else {
@@ -868,18 +889,16 @@ class Jimp extends Image {
868
889
  transformBuffer();
869
890
  }
870
891
  }
871
- else if (mimeType === "image/webp" /* STRINGS.MIME_WEBP */) {
892
+ else if (mimeType === "image/webp") {
872
893
  const tryWebpMux = async () => {
873
- let loaded;
874
894
  try {
875
- const webp = new (require('node-webpmux').Image)();
876
- loaded = true;
877
- if (Image.supported(18, 1)) {
878
- 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;
879
899
  }
880
- await webp.initLib();
881
900
  await webp.load(host.getBuffer(file));
882
- if (!(webp.hasAnim && (outputType === "image/webp" /* STRINGS.MIME_WEBP */ || outputType === jimp.MIME_GIF))) {
901
+ if (!(webp.hasAnim && (outputType === "image/webp" || outputType === jimp.MIME_GIF))) {
883
902
  transformBuffer(bmp.encode({ width: webp.width, height: webp.height, data: Image.toABGR(await (!webp.hasAnim ? webp.getImageData() : webp.getFrameData(0))) }).data);
884
903
  return true;
885
904
  }
@@ -941,10 +960,10 @@ class Jimp extends Image {
941
960
  return true;
942
961
  }
943
962
  catch (err) {
944
- if (loaded) {
945
- 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 });
946
965
  }
947
- else if (this.checkPackage(err, 'node-webpmux@3', 2048 /* LOG_TYPE.IMAGE */)) {
966
+ else if (this.checkPackage(err, 'node-webpmux@3', 2048)) {
948
967
  resolve();
949
968
  return true;
950
969
  }
@@ -967,7 +986,7 @@ class Jimp extends Image {
967
986
  }
968
987
  catch (err) {
969
988
  if (!await tryWebpMux()) {
970
- if (this.checkPackage(err, 'dwebp-bin@1', 2048 /* LOG_TYPE.IMAGE */)) {
989
+ if (this.checkPackage(err, 'dwebp-bin@1', 2048)) {
971
990
  resolve();
972
991
  }
973
992
  else {
@@ -986,5 +1005,9 @@ class Jimp extends Image {
986
1005
  return (_a = this.module).settings || (_a.settings = {});
987
1006
  }
988
1007
  }
1008
+ exports.default = Jimp;
989
1009
 
990
- 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.0",
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 {