@timber-js/app 0.2.0-alpha.219 → 0.2.0-alpha.220
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_chunks/chains-CDK1AWZQ.js +1 -0
- package/dist/_chunks/{chains-YKZ8WpIW.js → chains-e_Buf9vg.js} +2 -7
- package/dist/_chunks/{chains-YKZ8WpIW.js.map → chains-e_Buf9vg.js.map} +1 -1
- package/dist/_chunks/{cli-check-D2WtbNqo.js → cli-check-DDwCkeDY.js} +2 -2
- package/dist/_chunks/{cli-check-D2WtbNqo.js.map → cli-check-DDwCkeDY.js.map} +1 -1
- package/dist/_chunks/cli-context-BJQB6rtF.js +1 -0
- package/dist/_chunks/{cli-context-By7k2xVo.js → cli-context-_mPttGGY.js} +3 -3
- package/dist/_chunks/{cli-context-By7k2xVo.js.map → cli-context-_mPttGGY.js.map} +1 -1
- package/dist/_chunks/{cli-schema-sync-CySLKrO3.js → cli-schema-sync-BdIebmk3.js} +3 -12
- package/dist/_chunks/{cli-schema-sync-CySLKrO3.js.map → cli-schema-sync-BdIebmk3.js.map} +1 -1
- package/dist/_chunks/cli-schema-sync-CLNfXQDK.js +1 -0
- package/dist/_chunks/dev-server-DkytOEG2.js +1 -0
- package/dist/_chunks/{dev-server-B8zsT-ee.js → dev-server-DnODXuNM.js} +31 -14
- package/dist/_chunks/{dev-server-B8zsT-ee.js.map → dev-server-DnODXuNM.js.map} +1 -1
- package/dist/_chunks/flight-source-node-0dE-t3kq.js +519 -0
- package/dist/_chunks/flight-source-node-0dE-t3kq.js.map +1 -0
- package/dist/_chunks/{live-graph-BhtrZpRd.js → live-graph-tSWrv8eH.js} +2 -2
- package/dist/_chunks/{live-graph-BhtrZpRd.js.map → live-graph-tSWrv8eH.js.map} +1 -1
- package/dist/_chunks/node-response-Dibufw_b.js +187 -0
- package/dist/_chunks/node-response-Dibufw_b.js.map +1 -0
- package/dist/_chunks/{plugin-context-BPBPc3lL.js → plugin-context-DxMMvjUt.js} +2 -18
- package/dist/_chunks/{plugin-context-BPBPc3lL.js.map → plugin-context-DxMMvjUt.js.map} +1 -1
- package/dist/_chunks/plugin-context-uDHFqHHJ.js +1 -0
- package/dist/_chunks/{walkers-Ber9bamD.js → walkers-qu8TpiLh.js} +2 -2
- package/dist/_chunks/{walkers-Ber9bamD.js.map → walkers-qu8TpiLh.js.map} +1 -1
- package/dist/adapters/nitro-preview.d.ts.map +1 -1
- package/dist/adapters/nitro.js +40 -1
- package/dist/adapters/nitro.js.map +1 -1
- package/dist/analyze/crawl-entry.js +2 -2
- package/dist/analyze/graph-command.js +3 -3
- package/dist/analyze/graph-command.js.map +1 -1
- package/dist/cli.js +5 -5
- package/dist/cli.js.map +1 -1
- package/dist/client/browser-entry/form-state.d.ts.map +1 -1
- package/dist/client/browser-entry/index.d.ts +1 -1
- package/dist/client/browser-entry/rsc-stream.d.ts +4 -1
- package/dist/client/browser-entry/rsc-stream.d.ts.map +1 -1
- package/dist/client/react-root.d.ts +6 -0
- package/dist/client/react-root.d.ts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +36 -5
- package/dist/index.js.map +1 -1
- package/dist/plugins/encryption-runtime.d.ts +13 -0
- package/dist/plugins/encryption-runtime.d.ts.map +1 -0
- package/dist/plugins/entries.d.ts +1 -0
- package/dist/plugins/entries.d.ts.map +1 -1
- package/dist/plugins/server-bundle.d.ts +1 -1
- package/dist/plugins/server-bundle.d.ts.map +1 -1
- package/dist/routing/index.js +1 -1
- package/dist/rsc-runtime/encryption-runtime.d.ts +25 -0
- package/dist/rsc-runtime/encryption-runtime.d.ts.map +1 -0
- package/dist/rsc-runtime/render-flight.d.ts +14 -0
- package/dist/rsc-runtime/render-flight.d.ts.map +1 -0
- package/dist/rsc-runtime/rsc.d.ts +6 -2
- package/dist/rsc-runtime/rsc.d.ts.map +1 -1
- package/dist/rsc-runtime/ssr.d.ts +6 -12
- package/dist/rsc-runtime/ssr.d.ts.map +1 -1
- package/dist/server/action-handler.d.ts.map +1 -1
- package/dist/server/compress.d.ts +2 -0
- package/dist/server/compress.d.ts.map +1 -1
- package/dist/server/deny-renderer.d.ts +2 -1
- package/dist/server/deny-renderer.d.ts.map +1 -1
- package/dist/server/flight-injector-core.d.ts +125 -0
- package/dist/server/flight-injector-core.d.ts.map +1 -0
- package/dist/server/flight-scripts.d.ts +9 -0
- package/dist/server/flight-scripts.d.ts.map +1 -1
- package/dist/server/flight-source-node.d.ts +105 -0
- package/dist/server/flight-source-node.d.ts.map +1 -0
- package/dist/server/flight-source.d.ts +136 -0
- package/dist/server/flight-source.d.ts.map +1 -0
- package/dist/server/head-response.d.ts.map +1 -1
- package/dist/server/html-injector-core.d.ts +3 -91
- package/dist/server/html-injector-core.d.ts.map +1 -1
- package/dist/server/html-injectors.d.ts +5 -17
- package/dist/server/html-injectors.d.ts.map +1 -1
- package/dist/server/internal.js +78 -138
- package/dist/server/internal.js.map +1 -1
- package/dist/server/node-body-response.d.ts +55 -0
- package/dist/server/node-body-response.d.ts.map +1 -0
- package/dist/server/node-response.d.ts +62 -0
- package/dist/server/node-response.d.ts.map +1 -0
- package/dist/server/node-stream-transforms.d.ts +24 -21
- package/dist/server/node-stream-transforms.d.ts.map +1 -1
- package/dist/server/node-streams-platform.d.ts +21 -0
- package/dist/server/node-streams-platform.d.ts.map +1 -0
- package/dist/server/pipeline-helpers.d.ts.map +1 -1
- package/dist/server/pipeline.d.ts.map +1 -1
- package/dist/server/prebuilt-runtime.d.ts +2 -3
- package/dist/server/prebuilt-runtime.d.ts.map +1 -1
- package/dist/server/rsc-entry/index.d.ts.map +1 -1
- package/dist/server/rsc-entry/render-route.d.ts +4 -1
- package/dist/server/rsc-entry/render-route.d.ts.map +1 -1
- package/dist/server/rsc-entry/rsc-payload.d.ts +9 -6
- package/dist/server/rsc-entry/rsc-payload.d.ts.map +1 -1
- package/dist/server/rsc-entry/rsc-stream.d.ts +9 -4
- package/dist/server/rsc-entry/rsc-stream.d.ts.map +1 -1
- package/dist/server/rsc-entry/ssr-bridge.d.ts +2 -1
- package/dist/server/rsc-entry/ssr-bridge.d.ts.map +1 -1
- package/dist/server/rsc-entry/ssr-renderer.d.ts +2 -1
- package/dist/server/rsc-entry/ssr-renderer.d.ts.map +1 -1
- package/dist/server/ssr-bridge-types.d.ts +9 -4
- package/dist/server/ssr-bridge-types.d.ts.map +1 -1
- package/dist/server/ssr-entry.d.ts +8 -5
- package/dist/server/ssr-entry.d.ts.map +1 -1
- package/dist/server/ssr-render.d.ts +7 -16
- package/dist/server/ssr-render.d.ts.map +1 -1
- package/dist/server/stream-utils.d.ts +44 -10
- package/dist/server/stream-utils.d.ts.map +1 -1
- package/dist/shared/flight-base64.d.ts +13 -0
- package/dist/shared/flight-base64.d.ts.map +1 -0
- package/dist/shared/flight-segment.d.ts +18 -0
- package/dist/shared/flight-segment.d.ts.map +1 -0
- package/dist/shared/form-state-flight.d.ts +0 -4
- package/dist/shared/form-state-flight.d.ts.map +1 -1
- package/docs/learn/12-error-handling.mdx +1 -1
- package/package.json +1 -1
- package/src/adapters/nitro-preview.ts +39 -0
- package/src/client/browser-entry/form-state.ts +3 -2
- package/src/client/browser-entry/index.ts +14 -8
- package/src/client/browser-entry/rsc-stream.ts +14 -14
- package/src/client/react-root.ts +25 -2
- package/src/index.ts +2 -0
- package/src/plugins/encryption-runtime.ts +31 -0
- package/src/plugins/entries.ts +1 -1
- package/src/plugins/server-bundle.ts +17 -2
- package/src/rsc-runtime/encryption-runtime.ts +78 -0
- package/src/rsc-runtime/render-flight.ts +26 -0
- package/src/rsc-runtime/rsc.ts +7 -4
- package/src/rsc-runtime/ssr.ts +10 -48
- package/src/rsc-runtime/vendor-types.d.ts +31 -2
- package/src/server/action-handler.ts +43 -44
- package/src/server/compress.ts +49 -12
- package/src/server/deny-renderer.ts +14 -7
- package/src/server/flight-injector-core.ts +328 -0
- package/src/server/flight-scripts.ts +14 -2
- package/src/server/flight-source-node.ts +430 -0
- package/src/server/flight-source.ts +288 -0
- package/src/server/form-state-flight.ts +4 -4
- package/src/server/head-response.ts +6 -10
- package/src/server/html-injector-core.ts +24 -216
- package/src/server/html-injectors.ts +12 -71
- package/src/server/node-body-response.ts +236 -0
- package/src/server/node-response.ts +93 -0
- package/src/server/node-stream-transforms.ts +70 -53
- package/src/server/node-streams-platform.ts +23 -0
- package/src/server/pipeline-helpers.ts +3 -1
- package/src/server/pipeline.ts +14 -6
- package/src/server/prebuilt/capture-render.ts +5 -5
- package/src/server/prebuilt-runtime.ts +7 -10
- package/src/server/rsc-entry/error-renderer.ts +8 -4
- package/src/server/rsc-entry/index.ts +4 -2
- package/src/server/rsc-entry/render-route.ts +5 -2
- package/src/server/rsc-entry/rsc-payload.ts +52 -73
- package/src/server/rsc-entry/rsc-stream.ts +102 -106
- package/src/server/rsc-entry/ssr-bridge.ts +32 -10
- package/src/server/rsc-entry/ssr-renderer.ts +15 -18
- package/src/server/ssr-bridge-types.ts +10 -4
- package/src/server/ssr-entry.ts +56 -52
- package/src/server/ssr-render.ts +41 -76
- package/src/server/stream-utils.ts +125 -70
- package/src/shared/flight-base64.ts +23 -0
- package/src/shared/flight-segment.ts +35 -0
- package/src/shared/form-state-flight.ts +0 -15
- package/dist/_chunks/rolldown-runtime-D7D4PA-g.js +0 -13
|
@@ -20,18 +20,15 @@
|
|
|
20
20
|
|
|
21
21
|
import {
|
|
22
22
|
BufferAggregator,
|
|
23
|
-
FlightInjectorCore,
|
|
24
|
-
type FlightInjectorOptions,
|
|
25
23
|
createInjectorState,
|
|
26
24
|
createSuffixState,
|
|
27
|
-
encodeUtf8,
|
|
28
25
|
injectorFlush,
|
|
29
26
|
processInjectorChunk,
|
|
30
27
|
processSuffixChunk,
|
|
31
28
|
suffixFlush,
|
|
32
29
|
} from './html-injector-core.ts';
|
|
33
|
-
import {
|
|
34
|
-
import {
|
|
30
|
+
import { FlightInjectorCore, type FlightInjectorOptions } from './flight-injector-core.ts';
|
|
31
|
+
import type { FlightSource } from './flight-source.ts';
|
|
35
32
|
import { createTickCoalescer } from './coalesce-tick.ts';
|
|
36
33
|
|
|
37
34
|
// ─── Buffered Transform ──────────────────────────────────────────────────────
|
|
@@ -191,75 +188,25 @@ export function injectScripts(
|
|
|
191
188
|
// ─── RSC Flight Injection ────────────────────────────────────────────────────
|
|
192
189
|
|
|
193
190
|
/**
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
* Uses a **pull-based** ReadableStream — the consumer (the injection
|
|
197
|
-
* transform) drives reads from the RSC stream on demand. No background
|
|
198
|
-
* reader, no shared mutable arrays, no race conditions. Each RSC chunk
|
|
199
|
-
* becomes `<script>self.__timber_f.push([1,"data"])</script>`. The
|
|
200
|
-
* init script (which creates `__timber_f`) is in `<head>` via
|
|
201
|
-
* `flightInitScript()` — see flight-scripts.ts.
|
|
202
|
-
*/
|
|
203
|
-
export function createInlinedRscStream(
|
|
204
|
-
rscStream: ReadableStream<Uint8Array>,
|
|
205
|
-
renderTimeoutMs?: number,
|
|
206
|
-
nonce?: string
|
|
207
|
-
): ReadableStream<Uint8Array> {
|
|
208
|
-
const rscReader = rscStream.getReader();
|
|
209
|
-
const decoder = new TextDecoder('utf-8', { fatal: true });
|
|
210
|
-
const timeoutMs = resolveRenderTimeoutMs(renderTimeoutMs);
|
|
211
|
-
|
|
212
|
-
return new ReadableStream<Uint8Array>({
|
|
213
|
-
async pull(controller) {
|
|
214
|
-
try {
|
|
215
|
-
const readPromise = rscReader.read();
|
|
216
|
-
const { done, value } = await withTimeout(
|
|
217
|
-
readPromise,
|
|
218
|
-
timeoutMs,
|
|
219
|
-
'RSC stream read timed out'
|
|
220
|
-
);
|
|
221
|
-
if (done) {
|
|
222
|
-
controller.close();
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
if (value) {
|
|
226
|
-
const decoded = decoder.decode(value, { stream: true });
|
|
227
|
-
controller.enqueue(encodeUtf8(flightChunkScript(decoded, nonce)));
|
|
228
|
-
}
|
|
229
|
-
} catch (error) {
|
|
230
|
-
if (error instanceof RenderTimeoutError) {
|
|
231
|
-
rscReader.cancel(error).catch(() => {});
|
|
232
|
-
}
|
|
233
|
-
controller.error(error);
|
|
234
|
-
}
|
|
235
|
-
},
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Merge a raw RSC byte stream into the HTML stream, wrapping each RSC
|
|
191
|
+
* Merge the inline Flight source into the HTML stream, wrapping each Flight
|
|
241
192
|
* chunk in a `<script>` and injecting it between HTML chunks at safe
|
|
242
193
|
* boundaries. Suffix stripping is handled upstream by
|
|
243
194
|
* `createMoveSuffixStream` — this transform only interleaves.
|
|
244
195
|
*
|
|
245
|
-
* The state machine, pending queue, script wrapping
|
|
246
|
-
*
|
|
196
|
+
* The state machine, pending queue, script wrapping, and pull loop all live
|
|
197
|
+
* in `FlightInjectorCore` (`flight-injector-core.ts`). This wrapper only
|
|
247
198
|
* shuffles bytes between the core and the Web `TransformStream` controller.
|
|
248
199
|
*
|
|
249
|
-
* **Input must be the raw RSC stream, not the pre-scripted output of
|
|
250
|
-
* `createInlinedRscStream`.** The core wraps each chunk itself; passing
|
|
251
|
-
* an already-wrapped stream would double-wrap and break hydration.
|
|
252
|
-
*
|
|
253
200
|
* State machine phases: init → streaming → flushing → done; (any) → error.
|
|
254
201
|
*
|
|
255
202
|
* Inspired by Next.js `createFlightDataInjectionTransformStream`. See
|
|
256
203
|
* design/02 §"Scripts at Chunk Boundaries Only".
|
|
257
204
|
*/
|
|
258
205
|
function createFlightInjectionTransform(
|
|
259
|
-
|
|
206
|
+
rscSource: FlightSource,
|
|
260
207
|
options?: FlightInjectorOptions
|
|
261
208
|
): TransformStream<Uint8Array, Uint8Array> {
|
|
262
|
-
const core = new FlightInjectorCore(
|
|
209
|
+
const core = new FlightInjectorCore(rscSource, options);
|
|
263
210
|
// Controller reference — set on first transform() call so the pull
|
|
264
211
|
// loop can drain pending scripts between HTML chunks (after yielding).
|
|
265
212
|
let _controller: TransformStreamDefaultController<Uint8Array> | null = null;
|
|
@@ -326,10 +273,8 @@ function createFlightInjectionTransform(
|
|
|
326
273
|
* 2. moveSuffix strips `</body></html>` and re-emits at end so injected
|
|
327
274
|
* scripts appear before the closing tags.
|
|
328
275
|
*
|
|
329
|
-
* Wrapping happens inside `FlightInjectorCore
|
|
330
|
-
*
|
|
331
|
-
* — do not pre-wrap with `createInlinedRscStream` (that helper still
|
|
332
|
-
* exists for callers that want a standalone scripted stream).
|
|
276
|
+
* Wrapping happens inside `FlightInjectorCore`; the inline Flight source is
|
|
277
|
+
* passed to it as is.
|
|
333
278
|
*
|
|
334
279
|
* The client reads these script tags via `self.__timber_f` and feeds
|
|
335
280
|
* them to `createFromReadableStream` for progressive hydration. Stream
|
|
@@ -338,16 +283,12 @@ function createFlightInjectionTransform(
|
|
|
338
283
|
*/
|
|
339
284
|
export function injectRscPayload(
|
|
340
285
|
htmlStream: ReadableStream<Uint8Array>,
|
|
341
|
-
|
|
286
|
+
rscSource: FlightSource | undefined,
|
|
342
287
|
options?: FlightInjectorOptions
|
|
343
288
|
): ReadableStream<Uint8Array> {
|
|
344
|
-
if (!
|
|
345
|
-
// Pass the raw RSC stream directly to the flight injection transform.
|
|
346
|
-
// The transform's core (FlightInjectorCore) wraps each chunk in a
|
|
347
|
-
// <script> via flightChunkScript itself — do NOT pre-wrap with
|
|
348
|
-
// createInlinedRscStream here, that would double-wrap.
|
|
289
|
+
if (!rscSource) return htmlStream;
|
|
349
290
|
return htmlStream
|
|
350
|
-
.pipeThrough(createFlightInjectionTransform(
|
|
291
|
+
.pipeThrough(createFlightInjectionTransform(rscSource, options))
|
|
351
292
|
.pipeThrough(createMoveSuffixStream());
|
|
352
293
|
}
|
|
353
294
|
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `NodeBodyResponse` — a `Response` whose body is a Node `Readable`, so a
|
|
3
|
+
* response built on Node streams reaches the socket without a Web Streams
|
|
4
|
+
* round trip (LOCAL-1475). Node only: loaded behind `nodeStreamsPlatform`.
|
|
5
|
+
*
|
|
6
|
+
* The status and headers are an ordinary `Response`'s. The body stays a Node
|
|
7
|
+
* stream until one of two things takes it:
|
|
8
|
+
*
|
|
9
|
+
* - **An adapter**, through `_toNodeResponse()` — srvx's protocol (nitro and
|
|
10
|
+
* the dev server send with srvx; nitro-preview's sender speaks it too). The
|
|
11
|
+
* stream is piped straight into `ServerResponse`.
|
|
12
|
+
* - **Web-API code** reading `body`, `text()`, `arrayBuffer()`, `clone()`, …
|
|
13
|
+
* The response then converts its body once, with `Readable.toWeb`, and
|
|
14
|
+
* behaves as a native `Response` from then on. Correct anywhere, so code
|
|
15
|
+
* that does not know about Node bodies keeps working; pipeline code on the
|
|
16
|
+
* hot path goes through `node-response.ts` instead, which never converts.
|
|
17
|
+
*
|
|
18
|
+
* Pattern from srvx's `NodeResponse` (`FastResponse`).
|
|
19
|
+
*
|
|
20
|
+
* Design doc: 02-rendering-pipeline.md §"Response Bodies on Node"
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { Readable, Transform, pipeline, type Duplex } from 'node:stream';
|
|
24
|
+
import { NODE_BODY, type NodeBodyOps } from './node-response.ts';
|
|
25
|
+
import { createStreamTimingRecorder, wrapResponseWithStreamTiming } from './stream-utils.ts';
|
|
26
|
+
|
|
27
|
+
/** Statuses whose responses carry no body (Fetch "null body status"). */
|
|
28
|
+
const NULL_BODY_STATUSES = new Set([101, 103, 204, 205, 304]);
|
|
29
|
+
|
|
30
|
+
function ignoreError(): void {}
|
|
31
|
+
|
|
32
|
+
/** Destroy a body nothing will read, without an unhandled `'error'`. */
|
|
33
|
+
function destroyBody(body: Readable): void {
|
|
34
|
+
if (!body.listeners('error').includes(ignoreError)) body.on('error', ignoreError);
|
|
35
|
+
body.destroy();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class NodeBodyResponse extends Response {
|
|
39
|
+
#body: Readable | null;
|
|
40
|
+
#native: Response | null = null;
|
|
41
|
+
/** An adapter or a pipeline op took the body: it is used, as a read one is. */
|
|
42
|
+
#taken = false;
|
|
43
|
+
|
|
44
|
+
constructor(body: Readable, init?: ResponseInit) {
|
|
45
|
+
super(null, init);
|
|
46
|
+
if (NULL_BODY_STATUSES.has(this.status)) {
|
|
47
|
+
destroyBody(body);
|
|
48
|
+
this.#body = null;
|
|
49
|
+
} else {
|
|
50
|
+
this.#body = body;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Take the Node body; this response no longer has it. */
|
|
55
|
+
#take(): Readable | null {
|
|
56
|
+
const body = this.#body;
|
|
57
|
+
if (body) this.#taken = true;
|
|
58
|
+
this.#body = null;
|
|
59
|
+
return body;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* This response as a native one, converting the Node body once. Throws, as
|
|
64
|
+
* a native Response whose body was read does, if something already took
|
|
65
|
+
* the body (an adapter, a re-wrap), rather than reading as empty.
|
|
66
|
+
*/
|
|
67
|
+
#toNative(): Response {
|
|
68
|
+
if (!this.#native) {
|
|
69
|
+
if (this.#taken) throw new TypeError('Body is unusable: Body has already been read');
|
|
70
|
+
const body = this.#body;
|
|
71
|
+
this.#body = null;
|
|
72
|
+
this.#native = new Response(
|
|
73
|
+
body ? (Readable.toWeb(body) as ReadableStream<Uint8Array>) : null,
|
|
74
|
+
{
|
|
75
|
+
status: this.status,
|
|
76
|
+
statusText: this.statusText,
|
|
77
|
+
headers: this.headers,
|
|
78
|
+
}
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return this.#native;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
get [NODE_BODY](): NodeBodyOps {
|
|
85
|
+
return {
|
|
86
|
+
hasBody: this.#native ? this.#native.body !== null : this.#body !== null,
|
|
87
|
+
rewrap: (init) => {
|
|
88
|
+
if (this.#native) return new Response(this.#native.body, init);
|
|
89
|
+
const body = this.#take();
|
|
90
|
+
return body ? new NodeBodyResponse(body, init) : new Response(null, init);
|
|
91
|
+
},
|
|
92
|
+
discard: () => {
|
|
93
|
+
if (this.#native) {
|
|
94
|
+
this.#native.body?.cancel().catch(() => {});
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const body = this.#take();
|
|
98
|
+
if (body) destroyBody(body);
|
|
99
|
+
},
|
|
100
|
+
through: (stream, init) => {
|
|
101
|
+
const body = this.#native
|
|
102
|
+
? this.#native.body && webToNode(this.#native.body)
|
|
103
|
+
: this.#take();
|
|
104
|
+
return body
|
|
105
|
+
? new NodeBodyResponse(pipeThrough(body, stream), init)
|
|
106
|
+
: new Response(null, init);
|
|
107
|
+
},
|
|
108
|
+
timed: (startTime) => {
|
|
109
|
+
const init = { status: this.status, statusText: this.statusText, headers: this.headers };
|
|
110
|
+
if (this.#native) {
|
|
111
|
+
const native = this.#native.body;
|
|
112
|
+
return new Response(
|
|
113
|
+
native ? wrapResponseWithStreamTiming(native, startTime) : null,
|
|
114
|
+
init
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
const body = this.#take();
|
|
118
|
+
return body ? new NodeBodyResponse(timeBody(body, startTime), init) : this;
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* srvx's protocol for a response with a Node body: srvx pipes `body` into
|
|
125
|
+
* `ServerResponse` with `pipeline()`. Headers as a flat `[name, value, …]`
|
|
126
|
+
* list, as srvx's own `NodeResponse` returns them.
|
|
127
|
+
*/
|
|
128
|
+
_toNodeResponse(): {
|
|
129
|
+
status: number;
|
|
130
|
+
statusText: string;
|
|
131
|
+
headers: string[];
|
|
132
|
+
body: Readable | ReadableStream<Uint8Array> | null;
|
|
133
|
+
} {
|
|
134
|
+
const headers: string[] = [];
|
|
135
|
+
for (const [name, value] of this.headers) headers.push(name, value);
|
|
136
|
+
return {
|
|
137
|
+
status: this.status,
|
|
138
|
+
statusText: this.statusText,
|
|
139
|
+
headers,
|
|
140
|
+
body: this.#native ? this.#native.body : this.#take(),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ─── Web API: convert once, then behave natively ─────────────────────────
|
|
145
|
+
|
|
146
|
+
get body(): Response['body'] {
|
|
147
|
+
return this.#toNative().body;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
get bodyUsed(): boolean {
|
|
151
|
+
return this.#native ? this.#native.bodyUsed : this.#taken;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The other half of srvx's protocol: srvx/bun and srvx/deno send
|
|
156
|
+
* `res._response` for anything with `_toNodeResponse` (`toNativeResponse`),
|
|
157
|
+
* and their runtimes stream Web bodies natively. Converts once.
|
|
158
|
+
*/
|
|
159
|
+
get _response(): Response {
|
|
160
|
+
return this.#toNative();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
arrayBuffer(): Promise<ArrayBuffer> {
|
|
164
|
+
return this.#toNative().arrayBuffer();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
blob(): Promise<Blob> {
|
|
168
|
+
return this.#toNative().blob();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Built from arrayBuffer(): its type satisfies both lib.dom's `bytes()` and
|
|
172
|
+
// the Workers types', which some importers (the website) merge in.
|
|
173
|
+
bytes(): Promise<Uint8Array<ArrayBuffer>> {
|
|
174
|
+
return this.#toNative()
|
|
175
|
+
.arrayBuffer()
|
|
176
|
+
.then((buffer) => new Uint8Array(buffer));
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
formData(): Promise<FormData> {
|
|
180
|
+
return this.#toNative().formData();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
json(): Promise<unknown> {
|
|
184
|
+
return this.#toNative().json();
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
text(): Promise<string> {
|
|
188
|
+
return this.#toNative().text();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
clone(): Response {
|
|
192
|
+
return this.#toNative().clone();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** `body` piped through `stream`; an error or destroy on either end tears down both. */
|
|
197
|
+
function pipeThrough(body: Readable, stream: Duplex): Duplex {
|
|
198
|
+
stream.on('error', ignoreError);
|
|
199
|
+
pipeline(body, stream, ignoreError);
|
|
200
|
+
return stream;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** A Web body already taken out of this response, back as a Node stream. */
|
|
204
|
+
function webToNode(body: ReadableStream<Uint8Array>): Readable {
|
|
205
|
+
return Readable.fromWeb(body as import('node:stream/web').ReadableStream<Uint8Array>);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** A body that reports stream-lifecycle timing ({@link createStreamTimingRecorder}). */
|
|
209
|
+
function timeBody(body: Readable, startTime: number): Readable {
|
|
210
|
+
const timing = createStreamTimingRecorder(startTime);
|
|
211
|
+
const timed = new Transform({
|
|
212
|
+
transform(chunk: Uint8Array, _encoding, callback) {
|
|
213
|
+
timing.chunk(chunk.byteLength);
|
|
214
|
+
callback(null, chunk);
|
|
215
|
+
},
|
|
216
|
+
flush(callback) {
|
|
217
|
+
timing.end(false);
|
|
218
|
+
callback();
|
|
219
|
+
},
|
|
220
|
+
// Reached by the consumer leaving early (a cancellation) and by the
|
|
221
|
+
// producer failing; the producer path records itself first, below, so a
|
|
222
|
+
// render error is not logged as a disconnect — as the Web wrapper logs it.
|
|
223
|
+
destroy(error, callback) {
|
|
224
|
+
timing.end(true);
|
|
225
|
+
body.destroy(error ?? undefined);
|
|
226
|
+
callback(error);
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
timed.on('error', ignoreError);
|
|
230
|
+
body.on('error', (error) => {
|
|
231
|
+
timing.end(false);
|
|
232
|
+
timed.destroy(error);
|
|
233
|
+
});
|
|
234
|
+
body.pipe(timed);
|
|
235
|
+
return timed;
|
|
236
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Responses with a Node body — the platform-neutral half.
|
|
3
|
+
*
|
|
4
|
+
* On Node, SSR's HTML and Flight payload bodies are Node `Readable`s. They
|
|
5
|
+
* travel the pipeline as a `NodeBodyResponse` (`node-body-response.ts`, Node
|
|
6
|
+
* only): a real `Response` whose body stays a Node stream until the adapter
|
|
7
|
+
* pipes it to the socket, so a response is never converted Node → Web → Node
|
|
8
|
+
* (LOCAL-1475). srvx (nitro, the dev server) and nitro-preview's sender take
|
|
9
|
+
* the Node body through `_toNodeResponse()`. Any code that reads the body
|
|
10
|
+
* through the Web API still works: the response then converts once.
|
|
11
|
+
*
|
|
12
|
+
* Pipeline code shared with Workers re-wraps, strips, or times a response
|
|
13
|
+
* through the helpers here, which ask the response itself to do the Node
|
|
14
|
+
* work, so this module never imports `node:stream`. A response is recognised
|
|
15
|
+
* by a `Symbol.for` brand, not `instanceof`: the RSC and SSR environments
|
|
16
|
+
* load separate copies of the class.
|
|
17
|
+
*
|
|
18
|
+
* Design doc: 02-rendering-pipeline.md §"Response Bodies on Node"
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import type { Duplex } from 'node:stream';
|
|
22
|
+
|
|
23
|
+
/** The brand a Node-body response carries, keyed by a registry symbol. */
|
|
24
|
+
export const NODE_BODY = Symbol.for('timber.response.nodeBody');
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* What a Node-body response can do with its body, for code that cannot import
|
|
28
|
+
* the class: platform-neutral modules, and the standalone `_compress.mjs`,
|
|
29
|
+
* which reaches these through the same registry symbol. Each one takes the
|
|
30
|
+
* body: the response it was called on no longer has one.
|
|
31
|
+
*/
|
|
32
|
+
export interface NodeBodyOps {
|
|
33
|
+
/**
|
|
34
|
+
* Whether the response still owns a body. False for a null-body status
|
|
35
|
+
* (204, 205, 304, …) and once the body was taken. Being branded says
|
|
36
|
+
* nothing about this — ask here instead of reading `.body`, which would
|
|
37
|
+
* convert it.
|
|
38
|
+
*/
|
|
39
|
+
readonly hasBody: boolean;
|
|
40
|
+
/** The same body under a new status and headers. */
|
|
41
|
+
rewrap(init: ResponseInit): Response;
|
|
42
|
+
/** Release the body unread (HEAD, a discarded document), aborting its producer. */
|
|
43
|
+
discard(): void;
|
|
44
|
+
/** The same body, reporting stream timing when it ends (detailed Server-Timing). */
|
|
45
|
+
timed(startTime: number): Response;
|
|
46
|
+
/**
|
|
47
|
+
* The body piped through `stream` (gzip), as a Node-body response under
|
|
48
|
+
* `init`. An error or destroy on either end tears down both.
|
|
49
|
+
*/
|
|
50
|
+
through(stream: Duplex, init: ResponseInit): Response;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface NodeBodyBranded {
|
|
54
|
+
[NODE_BODY]: NodeBodyOps;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function isNodeBodyBranded(response: Response): response is Response & NodeBodyBranded {
|
|
58
|
+
return NODE_BODY in response;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The Node-body operations of `response`, or `null` for a Web-body response. */
|
|
62
|
+
export function nodeBodyOps(response: Response): NodeBodyOps | null {
|
|
63
|
+
return isNodeBodyBranded(response) ? response[NODE_BODY] : null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* `response`'s body under a new status and headers. A Node body moves to the
|
|
68
|
+
* new response without converting; a Web body is reused as is.
|
|
69
|
+
*/
|
|
70
|
+
export function withResponseInit(response: Response, init: ResponseInit): Response {
|
|
71
|
+
return nodeBodyOps(response)?.rewrap(init) ?? new Response(response.body, init);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Release a response's body unread. A live producer (an SSR render, a Flight
|
|
76
|
+
* render) is behind it; dropping the reference without this leaks it for the
|
|
77
|
+
* lifetime of the render (design/13-security.md A4).
|
|
78
|
+
*/
|
|
79
|
+
export function discardResponseBody(response: Response): void {
|
|
80
|
+
const ops = nodeBodyOps(response);
|
|
81
|
+
if (ops) {
|
|
82
|
+
ops.discard();
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
// `cancel()` rejects if the stream is already errored and throws
|
|
86
|
+
// synchronously if it is locked — neither is actionable, the body is being
|
|
87
|
+
// discarded either way.
|
|
88
|
+
try {
|
|
89
|
+
response.body?.cancel().catch(() => {});
|
|
90
|
+
} catch {
|
|
91
|
+
/* stream already locked or disturbed */
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -12,19 +12,17 @@
|
|
|
12
12
|
*
|
|
13
13
|
* Architecture:
|
|
14
14
|
* renderToPipeableStream → pipe(errorHandler) → pipe(headInjector)
|
|
15
|
-
* → pipe(flightInjector) →
|
|
15
|
+
* → pipe(flightInjector) → NodeBodyResponse → adapter → socket
|
|
16
16
|
*
|
|
17
|
-
* All chunks stay in
|
|
18
|
-
*
|
|
17
|
+
* All chunks stay in Node streams to the socket: the document is a
|
|
18
|
+
* `NodeBodyResponse` (node-body-response.ts), which the adapter pipes into
|
|
19
|
+
* `ServerResponse` with no Web Streams round trip.
|
|
19
20
|
*/
|
|
20
21
|
|
|
21
22
|
import { PassThrough, Transform, type Readable } from 'node:stream';
|
|
22
|
-
import { pipeline } from 'node:stream/promises';
|
|
23
23
|
|
|
24
24
|
import {
|
|
25
25
|
BufferAggregator,
|
|
26
|
-
FlightInjectorCore,
|
|
27
|
-
type FlightInjectorOptions,
|
|
28
26
|
SUFFIX_BYTES,
|
|
29
27
|
createInjectorState,
|
|
30
28
|
createSuffixState,
|
|
@@ -33,6 +31,8 @@ import {
|
|
|
33
31
|
processSuffixChunk,
|
|
34
32
|
suffixFlush,
|
|
35
33
|
} from './html-injector-core.ts';
|
|
34
|
+
import { FlightInjectorCore, type FlightInjectorOptions } from './flight-injector-core.ts';
|
|
35
|
+
import type { FlightSource } from './flight-source.ts';
|
|
36
36
|
import { logStreamingError } from './logger.ts';
|
|
37
37
|
import { createTickCoalescer } from './coalesce-tick.ts';
|
|
38
38
|
import { isAbortError, noindexScript } from './render-utils.ts';
|
|
@@ -110,16 +110,16 @@ export function createNodeMoveSuffixTransform(): Transform {
|
|
|
110
110
|
return;
|
|
111
111
|
}
|
|
112
112
|
if (result.kind === 'noSuffix') {
|
|
113
|
-
if (result.emitSafe) this.push(
|
|
113
|
+
if (result.emitSafe) this.push(result.emitSafe);
|
|
114
114
|
callback();
|
|
115
115
|
return;
|
|
116
116
|
}
|
|
117
|
-
if (result.before) this.push(
|
|
118
|
-
if (result.after) this.push(
|
|
117
|
+
if (result.before) this.push(result.before);
|
|
118
|
+
if (result.after) this.push(result.after);
|
|
119
119
|
callback();
|
|
120
120
|
},
|
|
121
121
|
flush(callback) {
|
|
122
|
-
this.push(
|
|
122
|
+
this.push(suffixFlush(state));
|
|
123
123
|
callback();
|
|
124
124
|
},
|
|
125
125
|
});
|
|
@@ -155,12 +155,12 @@ export function createNodeHeadInjector(headHtml: string): Transform {
|
|
|
155
155
|
callback(null, chunk);
|
|
156
156
|
return;
|
|
157
157
|
}
|
|
158
|
-
if (result.bytes) this.push(
|
|
158
|
+
if (result.bytes) this.push(result.bytes);
|
|
159
159
|
callback();
|
|
160
160
|
},
|
|
161
161
|
flush(callback) {
|
|
162
162
|
const leftover = injectorFlush(state);
|
|
163
|
-
if (leftover) this.push(
|
|
163
|
+
if (leftover) this.push(leftover);
|
|
164
164
|
callback();
|
|
165
165
|
},
|
|
166
166
|
});
|
|
@@ -182,15 +182,14 @@ export function createNodeHeadInjector(headHtml: string): Transform {
|
|
|
182
182
|
* HTML chunk) or `flush()` — never mid-tag. See design/02
|
|
183
183
|
* §"Scripts at Chunk Boundaries Only".
|
|
184
184
|
*
|
|
185
|
-
* The
|
|
186
|
-
*
|
|
187
|
-
* bridge point between Web Streams and Node.js streams in the pipeline.
|
|
185
|
+
* The inline Flight source is a Node `Readable` on Node (the tee'd branch
|
|
186
|
+
* of the Node Flight render), read by the core through `openFlightReader`.
|
|
188
187
|
*/
|
|
189
188
|
export function createNodeFlightInjector(
|
|
190
|
-
|
|
189
|
+
rscSource: FlightSource | undefined,
|
|
191
190
|
options?: FlightInjectorOptions
|
|
192
191
|
): Transform {
|
|
193
|
-
if (!
|
|
192
|
+
if (!rscSource) {
|
|
194
193
|
return new Transform({
|
|
195
194
|
transform(chunk, _enc, cb) {
|
|
196
195
|
cb(null, chunk);
|
|
@@ -198,10 +197,11 @@ export function createNodeFlightInjector(
|
|
|
198
197
|
});
|
|
199
198
|
}
|
|
200
199
|
|
|
201
|
-
const core = new FlightInjectorCore(
|
|
200
|
+
const core = new FlightInjectorCore(rscSource, options);
|
|
202
201
|
|
|
203
202
|
const drain = (): void => {
|
|
204
|
-
|
|
203
|
+
// push() wraps a Uint8Array as a Buffer view; Buffer.from() would copy it.
|
|
204
|
+
for (const script of core.pending.splice(0)) transform.push(script);
|
|
205
205
|
};
|
|
206
206
|
|
|
207
207
|
const transform = new Transform({
|
|
@@ -268,20 +268,21 @@ export function createNodeFlightInjector(
|
|
|
268
268
|
* aborting the connection mid-body. Abort errors (client disconnect,
|
|
269
269
|
* render-timeout abort) close the output silently — no noindex, no log.
|
|
270
270
|
*
|
|
271
|
-
* Why this is a pump function and not a Transform stage (TIM-1019):
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
* pipeline() call: the last stage pipes into it (pipe() ends the output
|
|
278
|
-
* on upstream success but leaves it open on upstream error), and the
|
|
279
|
-
* pipeline's rejection handler appends the noindex script.
|
|
271
|
+
* Why this is a pump function and not a Transform stage (TIM-1019): a
|
|
272
|
+
* failing stage takes the whole chain down, so a Transform *inside* it can
|
|
273
|
+
* never append bytes after a failure. The output PassThrough therefore lives
|
|
274
|
+
* outside the chain: the last stage pipes into it (pipe() ends the output on
|
|
275
|
+
* upstream success but leaves it open on upstream error), and the failure
|
|
276
|
+
* handler appends the noindex script.
|
|
280
277
|
*
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
*
|
|
278
|
+
* The stages are chained with `.pipe()` and wired by hand rather than with
|
|
279
|
+
* `stream.pipeline()`, as Next.js does: `pipeline()` allocates an
|
|
280
|
+
* AbortController and a premature-close error on every finish, a fixed cost
|
|
281
|
+
* that showed in profiles of small pages (LOCAL-1476). `pipe()` forwards
|
|
282
|
+
* neither errors nor early closes, so both are handled here: an error, or a
|
|
283
|
+
* stage closing before it ended, destroys every stage (tearing down Fizz and
|
|
284
|
+
* the Flight injector) and ends the output. A consumer that destroys the
|
|
285
|
+
* output before the end (client disconnect) destroys every stage too.
|
|
285
286
|
*/
|
|
286
287
|
export function runNodePipelineWithErrorHandling(
|
|
287
288
|
source: Readable,
|
|
@@ -290,33 +291,49 @@ export function runNodePipelineWithErrorHandling(
|
|
|
290
291
|
nonce?: string
|
|
291
292
|
): PassThrough {
|
|
292
293
|
const output = new PassThrough();
|
|
293
|
-
const
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
pipeline(stages, { signal: consumerGone.signal }).catch((error: unknown) => {
|
|
294
|
+
const stages: readonly Readable[] = [source, ...transforms];
|
|
295
|
+
let previous: Readable = source;
|
|
296
|
+
for (const transform of transforms) {
|
|
297
|
+
previous.pipe(transform);
|
|
298
|
+
previous = transform;
|
|
299
|
+
}
|
|
300
|
+
previous.pipe(output);
|
|
301
|
+
|
|
302
|
+
let settled = false;
|
|
303
|
+
const teardown = (): void => {
|
|
304
|
+
settled = true;
|
|
305
|
+
for (const stage of stages) {
|
|
306
|
+
stage.unpipe();
|
|
307
|
+
if (!stage.destroyed) stage.destroy();
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
const fail = (error: unknown): void => {
|
|
311
|
+
if (settled) return;
|
|
312
|
+
teardown();
|
|
313
313
|
if (output.writableEnded || output.destroyed) return;
|
|
314
|
-
if (isAbortError(error) || signal?.aborted
|
|
314
|
+
if (isAbortError(error) || signal?.aborted) {
|
|
315
315
|
output.end();
|
|
316
316
|
return;
|
|
317
317
|
}
|
|
318
318
|
logStreamingError({ error });
|
|
319
319
|
output.end(Buffer.from(noindexScript(nonce), 'utf-8'));
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
for (const stage of stages) {
|
|
323
|
+
stage.on('error', fail);
|
|
324
|
+
stage.once('close', () => {
|
|
325
|
+
if (!stage.readableEnded) {
|
|
326
|
+
fail(stage.errored ?? new Error('SSR stream closed before it ended'));
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
// The last stage ended: everything upstream finished cleanly.
|
|
331
|
+
previous.once('end', () => {
|
|
332
|
+
settled = true;
|
|
333
|
+
});
|
|
334
|
+
// The consumer left before the end: release Fizz and the Flight injector.
|
|
335
|
+
output.once('close', () => {
|
|
336
|
+
if (!output.readableEnded && !settled) teardown();
|
|
320
337
|
});
|
|
321
338
|
|
|
322
339
|
return output;
|