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

Xuanwo 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 0847f5cc5c fix(bindings/nodejs): reject zero ThrottleLayer arguments 
(#8833)
0847f5cc5c is described below

commit 0847f5cc5cce4b882c4887075d387d38deb9592a
Author: Xuanwo <[email protected]>
AuthorDate: Mon Nov 23 14:49:25 2026 +0800

    fix(bindings/nodejs): reject zero ThrottleLayer arguments (#8833)
    
    fix(bindings/nodejs): reject zero throttle layer arguments
    
    ThrottleLayer accepted zero bandwidth or burst and passed them to the
    Rust core layer during build, where assertions panicked across the N-API
    FFI boundary and aborted the Node.js process.
    
    Validate both values in the constructor and return InvalidArg so
    JavaScript callers receive a normal Error. Add regression tests for zero
    and for values that N-API converts to zero.
---
 bindings/nodejs/generated.d.ts       |  3 +-
 bindings/nodejs/src/layer.rs         | 22 +++++++++++++--
 bindings/nodejs/tests/layer.test.mjs | 54 ++++++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 4 deletions(-)

diff --git a/bindings/nodejs/generated.d.ts b/bindings/nodejs/generated.d.ts
index df7d9dd9c8..9bad0bbff6 100644
--- a/bindings/nodejs/generated.d.ts
+++ b/bindings/nodejs/generated.d.ts
@@ -1002,7 +1002,8 @@ export declare class ThrottleLayer {
    *
    * # Notes
    *
-   * Validation (bandwidth and burst must be greater than 0) is handled by the 
Rust core layer.
+   * `bandwidth` and `burst` must be greater than 0. Values that N-API converts
+   * to 0, such as `NaN`, `Infinity`, and `2 ** 32`, are rejected as well.
    */
   constructor(bandwidth: number, burst: number)
   build(): ExternalObject<Layer>
diff --git a/bindings/nodejs/src/layer.rs b/bindings/nodejs/src/layer.rs
index b78e458b71..b131a20fa4 100644
--- a/bindings/nodejs/src/layer.rs
+++ b/bindings/nodejs/src/layer.rs
@@ -17,8 +17,10 @@
 
 use std::time::Duration;
 
+use napi::bindgen_prelude::Error;
 use napi::bindgen_prelude::External;
 use napi::bindgen_prelude::Result;
+use napi::bindgen_prelude::Status;
 
 pub trait NodeLayer: Send + Sync {
     fn layer(&self, op: opendal::Operator) -> opendal::Operator;
@@ -460,10 +462,24 @@ impl ThrottleLayer {
     ///
     /// # Notes
     ///
-    /// Validation (bandwidth and burst must be greater than 0) is handled by 
the Rust core layer.
+    /// `bandwidth` and `burst` must be greater than 0. Values that N-API 
converts
+    /// to 0, such as `NaN`, `Infinity`, and `2 ** 32`, are rejected as well.
     #[napi(constructor)]
-    pub fn new(bandwidth: u32, burst: u32) -> Self {
-        Self { bandwidth, burst }
+    pub fn new(bandwidth: u32, burst: u32) -> Result<Self> {
+        if bandwidth == 0 {
+            return Err(Error::new(
+                Status::InvalidArg,
+                "bandwidth must be greater than 0",
+            ));
+        }
+        if burst == 0 {
+            return Err(Error::new(
+                Status::InvalidArg,
+                "burst must be greater than 0",
+            ));
+        }
+
+        Ok(Self { bandwidth, burst })
     }
 
     #[napi]
diff --git a/bindings/nodejs/tests/layer.test.mjs 
b/bindings/nodejs/tests/layer.test.mjs
new file mode 100644
index 0000000000..e85362557f
--- /dev/null
+++ b/bindings/nodejs/tests/layer.test.mjs
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { assert, test } from 'vitest'
+
+import { ThrottleLayer } from '../index.mjs'
+
+function assertInvalidArgument(createLayer, field) {
+  let error
+  try {
+    createLayer()
+  } catch (err) {
+    error = err
+  }
+
+  assert.instanceOf(error, Error)
+  assert.match(error.message, new RegExp(field))
+}
+
+// N-API converts these values to 0 when it reads the u32 constructor 
arguments.
+const zeroValues = [0, 2 ** 32, NaN, Infinity]
+
+for (const bandwidth of zeroValues) {
+  test(`ThrottleLayer rejects bandwidth ${bandwidth}`, () => {
+    assertInvalidArgument(() => new ThrottleLayer(bandwidth, 1024), 
'bandwidth')
+  })
+}
+
+for (const burst of zeroValues) {
+  test(`ThrottleLayer rejects burst ${burst}`, () => {
+    assertInvalidArgument(() => new ThrottleLayer(1024, burst), 'burst')
+  })
+}
+
+test('ThrottleLayer accepts positive bandwidth and burst', () => {
+  const layer = new ThrottleLayer(1024, 2048)
+  assert.ok(layer.build())
+})

Reply via email to