pub(crate) fn invocation(export: &str, args: &[serde_json::Value]) -> anyhow::Result<String> {
let export_name = serde_json::to_string(export)?;
let arguments = parsed(args)?;
Ok(format!(
"globalThis.__fetchletInvocation.call({export_name}, {arguments}, false)"
))
}
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
pub(crate) fn placed_invocation(
export: &str,
args: &[serde_json::Value],
bytes: &[(Vec<String>, u64)],
channels: &[(Vec<String>, u64)],
) -> anyhow::Result<String> {
let export_name = serde_json::to_string(export)?;
let arguments = parsed(args)?;
let bytes: Vec<_> = bytes
.iter()
.map(|(path, handle)| serde_json::json!({ "path": path, "handle": handle }))
.collect();
let channels: Vec<_> = channels
.iter()
.map(|(path, channel)| serde_json::json!({ "path": path, "channel": channel }))
.collect();
let places = serde_json::json!({ "bytes": bytes, "channels": channels });
Ok(format!(
"globalThis.__fetchletInvocation.call({export_name}, {arguments}, false, {places}, true)"
))
}
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
pub(crate) fn fetch_invocation(export: &str, args: &[serde_json::Value]) -> anyhow::Result<String> {
export_invocation(export, args, true)
}
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
fn export_invocation(
export: &str,
args: &[serde_json::Value],
stream_response_body: bool,
) -> anyhow::Result<String> {
let export_name = serde_json::to_string(export)?;
let arguments = parsed(args)?;
Ok(format!(
"globalThis.__fetchletInvocation.call({export_name}, {arguments}, {stream_response_body})"
))
}
fn parsed(args: &[serde_json::Value]) -> anyhow::Result<String> {
Ok(format!(
"JSON.parse({})",
serde_json::to_string(&serde_json::to_string(args)?)?
))
}
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
pub(crate) fn response_body_operation(operation: ResponseBodyOperation, body: u64) -> String {
let operation = match operation {
ResponseBodyOperation::Pull => "pull",
ResponseBodyOperation::Cancel => "cancel",
};
format!("globalThis.__fetchletInvocation.responseBody({operation:?}, {body})")
}
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
pub(crate) fn connection_invocation(channel: u64) -> String {
format!("globalThis.__fetchletInvocation.connection({channel})")
}
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
#[derive(Clone, Copy)]
pub(crate) enum ResponseBodyOperation {
Pull,
Cancel,
}
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
pub(crate) const PRELUDE: &str = include_str!("../../js/runtime.js");
#[cfg(any(feature = "boa", feature = "quickjs"))]
pub(crate) const STACK_TRACE: &str = include_str!("../../js/stack-trace.js");
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
pub(crate) const HOST_BOOTSTRAP: &str = include_str!("../../js/host-bridge.js");
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
pub(crate) const SEAL_HOST: &str = "delete globalThis.__fetchletClaimHost;";
pub(crate) const LOOP_STEP: &str = include_str!("../../js/event-loop.js");
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
pub(crate) const FLUSH_REJECTIONS: &str = "globalThis.__fetchletFlushRejections?.()";
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
pub(crate) fn observed(expression: &str) -> String {
let expression = expression.trim_end().trim_end_matches(';');
format!(
"((promise) => {{ promise.then(undefined, () => {{}}); return promise; }})({expression})"
)
}
#[cfg(any(feature = "boa", feature = "quickjs", feature = "v8"))]
pub(crate) const BACKGROUND_SETTLE: &str = include_str!("../../js/background.js");