-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathstream-node.js
More file actions
153 lines (140 loc) · 3.27 KB
/
stream-node.js
File metadata and controls
153 lines (140 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { PassThrough } from 'node:stream';
import { renderToChunks } from './lib/chunked.js';
/**
* @typedef {object} RenderToPipeableStreamOptions
* @property {() => void} [onShellReady]
* @property {() => void} [onAllReady]
* @property {(error) => void} [onError]
*/
/**
* @typedef {object} PipeableStream
* @property {() => void} abort
* @property {(writable: import('stream').Writable) => void} pipe
*/
/**
* @param {import('preact').VNode} vnode
* @param {RenderToPipeableStreamOptions} options
* @param {any} [context]
* @returns {PipeableStream}
*/
export function renderToPipeableStream(vnode, options, context) {
const encoder = new TextEncoder('utf-8');
const controller = new AbortController();
const stream = new PassThrough();
let waitingForDrain = null;
let aborted = false;
let shellReadyCalled = false;
let allReadyCalled = false;
let errored = false;
let shellReadyScheduled = false;
stream.on('error', () => {});
function callOnShellReady() {
if (shellReadyCalled || errored) return;
shellReadyCalled = true;
options.onShellReady && options.onShellReady();
}
function callOnAllReady() {
if (allReadyCalled || errored) return;
allReadyCalled = true;
options.onAllReady && options.onAllReady();
}
function callOnError(error) {
if (errored) return;
errored = true;
if (options.onError) {
options.onError(error);
} else {
throw error;
}
}
function scheduleOnShellReady() {
if (shellReadyCalled || shellReadyScheduled || errored) return;
shellReadyScheduled = true;
Promise.resolve().then(() => {
shellReadyScheduled = false;
callOnShellReady();
});
}
/**
* @returns {Promise<void>}
*/
function waitForDrain() {
if (waitingForDrain) return waitingForDrain;
waitingForDrain = new Promise((resolve, reject) => {
const cleanup = () => {
stream.off('drain', onDrain);
stream.off('close', onClose);
stream.off('error', onError);
waitingForDrain = null;
};
const onDrain = () => {
cleanup();
resolve();
};
const onClose = () => {
cleanup();
resolve();
};
const onError = (error) => {
cleanup();
reject(error);
};
stream.on('drain', onDrain);
stream.on('close', onClose);
stream.on('error', onError);
});
return waitingForDrain;
}
Promise.resolve()
.then(() =>
renderToChunks(vnode, {
context,
abortSignal: controller.signal,
async onWrite(s) {
scheduleOnShellReady();
if (stream.destroyed || stream.writableEnded) return;
if (!stream.write(encoder.encode(s))) {
await waitForDrain();
}
}
})
)
.then(() => {
callOnAllReady();
stream.end();
})
.catch((error) => {
stream.destroy();
callOnError(error);
});
return {
/**
* @param {unknown} [reason]
*/
abort(
reason = new Error(
'The render was aborted by the server without a reason.'
)
) {
// Remix/React-Router will always call abort after a timeout, even on success
if (
aborted ||
stream.closed ||
stream.destroyed ||
stream.writableEnded
) {
return;
}
aborted = true;
controller.abort(reason);
stream.destroy(reason);
callOnError(reason);
},
/**
* @param {import("stream").Writable} writable
*/
pipe(writable) {
stream.pipe(writable, { end: true });
}
};
}