@pi-r/jimp 0.2.2 → 0.3.0

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.
Files changed (2) hide show
  1. package/index.js +171 -155
  2. package/package.json +5 -5
package/index.js CHANGED
@@ -64,6 +64,20 @@ function performCommand(host, instance, localUri, command, outputType, finalAs,
64
64
  return transformCommand(localUri, new JimpHandler(img, instance, host), command, outputType, finalAs, parent);
65
65
  });
66
66
  }
67
+ function execOptions(settings) {
68
+ const exec = settings.jimp?.exec;
69
+ let uid, gid;
70
+ if ((0, types_1.isPlainObject)(exec)) {
71
+ let { uid: u, gid: g } = exec;
72
+ if ((u = parseInt(u)) >= 0) {
73
+ uid = u;
74
+ }
75
+ if ((g = parseInt(g)) >= 0) {
76
+ gid = g;
77
+ }
78
+ }
79
+ return { uid, gid };
80
+ }
67
81
  async function transformCommand(localFile, handler, command, outputType, outputAs, parent) {
68
82
  if (command) {
69
83
  handler.instance.setCommand(command, outputAs);
@@ -71,14 +85,12 @@ async function transformCommand(localFile, handler, command, outputType, outputA
71
85
  await handler.method();
72
86
  handler.resize();
73
87
  handler.crop();
74
- if (outputType === jimp.MIME_JPEG) {
75
- if (!outputAs) {
76
- handler.quality();
77
- }
78
- }
79
- else {
88
+ if (outputType !== jimp.MIME_JPEG) {
80
89
  handler.opacity();
81
90
  }
91
+ else if (!outputAs) {
92
+ handler.quality();
93
+ }
82
94
  switch (handler.rotateCount) {
83
95
  case 0:
84
96
  return handler;
@@ -86,9 +98,9 @@ async function transformCommand(localFile, handler, command, outputType, outputA
86
98
  return handler.rotate();
87
99
  default:
88
100
  return handler.rotate(localFile, (err, result) => {
89
- if (!err && handler.host) {
101
+ if (!err) {
90
102
  try {
91
- handler.host.add(result, parent);
103
+ handler.host?.add(result, parent);
92
104
  }
93
105
  catch {
94
106
  }
@@ -248,31 +260,32 @@ class JimpHandler {
248
260
  return;
249
261
  }
250
262
  const data = this.instance.methodData;
251
- if (data) {
252
- const handler = this.handler;
253
- for (const [name, args = []] of data) {
254
- try {
255
- const alias = getMethodName(name);
256
- if (!alias) {
257
- throw (0, types_1.errorValue)("Invalid method name" /* ERR_IMAGE.METHOD_NAME */, name);
258
- }
259
- if (alias === 'composite') {
260
- const [src, x, y, opts] = args;
261
- if ((0, types_1.isString)(src) && typeof x === 'number' && typeof y === 'number') {
262
- handler.composite(await jimp.read(src), x, y, opts);
263
- }
264
- else {
265
- throw (0, types_1.errorValue)("Invalid parameters" /* ERR_MESSAGE.PARAMETERS */, alias);
266
- }
263
+ if (!data) {
264
+ return;
265
+ }
266
+ const handler = this.handler;
267
+ for (const [name, args = []] of data) {
268
+ try {
269
+ const alias = getMethodName(name);
270
+ if (!alias) {
271
+ throw (0, types_1.errorValue)("Invalid method name" /* ERR_IMAGE.METHOD_NAME */, name);
272
+ }
273
+ if (alias === 'composite') {
274
+ const [src, x, y, opts] = args;
275
+ if ((0, types_1.isString)(src) && typeof x === 'number' && typeof y === 'number') {
276
+ handler.composite(await jimp.read(src), x, y, opts);
267
277
  }
268
278
  else {
269
- handler[alias](...args);
279
+ throw (0, types_1.errorValue)("Invalid parameters" /* ERR_MESSAGE.PARAMETERS */, alias);
270
280
  }
271
281
  }
272
- catch (err) {
273
- this.instance.writeFail(["Unknown" /* ERR_MESSAGE.UNKNOWN */, "jimp" /* STRINGS.MODULE_NAME */ + ': ' + name], err, 2048 /* LOG_TYPE.IMAGE */);
282
+ else {
283
+ handler[alias](...args);
274
284
  }
275
285
  }
286
+ catch (err) {
287
+ this.instance.writeFail(["Unknown" /* ERR_MESSAGE.UNKNOWN */, "jimp" /* STRINGS.MODULE_NAME */ + ': ' + name], err, 2048 /* LOG_TYPE.IMAGE */);
288
+ }
276
289
  }
277
290
  }
278
291
  resize() {
@@ -280,63 +293,64 @@ class JimpHandler {
280
293
  return;
281
294
  }
282
295
  const data = this.instance.resizeData;
283
- if (data) {
284
- const { width, height, color, algorithm, align, mode } = data;
285
- const handler = this.handler;
286
- if (!isNaN(color)) {
287
- handler.background(color);
288
- }
289
- let resizeMode = jimp.RESIZE_NEAREST_NEIGHBOR, flags = 0;
290
- switch (algorithm) {
291
- case 'bilinear':
292
- resizeMode = jimp.RESIZE_BILINEAR;
293
- break;
294
- case 'bicubic':
295
- resizeMode = jimp.RESIZE_BICUBIC;
296
- break;
297
- case 'hermite':
298
- resizeMode = jimp.RESIZE_HERMITE;
299
- break;
300
- case 'bezier':
301
- resizeMode = jimp.RESIZE_BEZIER;
302
- break;
303
- }
304
- switch (align[0]) {
305
- case 'left':
306
- flags |= jimp.HORIZONTAL_ALIGN_LEFT;
307
- break;
308
- case 'center':
309
- flags |= jimp.HORIZONTAL_ALIGN_CENTER;
310
- break;
311
- case 'right':
312
- flags |= jimp.HORIZONTAL_ALIGN_RIGHT;
313
- break;
314
- }
315
- switch (align[1]) {
316
- case 'top':
317
- flags |= jimp.VERTICAL_ALIGN_TOP;
318
- break;
319
- case 'middle':
320
- flags |= jimp.VERTICAL_ALIGN_MIDDLE;
321
- break;
322
- case 'bottom':
323
- flags |= jimp.VERTICAL_ALIGN_BOTTOM;
324
- break;
325
- }
326
- switch (mode) {
327
- case 'contain':
328
- handler.contain(width, height, flags);
329
- break;
330
- case 'cover':
331
- handler.cover(width, height, flags);
332
- break;
333
- case 'scale':
334
- handler.scaleToFit(width, height);
335
- break;
336
- default:
337
- handler.resize(width === Infinity ? jimp.AUTO : width, height === Infinity ? jimp.AUTO : height, resizeMode);
338
- break;
339
- }
296
+ if (!data) {
297
+ return;
298
+ }
299
+ const { width, height, color, algorithm, align, mode } = data;
300
+ const handler = this.handler;
301
+ if (!isNaN(color)) {
302
+ handler.background(color);
303
+ }
304
+ let resizeMode = jimp.RESIZE_NEAREST_NEIGHBOR, flags = 0;
305
+ switch (algorithm) {
306
+ case 'bilinear':
307
+ resizeMode = jimp.RESIZE_BILINEAR;
308
+ break;
309
+ case 'bicubic':
310
+ resizeMode = jimp.RESIZE_BICUBIC;
311
+ break;
312
+ case 'hermite':
313
+ resizeMode = jimp.RESIZE_HERMITE;
314
+ break;
315
+ case 'bezier':
316
+ resizeMode = jimp.RESIZE_BEZIER;
317
+ break;
318
+ }
319
+ switch (align[0]) {
320
+ case 'left':
321
+ flags |= jimp.HORIZONTAL_ALIGN_LEFT;
322
+ break;
323
+ case 'center':
324
+ flags |= jimp.HORIZONTAL_ALIGN_CENTER;
325
+ break;
326
+ case 'right':
327
+ flags |= jimp.HORIZONTAL_ALIGN_RIGHT;
328
+ break;
329
+ }
330
+ switch (align[1]) {
331
+ case 'top':
332
+ flags |= jimp.VERTICAL_ALIGN_TOP;
333
+ break;
334
+ case 'middle':
335
+ flags |= jimp.VERTICAL_ALIGN_MIDDLE;
336
+ break;
337
+ case 'bottom':
338
+ flags |= jimp.VERTICAL_ALIGN_BOTTOM;
339
+ break;
340
+ }
341
+ switch (mode) {
342
+ case 'contain':
343
+ handler.contain(width, height, flags);
344
+ break;
345
+ case 'cover':
346
+ handler.cover(width, height, flags);
347
+ break;
348
+ case 'scale':
349
+ handler.scaleToFit(width, height);
350
+ break;
351
+ default:
352
+ handler.resize(width === Infinity ? jimp.AUTO : width, height === Infinity ? jimp.AUTO : height, resizeMode);
353
+ break;
340
354
  }
341
355
  }
342
356
  background(value) {
@@ -346,13 +360,13 @@ class JimpHandler {
346
360
  this.handler.background(value);
347
361
  }
348
362
  finalize(output, callback) {
349
- var _a;
350
363
  if (this.aborted) {
351
364
  return;
352
365
  }
353
366
  const instance = this.instance;
354
367
  if (instance.outputAs === 'webp' && path.extname(output).toLowerCase() !== '.webp') {
355
- const webp = (_a = instance.settings).webp || (_a.webp = {});
368
+ const settings = instance.settings;
369
+ const webp = settings.webp || (settings.webp = {});
356
370
  const data = instance.qualityData;
357
371
  const replace = instance.getCommand().indexOf('@') !== -1;
358
372
  const filename = (0, util_1.renameExt)(output, 'webp', replace);
@@ -399,7 +413,7 @@ class JimpHandler {
399
413
  }
400
414
  args.push('-o', (0, util_1.normalizePath)(filename));
401
415
  try {
402
- child_process.execFile((0, util_1.getWebP_bin)('cwebp', webp.path), args, { shell: true, signal: this.instance.signal }, err => {
416
+ child_process.execFile((0, util_1.getWebP_bin)('cwebp', webp.path), args, { shell: true, signal: this.instance.signal, ...execOptions(settings) }, err => {
403
417
  if (err) {
404
418
  this.instance.writeFail(["Unable to convert file" /* ERR_MESSAGE.CONVERT_FILE */, path.basename(filename)], err, 2048 /* LOG_TYPE.IMAGE */);
405
419
  }
@@ -537,67 +551,67 @@ class Jimp extends Image {
537
551
  static async transform(file, command, options = {}) {
538
552
  const [outputType, saveAs, finalAs] = (0, util_1.parseFormat)(command = command.trim(), options.mimeType);
539
553
  const empty = () => options.tempFile ? '' : null;
540
- if (outputType) {
541
- const instance = new Jimp(options.module);
542
- let buffer = null;
543
- if (Buffer.isBuffer(file)) {
544
- const tempDir = TEMP_DIR || instance.getTempDir();
545
- if (!this.createDir(tempDir)) {
546
- return empty();
547
- }
548
- try {
549
- const { ext } = await this.resolveMime(file) || { ext: 'unknown' };
550
- buffer = file;
551
- fs.writeFileSync(file = path.join(tempDir, (0, types_1.generateUUID)() + '.' + ext), buffer);
552
- }
553
- catch {
554
- return empty();
555
- }
556
- options.cache = false;
557
- }
558
- const filename = path.basename(file);
559
- const broadcastId = options.broadcastId;
560
- if (broadcastId) {
561
- if ((0, types_1.isPlainObject)(broadcastId)) {
562
- instance.broadcastId = broadcastId.value;
563
- if (broadcastId.stripAnsi === false) {
564
- instance.supports('stripAnsi', false);
565
- }
566
- }
567
- else {
568
- instance.broadcastId = broadcastId;
569
- }
554
+ if (!outputType) {
555
+ return empty();
556
+ }
557
+ const instance = new Jimp(options.module);
558
+ let buffer = null;
559
+ if (Buffer.isBuffer(file)) {
560
+ const tempDir = TEMP_DIR || instance.getTempDir();
561
+ if (!this.createDir(tempDir)) {
562
+ return empty();
570
563
  }
571
- const writeMessage = (failed, cTimeMs) => {
572
- if (cTimeMs || options.startTime) {
573
- formatMessage.call(instance, filename + (0, util_1.showOutputType)(options.mimeType, outputType, finalAs), options.startTime, failed, cTimeMs);
574
- }
575
- };
576
- let tempKey, tempFile;
577
- if (options.cache) {
578
- let ctimeMs;
579
- [buffer, tempFile, ctimeMs] = getImageCache.call(instance, tempKey = file + command + (options.mimeType || ''));
580
- if (buffer) {
581
- writeMessage(false, ctimeMs);
582
- return Promise.resolve(buffer);
583
- }
564
+ try {
565
+ const { ext } = await this.resolveMime(file) || { ext: 'unknown' };
566
+ buffer = file;
567
+ fs.writeFileSync(file = path.join(tempDir, (0, types_1.generateUUID)() + '.' + ext), buffer);
584
568
  }
585
- instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp" /* STRINGS.MODULE_NAME */, ["Transforming image..." /* STRINGS.TRANSFORM */, filename], command);
586
- Image.initCpuUsage(instance);
587
- return performCommand(null, instance, file, command, outputType, finalAs)
588
- .then(async (handler) => {
589
- const result = await handler.getBuffer(options.tempFile, saveAs);
590
- instance.flushLog();
591
- writeMessage(!result || instance.errors.length > 0);
592
- if (result && tempKey && tempFile) {
593
- setImageCache.call(instance, tempKey, tempFile, result, file);
594
- }
595
- return result;
596
- })
597
- .catch(() => empty())
598
- .finally(() => buffer && !options.cache && removeFile(file));
569
+ catch {
570
+ return empty();
571
+ }
572
+ options.cache = false;
599
573
  }
600
- return empty();
574
+ const filename = path.basename(file);
575
+ const broadcastId = options.broadcastId;
576
+ if (broadcastId) {
577
+ if ((0, types_1.isPlainObject)(broadcastId)) {
578
+ instance.broadcastId = broadcastId.value;
579
+ if (broadcastId.stripAnsi === false) {
580
+ instance.supports('stripAnsi', false);
581
+ }
582
+ }
583
+ else {
584
+ instance.broadcastId = broadcastId;
585
+ }
586
+ }
587
+ const writeMessage = (failed, cTimeMs) => {
588
+ if (cTimeMs || options.startTime) {
589
+ formatMessage.call(instance, filename + (0, util_1.showOutputType)(options.mimeType, outputType, finalAs), options.startTime, failed, cTimeMs);
590
+ }
591
+ };
592
+ let tempKey, tempFile;
593
+ if (options.cache) {
594
+ let ctimeMs;
595
+ [buffer, tempFile, ctimeMs] = getImageCache.call(instance, tempKey = file + command + (options.mimeType || ''));
596
+ if (buffer) {
597
+ writeMessage(false, ctimeMs);
598
+ return Promise.resolve(buffer);
599
+ }
600
+ }
601
+ instance.formatMessage(Image.LOG_TYPE.IMAGE, "jimp" /* STRINGS.MODULE_NAME */, ["Transforming image..." /* STRINGS.TRANSFORM */, filename], command);
602
+ Image.initCpuUsage(instance);
603
+ return performCommand(null, instance, file, command, outputType, finalAs)
604
+ .then(async (handler) => {
605
+ const result = await handler.getBuffer(options.tempFile, saveAs);
606
+ instance.flushLog();
607
+ writeMessage(!result || instance.errors.length > 0);
608
+ if (result && tempKey && tempFile) {
609
+ setImageCache.call(instance, tempKey, tempFile, result, file);
610
+ }
611
+ return result;
612
+ })
613
+ .catch(() => empty())
614
+ .finally(() => buffer && !options.cache && removeFile(file));
601
615
  }
602
616
  parseRotate(value) {
603
617
  const data = super.parseRotate(value);
@@ -614,7 +628,6 @@ class Jimp extends Image {
614
628
  return Promise.reject((0, types_1.createAbortError)());
615
629
  }
616
630
  return new Promise(async (resolve, reject) => {
617
- var _a;
618
631
  const { host, file } = data;
619
632
  const localUri = host.getLocalUri(data);
620
633
  const mimeType = host.getMimeType(data);
@@ -644,8 +657,9 @@ class Jimp extends Image {
644
657
  const startTime = process.hrtime();
645
658
  const success = (result, ctimeMs) => {
646
659
  const filename = path.basename(result);
647
- if (file.document) {
648
- host.writeImage(file.document, data.getObject({ command, output: result }));
660
+ const document = file.document;
661
+ if (document) {
662
+ host.writeImage(document, data.getObject({ command, output: result }));
649
663
  }
650
664
  if (host.getLocalUri(data) !== result) {
651
665
  if (command.indexOf('%') !== -1) {
@@ -756,11 +770,12 @@ class Jimp extends Image {
756
770
  const args = [(0, util_1.normalizePath)(target)];
757
771
  const quality = cmd.quality;
758
772
  if (quality) {
759
- if (!isNaN(quality.value)) {
760
- args.push('-q', quality.value.toString());
773
+ const { value, method } = quality;
774
+ if (!isNaN(value)) {
775
+ args.push('-q', value.toString());
761
776
  }
762
- if (!isNaN(quality.method)) {
763
- args.push('-m', quality.method.toString());
777
+ if (!isNaN(method)) {
778
+ args.push('-m', method.toString());
764
779
  }
765
780
  }
766
781
  if (Array.isArray(gif2webp)) {
@@ -789,7 +804,7 @@ class Jimp extends Image {
789
804
  }
790
805
  args.push('-o', (0, util_1.normalizePath)(webp));
791
806
  try {
792
- child_process.execFile((0, util_1.getWebP_bin)('gif2webp', webp_path), args, { shell: true, signal: this.signal }, (err, stdout) => {
807
+ child_process.execFile((0, util_1.getWebP_bin)('gif2webp', webp_path), args, { shell: true, signal: this.signal, ...execOptions(this.settings) }, (err, stdout) => {
793
808
  if (!err) {
794
809
  this.addLog(types_1.STATUS_TYPE.INFO, stdout);
795
810
  finalize(webp);
@@ -936,10 +951,11 @@ class Jimp extends Image {
936
951
  }
937
952
  return false;
938
953
  };
939
- const { path: webp_path } = (_a = this.settings).webp || (_a.webp = {});
954
+ const settings = this.settings;
955
+ const { path: webp_path } = settings.webp || (settings.webp = {});
940
956
  const bmpFile = getTempPath.call(this, 'bmp');
941
957
  try {
942
- child_process.execFile((0, util_1.getWebP_bin)('dwebp', webp_path), [(0, util_1.normalizePath)(localUri), '-bmp', '-o', (0, util_1.normalizePath)(bmpFile)], { shell: true, signal: this.signal }, err => {
958
+ child_process.execFile((0, util_1.getWebP_bin)('dwebp', webp_path), [(0, util_1.normalizePath)(localUri), '-bmp', '-o', (0, util_1.normalizePath)(bmpFile)], { shell: true, signal: this.signal, ...execOptions(settings) }, err => {
943
959
  if (!err) {
944
960
  transformBuffer(bmpFile);
945
961
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/jimp",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Jimp image constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "publishConfig": {
@@ -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.6.0",
24
+ "@e-mc/types": "^0.6.0",
25
25
  "bmp-js": "^0.1.0",
26
- "gifwrap": "^0.9.4",
27
- "jimp": "^0.22.8"
26
+ "gifwrap": "^0.10.1",
27
+ "jimp": "^0.22.10"
28
28
  }
29
29
  }