|
8 | 8 | Uint8Array, |
9 | 9 | } = primordials; |
10 | 10 |
|
| 11 | +const { TextEncoder } = require('internal/encoding'); |
| 12 | + |
11 | 13 | const { |
12 | 14 | ReadableStream, |
13 | 15 | isReadableStream, |
@@ -54,6 +56,7 @@ const { |
54 | 56 | const { |
55 | 57 | createDeferredPromise, |
56 | 58 | kEmptyObject, |
| 59 | + normalizeEncoding, |
57 | 60 | } = require('internal/util'); |
58 | 61 |
|
59 | 62 | const { |
@@ -73,6 +76,8 @@ const finished = require('internal/streams/end-of-stream'); |
73 | 76 |
|
74 | 77 | const { UV_EOF } = internalBinding('uv'); |
75 | 78 |
|
| 79 | +const encoder = new TextEncoder(); |
| 80 | + |
76 | 81 | /** |
77 | 82 | * @typedef {import('../../stream').Writable} Writable |
78 | 83 | * @typedef {import('../../stream').Readable} Readable |
@@ -255,11 +260,17 @@ function newStreamWritableFromWritableStream(writableStream, options = kEmptyObj |
255 | 260 |
|
256 | 261 | write(chunk, encoding, callback) { |
257 | 262 | 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 | + } |
263 | 274 | } |
264 | 275 |
|
265 | 276 | function done(error) { |
@@ -674,11 +685,17 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options = |
674 | 685 |
|
675 | 686 | write(chunk, encoding, callback) { |
676 | 687 | 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 | + } |
682 | 699 | } |
683 | 700 |
|
684 | 701 | function done(error) { |
|