@pi-r/jimp 0.10.3 → 0.11.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pi-r/jimp",
3
- "version": "0.10.3",
4
- "description": "Jimp V0 image constructor for E-mc.",
3
+ "version": "0.11.1",
4
+ "description": "Jimp V1 image constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
7
7
  "publishConfig": {
@@ -9,7 +9,7 @@
9
9
  },
10
10
  "repository": {
11
11
  "type": "git",
12
- "url": "git+https://github.com/anpham6/pi-r.git",
12
+ "url": "git+https://github.com/anpham6/pi-r2.git",
13
13
  "directory": "src/module/jimp"
14
14
  },
15
15
  "keywords": [
@@ -18,12 +18,14 @@
18
18
  ],
19
19
  "author": "An Pham <anpham6@gmail.com>",
20
20
  "license": "MIT",
21
- "homepage": "https://github.com/anpham6/pi-r#readme",
21
+ "homepage": "https://github.com/anpham6/pi-r2#readme",
22
22
  "dependencies": {
23
- "@e-mc/image": "^0.12.7",
24
- "@e-mc/types": "^0.12.7",
23
+ "@e-mc/core": "^0.13.5",
24
+ "@e-mc/image": "^0.13.5",
25
+ "@e-mc/types": "^0.13.5",
26
+ "@jimp/utils": "^1.6.0",
25
27
  "bmp-js": "^0.1.0",
26
28
  "gifwrap": "^0.10.1",
27
- "jimp": "^0.22.12"
29
+ "jimp": "^1.6.0"
28
30
  }
29
31
  }
package/types/index.d.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  import type { IFileManager, IHost, IImage, ImageConstructor } from '@e-mc/types/lib';
2
2
  import type { ExternalAsset } from '@e-mc/types/lib/asset';
3
- import type { TransformOptions } from '@e-mc/types/lib/image';
3
+ import type { WorkerMessage } from '@e-mc/types/lib/core';
4
+ import type { CommandData, CropData, Dimension, Point, ResizeData, RotateData, TransformOptions } from '@e-mc/types/lib/image';
4
5
  import type { ImageModule, ImageSettings } from '@e-mc/types/lib/settings';
5
6
  import type { ExecAction } from '@e-mc/types/lib/module';
6
-
7
7
  import type { ImageHandler } from '@e-mc/image/types';
8
8
 
9
- import type * as jimp from 'jimp';
9
+ import type { JPEGOptions, JimpInstance } from 'jimp';
10
10
 
11
11
  export interface JimpSettings extends ImageSettings {
12
12
  jimp?: {
@@ -14,20 +14,64 @@ export interface JimpSettings extends ImageSettings {
14
14
  cache_expires?: number | string;
15
15
  rotate_clockwise?: boolean;
16
16
  gifwrap_quantize?: "dekker" | "sorokin" | "wu" | "none";
17
+ worker?: {
18
+ min?: number | string;
19
+ max?: number | string;
20
+ expires?: number | string;
21
+ };
22
+ options?: {
23
+ decode?: Record<string, AnyObject>;
24
+ encode?: Record<string, AnyObject>;
25
+ };
17
26
  };
18
27
  }
19
28
 
29
+ export interface JimpMessage<T = PlainObject> extends WorkerMessage<T, Buffer | string> {
30
+ commandData: CommandData;
31
+ outputType: string;
32
+ output?: string;
33
+ }
34
+
20
35
  export type ResultCallback<T = unknown, U = void, V = unknown> = (err: V, result: T) => U;
21
36
 
22
- export interface IJimpHandler<T extends IHost = IHost, U extends ImageModule = ImageModule> extends ImageHandler<jimp, T, IImage<T, U>, void, Promise<void>> {
23
- quality(): void;
24
- rotate(localFile?: string, callback?: ResultCallback<string>): Promise<this>;
37
+ export interface IJimpHandler<T extends IHost = IHost, U extends ImageModule = ImageModule<JimpSettings>, V = JimpInstance> extends ImageHandler<V, T, IImage<T, U>, Promise<void>, Promise<void>> {
38
+ rotate(localFile?: string, callback?: ResultCallback<string>): Promise<void>;
25
39
  background(value: number | [number, number, number, number]): void;
26
- writeAsync(output: string, callback?: ResultCallback): Promise<void>;
40
+ finalize(output: string, callback?: (err: unknown, result: string) => void, replace?: boolean): Promise<void>;
41
+ }
42
+
43
+ export interface IJimpImage<T extends IFileManager<U>, U extends ExternalAsset = ExternalAsset, V extends ImageModule = ImageModule<JimpSettings>> extends IImage<T, V> {
44
+ getEncodeOptions(): JPEGOptions | undefined;
27
45
  }
28
46
 
29
47
  export interface JimpImageConstructor<T extends IFileManager<U>, U extends ExternalAsset = ExternalAsset, V extends ImageModule = ImageModule<JimpSettings>> extends ConstructorDerived<ImageConstructor<T, V>> {
30
48
  transform<W extends TransformOptions>(file: string, command: string, options?: W): Promise<W extends { tempFile: true } ? string : Buffer | null>;
31
- readonly prototype: IImage<T, V>;
32
- new(module?: V, ...args: unknown[]): IImage<T, V>;
49
+ applyBackground(instance: JimpInstance, data: { color: number }): void;
50
+ applyResize(instance: JimpInstance, data: ResizeData): void;
51
+ applyCrop(instance: JimpInstance, data: CropData): void;
52
+ applyRotate(instance: JimpInstance, data: RotateData): void;
53
+ applyMethod(instance: JimpInstance, name: string, ...args: unknown[]): unknown[];
54
+ readonly prototype: IJimpImage<T, U, V>;
55
+ new(module?: V, ...args: unknown[]): IJimpImage<T, U, V>;
56
+ }
57
+
58
+ export interface WebpMuxFrame extends Dimension, Point {
59
+ delay: number;
60
+ }
61
+
62
+ export interface WebpMuxImage extends Readonly<Dimension> {
63
+ readonly anim: { loops: number; bgColor: [number, number, number, number] };
64
+ initLib(): Promise<void>;
65
+ load(value: Bufferable): Promise<void>;
66
+ demux(options: { path?: string; prefix?: string; buffers?: boolean }): Promise<void>;
67
+ getImageData(): Promise<Uint8Array>;
68
+ getFrameData(frame: number): Promise<Uint8Array>;
69
+ setFrameData(frame: number, source: Buffer, options: Dimension & { preset?: string; quality?: number; exact?: boolean; lossless?: number; method?: number }): Promise<void>;
70
+ save(path: string, options: Dimension & { bgColor?: number[] }): Promise<void>;
71
+ get hasAnim(): boolean;
72
+ get frames(): WebpMuxFrame[];
73
+ }
74
+
75
+ export interface WebpMux {
76
+ Image: new() => WebpMuxImage;
33
77
  }
package/util.d.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  declare namespace util {
2
- function parseFormat(command: string, mimeType?: string, gif?: boolean): [string, string, string];
2
+ function parseFormat(command: string, mimeType?: string): [string, string, string];
3
+ function detectExt(mimeType: string | undefined, webp?: boolean): string | undefined;
3
4
  function renameExt(output: string, ext: string, replace?: boolean): string;
4
5
  function normalizePath(value: string): string;
5
- /** @deprecated */
6
- function getWebP_bin(name: string, pathname: string | undefined): string;
7
6
  function importBinary(name: string, pathname: string | undefined): Promise<string>;
8
7
  function showInputType(value: string | undefined, outputType: string, finalAs: string): string;
9
8
  function showOutputType(value: string | undefined, outputType: string, finalAs: string): string;
package/util.js CHANGED
@@ -1,17 +1,10 @@
1
- "use strict";
2
- exports.MIME_OUTPUT = exports.MIME_INPUT = void 0;
3
- exports.parseFormat = parseFormat;
4
- exports.renameExt = renameExt;
5
- exports.normalizePath = normalizePath;
6
- exports.getWebP_bin = getWebP_bin;
7
- exports.importBinary = importBinary;
8
- exports.showInputType = showInputType;
9
- exports.showOutputType = showOutputType;
1
+ 'use strict';
2
+
10
3
  const path = require("node:path");
11
4
  const fs = require("node:fs");
12
5
  const types = require("@e-mc/types");
13
6
  const Image = require("@e-mc/image");
14
- exports.MIME_INPUT = new Set([
7
+ const MIME_INPUT = new Set([
15
8
  Image.MIME_PNG,
16
9
  Image.MIME_JPEG,
17
10
  Image.MIME_BMP,
@@ -19,49 +12,57 @@ exports.MIME_INPUT = new Set([
19
12
  Image.MIME_TIFF,
20
13
  Image.MIME_WEBP
21
14
  ]);
22
- exports.MIME_OUTPUT = new Set([
15
+ const MIME_OUTPUT = new Set([
23
16
  Image.MIME_PNG,
24
17
  Image.MIME_JPEG,
25
18
  Image.MIME_BMP,
26
19
  Image.MIME_GIF,
20
+ Image.MIME_TIFF,
27
21
  Image.MIME_WEBP
28
22
  ]);
29
- function parseFormat(command, mimeType, gif) {
23
+ function parseFormat(command, mimeType) {
30
24
  command = command.toLowerCase();
31
- for (let mime of exports.MIME_OUTPUT) {
25
+ for (let mime of MIME_OUTPUT) {
32
26
  let saveAs = mime.split('/')[1];
33
27
  if (command.startsWith(saveAs)) {
34
28
  let outputAs = '';
35
- if (saveAs !== 'gif') {
36
- switch (saveAs) {
37
- case 'jpeg':
38
- saveAs = 'jpg';
39
- break;
40
- case 'webp':
41
- if (mimeType === Image.MIME_JPEG) {
42
- mime = Image.MIME_JPEG;
43
- saveAs = 'jpg';
44
- }
45
- else if (gif && mimeType === Image.MIME_GIF) {
46
- mime = Image.MIME_GIF;
47
- saveAs = 'gif';
48
- }
49
- else {
50
- mime = Image.MIME_PNG;
51
- saveAs = 'png';
52
- }
53
- outputAs = 'webp';
54
- break;
55
- }
56
- }
57
- else if (!gif) {
58
- break;
29
+ switch (saveAs) {
30
+ case 'jpeg':
31
+ saveAs = 'jpg';
32
+ break;
33
+ case 'webp':
34
+ if (saveAs = detectExt(mimeType)) {
35
+ mime = mimeType;
36
+ }
37
+ else {
38
+ mime = Image.MIME_PNG;
39
+ saveAs = 'png';
40
+ }
41
+ outputAs = 'webp';
42
+ break;
59
43
  }
60
44
  return [mime, saveAs, outputAs];
61
45
  }
62
46
  }
63
47
  return ['', '', ''];
64
48
  }
49
+ function detectExt(mimeType, webp) {
50
+ switch (mimeType) {
51
+ case Image.MIME_JPEG:
52
+ return 'jpg';
53
+ case Image.MIME_GIF:
54
+ return 'gif';
55
+ case Image.MIME_TIFF:
56
+ return 'tiff';
57
+ case Image.MIME_BMP:
58
+ if (!webp) {
59
+ return 'bmp';
60
+ }
61
+ break;
62
+ case Image.MIME_PNG:
63
+ return 'png';
64
+ }
65
+ }
65
66
  function renameExt(output, ext, replace) {
66
67
  let result = types.renameExt(output.replace('.__copy__.', '.'), ext);
67
68
  if (!replace) {
@@ -74,15 +75,7 @@ function renameExt(output, ext, replace) {
74
75
  return result;
75
76
  }
76
77
  function normalizePath(value) {
77
- return '"' + value.replace(/"/g, '\\"') + '"';
78
- }
79
- function getWebP_bin(name, pathname) {
80
- if (pathname && fs.existsSync(pathname)) {
81
- name += Image.PLATFORM_WIN32 ? '.exe' : '';
82
- const bin = path.join(pathname, name);
83
- return types.sanitizeCmd(fs.existsSync(bin) ? bin : path.join(pathname, 'bin', name));
84
- }
85
- return require(name + '-bin');
78
+ return `"${value.replace(/"/g, '\\"')}"`;
86
79
  }
87
80
  async function importBinary(name, pathname) {
88
81
  if (pathname && fs.existsSync(pathname)) {
@@ -92,15 +85,25 @@ async function importBinary(name, pathname) {
92
85
  }
93
86
  return types.importESM(name + '-bin', true);
94
87
  }
95
- function showInputType(value, outputType, outputAs) {
96
- if (outputAs) {
97
- outputType = 'image/' + outputAs;
88
+ function showInputType(value, outputType, finalAs) {
89
+ if (finalAs) {
90
+ outputType = 'image/' + finalAs;
98
91
  }
99
92
  return value && outputType !== value ? value.split('/').pop() + ' -> ' : '';
100
93
  }
101
- function showOutputType(value, outputType, outputAs) {
102
- if (outputAs) {
103
- outputType = 'image/' + outputAs;
94
+ function showOutputType(value, outputType, finalAs) {
95
+ if (finalAs) {
96
+ outputType = 'image/' + finalAs;
104
97
  }
105
98
  return value !== outputType ? ' -> ' + outputType.split('/').pop() : '';
106
99
  }
100
+
101
+ exports.MIME_INPUT = MIME_INPUT;
102
+ exports.MIME_OUTPUT = MIME_OUTPUT;
103
+ exports.detectExt = detectExt;
104
+ exports.importBinary = importBinary;
105
+ exports.normalizePath = normalizePath;
106
+ exports.parseFormat = parseFormat;
107
+ exports.renameExt = renameExt;
108
+ exports.showInputType = showInputType;
109
+ exports.showOutputType = showOutputType;
package/worker/jimp.js ADDED
@@ -0,0 +1,64 @@
1
+ 'use strict';
2
+
3
+ var node_worker_threads = require('node:worker_threads');
4
+ var jimp = require('jimp');
5
+ var types = require('@e-mc/types');
6
+
7
+ const Image = require("@e-mc/image");
8
+ const JimpApp = require("@pi-r/jimp");
9
+ const PORT = node_worker_threads.workerData[0];
10
+ node_worker_threads.parentPort.on('message', (value) => {
11
+ const { data, commandData, output, options } = value;
12
+ jimp.Jimp.read(typeof data === 'string' ? data : Image.asBuffer(data))
13
+ .then(async (img) => {
14
+ const { method, resize, crop, rotate, opacity = -1 } = commandData;
15
+ if (method) {
16
+ for (const [name, args = []] of method) {
17
+ if (name === 'composite') {
18
+ const [src, x, y, opts] = args;
19
+ if (types.isString(src) && typeof x === 'number' && typeof y === 'number') {
20
+ img.composite(await jimp.Jimp.read(src), x, y, opts);
21
+ }
22
+ else {
23
+ throw types.errorMessage(name, "Invalid parameters", JSON.stringify(value));
24
+ }
25
+ }
26
+ else {
27
+ JimpApp.applyMethod(img, name, ...args);
28
+ }
29
+ }
30
+ }
31
+ if (resize) {
32
+ JimpApp.applyResize(img, resize);
33
+ }
34
+ if (crop) {
35
+ JimpApp.applyCrop(img, crop);
36
+ }
37
+ if (rotate) {
38
+ JimpApp.applyRotate(img, rotate);
39
+ }
40
+ if (opacity >= 0) {
41
+ img.opacity(opacity);
42
+ }
43
+ if (output) {
44
+ img.write(output, options)
45
+ .then(() => {
46
+ PORT.postMessage(output);
47
+ })
48
+ .catch((err) => {
49
+ console.error(err);
50
+ PORT.postMessage(null);
51
+ });
52
+ }
53
+ else {
54
+ void img.getBuffer(value.outputType, options)
55
+ .then(result => {
56
+ PORT.postMessage(result, [result.buffer]);
57
+ });
58
+ }
59
+ })
60
+ .catch((err) => {
61
+ console.error(err);
62
+ PORT.postMessage(null);
63
+ });
64
+ });