@solidjs/web 2.0.0-rc.13 → 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.
Files changed (59) hide show
  1. package/dist/server.dev.js +317 -185
  2. package/dist/server.js +298 -161
  3. package/dist/server.observe.js +299 -171
  4. package/dist/web.dev.js +207 -189
  5. package/dist/web.js +180 -178
  6. package/dist/web.observe.js +180 -180
  7. package/frames/dist/assets.js +92 -0
  8. package/frames/dist/bind.dev.js +326 -0
  9. package/frames/dist/bind.js +295 -0
  10. package/frames/dist/client.dev.js +665 -1114
  11. package/frames/dist/client.js +630 -1087
  12. package/frames/dist/client.observe.js +629 -1087
  13. package/frames/dist/regions.js +130 -0
  14. package/frames/dist/server.dev.js +400 -201
  15. package/frames/dist/server.js +397 -198
  16. package/frames/dist/server.observe.js +397 -198
  17. package/frames/dist/trace.js +45 -0
  18. package/frames/dist/wire.js +133 -0
  19. package/package.json +30 -4
  20. package/performance-tracks/dist/performance-tracks.dev.js +2 -1
  21. package/performance-tracks/dist/performance-tracks.observe.js +2 -1
  22. package/performance-tracks/types/index.d.ts +8 -6
  23. package/serialization/dist/decode.js +38 -9
  24. package/serialization/dist/serialization.js +40 -11
  25. package/serialization/types/serializer-decode.d.ts +24 -2
  26. package/server-functions/dist/client.dev.js +101 -62
  27. package/server-functions/dist/client.js +101 -62
  28. package/server-functions/dist/client.observe.js +101 -62
  29. package/server-functions/dist/server.dev.js +178 -67
  30. package/server-functions/dist/server.js +178 -67
  31. package/server-functions/dist/server.observe.js +178 -67
  32. package/skills/server-components/SKILL.md +65 -43
  33. package/types/client.d.ts +120 -8
  34. package/types/constants.d.ts +3 -1
  35. package/types/diagnostics.d.ts +8 -0
  36. package/types/frames/assets-tier.d.ts +78 -0
  37. package/types/frames/bind-tier.d.ts +60 -0
  38. package/types/frames/client.d.ts +37 -3
  39. package/types/frames/frame-client.d.ts +249 -78
  40. package/types/frames/frame-container-plugin.d.ts +2 -2
  41. package/types/frames/frame-sink.d.ts +33 -0
  42. package/types/frames/frame-transport.d.ts +66 -18
  43. package/types/frames/regions-tier.d.ts +45 -0
  44. package/types/frames/serializer-decode.d.ts +24 -2
  45. package/types/frames/server.d.ts +33 -9
  46. package/types/frames/trace-tier.d.ts +2 -0
  47. package/types/frames/wire-tier.d.ts +116 -0
  48. package/types/head.d.ts +1 -0
  49. package/types/index.d.ts +61 -16
  50. package/types/index.server.d.ts +3 -18
  51. package/types/jsx.d.ts +4 -13
  52. package/types/observe.d.ts +4 -2
  53. package/types/response.d.ts +26 -4
  54. package/types/serializer-decode.d.ts +24 -2
  55. package/types/server-functions/client.d.ts +30 -0
  56. package/types/server-functions/registry.d.ts +4 -1
  57. package/types/server-functions/shared.d.ts +12 -8
  58. package/types/server-mock.d.ts +22 -3
  59. package/types/server.d.ts +119 -17
@@ -158,7 +158,7 @@ function provideServerFunctionRPC(rpc) {
158
158
  }
159
159
 
160
160
  const FLASH_COOKIE = "flash";
161
- const FLASH_MATCHER = new RegExp(`(?:^|;\\s*)${FLASH_COOKIE}=([^;]+)`);
161
+ const FLASH_MATCHER = /(?:^|;\s*)flash=([^;]+)/;
162
162
  function hasFlashCookie(cookieHeader) {
163
163
  return !!cookieHeader && FLASH_MATCHER.test(cookieHeader);
164
164
  }
@@ -204,6 +204,7 @@ function hasFlightMetadata(response) {
204
204
  return response.headers.has(REDIRECT_HEADER) || response.headers.has(REVALIDATE_HEADER);
205
205
  }
206
206
  async function deliverFlightData(response, data) {
207
+ if (data) await Promise.allSettled(Object.values(data).flatMap(s => s && typeof s == "object" ? Object.values(s) : []));
207
208
  const folded = (response.headers.get(SINGLE_FLIGHT_HEADER) || "").split(",");
208
209
  const metadata = hasFlightMetadata(response);
209
210
  for (const source of getFlightDataSourceIds()) {
@@ -523,26 +524,42 @@ async function extractBody(source, codecOptions, wire) {
523
524
  return undefined;
524
525
  }
525
526
  function createChunk(data) {
526
- const encoder = new TextEncoder();
527
- const encodeData = encoder.encode(data);
527
+ const encodeData = CHUNK_ENCODER.encode(data);
528
528
  const bytes = encodeData.length;
529
+ if (bytes > MAX_CHUNK_BYTES) {
530
+ throw new RangeError("Server function chunk is too large to frame.");
531
+ }
529
532
  const chunk = new Uint8Array(12 + bytes);
530
- chunk.set(encoder.encode(`;0x${bytes.toString(16).padStart(8, "0")};`));
533
+ chunk.set(CHUNK_ENCODER.encode(`;0x${bytes.toString(16).padStart(8, "0")};`));
531
534
  chunk.set(encodeData, 12);
532
535
  return chunk;
533
536
  }
537
+ const CHUNK_ENCODER = /* @__PURE__ */new TextEncoder();
538
+ const CHUNK_DECODER = /* @__PURE__ */new TextDecoder("utf-8", {
539
+ fatal: true
540
+ });
541
+ const MAX_CHUNK_BYTES = 0xffffffff;
542
+ const RETAINED_STORE_BYTES = 64 * 1024;
543
+ const DONE = {
544
+ done: true,
545
+ value: undefined
546
+ };
547
+ function malformedStream() {
548
+ return new Error("Malformed server function stream.");
549
+ }
534
550
  class ChunkReader {
535
551
  constructor(stream) {
536
552
  this.reader = stream.getReader();
537
553
  this.store = new Uint8Array(0);
538
554
  this.buffer = this.store;
539
555
  this.done = false;
556
+ this.cancelled = false;
540
557
  }
541
558
  async readChunk() {
542
559
  const chunk = await this.reader.read();
543
560
  if (chunk.done) {
544
561
  this.done = true;
545
- return;
562
+ return this.cancelled;
546
563
  }
547
564
  const incoming = chunk.value;
548
565
  const store = this.store;
@@ -563,55 +580,66 @@ class ChunkReader {
563
580
  this.store = grown;
564
581
  this.buffer = grown.subarray(0, needed);
565
582
  }
583
+ return this.cancelled;
566
584
  }
567
585
  async next() {
586
+ if (this.cancelled) return DONE;
568
587
  while (this.buffer.length < 12) {
569
588
  if (this.done) {
570
- if (this.buffer.length === 0) return {
571
- done: true,
572
- value: undefined
573
- };
574
- throw new Error("Malformed server function stream.");
589
+ if (this.buffer.length === 0) return DONE;
590
+ throw malformedStream();
575
591
  }
576
- await this.readChunk();
577
- }
578
- const decoder = new TextDecoder();
579
- const bytes = Number.parseInt(decoder.decode(this.buffer.subarray(1, 11)), 16);
580
- if (Number.isNaN(bytes)) {
581
- throw new Error("Malformed server function stream.");
592
+ if (await this.readChunk()) return DONE;
582
593
  }
594
+ const header = String.fromCharCode(...this.buffer.subarray(0, 12));
595
+ if (!/^;0x[\dA-Fa-f]{8};$/.test(header)) throw malformedStream();
596
+ const bytes = parseInt(header.slice(3, 11), 16);
583
597
  while (bytes > this.buffer.length - 12) {
584
598
  if (this.done) {
585
- throw new Error("Malformed server function stream.");
599
+ throw malformedStream();
586
600
  }
587
- await this.readChunk();
601
+ if (await this.readChunk()) return DONE;
602
+ }
603
+ let partial;
604
+ try {
605
+ partial = CHUNK_DECODER.decode(this.buffer.subarray(12, 12 + bytes));
606
+ } catch {
607
+ throw malformedStream();
588
608
  }
589
- const partial = decoder.decode(this.buffer.subarray(12, 12 + bytes));
590
609
  this.buffer = this.buffer.subarray(12 + bytes);
610
+ if (this.store.length > RETAINED_STORE_BYTES && this.buffer.length <= RETAINED_STORE_BYTES) {
611
+ this.store = this.buffer = this.buffer.slice();
612
+ }
591
613
  return {
592
614
  done: false,
593
615
  value: partial
594
616
  };
595
617
  }
596
618
  async drain(interpret) {
597
- while (true) {
598
- const result = await this.next();
599
- if (result.done) {
600
- break;
619
+ try {
620
+ while (true) {
621
+ const result = await this.next();
622
+ if (result.done) {
623
+ break;
624
+ }
625
+ interpret(result.value);
601
626
  }
602
- interpret(result.value);
627
+ } catch (error) {
628
+ this.cancel(error).catch(() => {});
629
+ throw error;
603
630
  }
604
631
  }
605
632
  cancel(reason) {
633
+ this.cancelled = true;
606
634
  return this.reader.cancel(reason);
607
635
  }
608
636
  }
609
637
  function createEventChunk(data, id) {
610
638
  let event = id !== undefined && !id.includes("\0") ? `id: ${id}\n` : "";
611
639
  for (const line of data.split(/\r\n|\r|\n/)) event += `data: ${line}\n`;
612
- return new TextEncoder().encode(event + "\n");
640
+ return CHUNK_ENCODER.encode(event + "\n");
613
641
  }
614
- const EVENT_STREAM_HEARTBEAT = new TextEncoder().encode(":\n\n");
642
+ const EVENT_STREAM_HEARTBEAT = /* @__PURE__ */new TextEncoder().encode(":\n\n");
615
643
  function isEventStream(source) {
616
644
  const contentType = source.headers.get("content-type");
617
645
  return !!contentType && contentType.startsWith("text/event-stream");
@@ -754,40 +782,44 @@ async function deserializeStream(source, codecOptions, wire) {
754
782
  throw new Error("missing body");
755
783
  }
756
784
  const reader = wire && isEventStream(source) ? wire.open(source.body) : new ChunkReader(source.body);
757
- const result = await reader.next();
758
- if (!result.done) {
759
- if (result.value.startsWith(ERROR_TRAILER_PREFIX)) {
760
- throw errorFromTrailer(result.value);
761
- }
762
- const {
763
- createJSONDeserializer
764
- } = await import('@solidjs/web/serialization/decode');
765
- const deserializeChunk = createJSONDeserializer(codecOptions);
766
- function interpretChunk(chunk) {
767
- if (chunk.startsWith(ERROR_TRAILER_PREFIX)) {
768
- throw errorFromTrailer(chunk);
785
+ try {
786
+ const result = await reader.next();
787
+ if (!result.done) {
788
+ if (result.value.startsWith(ERROR_TRAILER_PREFIX)) {
789
+ throw errorFromTrailer(result.value);
790
+ }
791
+ const {
792
+ createJSONDeserializer
793
+ } = await import('@solidjs/web/serialization/decode');
794
+ const deserializeChunk = createJSONDeserializer(codecOptions);
795
+ function interpretChunk(chunk) {
796
+ if (chunk.startsWith(ERROR_TRAILER_PREFIX)) {
797
+ throw errorFromTrailer(chunk);
798
+ }
799
+ return deserializeChunk(JSON.parse(chunk));
769
800
  }
770
- return deserializeChunk(JSON.parse(chunk));
801
+ const connection = wire && wire.connection;
802
+ let endConnection;
803
+ if (connection) connection.ended = new Promise(resolve => endConnection = resolve);
804
+ const end = error => {
805
+ const sweep = () => deserializeChunk.abort(error);
806
+ if (connection) {
807
+ const close = () => deserializeChunk.close();
808
+ endConnection({
809
+ open: deserializeChunk.open(),
810
+ error,
811
+ sweep,
812
+ close
813
+ });
814
+ } else sweep();
815
+ };
816
+ reader.drain(interpretChunk).then(() => end(new Error("Server function stream ended unexpectedly.")), error => end(error));
817
+ return interpretChunk(result.value);
771
818
  }
772
- const connection = wire && wire.connection;
773
- let endConnection;
774
- if (connection) connection.ended = new Promise(resolve => endConnection = resolve);
775
- const end = error => {
776
- const sweep = () => deserializeChunk.abort(error);
777
- if (connection) {
778
- const close = () => deserializeChunk.close();
779
- endConnection({
780
- open: deserializeChunk.open(),
781
- error,
782
- sweep,
783
- close
784
- });
785
- } else sweep();
786
- };
787
- reader.drain(interpretChunk).then(() => end(new Error("Server function stream ended unexpectedly.")), error => end(error));
788
- return interpretChunk(result.value);
819
+ } catch (error) {
820
+ reader.cancel(error).catch(() => {});
821
+ throw error;
789
822
  }
790
- return undefined;
791
823
  }
792
824
  async function decodeResponse(response, codecOptions) {
793
825
  if (!response.body) return undefined;
@@ -916,10 +948,11 @@ async function createRequest(base, id, options, meta) {
916
948
  headers
917
949
  };
918
950
  if (config.prepareRequest) {
919
- const prepared = await config.prepareRequest(init, {
951
+ let prepared = config.prepareRequest(init, {
920
952
  id,
921
953
  meta
922
954
  });
955
+ if (prepared && typeof prepared.then === "function") prepared = await prepared;
923
956
  if (prepared && prepared !== init) {
924
957
  if (typeof prepared !== "object") {
925
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, ... } })");
@@ -1014,6 +1047,7 @@ async function dispatchServerFunction(base, id, options, args, meta, callArgs =
1014
1047
  id,
1015
1048
  meta
1016
1049
  }) : undefined;
1050
+ const sent = options;
1017
1051
  const controller = options.signal ? undefined : new AbortController();
1018
1052
  if (controller) options = {
1019
1053
  ...options,
@@ -1030,7 +1064,8 @@ async function dispatchServerFunction(base, id, options, args, meta, callArgs =
1030
1064
  id,
1031
1065
  meta,
1032
1066
  args: callArgs,
1033
- context
1067
+ context,
1068
+ retry: () => fetchServerFunction(base, id, sent, args, meta, callArgs)
1034
1069
  };
1035
1070
  if (options[LIVE_WIRE]) ctx[LIVE_WIRE] = options[LIVE_WIRE];
1036
1071
  const handled = handler.handle(response, ctx);
@@ -1094,7 +1129,8 @@ function createServerReference(id, name, base) {
1094
1129
  const hit = handler.intercept({
1095
1130
  id,
1096
1131
  meta: metadata,
1097
- args
1132
+ args,
1133
+ retry: send
1098
1134
  });
1099
1135
  if (hit !== undefined) return localOrSend(hit, send);
1100
1136
  }
@@ -1125,12 +1161,14 @@ function GET(fn) {
1125
1161
  const run = async (args, invokeOptions) => {
1126
1162
  const handler = config.responseHandler;
1127
1163
  if (handler && handler.intercept && !adoptedCall(invokeOptions)) {
1164
+ const retry = () => send(args, invokeOptions);
1128
1165
  const hit = handler.intercept({
1129
1166
  id,
1130
1167
  meta: metadata,
1131
- args
1168
+ args,
1169
+ retry
1132
1170
  });
1133
- if (hit !== undefined) return localOrSend(hit, () => send(args, invokeOptions));
1171
+ if (hit !== undefined) return localOrSend(hit, retry);
1134
1172
  }
1135
1173
  return send(args, invokeOptions);
1136
1174
  };
@@ -1204,6 +1242,7 @@ function live(fn) {
1204
1242
  };
1205
1243
  const makeIterable = (args, invokeOptions) => {
1206
1244
  const handler = config.responseHandler;
1245
+ handler && handler.onLive && handler.onLive();
1207
1246
  const local = handler && handler.intercept ? handler.intercept({
1208
1247
  id,
1209
1248
  meta: metadata,
@@ -51,7 +51,7 @@ function provideServerFunctionRPC(rpc) {
51
51
  }
52
52
 
53
53
  const FLASH_COOKIE = "flash";
54
- const FLASH_MATCHER = new RegExp(`(?:^|;\\s*)${FLASH_COOKIE}=([^;]+)`);
54
+ const FLASH_MATCHER = /(?:^|;\s*)flash=([^;]+)/;
55
55
  function hasFlashCookie(cookieHeader) {
56
56
  return !!cookieHeader && FLASH_MATCHER.test(cookieHeader);
57
57
  }
@@ -97,6 +97,7 @@ function hasFlightMetadata(response) {
97
97
  return response.headers.has(REDIRECT_HEADER) || response.headers.has(REVALIDATE_HEADER);
98
98
  }
99
99
  async function deliverFlightData(response, data) {
100
+ if (data) await Promise.allSettled(Object.values(data).flatMap(s => s && typeof s == "object" ? Object.values(s) : []));
100
101
  const folded = (response.headers.get(SINGLE_FLIGHT_HEADER) || "").split(",");
101
102
  const metadata = hasFlightMetadata(response);
102
103
  for (const source of getFlightDataSourceIds()) {
@@ -416,26 +417,42 @@ async function extractBody(source, codecOptions, wire) {
416
417
  return undefined;
417
418
  }
418
419
  function createChunk(data) {
419
- const encoder = new TextEncoder();
420
- const encodeData = encoder.encode(data);
420
+ const encodeData = CHUNK_ENCODER.encode(data);
421
421
  const bytes = encodeData.length;
422
+ if (bytes > MAX_CHUNK_BYTES) {
423
+ throw new RangeError("Server function chunk is too large to frame.");
424
+ }
422
425
  const chunk = new Uint8Array(12 + bytes);
423
- chunk.set(encoder.encode(`;0x${bytes.toString(16).padStart(8, "0")};`));
426
+ chunk.set(CHUNK_ENCODER.encode(`;0x${bytes.toString(16).padStart(8, "0")};`));
424
427
  chunk.set(encodeData, 12);
425
428
  return chunk;
426
429
  }
430
+ const CHUNK_ENCODER = /* @__PURE__ */new TextEncoder();
431
+ const CHUNK_DECODER = /* @__PURE__ */new TextDecoder("utf-8", {
432
+ fatal: true
433
+ });
434
+ const MAX_CHUNK_BYTES = 0xffffffff;
435
+ const RETAINED_STORE_BYTES = 64 * 1024;
436
+ const DONE = {
437
+ done: true,
438
+ value: undefined
439
+ };
440
+ function malformedStream() {
441
+ return new Error("Malformed server function stream.");
442
+ }
427
443
  class ChunkReader {
428
444
  constructor(stream) {
429
445
  this.reader = stream.getReader();
430
446
  this.store = new Uint8Array(0);
431
447
  this.buffer = this.store;
432
448
  this.done = false;
449
+ this.cancelled = false;
433
450
  }
434
451
  async readChunk() {
435
452
  const chunk = await this.reader.read();
436
453
  if (chunk.done) {
437
454
  this.done = true;
438
- return;
455
+ return this.cancelled;
439
456
  }
440
457
  const incoming = chunk.value;
441
458
  const store = this.store;
@@ -456,55 +473,66 @@ class ChunkReader {
456
473
  this.store = grown;
457
474
  this.buffer = grown.subarray(0, needed);
458
475
  }
476
+ return this.cancelled;
459
477
  }
460
478
  async next() {
479
+ if (this.cancelled) return DONE;
461
480
  while (this.buffer.length < 12) {
462
481
  if (this.done) {
463
- if (this.buffer.length === 0) return {
464
- done: true,
465
- value: undefined
466
- };
467
- throw new Error("Malformed server function stream.");
482
+ if (this.buffer.length === 0) return DONE;
483
+ throw malformedStream();
468
484
  }
469
- await this.readChunk();
470
- }
471
- const decoder = new TextDecoder();
472
- const bytes = Number.parseInt(decoder.decode(this.buffer.subarray(1, 11)), 16);
473
- if (Number.isNaN(bytes)) {
474
- throw new Error("Malformed server function stream.");
485
+ if (await this.readChunk()) return DONE;
475
486
  }
487
+ const header = String.fromCharCode(...this.buffer.subarray(0, 12));
488
+ if (!/^;0x[\dA-Fa-f]{8};$/.test(header)) throw malformedStream();
489
+ const bytes = parseInt(header.slice(3, 11), 16);
476
490
  while (bytes > this.buffer.length - 12) {
477
491
  if (this.done) {
478
- throw new Error("Malformed server function stream.");
492
+ throw malformedStream();
479
493
  }
480
- await this.readChunk();
494
+ if (await this.readChunk()) return DONE;
495
+ }
496
+ let partial;
497
+ try {
498
+ partial = CHUNK_DECODER.decode(this.buffer.subarray(12, 12 + bytes));
499
+ } catch {
500
+ throw malformedStream();
481
501
  }
482
- const partial = decoder.decode(this.buffer.subarray(12, 12 + bytes));
483
502
  this.buffer = this.buffer.subarray(12 + bytes);
503
+ if (this.store.length > RETAINED_STORE_BYTES && this.buffer.length <= RETAINED_STORE_BYTES) {
504
+ this.store = this.buffer = this.buffer.slice();
505
+ }
484
506
  return {
485
507
  done: false,
486
508
  value: partial
487
509
  };
488
510
  }
489
511
  async drain(interpret) {
490
- while (true) {
491
- const result = await this.next();
492
- if (result.done) {
493
- break;
512
+ try {
513
+ while (true) {
514
+ const result = await this.next();
515
+ if (result.done) {
516
+ break;
517
+ }
518
+ interpret(result.value);
494
519
  }
495
- interpret(result.value);
520
+ } catch (error) {
521
+ this.cancel(error).catch(() => {});
522
+ throw error;
496
523
  }
497
524
  }
498
525
  cancel(reason) {
526
+ this.cancelled = true;
499
527
  return this.reader.cancel(reason);
500
528
  }
501
529
  }
502
530
  function createEventChunk(data, id) {
503
531
  let event = id !== undefined && !id.includes("\0") ? `id: ${id}\n` : "";
504
532
  for (const line of data.split(/\r\n|\r|\n/)) event += `data: ${line}\n`;
505
- return new TextEncoder().encode(event + "\n");
533
+ return CHUNK_ENCODER.encode(event + "\n");
506
534
  }
507
- const EVENT_STREAM_HEARTBEAT = new TextEncoder().encode(":\n\n");
535
+ const EVENT_STREAM_HEARTBEAT = /* @__PURE__ */new TextEncoder().encode(":\n\n");
508
536
  function isEventStream(source) {
509
537
  const contentType = source.headers.get("content-type");
510
538
  return !!contentType && contentType.startsWith("text/event-stream");
@@ -647,40 +675,44 @@ async function deserializeStream(source, codecOptions, wire) {
647
675
  throw new Error("missing body");
648
676
  }
649
677
  const reader = wire && isEventStream(source) ? wire.open(source.body) : new ChunkReader(source.body);
650
- const result = await reader.next();
651
- if (!result.done) {
652
- if (result.value.startsWith(ERROR_TRAILER_PREFIX)) {
653
- throw errorFromTrailer(result.value);
654
- }
655
- const {
656
- createJSONDeserializer
657
- } = await import('@solidjs/web/serialization/decode');
658
- const deserializeChunk = createJSONDeserializer(codecOptions);
659
- function interpretChunk(chunk) {
660
- if (chunk.startsWith(ERROR_TRAILER_PREFIX)) {
661
- throw errorFromTrailer(chunk);
678
+ try {
679
+ const result = await reader.next();
680
+ if (!result.done) {
681
+ if (result.value.startsWith(ERROR_TRAILER_PREFIX)) {
682
+ throw errorFromTrailer(result.value);
662
683
  }
663
- return deserializeChunk(JSON.parse(chunk));
684
+ const {
685
+ createJSONDeserializer
686
+ } = await import('@solidjs/web/serialization/decode');
687
+ const deserializeChunk = createJSONDeserializer(codecOptions);
688
+ function interpretChunk(chunk) {
689
+ if (chunk.startsWith(ERROR_TRAILER_PREFIX)) {
690
+ throw errorFromTrailer(chunk);
691
+ }
692
+ return deserializeChunk(JSON.parse(chunk));
693
+ }
694
+ const connection = wire && wire.connection;
695
+ let endConnection;
696
+ if (connection) connection.ended = new Promise(resolve => endConnection = resolve);
697
+ const end = error => {
698
+ const sweep = () => deserializeChunk.abort(error);
699
+ if (connection) {
700
+ const close = () => deserializeChunk.close();
701
+ endConnection({
702
+ open: deserializeChunk.open(),
703
+ error,
704
+ sweep,
705
+ close
706
+ });
707
+ } else sweep();
708
+ };
709
+ reader.drain(interpretChunk).then(() => end(new Error("Server function stream ended unexpectedly.")), error => end(error));
710
+ return interpretChunk(result.value);
664
711
  }
665
- const connection = wire && wire.connection;
666
- let endConnection;
667
- if (connection) connection.ended = new Promise(resolve => endConnection = resolve);
668
- const end = error => {
669
- const sweep = () => deserializeChunk.abort(error);
670
- if (connection) {
671
- const close = () => deserializeChunk.close();
672
- endConnection({
673
- open: deserializeChunk.open(),
674
- error,
675
- sweep,
676
- close
677
- });
678
- } else sweep();
679
- };
680
- reader.drain(interpretChunk).then(() => end(new Error("Server function stream ended unexpectedly.")), error => end(error));
681
- return interpretChunk(result.value);
712
+ } catch (error) {
713
+ reader.cancel(error).catch(() => {});
714
+ throw error;
682
715
  }
683
- return undefined;
684
716
  }
685
717
  async function decodeResponse(response, codecOptions) {
686
718
  if (!response.body) return undefined;
@@ -801,10 +833,11 @@ async function createRequest(base, id, options, meta) {
801
833
  headers
802
834
  };
803
835
  if (config.prepareRequest) {
804
- const prepared = await config.prepareRequest(init, {
836
+ let prepared = config.prepareRequest(init, {
805
837
  id,
806
838
  meta
807
839
  });
840
+ if (prepared && typeof prepared.then === "function") prepared = await prepared;
808
841
  if (prepared && prepared !== init) {
809
842
  if (typeof prepared !== "object") {
810
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, ... } })");
@@ -885,6 +918,7 @@ async function dispatchServerFunction(base, id, options, args, meta, callArgs =
885
918
  id,
886
919
  meta
887
920
  }) : undefined;
921
+ const sent = options;
888
922
  const controller = options.signal ? undefined : new AbortController();
889
923
  if (controller) options = {
890
924
  ...options,
@@ -896,7 +930,8 @@ async function dispatchServerFunction(base, id, options, args, meta, callArgs =
896
930
  id,
897
931
  meta,
898
932
  args: callArgs,
899
- context
933
+ context,
934
+ retry: () => fetchServerFunction(base, id, sent, args, meta, callArgs)
900
935
  };
901
936
  if (options[LIVE_WIRE]) ctx[LIVE_WIRE] = options[LIVE_WIRE];
902
937
  const handled = handler.handle(response, ctx);
@@ -960,7 +995,8 @@ function createServerReference(id, name, base) {
960
995
  const hit = handler.intercept({
961
996
  id,
962
997
  meta: metadata,
963
- args
998
+ args,
999
+ retry: send
964
1000
  });
965
1001
  if (hit !== undefined) return localOrSend(hit, send);
966
1002
  }
@@ -991,12 +1027,14 @@ function GET(fn) {
991
1027
  const run = async (args, invokeOptions) => {
992
1028
  const handler = config.responseHandler;
993
1029
  if (handler && handler.intercept && !adoptedCall(invokeOptions)) {
1030
+ const retry = () => send(args, invokeOptions);
994
1031
  const hit = handler.intercept({
995
1032
  id,
996
1033
  meta: metadata,
997
- args
1034
+ args,
1035
+ retry
998
1036
  });
999
- if (hit !== undefined) return localOrSend(hit, () => send(args, invokeOptions));
1037
+ if (hit !== undefined) return localOrSend(hit, retry);
1000
1038
  }
1001
1039
  return send(args, invokeOptions);
1002
1040
  };
@@ -1054,6 +1092,7 @@ function live(fn) {
1054
1092
  };
1055
1093
  const makeIterable = (args, invokeOptions) => {
1056
1094
  const handler = config.responseHandler;
1095
+ handler && handler.onLive && handler.onLive();
1057
1096
  const local = handler && handler.intercept ? handler.intercept({
1058
1097
  id,
1059
1098
  meta: metadata,