Skip to content

Commit 5ad0b32

Browse files
jasnelladuh95
authored andcommitted
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: #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>
1 parent a204908 commit 5ad0b32

1 file changed

Lines changed: 22 additions & 28 deletions

File tree

‎lib/internal/webstreams/adapters.js‎

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,24 @@ const noop = () => {};
167167
// `failed`/`failure` latch a write that could not proceed. The callback
168168
// runs in the async context of the Node.js write that set it, as the
169169
// reaction to writer.write() did (see settle()).
170+
/**
171+
* Encodes a string chunk written to a Node.js Writable as a Uint8Array for a
172+
* WritableStream.
173+
* @param {string} chunk
174+
* @param {string} encoding
175+
* @returns {Uint8Array}
176+
*/
177+
function encodeStringChunk(chunk, encoding) {
178+
if (normalizeEncoding(encoding) === 'utf8')
179+
return encoder.encode(chunk);
180+
const buffer = Buffer.from(chunk, encoding);
181+
return new Uint8Array(
182+
TypedArrayPrototypeGetBuffer(buffer),
183+
TypedArrayPrototypeGetByteOffset(buffer),
184+
TypedArrayPrototypeGetByteLength(buffer),
185+
);
186+
}
187+
170188
function createWriteTracker(getDestination) {
171189
return {
172190
promise: null,
@@ -493,20 +511,8 @@ function newStreamWritableFromWritableStream(writableStream, options = kEmptyObj
493511
},
494512

495513
write(chunk, encoding, callback) {
496-
if (typeof chunk === 'string' && decodeStrings && !objectMode) {
497-
const enc = normalizeEncoding(encoding);
498-
499-
if (enc === 'utf8') {
500-
chunk = encoder.encode(chunk);
501-
} else {
502-
chunk = Buffer.from(chunk, encoding);
503-
chunk = new Uint8Array(
504-
TypedArrayPrototypeGetBuffer(chunk),
505-
TypedArrayPrototypeGetByteOffset(chunk),
506-
TypedArrayPrototypeGetByteLength(chunk),
507-
);
508-
}
509-
}
514+
if (typeof chunk === 'string' && decodeStrings && !objectMode)
515+
chunk = encodeStringChunk(chunk, encoding);
510516

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

950956
write(chunk, encoding, callback) {
951-
if (typeof chunk === 'string' && decodeStrings && !objectMode) {
952-
const enc = normalizeEncoding(encoding);
953-
954-
if (enc === 'utf8') {
955-
chunk = encoder.encode(chunk);
956-
} else {
957-
chunk = Buffer.from(chunk, encoding);
958-
chunk = new Uint8Array(
959-
TypedArrayPrototypeGetBuffer(chunk),
960-
TypedArrayPrototypeGetByteOffset(chunk),
961-
TypedArrayPrototypeGetByteLength(chunk),
962-
);
963-
}
964-
}
957+
if (typeof chunk === 'string' && decodeStrings && !objectMode)
958+
chunk = encodeStringChunk(chunk, encoding);
965959

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

0 commit comments

Comments
 (0)