You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by te...@apache.org on 2026/05/02 12:36:12 UTC

(opendal) branch main updated: feat(bindings/nodejs): add Operator.fromUri static factory (#7471)

This is an automated email from the ASF dual-hosted git repository.

TennyZhuang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new 2a24aecc7 feat(bindings/nodejs): add Operator.fromUri static factory (#7471)
2a24aecc7 is described below

commit 2a24aecc755277f2c4f83a01762721d704b56fae
Author: TennyZhuang <zt...@gmail.com>
AuthorDate: Sat May 2 20:36:06 2026 +0800

    feat(bindings/nodejs): add Operator.fromUri static factory (#7471)
    
    Add a static fromUri(uri, options?) factory method to the Node.js
    Operator class, mirroring Rust Operator::from_uri and Python
    Operator.from_uri (PR #6993).
    
    The URI encodes scheme and configuration in a single string, e.g.
      memory://localhost/
      s3://bucket/path?region=us-east-1
    
    Optional extra key-value options can be passed to override or
    supplement values encoded in the URI.
    
    Intentional divergence: none. Direct 1:1 mapping of Rust
    Operator::from_uri((uri, options)) via IntoOperatorUri.
    
    Scope: bindings/nodejs only (lib.rs + generated.d.ts).
    AI-assisted: od-claude-lead-050121, reviewed by OpenDAL regression team.
    
    Co-authored-by: tianyizhuang <ti...@tianyizhuangs-Mac-mini.local>
---
 bindings/nodejs/generated.d.ts | 17 +++++++++++++++++
 bindings/nodejs/src/lib.rs     | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git a/bindings/nodejs/generated.d.ts b/bindings/nodejs/generated.d.ts
index 77ddf0705..c8cc6681c 100644
--- a/bindings/nodejs/generated.d.ts
+++ b/bindings/nodejs/generated.d.ts
@@ -378,6 +378,23 @@ export declare class Operator {
    * Note that the current options key is snake_case.
    */
   constructor(scheme: string, options?: Record<string, string> | undefined | null)
+  /**
+   * Create a new operator from a URI string.
+   *
+   * The URI encodes the scheme and configuration in a single string, e.g.
+   * `memory://localhost/` or `s3://bucket/path?region=us-east-1`.
+   *
+   * Optional extra key-value options can be passed to override or supplement
+   * the values encoded in the URI.
+   *
+   * ### Example
+   *
+   * ```javascript
+   * const op = Operator.fromUri("memory://localhost/");
+   * const op2 = Operator.fromUri("s3://my-bucket/", { region: "us-east-1" });
+   * ```
+   */
+  static fromUri(uri: string, options?: Record<string, string> | undefined | null): Operator
   /** Get current operator(service)'s full capability. */
   capability(): Capability
   /**
diff --git a/bindings/nodejs/src/lib.rs b/bindings/nodejs/src/lib.rs
index e56380e0a..f65e5d28f 100644
--- a/bindings/nodejs/src/lib.rs
+++ b/bindings/nodejs/src/lib.rs
@@ -63,6 +63,38 @@ impl Operator {
         })
     }
 
+    /// Create a new operator from a URI string.
+    ///
+    /// The URI encodes the scheme and configuration in a single string, e.g.
+    /// `memory://localhost/` or `s3://bucket/path?region=us-east-1`.
+    ///
+    /// Optional extra key-value options can be passed to override or supplement
+    /// the values encoded in the URI.
+    ///
+    /// ### Example
+    ///
+    /// ```javascript
+    /// const op = Operator.fromUri("memory://localhost/");
+    /// const op2 = Operator.fromUri("s3://my-bucket/", { region: "us-east-1" });
+    /// ```
+    #[napi(factory, async_runtime)]
+    pub fn from_uri(uri: String, options: Option<HashMap<String, String>>) -> Result<Self> {
+        let options = options.unwrap_or_default();
+        let async_op = if options.is_empty() {
+            opendal::Operator::from_uri(uri).map_err(format_napi_error)?
+        } else {
+            opendal::Operator::from_uri((uri.as_str(), options)).map_err(format_napi_error)?
+        };
+
+        let blocking_op =
+            opendal::blocking::Operator::new(async_op.clone()).map_err(format_napi_error)?;
+
+        Ok(Operator {
+            async_op,
+            blocking_op,
+        })
+    }
+
     /// Get current operator(service)'s full capability.
     #[napi]
     pub fn capability(&self) -> Result<capability::Capability> {