#kiwi #sdk #run-time #hook #action #context #module

kiwi-sdk

The Kiwi SDK allows you to write pluggable modules that hook into the Kiwi runtime

5 releases

0.1.1 Apr 3, 2024
0.1.1-alpha.7 Feb 23, 2024
0.1.1-alpha.6 Feb 22, 2024

#525 in Authentication

Download history 315/week @ 2024-02-21 81/week @ 2024-02-28 3/week @ 2024-03-13 105/week @ 2024-04-03

108 downloads per month

MIT license

36KB
233 lines

Kiwi SDK

This crate provides utilities necessary for writing Kiwi plugins.

Examples

Intercept

//! A simple intercept hook that discards odd numbers from all counter sources
use kiwi_sdk::hook::intercept::{intercept, Action, Context, CounterEventCtx, EventCtx};

/// You must use the `#[intercept]` macro to define an intercept hook.
#[intercept]
fn handle(ctx: Context) -> Action {
    match ctx.event {
        // We only care about counter sources in this example
        EventCtx::Counter(CounterEventCtx {
            source_id: _,
            count,
        }) => {
            if count % 2 == 0 {
                // Returning `Action::Forward` instructs Kiwi to forward the event
                // to the associated client.
                return Action::Forward;
            } else {
                // Returning `Action::Discard` instructs Kiwi to discard the event,
                // preventing it from reaching the associated client.
                return Action::Discard;
            }
        }
        _ => {}
    }

    Action::Forward
}

Authenticate

//! A simple authenticate hook that allows all incoming HTTP requests
use kiwi_sdk::hook::authenticate::{authenticate, Outcome};
use kiwi_sdk::http::Request;

/// You must use the `#[authenticate]` macro to define an authenticate hook.
#[authenticate]
fn handle(req: Request<()>) -> Outcome {
    // Returning `Outcome::Authenticate` instructs Kiwi to allow the connection to be established.
    Outcome::Authenticate
}

Dependencies

~2.5MB
~50K SLoC