|
1 | | -import { Writable } from 'stream'; |
2 | | -import { SSRExecutorManager, SSRExecutorManagerOptions } from '../SSRExecutorManager.js'; |
| 1 | +import { SSRExecutorManager } from '../SSRExecutorManager.js'; |
| 2 | +import { AbortError } from '../../utils.js'; |
3 | 3 |
|
4 | 4 | /** |
5 | 5 | * Streaming executor manager for NodeJS environment. |
6 | 6 | */ |
7 | 7 | export class PipeableSSRExecutorManager extends SSRExecutorManager { |
8 | | - /** |
9 | | - * The stream that includes both React rendering chunks and executor hydration chunks. |
10 | | - */ |
11 | | - readonly stream: NodeJS.WritableStream; |
| 8 | + private _isPiped = false; |
| 9 | + private _pendingChunk = ''; |
12 | 10 |
|
13 | 11 | /** |
14 | | - * Creates a new {@link PipeableSSRExecutorManager} instance. |
| 12 | + * Outputs executor hydration chunks into the provided |
| 13 | + * [Writable Node.js Stream](https://nodejs.org/api/stream.html#writable-streams). |
15 | 14 | * |
16 | | - * @param stream The output stream to which both React chunks and executor hydration chunks are written. |
17 | | - * @param options Additional options. |
| 15 | + * @param stream The stream to write to. |
| 16 | + * @template Stream The stream to write to. |
18 | 17 | */ |
19 | | - constructor(stream: NodeJS.WritableStream, options?: SSRExecutorManagerOptions) { |
20 | | - super(options); |
21 | | - |
22 | | - this.stream = new Writable({ |
23 | | - write: (chunk, encoding, callback) => { |
24 | | - stream.write(chunk, encoding, error => { |
25 | | - if (error) { |
26 | | - callback(error); |
27 | | - return; |
28 | | - } |
29 | | - |
30 | | - if (!chunk.toString().endsWith('</script>')) { |
31 | | - callback(); |
32 | | - return; |
33 | | - } |
34 | | - |
35 | | - const hydrationChunk = this.nextHydrationChunk(); |
36 | | - |
37 | | - if (hydrationChunk !== '') { |
38 | | - stream.write(hydrationChunk, callback); |
39 | | - return; |
40 | | - } |
41 | | - |
42 | | - callback(); |
43 | | - }); |
44 | | - }, |
45 | | - |
46 | | - final: callback => { |
47 | | - stream.end(callback); |
48 | | - }, |
49 | | - }); |
| 18 | + pipe<Stream extends NodeJS.WritableStream>(stream: Stream): Stream { |
| 19 | + if (this._isPiped) { |
| 20 | + throw new Error('React Executor currently only supports piping to one stream'); |
| 21 | + } |
| 22 | + |
| 23 | + this._isPiped = true; |
| 24 | + |
| 25 | + const writeChunk = () => { |
| 26 | + if ((this._pendingChunk ||= this.nextHydrationChunk()) && stream.write(this._pendingChunk)) { |
| 27 | + console.log('EXECUTOR ' + this._pendingChunk + '\n\n'); |
| 28 | + this._pendingChunk = ''; |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + this.subscribe(writeChunk); |
| 33 | + |
| 34 | + stream.on('drain', writeChunk); |
| 35 | + |
| 36 | + stream.on('error', () => this.abort(AbortError('The stream errored while writing data'))); |
| 37 | + |
| 38 | + stream.on('cancel', () => this.abort(AbortError('The stream closed early'))); |
| 39 | + |
| 40 | + writeChunk(); |
| 41 | + |
| 42 | + return stream; |
50 | 43 | } |
51 | 44 | } |
0 commit comments