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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//! # 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::handle_request!(my_flare);
//!
//! fn my_flare() -> FlareAction {
//! 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
//! }
//! ```
//!
//! 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 recommended entrypoint is the [`handle_request!`] macro for HTTP
//! flares and [`ws_handlers!`] for WebSocket flares - both wire up the
//! `alloc` export, reset the arena on every invocation, and call your
//! handler. Use [`export_alloc!`] + [`reset_arena`] manually only when you
//! need to define the host exports yourself.
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.
///
/// Most flares should use [`handle_request!`] or [`ws_handlers!`] instead -
/// those macros include the `alloc` export for you.
/// Wire up an HTTP flare entrypoint with a single line.
///
/// Pass the name of a function that takes no arguments and returns a
/// [`FlareAction`]. The macro expands to:
///
/// - the `alloc` export the host runtime requires (via [`export_alloc!`]),
/// - a `#[no_mangle] extern "C" fn handle_request() -> i64` that resets the
/// bump arena, calls your handler, and encodes the action for the host.
///
/// Read inbound request data from the [`request`] module and write your
/// response with [`response`]. Returning the action - rather than calling a
/// host function - lets the host decide whether to ship the response,
/// transform an upstream response, or pass through to origin.
///
/// ```ignore
/// use flaron_sdk::{request, response, FlareAction};
///
/// flaron_sdk::handle_request!(my_flare);
///
/// fn my_flare() -> FlareAction {
/// let body = format!("hello from {} {}", request::method(), request::url());
/// response::set_status(200);
/// response::set_header("content-type", "text/plain");
/// response::set_body_str(&body);
/// FlareAction::Respond
/// }
/// ```
///
/// Use [`ws_handlers!`] for WebSocket flares instead - the two macros are
/// mutually exclusive within one crate because they both define `alloc`.
/// Wire up a WebSocket flare entrypoint with a single line.
///
/// Pass three function names in `open, message, close` order. Each function
/// takes no arguments and returns nothing. The macro expands to:
///
/// - the `alloc` export the host runtime requires (via [`export_alloc!`]),
/// - three `#[no_mangle] extern "C"` exports - `ws_open`, `ws_message`,
/// `ws_close` - that each reset the bump arena and dispatch to your
/// handler.
///
/// Read connection state and inbound frame data from the [`ws`] module
/// (`ws::conn_id`, `ws::event_text`, `ws::close_code`, etc.) and send
/// outbound frames with `ws::send_text` / `ws::send_binary`.
///
/// ```ignore
/// use flaron_sdk::{logging, ws};
///
/// flaron_sdk::ws_handlers!(on_open, on_message, on_close);
///
/// fn on_open() {
/// let conn = ws::conn_id();
/// logging::info(&format!("ws open conn={}", conn));
/// let _ = ws::send_text(&format!("welcome {}", conn));
/// }
///
/// fn on_message() {
/// let payload = ws::event_text();
/// let _ = ws::send_text(&format!("echo: {}", payload));
/// }
///
/// fn on_close() {
/// let code = ws::close_code();
/// logging::info(&format!("ws close code={}", code));
/// }
/// ```
///
/// Use [`handle_request!`] for HTTP flares instead - the two macros are
/// mutually exclusive within one crate because they both define `alloc`.