-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Rate Limiter Binding (#603)
* Implement Rate Limiter binding * Make success field public
- Loading branch information
1 parent
566166f
commit 9fc2fbd
Showing
4 changed files
with
82 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
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 @@ | ||
use js_sys::Promise; | ||
use wasm_bindgen::JsValue; | ||
|
||
#[wasm_bindgen::prelude::wasm_bindgen] | ||
extern "C" { | ||
#[wasm_bindgen(extends=js_sys::Object)] | ||
#[derive(Clone, PartialEq, Eq)] | ||
pub type RateLimiter; | ||
|
||
#[wasm_bindgen(method, catch)] | ||
pub fn limit(this: &RateLimiter, arg: js_sys::Object) -> Result<Promise, JsValue>; | ||
} |
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
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,66 @@ | ||
use crate::{send::SendFuture, EnvBinding, Result}; | ||
use serde::{Deserialize, Serialize}; | ||
use wasm_bindgen::{JsCast, JsValue}; | ||
use wasm_bindgen_futures::JsFuture; | ||
use worker_sys::RateLimiter as RateLimiterSys; | ||
|
||
pub struct RateLimiter(RateLimiterSys); | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct RateLimitOptions { | ||
key: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct RateLimitOutcome { | ||
pub success: bool, | ||
} | ||
|
||
unsafe impl Send for RateLimiter {} | ||
unsafe impl Sync for RateLimiter {} | ||
|
||
impl EnvBinding for RateLimiter { | ||
const TYPE_NAME: &'static str = "RateLimiter"; | ||
} | ||
impl RateLimiter { | ||
pub async fn limit(&self, key: String) -> Result<RateLimitOutcome> { | ||
let arg = serde_wasm_bindgen::to_value(&RateLimitOptions { key })?; | ||
let promise = self.0.limit(arg.into())?; | ||
let fut = SendFuture::new(JsFuture::from(promise)); | ||
let result = fut.await?; | ||
let outcome = serde_wasm_bindgen::from_value(result)?; | ||
Ok(outcome) | ||
} | ||
} | ||
|
||
impl JsCast for RateLimiter { | ||
fn instanceof(val: &JsValue) -> bool { | ||
val.is_instance_of::<RateLimiterSys>() | ||
} | ||
|
||
fn unchecked_from_js(val: JsValue) -> Self { | ||
Self(val.into()) | ||
} | ||
|
||
fn unchecked_from_js_ref(val: &JsValue) -> &Self { | ||
unsafe { &*(val as *const JsValue as *const Self) } | ||
} | ||
} | ||
|
||
impl From<RateLimiter> for JsValue { | ||
fn from(limiter: RateLimiter) -> Self { | ||
JsValue::from(limiter.0) | ||
} | ||
} | ||
|
||
impl AsRef<JsValue> for RateLimiter { | ||
fn as_ref(&self) -> &JsValue { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl From<RateLimiterSys> for RateLimiter { | ||
fn from(inner: RateLimiterSys) -> Self { | ||
Self(inner) | ||
} | ||
} |