@pi-r/jimp 0.2.2 → 0.2.8

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