wasm-image-optimization 0.0.3 → 0.0.5

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/README.md +66 -9
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,35 +1,92 @@
1
1
  # wasm-image-optimization
2
2
 
3
+ ```ts
4
+ // quality: 1-100
5
+ optimizeImage({image: ArrayBuffer, width?: number, height?:number,quality?: number}): Promise<ArrayBuffer>
6
+ ```
7
+
8
+ - source format
9
+ - jpeg
10
+ - png
11
+ - webp
12
+ - svg
13
+ - output format
14
+ - webp
15
+
3
16
  # usage
4
17
 
5
- Sample of Cloudflare Workers
18
+ Next.js image optimization with Cloudflare
6
19
 
7
20
  ```ts
8
21
  import { optimizeImage } from 'wasm-image-optimization';
9
-
10
22
  export interface Env {}
11
23
 
24
+ const isValidUrl = (url: string) => {
25
+ try {
26
+ new URL(url);
27
+ return true;
28
+ } catch (err) {
29
+ return false;
30
+ }
31
+ };
32
+
12
33
  const handleRequest = async (
13
34
  request: Request,
14
- env: Env,
35
+ _env: Env,
15
36
  ctx: ExecutionContext
16
37
  ): Promise<Response> => {
17
- const srcImage = await fetch(
18
- 'https://raw.githubusercontent.com/node-libraries/wasm-image-optimization/master/images/test01.jpg'
19
- ).then((res) => res.arrayBuffer());
38
+ const url = new URL(request.url);
39
+ const params = url.searchParams;
40
+ const imageUrl = params.get('url');
41
+ if (!imageUrl || !isValidUrl(imageUrl)) {
42
+ return new Response('url is required', { status: 400 });
43
+ }
44
+ const cache = caches.default;
45
+ const cachedResponse = await cache.match(new Request(url.toString(), request));
46
+ if (cachedResponse) {
47
+ return cachedResponse;
48
+ }
49
+
50
+ const width = params.get('w');
51
+ const quality = params.get('q');
52
+
53
+ const srcImage = await fetch(imageUrl, { cf: { cacheKey: imageUrl } })
54
+ .then((res) => (res.ok ? res.arrayBuffer() : null))
55
+ .catch((e) => null);
56
+
57
+ if (!srcImage) {
58
+ return new Response('image not found', { status: 404 });
59
+ }
20
60
  const image = await optimizeImage({
21
61
  image: srcImage,
22
- width: 200,
23
- height: 200,
62
+ width: width ? parseInt(width) : undefined,
63
+ quality: quality ? parseInt(quality) : undefined,
24
64
  });
25
- return new Response(image, {
65
+ const response = new Response(image, {
26
66
  headers: {
27
67
  'Content-Type': 'image/webp',
68
+ 'Cache-Control': 'public, max-age=31536000, immutable',
28
69
  },
29
70
  });
71
+ ctx.waitUntil(cache.put(request, response.clone()));
72
+ return response;
30
73
  };
31
74
 
32
75
  export default {
33
76
  fetch: handleRequest,
34
77
  };
35
78
  ```
79
+
80
+ - next.config.js
81
+
82
+ ```js
83
+ /**
84
+ * @type { import("next").NextConfig}
85
+ */
86
+ const config = {
87
+ images: {
88
+ path: 'https://xxx.yyy.workers.dev/',
89
+ },
90
+ };
91
+ export default config;
92
+ ```
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "wasm-image-optimization",
3
3
  "description": "Optimize images with wasm on edge runtime",
4
- "version": "0.0.3",
4
+ "version": "0.0.5",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "types": "./dist/cjs/index.d.ts",
7
7
  "exports": {