camel-wit 0.13.0

WIT interface definitions for rust-camel WASM plugins
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/// WIT source for the `plugin` world (standalone file).
pub const PLUGIN_WIT: &str = include_str!("../wit/camel-plugin.wit");

/// WIT source for the `bean` world (standalone file, same package as PLUGIN_WIT).
pub const BEAN_WIT: &str = include_str!("../wit/camel-bean.wit");

/// Combined WIT package with both `plugin` and `bean` worlds in a single document.
pub const FULL_WIT: &str = include_str!("../wit/camel-all.wit");

// TODO(WIT-006): WIT interface versioning strategy is not yet defined.
// Consider using `@since(version = X.Y.Z)` WIT annotations when supported by
// the wit-bindgen / wasm-component-ld toolchain to enable compatibility checks.

// ── Common content type constants ────────────────────────────────────────

/// MIME type for JSON data.
pub const APPLICATION_JSON: &str = "application/json";

/// MIME type for plain text.
pub const TEXT_PLAIN: &str = "text/plain";

/// MIME type for arbitrary binary data.
pub const APPLICATION_OCTET_STREAM: &str = "application/octet-stream";

/// MIME type for HTML documents.
pub const TEXT_HTML: &str = "text/html";

/// MIME type for XML documents.
pub const APPLICATION_XML: &str = "application/xml";

/// MIME type for URL-encoded form data.
pub const APPLICATION_FORM_URLENCODED: &str = "application/x-www-form-urlencoded";

/// Absolute path to the `wit/` directory bundled with this crate.
///
/// Returns the `wit/` subdirectory under the crate's manifest directory,
/// resolved at **compile time** via `CARGO_MANIFEST_DIR`. This points to the
/// `camel-wit` source directory (local path dep or registry unpack location).
///
/// # Stability
///
/// This path is stable during builds and in development tooling, but is
/// **not a reliable runtime path in redistributed binaries** — after
/// `cargo install`, the source tree is no longer available and callers may
/// see a missing-directory warning.
///
/// # When to use
///
/// Prefer the `*_WIT` string constants (`PLUGIN_WIT`, `BEAN_WIT`, `FULL_WIT`)
/// for embedding WIT content robustly. Use this function only for CLI tooling
/// that needs filesystem access at build/dev time (e.g. `wasm-tools`,
/// `wit-bindgen` CLI invoked from a build script).
pub fn wit_dir() -> &'static std::path::Path {
    std::path::Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/wit"))
}

use std::sync::atomic::{AtomicUsize, Ordering};

use camel_api::CamelError;

/// Default maximum number of resources a WIT host may allocate.
const DEFAULT_MAX_RESOURCES: usize = 1000;

/// Host-side resource tracker for WIT-based WASM plugins.
///
/// Enforces a configurable upper bound on the number of concurrently
/// allocated resources to prevent unbounded memory growth in the host.
///
/// Thread-safe: uses a CAS loop on an atomic counter so concurrent
/// `allocate` calls cannot race past the limit.
#[derive(Debug)]
pub struct WitHost {
    max_resources: usize,
    allocation_count: AtomicUsize,
}

impl WitHost {
    /// Creates a new `WitHost` with the default resource limit (1000).
    pub fn new() -> Self {
        Self::with_max_resources(DEFAULT_MAX_RESOURCES)
    }

    /// Creates a new `WitHost` with an explicit maximum resource count.
    pub fn with_max_resources(max: usize) -> Self {
        Self {
            max_resources: max,
            allocation_count: AtomicUsize::new(0),
        }
    }

    /// Allocates a resource slot.
    ///
    /// Returns `Err(CamelError::ProcessorError)` if the resource limit
    /// would be exceeded. Uses a CAS loop to avoid TOCTOU races when
    /// called concurrently from multiple threads.
    pub fn allocate(&self, _name: &str) -> Result<(), CamelError> {
        let mut current = self.allocation_count.load(Ordering::Relaxed);
        loop {
            if current >= self.max_resources {
                return Err(CamelError::ProcessorError("resource limit exceeded".into()));
            }
            match self.allocation_count.compare_exchange_weak(
                current,
                current + 1,
                Ordering::AcqRel,
                Ordering::Relaxed,
            ) {
                Ok(_) => return Ok(()),
                Err(actual) => current = actual,
            }
        }
    }

    /// Deallocates a resource slot, freeing capacity for future allocations.
    pub fn deallocate(&self, _name: &str) {
        self.allocation_count.fetch_sub(1, Ordering::AcqRel);
    }

    /// Returns the current number of allocated resources.
    pub fn resource_count(&self) -> usize {
        self.allocation_count.load(Ordering::Acquire)
    }

    /// Returns the configured maximum resource limit.
    pub fn max_resources(&self) -> usize {
        self.max_resources
    }
}

impl Default for WitHost {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_wit_host_rejects_beyond_max_resources() {
        let host = WitHost::with_max_resources(3);
        host.allocate("a").unwrap();
        host.allocate("b").unwrap();
        host.allocate("c").unwrap();
        let result = host.allocate("d"); // must fail
        assert!(result.is_err());
    }

    #[test]
    fn test_wit_host_default_limit_is_1000() {
        let host = WitHost::new();
        assert_eq!(host.max_resources(), 1000);
    }

    #[test]
    fn test_wit_host_allows_up_to_limit() {
        let host = WitHost::with_max_resources(2);
        assert!(host.allocate("x").is_ok());
        assert!(host.allocate("y").is_ok());
        assert!(host.allocate("z").is_err());
    }

    #[test]
    fn test_wit_host_deallocate_frees_slot() {
        let host = WitHost::with_max_resources(1);
        host.allocate("a").unwrap();
        assert!(host.allocate("b").is_err());
        host.deallocate("a");
        assert!(host.allocate("b").is_ok());
    }

    #[test]
    fn test_wit_host_resource_count_tracks_allocations() {
        let host = WitHost::new();
        assert_eq!(host.resource_count(), 0);
        host.allocate("a").unwrap();
        host.allocate("b").unwrap();
        assert_eq!(host.resource_count(), 2);
        host.deallocate("a");
        assert_eq!(host.resource_count(), 1);
    }

    #[test]
    fn test_wit_host_error_is_processor_error() {
        let host = WitHost::with_max_resources(1);
        host.allocate("a").unwrap();
        let err = host.allocate("b").unwrap_err();
        assert!(matches!(err, CamelError::ProcessorError(_)));
        assert!(err.to_string().contains("resource limit exceeded"));
    }

    // ── WIT-002: Tests for WIT definitions ──────────────────────────────────

    #[test]
    fn test_wit_dir_exists() {
        let dir = wit_dir();
        assert!(
            dir.exists(),
            "wit_dir() should point to an existing directory"
        );
        assert!(dir.is_dir(), "wit_dir() should be a directory");
    }

    #[test]
    fn test_wit_dir_contains_expected_files() {
        let dir = wit_dir();
        assert!(
            dir.join("camel-plugin.wit").exists(),
            "camel-plugin.wit missing"
        );
        assert!(
            dir.join("camel-bean.wit").exists(),
            "camel-bean.wit missing"
        );
        assert!(dir.join("camel-all.wit").exists(), "camel-all.wit missing");
    }

    #[test]
    fn test_plugin_wit_is_non_empty() {
        assert!(!PLUGIN_WIT.is_empty(), "PLUGIN_WIT should not be empty");
    }

    #[test]
    fn test_bean_wit_is_non_empty() {
        assert!(!BEAN_WIT.is_empty(), "BEAN_WIT should not be empty");
    }

    #[test]
    fn test_full_wit_is_non_empty() {
        assert!(!FULL_WIT.is_empty(), "FULL_WIT should not be empty");
    }

    #[test]
    fn test_wit_constants_contain_package_declaration() {
        assert!(PLUGIN_WIT.contains("package camel:plugin"));
        assert!(BEAN_WIT.contains("package camel:plugin"));
        assert!(FULL_WIT.contains("package camel:plugin"));
    }

    #[test]
    fn test_wit_exchange_has_route_and_message_id_fields() {
        // WIT-005: verify route-id and message-id fields are present
        assert!(
            FULL_WIT.contains("route-id"),
            "wasm-exchange should contain route-id field"
        );
        assert!(
            FULL_WIT.contains("message-id"),
            "wasm-exchange should contain message-id field"
        );
        assert!(
            PLUGIN_WIT.contains("route-id"),
            "plugin WIT should contain route-id field"
        );
        assert!(
            PLUGIN_WIT.contains("message-id"),
            "plugin WIT should contain message-id field"
        );
    }

    #[test]
    fn test_plugin_wit_contains_authorization_policy_world() {
        assert!(
            PLUGIN_WIT.contains("world authorization-policy"),
            "PLUGIN_WIT should contain 'world authorization-policy'"
        );
    }

    #[test]
    fn test_plugin_wit_authorization_policy_has_evaluate() {
        assert!(
            PLUGIN_WIT.contains("export evaluate: func(exchange: wasm-exchange) -> result<option<string>, wasm-error>"),
            "PLUGIN_WIT should contain evaluate export"
        );
    }

    #[test]
    fn test_plugin_wit_authorization_policy_has_init_with_config() {
        assert!(
            PLUGIN_WIT.contains(
                "export init: func(config: list<tuple<string, string>>) -> result<_, string>"
            ),
            "PLUGIN_WIT should contain init with config parameter"
        );
    }

    #[test]
    fn test_full_wit_contains_authorization_policy_world() {
        assert!(
            FULL_WIT.contains("world authorization-policy"),
            "FULL_WIT should contain 'world authorization-policy'"
        );
    }

    fn strip_comments(wit: &str) -> String {
        wit.lines()
            .filter(|l| !l.trim_start().starts_with("//"))
            .collect::<Vec<_>>()
            .join("\n")
            .trim()
            .to_string()
    }

    #[test]
    fn test_example_bean_wit_matches_canonical() {
        let example_dir = std::path::Path::new(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../../examples/wasm-bean-example/wit"
        ));
        if !example_dir.exists() {
            return;
        }
        let example_bean = std::fs::read_to_string(example_dir.join("camel-bean.wit"))
            .expect("read example bean wit");
        let canonical_stripped = strip_comments(BEAN_WIT);
        let example_stripped = strip_comments(&example_bean);
        assert_eq!(
            canonical_stripped, example_stripped,
            "examples/wasm-bean-example/wit/camel-bean.wit must match canonical without comments"
        );
    }

    #[test]
    fn test_example_plugin_wit_has_route_id_and_message_id() {
        let example_dir = std::path::Path::new(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../../examples/wasm-bean-example/wit"
        ));
        if !example_dir.exists() {
            return;
        }
        let example_plugin = std::fs::read_to_string(example_dir.join("camel-plugin.wit"))
            .expect("read example plugin wit");
        assert!(
            example_plugin.contains("route-id"),
            "example camel-plugin.wit must contain route-id"
        );
        assert!(
            example_plugin.contains("message-id"),
            "example camel-plugin.wit must contain message-id"
        );
        assert!(
            example_plugin.contains("world authorization-policy"),
            "example camel-plugin.wit must contain authorization-policy world"
        );
        assert!(
            example_plugin.contains(
                "export init: func(config: list<tuple<string, string>>) -> result<_, string>"
            ),
            "example camel-plugin.wit must contain init(config) in bean world"
        );
    }

    #[test]
    fn test_example_all_wit_has_all_worlds() {
        let example_dir = std::path::Path::new(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../../examples/wasm-bean-example/wit"
        ));
        if !example_dir.exists() {
            return;
        }
        let example_all = std::fs::read_to_string(example_dir.join("camel-all.wit"))
            .expect("read example all wit");
        assert!(
            example_all.contains("world plugin"),
            "camel-all.wit must contain plugin world"
        );
        assert!(
            example_all.contains("world bean"),
            "camel-all.wit must contain bean world"
        );
        assert!(
            example_all.contains("world authorization-policy"),
            "camel-all.wit must contain authorization-policy world"
        );
    }

    #[test]
    fn test_host_wit_matches_canonical() {
        let host_wit_dir = std::path::Path::new(concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../components/camel-component-wasm/wit"
        ));
        if !host_wit_dir.exists() {
            return;
        }
        let host_plugin = std::fs::read_to_string(host_wit_dir.join("camel-plugin.wit"))
            .expect("read host plugin wit");
        let canonical_stripped = strip_comments(PLUGIN_WIT);
        let host_stripped = strip_comments(&host_plugin);
        assert_eq!(
            canonical_stripped, host_stripped,
            "camel-component-wasm/wit/camel-plugin.wit must match canonical without comments"
        );
    }

    #[test]
    fn test_content_type_constants_compile() {
        // Verifies the exported constants are accessible and have expected values.
        assert_eq!(APPLICATION_JSON, "application/json");
        assert_eq!(TEXT_PLAIN, "text/plain");
        assert_eq!(APPLICATION_OCTET_STREAM, "application/octet-stream");
        assert_eq!(TEXT_HTML, "text/html");
        assert_eq!(APPLICATION_XML, "application/xml");
        assert_eq!(
            APPLICATION_FORM_URLENCODED,
            "application/x-www-form-urlencoded"
        );
    }
}