@solidjs/web 2.0.0-rc.14 → 2.0.0-rc.15

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.
@@ -1,9 +1,9 @@
1
1
  import { sharedConfig, ssrSanitizeError, creationStamp, ssrHandleError, runInServerComponentScope, shareAsyncIterable, getProjectionTrace } from 'solid-js/internal';
2
- import { NoHydration, getOwner, runWithOwner, createMemo, Hydration, createOwner } from 'solid-js';
2
+ import { getOwner, NoHydration, runWithOwner, createMemo, Hydration, createOwner } from 'solid-js';
3
3
  import { isResponseEnvelope, renderToStream, createLiveHoles, CLAIMS_STREAM, CLAIMS_DOCUMENT, SLOT_FACE_DATA, SLOT_FACE_MARKUP, slotValue, SLOT_FACE_STREAM, isSlotValue } from '@solidjs/web';
4
4
  import { Feature, createStream, toCrossJSONStream } from 'seroval';
5
5
  import { AbortSignalPlugin, CustomEventPlugin, DOMExceptionPlugin, EventPlugin, FormDataPlugin, HeadersPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLSearchParamsPlugin, URLPlugin } from 'seroval-plugins/web';
6
- import { textDigest, frameAddress, getEventServerFunctionInvocation, armLiveBody, createEventChunk, createChunk, ChunkReader, serializeStream, guardFailures } from '@solidjs/web/server-functions/server';
6
+ import { frameAddress, getEventServerFunctionInvocation, ChunkReader, serializeStream, guardFailures, armLiveBody, createEventChunk, createChunk, textDigest } from '@solidjs/web/server-functions/server';
7
7
 
8
8
  const DESCEND = Symbol("descend");
9
9
  const CYCLE = {};
@@ -132,9 +132,7 @@ function toBorderForm(value, envelopeContainers) {
132
132
  }
133
133
  function isShareableIterable(value) {
134
134
  try {
135
- if (value.__SEROVAL_STREAM__ === true) return false;
136
- if (typeof ReadableStream !== "undefined" && value instanceof ReadableStream) return false;
137
- return typeof value[Symbol.asyncIterator] === "function";
135
+ return typeof value[Symbol.asyncIterator] === "function" && !(typeof ReadableStream !== "undefined" && value instanceof ReadableStream);
138
136
  } catch {
139
137
  return false;
140
138
  }
@@ -854,9 +852,6 @@ function createFrameSink(write, frame, have, hooks) {
854
852
  }
855
853
  };
856
854
  }
857
- function renderToFrameStream(code, options = {}) {
858
- return frameStream(() => code, options);
859
- }
860
855
  function renderServerComponent(component, options = {}) {
861
856
  return frameStream((sink, frame) => {
862
857
  const props = createSlotProps(sink, frame);
@@ -1336,6 +1331,36 @@ function frameElementOpen(id) {
1336
1331
  const escaped = sharedConfig.context ? sharedConfig.context.escape(String(id), true) : String(id);
1337
1332
  return `<${FRAME_TAG} ${FRAME_ID_ATTR}="${escaped}" style="display:contents">`;
1338
1333
  }
1334
+ const DOCUMENT_SITES = Symbol();
1335
+ function contextSites(ctx) {
1336
+ if (ctx[DOCUMENT_SITES]) return ctx[DOCUMENT_SITES];
1337
+ const proto = Object.getPrototypeOf(ctx);
1338
+ if (proto && proto[DOCUMENT_SITES]) return ctx[DOCUMENT_SITES] = proto[DOCUMENT_SITES];
1339
+ if (proto && typeof proto.escape === "function") return ctx[DOCUMENT_SITES] = contextSites(proto);
1340
+ return ctx[DOCUMENT_SITES] = {
1341
+ byOwner: new Map(),
1342
+ counts: Object.create(null)
1343
+ };
1344
+ }
1345
+ function mintSiteId(sites, fnId) {
1346
+ const n = sites.counts[fnId] || 0;
1347
+ sites.counts[fnId] = n + 1;
1348
+ return n === 0 ? fnId : fnId + "~" + (n + 1);
1349
+ }
1350
+ function documentSiteId(fnId) {
1351
+ const ctx = sharedConfig.context;
1352
+ if (!ctx) return fnId;
1353
+ const sites = contextSites(ctx);
1354
+ const owner = getOwner ? getOwner() : null;
1355
+ if (!owner) return mintSiteId(sites, fnId);
1356
+ let byFn = sites.byOwner.get(owner);
1357
+ if (!byFn) sites.byOwner.set(owner, byFn = new Map());
1358
+ const prev = byFn.get(fnId);
1359
+ if (prev) return prev;
1360
+ const siteId = mintSiteId(sites, fnId);
1361
+ byFn.set(fnId, siteId);
1362
+ return siteId;
1363
+ }
1339
1364
  const FRAME_ELEMENT_CLOSE = `</${FRAME_TAG}>`;
1340
1365
  function documentNeeds(name) {
1341
1366
  const ctx = sharedConfig.context;
@@ -1402,6 +1427,9 @@ function armDocumentLiveHoles(ctx) {
1402
1427
  ctx.serialize("sc:live", new ReadableStream({
1403
1428
  start(c) {
1404
1429
  channel = c;
1430
+ },
1431
+ cancel() {
1432
+ closed = true;
1405
1433
  }
1406
1434
  }));
1407
1435
  const push = op => {
@@ -1485,28 +1513,31 @@ function frameTransformDirectResult(value, {
1485
1513
  }) {
1486
1514
  if (typeof value !== "function") return value;
1487
1515
  const component = value;
1488
- const wrapped = props => [{
1489
- t: frameElementOpen(id)
1490
- },
1491
- serverOwned(() => {
1492
- const page = sharedConfig.context;
1493
- armDocumentLiveHoles(page);
1494
- if (tierUrls && page && page.live && !page.live.tierUrls) page.live.tierUrls = tierUrls;
1495
- const ctx = Object.create(page);
1496
- ctx.claims = CLAIMS_DOCUMENT;
1497
- ctx.frameId = id;
1498
- sharedConfig.context = ctx;
1499
- try {
1500
- const slotProps = createDocumentSlotProps(props, id);
1501
- const live = !!wrapped[LIVE_SOURCE];
1502
- if (live) documentNeeds("wire");
1503
- return serverComponentScope(() => component(slotProps), live);
1504
- } finally {
1505
- sharedConfig.context = page;
1506
- }
1507
- }), {
1508
- t: FRAME_ELEMENT_CLOSE
1509
- }];
1516
+ const wrapped = props => {
1517
+ const siteId = documentSiteId(id);
1518
+ return [{
1519
+ t: frameElementOpen(siteId)
1520
+ },
1521
+ serverOwned(() => {
1522
+ const page = sharedConfig.context;
1523
+ armDocumentLiveHoles(page);
1524
+ if (tierUrls && page && page.live && !page.live.tierUrls) page.live.tierUrls = tierUrls;
1525
+ const ctx = Object.create(page);
1526
+ ctx.claims = CLAIMS_DOCUMENT;
1527
+ ctx.frameId = siteId;
1528
+ sharedConfig.context = ctx;
1529
+ try {
1530
+ const slotProps = createDocumentSlotProps(props, siteId);
1531
+ const live = !!wrapped[LIVE_SOURCE];
1532
+ if (live) documentNeeds("wire");
1533
+ return serverComponentScope(() => component(slotProps), live);
1534
+ } finally {
1535
+ sharedConfig.context = page;
1536
+ }
1537
+ }), {
1538
+ t: FRAME_ELEMENT_CLOSE
1539
+ }];
1540
+ };
1510
1541
  wrapped[SERVER_COMPONENT] = id;
1511
1542
  wrapped[SERVER_COMPONENT_SOURCE] = component;
1512
1543
  wrapped[SERVER_COMPONENT_ADDRESS] = frameAddress(id, args);
@@ -1795,6 +1826,7 @@ function serverComponentResponse(component, options = {}, init = {}) {
1795
1826
  });
1796
1827
  let closed = false;
1797
1828
  let stopLive = null;
1829
+ let failed = false;
1798
1830
  const body = new ReadableStream({
1799
1831
  start(controller) {
1800
1832
  const end = () => {
@@ -1814,6 +1846,7 @@ function serverComponentResponse(component, options = {}, init = {}) {
1814
1846
  });
1815
1847
  stream.pipe({
1816
1848
  write(chunk) {
1849
+ if (chunk.type === "error" && !chunk.key) failed = true;
1817
1850
  if (closed) return;
1818
1851
  try {
1819
1852
  controller.enqueue(frame(JSON.stringify(chunk)));
@@ -1836,6 +1869,10 @@ function serverComponentResponse(component, options = {}, init = {}) {
1836
1869
  });
1837
1870
  }
1838
1871
  });
1872
+ if (failed) {
1873
+ headers.set("Cache-Control", "no-store");
1874
+ for (const name of [...headers.keys()]) if (name === "surrogate-control" || name.endsWith("cdn-cache-control")) headers.delete(name);
1875
+ }
1839
1876
  return new Response(body, {
1840
1877
  status: init.status || 200,
1841
1878
  headers
@@ -2052,4 +2089,4 @@ function asyncArg(value) {
2052
2089
  return value;
2053
2090
  }
2054
2091
 
2055
- export { FRAME_HAVE_BUDGET, FRAME_HAVE_HEADER, FRAME_STREAM_HEADER, FRAME_TIERS_HEADER, SERVER_COMPONENT_BOOTSTRAP, ServerComponentPlugin, asyncArg, createFrameSink, frameTransformDirectResult, frameTransformFlightResult, frameTransformResult, isFrameStreamResponse, renderServerComponent, renderToFrameStream, serverComponentResponse };
2092
+ export { FRAME_HAVE_BUDGET, FRAME_HAVE_HEADER, FRAME_STREAM_HEADER, FRAME_TIERS_HEADER, SERVER_COMPONENT_BOOTSTRAP, ServerComponentPlugin, asyncArg, frameTransformDirectResult, frameTransformFlightResult, frameTransformResult, isFrameStreamResponse };
@@ -9,6 +9,9 @@ const state = globalThis[STATE] || (globalThis[STATE] = {
9
9
  function setContainerTraceMaterializer(fn) {
10
10
  state.materializeTrace = fn;
11
11
  }
12
+ function isSerovalStream(value) {
13
+ return value.__SEROVAL_STREAM__ === true || typeof value.on === "function" && typeof value[Symbol.asyncIterator] !== "function";
14
+ }
12
15
  function materialize(marker, claiming) {
13
16
  let value = state.materialized.get(marker.$tr);
14
17
  if (value === undefined) {
@@ -21,7 +24,7 @@ function materialize(marker, claiming) {
21
24
  function isContainerTraceMarker(value) {
22
25
  if (value == null || typeof value !== "object" || value.$tr == null) return false;
23
26
  const tr = value.$tr;
24
- return tr.__SEROVAL_STREAM__ === true || typeof tr[Symbol.asyncIterator] === "function";
27
+ return isSerovalStream(tr) || typeof tr[Symbol.asyncIterator] === "function";
25
28
  }
26
29
  function reviveContainerTraces(value, claiming) {
27
30
  if (!state.materializeTrace || value == null || typeof value !== "object") return value;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@solidjs/web",
3
3
  "description": "Solid's web runtime: client rendering, hydration, SSR, and DOM-specific control flow (Portal, Dynamic).",
4
- "version": "2.0.0-rc.14",
4
+ "version": "2.0.0-rc.15",
5
5
  "author": "Ryan Carniato",
6
6
  "license": "MIT",
7
7
  "homepage": "https://solidjs.com",
@@ -381,11 +381,11 @@
381
381
  "test-types": "tsc --project tsconfig.test.json && tsc --project tsconfig.test.augment.json"
382
382
  },
383
383
  "dependencies": {
384
- "seroval": "~1.6.7",
385
- "seroval-plugins": "~1.6.7"
384
+ "seroval": "1.6.7",
385
+ "seroval-plugins": "1.6.7"
386
386
  },
387
387
  "peerDependencies": {
388
- "solid-js": "^2.0.0-rc.14"
388
+ "solid-js": "^2.0.0-rc.15"
389
389
  },
390
390
  "devDependencies": {
391
391
  "@codspeed/vitest-plugin": "^5.4.0",
@@ -1,4 +1,4 @@
1
- import { fromCrossJSON, Feature, createStream } from 'seroval';
1
+ import { fromCrossJSON, isStream, Feature, createStream } from 'seroval';
2
2
  import { AbortSignalPlugin, CustomEventPlugin, DOMExceptionPlugin, EventPlugin, FormDataPlugin, HeadersPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLSearchParamsPlugin, URLPlugin } from 'seroval-plugins/web';
3
3
 
4
4
  const STATE = Symbol.for("solid.container-trace-state");
@@ -104,7 +104,7 @@ function createJSONDeserializer(options) {
104
104
  function classify(value) {
105
105
  if (value === null || typeof value !== "object") return 0;
106
106
  try {
107
- if (value.__SEROVAL_STREAM__) return STREAM;
107
+ if (isStream(value) && typeof value.on === "function") return STREAM;
108
108
  if (typeof value.s === "function" && typeof value.f === "function" && value.p instanceof Promise) return RESOLVER;
109
109
  } catch {}
110
110
  return 0;
@@ -1,4 +1,4 @@
1
- import { fromCrossJSON, Feature, createStream, OpaqueReference as OpaqueReference$1, createPlugin as createPlugin$1, toCrossJSONStream, Serializer, getCrossReferenceHeader } from 'seroval';
1
+ import { isStream, fromCrossJSON, Feature, createStream, OpaqueReference as OpaqueReference$1, createPlugin as createPlugin$1, toCrossJSONStream, Serializer, getCrossReferenceHeader } from 'seroval';
2
2
  import { AbortSignalPlugin, CustomEventPlugin, DOMExceptionPlugin, EventPlugin, FormDataPlugin, HeadersPlugin, ReadableStreamPlugin, RequestPlugin, ResponsePlugin, URLSearchParamsPlugin, URLPlugin } from 'seroval-plugins/web';
3
3
 
4
4
  const STATE = Symbol.for("solid.container-trace-state");
@@ -104,7 +104,7 @@ function createJSONDeserializer(options) {
104
104
  function classify(value) {
105
105
  if (value === null || typeof value !== "object") return 0;
106
106
  try {
107
- if (value.__SEROVAL_STREAM__) return STREAM;
107
+ if (isStream(value) && typeof value.on === "function") return STREAM;
108
108
  if (typeof value.s === "function" && typeof value.f === "function" && value.p instanceof Promise) return RESOLVER;
109
109
  } catch {}
110
110
  return 0;
@@ -948,10 +948,11 @@ async function createRequest(base, id, options, meta) {
948
948
  headers
949
949
  };
950
950
  if (config.prepareRequest) {
951
- const prepared = await config.prepareRequest(init, {
951
+ let prepared = config.prepareRequest(init, {
952
952
  id,
953
953
  meta
954
954
  });
955
+ if (prepared && typeof prepared.then === "function") prepared = await prepared;
955
956
  if (prepared && prepared !== init) {
956
957
  if (typeof prepared !== "object") {
957
958
  throw new Error("prepareRequest must return (or mutate and return) the RequestInit it received; " + `it returned a ${typeof prepared}. Spread the init: ` + "init => ({ ...init, headers: { ...init.headers, ... } })");
@@ -833,10 +833,11 @@ async function createRequest(base, id, options, meta) {
833
833
  headers
834
834
  };
835
835
  if (config.prepareRequest) {
836
- const prepared = await config.prepareRequest(init, {
836
+ let prepared = config.prepareRequest(init, {
837
837
  id,
838
838
  meta
839
839
  });
840
+ if (prepared && typeof prepared.then === "function") prepared = await prepared;
840
841
  if (prepared && prepared !== init) {
841
842
  if (typeof prepared !== "object") {
842
843
  throw new Error("prepareRequest must return (or mutate and return) the RequestInit it received; " + `it returned a ${typeof prepared}. Spread the init: ` + "init => ({ ...init, headers: { ...init.headers, ... } })");
@@ -948,10 +948,11 @@ async function createRequest(base, id, options, meta) {
948
948
  headers
949
949
  };
950
950
  if (config.prepareRequest) {
951
- const prepared = await config.prepareRequest(init, {
951
+ let prepared = config.prepareRequest(init, {
952
952
  id,
953
953
  meta
954
954
  });
955
+ if (prepared && typeof prepared.then === "function") prepared = await prepared;
955
956
  if (prepared && prepared !== init) {
956
957
  if (typeof prepared !== "object") {
957
958
  throw new Error("prepareRequest must return (or mutate and return) the RequestInit it received; " + `it returned a ${typeof prepared}. Spread the init: ` + "init => ({ ...init, headers: { ...init.headers, ... } })");
@@ -1625,20 +1625,28 @@ function createServerReference({
1625
1625
  const scope = run => provideEvent(evt, run);
1626
1626
  const wrap = resolveWrapInvocation(config.wrapInvocation);
1627
1627
  const hook = requestErrorHook(ogEvt);
1628
- let result = provideEventOnce(provideEvent, evt, () => {
1629
- const run = () => fn.apply(thisArg, args);
1630
- return observeInvocation({
1631
- id,
1632
- direct: true,
1633
- event: evt,
1634
- args
1635
- }, () => reportDirectFailure(() => wrap ? wrap(run, {
1636
- id,
1637
- args,
1638
- event: evt,
1639
- direct: true
1640
- }) : run(), id, hook));
1641
- });
1628
+ let result;
1629
+ try {
1630
+ result = provideEventOnce(provideEvent, evt, () => {
1631
+ const run = () => fn.apply(thisArg, args);
1632
+ return observeInvocation({
1633
+ id,
1634
+ direct: true,
1635
+ event: evt,
1636
+ args
1637
+ }, () => reportDirectFailure(() => wrap ? wrap(run, {
1638
+ id,
1639
+ args,
1640
+ event: evt,
1641
+ direct: true
1642
+ }) : run(), id, hook));
1643
+ });
1644
+ } catch (error) {
1645
+ throw directEnvelopeValue(error, evt);
1646
+ }
1647
+ result = result && typeof result.then === "function" ? result.then(value => directEnvelopeValue(value, evt), error => {
1648
+ throw directEnvelopeValue(error, evt);
1649
+ }) : directEnvelopeValue(result, evt);
1642
1650
  result = scopeDeferredResult(result, scope);
1643
1651
  const transform = config.transformDirectResult;
1644
1652
  if (transform && result && typeof result.then === "function") {
@@ -2522,7 +2530,7 @@ function encodeLiveResult(value, headers, status, codec, request, scope) {
2522
2530
  function encodeResult(value, headers, status, codec, signal, scope) {
2523
2531
  if (NULL_BODY_STATUSES.has(status)) {
2524
2532
  if (value === undefined || value === null) {
2525
- headers.set(BODY_FORMAT_HEADER, BodyFormat.Void);
2533
+ if (status !== 304) headers.set(BODY_FORMAT_HEADER, BodyFormat.Void);
2526
2534
  return new Response(null, {
2527
2535
  status,
2528
2536
  headers
@@ -2609,6 +2617,7 @@ function currentEvent() {
2609
2617
  }
2610
2618
  function reportDirectFailure(run, id, hook) {
2611
2619
  const report = error => {
2620
+ if (error instanceof Response || isResponseEnvelope(error)) throw error;
2612
2621
  reportServerError(error, {
2613
2622
  kind: "server-function",
2614
2623
  handling: "thrown",
@@ -2625,6 +2634,20 @@ function reportDirectFailure(run, id, hook) {
2625
2634
  }
2626
2635
  return result && typeof result.then === "function" ? result.then(undefined, report) : result;
2627
2636
  }
2637
+ function directEnvelopeValue(result, event) {
2638
+ if (!isResponseEnvelope(result)) return result;
2639
+ const {
2640
+ response,
2641
+ value
2642
+ } = result;
2643
+ const stub = event.response;
2644
+ if (response && stub && stub.headers && response.headers.getSetCookie) {
2645
+ for (const cookie of response.headers.getSetCookie()) {
2646
+ stub.headers.append("Set-Cookie", cookie);
2647
+ }
2648
+ }
2649
+ return value;
2650
+ }
2628
2651
  function sanitizeServerError(value) {
2629
2652
  if (DEV) return value;
2630
2653
  if (isSafeError(value)) return value;
@@ -2873,8 +2896,14 @@ async function handleServerFunctionRequest(request, options = {}) {
2873
2896
  });
2874
2897
  return finish(protectsRequest ? withCSRFVary(response) : response);
2875
2898
  }
2899
+ const commitResponse = raw => {
2900
+ if ((method === "GET" || method === "HEAD") && (sharedCacheMayStore(departingHeader(raw, event, "Cache-Control")) || cdnMayStore(departingHeader(raw, event, "CDN-Cache-Control")))) {
2901
+ withholdTrace(event);
2902
+ }
2903
+ return commitEventResponse(raw, event);
2904
+ };
2876
2905
  const refuseCommitted = raw => {
2877
- const response = commitEventResponse(raw, event);
2906
+ const response = commitResponse(raw);
2878
2907
  return finish(protectsRequest ? withCSRFVary(response) : response);
2879
2908
  };
2880
2909
  const provide = options.provideEvent || provideEvent;
@@ -3092,9 +3121,54 @@ async function handleServerFunctionRequest(request, options = {}) {
3092
3121
  return respondThrown(x);
3093
3122
  }
3094
3123
  };
3095
- const response = commitEventResponse(enforceComposedHeaderInvariants(ownResponse(await dispatch())), event);
3124
+ const response = commitResponse(enforceComposedHeaderInvariants(ownResponse(await dispatch())));
3096
3125
  return finish(protectsRequest ? withCSRFVary(response) : response);
3097
3126
  }
3127
+ function departingHeader(response, event, name) {
3128
+ const own = response.headers.get(name);
3129
+ if (own !== null) return own;
3130
+ const stub = event.response;
3131
+ return stub && stub.headers && !stub.committed ? stub.headers.get(name) : null;
3132
+ }
3133
+ function cdnMayStore(cdnCacheControl) {
3134
+ return cdnCacheControl !== null && !forbidsStoring(cdnCacheControl);
3135
+ }
3136
+ function sharedCacheMayStore(cacheControl) {
3137
+ let shared = false;
3138
+ return !!cacheControl && !eachDirective(cacheControl, name => {
3139
+ if (name === "public" || name === "s-maxage") shared = true;
3140
+ }) && shared;
3141
+ }
3142
+ function forbidsStoring(value) {
3143
+ return eachDirective(value, () => {});
3144
+ }
3145
+ function eachDirective(value, visit) {
3146
+ let start = 0;
3147
+ let quoted = false;
3148
+ for (let i = 0; i <= value.length; i++) {
3149
+ const c = value[i];
3150
+ if (quoted) {
3151
+ if (c === "\\") i++;else if (c === '"') quoted = false;
3152
+ continue;
3153
+ }
3154
+ if (c === '"') {
3155
+ quoted = true;
3156
+ continue;
3157
+ }
3158
+ if (c !== "," && i < value.length) continue;
3159
+ const directive = value.slice(start, i);
3160
+ start = i + 1;
3161
+ const eq = directive.indexOf("=");
3162
+ const name = (eq === -1 ? directive : directive.slice(0, eq)).trim().toLowerCase();
3163
+ if (name === "no-store" || name === "private" && eq === -1) return true;
3164
+ visit(name);
3165
+ }
3166
+ return false;
3167
+ }
3168
+ function withholdTrace(event) {
3169
+ if (event.response && event.response.committed) return;
3170
+ traceForEvent(event).emit = false;
3171
+ }
3098
3172
  function ownResponse(response) {
3099
3173
  try {
3100
3174
  return new Response(response.body, response);
@@ -1543,20 +1543,28 @@ function createServerReference({
1543
1543
  const scope = run => provideEvent(evt, run);
1544
1544
  const wrap = resolveWrapInvocation(config.wrapInvocation);
1545
1545
  const hook = requestErrorHook(ogEvt);
1546
- let result = provideEventOnce(provideEvent, evt, () => {
1547
- const run = () => fn.apply(thisArg, args);
1548
- return observeInvocation({
1549
- id,
1550
- direct: true,
1551
- event: evt,
1552
- args
1553
- }, () => reportDirectFailure(() => wrap ? wrap(run, {
1554
- id,
1555
- args,
1556
- event: evt,
1557
- direct: true
1558
- }) : run(), id, hook));
1559
- });
1546
+ let result;
1547
+ try {
1548
+ result = provideEventOnce(provideEvent, evt, () => {
1549
+ const run = () => fn.apply(thisArg, args);
1550
+ return observeInvocation({
1551
+ id,
1552
+ direct: true,
1553
+ event: evt,
1554
+ args
1555
+ }, () => reportDirectFailure(() => wrap ? wrap(run, {
1556
+ id,
1557
+ args,
1558
+ event: evt,
1559
+ direct: true
1560
+ }) : run(), id, hook));
1561
+ });
1562
+ } catch (error) {
1563
+ throw directEnvelopeValue(error, evt);
1564
+ }
1565
+ result = result && typeof result.then === "function" ? result.then(value => directEnvelopeValue(value, evt), error => {
1566
+ throw directEnvelopeValue(error, evt);
1567
+ }) : directEnvelopeValue(result, evt);
1560
1568
  result = scopeDeferredResult(result, scope);
1561
1569
  const transform = config.transformDirectResult;
1562
1570
  if (transform && result && typeof result.then === "function") {
@@ -2440,7 +2448,7 @@ function encodeLiveResult(value, headers, status, codec, request, scope) {
2440
2448
  function encodeResult(value, headers, status, codec, signal, scope) {
2441
2449
  if (NULL_BODY_STATUSES.has(status)) {
2442
2450
  if (value === undefined || value === null) {
2443
- headers.set(BODY_FORMAT_HEADER, BodyFormat.Void);
2451
+ if (status !== 304) headers.set(BODY_FORMAT_HEADER, BodyFormat.Void);
2444
2452
  return new Response(null, {
2445
2453
  status,
2446
2454
  headers
@@ -2527,6 +2535,7 @@ function currentEvent() {
2527
2535
  }
2528
2536
  function reportDirectFailure(run, id, hook) {
2529
2537
  const report = error => {
2538
+ if (error instanceof Response || isResponseEnvelope(error)) throw error;
2530
2539
  reportServerError(error, {
2531
2540
  kind: "server-function",
2532
2541
  handling: "thrown",
@@ -2543,6 +2552,20 @@ function reportDirectFailure(run, id, hook) {
2543
2552
  }
2544
2553
  return result && typeof result.then === "function" ? result.then(undefined, report) : result;
2545
2554
  }
2555
+ function directEnvelopeValue(result, event) {
2556
+ if (!isResponseEnvelope(result)) return result;
2557
+ const {
2558
+ response,
2559
+ value
2560
+ } = result;
2561
+ const stub = event.response;
2562
+ if (response && stub && stub.headers && response.headers.getSetCookie) {
2563
+ for (const cookie of response.headers.getSetCookie()) {
2564
+ stub.headers.append("Set-Cookie", cookie);
2565
+ }
2566
+ }
2567
+ return value;
2568
+ }
2546
2569
  function sanitizeServerError(value) {
2547
2570
  if (DEV) return value;
2548
2571
  if (isSafeError(value)) return value;
@@ -2780,8 +2803,14 @@ async function handleServerFunctionRequest(request, options = {}) {
2780
2803
  });
2781
2804
  return finish(protectsRequest ? withCSRFVary(response) : response);
2782
2805
  }
2806
+ const commitResponse = raw => {
2807
+ if ((method === "GET" || method === "HEAD") && (sharedCacheMayStore(departingHeader(raw, event, "Cache-Control")) || cdnMayStore(departingHeader(raw, event, "CDN-Cache-Control")))) {
2808
+ withholdTrace(event);
2809
+ }
2810
+ return commitEventResponse(raw, event);
2811
+ };
2783
2812
  const refuseCommitted = raw => {
2784
- const response = commitEventResponse(raw, event);
2813
+ const response = commitResponse(raw);
2785
2814
  return finish(protectsRequest ? withCSRFVary(response) : response);
2786
2815
  };
2787
2816
  const provide = options.provideEvent || provideEvent;
@@ -2999,9 +3028,54 @@ async function handleServerFunctionRequest(request, options = {}) {
2999
3028
  return respondThrown(x);
3000
3029
  }
3001
3030
  };
3002
- const response = commitEventResponse(enforceComposedHeaderInvariants(ownResponse(await dispatch())), event);
3031
+ const response = commitResponse(enforceComposedHeaderInvariants(ownResponse(await dispatch())));
3003
3032
  return finish(protectsRequest ? withCSRFVary(response) : response);
3004
3033
  }
3034
+ function departingHeader(response, event, name) {
3035
+ const own = response.headers.get(name);
3036
+ if (own !== null) return own;
3037
+ const stub = event.response;
3038
+ return stub && stub.headers && !stub.committed ? stub.headers.get(name) : null;
3039
+ }
3040
+ function cdnMayStore(cdnCacheControl) {
3041
+ return cdnCacheControl !== null && !forbidsStoring(cdnCacheControl);
3042
+ }
3043
+ function sharedCacheMayStore(cacheControl) {
3044
+ let shared = false;
3045
+ return !!cacheControl && !eachDirective(cacheControl, name => {
3046
+ if (name === "public" || name === "s-maxage") shared = true;
3047
+ }) && shared;
3048
+ }
3049
+ function forbidsStoring(value) {
3050
+ return eachDirective(value, () => {});
3051
+ }
3052
+ function eachDirective(value, visit) {
3053
+ let start = 0;
3054
+ let quoted = false;
3055
+ for (let i = 0; i <= value.length; i++) {
3056
+ const c = value[i];
3057
+ if (quoted) {
3058
+ if (c === "\\") i++;else if (c === '"') quoted = false;
3059
+ continue;
3060
+ }
3061
+ if (c === '"') {
3062
+ quoted = true;
3063
+ continue;
3064
+ }
3065
+ if (c !== "," && i < value.length) continue;
3066
+ const directive = value.slice(start, i);
3067
+ start = i + 1;
3068
+ const eq = directive.indexOf("=");
3069
+ const name = (eq === -1 ? directive : directive.slice(0, eq)).trim().toLowerCase();
3070
+ if (name === "no-store" || name === "private" && eq === -1) return true;
3071
+ visit(name);
3072
+ }
3073
+ return false;
3074
+ }
3075
+ function withholdTrace(event) {
3076
+ if (event.response && event.response.committed) return;
3077
+ traceForEvent(event).emit = false;
3078
+ }
3005
3079
  function ownResponse(response) {
3006
3080
  try {
3007
3081
  return new Response(response.body, response);