mirror of
https://github.com/Drop-OSS/droplet.git
synced 2025-11-16 01:31:26 +10:00
feat: no panik
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
use std::{
|
||||
collections::HashMap, fs::File, path::Path
|
||||
};
|
||||
use std::{collections::HashMap, fs::File, path::Path};
|
||||
|
||||
use anyhow::anyhow;
|
||||
use napi::{bindgen_prelude::*, sys::napi_value__, tokio_stream::StreamExt};
|
||||
use tokio_util::codec::{BytesCodec, FramedRead};
|
||||
|
||||
@ -15,7 +14,7 @@ use crate::version::{
|
||||
*/
|
||||
pub fn create_backend_constructor<'a>(
|
||||
path: &Path,
|
||||
) -> Option<Box<dyn FnOnce() -> Box<dyn VersionBackend + Send + 'a>>> {
|
||||
) -> Option<Box<dyn FnOnce() -> Result<Box<dyn VersionBackend + Send + 'a>>>> {
|
||||
if !path.exists() {
|
||||
return None;
|
||||
}
|
||||
@ -23,12 +22,14 @@ pub fn create_backend_constructor<'a>(
|
||||
let is_directory = path.is_dir();
|
||||
if is_directory {
|
||||
let base_dir = path.to_path_buf();
|
||||
return Some(Box::new(move || Box::new(PathVersionBackend { base_dir })));
|
||||
return Some(Box::new(move || {
|
||||
Ok(Box::new(PathVersionBackend { base_dir }))
|
||||
}));
|
||||
};
|
||||
|
||||
if path.to_string_lossy().ends_with(".zip") {
|
||||
let f = File::open(path.to_path_buf()).unwrap();
|
||||
return Some(Box::new(|| Box::new(ZipVersionBackend::new(f))));
|
||||
let f = File::open(path.to_path_buf()).ok()?;
|
||||
return Some(Box::new(|| Ok(Box::new(ZipVersionBackend::new(f)?))));
|
||||
}
|
||||
|
||||
None
|
||||
@ -58,10 +59,13 @@ impl<'a> DropletHandler<'a> {
|
||||
let fs_path = Path::new(&path);
|
||||
let constructor = create_backend_constructor(fs_path)?;
|
||||
|
||||
let existing_backend = self.backend_cache.entry(path).or_insert_with(|| {
|
||||
let backend = constructor();
|
||||
backend
|
||||
});
|
||||
let existing_backend = match self.backend_cache.entry(path) {
|
||||
std::collections::hash_map::Entry::Occupied(occupied_entry) => occupied_entry.into_mut(),
|
||||
std::collections::hash_map::Entry::Vacant(vacant_entry) => {
|
||||
let backend = constructor().ok()?;
|
||||
vacant_entry.insert(backend)
|
||||
}
|
||||
};
|
||||
|
||||
Some(existing_backend)
|
||||
}
|
||||
@ -80,7 +84,7 @@ impl<'a> DropletHandler<'a> {
|
||||
let backend = self
|
||||
.create_backend_for_path(path)
|
||||
.ok_or(napi::Error::from_reason("No backend for path"))?;
|
||||
let files = backend.list_files();
|
||||
let files = backend.list_files()?;
|
||||
Ok(files.into_iter().map(|e| e.relative_filename).collect())
|
||||
}
|
||||
|
||||
@ -90,11 +94,9 @@ impl<'a> DropletHandler<'a> {
|
||||
.create_backend_for_path(path)
|
||||
.ok_or(napi::Error::from_reason("No backend for path"))?;
|
||||
|
||||
let file = backend
|
||||
.peek_file(sub_path)
|
||||
.ok_or(napi::Error::from_reason("Can't find file to peek"))?;
|
||||
let file = backend.peek_file(sub_path)?;
|
||||
|
||||
return Ok(file.size.try_into().unwrap());
|
||||
Ok(file.size)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
@ -106,28 +108,24 @@ impl<'a> DropletHandler<'a> {
|
||||
env: Env,
|
||||
start: Option<BigInt>,
|
||||
end: Option<BigInt>,
|
||||
) -> Result<JsDropStreamable> {
|
||||
) -> anyhow::Result<JsDropStreamable> {
|
||||
let stream = reference.share_with(env, |handler| {
|
||||
let backend = handler
|
||||
.create_backend_for_path(path)
|
||||
.ok_or(napi::Error::from_reason("Failed to create backend."))?;
|
||||
.ok_or(anyhow!("Failed to create backend."))?;
|
||||
let version_file = VersionFile {
|
||||
relative_filename: sub_path,
|
||||
permission: 0, // Shouldn't matter
|
||||
size: 0, // Shouldn't matter
|
||||
};
|
||||
// Use `?` operator for cleaner error propagation from `Option`
|
||||
let reader = backend
|
||||
.reader(
|
||||
&version_file,
|
||||
start.map(|e| e.get_u64().1).unwrap_or(0),
|
||||
end.map(|e| e.get_u64().1).unwrap_or(0),
|
||||
)
|
||||
.ok_or(napi::Error::from_reason("Failed to create reader."))?;
|
||||
let reader = backend.reader(
|
||||
&version_file,
|
||||
start.map(|e| e.get_u64().1).unwrap_or(0),
|
||||
end.map(|e| e.get_u64().1).unwrap_or(0),
|
||||
)?;
|
||||
|
||||
let async_reader = ReadToAsyncRead {
|
||||
inner: reader,
|
||||
};
|
||||
let async_reader = ReadToAsyncRead { inner: reader };
|
||||
|
||||
// Create a FramedRead stream with BytesCodec for chunking
|
||||
let stream = FramedRead::new(async_reader, BytesCodec::new())
|
||||
@ -137,12 +135,12 @@ impl<'a> DropletHandler<'a> {
|
||||
// Apply Result::map to transform Ok(BytesMut) to Ok(Vec<u8>)
|
||||
.map(|bytes| bytes.to_vec())
|
||||
// Apply Result::map_err to transform Err(std::io::Error) to Err(napi::Error)
|
||||
.map_err(|e| napi::Error::from(e)) // napi::Error implements From<tokio::io::Error>
|
||||
.map_err(napi::Error::from) // napi::Error implements From<tokio::io::Error>
|
||||
});
|
||||
// Create the napi-rs ReadableStream from the tokio_stream::Stream
|
||||
// The unwrap() here means if stream creation fails, it will panic.
|
||||
// For a production system, consider returning Result<Option<...>> and handling this.
|
||||
Ok(ReadableStream::create_with_stream_bytes(&env, stream).unwrap())
|
||||
ReadableStream::create_with_stream_bytes(&env, stream)
|
||||
})?;
|
||||
|
||||
Ok(JsDropStreamable { inner: stream })
|
||||
|
||||
Reference in New Issue
Block a user