feat: deliver cancellable HTTP text requests through owner-scoped tasks

This commit is contained in:
2026-10-23 19:46:27 +08:00
parent b725cf488d
commit c418a425f3
2 changed files with 223 additions and 35 deletions
+41 -16
View File
@@ -196,9 +196,15 @@ pub fn cancel_owner(cx: &mut NativeContext<'_>, owner: ObjectRef) -> Result<(),
Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Operation {
Timer,
ReadText,
FetchText,
}
#[derive(Debug)]
struct After {
file: bool,
operation: Operation,
provider: crate::io_tasks::Provider,
signature: endor_core::CallSignature,
index: Index,
@@ -220,10 +226,10 @@ impl NativeCallable for After {
}
let (owner, key, delay, callback) = match args {
[owner, key, delay, callback] => (owner, key, delay, Some(callback)),
[owner, key, path] if self.file => (owner, key, path, None),
[owner, key, path] if self.operation != Operation::Timer => (owner, key, path, None),
_ => {
return Err(invalid(
"asynchronous request requires owner, key, delay/path and optional file callback",
"asynchronous request requires owner, key, delay/path and optional I/O callback",
));
}
};
@@ -248,14 +254,28 @@ impl NativeCallable for After {
if key.is_empty() || key.len() > 128 || key.chars().any(char::is_control) {
return Err(invalid("invalid task key"));
}
let (milliseconds, path) = if self.file {
let (milliseconds, request) = if self.operation != Operation::Timer {
let Value::Text(path) = delay else {
return Err(invalid("text path must be a String"));
};
if path.is_empty() || path.len() > 4096 || path.contains('\0') {
return Err(invalid("invalid text path"));
}
(0, Some(path.clone()))
let request = match self.operation {
Operation::ReadText => {
if path.is_empty() || path.len() > 4096 || path.contains('\0') {
return Err(invalid("invalid text path"));
}
crate::io_tasks::Request::ReadText(path.clone())
}
Operation::FetchText => {
if !crate::io_tasks::valid_url(path) {
return Err(invalid(
"expected HTTP(S) URL without credentials or control characters",
));
}
crate::io_tasks::Request::FetchText(path.clone())
}
Operation::Timer => unreachable!(),
};
(0, Some(request))
} else {
let Value::Number(delay) = delay else {
return Err(invalid(
@@ -309,13 +329,13 @@ impl NativeCallable for After {
.ok_or(invalid("task deadline overflow"))?,
callback,
state: State::Pending,
ready: !self.file,
ready: self.operation == Operation::Timer,
text: None,
error: None,
lease: None,
})?;
cx.attach_type(task, desktop_type(cx.store(), "Task", self.desktop)?)?;
if let Some(path) = path {
if let Some(request) = request {
let replacing = previous
.map(|previous| {
cx.store()
@@ -326,7 +346,7 @@ impl NativeCallable for After {
.unwrap_or(false);
let lease = self
.provider
.submit(cx.store().downgrade(task)?, path, replacing)?;
.submit(cx.store().downgrade(task)?, request, replacing)?;
let mut value = cx.store().payload::<Task>(task, "Task")?.clone();
value.lease = Some(lease);
cx.replace_payload(task, value)?;
@@ -683,18 +703,23 @@ pub fn install(
let index = Arc::new(Mutex::new(Vec::new()));
let definition = desktop_type(vm.object_store(), "Task", desktop)?;
let provider = crate::io_tasks::Provider::default();
for (name, file) in [("after", false), ("readText", true)] {
for (name, operation) in [
("after", Operation::Timer),
("readText", Operation::ReadText),
("fetchText", Operation::FetchText),
] {
let external = operation != Operation::Timer;
let function = vm.allocate(ContextNativeFunction::new(
name,
After {
file,
operation,
provider: provider.clone(),
signature: {
let mut signature = endor_core::CallSignature::exact(
vec![
endor_core::AnnotationType::Any,
endor_core::AnnotationType::String,
if file {
if external {
endor_core::AnnotationType::String
} else {
endor_core::AnnotationType::Number
@@ -702,13 +727,13 @@ pub fn install(
endor_core::AnnotationType::Function,
],
endor_core::AnnotationType::Nominal(definition),
if file {
if external {
endor_core::CallEffect::External
} else {
endor_core::CallEffect::Mutating
},
);
if file {
if external {
signature.arity = endor_core::CallArity::range(3, 4);
}
signature