Hyperdrive binding (#566)

* Starting to add hyperdrive binding

* Add Hyperdrive getters

* Expose getters from worker crate
This commit is contained in:
Alexander van Saase 2024-05-03 22:58:28 +02:00 committed by GitHub
parent 53b2ebc3ae
commit a2b50b728d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 99 additions and 1 deletions

View File

@ -6,6 +6,7 @@ mod durable_object;
mod dynamic_dispatcher;
mod fetcher;
mod fixed_length_stream;
mod hyperdrive;
mod incoming_request_cf_properties;
#[cfg(feature = "queue")]
mod queue;
@ -23,6 +24,7 @@ pub use durable_object::*;
pub use dynamic_dispatcher::*;
pub use fetcher::*;
pub use fixed_length_stream::*;
pub use hyperdrive::*;
pub use incoming_request_cf_properties::*;
#[cfg(feature = "queue")]
pub use queue::*;

View File

@ -0,0 +1,26 @@
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends=js_sys::Object)]
#[derive(Clone, PartialEq, Eq)]
pub type Hyperdrive;
#[wasm_bindgen(method, getter, js_name=connectionString)]
pub fn connection_string(this: &Hyperdrive) -> String;
#[wasm_bindgen(method, getter, js_name=host)]
pub fn host(this: &Hyperdrive) -> String;
#[wasm_bindgen(method, getter, js_name=port)]
pub fn port(this: &Hyperdrive) -> u16;
#[wasm_bindgen(method, getter, js_name=user)]
pub fn user(this: &Hyperdrive) -> String;
#[wasm_bindgen(method, getter, js_name=password)]
pub fn password(this: &Hyperdrive) -> String;
#[wasm_bindgen(method, getter, js_name=database)]
pub fn database(this: &Hyperdrive) -> String;
}

View File

@ -2,10 +2,10 @@ use std::fmt::Display;
#[cfg(feature = "d1")]
use crate::d1::D1Database;
use crate::error::Error;
#[cfg(feature = "queue")]
use crate::Queue;
use crate::{durable::ObjectNamespace, Bucket, DynamicDispatcher, Fetcher, Result};
use crate::{error::Error, hyperdrive::Hyperdrive};
use js_sys::Object;
use wasm_bindgen::{prelude::*, JsCast, JsValue};
@ -85,6 +85,10 @@ impl Env {
pub fn d1(&self, binding: &str) -> Result<D1Database> {
self.get_binding(binding)
}
pub fn hyperdrive(&self, binding: &str) -> Result<Hyperdrive> {
self.get_binding(binding)
}
}
pub trait EnvBinding: Sized + JsCast {

65
worker/src/hyperdrive.rs Normal file
View File

@ -0,0 +1,65 @@
use wasm_bindgen::{JsCast, JsValue};
use worker_sys::types::Hyperdrive as HyperdriveSys;
use crate::EnvBinding;
pub struct Hyperdrive(HyperdriveSys);
unsafe impl Send for Hyperdrive {}
unsafe impl Sync for Hyperdrive {}
impl EnvBinding for Hyperdrive {
const TYPE_NAME: &'static str = "Hyperdrive";
}
impl JsCast for Hyperdrive {
fn instanceof(val: &JsValue) -> bool {
val.is_instance_of::<HyperdriveSys>()
}
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 AsRef<JsValue> for Hyperdrive {
fn as_ref(&self) -> &JsValue {
&self.0
}
}
impl From<Hyperdrive> for JsValue {
fn from(hyperdrive: Hyperdrive) -> Self {
JsValue::from(hyperdrive.0)
}
}
impl Hyperdrive {
pub fn connection_string(&self) -> String {
self.0.connection_string()
}
pub fn host(&self) -> String {
self.0.host()
}
pub fn port(&self) -> u16 {
self.0.port()
}
pub fn user(&self) -> String {
self.0.user()
}
pub fn password(&self) -> String {
self.0.password()
}
pub fn database(&self) -> String {
self.0.database()
}
}

View File

@ -212,6 +212,7 @@ mod formdata;
mod global;
mod headers;
mod http;
mod hyperdrive;
#[cfg(feature = "queue")]
mod queue;
mod r2;