Skip to content

Commit 611ac93

Browse files
Add support for Version Metadata Binding (#598)
* Add support for Version Metadata Binding Allows users to fetch the version metadata fron the worker environment. This is useful when using Gradual Deployments. * Add timestamp field to version object --------- Co-authored-by: GoncaloGarcia <ggarcia@cloudflare.com>
1 parent 80d1c6c commit 611ac93

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Respo
173173

174174
## Durable Object, KV, Secret, & Variable Bindings
175175

176-
All "bindings" to your script (Durable Object & KV Namespaces, Secrets, and Variables) are
176+
All "bindings" to your script (Durable Object & KV Namespaces, Secrets, Variables and Version) are
177177
accessible from the `env` parameter provided to both the entrypoint (`main` in this example), and to
178178
the route handler callback (in the `ctx` argument), if you use the `Router` from the `worker` crate.
179179

@@ -214,6 +214,7 @@ For more information about how to configure these bindings, see:
214214

215215
- https://developers.cloudflare.com/workers/cli-wrangler/configuration#keys
216216
- https://developers.cloudflare.com/workers/learning/using-durable-objects#configuring-durable-object-bindings
217+
- https://developers.cloudflare.com/workers/runtime-apis/bindings/version-metadata/
217218

218219
## Durable Objects
219220

worker-sys/src/types.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod r2;
1414
mod schedule;
1515
mod socket;
1616
mod tls_client_auth;
17+
mod version;
1718
mod websocket_pair;
1819

1920
pub use context::*;
@@ -32,4 +33,5 @@ pub use r2::*;
3233
pub use schedule::*;
3334
pub use socket::*;
3435
pub use tls_client_auth::*;
36+
pub use version::*;
3537
pub use websocket_pair::*;

worker-sys/src/types/version.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use wasm_bindgen::prelude::*;
2+
3+
/// This type was created to support https://developers.cloudflare.com/workers/runtime-apis/bindings/version-metadata/
4+
#[wasm_bindgen]
5+
extern "C" {
6+
#[wasm_bindgen(extends=js_sys::Object)]
7+
#[derive(Clone, PartialEq, Eq)]
8+
pub type CfVersionMetadata;
9+
10+
#[wasm_bindgen(method, getter, js_name=id)]
11+
pub fn id(this: &CfVersionMetadata) -> String;
12+
13+
#[wasm_bindgen(method, getter, js_name=tag)]
14+
pub fn tag(this: &CfVersionMetadata) -> String;
15+
16+
#[wasm_bindgen(method, getter, js_name=timestamp)]
17+
pub fn timestamp(this: &CfVersionMetadata) -> String;
18+
}

worker/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ mod schedule;
225225
pub mod send;
226226
mod socket;
227227
mod streams;
228+
mod version;
228229
mod websocket;
229230

230231
pub type Result<T> = StdResult<T, error::Error>;

worker/src/version.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use crate::EnvBinding;
2+
use wasm_bindgen::{JsCast, JsValue};
3+
use worker_sys::types::CfVersionMetadata;
4+
5+
pub struct WorkerVersionMetadata(CfVersionMetadata);
6+
7+
unsafe impl Send for WorkerVersionMetadata {}
8+
unsafe impl Sync for WorkerVersionMetadata {}
9+
10+
impl EnvBinding for WorkerVersionMetadata {
11+
const TYPE_NAME: &'static str = "Object";
12+
}
13+
14+
impl WorkerVersionMetadata {
15+
pub fn id(&self) -> String {
16+
self.0.id()
17+
}
18+
19+
pub fn tag(&self) -> String {
20+
self.0.tag()
21+
}
22+
23+
pub fn timestamp(&self) -> String {
24+
self.0.timestamp()
25+
}
26+
}
27+
28+
impl JsCast for WorkerVersionMetadata {
29+
fn instanceof(val: &JsValue) -> bool {
30+
val.is_instance_of::<CfVersionMetadata>()
31+
}
32+
33+
fn unchecked_from_js(val: JsValue) -> Self {
34+
Self(val.into())
35+
}
36+
37+
fn unchecked_from_js_ref(val: &JsValue) -> &Self {
38+
unsafe { &*(val as *const JsValue as *const Self) }
39+
}
40+
}
41+
42+
impl From<WorkerVersionMetadata> for JsValue {
43+
fn from(cf_version: WorkerVersionMetadata) -> Self {
44+
JsValue::from(cf_version.0)
45+
}
46+
}
47+
48+
impl AsRef<JsValue> for WorkerVersionMetadata {
49+
fn as_ref(&self) -> &JsValue {
50+
&self.0
51+
}
52+
}

0 commit comments

Comments
 (0)