mirror of
https://github.com/Drop-OSS/droplet.git
synced 2025-11-14 08:41:25 +10:00
feat: performance improvements, fix zip
This commit is contained in:
@ -1,10 +1,6 @@
|
||||
use std::{
|
||||
fs::{self, metadata, File},
|
||||
io::Read,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use std::{collections::HashMap, fs::File, io::Read, path::Path};
|
||||
|
||||
use napi::{bindgen_prelude::*, tokio_stream::StreamExt};
|
||||
use napi::{bindgen_prelude::*, sys::napi_value__, tokio_stream::StreamExt};
|
||||
use tokio_util::codec::{BytesCodec, FramedRead};
|
||||
|
||||
use crate::version::{
|
||||
@ -12,120 +8,160 @@ use crate::version::{
|
||||
types::{ReadToAsyncRead, VersionBackend, VersionFile},
|
||||
};
|
||||
|
||||
pub fn _list_files(vec: &mut Vec<PathBuf>, path: &Path) {
|
||||
if metadata(path).unwrap().is_dir() {
|
||||
let paths = fs::read_dir(path).unwrap();
|
||||
for path_result in paths {
|
||||
let full_path = path_result.unwrap().path();
|
||||
if metadata(&full_path).unwrap().is_dir() {
|
||||
_list_files(vec, &full_path);
|
||||
} else {
|
||||
vec.push(full_path);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Append new backends here
|
||||
*/
|
||||
pub fn create_backend_constructor<'a>(
|
||||
path: &Path,
|
||||
) -> Option<Box<dyn FnOnce() -> Box<dyn VersionBackend + Send + 'a>>> {
|
||||
if !path.exists() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_backend_for_path<'a>(path: &Path) -> Option<Box<(dyn VersionBackend + Send + 'a)>> {
|
||||
let is_directory = path.is_dir();
|
||||
if is_directory {
|
||||
return Some(Box::new(PathVersionBackend {
|
||||
base_dir: path.to_path_buf(),
|
||||
}));
|
||||
let base_dir = path.to_path_buf();
|
||||
return Some(Box::new(move || 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(ZipVersionBackend::new(f)));
|
||||
return Some(Box::new(|| Box::new(ZipVersionBackend::new(f))));
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn has_backend_for_path(path: String) -> bool {
|
||||
let path = Path::new(&path);
|
||||
|
||||
let has_backend = create_backend_for_path(path).is_some();
|
||||
|
||||
has_backend
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn list_files(path: String) -> Result<Vec<String>> {
|
||||
let path = Path::new(&path);
|
||||
let mut backend =
|
||||
create_backend_for_path(path).ok_or(napi::Error::from_reason("No backend for path"))?;
|
||||
let files = backend.list_files();
|
||||
Ok(files.into_iter().map(|e| e.relative_filename).collect())
|
||||
}
|
||||
|
||||
/**
|
||||
* This is inefficient, but is used in attempt to keep the interface simple
|
||||
* Persistent object so we can cache things between commands
|
||||
*/
|
||||
#[napi]
|
||||
pub fn peek_file(path: String, sub_path: String) -> Result<u64> {
|
||||
let path = Path::new(&path);
|
||||
let mut backend =
|
||||
create_backend_for_path(path).ok_or(napi::Error::from_reason("No backend for path"))?;
|
||||
let files = backend.list_files();
|
||||
|
||||
let file = files
|
||||
.iter()
|
||||
.find(|e| e.relative_filename == sub_path)
|
||||
.ok_or(napi::Error::from_reason("Can't find file to peek"))?;
|
||||
return Ok(file.size.try_into().unwrap());
|
||||
#[napi(js_name = "DropletHandler")]
|
||||
pub struct DropletHandler<'a> {
|
||||
backend_cache: HashMap<String, Box<dyn VersionBackend + Send + 'a>>,
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn read_file(
|
||||
path: String,
|
||||
sub_path: String,
|
||||
env: &Env,
|
||||
start: Option<BigInt>,
|
||||
end: Option<BigInt>,
|
||||
) -> Option<ReadableStream<'_, BufferSlice<'_>>> {
|
||||
let path = Path::new(&path);
|
||||
let mut backend = create_backend_for_path(path).unwrap();
|
||||
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 mut reader = backend.reader(&version_file)?;
|
||||
|
||||
// Skip the 'start' amount of bytes without seek
|
||||
if let Some(skip) = start.clone() {
|
||||
reader.skip(skip.get_u64().1.into());
|
||||
// io::copy(&mut reader.by_ref().take(skip.into()), &mut io::sink()).unwrap();
|
||||
impl<'a> DropletHandler<'a> {
|
||||
#[napi(constructor)]
|
||||
pub fn new() -> Self {
|
||||
DropletHandler {
|
||||
backend_cache: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
let async_reader = if let Some(limit) = end {
|
||||
let amount = limit.get_u64().1 - start.map_or(Some(0), |v| Some(v.get_u64().1)).unwrap();
|
||||
ReadToAsyncRead {
|
||||
inner: Box::new(reader.take(amount.into())),
|
||||
backend,
|
||||
}
|
||||
} else {
|
||||
ReadToAsyncRead {
|
||||
inner: reader,
|
||||
backend,
|
||||
}
|
||||
};
|
||||
pub fn create_backend_for_path(
|
||||
&mut self,
|
||||
path: String,
|
||||
) -> Option<&mut Box<dyn VersionBackend + Send + 'a>> {
|
||||
let fs_path = Path::new(&path);
|
||||
let constructor = create_backend_constructor(fs_path)?;
|
||||
|
||||
// Create a FramedRead stream with BytesCodec for chunking
|
||||
let stream = FramedRead::new(async_reader, BytesCodec::new())
|
||||
// Use StreamExt::map to transform each Result item
|
||||
.map(|result_item| {
|
||||
result_item
|
||||
// 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>
|
||||
let existing_backend = self.backend_cache.entry(path).or_insert_with(|| {
|
||||
let backend = constructor();
|
||||
backend
|
||||
});
|
||||
// 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.
|
||||
Some(ReadableStream::create_with_stream_bytes(env, stream).unwrap())
|
||||
|
||||
Some(existing_backend)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn has_backend_for_path(&self, path: String) -> bool {
|
||||
let path = Path::new(&path);
|
||||
|
||||
let has_backend = create_backend_constructor(path).is_some();
|
||||
|
||||
has_backend
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn list_files(&mut self, path: String) -> Result<Vec<String>> {
|
||||
let backend = self
|
||||
.create_backend_for_path(path)
|
||||
.ok_or(napi::Error::from_reason("No backend for path"))?;
|
||||
let files = backend.list_files();
|
||||
Ok(files.into_iter().map(|e| e.relative_filename).collect())
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn peek_file(&mut self, path: String, sub_path: String) -> Result<u64> {
|
||||
let backend = self
|
||||
.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"))?;
|
||||
|
||||
return Ok(file.size.try_into().unwrap());
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub fn read_file(
|
||||
&mut self,
|
||||
reference: Reference<DropletHandler<'static>>,
|
||||
path: String,
|
||||
sub_path: String,
|
||||
env: Env,
|
||||
start: Option<BigInt>,
|
||||
end: Option<BigInt>,
|
||||
) -> 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."))?;
|
||||
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 mut reader = backend.reader(&version_file).ok_or(napi::Error::from_reason("Failed to create reader."))?;
|
||||
|
||||
if let Some(skip) = start.clone() {
|
||||
reader.skip(skip.get_u64().1.into());
|
||||
// io::copy(&mut reader.by_ref().take(skip.into()), &mut io::sink()).unwrap();
|
||||
}
|
||||
|
||||
let async_reader = if let Some(limit) = end {
|
||||
let amount = limit.get_u64().1 - start.map_or(Some(0), |v| Some(v.get_u64().1)).unwrap();
|
||||
ReadToAsyncRead {
|
||||
inner: Box::new(reader.take(amount.into())),
|
||||
}
|
||||
} else {
|
||||
ReadToAsyncRead { inner: reader }
|
||||
};
|
||||
|
||||
// Create a FramedRead stream with BytesCodec for chunking
|
||||
let stream = FramedRead::new(async_reader, BytesCodec::new())
|
||||
// Use StreamExt::map to transform each Result item
|
||||
.map(|result_item| {
|
||||
result_item
|
||||
// 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>
|
||||
});
|
||||
// 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())
|
||||
})?;
|
||||
|
||||
Ok(JsDropStreamable {
|
||||
inner: stream,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub struct JsDropStreamable {
|
||||
inner: SharedReference<DropletHandler<'static>, ReadableStream<'static, BufferSlice<'static>>>,
|
||||
}
|
||||
|
||||
#[napi]
|
||||
impl JsDropStreamable {
|
||||
#[napi]
|
||||
pub fn get_stream(&self) -> *mut napi_value__ {
|
||||
self.inner.raw()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user