spikard 0.3.5 → 0.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +659 -659
- data/ext/spikard_rb/Cargo.toml +17 -17
- data/ext/spikard_rb/extconf.rb +10 -10
- data/ext/spikard_rb/src/lib.rs +6 -6
- data/lib/spikard/app.rb +386 -386
- data/lib/spikard/background.rb +27 -27
- data/lib/spikard/config.rb +396 -396
- data/lib/spikard/converters.rb +13 -13
- data/lib/spikard/handler_wrapper.rb +113 -113
- data/lib/spikard/provide.rb +214 -214
- data/lib/spikard/response.rb +173 -173
- data/lib/spikard/schema.rb +243 -243
- data/lib/spikard/sse.rb +111 -111
- data/lib/spikard/streaming_response.rb +44 -44
- data/lib/spikard/testing.rb +221 -221
- data/lib/spikard/upload_file.rb +131 -131
- data/lib/spikard/version.rb +5 -5
- data/lib/spikard/websocket.rb +59 -59
- data/lib/spikard.rb +43 -43
- data/sig/spikard.rbs +366 -360
- data/vendor/crates/spikard-core/Cargo.toml +40 -40
- data/vendor/crates/spikard-core/src/bindings/mod.rs +3 -3
- data/vendor/crates/spikard-core/src/bindings/response.rs +133 -133
- data/vendor/crates/spikard-core/src/debug.rs +63 -63
- data/vendor/crates/spikard-core/src/di/container.rs +726 -726
- data/vendor/crates/spikard-core/src/di/dependency.rs +273 -273
- data/vendor/crates/spikard-core/src/di/error.rs +118 -118
- data/vendor/crates/spikard-core/src/di/factory.rs +538 -538
- data/vendor/crates/spikard-core/src/di/graph.rs +545 -545
- data/vendor/crates/spikard-core/src/di/mod.rs +192 -192
- data/vendor/crates/spikard-core/src/di/resolved.rs +411 -411
- data/vendor/crates/spikard-core/src/di/value.rs +283 -283
- data/vendor/crates/spikard-core/src/errors.rs +39 -39
- data/vendor/crates/spikard-core/src/http.rs +153 -153
- data/vendor/crates/spikard-core/src/lib.rs +29 -29
- data/vendor/crates/spikard-core/src/lifecycle.rs +422 -422
- data/vendor/crates/spikard-core/src/parameters.rs +722 -722
- data/vendor/crates/spikard-core/src/problem.rs +310 -310
- data/vendor/crates/spikard-core/src/request_data.rs +189 -189
- data/vendor/crates/spikard-core/src/router.rs +249 -249
- data/vendor/crates/spikard-core/src/schema_registry.rs +183 -183
- data/vendor/crates/spikard-core/src/type_hints.rs +304 -304
- data/vendor/crates/spikard-core/src/validation.rs +699 -699
- data/vendor/crates/spikard-http/Cargo.toml +68 -68
- data/vendor/crates/spikard-http/src/auth.rs +247 -247
- data/vendor/crates/spikard-http/src/background.rs +249 -249
- data/vendor/crates/spikard-http/src/bindings/mod.rs +3 -3
- data/vendor/crates/spikard-http/src/bindings/response.rs +1 -1
- data/vendor/crates/spikard-http/src/body_metadata.rs +8 -8
- data/vendor/crates/spikard-http/src/cors.rs +490 -490
- data/vendor/crates/spikard-http/src/debug.rs +63 -63
- data/vendor/crates/spikard-http/src/di_handler.rs +423 -423
- data/vendor/crates/spikard-http/src/handler_response.rs +190 -190
- data/vendor/crates/spikard-http/src/handler_trait.rs +228 -228
- data/vendor/crates/spikard-http/src/handler_trait_tests.rs +284 -284
- data/vendor/crates/spikard-http/src/lib.rs +529 -529
- data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +149 -149
- data/vendor/crates/spikard-http/src/lifecycle.rs +428 -428
- data/vendor/crates/spikard-http/src/middleware/mod.rs +285 -285
- data/vendor/crates/spikard-http/src/middleware/multipart.rs +86 -86
- data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +147 -147
- data/vendor/crates/spikard-http/src/middleware/validation.rs +287 -287
- data/vendor/crates/spikard-http/src/openapi/mod.rs +309 -309
- data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +190 -190
- data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +308 -308
- data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +195 -195
- data/vendor/crates/spikard-http/src/parameters.rs +1 -1
- data/vendor/crates/spikard-http/src/problem.rs +1 -1
- data/vendor/crates/spikard-http/src/query_parser.rs +369 -369
- data/vendor/crates/spikard-http/src/response.rs +399 -399
- data/vendor/crates/spikard-http/src/router.rs +1 -1
- data/vendor/crates/spikard-http/src/schema_registry.rs +1 -1
- data/vendor/crates/spikard-http/src/server/handler.rs +87 -87
- data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +98 -98
- data/vendor/crates/spikard-http/src/server/mod.rs +805 -805
- data/vendor/crates/spikard-http/src/server/request_extraction.rs +119 -119
- data/vendor/crates/spikard-http/src/sse.rs +447 -447
- data/vendor/crates/spikard-http/src/testing/form.rs +14 -14
- data/vendor/crates/spikard-http/src/testing/multipart.rs +60 -60
- data/vendor/crates/spikard-http/src/testing/test_client.rs +285 -285
- data/vendor/crates/spikard-http/src/testing.rs +377 -377
- data/vendor/crates/spikard-http/src/type_hints.rs +1 -1
- data/vendor/crates/spikard-http/src/validation.rs +1 -1
- data/vendor/crates/spikard-http/src/websocket.rs +324 -324
- data/vendor/crates/spikard-rb/Cargo.toml +42 -42
- data/vendor/crates/spikard-rb/build.rs +8 -8
- data/vendor/crates/spikard-rb/src/background.rs +63 -63
- data/vendor/crates/spikard-rb/src/config.rs +294 -294
- data/vendor/crates/spikard-rb/src/conversion.rs +453 -453
- data/vendor/crates/spikard-rb/src/di.rs +409 -409
- data/vendor/crates/spikard-rb/src/handler.rs +625 -625
- data/vendor/crates/spikard-rb/src/lib.rs +2771 -2771
- data/vendor/crates/spikard-rb/src/lifecycle.rs +274 -274
- data/vendor/crates/spikard-rb/src/server.rs +283 -283
- data/vendor/crates/spikard-rb/src/sse.rs +231 -231
- data/vendor/crates/spikard-rb/src/test_client.rs +404 -404
- data/vendor/crates/spikard-rb/src/test_sse.rs +143 -143
- data/vendor/crates/spikard-rb/src/test_websocket.rs +221 -221
- data/vendor/crates/spikard-rb/src/websocket.rs +233 -233
- metadata +1 -1
|
@@ -1,231 +1,231 @@
|
|
|
1
|
-
//! Ruby SSE producer bindings
|
|
2
|
-
//!
|
|
3
|
-
//! This module provides the bridge between Ruby blocks/procs and Rust's SSE system.
|
|
4
|
-
//! Uses magnus to safely call Ruby code from Rust async tasks.
|
|
5
|
-
|
|
6
|
-
use magnus::{RHash, Value, prelude::*, value::Opaque};
|
|
7
|
-
use serde_json::Value as JsonValue;
|
|
8
|
-
use spikard_http::{SseEvent, SseEventProducer};
|
|
9
|
-
use tracing::{debug, error};
|
|
10
|
-
|
|
11
|
-
/// Ruby implementation of SseEventProducer
|
|
12
|
-
pub struct RubySseEventProducer {
|
|
13
|
-
/// Producer name for debugging
|
|
14
|
-
name: String,
|
|
15
|
-
/// Ruby proc/callable for next_event (Opaque for Send safety)
|
|
16
|
-
next_event_proc: Opaque<Value>,
|
|
17
|
-
/// Ruby proc/callable for on_connect (Opaque for Send safety)
|
|
18
|
-
on_connect_proc: Option<Opaque<Value>>,
|
|
19
|
-
/// Ruby proc/callable for on_disconnect (Opaque for Send safety)
|
|
20
|
-
on_disconnect_proc: Option<Opaque<Value>>,
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
impl RubySseEventProducer {
|
|
24
|
-
/// Create a new Ruby SSE event producer
|
|
25
|
-
#[allow(dead_code)]
|
|
26
|
-
pub fn new(
|
|
27
|
-
name: String,
|
|
28
|
-
next_event_proc: Value,
|
|
29
|
-
on_connect_proc: Option<Value>,
|
|
30
|
-
on_disconnect_proc: Option<Value>,
|
|
31
|
-
) -> Self {
|
|
32
|
-
Self {
|
|
33
|
-
name,
|
|
34
|
-
next_event_proc: next_event_proc.into(),
|
|
35
|
-
on_connect_proc: on_connect_proc.map(|v| v.into()),
|
|
36
|
-
on_disconnect_proc: on_disconnect_proc.map(|v| v.into()),
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/// Convert Ruby value to JSON
|
|
41
|
-
fn ruby_to_json(ruby: &magnus::Ruby, value: Value) -> Result<JsonValue, String> {
|
|
42
|
-
if value.is_nil() {
|
|
43
|
-
return Ok(JsonValue::Null);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
let json_module: Value = ruby
|
|
47
|
-
.class_object()
|
|
48
|
-
.const_get("JSON")
|
|
49
|
-
.map_err(|e| format!("JSON module not available: {}", e))?;
|
|
50
|
-
|
|
51
|
-
let json_str: String = json_module
|
|
52
|
-
.funcall("generate", (value,))
|
|
53
|
-
.map_err(|e| format!("Failed to generate JSON: {}", e))?;
|
|
54
|
-
|
|
55
|
-
serde_json::from_str(&json_str).map_err(|e| format!("Failed to parse JSON: {}", e))
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
impl SseEventProducer for RubySseEventProducer {
|
|
60
|
-
async fn next_event(&self) -> Option<SseEvent> {
|
|
61
|
-
debug!("Ruby SSE producer '{}': next_event", self.name);
|
|
62
|
-
|
|
63
|
-
match magnus::Ruby::get()
|
|
64
|
-
.map_err(|e| format!("Failed to get Ruby: {}", e))
|
|
65
|
-
.and_then(|ruby| {
|
|
66
|
-
debug!("Ruby SSE producer: acquired Ruby VM");
|
|
67
|
-
|
|
68
|
-
let proc_value = ruby.get_inner(self.next_event_proc);
|
|
69
|
-
let result: Value = proc_value
|
|
70
|
-
.funcall("call", ())
|
|
71
|
-
.map_err(|e| format!("Producer '{}' call failed: {}", self.name, e))?;
|
|
72
|
-
|
|
73
|
-
debug!("Ruby SSE producer: called next_event proc");
|
|
74
|
-
|
|
75
|
-
if result.is_nil() {
|
|
76
|
-
debug!("Ruby SSE producer: received nil, ending stream");
|
|
77
|
-
return Ok(None);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
let result_hash = if let Some(hash) = RHash::from_value(result) {
|
|
81
|
-
hash
|
|
82
|
-
} else {
|
|
83
|
-
let hash_value: Value = result.funcall("to_h", ()).map_err(|e| {
|
|
84
|
-
format!(
|
|
85
|
-
"next_event must return a Hash/SseEvent convertible via to_h ({}): {}",
|
|
86
|
-
unsafe { result.classname() },
|
|
87
|
-
e
|
|
88
|
-
)
|
|
89
|
-
})?;
|
|
90
|
-
RHash::from_value(hash_value).ok_or_else(|| {
|
|
91
|
-
format!("next_event to_h must return a Hash, got {}", unsafe {
|
|
92
|
-
hash_value.classname()
|
|
93
|
-
})
|
|
94
|
-
})?
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
let data_value = result_hash
|
|
98
|
-
.get(ruby.to_symbol("data"))
|
|
99
|
-
.ok_or_else(|| "next_event Hash must have :data key".to_string())?;
|
|
100
|
-
|
|
101
|
-
let data_json = Self::ruby_to_json(&ruby, data_value)?;
|
|
102
|
-
|
|
103
|
-
let event_type: Option<String> = result_hash
|
|
104
|
-
.get(ruby.to_symbol("event_type"))
|
|
105
|
-
.and_then(|v| if v.is_nil() { None } else { String::try_convert(v).ok() });
|
|
106
|
-
|
|
107
|
-
let id: Option<String> = result_hash
|
|
108
|
-
.get(ruby.to_symbol("id"))
|
|
109
|
-
.and_then(|v| if v.is_nil() { None } else { String::try_convert(v).ok() });
|
|
110
|
-
|
|
111
|
-
let retry: Option<u64> = result_hash
|
|
112
|
-
.get(ruby.to_symbol("retry"))
|
|
113
|
-
.and_then(|v| if v.is_nil() { None } else { u64::try_convert(v).ok() });
|
|
114
|
-
|
|
115
|
-
let mut event = if let Some(et) = event_type {
|
|
116
|
-
SseEvent::with_type(et, data_json)
|
|
117
|
-
} else {
|
|
118
|
-
SseEvent::new(data_json)
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
if let Some(id_str) = id {
|
|
122
|
-
event = event.with_id(id_str);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if let Some(retry_ms) = retry {
|
|
126
|
-
event = event.with_retry(retry_ms);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
Ok(Some(event))
|
|
130
|
-
}) {
|
|
131
|
-
Ok(event) => event,
|
|
132
|
-
Err(e) => {
|
|
133
|
-
error!("Ruby error in next_event: {}", e);
|
|
134
|
-
None
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
async fn on_connect(&self) {
|
|
140
|
-
debug!("Ruby SSE producer '{}': on_connect", self.name);
|
|
141
|
-
|
|
142
|
-
if let Some(on_connect_proc) = self.on_connect_proc
|
|
143
|
-
&& let Err(e) = magnus::Ruby::get()
|
|
144
|
-
.map_err(|e| format!("Failed to get Ruby: {}", e))
|
|
145
|
-
.and_then(|ruby| {
|
|
146
|
-
debug!("Ruby SSE producer: on_connect acquired Ruby VM");
|
|
147
|
-
let proc_value = ruby.get_inner(on_connect_proc);
|
|
148
|
-
proc_value
|
|
149
|
-
.funcall::<_, _, Value>("call", ())
|
|
150
|
-
.map_err(|e| format!("on_connect '{}' call failed: {}", self.name, e))?;
|
|
151
|
-
debug!("Ruby SSE producer: on_connect completed");
|
|
152
|
-
Ok(())
|
|
153
|
-
})
|
|
154
|
-
{
|
|
155
|
-
error!("on_connect error: {}", e);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
async fn on_disconnect(&self) {
|
|
160
|
-
debug!("Ruby SSE producer '{}': on_disconnect", self.name);
|
|
161
|
-
|
|
162
|
-
if let Some(on_disconnect_proc) = self.on_disconnect_proc
|
|
163
|
-
&& let Err(e) = magnus::Ruby::get()
|
|
164
|
-
.map_err(|e| format!("Failed to get Ruby: {}", e))
|
|
165
|
-
.and_then(|ruby| {
|
|
166
|
-
let proc_value = ruby.get_inner(on_disconnect_proc);
|
|
167
|
-
proc_value
|
|
168
|
-
.funcall::<_, _, Value>("call", ())
|
|
169
|
-
.map_err(|e| format!("on_disconnect '{}' call failed: {}", self.name, e))?;
|
|
170
|
-
debug!("Ruby SSE producer: on_disconnect completed");
|
|
171
|
-
Ok(())
|
|
172
|
-
})
|
|
173
|
-
{
|
|
174
|
-
error!("on_disconnect error: {}", e);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
unsafe impl Send for RubySseEventProducer {}
|
|
180
|
-
unsafe impl Sync for RubySseEventProducer {}
|
|
181
|
-
|
|
182
|
-
/// Create SseState from Ruby producer object
|
|
183
|
-
///
|
|
184
|
-
/// This function is designed to be called from Ruby to register SSE producers.
|
|
185
|
-
#[allow(dead_code)]
|
|
186
|
-
pub fn create_sse_state(
|
|
187
|
-
ruby: &magnus::Ruby,
|
|
188
|
-
producer_obj: Value,
|
|
189
|
-
) -> Result<spikard_http::SseState<RubySseEventProducer>, magnus::Error> {
|
|
190
|
-
let next_event_proc: Value = producer_obj
|
|
191
|
-
.funcall("method", (ruby.to_symbol("next_event"),))
|
|
192
|
-
.map_err(|e| {
|
|
193
|
-
magnus::Error::new(
|
|
194
|
-
ruby.exception_arg_error(),
|
|
195
|
-
format!("next_event method not found: {}", e),
|
|
196
|
-
)
|
|
197
|
-
})?;
|
|
198
|
-
|
|
199
|
-
let on_connect_proc = producer_obj
|
|
200
|
-
.funcall::<_, _, Value>("method", (ruby.to_symbol("on_connect"),))
|
|
201
|
-
.ok();
|
|
202
|
-
|
|
203
|
-
let on_disconnect_proc = producer_obj
|
|
204
|
-
.funcall::<_, _, Value>("method", (ruby.to_symbol("on_disconnect"),))
|
|
205
|
-
.ok();
|
|
206
|
-
|
|
207
|
-
let event_schema = producer_obj
|
|
208
|
-
.funcall::<_, _, Value>("instance_variable_get", (ruby.to_symbol("@_event_schema"),))
|
|
209
|
-
.ok()
|
|
210
|
-
.and_then(|v| {
|
|
211
|
-
if v.is_nil() {
|
|
212
|
-
None
|
|
213
|
-
} else {
|
|
214
|
-
RubySseEventProducer::ruby_to_json(ruby, v).ok()
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
let ruby_producer = RubySseEventProducer::new(
|
|
219
|
-
"SseEventProducer".to_string(),
|
|
220
|
-
next_event_proc,
|
|
221
|
-
on_connect_proc,
|
|
222
|
-
on_disconnect_proc,
|
|
223
|
-
);
|
|
224
|
-
|
|
225
|
-
if event_schema.is_some() {
|
|
226
|
-
spikard_http::SseState::with_schema(ruby_producer, event_schema)
|
|
227
|
-
.map_err(|e| magnus::Error::new(ruby.exception_runtime_error(), e))
|
|
228
|
-
} else {
|
|
229
|
-
Ok(spikard_http::SseState::new(ruby_producer))
|
|
230
|
-
}
|
|
231
|
-
}
|
|
1
|
+
//! Ruby SSE producer bindings
|
|
2
|
+
//!
|
|
3
|
+
//! This module provides the bridge between Ruby blocks/procs and Rust's SSE system.
|
|
4
|
+
//! Uses magnus to safely call Ruby code from Rust async tasks.
|
|
5
|
+
|
|
6
|
+
use magnus::{RHash, Value, prelude::*, value::Opaque};
|
|
7
|
+
use serde_json::Value as JsonValue;
|
|
8
|
+
use spikard_http::{SseEvent, SseEventProducer};
|
|
9
|
+
use tracing::{debug, error};
|
|
10
|
+
|
|
11
|
+
/// Ruby implementation of SseEventProducer
|
|
12
|
+
pub struct RubySseEventProducer {
|
|
13
|
+
/// Producer name for debugging
|
|
14
|
+
name: String,
|
|
15
|
+
/// Ruby proc/callable for next_event (Opaque for Send safety)
|
|
16
|
+
next_event_proc: Opaque<Value>,
|
|
17
|
+
/// Ruby proc/callable for on_connect (Opaque for Send safety)
|
|
18
|
+
on_connect_proc: Option<Opaque<Value>>,
|
|
19
|
+
/// Ruby proc/callable for on_disconnect (Opaque for Send safety)
|
|
20
|
+
on_disconnect_proc: Option<Opaque<Value>>,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
impl RubySseEventProducer {
|
|
24
|
+
/// Create a new Ruby SSE event producer
|
|
25
|
+
#[allow(dead_code)]
|
|
26
|
+
pub fn new(
|
|
27
|
+
name: String,
|
|
28
|
+
next_event_proc: Value,
|
|
29
|
+
on_connect_proc: Option<Value>,
|
|
30
|
+
on_disconnect_proc: Option<Value>,
|
|
31
|
+
) -> Self {
|
|
32
|
+
Self {
|
|
33
|
+
name,
|
|
34
|
+
next_event_proc: next_event_proc.into(),
|
|
35
|
+
on_connect_proc: on_connect_proc.map(|v| v.into()),
|
|
36
|
+
on_disconnect_proc: on_disconnect_proc.map(|v| v.into()),
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/// Convert Ruby value to JSON
|
|
41
|
+
fn ruby_to_json(ruby: &magnus::Ruby, value: Value) -> Result<JsonValue, String> {
|
|
42
|
+
if value.is_nil() {
|
|
43
|
+
return Ok(JsonValue::Null);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let json_module: Value = ruby
|
|
47
|
+
.class_object()
|
|
48
|
+
.const_get("JSON")
|
|
49
|
+
.map_err(|e| format!("JSON module not available: {}", e))?;
|
|
50
|
+
|
|
51
|
+
let json_str: String = json_module
|
|
52
|
+
.funcall("generate", (value,))
|
|
53
|
+
.map_err(|e| format!("Failed to generate JSON: {}", e))?;
|
|
54
|
+
|
|
55
|
+
serde_json::from_str(&json_str).map_err(|e| format!("Failed to parse JSON: {}", e))
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
impl SseEventProducer for RubySseEventProducer {
|
|
60
|
+
async fn next_event(&self) -> Option<SseEvent> {
|
|
61
|
+
debug!("Ruby SSE producer '{}': next_event", self.name);
|
|
62
|
+
|
|
63
|
+
match magnus::Ruby::get()
|
|
64
|
+
.map_err(|e| format!("Failed to get Ruby: {}", e))
|
|
65
|
+
.and_then(|ruby| {
|
|
66
|
+
debug!("Ruby SSE producer: acquired Ruby VM");
|
|
67
|
+
|
|
68
|
+
let proc_value = ruby.get_inner(self.next_event_proc);
|
|
69
|
+
let result: Value = proc_value
|
|
70
|
+
.funcall("call", ())
|
|
71
|
+
.map_err(|e| format!("Producer '{}' call failed: {}", self.name, e))?;
|
|
72
|
+
|
|
73
|
+
debug!("Ruby SSE producer: called next_event proc");
|
|
74
|
+
|
|
75
|
+
if result.is_nil() {
|
|
76
|
+
debug!("Ruby SSE producer: received nil, ending stream");
|
|
77
|
+
return Ok(None);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let result_hash = if let Some(hash) = RHash::from_value(result) {
|
|
81
|
+
hash
|
|
82
|
+
} else {
|
|
83
|
+
let hash_value: Value = result.funcall("to_h", ()).map_err(|e| {
|
|
84
|
+
format!(
|
|
85
|
+
"next_event must return a Hash/SseEvent convertible via to_h ({}): {}",
|
|
86
|
+
unsafe { result.classname() },
|
|
87
|
+
e
|
|
88
|
+
)
|
|
89
|
+
})?;
|
|
90
|
+
RHash::from_value(hash_value).ok_or_else(|| {
|
|
91
|
+
format!("next_event to_h must return a Hash, got {}", unsafe {
|
|
92
|
+
hash_value.classname()
|
|
93
|
+
})
|
|
94
|
+
})?
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
let data_value = result_hash
|
|
98
|
+
.get(ruby.to_symbol("data"))
|
|
99
|
+
.ok_or_else(|| "next_event Hash must have :data key".to_string())?;
|
|
100
|
+
|
|
101
|
+
let data_json = Self::ruby_to_json(&ruby, data_value)?;
|
|
102
|
+
|
|
103
|
+
let event_type: Option<String> = result_hash
|
|
104
|
+
.get(ruby.to_symbol("event_type"))
|
|
105
|
+
.and_then(|v| if v.is_nil() { None } else { String::try_convert(v).ok() });
|
|
106
|
+
|
|
107
|
+
let id: Option<String> = result_hash
|
|
108
|
+
.get(ruby.to_symbol("id"))
|
|
109
|
+
.and_then(|v| if v.is_nil() { None } else { String::try_convert(v).ok() });
|
|
110
|
+
|
|
111
|
+
let retry: Option<u64> = result_hash
|
|
112
|
+
.get(ruby.to_symbol("retry"))
|
|
113
|
+
.and_then(|v| if v.is_nil() { None } else { u64::try_convert(v).ok() });
|
|
114
|
+
|
|
115
|
+
let mut event = if let Some(et) = event_type {
|
|
116
|
+
SseEvent::with_type(et, data_json)
|
|
117
|
+
} else {
|
|
118
|
+
SseEvent::new(data_json)
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
if let Some(id_str) = id {
|
|
122
|
+
event = event.with_id(id_str);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if let Some(retry_ms) = retry {
|
|
126
|
+
event = event.with_retry(retry_ms);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
Ok(Some(event))
|
|
130
|
+
}) {
|
|
131
|
+
Ok(event) => event,
|
|
132
|
+
Err(e) => {
|
|
133
|
+
error!("Ruby error in next_event: {}", e);
|
|
134
|
+
None
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async fn on_connect(&self) {
|
|
140
|
+
debug!("Ruby SSE producer '{}': on_connect", self.name);
|
|
141
|
+
|
|
142
|
+
if let Some(on_connect_proc) = self.on_connect_proc
|
|
143
|
+
&& let Err(e) = magnus::Ruby::get()
|
|
144
|
+
.map_err(|e| format!("Failed to get Ruby: {}", e))
|
|
145
|
+
.and_then(|ruby| {
|
|
146
|
+
debug!("Ruby SSE producer: on_connect acquired Ruby VM");
|
|
147
|
+
let proc_value = ruby.get_inner(on_connect_proc);
|
|
148
|
+
proc_value
|
|
149
|
+
.funcall::<_, _, Value>("call", ())
|
|
150
|
+
.map_err(|e| format!("on_connect '{}' call failed: {}", self.name, e))?;
|
|
151
|
+
debug!("Ruby SSE producer: on_connect completed");
|
|
152
|
+
Ok(())
|
|
153
|
+
})
|
|
154
|
+
{
|
|
155
|
+
error!("on_connect error: {}", e);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async fn on_disconnect(&self) {
|
|
160
|
+
debug!("Ruby SSE producer '{}': on_disconnect", self.name);
|
|
161
|
+
|
|
162
|
+
if let Some(on_disconnect_proc) = self.on_disconnect_proc
|
|
163
|
+
&& let Err(e) = magnus::Ruby::get()
|
|
164
|
+
.map_err(|e| format!("Failed to get Ruby: {}", e))
|
|
165
|
+
.and_then(|ruby| {
|
|
166
|
+
let proc_value = ruby.get_inner(on_disconnect_proc);
|
|
167
|
+
proc_value
|
|
168
|
+
.funcall::<_, _, Value>("call", ())
|
|
169
|
+
.map_err(|e| format!("on_disconnect '{}' call failed: {}", self.name, e))?;
|
|
170
|
+
debug!("Ruby SSE producer: on_disconnect completed");
|
|
171
|
+
Ok(())
|
|
172
|
+
})
|
|
173
|
+
{
|
|
174
|
+
error!("on_disconnect error: {}", e);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
unsafe impl Send for RubySseEventProducer {}
|
|
180
|
+
unsafe impl Sync for RubySseEventProducer {}
|
|
181
|
+
|
|
182
|
+
/// Create SseState from Ruby producer object
|
|
183
|
+
///
|
|
184
|
+
/// This function is designed to be called from Ruby to register SSE producers.
|
|
185
|
+
#[allow(dead_code)]
|
|
186
|
+
pub fn create_sse_state(
|
|
187
|
+
ruby: &magnus::Ruby,
|
|
188
|
+
producer_obj: Value,
|
|
189
|
+
) -> Result<spikard_http::SseState<RubySseEventProducer>, magnus::Error> {
|
|
190
|
+
let next_event_proc: Value = producer_obj
|
|
191
|
+
.funcall("method", (ruby.to_symbol("next_event"),))
|
|
192
|
+
.map_err(|e| {
|
|
193
|
+
magnus::Error::new(
|
|
194
|
+
ruby.exception_arg_error(),
|
|
195
|
+
format!("next_event method not found: {}", e),
|
|
196
|
+
)
|
|
197
|
+
})?;
|
|
198
|
+
|
|
199
|
+
let on_connect_proc = producer_obj
|
|
200
|
+
.funcall::<_, _, Value>("method", (ruby.to_symbol("on_connect"),))
|
|
201
|
+
.ok();
|
|
202
|
+
|
|
203
|
+
let on_disconnect_proc = producer_obj
|
|
204
|
+
.funcall::<_, _, Value>("method", (ruby.to_symbol("on_disconnect"),))
|
|
205
|
+
.ok();
|
|
206
|
+
|
|
207
|
+
let event_schema = producer_obj
|
|
208
|
+
.funcall::<_, _, Value>("instance_variable_get", (ruby.to_symbol("@_event_schema"),))
|
|
209
|
+
.ok()
|
|
210
|
+
.and_then(|v| {
|
|
211
|
+
if v.is_nil() {
|
|
212
|
+
None
|
|
213
|
+
} else {
|
|
214
|
+
RubySseEventProducer::ruby_to_json(ruby, v).ok()
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
let ruby_producer = RubySseEventProducer::new(
|
|
219
|
+
"SseEventProducer".to_string(),
|
|
220
|
+
next_event_proc,
|
|
221
|
+
on_connect_proc,
|
|
222
|
+
on_disconnect_proc,
|
|
223
|
+
);
|
|
224
|
+
|
|
225
|
+
if event_schema.is_some() {
|
|
226
|
+
spikard_http::SseState::with_schema(ruby_producer, event_schema)
|
|
227
|
+
.map_err(|e| magnus::Error::new(ruby.exception_runtime_error(), e))
|
|
228
|
+
} else {
|
|
229
|
+
Ok(spikard_http::SseState::new(ruby_producer))
|
|
230
|
+
}
|
|
231
|
+
}
|