1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! # flaron-sdk
//!
//! Rust SDK for building **flares** - Wasm functions that run on the
//! [Flaron][flaron] CDN edge. A flare receives an HTTP request (or WebSocket
//! event) at the nearest edge, runs your Rust code in a sandboxed Wasm
//! runtime, and returns a response with single-digit-millisecond latency.
//!
//! [flaron]: https://flaron.dev
//!
//! ## Quick start
//!
//! ```toml
//! # Cargo.toml
//! [package]
//! name = "my-flare"
//! version = "0.1.0"
//! edition = "2021"
//!
//! [lib]
//! crate-type = ["cdylib"]
//!
//! [dependencies]
//! flaron-sdk = "0.1"
//! ```
//!
//! ```ignore
//! // src/lib.rs
//! use flaron_sdk::{request, response, FlareAction};
//!
//! flaron_sdk::export_alloc!();
//!
//! #[no_mangle]
//! pub extern "C" fn handle_request() -> i64 {
//! flaron_sdk::reset_arena();
//!
//! let body = format!("hello from {} {}", request::method(), request::url());
//! response::set_status(200);
//! response::set_header("content-type", "text/plain");
//! response::set_body(body.as_bytes());
//!
//! FlareAction::Respond.to_i64()
//! }
//! ```
//!
//! Build with:
//!
//! ```sh
//! cargo build --release --target wasm32-unknown-unknown
//! ```
//!
//! Then deploy `target/wasm32-unknown-unknown/release/my_flare.wasm` to
//! Flaron via the dashboard or `flaronctl`.
//!
//! ## What's in the SDK
//!
//! | Module | What it does |
//! |-----------------------|-----------------------------------------------------------|
//! | [`request`] | Read inbound request (method, URL, headers, body) |
//! | [`response`] | Write outbound response (status, headers, body) |
//! | [`beam`] | Outbound HTTP from the edge ([`beam::fetch`]) |
//! | [`spark`] | Per-site KV with TTL, persisted to disk on the edge |
//! | [`plasma`] | Cross-edge CRDT KV - counters, presence, leaderboards |
//! | [`secrets`] | Read domain-scoped secrets allowlisted for this flare |
//! | [`crypto`] | Hash, HMAC, AES-GCM encrypt/decrypt, JWT signing, RNG |
//! | [`encoding`] | Base64, hex, URL encode/decode helpers |
//! | [`id`] | UUID v4/v7, ULID, KSUID, Nanoid, Snowflake generators |
//! | [`time`] | Timestamps in unix / ms / ns / RFC3339 / HTTP / ISO8601 |
//! | [`logging`] | Structured logs surfaced via the edge node's slog stream |
//! | [`ws`] | WebSocket: send, close, read events from open/message/close |
//!
//! ## Memory model
//!
//! Each flare invocation gets a fresh 256 KiB bump arena that the host writes
//! into via the guest's exported `alloc` function. The SDK resets the arena
//! at the top of every invocation so memory is reclaimed automatically - you
//! never need to free anything yourself. The [`export_alloc!`] macro wires
//! the export up; the [`handle_request!`] / [`ws_handlers!`] macros (TODO)
//! will reset the arena for you. Until those exist, call [`reset_arena`] at
//! the top of your exports.
pub
pub use ;
pub use ;
pub use ;
pub use PlasmaError;
pub use ;
pub use WsSendError;
/// Action returned by an `handle_request()` export to tell the host how to
/// proceed once the flare's body has run.
///
/// The flaron host runtime decodes this from the high 32 bits of the i64
/// return value (`(action << 32)` - produced by [`FlareAction::to_i64`]).
/// Export the guest `alloc` function the flaron host runtime requires.
///
/// Call this once at the crate root of your flare:
///
/// ```ignore
/// flaron_sdk::export_alloc!();
/// ```
///
/// This expands to a `#[no_mangle]` `extern "C" fn alloc(size: i32) -> i32`
/// that delegates to [`guest_alloc`]. Without it, every host function that
/// returns data to the guest will fail.