@async/framework 0.11.16 → 0.11.17

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/browser.umd.js CHANGED
@@ -6105,7 +6105,19 @@
6105
6105
  })();
6106
6106
 
6107
6107
  const __boundaryReceiverModule = (() => {
6108
+ const { normalizeAttributeConfig, readAttribute } = __attributesModule;
6109
+ const { renderTemplate } = __htmlModule;
6108
6110
  const defaultRecentLimit = 50;
6111
+ const builtBackpatchAttribute = "data-async-backpatch";
6112
+ const pendingTargetAttribute = "data-pending-id";
6113
+ const revealOrders = new Set(["as-ready", "forwards", "backwards", "together"]);
6114
+ const revealTails = new Set(["collapsed", "hidden"]);
6115
+ const structuralAttributeNames = new Set(["innerhtml", "outerhtml", "textcontent", "children", "childnodes"]);
6116
+
6117
+ const AsyncStream = Object.freeze({
6118
+ applyScript,
6119
+ applyCurrentScript
6120
+ });
6109
6121
 
6110
6122
  function createBoundaryReceiver(options = {}) {
6111
6123
  const loader = options.loader;
@@ -6113,6 +6125,7 @@
6113
6125
  const cache = options.cache ?? loader?.cache;
6114
6126
  const scheduler = options.scheduler ?? loader?.scheduler;
6115
6127
  const router = options.router ?? loader?.router;
6128
+ const attributes = normalizeAttributeConfig(options.attributes ?? loader?.attributes);
6116
6129
  const recentLimit = options.recentLimit ?? defaultRecentLimit;
6117
6130
  const throwOnError = options.throwOnError === true;
6118
6131
  const onApply = typeof options.onApply === "function" ? options.onApply : undefined;
@@ -6130,6 +6143,7 @@
6130
6143
  }
6131
6144
 
6132
6145
  const boundaries = new Map();
6146
+ const revealGroups = new Map();
6133
6147
  const recent = [];
6134
6148
  let destroyed = false;
6135
6149
 
@@ -6178,6 +6192,7 @@
6178
6192
  return {
6179
6193
  destroyed,
6180
6194
  boundaries: snapshot,
6195
+ reveal: inspectRevealGroups(revealGroups),
6181
6196
  recent: recent.map((entry) => ({ ...entry }))
6182
6197
  };
6183
6198
  },
@@ -6185,6 +6200,7 @@
6185
6200
  reset(boundary) {
6186
6201
  if (boundary === undefined) {
6187
6202
  boundaries.clear();
6203
+ revealGroups.clear();
6188
6204
  recent.length = 0;
6189
6205
  return receiver;
6190
6206
  }
@@ -6195,12 +6211,20 @@
6195
6211
  recent.splice(index, 1);
6196
6212
  }
6197
6213
  }
6214
+ for (const group of revealGroups.values()) {
6215
+ for (const [index, item] of group.pending) {
6216
+ if (item.normalized.boundary === boundary) {
6217
+ group.pending.delete(index);
6218
+ }
6219
+ }
6220
+ }
6198
6221
  return receiver;
6199
6222
  },
6200
6223
 
6201
6224
  destroy() {
6202
6225
  destroyed = true;
6203
6226
  boundaries.clear();
6227
+ revealGroups.clear();
6204
6228
  recent.length = 0;
6205
6229
  }
6206
6230
  };
@@ -6208,32 +6232,10 @@
6208
6232
  return receiver;
6209
6233
 
6210
6234
  async function applyBoundaryPatch(record, normalized, patch) {
6211
- if (normalized.seq <= record.lastSeq) {
6212
- const result = {
6213
- status: "ignored-stale",
6214
- boundary: normalized.boundary,
6215
- seq: normalized.seq,
6216
- lastSeq: record.lastSeq
6217
- };
6218
- record.ignored += 1;
6219
- record.lastStatus = result.status;
6220
- remember(result);
6221
- onIgnore?.(result, patch);
6222
- return result;
6223
- }
6224
-
6225
- if (normalized.parentScope !== undefined && isScopeDestroyed(normalized.parentScope)) {
6226
- const result = {
6227
- status: "ignored-destroyed",
6228
- boundary: normalized.boundary,
6229
- seq: normalized.seq,
6230
- parentScope: normalized.parentScope
6231
- };
6232
- record.ignored += 1;
6233
- record.lastStatus = result.status;
6234
- remember(result);
6235
- onIgnore?.(result, patch);
6236
- return result;
6235
+ const ignored = preflightIgnoredResult(record, normalized);
6236
+ if (ignored) {
6237
+ rememberIgnored(record, ignored, patch);
6238
+ return ignored;
6237
6239
  }
6238
6240
 
6239
6241
  if (Object.hasOwn(normalized, "error")) {
@@ -6255,35 +6257,91 @@
6255
6257
  return result;
6256
6258
  }
6257
6259
 
6258
- if (normalized.signals) {
6259
- if (!signals || typeof signals.set !== "function") {
6260
- throw new Error("Boundary patch includes signals, but no signal registry is available.");
6261
- }
6262
- for (const [path, value] of Object.entries(normalized.signals)) {
6263
- signals.set(path, value);
6264
- }
6260
+ applyStateEffects(normalized);
6261
+
6262
+ if (normalized.reveal) {
6263
+ return await applyRevealPatch(record, normalized, patch);
6265
6264
  }
6266
6265
 
6267
- if (normalized.cache?.browser) {
6268
- if (!cache || typeof cache.restore !== "function") {
6269
- throw new Error("Boundary patch includes browser cache, but no cache registry is available.");
6266
+ return await commitBoundaryPatch(record, normalized, patch, { stateApplied: true });
6267
+ }
6268
+
6269
+ async function applyRevealPatch(record, normalized, patch) {
6270
+ const group = revealGroup(normalized.reveal);
6271
+ const index = normalized.reveal.index;
6272
+ if (group.committed.has(index)) {
6273
+ throw new TypeError(`Reveal group "${group.id}" already committed index ${index}.`);
6274
+ }
6275
+ if (group.pending.has(index)) {
6276
+ throw new TypeError(`Reveal group "${group.id}" already has a pending patch for index ${index}.`);
6277
+ }
6278
+
6279
+ const item = { record, normalized, patch };
6280
+ group.pending.set(index, item);
6281
+ const ready = takeReadyRevealItems(group);
6282
+ if (!ready.includes(item)) {
6283
+ const result = {
6284
+ status: "buffered",
6285
+ boundary: normalized.boundary,
6286
+ seq: normalized.seq,
6287
+ reveal: revealResultMetadata(normalized.reveal)
6288
+ };
6289
+ record.lastStatus = result.status;
6290
+ remember(result);
6291
+ updateRevealTail(group);
6292
+ return result;
6293
+ }
6294
+
6295
+ let currentResult;
6296
+ for (const readyItem of ready) {
6297
+ const result = await commitBoundaryPatch(readyItem.record, readyItem.normalized, readyItem.patch, {
6298
+ stateApplied: true
6299
+ });
6300
+ group.committed.add(readyItem.normalized.reveal.index);
6301
+ if (readyItem === item) {
6302
+ currentResult = result;
6270
6303
  }
6271
- cache.restore(normalized.cache.browser);
6304
+ }
6305
+ updateRevealTail(group);
6306
+ return currentResult;
6307
+ }
6308
+
6309
+ async function commitBoundaryPatch(record, normalized, patch, options = {}) {
6310
+ const ignored = preflightIgnoredResult(record, normalized);
6311
+ if (ignored) {
6312
+ rememberIgnored(record, ignored, patch);
6313
+ return ignored;
6314
+ }
6315
+
6316
+ if (!options.stateApplied) {
6317
+ applyStateEffects(normalized);
6272
6318
  }
6273
6319
 
6320
+ let boundaryElement;
6321
+ let replacementCount = 0;
6274
6322
  if (normalized.html != null) {
6275
- loader.swap(normalized.boundary, normalized.html);
6323
+ boundaryElement = loader.swap(normalized.boundary, normalized.html);
6324
+ }
6325
+ if (normalized.replace) {
6326
+ boundaryElement ??= findBoundaryElement(loader.root, normalized.boundary, attributes);
6327
+ replacementCount = applyReplacements(boundaryElement, normalized.replace);
6328
+ }
6329
+
6330
+ let attrs;
6331
+ if (normalized.attrs) {
6332
+ boundaryElement ??= findBoundaryElement(loader.root, normalized.boundary, attributes);
6333
+ attrs = applyAttributePatches(boundaryElement, normalized.attrs);
6276
6334
  }
6277
6335
 
6278
6336
  await flushScheduler(scheduler, normalized.scope);
6279
6337
 
6280
6338
  if (normalized.redirect) {
6281
- const result = {
6339
+ const result = withPatchMetadata({
6282
6340
  status: "redirected",
6283
6341
  boundary: normalized.boundary,
6284
6342
  seq: normalized.seq,
6285
6343
  redirect: normalized.redirect
6286
- };
6344
+ }, attrs, replacementCount);
6287
6345
  await followRedirect(normalized.redirect, router, loader);
6288
6346
  record.applied += 1;
6289
6347
  record.lastSeq = normalized.seq;
@@ -6293,11 +6351,11 @@
6293
6351
  return result;
6294
6352
  }
6295
6353
 
6296
- const result = {
6354
+ const result = withPatchMetadata({
6297
6355
  status: "applied",
6298
6356
  boundary: normalized.boundary,
6299
6357
  seq: normalized.seq
6300
- };
6358
+ }, attrs, replacementCount);
6301
6359
  record.applied += 1;
6302
6360
  record.lastSeq = normalized.seq;
6303
6361
  record.lastStatus = result.status;
@@ -6306,6 +6364,157 @@
6306
6364
  return result;
6307
6365
  }
6308
6366
 
6367
+ function applyStateEffects(normalized) {
6368
+ if (normalized.signals) {
6369
+ if (!signals || typeof signals.set !== "function") {
6370
+ throw new Error("Boundary patch includes signals, but no signal registry is available.");
6371
+ }
6372
+ for (const [path, value] of Object.entries(normalized.signals)) {
6373
+ signals.set(path, value);
6374
+ }
6375
+ }
6376
+
6377
+ if (normalized.cache?.browser) {
6378
+ if (!cache || typeof cache.restore !== "function") {
6379
+ throw new Error("Boundary patch includes browser cache, but no cache registry is available.");
6380
+ }
6381
+ cache.restore(normalized.cache.browser);
6382
+ }
6383
+ }
6384
+
6385
+ function applyReplacements(boundaryElement, replacements) {
6386
+ let applied = 0;
6387
+ for (const replacement of replacements) {
6388
+ if (replacement.mode === "boundary") {
6389
+ const target = findBoundaryElement(loader.root, replacement.target, attributes);
6390
+ if (!containsOrEquals(boundaryElement, target)) {
6391
+ throw new Error(`Boundary replacement target "${replacement.target}" is outside boundary "${boundaryIdFor(boundaryElement, attributes)}".`);
6392
+ }
6393
+ loader.swap(replacement.target, replacement.html);
6394
+ applied += 1;
6395
+ continue;
6396
+ }
6397
+
6398
+ const target = findUniqueScopedElement(
6399
+ boundaryElement,
6400
+ (element) => element.getAttribute?.(pendingTargetAttribute) === replacement.target,
6401
+ `Pending replacement target "${replacement.target}"`
6402
+ );
6403
+ const fragment = toFragment(replacement.html, ownerDocumentOf(boundaryElement));
6404
+ const inserted = [...fragment.childNodes];
6405
+ target.replaceWith(fragment);
6406
+ for (const node of inserted) {
6407
+ if (node.nodeType === 1 || node.nodeType === 11) {
6408
+ loader.scan?.(node);
6409
+ }
6410
+ }
6411
+ applied += 1;
6412
+ }
6413
+ return applied;
6414
+ }
6415
+
6416
+ function applyAttributePatches(boundaryElement, attrs) {
6417
+ let applied = 0;
6418
+ for (const attr of attrs.items) {
6419
+ const target = attrs.kind === "built"
6420
+ ? resolveBuiltAttrTarget(boundaryElement, attr.target)
6421
+ : resolveNamedAttrTarget(boundaryElement, attr.target);
6422
+ setPatchedAttribute(target, attr.name, attr.value);
6423
+ applied += 1;
6424
+ }
6425
+ return {
6426
+ applied,
6427
+ ignored: 0
6428
+ };
6429
+ }
6430
+
6431
+ function resolveBuiltAttrTarget(boundaryElement, targetIndex) {
6432
+ const targets = scopedElements(boundaryElement)
6433
+ .filter((element) => element.hasAttribute?.(builtBackpatchAttribute));
6434
+ const target = targets[targetIndex];
6435
+ if (!target) {
6436
+ throw new Error(`Built attribute patch target ${targetIndex} was not found in boundary "${boundaryIdFor(boundaryElement, attributes)}".`);
6437
+ }
6438
+ return target;
6439
+ }
6440
+
6441
+ function resolveNamedAttrTarget(boundaryElement, targetName) {
6442
+ return findUniqueScopedElement(
6443
+ boundaryElement,
6444
+ (element) => readAttribute(element, attributes, "async", "patch") === targetName,
6445
+ `Attribute patch target "${targetName}"`
6446
+ );
6447
+ }
6448
+
6449
+ function findUniqueScopedElement(boundaryElement, predicate, label) {
6450
+ const matches = scopedElements(boundaryElement).filter(predicate);
6451
+ if (matches.length === 0) {
6452
+ throw new Error(`${label} was not found.`);
6453
+ }
6454
+ if (matches.length > 1) {
6455
+ throw new Error(`${label} is ambiguous.`);
6456
+ }
6457
+ return matches[0];
6458
+ }
6459
+
6460
+ function scopedElements(boundaryElement) {
6461
+ return elementsIn(boundaryElement)
6462
+ .filter((element) => !isNestedBoundaryElement(element, boundaryElement, attributes));
6463
+ }
6464
+
6465
+ function revealGroup(reveal) {
6466
+ const existing = revealGroups.get(reveal.group);
6467
+ if (existing) {
6468
+ if (existing.count !== reveal.count || existing.order !== reveal.order || existing.tail !== reveal.tail) {
6469
+ throw new TypeError(`Reveal group "${reveal.group}" metadata does not match earlier patches.`);
6470
+ }
6471
+ return existing;
6472
+ }
6473
+
6474
+ const group = {
6475
+ id: reveal.group,
6476
+ count: reveal.count,
6477
+ order: reveal.order,
6478
+ tail: reveal.tail,
6479
+ pending: new Map(),
6480
+ committed: new Set(),
6481
+ nextForward: 0,
6482
+ nextBackward: reveal.count - 1
6483
+ };
6484
+ revealGroups.set(reveal.group, group);
6485
+ return group;
6486
+ }
6487
+
6488
+ function updateRevealTail(group) {
6489
+ if (!group.tail) {
6490
+ return;
6491
+ }
6492
+ const container = findRevealContainer(loader.root, group.id, attributes);
6493
+ if (!container) {
6494
+ return;
6495
+ }
6496
+ const children = directChildBoundaryElements(container, attributes).slice(0, group.count);
6497
+ if (children.length === 0) {
6498
+ return;
6499
+ }
6500
+
6501
+ const pending = [];
6502
+ for (let index = 0; index < children.length; index += 1) {
6503
+ if (!group.committed.has(index)) {
6504
+ pending.push(index);
6505
+ } else {
6506
+ setTailHidden(children[index], false);
6507
+ }
6508
+ }
6509
+
6510
+ const visiblePending = group.tail === "collapsed" && pending.length > 0
6511
+ ? pendingOrderFor(group, pending)[0]
6512
+ : undefined;
6513
+ for (const index of pending) {
6514
+ setTailHidden(children[index], group.tail === "hidden" || index !== visiblePending);
6515
+ }
6516
+ }
6517
+
6309
6518
  function boundaryRecord(boundary) {
6310
6519
  if (!boundaries.has(boundary)) {
6311
6520
  boundaries.set(boundary, {
@@ -6320,6 +6529,34 @@
6320
6529
  return boundaries.get(boundary);
6321
6530
  }
6322
6531
 
6532
+ function preflightIgnoredResult(record, normalized) {
6533
+ if (normalized.seq <= record.lastSeq) {
6534
+ return {
6535
+ status: "ignored-stale",
6536
+ boundary: normalized.boundary,
6537
+ seq: normalized.seq,
6538
+ lastSeq: record.lastSeq
6539
+ };
6540
+ }
6541
+
6542
+ if (normalized.parentScope !== undefined && isScopeDestroyed(normalized.parentScope)) {
6543
+ return {
6544
+ status: "ignored-destroyed",
6545
+ boundary: normalized.boundary,
6546
+ seq: normalized.seq,
6547
+ parentScope: normalized.parentScope
6548
+ };
6549
+ }
6550
+ return null;
6551
+ }
6552
+
6553
+ function rememberIgnored(record, result, patch) {
6554
+ record.ignored += 1;
6555
+ record.lastStatus = result.status;
6556
+ remember(result);
6557
+ onIgnore?.(result, patch);
6558
+ }
6559
+
6323
6560
  function remember(result) {
6324
6561
  if (recentLimit === 0) {
6325
6562
  return;
@@ -6331,6 +6568,132 @@
6331
6568
  }
6332
6569
  }
6333
6570
 
6571
+ function applyScript(script, options = {}) {
6572
+ if (!script || typeof script !== "object" || typeof script.textContent !== "string") {
6573
+ throw new TypeError("AsyncStream.applyScript(script) requires a JSON script element.");
6574
+ }
6575
+ const attributes = normalizeAttributeConfig(options.attributes ?? options.receiver?.attributes);
6576
+ const patch = parseStreamPatch(script.textContent);
6577
+ const root = options.root ?? script.ownerDocument ?? globalThis.document;
6578
+ const resolved = resolveStreamPatch(patch, { root, attributes });
6579
+ const receiver = resolveReceiver(options);
6580
+ return receiver.apply(resolved);
6581
+ }
6582
+
6583
+ function applyCurrentScript(scriptOrOptions, maybeOptions) {
6584
+ const script = isElementLike(scriptOrOptions)
6585
+ ? scriptOrOptions
6586
+ : globalThis.document?.currentScript;
6587
+ const options = isElementLike(scriptOrOptions) ? maybeOptions ?? {} : scriptOrOptions ?? {};
6588
+ return applyScript(script, options);
6589
+ }
6590
+
6591
+ function resolveReceiver(options = {}) {
6592
+ if (options.receiver && typeof options.receiver.apply === "function") {
6593
+ return options.receiver;
6594
+ }
6595
+ const runtime = options.runtime ?? globalThis.Async?.runtime;
6596
+ const loader = options.loader ?? runtime?.loader;
6597
+ if (!loader) {
6598
+ throw new TypeError("AsyncStream requires receiver, loader, or runtime.loader.");
6599
+ }
6600
+ return createBoundaryReceiver({
6601
+ loader,
6602
+ signals: options.signals ?? runtime?.signals,
6603
+ cache: options.cache ?? runtime?.browser?.cache,
6604
+ scheduler: options.scheduler ?? runtime?.scheduler,
6605
+ router: options.router ?? runtime?.router,
6606
+ attributes: options.attributes ?? runtime?.attributes ?? loader.attributes
6607
+ });
6608
+ }
6609
+
6610
+ function parseStreamPatch(source) {
6611
+ try {
6612
+ return JSON.parse(source);
6613
+ } catch (error) {
6614
+ throw new TypeError(`Async stream patch JSON is invalid: ${error.message}`);
6615
+ }
6616
+ }
6617
+
6618
+ function resolveStreamPatch(patch, { root, attributes }) {
6619
+ if (!patch || typeof patch !== "object" || Array.isArray(patch)) {
6620
+ throw new TypeError("Async stream patch JSON must be an object.");
6621
+ }
6622
+ const resolved = { ...patch };
6623
+ if (patch.replace !== undefined) {
6624
+ resolved.replace = resolveStreamReplacements(patch.replace, root, attributes);
6625
+ }
6626
+ const synthesizedReveal = synthesizeRevealMetadata(resolved, root, attributes);
6627
+ if (patch.reveal !== undefined && synthesizedReveal && !sameRevealMetadata(patch.reveal, synthesizedReveal)) {
6628
+ throw new TypeError("Explicit stream reveal metadata conflicts with DOM reveal metadata.");
6629
+ }
6630
+ if (patch.reveal === undefined && synthesizedReveal) {
6631
+ resolved.reveal = synthesizedReveal;
6632
+ }
6633
+ return resolved;
6634
+ }
6635
+
6636
+ function resolveStreamReplacements(value, root, attributes) {
6637
+ const replacements = Array.isArray(value) ? value : [value];
6638
+ return replacements.map((replacement) => {
6639
+ if (!isPlainObject(replacement)) {
6640
+ throw new TypeError("Stream replacement records must be objects.");
6641
+ }
6642
+ if (replacement.template === undefined || Object.hasOwn(replacement, "html")) {
6643
+ return replacement;
6644
+ }
6645
+ const template = findStreamTemplate(root, replacement.template, attributes);
6646
+ if (!template) {
6647
+ throw new Error(`Stream template "${replacement.template}" was not found.`);
6648
+ }
6649
+ return {
6650
+ ...replacement,
6651
+ html: template.innerHTML
6652
+ };
6653
+ });
6654
+ }
6655
+
6656
+ function findStreamTemplate(root, id, attributes) {
6657
+ if (typeof id !== "string" || id.length === 0) {
6658
+ throw new TypeError("Stream replacement template must be a non-empty string.");
6659
+ }
6660
+ return elementsIn(root)
6661
+ .find((element) => element.tagName === "TEMPLATE" && readAttribute(element, attributes, "async", "stream-template") === id)
6662
+ ?? null;
6663
+ }
6664
+
6665
+ function synthesizeRevealMetadata(patch, root, attributes) {
6666
+ if (typeof patch.boundary !== "string" || patch.boundary.length === 0) {
6667
+ return null;
6668
+ }
6669
+ const boundary = findBoundaryElement(root, patch.boundary, attributes, { required: false });
6670
+ const container = boundary?.parentElement;
6671
+ const group = container ? readAttribute(container, attributes, "async", "reveal") : null;
6672
+ if (!group) {
6673
+ return null;
6674
+ }
6675
+ const children = directChildBoundaryElements(container, attributes);
6676
+ const index = children.indexOf(boundary);
6677
+ if (index === -1) {
6678
+ return null;
6679
+ }
6680
+ return {
6681
+ group,
6682
+ index,
6683
+ count: children.length,
6684
+ order: readAttribute(container, attributes, "async", "reveal-order") || "as-ready",
6685
+ tail: readAttribute(container, attributes, "async", "reveal-tail") || undefined
6686
+ };
6687
+ }
6688
+
6689
+ function sameRevealMetadata(left, right) {
6690
+ return left?.group === right.group &&
6691
+ left?.index === right.index &&
6692
+ left?.count === right.count &&
6693
+ (left?.order ?? "as-ready") === right.order &&
6694
+ left?.tail === right.tail;
6695
+ }
6696
+
6334
6697
  function validatePatch(patch) {
6335
6698
  if (!patch || typeof patch !== "object" || Array.isArray(patch)) {
6336
6699
  throw new TypeError("receiver.apply(patch) requires a boundary patch object.");
@@ -6360,16 +6723,127 @@
6360
6723
  throw new TypeError("Boundary patch scope must be a string.");
6361
6724
  }
6362
6725
 
6726
+ const normalized = { ...patch };
6727
+ if (patch.attrs !== undefined) {
6728
+ normalized.attrs = normalizeAttributePatches(patch.attrs);
6729
+ }
6730
+ if (patch.replace !== undefined) {
6731
+ normalized.replace = normalizeReplacements(patch.replace);
6732
+ }
6733
+ if (patch.reveal !== undefined) {
6734
+ normalized.reveal = normalizeRevealMetadata(patch.reveal);
6735
+ }
6736
+
6363
6737
  const hasHtml = Object.hasOwn(patch, "html") && patch.html != null;
6364
6738
  const hasSignals = patch.signals && Object.keys(patch.signals).length > 0;
6365
6739
  const hasBrowserCache = patch.cache?.browser && Object.keys(patch.cache.browser).length > 0;
6366
6740
  const hasRedirect = Boolean(patch.redirect);
6367
6741
  const hasError = Object.hasOwn(patch, "error");
6368
- if (!hasHtml && !hasSignals && !hasBrowserCache && !hasRedirect && !hasError) {
6369
- throw new TypeError("Boundary patch must include html, signals, cache.browser, redirect, or error.");
6742
+ const hasAttrs = normalized.attrs?.items.length > 0;
6743
+ const hasReplace = normalized.replace?.length > 0;
6744
+ if (!hasHtml && !hasSignals && !hasBrowserCache && !hasRedirect && !hasError && !hasAttrs && !hasReplace) {
6745
+ throw new TypeError("Boundary patch must include html, replace, attrs, signals, cache.browser, redirect, or error.");
6370
6746
  }
6371
6747
 
6372
- return patch;
6748
+ return normalized;
6749
+ }
6750
+
6751
+ function normalizeAttributePatches(attrs) {
6752
+ if (!Array.isArray(attrs)) {
6753
+ throw new TypeError("Boundary patch attrs must be an array.");
6754
+ }
6755
+ if (attrs.length === 0) {
6756
+ return { kind: "built", items: [] };
6757
+ }
6758
+ const named = Array.isArray(attrs[0]);
6759
+ if (named) {
6760
+ return {
6761
+ kind: "named",
6762
+ items: attrs.map((tuple) => normalizeNamedAttributePatch(tuple))
6763
+ };
6764
+ }
6765
+ if (attrs.some(Array.isArray)) {
6766
+ throw new TypeError("Boundary patch attrs cannot mix built triples and no-build tuples.");
6767
+ }
6768
+ if (attrs.length % 3 !== 0) {
6769
+ throw new TypeError("Built attribute patch triples must have a length divisible by 3.");
6770
+ }
6771
+ const items = [];
6772
+ for (let index = 0; index < attrs.length; index += 3) {
6773
+ const target = attrs[index];
6774
+ const name = attrs[index + 1];
6775
+ const value = attrs[index + 2];
6776
+ assertBuiltTarget(target);
6777
+ assertAttributeName(name);
6778
+ assertAttributeValue(value);
6779
+ items.push({ target, name, value });
6780
+ }
6781
+ return { kind: "built", items };
6782
+ }
6783
+
6784
+ function normalizeNamedAttributePatch(tuple) {
6785
+ if (!Array.isArray(tuple) || tuple.length !== 3) {
6786
+ throw new TypeError("No-build attribute patches must be [targetName, attrName, value] tuples.");
6787
+ }
6788
+ const [target, name, value] = tuple;
6789
+ if (typeof target !== "string" || target.length === 0) {
6790
+ throw new TypeError("No-build attribute patch target names must be non-empty strings.");
6791
+ }
6792
+ assertAttributeName(name);
6793
+ assertAttributeValue(value);
6794
+ return { target, name, value };
6795
+ }
6796
+
6797
+ function normalizeReplacements(value) {
6798
+ const replacements = Array.isArray(value) ? value : [value];
6799
+ return replacements.map((replacement) => {
6800
+ if (!isPlainObject(replacement)) {
6801
+ throw new TypeError("Boundary patch replace records must be objects.");
6802
+ }
6803
+ if (typeof replacement.target !== "string" || replacement.target.length === 0) {
6804
+ throw new TypeError("Boundary patch replace target must be a non-empty string.");
6805
+ }
6806
+ if (replacement.mode !== undefined && replacement.mode !== "pending" && replacement.mode !== "boundary") {
6807
+ throw new TypeError("Boundary patch replace mode must be \"pending\" or \"boundary\".");
6808
+ }
6809
+ if (!Object.hasOwn(replacement, "html") || replacement.html == null) {
6810
+ throw new TypeError("Boundary patch replace records must include html.");
6811
+ }
6812
+ return {
6813
+ target: replacement.target,
6814
+ html: replacement.html,
6815
+ mode: replacement.mode ?? "pending"
6816
+ };
6817
+ });
6818
+ }
6819
+
6820
+ function normalizeRevealMetadata(reveal) {
6821
+ if (!isPlainObject(reveal)) {
6822
+ throw new TypeError("Boundary patch reveal metadata must be an object.");
6823
+ }
6824
+ const order = reveal.order ?? "as-ready";
6825
+ if (typeof reveal.group !== "string" || reveal.group.length === 0) {
6826
+ throw new TypeError("Reveal group must be a non-empty string.");
6827
+ }
6828
+ if (!Number.isInteger(reveal.count) || reveal.count < 1) {
6829
+ throw new TypeError("Reveal count must be a positive integer.");
6830
+ }
6831
+ if (!Number.isInteger(reveal.index) || reveal.index < 0 || reveal.index >= reveal.count) {
6832
+ throw new TypeError("Reveal index must be an integer from 0 to count - 1.");
6833
+ }
6834
+ if (!revealOrders.has(order)) {
6835
+ throw new TypeError("Reveal order must be as-ready, forwards, backwards, or together.");
6836
+ }
6837
+ if (reveal.tail !== undefined && !revealTails.has(reveal.tail)) {
6838
+ throw new TypeError("Reveal tail must be collapsed or hidden.");
6839
+ }
6840
+ return {
6841
+ group: reveal.group,
6842
+ index: reveal.index,
6843
+ count: reveal.count,
6844
+ order,
6845
+ tail: reveal.tail
6846
+ };
6373
6847
  }
6374
6848
 
6375
6849
  function assertBoundary(boundary) {
@@ -6378,6 +6852,41 @@
6378
6852
  }
6379
6853
  }
6380
6854
 
6855
+ function assertBuiltTarget(target) {
6856
+ if (!Number.isInteger(target) || target < 0) {
6857
+ throw new TypeError("Built attribute patch target indexes must be non-negative integers.");
6858
+ }
6859
+ }
6860
+
6861
+ function assertAttributeName(name) {
6862
+ if (typeof name !== "string" || name.length === 0) {
6863
+ throw new TypeError("Attribute patch names must be non-empty strings.");
6864
+ }
6865
+ const normalized = name.toLowerCase();
6866
+ if (
6867
+ normalized.startsWith("on") ||
6868
+ structuralAttributeNames.has(normalized) ||
6869
+ /[\s<>"'=]/.test(name)
6870
+ ) {
6871
+ throw new TypeError(`Attribute patch name "${name}" is not allowed.`);
6872
+ }
6873
+ }
6874
+
6875
+ function assertAttributeValue(value) {
6876
+ const type = typeof value;
6877
+ if (value != null && type !== "string" && type !== "number" && type !== "boolean") {
6878
+ throw new TypeError("Attribute patch values must be strings, numbers, booleans, null, or undefined.");
6879
+ }
6880
+ }
6881
+
6882
+ function setPatchedAttribute(element, name, value) {
6883
+ if (value === false || value == null) {
6884
+ element.removeAttribute(name);
6885
+ return;
6886
+ }
6887
+ element.setAttribute(name, value === true ? "" : String(value));
6888
+ }
6889
+
6381
6890
  async function flushScheduler(scheduler, scope) {
6382
6891
  if (!scheduler) {
6383
6892
  return;
@@ -6400,6 +6909,90 @@
6400
6909
  location?.assign?.(redirect);
6401
6910
  }
6402
6911
 
6912
+ function takeReadyRevealItems(group) {
6913
+ if (group.order === "as-ready") {
6914
+ return takePendingIndexes(group, [...group.pending.keys()].sort((left, right) => left - right));
6915
+ }
6916
+ if (group.order === "forwards") {
6917
+ const indexes = [];
6918
+ while (group.pending.has(group.nextForward)) {
6919
+ indexes.push(group.nextForward);
6920
+ group.nextForward += 1;
6921
+ }
6922
+ return takePendingIndexes(group, indexes);
6923
+ }
6924
+ if (group.order === "backwards") {
6925
+ const indexes = [];
6926
+ while (group.pending.has(group.nextBackward)) {
6927
+ indexes.push(group.nextBackward);
6928
+ group.nextBackward -= 1;
6929
+ }
6930
+ return takePendingIndexes(group, indexes);
6931
+ }
6932
+ if (group.committed.size + group.pending.size < group.count) {
6933
+ return [];
6934
+ }
6935
+ const indexes = [];
6936
+ for (let index = 0; index < group.count; index += 1) {
6937
+ if (group.pending.has(index)) {
6938
+ indexes.push(index);
6939
+ }
6940
+ }
6941
+ return takePendingIndexes(group, indexes);
6942
+ }
6943
+
6944
+ function takePendingIndexes(group, indexes) {
6945
+ const items = [];
6946
+ for (const index of indexes) {
6947
+ const item = group.pending.get(index);
6948
+ if (item) {
6949
+ group.pending.delete(index);
6950
+ items.push(item);
6951
+ }
6952
+ }
6953
+ return items;
6954
+ }
6955
+
6956
+ function pendingOrderFor(group, pending) {
6957
+ return group.order === "backwards"
6958
+ ? pending.slice().sort((left, right) => right - left)
6959
+ : pending.slice().sort((left, right) => left - right);
6960
+ }
6961
+
6962
+ function inspectRevealGroups(groups) {
6963
+ const inspected = {};
6964
+ for (const [id, group] of groups) {
6965
+ inspected[id] = {
6966
+ count: group.count,
6967
+ order: group.order,
6968
+ tail: group.tail,
6969
+ pending: [...group.pending.keys()].sort((left, right) => left - right),
6970
+ committed: [...group.committed].sort((left, right) => left - right)
6971
+ };
6972
+ }
6973
+ return inspected;
6974
+ }
6975
+
6976
+ function revealResultMetadata(reveal) {
6977
+ return {
6978
+ group: reveal.group,
6979
+ index: reveal.index,
6980
+ count: reveal.count,
6981
+ order: reveal.order,
6982
+ tail: reveal.tail
6983
+ };
6984
+ }
6985
+
6986
+ function withPatchMetadata(result, attrs, replacementCount) {
6987
+ if (attrs) {
6988
+ result.attrs = attrs;
6989
+ }
6990
+ if (replacementCount > 0) {
6991
+ result.replace = { applied: replacementCount };
6992
+ }
6993
+ return result;
6994
+ }
6995
+
6403
6996
  function toStableError(value) {
6404
6997
  if (value instanceof Error) {
6405
6998
  return value;
@@ -6425,13 +7018,118 @@
6425
7018
  if (result.status === "redirected") {
6426
7019
  entry.redirect = result.redirect;
6427
7020
  }
7021
+ if (result.status === "buffered") {
7022
+ entry.reveal = result.reveal;
7023
+ }
7024
+ if (result.attrs) {
7025
+ entry.attrs = { ...result.attrs };
7026
+ }
7027
+ if (result.replace) {
7028
+ entry.replace = { ...result.replace };
7029
+ }
6428
7030
  return entry;
6429
7031
  }
6430
7032
 
7033
+ function findBoundaryElement(root, boundaryId, attributes, options = {}) {
7034
+ const boundary = elementsIn(root)
7035
+ .find((element) => boundaryIdFor(element, attributes) === String(boundaryId));
7036
+ if (!boundary && options.required !== false) {
7037
+ throw new Error(`Boundary "${boundaryId}" was not found.`);
7038
+ }
7039
+ return boundary ?? null;
7040
+ }
7041
+
7042
+ function boundaryIdFor(element, attributes) {
7043
+ if (element?.tagName === "ASYNC-SUSPENSE" && element.hasAttribute?.("for")) {
7044
+ return element.getAttribute("for");
7045
+ }
7046
+ return readAttribute(element, attributes, "async", "boundary");
7047
+ }
7048
+
7049
+ function directChildBoundaryElements(container, attributes) {
7050
+ return [...(container?.children ?? [])]
7051
+ .filter((element) => boundaryIdFor(element, attributes) != null);
7052
+ }
7053
+
7054
+ function findRevealContainer(root, group, attributes) {
7055
+ return elementsIn(root)
7056
+ .find((element) => readAttribute(element, attributes, "async", "reveal") === group)
7057
+ ?? null;
7058
+ }
7059
+
7060
+ function isNestedBoundaryElement(element, boundaryElement, attributes) {
7061
+ if (element === boundaryElement) {
7062
+ return false;
7063
+ }
7064
+ if (boundaryIdFor(element, attributes) != null) {
7065
+ return true;
7066
+ }
7067
+ let parent = element.parentElement;
7068
+ while (parent && parent !== boundaryElement) {
7069
+ if (boundaryIdFor(parent, attributes) != null) {
7070
+ return true;
7071
+ }
7072
+ parent = parent.parentElement;
7073
+ }
7074
+ return false;
7075
+ }
7076
+
7077
+ function setTailHidden(element, hidden) {
7078
+ if (hidden) {
7079
+ element.setAttribute("hidden", "");
7080
+ if ("hidden" in element) {
7081
+ element.hidden = true;
7082
+ }
7083
+ return;
7084
+ }
7085
+ element.removeAttribute("hidden");
7086
+ if ("hidden" in element) {
7087
+ element.hidden = false;
7088
+ }
7089
+ }
7090
+
7091
+ function containsOrEquals(parent, child) {
7092
+ return parent === child || parent.contains?.(child);
7093
+ }
7094
+
7095
+ function ownerDocumentOf(element) {
7096
+ return element.ownerDocument ?? globalThis.document;
7097
+ }
7098
+
7099
+ function toFragment(value, documentRef) {
7100
+ if (value?.nodeType === 11) {
7101
+ return value.cloneNode(true);
7102
+ }
7103
+ if (value?.tagName === "TEMPLATE") {
7104
+ return value.content.cloneNode(true);
7105
+ }
7106
+ if (value?.nodeType) {
7107
+ const fragment = documentRef.createDocumentFragment();
7108
+ fragment.append(value.cloneNode(true));
7109
+ return fragment;
7110
+ }
7111
+ const template = documentRef.createElement("template");
7112
+ template.innerHTML = typeof value === "string" ? value : renderTemplate(value);
7113
+ return template.content.cloneNode(true);
7114
+ }
7115
+
7116
+ function elementsIn(scope) {
7117
+ const elements = [];
7118
+ if (scope?.nodeType === 1) {
7119
+ elements.push(scope);
7120
+ }
7121
+ elements.push(...(scope?.querySelectorAll?.("*") ?? []));
7122
+ return elements;
7123
+ }
7124
+
7125
+ function isElementLike(value) {
7126
+ return Boolean(value?.nodeType === 1 && typeof value.textContent === "string");
7127
+ }
7128
+
6431
7129
  function isPlainObject(value) {
6432
7130
  return Boolean(value && typeof value === "object" && !Array.isArray(value));
6433
7131
  }
6434
- return { createBoundaryReceiver };
7132
+ return { createBoundaryReceiver, AsyncStream };
6435
7133
  })();
6436
7134
 
6437
7135
  const __delayModule = (() => {
@@ -6541,6 +7239,7 @@
6541
7239
  const { readSnapshot: readSnapshot } = __appModule;
6542
7240
  const { attributeName: attributeName } = __attributesModule;
6543
7241
  const { defineAttributeConfig: defineAttributeConfig } = __attributesModule;
7242
+ const { AsyncStream: AsyncStream } = __boundaryReceiverModule;
6544
7243
  const { createBoundaryReceiver: createBoundaryReceiver } = __boundaryReceiverModule;
6545
7244
  const { createCacheRegistry: createCacheRegistry } = __cacheModule;
6546
7245
  const { defineCache: defineCache } = __cacheModule;
@@ -6572,7 +7271,7 @@
6572
7271
  const { createSignalRegistry: createSignalRegistry } = __signalsModule;
6573
7272
  const { effect: effect } = __signalsModule;
6574
7273
  const { signal: signal } = __signalsModule;
6575
- const api = { asyncSignal, Async, createApp, defineApp, readSnapshot, attributeName, defineAttributeConfig, createBoundaryReceiver, createCacheRegistry, defineCache, component, createComponentRegistry, defineComponent, delay, defineAsyncContainerElement, defineAsyncSuspenseElement, createHandlerRegistry, html, createLazyRegistry, defineRegistrySnapshot, Loader, AsyncLoader, createPartialRegistry, createRegistryStore, createRouteRegistry, createRouter, defineRoute, route, createScheduler, applyServerResult, createServerProxy, resolveServerCommandArguments, unwrapServerResult, computed, createSignal, createSignalRegistry, effect, signal };
7274
+ const api = { asyncSignal, Async, createApp, defineApp, readSnapshot, attributeName, defineAttributeConfig, AsyncStream, createBoundaryReceiver, createCacheRegistry, defineCache, component, createComponentRegistry, defineComponent, delay, defineAsyncContainerElement, defineAsyncSuspenseElement, createHandlerRegistry, html, createLazyRegistry, defineRegistrySnapshot, Loader, AsyncLoader, createPartialRegistry, createRegistryStore, createRouteRegistry, createRouter, defineRoute, route, createScheduler, applyServerResult, createServerProxy, resolveServerCommandArguments, unwrapServerResult, computed, createSignal, createSignalRegistry, effect, signal };
6576
7275
  assertNoUmdNamespaceConflicts(api, Async);
6577
7276
  Object.assign(Async, api);
6578
7277
  Async.Async = Async;