Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions worker-sys/src/types/durable_object.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use wasm_bindgen::prelude::*;

mod container;
mod id;
mod namespace;
mod sql_storage;
mod state;
mod storage;
mod transaction;

pub use container::*;
pub use id::*;
pub use namespace::*;
pub use sql_storage::*;
Expand Down
62 changes: 62 additions & 0 deletions worker-sys/src/types/durable_object/container.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use js_sys::{Object, Promise};
use wasm_bindgen::prelude::*;

use crate::Fetcher;

/// ```ts
/// interface Container {
/// get running(): boolean;
/// start(options?: ContainerStartupOptions): void;
/// monitor(): Promise<void>;
/// destroy(error?: any): Promise<void>;
/// signal(signo: number): void;
/// getTcpPort(port: number): Fetcher;
/// }
///
/// interface ContainerStartupOptions {
/// entrypoint?: string[];
/// enableInternet: boolean;
/// env?: Record<string, string>;
/// }
/// ```

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends=js_sys::Object)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub type Container;

#[wasm_bindgen(method, getter)]
pub fn running(this: &Container) -> bool;

#[wasm_bindgen(method)]
pub fn start(this: &Container, options: Option<&ContainerStartupOptions>);

#[wasm_bindgen(method)]
pub fn monitor(this: &Container) -> Promise;

#[wasm_bindgen(method)]
pub fn destroy(this: &Container, error: &JsValue) -> Promise;

#[wasm_bindgen(method)]
pub fn signal(this: &Container, signo: i32);

#[wasm_bindgen(method, js_name=getTcpPort)]
pub fn get_tcp_port(this: &Container, port: i32) -> Fetcher;
}

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends=js_sys::Object)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub type ContainerStartupOptions;

#[wasm_bindgen(method, getter)]
pub fn entrypoint(this: &ContainerStartupOptions) -> Option<Vec<String>>;

#[wasm_bindgen(method, getter, js_name=enableInternet)]
pub fn enable_internet(this: &ContainerStartupOptions) -> bool;

#[wasm_bindgen(method, catch, getter)]
pub fn env(this: &ContainerStartupOptions) -> Result<Object, JsValue>;
}
7 changes: 6 additions & 1 deletion worker-sys/src/types/durable_object/state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use wasm_bindgen::prelude::*;

use crate::types::{DurableObjectId, DurableObjectStorage, WebSocketRequestResponsePair};
use crate::types::{
Container, DurableObjectId, DurableObjectStorage, WebSocketRequestResponsePair,
};

#[wasm_bindgen]
extern "C" {
Expand All @@ -13,6 +15,9 @@ extern "C" {
#[wasm_bindgen(method, catch, getter)]
pub fn storage(this: &DurableObjectState) -> Result<DurableObjectStorage, JsValue>;

#[wasm_bindgen(method, catch, getter)]
pub fn container(this: &DurableObjectState) -> Result<Container, JsValue>;

#[wasm_bindgen(method, catch, js_name=waitUntil)]
pub fn wait_until(this: &DurableObjectState, promise: &js_sys::Promise) -> Result<(), JsValue>;

Expand Down
111 changes: 111 additions & 0 deletions worker/src/container.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use wasm_bindgen::{JsCast, JsValue};

use crate::{Fetcher, Result};

#[derive(Debug)]
pub struct Container {
pub(super) inner: worker_sys::Container,
}

impl Container {
pub fn running(&self) -> bool {
self.inner.running()
}

pub fn start(&self, options: Option<ContainerStartupOptions>) {
self.inner.start(options.as_ref().map(|o| &o.inner))
}

pub fn get_tcp_port(&self, port: i32) -> Fetcher {
self.inner.get_tcp_port(port).into()
}
}

unsafe impl Sync for Container {}
unsafe impl Send for Container {}

impl From<worker_sys::Container> for Container {
fn from(inner: worker_sys::Container) -> Self {
Self { inner }
}
}

impl AsRef<JsValue> for Container {
fn as_ref(&self) -> &JsValue {
&self.inner
}
}

impl From<Container> for JsValue {
fn from(database: Container) -> Self {
JsValue::from(database.inner)
}
}

impl JsCast for Container {
fn instanceof(val: &JsValue) -> bool {
val.is_instance_of::<worker_sys::Container>()
}

fn unchecked_from_js(val: JsValue) -> Self {
Self { inner: val.into() }
}

fn unchecked_from_js_ref(val: &JsValue) -> &Self {
unsafe { &*(val as *const JsValue as *const Self) }
}
}

#[derive(Debug)]
pub struct ContainerStartupOptions {
inner: worker_sys::ContainerStartupOptions,
}

impl ContainerStartupOptions {
pub fn entrypoint(&self) -> Option<Vec<String>> {
self.inner.entrypoint()
}

pub fn enable_internet(&self) -> bool {
self.inner.enable_internet()
}

pub fn env(&self) -> Result<js_sys::Object> {
Ok(self.inner.env()?)
}
}

unsafe impl Sync for ContainerStartupOptions {}
unsafe impl Send for ContainerStartupOptions {}

impl From<worker_sys::ContainerStartupOptions> for ContainerStartupOptions {
fn from(inner: worker_sys::ContainerStartupOptions) -> Self {
Self { inner }
}
}

impl AsRef<JsValue> for ContainerStartupOptions {
fn as_ref(&self) -> &JsValue {
&self.inner
}
}

impl From<ContainerStartupOptions> for JsValue {
fn from(database: ContainerStartupOptions) -> Self {
JsValue::from(database.inner)
}
}

impl JsCast for ContainerStartupOptions {
fn instanceof(val: &JsValue) -> bool {
val.is_instance_of::<worker_sys::ContainerStartupOptions>()
}

fn unchecked_from_js(val: JsValue) -> Self {
Self { inner: val.into() }
}

fn unchecked_from_js_ref(val: &JsValue) -> &Self {
unsafe { &*(val as *const JsValue as *const Self) }
}
}
5 changes: 5 additions & 0 deletions worker/src/durable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use std::{fmt::Display, ops::Deref, time::Duration};

use crate::{
container::Container,
date::Date,
env::{Env, EnvBinding},
error::Error,
Expand Down Expand Up @@ -230,6 +231,10 @@ impl State {
}
}

pub fn container(&self) -> Option<Container> {
self.inner.container().ok().map(|inner| Container { inner })
}

pub fn wait_until<F>(&self, future: F)
where
F: Future<Output = ()> + 'static,
Expand Down
2 changes: 2 additions & 0 deletions worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ pub use crate::abort::*;
pub use crate::ai::*;
pub use crate::analytics_engine::*;
pub use crate::cache::{Cache, CacheDeletionOutcome, CacheKey};
pub use crate::container::*;
pub use crate::context::Context;
pub use crate::cors::Cors;
#[cfg(feature = "d1")]
Expand Down Expand Up @@ -203,6 +204,7 @@ mod ai;
mod analytics_engine;
mod cache;
mod cf;
mod container;
mod context;
mod cors;
pub mod crypto;
Expand Down