Repository: awslabs/llrt
File: src/fs/read_dir.rs (Rust)

Code before:
    let with_recursive = options
        .0
        .as_ref()
        .and_then(|opts| opts.get("recursive").ok())
        .and_then(|recursive: Value| recursive.as_bool())
        .unwrap_or_default();

    let skip_root_pos = {
        match path.as_str() {
            // . | ./
            "." | CURRENT_DIR_STR => CURRENT_DIR_STR.len(),
            // ./path
            _ if path.starts_with(CURRENT_DIR_STR) => path.len() + 1,
            // path
            _ => {
                if !is_absolute(path.clone()) {
                    path = [CURRENT_DIR_STR.to_string(), path].concat();
                }
                path.len() + 1
            }
        }
    };
    let mut items = Vec::with_capacity(64);
    let mut dirs = VecDeque::from_iter([PathBuf::from(path)]);

    while let Some(mut dir_buf) = dirs.pop_back() {
        let mut dir = fs::read_dir(dir_buf).await.or_throw(&ctx)?;
        while let Some(child) = dir.next_entry().await? {
            if let Some(name) = child.path().file_name() {
                let metadata = if with_file_types || with_recursive {
                    Some(child.metadata().await?)
                } else {
                    None
                };

                if with_recursive && metadata.as_ref().is_some_and(|metadata| metadata.is_dir()) {
                    dirs.push_back(child.path());
                }

                items.push((
                    child.path().into_os_string().to_string_lossy()[skip_root_pos..].to_string(),
                    if with_file_types { metadata } else { None },
                ))
            }
        }
    }

    items.sort_by(|(a, _), (b, _)| a.partial_cmp(b).unwrap());

    Ok(ReadDir { items })
}

Review comment (suggestion):
Nice we have a non-recursive traversal 👍 
I have similar functionality in here that could be changed to return an iterator instead and reused here.
https://github.com/awslabs/llrt/blob/37eb92b062f858a42567c1457ee199d977c51153/src/utils/io.rs#L45
Also, we might speed up things if we parallelize the walk of subdirectories by spawning tasks that joining the awaits that calls next entries.

Code after:
            .get("withFileTypes")
            .ok()
            .and_then(|file_types: Value| file_types.as_bool())
            .unwrap_or_default();

        with_recursive = options
            .get("recursive")
            .ok()
            .and_then(|recursive: Value| recursive.as_bool())
            .unwrap_or_default();
    };

    let skip_root_pos = {
        match path.as_str() {
            // . | ./
            "." | CURRENT_DIR_STR => CURRENT_DIR_STR.len(),
            // ./path
            _ if path.starts_with(CURRENT_DIR_STR) => path.len() + 1,
            // path
            _ => {
                if !is_absolute(path.clone()) {
                    path = [CURRENT_DIR_STR.to_string(), path].concat();
                }
                path.len() + 1
            }
        }
    };
    let mut items = Vec::with_capacity(64);

    let mut directory_walker = DirectoryWalker::new(PathBuf::from(path));

    async fn walk(
        walker: &mut DirectoryWalker,
        with_recursive: bool,
    ) -> io::Result<Option<PathBuf>> {
        if with_recursive {
            walker.walk_recursive().await
        } else {
            walker.walk().await
        }
    }

    // eat root
    walk(&mut directory_walker, with_recursive).await?;

    while let Some(child) = walk(&mut directory_walker, with_recursive).await? {
        let metadata = if with_file_types {
            Some(fs::metadata(&child).await?)
        } else {
            None
        };