forked from awaitlink/qr-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8386990
Showing
9 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/target | ||
**/*.rs.bk | ||
Cargo.lock | ||
bin/ | ||
pkg/ | ||
wasm-pack.log | ||
worker/generated/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
[package] | ||
name = "qr" | ||
version = "0.1.0" | ||
authors = ["Artem Varaksa <[email protected]>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[features] | ||
default = ["console_error_panic_hook"] | ||
|
||
[dependencies] | ||
qrcode-generator = "1.0" | ||
url = "2.1" | ||
cfg-if = "0.1.2" | ||
wasm-bindgen = "0.2" | ||
|
||
# The `console_error_panic_hook` crate provides better debugging of panics by | ||
# logging them with `console.error`. This is great for development, but requires | ||
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for | ||
# code size when deploying. | ||
console_error_panic_hook = { version = "0.1.1", optional = true } | ||
|
||
# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size | ||
# compared to the default allocator's ~10K. It is slower than the default | ||
# allocator, however. | ||
wee_alloc = { version = "0.4.2", optional = true } | ||
|
||
[dev-dependencies] | ||
wasm-bindgen-test = "0.2" | ||
|
||
[profile.release] | ||
# Tell `rustc` to optimize for small code size. | ||
opt-level = "s" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# 👷♀️🦀🕸️ `rustwasm-worker-template` | ||
|
||
A template for kick starting a Cloudflare worker project using | ||
[`wasm-pack`](https://github.com/rustwasm/wasm-pack). | ||
|
||
This template is designed for compiling Rust libraries into WebAssembly and | ||
publishing the resulting worker to Cloudflare's worker infrastructure. | ||
|
||
## 🔋 Batteries Included | ||
|
||
* [`wasm-bindgen`](https://github.com/rustwasm/wasm-bindgen) for communicating | ||
between WebAssembly and JavaScript. | ||
* [`console_error_panic_hook`](https://github.com/rustwasm/console_error_panic_hook) | ||
for logging panic messages to the developer console. | ||
* [`wee_alloc`](https://github.com/rustwasm/wee_alloc), an allocator optimized | ||
for small code size. | ||
|
||
## 🚴 Usage | ||
|
||
### 🐑 Use `wrangler generate` to Clone this Template | ||
|
||
[Learn more about `wrangler generate` here.](https://github.com/cloudflare/wrangler) | ||
|
||
``` | ||
wrangler generate wasm-worker https://github.com/cloudflare/rustwasm-worker-template.git | ||
cd wasm-worker | ||
``` | ||
|
||
### 🛠️ Build with `wasm-pack build` | ||
|
||
``` | ||
wasm-pack build | ||
``` | ||
|
||
### 🔬 Test in Headless Browsers with `wasm-pack test` | ||
|
||
``` | ||
wasm-pack test --headless --firefox | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
mod utils; | ||
|
||
use cfg_if::cfg_if; | ||
use qrcode_generator::QrCodeEcc; | ||
use url::Url; | ||
use wasm_bindgen::prelude::*; | ||
|
||
cfg_if! { | ||
// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global | ||
// allocator. | ||
if #[cfg(feature = "wee_alloc")] { | ||
extern crate wee_alloc; | ||
#[global_allocator] | ||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; | ||
} | ||
} | ||
|
||
const DEFAULT_SIZE: usize = 512; | ||
const DEFAULT_ECC: QrCodeEcc = QrCodeEcc::Low; | ||
|
||
#[wasm_bindgen] | ||
pub fn handle_request(url: String) -> String { | ||
let url = Url::parse(&url).unwrap(); | ||
|
||
let text = match url.query() { | ||
Some(text) => text.to_owned(), | ||
None => url.clone().into_string(), | ||
}; | ||
|
||
let result = qrcode_generator::to_svg_to_string( | ||
text, | ||
match url.path_segments().unwrap().nth(1) { | ||
Some("l") => QrCodeEcc::Low, | ||
Some("m") => QrCodeEcc::Medium, | ||
Some("q") => QrCodeEcc::Quartile, | ||
Some("h") => QrCodeEcc::High, | ||
Some(_) => DEFAULT_ECC, | ||
None => DEFAULT_ECC, | ||
}, | ||
match url.path_segments().unwrap().nth(0) { | ||
Some(size_str) => match size_str.parse() { | ||
Ok(size) => size, | ||
Err(_) => DEFAULT_SIZE, | ||
}, | ||
None => DEFAULT_SIZE, | ||
}, | ||
None, | ||
) | ||
.unwrap(); | ||
|
||
format!("{}", result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use cfg_if::cfg_if; | ||
|
||
cfg_if! { | ||
// When the `console_error_panic_hook` feature is enabled, we can call the | ||
// `set_panic_hook` function at least once during initialization, and then | ||
// we will get better error messages if our code ever panics. | ||
// | ||
// For more details see | ||
// https://github.com/rustwasm/console_error_panic_hook#readme | ||
if #[cfg(feature = "console_error_panic_hook")] { | ||
extern crate console_error_panic_hook; | ||
pub use self::console_error_panic_hook::set_once as set_panic_hook; | ||
} else { | ||
#[inline] | ||
pub fn set_panic_hook() {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
//! Test suite for the Web and headless browsers. | ||
#![cfg(target_arch = "wasm32")] | ||
|
||
use wasm_bindgen_test::*; | ||
|
||
wasm_bindgen_test_configure!(run_in_browser); | ||
|
||
#[wasm_bindgen_test] | ||
fn pass() { | ||
assert_eq!(1 + 1, 2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"body_part": "script", | ||
"bindings": [ | ||
{ | ||
"name": "wasm", | ||
"type": "wasm_module", | ||
"part": "wasmprogram" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
addEventListener('fetch', event => { | ||
event.respondWith(handleRequest(event.request)); | ||
}); | ||
|
||
async function handleRequest(request) { | ||
const { handle_request } = wasm_bindgen; | ||
await wasm_bindgen(wasm); | ||
const output = handle_request(request.url); | ||
|
||
let res = new Response(output, { status: 200 }); | ||
res.headers.set('Content-type', 'image/svg+xml'); | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
name = "qr" | ||
type = "rust" | ||
zone_id = "" | ||
private = false | ||
account_id = "" | ||
route = "" |