Commit 3193305572b for nodejs

commit 3193305572b088fdddffc2d8be0dbed1e538d6c3
Author: James M Snell <jasnell@gmail.com>
Date:   Sun Nov 15 18:02:35 2026 +0000

    stream: deduplicate string chunk encoding in web stream adapters

    The Writable and Duplex adapters created from web streams duplicated
    the logic that encodes string chunks as a Uint8Array before writing
    them to the WritableStream. Move it into a shared encodeStringChunk()
    helper.

    Assisted-by: Claude
    Signed-off-by: James M Snell <jasnell@gmail.com>
    PR-URL: https://github.com/nodejs/node/pull/67495
    Reviewed-By: Guilherme Araújo <arauujogui@gmail.com>
    Reviewed-By: Filip Skokan <panva.ip@gmail.com>
    Reviewed-By: Robert Nagy <ronagy@icloud.com>
    Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>

diff --git a/lib/internal/webstreams/adapters.js b/lib/internal/webstreams/adapters.js
index fc5ada12be3..443bea0386c 100644
--- a/lib/internal/webstreams/adapters.js
+++ b/lib/internal/webstreams/adapters.js
@@ -167,6 +167,24 @@ const noop = () => {};
 // `failed`/`failure` latch a write that could not proceed. The callback
 // runs in the async context of the Node.js write that set it, as the
 // reaction to writer.write() did (see settle()).
+/**
+ * Encodes a string chunk written to a Node.js Writable as a Uint8Array for a
+ * WritableStream.
+ * @param {string} chunk
+ * @param {string} encoding
+ * @returns {Uint8Array}
+ */
+function encodeStringChunk(chunk, encoding) {
+  if (normalizeEncoding(encoding) === 'utf8')
+    return encoder.encode(chunk);
+  const buffer = Buffer.from(chunk, encoding);
+  return new Uint8Array(
+    TypedArrayPrototypeGetBuffer(buffer),
+    TypedArrayPrototypeGetByteOffset(buffer),
+    TypedArrayPrototypeGetByteLength(buffer),
+  );
+}
+
 function createWriteTracker(getDestination) {
   return {
     promise: null,
@@ -493,20 +511,8 @@ function newStreamWritableFromWritableStream(writableStream, options = kEmptyObj
     },

     write(chunk, encoding, callback) {
-      if (typeof chunk === 'string' && decodeStrings && !objectMode) {
-        const enc = normalizeEncoding(encoding);
-
-        if (enc === 'utf8') {
-          chunk = encoder.encode(chunk);
-        } else {
-          chunk = Buffer.from(chunk, encoding);
-          chunk = new Uint8Array(
-            TypedArrayPrototypeGetBuffer(chunk),
-            TypedArrayPrototypeGetByteOffset(chunk),
-            TypedArrayPrototypeGetByteLength(chunk),
-          );
-        }
-      }
+      if (typeof chunk === 'string' && decodeStrings && !objectMode)
+        chunk = encodeStringChunk(chunk, encoding);

       writeTracker.callback = callback;
       dispatchWrite(writer, chunk, writeTracker);
@@ -948,20 +954,8 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options =
     },

     write(chunk, encoding, callback) {
-      if (typeof chunk === 'string' && decodeStrings && !objectMode) {
-        const enc = normalizeEncoding(encoding);
-
-        if (enc === 'utf8') {
-          chunk = encoder.encode(chunk);
-        } else {
-          chunk = Buffer.from(chunk, encoding);
-          chunk = new Uint8Array(
-            TypedArrayPrototypeGetBuffer(chunk),
-            TypedArrayPrototypeGetByteOffset(chunk),
-            TypedArrayPrototypeGetByteLength(chunk),
-          );
-        }
-      }
+      if (typeof chunk === 'string' && decodeStrings && !objectMode)
+        chunk = encodeStringChunk(chunk, encoding);

       writeTracker.callback = callback;
       dispatchWrite(writer, chunk, writeTracker);