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
use std::fmt;
use std::pin::Pin;
use std::task::{Context, Poll};

use bytes::{Bytes, BytesMut};
use http_body::{Frame, SizeHint};
use tokio::sync::mpsc;

use crate::config::check_limit;
use crate::{Error, Result};

/// One message from a body producer: a data chunk and whether it is the last one.
///
/// A producer that stops without sending a last chunk aborted the body; the
/// consumer then observes [`Error::InvocationClosed`].
pub(crate) type BodyChunk = Result<(Bytes, bool)>;

#[cfg_attr(
    not(any(feature = "boa", feature = "quickjs", feature = "v8")),
    allow(dead_code)
)]
pub(crate) type BodySender = mpsc::Sender<BodyChunk>;

/// An HTTP body owned by fetchlet.
///
/// A body is buffered, streamed from the worker that owns a JavaScript
/// invocation, or streamed from another `http_body::Body`. Streaming bodies
/// from a worker hold that invocation's lease until they finish or are
/// dropped; dropping one cancels the JavaScript stream.
#[derive(Debug, Default)]
pub struct Body {
    inner: Inner,
}

#[cfg_attr(
    not(any(feature = "boa", feature = "quickjs", feature = "v8")),
    allow(dead_code)
)]
#[derive(Debug)]
enum Inner {
    Buffered(Option<Bytes>),
    Streaming {
        prefix: Option<Bytes>,
        receiver: mpsc::Receiver<BodyChunk>,
        finished: bool,
    },
    External(ExternalBody),
}

/// A caller-supplied body, read on demand by the invocation that receives it.
pub(crate) struct ExternalBody(Pin<Box<dyn http_body::Body<Data = Bytes, Error = Error> + Send>>);

impl fmt::Debug for ExternalBody {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("ExternalBody")
    }
}

/// A streaming body that fails once it has produced more than `limit` bytes.
struct Limited {
    body: Body,
    read: usize,
    limit: usize,
    resource: &'static str,
}

impl http_body::Body for Limited {
    type Data = Bytes;

    type Error = Error;

    fn poll_frame(
        self: Pin<&mut Self>,
        context: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Bytes>>>> {
        let this = self.get_mut();
        let frame = match Pin::new(&mut this.body).poll_frame(context) {
            Poll::Ready(Some(Ok(frame))) => frame,
            other => return other,
        };
        if let Some(chunk) = frame.data_ref() {
            this.read = this.read.saturating_add(chunk.len());
            if this.read > this.limit {
                return Poll::Ready(Some(Err(Error::LimitExceeded(this.resource))));
            }
        }
        Poll::Ready(Some(Ok(frame)))
    }

    fn is_end_stream(&self) -> bool {
        self.body.is_end_stream()
    }

    fn size_hint(&self) -> SizeHint {
        self.body.size_hint()
    }
}

/// Adapts the data and error types of a caller-supplied body.
struct MapBody<B>(B);

impl<B> http_body::Body for MapBody<B>
where
    B: http_body::Body + Unpin,
    B::Data: Into<Bytes>,
    B::Error: fmt::Display,
{
    type Data = Bytes;

    type Error = Error;

    fn poll_frame(
        self: Pin<&mut Self>,
        context: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Bytes>>>> {
        let inner = Pin::new(&mut self.get_mut().0);
        inner.poll_frame(context).map(|frame| {
            frame.map(|frame| {
                frame
                    .map(|frame| frame.map_data(Into::into))
                    .map_err(|error| Error::Transport(error.to_string()))
            })
        })
    }

    fn is_end_stream(&self) -> bool {
        self.0.is_end_stream()
    }

    fn size_hint(&self) -> SizeHint {
        self.0.size_hint()
    }
}

impl Default for Inner {
    fn default() -> Self {
        Self::Buffered(None)
    }
}

impl Body {
    pub fn empty() -> Self {
        Self::default()
    }

    /// A streaming body whose chunks arrive through a bounded channel.
    #[cfg_attr(
        not(any(
            feature = "quickjs",
            feature = "boa",
            feature = "v8",
            feature = "fuzzing",
            test
        )),
        allow(dead_code)
    )]
    pub(crate) fn channel(prefix: Bytes, capacity: usize) -> (BodySender, Self) {
        let (sender, receiver) = mpsc::channel(capacity);
        let body = Self {
            inner: Inner::Streaming {
                prefix: (!prefix.is_empty()).then_some(prefix),
                receiver,
                finished: false,
            },
        };
        (sender, body)
    }

    /// Stream another HTTP body, such as an incoming server request body.
    ///
    /// The receiving invocation reads it on demand and applies its body budget
    /// to the bytes it reads. Trailers are not forwarded. Errors from `body`
    /// reach JavaScript as a general transport error; their detail stays in Rust.
    pub fn from_http_body<B>(body: B) -> Self
    where
        B: http_body::Body + Send + 'static,
        B::Data: Into<Bytes>,
        B::Error: fmt::Display,
    {
        if body.is_end_stream() {
            return Self::empty();
        }
        Self {
            inner: Inner::External(ExternalBody(Box::pin(MapBody(Box::pin(body))))),
        }
    }

    /// Stream a body of fetchlet's own, whose errors, such as
    /// `LimitExceeded`, reach the caller as they are.
    #[cfg_attr(
        not(any(
            feature = "boa",
            feature = "quickjs",
            feature = "v8",
            feature = "fuzzing",
            test
        )),
        allow(dead_code)
    )]
    pub(crate) fn from_fetchlet_body(
        body: impl http_body::Body<Data = Bytes, Error = Error> + Send + 'static,
    ) -> Self {
        Self {
            inner: Inner::External(ExternalBody(Box::pin(body))),
        }
    }

    /// The length the body declares at least: exact when buffered, otherwise
    /// its size hint, such as a `Content-Length`.
    pub(crate) fn declared_len(&self) -> usize {
        usize::try_from(http_body::Body::size_hint(self).lower()).unwrap_or(usize::MAX)
    }

    /// Whether every byte of the body is already in memory.
    #[cfg_attr(
        not(any(
            feature = "quickjs",
            feature = "boa",
            feature = "v8",
            feature = "fuzzing",
            test
        )),
        allow(dead_code)
    )]
    pub(crate) fn is_buffered(&self) -> bool {
        matches!(self.inner, Inner::Buffered(_))
    }

    /// Collect the body, rejecting a result larger than `limit` bytes.
    pub async fn collect(self, limit: usize) -> Result<Bytes> {
        self.collect_within(Some(limit), "body bytes").await
    }

    pub(crate) async fn collect_within(
        self,
        limit: Option<usize>,
        resource: &'static str,
    ) -> Result<Bytes> {
        match self.inner {
            Inner::Buffered(data) => {
                let data = data.unwrap_or_default();
                check_limit(limit, data.len(), resource)?;
                Ok(data)
            }
            Inner::Streaming {
                prefix,
                mut receiver,
                finished,
            } => {
                let mut data = BytesMut::new();
                if let Some(prefix) = prefix {
                    check_limit(limit, prefix.len(), resource)?;
                    data.extend_from_slice(&prefix);
                }
                if finished {
                    return Ok(data.freeze());
                }
                loop {
                    let (chunk, last) = receiver.recv().await.ok_or(Error::InvocationClosed)??;
                    let size = data
                        .len()
                        .checked_add(chunk.len())
                        .ok_or(Error::LimitExceeded(resource))?;
                    check_limit(limit, size, resource)?;
                    data.extend_from_slice(&chunk);
                    if last {
                        return Ok(data.freeze());
                    }
                }
            }
            Inner::External(ExternalBody(mut body)) => {
                let mut data = BytesMut::new();
                while let Some(frame) =
                    std::future::poll_fn(|context| body.as_mut().poll_frame(context)).await
                {
                    let Ok(chunk) = frame?.into_data() else {
                        continue;
                    };
                    let size = data
                        .len()
                        .checked_add(chunk.len())
                        .ok_or(Error::LimitExceeded(resource))?;
                    check_limit(limit, size, resource)?;
                    data.extend_from_slice(&chunk);
                }
                Ok(data.freeze())
            }
        }
    }

    /// Fail a streaming body with `LimitExceeded(resource)` once more than
    /// `limit` bytes have been read from it. A buffered body is returned as
    /// it is; check its length directly.
    pub(crate) fn limited(self, limit: Option<usize>, resource: &'static str) -> Self {
        match (limit, &self.inner) {
            (None, _) | (_, Inner::Buffered(_)) => self,
            (Some(limit), _) => Self {
                inner: Inner::External(ExternalBody(Box::pin(Limited {
                    body: self,
                    read: 0,
                    limit,
                    resource,
                }))),
            },
        }
    }

    /// Buffer a streaming body so that synchronous host checks can inspect it.
    pub(crate) async fn into_buffered(
        self,
        limit: Option<usize>,
        resource: &'static str,
    ) -> Result<Self> {
        match self.inner {
            Inner::Buffered(_) => {
                check_limit(limit, self.buffered()?.len(), resource)?;
                Ok(self)
            }
            Inner::Streaming { .. } | Inner::External(_) => {
                Ok(self.collect_within(limit, resource).await?.into())
            }
        }
    }

    /// The bytes of a buffered body, without copying.
    #[cfg_attr(
        not(any(
            feature = "quickjs",
            feature = "boa",
            feature = "v8",
            feature = "fuzzing",
            test
        )),
        allow(dead_code)
    )]
    pub(crate) fn into_bytes(self) -> Result<Bytes> {
        match self.inner {
            Inner::Buffered(data) => Ok(data.unwrap_or_default()),
            Inner::Streaming { .. } | Inner::External(_) => Err(Error::InvalidRequest(
                "a streaming body must be collected before this operation".into(),
            )),
        }
    }

    /// The bytes of a buffered body. Streaming bodies are collected first.
    pub(crate) fn buffered(&self) -> Result<&[u8]> {
        match &self.inner {
            Inner::Buffered(data) => Ok(data.as_deref().unwrap_or_default()),
            Inner::Streaming { .. } | Inner::External(_) => Err(Error::InvalidRequest(
                "a streaming body must be collected before this operation".into(),
            )),
        }
    }
}

impl From<Bytes> for Body {
    fn from(data: Bytes) -> Self {
        Self {
            inner: Inner::Buffered((!data.is_empty()).then_some(data)),
        }
    }
}

impl From<Vec<u8>> for Body {
    fn from(data: Vec<u8>) -> Self {
        Bytes::from(data).into()
    }
}

impl From<String> for Body {
    fn from(data: String) -> Self {
        Bytes::from(data).into()
    }
}

impl From<&'static str> for Body {
    fn from(data: &'static str) -> Self {
        Bytes::from_static(data.as_bytes()).into()
    }
}

impl http_body::Body for Body {
    type Data = Bytes;

    type Error = Error;

    fn poll_frame(
        self: Pin<&mut Self>,
        context: &mut Context<'_>,
    ) -> Poll<Option<Result<Frame<Bytes>>>> {
        match &mut self.get_mut().inner {
            Inner::Buffered(data) => Poll::Ready(data.take().map(|data| Ok(Frame::data(data)))),
            Inner::Streaming {
                prefix,
                receiver,
                finished,
            } => {
                if let Some(prefix) = prefix.take() {
                    return Poll::Ready(Some(Ok(Frame::data(prefix))));
                }
                loop {
                    if *finished {
                        return Poll::Ready(None);
                    }
                    match receiver.poll_recv(context) {
                        Poll::Pending => return Poll::Pending,
                        Poll::Ready(None) => {
                            *finished = true;
                            return Poll::Ready(Some(Err(Error::InvocationClosed)));
                        }
                        Poll::Ready(Some(Err(error))) => {
                            *finished = true;
                            return Poll::Ready(Some(Err(error)));
                        }
                        Poll::Ready(Some(Ok((chunk, last)))) => {
                            *finished = last;
                            if !chunk.is_empty() {
                                return Poll::Ready(Some(Ok(Frame::data(chunk))));
                            }
                        }
                    }
                }
            }
            Inner::External(ExternalBody(body)) => loop {
                // Trailers are not part of the Fetch body.
                match body.as_mut().poll_frame(context) {
                    Poll::Ready(Some(Ok(frame))) if !frame.is_data() => continue,
                    other => return other,
                }
            },
        }
    }

    fn is_end_stream(&self) -> bool {
        match &self.inner {
            Inner::Buffered(data) => data.is_none(),
            Inner::Streaming {
                prefix, finished, ..
            } => prefix.is_none() && *finished,
            Inner::External(ExternalBody(body)) => body.is_end_stream(),
        }
    }

    fn size_hint(&self) -> SizeHint {
        match &self.inner {
            Inner::Buffered(data) => {
                SizeHint::with_exact(data.as_ref().map_or(0, |data| data.len() as u64))
            }
            Inner::Streaming { .. } => SizeHint::default(),
            Inner::External(ExternalBody(body)) => body.size_hint(),
        }
    }
}