#![cfg(feature = "napi")]
use napi::{
bindgen_prelude::*,
JsObject, JsString, JsBuffer,
};
#[cfg(feature = "s3")]
#[napi]
pub struct JsS3Reader {
inner: S3Reader,
}
#[cfg(feature = "s3")]
#[napi]
impl JsS3Reader {
#[napi(constructor)]
pub fn new(region: String) -> napi::Result<Self> {
let inner = S3Reader::new(®ion)
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))?;
Ok(JsS3Reader { inner })
}
#[napi]
pub fn with_cache(mut self, cache_dir: String) -> napi::Result<Self> {
self.inner = self.inner.with_cache(cache_dir);
Ok(self)
}
#[napi]
pub async fn read_file(
&self,
bucket: String,
key: String,
) -> napi::Result<Vec<u8>> {
let reader = self.inner.clone();
tokio::spawn(async move {
reader.read_file(&bucket, &key)
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))
})
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))?
}
#[napi]
pub async fn write_file(
&self,
bucket: String,
key: String,
data: Vec<u8>,
) -> napi::Result<()> {
let reader = self.inner.clone();
tokio::spawn(async move {
reader.write_file(&bucket, &key, data)
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))
})
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))?
}
#[napi]
pub async fn list_files(
&self,
bucket: String,
prefix: Option<String>,
) -> napi::Result<Vec<String>> {
let reader = self.inner.clone();
tokio::spawn(async move {
reader.list_files(&bucket, prefix.as_deref())
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))
})
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))?
}
#[napi]
pub async fn get_metadata(
&self,
bucket: String,
key: String,
) -> napi::Result<JsObject> {
Err(napi::Error::new(
napi::Status::GenericFailure,
"Cloud SDK bindings available in v1.1".to_string(),
))
}
}
#[cfg(feature = "azure")]
#[napi]
pub struct JsAzureBlobReader {
inner: AzureBlobReader,
}
#[cfg(feature = "azure")]
#[napi]
impl JsAzureBlobReader {
#[napi(constructor)]
pub fn new(storage_account: String, account_key: String) -> napi::Result<Self> {
let inner = AzureBlobReader::new(&storage_account, &account_key)
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))?;
Ok(JsAzureBlobReader { inner })
}
#[napi]
pub fn with_cache(mut self, cache_dir: String) -> napi::Result<Self> {
self.inner = self.inner.with_cache(cache_dir);
Ok(self)
}
#[napi]
pub async fn read_file(
&self,
container: String,
blob_path: String,
) -> napi::Result<Vec<u8>> {
let reader = self.inner.clone();
tokio::spawn(async move {
reader.read_file(&container, &blob_path)
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))
})
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))?
}
#[napi]
pub async fn write_file(
&self,
container: String,
blob_path: String,
data: Vec<u8>,
) -> napi::Result<()> {
let reader = self.inner.clone();
tokio::spawn(async move {
reader.write_file(&container, &blob_path, data)
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))
})
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))?
}
#[napi]
pub async fn list_blobs(
&self,
container: String,
prefix: Option<String>,
) -> napi::Result<Vec<String>> {
let reader = self.inner.clone();
tokio::spawn(async move {
reader.list_blobs(&container, prefix.as_deref())
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))
})
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))?
}
#[napi]
pub async fn get_metadata(
&self,
container: String,
blob_path: String,
) -> napi::Result<JsObject> {
Err(napi::Error::new(
napi::Status::GenericFailure,
"Cloud SDK bindings available in v1.1".to_string(),
))
}
}
#[cfg(feature = "gcs")]
#[napi]
pub struct JsGcsReader {
inner: GcsReader,
}
#[cfg(feature = "gcs")]
#[napi]
impl JsGcsReader {
#[napi(constructor)]
pub fn new(project_id: String) -> napi::Result<Self> {
let inner = GcsReader::new(&project_id)
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))?;
Ok(JsGcsReader { inner })
}
#[napi]
pub fn with_cache(mut self, cache_dir: String) -> napi::Result<Self> {
self.inner = self.inner.with_cache(cache_dir);
Ok(self)
}
#[napi]
pub async fn read_file(
&self,
bucket: String,
object_path: String,
) -> napi::Result<Vec<u8>> {
let reader = self.inner.clone();
tokio::spawn(async move {
reader.read_file(&bucket, &object_path)
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))
})
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))?
}
#[napi]
pub async fn write_file(
&self,
bucket: String,
object_path: String,
data: Vec<u8>,
) -> napi::Result<()> {
let reader = self.inner.clone();
tokio::spawn(async move {
reader.write_file(&bucket, &object_path, data)
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))
})
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))?
}
#[napi]
pub async fn list_objects(
&self,
bucket: String,
prefix: Option<String>,
) -> napi::Result<Vec<String>> {
let reader = self.inner.clone();
tokio::spawn(async move {
reader.list_objects(&bucket, prefix.as_deref())
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))
})
.await
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e.to_string()))?
}
#[napi]
pub async fn get_metadata(
&self,
bucket: String,
object_path: String,
) -> napi::Result<JsObject> {
Err(napi::Error::new(
napi::Status::GenericFailure,
"Cloud SDK bindings available in v1.1".to_string(),
))
}
}