-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathwrapper.rs
More file actions
541 lines (484 loc) · 18.9 KB
/
Copy pathwrapper.rs
File metadata and controls
541 lines (484 loc) · 18.9 KB
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Wrapper for broadcast channel functionality
//!
//! This module provides broadcast channel wrappers for different use cases:
//!
//! ## `BroadcastChannelWrapper` - JavaScript ↔ Rust Communication (Recommended)
//!
//! This wrapper shares the same underlying channel as JavaScript's `BroadcastChannel` API,
//! enabling bidirectional communication between Rust and JavaScript.
//!
//! This is the **default and recommended** wrapper, maintaining compatibility with
//! origin/master behavior.
//!
//! ## `IsolatedBroadcastChannel` - Rust-to-Rust Communication
//!
//! An isolated channel for Rust-to-Rust communication only.
//! It does NOT share the underlying channel with JavaScript's `BroadcastChannel` API.
//!
//! Use this when you need multiple Rust components to communicate via broadcast channels
//! without involving JavaScript.
//!
//! ## Example
//!
//! ### JavaScript ↔ Rust (BroadcastChannelWrapper)
//! ```rust,ignore
//! use rustyscript::{BroadcastChannelWrapper, Runtime, RuntimeOptions};
//!
//! let options = RuntimeOptions::default();
//! let channel = options.extension_options.broadcast_channel.clone();
//!
//! let mut runtime = Runtime::new(options)?;
//! let wrapper = BroadcastChannelWrapper::new(&channel, "my_channel")?;
//!
//! // Send from Rust to JavaScript
//! wrapper.send_sync(&mut runtime, "hello")?;
//! // JavaScript BroadcastChannel('my_channel') receives this message
//! ```
//!
//! ### Rust ↔ Rust (IsolatedBroadcastChannel)
//! ```rust,ignore
//! use rustyscript::{IsolatedBroadcastChannel, Runtime, RuntimeOptions};
//!
//! let channel = IsolatedBroadcastChannel::new();
//! let mut runtime = Runtime::new(RuntimeOptions::default())?;
//!
//! let sub1 = channel.subscribe("my_channel")?;
//! let sub2 = channel.subscribe("my_channel")?;
//!
//! sub1.send_sync(&mut runtime, "hello")?; // sub2 receives
//! // JavaScript BroadcastChannel does NOT receive this
//! ```
use std::sync::Arc;
use std::time::Duration;
use deno_core::parking_lot::Mutex;
use deno_web::InMemoryBroadcastChannel;
use serde::{de::DeserializeOwned, Serialize};
use tokio::sync::broadcast;
use tokio::sync::mpsc;
use uuid::Uuid;
use crate::{big_json_args, Error, Runtime};
/// Message type matching deno_web's internal InMemoryChannelMessage structure
#[derive(Clone, Debug)]
struct InMemoryChannelMessage {
name: Arc<String>,
data: Arc<Vec<u8>>,
uuid: Uuid,
}
/// Helper struct to wrap a broadcast channel
///
/// Takes care of some of the boilerplate for serialization/deserialization.
/// Messages are serialized through the JavaScript runtime to ensure compatibility
/// with the JavaScript BroadcastChannel API.
///
/// This wrapper shares the same underlying channel as JavaScript's `BroadcastChannel`,
/// enabling bidirectional Rust ↔ JavaScript communication.
pub struct BroadcastChannelWrapper {
sender: Arc<Mutex<broadcast::Sender<InMemoryChannelMessage>>>,
receiver: tokio::sync::Mutex<(
broadcast::Receiver<InMemoryChannelMessage>,
mpsc::UnboundedReceiver<()>,
)>,
cancel_tx: mpsc::UnboundedSender<()>,
name: String,
uuid: Uuid,
}
impl BroadcastChannelWrapper {
/// Create a new broadcast channel wrapper and subscribe to the channel
///
/// This wrapper shares the same underlying channel as JavaScript's BroadcastChannel,
/// enabling bidirectional communication.
///
/// Unsubscribe is called when the wrapper is dropped
///
/// # Safety
/// This function uses unsafe code to access the private field of `InMemoryBroadcastChannel`.
/// The memory layout is stable because it's a simple tuple struct wrapping `Arc<Mutex<...>>`.
///
/// # Errors
/// Will return an error if the channel cannot be subscribed to
pub fn new(channel: &InMemoryBroadcastChannel, name: impl ToString) -> Result<Self, Error> {
// SAFETY: InMemoryBroadcastChannel is repr(Rust) tuple struct with single field:
// pub struct InMemoryBroadcastChannel(Arc<Mutex<broadcast::Sender<InMemoryChannelMessage>>>);
//
// We can access the field by transmuting to the inner type:
let sender: &Arc<Mutex<broadcast::Sender<InMemoryChannelMessage>>> = unsafe {
&*(channel as *const InMemoryBroadcastChannel
as *const Arc<Mutex<broadcast::Sender<InMemoryChannelMessage>>>)
};
let sender = sender.clone();
let (cancel_tx, cancel_rx) = mpsc::unbounded_channel();
let broadcast_rx = sender.lock().subscribe();
let receiver = tokio::sync::Mutex::new((broadcast_rx, cancel_rx));
let uuid = Uuid::new_v4();
let name = name.to_string();
Ok(Self {
sender,
receiver,
cancel_tx,
name,
uuid,
})
}
/// Get the name of this channel
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
/// Send a message to the channel, blocking until the message is sent
///
/// # Errors
/// Will return an error if the message cannot be serialized or sent
pub fn send_sync<T: Serialize>(&self, runtime: &mut Runtime, data: T) -> Result<(), Error> {
let tokio_rt = runtime.tokio_runtime();
tokio_rt.block_on(self.send(runtime, data))
}
/// Send a message to the channel
///
/// # Errors
/// Will return an error if the message cannot be serialized or sent
pub async fn send<T: Serialize>(&self, runtime: &mut Runtime, data: T) -> Result<(), Error> {
// Serialize through JavaScript for compatibility
let data: Vec<u8> = runtime
.call_function_async(None, "broadcast_serialize", &data)
.await?;
let message = InMemoryChannelMessage {
name: Arc::new(self.name.clone()),
data: Arc::new(data),
uuid: self.uuid,
};
self.sender
.lock()
.send(message)
.map_err(|e| Error::Runtime(format!("Failed to send broadcast message: {e}")))?;
Ok(())
}
/// Receive a message from the channel, waiting for a message to arrive,
/// or until the timeout is reached
///
/// Returns `None` if the timeout is reached or the channel is closed
///
/// # Errors
/// Will return an error if the message cannot be deserialized
/// or if receiving the message fails
pub async fn recv<T: DeserializeOwned>(
&self,
runtime: &mut Runtime,
timeout: Option<Duration>,
) -> Result<Option<T>, Error> {
let mut guard = self.receiver.lock().await;
let (broadcast_rx, cancel_rx) = &mut *guard;
loop {
let result = if let Some(timeout) = timeout {
tokio::select! {
r = broadcast_rx.recv() => r,
() = tokio::time::sleep(timeout) => return Ok(None),
_ = cancel_rx.recv() => return Ok(None),
}
} else {
tokio::select! {
r = broadcast_rx.recv() => r,
_ = cancel_rx.recv() => return Ok(None),
}
};
use tokio::sync::broadcast::error::RecvError::*;
match result {
Err(Closed) => return Ok(None),
Err(Lagged(_)) => continue, // Backlogged, messages dropped - try again
Ok(message) if message.uuid == self.uuid => continue, // Self-send, skip
Ok(message) if *message.name != self.name => continue, // Different channel name
Ok(message) => {
// Deserialize through JavaScript for compatibility
let data: T = runtime
.call_function_async(
None,
"broadcast_deserialize",
big_json_args!(Vec::clone(&message.data)),
)
.await?;
return Ok(Some(data));
}
}
}
}
/// Receive a message from the channel, blocking until a message arrives,
/// or until the timeout is reached
///
/// Returns `None` if the timeout is reached or the channel is closed
///
/// # Errors
/// Will return an error if the message cannot be deserialized
/// or if receiving the message fails
pub fn recv_sync<T: DeserializeOwned>(
&self,
runtime: &mut Runtime,
timeout: Option<Duration>,
) -> Result<Option<T>, Error> {
let tokio_rt = runtime.tokio_runtime();
tokio_rt.block_on(self.recv(runtime, timeout))
}
/// Close this subscription
///
/// After calling this, `recv` will return `None`
pub fn close(&self) {
let _ = self.cancel_tx.send(());
}
}
impl Drop for BroadcastChannelWrapper {
fn drop(&mut self) {
self.close();
}
}
// ============================================================================
// IsolatedBroadcastChannel - for Rust-to-Rust communication only
// ============================================================================
/// Message type for internal isolated broadcast channel communication
#[derive(Clone, Debug)]
struct IsolatedChannelMessage {
name: Arc<String>,
data: Arc<Vec<u8>>,
sender_id: Uuid,
}
/// An isolated broadcast channel that can be shared across multiple runtimes
///
/// This is the backing storage for isolated broadcast channel communication.
/// Clone this to share the channel between multiple wrappers.
///
/// **Note**: This channel is isolated and does NOT share messages with JavaScript's
/// `BroadcastChannel` API. For JavaScript integration, use `BroadcastChannelWrapper`.
#[derive(Clone)]
pub struct IsolatedBroadcastChannel {
sender: Arc<Mutex<broadcast::Sender<IsolatedChannelMessage>>>,
}
impl Default for IsolatedBroadcastChannel {
fn default() -> Self {
Self::new()
}
}
impl IsolatedBroadcastChannel {
/// Create a new isolated broadcast channel
#[must_use]
pub fn new() -> Self {
let (sender, _) = broadcast::channel(256);
Self {
sender: Arc::new(Mutex::new(sender)),
}
}
/// Subscribe to this channel, creating a wrapper for sending/receiving messages
///
/// # Errors
/// Will return an error if the subscription cannot be created
pub fn subscribe(&self, name: impl ToString) -> Result<IsolatedBroadcastChannelWrapper, Error> {
IsolatedBroadcastChannelWrapper::new(self, name)
}
}
/// Helper struct to wrap an isolated broadcast channel subscription
///
/// Takes care of some of the boilerplate for serialization/deserialization.
/// Messages are serialized through the JavaScript runtime to ensure compatibility.
///
/// **Note**: This wrapper uses an isolated channel and does NOT communicate with
/// JavaScript's `BroadcastChannel` API.
pub struct IsolatedBroadcastChannelWrapper {
channel: IsolatedBroadcastChannel,
receiver: tokio::sync::Mutex<(
broadcast::Receiver<IsolatedChannelMessage>,
mpsc::UnboundedReceiver<()>,
)>,
cancel_tx: mpsc::UnboundedSender<()>,
name: String,
uuid: Uuid,
}
impl IsolatedBroadcastChannelWrapper {
/// Create a new isolated broadcast channel wrapper and subscribe to the channel
///
/// Unsubscribe is called when the wrapper is dropped
///
/// # Errors
/// Will return an error if the channel cannot be subscribed to
pub fn new(channel: &IsolatedBroadcastChannel, name: impl ToString) -> Result<Self, Error> {
let (cancel_tx, cancel_rx) = mpsc::unbounded_channel();
let broadcast_rx = channel.sender.lock().subscribe();
let receiver = tokio::sync::Mutex::new((broadcast_rx, cancel_rx));
let uuid = Uuid::new_v4();
let name = name.to_string();
Ok(Self {
channel: channel.clone(),
receiver,
cancel_tx,
name,
uuid,
})
}
/// Get the name of this channel
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
/// Send a message to the channel, blocking until the message is sent
///
/// # Errors
/// Will return an error if the message cannot be serialized or sent
pub fn send_sync<T: Serialize>(&self, runtime: &mut Runtime, data: T) -> Result<(), Error> {
let tokio_rt = runtime.tokio_runtime();
tokio_rt.block_on(self.send(runtime, data))
}
/// Send a message to the channel
///
/// # Errors
/// Will return an error if the message cannot be serialized or sent
pub async fn send<T: Serialize>(&self, runtime: &mut Runtime, data: T) -> Result<(), Error> {
// Serialize through JavaScript for compatibility
let data: Vec<u8> = runtime
.call_function_async(None, "broadcast_serialize", &data)
.await?;
let message = IsolatedChannelMessage {
name: Arc::new(self.name.clone()),
data: Arc::new(data),
sender_id: self.uuid,
};
self.channel
.sender
.lock()
.send(message)
.map_err(|e| Error::Runtime(format!("Failed to send broadcast message: {e}")))?;
Ok(())
}
/// Receive a message from the channel, waiting for a message to arrive,
/// or until the timeout is reached
///
/// Returns `None` if the timeout is reached or the channel is closed
///
/// # Errors
/// Will return an error if the message cannot be deserialized
/// or if receiving the message fails
pub async fn recv<T: DeserializeOwned>(
&self,
runtime: &mut Runtime,
timeout: Option<Duration>,
) -> Result<Option<T>, Error> {
let mut guard = self.receiver.lock().await;
let (broadcast_rx, cancel_rx) = &mut *guard;
loop {
let result = if let Some(timeout) = timeout {
tokio::select! {
r = broadcast_rx.recv() => r,
() = tokio::time::sleep(timeout) => return Ok(None),
_ = cancel_rx.recv() => return Ok(None),
}
} else {
tokio::select! {
r = broadcast_rx.recv() => r,
_ = cancel_rx.recv() => return Ok(None),
}
};
use tokio::sync::broadcast::error::RecvError::*;
match result {
Err(Closed) => return Ok(None),
Err(Lagged(_)) => continue, // Backlogged, messages dropped - try again
Ok(message) if message.sender_id == self.uuid => continue, // Self-send, skip
Ok(message) if *message.name != self.name => continue, // Different channel name
Ok(message) => {
// Deserialize through JavaScript for compatibility
let data: T = runtime
.call_function_async(
None,
"broadcast_deserialize",
big_json_args!(Vec::clone(&message.data)),
)
.await?;
return Ok(Some(data));
}
}
}
}
/// Receive a message from the channel, blocking until a message arrives,
/// or until the timeout is reached
///
/// Returns `None` if the timeout is reached or the channel is closed
///
/// # Errors
/// Will return an error if the message cannot be deserialized
/// or if receiving the message fails
pub fn recv_sync<T: DeserializeOwned>(
&self,
runtime: &mut Runtime,
timeout: Option<Duration>,
) -> Result<Option<T>, Error> {
let tokio_rt = runtime.tokio_runtime();
tokio_rt.block_on(self.recv(runtime, timeout))
}
/// Close this subscription
///
/// After calling this, `recv` will return `None`
pub fn close(&self) {
let _ = self.cancel_tx.send(());
}
}
impl Drop for IsolatedBroadcastChannelWrapper {
fn drop(&mut self) {
self.close();
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::{Runtime, RuntimeOptions};
#[test]
fn test_isolated_broadcast_channel_send_recv() {
// This test demonstrates Rust-to-Rust communication via the IsolatedBroadcastChannel.
// Note: This wrapper is for Rust-side communication only.
// For JavaScript BroadcastChannel, use BroadcastChannelWrapper.
let channel = IsolatedBroadcastChannel::new();
// Create a runtime for serialization
let mut runtime = Runtime::new(RuntimeOptions::default()).unwrap();
// Create two subscriptions on the same channel
let wrapper1 = channel.subscribe("test_channel").unwrap();
let wrapper2 = channel.subscribe("test_channel").unwrap();
// Use async to send and receive
let tokio_rt = runtime.tokio_runtime();
tokio_rt.block_on(async {
// Send from wrapper1
let send_result: Result<(), crate::Error> =
wrapper1.send::<&str>(&mut runtime, "hello from rust").await;
send_result.unwrap();
// Receive from wrapper2
let recv_result: Result<Option<String>, crate::Error> = wrapper2
.recv::<String>(&mut runtime, Some(std::time::Duration::from_secs(1)))
.await;
let received: String = recv_result.unwrap().unwrap();
assert_eq!(received, "hello from rust");
});
}
#[test]
fn test_isolated_broadcast_channel_timeout() {
let channel = IsolatedBroadcastChannel::new();
let mut runtime = Runtime::new(RuntimeOptions::default()).unwrap();
let wrapper = channel.subscribe("timeout_test").unwrap();
// Try to receive with a short timeout - should return None
let result = wrapper
.recv_sync::<String>(&mut runtime, Some(std::time::Duration::from_millis(100)))
.unwrap();
assert!(result.is_none());
}
#[test]
fn test_isolated_broadcast_channel_different_names() {
// Messages should only be received by subscriptions with matching names
let channel = IsolatedBroadcastChannel::new();
let mut runtime = Runtime::new(RuntimeOptions::default()).unwrap();
let wrapper_a = channel.subscribe("channel_a").unwrap();
let wrapper_b = channel.subscribe("channel_b").unwrap();
let tokio_rt = runtime.tokio_runtime();
tokio_rt.block_on(async {
// Send to channel_a
let send_result: Result<(), crate::Error> =
wrapper_a.send::<&str>(&mut runtime, "message for a").await;
send_result.unwrap();
// wrapper_b should not receive this message (different channel name)
let recv_result: Result<Option<String>, crate::Error> = wrapper_b
.recv::<String>(&mut runtime, Some(std::time::Duration::from_millis(100)))
.await;
let result: Option<String> = recv_result.unwrap();
assert!(result.is_none());
});
}
}