Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ export const main = task({
});

```

> [!NOTE]
> TypeScript/JavaScript projects require a task named `"main"` as the entrypoint.
> The runtime requires a task named `"main"` as the entrypoint. Python can define the main task itself, but it's recommended to set it manually as well.


When you run `capsule run main.py` (or `main.ts`), your code is compiled into a WebAssembly module and executed in a dedicated sandbox to isolate tasks.
Expand Down
21 changes: 21 additions & 0 deletions crates/capsule-cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/capsule-cli/docs/README-pypi.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def analyze_data(dataset: list) -> dict:
return {"processed": len(dataset), "status": "complete"}
```

> [!NOTE]
> The runtime requires a task named `"main"` as the entrypoint. Python can define the main task itself, but it's recommended to set it manually.


When you run `capsule run main.py`, your code is compiled into a WebAssembly module and executed in a dedicated sandbox.

## Documentation
Expand Down
4 changes: 3 additions & 1 deletion crates/capsule-cli/npm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export const analyzeData = task({
return { processed: dataset.length, status: "complete" };
});

// The "main" task is required as the entrypoint
export const main = task({
name: "main",
compute: "HIGH"
Expand All @@ -69,6 +68,9 @@ export const main = task({
});
```

> [!NOTE]
> The runtime requires a task named `"main"` as the entrypoint.

When you run `capsule run main.ts`, your code is compiled into a WebAssembly module and executed in a dedicated sandbox.

## Documentation
Expand Down
6 changes: 1 addition & 5 deletions crates/capsule-cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,7 @@ pub async fn execute(

let execution_policy =
extract_main_execution_policy(compile_result.task_registry, &manifest.capsule_toml)
.unwrap_or_else(|| {
ExecutionPolicy::default()
.compute(Some(Compute::Custom(u64::MAX)))
.allowed_files(vec![".".to_string()])
});
.unwrap_or_else(|| ExecutionPolicy::default().compute(Some(Compute::Custom(u64::MAX))));

let runtime = Runtime::new(runtime_config, manifest.capsule_toml)?;

Expand Down
21 changes: 21 additions & 0 deletions crates/capsule-core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/capsule-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ chrono = "0.4"
colored = "2.1"
toml = "0.9.11"
blake3 = "1"
rayon = "1.10"
walkdir = "2"

rustpython-parser = "0.4"
swc_ecma_parser = "33.0"
Expand Down
5 changes: 3 additions & 2 deletions crates/capsule-core/src/config/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::Deserialize;
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};

Expand All @@ -10,8 +11,8 @@ pub enum ManifestError {
ParseError(String),
}

impl std::fmt::Display for ManifestError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for ManifestError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ManifestError::FsError(e) => write!(f, "Filesystem error > {}", e),
ManifestError::ParseError(e) => write!(f, "Parse error > {}", e),
Expand Down
13 changes: 6 additions & 7 deletions crates/capsule-core/src/wasm/compiler/javascript.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::collections::HashMap;
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use crate::config::fingerprint::SourceFingerprint;
use crate::wasm::utilities::introspection::javascript;
use crate::wasm::utilities::introspection::scanner;
use crate::wasm::utilities::wit_manager::WitManager;

#[derive(Debug)]
Expand All @@ -14,8 +15,8 @@ pub enum JavascriptWasmCompilerError {
CompileFailed(String),
}

impl std::fmt::Display for JavascriptWasmCompilerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl fmt::Display for JavascriptWasmCompilerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
JavascriptWasmCompilerError::FsError(msg) => write!(f, "Filesystem error > {}", msg),
JavascriptWasmCompilerError::CommandFailed(msg) => {
Expand Down Expand Up @@ -317,9 +318,7 @@ export const taskRunner = exports;
}

pub fn introspect_task_registry(&self) -> Option<HashMap<String, serde_json::Value>> {
let source = fs::read_to_string(&self.source_path).ok()?;
let is_typescript = self.source_path.extension().is_some_and(|ext| ext == "ts");

javascript::extract_js_task_configs(&source, is_typescript)
let source_dir = self.source_path.parent()?;
scanner::scan_js_tasks(source_dir)
}
}
7 changes: 3 additions & 4 deletions crates/capsule-core/src/wasm/compiler/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::process::Command;
use std::process::Stdio;

use crate::config::fingerprint::SourceFingerprint;
use crate::wasm::utilities::introspection::python;
use crate::wasm::utilities::introspection::scanner;
use crate::wasm::utilities::wit_manager::WitManager;

pub enum PythonWasmCompilerError {
Expand Down Expand Up @@ -308,8 +308,7 @@ from capsule.app import TaskRunner, exports
}

pub fn introspect_task_registry(&self) -> Option<HashMap<String, serde_json::Value>> {
let source = fs::read_to_string(&self.source_path).ok()?;

python::extract_python_task_configs(&source)
let source_dir = self.source_path.parent()?;
scanner::scan_python_tasks(source_dir)
}
}
2 changes: 1 addition & 1 deletion crates/capsule-core/src/wasm/execution_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Compute {
}
}

#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ExecutionPolicy {
pub name: String,
pub compute: Compute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn extract_js_task_configs(
let mut parser = Parser::new(syntax, StringInput::from(&*fm), None);
let module = parser.parse_module().ok()?;

let mut tasks: HashMap<String, serde_json::Value> = HashMap::new();
let mut tasks = HashMap::new();

for item in &module.body {
if let ModuleItem::Stmt(stmt) = item
Expand Down Expand Up @@ -82,6 +82,7 @@ fn extract_task_call(
.and_then(|v| v.as_str())
.unwrap_or(&var_name)
.to_string();

config.insert(
"name".to_string(),
serde_json::Value::String(task_name.clone()),
Expand Down Expand Up @@ -132,6 +133,7 @@ fn extract_js_literal(expr: &Expr) -> Option<serde_json::Value> {
.filter_map(|e| e.as_ref())
.map(|e| extract_js_literal(&e.expr))
.collect();

items.map(serde_json::Value::Array)
}
_ => None,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod javascript;
pub mod python;
pub mod scanner;
18 changes: 3 additions & 15 deletions crates/capsule-core/src/wasm/utilities/introspection/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn extract_task_decorator(
) -> Option<HashMap<String, serde_json::Value>> {
match decorator {
ast::Expr::Name(name) if name.id.as_str() == "task" => {
let mut config: HashMap<String, serde_json::Value> = HashMap::new();
let mut config = HashMap::new();
config.insert(
"name".to_string(),
serde_json::Value::String(func_name.to_string()),
Expand All @@ -58,7 +58,7 @@ fn extract_call_args(
call: &ast::ExprCall,
func_name: &str,
) -> Option<HashMap<String, serde_json::Value>> {
let mut config: HashMap<String, serde_json::Value> = HashMap::new();
let mut config = HashMap::new();

for keyword in &call.keywords {
if let Some(arg_name) = &keyword.arg
Expand Down Expand Up @@ -96,6 +96,7 @@ fn extract_literal(expr: &ast::Expr) -> Option<serde_json::Value> {
ast::Expr::List(list) => {
let items: Option<Vec<serde_json::Value>> =
list.elts.iter().map(extract_literal).collect();

items.map(serde_json::Value::Array)
}
_ => None,
Expand Down Expand Up @@ -131,19 +132,6 @@ def main():
assert!(configs.contains_key("main"));
}

#[test]
fn test_task_with_allowed_files() {
let source = r#"
@task(name="main", allowed_files=["./data", "./config"])
def main():
pass
"#;
let configs = extract_python_task_configs(source).unwrap();
let allowed = configs["main"]["allowed_files"].as_array().unwrap();
assert_eq!(allowed.len(), 2);
assert_eq!(allowed[0], "./data");
}

#[test]
fn test_no_task_decorator() {
let source = r#"
Expand Down
Loading