@timber-js/app 0.2.0-alpha.239 → 0.2.0-alpha.240
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/{actions-Top9GftB.js → actions-DIgnvDrG.js} +4 -4
- package/dist/_chunks/{actions-Top9GftB.js.map → actions-DIgnvDrG.js.map} +1 -1
- package/dist/_chunks/{als-registry-DpygwCp0.js → als-registry-PPYstxK0.js} +2 -1
- package/dist/_chunks/{als-registry-DpygwCp0.js.map → als-registry-PPYstxK0.js.map} +1 -1
- package/dist/_chunks/{cache-api-Bo8c7XVF.js → cache-api-uWQwuxWb.js} +5 -5
- package/dist/_chunks/{cache-api-Bo8c7XVF.js.map → cache-api-uWQwuxWb.js.map} +1 -1
- package/dist/_chunks/{chains-DMoHX4yH.js → chains-C7CExrUI.js} +2 -2
- package/dist/_chunks/{chains-DMoHX4yH.js.map → chains-C7CExrUI.js.map} +1 -1
- package/dist/_chunks/chains-CSslTUdI.js +1 -0
- package/dist/_chunks/{classify-BwyAd-dZ.js → classify-Cj9EeZoL.js} +2 -2
- package/dist/_chunks/{classify-BwyAd-dZ.js.map → classify-Cj9EeZoL.js.map} +1 -1
- package/dist/_chunks/{cli-check-DQAD9MLo.js → cli-check-C3QOAru7.js} +3 -3
- package/dist/_chunks/{cli-check-DQAD9MLo.js.map → cli-check-C3QOAru7.js.map} +1 -1
- package/dist/_chunks/{cli-schema-sync-C_xNLqBj.js → cli-schema-sync-BRxZz66D.js} +1 -1
- package/dist/_chunks/{cli-schema-sync-CmGls3dc.js → cli-schema-sync-DQUS_o3V.js} +2 -2
- package/dist/_chunks/{cli-schema-sync-CmGls3dc.js.map → cli-schema-sync-DQUS_o3V.js.map} +1 -1
- package/dist/_chunks/{client-dep-entries-C5BTQtFS.js → client-dep-entries-ZETBdLgv.js} +2 -2
- package/dist/_chunks/{client-dep-entries-C5BTQtFS.js.map → client-dep-entries-ZETBdLgv.js.map} +1 -1
- package/dist/_chunks/{convention-lint-GQA6WTRo.js → convention-lint-BoVISNS6.js} +3 -3
- package/dist/_chunks/{convention-lint-GQA6WTRo.js.map → convention-lint-BoVISNS6.js.map} +1 -1
- package/dist/_chunks/dev-server-B8roKA3Q.js +1 -0
- package/dist/_chunks/{dev-server-Cr3_Fu5A.js → dev-server-C5R6jx8Q.js} +2 -2
- package/dist/_chunks/{dev-server-Cr3_Fu5A.js.map → dev-server-C5R6jx8Q.js.map} +1 -1
- package/dist/_chunks/{dev-span-RsVeYYc_.js → dev-span-0aG7Q_Ph.js} +3 -7
- package/dist/_chunks/dev-span-0aG7Q_Ph.js.map +1 -0
- package/dist/_chunks/{error-boundary-Bh-egWUI.js → error-boundary-DB51f9ih.js} +98 -419
- package/dist/_chunks/error-boundary-DB51f9ih.js.map +1 -0
- package/dist/_chunks/{flight-source-node-BsrlUjMO.js → flight-source-node-D9cHBhjP.js} +7 -4
- package/dist/_chunks/flight-source-node-D9cHBhjP.js.map +1 -0
- package/dist/_chunks/{json-lossy-check-D47R9Fp5.js → json-lossy-check-Y1GxqVX_.js} +2 -2
- package/dist/_chunks/{json-lossy-check-D47R9Fp5.js.map → json-lossy-check-Y1GxqVX_.js.map} +1 -1
- package/dist/_chunks/{live-graph-HcLScfiV.js → live-graph-BDQWvnMm.js} +4 -4
- package/dist/_chunks/{live-graph-HcLScfiV.js.map → live-graph-BDQWvnMm.js.map} +1 -1
- package/dist/_chunks/{logger-DDLtp5e-.js → logger-DmRR9tmZ.js} +2 -2
- package/dist/_chunks/{logger-DDLtp5e-.js.map → logger-DmRR9tmZ.js.map} +1 -1
- package/dist/_chunks/metadata-routes-MXNjWOt5.js.map +1 -1
- package/dist/_chunks/{node-response-omewa8e6.js → node-response-yp8zdyRp.js} +2 -2
- package/dist/_chunks/{node-response-omewa8e6.js.map → node-response-yp8zdyRp.js.map} +1 -1
- package/dist/_chunks/plugin-context-BtaQcMuh.js.map +1 -1
- package/dist/_chunks/{poison-scan-DONdiCGX.js → poison-scan-WQgUydF_.js} +2 -2
- package/dist/_chunks/{poison-scan-DONdiCGX.js.map → poison-scan-WQgUydF_.js.map} +1 -1
- package/dist/_chunks/{scanner-DSAJjH0x.js → scanner-C9Lrf77z.js} +3 -2
- package/dist/_chunks/{scanner-DSAJjH0x.js.map → scanner-C9Lrf77z.js.map} +1 -1
- package/dist/_chunks/{sizeof-DVhEVN_y.js → sizeof-BISkSaU0.js} +2 -2
- package/dist/_chunks/{sizeof-DVhEVN_y.js.map → sizeof-BISkSaU0.js.map} +1 -1
- package/dist/_chunks/{walkers-96YopJds.js → walkers-Bxv4v7tX.js} +3 -3
- package/dist/_chunks/{walkers-96YopJds.js.map → walkers-Bxv4v7tX.js.map} +1 -1
- package/dist/adapters/cloudflare-kv-cache.js +1 -1
- package/dist/analyze/crawl-entry.js +3 -3
- package/dist/analyze/graph-command.js +2 -2
- package/dist/cache/index.js +2 -2
- package/dist/cache/stores/memory.js +1 -1
- package/dist/cdn/workers-cache-purge.js +1 -1
- package/dist/cli.js +3 -3
- package/dist/client/browser-entry/action-dispatch.d.ts.map +1 -1
- package/dist/client/browser-entry/hydrate.d.ts +1 -1
- package/dist/client/browser-entry/router-init.d.ts.map +1 -1
- package/dist/client/error-boundary.d.ts +1 -0
- package/dist/client/error-boundary.d.ts.map +1 -1
- package/dist/client/error-boundary.js +1 -1
- package/dist/client/history.d.ts +4 -5
- package/dist/client/history.d.ts.map +1 -1
- package/dist/client/index.js +6 -7
- package/dist/client/index.js.map +1 -1
- package/dist/client/internal.d.ts +1 -1
- package/dist/client/internal.d.ts.map +1 -1
- package/dist/client/internal.js +194 -55
- package/dist/client/internal.js.map +1 -1
- package/dist/client/navigation-commit.d.ts +6 -25
- package/dist/client/navigation-commit.d.ts.map +1 -1
- package/dist/client/outlet-content-cache.d.ts +59 -0
- package/dist/client/outlet-content-cache.d.ts.map +1 -0
- package/dist/client/outlet-scope-context.d.ts +30 -0
- package/dist/client/outlet-scope-context.d.ts.map +1 -0
- package/dist/client/params-context.d.ts +10 -11
- package/dist/client/params-context.d.ts.map +1 -1
- package/dist/client/router-pipeline.d.ts +5 -5
- package/dist/client/router-pipeline.d.ts.map +1 -1
- package/dist/client/router-types.d.ts +10 -24
- package/dist/client/router-types.d.ts.map +1 -1
- package/dist/client/router.d.ts.map +1 -1
- package/dist/client/rsc-fetch.d.ts +4 -17
- package/dist/client/rsc-fetch.d.ts.map +1 -1
- package/dist/client/segment-cache.d.ts +15 -103
- package/dist/client/segment-cache.d.ts.map +1 -1
- package/dist/client/segment-outlet.d.ts +28 -52
- package/dist/client/segment-outlet.d.ts.map +1 -1
- package/dist/client/stale-segment.d.ts +39 -0
- package/dist/client/stale-segment.d.ts.map +1 -0
- package/dist/client/use-segment-params.d.ts +1 -1
- package/dist/config-types.d.ts +7 -6
- package/dist/config-types.d.ts.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -732
- package/dist/index.js.map +1 -1
- package/dist/native/rsc/manifest.json +6 -4
- package/dist/native/rsc/routing/segment-keys.js +3 -11
- package/dist/native/rsc/routing/segment-keys.js.map +1 -1
- package/dist/native/rsc/server/access-gate.js +3 -7
- package/dist/native/rsc/server/access-gate.js.map +1 -1
- package/dist/native/rsc/server/als-registry.js +2 -1
- package/dist/native/rsc/server/als-registry.js.map +1 -1
- package/dist/native/rsc/server/build-manifest.js.map +1 -1
- package/dist/native/rsc/server/chain-url-parts.js +1 -6
- package/dist/native/rsc/server/chain-url-parts.js.map +1 -1
- package/dist/native/rsc/server/client-state-tree.js +33 -0
- package/dist/native/rsc/server/client-state-tree.js.map +1 -0
- package/dist/native/rsc/server/flight-source-node.js +5 -2
- package/dist/native/rsc/server/flight-source-node.js.map +1 -1
- package/dist/native/rsc/server/flight-source.js +46 -7
- package/dist/native/rsc/server/flight-source.js.map +1 -1
- package/dist/native/rsc/server/pipeline-interception.js +1 -1
- package/dist/native/rsc/server/publish-params.js.map +1 -1
- package/dist/native/rsc/server/route-element-builder.js +43 -61
- package/dist/native/rsc/server/route-element-builder.js.map +1 -1
- package/dist/native/rsc/server/rsc-entry/helpers.js +5 -28
- package/dist/native/rsc/server/rsc-entry/helpers.js.map +1 -1
- package/dist/native/rsc/server/rsc-entry/render-route.js +33 -7
- package/dist/native/rsc/server/rsc-entry/render-route.js.map +1 -1
- package/dist/native/rsc/server/rsc-entry/revalidate-renderer.js +2 -1
- package/dist/native/rsc/server/rsc-entry/revalidate-renderer.js.map +1 -1
- package/dist/native/rsc/server/rsc-entry/rsc-payload.js +2 -5
- package/dist/native/rsc/server/rsc-entry/rsc-payload.js.map +1 -1
- package/dist/native/rsc/server/rsc-entry/rsc-stream.js +44 -40
- package/dist/native/rsc/server/rsc-entry/rsc-stream.js.map +1 -1
- package/dist/native/rsc/server/rsc-entry/ssr-renderer.js +1 -1
- package/dist/native/rsc/server/rsc-entry/ssr-renderer.js.map +1 -1
- package/dist/native/rsc/server/segment-hash/capture.js +107 -0
- package/dist/native/rsc/server/segment-hash/capture.js.map +1 -0
- package/dist/native/rsc/server/segment-hash/outlet.js +81 -0
- package/dist/native/rsc/server/segment-hash/outlet.js.map +1 -0
- package/dist/native/rsc/server/segment-hash/stream-tap.js +139 -0
- package/dist/native/rsc/server/segment-hash/stream-tap.js.map +1 -0
- package/dist/native/rsc/server/segment-hash/value-hash.js +171 -0
- package/dist/native/rsc/server/segment-hash/value-hash.js.map +1 -0
- package/dist/native/rsc/server/slot-resolver.js +20 -124
- package/dist/native/rsc/server/slot-resolver.js.map +1 -1
- package/dist/native/rsc/server/vite-handoff-rsc.js.map +1 -1
- package/dist/native/rsc/shared/outlet-decision.js +5 -0
- package/dist/native/rsc/shared/outlet-decision.js.map +1 -0
- package/dist/native/ssr/client/error-boundary.js +16 -3
- package/dist/native/ssr/client/error-boundary.js.map +1 -1
- package/dist/native/ssr/client/navigation-commit.js +1 -5
- package/dist/native/ssr/client/navigation-commit.js.map +1 -1
- package/dist/native/ssr/client/outlet-content-cache.js +53 -0
- package/dist/native/ssr/client/outlet-content-cache.js.map +1 -0
- package/dist/native/ssr/client/outlet-scope-context.js +12 -0
- package/dist/native/ssr/client/outlet-scope-context.js.map +1 -0
- package/dist/native/ssr/client/params-context.js.map +1 -1
- package/dist/native/ssr/client/rsc-fetch.js +1 -12
- package/dist/native/ssr/client/rsc-fetch.js.map +1 -1
- package/dist/native/ssr/client/segment-outlet.js +34 -13
- package/dist/native/ssr/client/segment-outlet.js.map +1 -1
- package/dist/native/ssr/client/stale-segment.js +29 -0
- package/dist/native/ssr/client/stale-segment.js.map +1 -0
- package/dist/native/ssr/manifest.json +5 -4
- package/dist/native/ssr/routing/segment-keys.js +5 -1
- package/dist/native/ssr/routing/segment-keys.js.map +1 -1
- package/dist/native/ssr/server/access-gate.js +3 -7
- package/dist/native/ssr/server/access-gate.js.map +1 -1
- package/dist/native/ssr/server/als-registry.js +1 -0
- package/dist/native/ssr/server/als-registry.js.map +1 -1
- package/dist/native/ssr/server/build-manifest.js.map +1 -1
- package/dist/native/ssr/server/client-state-tree.js +33 -0
- package/dist/native/ssr/server/client-state-tree.js.map +1 -0
- package/dist/native/ssr/server/flight-source-node.js +5 -2
- package/dist/native/ssr/server/flight-source-node.js.map +1 -1
- package/dist/native/ssr/server/flight-source.js +1 -0
- package/dist/native/ssr/server/flight-source.js.map +1 -1
- package/dist/native/ssr/server/pipeline-interception.js +1 -1
- package/dist/native/ssr/server/segment-hash/value-hash.js +162 -0
- package/dist/native/ssr/server/segment-hash/value-hash.js.map +1 -0
- package/dist/native/ssr/server/ssr-wrappers.js.map +1 -1
- package/dist/native/ssr/server/vite-handoff-rsc.js.map +1 -1
- package/dist/native/ssr/shared/payload-root.js.map +1 -1
- package/dist/plugin-context.d.ts +0 -16
- package/dist/plugin-context.d.ts.map +1 -1
- package/dist/plugins/build-manifest.d.ts.map +1 -1
- package/dist/routing/index.js +2 -2
- package/dist/routing/segment-keys.d.ts +8 -21
- package/dist/routing/segment-keys.d.ts.map +1 -1
- package/dist/server/access-gate.d.ts +0 -14
- package/dist/server/access-gate.d.ts.map +1 -1
- package/dist/server/als-registry.d.ts +16 -0
- package/dist/server/als-registry.d.ts.map +1 -1
- package/dist/server/build-manifest.d.ts +1 -10
- package/dist/server/build-manifest.d.ts.map +1 -1
- package/dist/server/chain-url-parts.d.ts +3 -13
- package/dist/server/chain-url-parts.d.ts.map +1 -1
- package/dist/server/client-state-tree.d.ts +31 -0
- package/dist/server/client-state-tree.d.ts.map +1 -0
- package/dist/server/flight-source-node.d.ts +6 -1
- package/dist/server/flight-source-node.d.ts.map +1 -1
- package/dist/server/flight-source.d.ts +27 -0
- package/dist/server/flight-source.d.ts.map +1 -1
- package/dist/server/index.js +2 -2
- package/dist/server/internal.js +48 -47
- package/dist/server/internal.js.map +1 -1
- package/dist/server/publish-params.d.ts +5 -4
- package/dist/server/publish-params.d.ts.map +1 -1
- package/dist/server/route-element-builder.d.ts +4 -19
- package/dist/server/route-element-builder.d.ts.map +1 -1
- package/dist/server/rsc-entry/helpers.d.ts +7 -13
- package/dist/server/rsc-entry/helpers.d.ts.map +1 -1
- package/dist/server/rsc-entry/render-route.d.ts.map +1 -1
- package/dist/server/rsc-entry/revalidate-renderer.d.ts.map +1 -1
- package/dist/server/rsc-entry/rsc-payload.d.ts +1 -2
- package/dist/server/rsc-entry/rsc-payload.d.ts.map +1 -1
- package/dist/server/rsc-entry/rsc-stream.d.ts +11 -2
- package/dist/server/rsc-entry/rsc-stream.d.ts.map +1 -1
- package/dist/server/rsc-entry/ssr-renderer.d.ts +0 -2
- package/dist/server/rsc-entry/ssr-renderer.d.ts.map +1 -1
- package/dist/server/segment-hash/capture.d.ts +77 -0
- package/dist/server/segment-hash/capture.d.ts.map +1 -0
- package/dist/server/segment-hash/outlet.d.ts +65 -0
- package/dist/server/segment-hash/outlet.d.ts.map +1 -0
- package/dist/server/segment-hash/stream-tap.d.ts +49 -0
- package/dist/server/segment-hash/stream-tap.d.ts.map +1 -0
- package/dist/server/segment-hash/value-hash.d.ts +70 -0
- package/dist/server/segment-hash/value-hash.d.ts.map +1 -0
- package/dist/server/slot-resolver.d.ts +13 -78
- package/dist/server/slot-resolver.d.ts.map +1 -1
- package/dist/server/ssr-wrappers.d.ts +7 -9
- package/dist/server/ssr-wrappers.d.ts.map +1 -1
- package/dist/server/vite-handoff-rsc.d.ts +10 -1
- package/dist/server/vite-handoff-rsc.d.ts.map +1 -1
- package/dist/shared/outlet-decision.d.ts +48 -0
- package/dist/shared/outlet-decision.d.ts.map +1 -0
- package/dist/shared/payload-root.d.ts +5 -8
- package/dist/shared/payload-root.d.ts.map +1 -1
- package/dist/shared/segment-info.d.ts +13 -28
- package/dist/shared/segment-info.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/client/browser-entry/action-dispatch.ts +1 -15
- package/src/client/browser-entry/hydrate.ts +3 -3
- package/src/client/browser-entry/router-init.ts +28 -47
- package/src/client/error-boundary.tsx +49 -9
- package/src/client/history.ts +4 -5
- package/src/client/internal.ts +1 -1
- package/src/client/navigation-commit.ts +25 -83
- package/src/client/outlet-content-cache.ts +119 -0
- package/src/client/outlet-scope-context.ts +42 -0
- package/src/client/params-context.ts +11 -12
- package/src/client/router-pipeline.ts +10 -74
- package/src/client/router-types.ts +11 -26
- package/src/client/router.ts +7 -22
- package/src/client/rsc-fetch.ts +5 -30
- package/src/client/segment-cache.ts +19 -264
- package/src/client/segment-outlet.tsx +80 -72
- package/src/client/stale-segment.ts +60 -0
- package/src/client/use-segment-params.ts +1 -1
- package/src/config-types.ts +7 -6
- package/src/index.ts +0 -2
- package/src/plugin-context.ts +0 -16
- package/src/plugins/build-manifest.ts +1 -22
- package/src/routing/scanner.ts +12 -0
- package/src/routing/segment-keys.ts +9 -35
- package/src/rsc-runtime/vendor-types.d.ts +8 -0
- package/src/server/access-gate.tsx +3 -32
- package/src/server/als-registry.ts +23 -0
- package/src/server/build-manifest.ts +1 -11
- package/src/server/chain-url-parts.ts +3 -21
- package/src/server/client-state-tree.ts +75 -0
- package/src/server/flight-source-node.ts +7 -2
- package/src/server/flight-source.ts +102 -9
- package/src/server/pipeline-interception.ts +1 -1
- package/src/server/publish-params.ts +5 -4
- package/src/server/route-element-builder.ts +77 -206
- package/src/server/rsc-entry/helpers.ts +11 -65
- package/src/server/rsc-entry/render-route.ts +54 -28
- package/src/server/rsc-entry/revalidate-renderer.ts +2 -1
- package/src/server/rsc-entry/rsc-payload.ts +11 -39
- package/src/server/rsc-entry/rsc-stream.ts +123 -101
- package/src/server/rsc-entry/ssr-renderer.ts +1 -3
- package/src/server/segment-hash/capture.ts +232 -0
- package/src/server/segment-hash/outlet.ts +185 -0
- package/src/server/segment-hash/stream-tap.ts +244 -0
- package/src/server/segment-hash/value-hash.ts +338 -0
- package/src/server/slot-resolver.ts +37 -323
- package/src/server/ssr-wrappers.tsx +7 -9
- package/src/server/vite-handoff-rsc.ts +12 -1
- package/src/shared/outlet-decision.ts +54 -0
- package/src/shared/payload-root.ts +5 -8
- package/src/shared/segment-info.ts +13 -28
- package/dist/_chunks/chains-CmzbpG-6.js +0 -1
- package/dist/_chunks/dev-server--4_Ogy_6.js +0 -1
- package/dist/_chunks/dev-span-RsVeYYc_.js.map +0 -1
- package/dist/_chunks/error-boundary-Bh-egWUI.js.map +0 -1
- package/dist/_chunks/flight-source-node-BsrlUjMO.js.map +0 -1
- package/dist/client/segment-update-context.d.ts +0 -15
- package/dist/client/segment-update-context.d.ts.map +0 -1
- package/dist/client/slot-content-cache-context.d.ts +0 -35
- package/dist/client/slot-content-cache-context.d.ts.map +0 -1
- package/dist/native/rsc/server/request-dep.js +0 -12
- package/dist/native/rsc/server/request-dep.js.map +0 -1
- package/dist/native/rsc/server/route-element-helpers.js +0 -20
- package/dist/native/rsc/server/route-element-helpers.js.map +0 -1
- package/dist/native/rsc/server/skippable-prefix.js +0 -72
- package/dist/native/rsc/server/skippable-prefix.js.map +0 -1
- package/dist/native/rsc/server/state-tree-diff.js +0 -35
- package/dist/native/rsc/server/state-tree-diff.js.map +0 -1
- package/dist/native/ssr/client/segment-cache.js +0 -119
- package/dist/native/ssr/client/segment-cache.js.map +0 -1
- package/dist/native/ssr/client/segment-update-context.js +0 -9
- package/dist/native/ssr/client/segment-update-context.js.map +0 -1
- package/dist/native/ssr/client/slot-content-cache-context.js +0 -8
- package/dist/native/ssr/client/slot-content-cache-context.js.map +0 -1
- package/dist/native/ssr/server/state-tree-diff.js +0 -23
- package/dist/native/ssr/server/state-tree-diff.js.map +0 -1
- package/dist/plugins/request-dep/analysis.d.ts +0 -106
- package/dist/plugins/request-dep/analysis.d.ts.map +0 -1
- package/dist/plugins/request-dep/index.d.ts +0 -75
- package/dist/plugins/request-dep/index.d.ts.map +0 -1
- package/dist/plugins/request-dep/origin.d.ts +0 -61
- package/dist/plugins/request-dep/origin.d.ts.map +0 -1
- package/dist/server/request-dep.d.ts +0 -15
- package/dist/server/request-dep.d.ts.map +0 -1
- package/dist/server/route-element-helpers.d.ts +0 -12
- package/dist/server/route-element-helpers.d.ts.map +0 -1
- package/dist/server/skippable-prefix.d.ts +0 -29
- package/dist/server/skippable-prefix.d.ts.map +0 -1
- package/dist/server/state-tree-diff.d.ts +0 -78
- package/dist/server/state-tree-diff.d.ts.map +0 -1
- package/src/client/segment-update-context.ts +0 -23
- package/src/client/slot-content-cache-context.ts +0 -43
- package/src/plugins/request-dep/analysis.ts +0 -447
- package/src/plugins/request-dep/index.ts +0 -376
- package/src/plugins/request-dep/origin.ts +0 -310
- package/src/server/request-dep.ts +0 -37
- package/src/server/route-element-helpers.ts +0 -37
- package/src/server/skippable-prefix.ts +0 -172
- package/src/server/state-tree-diff.ts +0 -132
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The capture: a segment rendered on its own Flight stream, inside the current
|
|
3
|
+
* request — the segment's one and only render.
|
|
4
|
+
*
|
|
5
|
+
* The outlet gate captures a segment when the client advertised a hash for it.
|
|
6
|
+
* The capture is decoded as it streams by React's own Flight client in the
|
|
7
|
+
* RSC environment, and the decoded value graph is hashed (value-hash.ts):
|
|
8
|
+
*
|
|
9
|
+
* - its hash equals the client's → a skip, and the decoded tree is dropped;
|
|
10
|
+
* - it differs → the decoded tree is emitted as the segment's live content.
|
|
11
|
+
* Nothing is rendered a second time: server components already ran, and the
|
|
12
|
+
* main render only serializes what they produced.
|
|
13
|
+
*
|
|
14
|
+
* A capture that reaches a Suspense boundary or a promise prop still pending
|
|
15
|
+
* is not waited on (the shell rule is not built): its decoded tree is emitted
|
|
16
|
+
* live at once and the pending content streams when it would have anyway.
|
|
17
|
+
*
|
|
18
|
+
* Nested outlets are holes. Each renders as a `SegmentOutlet` whose decision is
|
|
19
|
+
* an opaque marker, written as a Flight temporary reference. Decoding
|
|
20
|
+
* replaces it with a lazy decision for the nested segment, made only if the
|
|
21
|
+
* decoded tree is sent — a skip decides the nested segments itself.
|
|
22
|
+
*
|
|
23
|
+
* Because the capture is the render, it writes request state (metadata
|
|
24
|
+
* entries, deny status) directly, and reports errors through the main
|
|
25
|
+
* render's error handler. An error decoded into the live content keeps its
|
|
26
|
+
* digest: the main render passes it through rather than reporting it again.
|
|
27
|
+
*
|
|
28
|
+
* See design/52-segment-hash-dedupe.md §"Capture".
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { createHash } from 'node:crypto';
|
|
32
|
+
import { createElement, type ReactNode } from 'react';
|
|
33
|
+
import { segmentCaptureAls } from '../als-registry.ts';
|
|
34
|
+
import { flightChunkBytes, openFlightReader } from '../flight-source.ts';
|
|
35
|
+
import { withTimeout } from '../render-timeout.ts';
|
|
36
|
+
import { createFromReadableStream, renderFlight } from '../vite-handoff-rsc.ts';
|
|
37
|
+
import type {
|
|
38
|
+
CaptureHole,
|
|
39
|
+
OutletDecision,
|
|
40
|
+
OutletDecisionValue,
|
|
41
|
+
} from '../../shared/outlet-decision.ts';
|
|
42
|
+
import { hashValue } from './value-hash.ts';
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Inside a capture render, the marker a nested outlet renders with in place of
|
|
46
|
+
* its decision; undefined outside one.
|
|
47
|
+
*/
|
|
48
|
+
export function captureHoleMarker(segmentPath: string): CaptureHole | undefined {
|
|
49
|
+
return segmentCaptureAls.getStore()?.hole(segmentPath);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A short, stable `useId()` prefix for the capture of `segmentPath`. */
|
|
53
|
+
function captureIdPrefix(segmentPath: string): string {
|
|
54
|
+
return 's' + createHash('sha256').update(segmentPath).digest('base64url').slice(0, 8) + '-';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface CaptureOptions {
|
|
58
|
+
/** The segment captured — its `useId()` prefix derives from it. */
|
|
59
|
+
segmentPath: string;
|
|
60
|
+
/** Upper bound on the whole capture stream. */
|
|
61
|
+
timeoutMs: number;
|
|
62
|
+
/** The request's signal — a disconnect abandons the capture. */
|
|
63
|
+
signal?: AbortSignal;
|
|
64
|
+
/** Subscribes to the main render's end — a capture never outlives it. */
|
|
65
|
+
onRenderEnd?: (cancel: () => void) => () => void;
|
|
66
|
+
/** The main render's error handler: its digests, signals and reports. */
|
|
67
|
+
onError?: (error: unknown) => string | void;
|
|
68
|
+
/** Whether a decoded element type is timber's `SegmentOutlet`. */
|
|
69
|
+
isOutlet: (type: unknown) => boolean;
|
|
70
|
+
/** The decision for a nested outlet, made only if the capture is sent live. */
|
|
71
|
+
decideHole: (segmentPath: string) => OutletDecisionValue | undefined;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** What a capture found, as soon as it can say. */
|
|
75
|
+
export type SegmentCapture =
|
|
76
|
+
/** Everything the segment reaches settled: its hash is known. */
|
|
77
|
+
| {
|
|
78
|
+
kind: 'complete';
|
|
79
|
+
hash: string;
|
|
80
|
+
/**
|
|
81
|
+
* The nested outlets that rendered as holes. One missing here was not
|
|
82
|
+
* rendered — an access gate above it denied — and a skip must not decide
|
|
83
|
+
* it: deciding renders its content.
|
|
84
|
+
*/
|
|
85
|
+
holes: ReadonlySet<string>;
|
|
86
|
+
/** The decoded content, to send if the segment is not skipped. */
|
|
87
|
+
content: ReactNode;
|
|
88
|
+
/** The segment is skipped: stop reading the capture. */
|
|
89
|
+
discard(): void;
|
|
90
|
+
}
|
|
91
|
+
/** Not waited for — still streaming, or not hashable. Send it live. */
|
|
92
|
+
| { kind: 'live'; content: ReactNode };
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* A decision made the first time something asks for it. The main render asks
|
|
96
|
+
* when it serializes the decoded tree (Flight awaits any thenable prop); a
|
|
97
|
+
* skip never does.
|
|
98
|
+
*/
|
|
99
|
+
function lazyDecision(decide: () => OutletDecisionValue | undefined): PromiseLike<OutletDecision> {
|
|
100
|
+
let decided: Promise<OutletDecision> | undefined;
|
|
101
|
+
return {
|
|
102
|
+
then(onFulfilled, onRejected) {
|
|
103
|
+
decided ??= Promise.resolve(decide()).then((d) => {
|
|
104
|
+
// The client refuses an outlet without a decision rather than show
|
|
105
|
+
// stale content (client/segment-outlet.tsx).
|
|
106
|
+
if (d === undefined) throw new Error('[timber] a capture hole has no segment to decide');
|
|
107
|
+
return d;
|
|
108
|
+
});
|
|
109
|
+
return decided.then(onFulfilled, onRejected);
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Render `content` as a capture. Settles once its hash is known or not waited for. */
|
|
115
|
+
export async function captureSegment(
|
|
116
|
+
content: ReactNode,
|
|
117
|
+
options: CaptureOptions
|
|
118
|
+
): Promise<SegmentCapture> {
|
|
119
|
+
// Hole markers, by temporary-reference id, and the paths they stand for.
|
|
120
|
+
const temporaryReferences = new WeakMap<object, string>();
|
|
121
|
+
const holePaths = new Map<string, string>();
|
|
122
|
+
const source = segmentCaptureAls.run(
|
|
123
|
+
{
|
|
124
|
+
hole(segmentPath) {
|
|
125
|
+
const id = String(holePaths.size);
|
|
126
|
+
const marker: CaptureHole = { captureHole: true };
|
|
127
|
+
holePaths.set(id, segmentPath);
|
|
128
|
+
temporaryReferences.set(marker, id);
|
|
129
|
+
return marker;
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
() =>
|
|
133
|
+
renderFlight(content, {
|
|
134
|
+
signal: options.signal,
|
|
135
|
+
timeoutMs: options.timeoutMs,
|
|
136
|
+
onError: options.onError,
|
|
137
|
+
temporaryReferences,
|
|
138
|
+
// The capture counts `useId()` from zero like the main render does;
|
|
139
|
+
// a prefix of its own, stable per segment, keeps their ids apart in
|
|
140
|
+
// one document and keeps the hash the same from render to render.
|
|
141
|
+
identifierPrefix: captureIdPrefix(options.segmentPath),
|
|
142
|
+
})
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
const reader = openFlightReader(source);
|
|
146
|
+
// Cancelling the reader aborts the capture's render. Unsubscribed when the
|
|
147
|
+
// capture's stream ends.
|
|
148
|
+
let ended = false;
|
|
149
|
+
const unsubscribe = options.onRenderEnd?.(() => {
|
|
150
|
+
ended = true;
|
|
151
|
+
reader.cancel();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// Pump the capture into the decoder, every read under the deadline.
|
|
155
|
+
const deadline = Date.now() + options.timeoutMs;
|
|
156
|
+
const bytes = new ReadableStream<Uint8Array>({
|
|
157
|
+
async pull(controller) {
|
|
158
|
+
try {
|
|
159
|
+
const result = await withTimeout(
|
|
160
|
+
reader.read(),
|
|
161
|
+
Math.max(0, deadline - Date.now()),
|
|
162
|
+
'segment capture'
|
|
163
|
+
);
|
|
164
|
+
if (result.done) {
|
|
165
|
+
unsubscribe?.();
|
|
166
|
+
controller.close();
|
|
167
|
+
} else {
|
|
168
|
+
controller.enqueue(flightChunkBytes(result.value));
|
|
169
|
+
}
|
|
170
|
+
} catch (error) {
|
|
171
|
+
// Failed, cancelled, or out of time: release the render. Sent live,
|
|
172
|
+
// the decoded content fails at the same point — the main render
|
|
173
|
+
// answers it.
|
|
174
|
+
unsubscribe?.();
|
|
175
|
+
reader.cancel();
|
|
176
|
+
controller.error(error);
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
cancel() {
|
|
180
|
+
unsubscribe?.();
|
|
181
|
+
reader.cancel();
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const decisions = new Map<string, PromiseLike<OutletDecision>>();
|
|
186
|
+
const root = createFromReadableStream(bytes, {
|
|
187
|
+
temporaryReferences: {
|
|
188
|
+
get(key: string) {
|
|
189
|
+
// Flight's client asks for `$<id>`; each hole is decided once, lazily.
|
|
190
|
+
const id = key.slice(1);
|
|
191
|
+
const path = holePaths.get(id);
|
|
192
|
+
if (path === undefined) return undefined;
|
|
193
|
+
let decision = decisions.get(id);
|
|
194
|
+
if (!decision) {
|
|
195
|
+
decision = lazyDecision(() => options.decideHole(path));
|
|
196
|
+
decisions.set(id, decision);
|
|
197
|
+
}
|
|
198
|
+
return decision;
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
let decoded: ReactNode;
|
|
203
|
+
try {
|
|
204
|
+
decoded = (await root) as ReactNode;
|
|
205
|
+
} catch {
|
|
206
|
+
// The capture's root errored (or never arrived). Sent live as the
|
|
207
|
+
// rejected root, the main render writes that error where the segment
|
|
208
|
+
// would be — once, with the digest the capture already reported.
|
|
209
|
+
// A server component that only awaits the decoded root: no app code runs.
|
|
210
|
+
const DecodedCaptureRoot = async (): Promise<ReactNode> => (await root) as ReactNode;
|
|
211
|
+
return { kind: 'live', content: createElement(DecodedCaptureRoot) };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
for (;;) {
|
|
215
|
+
const outcome = hashValue(decoded, { isOutlet: options.isOutlet });
|
|
216
|
+
if (outcome.status === 'complete') {
|
|
217
|
+
return {
|
|
218
|
+
kind: 'complete',
|
|
219
|
+
hash: outcome.hash,
|
|
220
|
+
holes: new Set(holePaths.values()),
|
|
221
|
+
content: decoded,
|
|
222
|
+
discard: () => reader.cancel(),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
if (outcome.status === 'unhashable' || outcome.streaming || ended) {
|
|
226
|
+
return { kind: 'live', content: decoded };
|
|
227
|
+
}
|
|
228
|
+
// Pending async work outside any Suspense boundary: the client cannot
|
|
229
|
+
// commit before it arrives either, so waiting for it is invisible.
|
|
230
|
+
await Promise.allSettled(outcome.waitFor.map((t) => Promise.resolve(t)));
|
|
231
|
+
}
|
|
232
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The outlet gate: renders one segment as a `SegmentOutlet` whose decision is
|
|
3
|
+
* either its live content or a skip.
|
|
4
|
+
*
|
|
5
|
+
* Every segment renders on every navigation; access runs for every one. What
|
|
6
|
+
* hash dedupe saves is bytes: when the client advertised a hash for a segment,
|
|
7
|
+
* the gate captures the segment (capture.ts), and if the capture's hash
|
|
8
|
+
* equals the client's, the segment is sent as a skip — the client keeps the
|
|
9
|
+
* content it holds. Otherwise the live content is sent with a hash the client
|
|
10
|
+
* stores beside it (stream-tap.ts). Nothing is predicted: a skip happens only
|
|
11
|
+
* when a render the server just ran produced exactly what the client holds.
|
|
12
|
+
*
|
|
13
|
+
* A skip carries the decisions of the segments nested directly inside it —
|
|
14
|
+
* only those the capture rendered as holes. Their access gates ran in the
|
|
15
|
+
* capture; a nested gate that denied rendered its deny page instead of the
|
|
16
|
+
* hole, so its segment is never decided, never rendered, never sent.
|
|
17
|
+
*
|
|
18
|
+
* See design/52-segment-hash-dedupe.md §"Mechanism".
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { createElement, type ReactElement, type ReactNode } from 'react';
|
|
22
|
+
import type { OutletDecision, OutletDecisionValue } from '../../shared/outlet-decision.ts';
|
|
23
|
+
import { clientComponents } from '../vite-handoff-rsc.ts';
|
|
24
|
+
import { stripReferenceCacheTag } from './value-hash.ts';
|
|
25
|
+
import { captureHoleMarker, captureSegment, type SegmentCapture } from './capture.ts';
|
|
26
|
+
import type { SegmentHashTap } from './stream-tap.ts';
|
|
27
|
+
|
|
28
|
+
/** One outlet in the element tree, and the outlets directly inside it. */
|
|
29
|
+
export interface OutletSpec {
|
|
30
|
+
segmentPath: string;
|
|
31
|
+
content: ReactNode;
|
|
32
|
+
nested: OutletSpec[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Per-request inputs to every gate in one render. */
|
|
36
|
+
export interface SegmentHashRender {
|
|
37
|
+
/** The hashes the client advertised (`X-Timber-State-Tree`), by path. */
|
|
38
|
+
clientHashes: ReadonlyMap<string, string> | null;
|
|
39
|
+
/** Hashes the outgoing stream; null when `clientSegmentCache` is off. */
|
|
40
|
+
tap: SegmentHashTap | null;
|
|
41
|
+
/**
|
|
42
|
+
* Bounds each capture's stream. A capture also ends with the main render
|
|
43
|
+
* (`tap.onEnd`), which usually comes first.
|
|
44
|
+
*/
|
|
45
|
+
captureTimeoutMs: number;
|
|
46
|
+
/**
|
|
47
|
+
* The main render's error handler. A capture is a segment's render, so its
|
|
48
|
+
* errors are the render's: reported, digested and signalled once, there.
|
|
49
|
+
*/
|
|
50
|
+
onCaptureError?: (error: unknown) => string | void;
|
|
51
|
+
signal?: AbortSignal;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A render with dedupe off: every segment live, no hashes. */
|
|
55
|
+
export const NO_SEGMENT_HASHING: SegmentHashRender = {
|
|
56
|
+
clientHashes: null,
|
|
57
|
+
tap: null,
|
|
58
|
+
captureTimeoutMs: 0,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Whether a decoded element type is timber's `SegmentOutlet` client
|
|
63
|
+
* reference. Matched by reference id — module and export — so an app
|
|
64
|
+
* component that happened to be exported as `SegmentOutlet` is not read as a
|
|
65
|
+
* hole, which would keep changes inside it out of its segment's hash.
|
|
66
|
+
*/
|
|
67
|
+
export function isOutletType(type: unknown): boolean {
|
|
68
|
+
if (typeof type !== 'function' && (typeof type !== 'object' || type === null)) return false;
|
|
69
|
+
const id = (type as { $$id?: unknown }).$$id;
|
|
70
|
+
const outlet = (clientComponents().SegmentOutlet as { $$id?: unknown }).$$id;
|
|
71
|
+
return (
|
|
72
|
+
typeof id === 'string' &&
|
|
73
|
+
typeof outlet === 'string' &&
|
|
74
|
+
stripReferenceCacheTag(id) === stripReferenceCacheTag(outlet)
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The element for an outlet: a server component that decides when it renders
|
|
80
|
+
* — after every access gate above it — and inside a capture renders a bare
|
|
81
|
+
* hole, since the nested segment's content belongs to its own hash.
|
|
82
|
+
*
|
|
83
|
+
* A closure with no props rather than a component taking `{ spec, render }`:
|
|
84
|
+
* dev Flight serializes a server component's props as debug info, and these
|
|
85
|
+
* would repeat the whole subtree in every dev payload.
|
|
86
|
+
*/
|
|
87
|
+
export function outletGate(spec: OutletSpec, render: SegmentHashRender): ReactElement {
|
|
88
|
+
function OutletGate(): ReactElement {
|
|
89
|
+
const { SegmentOutlet } = clientComponents();
|
|
90
|
+
const marker = captureHoleMarker(spec.segmentPath);
|
|
91
|
+
if (marker)
|
|
92
|
+
return createElement(SegmentOutlet, { segmentPath: spec.segmentPath, decision: marker });
|
|
93
|
+
return createElement(SegmentOutlet, {
|
|
94
|
+
segmentPath: spec.segmentPath,
|
|
95
|
+
decision: decide(spec, render),
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
return createElement(OutletGate);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function decide(spec: OutletSpec, render: SegmentHashRender): OutletDecisionValue {
|
|
102
|
+
const clientHash = render.clientHashes?.get(spec.segmentPath);
|
|
103
|
+
if (clientHash === undefined) return live(spec, render, spec.content);
|
|
104
|
+
return detached(compare(spec, render, clientHash));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* The decision as a promise of its own, created here and settled from the
|
|
109
|
+
* capture's callback — not a link in the capture's promise chain.
|
|
110
|
+
*
|
|
111
|
+
* Development Flight follows the async chain of every promise it serializes
|
|
112
|
+
* (async_hooks in the Node build, and each awaited promise's `_debugInfo`) and
|
|
113
|
+
* writes what it finds as debug rows, awaited values included. The chain
|
|
114
|
+
* through a capture ends at its decoded root, so a decision chained to it
|
|
115
|
+
* would carry a skipped segment's content into the payload as debug info —
|
|
116
|
+
* whenever the timing made those reads count as I/O.
|
|
117
|
+
*/
|
|
118
|
+
function detached<T>(source: Promise<T>): Promise<T> {
|
|
119
|
+
return new Promise<T>((resolve, reject) => {
|
|
120
|
+
source.then(resolve, reject);
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function compare(
|
|
125
|
+
spec: OutletSpec,
|
|
126
|
+
render: SegmentHashRender,
|
|
127
|
+
clientHash: string
|
|
128
|
+
): Promise<OutletDecision> {
|
|
129
|
+
return captureSegment(spec.content, {
|
|
130
|
+
segmentPath: spec.segmentPath,
|
|
131
|
+
timeoutMs: render.captureTimeoutMs,
|
|
132
|
+
signal: render.signal,
|
|
133
|
+
onRenderEnd: render.tap?.onEnd,
|
|
134
|
+
onError: render.onCaptureError,
|
|
135
|
+
isOutlet: isOutletType,
|
|
136
|
+
decideHole: (path) => decideNested(spec, path, render),
|
|
137
|
+
}).then(
|
|
138
|
+
(capture) => {
|
|
139
|
+
if (capture.kind === 'complete' && capture.hash === clientHash) {
|
|
140
|
+
capture.discard();
|
|
141
|
+
return skip(spec, render, capture);
|
|
142
|
+
}
|
|
143
|
+
// Sent live: the capture's own elements, decoded — not a second render.
|
|
144
|
+
return live(spec, render, capture.content);
|
|
145
|
+
},
|
|
146
|
+
// The capture could not be started at all (renderFlight threw before
|
|
147
|
+
// rendering anything): render the segment inline, as without a hash.
|
|
148
|
+
() => live(spec, render, spec.content)
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** The decision for the nested outlet at `path`, which rendered as a hole. */
|
|
153
|
+
function decideNested(
|
|
154
|
+
spec: OutletSpec,
|
|
155
|
+
path: string,
|
|
156
|
+
render: SegmentHashRender
|
|
157
|
+
): OutletDecisionValue | undefined {
|
|
158
|
+
const nested = spec.nested.find((n) => n.segmentPath === path);
|
|
159
|
+
// A hole the builder did not record under this segment cannot be decided;
|
|
160
|
+
// the client refuses an outlet without a decision rather than show it stale.
|
|
161
|
+
return nested ? decide(nested, render) : undefined;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function live(spec: OutletSpec, render: SegmentHashRender, children: ReactNode): OutletDecision {
|
|
165
|
+
if (!render.tap) return { children };
|
|
166
|
+
return { hash: render.tap.advertise(spec.segmentPath), children };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* A skip decides exactly the nested outlets its capture rendered as holes —
|
|
171
|
+
* the ones whose access gates passed. Those are also exactly the outlets in
|
|
172
|
+
* the client's cached copy, since the hashes matched. A nested outlet whose
|
|
173
|
+
* gate denied is not decided at all: deciding it would render its content.
|
|
174
|
+
*/
|
|
175
|
+
function skip(
|
|
176
|
+
spec: OutletSpec,
|
|
177
|
+
render: SegmentHashRender,
|
|
178
|
+
capture: Extract<SegmentCapture, { kind: 'complete' }>
|
|
179
|
+
): OutletDecision {
|
|
180
|
+
const outlets: Record<string, OutletDecisionValue> = {};
|
|
181
|
+
for (const nested of spec.nested) {
|
|
182
|
+
if (capture.holes.has(nested.segmentPath)) outlets[nested.segmentPath] = decide(nested, render);
|
|
183
|
+
}
|
|
184
|
+
return { skip: true, hash: capture.hash, outlets };
|
|
185
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The advertised hash: each live segment's hash, computed from what the main
|
|
3
|
+
* Flight stream actually sent.
|
|
4
|
+
*
|
|
5
|
+
* A live `SegmentOutlet` carries `decision.hash` as a promise. The tap feeds
|
|
6
|
+
* the outgoing stream to React's own Flight client in the RSC environment,
|
|
7
|
+
* finds each live outlet in the decoded tree, and once everything the
|
|
8
|
+
* decision's `children` reach has settled, resolves that promise with the
|
|
9
|
+
* hash of those decoded values (value-hash.ts). Flight then writes the hash
|
|
10
|
+
* as one more row. The client stores it beside the content it committed, so
|
|
11
|
+
* the hash it later advertises describes exactly what it received
|
|
12
|
+
* (design/52 §"Two hashes, two jobs").
|
|
13
|
+
*
|
|
14
|
+
* Nothing about this can stall the response: a hash the tap cannot compute
|
|
15
|
+
* resolves to null once the decoded payload has been explored without
|
|
16
|
+
* finding its outlet, or when the stream ends, and every pending hash
|
|
17
|
+
* resolves to null once `timeoutMs` passes — a pending promise would otherwise hold the
|
|
18
|
+
* Flight stream open.
|
|
19
|
+
*
|
|
20
|
+
* See design/52-segment-hash-dedupe.md §"Wire format".
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { hashSettledValue } from './value-hash.ts';
|
|
24
|
+
|
|
25
|
+
export interface SegmentHashTap {
|
|
26
|
+
/**
|
|
27
|
+
* The hash of live segment `segmentPath`'s content in this stream, once it
|
|
28
|
+
* has all arrived. Null when it cannot be computed.
|
|
29
|
+
*/
|
|
30
|
+
advertise(segmentPath: string): Promise<string | null>;
|
|
31
|
+
/** Feed one chunk of the outgoing stream. */
|
|
32
|
+
observe(chunk: Uint8Array): void;
|
|
33
|
+
/** The stream ended or was cancelled: give up on everything pending. */
|
|
34
|
+
end(): void;
|
|
35
|
+
/**
|
|
36
|
+
* Run `cancel` once the stream ends, however it ends; returns the
|
|
37
|
+
* unsubscribe. Captures started for this render stop with it (capture.ts) —
|
|
38
|
+
* nothing waits on them any more. A callback list rather than an
|
|
39
|
+
* AbortSignal: aborting a signal from the stream's teardown kept every
|
|
40
|
+
* finished request reachable in dev (tests/e2e/dev-request-retention.test.ts).
|
|
41
|
+
*/
|
|
42
|
+
onEnd(cancel: () => void): () => void;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface SegmentHashTapOptions {
|
|
46
|
+
timeoutMs: number;
|
|
47
|
+
/** Whether a decoded element type is timber's `SegmentOutlet`. */
|
|
48
|
+
isOutlet: (type: unknown) => boolean;
|
|
49
|
+
/** React's Flight client in the RSC environment. */
|
|
50
|
+
decode: (stream: ReadableStream<Uint8Array>) => PromiseLike<unknown>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const LAZY = Symbol.for('react.lazy');
|
|
54
|
+
const ELEMENT = new Set([Symbol.for('react.transitional.element'), Symbol.for('react.element')]);
|
|
55
|
+
|
|
56
|
+
function isThenable(value: unknown): value is PromiseLike<unknown> {
|
|
57
|
+
return (
|
|
58
|
+
typeof value === 'object' &&
|
|
59
|
+
value !== null &&
|
|
60
|
+
typeof (value as { then?: unknown }).then === 'function'
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Await a decoded promise; a rejection settles to undefined. */
|
|
65
|
+
async function settled(value: PromiseLike<unknown>): Promise<unknown> {
|
|
66
|
+
try {
|
|
67
|
+
return await value;
|
|
68
|
+
} catch {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function createSegmentHashTap(options: SegmentHashTapOptions): SegmentHashTap {
|
|
74
|
+
const pending = new Map<string, (hash: string | null) => void>();
|
|
75
|
+
const computed = new Map<string, Promise<string | null>>();
|
|
76
|
+
const ambiguous = new Set<string>();
|
|
77
|
+
const onEnd = new Set<() => void>();
|
|
78
|
+
const state = { aborted: false };
|
|
79
|
+
let explored = false;
|
|
80
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
81
|
+
let controller: ReadableStreamDefaultController<Uint8Array> | null = null;
|
|
82
|
+
|
|
83
|
+
const stream = new ReadableStream<Uint8Array>({
|
|
84
|
+
start(c) {
|
|
85
|
+
controller = c;
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
function settle(path: string, hash: string | null): void {
|
|
90
|
+
const resolve = pending.get(path);
|
|
91
|
+
if (!resolve) return;
|
|
92
|
+
pending.delete(path);
|
|
93
|
+
resolve(hash);
|
|
94
|
+
if (pending.size === 0 && timer !== undefined) {
|
|
95
|
+
clearTimeout(timer);
|
|
96
|
+
timer = undefined;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function deliver(path: string): void {
|
|
101
|
+
const hash = computed.get(path);
|
|
102
|
+
if (!hash || !pending.has(path)) return;
|
|
103
|
+
// A hash that cannot be computed is no hash — never a rejection left to
|
|
104
|
+
// crash the process.
|
|
105
|
+
void hash.then(
|
|
106
|
+
(h) => settle(path, ambiguous.has(path) ? null : h),
|
|
107
|
+
() => settle(path, null)
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ─── Finding the outlets in the decoded tree ─────────────────────────────
|
|
112
|
+
|
|
113
|
+
const visited = new WeakSet<object>();
|
|
114
|
+
|
|
115
|
+
/** Walk a decoded value for outlet elements, waiting on what is pending. */
|
|
116
|
+
async function explore(value: unknown): Promise<void> {
|
|
117
|
+
if (state.aborted || value === null || typeof value !== 'object') return;
|
|
118
|
+
if (visited.has(value)) return;
|
|
119
|
+
visited.add(value);
|
|
120
|
+
const tagged = value as { $$typeof?: unknown; _payload?: unknown };
|
|
121
|
+
if (tagged.$$typeof === LAZY) {
|
|
122
|
+
if (isThenable(tagged._payload)) await explore(await settled(tagged._payload));
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (tagged.$$typeof !== undefined && ELEMENT.has(tagged.$$typeof as symbol)) {
|
|
126
|
+
const { props } = value as { props: Record<string, unknown> | null };
|
|
127
|
+
// A client component's type arrives as a lazy reference to its module.
|
|
128
|
+
let type = (value as { type: unknown }).type;
|
|
129
|
+
const lazyType = type as { $$typeof?: unknown; _payload?: unknown } | null;
|
|
130
|
+
if (lazyType && typeof lazyType === 'object' && lazyType.$$typeof === LAZY) {
|
|
131
|
+
type = isThenable(lazyType._payload) ? await settled(lazyType._payload) : undefined;
|
|
132
|
+
}
|
|
133
|
+
if (options.isOutlet(type) && props) {
|
|
134
|
+
await outlet(props.segmentPath, props.decision);
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
await explore(props);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
if (isThenable(value)) {
|
|
141
|
+
await explore(await settled(value));
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const children: unknown[] = Array.isArray(value)
|
|
145
|
+
? value
|
|
146
|
+
: value instanceof Map
|
|
147
|
+
? [...value.keys(), ...value.values()]
|
|
148
|
+
: value instanceof Set
|
|
149
|
+
? [...value]
|
|
150
|
+
: Object.getPrototypeOf(value) === Object.prototype ||
|
|
151
|
+
Object.getPrototypeOf(value) === null
|
|
152
|
+
? Object.values(value)
|
|
153
|
+
: [];
|
|
154
|
+
await Promise.all(children.map(explore));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** One outlet: hash it if live, and find the outlets inside it. */
|
|
158
|
+
async function outlet(segmentPath: unknown, decisionValue: unknown): Promise<void> {
|
|
159
|
+
if (typeof segmentPath !== 'string') return;
|
|
160
|
+
const decision = isThenable(decisionValue) ? await settled(decisionValue) : decisionValue;
|
|
161
|
+
if (!decision || typeof decision !== 'object') return;
|
|
162
|
+
const d = decision as { skip?: unknown; children?: unknown; outlets?: unknown };
|
|
163
|
+
if (d.skip === true) {
|
|
164
|
+
// A skip carries its nested segments' decisions.
|
|
165
|
+
const outlets = d.outlets && typeof d.outlets === 'object' ? d.outlets : {};
|
|
166
|
+
await Promise.all(Object.entries(outlets).map(([path, nested]) => outlet(path, nested)));
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (computed.has(segmentPath)) ambiguous.add(segmentPath);
|
|
170
|
+
computed.set(segmentPath, hashSettledValue(d.children, { isOutlet: options.isOutlet }, state));
|
|
171
|
+
deliver(segmentPath);
|
|
172
|
+
await explore(d.children);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Everything the payload reaches has been explored: an outlet whose hash is
|
|
177
|
+
* still pending is one the walk did not find (a value kind it does not
|
|
178
|
+
* descend into). Its hash is none — it must not hold the stream open until
|
|
179
|
+
* the deadline.
|
|
180
|
+
*/
|
|
181
|
+
function exploredAll(): void {
|
|
182
|
+
explored = true;
|
|
183
|
+
for (const path of [...pending.keys()]) if (!computed.has(path)) settle(path, null);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Decoding starts at once and reads whatever `observe` feeds it. A decoder
|
|
187
|
+
// that throws — even synchronously — ends hashing, never the response.
|
|
188
|
+
void new Promise<unknown>((resolve) => resolve(options.decode(stream)))
|
|
189
|
+
.then((root) => explore(root))
|
|
190
|
+
.then(exploredAll)
|
|
191
|
+
.catch(() => end());
|
|
192
|
+
|
|
193
|
+
function end(): void {
|
|
194
|
+
if (state.aborted) return;
|
|
195
|
+
state.aborted = true;
|
|
196
|
+
for (const cancel of [...onEnd]) cancel();
|
|
197
|
+
onEnd.clear();
|
|
198
|
+
if (timer !== undefined) clearTimeout(timer);
|
|
199
|
+
timer = undefined;
|
|
200
|
+
try {
|
|
201
|
+
controller?.close();
|
|
202
|
+
} catch {
|
|
203
|
+
// Already closed or errored.
|
|
204
|
+
}
|
|
205
|
+
controller = null;
|
|
206
|
+
for (const path of [...pending.keys()]) settle(path, null);
|
|
207
|
+
computed.clear();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return {
|
|
211
|
+
advertise(segmentPath) {
|
|
212
|
+
if (state.aborted) return Promise.resolve(null);
|
|
213
|
+
if (pending.has(segmentPath)) {
|
|
214
|
+
// Two live outlets claimed one path: neither hash is trustworthy.
|
|
215
|
+
ambiguous.add(segmentPath);
|
|
216
|
+
settle(segmentPath, null);
|
|
217
|
+
return Promise.resolve(null);
|
|
218
|
+
}
|
|
219
|
+
if (explored && !computed.has(segmentPath)) return Promise.resolve(null);
|
|
220
|
+
const promise = new Promise<string | null>((resolve) => pending.set(segmentPath, resolve));
|
|
221
|
+
timer ??= setTimeout(end, options.timeoutMs);
|
|
222
|
+
deliver(segmentPath);
|
|
223
|
+
return promise;
|
|
224
|
+
},
|
|
225
|
+
observe(chunk) {
|
|
226
|
+
// The tap runs inside the response's stream: nothing it does may break
|
|
227
|
+
// that stream. A chunk it cannot hand on ends hashing for this response.
|
|
228
|
+
try {
|
|
229
|
+
controller?.enqueue(chunk);
|
|
230
|
+
} catch {
|
|
231
|
+
end();
|
|
232
|
+
}
|
|
233
|
+
},
|
|
234
|
+
end,
|
|
235
|
+
onEnd(cancel) {
|
|
236
|
+
if (state.aborted) {
|
|
237
|
+
cancel();
|
|
238
|
+
return () => {};
|
|
239
|
+
}
|
|
240
|
+
onEnd.add(cancel);
|
|
241
|
+
return () => onEnd.delete(cancel);
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
}
|