

GitHub - cloudflare/cloudflare-workers-wasm-demo
source link: https://github.com/cloudflare/cloudflare-workers-wasm-demo
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md
Cloudflare Workers WebAssembly Demo
This repository implements image resizing that runs on Cloudflare's edge network using WebAssembly in a Cloudflare Worker.
For this demo, we sought to minimize the dependencies of the WASM code, so that we could completely avoid the need for a C standard library. A more practical approach to building WASM applications would be to use Emscripten to provide a working library. However, as of this writing, Emscripten still needs some tweaks to correctly target Cloudflare Workers.
In order to minimize dependencies, we implement image encoding, decoding, and resizing using the stb library.
What's in this repository
main.c
-- The C code to be complied to WebAssembly.stb
-- Git submodule pointing to the stb library, which provides single-file implementations of image-handling operations.bootstrap.h
-- Minimal implementation of C library functions needed by stb.stubs
-- Fake, empty C header files, just to satisfy the#include
s in stb.worker.js
-- Cloudflare Worker JavaScript that loads and runs the WASM.worker-metadata.json
-- Used when uploading the worker via the API, to bind the worker to the WASM. Not needed when uploading via the UI.
How to build
- Install Clang.
- Build lld from source in order to get the
wasm-ld
tool. You may need to pass these flags on thecmake
command line:-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly -DLLVM_TARGETS_TO_BUILD=WebAssembly
make
How to deploy
Via the UI:
- Create a script in the "Workers" tab of the Cloudflare dashboard.
- Copy the contents of
worker.js
into the script editor. - Click on
Resources
at the top of the script editor. - Create a WebAssembly binding named
RESIZER_WASM
. - Upload
resizer.wasm
to this binding. - Save the script.
- Create a route where your script will run.
- Now any image file under that route will accept a
?width=
query parameter to downscale the image.
Via the API -- free/pro/biz (single-script) users -- THIS WILL REPLACE YOUR EXISTING SCRIPT:
curl -X PUT -H "X-Auth-Email: $API_EMAIL" -H "X-Auth-Key: $API_KEY" \
"https://api.cloudflare.com/client/v4/zones/$ZONE_ID/workers/script" \
-F [email protected] \
-F [email protected] \
-F [email protected]
Via the API -- enterprise (multi-script) users -- creates a script named "wasm-demo":
curl -X PUT -H "X-Auth-Email: $API_EMAIL" -H "X-Auth-Key: $API_KEY" \
"https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/workers/scripts/wasm-demo" \
-F [email protected] \
-F [email protected] \
-F [email protected]
Recommend
-
92
Photo of Indian Spices , by...
-
62
A few months ago we released a new way for people to run serverless Javascript called Cloudflare Workers. We believe Workers is the fastest way to execute serverless functions. If it is truely the fastest, and it...
-
69
Hello! For the last 6 months, I’ve had a problem on this blog where every so often a page would show up like this: Instead of...
-
3
Bypass Cloudflare General Related to my Medium post: How to bypass Cloudflare bot protection Detailed operation
-
41
Miniflare Miniflare is a simulator for developing and testing Cloudflare...
-
4
Reality check for Cloudflare Wasm Workers and Rust Published on: September 17, 2021With native Rust support recently announced for Cloudflare Workers, I wanted to...
-
8
Create Serverless APIs using Cloudflare Workers, Durable Objects & Wrangler Features Easy routing & state management Basic JWT-based Auth (register, login, refresh token, dele...
-
12
cf-workers-telegram-bot serverless telegram bot on cf workers The original worker.js is the content of Nikhil John's https://github.com/nikh...
-
5
How Rust and Wasm power Cloudflare's 1.1.1.1 02/28/2023
-
6
Wasm core dumps and debugging Rust in Cloudflare Workers 08/14/2023
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK