wasm-image-optimization 0.0.4 → 0.0.6

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,5 +1,27 @@
1
1
  # wasm-image-optimization
2
2
 
3
+ - Cloudflare workers
4
+ `import { optimizeImage } from 'wasm-image-optimization';`
5
+ - Edge runtime
6
+ `import { optimizeImage } from 'wasm-image-optimization/edge';`
7
+ - Node runtime
8
+ `import { optimizeImage } from 'wasm-image-optimization/node';`
9
+
10
+ - function
11
+
12
+ ```ts
13
+ // quality: 1-100
14
+ optimizeImage({image: ArrayBuffer, width?: number, height?:number,quality?: number}): Promise<ArrayBuffer>
15
+ ```
16
+
17
+ - source format
18
+ - jpeg
19
+ - png
20
+ - webp
21
+ - svg
22
+ - output format
23
+ - webp
24
+
3
25
  # usage
4
26
 
5
27
  Next.js image optimization with Cloudflare
@@ -8,30 +30,55 @@ Next.js image optimization with Cloudflare
8
30
  import { optimizeImage } from 'wasm-image-optimization';
9
31
  export interface Env {}
10
32
 
33
+ const isValidUrl = (url: string) => {
34
+ try {
35
+ new URL(url);
36
+ return true;
37
+ } catch (err) {
38
+ return false;
39
+ }
40
+ };
41
+
11
42
  const handleRequest = async (
12
43
  request: Request,
13
44
  _env: Env,
14
- _ctx: ExecutionContext
45
+ ctx: ExecutionContext
15
46
  ): Promise<Response> => {
16
- const params = new URL(request.url).searchParams;
17
- const url = params.get('url');
18
- if (!url) {
47
+ const url = new URL(request.url);
48
+ const params = url.searchParams;
49
+ const imageUrl = params.get('url');
50
+ if (!imageUrl || !isValidUrl(imageUrl)) {
19
51
  return new Response('url is required', { status: 400 });
20
52
  }
53
+ const cache = caches.default;
54
+ const cachedResponse = await cache.match(new Request(url.toString(), request));
55
+ if (cachedResponse) {
56
+ return cachedResponse;
57
+ }
58
+
21
59
  const width = params.get('w');
22
60
  const quality = params.get('q');
23
- const srcImage = await fetch(url).then((res) => res.arrayBuffer());
61
+
62
+ const srcImage = await fetch(imageUrl, { cf: { cacheKey: imageUrl } })
63
+ .then((res) => (res.ok ? res.arrayBuffer() : null))
64
+ .catch((e) => null);
65
+
66
+ if (!srcImage) {
67
+ return new Response('image not found', { status: 404 });
68
+ }
24
69
  const image = await optimizeImage({
25
70
  image: srcImage,
26
71
  width: width ? parseInt(width) : undefined,
27
72
  quality: quality ? parseInt(quality) : undefined,
28
73
  });
29
- return new Response(image, {
74
+ const response = new Response(image, {
30
75
  headers: {
31
76
  'Content-Type': 'image/webp',
32
77
  'Cache-Control': 'public, max-age=31536000, immutable',
33
78
  },
34
79
  });
80
+ ctx.waitUntil(cache.put(request, response.clone()));
81
+ return response;
35
82
  };
36
83
 
37
84
  export default {
package/dist/cjs/index.js CHANGED
@@ -1,9 +1,15 @@
1
- import LibImage from './libImage.js';
2
- import WASM from '../esm/libImage.wasm';
3
- const libImage = LibImage({
4
- instantiateWasm: async (imports, receiver) => {
5
- receiver(await WebAssembly.instantiate(WASM, imports));
6
- },
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.optimizeImage = void 0;
7
+ const libImage_1 = __importDefault(require("../workers/libImage"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const fs_1 = __importDefault(require("fs"));
10
+ const libImage = (0, libImage_1.default)({
11
+ wasmBinary: fs_1.default.readFileSync(path_1.default.resolve(__dirname, '../esm/libImage.wasm')),
7
12
  });
8
- export const optimizeImage = async ({ image, width = 0, height = 0, quality = 100, }) => libImage.then(({ optimize }) => optimize(image, width, height, quality));
13
+ const optimizeImage = async ({ image, width = 0, height = 0, quality = 100, }) => libImage.then(({ optimize }) => optimize(image, width, height, quality));
14
+ exports.optimizeImage = optimizeImage;
9
15
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cjs/index.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,eAAe,CAAC;AACrC,OAAO,IAAI,MAAM,sBAAsB,CAAC;AAExC,MAAM,QAAQ,GAAG,QAAQ,CAAC;IACxB,eAAe,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE;QAC3C,QAAQ,CAAC,MAAM,WAAW,CAAC,WAAW,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;IACzD,CAAC;CACF,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,EAAE,EAClC,KAAK,EACL,KAAK,GAAG,CAAC,EACT,MAAM,GAAG,CAAC,EACV,OAAO,GAAG,GAAG,GAMd,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC","sourcesContent":["import LibImage from './libImage.js';\nimport WASM from '../esm/libImage.wasm';\n\nconst libImage = LibImage({\n instantiateWasm: async (imports, receiver) => {\n receiver(await WebAssembly.instantiate(WASM, imports));\n },\n});\n\nexport const optimizeImage = async ({\n image,\n width = 0,\n height = 0,\n quality = 100,\n}: {\n image: BufferSource;\n width?: number;\n height?: number;\n quality?: number;\n}) => libImage.then(({ optimize }) => optimize(image, width, height, quality));\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cjs/index.ts"],"names":[],"mappings":";;;;;;AAAA,mEAA2C;AAC3C,gDAAwB;AACxB,4CAAoB;AAEpB,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,GAMd,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;AAVlE,QAAA,aAAa,iBAUqD","sourcesContent":["import LibImage from '../workers/libImage';\nimport path from 'path';\nimport fs from 'fs';\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}: {\n image: BufferSource;\n width?: number;\n height?: number;\n quality?: number;\n}) => libImage.then(({ optimize }) => optimize(image, width, height, quality));\n"]}