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.
- package/README.md +44 -6
- 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
|
-
|
|
36
|
+
ctx: ExecutionContext
|
|
15
37
|
): Promise<Response> => {
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 {
|