Skip to content

Commit

Permalink
feat(binding/node.js): update binding func to operator's style
Browse files Browse the repository at this point in the history
Signed-off-by: suyanhanx <suyanhanx@gmail.com>
  • Loading branch information
suyanhanx committed Mar 3, 2023
1 parent 582c81a commit 3ca125e
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 61 deletions.
5 changes: 2 additions & 3 deletions bindings/nodejs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ crate-type = ["cdylib"]

[dependencies]
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2", default-features = false, features = ["napi4", "async"] }
napi-derive = "2"
napi = { version = "2.11.2", default-features = false, features = ["napi4", "async"] }
napi-derive = "2.11.1"
opendal = { version = "0.29", path = "../../"}
bytes = "1.4.0"
chrono = "0.4.23"
futures = "0.3.26"

Expand Down
12 changes: 6 additions & 6 deletions bindings/nodejs/__test__/index.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ import { OperatorFactory } from '../index.js'

test('test memory write & read', async (t) => {
let op = OperatorFactory.memory()
let content = "hello wowld"
let path = 'test.txt'
let content = "hello world"
let path = 'test'

await op.put(path, content)
await op.write(path, Array.from(new TextEncoder().encode(content)))

let meta = await op.head(path)
let meta = await op.meta(path)
t.is(meta.size, content.length)

let res = await op.get('test.txt')
t.is(content, res)
let res = await op.read(path)
t.is(content, new TextDecoder().decode(Buffer.from(res)))

await op.delete(path)
})
12 changes: 5 additions & 7 deletions bindings/nodejs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@

/* auto-generated by NAPI-RS */

export function debug(): string
export class OperatorFactory {
static memory(): OpendalStore
static memory(): Operator
}
export class ObjectMeta {
location: string
lastModified: number
size: number
}
export class OpendalStore {
meta(): string
head(path: string): Promise<ObjectMeta>
put(path: string, content: string): Promise<void>
get(path: string): Promise<string>
export class Operator {
meta(path: string): Promise<ObjectMeta>
write(path: string, content: Array<number>): Promise<void>
read(path: string): Promise<Array<number>>
delete(path: string): Promise<void>
}
5 changes: 2 additions & 3 deletions bindings/nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,8 @@ if (!nativeBinding) {
throw new Error(`Failed to load native binding`)
}

const { debug, OperatorFactory, ObjectMeta, OpendalStore } = nativeBinding
const { OperatorFactory, ObjectMeta, Operator } = nativeBinding

module.exports.debug = debug
module.exports.OperatorFactory = OperatorFactory
module.exports.ObjectMeta = ObjectMeta
module.exports.OpendalStore = OpendalStore
module.exports.Operator = Operator
2 changes: 1 addition & 1 deletion bindings/nodejs/npm/darwin-arm64/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
"engines": {
"node": ">= 10"
}
}
}
2 changes: 1 addition & 1 deletion bindings/nodejs/npm/darwin-universal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
"engines": {
"node": ">= 10"
}
}
}
2 changes: 1 addition & 1 deletion bindings/nodejs/npm/darwin-x64/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
"engines": {
"node": ">= 10"
}
}
}
2 changes: 1 addition & 1 deletion bindings/nodejs/npm/linux-arm-gnueabihf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
"engines": {
"node": ">= 10"
}
}
}
2 changes: 1 addition & 1 deletion bindings/nodejs/npm/linux-arm64-gnu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
"libc": [
"glibc"
]
}
}
2 changes: 1 addition & 1 deletion bindings/nodejs/npm/linux-x64-gnu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
"libc": [
"glibc"
]
}
}
56 changes: 20 additions & 36 deletions bindings/nodejs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,22 @@ use chrono::DateTime;
use chrono::NaiveDateTime;
use chrono::Utc;
use opendal::services;
use opendal::Operator;
use bytes::Bytes;
use futures::prelude::*;
use napi::bindgen_prelude::*;

#[napi]
pub fn debug() -> String {
let op = Operator::create(services::Memory::default())
.unwrap()
.finish();
format!("{:?}", op.metadata())
}


#[napi]
pub struct OperatorFactory {}

#[napi]
impl OperatorFactory {
#[napi]
pub fn memory() -> napi::Result<OpendalStore> {
let op = Operator::create(services::Memory::default())
pub fn memory() -> Result<Operator> {
let op = opendal::Operator::create(services::Memory::default())
.unwrap()
.finish();

Ok(OpendalStore::new(op))
Ok(Operator::new(op))
}
}

Expand All @@ -62,28 +52,23 @@ pub struct ObjectMeta {
}

#[napi]
pub struct OpendalStore {
inner: Operator
pub struct Operator {
inner: opendal::Operator
}

#[napi]
impl OpendalStore {
pub fn new(op: Operator) -> Self {
impl Operator {
pub fn new(op: opendal::Operator) -> Self {
Self { inner: op }
}

#[napi]
pub fn meta(&self) -> Result<String> {
Ok(format!("{:?}", self.inner.metadata()))
}

#[napi]
pub async fn head(&self, path: String) -> Result<ObjectMeta> {
pub async fn meta(&self, path: String) -> Result<ObjectMeta> {
let o = self.inner.object(&path);
let meta = o
.stat()
.await
.map_err(|err| Error::new(Status::Unknown, format!("stats get failure: {}", err)))
.map_err(format_napi_error)
.unwrap();

let (secs, nsecs) = meta
Expand All @@ -103,33 +88,32 @@ impl OpendalStore {
}

#[napi]
pub async fn put(&self, path: String, content: String) -> Result<()> {
pub async fn write(&self, path: String, content: Vec<u8>) -> Result<()> {
self.inner.object(&path)
.write(Bytes::from(content))
.map_err(|err|
Error::new(Status::Unknown, format!("write failure: {}", err))
)
.write(content)
.map_err(format_napi_error)
.await
}

#[napi]
pub async fn get(&self, path: String) -> Result<String> {
pub async fn read(&self, path: String) -> Result<Vec<u8>> {
let res = self.inner.object(&path)
.read()
.await
.map_err(|err|
Error::new(Status::Unknown, format!("read failure: {}", err)))
.map_err(format_napi_error)
.unwrap();
Ok(str::from_utf8(&res)
.unwrap()
.to_string())
Ok(res)
}

#[napi]
pub async fn delete(&self, path: String) -> Result<()> {
let o = self.inner.object(&path);
o.delete()
.await
.map_err(|err| Error::new(Status::Unknown, format!("delete failure: {}", err)))
.map_err(format_napi_error)
}
}

fn format_napi_error(err: opendal::Error) -> Error {
Error::from_reason(format!("{}", err))
}

0 comments on commit 3ca125e

Please sign in to comment.