Repository: denoland/deno
Pull request: fix(ext/napi): suppress deprecated Buffer warnings

Fixes: https://github.com/denoland/deno/issues/30584

Changed files:
filename: ext/napi/lib.rs
patch: @@ -391,7 +391,7 @@ pub struct Env {
   pub last_error: napi_extended_error_info,
   pub last_exception: Option<v8::Global<v8::Value>>,
   pub global: v8::Global<v8::Object>,
-  pub buffer_constructor: v8::Global<v8::Function>,
+  pub create_buffer: v8::Global<v8::Function>,
   pub report_error: v8::Global<v8::Function>,
 }
 
@@ -404,7 +404,7 @@ impl Env {
     isolate_ptr: v8::UnsafeRawIsolatePtr,
     context: v8::Global<v8::Context>,
     global: v8::Global<v8::Object>,
-    buffer_constructor: v8::Global<v8::Function>,
+    create_buffer: v8::Global<v8::Function>,
     report_error: v8::Global<v8::Function>,
     sender: V8CrossThreadTaskSpawner,
     cleanup_hooks: Rc<RefCell<Vec<(napi_cleanup_hook, *mut c_void)>>>,
@@ -414,7 +414,7 @@ impl Env {
       isolate_ptr,
       context: context.into_raw(),
       global,
-      buffer_constructor,
+      create_buffer,
       report_error,
       shared: std::ptr::null_mut(),
       open_handle_scopes: 0,
@@ -559,7 +559,7 @@ fn op_napi_open<NP, 'scope>(
   op_state: Rc<RefCell<OpState>>,
   #[string] path: &str,
   global: v8::Local<'scope, v8::Object>,
-  buffer_constructor: v8::Local<'scope, v8::Function>,
+  create_buffer: v8::Local<'scope, v8::Function>,
   report_error: v8::Local<'scope, v8::Function>,
 ) -> Result<v8::Local<'scope, v8::Value>, NApiError>
 where
@@ -605,7 +605,7 @@ where
     unsafe { isolate.as_raw_isolate_ptr() },
     v8::Global::new(scope, ctx),
     v8::Global::new(scope, global),
-    v8::Global::new(scope, buffer_constructor),
+    v8::Global::new(scope, create_buffer),
     v8::Global::new(scope, report_error),
     async_work_sender,
     cleanup_hooks,

filename: ext/napi/node_api.rs
patch: @@ -299,9 +299,9 @@ fn napi_create_buffer<'s>(
 
   let ab = v8::ArrayBuffer::new(scope, length);
 
-  let buffer_constructor = v8::Local::new(scope, &env.buffer_constructor);
-  let Some(buffer) = buffer_constructor.new_instance(scope, &[ab.into()])
-  else {
+  let create_buffer = v8::Local::new(scope, &env.create_buffer);
+  let recv = v8::null(scope).into();
+  let Some(buffer) = create_buffer.call(scope, recv, &[ab.into()]) else {
     return napi_generic_failure;
   };
 
@@ -341,9 +341,9 @@ fn napi_create_external_buffer<'s>(
   v8::callback_scope!(unsafe scope, env.context());
   let ab = v8::ArrayBuffer::with_backing_store(scope, &store.make_shared());
 
-  let buffer_constructor = v8::Local::new(scope, &env.buffer_constructor);
-  let Some(buffer) = buffer_constructor.new_instance(scope, &[ab.into()])
-  else {
+  let create_buffer = v8::Local::new(scope, &env.create_buffer);
+  let recv = v8::null(scope).into();
+  let Some(buffer) = create_buffer.call(scope, recv, &[ab.into()]) else {
     return napi_generic_failure;
   };
 
@@ -368,9 +368,9 @@ fn napi_create_buffer_copy<'s>(
 
   let ab = v8::ArrayBuffer::new(scope, length);
 
-  let buffer_constructor = v8::Local::new(scope, &env.buffer_constructor);
-  let Some(buffer) = buffer_constructor.new_instance(scope, &[ab.into()])
-  else {
+  let create_buffer = v8::Local::new(scope, &env.create_buffer);
+  let recv = v8::null(scope).into();
+  let Some(buffer) = create_buffer.call(scope, recv, &[ab.into()]) else {
     return napi_generic_failure;
   };
 

filename: ext/node/polyfills/01_require.js
patch: @@ -1127,7 +1127,7 @@ Module._extensions[".node"] = function (module, filename) {
   module.exports = op_napi_open(
     filename,
     globalThis,
-    buffer.Buffer,
+    buffer.Buffer.from,
     reportError,
   );
 };

filename: tests/napi/init_test.js
patch: @@ -10,7 +10,7 @@ Deno.test("ctr initialization (napi_module_register)", {
   ignore: Deno.build.os == "windows",
 }, function () {
   const path = new URL(`./module.${libSuffix}`, import.meta.url).pathname;
-  const obj = ops.op_napi_open(path, {}, Buffer, reportError);
+  const obj = ops.op_napi_open(path, {}, Buffer.from, reportError);
   assert(obj != null);
   assert(typeof obj === "object");
 });
@@ -19,7 +19,7 @@ Deno.test("ctr initialization by multiple threads (napi_module_register)", {
   ignore: Deno.build.os == "windows",
 }, async function () {
   const path = new URL(`./module.${libSuffix}`, import.meta.url).pathname;
-  const obj = ops.op_napi_open(path, {}, Buffer, reportError);
+  const obj = ops.op_napi_open(path, {}, Buffer.from, reportError);
   const common = import.meta.resolve("./common.js");
   assert(obj != null);
   assert(typeof obj === "object");
@@ -29,9 +29,9 @@ Deno.test("ctr initialization by multiple threads (napi_module_register)", {
     import { Buffer } from "node:buffer";
     import { parentPort } from "node:worker_threads";
     import { assert } from "${common}";
-    
+
     const ops = Deno[Deno.internal].core.ops;
-    const obj = ops.op_napi_open("${path}", {}, Buffer, reportError);
+    const obj = ops.op_napi_open("${path}", {}, Buffer.from, reportError);
     assert(obj != null);
     assert(typeof obj === "object");
     parentPort.postMessage("ok");