mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
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>
This commit is contained in:
1 parent
67798df884
commit
3193305572
1 file changed
+22
-28
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user