88 Uint8Array,
99} = primordials;
1010
11+ const { TextEncoder } = require('internal/encoding');
12+
1113const {
1214 ReadableStream,
1315 isReadableStream,
@@ -54,6 +56,7 @@ const {
5456const {
5557 createDeferredPromise,
5658 kEmptyObject,
59+ normalizeEncoding,
5760} = require('internal/util');
5861
5962const {
@@ -73,6 +76,8 @@ const finished = require('internal/streams/end-of-stream');
7376
7477const { UV_EOF } = internalBinding('uv');
7578
79+ const encoder = new TextEncoder();
80+
7681/**
7782 * @typedef {import('../../stream').Writable} Writable
7883 * @typedef {import('../../stream').Readable} Readable
@@ -255,11 +260,17 @@ function newStreamWritableFromWritableStream(writableStream, options = kEmptyObj
255260
256261 write(chunk, encoding, callback) {
257262 if (typeof chunk === 'string' && decodeStrings && !objectMode) {
258- chunk = Buffer.from(chunk, encoding);
259- chunk = new Uint8Array(
260- chunk.buffer,
261- chunk.byteOffset,
262- chunk.byteLength);
263+ const enc = normalizeEncoding(encoding);
264+
265+ if (enc === 'utf8') {
266+ chunk = encoder.encode(chunk);
267+ } else {
268+ chunk = Buffer.from(chunk, encoding);
269+ chunk = new Uint8Array(
270+ chunk.buffer,
271+ chunk.byteOffset,
272+ chunk.byteLength);
273+ }
263274 }
264275
265276 function done(error) {
@@ -674,11 +685,17 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options =
674685
675686 write(chunk, encoding, callback) {
676687 if (typeof chunk === 'string' && decodeStrings && !objectMode) {
677- chunk = Buffer.from(chunk, encoding);
678- chunk = new Uint8Array(
679- chunk.buffer,
680- chunk.byteOffset,
681- chunk.byteLength);
688+ const enc = normalizeEncoding(encoding);
689+
690+ if (enc === 'utf8') {
691+ chunk = encoder.encode(chunk);
692+ } else {
693+ chunk = Buffer.from(chunk, encoding);
694+ chunk = new Uint8Array(
695+ chunk.buffer,
696+ chunk.byteOffset,
697+ chunk.byteLength);
698+ }
682699 }
683700
684701 function done(error) {
0 commit comments