Skip to content

Commit 9fc2fbd

Browse files
authored
Implement Rate Limiter Binding (#603)
* Implement Rate Limiter binding * Make success field public
1 parent 566166f commit 9fc2fbd

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

worker-sys/src/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod incoming_request_cf_properties;
1111
#[cfg(feature = "queue")]
1212
mod queue;
1313
mod r2;
14+
mod rate_limit;
1415
mod schedule;
1516
mod socket;
1617
mod tls_client_auth;
@@ -30,6 +31,7 @@ pub use incoming_request_cf_properties::*;
3031
#[cfg(feature = "queue")]
3132
pub use queue::*;
3233
pub use r2::*;
34+
pub use rate_limit::*;
3335
pub use schedule::*;
3436
pub use socket::*;
3537
pub use tls_client_auth::*;

worker-sys/src/types/rate_limit.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use js_sys::Promise;
2+
use wasm_bindgen::JsValue;
3+
4+
#[wasm_bindgen::prelude::wasm_bindgen]
5+
extern "C" {
6+
#[wasm_bindgen(extends=js_sys::Object)]
7+
#[derive(Clone, PartialEq, Eq)]
8+
pub type RateLimiter;
9+
10+
#[wasm_bindgen(method, catch)]
11+
pub fn limit(this: &RateLimiter, arg: js_sys::Object) -> Result<Promise, JsValue>;
12+
}

worker/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ pub use crate::hyperdrive::*;
183183
#[cfg(feature = "queue")]
184184
pub use crate::queue::*;
185185
pub use crate::r2::*;
186+
pub use crate::rate_limit::RateLimiter;
186187
pub use crate::request::{FromRequest, Request};
187188
pub use crate::request_init::*;
188189
pub use crate::response::{EncodeBody, IntoResponse, Response, ResponseBody, ResponseBuilder};
@@ -218,6 +219,7 @@ mod hyperdrive;
218219
#[cfg(feature = "queue")]
219220
mod queue;
220221
mod r2;
222+
mod rate_limit;
221223
mod request;
222224
mod request_init;
223225
mod response;

worker/src/rate_limit.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use crate::{send::SendFuture, EnvBinding, Result};
2+
use serde::{Deserialize, Serialize};
3+
use wasm_bindgen::{JsCast, JsValue};
4+
use wasm_bindgen_futures::JsFuture;
5+
use worker_sys::RateLimiter as RateLimiterSys;
6+
7+
pub struct RateLimiter(RateLimiterSys);
8+
9+
#[derive(Serialize, Deserialize)]
10+
struct RateLimitOptions {
11+
key: String,
12+
}
13+
14+
#[derive(Serialize, Deserialize)]
15+
pub struct RateLimitOutcome {
16+
pub success: bool,
17+
}
18+
19+
unsafe impl Send for RateLimiter {}
20+
unsafe impl Sync for RateLimiter {}
21+
22+
impl EnvBinding for RateLimiter {
23+
const TYPE_NAME: &'static str = "RateLimiter";
24+
}
25+
impl RateLimiter {
26+
pub async fn limit(&self, key: String) -> Result<RateLimitOutcome> {
27+
let arg = serde_wasm_bindgen::to_value(&RateLimitOptions { key })?;
28+
let promise = self.0.limit(arg.into())?;
29+
let fut = SendFuture::new(JsFuture::from(promise));
30+
let result = fut.await?;
31+
let outcome = serde_wasm_bindgen::from_value(result)?;
32+
Ok(outcome)
33+
}
34+
}
35+
36+
impl JsCast for RateLimiter {
37+
fn instanceof(val: &JsValue) -> bool {
38+
val.is_instance_of::<RateLimiterSys>()
39+
}
40+
41+
fn unchecked_from_js(val: JsValue) -> Self {
42+
Self(val.into())
43+
}
44+
45+
fn unchecked_from_js_ref(val: &JsValue) -> &Self {
46+
unsafe { &*(val as *const JsValue as *const Self) }
47+
}
48+
}
49+
50+
impl From<RateLimiter> for JsValue {
51+
fn from(limiter: RateLimiter) -> Self {
52+
JsValue::from(limiter.0)
53+
}
54+
}
55+
56+
impl AsRef<JsValue> for RateLimiter {
57+
fn as_ref(&self) -> &JsValue {
58+
&self.0
59+
}
60+
}
61+
62+
impl From<RateLimiterSys> for RateLimiter {
63+
fn from(inner: RateLimiterSys) -> Self {
64+
Self(inner)
65+
}
66+
}

0 commit comments

Comments
 (0)