fetchlet 0.1.1

Rust-hosted JavaScript functions and Fetch handlers with explicit host services
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
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
use std::future::{Future, poll_fn};
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll, Waker};

use futures_core::Stream;
use futures_sink::Sink;
use tokio::sync::{AcquireError, OwnedSemaphorePermit, Semaphore, mpsc};

use super::{CloseFrame, Message};
use crate::config::check_limit;
use crate::{Error, Limits, Result};

/// What one end of a channel receives: a message, a socket's signal, or the
/// close that ends it.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ChannelEvent {
    Message(Message),
    /// The other end sent its last message and still reads, as a TCP half
    /// close. Only sockets carry it: JavaScript sends it when a socket's
    /// writable closes, and a socket transport with [`ChannelEnd::finish`]
    /// when the peer's bytes end.
    End,
    /// The connection speaks TLS from this point of the stream. JavaScript
    /// sends it from `startTls()` on a socket opened with
    /// [`SecureTransport::StartTls`](crate::SecureTransport::StartTls), and
    /// the transport answers with [`ChannelEnd::confirm_tls`] once the
    /// handshake completes.
    StartTls,
    Close(CloseFrame),
}

/// The budgets of one channel, taken from the invocation's [`Limits`].
#[derive(Clone, Copy)]
struct Budgets {
    /// `Limits::channel_message_bytes`.
    message_bytes: Option<usize>,
    /// `Limits::channel_buffer_bytes`, bounded by what one semaphore
    /// acquisition can take.
    buffer_bytes: Option<usize>,
}

impl Budgets {
    /// The queue space a message takes: its size, at least one byte so that
    /// empty messages also wait, and at most the whole queue so that one
    /// message within the message budget can always pass.
    fn cost(&self, len: usize) -> u32 {
        let cost = len.max(1).min(self.buffer_bytes.unwrap_or(usize::MAX));
        u32::try_from(cost).unwrap_or(u32::MAX)
    }
}

/// Room in JavaScript's queue for the message waiting to go out.
type Acquire = Pin<
    Box<dyn Future<Output = std::result::Result<OwnedSemaphorePermit, AcquireError>> + Send + Sync>,
>;

/// A message waiting for room in JavaScript's queue.
struct Pending {
    message: Message,
    room: Option<Acquire>,
}

/// Rust's end of a channel whose other end is JavaScript in one invocation.
///
/// Messages arrive in the order the other end sent them, and a close arrives
/// once, after them. An end that goes away without closing reports
/// [`CloseFrame::ABNORMAL`]. Dropping this end closes the channel that way
/// for JavaScript.
///
/// Each direction has a queue bounded in bytes by
/// `Limits::channel_buffer_bytes`. [`ChannelEnd::send`] waits for room, so
/// a slow JavaScript reader slows the peer that feeds this end.
///
/// The end is also a [`Stream`] of the [`ChannelEvent`]s that
/// [`recv`](Self::recv) returns and a [`Sink`] of [`Message`]s, for
/// combinators such as `forward` and `split`. The sink holds one message
/// while it waits for room; closing the sink flushes it and closes the
/// channel normally.
pub struct ChannelEnd {
    to_guest: mpsc::UnboundedSender<ChannelEvent>,
    from_guest: mpsc::UnboundedReceiver<ChannelEvent>,
    /// Queue bytes toward JavaScript still free; JavaScript returns them as
    /// it receives messages, and closes it when its end goes away.
    credit: Option<Arc<Semaphore>>,
    /// Bytes JavaScript sent that this end has not received.
    buffered: Arc<AtomicUsize>,
    /// JavaScript's end waiting for room in its queue, woken as this end
    /// receives.
    room: Arc<Mutex<Option<Waker>>>,
    budgets: Budgets,
    /// The message the sink accepted, until it fits JavaScript's queue.
    pending: Option<Pending>,
    /// This end sent its close.
    closed: bool,
    /// This end sent its last message.
    ended: bool,
    /// This end received a close, or reported the abnormal end.
    finished: bool,
}

impl std::fmt::Debug for ChannelEnd {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("ChannelEnd")
            .field("closed", &self.closed)
            .field("finished", &self.finished)
            .finish_non_exhaustive()
    }
}

impl ChannelEnd {
    /// Queue a message for JavaScript, waiting while its queue is full.
    ///
    /// Fails with `LimitExceeded` for a message over
    /// `Limits::channel_message_bytes`, and with `InvocationClosed` once this
    /// end has closed or JavaScript's end has gone away.
    ///
    /// A message the sink holds goes out first. Dropped while it waits, the
    /// message stays held and goes out before the next one.
    pub async fn send(&mut self, message: impl Into<Message>) -> Result<()> {
        poll_fn(|context| self.poll_pending(context)).await?;
        self.hold(message.into())?;
        poll_fn(|context| self.poll_pending(context)).await
    }

    /// Hold `message` until JavaScript's queue has room for it.
    fn hold(&mut self, message: Message) -> Result<()> {
        if self.closed || self.ended {
            return Err(Error::InvocationClosed);
        }
        check_limit(
            self.budgets.message_bytes,
            message.len(),
            "channel message bytes",
        )?;
        let room = self.credit.as_ref().map(|credit| {
            let cost = self.budgets.cost(message.len());
            Box::pin(Arc::clone(credit).acquire_many_owned(cost)) as Acquire
        });
        self.pending = Some(Pending { message, room });
        Ok(())
    }

    /// Queue the held message once it has room.
    fn poll_pending(&mut self, context: &mut Context<'_>) -> Poll<Result<()>> {
        let Some(pending) = &mut self.pending else {
            return Poll::Ready(Ok(()));
        };
        if let Some(room) = &mut pending.room {
            match room.as_mut().poll(context) {
                Poll::Pending => return Poll::Pending,
                Poll::Ready(Ok(permit)) => permit.forget(),
                Poll::Ready(Err(_)) => {
                    self.pending = None;
                    return Poll::Ready(Err(Error::InvocationClosed));
                }
            }
        }
        let Some(Pending { message, .. }) = self.pending.take() else {
            return Poll::Ready(Ok(()));
        };
        Poll::Ready(
            self.to_guest
                .send(ChannelEvent::Message(message))
                .map_err(|_| Error::InvocationClosed),
        )
    }

    /// Close the channel. JavaScript sees the close after the messages sent
    /// before it; a message the sink still holds for room is dropped.
    /// Closing twice sends one close.
    pub fn close(&mut self, frame: CloseFrame) {
        self.pending = None;
        if !self.closed {
            self.closed = true;
            let _ = self.to_guest.send(ChannelEvent::Close(frame));
        }
    }

    /// End this end's messages and keep receiving: a socket's half close,
    /// such as when the peer's bytes end. JavaScript's readable ends; later
    /// sends fail with `InvocationClosed`. Ending twice sends one end.
    pub fn finish(&mut self) {
        if !self.closed && !self.ended {
            self.ended = true;
            let _ = self.to_guest.send(ChannelEvent::End);
        }
    }

    /// Answer JavaScript's [`ChannelEvent::StartTls`] once the connection
    /// speaks TLS: the socket `startTls()` returned opens.
    pub fn confirm_tls(&mut self) {
        if !self.closed && !self.ended {
            let _ = self.to_guest.send(ChannelEvent::StartTls);
        }
    }

    /// The next message or signal from JavaScript, or its close, then None.
    pub async fn recv(&mut self) -> Option<ChannelEvent> {
        poll_fn(|context| self.poll_recv(context)).await
    }

    /// Poll for the next message or signal from JavaScript, or its close,
    /// then None.
    pub fn poll_recv(&mut self, context: &mut Context<'_>) -> Poll<Option<ChannelEvent>> {
        if self.finished {
            return Poll::Ready(None);
        }
        let event = match self.from_guest.poll_recv(context) {
            Poll::Pending => return Poll::Pending,
            Poll::Ready(Some(ChannelEvent::Message(message))) => {
                self.buffered.fetch_sub(message.len(), Ordering::AcqRel);
                if let Some(waker) = lock(&self.room).take() {
                    waker.wake();
                }
                return Poll::Ready(Some(ChannelEvent::Message(message)));
            }
            Poll::Ready(Some(ChannelEvent::Close(frame))) => ChannelEvent::Close(frame),
            Poll::Ready(Some(signal)) => return Poll::Ready(Some(signal)),
            Poll::Ready(None) => ChannelEvent::Close(CloseFrame::abnormal()),
        };
        self.finished = true;
        Poll::Ready(Some(event))
    }

    /// Whether this end has closed.
    pub fn is_closed(&self) -> bool {
        self.closed
    }

    /// The largest message this end may send:
    /// `Limits::channel_message_bytes`, or None without that budget. A
    /// transport reading a stream sizes its reads by it.
    pub fn max_message_bytes(&self) -> Option<usize> {
        self.budgets.message_bytes
    }

    /// Wait until JavaScript's end goes away, as it does when the invocation
    /// ends. A transport blocked on its peer, such as a socket write, races
    /// this so it does not outlive the invocation.
    pub async fn guest_gone(&self) {
        self.to_guest.closed().await;
    }

    /// Whether JavaScript's end went away.
    #[cfg(feature = "fuzzing")]
    pub(crate) fn peer_gone(&self) -> bool {
        self.to_guest.is_closed()
    }
}

impl Stream for ChannelEnd {
    type Item = ChannelEvent;

    fn poll_next(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Option<ChannelEvent>> {
        self.get_mut().poll_recv(context)
    }
}

impl Sink<Message> for ChannelEnd {
    type Error = Error;

    /// Ready once the message held before has room, failing with
    /// `InvocationClosed` after a close or the end of messages.
    fn poll_ready(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Result<()>> {
        let end = self.get_mut();
        match end.poll_pending(context) {
            Poll::Ready(Ok(())) if end.closed || end.ended => {
                Poll::Ready(Err(Error::InvocationClosed))
            }
            polled => polled,
        }
    }

    /// Hold a message until it has room; fails as [`ChannelEnd::send`]
    /// does for a message over the budget.
    fn start_send(self: Pin<&mut Self>, message: Message) -> Result<()> {
        self.get_mut().hold(message)
    }

    fn poll_flush(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Result<()>> {
        self.get_mut().poll_pending(context)
    }

    /// Flush the held message, then close the channel with
    /// [`CloseFrame::NORMAL`].
    fn poll_close(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Result<()>> {
        let end = self.get_mut();
        let flushed = end.poll_pending(context);
        if flushed.is_pending() {
            return Poll::Pending;
        }
        end.close(CloseFrame::new(CloseFrame::NORMAL, ""));
        flushed
    }
}

impl Drop for ChannelEnd {
    fn drop(&mut self) {
        // A JavaScript write waiting for room learns that nobody will read.
        if let Some(waker) = lock(&self.room).take() {
            waker.wake();
        }
    }
}

/// JavaScript's end of a channel, kept in its invocation's state.
pub(crate) struct GuestEnd {
    to_host: mpsc::UnboundedSender<ChannelEvent>,
    from_host: mpsc::UnboundedReceiver<ChannelEvent>,
    credit: Option<Arc<Semaphore>>,
    buffered: Arc<AtomicUsize>,
    room: Arc<Mutex<Option<Waker>>>,
    budgets: Budgets,
    closed: bool,
    /// JavaScript sent its last message: a socket's writable closed.
    ended: bool,
    finished: bool,
}

fn lock(room: &Mutex<Option<Waker>>) -> std::sync::MutexGuard<'_, Option<Waker>> {
    room.lock().unwrap_or_else(|error| error.into_inner())
}

/// Why JavaScript's end could not queue a message.
pub(crate) enum Refused {
    /// The end has closed or the channel has ended; the message is dropped,
    /// as WebSocket drops messages sent after a close.
    Closed,
    /// The message or the queue exceeded its budget.
    TooBig,
}

impl GuestEnd {
    /// Queue a message for Rust. JavaScript does not wait: a message over a
    /// budget is refused, and the caller closes the channel.
    pub fn send(&mut self, message: Message) -> std::result::Result<(), Refused> {
        if self.closed || self.ended || self.finished {
            return Err(Refused::Closed);
        }
        let len = message.len();
        if check_limit(self.budgets.message_bytes, len, "channel message bytes").is_err() {
            return Err(Refused::TooBig);
        }
        let buffered = self.buffered.load(Ordering::Acquire).saturating_add(len);
        if check_limit(self.budgets.buffer_bytes, buffered, "channel buffer bytes").is_err() {
            return Err(Refused::TooBig);
        }
        self.buffered.fetch_add(len, Ordering::AcqRel);
        // A Rust end that went away reports the abnormal close through recv.
        let _ = self.to_host.send(ChannelEvent::Message(message));
        Ok(())
    }

    /// Send JavaScript's last message mark to Rust, once: a socket's half
    /// close. Later messages are refused as after a close.
    pub fn end(&mut self) {
        if !self.ended {
            self.ended = true;
            if !self.closed {
                let _ = self.to_host.send(ChannelEvent::End);
            }
        }
    }

    /// Whether JavaScript ended its messages.
    pub fn is_ended(&self) -> bool {
        self.ended
    }

    /// Ask Rust to switch the connection to TLS at this point of the stream.
    pub fn start_tls(&mut self) {
        if !self.closed && !self.ended {
            let _ = self.to_host.send(ChannelEvent::StartTls);
        }
    }

    /// Send JavaScript's close to Rust, once.
    pub fn close(&mut self, frame: CloseFrame) {
        if !self.closed {
            self.closed = true;
            let _ = self.to_host.send(ChannelEvent::Close(frame));
        }
    }

    /// The bytes one message may have now: the queue's free space, at most
    /// the message budget. Pending, and woken as Rust receives, while the
    /// queue is full. Zero only when the message budget is zero.
    pub fn poll_room(&mut self, context: &mut Context<'_>) -> Poll<usize> {
        let message = self.budgets.message_bytes.unwrap_or(usize::MAX);
        let Some(queue) = self.budgets.buffer_bytes else {
            return Poll::Ready(message);
        };
        // With Rust's end gone, the send that follows fails the write.
        if self.to_host.is_closed() {
            return Poll::Ready(message.max(1));
        }
        // Registered before the check, so a receive in between still wakes.
        *lock(&self.room) = Some(context.waker().clone());
        let free = queue.saturating_sub(self.buffered.load(Ordering::Acquire));
        if free == 0 && message > 0 {
            return Poll::Pending;
        }
        lock(&self.room).take();
        Poll::Ready(free.min(message))
    }

    /// Bytes sent that Rust has not received yet: WebSocket's
    /// `bufferedAmount`.
    pub fn buffered(&self) -> usize {
        self.buffered.load(Ordering::Acquire)
    }

    pub fn is_closed(&self) -> bool {
        self.closed
    }

    /// The next event from Rust. A Rust end that went away without closing
    /// yields an abnormal close. After a close it yields None.
    pub fn poll_recv(&mut self, context: &mut Context<'_>) -> Poll<Option<ChannelEvent>> {
        if self.finished {
            return Poll::Ready(None);
        }
        let event = match self.from_host.poll_recv(context) {
            Poll::Pending => return Poll::Pending,
            Poll::Ready(Some(ChannelEvent::Message(message))) => {
                if let Some(credit) = &self.credit {
                    credit.add_permits(self.budgets.cost(message.len()) as usize);
                }
                return Poll::Ready(Some(ChannelEvent::Message(message)));
            }
            Poll::Ready(Some(ChannelEvent::Close(frame))) => ChannelEvent::Close(frame),
            Poll::Ready(Some(signal)) => return Poll::Ready(Some(signal)),
            Poll::Ready(None) => ChannelEvent::Close(CloseFrame::abnormal()),
        };
        self.finished = true;
        Poll::Ready(Some(event))
    }
}

impl Drop for GuestEnd {
    fn drop(&mut self) {
        // A Rust sender waiting for room learns that nobody will read.
        if let Some(credit) = &self.credit {
            credit.close();
        }
    }
}

/// A new channel within the budgets of `limits`.
pub(crate) fn pair(limits: &Limits) -> (ChannelEnd, GuestEnd) {
    let budgets = Budgets {
        message_bytes: limits.channel_message_bytes,
        buffer_bytes: limits
            .channel_buffer_bytes
            .map(|bytes| bytes.min(u32::MAX as usize)),
    };
    let (to_guest, from_host) = mpsc::unbounded_channel();
    let (to_host, from_guest) = mpsc::unbounded_channel();
    let credit = budgets
        .buffer_bytes
        .map(|bytes| Arc::new(Semaphore::new(bytes)));
    let buffered = Arc::new(AtomicUsize::new(0));
    let room = Arc::default();
    let host = ChannelEnd {
        to_guest,
        from_guest,
        credit: credit.clone(),
        buffered: buffered.clone(),
        room: Arc::clone(&room),
        budgets,
        pending: None,
        closed: false,
        ended: false,
        finished: false,
    };
    let guest = GuestEnd {
        to_host,
        from_host,
        credit,
        buffered,
        room,
        budgets,
        closed: false,
        ended: false,
        finished: false,
    };
    (host, guest)
}

#[cfg(test)]
mod tests {
    use std::future::poll_fn;
    use std::time::Duration;

    use super::*;

    fn guest_message(guest: &mut GuestEnd) -> Poll<Option<ChannelEvent>> {
        let waker = Waker::noop();
        guest.poll_recv(&mut Context::from_waker(waker))
    }

    #[tokio::test]
    async fn a_send_dropped_while_it_waits_keeps_its_place() {
        let limits = Limits {
            channel_buffer_bytes: Some(4),
            ..Limits::default()
        };
        let (mut end, mut guest) = pair(&limits);
        end.send("full").await.unwrap();
        // No room: the send is dropped while it waits.
        let waited = tokio::time::timeout(Duration::from_millis(20), end.send("held")).await;
        assert!(waited.is_err());

        assert_eq!(
            guest_message(&mut guest),
            Poll::Ready(Some(ChannelEvent::Message("full".into())))
        );
        // The held message goes out first, and the next one waits for it.
        let waited = tokio::time::timeout(Duration::from_millis(20), end.send("next")).await;
        assert!(waited.is_err());
        assert_eq!(
            guest_message(&mut guest),
            Poll::Ready(Some(ChannelEvent::Message("held".into())))
        );
        assert_eq!(guest_message(&mut guest), Poll::Pending);
        poll_fn(|context| Pin::new(&mut end).poll_flush(context))
            .await
            .unwrap();
        assert_eq!(
            guest_message(&mut guest),
            Poll::Ready(Some(ChannelEvent::Message("next".into())))
        );
    }

    #[tokio::test]
    async fn close_drops_a_held_message_and_the_sink_refuses_after_it() {
        let limits = Limits {
            channel_buffer_bytes: Some(4),
            ..Limits::default()
        };
        let (mut end, mut guest) = pair(&limits);
        end.send("full").await.unwrap();
        let waited = tokio::time::timeout(Duration::from_millis(20), end.send("held")).await;
        assert!(waited.is_err());
        end.close(CloseFrame::new(CloseFrame::NORMAL, ""));
        assert_eq!(
            guest_message(&mut guest),
            Poll::Ready(Some(ChannelEvent::Message("full".into())))
        );
        assert_eq!(
            guest_message(&mut guest),
            Poll::Ready(Some(ChannelEvent::Close(CloseFrame::new(
                CloseFrame::NORMAL,
                ""
            ))))
        );
        let ready = poll_fn(|context| Pin::new(&mut end).poll_ready(context)).await;
        assert!(matches!(ready, Err(Error::InvocationClosed)));
    }
}