@bgub/fig-server 2.1.0 → 2.2.1

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/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { i as escapeText, n as escapeScriptJson, t as escapeAttribute } from "./escaping-2fHOwhEM.js";
2
2
  import { a as componentStack, c as noLane, d as serverErrorPayload, f as streamFlowBlocked, i as cloneContextValues, l as nonceAttribute, m as withContextValue, n as suppressesImagePreloads, o as createStaticDispatcher, p as streamHighWaterMark, r as STREAMED_METADATA_ATTRIBUTE, s as deferred, t as imagePreloadFromHostProps, u as noop } from "./image-preloads-DwTY3DIk.js";
3
3
  import { Fragment, createElement, isValidElement } from "@bgub/fig";
4
- import { ACTIVITY_TEMPLATE_ATTRIBUTE, EARLY_EVENT_HANDLER_PROPERTY, EARLY_EVENT_QUEUE_PROPERTY, HYDRATION_SKIP_ATTRIBUTE, REPLAYABLE_EVENT_TYPES, STREAMED_DATA_PROPERTY, SUSPENSE_BROWSER_MARKER, SUSPENSE_CLIENT_MARKER, SUSPENSE_COMPLETED_MARKER, SUSPENSE_END_MARKER, SUSPENSE_MARKER_PREFIX, SUSPENSE_PENDING_PREFIX, TEXT_SEPARATOR_DATA, VIEW_TRANSITION_CLASS_ATTRIBUTE, VIEW_TRANSITION_NAME_ATTRIBUTE, VIEW_TRANSITION_PENDING_PROPERTY, VIEW_TRANSITION_TIMEOUT_MS, assetResourceFromHostProps, assetResourceHostProps, assetResourceKey, attachDataStore, collectChildren, createRendererDataStore, encodePayloadDataEntries, invalidChildError, isActivity, isAssets, isClientReference, isContext, isErrorBoundary, isFigAssetResource, isPortal, isSuspense, isThenable, isViewTransition, normalizeDataResourceKey, readThenable, setCurrentDataStore, setCurrentDispatcher } from "@bgub/fig/internal";
4
+ import { ACTIVITY_TEMPLATE_ATTRIBUTE, EARLY_EVENT_HANDLER_PROPERTY, EARLY_EVENT_QUEUE_PROPERTY, HYDRATION_SKIP_ATTRIBUTE, REPLAYABLE_EVENT_TYPES, STREAMED_DATA_PROPERTY, SUSPENSE_BROWSER_MARKER, SUSPENSE_CLIENT_MARKER, SUSPENSE_COMPLETED_MARKER, SUSPENSE_END_MARKER, SUSPENSE_MARKER_PREFIX, SUSPENSE_PENDING_PREFIX, TEXT_SEPARATOR_DATA, VIEW_TRANSITION_CLASS_ATTRIBUTE, VIEW_TRANSITION_NAME_ATTRIBUTE, VIEW_TRANSITION_PENDING_PROPERTY, VIEW_TRANSITION_TIMEOUT_MS, assetResourceFromHostProps, assetResourceHostProps, assetResourceKey, attachDataStore, collectChildren, createRendererDataStore, decodePayloadDataEntries, encodePayloadDataEntries, invalidChildError, isActivity, isAssets, isClientReference, isContext, isErrorBoundary, isFigAssetResource, isPortal, isSuspense, isThenable, isViewTransition, normalizeDataResourceKey, readThenable, setCurrentDataStore, setCurrentDispatcher } from "@bgub/fig/internal";
5
5
  //#region src/html.ts
6
6
  const voidElements = /* @__PURE__ */ new Set([
7
7
  "area",
@@ -591,6 +591,7 @@ function prefixedId(request, kind, id) {
591
591
  //#endregion
592
592
  //#region src/renderer-flush.ts
593
593
  const documentHeadMarker = Symbol("fig.document-head");
594
+ const shellDataMarkersSlot = Symbol("fig.shell-data-markers-slot");
594
595
  const leadingNewlineStartMarker = Symbol("fig.leading-newline-start");
595
596
  const leadingNewlineEndMarker = Symbol("fig.leading-newline-end");
596
597
  const RUNTIME_REF = "__figSSR";
@@ -604,8 +605,11 @@ function flushCompletedQueues(request) {
604
605
  try {
605
606
  sealHead(request);
606
607
  if (request.rootSegment.status !== "flushed") {
608
+ request.shellDataRows = claimShellDependencies(request);
609
+ if (request.documentHasHead === null) writeShellDataMarkers(request);
607
610
  flushSegment(request, request.rootSegment);
608
611
  request.pendingDataKeys?.clear();
612
+ writeDataRows(request, request.shellDataRows.splice(0));
609
613
  flushWriteBuffer(request);
610
614
  }
611
615
  if (!drainBoundaryQueue(request, request.clientRenderedBoundaries, flushClientRenderedBoundary) && !drainBoundaryQueue(request, request.completedBoundaries, flushCompletedBoundary)) drainBoundaryQueue(request, request.partialBoundaries, flushPartialBoundary);
@@ -615,7 +619,12 @@ function flushCompletedQueues(request) {
615
619
  }
616
620
  if (request.abortableTasks.size === 0 && request.completedBoundaries.size === 0 && request.clientRenderedBoundaries.size === 0 && request.partialBoundaries.size === 0) {
617
621
  request.status = "closed";
618
- request.dispose();
622
+ try {
623
+ request.dispose();
624
+ } catch (error) {
625
+ request.controller.error(error);
626
+ return;
627
+ }
619
628
  request.controller.close();
620
629
  }
621
630
  }
@@ -712,12 +721,12 @@ function flushBoundarySegment(request, boundary, segment) {
712
721
  flushSegment(request, segment);
713
722
  request.write("</div>");
714
723
  }
715
- if (segment !== boundary.contentSegment) writeSegmentRevealScript(request, segment, blockingIds);
724
+ if (segment !== boundary.contentSegment) writeSegmentRevealScript(request, boundary, segment, blockingIds);
716
725
  }
717
- function writeSegmentRevealScript(request, segment, blockingIds) {
726
+ function writeSegmentRevealScript(request, boundary, segment, blockingIds) {
718
727
  const id = ensureSegmentId(request, segment);
719
728
  writeRuntime(request);
720
- writeDataOperation(request);
729
+ writeDataOperation(request, boundary);
721
730
  writeScript(request, withAssetGate(blockingIds, `${RUNTIME_REF}.s(${jsString(placeholderId(request, id))},${jsString(segmentId(request, id))})`));
722
731
  }
723
732
  function writeBoundaryRevealScript(request, boundary) {
@@ -725,13 +734,39 @@ function writeBoundaryRevealScript(request, boundary) {
725
734
  const metadata = switchBoundaryMetadata(request, boundary);
726
735
  const metadataArgument = metadata === null ? "" : `,${metadata}`;
727
736
  writeRuntime(request);
728
- writeDataOperation(request);
737
+ writeDataOperation(request, boundary);
729
738
  const boundaryRef = jsString(boundaryId(request, ensureBoundaryId(request, boundary)));
730
739
  const contentRef = jsString(segmentId(request, ensureSegmentId(request, boundary.contentSegment)));
731
740
  writeScript(request, withAssetGate(blockingIds, boundary.activityId === null ? `${RUNTIME_REF}.c(${boundaryRef},${contentRef}${metadataArgument})` : `${RUNTIME_REF}.ac(${jsString(boundary.activityId)},${boundaryRef},${contentRef}${metadataArgument})`));
741
+ writeLateDataOperations(request, boundary);
742
+ }
743
+ function claimShellDependencies(request) {
744
+ if (request.pendingDataKeys === null) return [];
745
+ const reads = /* @__PURE__ */ new Set();
746
+ collectShellReads(request.rootSegment, reads);
747
+ const rows = encodeDataRows(unqueuedData(request, request.dataStore.snapshotDependencies(reads)));
748
+ for (const row of rows) request.writtenDataKeys.add(normalizeDataResourceKey(row.key));
749
+ return rows;
750
+ }
751
+ function collectShellReads(segment, reads) {
752
+ if (segment.boundary?.status === "completed") {
753
+ for (const completed of segment.boundary.completedSegments) collectShellReads(completed, reads);
754
+ return;
755
+ }
756
+ for (const key of segment.dataKeys ?? []) reads.add(key);
757
+ for (const child of segment.children) collectShellReads(child, reads);
758
+ }
759
+ function writeShellDataMarkers(request) {
760
+ const rows = request.shellDataRows;
761
+ if (rows.length === 0) return;
762
+ writeRuntime(request);
763
+ writeDataRows(request, rows.map((row) => ({
764
+ key: row.key,
765
+ pending: true
766
+ })));
732
767
  }
733
768
  function takeData(request) {
734
- return claimData(request, request.dataStore.snapshot());
769
+ return [...[...new Set(request.queuedDataRows.values())].flatMap((batch) => decodePayloadDataEntries(takeQueuedDataBatch(request, batch))), ...claimData(request, request.dataStore.snapshot())];
735
770
  }
736
771
  function claimData(request, entries) {
737
772
  return entries.filter((entry) => {
@@ -742,11 +777,53 @@ function claimData(request, entries) {
742
777
  return true;
743
778
  });
744
779
  }
745
- function writeDataOperation(request) {
780
+ function unqueuedData(request, entries) {
781
+ return entries.filter((entry) => {
782
+ const key = normalizeDataResourceKey(entry.key);
783
+ if (request.writtenDataKeys.has(key)) return false;
784
+ if (request.queuedDataRows.has(key)) return false;
785
+ return request.streamDataFilter?.(entry) !== false;
786
+ });
787
+ }
788
+ function writeDataOperation(request, boundary) {
746
789
  const pending = request.pendingDataKeys;
747
790
  if (pending === null || pending.size === 0) return;
748
- const rows = encodeDataRows(claimData(request, request.dataStore.snapshotKeys(pending)));
791
+ for (const key of pending) {
792
+ const batch = request.queuedDataRows.get(key);
793
+ if (batch !== void 0) writeDataRows(request, takeQueuedDataBatch(request, batch));
794
+ }
795
+ const dependencies = [];
796
+ const rows = encodeDataRows(claimData(request, request.dataStore.snapshotKeys(pending, dependencies)));
749
797
  pending.clear();
798
+ for (const entry of dependencies) {
799
+ const batch = request.queuedDataRows.get(normalizeDataResourceKey(entry.key));
800
+ if (batch !== void 0 && !boundary.lateDataRows.includes(batch)) boundary.lateDataRows.push(batch);
801
+ }
802
+ const late = encodeDataRows(unqueuedData(request, dependencies));
803
+ if (late.length > 0) {
804
+ boundary.lateDataRows.push(late);
805
+ for (const row of late) request.queuedDataRows.set(normalizeDataResourceKey(row.key), late);
806
+ }
807
+ writeDataRows(request, [...late.map((row) => ({
808
+ key: row.key,
809
+ pending: true
810
+ })), ...rows]);
811
+ }
812
+ function writeLateDataOperations(request, boundary) {
813
+ const batches = boundary.lateDataRows;
814
+ boundary.lateDataRows = [];
815
+ for (const rows of batches) writeDataRows(request, takeQueuedDataBatch(request, rows));
816
+ }
817
+ function takeQueuedDataBatch(request, batch) {
818
+ const rows = batch.splice(0);
819
+ for (const row of rows) {
820
+ const key = normalizeDataResourceKey(row.key);
821
+ request.queuedDataRows.delete(key);
822
+ request.writtenDataKeys.add(key);
823
+ }
824
+ return rows;
825
+ }
826
+ function writeDataRows(request, rows) {
750
827
  if (rows.length === 0) return;
751
828
  writeScript(request, `${RUNTIME_REF}.d(${jsString(request.identifierPrefix)},${escapeScriptJson(rows)})`);
752
829
  }
@@ -829,6 +906,7 @@ function flushClientRenderedBoundary(request, boundary) {
829
906
  const call = boundary.activityId === null ? `${RUNTIME_REF}.x(${boundaryRef},${digest},${message}${cleanup})` : `${RUNTIME_REF}.ax(${jsString(boundary.activityId)},${boundaryRef},${digest},${message}${cleanup})`;
830
907
  boundary.stagedSegmentIds.clear();
831
908
  writeScript(request, call);
909
+ writeLateDataOperations(request, boundary);
832
910
  }
833
911
  function drainBoundaryQueue(request, queue, flush) {
834
912
  for (;;) {
@@ -856,6 +934,10 @@ function writeChunk(request, chunk, segment) {
856
934
  request.write(request.leadingNewlineStack.at(-1) === false && chunk.value.startsWith("\n") ? `\n${chunk.value}` : chunk.value);
857
935
  return;
858
936
  }
937
+ if (chunk === shellDataMarkersSlot) {
938
+ writeShellDataMarkers(request);
939
+ return;
940
+ }
859
941
  if (chunk !== documentHeadMarker) {
860
942
  request.write(chunk);
861
943
  return;
@@ -908,6 +990,8 @@ function createServerRenderRequest(node, options, mode) {
908
990
  pendingDataKeys: mode?.prerender === true || options.streamData === false ? null : /* @__PURE__ */ new Set(),
909
991
  streamDataFilter: typeof options.streamData === "object" ? options.streamData.filter ?? null : null,
910
992
  writtenDataKeys: /* @__PURE__ */ new Set(),
993
+ queuedDataRows: /* @__PURE__ */ new Map(),
994
+ shellDataRows: [],
911
995
  dispose: disposeRequest,
912
996
  abortReason: null,
913
997
  identifierPrefix,
@@ -1363,6 +1447,7 @@ function renderSuspense(props, frame) {
1363
1447
  error: null,
1364
1448
  fallbackSegment: null,
1365
1449
  fallbackTasks: /* @__PURE__ */ new Set(),
1450
+ lateDataRows: [],
1366
1451
  stagedSegmentIds: /* @__PURE__ */ new Set(),
1367
1452
  id: null,
1368
1453
  metadataVisible: false,
@@ -1455,7 +1540,10 @@ function renderHostElement(type, props, frame) {
1455
1540
  consumePendingLeadingNewline(frame);
1456
1541
  const viewTransition = frame.viewTransition;
1457
1542
  writeElementStart(type, viewTransition === null ? props : viewTransitionHostProps(props, viewTransition), frame.segment, frame.selectProps);
1458
- if (isDocumentHead) frame.segment.write(earlyEventCaptureMarkup(frame.request));
1543
+ if (isDocumentHead) {
1544
+ frame.segment.write(earlyEventCaptureMarkup(frame.request));
1545
+ frame.segment.write(shellDataMarkersSlot);
1546
+ }
1459
1547
  if (isVoid) return;
1460
1548
  const previousPendingLeadingNewline = frame.pendingLeadingNewline;
1461
1549
  const tracksLeadingNewline = type === "pre" || type === "textarea";
@@ -1584,7 +1672,7 @@ function discardBoundaryServerWork(request, boundary) {
1584
1672
  request.completedBoundaries.delete(boundary);
1585
1673
  request.partialBoundaries.delete(boundary);
1586
1674
  const discardedSegments = /* @__PURE__ */ new Set();
1587
- discardSegmentTree(request, boundary.contentSegment, discardedSegments, boundary.stagedSegmentIds);
1675
+ discardSegmentTree(request, boundary.contentSegment, discardedSegments, boundary);
1588
1676
  for (const task of request.abortableTasks) if (discardedSegments.has(task.frame.segment)) abortTask(task);
1589
1677
  if (boundary.parentFlushed) request.clientRenderedBoundaries.add(boundary);
1590
1678
  }
@@ -1631,20 +1719,22 @@ function reportBrowserBailout(request, bailout, stack) {
1631
1719
  report(error, { componentStack: componentStack(stack) });
1632
1720
  } catch {}
1633
1721
  }
1634
- function discardSegmentTree(request, segment, discarded, stagedSegmentIds) {
1722
+ function discardSegmentTree(request, segment, discarded, owner) {
1635
1723
  discarded.add(segment);
1636
1724
  const boundary = segment.boundary;
1637
1725
  if (boundary !== null) {
1638
1726
  boundary.status = "client-rendered";
1639
- for (const id of boundary.stagedSegmentIds) stagedSegmentIds.add(id);
1727
+ for (const id of boundary.stagedSegmentIds) owner.stagedSegmentIds.add(id);
1640
1728
  boundary.stagedSegmentIds.clear();
1729
+ owner.lateDataRows.push(...boundary.lateDataRows);
1730
+ boundary.lateDataRows = [];
1641
1731
  boundary.completedSegments.length = 0;
1642
1732
  request.completedBoundaries.delete(boundary);
1643
1733
  request.partialBoundaries.delete(boundary);
1644
1734
  request.clientRenderedBoundaries.delete(boundary);
1645
- discardSegmentTree(request, boundary.contentSegment, discarded, stagedSegmentIds);
1735
+ discardSegmentTree(request, boundary.contentSegment, discarded, owner);
1646
1736
  }
1647
- for (const child of segment.children) discardSegmentTree(request, child, discarded, stagedSegmentIds);
1737
+ for (const child of segment.children) discardSegmentTree(request, child, discarded, owner);
1648
1738
  }
1649
1739
  function abort(request, reason) {
1650
1740
  if (request.status === "closed") return;
@@ -1655,7 +1745,12 @@ function abort(request, reason) {
1655
1745
  fatalError(request, error);
1656
1746
  return;
1657
1747
  }
1658
- request.dispose();
1748
+ try {
1749
+ request.dispose();
1750
+ } catch {
1751
+ fatalError(request, error);
1752
+ return;
1753
+ }
1659
1754
  for (const task of request.abortableTasks) {
1660
1755
  const { boundary, stack } = task.frame;
1661
1756
  if (boundary !== null) markBoundaryClientRendered(request, boundary, error, stack, {});
@@ -1667,7 +1762,9 @@ function abort(request, reason) {
1667
1762
  function fatalError(request, error) {
1668
1763
  if (request.status === "closed") return;
1669
1764
  request.status = "closed";
1670
- request.dispose();
1765
+ try {
1766
+ request.dispose();
1767
+ } catch {}
1671
1768
  request.headReady.reject(error);
1672
1769
  request.shellReady.reject(error);
1673
1770
  request.allReady.reject(error);