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 +53 -6
- package/dist/cjs/index.js +13 -7
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/libImage.js +1 -1
- package/dist/esm/libImage.wasm +0 -0
- package/dist/workers/index.d.ts +6 -0
- package/dist/workers/index.js +9 -0
- package/dist/workers/index.js.map +1 -0
- package/dist/{cjs → workers}/libImage.js +1 -1
- package/package.json +26 -12
- package/tsconfig.csj.json +7 -0
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
|
-
|
|
45
|
+
ctx: ExecutionContext
|
|
15
46
|
): Promise<Response> => {
|
|
16
|
-
const
|
|
17
|
-
const
|
|
18
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
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
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/cjs/index.ts"],"names":[],"mappings":"AAAA,
|
|
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"]}
|