Skip to content

Commit

Permalink
bump napi
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Oct 8, 2024
1 parent 6a5888f commit 1affc36
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
6 changes: 3 additions & 3 deletions bindings/nodejs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ crate-type = ["cdylib"]
doc = false

[dependencies]
futures = "0.3.28"
napi = { version = "2.11.3", default-features = false, features = [
futures = "0.3.31"
napi = { version = "3.0.0-alpha.13", default-features = false, features = [
"napi6",
"async",
] }
napi-derive = "2.14.6"
napi-derive = "3.0.0-alpha.13"
# this crate won't be published, we always use the local version
opendal = { version = ">=0", path = "../../core", features = [
"layers-blocking",
Expand Down
23 changes: 12 additions & 11 deletions bindings/nodejs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
extern crate napi_derive;

use std::collections::HashMap;
use std::fmt::Display;
use std::io::Read;
use std::str::FromStr;
use std::time::Duration;
Expand Down Expand Up @@ -741,7 +740,12 @@ impl BlockingReader {
#[napi]
pub fn read(&mut self, mut buf: Buffer) -> Result<usize> {
let buf = buf.as_mut();
let n = self.inner.read(buf).map_err(format_napi_error)?;
let n = self.inner.read(buf).map_err(|err| {
format_napi_error(
opendal::Error::new(opendal::ErrorKind::Unexpected, err.to_string())
.set_source(err),
)
})?;
Ok(n)
}
}
Expand Down Expand Up @@ -1200,8 +1204,9 @@ impl AsRef<str> for ErrorCode {
}
}

impl From<opendal::Error> for ErrorCode {
fn from(err: opendal::Error) -> Self {
/// Format opendal error to napi error.
fn format_napi_error(err: opendal::Error) -> Error<ErrorCode> {
Error::new(
match err.kind() {
opendal::ErrorKind::Unexpected => ErrorCode::Unexpected,
opendal::ErrorKind::Unsupported => ErrorCode::Unsupported,
Expand All @@ -1216,11 +1221,7 @@ impl From<opendal::Error> for ErrorCode {
opendal::ErrorKind::ConditionNotMatch => ErrorCode::ConditionNotMatch,
opendal::ErrorKind::RangeNotSatisfied => ErrorCode::RangeNotSatisfied,
kind => unimplemented!("error kind not supported: {:?}", kind),
}
}
}

/// Format opendal error to napi error.
fn format_napi_error(err: opendal::Error) -> Error<ErrorCode> {
Error::new(err.kind().into(), err.to_string())
},
err.to_string(),
)
}

0 comments on commit 1affc36

Please sign in to comment.