HostServices defines every capability your Rust host grants to JavaScript:
environment values, Rust functions, bidirectional channels, outbound fetch(),
TCP sockets, WebSockets, and a console sink. Without explicit configuration,
JavaScript has no access to the network or host environment.
Bindings
use fetchlet::{
App, Backend, Body, FetchPolicy, FetchService, HostServices,
Module, Response, transport_fn,
};
let policy = FetchPolicy::deny_all()
.allow_origin("https://api.example.test")?;
let service = FetchService::new(
transport_fn(|request| async move {
let message = format!("handled {}", request.uri().path());
Ok(Response::new(Body::from(message)))
}),
policy,
);
let hosts = HostServices::new()
.outbound(service.clone())
.service("API", service)?
.env("APP_ENV", "production")?;
let mut app = App::builder(Backend::quickjs())
.module(Module::new(bundle))
.host_services(hosts)
.build()?;In JavaScript, these appear on fetchlet.env (or via getBindings() from
fetchlet/runtime/adapter, and on your framework’s request context):
fetch(url, init)callsoutboundenv.API.fetch(url, init)calls the named service"API"env.APP_ENVreads"production"
Bindings are read-only and fixed before modules evaluate. Only expose values
JavaScript needs to read — keep upstream API keys and tokens in Rust using
FetchService::credentials().
Ordinary .env(name, value) bindings stay out of Node’s process.env. If a
third-party JavaScript library reads process.env.KEY directly, opt specific
keys in with .process_env(name, value) — this binds the key on both
fetchlet.env and process.env without leaking the rest of your environment.
For real network requests over HTTP/1.1 and HTTP/2, pass fetchlet_net::HttpClient
as the transport (Outbound networking).
Function bindings
HostServices::functions(name, functions) exposes Rust functions directly on
env so JavaScript can call Rust without constructing an HTTP Request:
use fetchlet::{FunctionError, FunctionRequest, Functions, HostServices};
use serde_json::{Value, json};
let db = Functions::new()
.method("query", |request: FunctionRequest| async move {
let (sql, params): (String, Vec<Value>) = request.args()?;
let user = request.context().get::<User>();
let rows = run(user, &sql, ¶ms)
.await
.map_err(|error| FunctionError::new(error.to_string()).code("QUERY_FAILED"))?;
Ok(json!(rows))
})
.sync_method("escape", |request: FunctionRequest| {
let (text,): (String,) = request.args()?;
Ok(json!(escape(&text)))
});
let hosts = HostServices::new().functions("DB", db)?;const rows = await env.DB.query("select 1", []);
const quoted = env.DB.escape(name);- Values and binary data: arguments and results are JSON (bounded by
Limits::bridge_message_bytes). AnyArrayBufferor view (Uint8Array, NodeBuffer) transfers directly as raw bytes without JSON encoding, and deserializes intoVec<u8>orbytes::Bytes. Return raw bytes directly or embed them inside JSON withFunctionResponse::bytes_at. - Returning channels:
FunctionResponse::channel_at(pointer, request.limits())attaches a liveHostChannelinside the return value:
use fetchlet::{FunctionRequest, FunctionResponse, Functions};
let events = Functions::new().method("subscribe", |request: FunctionRequest| async move {
let (topic,): (String,) = request.args()?;
let mut response = FunctionResponse::new(json!({ "topic": topic, "events": null }));
let end = response.channel_at("/events", request.limits())?;
tokio::spawn(publish(topic, end));
Ok(response)
});const { events } = await env.EVENTS.subscribe("news");
const socket = events.socket();
socket.addEventListener("message", (event) => console.log(event.data));- Async vs. sync methods: async methods return a
Promise, respectLimits::timeout, and cancel when the invocation ends. Sync methods block the JS thread and cannot be interrupted by a deadline, so keep them to fast in-memory work. - Errors: invalid arguments throw a
TypeError(hiding Serde internals);FunctionError::new(msg).code(code)arrives as a JSErrorwithcode; panics fail the call withPANICKEDwithout crashing the instance. - Context & limits: read the trusted
Contextviarequest.context(). Calling the sameAppHandlerecursively fails withError::Reentrant.Limits::function_callscaps calls per invocation.
Channels
HostServices::channel(name, service) lets JavaScript open bidirectional
message or byte streams via env.NAME.connect(options):
use fetchlet::{ChannelRequest, CloseFrame, HostServices, channel_fn};
let hosts = HostServices::new().channel(
"PROGRESS",
channel_fn(|request: ChannelRequest| async move {
let mut end = request.end;
tokio::spawn(async move {
for step in 1..=3 {
if end.send(format!("step {step}")).await.is_err() {
return;
}
}
end.close(CloseFrame::new(CloseFrame::NORMAL, "done"));
});
Ok(())
}),
)?;ChannelEndimplementsfutures::Stream<Item = ChannelEvent>andfutures::Sink<Message>, with backpressure bounded byLimits::open_channels,channel_message_bytes, andchannel_buffer_bytes.- On the JavaScript side, consume the channel with
channel.socket()(WebSocket-style messages) orchannel.stream()({ readable, writable }byte streams). - Channels close automatically (code
1006) when their invocation ends.
Calls with bytes, channels and context
When Rust invokes an exported JavaScript function with App::call_with or
AppHandle::call_with, Call lets you pass raw byte arrays, live channels, and
a trusted Context:
use fetchlet::{Call, ChannelEvent};
use serde_json::json;
let mut call = Call::new("transcode");
call.json(json!({ "format": "webm", "progress": null }));
let mut progress = call.channel_at("/0/progress", app.limits())?;
let mut input = call.channel(app.limits());
let (result, _) = tokio::join!(app.call_with(call), async {
input.send(chunk).await?;
while let Some(ChannelEvent::Message(step)) = progress.recv().await {
report(step);
}
anyhow::Ok(())
});bytes()/bytes_at(pointer)passUint8Arrayarguments without JSON encoding;channel()/channel_at(pointer, limits)passHostChannelobjects.context(Context)attaches trusted Rust values accessible toFunctionsmethods and internal fetches called during that invocation.CallOutput::deserialize::<T>()reads both JSON fields and returnedUint8Array/Buffervalues back into Rust types such asVec<u8>orbytes::Bytes:
let mut call = Call::new("thumbnail");
call.bytes(image).json(json!({ "width": 128 }));
call.context(Context::new().with(Tenant("a".into())));
let (name, thumbnail): (String, Vec<u8>) = app.call_with(call).await?.deserialize()?;Sockets
HostServices::sockets(service) enables connect(address, options) from
fetchlet/runtime (WinterTC Sockets API) using a SocketPolicy allowlist and a
SocketTransport (such as fetchlet_net::TcpTransport
or a custom socket_fn):
use fetchlet::{HostServices, SocketPolicy, SocketService};
use fetchlet_net::TcpTransport;
let policy = SocketPolicy::deny_all()
.allow("db.example.com", 5432)?
.allow_ports("cache.internal", 6379..=6380)?;
let transport = TcpTransport::new().allow_private_addresses();
let hosts = HostServices::new().sockets(SocketService::new(transport, policy));SocketPolicymatches exact normalized hostnames/IPs and port ranges; useSocketService::rewriteto map logical names before authorization andforward_contextto pass the trustedContext.- Sockets count against
Limits::open_channels,channel_message_bytes, andchannel_buffer_bytes, support half-close (ChannelEvent::End) and STARTTLS (ChannelEvent::StartTls/ChannelEnd::confirm_tls), and close when the invocation ends.
Client WebSockets
HostServices::websockets(service) enables JavaScript’s global new WebSocket()
with a FetchPolicy (ws: / wss: map to http: / https:) and a
WebSocketTransport (such as fetchlet_net::WebSocketClient
or websocket_fn):
use fetchlet::{FetchPolicy, HostServices, WebSocketService};
use fetchlet_net::WebSocketClient;
let policy = FetchPolicy::deny_all().allow_origin("https://chat.example.com")?;
let service = WebSocketService::new(WebSocketClient::new(), policy)
.credentials(|url, headers| {
if url.host_str() == Some("chat.example.com") {
headers.insert("authorization", "Bearer token".parse()?);
}
Ok(())
});
let hosts = HostServices::new().websockets(service);rewriteruns before authorization;credentialsruns after and attaches headers to the opening handshake without exposing them to JavaScript.- Refused origins, failed handshakes, or missing services fire
errorandclose(1006) in JavaScript. Sockets close when the invocation ends.
Console
HostServices::console captures console.debug, log, info, warn, and
error calls. Frameworks like SvelteKit log server errors through
console.error, so wiring this up makes server-side failures visible:
let hosts = HostServices::new()
.console(|level, message| eprintln!("[js {level:?}] {message}"));Without a callback, console output is discarded. Messages larger than
Limits::bridge_message_bytes are replaced with
[fetchlet: console message dropped]. Because the callback runs on the
JavaScript thread, offload slow I/O to another task or channel.
Outbound fetch lifecycle
Every fetch() call from JavaScript runs through FetchService in a fixed
order:
- Read or stream request body (checking
request_body_bytes,request_body_chunks, andbridge_message_bytes). - Count subrequest against
Limits::subrequestsand select the target service (outboundor named binding). - Rewrite — optional
FetchService::rewritecallback may modify the request. - Authorize — check the rewritten origin, method, headers, and body against
FetchPolicy. - Attach credentials — optional
FetchService::credentialscallback adds secrets for this single hop only. - Send — invoke the single-hop
FetchTransport. - Redirects — rebuild the next request from the pre-credential request and
repeat steps 3–6 (cross-origin redirects also strip guest
AuthorizationandCookieheaders).
To route specific origins to in-process Rust handlers or an axum router without
opening a socket, use OriginRouter (In-process fetch).
Streaming request bodies
An outbound fetch() whose body is a ReadableStream (with duplex: "half")
streams chunks on demand to the transport without buffering the whole payload:
- Any redirect other than
303fails withTRANSPORT(because the stream cannot be replayed);redirect: "manual"returns the redirect response. - Network transports require HTTP/2 (
RequireHttp2) unless the policy enablesFetchPolicy::allow_http1_streaming(). In-process transports ignore this requirement. request_body_bytes,request_body_chunks, andbridge_message_bytesare enforced per chunk; aborting the fetch or early server responses cancel the JavaScript stream.
Writing a custom FetchTransport
If you implement FetchTransport directly (instead of using HttpClient,
transport_fn, or router_transport):
- Handle a single HTTP hop and return redirect responses to
FetchService. - Cancel in-flight work when your future or returned
Bodyis dropped. - Enforce
response_body_byteswhile receiving and honorRequireHttp2on network connections. - Validate DNS resolution and destination IP addresses for network reachability.
When spawning background tasks inside a transport (such as connection pools),
configure AppBuilder::host_runtime(handle) so they continue running on a
shared Tokio runtime while an instance executes JavaScript
(fetchlet_http::Server sets this automatically).
Related guides
- In-process fetch: call Rust handlers and axum routers via
OriginRouter - Outbound networking:
HttpClient,TcpTransport, andWebSocketClient - Resource limits: every
Limitsfield and its scope - Running JavaScript:
App,AppHandle, pools, and process isolation