@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.
package/dist/index.mjs CHANGED
@@ -45,26 +45,6 @@ var __privateWrapper = (obj, member, setter, getter) => ({
45
45
  return __privateGet(obj, member, getter);
46
46
  }
47
47
  });
48
- var __async = (__this, __arguments, generator) => {
49
- return new Promise((resolve, reject) => {
50
- var fulfilled = (value) => {
51
- try {
52
- step(generator.next(value));
53
- } catch (e) {
54
- reject(e);
55
- }
56
- };
57
- var rejected = (value) => {
58
- try {
59
- step(generator.throw(value));
60
- } catch (e) {
61
- reject(e);
62
- }
63
- };
64
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
65
- step((generator = generator.apply(__this, __arguments)).next());
66
- });
67
- };
68
48
 
69
49
  // src/error.ts
70
50
  var FetchError = class _FetchError extends Error {
@@ -79,22 +59,20 @@ var FetchError = class _FetchError extends Error {
79
59
  this.json = json;
80
60
  this.headers = headers;
81
61
  }
82
- static fromResponse(response, url) {
83
- return __async(this, null, function* () {
84
- const status = response.status;
85
- const headers = Object.fromEntries([...response.headers.entries()]);
86
- let text = void 0;
87
- let json = void 0;
88
- const contentType = response.headers.get(`content-type`);
89
- if (!response.bodyUsed) {
90
- if (contentType && contentType.includes(`application/json`)) {
91
- json = yield response.json();
92
- } else {
93
- text = yield response.text();
94
- }
62
+ static async fromResponse(response, url) {
63
+ const status = response.status;
64
+ const headers = Object.fromEntries([...response.headers.entries()]);
65
+ let text = void 0;
66
+ let json = void 0;
67
+ const contentType = response.headers.get(`content-type`);
68
+ if (!response.bodyUsed) {
69
+ if (contentType && contentType.includes(`application/json`)) {
70
+ json = await response.json();
71
+ } else {
72
+ text = await response.text();
95
73
  }
96
- return new _FetchError(status, text, json, headers, url);
97
- });
74
+ }
75
+ return new _FetchError(status, text, json, headers, url);
98
76
  }
99
77
  };
100
78
  var FetchBackoffAbortError = class extends Error {
@@ -451,11 +429,9 @@ function isUpToDateMessage(message) {
451
429
  return isControlMessage(message) && message.headers.control === `up-to-date`;
452
430
  }
453
431
  function getOffset(message) {
432
+ if (message.headers.control != `up-to-date`) return;
454
433
  const lsn = message.headers.global_last_seen_lsn;
455
- if (!lsn) {
456
- return;
457
- }
458
- return `${lsn}_0`;
434
+ return lsn ? `${lsn}_0` : void 0;
459
435
  }
460
436
  function isVisibleInSnapshot(txid, snapshot) {
461
437
  const xid = BigInt(txid);
@@ -538,7 +514,7 @@ function createFetchWithBackoff(fetchClient, backoffOptions = BackoffDefaults) {
538
514
  onFailedAttempt,
539
515
  maxRetries = Infinity
540
516
  } = backoffOptions;
541
- return (...args) => __async(this, null, function* () {
517
+ return async (...args) => {
542
518
  var _a;
543
519
  const url = args[0];
544
520
  const options = args[1];
@@ -546,11 +522,11 @@ function createFetchWithBackoff(fetchClient, backoffOptions = BackoffDefaults) {
546
522
  let attempt = 0;
547
523
  while (true) {
548
524
  try {
549
- const result = yield fetchClient(...args);
525
+ const result = await fetchClient(...args);
550
526
  if (result.ok) {
551
527
  return result;
552
528
  }
553
- const err = yield FetchError.fromResponse(result, url.toString());
529
+ const err = await FetchError.fromResponse(result, url.toString());
554
530
  throw err;
555
531
  } catch (e) {
556
532
  onFailedAttempt == null ? void 0 : onFailedAttempt();
@@ -578,24 +554,24 @@ function createFetchWithBackoff(fetchClient, backoffOptions = BackoffDefaults) {
578
554
  `Retry attempt #${attempt} after ${waitMs}ms (${source}, serverMin=${serverMinimumMs}ms, clientBackoff=${clientBackoffMs}ms)`
579
555
  );
580
556
  }
581
- yield new Promise((resolve) => setTimeout(resolve, waitMs));
557
+ await new Promise((resolve) => setTimeout(resolve, waitMs));
582
558
  delay = Math.min(delay * multiplier, maxDelay);
583
559
  }
584
560
  }
585
561
  }
586
- });
562
+ };
587
563
  }
588
564
  var NO_BODY_STATUS_CODES = [201, 204, 205];
589
565
  function createFetchWithConsumedMessages(fetchClient) {
590
- return (...args) => __async(this, null, function* () {
566
+ return async (...args) => {
591
567
  var _a, _b;
592
568
  const url = args[0];
593
- const res = yield fetchClient(...args);
569
+ const res = await fetchClient(...args);
594
570
  try {
595
571
  if (res.status < 200 || NO_BODY_STATUS_CODES.includes(res.status)) {
596
572
  return res;
597
573
  }
598
- const text = yield res.text();
574
+ const text = await res.text();
599
575
  return new Response(text, res);
600
576
  } catch (err) {
601
577
  if ((_b = (_a = args[1]) == null ? void 0 : _a.signal) == null ? void 0 : _b.aborted) {
@@ -610,7 +586,7 @@ function createFetchWithConsumedMessages(fetchClient) {
610
586
  err instanceof Error ? err.message : typeof err === `string` ? err : `failed to read body`
611
587
  );
612
588
  }
613
- });
589
+ };
614
590
  }
615
591
  var ChunkPrefetchDefaults = {
616
592
  maxChunksToPrefetch: 2
@@ -618,14 +594,15 @@ var ChunkPrefetchDefaults = {
618
594
  function createFetchWithChunkBuffer(fetchClient, prefetchOptions = ChunkPrefetchDefaults) {
619
595
  const { maxChunksToPrefetch } = prefetchOptions;
620
596
  let prefetchQueue;
621
- const prefetchClient = (...args) => __async(this, null, function* () {
597
+ const prefetchClient = async (...args) => {
622
598
  const url = args[0].toString();
623
599
  const prefetchedRequest = prefetchQueue == null ? void 0 : prefetchQueue.consume(...args);
624
600
  if (prefetchedRequest) {
625
601
  return prefetchedRequest;
626
602
  }
627
603
  prefetchQueue == null ? void 0 : prefetchQueue.abort();
628
- const response = yield fetchClient(...args);
604
+ prefetchQueue = void 0;
605
+ const response = await fetchClient(...args);
629
606
  const nextUrl = getNextChunkUrl(url, response);
630
607
  if (nextUrl) {
631
608
  prefetchQueue = new PrefetchQueue({
@@ -636,7 +613,7 @@ function createFetchWithChunkBuffer(fetchClient, prefetchOptions = ChunkPrefetch
636
613
  });
637
614
  }
638
615
  return response;
639
- });
616
+ };
640
617
  return prefetchClient;
641
618
  }
642
619
  var requiredElectricResponseHeaders = [
@@ -646,8 +623,8 @@ var requiredElectricResponseHeaders = [
646
623
  var requiredLiveResponseHeaders = [`electric-cursor`];
647
624
  var requiredNonLiveResponseHeaders = [`electric-schema`];
648
625
  function createFetchWithResponseHeadersCheck(fetchClient) {
649
- return (...args) => __async(this, null, function* () {
650
- const response = yield fetchClient(...args);
626
+ return async (...args) => {
627
+ const response = await fetchClient(...args);
651
628
  if (response.ok) {
652
629
  const headers = response.headers;
653
630
  const missingHeaders = [];
@@ -677,7 +654,7 @@ function createFetchWithResponseHeadersCheck(fetchClient) {
677
654
  }
678
655
  }
679
656
  return response;
680
- });
657
+ };
681
658
  }
682
659
  var _fetchClient, _maxPrefetchedRequests, _prefetchQueue, _queueHeadUrl, _queueTailUrl, _PrefetchQueue_instances, prefetch_fn;
683
660
  var PrefetchQueue = class {
@@ -697,12 +674,17 @@ var PrefetchQueue = class {
697
674
  }
698
675
  abort() {
699
676
  __privateGet(this, _prefetchQueue).forEach(([_, aborter]) => aborter.abort());
677
+ __privateGet(this, _prefetchQueue).clear();
700
678
  }
701
679
  consume(...args) {
702
- var _a;
703
680
  const url = args[0].toString();
704
- const request = (_a = __privateGet(this, _prefetchQueue).get(url)) == null ? void 0 : _a[0];
705
- if (!request || url !== __privateGet(this, _queueHeadUrl)) return;
681
+ const entry = __privateGet(this, _prefetchQueue).get(url);
682
+ if (!entry || url !== __privateGet(this, _queueHeadUrl)) return;
683
+ const [request, aborter] = entry;
684
+ if (aborter.signal.aborted) {
685
+ __privateGet(this, _prefetchQueue).delete(url);
686
+ return;
687
+ }
706
688
  __privateGet(this, _prefetchQueue).delete(url);
707
689
  request.then((response) => {
708
690
  const nextUrl = getNextChunkUrl(url, response);
@@ -751,6 +733,13 @@ function getNextChunkUrl(url, res) {
751
733
  if (!shapeHandle || !lastOffset || isUpToDate) return;
752
734
  const nextUrl = new URL(url);
753
735
  if (nextUrl.searchParams.has(LIVE_QUERY_PARAM)) return;
736
+ const expiredHandle = nextUrl.searchParams.get(EXPIRED_HANDLE_QUERY_PARAM);
737
+ if (expiredHandle && shapeHandle === expiredHandle) {
738
+ console.warn(
739
+ `[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.`
740
+ );
741
+ return;
742
+ }
754
743
  nextUrl.searchParams.set(SHAPE_HANDLE_QUERY_PARAM, shapeHandle);
755
744
  nextUrl.searchParams.set(OFFSET_QUERY_PARAM, lastOffset);
756
745
  nextUrl.searchParams.sort();
@@ -1025,43 +1014,35 @@ var RESERVED_PARAMS = /* @__PURE__ */ new Set([
1025
1014
  LIVE_QUERY_PARAM,
1026
1015
  OFFSET_QUERY_PARAM
1027
1016
  ]);
1028
- function resolveValue(value) {
1029
- return __async(this, null, function* () {
1030
- if (typeof value === `function`) {
1031
- return value();
1032
- }
1033
- return value;
1034
- });
1017
+ async function resolveValue(value) {
1018
+ if (typeof value === `function`) {
1019
+ return value();
1020
+ }
1021
+ return value;
1035
1022
  }
1036
- function toInternalParams(params) {
1037
- return __async(this, null, function* () {
1038
- const entries = Object.entries(params);
1039
- const resolvedEntries = yield Promise.all(
1040
- entries.map((_0) => __async(this, [_0], function* ([key, value]) {
1041
- if (value === void 0) return [key, void 0];
1042
- const resolvedValue = yield resolveValue(value);
1043
- return [
1044
- key,
1045
- Array.isArray(resolvedValue) ? resolvedValue.join(`,`) : resolvedValue
1046
- ];
1047
- }))
1048
- );
1049
- return Object.fromEntries(
1050
- resolvedEntries.filter(([_, value]) => value !== void 0)
1051
- );
1052
- });
1023
+ async function toInternalParams(params) {
1024
+ const entries = Object.entries(params);
1025
+ const resolvedEntries = await Promise.all(
1026
+ entries.map(async ([key, value]) => {
1027
+ if (value === void 0) return [key, void 0];
1028
+ const resolvedValue = await resolveValue(value);
1029
+ return [
1030
+ key,
1031
+ Array.isArray(resolvedValue) ? resolvedValue.join(`,`) : resolvedValue
1032
+ ];
1033
+ })
1034
+ );
1035
+ return Object.fromEntries(
1036
+ resolvedEntries.filter(([_, value]) => value !== void 0)
1037
+ );
1053
1038
  }
1054
- function resolveHeaders(headers) {
1055
- return __async(this, null, function* () {
1056
- if (!headers) return {};
1057
- const entries = Object.entries(headers);
1058
- const resolvedEntries = yield Promise.all(
1059
- entries.map((_0) => __async(this, [_0], function* ([key, value]) {
1060
- return [key, yield resolveValue(value)];
1061
- }))
1062
- );
1063
- return Object.fromEntries(resolvedEntries);
1064
- });
1039
+ async function resolveHeaders(headers) {
1040
+ if (!headers) return {};
1041
+ const entries = Object.entries(headers);
1042
+ const resolvedEntries = await Promise.all(
1043
+ entries.map(async ([key, value]) => [key, await resolveValue(value)])
1044
+ );
1045
+ return Object.fromEntries(resolvedEntries);
1065
1046
  }
1066
1047
  function canonicalShapeKey(url) {
1067
1048
  const cleanUrl = new URL(url.origin + url.pathname);
@@ -1223,16 +1204,14 @@ var ShapeStream = class {
1223
1204
  * long polling, ensuring that the stream receives an up to date message with the
1224
1205
  * latest LSN from Postgres at that point in time.
1225
1206
  */
1226
- forceDisconnectAndRefresh() {
1227
- return __async(this, null, function* () {
1228
- var _a, _b;
1229
- __privateSet(this, _isRefreshing, true);
1230
- if (__privateGet(this, _isUpToDate) && !((_a = __privateGet(this, _requestAbortController)) == null ? void 0 : _a.signal.aborted)) {
1231
- (_b = __privateGet(this, _requestAbortController)) == null ? void 0 : _b.abort(FORCE_DISCONNECT_AND_REFRESH);
1232
- }
1233
- yield __privateMethod(this, _ShapeStream_instances, nextTick_fn).call(this);
1234
- __privateSet(this, _isRefreshing, false);
1235
- });
1207
+ async forceDisconnectAndRefresh() {
1208
+ var _a, _b;
1209
+ __privateSet(this, _isRefreshing, true);
1210
+ if (__privateGet(this, _isUpToDate) && !((_a = __privateGet(this, _requestAbortController)) == null ? void 0 : _a.signal.aborted)) {
1211
+ (_b = __privateGet(this, _requestAbortController)) == null ? void 0 : _b.abort(FORCE_DISCONNECT_AND_REFRESH);
1212
+ }
1213
+ await __privateMethod(this, _ShapeStream_instances, nextTick_fn).call(this);
1214
+ __privateSet(this, _isRefreshing, false);
1236
1215
  }
1237
1216
  /**
1238
1217
  * Request a snapshot for subset of data and inject it into the subscribed data stream.
@@ -1248,40 +1227,39 @@ var ShapeStream = class {
1248
1227
  * @param opts - The options for the snapshot request.
1249
1228
  * @returns The metadata and the data for the snapshot.
1250
1229
  */
1251
- requestSnapshot(opts) {
1252
- return __async(this, null, function* () {
1253
- if (__privateGet(this, _mode) === `full`) {
1254
- throw new Error(
1255
- `Snapshot requests are not supported in ${__privateGet(this, _mode)} mode, as the consumer is guaranteed to observe all data`
1256
- );
1230
+ async requestSnapshot(opts) {
1231
+ if (__privateGet(this, _mode) === `full`) {
1232
+ throw new Error(
1233
+ `Snapshot requests are not supported in ${__privateGet(this, _mode)} mode, as the consumer is guaranteed to observe all data`
1234
+ );
1235
+ }
1236
+ if (!__privateGet(this, _started)) await __privateMethod(this, _ShapeStream_instances, start_fn).call(this);
1237
+ await __privateMethod(this, _ShapeStream_instances, waitForStreamEnd_fn).call(this);
1238
+ __privateWrapper(this, _activeSnapshotRequests)._++;
1239
+ try {
1240
+ if (__privateGet(this, _activeSnapshotRequests) === 1) {
1241
+ __privateMethod(this, _ShapeStream_instances, pause_fn).call(this);
1257
1242
  }
1258
- if (!__privateGet(this, _started)) yield __privateMethod(this, _ShapeStream_instances, start_fn).call(this);
1259
- yield __privateMethod(this, _ShapeStream_instances, waitForStreamEnd_fn).call(this);
1260
- __privateWrapper(this, _activeSnapshotRequests)._++;
1261
- try {
1262
- if (__privateGet(this, _activeSnapshotRequests) === 1) {
1263
- __privateMethod(this, _ShapeStream_instances, pause_fn).call(this);
1264
- }
1265
- const { metadata, data } = yield this.fetchSnapshot(opts);
1266
- const dataWithEndBoundary = data.concat([
1267
- { headers: __spreadValues({ control: `snapshot-end` }, metadata) }
1268
- ]);
1269
- __privateGet(this, _snapshotTracker).addSnapshot(
1270
- metadata,
1271
- new Set(data.map((message) => message.key))
1272
- );
1273
- __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, dataWithEndBoundary, false);
1274
- return {
1275
- metadata,
1276
- data
1277
- };
1278
- } finally {
1279
- __privateWrapper(this, _activeSnapshotRequests)._--;
1280
- if (__privateGet(this, _activeSnapshotRequests) === 0) {
1281
- __privateMethod(this, _ShapeStream_instances, resume_fn).call(this);
1282
- }
1243
+ const { metadata, data } = await this.fetchSnapshot(opts);
1244
+ const dataWithEndBoundary = data.concat([
1245
+ { headers: __spreadValues({ control: `snapshot-end` }, metadata) },
1246
+ { headers: __spreadValues({ control: `subset-end` }, opts) }
1247
+ ]);
1248
+ __privateGet(this, _snapshotTracker).addSnapshot(
1249
+ metadata,
1250
+ new Set(data.map((message) => message.key))
1251
+ );
1252
+ __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, dataWithEndBoundary, false);
1253
+ return {
1254
+ metadata,
1255
+ data
1256
+ };
1257
+ } finally {
1258
+ __privateWrapper(this, _activeSnapshotRequests)._--;
1259
+ if (__privateGet(this, _activeSnapshotRequests) === 0) {
1260
+ __privateMethod(this, _ShapeStream_instances, resume_fn).call(this);
1283
1261
  }
1284
- });
1262
+ }
1285
1263
  }
1286
1264
  /**
1287
1265
  * Fetch a snapshot for subset of data.
@@ -1290,36 +1268,34 @@ var ShapeStream = class {
1290
1268
  * @param opts - The options for the snapshot request.
1291
1269
  * @returns The metadata and the data for the snapshot.
1292
1270
  */
1293
- fetchSnapshot(opts) {
1294
- return __async(this, null, function* () {
1295
- var _a;
1296
- const { fetchUrl, requestHeaders } = yield __privateMethod(this, _ShapeStream_instances, constructUrl_fn).call(this, this.options.url, true, opts);
1297
- const response = yield __privateGet(this, _fetchClient2).call(this, fetchUrl.toString(), {
1298
- headers: requestHeaders
1299
- });
1300
- if (!response.ok) {
1301
- throw new FetchError(
1302
- response.status,
1303
- void 0,
1304
- void 0,
1305
- Object.fromEntries([...response.headers.entries()]),
1306
- fetchUrl.toString()
1307
- );
1308
- }
1309
- const schema = (_a = __privateGet(this, _schema)) != null ? _a : getSchemaFromHeaders(response.headers, {
1310
- required: true,
1311
- url: fetchUrl.toString()
1312
- });
1313
- const { metadata, data: rawData } = yield response.json();
1314
- const data = __privateGet(this, _messageParser).parseSnapshotData(
1315
- rawData,
1316
- schema
1271
+ async fetchSnapshot(opts) {
1272
+ var _a;
1273
+ const { fetchUrl, requestHeaders } = await __privateMethod(this, _ShapeStream_instances, constructUrl_fn).call(this, this.options.url, true, opts);
1274
+ const response = await __privateGet(this, _fetchClient2).call(this, fetchUrl.toString(), {
1275
+ headers: requestHeaders
1276
+ });
1277
+ if (!response.ok) {
1278
+ throw new FetchError(
1279
+ response.status,
1280
+ void 0,
1281
+ void 0,
1282
+ Object.fromEntries([...response.headers.entries()]),
1283
+ fetchUrl.toString()
1317
1284
  );
1318
- return {
1319
- metadata,
1320
- data
1321
- };
1285
+ }
1286
+ const schema = (_a = __privateGet(this, _schema)) != null ? _a : getSchemaFromHeaders(response.headers, {
1287
+ required: true,
1288
+ url: fetchUrl.toString()
1322
1289
  });
1290
+ const { metadata, data: rawData } = await response.json();
1291
+ const data = __privateGet(this, _messageParser).parseSnapshotData(
1292
+ rawData,
1293
+ schema
1294
+ );
1295
+ return {
1296
+ metadata,
1297
+ data
1298
+ };
1323
1299
  }
1324
1300
  };
1325
1301
  _error = new WeakMap();
@@ -1363,379 +1339,369 @@ _ShapeStream_instances = new WeakSet();
1363
1339
  replayMode_get = function() {
1364
1340
  return __privateGet(this, _lastSeenCursor) !== void 0;
1365
1341
  };
1366
- start_fn = function() {
1367
- return __async(this, null, function* () {
1368
- var _a, _b, _c, _d, _e;
1369
- __privateSet(this, _started, true);
1370
- try {
1371
- yield __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1372
- } catch (err) {
1373
- __privateSet(this, _error, err);
1374
- if (__privateGet(this, _onError)) {
1375
- const retryOpts = yield __privateGet(this, _onError).call(this, err);
1376
- if (retryOpts && typeof retryOpts === `object`) {
1377
- if (retryOpts.params) {
1378
- this.options.params = __spreadValues(__spreadValues({}, (_a = this.options.params) != null ? _a : {}), retryOpts.params);
1379
- }
1380
- if (retryOpts.headers) {
1381
- this.options.headers = __spreadValues(__spreadValues({}, (_b = this.options.headers) != null ? _b : {}), retryOpts.headers);
1382
- }
1383
- __privateSet(this, _error, null);
1384
- __privateSet(this, _started, false);
1385
- yield __privateMethod(this, _ShapeStream_instances, start_fn).call(this);
1386
- return;
1342
+ start_fn = async function() {
1343
+ var _a, _b, _c, _d, _e;
1344
+ __privateSet(this, _started, true);
1345
+ try {
1346
+ await __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1347
+ } catch (err) {
1348
+ __privateSet(this, _error, err);
1349
+ if (__privateGet(this, _onError)) {
1350
+ const retryOpts = await __privateGet(this, _onError).call(this, err);
1351
+ if (retryOpts && typeof retryOpts === `object`) {
1352
+ if (retryOpts.params) {
1353
+ this.options.params = __spreadValues(__spreadValues({}, (_a = this.options.params) != null ? _a : {}), retryOpts.params);
1387
1354
  }
1388
- if (err instanceof Error) {
1389
- __privateMethod(this, _ShapeStream_instances, sendErrorToSubscribers_fn).call(this, err);
1355
+ if (retryOpts.headers) {
1356
+ this.options.headers = __spreadValues(__spreadValues({}, (_b = this.options.headers) != null ? _b : {}), retryOpts.headers);
1390
1357
  }
1391
- __privateSet(this, _connected, false);
1392
- (_c = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _c.call(this);
1358
+ __privateSet(this, _error, null);
1359
+ __privateSet(this, _started, false);
1360
+ await __privateMethod(this, _ShapeStream_instances, start_fn).call(this);
1393
1361
  return;
1394
1362
  }
1395
1363
  if (err instanceof Error) {
1396
1364
  __privateMethod(this, _ShapeStream_instances, sendErrorToSubscribers_fn).call(this, err);
1397
1365
  }
1398
1366
  __privateSet(this, _connected, false);
1399
- (_d = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _d.call(this);
1400
- throw err;
1367
+ (_c = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _c.call(this);
1368
+ return;
1369
+ }
1370
+ if (err instanceof Error) {
1371
+ __privateMethod(this, _ShapeStream_instances, sendErrorToSubscribers_fn).call(this, err);
1401
1372
  }
1402
1373
  __privateSet(this, _connected, false);
1403
- (_e = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _e.call(this);
1404
- });
1374
+ (_d = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _d.call(this);
1375
+ throw err;
1376
+ }
1377
+ __privateSet(this, _connected, false);
1378
+ (_e = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _e.call(this);
1405
1379
  };
1406
- requestShape_fn = function() {
1407
- return __async(this, null, function* () {
1408
- var _a, _b;
1409
- if (__privateGet(this, _state) === `pause-requested`) {
1410
- __privateSet(this, _state, `paused`);
1411
- return;
1380
+ requestShape_fn = async function() {
1381
+ var _a, _b;
1382
+ if (__privateGet(this, _state) === `pause-requested`) {
1383
+ __privateSet(this, _state, `paused`);
1384
+ return;
1385
+ }
1386
+ if (!this.options.subscribe && (((_a = this.options.signal) == null ? void 0 : _a.aborted) || __privateGet(this, _isUpToDate))) {
1387
+ return;
1388
+ }
1389
+ const resumingFromPause = __privateGet(this, _state) === `paused`;
1390
+ __privateSet(this, _state, `active`);
1391
+ const { url, signal } = this.options;
1392
+ const { fetchUrl, requestHeaders } = await __privateMethod(this, _ShapeStream_instances, constructUrl_fn).call(this, url, resumingFromPause);
1393
+ const abortListener = await __privateMethod(this, _ShapeStream_instances, createAbortListener_fn).call(this, signal);
1394
+ const requestAbortController = __privateGet(this, _requestAbortController);
1395
+ try {
1396
+ await __privateMethod(this, _ShapeStream_instances, fetchShape_fn).call(this, {
1397
+ fetchUrl,
1398
+ requestAbortController,
1399
+ headers: requestHeaders,
1400
+ resumingFromPause
1401
+ });
1402
+ } catch (e) {
1403
+ if ((e instanceof FetchError || e instanceof FetchBackoffAbortError) && requestAbortController.signal.aborted && requestAbortController.signal.reason === FORCE_DISCONNECT_AND_REFRESH) {
1404
+ return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1412
1405
  }
1413
- if (!this.options.subscribe && (((_a = this.options.signal) == null ? void 0 : _a.aborted) || __privateGet(this, _isUpToDate))) {
1406
+ if (e instanceof FetchBackoffAbortError) {
1407
+ const currentState = __privateGet(this, _state);
1408
+ if (requestAbortController.signal.aborted && requestAbortController.signal.reason === PAUSE_STREAM && currentState === `pause-requested`) {
1409
+ __privateSet(this, _state, `paused`);
1410
+ }
1414
1411
  return;
1415
1412
  }
1416
- const resumingFromPause = __privateGet(this, _state) === `paused`;
1417
- __privateSet(this, _state, `active`);
1418
- const { url, signal } = this.options;
1419
- const { fetchUrl, requestHeaders } = yield __privateMethod(this, _ShapeStream_instances, constructUrl_fn).call(this, url, resumingFromPause);
1420
- const abortListener = yield __privateMethod(this, _ShapeStream_instances, createAbortListener_fn).call(this, signal);
1421
- const requestAbortController = __privateGet(this, _requestAbortController);
1422
- try {
1423
- yield __privateMethod(this, _ShapeStream_instances, fetchShape_fn).call(this, {
1424
- fetchUrl,
1425
- requestAbortController,
1426
- headers: requestHeaders,
1427
- resumingFromPause
1428
- });
1429
- } catch (e) {
1430
- if ((e instanceof FetchError || e instanceof FetchBackoffAbortError) && requestAbortController.signal.aborted && requestAbortController.signal.reason === FORCE_DISCONNECT_AND_REFRESH) {
1431
- return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1432
- }
1433
- if (e instanceof FetchBackoffAbortError) {
1434
- const currentState = __privateGet(this, _state);
1435
- if (requestAbortController.signal.aborted && requestAbortController.signal.reason === PAUSE_STREAM && currentState === `pause-requested`) {
1436
- __privateSet(this, _state, `paused`);
1437
- }
1438
- return;
1439
- }
1440
- if (!(e instanceof FetchError)) throw e;
1441
- if (e.status == 409) {
1442
- if (__privateGet(this, _shapeHandle)) {
1443
- const shapeKey = canonicalShapeKey(fetchUrl);
1444
- expiredShapesCache.markExpired(shapeKey, __privateGet(this, _shapeHandle));
1445
- }
1446
- const newShapeHandle = e.headers[SHAPE_HANDLE_HEADER] || `${__privateGet(this, _shapeHandle)}-next`;
1447
- __privateMethod(this, _ShapeStream_instances, reset_fn).call(this, newShapeHandle);
1448
- yield __privateMethod(this, _ShapeStream_instances, publish_fn).call(this, Array.isArray(e.json) ? e.json : [e.json]);
1449
- return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1450
- } else {
1451
- throw e;
1413
+ if (!(e instanceof FetchError)) throw e;
1414
+ if (e.status == 409) {
1415
+ if (__privateGet(this, _shapeHandle)) {
1416
+ const shapeKey = canonicalShapeKey(fetchUrl);
1417
+ expiredShapesCache.markExpired(shapeKey, __privateGet(this, _shapeHandle));
1452
1418
  }
1453
- } finally {
1454
- if (abortListener && signal) {
1455
- signal.removeEventListener(`abort`, abortListener);
1456
- }
1457
- __privateSet(this, _requestAbortController, void 0);
1419
+ const newShapeHandle = e.headers[SHAPE_HANDLE_HEADER] || `${__privateGet(this, _shapeHandle)}-next`;
1420
+ __privateMethod(this, _ShapeStream_instances, reset_fn).call(this, newShapeHandle);
1421
+ await __privateMethod(this, _ShapeStream_instances, publish_fn).call(this, Array.isArray(e.json) ? e.json : [e.json]);
1422
+ return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1423
+ } else {
1424
+ throw e;
1458
1425
  }
1459
- (_b = __privateGet(this, _tickPromiseResolver)) == null ? void 0 : _b.call(this);
1460
- return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1461
- });
1426
+ } finally {
1427
+ if (abortListener && signal) {
1428
+ signal.removeEventListener(`abort`, abortListener);
1429
+ }
1430
+ __privateSet(this, _requestAbortController, void 0);
1431
+ }
1432
+ (_b = __privateGet(this, _tickPromiseResolver)) == null ? void 0 : _b.call(this);
1433
+ return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1462
1434
  };
1463
- constructUrl_fn = function(url, resumingFromPause, subsetParams) {
1464
- return __async(this, null, function* () {
1465
- var _a, _b, _c, _d;
1466
- const [requestHeaders, params] = yield Promise.all([
1467
- resolveHeaders(this.options.headers),
1468
- this.options.params ? toInternalParams(convertWhereParamsToObj(this.options.params)) : void 0
1469
- ]);
1470
- if (params) validateParams(params);
1471
- const fetchUrl = new URL(url);
1472
- if (params) {
1473
- if (params.table) setQueryParam(fetchUrl, TABLE_QUERY_PARAM, params.table);
1474
- if (params.where && typeof params.where === `string`) {
1475
- const encodedWhere = encodeWhereClause(
1476
- params.where,
1477
- (_a = this.options.columnMapper) == null ? void 0 : _a.encode
1478
- );
1479
- setQueryParam(fetchUrl, WHERE_QUERY_PARAM, encodedWhere);
1480
- }
1481
- if (params.columns) {
1482
- const originalColumns = yield resolveValue((_b = this.options.params) == null ? void 0 : _b.columns);
1483
- if (Array.isArray(originalColumns)) {
1484
- let encodedColumns = originalColumns.map(String);
1485
- if (this.options.columnMapper) {
1486
- encodedColumns = encodedColumns.map(
1487
- this.options.columnMapper.encode
1488
- );
1489
- }
1490
- const serializedColumns = encodedColumns.map(quoteIdentifier).join(`,`);
1491
- setQueryParam(fetchUrl, COLUMNS_QUERY_PARAM, serializedColumns);
1492
- } else {
1493
- setQueryParam(fetchUrl, COLUMNS_QUERY_PARAM, params.columns);
1435
+ constructUrl_fn = async function(url, resumingFromPause, subsetParams) {
1436
+ var _a, _b, _c, _d;
1437
+ const [requestHeaders, params] = await Promise.all([
1438
+ resolveHeaders(this.options.headers),
1439
+ this.options.params ? toInternalParams(convertWhereParamsToObj(this.options.params)) : void 0
1440
+ ]);
1441
+ if (params) validateParams(params);
1442
+ const fetchUrl = new URL(url);
1443
+ if (params) {
1444
+ if (params.table) setQueryParam(fetchUrl, TABLE_QUERY_PARAM, params.table);
1445
+ if (params.where && typeof params.where === `string`) {
1446
+ const encodedWhere = encodeWhereClause(
1447
+ params.where,
1448
+ (_a = this.options.columnMapper) == null ? void 0 : _a.encode
1449
+ );
1450
+ setQueryParam(fetchUrl, WHERE_QUERY_PARAM, encodedWhere);
1451
+ }
1452
+ if (params.columns) {
1453
+ const originalColumns = await resolveValue((_b = this.options.params) == null ? void 0 : _b.columns);
1454
+ if (Array.isArray(originalColumns)) {
1455
+ let encodedColumns = originalColumns.map(String);
1456
+ if (this.options.columnMapper) {
1457
+ encodedColumns = encodedColumns.map(
1458
+ this.options.columnMapper.encode
1459
+ );
1494
1460
  }
1495
- }
1496
- if (params.replica) setQueryParam(fetchUrl, REPLICA_PARAM, params.replica);
1497
- if (params.params)
1498
- setQueryParam(fetchUrl, WHERE_PARAMS_PARAM, params.params);
1499
- const customParams = __spreadValues({}, params);
1500
- delete customParams.table;
1501
- delete customParams.where;
1502
- delete customParams.columns;
1503
- delete customParams.replica;
1504
- delete customParams.params;
1505
- for (const [key, value] of Object.entries(customParams)) {
1506
- setQueryParam(fetchUrl, key, value);
1461
+ const serializedColumns = encodedColumns.map(quoteIdentifier).join(`,`);
1462
+ setQueryParam(fetchUrl, COLUMNS_QUERY_PARAM, serializedColumns);
1463
+ } else {
1464
+ setQueryParam(fetchUrl, COLUMNS_QUERY_PARAM, params.columns);
1507
1465
  }
1508
1466
  }
1509
- if (subsetParams) {
1510
- if (subsetParams.where && typeof subsetParams.where === `string`) {
1511
- const encodedWhere = encodeWhereClause(
1512
- subsetParams.where,
1513
- (_c = this.options.columnMapper) == null ? void 0 : _c.encode
1514
- );
1515
- setQueryParam(fetchUrl, SUBSET_PARAM_WHERE, encodedWhere);
1516
- }
1517
- if (subsetParams.params)
1518
- fetchUrl.searchParams.set(
1519
- SUBSET_PARAM_WHERE_PARAMS,
1520
- JSON.stringify(subsetParams.params)
1521
- );
1522
- if (subsetParams.limit)
1523
- setQueryParam(fetchUrl, SUBSET_PARAM_LIMIT, subsetParams.limit);
1524
- if (subsetParams.offset)
1525
- setQueryParam(fetchUrl, SUBSET_PARAM_OFFSET, subsetParams.offset);
1526
- if (subsetParams.orderBy && typeof subsetParams.orderBy === `string`) {
1527
- const encodedOrderBy = encodeWhereClause(
1528
- subsetParams.orderBy,
1529
- (_d = this.options.columnMapper) == null ? void 0 : _d.encode
1530
- );
1531
- setQueryParam(fetchUrl, SUBSET_PARAM_ORDER_BY, encodedOrderBy);
1532
- }
1467
+ if (params.replica) setQueryParam(fetchUrl, REPLICA_PARAM, params.replica);
1468
+ if (params.params)
1469
+ setQueryParam(fetchUrl, WHERE_PARAMS_PARAM, params.params);
1470
+ const customParams = __spreadValues({}, params);
1471
+ delete customParams.table;
1472
+ delete customParams.where;
1473
+ delete customParams.columns;
1474
+ delete customParams.replica;
1475
+ delete customParams.params;
1476
+ for (const [key, value] of Object.entries(customParams)) {
1477
+ setQueryParam(fetchUrl, key, value);
1533
1478
  }
1534
- fetchUrl.searchParams.set(OFFSET_QUERY_PARAM, __privateGet(this, _lastOffset));
1535
- fetchUrl.searchParams.set(LOG_MODE_QUERY_PARAM, __privateGet(this, _mode));
1536
- const isSnapshotRequest = subsetParams !== void 0;
1537
- if (__privateGet(this, _isUpToDate) && !isSnapshotRequest) {
1538
- if (!__privateGet(this, _isRefreshing) && !resumingFromPause) {
1539
- fetchUrl.searchParams.set(LIVE_QUERY_PARAM, `true`);
1540
- }
1541
- fetchUrl.searchParams.set(
1542
- LIVE_CACHE_BUSTER_QUERY_PARAM,
1543
- __privateGet(this, _liveCacheBuster)
1479
+ }
1480
+ if (subsetParams) {
1481
+ if (subsetParams.where && typeof subsetParams.where === `string`) {
1482
+ const encodedWhere = encodeWhereClause(
1483
+ subsetParams.where,
1484
+ (_c = this.options.columnMapper) == null ? void 0 : _c.encode
1544
1485
  );
1486
+ setQueryParam(fetchUrl, SUBSET_PARAM_WHERE, encodedWhere);
1545
1487
  }
1546
- if (__privateGet(this, _shapeHandle)) {
1547
- fetchUrl.searchParams.set(SHAPE_HANDLE_QUERY_PARAM, __privateGet(this, _shapeHandle));
1488
+ if (subsetParams.params)
1489
+ fetchUrl.searchParams.set(
1490
+ SUBSET_PARAM_WHERE_PARAMS,
1491
+ JSON.stringify(subsetParams.params)
1492
+ );
1493
+ if (subsetParams.limit)
1494
+ setQueryParam(fetchUrl, SUBSET_PARAM_LIMIT, subsetParams.limit);
1495
+ if (subsetParams.offset)
1496
+ setQueryParam(fetchUrl, SUBSET_PARAM_OFFSET, subsetParams.offset);
1497
+ if (subsetParams.orderBy && typeof subsetParams.orderBy === `string`) {
1498
+ const encodedOrderBy = encodeWhereClause(
1499
+ subsetParams.orderBy,
1500
+ (_d = this.options.columnMapper) == null ? void 0 : _d.encode
1501
+ );
1502
+ setQueryParam(fetchUrl, SUBSET_PARAM_ORDER_BY, encodedOrderBy);
1548
1503
  }
1549
- const shapeKey = canonicalShapeKey(fetchUrl);
1550
- const expiredHandle = expiredShapesCache.getExpiredHandle(shapeKey);
1551
- if (expiredHandle) {
1552
- fetchUrl.searchParams.set(EXPIRED_HANDLE_QUERY_PARAM, expiredHandle);
1504
+ }
1505
+ fetchUrl.searchParams.set(OFFSET_QUERY_PARAM, __privateGet(this, _lastOffset));
1506
+ fetchUrl.searchParams.set(LOG_MODE_QUERY_PARAM, __privateGet(this, _mode));
1507
+ const isSnapshotRequest = subsetParams !== void 0;
1508
+ if (__privateGet(this, _isUpToDate) && !isSnapshotRequest) {
1509
+ if (!__privateGet(this, _isRefreshing) && !resumingFromPause) {
1510
+ fetchUrl.searchParams.set(LIVE_QUERY_PARAM, `true`);
1553
1511
  }
1554
- fetchUrl.searchParams.sort();
1555
- return {
1556
- fetchUrl,
1557
- requestHeaders
1558
- };
1559
- });
1512
+ fetchUrl.searchParams.set(
1513
+ LIVE_CACHE_BUSTER_QUERY_PARAM,
1514
+ __privateGet(this, _liveCacheBuster)
1515
+ );
1516
+ }
1517
+ if (__privateGet(this, _shapeHandle)) {
1518
+ fetchUrl.searchParams.set(SHAPE_HANDLE_QUERY_PARAM, __privateGet(this, _shapeHandle));
1519
+ }
1520
+ const shapeKey = canonicalShapeKey(fetchUrl);
1521
+ const expiredHandle = expiredShapesCache.getExpiredHandle(shapeKey);
1522
+ if (expiredHandle) {
1523
+ fetchUrl.searchParams.set(EXPIRED_HANDLE_QUERY_PARAM, expiredHandle);
1524
+ }
1525
+ fetchUrl.searchParams.sort();
1526
+ return {
1527
+ fetchUrl,
1528
+ requestHeaders
1529
+ };
1560
1530
  };
1561
- createAbortListener_fn = function(signal) {
1562
- return __async(this, null, function* () {
1563
- var _a;
1564
- __privateSet(this, _requestAbortController, new AbortController());
1565
- if (signal) {
1566
- const abortListener = () => {
1567
- var _a2;
1568
- (_a2 = __privateGet(this, _requestAbortController)) == null ? void 0 : _a2.abort(signal.reason);
1569
- };
1570
- signal.addEventListener(`abort`, abortListener, { once: true });
1571
- if (signal.aborted) {
1572
- (_a = __privateGet(this, _requestAbortController)) == null ? void 0 : _a.abort(signal.reason);
1573
- }
1574
- return abortListener;
1531
+ createAbortListener_fn = async function(signal) {
1532
+ var _a;
1533
+ __privateSet(this, _requestAbortController, new AbortController());
1534
+ if (signal) {
1535
+ const abortListener = () => {
1536
+ var _a2;
1537
+ (_a2 = __privateGet(this, _requestAbortController)) == null ? void 0 : _a2.abort(signal.reason);
1538
+ };
1539
+ signal.addEventListener(`abort`, abortListener, { once: true });
1540
+ if (signal.aborted) {
1541
+ (_a = __privateGet(this, _requestAbortController)) == null ? void 0 : _a.abort(signal.reason);
1575
1542
  }
1576
- });
1543
+ return abortListener;
1544
+ }
1577
1545
  };
1578
- onInitialResponse_fn = function(response) {
1579
- return __async(this, null, function* () {
1580
- var _a;
1581
- const { headers, status } = response;
1582
- const shapeHandle = headers.get(SHAPE_HANDLE_HEADER);
1583
- if (shapeHandle) {
1546
+ onInitialResponse_fn = async function(response) {
1547
+ var _a;
1548
+ const { headers, status } = response;
1549
+ const shapeHandle = headers.get(SHAPE_HANDLE_HEADER);
1550
+ if (shapeHandle) {
1551
+ const shapeKey = __privateGet(this, _currentFetchUrl) ? canonicalShapeKey(__privateGet(this, _currentFetchUrl)) : null;
1552
+ const expiredHandle = shapeKey ? expiredShapesCache.getExpiredHandle(shapeKey) : null;
1553
+ if (shapeHandle !== expiredHandle) {
1584
1554
  __privateSet(this, _shapeHandle, shapeHandle);
1555
+ } else {
1556
+ console.warn(
1557
+ `[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)}".`
1558
+ );
1585
1559
  }
1586
- const lastOffset = headers.get(CHUNK_LAST_OFFSET_HEADER);
1587
- if (lastOffset) {
1588
- __privateSet(this, _lastOffset, lastOffset);
1589
- }
1590
- const liveCacheBuster = headers.get(LIVE_CACHE_BUSTER_HEADER);
1591
- if (liveCacheBuster) {
1592
- __privateSet(this, _liveCacheBuster, liveCacheBuster);
1593
- }
1594
- __privateSet(this, _schema, (_a = __privateGet(this, _schema)) != null ? _a : getSchemaFromHeaders(headers));
1595
- if (status === 204) {
1596
- __privateSet(this, _lastSyncedAt, Date.now());
1597
- }
1598
- });
1560
+ }
1561
+ const lastOffset = headers.get(CHUNK_LAST_OFFSET_HEADER);
1562
+ if (lastOffset) {
1563
+ __privateSet(this, _lastOffset, lastOffset);
1564
+ }
1565
+ const liveCacheBuster = headers.get(LIVE_CACHE_BUSTER_HEADER);
1566
+ if (liveCacheBuster) {
1567
+ __privateSet(this, _liveCacheBuster, liveCacheBuster);
1568
+ }
1569
+ __privateSet(this, _schema, (_a = __privateGet(this, _schema)) != null ? _a : getSchemaFromHeaders(headers));
1570
+ if (status === 204) {
1571
+ __privateSet(this, _lastSyncedAt, Date.now());
1572
+ }
1599
1573
  };
1600
- onMessages_fn = function(batch, isSseMessage = false) {
1601
- return __async(this, null, function* () {
1602
- var _a;
1603
- if (batch.length > 0) {
1604
- __privateSet(this, _isMidStream, true);
1605
- const lastMessage = batch[batch.length - 1];
1606
- if (isUpToDateMessage(lastMessage)) {
1607
- if (isSseMessage) {
1608
- const offset = getOffset(lastMessage);
1609
- if (offset) {
1610
- __privateSet(this, _lastOffset, offset);
1611
- }
1612
- }
1613
- __privateSet(this, _lastSyncedAt, Date.now());
1614
- __privateSet(this, _isUpToDate, true);
1615
- __privateSet(this, _isMidStream, false);
1616
- (_a = __privateGet(this, _midStreamPromiseResolver)) == null ? void 0 : _a.call(this);
1617
- if (__privateGet(this, _ShapeStream_instances, replayMode_get) && !isSseMessage) {
1618
- const currentCursor = __privateGet(this, _liveCacheBuster);
1619
- if (currentCursor === __privateGet(this, _lastSeenCursor)) {
1620
- return;
1621
- }
1622
- }
1623
- __privateSet(this, _lastSeenCursor, void 0);
1624
- if (__privateGet(this, _currentFetchUrl)) {
1625
- const shapeKey = canonicalShapeKey(__privateGet(this, _currentFetchUrl));
1626
- upToDateTracker.recordUpToDate(shapeKey, __privateGet(this, _liveCacheBuster));
1574
+ onMessages_fn = async function(batch, isSseMessage = false) {
1575
+ var _a;
1576
+ if (batch.length > 0) {
1577
+ __privateSet(this, _isMidStream, true);
1578
+ const lastMessage = batch[batch.length - 1];
1579
+ if (isUpToDateMessage(lastMessage)) {
1580
+ if (isSseMessage) {
1581
+ const offset = getOffset(lastMessage);
1582
+ if (offset) {
1583
+ __privateSet(this, _lastOffset, offset);
1627
1584
  }
1628
1585
  }
1629
- const messagesToProcess = batch.filter((message) => {
1630
- if (isChangeMessage(message)) {
1631
- return !__privateGet(this, _snapshotTracker).shouldRejectMessage(message);
1586
+ __privateSet(this, _lastSyncedAt, Date.now());
1587
+ __privateSet(this, _isUpToDate, true);
1588
+ __privateSet(this, _isMidStream, false);
1589
+ (_a = __privateGet(this, _midStreamPromiseResolver)) == null ? void 0 : _a.call(this);
1590
+ if (__privateGet(this, _ShapeStream_instances, replayMode_get) && !isSseMessage) {
1591
+ const currentCursor = __privateGet(this, _liveCacheBuster);
1592
+ if (currentCursor === __privateGet(this, _lastSeenCursor)) {
1593
+ return;
1632
1594
  }
1633
- return true;
1634
- });
1635
- yield __privateMethod(this, _ShapeStream_instances, publish_fn).call(this, messagesToProcess);
1636
- }
1637
- });
1638
- };
1639
- fetchShape_fn = function(opts) {
1640
- return __async(this, null, function* () {
1641
- var _a;
1642
- __privateSet(this, _currentFetchUrl, opts.fetchUrl);
1643
- if (!__privateGet(this, _isUpToDate) && !__privateGet(this, _ShapeStream_instances, replayMode_get)) {
1644
- const shapeKey = canonicalShapeKey(opts.fetchUrl);
1645
- const lastSeenCursor = upToDateTracker.shouldEnterReplayMode(shapeKey);
1646
- if (lastSeenCursor) {
1647
- __privateSet(this, _lastSeenCursor, lastSeenCursor);
1595
+ }
1596
+ __privateSet(this, _lastSeenCursor, void 0);
1597
+ if (__privateGet(this, _currentFetchUrl)) {
1598
+ const shapeKey = canonicalShapeKey(__privateGet(this, _currentFetchUrl));
1599
+ upToDateTracker.recordUpToDate(shapeKey, __privateGet(this, _liveCacheBuster));
1648
1600
  }
1649
1601
  }
1650
- const useSse = (_a = this.options.liveSse) != null ? _a : this.options.experimentalLiveSse;
1651
- if (__privateGet(this, _isUpToDate) && useSse && !__privateGet(this, _isRefreshing) && !opts.resumingFromPause && !__privateGet(this, _sseFallbackToLongPolling)) {
1652
- opts.fetchUrl.searchParams.set(EXPERIMENTAL_LIVE_SSE_QUERY_PARAM, `true`);
1653
- opts.fetchUrl.searchParams.set(LIVE_SSE_QUERY_PARAM, `true`);
1654
- return __privateMethod(this, _ShapeStream_instances, requestShapeSSE_fn).call(this, opts);
1602
+ const messagesToProcess = batch.filter((message) => {
1603
+ if (isChangeMessage(message)) {
1604
+ return !__privateGet(this, _snapshotTracker).shouldRejectMessage(message);
1605
+ }
1606
+ return true;
1607
+ });
1608
+ await __privateMethod(this, _ShapeStream_instances, publish_fn).call(this, messagesToProcess);
1609
+ }
1610
+ };
1611
+ fetchShape_fn = async function(opts) {
1612
+ var _a;
1613
+ __privateSet(this, _currentFetchUrl, opts.fetchUrl);
1614
+ if (!__privateGet(this, _isUpToDate) && !__privateGet(this, _ShapeStream_instances, replayMode_get)) {
1615
+ const shapeKey = canonicalShapeKey(opts.fetchUrl);
1616
+ const lastSeenCursor = upToDateTracker.shouldEnterReplayMode(shapeKey);
1617
+ if (lastSeenCursor) {
1618
+ __privateSet(this, _lastSeenCursor, lastSeenCursor);
1655
1619
  }
1656
- return __privateMethod(this, _ShapeStream_instances, requestShapeLongPoll_fn).call(this, opts);
1657
- });
1620
+ }
1621
+ const useSse = (_a = this.options.liveSse) != null ? _a : this.options.experimentalLiveSse;
1622
+ if (__privateGet(this, _isUpToDate) && useSse && !__privateGet(this, _isRefreshing) && !opts.resumingFromPause && !__privateGet(this, _sseFallbackToLongPolling)) {
1623
+ opts.fetchUrl.searchParams.set(EXPERIMENTAL_LIVE_SSE_QUERY_PARAM, `true`);
1624
+ opts.fetchUrl.searchParams.set(LIVE_SSE_QUERY_PARAM, `true`);
1625
+ return __privateMethod(this, _ShapeStream_instances, requestShapeSSE_fn).call(this, opts);
1626
+ }
1627
+ return __privateMethod(this, _ShapeStream_instances, requestShapeLongPoll_fn).call(this, opts);
1658
1628
  };
1659
- requestShapeLongPoll_fn = function(opts) {
1660
- return __async(this, null, function* () {
1661
- const { fetchUrl, requestAbortController, headers } = opts;
1662
- const response = yield __privateGet(this, _fetchClient2).call(this, fetchUrl.toString(), {
1663
- signal: requestAbortController.signal,
1664
- headers
1665
- });
1666
- __privateSet(this, _connected, true);
1667
- yield __privateMethod(this, _ShapeStream_instances, onInitialResponse_fn).call(this, response);
1668
- const schema = __privateGet(this, _schema);
1669
- const res = yield response.text();
1670
- const messages = res || `[]`;
1671
- const batch = __privateGet(this, _messageParser).parse(messages, schema);
1672
- yield __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, batch);
1629
+ requestShapeLongPoll_fn = async function(opts) {
1630
+ const { fetchUrl, requestAbortController, headers } = opts;
1631
+ const response = await __privateGet(this, _fetchClient2).call(this, fetchUrl.toString(), {
1632
+ signal: requestAbortController.signal,
1633
+ headers
1673
1634
  });
1635
+ __privateSet(this, _connected, true);
1636
+ await __privateMethod(this, _ShapeStream_instances, onInitialResponse_fn).call(this, response);
1637
+ const schema = __privateGet(this, _schema);
1638
+ const res = await response.text();
1639
+ const messages = res || `[]`;
1640
+ const batch = __privateGet(this, _messageParser).parse(messages, schema);
1641
+ await __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, batch);
1674
1642
  };
1675
- requestShapeSSE_fn = function(opts) {
1676
- return __async(this, null, function* () {
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
- yield fetchEventSource(fetchUrl.toString(), {
1686
- headers: sseHeaders,
1687
- fetch: fetch2,
1688
- onopen: (response) => __async(this, null, function* () {
1689
- __privateSet(this, _connected, true);
1690
- yield __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
1698
- );
1699
- buffer.push(message);
1700
- if (isUpToDateMessage(message)) {
1701
- __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, buffer, true);
1702
- buffer = [];
1703
- }
1704
- }
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))
1643
+ requestShapeSSE_fn = async function(opts) {
1644
+ const { fetchUrl, requestAbortController, headers } = opts;
1645
+ const fetch2 = __privateGet(this, _sseFetchClient);
1646
+ __privateSet(this, _lastSseConnectionStartTime, Date.now());
1647
+ const sseHeaders = __spreadProps(__spreadValues({}, headers), {
1648
+ Accept: `text/event-stream`
1649
+ });
1650
+ try {
1651
+ let buffer = [];
1652
+ await fetchEventSource(fetchUrl.toString(), {
1653
+ headers: sseHeaders,
1654
+ fetch: fetch2,
1655
+ onopen: async (response) => {
1656
+ __privateSet(this, _connected, true);
1657
+ await __privateMethod(this, _ShapeStream_instances, onInitialResponse_fn).call(this, response);
1658
+ },
1659
+ onmessage: (event) => {
1660
+ if (event.data) {
1661
+ const schema = __privateGet(this, _schema);
1662
+ const message = __privateGet(this, _messageParser).parse(
1663
+ event.data,
1664
+ schema
1730
1665
  );
1731
- const delayMs = Math.floor(Math.random() * maxDelay);
1732
- yield new Promise((resolve) => setTimeout(resolve, delayMs));
1666
+ buffer.push(message);
1667
+ if (isUpToDateMessage(message)) {
1668
+ __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, buffer, true);
1669
+ buffer = [];
1670
+ }
1733
1671
  }
1734
- } else if (connectionDuration >= __privateGet(this, _minSseConnectionDuration)) {
1735
- __privateSet(this, _consecutiveShortSseConnections, 0);
1672
+ },
1673
+ onerror: (error) => {
1674
+ throw error;
1675
+ },
1676
+ signal: requestAbortController.signal
1677
+ });
1678
+ } catch (error) {
1679
+ if (requestAbortController.signal.aborted) {
1680
+ throw new FetchBackoffAbortError();
1681
+ }
1682
+ throw error;
1683
+ } finally {
1684
+ const connectionDuration = Date.now() - __privateGet(this, _lastSseConnectionStartTime);
1685
+ const wasAborted = requestAbortController.signal.aborted;
1686
+ if (connectionDuration < __privateGet(this, _minSseConnectionDuration) && !wasAborted) {
1687
+ __privateWrapper(this, _consecutiveShortSseConnections)._++;
1688
+ if (__privateGet(this, _consecutiveShortSseConnections) >= __privateGet(this, _maxShortSseConnections)) {
1689
+ __privateSet(this, _sseFallbackToLongPolling, true);
1690
+ console.warn(
1691
+ `[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.`
1692
+ );
1693
+ } else {
1694
+ const maxDelay = Math.min(
1695
+ __privateGet(this, _sseBackoffMaxDelay),
1696
+ __privateGet(this, _sseBackoffBaseDelay) * Math.pow(2, __privateGet(this, _consecutiveShortSseConnections))
1697
+ );
1698
+ const delayMs = Math.floor(Math.random() * maxDelay);
1699
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
1736
1700
  }
1701
+ } else if (connectionDuration >= __privateGet(this, _minSseConnectionDuration)) {
1702
+ __privateSet(this, _consecutiveShortSseConnections, 0);
1737
1703
  }
1738
- });
1704
+ }
1739
1705
  };
1740
1706
  pause_fn = function() {
1741
1707
  var _a;
@@ -1745,65 +1711,63 @@ pause_fn = function() {
1745
1711
  }
1746
1712
  };
1747
1713
  resume_fn = function() {
1714
+ var _a;
1748
1715
  if (__privateGet(this, _started) && (__privateGet(this, _state) === `paused` || __privateGet(this, _state) === `pause-requested`)) {
1716
+ if ((_a = this.options.signal) == null ? void 0 : _a.aborted) {
1717
+ return;
1718
+ }
1749
1719
  if (__privateGet(this, _state) === `pause-requested`) {
1750
1720
  __privateSet(this, _state, `active`);
1751
1721
  }
1752
1722
  __privateMethod(this, _ShapeStream_instances, start_fn).call(this);
1753
1723
  }
1754
1724
  };
1755
- nextTick_fn = function() {
1756
- return __async(this, null, function* () {
1757
- if (__privateGet(this, _tickPromise)) {
1758
- return __privateGet(this, _tickPromise);
1759
- }
1760
- __privateSet(this, _tickPromise, new Promise((resolve, reject) => {
1761
- __privateSet(this, _tickPromiseResolver, resolve);
1762
- __privateSet(this, _tickPromiseRejecter, reject);
1763
- }));
1764
- __privateGet(this, _tickPromise).finally(() => {
1765
- __privateSet(this, _tickPromise, void 0);
1766
- __privateSet(this, _tickPromiseResolver, void 0);
1767
- __privateSet(this, _tickPromiseRejecter, void 0);
1768
- });
1725
+ nextTick_fn = async function() {
1726
+ if (__privateGet(this, _tickPromise)) {
1769
1727
  return __privateGet(this, _tickPromise);
1728
+ }
1729
+ __privateSet(this, _tickPromise, new Promise((resolve, reject) => {
1730
+ __privateSet(this, _tickPromiseResolver, resolve);
1731
+ __privateSet(this, _tickPromiseRejecter, reject);
1732
+ }));
1733
+ __privateGet(this, _tickPromise).finally(() => {
1734
+ __privateSet(this, _tickPromise, void 0);
1735
+ __privateSet(this, _tickPromiseResolver, void 0);
1736
+ __privateSet(this, _tickPromiseRejecter, void 0);
1770
1737
  });
1738
+ return __privateGet(this, _tickPromise);
1771
1739
  };
1772
- waitForStreamEnd_fn = function() {
1773
- return __async(this, null, function* () {
1774
- if (!__privateGet(this, _isMidStream)) {
1775
- return;
1776
- }
1777
- if (__privateGet(this, _midStreamPromise)) {
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);
1786
- });
1740
+ waitForStreamEnd_fn = async function() {
1741
+ if (!__privateGet(this, _isMidStream)) {
1742
+ return;
1743
+ }
1744
+ if (__privateGet(this, _midStreamPromise)) {
1787
1745
  return __privateGet(this, _midStreamPromise);
1746
+ }
1747
+ __privateSet(this, _midStreamPromise, new Promise((resolve) => {
1748
+ __privateSet(this, _midStreamPromiseResolver, resolve);
1749
+ }));
1750
+ __privateGet(this, _midStreamPromise).finally(() => {
1751
+ __privateSet(this, _midStreamPromise, void 0);
1752
+ __privateSet(this, _midStreamPromiseResolver, void 0);
1788
1753
  });
1754
+ return __privateGet(this, _midStreamPromise);
1789
1755
  };
1790
- publish_fn = function(messages) {
1791
- return __async(this, null, function* () {
1792
- __privateSet(this, _messageChain, __privateGet(this, _messageChain).then(
1793
- () => Promise.all(
1794
- Array.from(__privateGet(this, _subscribers).values()).map((_0) => __async(this, [_0], function* ([callback, __]) {
1795
- try {
1796
- yield callback(messages);
1797
- } catch (err) {
1798
- queueMicrotask(() => {
1799
- throw err;
1800
- });
1801
- }
1802
- }))
1803
- )
1804
- ));
1805
- return __privateGet(this, _messageChain);
1806
- });
1756
+ publish_fn = async function(messages) {
1757
+ __privateSet(this, _messageChain, __privateGet(this, _messageChain).then(
1758
+ () => Promise.all(
1759
+ Array.from(__privateGet(this, _subscribers).values()).map(async ([callback, __]) => {
1760
+ try {
1761
+ await callback(messages);
1762
+ } catch (err) {
1763
+ queueMicrotask(() => {
1764
+ throw err;
1765
+ });
1766
+ }
1767
+ })
1768
+ )
1769
+ ));
1770
+ return __privateGet(this, _messageChain);
1807
1771
  };
1808
1772
  sendErrorToSubscribers_fn = function(error) {
1809
1773
  __privateGet(this, _subscribers).forEach(([_, errorFn]) => {
@@ -1975,13 +1939,11 @@ var Shape = class {
1975
1939
  * Request a snapshot for subset of data. Only available when mode is changes_only.
1976
1940
  * Returns void; data will be emitted via the stream and processed by this Shape.
1977
1941
  */
1978
- requestSnapshot(params) {
1979
- return __async(this, null, function* () {
1980
- const key = JSON.stringify(params);
1981
- __privateGet(this, _requestedSubSnapshots).add(key);
1982
- yield __privateMethod(this, _Shape_instances, awaitUpToDate_fn).call(this);
1983
- yield this.stream.requestSnapshot(params);
1984
- });
1942
+ async requestSnapshot(params) {
1943
+ const key = JSON.stringify(params);
1944
+ __privateGet(this, _requestedSubSnapshots).add(key);
1945
+ await __privateMethod(this, _Shape_instances, awaitUpToDate_fn).call(this);
1946
+ await this.stream.requestSnapshot(params);
1985
1947
  }
1986
1948
  subscribe(callback) {
1987
1949
  const subscriptionId = Math.random();
@@ -2063,38 +2025,34 @@ process_fn = function(messages) {
2063
2025
  });
2064
2026
  if (shouldNotify) __privateMethod(this, _Shape_instances, notify_fn).call(this);
2065
2027
  };
2066
- reexecuteSnapshots_fn = function() {
2067
- return __async(this, null, function* () {
2068
- yield __privateMethod(this, _Shape_instances, awaitUpToDate_fn).call(this);
2069
- yield Promise.all(
2070
- Array.from(__privateGet(this, _requestedSubSnapshots)).map((jsonParams) => __async(this, null, function* () {
2071
- try {
2072
- const snapshot = JSON.parse(jsonParams);
2073
- yield this.stream.requestSnapshot(snapshot);
2074
- } catch (_) {
2075
- }
2076
- }))
2077
- );
2078
- });
2028
+ reexecuteSnapshots_fn = async function() {
2029
+ await __privateMethod(this, _Shape_instances, awaitUpToDate_fn).call(this);
2030
+ await Promise.all(
2031
+ Array.from(__privateGet(this, _requestedSubSnapshots)).map(async (jsonParams) => {
2032
+ try {
2033
+ const snapshot = JSON.parse(jsonParams);
2034
+ await this.stream.requestSnapshot(snapshot);
2035
+ } catch (_) {
2036
+ }
2037
+ })
2038
+ );
2079
2039
  };
2080
- awaitUpToDate_fn = function() {
2081
- return __async(this, null, function* () {
2082
- if (this.stream.isUpToDate) return;
2083
- yield new Promise((resolve) => {
2084
- const check = () => {
2085
- if (this.stream.isUpToDate) {
2086
- clearInterval(interval);
2087
- unsub();
2088
- resolve();
2089
- }
2090
- };
2091
- const interval = setInterval(check, 10);
2092
- const unsub = this.stream.subscribe(
2093
- () => check(),
2094
- () => check()
2095
- );
2096
- check();
2097
- });
2040
+ awaitUpToDate_fn = async function() {
2041
+ if (this.stream.isUpToDate) return;
2042
+ await new Promise((resolve) => {
2043
+ const check = () => {
2044
+ if (this.stream.isUpToDate) {
2045
+ clearInterval(interval);
2046
+ unsub();
2047
+ resolve();
2048
+ }
2049
+ };
2050
+ const interval = setInterval(check, 10);
2051
+ const unsub = this.stream.subscribe(
2052
+ () => check(),
2053
+ () => check()
2054
+ );
2055
+ check();
2098
2056
  });
2099
2057
  };
2100
2058
  updateShapeStatus_fn = function(status) {