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
use std::collections::BTreeMap;
use std::sync::Arc;
use super::FetchService;
use super::channel::ChannelService;
use super::console::{ConsoleLevel, ConsoleSink};
use super::function::Functions;
use super::socket::SocketService;
use super::websocket::WebSocketService;
use crate::{Error, Result};
/// Fixed bindings and services for one App. No process environment is inherited.
#[derive(Clone, Default)]
pub struct HostServices {
pub(crate) outbound: Option<FetchService>,
pub(crate) services: BTreeMap<String, FetchService>,
pub(crate) channels: BTreeMap<String, Arc<dyn ChannelService>>,
pub(crate) sockets: Option<SocketService>,
pub(crate) websockets: Option<WebSocketService>,
pub(crate) env: BTreeMap<String, String>,
pub(crate) process_env: BTreeMap<String, String>,
pub(crate) functions: BTreeMap<String, Functions>,
pub(crate) console: Option<ConsoleSink>,
}
impl HostServices {
pub fn new() -> Self {
Self::default()
}
pub fn outbound(mut self, service: FetchService) -> Self {
self.outbound = Some(service);
self
}
pub fn service(mut self, name: impl Into<String>, service: FetchService) -> Result<Self> {
let name = self.unbound(name.into())?;
self.services.insert(name, service);
Ok(self)
}
/// Serve `connect(address, options)` from `fetchlet/runtime`: each
/// connection the service's policy allows opens through its transport,
/// as a channel of the invocation. Without a socket service, `connect`
/// fails with `PermissionDenied`.
pub fn sockets(mut self, service: SocketService) -> Self {
self.sockets = Some(service);
self
}
/// Serve the global `WebSocket` of the Web API profile: each connection
/// to an origin the service's policy allows opens through its
/// transport, as a channel of the invocation. Without a WebSocket
/// service, a `WebSocket` fails as a refused connection.
pub fn websockets(mut self, service: WebSocketService) -> Self {
self.websockets = Some(service);
self
}
/// Bind a channel service under `name` in `env`. JavaScript opens a
/// channel with `env.NAME.connect(options)`; the channel belongs to that
/// invocation and closes when it ends.
pub fn channel(
mut self,
name: impl Into<String>,
service: impl ChannelService,
) -> Result<Self> {
let name = self.unbound(name.into())?;
self.channels.insert(name, Arc::new(service));
Ok(self)
}
/// Bind Rust functions under `name` in `env`. JavaScript calls each
/// method as `env.NAME.method(...args)`; see [`Functions`] for how
/// arguments, results and errors cross. A method named `then`, or one
/// added twice, fails with `Configuration`.
pub fn functions(mut self, name: impl Into<String>, functions: Functions) -> Result<Self> {
functions.validate()?;
let name = self.unbound(name.into())?;
self.functions.insert(name, functions);
Ok(self)
}
/// `name`, if no binding has it yet: env values, services, channels and
/// functions share one namespace.
fn unbound(&self, name: String) -> Result<String> {
if self.services.contains_key(&name)
|| self.env.contains_key(&name)
|| self.channels.contains_key(&name)
|| self.functions.contains_key(&name)
{
return Err(Error::Configuration("duplicate binding".into()));
}
Ok(name)
}
/// Sends `console.debug`, `log`, `info`, `warn` and `error` of the App's
/// JavaScript to `sink`. Without a sink, console output is discarded.
/// Each message is one bridge message, so `Limits::bridge_message_bytes`
/// bounds its size; a longer message arrives as a notice that it was dropped.
pub fn console(mut self, sink: impl Fn(ConsoleLevel, &str) + Send + Sync + 'static) -> Self {
self.console = Some(ConsoleSink::new(sink));
self
}
pub fn env(mut self, name: impl Into<String>, value: impl Into<String>) -> Result<Self> {
let name = self.unbound(name.into())?;
let value = value.into();
self.env.insert(name, value);
Ok(self)
}
/// Bind a string value in `fetchlet.env` and also expose it on Node's
/// `process.env`. Use this opt-in method for third-party libraries that
/// read `process.env.KEY` directly; ordinary [`env`](Self::env) bindings
/// stay out of `process.env`.
pub fn process_env(
mut self,
name: impl Into<String>,
value: impl Into<String>,
) -> Result<Self> {
let name = self.unbound(name.into())?;
let value = value.into();
self.env.insert(name.clone(), value.clone());
self.process_env.insert(name, value);
Ok(self)
}
}