@electric-sql/client 1.2.2 → 1.3.1

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.
@@ -61,26 +61,6 @@ var __privateWrapper = (obj, member, setter, getter) => ({
61
61
  return __privateGet(obj, member, getter);
62
62
  }
63
63
  });
64
- var __async = (__this, __arguments, generator) => {
65
- return new Promise((resolve, reject) => {
66
- var fulfilled = (value) => {
67
- try {
68
- step(generator.next(value));
69
- } catch (e) {
70
- reject(e);
71
- }
72
- };
73
- var rejected = (value) => {
74
- try {
75
- step(generator.throw(value));
76
- } catch (e) {
77
- reject(e);
78
- }
79
- };
80
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
81
- step((generator = generator.apply(__this, __arguments)).next());
82
- });
83
- };
84
64
 
85
65
  // src/index.ts
86
66
  var src_exports = {};
@@ -114,22 +94,20 @@ var FetchError = class _FetchError extends Error {
114
94
  this.json = json;
115
95
  this.headers = headers;
116
96
  }
117
- static fromResponse(response, url) {
118
- return __async(this, null, function* () {
119
- const status = response.status;
120
- const headers = Object.fromEntries([...response.headers.entries()]);
121
- let text = void 0;
122
- let json = void 0;
123
- const contentType = response.headers.get(`content-type`);
124
- if (!response.bodyUsed) {
125
- if (contentType && contentType.includes(`application/json`)) {
126
- json = yield response.json();
127
- } else {
128
- text = yield response.text();
129
- }
97
+ static async fromResponse(response, url) {
98
+ const status = response.status;
99
+ const headers = Object.fromEntries([...response.headers.entries()]);
100
+ let text = void 0;
101
+ let json = void 0;
102
+ const contentType = response.headers.get(`content-type`);
103
+ if (!response.bodyUsed) {
104
+ if (contentType && contentType.includes(`application/json`)) {
105
+ json = await response.json();
106
+ } else {
107
+ text = await response.text();
130
108
  }
131
- return new _FetchError(status, text, json, headers, url);
132
- });
109
+ }
110
+ return new _FetchError(status, text, json, headers, url);
133
111
  }
134
112
  };
135
113
  var FetchBackoffAbortError = class extends Error {
@@ -486,11 +464,9 @@ function isUpToDateMessage(message) {
486
464
  return isControlMessage(message) && message.headers.control === `up-to-date`;
487
465
  }
488
466
  function getOffset(message) {
467
+ if (message.headers.control != `up-to-date`) return;
489
468
  const lsn = message.headers.global_last_seen_lsn;
490
- if (!lsn) {
491
- return;
492
- }
493
- return `${lsn}_0`;
469
+ return lsn ? `${lsn}_0` : void 0;
494
470
  }
495
471
  function isVisibleInSnapshot(txid, snapshot) {
496
472
  const xid = BigInt(txid);
@@ -573,7 +549,7 @@ function createFetchWithBackoff(fetchClient, backoffOptions = BackoffDefaults) {
573
549
  onFailedAttempt,
574
550
  maxRetries = Infinity
575
551
  } = backoffOptions;
576
- return (...args) => __async(this, null, function* () {
552
+ return async (...args) => {
577
553
  var _a;
578
554
  const url = args[0];
579
555
  const options = args[1];
@@ -581,11 +557,11 @@ function createFetchWithBackoff(fetchClient, backoffOptions = BackoffDefaults) {
581
557
  let attempt = 0;
582
558
  while (true) {
583
559
  try {
584
- const result = yield fetchClient(...args);
560
+ const result = await fetchClient(...args);
585
561
  if (result.ok) {
586
562
  return result;
587
563
  }
588
- const err = yield FetchError.fromResponse(result, url.toString());
564
+ const err = await FetchError.fromResponse(result, url.toString());
589
565
  throw err;
590
566
  } catch (e) {
591
567
  onFailedAttempt == null ? void 0 : onFailedAttempt();
@@ -613,24 +589,24 @@ function createFetchWithBackoff(fetchClient, backoffOptions = BackoffDefaults) {
613
589
  `Retry attempt #${attempt} after ${waitMs}ms (${source}, serverMin=${serverMinimumMs}ms, clientBackoff=${clientBackoffMs}ms)`
614
590
  );
615
591
  }
616
- yield new Promise((resolve) => setTimeout(resolve, waitMs));
592
+ await new Promise((resolve) => setTimeout(resolve, waitMs));
617
593
  delay = Math.min(delay * multiplier, maxDelay);
618
594
  }
619
595
  }
620
596
  }
621
- });
597
+ };
622
598
  }
623
599
  var NO_BODY_STATUS_CODES = [201, 204, 205];
624
600
  function createFetchWithConsumedMessages(fetchClient) {
625
- return (...args) => __async(this, null, function* () {
601
+ return async (...args) => {
626
602
  var _a, _b;
627
603
  const url = args[0];
628
- const res = yield fetchClient(...args);
604
+ const res = await fetchClient(...args);
629
605
  try {
630
606
  if (res.status < 200 || NO_BODY_STATUS_CODES.includes(res.status)) {
631
607
  return res;
632
608
  }
633
- const text = yield res.text();
609
+ const text = await res.text();
634
610
  return new Response(text, res);
635
611
  } catch (err) {
636
612
  if ((_b = (_a = args[1]) == null ? void 0 : _a.signal) == null ? void 0 : _b.aborted) {
@@ -645,7 +621,7 @@ function createFetchWithConsumedMessages(fetchClient) {
645
621
  err instanceof Error ? err.message : typeof err === `string` ? err : `failed to read body`
646
622
  );
647
623
  }
648
- });
624
+ };
649
625
  }
650
626
  var ChunkPrefetchDefaults = {
651
627
  maxChunksToPrefetch: 2
@@ -653,14 +629,15 @@ var ChunkPrefetchDefaults = {
653
629
  function createFetchWithChunkBuffer(fetchClient, prefetchOptions = ChunkPrefetchDefaults) {
654
630
  const { maxChunksToPrefetch } = prefetchOptions;
655
631
  let prefetchQueue;
656
- const prefetchClient = (...args) => __async(this, null, function* () {
632
+ const prefetchClient = async (...args) => {
657
633
  const url = args[0].toString();
658
634
  const prefetchedRequest = prefetchQueue == null ? void 0 : prefetchQueue.consume(...args);
659
635
  if (prefetchedRequest) {
660
636
  return prefetchedRequest;
661
637
  }
662
638
  prefetchQueue == null ? void 0 : prefetchQueue.abort();
663
- const response = yield fetchClient(...args);
639
+ prefetchQueue = void 0;
640
+ const response = await fetchClient(...args);
664
641
  const nextUrl = getNextChunkUrl(url, response);
665
642
  if (nextUrl) {
666
643
  prefetchQueue = new PrefetchQueue({
@@ -671,7 +648,7 @@ function createFetchWithChunkBuffer(fetchClient, prefetchOptions = ChunkPrefetch
671
648
  });
672
649
  }
673
650
  return response;
674
- });
651
+ };
675
652
  return prefetchClient;
676
653
  }
677
654
  var requiredElectricResponseHeaders = [
@@ -681,8 +658,8 @@ var requiredElectricResponseHeaders = [
681
658
  var requiredLiveResponseHeaders = [`electric-cursor`];
682
659
  var requiredNonLiveResponseHeaders = [`electric-schema`];
683
660
  function createFetchWithResponseHeadersCheck(fetchClient) {
684
- return (...args) => __async(this, null, function* () {
685
- const response = yield fetchClient(...args);
661
+ return async (...args) => {
662
+ const response = await fetchClient(...args);
686
663
  if (response.ok) {
687
664
  const headers = response.headers;
688
665
  const missingHeaders = [];
@@ -712,7 +689,7 @@ function createFetchWithResponseHeadersCheck(fetchClient) {
712
689
  }
713
690
  }
714
691
  return response;
715
- });
692
+ };
716
693
  }
717
694
  var _fetchClient, _maxPrefetchedRequests, _prefetchQueue, _queueHeadUrl, _queueTailUrl, _PrefetchQueue_instances, prefetch_fn;
718
695
  var PrefetchQueue = class {
@@ -732,12 +709,17 @@ var PrefetchQueue = class {
732
709
  }
733
710
  abort() {
734
711
  __privateGet(this, _prefetchQueue).forEach(([_, aborter]) => aborter.abort());
712
+ __privateGet(this, _prefetchQueue).clear();
735
713
  }
736
714
  consume(...args) {
737
- var _a;
738
715
  const url = args[0].toString();
739
- const request = (_a = __privateGet(this, _prefetchQueue).get(url)) == null ? void 0 : _a[0];
740
- if (!request || url !== __privateGet(this, _queueHeadUrl)) return;
716
+ const entry = __privateGet(this, _prefetchQueue).get(url);
717
+ if (!entry || url !== __privateGet(this, _queueHeadUrl)) return;
718
+ const [request, aborter] = entry;
719
+ if (aborter.signal.aborted) {
720
+ __privateGet(this, _prefetchQueue).delete(url);
721
+ return;
722
+ }
741
723
  __privateGet(this, _prefetchQueue).delete(url);
742
724
  request.then((response) => {
743
725
  const nextUrl = getNextChunkUrl(url, response);
@@ -786,6 +768,13 @@ function getNextChunkUrl(url, res) {
786
768
  if (!shapeHandle || !lastOffset || isUpToDate) return;
787
769
  const nextUrl = new URL(url);
788
770
  if (nextUrl.searchParams.has(LIVE_QUERY_PARAM)) return;
771
+ const expiredHandle = nextUrl.searchParams.get(EXPIRED_HANDLE_QUERY_PARAM);
772
+ if (expiredHandle && shapeHandle === expiredHandle) {
773
+ console.warn(
774
+ `[Electric] Received stale cached response with expired shape handle. This should not happen and indicates a proxy/CDN caching misconfiguration. The response contained handle "${shapeHandle}" which was previously marked as expired. Check that your proxy includes all query parameters (especially 'handle' and 'offset') in its cache key. Skipping prefetch to prevent infinite 409 loop.`
775
+ );
776
+ return;
777
+ }
789
778
  nextUrl.searchParams.set(SHAPE_HANDLE_QUERY_PARAM, shapeHandle);
790
779
  nextUrl.searchParams.set(OFFSET_QUERY_PARAM, lastOffset);
791
780
  nextUrl.searchParams.sort();
@@ -1058,43 +1047,35 @@ var RESERVED_PARAMS = /* @__PURE__ */ new Set([
1058
1047
  LIVE_QUERY_PARAM,
1059
1048
  OFFSET_QUERY_PARAM
1060
1049
  ]);
1061
- function resolveValue(value) {
1062
- return __async(this, null, function* () {
1063
- if (typeof value === `function`) {
1064
- return value();
1065
- }
1066
- return value;
1067
- });
1050
+ async function resolveValue(value) {
1051
+ if (typeof value === `function`) {
1052
+ return value();
1053
+ }
1054
+ return value;
1068
1055
  }
1069
- function toInternalParams(params) {
1070
- return __async(this, null, function* () {
1071
- const entries = Object.entries(params);
1072
- const resolvedEntries = yield Promise.all(
1073
- entries.map((_0) => __async(this, [_0], function* ([key, value]) {
1074
- if (value === void 0) return [key, void 0];
1075
- const resolvedValue = yield resolveValue(value);
1076
- return [
1077
- key,
1078
- Array.isArray(resolvedValue) ? resolvedValue.join(`,`) : resolvedValue
1079
- ];
1080
- }))
1081
- );
1082
- return Object.fromEntries(
1083
- resolvedEntries.filter(([_, value]) => value !== void 0)
1084
- );
1085
- });
1056
+ async function toInternalParams(params) {
1057
+ const entries = Object.entries(params);
1058
+ const resolvedEntries = await Promise.all(
1059
+ entries.map(async ([key, value]) => {
1060
+ if (value === void 0) return [key, void 0];
1061
+ const resolvedValue = await resolveValue(value);
1062
+ return [
1063
+ key,
1064
+ Array.isArray(resolvedValue) ? resolvedValue.join(`,`) : resolvedValue
1065
+ ];
1066
+ })
1067
+ );
1068
+ return Object.fromEntries(
1069
+ resolvedEntries.filter(([_, value]) => value !== void 0)
1070
+ );
1086
1071
  }
1087
- function resolveHeaders(headers) {
1088
- return __async(this, null, function* () {
1089
- if (!headers) return {};
1090
- const entries = Object.entries(headers);
1091
- const resolvedEntries = yield Promise.all(
1092
- entries.map((_0) => __async(this, [_0], function* ([key, value]) {
1093
- return [key, yield resolveValue(value)];
1094
- }))
1095
- );
1096
- return Object.fromEntries(resolvedEntries);
1097
- });
1072
+ async function resolveHeaders(headers) {
1073
+ if (!headers) return {};
1074
+ const entries = Object.entries(headers);
1075
+ const resolvedEntries = await Promise.all(
1076
+ entries.map(async ([key, value]) => [key, await resolveValue(value)])
1077
+ );
1078
+ return Object.fromEntries(resolvedEntries);
1098
1079
  }
1099
1080
  function canonicalShapeKey(url) {
1100
1081
  const cleanUrl = new URL(url.origin + url.pathname);
@@ -1256,16 +1237,14 @@ var ShapeStream = class {
1256
1237
  * long polling, ensuring that the stream receives an up to date message with the
1257
1238
  * latest LSN from Postgres at that point in time.
1258
1239
  */
1259
- forceDisconnectAndRefresh() {
1260
- return __async(this, null, function* () {
1261
- var _a, _b;
1262
- __privateSet(this, _isRefreshing, true);
1263
- if (__privateGet(this, _isUpToDate) && !((_a = __privateGet(this, _requestAbortController)) == null ? void 0 : _a.signal.aborted)) {
1264
- (_b = __privateGet(this, _requestAbortController)) == null ? void 0 : _b.abort(FORCE_DISCONNECT_AND_REFRESH);
1265
- }
1266
- yield __privateMethod(this, _ShapeStream_instances, nextTick_fn).call(this);
1267
- __privateSet(this, _isRefreshing, false);
1268
- });
1240
+ async forceDisconnectAndRefresh() {
1241
+ var _a, _b;
1242
+ __privateSet(this, _isRefreshing, true);
1243
+ if (__privateGet(this, _isUpToDate) && !((_a = __privateGet(this, _requestAbortController)) == null ? void 0 : _a.signal.aborted)) {
1244
+ (_b = __privateGet(this, _requestAbortController)) == null ? void 0 : _b.abort(FORCE_DISCONNECT_AND_REFRESH);
1245
+ }
1246
+ await __privateMethod(this, _ShapeStream_instances, nextTick_fn).call(this);
1247
+ __privateSet(this, _isRefreshing, false);
1269
1248
  }
1270
1249
  /**
1271
1250
  * Request a snapshot for subset of data and inject it into the subscribed data stream.
@@ -1281,40 +1260,39 @@ var ShapeStream = class {
1281
1260
  * @param opts - The options for the snapshot request.
1282
1261
  * @returns The metadata and the data for the snapshot.
1283
1262
  */
1284
- requestSnapshot(opts) {
1285
- return __async(this, null, function* () {
1286
- if (__privateGet(this, _mode) === `full`) {
1287
- throw new Error(
1288
- `Snapshot requests are not supported in ${__privateGet(this, _mode)} mode, as the consumer is guaranteed to observe all data`
1289
- );
1263
+ async requestSnapshot(opts) {
1264
+ if (__privateGet(this, _mode) === `full`) {
1265
+ throw new Error(
1266
+ `Snapshot requests are not supported in ${__privateGet(this, _mode)} mode, as the consumer is guaranteed to observe all data`
1267
+ );
1268
+ }
1269
+ if (!__privateGet(this, _started)) await __privateMethod(this, _ShapeStream_instances, start_fn).call(this);
1270
+ await __privateMethod(this, _ShapeStream_instances, waitForStreamEnd_fn).call(this);
1271
+ __privateWrapper(this, _activeSnapshotRequests)._++;
1272
+ try {
1273
+ if (__privateGet(this, _activeSnapshotRequests) === 1) {
1274
+ __privateMethod(this, _ShapeStream_instances, pause_fn).call(this);
1290
1275
  }
1291
- if (!__privateGet(this, _started)) yield __privateMethod(this, _ShapeStream_instances, start_fn).call(this);
1292
- yield __privateMethod(this, _ShapeStream_instances, waitForStreamEnd_fn).call(this);
1293
- __privateWrapper(this, _activeSnapshotRequests)._++;
1294
- try {
1295
- if (__privateGet(this, _activeSnapshotRequests) === 1) {
1296
- __privateMethod(this, _ShapeStream_instances, pause_fn).call(this);
1297
- }
1298
- const { metadata, data } = yield this.fetchSnapshot(opts);
1299
- const dataWithEndBoundary = data.concat([
1300
- { headers: __spreadValues({ control: `snapshot-end` }, metadata) }
1301
- ]);
1302
- __privateGet(this, _snapshotTracker).addSnapshot(
1303
- metadata,
1304
- new Set(data.map((message) => message.key))
1305
- );
1306
- __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, dataWithEndBoundary, false);
1307
- return {
1308
- metadata,
1309
- data
1310
- };
1311
- } finally {
1312
- __privateWrapper(this, _activeSnapshotRequests)._--;
1313
- if (__privateGet(this, _activeSnapshotRequests) === 0) {
1314
- __privateMethod(this, _ShapeStream_instances, resume_fn).call(this);
1315
- }
1276
+ const { metadata, data } = await this.fetchSnapshot(opts);
1277
+ const dataWithEndBoundary = data.concat([
1278
+ { headers: __spreadValues({ control: `snapshot-end` }, metadata) },
1279
+ { headers: __spreadValues({ control: `subset-end` }, opts) }
1280
+ ]);
1281
+ __privateGet(this, _snapshotTracker).addSnapshot(
1282
+ metadata,
1283
+ new Set(data.map((message) => message.key))
1284
+ );
1285
+ __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, dataWithEndBoundary, false);
1286
+ return {
1287
+ metadata,
1288
+ data
1289
+ };
1290
+ } finally {
1291
+ __privateWrapper(this, _activeSnapshotRequests)._--;
1292
+ if (__privateGet(this, _activeSnapshotRequests) === 0) {
1293
+ __privateMethod(this, _ShapeStream_instances, resume_fn).call(this);
1316
1294
  }
1317
- });
1295
+ }
1318
1296
  }
1319
1297
  /**
1320
1298
  * Fetch a snapshot for subset of data.
@@ -1323,36 +1301,34 @@ var ShapeStream = class {
1323
1301
  * @param opts - The options for the snapshot request.
1324
1302
  * @returns The metadata and the data for the snapshot.
1325
1303
  */
1326
- fetchSnapshot(opts) {
1327
- return __async(this, null, function* () {
1328
- var _a;
1329
- const { fetchUrl, requestHeaders } = yield __privateMethod(this, _ShapeStream_instances, constructUrl_fn).call(this, this.options.url, true, opts);
1330
- const response = yield __privateGet(this, _fetchClient2).call(this, fetchUrl.toString(), {
1331
- headers: requestHeaders
1332
- });
1333
- if (!response.ok) {
1334
- throw new FetchError(
1335
- response.status,
1336
- void 0,
1337
- void 0,
1338
- Object.fromEntries([...response.headers.entries()]),
1339
- fetchUrl.toString()
1340
- );
1341
- }
1342
- const schema = (_a = __privateGet(this, _schema)) != null ? _a : getSchemaFromHeaders(response.headers, {
1343
- required: true,
1344
- url: fetchUrl.toString()
1345
- });
1346
- const { metadata, data: rawData } = yield response.json();
1347
- const data = __privateGet(this, _messageParser).parseSnapshotData(
1348
- rawData,
1349
- schema
1304
+ async fetchSnapshot(opts) {
1305
+ var _a;
1306
+ const { fetchUrl, requestHeaders } = await __privateMethod(this, _ShapeStream_instances, constructUrl_fn).call(this, this.options.url, true, opts);
1307
+ const response = await __privateGet(this, _fetchClient2).call(this, fetchUrl.toString(), {
1308
+ headers: requestHeaders
1309
+ });
1310
+ if (!response.ok) {
1311
+ throw new FetchError(
1312
+ response.status,
1313
+ void 0,
1314
+ void 0,
1315
+ Object.fromEntries([...response.headers.entries()]),
1316
+ fetchUrl.toString()
1350
1317
  );
1351
- return {
1352
- metadata,
1353
- data
1354
- };
1318
+ }
1319
+ const schema = (_a = __privateGet(this, _schema)) != null ? _a : getSchemaFromHeaders(response.headers, {
1320
+ required: true,
1321
+ url: fetchUrl.toString()
1355
1322
  });
1323
+ const { metadata, data: rawData } = await response.json();
1324
+ const data = __privateGet(this, _messageParser).parseSnapshotData(
1325
+ rawData,
1326
+ schema
1327
+ );
1328
+ return {
1329
+ metadata,
1330
+ data
1331
+ };
1356
1332
  }
1357
1333
  };
1358
1334
  _error = new WeakMap();
@@ -1396,379 +1372,369 @@ _ShapeStream_instances = new WeakSet();
1396
1372
  replayMode_get = function() {
1397
1373
  return __privateGet(this, _lastSeenCursor) !== void 0;
1398
1374
  };
1399
- start_fn = function() {
1400
- return __async(this, null, function* () {
1401
- var _a, _b, _c, _d, _e;
1402
- __privateSet(this, _started, true);
1403
- try {
1404
- yield __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1405
- } catch (err) {
1406
- __privateSet(this, _error, err);
1407
- if (__privateGet(this, _onError)) {
1408
- const retryOpts = yield __privateGet(this, _onError).call(this, err);
1409
- if (retryOpts && typeof retryOpts === `object`) {
1410
- if (retryOpts.params) {
1411
- this.options.params = __spreadValues(__spreadValues({}, (_a = this.options.params) != null ? _a : {}), retryOpts.params);
1412
- }
1413
- if (retryOpts.headers) {
1414
- this.options.headers = __spreadValues(__spreadValues({}, (_b = this.options.headers) != null ? _b : {}), retryOpts.headers);
1415
- }
1416
- __privateSet(this, _error, null);
1417
- __privateSet(this, _started, false);
1418
- yield __privateMethod(this, _ShapeStream_instances, start_fn).call(this);
1419
- return;
1375
+ start_fn = async function() {
1376
+ var _a, _b, _c, _d, _e;
1377
+ __privateSet(this, _started, true);
1378
+ try {
1379
+ await __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1380
+ } catch (err) {
1381
+ __privateSet(this, _error, err);
1382
+ if (__privateGet(this, _onError)) {
1383
+ const retryOpts = await __privateGet(this, _onError).call(this, err);
1384
+ if (retryOpts && typeof retryOpts === `object`) {
1385
+ if (retryOpts.params) {
1386
+ this.options.params = __spreadValues(__spreadValues({}, (_a = this.options.params) != null ? _a : {}), retryOpts.params);
1420
1387
  }
1421
- if (err instanceof Error) {
1422
- __privateMethod(this, _ShapeStream_instances, sendErrorToSubscribers_fn).call(this, err);
1388
+ if (retryOpts.headers) {
1389
+ this.options.headers = __spreadValues(__spreadValues({}, (_b = this.options.headers) != null ? _b : {}), retryOpts.headers);
1423
1390
  }
1424
- __privateSet(this, _connected, false);
1425
- (_c = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _c.call(this);
1391
+ __privateSet(this, _error, null);
1392
+ __privateSet(this, _started, false);
1393
+ await __privateMethod(this, _ShapeStream_instances, start_fn).call(this);
1426
1394
  return;
1427
1395
  }
1428
1396
  if (err instanceof Error) {
1429
1397
  __privateMethod(this, _ShapeStream_instances, sendErrorToSubscribers_fn).call(this, err);
1430
1398
  }
1431
1399
  __privateSet(this, _connected, false);
1432
- (_d = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _d.call(this);
1433
- throw err;
1400
+ (_c = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _c.call(this);
1401
+ return;
1402
+ }
1403
+ if (err instanceof Error) {
1404
+ __privateMethod(this, _ShapeStream_instances, sendErrorToSubscribers_fn).call(this, err);
1434
1405
  }
1435
1406
  __privateSet(this, _connected, false);
1436
- (_e = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _e.call(this);
1437
- });
1407
+ (_d = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _d.call(this);
1408
+ throw err;
1409
+ }
1410
+ __privateSet(this, _connected, false);
1411
+ (_e = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _e.call(this);
1438
1412
  };
1439
- requestShape_fn = function() {
1440
- return __async(this, null, function* () {
1441
- var _a, _b;
1442
- if (__privateGet(this, _state) === `pause-requested`) {
1443
- __privateSet(this, _state, `paused`);
1444
- return;
1413
+ requestShape_fn = async function() {
1414
+ var _a, _b;
1415
+ if (__privateGet(this, _state) === `pause-requested`) {
1416
+ __privateSet(this, _state, `paused`);
1417
+ return;
1418
+ }
1419
+ if (!this.options.subscribe && (((_a = this.options.signal) == null ? void 0 : _a.aborted) || __privateGet(this, _isUpToDate))) {
1420
+ return;
1421
+ }
1422
+ const resumingFromPause = __privateGet(this, _state) === `paused`;
1423
+ __privateSet(this, _state, `active`);
1424
+ const { url, signal } = this.options;
1425
+ const { fetchUrl, requestHeaders } = await __privateMethod(this, _ShapeStream_instances, constructUrl_fn).call(this, url, resumingFromPause);
1426
+ const abortListener = await __privateMethod(this, _ShapeStream_instances, createAbortListener_fn).call(this, signal);
1427
+ const requestAbortController = __privateGet(this, _requestAbortController);
1428
+ try {
1429
+ await __privateMethod(this, _ShapeStream_instances, fetchShape_fn).call(this, {
1430
+ fetchUrl,
1431
+ requestAbortController,
1432
+ headers: requestHeaders,
1433
+ resumingFromPause
1434
+ });
1435
+ } catch (e) {
1436
+ if ((e instanceof FetchError || e instanceof FetchBackoffAbortError) && requestAbortController.signal.aborted && requestAbortController.signal.reason === FORCE_DISCONNECT_AND_REFRESH) {
1437
+ return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1445
1438
  }
1446
- if (!this.options.subscribe && (((_a = this.options.signal) == null ? void 0 : _a.aborted) || __privateGet(this, _isUpToDate))) {
1439
+ if (e instanceof FetchBackoffAbortError) {
1440
+ const currentState = __privateGet(this, _state);
1441
+ if (requestAbortController.signal.aborted && requestAbortController.signal.reason === PAUSE_STREAM && currentState === `pause-requested`) {
1442
+ __privateSet(this, _state, `paused`);
1443
+ }
1447
1444
  return;
1448
1445
  }
1449
- const resumingFromPause = __privateGet(this, _state) === `paused`;
1450
- __privateSet(this, _state, `active`);
1451
- const { url, signal } = this.options;
1452
- const { fetchUrl, requestHeaders } = yield __privateMethod(this, _ShapeStream_instances, constructUrl_fn).call(this, url, resumingFromPause);
1453
- const abortListener = yield __privateMethod(this, _ShapeStream_instances, createAbortListener_fn).call(this, signal);
1454
- const requestAbortController = __privateGet(this, _requestAbortController);
1455
- try {
1456
- yield __privateMethod(this, _ShapeStream_instances, fetchShape_fn).call(this, {
1457
- fetchUrl,
1458
- requestAbortController,
1459
- headers: requestHeaders,
1460
- resumingFromPause
1461
- });
1462
- } catch (e) {
1463
- if ((e instanceof FetchError || e instanceof FetchBackoffAbortError) && requestAbortController.signal.aborted && requestAbortController.signal.reason === FORCE_DISCONNECT_AND_REFRESH) {
1464
- return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1465
- }
1466
- if (e instanceof FetchBackoffAbortError) {
1467
- const currentState = __privateGet(this, _state);
1468
- if (requestAbortController.signal.aborted && requestAbortController.signal.reason === PAUSE_STREAM && currentState === `pause-requested`) {
1469
- __privateSet(this, _state, `paused`);
1470
- }
1471
- return;
1472
- }
1473
- if (!(e instanceof FetchError)) throw e;
1474
- if (e.status == 409) {
1475
- if (__privateGet(this, _shapeHandle)) {
1476
- const shapeKey = canonicalShapeKey(fetchUrl);
1477
- expiredShapesCache.markExpired(shapeKey, __privateGet(this, _shapeHandle));
1478
- }
1479
- const newShapeHandle = e.headers[SHAPE_HANDLE_HEADER] || `${__privateGet(this, _shapeHandle)}-next`;
1480
- __privateMethod(this, _ShapeStream_instances, reset_fn).call(this, newShapeHandle);
1481
- yield __privateMethod(this, _ShapeStream_instances, publish_fn).call(this, Array.isArray(e.json) ? e.json : [e.json]);
1482
- return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1483
- } else {
1484
- throw e;
1446
+ if (!(e instanceof FetchError)) throw e;
1447
+ if (e.status == 409) {
1448
+ if (__privateGet(this, _shapeHandle)) {
1449
+ const shapeKey = canonicalShapeKey(fetchUrl);
1450
+ expiredShapesCache.markExpired(shapeKey, __privateGet(this, _shapeHandle));
1485
1451
  }
1486
- } finally {
1487
- if (abortListener && signal) {
1488
- signal.removeEventListener(`abort`, abortListener);
1489
- }
1490
- __privateSet(this, _requestAbortController, void 0);
1452
+ const newShapeHandle = e.headers[SHAPE_HANDLE_HEADER] || `${__privateGet(this, _shapeHandle)}-next`;
1453
+ __privateMethod(this, _ShapeStream_instances, reset_fn).call(this, newShapeHandle);
1454
+ await __privateMethod(this, _ShapeStream_instances, publish_fn).call(this, Array.isArray(e.json) ? e.json : [e.json]);
1455
+ return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1456
+ } else {
1457
+ throw e;
1491
1458
  }
1492
- (_b = __privateGet(this, _tickPromiseResolver)) == null ? void 0 : _b.call(this);
1493
- return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1494
- });
1459
+ } finally {
1460
+ if (abortListener && signal) {
1461
+ signal.removeEventListener(`abort`, abortListener);
1462
+ }
1463
+ __privateSet(this, _requestAbortController, void 0);
1464
+ }
1465
+ (_b = __privateGet(this, _tickPromiseResolver)) == null ? void 0 : _b.call(this);
1466
+ return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1495
1467
  };
1496
- constructUrl_fn = function(url, resumingFromPause, subsetParams) {
1497
- return __async(this, null, function* () {
1498
- var _a, _b, _c, _d;
1499
- const [requestHeaders, params] = yield Promise.all([
1500
- resolveHeaders(this.options.headers),
1501
- this.options.params ? toInternalParams(convertWhereParamsToObj(this.options.params)) : void 0
1502
- ]);
1503
- if (params) validateParams(params);
1504
- const fetchUrl = new URL(url);
1505
- if (params) {
1506
- if (params.table) setQueryParam(fetchUrl, TABLE_QUERY_PARAM, params.table);
1507
- if (params.where && typeof params.where === `string`) {
1508
- const encodedWhere = encodeWhereClause(
1509
- params.where,
1510
- (_a = this.options.columnMapper) == null ? void 0 : _a.encode
1511
- );
1512
- setQueryParam(fetchUrl, WHERE_QUERY_PARAM, encodedWhere);
1513
- }
1514
- if (params.columns) {
1515
- const originalColumns = yield resolveValue((_b = this.options.params) == null ? void 0 : _b.columns);
1516
- if (Array.isArray(originalColumns)) {
1517
- let encodedColumns = originalColumns.map(String);
1518
- if (this.options.columnMapper) {
1519
- encodedColumns = encodedColumns.map(
1520
- this.options.columnMapper.encode
1521
- );
1522
- }
1523
- const serializedColumns = encodedColumns.map(quoteIdentifier).join(`,`);
1524
- setQueryParam(fetchUrl, COLUMNS_QUERY_PARAM, serializedColumns);
1525
- } else {
1526
- setQueryParam(fetchUrl, COLUMNS_QUERY_PARAM, params.columns);
1468
+ constructUrl_fn = async function(url, resumingFromPause, subsetParams) {
1469
+ var _a, _b, _c, _d;
1470
+ const [requestHeaders, params] = await Promise.all([
1471
+ resolveHeaders(this.options.headers),
1472
+ this.options.params ? toInternalParams(convertWhereParamsToObj(this.options.params)) : void 0
1473
+ ]);
1474
+ if (params) validateParams(params);
1475
+ const fetchUrl = new URL(url);
1476
+ if (params) {
1477
+ if (params.table) setQueryParam(fetchUrl, TABLE_QUERY_PARAM, params.table);
1478
+ if (params.where && typeof params.where === `string`) {
1479
+ const encodedWhere = encodeWhereClause(
1480
+ params.where,
1481
+ (_a = this.options.columnMapper) == null ? void 0 : _a.encode
1482
+ );
1483
+ setQueryParam(fetchUrl, WHERE_QUERY_PARAM, encodedWhere);
1484
+ }
1485
+ if (params.columns) {
1486
+ const originalColumns = await resolveValue((_b = this.options.params) == null ? void 0 : _b.columns);
1487
+ if (Array.isArray(originalColumns)) {
1488
+ let encodedColumns = originalColumns.map(String);
1489
+ if (this.options.columnMapper) {
1490
+ encodedColumns = encodedColumns.map(
1491
+ this.options.columnMapper.encode
1492
+ );
1527
1493
  }
1528
- }
1529
- if (params.replica) setQueryParam(fetchUrl, REPLICA_PARAM, params.replica);
1530
- if (params.params)
1531
- setQueryParam(fetchUrl, WHERE_PARAMS_PARAM, params.params);
1532
- const customParams = __spreadValues({}, params);
1533
- delete customParams.table;
1534
- delete customParams.where;
1535
- delete customParams.columns;
1536
- delete customParams.replica;
1537
- delete customParams.params;
1538
- for (const [key, value] of Object.entries(customParams)) {
1539
- setQueryParam(fetchUrl, key, value);
1494
+ const serializedColumns = encodedColumns.map(quoteIdentifier).join(`,`);
1495
+ setQueryParam(fetchUrl, COLUMNS_QUERY_PARAM, serializedColumns);
1496
+ } else {
1497
+ setQueryParam(fetchUrl, COLUMNS_QUERY_PARAM, params.columns);
1540
1498
  }
1541
1499
  }
1542
- if (subsetParams) {
1543
- if (subsetParams.where && typeof subsetParams.where === `string`) {
1544
- const encodedWhere = encodeWhereClause(
1545
- subsetParams.where,
1546
- (_c = this.options.columnMapper) == null ? void 0 : _c.encode
1547
- );
1548
- setQueryParam(fetchUrl, SUBSET_PARAM_WHERE, encodedWhere);
1549
- }
1550
- if (subsetParams.params)
1551
- fetchUrl.searchParams.set(
1552
- SUBSET_PARAM_WHERE_PARAMS,
1553
- JSON.stringify(subsetParams.params)
1554
- );
1555
- if (subsetParams.limit)
1556
- setQueryParam(fetchUrl, SUBSET_PARAM_LIMIT, subsetParams.limit);
1557
- if (subsetParams.offset)
1558
- setQueryParam(fetchUrl, SUBSET_PARAM_OFFSET, subsetParams.offset);
1559
- if (subsetParams.orderBy && typeof subsetParams.orderBy === `string`) {
1560
- const encodedOrderBy = encodeWhereClause(
1561
- subsetParams.orderBy,
1562
- (_d = this.options.columnMapper) == null ? void 0 : _d.encode
1563
- );
1564
- setQueryParam(fetchUrl, SUBSET_PARAM_ORDER_BY, encodedOrderBy);
1565
- }
1500
+ if (params.replica) setQueryParam(fetchUrl, REPLICA_PARAM, params.replica);
1501
+ if (params.params)
1502
+ setQueryParam(fetchUrl, WHERE_PARAMS_PARAM, params.params);
1503
+ const customParams = __spreadValues({}, params);
1504
+ delete customParams.table;
1505
+ delete customParams.where;
1506
+ delete customParams.columns;
1507
+ delete customParams.replica;
1508
+ delete customParams.params;
1509
+ for (const [key, value] of Object.entries(customParams)) {
1510
+ setQueryParam(fetchUrl, key, value);
1566
1511
  }
1567
- fetchUrl.searchParams.set(OFFSET_QUERY_PARAM, __privateGet(this, _lastOffset));
1568
- fetchUrl.searchParams.set(LOG_MODE_QUERY_PARAM, __privateGet(this, _mode));
1569
- const isSnapshotRequest = subsetParams !== void 0;
1570
- if (__privateGet(this, _isUpToDate) && !isSnapshotRequest) {
1571
- if (!__privateGet(this, _isRefreshing) && !resumingFromPause) {
1572
- fetchUrl.searchParams.set(LIVE_QUERY_PARAM, `true`);
1573
- }
1574
- fetchUrl.searchParams.set(
1575
- LIVE_CACHE_BUSTER_QUERY_PARAM,
1576
- __privateGet(this, _liveCacheBuster)
1512
+ }
1513
+ if (subsetParams) {
1514
+ if (subsetParams.where && typeof subsetParams.where === `string`) {
1515
+ const encodedWhere = encodeWhereClause(
1516
+ subsetParams.where,
1517
+ (_c = this.options.columnMapper) == null ? void 0 : _c.encode
1577
1518
  );
1519
+ setQueryParam(fetchUrl, SUBSET_PARAM_WHERE, encodedWhere);
1578
1520
  }
1579
- if (__privateGet(this, _shapeHandle)) {
1580
- fetchUrl.searchParams.set(SHAPE_HANDLE_QUERY_PARAM, __privateGet(this, _shapeHandle));
1521
+ if (subsetParams.params)
1522
+ fetchUrl.searchParams.set(
1523
+ SUBSET_PARAM_WHERE_PARAMS,
1524
+ JSON.stringify(subsetParams.params)
1525
+ );
1526
+ if (subsetParams.limit)
1527
+ setQueryParam(fetchUrl, SUBSET_PARAM_LIMIT, subsetParams.limit);
1528
+ if (subsetParams.offset)
1529
+ setQueryParam(fetchUrl, SUBSET_PARAM_OFFSET, subsetParams.offset);
1530
+ if (subsetParams.orderBy && typeof subsetParams.orderBy === `string`) {
1531
+ const encodedOrderBy = encodeWhereClause(
1532
+ subsetParams.orderBy,
1533
+ (_d = this.options.columnMapper) == null ? void 0 : _d.encode
1534
+ );
1535
+ setQueryParam(fetchUrl, SUBSET_PARAM_ORDER_BY, encodedOrderBy);
1581
1536
  }
1582
- const shapeKey = canonicalShapeKey(fetchUrl);
1583
- const expiredHandle = expiredShapesCache.getExpiredHandle(shapeKey);
1584
- if (expiredHandle) {
1585
- fetchUrl.searchParams.set(EXPIRED_HANDLE_QUERY_PARAM, expiredHandle);
1537
+ }
1538
+ fetchUrl.searchParams.set(OFFSET_QUERY_PARAM, __privateGet(this, _lastOffset));
1539
+ fetchUrl.searchParams.set(LOG_MODE_QUERY_PARAM, __privateGet(this, _mode));
1540
+ const isSnapshotRequest = subsetParams !== void 0;
1541
+ if (__privateGet(this, _isUpToDate) && !isSnapshotRequest) {
1542
+ if (!__privateGet(this, _isRefreshing) && !resumingFromPause) {
1543
+ fetchUrl.searchParams.set(LIVE_QUERY_PARAM, `true`);
1586
1544
  }
1587
- fetchUrl.searchParams.sort();
1588
- return {
1589
- fetchUrl,
1590
- requestHeaders
1591
- };
1592
- });
1545
+ fetchUrl.searchParams.set(
1546
+ LIVE_CACHE_BUSTER_QUERY_PARAM,
1547
+ __privateGet(this, _liveCacheBuster)
1548
+ );
1549
+ }
1550
+ if (__privateGet(this, _shapeHandle)) {
1551
+ fetchUrl.searchParams.set(SHAPE_HANDLE_QUERY_PARAM, __privateGet(this, _shapeHandle));
1552
+ }
1553
+ const shapeKey = canonicalShapeKey(fetchUrl);
1554
+ const expiredHandle = expiredShapesCache.getExpiredHandle(shapeKey);
1555
+ if (expiredHandle) {
1556
+ fetchUrl.searchParams.set(EXPIRED_HANDLE_QUERY_PARAM, expiredHandle);
1557
+ }
1558
+ fetchUrl.searchParams.sort();
1559
+ return {
1560
+ fetchUrl,
1561
+ requestHeaders
1562
+ };
1593
1563
  };
1594
- createAbortListener_fn = function(signal) {
1595
- return __async(this, null, function* () {
1596
- var _a;
1597
- __privateSet(this, _requestAbortController, new AbortController());
1598
- if (signal) {
1599
- const abortListener = () => {
1600
- var _a2;
1601
- (_a2 = __privateGet(this, _requestAbortController)) == null ? void 0 : _a2.abort(signal.reason);
1602
- };
1603
- signal.addEventListener(`abort`, abortListener, { once: true });
1604
- if (signal.aborted) {
1605
- (_a = __privateGet(this, _requestAbortController)) == null ? void 0 : _a.abort(signal.reason);
1606
- }
1607
- return abortListener;
1564
+ createAbortListener_fn = async function(signal) {
1565
+ var _a;
1566
+ __privateSet(this, _requestAbortController, new AbortController());
1567
+ if (signal) {
1568
+ const abortListener = () => {
1569
+ var _a2;
1570
+ (_a2 = __privateGet(this, _requestAbortController)) == null ? void 0 : _a2.abort(signal.reason);
1571
+ };
1572
+ signal.addEventListener(`abort`, abortListener, { once: true });
1573
+ if (signal.aborted) {
1574
+ (_a = __privateGet(this, _requestAbortController)) == null ? void 0 : _a.abort(signal.reason);
1608
1575
  }
1609
- });
1576
+ return abortListener;
1577
+ }
1610
1578
  };
1611
- onInitialResponse_fn = function(response) {
1612
- return __async(this, null, function* () {
1613
- var _a;
1614
- const { headers, status } = response;
1615
- const shapeHandle = headers.get(SHAPE_HANDLE_HEADER);
1616
- if (shapeHandle) {
1579
+ onInitialResponse_fn = async function(response) {
1580
+ var _a;
1581
+ const { headers, status } = response;
1582
+ const shapeHandle = headers.get(SHAPE_HANDLE_HEADER);
1583
+ if (shapeHandle) {
1584
+ const shapeKey = __privateGet(this, _currentFetchUrl) ? canonicalShapeKey(__privateGet(this, _currentFetchUrl)) : null;
1585
+ const expiredHandle = shapeKey ? expiredShapesCache.getExpiredHandle(shapeKey) : null;
1586
+ if (shapeHandle !== expiredHandle) {
1617
1587
  __privateSet(this, _shapeHandle, shapeHandle);
1588
+ } else {
1589
+ console.warn(
1590
+ `[Electric] Received stale cached response with expired shape handle. This should not happen and indicates a proxy/CDN caching misconfiguration. The response contained handle "${shapeHandle}" which was previously marked as expired. Check that your proxy includes all query parameters (especially 'handle' and 'offset') in its cache key. Ignoring the stale handle and continuing with handle "${__privateGet(this, _shapeHandle)}".`
1591
+ );
1618
1592
  }
1619
- const lastOffset = headers.get(CHUNK_LAST_OFFSET_HEADER);
1620
- if (lastOffset) {
1621
- __privateSet(this, _lastOffset, lastOffset);
1622
- }
1623
- const liveCacheBuster = headers.get(LIVE_CACHE_BUSTER_HEADER);
1624
- if (liveCacheBuster) {
1625
- __privateSet(this, _liveCacheBuster, liveCacheBuster);
1626
- }
1627
- __privateSet(this, _schema, (_a = __privateGet(this, _schema)) != null ? _a : getSchemaFromHeaders(headers));
1628
- if (status === 204) {
1629
- __privateSet(this, _lastSyncedAt, Date.now());
1630
- }
1631
- });
1593
+ }
1594
+ const lastOffset = headers.get(CHUNK_LAST_OFFSET_HEADER);
1595
+ if (lastOffset) {
1596
+ __privateSet(this, _lastOffset, lastOffset);
1597
+ }
1598
+ const liveCacheBuster = headers.get(LIVE_CACHE_BUSTER_HEADER);
1599
+ if (liveCacheBuster) {
1600
+ __privateSet(this, _liveCacheBuster, liveCacheBuster);
1601
+ }
1602
+ __privateSet(this, _schema, (_a = __privateGet(this, _schema)) != null ? _a : getSchemaFromHeaders(headers));
1603
+ if (status === 204) {
1604
+ __privateSet(this, _lastSyncedAt, Date.now());
1605
+ }
1632
1606
  };
1633
- onMessages_fn = function(batch, isSseMessage = false) {
1634
- return __async(this, null, function* () {
1635
- var _a;
1636
- if (batch.length > 0) {
1637
- __privateSet(this, _isMidStream, true);
1638
- const lastMessage = batch[batch.length - 1];
1639
- if (isUpToDateMessage(lastMessage)) {
1640
- if (isSseMessage) {
1641
- const offset = getOffset(lastMessage);
1642
- if (offset) {
1643
- __privateSet(this, _lastOffset, offset);
1644
- }
1645
- }
1646
- __privateSet(this, _lastSyncedAt, Date.now());
1647
- __privateSet(this, _isUpToDate, true);
1648
- __privateSet(this, _isMidStream, false);
1649
- (_a = __privateGet(this, _midStreamPromiseResolver)) == null ? void 0 : _a.call(this);
1650
- if (__privateGet(this, _ShapeStream_instances, replayMode_get) && !isSseMessage) {
1651
- const currentCursor = __privateGet(this, _liveCacheBuster);
1652
- if (currentCursor === __privateGet(this, _lastSeenCursor)) {
1653
- return;
1654
- }
1655
- }
1656
- __privateSet(this, _lastSeenCursor, void 0);
1657
- if (__privateGet(this, _currentFetchUrl)) {
1658
- const shapeKey = canonicalShapeKey(__privateGet(this, _currentFetchUrl));
1659
- upToDateTracker.recordUpToDate(shapeKey, __privateGet(this, _liveCacheBuster));
1607
+ onMessages_fn = async function(batch, isSseMessage = false) {
1608
+ var _a;
1609
+ if (batch.length > 0) {
1610
+ __privateSet(this, _isMidStream, true);
1611
+ const lastMessage = batch[batch.length - 1];
1612
+ if (isUpToDateMessage(lastMessage)) {
1613
+ if (isSseMessage) {
1614
+ const offset = getOffset(lastMessage);
1615
+ if (offset) {
1616
+ __privateSet(this, _lastOffset, offset);
1660
1617
  }
1661
1618
  }
1662
- const messagesToProcess = batch.filter((message) => {
1663
- if (isChangeMessage(message)) {
1664
- return !__privateGet(this, _snapshotTracker).shouldRejectMessage(message);
1619
+ __privateSet(this, _lastSyncedAt, Date.now());
1620
+ __privateSet(this, _isUpToDate, true);
1621
+ __privateSet(this, _isMidStream, false);
1622
+ (_a = __privateGet(this, _midStreamPromiseResolver)) == null ? void 0 : _a.call(this);
1623
+ if (__privateGet(this, _ShapeStream_instances, replayMode_get) && !isSseMessage) {
1624
+ const currentCursor = __privateGet(this, _liveCacheBuster);
1625
+ if (currentCursor === __privateGet(this, _lastSeenCursor)) {
1626
+ return;
1665
1627
  }
1666
- return true;
1667
- });
1668
- yield __privateMethod(this, _ShapeStream_instances, publish_fn).call(this, messagesToProcess);
1669
- }
1670
- });
1671
- };
1672
- fetchShape_fn = function(opts) {
1673
- return __async(this, null, function* () {
1674
- var _a;
1675
- __privateSet(this, _currentFetchUrl, opts.fetchUrl);
1676
- if (!__privateGet(this, _isUpToDate) && !__privateGet(this, _ShapeStream_instances, replayMode_get)) {
1677
- const shapeKey = canonicalShapeKey(opts.fetchUrl);
1678
- const lastSeenCursor = upToDateTracker.shouldEnterReplayMode(shapeKey);
1679
- if (lastSeenCursor) {
1680
- __privateSet(this, _lastSeenCursor, lastSeenCursor);
1628
+ }
1629
+ __privateSet(this, _lastSeenCursor, void 0);
1630
+ if (__privateGet(this, _currentFetchUrl)) {
1631
+ const shapeKey = canonicalShapeKey(__privateGet(this, _currentFetchUrl));
1632
+ upToDateTracker.recordUpToDate(shapeKey, __privateGet(this, _liveCacheBuster));
1681
1633
  }
1682
1634
  }
1683
- const useSse = (_a = this.options.liveSse) != null ? _a : this.options.experimentalLiveSse;
1684
- if (__privateGet(this, _isUpToDate) && useSse && !__privateGet(this, _isRefreshing) && !opts.resumingFromPause && !__privateGet(this, _sseFallbackToLongPolling)) {
1685
- opts.fetchUrl.searchParams.set(EXPERIMENTAL_LIVE_SSE_QUERY_PARAM, `true`);
1686
- opts.fetchUrl.searchParams.set(LIVE_SSE_QUERY_PARAM, `true`);
1687
- return __privateMethod(this, _ShapeStream_instances, requestShapeSSE_fn).call(this, opts);
1635
+ const messagesToProcess = batch.filter((message) => {
1636
+ if (isChangeMessage(message)) {
1637
+ return !__privateGet(this, _snapshotTracker).shouldRejectMessage(message);
1638
+ }
1639
+ return true;
1640
+ });
1641
+ await __privateMethod(this, _ShapeStream_instances, publish_fn).call(this, messagesToProcess);
1642
+ }
1643
+ };
1644
+ fetchShape_fn = async function(opts) {
1645
+ var _a;
1646
+ __privateSet(this, _currentFetchUrl, opts.fetchUrl);
1647
+ if (!__privateGet(this, _isUpToDate) && !__privateGet(this, _ShapeStream_instances, replayMode_get)) {
1648
+ const shapeKey = canonicalShapeKey(opts.fetchUrl);
1649
+ const lastSeenCursor = upToDateTracker.shouldEnterReplayMode(shapeKey);
1650
+ if (lastSeenCursor) {
1651
+ __privateSet(this, _lastSeenCursor, lastSeenCursor);
1688
1652
  }
1689
- return __privateMethod(this, _ShapeStream_instances, requestShapeLongPoll_fn).call(this, opts);
1690
- });
1653
+ }
1654
+ const useSse = (_a = this.options.liveSse) != null ? _a : this.options.experimentalLiveSse;
1655
+ if (__privateGet(this, _isUpToDate) && useSse && !__privateGet(this, _isRefreshing) && !opts.resumingFromPause && !__privateGet(this, _sseFallbackToLongPolling)) {
1656
+ opts.fetchUrl.searchParams.set(EXPERIMENTAL_LIVE_SSE_QUERY_PARAM, `true`);
1657
+ opts.fetchUrl.searchParams.set(LIVE_SSE_QUERY_PARAM, `true`);
1658
+ return __privateMethod(this, _ShapeStream_instances, requestShapeSSE_fn).call(this, opts);
1659
+ }
1660
+ return __privateMethod(this, _ShapeStream_instances, requestShapeLongPoll_fn).call(this, opts);
1691
1661
  };
1692
- requestShapeLongPoll_fn = function(opts) {
1693
- return __async(this, null, function* () {
1694
- const { fetchUrl, requestAbortController, headers } = opts;
1695
- const response = yield __privateGet(this, _fetchClient2).call(this, fetchUrl.toString(), {
1696
- signal: requestAbortController.signal,
1697
- headers
1698
- });
1699
- __privateSet(this, _connected, true);
1700
- yield __privateMethod(this, _ShapeStream_instances, onInitialResponse_fn).call(this, response);
1701
- const schema = __privateGet(this, _schema);
1702
- const res = yield response.text();
1703
- const messages = res || `[]`;
1704
- const batch = __privateGet(this, _messageParser).parse(messages, schema);
1705
- yield __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, batch);
1662
+ requestShapeLongPoll_fn = async function(opts) {
1663
+ const { fetchUrl, requestAbortController, headers } = opts;
1664
+ const response = await __privateGet(this, _fetchClient2).call(this, fetchUrl.toString(), {
1665
+ signal: requestAbortController.signal,
1666
+ headers
1706
1667
  });
1668
+ __privateSet(this, _connected, true);
1669
+ await __privateMethod(this, _ShapeStream_instances, onInitialResponse_fn).call(this, response);
1670
+ const schema = __privateGet(this, _schema);
1671
+ const res = await response.text();
1672
+ const messages = res || `[]`;
1673
+ const batch = __privateGet(this, _messageParser).parse(messages, schema);
1674
+ await __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, batch);
1707
1675
  };
1708
- requestShapeSSE_fn = function(opts) {
1709
- return __async(this, null, function* () {
1710
- const { fetchUrl, requestAbortController, headers } = opts;
1711
- const fetch2 = __privateGet(this, _sseFetchClient);
1712
- __privateSet(this, _lastSseConnectionStartTime, Date.now());
1713
- const sseHeaders = __spreadProps(__spreadValues({}, headers), {
1714
- Accept: `text/event-stream`
1715
- });
1716
- try {
1717
- let buffer = [];
1718
- yield (0, import_fetch_event_source.fetchEventSource)(fetchUrl.toString(), {
1719
- headers: sseHeaders,
1720
- fetch: fetch2,
1721
- onopen: (response) => __async(this, null, function* () {
1722
- __privateSet(this, _connected, true);
1723
- yield __privateMethod(this, _ShapeStream_instances, onInitialResponse_fn).call(this, response);
1724
- }),
1725
- onmessage: (event) => {
1726
- if (event.data) {
1727
- const schema = __privateGet(this, _schema);
1728
- const message = __privateGet(this, _messageParser).parse(
1729
- event.data,
1730
- schema
1731
- );
1732
- buffer.push(message);
1733
- if (isUpToDateMessage(message)) {
1734
- __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, buffer, true);
1735
- buffer = [];
1736
- }
1737
- }
1738
- },
1739
- onerror: (error) => {
1740
- throw error;
1741
- },
1742
- signal: requestAbortController.signal
1743
- });
1744
- } catch (error) {
1745
- if (requestAbortController.signal.aborted) {
1746
- throw new FetchBackoffAbortError();
1747
- }
1748
- throw error;
1749
- } finally {
1750
- const connectionDuration = Date.now() - __privateGet(this, _lastSseConnectionStartTime);
1751
- const wasAborted = requestAbortController.signal.aborted;
1752
- if (connectionDuration < __privateGet(this, _minSseConnectionDuration) && !wasAborted) {
1753
- __privateWrapper(this, _consecutiveShortSseConnections)._++;
1754
- if (__privateGet(this, _consecutiveShortSseConnections) >= __privateGet(this, _maxShortSseConnections)) {
1755
- __privateSet(this, _sseFallbackToLongPolling, true);
1756
- console.warn(
1757
- `[Electric] SSE connections are closing immediately (possibly due to proxy buffering or misconfiguration). Falling back to long polling. Your proxy must support streaming SSE responses (not buffer the complete response). Configuration: Nginx add 'X-Accel-Buffering: no', Caddy add 'flush_interval -1' to reverse_proxy. Note: Do NOT disable caching entirely - Electric uses cache headers to enable request collapsing for efficiency.`
1758
- );
1759
- } else {
1760
- const maxDelay = Math.min(
1761
- __privateGet(this, _sseBackoffMaxDelay),
1762
- __privateGet(this, _sseBackoffBaseDelay) * Math.pow(2, __privateGet(this, _consecutiveShortSseConnections))
1676
+ requestShapeSSE_fn = async function(opts) {
1677
+ const { fetchUrl, requestAbortController, headers } = opts;
1678
+ const fetch2 = __privateGet(this, _sseFetchClient);
1679
+ __privateSet(this, _lastSseConnectionStartTime, Date.now());
1680
+ const sseHeaders = __spreadProps(__spreadValues({}, headers), {
1681
+ Accept: `text/event-stream`
1682
+ });
1683
+ try {
1684
+ let buffer = [];
1685
+ await (0, import_fetch_event_source.fetchEventSource)(fetchUrl.toString(), {
1686
+ headers: sseHeaders,
1687
+ fetch: fetch2,
1688
+ onopen: async (response) => {
1689
+ __privateSet(this, _connected, true);
1690
+ await __privateMethod(this, _ShapeStream_instances, onInitialResponse_fn).call(this, response);
1691
+ },
1692
+ onmessage: (event) => {
1693
+ if (event.data) {
1694
+ const schema = __privateGet(this, _schema);
1695
+ const message = __privateGet(this, _messageParser).parse(
1696
+ event.data,
1697
+ schema
1763
1698
  );
1764
- const delayMs = Math.floor(Math.random() * maxDelay);
1765
- yield new Promise((resolve) => setTimeout(resolve, delayMs));
1699
+ buffer.push(message);
1700
+ if (isUpToDateMessage(message)) {
1701
+ __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, buffer, true);
1702
+ buffer = [];
1703
+ }
1766
1704
  }
1767
- } else if (connectionDuration >= __privateGet(this, _minSseConnectionDuration)) {
1768
- __privateSet(this, _consecutiveShortSseConnections, 0);
1705
+ },
1706
+ onerror: (error) => {
1707
+ throw error;
1708
+ },
1709
+ signal: requestAbortController.signal
1710
+ });
1711
+ } catch (error) {
1712
+ if (requestAbortController.signal.aborted) {
1713
+ throw new FetchBackoffAbortError();
1714
+ }
1715
+ throw error;
1716
+ } finally {
1717
+ const connectionDuration = Date.now() - __privateGet(this, _lastSseConnectionStartTime);
1718
+ const wasAborted = requestAbortController.signal.aborted;
1719
+ if (connectionDuration < __privateGet(this, _minSseConnectionDuration) && !wasAborted) {
1720
+ __privateWrapper(this, _consecutiveShortSseConnections)._++;
1721
+ if (__privateGet(this, _consecutiveShortSseConnections) >= __privateGet(this, _maxShortSseConnections)) {
1722
+ __privateSet(this, _sseFallbackToLongPolling, true);
1723
+ console.warn(
1724
+ `[Electric] SSE connections are closing immediately (possibly due to proxy buffering or misconfiguration). Falling back to long polling. Your proxy must support streaming SSE responses (not buffer the complete response). Configuration: Nginx add 'X-Accel-Buffering: no', Caddy add 'flush_interval -1' to reverse_proxy. Note: Do NOT disable caching entirely - Electric uses cache headers to enable request collapsing for efficiency.`
1725
+ );
1726
+ } else {
1727
+ const maxDelay = Math.min(
1728
+ __privateGet(this, _sseBackoffMaxDelay),
1729
+ __privateGet(this, _sseBackoffBaseDelay) * Math.pow(2, __privateGet(this, _consecutiveShortSseConnections))
1730
+ );
1731
+ const delayMs = Math.floor(Math.random() * maxDelay);
1732
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
1769
1733
  }
1734
+ } else if (connectionDuration >= __privateGet(this, _minSseConnectionDuration)) {
1735
+ __privateSet(this, _consecutiveShortSseConnections, 0);
1770
1736
  }
1771
- });
1737
+ }
1772
1738
  };
1773
1739
  pause_fn = function() {
1774
1740
  var _a;
@@ -1778,65 +1744,63 @@ pause_fn = function() {
1778
1744
  }
1779
1745
  };
1780
1746
  resume_fn = function() {
1747
+ var _a;
1781
1748
  if (__privateGet(this, _started) && (__privateGet(this, _state) === `paused` || __privateGet(this, _state) === `pause-requested`)) {
1749
+ if ((_a = this.options.signal) == null ? void 0 : _a.aborted) {
1750
+ return;
1751
+ }
1782
1752
  if (__privateGet(this, _state) === `pause-requested`) {
1783
1753
  __privateSet(this, _state, `active`);
1784
1754
  }
1785
1755
  __privateMethod(this, _ShapeStream_instances, start_fn).call(this);
1786
1756
  }
1787
1757
  };
1788
- nextTick_fn = function() {
1789
- return __async(this, null, function* () {
1790
- if (__privateGet(this, _tickPromise)) {
1791
- return __privateGet(this, _tickPromise);
1792
- }
1793
- __privateSet(this, _tickPromise, new Promise((resolve, reject) => {
1794
- __privateSet(this, _tickPromiseResolver, resolve);
1795
- __privateSet(this, _tickPromiseRejecter, reject);
1796
- }));
1797
- __privateGet(this, _tickPromise).finally(() => {
1798
- __privateSet(this, _tickPromise, void 0);
1799
- __privateSet(this, _tickPromiseResolver, void 0);
1800
- __privateSet(this, _tickPromiseRejecter, void 0);
1801
- });
1758
+ nextTick_fn = async function() {
1759
+ if (__privateGet(this, _tickPromise)) {
1802
1760
  return __privateGet(this, _tickPromise);
1761
+ }
1762
+ __privateSet(this, _tickPromise, new Promise((resolve, reject) => {
1763
+ __privateSet(this, _tickPromiseResolver, resolve);
1764
+ __privateSet(this, _tickPromiseRejecter, reject);
1765
+ }));
1766
+ __privateGet(this, _tickPromise).finally(() => {
1767
+ __privateSet(this, _tickPromise, void 0);
1768
+ __privateSet(this, _tickPromiseResolver, void 0);
1769
+ __privateSet(this, _tickPromiseRejecter, void 0);
1803
1770
  });
1771
+ return __privateGet(this, _tickPromise);
1804
1772
  };
1805
- waitForStreamEnd_fn = function() {
1806
- return __async(this, null, function* () {
1807
- if (!__privateGet(this, _isMidStream)) {
1808
- return;
1809
- }
1810
- if (__privateGet(this, _midStreamPromise)) {
1811
- return __privateGet(this, _midStreamPromise);
1812
- }
1813
- __privateSet(this, _midStreamPromise, new Promise((resolve) => {
1814
- __privateSet(this, _midStreamPromiseResolver, resolve);
1815
- }));
1816
- __privateGet(this, _midStreamPromise).finally(() => {
1817
- __privateSet(this, _midStreamPromise, void 0);
1818
- __privateSet(this, _midStreamPromiseResolver, void 0);
1819
- });
1773
+ waitForStreamEnd_fn = async function() {
1774
+ if (!__privateGet(this, _isMidStream)) {
1775
+ return;
1776
+ }
1777
+ if (__privateGet(this, _midStreamPromise)) {
1820
1778
  return __privateGet(this, _midStreamPromise);
1779
+ }
1780
+ __privateSet(this, _midStreamPromise, new Promise((resolve) => {
1781
+ __privateSet(this, _midStreamPromiseResolver, resolve);
1782
+ }));
1783
+ __privateGet(this, _midStreamPromise).finally(() => {
1784
+ __privateSet(this, _midStreamPromise, void 0);
1785
+ __privateSet(this, _midStreamPromiseResolver, void 0);
1821
1786
  });
1787
+ return __privateGet(this, _midStreamPromise);
1822
1788
  };
1823
- publish_fn = function(messages) {
1824
- return __async(this, null, function* () {
1825
- __privateSet(this, _messageChain, __privateGet(this, _messageChain).then(
1826
- () => Promise.all(
1827
- Array.from(__privateGet(this, _subscribers).values()).map((_0) => __async(this, [_0], function* ([callback, __]) {
1828
- try {
1829
- yield callback(messages);
1830
- } catch (err) {
1831
- queueMicrotask(() => {
1832
- throw err;
1833
- });
1834
- }
1835
- }))
1836
- )
1837
- ));
1838
- return __privateGet(this, _messageChain);
1839
- });
1789
+ publish_fn = async function(messages) {
1790
+ __privateSet(this, _messageChain, __privateGet(this, _messageChain).then(
1791
+ () => Promise.all(
1792
+ Array.from(__privateGet(this, _subscribers).values()).map(async ([callback, __]) => {
1793
+ try {
1794
+ await callback(messages);
1795
+ } catch (err) {
1796
+ queueMicrotask(() => {
1797
+ throw err;
1798
+ });
1799
+ }
1800
+ })
1801
+ )
1802
+ ));
1803
+ return __privateGet(this, _messageChain);
1840
1804
  };
1841
1805
  sendErrorToSubscribers_fn = function(error) {
1842
1806
  __privateGet(this, _subscribers).forEach(([_, errorFn]) => {
@@ -2008,13 +1972,11 @@ var Shape = class {
2008
1972
  * Request a snapshot for subset of data. Only available when mode is changes_only.
2009
1973
  * Returns void; data will be emitted via the stream and processed by this Shape.
2010
1974
  */
2011
- requestSnapshot(params) {
2012
- return __async(this, null, function* () {
2013
- const key = JSON.stringify(params);
2014
- __privateGet(this, _requestedSubSnapshots).add(key);
2015
- yield __privateMethod(this, _Shape_instances, awaitUpToDate_fn).call(this);
2016
- yield this.stream.requestSnapshot(params);
2017
- });
1975
+ async requestSnapshot(params) {
1976
+ const key = JSON.stringify(params);
1977
+ __privateGet(this, _requestedSubSnapshots).add(key);
1978
+ await __privateMethod(this, _Shape_instances, awaitUpToDate_fn).call(this);
1979
+ await this.stream.requestSnapshot(params);
2018
1980
  }
2019
1981
  subscribe(callback) {
2020
1982
  const subscriptionId = Math.random();
@@ -2096,38 +2058,34 @@ process_fn = function(messages) {
2096
2058
  });
2097
2059
  if (shouldNotify) __privateMethod(this, _Shape_instances, notify_fn).call(this);
2098
2060
  };
2099
- reexecuteSnapshots_fn = function() {
2100
- return __async(this, null, function* () {
2101
- yield __privateMethod(this, _Shape_instances, awaitUpToDate_fn).call(this);
2102
- yield Promise.all(
2103
- Array.from(__privateGet(this, _requestedSubSnapshots)).map((jsonParams) => __async(this, null, function* () {
2104
- try {
2105
- const snapshot = JSON.parse(jsonParams);
2106
- yield this.stream.requestSnapshot(snapshot);
2107
- } catch (_) {
2108
- }
2109
- }))
2110
- );
2111
- });
2061
+ reexecuteSnapshots_fn = async function() {
2062
+ await __privateMethod(this, _Shape_instances, awaitUpToDate_fn).call(this);
2063
+ await Promise.all(
2064
+ Array.from(__privateGet(this, _requestedSubSnapshots)).map(async (jsonParams) => {
2065
+ try {
2066
+ const snapshot = JSON.parse(jsonParams);
2067
+ await this.stream.requestSnapshot(snapshot);
2068
+ } catch (_) {
2069
+ }
2070
+ })
2071
+ );
2112
2072
  };
2113
- awaitUpToDate_fn = function() {
2114
- return __async(this, null, function* () {
2115
- if (this.stream.isUpToDate) return;
2116
- yield new Promise((resolve) => {
2117
- const check = () => {
2118
- if (this.stream.isUpToDate) {
2119
- clearInterval(interval);
2120
- unsub();
2121
- resolve();
2122
- }
2123
- };
2124
- const interval = setInterval(check, 10);
2125
- const unsub = this.stream.subscribe(
2126
- () => check(),
2127
- () => check()
2128
- );
2129
- check();
2130
- });
2073
+ awaitUpToDate_fn = async function() {
2074
+ if (this.stream.isUpToDate) return;
2075
+ await new Promise((resolve) => {
2076
+ const check = () => {
2077
+ if (this.stream.isUpToDate) {
2078
+ clearInterval(interval);
2079
+ unsub();
2080
+ resolve();
2081
+ }
2082
+ };
2083
+ const interval = setInterval(check, 10);
2084
+ const unsub = this.stream.subscribe(
2085
+ () => check(),
2086
+ () => check()
2087
+ );
2088
+ check();
2131
2089
  });
2132
2090
  };
2133
2091
  updateShapeStatus_fn = function(status) {