@@ -32,7 +32,10 @@ const {
3232 Promise,
3333 SafeSet,
3434 SymbolAsyncIterator,
35- Symbol
35+ Symbol,
36+ TypedArrayPrototypeGetBuffer,
37+ TypedArrayPrototypeGetByteOffset,
38+ TypedArrayPrototypeGetByteLength,
3639} = primordials ;
3740
3841module . exports = Readable ;
@@ -42,6 +45,8 @@ const EE = require('events');
4245const { Stream, prependListener } = require ( 'internal/streams/legacy' ) ;
4346const { Buffer } = require ( 'buffer' ) ;
4447
48+ const { TextEncoder } = require ( 'internal/encoding' ) ;
49+
4550const {
4651 addAbortSignal,
4752} = require ( 'internal/streams/add-abort-signal' ) ;
@@ -73,6 +78,9 @@ const kPaused = Symbol('kPaused');
7378
7479const { StringDecoder } = require ( 'string_decoder' ) ;
7580const from = require ( 'internal/streams/from' ) ;
81+ const { normalizeEncoding } = require ( 'internal/util' ) ;
82+ const { FastBuffer } = require ( 'internal/buffer' ) ;
83+ const encoder = new TextEncoder ( ) ;
7684
7785ObjectSetPrototypeOf ( Readable . prototype , Stream . prototype ) ;
7886ObjectSetPrototypeOf ( Readable , Stream ) ;
@@ -251,9 +259,21 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
251259 if ( addToFront && state . encoding ) {
252260 // When unshifting, if state.encoding is set, we have to save
253261 // the string in the BufferList with the state encoding.
262+
254263 chunk = Buffer . from ( chunk , encoding ) . toString ( state . encoding ) ;
255264 } else {
256- chunk = Buffer . from ( chunk , encoding ) ;
265+ const enc = normalizeEncoding ( encoding ) ;
266+
267+ if ( enc === 'utf8' ) {
268+ const buf = encoder . encode ( chunk ) ;
269+ chunk = new FastBuffer (
270+ TypedArrayPrototypeGetBuffer ( buf ) ,
271+ TypedArrayPrototypeGetByteOffset ( buf ) ,
272+ TypedArrayPrototypeGetByteLength ( buf ) ,
273+ ) ;
274+ } else {
275+ chunk = Buffer . from ( chunk , enc ) ;
276+ }
257277 encoding = '' ;
258278 }
259279 }
0 commit comments