@bgub/fig-server 2.1.0 → 2.2.0

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",
@@ -615,7 +615,12 @@ function flushCompletedQueues(request) {
615
615
  }
616
616
  if (request.abortableTasks.size === 0 && request.completedBoundaries.size === 0 && request.clientRenderedBoundaries.size === 0 && request.partialBoundaries.size === 0) {
617
617
  request.status = "closed";
618
- request.dispose();
618
+ try {
619
+ request.dispose();
620
+ } catch (error) {
621
+ request.controller.error(error);
622
+ return;
623
+ }
619
624
  request.controller.close();
620
625
  }
621
626
  }
@@ -712,12 +717,12 @@ function flushBoundarySegment(request, boundary, segment) {
712
717
  flushSegment(request, segment);
713
718
  request.write("</div>");
714
719
  }
715
- if (segment !== boundary.contentSegment) writeSegmentRevealScript(request, segment, blockingIds);
720
+ if (segment !== boundary.contentSegment) writeSegmentRevealScript(request, boundary, segment, blockingIds);
716
721
  }
717
- function writeSegmentRevealScript(request, segment, blockingIds) {
722
+ function writeSegmentRevealScript(request, boundary, segment, blockingIds) {
718
723
  const id = ensureSegmentId(request, segment);
719
724
  writeRuntime(request);
720
- writeDataOperation(request);
725
+ writeDataOperation(request, boundary);
721
726
  writeScript(request, withAssetGate(blockingIds, `${RUNTIME_REF}.s(${jsString(placeholderId(request, id))},${jsString(segmentId(request, id))})`));
722
727
  }
723
728
  function writeBoundaryRevealScript(request, boundary) {
@@ -725,13 +730,14 @@ function writeBoundaryRevealScript(request, boundary) {
725
730
  const metadata = switchBoundaryMetadata(request, boundary);
726
731
  const metadataArgument = metadata === null ? "" : `,${metadata}`;
727
732
  writeRuntime(request);
728
- writeDataOperation(request);
733
+ writeDataOperation(request, boundary);
729
734
  const boundaryRef = jsString(boundaryId(request, ensureBoundaryId(request, boundary)));
730
735
  const contentRef = jsString(segmentId(request, ensureSegmentId(request, boundary.contentSegment)));
731
736
  writeScript(request, withAssetGate(blockingIds, boundary.activityId === null ? `${RUNTIME_REF}.c(${boundaryRef},${contentRef}${metadataArgument})` : `${RUNTIME_REF}.ac(${jsString(boundary.activityId)},${boundaryRef},${contentRef}${metadataArgument})`));
737
+ writeLateDataOperations(request, boundary);
732
738
  }
733
739
  function takeData(request) {
734
- return claimData(request, request.dataStore.snapshot());
740
+ return [...[...new Set(request.queuedDataRows.values())].flatMap((batch) => decodePayloadDataEntries(takeQueuedDataBatch(request, batch))), ...claimData(request, request.dataStore.snapshot())];
735
741
  }
736
742
  function claimData(request, entries) {
737
743
  return entries.filter((entry) => {
@@ -742,11 +748,53 @@ function claimData(request, entries) {
742
748
  return true;
743
749
  });
744
750
  }
745
- function writeDataOperation(request) {
751
+ function unqueuedData(request, entries) {
752
+ return entries.filter((entry) => {
753
+ const key = normalizeDataResourceKey(entry.key);
754
+ if (request.writtenDataKeys.has(key)) return false;
755
+ if (request.queuedDataRows.has(key)) return false;
756
+ return request.streamDataFilter?.(entry) !== false;
757
+ });
758
+ }
759
+ function writeDataOperation(request, boundary) {
746
760
  const pending = request.pendingDataKeys;
747
761
  if (pending === null || pending.size === 0) return;
748
- const rows = encodeDataRows(claimData(request, request.dataStore.snapshotKeys(pending)));
762
+ for (const key of pending) {
763
+ const batch = request.queuedDataRows.get(key);
764
+ if (batch !== void 0) writeDataRows(request, takeQueuedDataBatch(request, batch));
765
+ }
766
+ const dependencies = [];
767
+ const rows = encodeDataRows(claimData(request, request.dataStore.snapshotKeys(pending, dependencies)));
749
768
  pending.clear();
769
+ for (const entry of dependencies) {
770
+ const batch = request.queuedDataRows.get(normalizeDataResourceKey(entry.key));
771
+ if (batch !== void 0 && !boundary.lateDataRows.includes(batch)) boundary.lateDataRows.push(batch);
772
+ }
773
+ const late = encodeDataRows(unqueuedData(request, dependencies));
774
+ if (late.length > 0) {
775
+ boundary.lateDataRows.push(late);
776
+ for (const row of late) request.queuedDataRows.set(normalizeDataResourceKey(row.key), late);
777
+ }
778
+ writeDataRows(request, [...late.map((row) => ({
779
+ key: row.key,
780
+ pending: true
781
+ })), ...rows]);
782
+ }
783
+ function writeLateDataOperations(request, boundary) {
784
+ const batches = boundary.lateDataRows;
785
+ boundary.lateDataRows = [];
786
+ for (const rows of batches) writeDataRows(request, takeQueuedDataBatch(request, rows));
787
+ }
788
+ function takeQueuedDataBatch(request, batch) {
789
+ const rows = batch.splice(0);
790
+ for (const row of rows) {
791
+ const key = normalizeDataResourceKey(row.key);
792
+ request.queuedDataRows.delete(key);
793
+ request.writtenDataKeys.add(key);
794
+ }
795
+ return rows;
796
+ }
797
+ function writeDataRows(request, rows) {
750
798
  if (rows.length === 0) return;
751
799
  writeScript(request, `${RUNTIME_REF}.d(${jsString(request.identifierPrefix)},${escapeScriptJson(rows)})`);
752
800
  }
@@ -829,6 +877,7 @@ function flushClientRenderedBoundary(request, boundary) {
829
877
  const call = boundary.activityId === null ? `${RUNTIME_REF}.x(${boundaryRef},${digest},${message}${cleanup})` : `${RUNTIME_REF}.ax(${jsString(boundary.activityId)},${boundaryRef},${digest},${message}${cleanup})`;
830
878
  boundary.stagedSegmentIds.clear();
831
879
  writeScript(request, call);
880
+ writeLateDataOperations(request, boundary);
832
881
  }
833
882
  function drainBoundaryQueue(request, queue, flush) {
834
883
  for (;;) {
@@ -908,6 +957,7 @@ function createServerRenderRequest(node, options, mode) {
908
957
  pendingDataKeys: mode?.prerender === true || options.streamData === false ? null : /* @__PURE__ */ new Set(),
909
958
  streamDataFilter: typeof options.streamData === "object" ? options.streamData.filter ?? null : null,
910
959
  writtenDataKeys: /* @__PURE__ */ new Set(),
960
+ queuedDataRows: /* @__PURE__ */ new Map(),
911
961
  dispose: disposeRequest,
912
962
  abortReason: null,
913
963
  identifierPrefix,
@@ -1363,6 +1413,7 @@ function renderSuspense(props, frame) {
1363
1413
  error: null,
1364
1414
  fallbackSegment: null,
1365
1415
  fallbackTasks: /* @__PURE__ */ new Set(),
1416
+ lateDataRows: [],
1366
1417
  stagedSegmentIds: /* @__PURE__ */ new Set(),
1367
1418
  id: null,
1368
1419
  metadataVisible: false,
@@ -1584,7 +1635,7 @@ function discardBoundaryServerWork(request, boundary) {
1584
1635
  request.completedBoundaries.delete(boundary);
1585
1636
  request.partialBoundaries.delete(boundary);
1586
1637
  const discardedSegments = /* @__PURE__ */ new Set();
1587
- discardSegmentTree(request, boundary.contentSegment, discardedSegments, boundary.stagedSegmentIds);
1638
+ discardSegmentTree(request, boundary.contentSegment, discardedSegments, boundary);
1588
1639
  for (const task of request.abortableTasks) if (discardedSegments.has(task.frame.segment)) abortTask(task);
1589
1640
  if (boundary.parentFlushed) request.clientRenderedBoundaries.add(boundary);
1590
1641
  }
@@ -1631,20 +1682,22 @@ function reportBrowserBailout(request, bailout, stack) {
1631
1682
  report(error, { componentStack: componentStack(stack) });
1632
1683
  } catch {}
1633
1684
  }
1634
- function discardSegmentTree(request, segment, discarded, stagedSegmentIds) {
1685
+ function discardSegmentTree(request, segment, discarded, owner) {
1635
1686
  discarded.add(segment);
1636
1687
  const boundary = segment.boundary;
1637
1688
  if (boundary !== null) {
1638
1689
  boundary.status = "client-rendered";
1639
- for (const id of boundary.stagedSegmentIds) stagedSegmentIds.add(id);
1690
+ for (const id of boundary.stagedSegmentIds) owner.stagedSegmentIds.add(id);
1640
1691
  boundary.stagedSegmentIds.clear();
1692
+ owner.lateDataRows.push(...boundary.lateDataRows);
1693
+ boundary.lateDataRows = [];
1641
1694
  boundary.completedSegments.length = 0;
1642
1695
  request.completedBoundaries.delete(boundary);
1643
1696
  request.partialBoundaries.delete(boundary);
1644
1697
  request.clientRenderedBoundaries.delete(boundary);
1645
- discardSegmentTree(request, boundary.contentSegment, discarded, stagedSegmentIds);
1698
+ discardSegmentTree(request, boundary.contentSegment, discarded, owner);
1646
1699
  }
1647
- for (const child of segment.children) discardSegmentTree(request, child, discarded, stagedSegmentIds);
1700
+ for (const child of segment.children) discardSegmentTree(request, child, discarded, owner);
1648
1701
  }
1649
1702
  function abort(request, reason) {
1650
1703
  if (request.status === "closed") return;
@@ -1655,7 +1708,12 @@ function abort(request, reason) {
1655
1708
  fatalError(request, error);
1656
1709
  return;
1657
1710
  }
1658
- request.dispose();
1711
+ try {
1712
+ request.dispose();
1713
+ } catch {
1714
+ fatalError(request, error);
1715
+ return;
1716
+ }
1659
1717
  for (const task of request.abortableTasks) {
1660
1718
  const { boundary, stack } = task.frame;
1661
1719
  if (boundary !== null) markBoundaryClientRendered(request, boundary, error, stack, {});
@@ -1667,7 +1725,9 @@ function abort(request, reason) {
1667
1725
  function fatalError(request, error) {
1668
1726
  if (request.status === "closed") return;
1669
1727
  request.status = "closed";
1670
- request.dispose();
1728
+ try {
1729
+ request.dispose();
1730
+ } catch {}
1671
1731
  request.headReady.reject(error);
1672
1732
  request.shellReady.reject(error);
1673
1733
  request.allReady.reject(error);