MCPcopy Index your code
hub / github.com/resend/react-email / readStream

Function readStream

packages/render/src/node/read-stream.ts:7–58  ·  view source on GitHub ↗
(
  stream: PipeableStream | ReactDOMServerReadableStream,
)

Source from the content-addressed store, hash-verified

5} from 'react-dom/server.browser';
6
7export const readStream = async (
8 stream: PipeableStream | ReactDOMServerReadableStream,
9) => {
10 let result = '';
11 // Create a single TextDecoder instance to handle streaming properly
12 // This fixes issues with multi-byte characters (e.g., CJK) being split across chunks
13 const decoder = new TextDecoder('utf-8');
14
15 if ('pipeTo' in stream) {
16 // means it's a readable stream
17 const writableStream = new WritableStream({
18 write(chunk: BufferSource) {
19 // Use stream: true to handle multi-byte characters split across chunks
20 result += decoder.decode(chunk, { stream: true });
21 },
22 close() {
23 // Flush any remaining bytes
24 result += decoder.decode();
25 },
26 });
27 await stream.pipeTo(writableStream);
28 } else {
29 const writable = new Writable({
30 write(chunk: BufferSource, _encoding, callback) {
31 // Use stream: true to handle multi-byte characters split across chunks
32 result += decoder.decode(chunk, { stream: true });
33
34 callback();
35 },
36 final(callback) {
37 // Flush any remaining bytes
38 result += decoder.decode();
39 callback();
40 },
41 });
42 await new Promise<void>((resolve, reject) => {
43 writable.on('pipe', (source) => {
44 source.on('error', (err: Error) => {
45 writable.destroy(err);
46 });
47 });
48 writable.on('error', reject);
49 writable.on('close', () => {
50 resolve();
51 });
52
53 stream.pipe(writable);
54 });
55 }
56
57 return result;
58};

Callers 3

renderFunction · 0.90
onAllReadyFunction · 0.90

Calls 2

pipeToMethod · 0.80
onMethod · 0.80

Tested by

no test coverage detected