wrangler/upload/form/
wasm_module.rs

1use std::path::PathBuf;
2
3use super::binding::Binding;
4use super::filestem_from_path;
5use anyhow::{anyhow, Result};
6
7// Note: This is only used for service-worker scripts.
8// modules scripts use the universal Module class instead of this.
9
10#[derive(Debug)]
11pub struct WasmModule {
12    path: PathBuf,
13    filename: String,
14    binding: String,
15}
16
17impl WasmModule {
18    pub fn new(path: PathBuf, binding: String) -> Result<Self> {
19        let filename = filestem_from_path(&path)
20            .ok_or_else(|| anyhow!("filename should not be empty: {}", path.display()))?;
21
22        Ok(Self {
23            path,
24            filename,
25            binding,
26        })
27    }
28
29    // `name` corresponds to the binding used in the worker js
30    // `part` corresponds to the name given to the file in the upload form
31    pub fn binding(&self) -> Binding {
32        Binding::new_wasm_module(self.binding.clone(), self.filename.clone())
33    }
34
35    pub fn path(&self) -> PathBuf {
36        self.path.clone()
37    }
38
39    pub fn filename(&self) -> String {
40        self.filename.to_string()
41    }
42}