wasm-image-optimization 0.0.4 → 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 +44 -6
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,5 +1,18 @@
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
18
  Next.js image optimization with Cloudflare
@@ -8,30 +21,55 @@ Next.js image optimization with Cloudflare
8
21
  import { optimizeImage } from 'wasm-image-optimization';
9
22
  export interface Env {}
10
23
 
24
+ const isValidUrl = (url: string) => {
25
+ try {
26
+ new URL(url);
27
+ return true;
28
+ } catch (err) {
29
+ return false;
30
+ }
31
+ };
32
+
11
33
  const handleRequest = async (
12
34
  request: Request,
13
35
  _env: Env,
14
- _ctx: ExecutionContext
36
+ ctx: ExecutionContext
15
37
  ): Promise<Response> => {
16
- const params = new URL(request.url).searchParams;
17
- const url = params.get('url');
18
- if (!url) {
38
+ const url = new URL(request.url);
39
+ const params = url.searchParams;
40
+ const imageUrl = params.get('url');
41
+ if (!imageUrl || !isValidUrl(imageUrl)) {
19
42
  return new Response('url is required', { status: 400 });
20
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
+
21
50
  const width = params.get('w');
22
51
  const quality = params.get('q');
23
- const srcImage = await fetch(url).then((res) => res.arrayBuffer());
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
+ }
24
60
  const image = await optimizeImage({
25
61
  image: srcImage,
26
62
  width: width ? parseInt(width) : undefined,
27
63
  quality: quality ? parseInt(quality) : undefined,
28
64
  });
29
- return new Response(image, {
65
+ const response = new Response(image, {
30
66
  headers: {
31
67
  'Content-Type': 'image/webp',
32
68
  'Cache-Control': 'public, max-age=31536000, immutable',
33
69
  },
34
70
  });
71
+ ctx.waitUntil(cache.put(request, response.clone()));
72
+ return response;
35
73
  };
36
74
 
37
75
  export default {
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.4",
4
+ "version": "0.0.5",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "types": "./dist/cjs/index.d.ts",
7
7
  "exports": {