@@ -42,6 +42,8 @@ const EE = require('events');
42
42
const { Stream, prependListener } = require ( 'internal/streams/legacy' ) ;
43
43
const { Buffer } = require ( 'buffer' ) ;
44
44
45
+ const { TextEncoder } = require ( 'internal/encoding' ) ;
46
+
45
47
const {
46
48
addAbortSignal,
47
49
} = require ( 'internal/streams/add-abort-signal' ) ;
@@ -73,6 +75,9 @@ const kPaused = Symbol('kPaused');
73
75
74
76
const { StringDecoder } = require ( 'string_decoder' ) ;
75
77
const from = require ( 'internal/streams/from' ) ;
78
+ const { normalizeEncoding } = require ( 'internal/util' ) ;
79
+ const { FastBuffer } = require ( 'internal/buffer' ) ;
80
+ const encoder = new TextEncoder ( ) ;
76
81
77
82
ObjectSetPrototypeOf ( Readable . prototype , Stream . prototype ) ;
78
83
ObjectSetPrototypeOf ( Readable , Stream ) ;
@@ -251,9 +256,17 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
251
256
if ( addToFront && state . encoding ) {
252
257
// When unshifting, if state.encoding is set, we have to save
253
258
// the string in the BufferList with the state encoding.
259
+
254
260
chunk = Buffer . from ( chunk , encoding ) . toString ( state . encoding ) ;
255
261
} else {
256
- chunk = Buffer . from ( chunk , encoding ) ;
262
+ const enc = normalizeEncoding ( encoding ) ;
263
+
264
+ if ( enc === 'utf8' ) {
265
+ const uint8 = encoder . encode ( chunk ) ;
266
+ chunk = new FastBuffer ( uint8 , 0 , uint8 . length ) ;
267
+ } else {
268
+ chunk = Buffer . from ( chunk , encoding ) ;
269
+ }
257
270
encoding = '' ;
258
271
}
259
272
}
0 commit comments