@pi-r/jimp 0.9.4 → 0.10.1

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/index.js CHANGED
@@ -9,6 +9,7 @@ const node_crypto_1 = require("node:crypto");
9
9
  const types_1 = require("@e-mc/types");
10
10
  const util_1 = require("@pi-r/jimp/util");
11
11
  const Image = require('@e-mc/image');
12
+ const kJimp = Symbol.for('jimp:constructor');
12
13
  let WEBPMUX = null, WEBPMUX_INIT = false;
13
14
  try {
14
15
  WEBPMUX = require('node-webpmux');
@@ -80,7 +81,7 @@ function getMethodName(value) {
80
81
  return value;
81
82
  }
82
83
  }
83
- async function performCommand(host, instance, localUri, command, outputType, outputAs, buffer, parent) {
84
+ async function performCommand(host, instance, localUri, command, outputType, outputAs, { buffer, parent } = {}) {
84
85
  return await jimp.read((buffer || localUri))
85
86
  .then(async (img) => {
86
87
  return await transformCommand(localUri, new JimpHandler(img, instance, host), command, outputType, outputAs, parent);
@@ -145,8 +146,8 @@ async function setImageCache(instance, tempKey, tempFile, output, localFile) {
145
146
  else {
146
147
  stored[tempKey] = { tempKey, tempFile, ctimeMs: Date.now() };
147
148
  }
148
- if (instance.settings.jimp.cache_expires) {
149
- fs.writeFile(tempFile + '.json', JSON.stringify(stored[tempKey]), 'utf-8', () => { });
149
+ if (instance.settings.jimp?.cache_expires) {
150
+ fs.writeFile(tempFile + '.json', JSON.stringify(stored[tempKey]), 'utf8', () => { });
150
151
  }
151
152
  }
152
153
  catch {
@@ -195,7 +196,7 @@ function getCacheData(instance) {
195
196
  if (item.isFile() && path.extname(item.name) === '.json') {
196
197
  const pathname = path.join(TEMP_DIR, item.name);
197
198
  try {
198
- const data = JSON.parse(fs.readFileSync(pathname, 'utf-8'));
199
+ const data = JSON.parse(fs.readFileSync(pathname, 'utf8'));
199
200
  if ((0, types_1.isPlainObject)(data) && fs.existsSync(data.tempFile)) {
200
201
  if (data.ctimeMs + expires > current) {
201
202
  CACHE_TRANSFORM[data.tempKey] = data;
@@ -257,9 +258,8 @@ class JimpHandler {
257
258
  const data = this.instance.rotateData;
258
259
  if (data) {
259
260
  const { values, color } = data;
260
- const handler = this.handler;
261
261
  if (!isNaN(color)) {
262
- handler.background(color);
262
+ this.handler.background(color);
263
263
  }
264
264
  const tasks = [];
265
265
  const length = values.length;
@@ -269,11 +269,11 @@ class JimpHandler {
269
269
  const ext = path.extname(localFile);
270
270
  for (let i = 1; i < length; ++i) {
271
271
  const value = values[i];
272
- const img = handler.clone().rotate(value);
272
+ const img = this.handler.clone().rotate(value);
273
273
  const output = leading + value + ext;
274
274
  tasks.push(img.writeAsync(output)
275
275
  .then(() => {
276
- void this.finalize(output, callback);
276
+ void this.finalize(output, callback, false);
277
277
  })
278
278
  .catch((err) => {
279
279
  this.instance.writeFail(["Unable to rotate image", "jimp"], err, 2048);
@@ -281,7 +281,7 @@ class JimpHandler {
281
281
  }
282
282
  }
283
283
  if (deg) {
284
- handler.rotate(deg);
284
+ this.handler.rotate(deg);
285
285
  }
286
286
  if (tasks.length > 0) {
287
287
  return Promise.all(tasks).then(() => this);
@@ -295,7 +295,6 @@ class JimpHandler {
295
295
  }
296
296
  const data = this.instance.methodData;
297
297
  if (data) {
298
- const handler = this.handler;
299
298
  for (const [name, args = []] of data) {
300
299
  try {
301
300
  const alias = getMethodName(name);
@@ -305,14 +304,14 @@ class JimpHandler {
305
304
  if (alias === 'composite') {
306
305
  const [src, x, y, opts] = args;
307
306
  if ((0, types_1.isString)(src) && typeof x === 'number' && typeof y === 'number') {
308
- handler.composite(await jimp.read(src), x, y, opts);
307
+ this.handler.composite(await jimp.read(src), x, y, opts);
309
308
  }
310
309
  else {
311
310
  throw (0, types_1.errorValue)("Invalid parameters", alias);
312
311
  }
313
312
  }
314
313
  else {
315
- handler[alias](...args);
314
+ this.handler[alias](...args);
316
315
  }
317
316
  }
318
317
  catch (err) {
@@ -328,25 +327,10 @@ class JimpHandler {
328
327
  const data = this.instance.resizeData;
329
328
  if (data) {
330
329
  const { width, height, color, algorithm, align, mode } = data;
331
- const handler = this.handler;
332
330
  if (!isNaN(color)) {
333
- handler.background(color);
334
- }
335
- let resizeMode = jimp.RESIZE_NEAREST_NEIGHBOR, flags = 0;
336
- switch (algorithm) {
337
- case 'bilinear':
338
- resizeMode = jimp.RESIZE_BILINEAR;
339
- break;
340
- case 'bicubic':
341
- resizeMode = jimp.RESIZE_BICUBIC;
342
- break;
343
- case 'hermite':
344
- resizeMode = jimp.RESIZE_HERMITE;
345
- break;
346
- case 'bezier':
347
- resizeMode = jimp.RESIZE_BEZIER;
348
- break;
331
+ this.handler.background(color);
349
332
  }
333
+ let flags = 0;
350
334
  switch (align[0]) {
351
335
  case 'left':
352
336
  flags |= jimp.HORIZONTAL_ALIGN_LEFT;
@@ -371,17 +355,36 @@ class JimpHandler {
371
355
  }
372
356
  switch (mode) {
373
357
  case 'contain':
374
- handler.contain(width, height, flags);
358
+ this.handler.contain(width, height, flags);
375
359
  break;
376
360
  case 'cover':
377
- handler.cover(width, height, flags);
361
+ this.handler.cover(width, height, flags);
378
362
  break;
379
363
  case 'scale':
380
- handler.scaleToFit(width, height);
364
+ this.handler.scaleToFit(width, height);
381
365
  break;
382
- default:
383
- handler.resize(width === Infinity ? jimp.AUTO : width, height === Infinity ? jimp.AUTO : height, resizeMode);
366
+ default: {
367
+ let resizeMode = jimp.RESIZE_NEAREST_NEIGHBOR;
368
+ switch (algorithm) {
369
+ case 'bilinear':
370
+ resizeMode = jimp.RESIZE_BILINEAR;
371
+ break;
372
+ case 'bicubic':
373
+ resizeMode = jimp.RESIZE_BICUBIC;
374
+ break;
375
+ case 'hermite':
376
+ resizeMode = jimp.RESIZE_HERMITE;
377
+ break;
378
+ case 'bezier':
379
+ resizeMode = jimp.RESIZE_BEZIER;
380
+ break;
381
+ case 'none':
382
+ resizeMode = undefined;
383
+ break;
384
+ }
385
+ this.handler.resize(width === Infinity ? jimp.AUTO : width, height === Infinity ? jimp.AUTO : height, resizeMode);
384
386
  break;
387
+ }
385
388
  }
386
389
  }
387
390
  }
@@ -391,7 +394,7 @@ class JimpHandler {
391
394
  }
392
395
  this.handler.background(value);
393
396
  }
394
- async finalize(output, callback) {
397
+ async finalize(output, callback, replace) {
395
398
  if (this.aborted) {
396
399
  return;
397
400
  }
@@ -400,7 +403,7 @@ class JimpHandler {
400
403
  const settings = instance.settings;
401
404
  const webp = settings.webp ||= {};
402
405
  const data = instance.qualityData;
403
- const replace = instance.getCommand().includes('@');
406
+ replace ??= instance.getCommand().includes('@');
404
407
  const filename = (0, util_1.renameExt)(output, 'webp', replace);
405
408
  const args = [(0, util_1.normalizePath)(output)];
406
409
  if (data) {
@@ -445,7 +448,7 @@ class JimpHandler {
445
448
  }
446
449
  args.push('-o', (0, util_1.normalizePath)(filename));
447
450
  try {
448
- child_process.execFile(await (0, util_1.importBinary)('cwebp', webp.path), args, { shell: true, signal: this.instance.signal, ...execOptions(settings) }, err => {
451
+ child_process.execFile((0, types_1.sanitizeCmd)(await (0, util_1.importBinary)('cwebp', webp.path), args), { shell: true, signal: this.instance.signal, ...execOptions(settings) }, err => {
449
452
  if (err) {
450
453
  this.instance.writeFail(["Unable to convert file", path.basename(filename)], err, 2048);
451
454
  }
@@ -521,7 +524,7 @@ class JimpHandler {
521
524
  }
522
525
  const data = this.instance.cropData;
523
526
  if (data) {
524
- this.handler = this.handler.crop(data.x, data.y, data.width, data.height);
527
+ this.handler.crop(data.x, data.y, data.width, data.height);
525
528
  }
526
529
  }
527
530
  opacity() {
@@ -530,7 +533,7 @@ class JimpHandler {
530
533
  }
531
534
  const value = this.instance.opacityValue ?? NaN;
532
535
  if (value >= 0) {
533
- this.handler = this.handler.opacity(value);
536
+ this.handler.opacity(value);
534
537
  }
535
538
  }
536
539
  quality() {
@@ -539,7 +542,7 @@ class JimpHandler {
539
542
  }
540
543
  const data = this.instance.qualityData;
541
544
  if (data && !isNaN(data.value)) {
542
- this.handler = this.handler.quality(data.value);
545
+ this.handler.quality(data.value);
543
546
  }
544
547
  }
545
548
  write(output, callback) {
@@ -589,6 +592,7 @@ class JimpHandler {
589
592
  }
590
593
  }
591
594
  class Jimp extends Image {
595
+ static [kJimp] = true;
592
596
  static async transform(file, command, options = {}) {
593
597
  const [outputType, saveAs, outputAs] = (0, util_1.parseFormat)(command = command.trim(), options.mimeType);
594
598
  if (!outputType) {
@@ -760,8 +764,8 @@ class Jimp extends Image {
760
764
  };
761
765
  const transformBuffer = (bmpFile) => {
762
766
  startMessage();
763
- performCommand(host, this, localUri, command, outputType, outputAs, bmpFile || file.buffer, file)
764
- .then((img) => {
767
+ performCommand(host, this, localUri, command, outputType, outputAs, { buffer: bmpFile || file.buffer, parent: file })
768
+ .then(img => {
765
769
  if (typeof bmpFile === 'string') {
766
770
  removeFile(bmpFile);
767
771
  }
@@ -789,6 +793,9 @@ class Jimp extends Image {
789
793
  if (!err && result) {
790
794
  finalize(result);
791
795
  }
796
+ else if (Image.isErrorCode(err, types_1.ERR_CODE.MODULE_NOT_FOUND)) {
797
+ resolve();
798
+ }
792
799
  else {
793
800
  errorResponse(err || new Error("Unknown"));
794
801
  }
@@ -828,11 +835,10 @@ class Jimp extends Image {
828
835
  for (let i = 0, length = gif2webp.length; i < length; ++i) {
829
836
  const arg = gif2webp[i];
830
837
  switch (arg) {
831
- case '-h':
832
- case '-version':
833
- continue;
834
838
  case '-o':
835
839
  ++i;
840
+ case '-h':
841
+ case '-version':
836
842
  continue;
837
843
  case '-q':
838
844
  case '-m':
@@ -850,7 +856,7 @@ class Jimp extends Image {
850
856
  }
851
857
  args.push('-o', (0, util_1.normalizePath)(webp));
852
858
  try {
853
- child_process.execFile(await (0, util_1.importBinary)('gif2webp', webp_path), args, { shell: true, signal: this.signal, ...execOptions(this.settings) }, (err, stdout) => {
859
+ child_process.execFile((0, types_1.sanitizeCmd)(await (0, util_1.importBinary)('gif2webp', webp_path), args), { shell: true, signal: this.signal, ...execOptions(this.settings) }, (err, stdout) => {
854
860
  if (!err) {
855
861
  this.addLog(types_1.STATUS_TYPE.INFO, stdout);
856
862
  finalize(webp);
@@ -1031,7 +1037,7 @@ class Jimp extends Image {
1031
1037
  const { path: webp_path } = settings.webp ||= {};
1032
1038
  const bmpFile = getTempPath(this, 'bmp');
1033
1039
  try {
1034
- child_process.execFile(await (0, util_1.importBinary)('dwebp', webp_path), [(0, util_1.normalizePath)(localUri), '-bmp', '-o', (0, util_1.normalizePath)(bmpFile)], { shell: true, signal: this.signal, ...execOptions(settings) }, err => {
1040
+ child_process.execFile((0, types_1.sanitizeCmd)(await (0, util_1.importBinary)('dwebp', webp_path), [(0, util_1.normalizePath)(localUri), '-bmp', '-o', (0, util_1.normalizePath)(bmpFile)]), { shell: true, signal: this.signal, ...execOptions(settings) }, err => {
1035
1041
  if (!err) {
1036
1042
  transformBuffer(bmpFile);
1037
1043
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/jimp",
3
- "version": "0.9.4",
3
+ "version": "0.10.1",
4
4
  "description": "Jimp V0 image constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -20,8 +20,8 @@
20
20
  "license": "MIT",
21
21
  "homepage": "https://github.com/anpham6/pi-r#readme",
22
22
  "dependencies": {
23
- "@e-mc/image": "^0.11.5",
24
- "@e-mc/types": "^0.11.5",
23
+ "@e-mc/image": "^0.12.3",
24
+ "@e-mc/types": "^0.12.3",
25
25
  "bmp-js": "^0.1.0",
26
26
  "gifwrap": "^0.10.1",
27
27
  "jimp": "^0.22.12"
package/types/index.d.ts CHANGED
@@ -19,8 +19,7 @@ export interface JimpSettings extends ImageSettings {
19
19
 
20
20
  export type ResultCallback<T = unknown, U = void, V = unknown> = (err: V, result: T) => U;
21
21
 
22
- export interface IJimpHandler<T extends IHost = IHost, U extends ImageModule = ImageModule> extends ImageHandler<jimp, T, IImage<T, U>> {
23
- method(): Promise<void>;
22
+ export interface IJimpHandler<T extends IHost = IHost, U extends ImageModule = ImageModule> extends ImageHandler<jimp, T, IImage<T, U>, void, Promise<void>> {
24
23
  quality(): void;
25
24
  rotate(localFile?: string, callback?: ResultCallback<string>): Promise<this>;
26
25
  background(value: number | [number, number, number, number]): void;
package/util.js CHANGED
@@ -9,23 +9,22 @@ exports.showInputType = showInputType;
9
9
  exports.showOutputType = showOutputType;
10
10
  const path = require("node:path");
11
11
  const fs = require("node:fs");
12
- const jimp = require("jimp");
13
12
  const types = require("@e-mc/types");
14
13
  const Image = require("@e-mc/image");
15
14
  exports.MIME_INPUT = new Set([
16
- jimp.MIME_PNG,
17
- jimp.MIME_JPEG,
18
- jimp.MIME_BMP,
19
- jimp.MIME_GIF,
20
- jimp.MIME_TIFF,
21
- "image/webp"
15
+ Image.MIME_PNG,
16
+ Image.MIME_JPEG,
17
+ Image.MIME_BMP,
18
+ Image.MIME_GIF,
19
+ Image.MIME_TIFF,
20
+ Image.MIME_WEBP
22
21
  ]);
23
22
  exports.MIME_OUTPUT = new Set([
24
- jimp.MIME_PNG,
25
- jimp.MIME_JPEG,
26
- jimp.MIME_BMP,
27
- jimp.MIME_GIF,
28
- "image/webp"
23
+ Image.MIME_PNG,
24
+ Image.MIME_JPEG,
25
+ Image.MIME_BMP,
26
+ Image.MIME_GIF,
27
+ Image.MIME_WEBP
29
28
  ]);
30
29
  function parseFormat(command, mimeType, gif) {
31
30
  command = command.toLowerCase();
@@ -39,16 +38,16 @@ function parseFormat(command, mimeType, gif) {
39
38
  saveAs = 'jpg';
40
39
  break;
41
40
  case 'webp':
42
- if (mimeType === jimp.MIME_JPEG) {
43
- mime = jimp.MIME_JPEG;
41
+ if (mimeType === Image.MIME_JPEG) {
42
+ mime = Image.MIME_JPEG;
44
43
  saveAs = 'jpg';
45
44
  }
46
- else if (gif && mimeType === jimp.MIME_GIF) {
47
- mime = jimp.MIME_GIF;
45
+ else if (gif && mimeType === Image.MIME_GIF) {
46
+ mime = Image.MIME_GIF;
48
47
  saveAs = 'gif';
49
48
  }
50
49
  else {
51
- mime = jimp.MIME_PNG;
50
+ mime = Image.MIME_PNG;
52
51
  saveAs = 'png';
53
52
  }
54
53
  outputAs = 'webp';
@@ -81,7 +80,7 @@ function getWebP_bin(name, pathname) {
81
80
  if (pathname && fs.existsSync(pathname)) {
82
81
  name += Image.PLATFORM_WIN32 ? '.exe' : '';
83
82
  const bin = path.join(pathname, name);
84
- return Image.sanitizeCmd(fs.existsSync(bin) ? bin : path.join(pathname, 'bin', name));
83
+ return types.sanitizeCmd(fs.existsSync(bin) ? bin : path.join(pathname, 'bin', name));
85
84
  }
86
85
  return require(name + '-bin');
87
86
  }
@@ -89,7 +88,7 @@ async function importBinary(name, pathname) {
89
88
  if (pathname && fs.existsSync(pathname)) {
90
89
  name += Image.PLATFORM_WIN32 ? '.exe' : '';
91
90
  const bin = path.join(pathname, name);
92
- return Image.sanitizeCmd(fs.existsSync(bin) ? bin : path.join(pathname, 'bin', name));
91
+ return fs.existsSync(bin) ? bin : path.join(pathname, 'bin', name);
93
92
  }
94
93
  return types.importESM(name + '-bin', true);
95
94
  }