wasm-image-optimization 1.0.3 → 1.1.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,13 +1,10 @@
1
- # wasm-image-optimization
2
-
3
- - The 'avif' version is available here
4
- https://www.npmjs.com/package/wasm-image-optimization-avif
1
+ # wasm-image-optimization-avif
5
2
 
6
3
  - Cloudflare workers
7
4
  `import { optimizeImage } from 'wasm-image-optimization';`
8
5
  - Next.js(Webpack)
9
6
  `import { optimizeImage } from 'wasm-image-optimization/next';`
10
- - ESM(import base)
7
+ - ESM(import base) & Deno Deploy
11
8
  `import { optimizeImage } from 'wasm-image-optimization/esm';`
12
9
  - CJS(file base)
13
10
  `import { optimizeImage } from 'wasm-image-optimization/node';`
@@ -16,7 +13,10 @@
16
13
 
17
14
  ```ts
18
15
  // quality: 1-100
19
- optimizeImage({image: ArrayBuffer, width?: number, height?:number,quality?: number,format?: "png" | "jpeg" | "webp"}): Promise<ArrayBuffer>
16
+ optimizeImage({image: ArrayBuffer, width?: number, height?:number,quality?: number,format?: "png" | "jpeg" | "avif" | "webp"}): Promise<ArrayBuffer>
17
+
18
+ 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;}>
19
+
20
20
  ```
21
21
 
22
22
  - source format
@@ -28,27 +28,13 @@ optimizeImage({image: ArrayBuffer, width?: number, height?:number,quality?: numb
28
28
  - output format
29
29
  - jpeg
30
30
  - png
31
- - webp(default)
31
+ - avif(default)
32
+ - webp
32
33
 
33
34
  # usage
34
35
 
35
36
  ## Next.js image optimization with Cloudflare
36
37
 
37
- ### Parameters
38
-
39
- | Name | Type | Description |
40
- | ---- | ------ | -------------------------- |
41
- | url | string | Image URL |
42
- | q | number | Quality |
43
- | w | number | Width |
44
- | type | string | Output type(webp/png/jpeg) |
45
-
46
- https://github.com/SoraKumo001/cloudflare-workers-image-optimization
47
-
48
- https://xxx.yyy.workers.dev/?url=https://xxx.png&q=80&w=200
49
-
50
- ### Source code
51
-
52
38
  ```ts
53
39
  import { optimizeImage } from "wasm-image-optimization";
54
40
 
@@ -66,13 +52,6 @@ const handleRequest = async (
66
52
  _env: {},
67
53
  ctx: ExecutionContext
68
54
  ): Promise<Response> => {
69
- const url = new URL(request.url);
70
- const params = url.searchParams;
71
- const type = ["webp", "png", "jpeg"].find((v) => v === params.get("type")) as
72
- | "webp"
73
- | "png"
74
- | "jpeg"
75
- | undefined;
76
55
  const accept = request.headers.get("accept");
77
56
  const isWebp =
78
57
  accept
@@ -80,7 +59,16 @@ const handleRequest = async (
80
59
  .map((format) => format.trim())
81
60
  .some((format) => ["image/webp", "*/*", "image/*"].includes(format)) ??
82
61
  true;
62
+ const isAvif =
63
+ accept
64
+ ?.split(",")
65
+ .map((format) => format.trim())
66
+ .some((format) => ["image/avif", "*/*", "image/*"].includes(format)) ??
67
+ true;
83
68
 
69
+ const url = new URL(request.url);
70
+
71
+ const params = url.searchParams;
84
72
  const imageUrl = params.get("url");
85
73
  if (!imageUrl || !isValidUrl(imageUrl)) {
86
74
  return new Response("url is required", { status: 400 });
@@ -122,8 +110,13 @@ const handleRequest = async (
122
110
  return response;
123
111
  }
124
112
 
125
- const format =
126
- type ?? (isWebp ? "webp" : contentType === "image/jpeg" ? "jpeg" : "png");
113
+ const format = isAvif
114
+ ? "avif"
115
+ : isWebp
116
+ ? "webp"
117
+ : contentType === "image/jpeg"
118
+ ? "jpeg"
119
+ : "png";
127
120
  const image = await optimizeImage({
128
121
  image: srcImage,
129
122
  width: width ? parseInt(width) : undefined,
@@ -164,19 +157,14 @@ export default config;
164
157
 
165
158
  ### Parameters
166
159
 
167
- | Name | Type | Description |
168
- | ---- | ------ | -------------------------- |
169
- | url | string | Image URL |
170
- | q | number | Quality |
171
- | w | number | Width |
172
- | type | string | Output type(webp/png/jpeg) |
160
+ | Name | Type | Description |
161
+ | ---- | ------ | ----------- |
162
+ | url | string | Image URL |
163
+ | q | number | Quality |
164
+ | w | number | Width |
173
165
 
174
166
  https://xxxx.deno.dev/?url=https://xxx.png&q=80&w=200
175
167
 
176
- ### Source code
177
-
178
- https://github.com/SoraKumo001/deno-wasm-image-optimization
179
-
180
168
  ```ts
181
169
  import { optimizeImage } from "npm:wasm-image-optimization/esm";
182
170
 
@@ -189,35 +177,46 @@ const isValidUrl = (url: string) => {
189
177
  }
190
178
  };
191
179
 
192
- const cache = await caches.open("image-cache");
180
+ const isType = (accept: string | null, type: string) => {
181
+ return (
182
+ accept
183
+ ?.split(",")
184
+ .map((format) => format.trim())
185
+ .some((format) => [`image/${type}`, "*/*", "image/*"].includes(format)) ??
186
+ true
187
+ );
188
+ };
193
189
 
194
190
  Deno.serve(async (request) => {
191
+ const url = new URL(request.url);
192
+ const params = url.searchParams;
193
+ const type = ["avif", "webp", "png", "jpeg"].find(
194
+ (v) => v === params.get("type")
195
+ ) as "avif" | "webp" | "png" | "jpeg" | undefined;
196
+ const accept = request.headers.get("accept");
197
+ const isAvif = isType(accept, "avif");
198
+ const isWebp = isType(accept, "webp");
199
+
200
+ const cache = await caches.open(
201
+ `image-${isAvif ? "-avif" : ""}${isWebp ? "-webp" : ""}`
202
+ );
203
+
195
204
  const cached = await cache.match(request);
196
205
  if (cached) {
197
206
  return cached;
198
207
  }
199
208
 
200
- const url = new URL(request.url);
201
- const params = url.searchParams;
202
- const type = ["webp", "png", "jpeg"].find((v) => v === params.get("type")) as
203
- | "webp"
204
- | "png"
205
- | "jpeg"
206
- | undefined;
207
- const accept = request.headers.get("accept");
208
- const isWebp =
209
- accept
210
- ?.split(",")
211
- .map((format) => format.trim())
212
- .some((format) => ["image/webp", "*/*", "image/*"].includes(format)) ??
213
- true;
214
-
215
209
  const imageUrl = params.get("url");
216
210
  if (!imageUrl || !isValidUrl(imageUrl)) {
217
211
  return new Response("url is required", { status: 400 });
218
212
  }
219
213
 
220
- url.searchParams.append("webp", isWebp.toString());
214
+ if (isAvif) {
215
+ url.searchParams.append("avif", isAvif.toString());
216
+ } else if (isWebp) {
217
+ url.searchParams.append("webp", isWebp.toString());
218
+ }
219
+
221
220
  const cacheKey = new Request(url.toString());
222
221
  const cachedResponse = await cache.match(cacheKey);
223
222
  if (cachedResponse) {
@@ -251,7 +250,14 @@ Deno.serve(async (request) => {
251
250
  }
252
251
 
253
252
  const format =
254
- type ?? (isWebp ? "webp" : contentType === "image/jpeg" ? "jpeg" : "png");
253
+ type ??
254
+ (isAvif
255
+ ? "avif"
256
+ : isWebp
257
+ ? "webp"
258
+ : contentType === "image/jpeg"
259
+ ? "jpeg"
260
+ : "png");
255
261
  const image = await optimizeImage({
256
262
  image: srcImage,
257
263
  width: width ? parseInt(width) : undefined,
@@ -4,4 +4,17 @@ export declare const optimizeImage: ({ image, width, height, quality, format, }:
4
4
  height?: number;
5
5
  quality?: number;
6
6
  format?: "jpeg" | "png" | "webp" | "avif";
7
- }) => Promise<Uint8Array | null>;
7
+ }) => Promise<Uint8Array | undefined>;
8
+ export declare const optimizeImageExt: ({ image, width, height, quality, format, }: {
9
+ image: BufferSource;
10
+ width?: number;
11
+ height?: number;
12
+ quality?: number;
13
+ format?: "jpeg" | "png" | "webp" | "avif";
14
+ }) => Promise<{
15
+ data: Uint8Array;
16
+ originalWidth: number;
17
+ originalHeight: number;
18
+ width: number;
19
+ height: number;
20
+ } | null>;
package/dist/cjs/index.js CHANGED
@@ -3,13 +3,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.optimizeImage = void 0;
6
+ exports.optimizeImageExt = exports.optimizeImage = void 0;
7
7
  const fs_1 = __importDefault(require("fs"));
8
8
  const path_1 = __importDefault(require("path"));
9
9
  const libImage_1 = __importDefault(require("../workers/libImage"));
10
10
  const libImage = (0, libImage_1.default)({
11
11
  wasmBinary: fs_1.default.readFileSync(path_1.default.resolve(__dirname, "../esm/libImage.wasm")),
12
12
  });
13
- const optimizeImage = async ({ image, width = 0, height = 0, quality = 100, format = "webp", }) => libImage.then(({ optimize }) => optimize(image, width, height, quality, format));
13
+ const optimizeImage = async ({ image, width = 0, height = 0, quality = 100, format = "avif", }) => libImage.then(({ optimize }) => {
14
+ const result = optimize(image, width, height, quality, format);
15
+ return result?.data;
16
+ });
14
17
  exports.optimizeImage = optimizeImage;
18
+ const optimizeImageExt = async ({ image, width = 0, height = 0, quality = 100, format = "avif", }) => libImage.then(({ optimize }) => {
19
+ return optimize(image, width, height, quality, format);
20
+ });
21
+ exports.optimizeImageExt = optimizeImageExt;
15
22
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cjs/index.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAoB;AACpB,gDAAwB;AACxB,mEAA2C;AAE3C,MAAM,QAAQ,GAAG,IAAA,kBAAQ,EAAC;IACxB,UAAU,EAAE,YAAE,CAAC,YAAY,CAAC,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,sBAAsB,CAAC,CAAC;CAC7E,CAAC,CAAC;AAEI,MAAM,aAAa,GAAG,KAAK,EAAE,EAClC,KAAK,EACL,KAAK,GAAG,CAAC,EACT,MAAM,GAAG,CAAC,EACV,OAAO,GAAG,GAAG,EACb,MAAM,GAAG,MAAM,GAOhB,EAAE,EAAE,CACH,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,CAC7B,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAChD,CAAC;AAfS,QAAA,aAAa,iBAetB","sourcesContent":["import fs from \"fs\";\nimport path from \"path\";\nimport LibImage from \"../workers/libImage\";\n\nconst libImage = LibImage({\n wasmBinary: fs.readFileSync(path.resolve(__dirname, \"../esm/libImage.wasm\")),\n});\n\nexport const optimizeImage = async ({\n image,\n width = 0,\n height = 0,\n quality = 100,\n format = \"webp\",\n}: {\n image: BufferSource;\n width?: number;\n height?: number;\n quality?: number;\n format?: \"jpeg\" | \"png\" | \"webp\" | \"avif\";\n}) =>\n libImage.then(({ optimize }) =>\n optimize(image, width, height, quality, format)\n );\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cjs/index.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAoB;AACpB,gDAAwB;AACxB,mEAA2C;AAE3C,MAAM,QAAQ,GAAG,IAAA,kBAAQ,EAAC;IACxB,UAAU,EAAE,YAAE,CAAC,YAAY,CAAC,cAAI,CAAC,OAAO,CAAC,SAAS,EAAE,sBAAsB,CAAC,CAAC;CAC7E,CAAC,CAAC;AAEI,MAAM,aAAa,GAAG,KAAK,EAAE,EAClC,KAAK,EACL,KAAK,GAAG,CAAC,EACT,MAAM,GAAG,CAAC,EACV,OAAO,GAAG,GAAG,EACb,MAAM,GAAG,MAAM,GAOhB,EAAE,EAAE,CACH,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;IAC7B,MAAM,MAAM,GAAG,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IAC/D,OAAO,MAAM,EAAE,IAAI,CAAC;AACtB,CAAC,CAAC,CAAC;AAhBQ,QAAA,aAAa,iBAgBrB;AAEE,MAAM,gBAAgB,GAAG,KAAK,EAAE,EACrC,KAAK,EACL,KAAK,GAAG,CAAC,EACT,MAAM,GAAG,CAAC,EACV,OAAO,GAAG,GAAG,EACb,MAAM,GAAG,MAAM,GAOhB,EAAE,EAAE,CACH,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;IAC7B,OAAO,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;AACzD,CAAC,CAAC,CAAC;AAfQ,QAAA,gBAAgB,oBAexB","sourcesContent":["import fs from \"fs\";\nimport path from \"path\";\nimport LibImage from \"../workers/libImage\";\n\nconst libImage = LibImage({\n wasmBinary: fs.readFileSync(path.resolve(__dirname, \"../esm/libImage.wasm\")),\n});\n\nexport const optimizeImage = async ({\n image,\n width = 0,\n height = 0,\n quality = 100,\n format = \"avif\",\n}: {\n image: BufferSource;\n width?: number;\n height?: number;\n quality?: number;\n format?: \"jpeg\" | \"png\" | \"webp\" | \"avif\";\n}) =>\n libImage.then(({ optimize }) => {\n const result = optimize(image, width, height, quality, format);\n return result?.data;\n });\n\nexport const optimizeImageExt = async ({\n image,\n width = 0,\n height = 0,\n quality = 100,\n format = \"avif\",\n}: {\n image: BufferSource;\n width?: number;\n height?: number;\n quality?: number;\n format?: \"jpeg\" | \"png\" | \"webp\" | \"avif\";\n}) =>\n libImage.then(({ optimize }) => {\n return optimize(image, width, height, quality, format);\n });\n"]}
@@ -4,4 +4,17 @@ export declare const optimizeImage: ({ image, width, height, quality, format, }:
4
4
  height?: number;
5
5
  quality?: number;
6
6
  format?: "jpeg" | "png" | "webp" | "avif";
7
- }) => Promise<Uint8Array | null>;
7
+ }) => Promise<Uint8Array | undefined>;
8
+ export declare const optimizeImageExt: ({ image, width, height, quality, format, }: {
9
+ image: BufferSource;
10
+ width?: number;
11
+ height?: number;
12
+ quality?: number;
13
+ format?: "jpeg" | "png" | "webp" | "avif";
14
+ }) => Promise<{
15
+ data: Uint8Array;
16
+ originalWidth: number;
17
+ originalHeight: number;
18
+ width: number;
19
+ height: number;
20
+ } | null>;
package/dist/esm/index.js CHANGED
@@ -1,4 +1,10 @@
1
1
  import LibImage from "./libImage.js";
2
2
  const libImage = LibImage();
3
- export const optimizeImage = async ({ image, width = 0, height = 0, quality = 100, format = "webp", }) => libImage.then(({ optimize }) => optimize(image, width, height, quality, format));
3
+ export const optimizeImage = async ({ image, width = 0, height = 0, quality = 100, format = "webp", }) => libImage.then(({ optimize }) => {
4
+ const result = optimize(image, width, height, quality, format);
5
+ return result?.data;
6
+ });
7
+ export const optimizeImageExt = async ({ image, width = 0, height = 0, quality = 100, format = "avif", }) => libImage.then(({ optimize }) => {
8
+ return optimize(image, width, height, quality, format);
9
+ });
4
10
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/esm/index.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,eAAe,CAAC;AAErC,MAAM,QAAQ,GAAG,QAAQ,EAAE,CAAC;AAE5B,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,EAAE,EAClC,KAAK,EACL,KAAK,GAAG,CAAC,EACT,MAAM,GAAG,CAAC,EACV,OAAO,GAAG,GAAG,EACb,MAAM,GAAG,MAAM,GAOhB,EAAE,EAAE,CACH,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,CAC7B,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAChD,CAAC","sourcesContent":["import LibImage from \"./libImage.js\";\n\nconst libImage = LibImage();\n\nexport const optimizeImage = async ({\n image,\n width = 0,\n height = 0,\n quality = 100,\n format = \"webp\",\n}: {\n image: BufferSource;\n width?: number;\n height?: number;\n quality?: number;\n format?: \"jpeg\" | \"png\" | \"webp\" | \"avif\";\n}) =>\n libImage.then(({ optimize }) =>\n optimize(image, width, height, quality, format)\n );\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/esm/index.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,eAAe,CAAC;AAErC,MAAM,QAAQ,GAAG,QAAQ,EAAE,CAAC;AAE5B,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,EAAE,EAClC,KAAK,EACL,KAAK,GAAG,CAAC,EACT,MAAM,GAAG,CAAC,EACV,OAAO,GAAG,GAAG,EACb,MAAM,GAAG,MAAM,GAOhB,EAAE,EAAE,CACH,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;IAC7B,MAAM,MAAM,GAAG,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;IAC/D,OAAO,MAAM,EAAE,IAAI,CAAC;AACtB,CAAC,CAAC,CAAC;AAEL,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,EAAE,EACrC,KAAK,EACL,KAAK,GAAG,CAAC,EACT,MAAM,GAAG,CAAC,EACV,OAAO,GAAG,GAAG,EACb,MAAM,GAAG,MAAM,GAOhB,EAAE,EAAE,CACH,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE;IAC7B,OAAO,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;AACzD,CAAC,CAAC,CAAC","sourcesContent":["import LibImage from \"./libImage.js\";\n\nconst libImage = LibImage();\n\nexport const optimizeImage = async ({\n image,\n width = 0,\n height = 0,\n quality = 100,\n format = \"webp\",\n}: {\n image: BufferSource;\n width?: number;\n height?: number;\n quality?: number;\n format?: \"jpeg\" | \"png\" | \"webp\" | \"avif\";\n}) =>\n libImage.then(({ optimize }) => {\n const result = optimize(image, width, height, quality, format);\n return result?.data;\n });\n\nexport const optimizeImageExt = async ({\n image,\n width = 0,\n height = 0,\n quality = 100,\n format = \"avif\",\n}: {\n image: BufferSource;\n width?: number;\n height?: number;\n quality?: number;\n format?: \"jpeg\" | \"png\" | \"webp\" | \"avif\";\n}) =>\n libImage.then(({ optimize }) => {\n return optimize(image, width, height, quality, format);\n });\n"]}