Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: url parsing issue and .next dir not found (#1542)

tribhuwanbac34850ea9b
diff --git a/screenpipe-core/src/pipes.rs b/screenpipe-core/src/pipes.rs
index 5f8ca85d6..f691e9a86 100644
--- a/screenpipe-core/src/pipes.rs
+++ b/screenpipe-core/src/pipes.rs
@@ -77,20 +77,37 @@ fn generate_cron_secret() -> String {
}
// Update this function near the top of the file
-fn sanitize_pipe_name(name: &str) -> String {
+pub fn sanitize_pipe_name(name: &str) -> String {
// First check if this is a GitHub URL and extract the repo name
if let Ok(url) = Url::parse(name) {
if url.host_str() == Some("github.com") {
let path_segments: Vec<&str> = url.path_segments().unwrap().collect();
if path_segments.len() >= 2 {
- // Use the repository name (second segment) instead of branch name
- let repo_name = path_segments[1];
- debug!("Using repository name for pipe: {}", repo_name);
- return repo_name.to_string();
+ if let Some(tree_index) = path_segments.iter().position(|&s| s == "tree") {
+ let remaining_segments = &path_segments[tree_index + 1..];
+ if remaining_segments.len() > 2 {
+ debug!("Using repository name for pipe: {:?}",
+ remaining_segments.last().unwrap().to_string()
+ );
+ return remaining_segments.last().unwrap().to_string();
+ } else {
+ debug!("Using repository name for pipe: {:?}", path_segments[1]);
+ return path_segments[1].to_string();
+ }
+ } else {
+ debug!("Using repository name for pipe: {:?}", path_segments[1]);
+ return path_segments[1].to_string();
+ }
}
}
}
+ if Path::new(name).is_dir() {
+ if let Some(basename) = Path::new(name).file_name() {
+ return basename.to_string_lossy().to_string();
+ }
+ }
+
// Fall back to original sanitization logic for non-GitHub URLs
let re = Regex::new(r"[^a-zA-Z0-9_-]").unwrap();
let sanitized = re.replace_all(name, "-").to_string();
@@ -798,28 +815,8 @@ async fn retry_install(bun_path: &Path, dest_dir: &Path, max_retries: u32) -> Re
pub async fn download_pipe(source: &str, screenpipe_dir: PathBuf) -> anyhow::Result<PathBuf> {
info!("Processing pipe from source: {}", source);
- let is_local = Url::parse(source).is_err();
-
- let mut pipe_name =
- sanitize_pipe_name(Path::new(source).file_name().unwrap().to_str().unwrap());
-
- // For GitHub URLs, extract the repository name directly
- if !is_local {
- if let Ok(url) = Url::parse(source) {
- if url.host_str() == Some("github.com") {
- let path_segments: Vec<&str> = url.path_segments().unwrap().collect();
- if path_segments.len() >= 2 {
- pipe_name = path_segments[1].to_string();
- debug!("Using repository name for pipe: {}", pipe_name);
- }
- }
- }
- }
-
- // Add _local suffix for local pipes
- if is_local {
- pipe_name.push_str("_local");
- }
+ let pipe_name =
+ sanitize_pipe_name(Path::new(source).to_str().unwrap());
let dest_dir = screenpipe_dir.join("pipes").join(&pipe_name);
@@ -1406,7 +1403,8 @@ async fn run_cron_schedule(
let last_run = match get_last_cron_execution(&pipe_dir, path).await {
Ok(time) => time,
Err(e) => {
- error!("failed to get last cron execution: {}", e);
+ error!("[{}] failed to get last cron execution: {}",
+ pipe_dir.file_name().unwrap_or_default().to_string_lossy(), e);
None
}
};
@@ -1441,7 +1439,7 @@ async fn run_cron_schedule(
};
info!(
- "next cron execution for pipe {} at path {} in {} seconds",
+ "[{}] next cron execution for pipe at path {} in {} seconds",
pipe,
path,
duration.as_secs()
@@ -1502,25 +1500,33 @@ pub async fn cleanup_pipe_crons(pipe: &str) -> Result<()> {
async fn try_build_nextjs(pipe_dir: &Path, bun_path: &Path) -> Result<BuildStatus> {
info!(
- "checking if i need to build the next.js project in: {:?}",
- pipe_dir
+ "[{}] checking if i need to build the next.js project",
+ pipe_dir.file_name().unwrap_or_default().to_string_lossy()
);
- // Check if build already exists and is valid
+ // read the build value from pipe.json
let build_dir = pipe_dir.join(".next");
- if build_dir.exists() {
- let build_manifest = build_dir.join("build-manifest.json");
- if build_manifest.exists() {
- info!("found existing next.js build, skipping rebuild");
+ let pipe_json_path = pipe_dir.join("pipe.json");
+ if pipe_json_path.exists() {
+ let pipe_json = tokio::fs::read_to_string(&pipe_json_path).await?;
+ let pipe_config: Value = serde_json::from_str(&pipe_json)?;
+ let is_build = pipe_config.get("build").and_then(Value::as_bool).unwrap_or(false);
+ if is_build {
+ info!("[{}] pipe is already build, skipping rebuild",
+ pipe_dir.file_name().unwrap_or_default().to_string_lossy()
+ );
return Ok(BuildStatus::Success);
+ } else {
+ if !is_build && build_dir.exists() {
+ debug!("[{}] removing invalid next.js build directory",
+ pipe_dir.file_name().unwrap_or_default().to_string_lossy()
+ );
+ tokio::fs::remove_dir_all(&build_dir).await?;
+ }
}
- // Invalid/incomplete build directory - remove it
- debug!("removing invalid next.js build directory");
- tokio::fs::remove_dir_all(&build_dir).await?;
}
// Update build status to InProgress in pipe.json
- let pipe_json_path = pipe_dir.join("pipe.json");
if pipe_json_path.exists() {
let pipe_json = tokio::fs::read_to_string(&pipe_json_path).await?;
let mut pipe_config: Value = serde_json::from_str(&pipe_json)?;
@@ -1529,7 +1535,9 @@ async fn try_build_nextjs(pipe_dir: &Path, bun_path: &Path) -> Result<BuildStatu
tokio::fs::write(&pipe_json_path, updated_pipe_json).await?;
}
- info!("running next.js build");
+ info!("[{}] running next.js build",
+ pipe_dir.file_name().unwrap_or_default().to_string_lossy()
+ );
let build_output = Command::new(bun_path)
.arg("run")
.arg("--bun")
@@ -1539,11 +1547,28 @@ async fn try_build_nextjs(pipe_dir: &Path, bun_path: &Path) -> Result<BuildStatu
.await?;
if build_output.status.success() {
- info!("next.js build completed successfully");
+ let pipe_json = tokio::fs::read_to_string(&pipe_json_path).await?;
+ let mut pipe_config: Value = serde_json::from_str(&pipe_json)?;
+ pipe_config["build"] = json!(true);
+ let updated_pipe_json = serde_json::to_string_pretty(&pipe_config)?;
+ let mut file = File::create(&pipe_json_path).await?;
+ file.write_all(updated_pipe_json.as_bytes()).await?;
+ info!("[{}] next.js pipe build successfully",
+ pipe_dir.file_name().unwrap_or_default().to_string_lossy()
+ );
Ok(BuildStatus::Success)
} else {
+ let pipe_json = tokio::fs::read_to_string(&pipe_json_path).await?;
+ let mut pipe_config: Value = serde_json::from_str(&pipe_json)?;
+ pipe_config["build"] = json!(false);
+ let updated_pipe_json = serde_json::to_string_pretty(&pipe_config)?;
+ let mut file = File::create(&pipe_json_path).await?;
+ file.write_all(updated_pipe_json.as_bytes()).await?;
let error_message = String::from_utf8_lossy(&build_output.stderr).to_string();
- error!("next.js build failed: {}", error_message);
+ error!("[{}] next.js build failed: {}",
+ pipe_dir.file_name().unwrap_or_default().to_string_lossy(),
+ error_message
+ );
Ok(BuildStatus::Failed(error_message))
}
}
@@ -1769,6 +1794,7 @@ pub async fn download_pipe_private(
let pipe_json = tokio::fs::read_to_string(&final_pipe_json).await?;
let mut pipe_config: Value = serde_json::from_str(&pipe_json)?;
pipe_config["is_nextjs"] = json!(true);
+ pipe_config["build"] = json!(true);
pipe_config["buildStatus"] = json!({
"status": "success",
"step": "completed"
diff --git a/screenpipe-core/tests/pipes_test.rs b/screenpipe-core/tests/pipes_test.rs
index 0da0f2f0c..b395282e4 100644
--- a/screenpipe-core/tests/pipes_test.rs
+++ b/screenpipe-core/tests/pipes_test.rs
@@ -1,7 +1,17 @@
#[cfg(test)]
mod tests {
use chrono::{TimeZone, Utc};
- use screenpipe_core::{download_pipe, get_last_cron_execution, run_pipe, save_cron_execution};
+ use screenpipe_core::{
+ run_pipe,
+ download_pipe,
+ sanitize_pipe_name,
+ save_cron_execution,
+ download_pipe_private,
+ get_last_cron_execution,
+ PipeState
+ };
+
+ use tokio::io::AsyncWriteExt;
use serde_json::json;
use std::sync::Arc;
use std::sync::Once;
@@ -240,8 +250,53 @@ mod tests {
}
#[tokio::test]
- #[cfg(windows)]
- async fn test_download_pipe_windows_path() {
+ async fn test_sanitize_pipe_name() {
+ init();
+
+ // test default pipe url
+ let github_default_url = "https://github.com/KentTDang/AI-Interview-Coach/";
+ assert_eq!(sanitize_pipe_name(github_default_url), "AI-Interview-Coach");
+
+ // test pipe url with branch
+ let github_url = "https://github.com/KentTDang/AI-Interview-Coach/tree/main";
+ assert_eq!(sanitize_pipe_name(github_url), "AI-Interview-Coach");
+
+ // test pipe url as a subdirectory
+ let github_url_subdir = "https://github.com/mediar-ai/screenpipe/tree/main/pipes/search";
+ assert_eq!(sanitize_pipe_name(github_url_subdir), "search");
+
+ // test a local directory path
+ let temp_dir = TempDir::new().unwrap();
+ let source_dir = temp_dir.path().join("test-pipe-name");
+ tokio::fs::create_dir_all(&source_dir).await.unwrap();
+ assert_eq!(sanitize_pipe_name(
+ source_dir.to_str().expect("failed to convert path to str")
+ ), "test-pipe-name");
+
+ // test with a non-GitHub URL
+ let non_github_url = "https://example.com/some/path";
+ assert_eq!(sanitize_pipe_name(non_github_url), "https---example-com-some-path");
+
+ // url including invalid characters
+ let invalid_chars = "invalid:name/with*chars";
+ assert_eq!(sanitize_pipe_name(invalid_chars), "invalid-name-with-chars");
+ }
+
+ #[tokio::test]
+ async fn test_non_existence_local_pipe() {
+ init();
+ let temp_dir = TempDir::new().unwrap();
+ let screenpipe_dir = temp_dir.path().to_path_buf();
+
+ let source_dir = temp_dir.path().join("source_pipe");
+ let result = download_pipe(&source_dir.to_str().expect("failed bathbuf to str"),
+ screenpipe_dir.clone()).await;
+
+ assert!(result.is_err(), "test failed for non existence local pipe: {:?}", result.err());
+ }
+
+ #[tokio::test]
+ async fn test_downloading_and_running_sideloaded_pipe() {
init();
let temp_dir = TempDir::new().unwrap();
let screenpipe_dir = temp_dir.path().to_path_buf();
@@ -258,29 +313,246 @@ mod tests {
.await
.unwrap();
- // Get the absolute Windows path with backslashes
- let source_path = source_dir.to_str().unwrap().replace("/", "\\");
- println!("Testing Windows path: {}", source_path);
-
// Try to download the pipe using the Windows path
- let result = download_pipe(&source_path, screenpipe_dir.clone()).await;
+ let result = download_pipe(&source_dir.to_str().expect("failed to convert to str"),
+ screenpipe_dir.clone()
+ ).await;
- // The function should succeed with a Windows path
+
+ // The function should succeed for every os
assert!(
result.is_ok(),
- "Failed to handle Windows path: {:?}",
+ "Failed to download pipe: {:?}",
result.err()
);
// Verify the pipe was copied correctly
let pipe_name = source_dir.file_name().unwrap().to_str().unwrap();
- let dest_path = screenpipe_dir
- .join("pipes")
- .join(format!("{}_local", pipe_name));
+ let dest_path = screenpipe_dir.join("pipes").join(pipe_name);
+
assert!(dest_path.exists(), "Destination pipe directory not found");
+ assert!(dest_path.join("pipe.js").exists(), "pipe.js not found in destination");
+
+ // tests for urls
+ let urls = vec![
+ "https://github.com/KentTDang/AI-Interview-Coach/",
+ "https://github.com/KentTDang/AI-Interview-Coach/tree/main",
+ "https://github.com/mediar-ai/screenpipe/tree/main/pipes/search",
+ ];
+
+ for url in urls {
+ let result = download_pipe(url, screenpipe_dir.clone()).await;
+ assert!(result.is_ok(), "Failed to download pipe from URL: {}", url);
+
+ let pipe_name = sanitize_pipe_name(url);
+ let dest_path = screenpipe_dir.join("pipes").join(&pipe_name);
+ assert!(dest_path.exists(), "Destination pipe directory not found for URL: {}", url);
+
+ // verify pipe.json
+ let pipe_json_path = dest_path.join("pipe.json");
+ let pipe_json_content = tokio::fs::read_to_string(&pipe_json_path).await.unwrap();
+ let pipe_json: serde_json::Value = serde_json::from_str(&pipe_json_content).expect("Invalid JSON format");
+ assert!(pipe_json.is_object(), "expected json to be an object");
+
+ // enable side loaded pipe, even tho its enabled by default
+ let pipe_dir = dest_path;
+ let pipe_json_path = pipe_dir.join("pipe.json");
+ let pipe_json = tokio::fs::read_to_string(&pipe_json_path).await.unwrap();
+ let mut pipe_config: serde_json::Value = serde_json::from_str(&pipe_json).unwrap();
+ pipe_config["enabled"] = json!(true);
+ let updated_pipe_json = serde_json::to_string_pretty(&pipe_config);
+ let mut file = tokio::fs::File::create(&pipe_json_path).await.unwrap();
+ file.write_all(updated_pipe_json.expect("failed to write").as_bytes()).await.unwrap();
+
+ // run pipe
+ let run_result = run_pipe(&pipe_name, screenpipe_dir.clone()).await;
+
+ let (_child, pipe_state) = run_result.unwrap();
+
+ // for `bun i` command, keeping it max
+ sleep(Duration::from_secs(20)).await;
+
+ match pipe_state {
+ PipeState::Port(port) => {
+ // verify the pipe is running on the expected port
+ let client = reqwest::Client::new();
+ let response = client.get(format!("http://localhost:{}", port)).send().await;
+
+ assert!(response.is_ok(), "Failed to connect to the pipe on port {}", port);
+
+ // if successfull clean up the process
+ #[cfg(windows)]
+ {
+ let output = tokio::process::Command::new("powershell")
+ .arg("-NoProfile")
+ .arg("-WindowStyle")
+ .arg("hidden")
+ .arg("-Command")
+ .arg(format!(
+ r#"Get-WmiObject Win32_Process | Where-Object {{ $_.CommandLine -like "*\pipes\{}\*" }} | ForEach-Object {{ taskkill.exe /T /F /PID $_.ProcessId }}"#,
+ &pipe_name.to_string()
+ ))
+ .creation_flags(0x08000000)
+ .output()
+ .await
+ .expect("Failed to execute PowerShell command");
+
+ assert!(
+ output.status.success(),
+ "{} hasn't ran successfully",
+ pipe_name
+ );
+
+ }
+
+ #[cfg(unix)]
+ {
+ let command = format!(
+ "ps axuw | grep 'pipes/{}/' | grep -v grep | awk '{{print $2}}' | xargs -I {{}} kill -TERM {{}}",
+ &pipe_name.to_string()
+ );
+
+ let output = tokio::process::Command::new("sh")
+ .arg("-c")
+ .arg(command)
+ .output()
+ .await
+ .expect("Failed to execute ps command");
+
+ assert!(
+ output.status.success(),
+ "{} hasn't ran successfully",
+ &pipe_name.to_string()
+ );
+ }
+ }
+ PipeState::Pid(_pid) => {
+ // pipe process will not be running at this point
+ // check by `ps axuw | grep pipes | grep -v grep`
+ }
+ }
+
+ }
+ }
+
+ #[tokio::test]
+ async fn test_downloading_and_running_private_pipe() {
+ init();
+ let temp_dir = TempDir::new().unwrap();
+ let screenpipe_dir = temp_dir.path().to_path_buf();
+
+ let pipe_name = "data-table";
+ let source = "https://raw.githubusercontent.com/tribhuwan-kumar/anime/master/0.1.8.zip";
+ let result = download_pipe_private("data-table", source, screenpipe_dir.clone()).await;
+
assert!(
- dest_path.join("pipe.js").exists(),
- "pipe.js not found in destination"
+ result.is_ok(),
+ "Failed to download private pipe: {:?}",
+ result.err()
);
+ assert!(screenpipe_dir.join("pipes").join(pipe_name).exists(),
+ "test failed for downloading private pipe: {:?}",
+ result.err()
+ );
+
+ // any zip shouldn't exists
+ let mut entries = tokio::fs::read_dir(screenpipe_dir.join("pipes").join(pipe_name)).await.unwrap();
+ let mut zip_exists = false;
+ while let Some(entry) = entries.next_entry().await.unwrap() {
+ let path = entry.path();
+ if path.extension().and_then(|ext| ext.to_str()) == Some("zip") {
+ zip_exists = true;
+ break;
+ }
+ }
+ assert!(!zip_exists,
+ "failed zip extraction for downloading private pipe: {:?}",
+ result.err()
+ );
+
+ // verify pipe.json
+ let pipe_json_path = screenpipe_dir.join("pipes").join(pipe_name).join("pipe.json");
+ let pipe_json_content = tokio::fs::read_to_string(&pipe_json_path).await.unwrap();
+ let pipe_json: serde_json::Value = serde_json::from_str(&pipe_json_content).expect("Invalid JSON format");
+
+ assert!(pipe_json.is_object(), "expected json to be an object");
+
+ // enable by writng pipe.json
+ let pipe_dir = screenpipe_dir.join("pipes").join(pipe_name);
+ let pipe_json_path = pipe_dir.join("pipe.json");
+ let pipe_json = tokio::fs::read_to_string(&pipe_json_path).await.unwrap();
+ let mut pipe_config: serde_json::Value = serde_json::from_str(&pipe_json).unwrap();
+ pipe_config["enabled"] = json!(true);
+ let updated_pipe_json = serde_json::to_string_pretty(&pipe_config);
+ let mut file = tokio::fs::File::create(&pipe_json_path).await.unwrap();
+ file.write_all(updated_pipe_json.expect("failed to write").as_bytes()).await.unwrap();
+
+ // run pipe
+ let run_result = run_pipe(pipe_name, screenpipe_dir.clone()).await;
+
+ let (_child, pipe_state) = run_result.unwrap();
+
+ // for `bun i` command, keeping it max
+ sleep(Duration::from_secs(20)).await;
+
+ match pipe_state {
+ PipeState::Port(port) => {
+ // verify the pipe is running on the expected port
+ let client = reqwest::Client::new();
+ let response = client.get(format!("http://localhost:{}", port)).send().await;
+
+ assert!(response.is_ok(), "Failed to connect to the pipe on port {}", port);
+
+ // if successfull clean up the process
+ #[cfg(windows)]
+ {
+ let output = tokio::process::Command::new("powershell")
+ .arg("-NoProfile")
+ .arg("-WindowStyle")
+ .arg("hidden")
+ .arg("-Command")
+ .arg(format!(
+ r#"Get-WmiObject Win32_Process | Where-Object {{ $_.CommandLine -like "*\pipes\{}\*" }} | ForEach-Object {{ taskkill.exe /T /F /PID $_.ProcessId }}"#,
+ &pipe_name.to_string()
+ ))
+ .creation_flags(0x08000000)
+ .output()
+ .await
+ .expect("Failed to execute PowerShell command");
+
+ assert!(
+ output.status.success(),
+ "{} hasn't ran successfully",
+ pipe_name
+ );
+
+ }
+
+ #[cfg(unix)]
+ {
+ let command = format!(
+ "ps axuw | grep 'pipes/{}/' | grep -v grep | awk '{{print $2}}' | xargs -I {{}} kill -TERM {{}}",
+ &pipe_name.to_string()
+ );
+
+ let output = tokio::process::Command::new("sh")
+ .arg("-c")
+ .arg(command)
+ .output()
+ .await
+ .expect("Failed to execute ps command");
+
+ assert!(
+ output.status.success(),
+ "{} hasn't ran successfully",
+ pipe_name
+ );
+ }
+ }
+ PipeState::Pid(_pid) => {
+ // pipe process will not be running at this point
+ // check by `ps axuw | grep pipes | grep -v grep`
+ }
+ }
}
}
diff --git a/screenpipe-server/src/pipe_manager.rs b/screenpipe-server/src/pipe_manager.rs
index cd890436f..18212d980 100644
--- a/screenpipe-server/src/pipe_manager.rs
+++ b/screenpipe-server/src/pipe_manager.rs
@@ -568,7 +568,7 @@ impl PipeManager {
}
}
Err(e) => {
- println!("failed to start pipe {}: {}", id, e);
+ error!("[{}] failed to start pipe {}:", id, e);
Err(e)
}
}