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

kiwi-sdk

Kiwi SDK 允许您编写可插入模块,以便将其挂钩到 Kiwi 运行时

5 个版本

0.1.1 2024年4月3日
0.1.1-alpha.72024年2月23日
0.1.1-alpha.62024年2月22日

#2 in #kiwi

Download history 5/week @ 2024-06-11 1/week @ 2024-07-02

425 每月下载量

MIT 许可证

36KB
233 代码行

Kiwi SDK

此crate提供编写Kiwi插件的必要工具。

示例

拦截

//! 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
}

验证

//! 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
}

依赖项

~2.5MB
~50K SLoC