chore(storage): bump y-octo (#5751)

This commit is contained in:
liuyi 2024-01-31 06:54:33 +00:00
parent db8e49b046
commit 72d9cc1e5b
No known key found for this signature in database
GPG Key ID: 56709255DC7EC728
5 changed files with 58 additions and 1101 deletions

1000
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -15,8 +15,6 @@ try {
: require('../../../storage.node'); : require('../../../storage.node');
} }
export { storageModule as OctoBaseStorageModule };
export const mergeUpdatesInApplyWay = storageModule.mergeUpdatesInApplyWay; export const mergeUpdatesInApplyWay = storageModule.mergeUpdatesInApplyWay;
export const verifyChallengeResponse = async ( export const verifyChallengeResponse = async (

View File

@ -8,9 +8,6 @@ crate-type = ["cdylib"]
[dependencies] [dependencies]
chrono = "0.4" chrono = "0.4"
jwst-codec = { git = "https://github.com/toeverything/OctoBase.git", rev = "49a6b7a" }
jwst-core = { git = "https://github.com/toeverything/OctoBase.git", rev = "49a6b7a" }
jwst-storage = { git = "https://github.com/toeverything/OctoBase.git", rev = "49a6b7a" }
napi = { version = "2", default-features = false, features = [ napi = { version = "2", default-features = false, features = [
"napi5", "napi5",
"async", "async",
@ -18,6 +15,7 @@ napi = { version = "2", default-features = false, features = [
napi-derive = { version = "2", features = ["type-def"] } napi-derive = { version = "2", features = ["type-def"] }
rand = "0.8" rand = "0.8"
sha3 = "0.10" sha3 = "0.10"
y-octo = { git = "https://github.com/y-crdt/y-octo.git", branch = "main" }
[dev-dependencies] [dev-dependencies]
tokio = "1" tokio = "1"

View File

@ -1,28 +1,6 @@
/* auto-generated by NAPI-RS */ /* auto-generated by NAPI-RS */
/* eslint-disable */ /* eslint-disable */
export class Storage {
/** Create a storage instance and establish connection to persist store. */
static connect(database: string, debugOnlyAutoMigrate?: boolean | undefined | null): Promise<Storage>
/** List all blobs in a workspace. */
listBlobs(workspaceId?: string | undefined | null): Promise<Array<string>>
/** Fetch a workspace blob. */
getBlob(workspaceId: string, name: string): Promise<Blob | null>
/** Upload a blob into workspace storage. */
uploadBlob(workspaceId: string, blob: Buffer): Promise<string>
/** Delete a blob from workspace storage. */
deleteBlob(workspaceId: string, hash: string): Promise<boolean>
/** Workspace size taken by blobs. */
blobsSize(workspaces: Array<string>): Promise<number>
}
export interface Blob {
contentType: string
lastModified: string
size: number
data: Buffer
}
/** /**
* Merge updates in form like `Y.applyUpdate(doc, update)` way and return the * Merge updates in form like `Y.applyUpdate(doc, update)` way and return the
* result binary. * result binary.

View File

@ -2,16 +2,10 @@
pub mod hashcash; pub mod hashcash;
use std::{ use std::fmt::{Debug, Display};
collections::HashMap,
fmt::{Debug, Display},
path::PathBuf,
};
use jwst_codec::Doc;
use jwst_core::BlobStorage;
use jwst_storage::{BlobStorageType, JwstStorage, JwstStorageError};
use napi::{bindgen_prelude::*, Error, Result, Status}; use napi::{bindgen_prelude::*, Error, Result, Status};
use y_octo::Doc;
#[macro_use] #[macro_use]
extern crate napi_derive; extern crate napi_derive;
@ -35,132 +29,13 @@ macro_rules! map_err {
}; };
} }
macro_rules! napi_wrap {
($( ($name: ident, $target: ident) ),*) => {
$(
#[napi]
pub struct $name($target);
impl std::ops::Deref for $name {
type Target = $target;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<$target> for $name {
fn from(t: $target) -> Self {
Self(t)
}
}
)*
};
}
napi_wrap!((Storage, JwstStorage));
#[napi(object)]
pub struct Blob {
pub content_type: String,
pub last_modified: String,
pub size: i64,
pub data: Buffer,
}
#[napi]
impl Storage {
/// Create a storage instance and establish connection to persist store.
#[napi]
pub async fn connect(database: String, debug_only_auto_migrate: Option<bool>) -> Result<Storage> {
let inner = match if cfg!(debug_assertions) && debug_only_auto_migrate.unwrap_or(false) {
JwstStorage::new_with_migration(&database, BlobStorageType::DB).await
} else {
JwstStorage::new(&database, BlobStorageType::DB).await
} {
Ok(storage) => storage,
Err(JwstStorageError::Db(e)) => {
return Err(Error::new(
Status::GenericFailure,
format!("failed to connect to database: {}", e),
));
}
Err(e) => return Err(Error::new(Status::GenericFailure, e.to_string())),
};
Ok(inner.into())
}
/// List all blobs in a workspace.
#[napi]
pub async fn list_blobs(&self, workspace_id: Option<String>) -> Result<Vec<String>> {
map_err!(self.blobs().list_blobs(workspace_id).await)
}
/// Fetch a workspace blob.
#[napi]
pub async fn get_blob(&self, workspace_id: String, name: String) -> Result<Option<Blob>> {
let (id, params) = {
let path = PathBuf::from(name.clone());
let ext = path
.extension()
.and_then(|s| s.to_str().map(|s| s.to_string()));
let id = path
.file_stem()
.and_then(|s| s.to_str().map(|s| s.to_string()))
.unwrap_or(name);
(id, ext.map(|ext| HashMap::from([("format".into(), ext)])))
};
let Ok(meta) = self
.blobs()
.get_metadata(Some(workspace_id.clone()), id.clone(), params.clone())
.await
else {
return Ok(None);
};
let Ok(file) = self.blobs().get_blob(Some(workspace_id), id, params).await else {
return Ok(None);
};
Ok(Some(Blob {
content_type: meta.content_type,
last_modified: format!("{:?}", meta.last_modified),
size: meta.size,
data: file.into(),
}))
}
/// Upload a blob into workspace storage.
#[napi]
pub async fn upload_blob(&self, workspace_id: String, blob: Buffer) -> Result<String> {
// TODO: can optimize, avoid copy
let blob = blob.as_ref().to_vec();
map_err!(self.blobs().put_blob(Some(workspace_id), blob).await)
}
/// Delete a blob from workspace storage.
#[napi]
pub async fn delete_blob(&self, workspace_id: String, hash: String) -> Result<bool> {
map_err!(self.blobs().delete_blob(Some(workspace_id), hash).await)
}
/// Workspace size taken by blobs.
#[napi]
pub async fn blobs_size(&self, workspaces: Vec<String>) -> Result<i64> {
map_err!(self.blobs().get_blobs_size(workspaces).await)
}
}
/// Merge updates in form like `Y.applyUpdate(doc, update)` way and return the /// Merge updates in form like `Y.applyUpdate(doc, update)` way and return the
/// result binary. /// result binary.
#[napi(catch_unwind)] #[napi(catch_unwind)]
pub fn merge_updates_in_apply_way(updates: Vec<Buffer>) -> Result<Buffer> { pub fn merge_updates_in_apply_way(updates: Vec<Buffer>) -> Result<Buffer> {
let mut doc = Doc::default(); let mut doc = Doc::default();
for update in updates { for update in updates {
map_err!(doc.apply_update_from_binary(update.as_ref().to_vec()))?; map_err!(doc.apply_update_from_binary_v1(update.as_ref()))?;
} }
let buf = map_err!(doc.encode_update_v1())?; let buf = map_err!(doc.encode_update_v1())?;