wasm-image-optimization 1.1.8 → 1.2.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.
package/README.md CHANGED
@@ -1,23 +1,59 @@
1
1
  # wasm-image-optimization
2
2
 
3
+ ## Supported Environments
4
+
3
5
  - Cloudflare workers
4
6
  `import { optimizeImage } from 'wasm-image-optimization';`
5
- - Next.js(Webpack)
7
+ - Next.js (Webpack)
6
8
  `import { optimizeImage } from 'wasm-image-optimization/next';`
7
- - ESM(import base) & Deno Deploy
8
- `import { optimizeImage } from 'wasm-image-optimization/esm';`
9
- - CJS(file base)
10
- `import { optimizeImage } from 'wasm-image-optimization/node';`
11
- - Vite(Browser)
9
+ - ESM (import base) & Deno Deploy
10
+ `import { optimizeImage } from 'wasm-image-optimization';`
11
+ - Node.js
12
+ `import { optimizeImage } from 'wasm-image-optimization';`
13
+ - Node.js(Multi thread)
14
+ `import { optimizeImage } from 'wasm-image-optimization/node-worker';`
15
+ - Vite (Browser)
12
16
  `import { optimizeImage } from 'wasm-image-optimization/vite';`
17
+ - Web Worker (Browser) Multi process
18
+ `import { optimizeImage } from 'wasm-image-optimization/web-worker';`
19
+
20
+ ## WebWorker on Vite
13
21
 
14
- - function
22
+ - vite.config.ts
15
23
 
16
24
  ```ts
17
- // quality: 1-100
18
- optimizeImage({image: ArrayBuffer, width?: number, height?:number,quality?: number,format?: "png" | "jpeg" | "avif" | "webp"}): Promise<ArrayBuffer>
25
+ import wasmImageOptimizationPlugin from "wasm-image-optimization/vite-plugin";
19
26
 
20
- optimizeImageExt({image: ArrayBuffer, width?: number, height?:number,quality?: number,format?: "png" | "jpeg" | "avif" | "webp"}): Promise<{data: Uint8Array;originalWidth: number;originalHeight: number; width: number;height: number;}>
27
+ export default defineConfig(() => ({
28
+ plugins: [wasmImageOptimizationPlugin()],
29
+ }));
30
+ ```
31
+
32
+ ## function
33
+
34
+ ```ts
35
+
36
+ optimizeImage({
37
+ image: ArrayBuffer,
38
+ width?: number,
39
+ height?:number,
40
+ quality?: number, // quality: 1-100
41
+ format?: "png" | "jpeg" | "avif" | "webp"})
42
+ :Promise<
43
+ ArrayBuffer>
44
+
45
+ optimizeImageExt({
46
+ image: ArrayBuffer,
47
+ width?: number,
48
+ height?:number,
49
+ quality?: number,
50
+ format?: "png" | "jpeg" | "avif" | "webp"})
51
+ :Promise<{
52
+ data: Uint8Array;
53
+ originalWidth: number;
54
+ originalHeight: number;
55
+ width: number;
56
+ height: number;}>
21
57
 
22
58
  ```
23
59
 
@@ -33,9 +69,53 @@ optimizeImageExt({image: ArrayBuffer, width?: number, height?:number,quality?: n
33
69
  - webp
34
70
  - avif(default)
35
71
 
36
- # usage
72
+ ## usage
73
+
74
+ ### Node.js (CLI)
75
+
76
+ ```ts
77
+ import fs from "node:fs";
78
+ import {
79
+ optimizeImage,
80
+ waitAll,
81
+ close,
82
+ } from "wasm-image-optimization/node-worker";
83
+
84
+ const formats = ["webp", "jpeg", "png", "avif"] as const;
85
+
86
+ const main = async () => {
87
+ fs.mkdirSync("./image_output", { recursive: true });
88
+ const files = fs.readdirSync("./images");
89
+ for (const file of files) {
90
+ const image = fs.readFileSync(`./images/${file}`);
91
+ for (const format of formats) {
92
+ optimizeImage({
93
+ image,
94
+ quality: 100,
95
+ format,
96
+ width: 1000,
97
+ }).then((encoded) => {
98
+ console.log(
99
+ !!encoded,
100
+ file,
101
+ format,
102
+ encoded && `${Math.floor(encoded.length / 1024)}KB`
103
+ );
104
+ if (encoded) {
105
+ const fileName = file.split(".")[0];
106
+ fs.promises.writeFile(`image_output/${fileName}.${format}`, encoded);
107
+ }
108
+ });
109
+ }
110
+ }
111
+ await waitAll(); // Wait for all workers to finish before exiting the program.
112
+ close(); // Close all workers
113
+ console.log("exit");
114
+ };
115
+ main();
116
+ ```
37
117
 
38
- ## Next.js image optimization with Cloudflare
118
+ ### Next.js image optimization with Cloudflare
39
119
 
40
120
  ```ts
41
121
  import { optimizeImage } from "wasm-image-optimization";
@@ -155,9 +235,9 @@ const config = {
155
235
  export default config;
156
236
  ```
157
237
 
158
- ## Deno Deploy
238
+ ### Deno Deploy
159
239
 
160
- ### Parameters
240
+ #### Parameters
161
241
 
162
242
  | Name | Type | Description |
163
243
  | ---- | ------ | ----------- |
@@ -1 +1 @@
1
- {"version":3,"file":"optimizeImage.js","sourceRoot":"","sources":["../../../src/lib/optimizeImage.ts"],"names":[],"mappings":";;;AAUA,MAAM,MAAM,GAAG,CACb,MAAsD,EACtD,aAAyB,EACzB,EAAE;IACF,MAAM,CAAC,GAAG,MAAM;QACd,CAAC,CAAC,EAAE,GAAG,MAAM,EAAE,IAAI,EAAE,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE;QACnD,CAAC,CAAC,SAAS,CAAC;IACd,aAAa,EAAE,CAAC;IAChB,OAAO,CAAC,CAAC;AACX,CAAC,CAAC;AACK,MAAM,cAAc,GAAG,KAAK,EAAE,EACnC,KAAK,EACL,KAAK,GAAG,CAAC,EACT,MAAM,GAAG,CAAC,EACV,OAAO,GAAG,GAAG,EACb,MAAM,GAAG,MAAM,EACf,QAAQ,GAGT,EAAE,EAAE,CACH,IAAA,yBAAiB,EAAC,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC,IAAI,CACzE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,CACf,CAAC;AAZS,QAAA,cAAc,kBAYvB;AAEG,MAAM,iBAAiB,GAAG,KAAK,EAAE,EACtC,KAAK,EACL,KAAK,GAAG,CAAC,EACT,MAAM,GAAG,CAAC,EACV,OAAO,GAAG,GAAG,EACb,MAAM,GAAG,MAAM,EACf,QAAQ,GAGT,EAAE,EAAE,CACH,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,QAAQ,EAAE,aAAa,EAAE,EAAE,EAAE,CAC5C,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,aAAa,CAAC,CACvE,CAAC;AAZS,QAAA,iBAAiB,qBAY1B","sourcesContent":["import { type ModuleType } from \"../esm/libImage.js\";\n\nexport type OptimizeParams = {\n image: BufferSource | string; // The input image data\n width?: number; // The desired output width (optional)\n height?: number; // The desired output height (optional)\n quality?: number; // The desired output quality (0-100, optional)\n format?: \"jpeg\" | \"png\" | \"webp\" | \"avif\"; // The desired output format (optional)\n};\n\nconst result = (\n result: ReturnType<ModuleType[\"optimize\"]> | undefined,\n releaseResult: () => void\n) => {\n const r = result\n ? { ...result, data: Uint8Array.from(result.data) }\n : undefined;\n releaseResult();\n return r;\n};\nexport const _optimizeImage = async ({\n image,\n width = 0,\n height = 0,\n quality = 100,\n format = \"avif\",\n libImage,\n}: OptimizeParams & {\n libImage: Promise<ModuleType>;\n}) =>\n _optimizeImageExt({ image, width, height, quality, format, libImage }).then(\n (r) => r?.data\n );\n\nexport const _optimizeImageExt = async ({\n image,\n width = 0,\n height = 0,\n quality = 100,\n format = \"avif\",\n libImage,\n}: OptimizeParams & {\n libImage: Promise<ModuleType>;\n}) =>\n libImage.then(({ optimize, releaseResult }) =>\n result(optimize(image, width, height, quality, format), releaseResult)\n );\n"]}
1
+ {"version":3,"file":"optimizeImage.js","sourceRoot":"","sources":["../../../src/lib/optimizeImage.ts"],"names":[],"mappings":";;;AAUA,MAAM,MAAM,GAAG,CACb,MAAsD,EACtD,aAAyB,EACzB,EAAE;IACF,MAAM,CAAC,GAAG,MAAM;QACd,CAAC,CAAC,EAAE,GAAG,MAAM,EAAE,IAAI,EAAE,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE;QACnD,CAAC,CAAC,SAAS,CAAC;IACd,aAAa,EAAE,CAAC;IAChB,OAAO,CAAC,CAAC;AACX,CAAC,CAAC;AACK,MAAM,cAAc,GAAG,KAAK,EAAE,EACnC,KAAK,EACL,KAAK,GAAG,CAAC,EACT,MAAM,GAAG,CAAC,EACV,OAAO,GAAG,GAAG,EACb,MAAM,GAAG,MAAM,EACf,QAAQ,GAGT,EAAE,EAAE,CACH,IAAA,yBAAiB,EAAC,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC,IAAI,CACzE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,CACf,CAAC;AAZS,QAAA,cAAc,kBAYvB;AAEG,MAAM,iBAAiB,GAAG,KAAK,EAAE,EACtC,KAAK,EACL,KAAK,GAAG,CAAC,EACT,MAAM,GAAG,CAAC,EACV,OAAO,GAAG,GAAG,EACb,MAAM,GAAG,MAAM,EACf,QAAQ,GAGT,EAAE,EAAE,CACH,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,QAAQ,EAAE,aAAa,EAAE,EAAE,EAAE,CAC5C,MAAM,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,aAAa,CAAC,CACvE,CAAC;AAZS,QAAA,iBAAiB,qBAY1B","sourcesContent":["import { type ModuleType } from \"../esm/libImage.js\";\n\nexport type OptimizeParams = {\n image: BufferSource | string; // The input image data\n width?: number; // The desired output width (optional)\n height?: number; // The desired output height (optional)\n quality?: number; // The desired output quality (0-100, optional)\n format?: \"jpeg\" | \"png\" | \"webp\" | \"avif\"; // The desired output format (optional)\n};\n\nconst result = (\n result: ReturnType<ModuleType[\"optimize\"]> | undefined,\n releaseResult: () => void,\n) => {\n const r = result\n ? { ...result, data: Uint8Array.from(result.data) }\n : undefined;\n releaseResult();\n return r;\n};\nexport const _optimizeImage = async ({\n image,\n width = 0,\n height = 0,\n quality = 100,\n format = \"avif\",\n libImage,\n}: OptimizeParams & {\n libImage: Promise<ModuleType>;\n}) =>\n _optimizeImageExt({ image, width, height, quality, format, libImage }).then(\n (r) => r?.data,\n );\n\nexport const _optimizeImageExt = async ({\n image,\n width = 0,\n height = 0,\n quality = 100,\n format = \"avif\",\n libImage,\n}: OptimizeParams & {\n libImage: Promise<ModuleType>;\n}) =>\n libImage.then(({ optimize, releaseResult }) =>\n result(optimize(image, width, height, quality, format), releaseResult),\n );\n"]}
@@ -1,6 +1,6 @@
1
1
  declare const map: {
2
- optimizeImage: (params: import("../lib/optimizeImage.js").OptimizeParams) => Promise<Uint8Array<ArrayBuffer> | undefined>;
3
- optimizeImageExt: (params: import("../lib/optimizeImage.js").OptimizeParams) => Promise<{
2
+ optimizeImage: (params: import("../lib/optimizeImage").OptimizeParams) => Promise<Uint8Array<ArrayBuffer> | undefined>;
3
+ optimizeImageExt: (params: import("../lib/optimizeImage").OptimizeParams) => Promise<{
4
4
  data: Uint8Array<ArrayBuffer>;
5
5
  originalWidth: number;
6
6
  originalHeight: number;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const node_1 = require("worker-lib/node");
4
+ const _1 = require("./");
5
+ const map = (0, node_1.initWorker)({
6
+ optimizeImage: _1.optimizeImage,
7
+ optimizeImageExt: _1.optimizeImageExt,
8
+ });
9
+ //# sourceMappingURL=_node-worker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_node-worker.js","sourceRoot":"","sources":["../../../src/node/_node-worker.ts"],"names":[],"mappings":";;AAAA,0CAA6C;AAC7C,yBAAqD;AAErD,MAAM,GAAG,GAAG,IAAA,iBAAU,EAAC;IACrB,aAAa,EAAb,gBAAa;IACb,gBAAgB,EAAhB,mBAAgB;CACjB,CAAC,CAAC","sourcesContent":["import { initWorker } from \"worker-lib/node\";\nimport { optimizeImage, optimizeImageExt } from \"./\";\n\nconst map = initWorker({\n optimizeImage,\n optimizeImageExt,\n});\n\nexport type WorkerType = typeof map;\n"]}
@@ -0,0 +1,11 @@
1
+ import { type OptimizeParams } from "../lib/optimizeImage.js";
2
+ declare const waitAll: () => Promise<void>, close: () => void, setLimit: (limit: number) => void;
3
+ export declare const optimizeImage: (params: OptimizeParams) => Promise<Uint8Array<ArrayBuffer> | undefined>;
4
+ export declare const optimizeImageExt: (params: OptimizeParams) => Promise<{
5
+ data: Uint8Array<ArrayBuffer>;
6
+ originalWidth: number;
7
+ originalHeight: number;
8
+ width: number;
9
+ height: number;
10
+ } | undefined>;
11
+ export { waitAll, close, setLimit };
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.setLimit = exports.close = exports.waitAll = exports.optimizeImageExt = exports.optimizeImage = void 0;
7
+ const node_path_1 = __importDefault(require("node:path"));
8
+ const node_worker_threads_1 = require("node:worker_threads");
9
+ const node_1 = require("worker-lib/node");
10
+ const { execute, waitAll, close, setLimit } = (0, node_1.createWorker)(() => {
11
+ const url = node_path_1.default.resolve(__dirname, "./node-worker.js");
12
+ return new node_worker_threads_1.Worker(url);
13
+ });
14
+ exports.waitAll = waitAll;
15
+ exports.close = close;
16
+ exports.setLimit = setLimit;
17
+ const optimizeImage = async (params) => execute("optimizeImage", params);
18
+ exports.optimizeImage = optimizeImage;
19
+ const optimizeImageExt = async (params) => execute("optimizeImageExt", params);
20
+ exports.optimizeImageExt = optimizeImageExt;
21
+ //# sourceMappingURL=cjs-worker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cjs-worker.js","sourceRoot":"","sources":["../../../src/node/cjs-worker.ts"],"names":[],"mappings":";;;;;;AAAA,0DAA6B;AAC7B,6DAA6C;AAC7C,0CAA+C;AAQ/C,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,GAAG,IAAA,mBAAY,EAAa,GAAG,EAAE;IAC1E,MAAM,GAAG,GAAG,mBAAI,CAAC,OAAO,CAAC,SAAS,EAAE,kBAAkB,CAAC,CAAC;IACxD,OAAO,IAAI,4BAAM,CAAC,GAAG,CAAC,CAAC;AACzB,CAAC,CAAC,CAAC;AAQM,0BAAO;AAAE,sBAAK;AAAE,4BAAQ;AAN1B,MAAM,aAAa,GAAG,KAAK,EAAE,MAAsB,EAAE,EAAE,CAC5D,OAAO,CAAC,eAAe,EAAE,MAAM,CAAC,CAAC;AADtB,QAAA,aAAa,iBACS;AAE5B,MAAM,gBAAgB,GAAG,KAAK,EAAE,MAAsB,EAAE,EAAE,CAC/D,OAAO,CAAC,kBAAkB,EAAE,MAAM,CAAC,CAAC;AADzB,QAAA,gBAAgB,oBACS","sourcesContent":["import path from \"node:path\";\nimport { Worker } from \"node:worker_threads\";\nimport { createWorker } from \"worker-lib/node\";\nimport {\n _optimizeImage,\n _optimizeImageExt,\n type OptimizeParams,\n} from \"../lib/optimizeImage.js\";\nimport type { WorkerType } from \"./_node-worker.js\";\n\nconst { execute, waitAll, close, setLimit } = createWorker<WorkerType>(() => {\n const url = path.resolve(__dirname, \"./node-worker.js\");\n return new Worker(url);\n});\n\nexport const optimizeImage = async (params: OptimizeParams) =>\n execute(\"optimizeImage\", params);\n\nexport const optimizeImageExt = async (params: OptimizeParams) =>\n execute(\"optimizeImageExt\", params);\n\nexport { waitAll, close, setLimit };\n"]}
@@ -8,7 +8,7 @@ const fs_1 = __importDefault(require("fs"));
8
8
  const path_1 = __importDefault(require("path"));
9
9
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
10
10
  /* @ts-ignore */
11
- const libImage_js_1 = __importDefault(require("../../workers/libImage.js"));
11
+ const libImage_js_1 = __importDefault(require("../../cjs/libImage.js"));
12
12
  const optimizeImage_js_1 = require("../lib/optimizeImage.js");
13
13
  const libImage = (0, libImage_js_1.default)({
14
14
  wasmBinary: fs_1.default.readFileSync(path_1.default.resolve(__dirname, "../../esm/libImage.wasm")),
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/index.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAoB;AACpB,gDAAwB;AAExB,6DAA6D;AAC7D,gBAAgB;AAChB,4EAAiD;AACjD,8DAIiC;AAEjC,MAAM,QAAQ,GAAG,IAAA,qBAAQ,EAAC;IACxB,UAAU,EAAE,YAAE,CAAC,YAAY,CACzB,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,yBAAyB,CAAC,CAC1C;CACX,CAAC,CAAC;AAEI,MAAM,aAAa,GAAG,KAAK,EAAE,MAAsB,EAAE,EAAE,CAC5D,IAAA,iCAAc,EAAC,EAAE,GAAG,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;AAD7B,QAAA,aAAa,iBACgB;AAEnC,MAAM,gBAAgB,GAAG,KAAK,EAAE,MAAsB,EAAE,EAAE,CAC/D,IAAA,oCAAiB,EAAC,EAAE,GAAG,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;AADhC,QAAA,gBAAgB,oBACgB","sourcesContent":["import fs from \"fs\";\nimport path from \"path\";\n\n// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n/* @ts-ignore */\nimport LibImage from \"../../workers/libImage.js\";\nimport {\n _optimizeImage,\n _optimizeImageExt,\n type OptimizeParams,\n} from \"../lib/optimizeImage.js\";\n\nconst libImage = LibImage({\n wasmBinary: fs.readFileSync(\n path.resolve(__dirname, \"../../esm/libImage.wasm\")\n ) as never,\n});\n\nexport const optimizeImage = async (params: OptimizeParams) =>\n _optimizeImage({ ...params, libImage });\n\nexport const optimizeImageExt = async (params: OptimizeParams) =>\n _optimizeImageExt({ ...params, libImage });\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/index.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAoB;AACpB,gDAAwB;AAExB,6DAA6D;AAC7D,gBAAgB;AAChB,wEAA6C;AAC7C,8DAIiC;AAEjC,MAAM,QAAQ,GAAG,IAAA,qBAAQ,EAAC;IACxB,UAAU,EAAE,YAAE,CAAC,YAAY,CACzB,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,yBAAyB,CAAC,CAC1C;CACX,CAAC,CAAC;AAEI,MAAM,aAAa,GAAG,KAAK,EAAE,MAAsB,EAAE,EAAE,CAC5D,IAAA,iCAAc,EAAC,EAAE,GAAG,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;AAD7B,QAAA,aAAa,iBACgB;AAEnC,MAAM,gBAAgB,GAAG,KAAK,EAAE,MAAsB,EAAE,EAAE,CAC/D,IAAA,oCAAiB,EAAC,EAAE,GAAG,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;AADhC,QAAA,gBAAgB,oBACgB","sourcesContent":["import fs from \"fs\";\nimport path from \"path\";\n\n// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n/* @ts-ignore */\nimport LibImage from \"../../cjs/libImage.js\";\nimport {\n _optimizeImage,\n _optimizeImageExt,\n type OptimizeParams,\n} from \"../lib/optimizeImage.js\";\n\nconst libImage = LibImage({\n wasmBinary: fs.readFileSync(\n path.resolve(__dirname, \"../../esm/libImage.wasm\")\n ) as never,\n});\n\nexport const optimizeImage = async (params: OptimizeParams) =>\n _optimizeImage({ ...params, libImage });\n\nexport const optimizeImageExt = async (params: OptimizeParams) =>\n _optimizeImageExt({ ...params, libImage });\n"]}