@solidjs/web 2.0.0-rc.17 → 2.0.0-rc.19

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.
@@ -2,7 +2,7 @@ import { createRenderEffect, getOwner, createMemo, OBSERVE, DEV, createRoot, $PR
2
2
  export { Errored, For, Hydration, Loading, Match, NoHydration, Repeat, Reveal, Show, Switch, createComponent, dynamicComponent, getOwner, merge as mergeProps, untrack } from 'solid-js';
3
3
  import { sharedConfig, ssrSanitizeError, beginEdgesRender, reportServerError, viewOf, sourceOwners, ssrHandleError, inServerComponentScope, creationStamp, dynamicCore, getProjectionTrace, shareAsyncIterable } from 'solid-js/internal';
4
4
  export { ssrScope as scope } from 'solid-js/internal';
5
- import { createStream, Serializer, getCrossReferenceHeader, Feature } from 'seroval';
5
+ import { createStream, Feature, getCrossReferenceHeader, crossSerializeStream } from 'seroval';
6
6
  import { AbortSignalPlugin, CustomEventPlugin, DOMExceptionPlugin, EventPlugin, FormDataPlugin, HeadersPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLSearchParamsPlugin, URLPlugin } from 'seroval-plugins/web';
7
7
 
8
8
  const DOMWithState = {
@@ -543,15 +543,92 @@ function resolveSerializerPlugins(customPlugins) {
543
543
  }
544
544
 
545
545
  const defaultDisabledFeatures = () => Feature.AggregateError | Feature.BigIntTypedArray;
546
- const serializeOnlyDisabledFeatures = serializeErrorStacks => (serializeErrorStacks === undefined ? process.env.NODE_ENV === "development" : serializeErrorStacks) ? 0 : Feature.ErrorPrototypeStack;
546
+ const serializeOnlyDisabledFeatures = serializeErrorStacks => (process.env.NODE_ENV === "development" ) ? 0 : Feature.ErrorPrototypeStack;
547
547
  const HYDRATION_GLOBAL = "_$HY.r";
548
- function createSerializer(options) {
549
- return new Serializer({
550
- ...options,
551
- plugins: resolveSerializerPlugins(options.plugins),
552
- disabledFeatures: (options.disabledFeatures === undefined ? defaultDisabledFeatures() : options.disabledFeatures) | serializeOnlyDisabledFeatures(options.serializeErrorStacks)
553
- });
548
+ function createKeyedSerializer(serialize, onError, onDone) {
549
+ const cancels = new Set();
550
+ let pendingWrites = 0;
551
+ let flushed = false;
552
+ let done = false;
553
+ let closed = false;
554
+ const maybeDone = () => {
555
+ if (flushed && pendingWrites === 0 && !done) {
556
+ done = true;
557
+ onDone && onDone();
558
+ }
559
+ };
560
+ return {
561
+ write(key, value) {
562
+ if (flushed) return;
563
+ pendingWrites++;
564
+ let settled = false;
565
+ let failed = false;
566
+ let started = false;
567
+ let cancel = null;
568
+ const settle = () => {
569
+ if (settled) return;
570
+ settled = true;
571
+ if (cancel) cancels.delete(cancel);
572
+ pendingWrites--;
573
+ maybeDone();
574
+ };
575
+ let stream;
576
+ try {
577
+ stream = serialize(key, value, {
578
+ onError: onError && (err => {
579
+ failed = true;
580
+ onError(err);
581
+ if (cancel) {
582
+ cancel();
583
+ settle();
584
+ }
585
+ }),
586
+ onDone: settle
587
+ }, initial => {
588
+ if (initial) started = true;
589
+ return !closed;
590
+ });
591
+ } catch (err) {
592
+ settle();
593
+ throw err;
594
+ }
595
+ if (settled) return;
596
+ if (closed || failed) {
597
+ stream();
598
+ settle();
599
+ return;
600
+ }
601
+ cancel = stream;
602
+ cancels.add(cancel);
603
+ },
604
+ flush() {
605
+ flushed = true;
606
+ maybeDone();
607
+ },
608
+ close() {
609
+ closed = flushed = true;
610
+ for (const cancel of cancels) cancel();
611
+ cancels.clear();
612
+ if (!done) {
613
+ done = true;
614
+ onDone && onDone();
615
+ }
616
+ }
617
+ };
554
618
  }
619
+ const KEY_ESCAPES = {
620
+ '"': '\\"',
621
+ "\\": "\\\\",
622
+ "\n": "\\n",
623
+ "\r": "\\r",
624
+ "\b": "\\b",
625
+ "\t": "\\t",
626
+ "\f": "\\f",
627
+ "<": "\\x3C",
628
+ "\u2028": "\\u2028",
629
+ "\u2029": "\\u2029"
630
+ };
631
+ const serializeKey = key => ("" + key).replace(/["\\\n\r\b\t\f<\u2028\u2029]/g, c => KEY_ESCAPES[c]);
555
632
  function createHydrationSerializer({
556
633
  onData,
557
634
  onDone,
@@ -559,14 +636,20 @@ function createHydrationSerializer({
559
636
  onError,
560
637
  plugins
561
638
  }) {
562
- return createSerializer({
639
+ const resolvedPlugins = resolveSerializerPlugins(plugins);
640
+ const disabledFeatures = defaultDisabledFeatures() | serializeOnlyDisabledFeatures();
641
+ const refs = new Map();
642
+ return createKeyedSerializer((key, value, callbacks, live) => crossSerializeStream(value, {
563
643
  scopeId,
564
- plugins,
565
- globalIdentifier: HYDRATION_GLOBAL,
566
- onData,
567
- onDone,
568
- onError
569
- });
644
+ refs,
645
+ plugins: resolvedPlugins,
646
+ disabledFeatures,
647
+ onSerialize(data, initial) {
648
+ if (!live(initial)) return;
649
+ onData(initial ? HYDRATION_GLOBAL + '["' + serializeKey(key) + '"]=' + data : data);
650
+ },
651
+ ...callbacks
652
+ }), onError, onDone);
570
653
  }
571
654
  function getLocalHeaderScript(id) {
572
655
  return getCrossReferenceHeader(id) + ";";
@@ -1759,7 +1842,7 @@ function isAttrName(prop) {
1759
1842
  if (safeAttrNames.size < 512) safeAttrNames.add(prop);
1760
1843
  return true;
1761
1844
  }
1762
- const REPLACE_SCRIPT = `function $df(e){return _$HY.f?_$HY.f(e):$dfr(e)}function $dfr(e,n,o,t){if(!(n=document.getElementById(e)))return 0;if(!(o=document.getElementById("pl-"+e)))return(_$HY.dq=_$HY.dq||{})[e]=1,0;for(;o&&(8!==o.nodeType||o.nodeValue!=="pl-"+e);)t=o.nextSibling,o.remove(),o=t;t=o.parentNode,o.replaceWith(n.content),n.remove(),(_$HY.v=_$HY.v||{})[e]=1,_$HY.fe(e,t),_$HY.hp&&_$HY.hp[e]&&($dh(_$HY.hp[e]),delete _$HY.hp[e]),$dfd();return 1}function $dfl(e,o,n){if(!(o=document.getElementById("pl-"+e)))return(_$HY.dlq=_$HY.dlq||{})[e]=1,0;if(o._$fl)return 1;for(n=o.nextSibling;n;){if(8===n.nodeType&&n.nodeValue==="pl-"+e){o.parentNode&&o.parentNode.insertBefore(o.content.cloneNode(!0),n),o._$fl=1,$dfd();return 1}n=n.nextSibling}return 0}function $dflj(e,i){for(i=0;i<e.length;i++)$dfl(e[i])}function $dfd(e,i){if(e=_$HY.dq){_$HY.dq=0;for(i in e)$df(i)}if(e=_$HY.dlq){_$HY.dlq=0;for(i in e)$dfl(i)}}function $dfs(e,c,d){(_$HY.sc=_$HY.sc||{})[e]=c,d&&((_$HY.sd=_$HY.sd||{})[e]=1)}function $dfg(e,g,i,k){if(!(g=_$HY.sg&&_$HY.sg[e]))return;for(i=0;i<g.length;i++)if(_$HY.sc&&_$HY.sc[g[i]]>0)return;for(i=0;i<g.length;i++)k=g[i],delete _$HY.sg[k],$df(k)}function $dfc(e){if(--_$HY.sc[e]<=0){delete _$HY.sc[e],_$HY.sg&&_$HY.sg[e]?$dfg(e):!(_$HY.sd&&_$HY.sd[e])&&$df(e);_$HY.sd&&delete _$HY.sd[e]}}function $dfj(e,i,n){for(i=0;i<e.length;i++)if(_$HY.sc&&_$HY.sc[e[i]]>0){for(n=0;n<e.length;n++)(_$HY.sg=_$HY.sg||{})[e[n]]=e;return}for(i=0;i<e.length;i++)$df(e[i])}function $dfe(e,t,k){(t=e.target)&&t.getAttribute&&!t._$dc&&(k=t.getAttribute("data-dfc"))&&(t._$dc=1,$dfc(k))}document.addEventListener("load",$dfe,!0);document.addEventListener("error",$dfe,!0)`;
1845
+ const REPLACE_SCRIPT = `function $df(e){return _$HY.f?_$HY.f(e):$dfr(e)}function $dfr(e,n,o,t){if(!(n=document.getElementById(e)))return 0;if(!(o=document.getElementById("pl-"+e)))return(_$HY.dq=_$HY.dq||{})[e]=1,0;for(;o&&(8!==o.nodeType||o.nodeValue!=="pl-"+e);)t=o.nextSibling,o.remove(),o=t;t=o.parentNode,(_$HY.v=_$HY.v||{})[e]=n.hasAttribute("e")?[...n.content.childNodes]:1,o.replaceWith(n.content),n.remove(),_$HY.fe(e,t),_$HY.hp&&_$HY.hp[e]&&($dh(_$HY.hp[e]),delete _$HY.hp[e]),$dfd();return 1}function $dfl(e,o,n){if(!(o=document.getElementById("pl-"+e)))return(_$HY.dlq=_$HY.dlq||{})[e]=1,0;if(o._$fl)return 1;for(n=o.nextSibling;n;){if(8===n.nodeType&&n.nodeValue==="pl-"+e){o.parentNode&&o.parentNode.insertBefore(o.content.cloneNode(!0),n),o._$fl=1,$dfd();return 1}n=n.nextSibling}return 0}function $dflj(e,i){for(i=0;i<e.length;i++)$dfl(e[i])}function $dfd(e,i){if(e=_$HY.dq){_$HY.dq=0;for(i in e)$df(i)}if(e=_$HY.dlq){_$HY.dlq=0;for(i in e)$dfl(i)}}function $dfs(e,c,d){(_$HY.sc=_$HY.sc||{})[e]=c,d&&((_$HY.sd=_$HY.sd||{})[e]=1)}function $dfg(e,g,i,k){if(!(g=_$HY.sg&&_$HY.sg[e]))return;for(i=0;i<g.length;i++)if(_$HY.sc&&_$HY.sc[g[i]]>0)return;for(i=0;i<g.length;i++)k=g[i],delete _$HY.sg[k],$df(k)}function $dfc(e){if(--_$HY.sc[e]<=0){delete _$HY.sc[e],_$HY.sg&&_$HY.sg[e]?$dfg(e):!(_$HY.sd&&_$HY.sd[e])&&$df(e);_$HY.sd&&delete _$HY.sd[e]}}function $dfj(e,i,n){for(i=0;i<e.length;i++)if(_$HY.sc&&_$HY.sc[e[i]]>0){for(n=0;n<e.length;n++)(_$HY.sg=_$HY.sg||{})[e[n]]=e;return}for(i=0;i<e.length;i++)$df(e[i])}function $dfe(e,t,k){(t=e.target)&&t.getAttribute&&!t._$dc&&(k=t.getAttribute("data-dfc"))&&(t._$dc=1,$dfc(k))}document.addEventListener("load",$dfe,!0);document.addEventListener("error",$dfe,!0)`;
1763
1846
  const HEAD_SCRIPT = `function $dha(o,i,e,n){for(i=0;i<o.length;i++)e=o[i],"t"==e[0]?((n=document.querySelector("title"))?n.hasAttribute("data-dh")||n.setAttribute("data-dhf",n.textContent):(n=document.createElement("title"),document.head.appendChild(n)),n.textContent=e[1],n.setAttribute("data-dh","title")):"r"==e[0]?$dhr(e[1]):(n=document.createElement(e[2]),Object.keys(e[3]).forEach(function(a){n.setAttribute(a,e[3][a])}),null!=e[4]&&(n.textContent=e[4]),n.setAttribute("data-dh",e[1]),document.head.appendChild(n))}function $dhr(v,l,i){for(l=document.head.querySelectorAll("[data-dh]"),i=0;i<l.length;i++)l[i].getAttribute("data-dh")==v&&l[i].remove()}function $dh(o){_$HY.h?_$HY.h(o):$dha(o)}`;
1764
1847
  function renderToString(code, options = {}) {
1765
1848
  const {
@@ -2030,7 +2113,6 @@ function renderToStream(code, options = {}) {
2030
2113
  const blockingPromises = new Set();
2031
2114
  const canBatchStubs = !options.serializer && !options.sink;
2032
2115
  let stubBatch = null;
2033
- const STUB_BATCH_KEY = "$B";
2034
2116
  let stableOrder = false;
2035
2117
  let parkedWrites = null;
2036
2118
  const byKey = (a, b) => a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0;
@@ -2045,19 +2127,10 @@ function renderToStream(code, options = {}) {
2045
2127
  };
2046
2128
  const flushStubBatch = () => {
2047
2129
  if (!stubBatch) return;
2048
- const batch = stubBatch;
2130
+ const entries = [...stubBatch];
2049
2131
  stubBatch = null;
2050
- if (batch.size === 1) {
2051
- const [id, p] = batch.entries().next().value;
2052
- serializer.write(id, p);
2053
- return;
2054
- }
2055
- const obj = {};
2056
- const entries = [...batch];
2057
2132
  if (stableOrder) entries.sort(byKey);
2058
- for (const [id, p] of entries) obj[id] = p;
2059
- serializer.write(STUB_BATCH_KEY, obj);
2060
- pushTask(`(b=>{for(var k in b)_$HY.r[k]=b[k];delete _$HY.r["${STUB_BATCH_KEY}"]})(_$HY.r["${STUB_BATCH_KEY}"])`);
2133
+ for (let i = 0; i < entries.length; i++) serializer.write(entries[i][0], entries[i][1]);
2061
2134
  };
2062
2135
  let headerEmitted = false;
2063
2136
  const pushTask = task => {
@@ -2094,6 +2167,7 @@ function renderToStream(code, options = {}) {
2094
2167
  fragment(key, value, meta) {
2095
2168
  const deferActivation = !!meta.revealGroup;
2096
2169
  const styles = meta.styles;
2170
+ const tpl = `<template id="${key}"${meta.error ? " e" : ""}>${value}</template>`;
2097
2171
  for (let i = 0; i < styles.inline.length; i++) {
2098
2172
  buffer.write(renderInlineStyle(styles.inline[i], nonce));
2099
2173
  }
@@ -2101,12 +2175,12 @@ function renderToStream(code, options = {}) {
2101
2175
  const styleAttr = nonceAttr(nonce, "style");
2102
2176
  emitTask(`$dfs("${key}",${styles.links.length},${deferActivation ? 1 : 0})`);
2103
2177
  writeTasks();
2104
- buffer.write(`<template id="${key}">${value}</template>`);
2178
+ buffer.write(tpl);
2105
2179
  for (const entry of styles.links) {
2106
2180
  buffer.write(typeof entry === "string" ? `<link rel="stylesheet" href="${entry}"${styleAttr} data-dfc="${key}">` : `<link${entry.attrHtml}${styleAttr} data-dfc="${key}">`);
2107
2181
  }
2108
2182
  } else {
2109
- buffer.write(`<template id="${key}">${value}</template>`);
2183
+ buffer.write(tpl);
2110
2184
  if (!deferActivation) {
2111
2185
  emitTask(`$df("${key}")`);
2112
2186
  }
@@ -2219,22 +2293,26 @@ function renderToStream(code, options = {}) {
2219
2293
  return raced;
2220
2294
  };
2221
2295
  const abandonSubtree = (key, error) => {
2296
+ if (key == null) return;
2222
2297
  let fragments = 0;
2223
2298
  let serialized = 0;
2224
2299
  for (const [k, entry] of registry) {
2225
- if (k.length > key.length && k.startsWith(key)) {
2300
+ if (k != null && k.length > key.length && k.startsWith(key)) {
2226
2301
  registry.delete(k);
2227
2302
  entry.resolve();
2228
2303
  fragments++;
2229
2304
  }
2230
2305
  }
2306
+ const unregister = [];
2231
2307
  for (const [id, settle] of pendingSerialized) {
2232
2308
  if (id.startsWith(key)) {
2233
2309
  pendingSerialized.delete(id);
2234
2310
  settle();
2235
2311
  serialized++;
2312
+ if (!stubBatch?.delete(id)) unregister.push(`delete _$HY.r[${JSON.stringify(id)}]`);
2236
2313
  }
2237
2314
  }
2315
+ if (unregister.length && !options.sink) pushTask(unregister.join(";"));
2238
2316
  if ((fragments || serialized)) emitFinding({
2239
2317
  code: "SSR_SUBTREE_ABANDONED",
2240
2318
  kind: "ssr",
@@ -2461,7 +2539,7 @@ function renderToStream(code, options = {}) {
2461
2539
  const styles = collectStreamStyles(key, tracking, headStyles);
2462
2540
  const headOps = error ? null : flushHeadFragment(headRegistry, key, nonce);
2463
2541
  if (headOps) emitHeadOps(key, headOps);
2464
- sink.fragment(key, resolveSSRSelectValues(value !== undefined ? value : " "), {
2542
+ sink.fragment(key, resolveSSRSelectValues(value !== undefined ? value : error ? "" : " "), {
2465
2543
  styles,
2466
2544
  revealGroup,
2467
2545
  error: error ? ssrSanitizeError(error, null, undefined, undefined, errorScope) : error
@@ -3032,11 +3110,19 @@ function createLiveHoles(sink, scoped, settled, errorScope) {
3032
3110
  }
3033
3111
  function rowOps(last, rows) {
3034
3112
  const ops = [];
3035
- for (let i = 0; i < rows.length; i++) if (i >= last.length || rows[i] !== last[i]) ops.push([i, rows[i]]);
3036
- return ops.length < rows.length ? {
3113
+ const html = [];
3114
+ for (let i = 0; i < rows.length; i++) if (i >= last.length || rows[i] !== last[i]) {
3115
+ const patch = i < last.length ? rowPatch(last[i], rows[i]) : null;
3116
+ ops.push(patch ? [i, patch.p, patch.h] : [i, rows[i]]);
3117
+ html.push(rows[i]);
3118
+ }
3119
+ if (ops.length >= rows.length) return null;
3120
+ return Object.defineProperty({
3037
3121
  n: rows.length,
3038
3122
  r: ops
3039
- } : null;
3123
+ }, ROW_HTML, {
3124
+ value: html
3125
+ });
3040
3126
  }
3041
3127
  function resolveHoleValue(hole) {
3042
3128
  const result = {
@@ -3405,6 +3491,76 @@ function attrNames(text) {
3405
3491
  while (m = re.exec(text)) names.push(m[1]);
3406
3492
  return names;
3407
3493
  }
3494
+ const ROW_HTML = Symbol.for("solid.rowHtml");
3495
+ const ROW_TOKEN = /<(\/?)([a-zA-Z][^\s/>]*)((?:[^>"']|"[^"]*"|'[^']*')*)>|<!--([\s\S]*?)-->/g;
3496
+ const ROW_VOID = /^(?:area|base|br|col|embed|hr|img|input|link|meta|source|track|wbr)$/i;
3497
+ const ROW_FOREIGN = /^(?:svg|math)$/i;
3498
+ const ROW_RAW = /^(?:script|style|textarea|title|template)$/i;
3499
+ const ROW_KEY = /\s_key(?:="[^"]*")?(?![^\s/])/;
3500
+ const rowKey = attrs => (ROW_KEY.exec(attrs) || [""])[0];
3501
+ const ROW_ENTITY = /&(?:amp|lt|gt|quot|#39);/g;
3502
+ const ROW_ENTITIES = {
3503
+ "&amp;": "&",
3504
+ "&lt;": "<",
3505
+ "&gt;": ">",
3506
+ "&quot;": '"',
3507
+ "&#39;": "'"
3508
+ };
3509
+ function rowText(text) {
3510
+ if (!text.includes("&")) return text;
3511
+ if (text.replace(ROW_ENTITY, "").includes("&")) return null;
3512
+ return text.replace(ROW_ENTITY, e => ROW_ENTITIES[e]);
3513
+ }
3514
+ const ROW_HASH_SEED = 0x811c9dc5;
3515
+ function rowShape(h, tag, parent) {
3516
+ for (let i = 0; i < tag.length; i++) h = Math.imul(h ^ (tag.charCodeAt(i) | 32), 16777619);
3517
+ return Math.imul(h ^ parent + 2, 16777619);
3518
+ }
3519
+ function rowPatch(a, b) {
3520
+ const ra = new RegExp(ROW_TOKEN);
3521
+ const rb = new RegExp(ROW_TOKEN);
3522
+ const p = [];
3523
+ const stack = [[-1, 0, false]];
3524
+ let el = -1;
3525
+ let h = ROW_HASH_SEED;
3526
+ let ia = 0;
3527
+ let ib = 0;
3528
+ for (;;) {
3529
+ const ma = ra.exec(a);
3530
+ const mb = rb.exec(b);
3531
+ const ta = a.slice(ia, ma ? ma.index : a.length);
3532
+ const tb = b.slice(ib, mb ? mb.index : b.length);
3533
+ const top = stack[stack.length - 1];
3534
+ if (ta !== tb) {
3535
+ const text = rowText(tb);
3536
+ if (text === null) return null;
3537
+ p.push([top[0], top[1], text]);
3538
+ }
3539
+ if (!ma || !mb) return !ma && !mb ? {
3540
+ p,
3541
+ h: h >>> 0
3542
+ } : null;
3543
+ ia = ra.lastIndex;
3544
+ ib = rb.lastIndex;
3545
+ if (ma[4] !== undefined || mb[4] !== undefined) {
3546
+ if (ma[4] !== mb[4]) return null;
3547
+ top[1]++;
3548
+ } else if (ma[1] !== mb[1] || ma[2] !== mb[2]) return null;else if (ROW_RAW.test(ma[2])) return null;else if (ma[1]) {
3549
+ if (stack.length === 1) return null;
3550
+ stack.pop();
3551
+ } else {
3552
+ el++;
3553
+ h = rowShape(h, ma[2], top[0]);
3554
+ top[1]++;
3555
+ const foreign = top[2] || ROW_FOREIGN.test(ma[2]);
3556
+ if (ma[3] !== mb[3]) {
3557
+ if (foreign || rowKey(ma[3]) !== rowKey(mb[3])) return null;
3558
+ p.push([el, mb[3].replace(/\/$/, "")]);
3559
+ }
3560
+ if (!ROW_VOID.test(ma[2]) && !ma[3].endsWith("/")) stack.push([el, 0, foreign]);
3561
+ }
3562
+ }
3563
+ }
3408
3564
  function ssr(t) {
3409
3565
  const len = arguments.length;
3410
3566
  if (len === 1) return {
package/dist/server.js CHANGED
@@ -2,7 +2,7 @@ import { createRenderEffect, createMemo, getOwner, createRoot, $PROXY, onCleanup
2
2
  export { Errored, For, Hydration, Loading, Match, NoHydration, Repeat, Reveal, Show, Switch, createComponent, dynamicComponent, getOwner, merge as mergeProps, untrack } from 'solid-js';
3
3
  import { sharedConfig, ssrSanitizeError, beginEdgesRender, reportServerError, viewOf, sourceOwners, ssrHandleError, inServerComponentScope, creationStamp, dynamicCore, getProjectionTrace, shareAsyncIterable } from 'solid-js/internal';
4
4
  export { ssrScope as scope } from 'solid-js/internal';
5
- import { createStream, Serializer, getCrossReferenceHeader, Feature } from 'seroval';
5
+ import { createStream, Feature, getCrossReferenceHeader, crossSerializeStream } from 'seroval';
6
6
  import { AbortSignalPlugin, CustomEventPlugin, DOMExceptionPlugin, EventPlugin, FormDataPlugin, HeadersPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLSearchParamsPlugin, URLPlugin } from 'seroval-plugins/web';
7
7
 
8
8
  const DOMWithState = {
@@ -447,15 +447,92 @@ function resolveSerializerPlugins(customPlugins) {
447
447
  }
448
448
 
449
449
  const defaultDisabledFeatures = () => Feature.AggregateError | Feature.BigIntTypedArray;
450
- const serializeOnlyDisabledFeatures = serializeErrorStacks => (serializeErrorStacks === undefined ? process.env.NODE_ENV === "development" : serializeErrorStacks) ? 0 : Feature.ErrorPrototypeStack;
450
+ const serializeOnlyDisabledFeatures = serializeErrorStacks => (process.env.NODE_ENV === "development" ) ? 0 : Feature.ErrorPrototypeStack;
451
451
  const HYDRATION_GLOBAL = "_$HY.r";
452
- function createSerializer(options) {
453
- return new Serializer({
454
- ...options,
455
- plugins: resolveSerializerPlugins(options.plugins),
456
- disabledFeatures: (options.disabledFeatures === undefined ? defaultDisabledFeatures() : options.disabledFeatures) | serializeOnlyDisabledFeatures(options.serializeErrorStacks)
457
- });
452
+ function createKeyedSerializer(serialize, onError, onDone) {
453
+ const cancels = new Set();
454
+ let pendingWrites = 0;
455
+ let flushed = false;
456
+ let done = false;
457
+ let closed = false;
458
+ const maybeDone = () => {
459
+ if (flushed && pendingWrites === 0 && !done) {
460
+ done = true;
461
+ onDone && onDone();
462
+ }
463
+ };
464
+ return {
465
+ write(key, value) {
466
+ if (flushed) return;
467
+ pendingWrites++;
468
+ let settled = false;
469
+ let failed = false;
470
+ let started = false;
471
+ let cancel = null;
472
+ const settle = () => {
473
+ if (settled) return;
474
+ settled = true;
475
+ if (cancel) cancels.delete(cancel);
476
+ pendingWrites--;
477
+ maybeDone();
478
+ };
479
+ let stream;
480
+ try {
481
+ stream = serialize(key, value, {
482
+ onError: onError && (err => {
483
+ failed = true;
484
+ onError(err);
485
+ if (cancel) {
486
+ cancel();
487
+ settle();
488
+ }
489
+ }),
490
+ onDone: settle
491
+ }, initial => {
492
+ if (initial) started = true;
493
+ return !closed;
494
+ });
495
+ } catch (err) {
496
+ settle();
497
+ throw err;
498
+ }
499
+ if (settled) return;
500
+ if (closed || failed) {
501
+ stream();
502
+ settle();
503
+ return;
504
+ }
505
+ cancel = stream;
506
+ cancels.add(cancel);
507
+ },
508
+ flush() {
509
+ flushed = true;
510
+ maybeDone();
511
+ },
512
+ close() {
513
+ closed = flushed = true;
514
+ for (const cancel of cancels) cancel();
515
+ cancels.clear();
516
+ if (!done) {
517
+ done = true;
518
+ onDone && onDone();
519
+ }
520
+ }
521
+ };
458
522
  }
523
+ const KEY_ESCAPES = {
524
+ '"': '\\"',
525
+ "\\": "\\\\",
526
+ "\n": "\\n",
527
+ "\r": "\\r",
528
+ "\b": "\\b",
529
+ "\t": "\\t",
530
+ "\f": "\\f",
531
+ "<": "\\x3C",
532
+ "\u2028": "\\u2028",
533
+ "\u2029": "\\u2029"
534
+ };
535
+ const serializeKey = key => ("" + key).replace(/["\\\n\r\b\t\f<\u2028\u2029]/g, c => KEY_ESCAPES[c]);
459
536
  function createHydrationSerializer({
460
537
  onData,
461
538
  onDone,
@@ -463,14 +540,20 @@ function createHydrationSerializer({
463
540
  onError,
464
541
  plugins
465
542
  }) {
466
- return createSerializer({
543
+ const resolvedPlugins = resolveSerializerPlugins(plugins);
544
+ const disabledFeatures = defaultDisabledFeatures() | serializeOnlyDisabledFeatures();
545
+ const refs = new Map();
546
+ return createKeyedSerializer((key, value, callbacks, live) => crossSerializeStream(value, {
467
547
  scopeId,
468
- plugins,
469
- globalIdentifier: HYDRATION_GLOBAL,
470
- onData,
471
- onDone,
472
- onError
473
- });
548
+ refs,
549
+ plugins: resolvedPlugins,
550
+ disabledFeatures,
551
+ onSerialize(data, initial) {
552
+ if (!live(initial)) return;
553
+ onData(initial ? HYDRATION_GLOBAL + '["' + serializeKey(key) + '"]=' + data : data);
554
+ },
555
+ ...callbacks
556
+ }), onError, onDone);
474
557
  }
475
558
  function getLocalHeaderScript(id) {
476
559
  return getCrossReferenceHeader(id) + ";";
@@ -1494,7 +1577,7 @@ function isAttrName(prop) {
1494
1577
  if (safeAttrNames.size < 512) safeAttrNames.add(prop);
1495
1578
  return true;
1496
1579
  }
1497
- const REPLACE_SCRIPT = `function $df(e){return _$HY.f?_$HY.f(e):$dfr(e)}function $dfr(e,n,o,t){if(!(n=document.getElementById(e)))return 0;if(!(o=document.getElementById("pl-"+e)))return(_$HY.dq=_$HY.dq||{})[e]=1,0;for(;o&&(8!==o.nodeType||o.nodeValue!=="pl-"+e);)t=o.nextSibling,o.remove(),o=t;t=o.parentNode,o.replaceWith(n.content),n.remove(),(_$HY.v=_$HY.v||{})[e]=1,_$HY.fe(e,t),_$HY.hp&&_$HY.hp[e]&&($dh(_$HY.hp[e]),delete _$HY.hp[e]),$dfd();return 1}function $dfl(e,o,n){if(!(o=document.getElementById("pl-"+e)))return(_$HY.dlq=_$HY.dlq||{})[e]=1,0;if(o._$fl)return 1;for(n=o.nextSibling;n;){if(8===n.nodeType&&n.nodeValue==="pl-"+e){o.parentNode&&o.parentNode.insertBefore(o.content.cloneNode(!0),n),o._$fl=1,$dfd();return 1}n=n.nextSibling}return 0}function $dflj(e,i){for(i=0;i<e.length;i++)$dfl(e[i])}function $dfd(e,i){if(e=_$HY.dq){_$HY.dq=0;for(i in e)$df(i)}if(e=_$HY.dlq){_$HY.dlq=0;for(i in e)$dfl(i)}}function $dfs(e,c,d){(_$HY.sc=_$HY.sc||{})[e]=c,d&&((_$HY.sd=_$HY.sd||{})[e]=1)}function $dfg(e,g,i,k){if(!(g=_$HY.sg&&_$HY.sg[e]))return;for(i=0;i<g.length;i++)if(_$HY.sc&&_$HY.sc[g[i]]>0)return;for(i=0;i<g.length;i++)k=g[i],delete _$HY.sg[k],$df(k)}function $dfc(e){if(--_$HY.sc[e]<=0){delete _$HY.sc[e],_$HY.sg&&_$HY.sg[e]?$dfg(e):!(_$HY.sd&&_$HY.sd[e])&&$df(e);_$HY.sd&&delete _$HY.sd[e]}}function $dfj(e,i,n){for(i=0;i<e.length;i++)if(_$HY.sc&&_$HY.sc[e[i]]>0){for(n=0;n<e.length;n++)(_$HY.sg=_$HY.sg||{})[e[n]]=e;return}for(i=0;i<e.length;i++)$df(e[i])}function $dfe(e,t,k){(t=e.target)&&t.getAttribute&&!t._$dc&&(k=t.getAttribute("data-dfc"))&&(t._$dc=1,$dfc(k))}document.addEventListener("load",$dfe,!0);document.addEventListener("error",$dfe,!0)`;
1580
+ const REPLACE_SCRIPT = `function $df(e){return _$HY.f?_$HY.f(e):$dfr(e)}function $dfr(e,n,o,t){if(!(n=document.getElementById(e)))return 0;if(!(o=document.getElementById("pl-"+e)))return(_$HY.dq=_$HY.dq||{})[e]=1,0;for(;o&&(8!==o.nodeType||o.nodeValue!=="pl-"+e);)t=o.nextSibling,o.remove(),o=t;t=o.parentNode,(_$HY.v=_$HY.v||{})[e]=n.hasAttribute("e")?[...n.content.childNodes]:1,o.replaceWith(n.content),n.remove(),_$HY.fe(e,t),_$HY.hp&&_$HY.hp[e]&&($dh(_$HY.hp[e]),delete _$HY.hp[e]),$dfd();return 1}function $dfl(e,o,n){if(!(o=document.getElementById("pl-"+e)))return(_$HY.dlq=_$HY.dlq||{})[e]=1,0;if(o._$fl)return 1;for(n=o.nextSibling;n;){if(8===n.nodeType&&n.nodeValue==="pl-"+e){o.parentNode&&o.parentNode.insertBefore(o.content.cloneNode(!0),n),o._$fl=1,$dfd();return 1}n=n.nextSibling}return 0}function $dflj(e,i){for(i=0;i<e.length;i++)$dfl(e[i])}function $dfd(e,i){if(e=_$HY.dq){_$HY.dq=0;for(i in e)$df(i)}if(e=_$HY.dlq){_$HY.dlq=0;for(i in e)$dfl(i)}}function $dfs(e,c,d){(_$HY.sc=_$HY.sc||{})[e]=c,d&&((_$HY.sd=_$HY.sd||{})[e]=1)}function $dfg(e,g,i,k){if(!(g=_$HY.sg&&_$HY.sg[e]))return;for(i=0;i<g.length;i++)if(_$HY.sc&&_$HY.sc[g[i]]>0)return;for(i=0;i<g.length;i++)k=g[i],delete _$HY.sg[k],$df(k)}function $dfc(e){if(--_$HY.sc[e]<=0){delete _$HY.sc[e],_$HY.sg&&_$HY.sg[e]?$dfg(e):!(_$HY.sd&&_$HY.sd[e])&&$df(e);_$HY.sd&&delete _$HY.sd[e]}}function $dfj(e,i,n){for(i=0;i<e.length;i++)if(_$HY.sc&&_$HY.sc[e[i]]>0){for(n=0;n<e.length;n++)(_$HY.sg=_$HY.sg||{})[e[n]]=e;return}for(i=0;i<e.length;i++)$df(e[i])}function $dfe(e,t,k){(t=e.target)&&t.getAttribute&&!t._$dc&&(k=t.getAttribute("data-dfc"))&&(t._$dc=1,$dfc(k))}document.addEventListener("load",$dfe,!0);document.addEventListener("error",$dfe,!0)`;
1498
1581
  const HEAD_SCRIPT = `function $dha(o,i,e,n){for(i=0;i<o.length;i++)e=o[i],"t"==e[0]?((n=document.querySelector("title"))?n.hasAttribute("data-dh")||n.setAttribute("data-dhf",n.textContent):(n=document.createElement("title"),document.head.appendChild(n)),n.textContent=e[1],n.setAttribute("data-dh","title")):"r"==e[0]?$dhr(e[1]):(n=document.createElement(e[2]),Object.keys(e[3]).forEach(function(a){n.setAttribute(a,e[3][a])}),null!=e[4]&&(n.textContent=e[4]),n.setAttribute("data-dh",e[1]),document.head.appendChild(n))}function $dhr(v,l,i){for(l=document.head.querySelectorAll("[data-dh]"),i=0;i<l.length;i++)l[i].getAttribute("data-dh")==v&&l[i].remove()}function $dh(o){_$HY.h?_$HY.h(o):$dha(o)}`;
1499
1582
  function renderToString(code, options = {}) {
1500
1583
  const {
@@ -1744,7 +1827,6 @@ function renderToStream(code, options = {}) {
1744
1827
  const blockingPromises = new Set();
1745
1828
  const canBatchStubs = !options.serializer && !options.sink;
1746
1829
  let stubBatch = null;
1747
- const STUB_BATCH_KEY = "$B";
1748
1830
  let stableOrder = false;
1749
1831
  let parkedWrites = null;
1750
1832
  const byKey = (a, b) => a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0;
@@ -1759,19 +1841,10 @@ function renderToStream(code, options = {}) {
1759
1841
  };
1760
1842
  const flushStubBatch = () => {
1761
1843
  if (!stubBatch) return;
1762
- const batch = stubBatch;
1844
+ const entries = [...stubBatch];
1763
1845
  stubBatch = null;
1764
- if (batch.size === 1) {
1765
- const [id, p] = batch.entries().next().value;
1766
- serializer.write(id, p);
1767
- return;
1768
- }
1769
- const obj = {};
1770
- const entries = [...batch];
1771
1846
  if (stableOrder) entries.sort(byKey);
1772
- for (const [id, p] of entries) obj[id] = p;
1773
- serializer.write(STUB_BATCH_KEY, obj);
1774
- pushTask(`(b=>{for(var k in b)_$HY.r[k]=b[k];delete _$HY.r["${STUB_BATCH_KEY}"]})(_$HY.r["${STUB_BATCH_KEY}"])`);
1847
+ for (let i = 0; i < entries.length; i++) serializer.write(entries[i][0], entries[i][1]);
1775
1848
  };
1776
1849
  let headerEmitted = false;
1777
1850
  const pushTask = task => {
@@ -1808,6 +1881,7 @@ function renderToStream(code, options = {}) {
1808
1881
  fragment(key, value, meta) {
1809
1882
  const deferActivation = !!meta.revealGroup;
1810
1883
  const styles = meta.styles;
1884
+ const tpl = `<template id="${key}"${meta.error ? " e" : ""}>${value}</template>`;
1811
1885
  for (let i = 0; i < styles.inline.length; i++) {
1812
1886
  buffer.write(renderInlineStyle(styles.inline[i], nonce));
1813
1887
  }
@@ -1815,12 +1889,12 @@ function renderToStream(code, options = {}) {
1815
1889
  const styleAttr = nonceAttr(nonce, "style");
1816
1890
  emitTask(`$dfs("${key}",${styles.links.length},${deferActivation ? 1 : 0})`);
1817
1891
  writeTasks();
1818
- buffer.write(`<template id="${key}">${value}</template>`);
1892
+ buffer.write(tpl);
1819
1893
  for (const entry of styles.links) {
1820
1894
  buffer.write(typeof entry === "string" ? `<link rel="stylesheet" href="${entry}"${styleAttr} data-dfc="${key}">` : `<link${entry.attrHtml}${styleAttr} data-dfc="${key}">`);
1821
1895
  }
1822
1896
  } else {
1823
- buffer.write(`<template id="${key}">${value}</template>`);
1897
+ buffer.write(tpl);
1824
1898
  if (!deferActivation) {
1825
1899
  emitTask(`$df("${key}")`);
1826
1900
  }
@@ -1933,18 +2007,22 @@ function renderToStream(code, options = {}) {
1933
2007
  return raced;
1934
2008
  };
1935
2009
  const abandonSubtree = (key, error) => {
2010
+ if (key == null) return;
1936
2011
  for (const [k, entry] of registry) {
1937
- if (k.length > key.length && k.startsWith(key)) {
2012
+ if (k != null && k.length > key.length && k.startsWith(key)) {
1938
2013
  registry.delete(k);
1939
2014
  entry.resolve();
1940
2015
  }
1941
2016
  }
2017
+ const unregister = [];
1942
2018
  for (const [id, settle] of pendingSerialized) {
1943
2019
  if (id.startsWith(key)) {
1944
2020
  pendingSerialized.delete(id);
1945
2021
  settle();
2022
+ if (!stubBatch?.delete(id)) unregister.push(`delete _$HY.r[${JSON.stringify(id)}]`);
1946
2023
  }
1947
2024
  }
2025
+ if (unregister.length && !options.sink) pushTask(unregister.join(";"));
1948
2026
  };
1949
2027
  const writeTasks = () => {
1950
2028
  if (tasks.length && !completed && firstFlushed) {
@@ -2159,7 +2237,7 @@ function renderToStream(code, options = {}) {
2159
2237
  const styles = collectStreamStyles(key, tracking, headStyles);
2160
2238
  const headOps = error ? null : flushHeadFragment(headRegistry, key, nonce);
2161
2239
  if (headOps) emitHeadOps(key, headOps);
2162
- sink.fragment(key, resolveSSRSelectValues(value !== undefined ? value : " "), {
2240
+ sink.fragment(key, resolveSSRSelectValues(value !== undefined ? value : error ? "" : " "), {
2163
2241
  styles,
2164
2242
  revealGroup,
2165
2243
  error: error ? ssrSanitizeError(error, null, undefined, undefined, errorScope) : error
@@ -2719,11 +2797,19 @@ function createLiveHoles(sink, scoped, settled, errorScope) {
2719
2797
  }
2720
2798
  function rowOps(last, rows) {
2721
2799
  const ops = [];
2722
- for (let i = 0; i < rows.length; i++) if (i >= last.length || rows[i] !== last[i]) ops.push([i, rows[i]]);
2723
- return ops.length < rows.length ? {
2800
+ const html = [];
2801
+ for (let i = 0; i < rows.length; i++) if (i >= last.length || rows[i] !== last[i]) {
2802
+ const patch = i < last.length ? rowPatch(last[i], rows[i]) : null;
2803
+ ops.push(patch ? [i, patch.p] : [i, rows[i]]);
2804
+ html.push(rows[i]);
2805
+ }
2806
+ if (ops.length >= rows.length) return null;
2807
+ return Object.defineProperty({
2724
2808
  n: rows.length,
2725
2809
  r: ops
2726
- } : null;
2810
+ }, ROW_HTML, {
2811
+ value: html
2812
+ });
2727
2813
  }
2728
2814
  function resolveHoleValue(hole) {
2729
2815
  const result = {
@@ -3089,6 +3175,71 @@ function attrNames(text) {
3089
3175
  while (m = re.exec(text)) names.push(m[1]);
3090
3176
  return names;
3091
3177
  }
3178
+ const ROW_HTML = Symbol.for("solid.rowHtml");
3179
+ const ROW_TOKEN = /<(\/?)([a-zA-Z][^\s/>]*)((?:[^>"']|"[^"]*"|'[^']*')*)>|<!--([\s\S]*?)-->/g;
3180
+ const ROW_VOID = /^(?:area|base|br|col|embed|hr|img|input|link|meta|source|track|wbr)$/i;
3181
+ const ROW_FOREIGN = /^(?:svg|math)$/i;
3182
+ const ROW_RAW = /^(?:script|style|textarea|title|template)$/i;
3183
+ const ROW_KEY = /\s_key(?:="[^"]*")?(?![^\s/])/;
3184
+ const rowKey = attrs => (ROW_KEY.exec(attrs) || [""])[0];
3185
+ const ROW_ENTITY = /&(?:amp|lt|gt|quot|#39);/g;
3186
+ const ROW_ENTITIES = {
3187
+ "&amp;": "&",
3188
+ "&lt;": "<",
3189
+ "&gt;": ">",
3190
+ "&quot;": '"',
3191
+ "&#39;": "'"
3192
+ };
3193
+ function rowText(text) {
3194
+ if (!text.includes("&")) return text;
3195
+ if (text.replace(ROW_ENTITY, "").includes("&")) return null;
3196
+ return text.replace(ROW_ENTITY, e => ROW_ENTITIES[e]);
3197
+ }
3198
+ const ROW_HASH_SEED = 0x811c9dc5;
3199
+ function rowPatch(a, b) {
3200
+ const ra = new RegExp(ROW_TOKEN);
3201
+ const rb = new RegExp(ROW_TOKEN);
3202
+ const p = [];
3203
+ const stack = [[-1, 0, false]];
3204
+ let el = -1;
3205
+ let h = ROW_HASH_SEED;
3206
+ let ia = 0;
3207
+ let ib = 0;
3208
+ for (;;) {
3209
+ const ma = ra.exec(a);
3210
+ const mb = rb.exec(b);
3211
+ const ta = a.slice(ia, ma ? ma.index : a.length);
3212
+ const tb = b.slice(ib, mb ? mb.index : b.length);
3213
+ const top = stack[stack.length - 1];
3214
+ if (ta !== tb) {
3215
+ const text = rowText(tb);
3216
+ if (text === null) return null;
3217
+ p.push([top[0], top[1], text]);
3218
+ }
3219
+ if (!ma || !mb) return !ma && !mb ? {
3220
+ p,
3221
+ h: h >>> 0
3222
+ } : null;
3223
+ ia = ra.lastIndex;
3224
+ ib = rb.lastIndex;
3225
+ if (ma[4] !== undefined || mb[4] !== undefined) {
3226
+ if (ma[4] !== mb[4]) return null;
3227
+ top[1]++;
3228
+ } else if (ma[1] !== mb[1] || ma[2] !== mb[2]) return null;else if (ROW_RAW.test(ma[2])) return null;else if (ma[1]) {
3229
+ if (stack.length === 1) return null;
3230
+ stack.pop();
3231
+ } else {
3232
+ el++;
3233
+ top[1]++;
3234
+ const foreign = top[2] || ROW_FOREIGN.test(ma[2]);
3235
+ if (ma[3] !== mb[3]) {
3236
+ if (foreign || rowKey(ma[3]) !== rowKey(mb[3])) return null;
3237
+ p.push([el, mb[3].replace(/\/$/, "")]);
3238
+ }
3239
+ if (!ROW_VOID.test(ma[2]) && !ma[3].endsWith("/")) stack.push([el, 0, foreign]);
3240
+ }
3241
+ }
3242
+ }
3092
3243
  function ssr(t) {
3093
3244
  const len = arguments.length;
3094
3245
  if (len === 1) return {