edgee_components_runtime/edge_function/versions/v1_0_0/
mod.rs

1use crate::{config::EdgeFunctionComponents, context::ComponentsContext};
2use wasmtime::{component::Linker, Engine, Store};
3
4use crate::context::HostState;
5use crate::edge_function::versions::v1_0_0::edge_function::EdgeFunctionV100;
6use crate::edge_function::versions::v1_0_0::edge_function::EdgeFunctionV100Pre;
7
8pub mod edge_function {
9    wasmtime::component::bindgen!({
10        path: "src/edge_function/wit",
11        world: "edge-function-v100",
12        imports: {
13            default: tracing | trappable,
14        },
15        exports: {
16            default: async,
17        },
18        require_store_data_send: true,
19        with: {
20            "wasi:http/types/outgoing-body": wasmtime_wasi_http::body::HostOutgoingBody,
21            "wasi:http/types/future-incoming-response": wasmtime_wasi_http::types::HostFutureIncomingResponse,
22            "wasi:http/types/outgoing-response": wasmtime_wasi_http::types::HostOutgoingResponse,
23            "wasi:http/types/future-trailers": wasmtime_wasi_http::body::HostFutureTrailers,
24            "wasi:http/types/incoming-body": wasmtime_wasi_http::body::HostIncomingBody,
25            "wasi:http/types/incoming-response": wasmtime_wasi_http::types::HostIncomingResponse,
26            "wasi:http/types/response-outparam": wasmtime_wasi_http::types::HostResponseOutparam,
27            "wasi:http/types/outgoing-request": wasmtime_wasi_http::types::HostOutgoingRequest,
28            "wasi:http/types/incoming-request": wasmtime_wasi_http::types::HostIncomingRequest,
29            "wasi:http/types/fields": wasmtime_wasi_http::types::HostFields,
30            "wasi:http/types/request-options": wasmtime_wasi_http::types::HostRequestOptions,
31        },
32    });
33}
34
35pub fn pre_instanciate_edge_function_component_1_0_0(
36    engine: &Engine,
37    component_config: &EdgeFunctionComponents,
38) -> anyhow::Result<EdgeFunctionV100Pre<HostState>> {
39    let mut linker = Linker::new(engine);
40    wasmtime_wasi::p2::add_to_linker_async(&mut linker)?;
41    wasmtime_wasi_http::add_only_http_to_linker_async(&mut linker)?;
42    let span = tracing::info_span!("component-context", component = %component_config.id, category = "edge-function");
43    let _span = span.enter();
44
45    tracing::debug!("Start pre-instantiate edge-function component");
46
47    let start = std::time::Instant::now();
48    let component = crate::helpers::instanciate_component(
49        engine,
50        &component_config.file,
51        &component_config.serialized_file,
52    )?;
53    println!(
54        "Component {} loaded successfully is in {} ms",
55        component_config.id,
56        start.elapsed().as_millis()
57    );
58
59    let start = std::time::Instant::now();
60    let instance_pre = linker.instantiate_pre(&component)?;
61    println!(
62        "Component {} instantiated successfully in {} ms",
63        component_config.id,
64        start.elapsed().as_millis()
65    );
66    let instance_pre = EdgeFunctionV100Pre::new(instance_pre)?;
67    tracing::debug!("Finished pre-instantiate edge-function component");
68
69    Ok(instance_pre)
70}
71
72impl ComponentsContext {
73    pub async fn get_edge_function_1_0_0_instance(
74        &self,
75        id: &str,
76        store: &mut Store<HostState>,
77    ) -> anyhow::Result<EdgeFunctionV100> {
78        let instance_pre = self.components.edge_function_1_0_0.get(id);
79
80        if instance_pre.is_none() {
81            return Err(anyhow::anyhow!("component not found: {}", id));
82        }
83
84        instance_pre.unwrap().instantiate_async(store).await
85    }
86
87    pub fn add_edge_function_1_0_0_instance(
88        &mut self,
89        component_config: EdgeFunctionComponents,
90        instance_pre: EdgeFunctionV100Pre<HostState>,
91    ) {
92        if !self
93            .components
94            .edge_function_1_0_0
95            .contains_key(&component_config.id)
96        {
97            self.components
98                .edge_function_1_0_0
99                .insert(component_config.id.clone(), instance_pre);
100        }
101    }
102}