1
1
use serde:: { Deserialize , Serialize } ;
2
+ use spin_factor_outbound_http:: wasi_2023_10_18:: ProxyIndices as ProxyIndices2023_10_18 ;
3
+ use spin_factor_outbound_http:: wasi_2023_11_10:: ProxyIndices as ProxyIndices2023_11_10 ;
2
4
use wasmtime:: component:: Component ;
5
+ use wasmtime_wasi:: bindings:: CommandIndices ;
6
+ use wasmtime_wasi_http:: bindings:: ProxyIndices ;
3
7
4
8
#[ derive( Clone , Debug , Default , Deserialize , Serialize ) ]
5
9
#[ serde( deny_unknown_fields) ]
@@ -14,64 +18,57 @@ pub fn default_base() -> String {
14
18
}
15
19
16
20
/// The type of http handler export used by a component.
17
- #[ derive( Clone , Copy ) ]
18
21
pub enum HandlerType {
19
22
Spin ,
20
- Wagi ,
21
- Wasi0_2 ,
22
- Wasi2023_11_10 ,
23
- Wasi2023_10_18 ,
23
+ Wagi ( CommandIndices ) ,
24
+ Wasi0_2 ( ProxyIndices ) ,
25
+ Wasi2023_11_10 ( ProxyIndices2023_11_10 ) ,
26
+ Wasi2023_10_18 ( ProxyIndices2023_10_18 ) ,
24
27
}
25
28
26
29
/// The `incoming-handler` export for `wasi:http` version rc-2023-10-18
27
- pub const WASI_HTTP_EXPORT_2023_10_18 : & str =
"wasi:http/[email protected] " ;
30
+ const WASI_HTTP_EXPORT_2023_10_18 : & str =
"wasi:http/[email protected] " ;
28
31
/// The `incoming-handler` export for `wasi:http` version rc-2023-11-10
29
- pub const WASI_HTTP_EXPORT_2023_11_10 : & str =
"wasi:http/[email protected] " ;
32
+ const WASI_HTTP_EXPORT_2023_11_10 : & str =
"wasi:http/[email protected] " ;
30
33
/// The `incoming-handler` export prefix for all `wasi:http` 0.2 versions
31
- pub const WASI_HTTP_EXPORT_0_2_PREFIX : & str =
"wasi:http/[email protected] " ;
34
+ const WASI_HTTP_EXPORT_0_2_PREFIX : & str =
"wasi:http/[email protected] " ;
32
35
/// The `inbound-http` export for `fermyon:spin`
33
- pub const SPIN_HTTP_EXPORT : & str = "fermyon:spin/inbound-http" ;
36
+ const SPIN_HTTP_EXPORT : & str = "fermyon:spin/inbound-http" ;
34
37
35
38
impl HandlerType {
36
39
/// Determine the handler type from the exports of a component.
37
- pub fn from_component (
38
- engine : & wasmtime:: Engine ,
39
- component : & Component ,
40
- ) -> anyhow:: Result < HandlerType > {
41
- let mut handler_ty = None ;
40
+ pub fn from_component ( component : & Component ) -> anyhow:: Result < HandlerType > {
41
+ let mut candidates = Vec :: new ( ) ;
42
+ if let Ok ( indices) = ProxyIndices :: new ( component) {
43
+ candidates. push ( HandlerType :: Wasi0_2 ( indices) ) ;
44
+ }
45
+ if let Ok ( indices) = ProxyIndices2023_10_18 :: new ( component) {
46
+ candidates. push ( HandlerType :: Wasi2023_10_18 ( indices) ) ;
47
+ }
48
+ if let Ok ( indices) = ProxyIndices2023_11_10 :: new ( component) {
49
+ candidates. push ( HandlerType :: Wasi2023_11_10 ( indices) ) ;
50
+ }
51
+ if component. export_index ( None , SPIN_HTTP_EXPORT ) . is_some ( ) {
52
+ candidates. push ( HandlerType :: Spin ) ;
53
+ }
42
54
43
- let mut set = |ty : HandlerType | {
44
- if handler_ty. is_none ( ) {
45
- handler_ty = Some ( ty) ;
46
- Ok ( ( ) )
47
- } else {
48
- Err ( anyhow:: anyhow!(
49
- "component exports multiple different handlers but \
50
- it's expected to export only one"
51
- ) )
52
- }
53
- } ;
54
- let ty = component. component_type ( ) ;
55
- for ( name, _) in ty. exports ( engine) {
56
- match name {
57
- WASI_HTTP_EXPORT_2023_10_18 => set ( HandlerType :: Wasi2023_10_18 ) ?,
58
- WASI_HTTP_EXPORT_2023_11_10 => set ( HandlerType :: Wasi2023_11_10 ) ?,
59
- SPIN_HTTP_EXPORT => set ( HandlerType :: Spin ) ?,
60
- name if name. starts_with ( WASI_HTTP_EXPORT_0_2_PREFIX ) => set ( HandlerType :: Wasi0_2 ) ?,
61
- _ => { }
55
+ match candidates. len ( ) {
56
+ 0 => {
57
+ anyhow:: bail!(
58
+ "Expected component to export one of \
59
+ `{WASI_HTTP_EXPORT_2023_10_18}`, \
60
+ `{WASI_HTTP_EXPORT_2023_11_10}`, \
61
+ `{WASI_HTTP_EXPORT_0_2_PREFIX}.*`, \
62
+ or `{SPIN_HTTP_EXPORT}` but it exported none of those. \
63
+ This may mean the component handles a different trigger, or that its `wasi:http` export is newer then those supported by Spin. \
64
+ If you're sure this is an HTTP module, check if a Spin upgrade is available: this may handle the newer version."
65
+ )
62
66
}
67
+ 1 => Ok ( candidates. pop ( ) . unwrap ( ) ) ,
68
+ _ => anyhow:: bail!(
69
+ "component exports multiple different handlers but \
70
+ it's expected to export only one"
71
+ ) ,
63
72
}
64
-
65
- handler_ty. ok_or_else ( || {
66
- anyhow:: anyhow!(
67
- "Expected component to export one of \
68
- `{WASI_HTTP_EXPORT_2023_10_18}`, \
69
- `{WASI_HTTP_EXPORT_2023_11_10}`, \
70
- `{WASI_HTTP_EXPORT_0_2_PREFIX}.*`, \
71
- or `{SPIN_HTTP_EXPORT}` but it exported none of those. \
72
- This may mean the component handles a different trigger, or that its `wasi:http` export is newer then those supported by Spin. \
73
- If you're sure this is an HTTP module, check if a Spin upgrade is available: this may handle the newer version."
74
- )
75
- } )
76
73
}
77
74
}
0 commit comments