@electric-sql/client 1.2.2 → 1.3.0

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);
@@ -1058,43 +1040,35 @@ var RESERVED_PARAMS = /* @__PURE__ */ new Set([
1058
1040
  LIVE_QUERY_PARAM,
1059
1041
  OFFSET_QUERY_PARAM
1060
1042
  ]);
1061
- function resolveValue(value) {
1062
- return __async(this, null, function* () {
1063
- if (typeof value === `function`) {
1064
- return value();
1065
- }
1066
- return value;
1067
- });
1043
+ async function resolveValue(value) {
1044
+ if (typeof value === `function`) {
1045
+ return value();
1046
+ }
1047
+ return value;
1068
1048
  }
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
- });
1049
+ async function toInternalParams(params) {
1050
+ const entries = Object.entries(params);
1051
+ const resolvedEntries = await Promise.all(
1052
+ entries.map(async ([key, value]) => {
1053
+ if (value === void 0) return [key, void 0];
1054
+ const resolvedValue = await resolveValue(value);
1055
+ return [
1056
+ key,
1057
+ Array.isArray(resolvedValue) ? resolvedValue.join(`,`) : resolvedValue
1058
+ ];
1059
+ })
1060
+ );
1061
+ return Object.fromEntries(
1062
+ resolvedEntries.filter(([_, value]) => value !== void 0)
1063
+ );
1086
1064
  }
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
- });
1065
+ async function resolveHeaders(headers) {
1066
+ if (!headers) return {};
1067
+ const entries = Object.entries(headers);
1068
+ const resolvedEntries = await Promise.all(
1069
+ entries.map(async ([key, value]) => [key, await resolveValue(value)])
1070
+ );
1071
+ return Object.fromEntries(resolvedEntries);
1098
1072
  }
1099
1073
  function canonicalShapeKey(url) {
1100
1074
  const cleanUrl = new URL(url.origin + url.pathname);
@@ -1256,16 +1230,14 @@ var ShapeStream = class {
1256
1230
  * long polling, ensuring that the stream receives an up to date message with the
1257
1231
  * latest LSN from Postgres at that point in time.
1258
1232
  */
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
- });
1233
+ async forceDisconnectAndRefresh() {
1234
+ var _a, _b;
1235
+ __privateSet(this, _isRefreshing, true);
1236
+ if (__privateGet(this, _isUpToDate) && !((_a = __privateGet(this, _requestAbortController)) == null ? void 0 : _a.signal.aborted)) {
1237
+ (_b = __privateGet(this, _requestAbortController)) == null ? void 0 : _b.abort(FORCE_DISCONNECT_AND_REFRESH);
1238
+ }
1239
+ await __privateMethod(this, _ShapeStream_instances, nextTick_fn).call(this);
1240
+ __privateSet(this, _isRefreshing, false);
1269
1241
  }
1270
1242
  /**
1271
1243
  * Request a snapshot for subset of data and inject it into the subscribed data stream.
@@ -1281,40 +1253,39 @@ var ShapeStream = class {
1281
1253
  * @param opts - The options for the snapshot request.
1282
1254
  * @returns The metadata and the data for the snapshot.
1283
1255
  */
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
- );
1256
+ async requestSnapshot(opts) {
1257
+ if (__privateGet(this, _mode) === `full`) {
1258
+ throw new Error(
1259
+ `Snapshot requests are not supported in ${__privateGet(this, _mode)} mode, as the consumer is guaranteed to observe all data`
1260
+ );
1261
+ }
1262
+ if (!__privateGet(this, _started)) await __privateMethod(this, _ShapeStream_instances, start_fn).call(this);
1263
+ await __privateMethod(this, _ShapeStream_instances, waitForStreamEnd_fn).call(this);
1264
+ __privateWrapper(this, _activeSnapshotRequests)._++;
1265
+ try {
1266
+ if (__privateGet(this, _activeSnapshotRequests) === 1) {
1267
+ __privateMethod(this, _ShapeStream_instances, pause_fn).call(this);
1290
1268
  }
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
- }
1269
+ const { metadata, data } = await this.fetchSnapshot(opts);
1270
+ const dataWithEndBoundary = data.concat([
1271
+ { headers: __spreadValues({ control: `snapshot-end` }, metadata) },
1272
+ { headers: __spreadValues({ control: `subset-end` }, opts) }
1273
+ ]);
1274
+ __privateGet(this, _snapshotTracker).addSnapshot(
1275
+ metadata,
1276
+ new Set(data.map((message) => message.key))
1277
+ );
1278
+ __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, dataWithEndBoundary, false);
1279
+ return {
1280
+ metadata,
1281
+ data
1282
+ };
1283
+ } finally {
1284
+ __privateWrapper(this, _activeSnapshotRequests)._--;
1285
+ if (__privateGet(this, _activeSnapshotRequests) === 0) {
1286
+ __privateMethod(this, _ShapeStream_instances, resume_fn).call(this);
1316
1287
  }
1317
- });
1288
+ }
1318
1289
  }
1319
1290
  /**
1320
1291
  * Fetch a snapshot for subset of data.
@@ -1323,36 +1294,34 @@ var ShapeStream = class {
1323
1294
  * @param opts - The options for the snapshot request.
1324
1295
  * @returns The metadata and the data for the snapshot.
1325
1296
  */
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
1297
+ async fetchSnapshot(opts) {
1298
+ var _a;
1299
+ const { fetchUrl, requestHeaders } = await __privateMethod(this, _ShapeStream_instances, constructUrl_fn).call(this, this.options.url, true, opts);
1300
+ const response = await __privateGet(this, _fetchClient2).call(this, fetchUrl.toString(), {
1301
+ headers: requestHeaders
1302
+ });
1303
+ if (!response.ok) {
1304
+ throw new FetchError(
1305
+ response.status,
1306
+ void 0,
1307
+ void 0,
1308
+ Object.fromEntries([...response.headers.entries()]),
1309
+ fetchUrl.toString()
1350
1310
  );
1351
- return {
1352
- metadata,
1353
- data
1354
- };
1311
+ }
1312
+ const schema = (_a = __privateGet(this, _schema)) != null ? _a : getSchemaFromHeaders(response.headers, {
1313
+ required: true,
1314
+ url: fetchUrl.toString()
1355
1315
  });
1316
+ const { metadata, data: rawData } = await response.json();
1317
+ const data = __privateGet(this, _messageParser).parseSnapshotData(
1318
+ rawData,
1319
+ schema
1320
+ );
1321
+ return {
1322
+ metadata,
1323
+ data
1324
+ };
1356
1325
  }
1357
1326
  };
1358
1327
  _error = new WeakMap();
@@ -1396,379 +1365,361 @@ _ShapeStream_instances = new WeakSet();
1396
1365
  replayMode_get = function() {
1397
1366
  return __privateGet(this, _lastSeenCursor) !== void 0;
1398
1367
  };
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;
1368
+ start_fn = async function() {
1369
+ var _a, _b, _c, _d, _e;
1370
+ __privateSet(this, _started, true);
1371
+ try {
1372
+ await __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1373
+ } catch (err) {
1374
+ __privateSet(this, _error, err);
1375
+ if (__privateGet(this, _onError)) {
1376
+ const retryOpts = await __privateGet(this, _onError).call(this, err);
1377
+ if (retryOpts && typeof retryOpts === `object`) {
1378
+ if (retryOpts.params) {
1379
+ this.options.params = __spreadValues(__spreadValues({}, (_a = this.options.params) != null ? _a : {}), retryOpts.params);
1420
1380
  }
1421
- if (err instanceof Error) {
1422
- __privateMethod(this, _ShapeStream_instances, sendErrorToSubscribers_fn).call(this, err);
1381
+ if (retryOpts.headers) {
1382
+ this.options.headers = __spreadValues(__spreadValues({}, (_b = this.options.headers) != null ? _b : {}), retryOpts.headers);
1423
1383
  }
1424
- __privateSet(this, _connected, false);
1425
- (_c = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _c.call(this);
1384
+ __privateSet(this, _error, null);
1385
+ __privateSet(this, _started, false);
1386
+ await __privateMethod(this, _ShapeStream_instances, start_fn).call(this);
1426
1387
  return;
1427
1388
  }
1428
1389
  if (err instanceof Error) {
1429
1390
  __privateMethod(this, _ShapeStream_instances, sendErrorToSubscribers_fn).call(this, err);
1430
1391
  }
1431
1392
  __privateSet(this, _connected, false);
1432
- (_d = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _d.call(this);
1433
- throw err;
1393
+ (_c = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _c.call(this);
1394
+ return;
1395
+ }
1396
+ if (err instanceof Error) {
1397
+ __privateMethod(this, _ShapeStream_instances, sendErrorToSubscribers_fn).call(this, err);
1434
1398
  }
1435
1399
  __privateSet(this, _connected, false);
1436
- (_e = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _e.call(this);
1437
- });
1400
+ (_d = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _d.call(this);
1401
+ throw err;
1402
+ }
1403
+ __privateSet(this, _connected, false);
1404
+ (_e = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _e.call(this);
1438
1405
  };
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;
1406
+ requestShape_fn = async function() {
1407
+ var _a, _b;
1408
+ if (__privateGet(this, _state) === `pause-requested`) {
1409
+ __privateSet(this, _state, `paused`);
1410
+ return;
1411
+ }
1412
+ if (!this.options.subscribe && (((_a = this.options.signal) == null ? void 0 : _a.aborted) || __privateGet(this, _isUpToDate))) {
1413
+ return;
1414
+ }
1415
+ const resumingFromPause = __privateGet(this, _state) === `paused`;
1416
+ __privateSet(this, _state, `active`);
1417
+ const { url, signal } = this.options;
1418
+ const { fetchUrl, requestHeaders } = await __privateMethod(this, _ShapeStream_instances, constructUrl_fn).call(this, url, resumingFromPause);
1419
+ const abortListener = await __privateMethod(this, _ShapeStream_instances, createAbortListener_fn).call(this, signal);
1420
+ const requestAbortController = __privateGet(this, _requestAbortController);
1421
+ try {
1422
+ await __privateMethod(this, _ShapeStream_instances, fetchShape_fn).call(this, {
1423
+ fetchUrl,
1424
+ requestAbortController,
1425
+ headers: requestHeaders,
1426
+ resumingFromPause
1427
+ });
1428
+ } catch (e) {
1429
+ if ((e instanceof FetchError || e instanceof FetchBackoffAbortError) && requestAbortController.signal.aborted && requestAbortController.signal.reason === FORCE_DISCONNECT_AND_REFRESH) {
1430
+ return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1445
1431
  }
1446
- if (!this.options.subscribe && (((_a = this.options.signal) == null ? void 0 : _a.aborted) || __privateGet(this, _isUpToDate))) {
1432
+ if (e instanceof FetchBackoffAbortError) {
1433
+ const currentState = __privateGet(this, _state);
1434
+ if (requestAbortController.signal.aborted && requestAbortController.signal.reason === PAUSE_STREAM && currentState === `pause-requested`) {
1435
+ __privateSet(this, _state, `paused`);
1436
+ }
1447
1437
  return;
1448
1438
  }
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);
1439
+ if (!(e instanceof FetchError)) throw e;
1440
+ if (e.status == 409) {
1441
+ if (__privateGet(this, _shapeHandle)) {
1442
+ const shapeKey = canonicalShapeKey(fetchUrl);
1443
+ expiredShapesCache.markExpired(shapeKey, __privateGet(this, _shapeHandle));
1465
1444
  }
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;
1485
- }
1486
- } finally {
1487
- if (abortListener && signal) {
1488
- signal.removeEventListener(`abort`, abortListener);
1489
- }
1490
- __privateSet(this, _requestAbortController, void 0);
1445
+ const newShapeHandle = e.headers[SHAPE_HANDLE_HEADER] || `${__privateGet(this, _shapeHandle)}-next`;
1446
+ __privateMethod(this, _ShapeStream_instances, reset_fn).call(this, newShapeHandle);
1447
+ await __privateMethod(this, _ShapeStream_instances, publish_fn).call(this, Array.isArray(e.json) ? e.json : [e.json]);
1448
+ return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1449
+ } else {
1450
+ throw e;
1491
1451
  }
1492
- (_b = __privateGet(this, _tickPromiseResolver)) == null ? void 0 : _b.call(this);
1493
- return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1494
- });
1452
+ } finally {
1453
+ if (abortListener && signal) {
1454
+ signal.removeEventListener(`abort`, abortListener);
1455
+ }
1456
+ __privateSet(this, _requestAbortController, void 0);
1457
+ }
1458
+ (_b = __privateGet(this, _tickPromiseResolver)) == null ? void 0 : _b.call(this);
1459
+ return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
1495
1460
  };
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);
1461
+ constructUrl_fn = async function(url, resumingFromPause, subsetParams) {
1462
+ var _a, _b, _c, _d;
1463
+ const [requestHeaders, params] = await Promise.all([
1464
+ resolveHeaders(this.options.headers),
1465
+ this.options.params ? toInternalParams(convertWhereParamsToObj(this.options.params)) : void 0
1466
+ ]);
1467
+ if (params) validateParams(params);
1468
+ const fetchUrl = new URL(url);
1469
+ if (params) {
1470
+ if (params.table) setQueryParam(fetchUrl, TABLE_QUERY_PARAM, params.table);
1471
+ if (params.where && typeof params.where === `string`) {
1472
+ const encodedWhere = encodeWhereClause(
1473
+ params.where,
1474
+ (_a = this.options.columnMapper) == null ? void 0 : _a.encode
1475
+ );
1476
+ setQueryParam(fetchUrl, WHERE_QUERY_PARAM, encodedWhere);
1477
+ }
1478
+ if (params.columns) {
1479
+ const originalColumns = await resolveValue((_b = this.options.params) == null ? void 0 : _b.columns);
1480
+ if (Array.isArray(originalColumns)) {
1481
+ let encodedColumns = originalColumns.map(String);
1482
+ if (this.options.columnMapper) {
1483
+ encodedColumns = encodedColumns.map(
1484
+ this.options.columnMapper.encode
1485
+ );
1527
1486
  }
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);
1487
+ const serializedColumns = encodedColumns.map(quoteIdentifier).join(`,`);
1488
+ setQueryParam(fetchUrl, COLUMNS_QUERY_PARAM, serializedColumns);
1489
+ } else {
1490
+ setQueryParam(fetchUrl, COLUMNS_QUERY_PARAM, params.columns);
1540
1491
  }
1541
1492
  }
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
- }
1493
+ if (params.replica) setQueryParam(fetchUrl, REPLICA_PARAM, params.replica);
1494
+ if (params.params)
1495
+ setQueryParam(fetchUrl, WHERE_PARAMS_PARAM, params.params);
1496
+ const customParams = __spreadValues({}, params);
1497
+ delete customParams.table;
1498
+ delete customParams.where;
1499
+ delete customParams.columns;
1500
+ delete customParams.replica;
1501
+ delete customParams.params;
1502
+ for (const [key, value] of Object.entries(customParams)) {
1503
+ setQueryParam(fetchUrl, key, value);
1566
1504
  }
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)
1505
+ }
1506
+ if (subsetParams) {
1507
+ if (subsetParams.where && typeof subsetParams.where === `string`) {
1508
+ const encodedWhere = encodeWhereClause(
1509
+ subsetParams.where,
1510
+ (_c = this.options.columnMapper) == null ? void 0 : _c.encode
1577
1511
  );
1512
+ setQueryParam(fetchUrl, SUBSET_PARAM_WHERE, encodedWhere);
1578
1513
  }
1579
- if (__privateGet(this, _shapeHandle)) {
1580
- fetchUrl.searchParams.set(SHAPE_HANDLE_QUERY_PARAM, __privateGet(this, _shapeHandle));
1514
+ if (subsetParams.params)
1515
+ fetchUrl.searchParams.set(
1516
+ SUBSET_PARAM_WHERE_PARAMS,
1517
+ JSON.stringify(subsetParams.params)
1518
+ );
1519
+ if (subsetParams.limit)
1520
+ setQueryParam(fetchUrl, SUBSET_PARAM_LIMIT, subsetParams.limit);
1521
+ if (subsetParams.offset)
1522
+ setQueryParam(fetchUrl, SUBSET_PARAM_OFFSET, subsetParams.offset);
1523
+ if (subsetParams.orderBy && typeof subsetParams.orderBy === `string`) {
1524
+ const encodedOrderBy = encodeWhereClause(
1525
+ subsetParams.orderBy,
1526
+ (_d = this.options.columnMapper) == null ? void 0 : _d.encode
1527
+ );
1528
+ setQueryParam(fetchUrl, SUBSET_PARAM_ORDER_BY, encodedOrderBy);
1581
1529
  }
1582
- const shapeKey = canonicalShapeKey(fetchUrl);
1583
- const expiredHandle = expiredShapesCache.getExpiredHandle(shapeKey);
1584
- if (expiredHandle) {
1585
- fetchUrl.searchParams.set(EXPIRED_HANDLE_QUERY_PARAM, expiredHandle);
1530
+ }
1531
+ fetchUrl.searchParams.set(OFFSET_QUERY_PARAM, __privateGet(this, _lastOffset));
1532
+ fetchUrl.searchParams.set(LOG_MODE_QUERY_PARAM, __privateGet(this, _mode));
1533
+ const isSnapshotRequest = subsetParams !== void 0;
1534
+ if (__privateGet(this, _isUpToDate) && !isSnapshotRequest) {
1535
+ if (!__privateGet(this, _isRefreshing) && !resumingFromPause) {
1536
+ fetchUrl.searchParams.set(LIVE_QUERY_PARAM, `true`);
1586
1537
  }
1587
- fetchUrl.searchParams.sort();
1588
- return {
1589
- fetchUrl,
1590
- requestHeaders
1591
- };
1592
- });
1538
+ fetchUrl.searchParams.set(
1539
+ LIVE_CACHE_BUSTER_QUERY_PARAM,
1540
+ __privateGet(this, _liveCacheBuster)
1541
+ );
1542
+ }
1543
+ if (__privateGet(this, _shapeHandle)) {
1544
+ fetchUrl.searchParams.set(SHAPE_HANDLE_QUERY_PARAM, __privateGet(this, _shapeHandle));
1545
+ }
1546
+ const shapeKey = canonicalShapeKey(fetchUrl);
1547
+ const expiredHandle = expiredShapesCache.getExpiredHandle(shapeKey);
1548
+ if (expiredHandle) {
1549
+ fetchUrl.searchParams.set(EXPIRED_HANDLE_QUERY_PARAM, expiredHandle);
1550
+ }
1551
+ fetchUrl.searchParams.sort();
1552
+ return {
1553
+ fetchUrl,
1554
+ requestHeaders
1555
+ };
1593
1556
  };
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;
1557
+ createAbortListener_fn = async function(signal) {
1558
+ var _a;
1559
+ __privateSet(this, _requestAbortController, new AbortController());
1560
+ if (signal) {
1561
+ const abortListener = () => {
1562
+ var _a2;
1563
+ (_a2 = __privateGet(this, _requestAbortController)) == null ? void 0 : _a2.abort(signal.reason);
1564
+ };
1565
+ signal.addEventListener(`abort`, abortListener, { once: true });
1566
+ if (signal.aborted) {
1567
+ (_a = __privateGet(this, _requestAbortController)) == null ? void 0 : _a.abort(signal.reason);
1608
1568
  }
1609
- });
1569
+ return abortListener;
1570
+ }
1610
1571
  };
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) {
1617
- __privateSet(this, _shapeHandle, shapeHandle);
1618
- }
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
- });
1572
+ onInitialResponse_fn = async function(response) {
1573
+ var _a;
1574
+ const { headers, status } = response;
1575
+ const shapeHandle = headers.get(SHAPE_HANDLE_HEADER);
1576
+ if (shapeHandle) {
1577
+ __privateSet(this, _shapeHandle, shapeHandle);
1578
+ }
1579
+ const lastOffset = headers.get(CHUNK_LAST_OFFSET_HEADER);
1580
+ if (lastOffset) {
1581
+ __privateSet(this, _lastOffset, lastOffset);
1582
+ }
1583
+ const liveCacheBuster = headers.get(LIVE_CACHE_BUSTER_HEADER);
1584
+ if (liveCacheBuster) {
1585
+ __privateSet(this, _liveCacheBuster, liveCacheBuster);
1586
+ }
1587
+ __privateSet(this, _schema, (_a = __privateGet(this, _schema)) != null ? _a : getSchemaFromHeaders(headers));
1588
+ if (status === 204) {
1589
+ __privateSet(this, _lastSyncedAt, Date.now());
1590
+ }
1632
1591
  };
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));
1592
+ onMessages_fn = async function(batch, isSseMessage = false) {
1593
+ var _a;
1594
+ if (batch.length > 0) {
1595
+ __privateSet(this, _isMidStream, true);
1596
+ const lastMessage = batch[batch.length - 1];
1597
+ if (isUpToDateMessage(lastMessage)) {
1598
+ if (isSseMessage) {
1599
+ const offset = getOffset(lastMessage);
1600
+ if (offset) {
1601
+ __privateSet(this, _lastOffset, offset);
1660
1602
  }
1661
1603
  }
1662
- const messagesToProcess = batch.filter((message) => {
1663
- if (isChangeMessage(message)) {
1664
- return !__privateGet(this, _snapshotTracker).shouldRejectMessage(message);
1604
+ __privateSet(this, _lastSyncedAt, Date.now());
1605
+ __privateSet(this, _isUpToDate, true);
1606
+ __privateSet(this, _isMidStream, false);
1607
+ (_a = __privateGet(this, _midStreamPromiseResolver)) == null ? void 0 : _a.call(this);
1608
+ if (__privateGet(this, _ShapeStream_instances, replayMode_get) && !isSseMessage) {
1609
+ const currentCursor = __privateGet(this, _liveCacheBuster);
1610
+ if (currentCursor === __privateGet(this, _lastSeenCursor)) {
1611
+ return;
1665
1612
  }
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);
1613
+ }
1614
+ __privateSet(this, _lastSeenCursor, void 0);
1615
+ if (__privateGet(this, _currentFetchUrl)) {
1616
+ const shapeKey = canonicalShapeKey(__privateGet(this, _currentFetchUrl));
1617
+ upToDateTracker.recordUpToDate(shapeKey, __privateGet(this, _liveCacheBuster));
1681
1618
  }
1682
1619
  }
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);
1620
+ const messagesToProcess = batch.filter((message) => {
1621
+ if (isChangeMessage(message)) {
1622
+ return !__privateGet(this, _snapshotTracker).shouldRejectMessage(message);
1623
+ }
1624
+ return true;
1625
+ });
1626
+ await __privateMethod(this, _ShapeStream_instances, publish_fn).call(this, messagesToProcess);
1627
+ }
1628
+ };
1629
+ fetchShape_fn = async function(opts) {
1630
+ var _a;
1631
+ __privateSet(this, _currentFetchUrl, opts.fetchUrl);
1632
+ if (!__privateGet(this, _isUpToDate) && !__privateGet(this, _ShapeStream_instances, replayMode_get)) {
1633
+ const shapeKey = canonicalShapeKey(opts.fetchUrl);
1634
+ const lastSeenCursor = upToDateTracker.shouldEnterReplayMode(shapeKey);
1635
+ if (lastSeenCursor) {
1636
+ __privateSet(this, _lastSeenCursor, lastSeenCursor);
1688
1637
  }
1689
- return __privateMethod(this, _ShapeStream_instances, requestShapeLongPoll_fn).call(this, opts);
1690
- });
1638
+ }
1639
+ const useSse = (_a = this.options.liveSse) != null ? _a : this.options.experimentalLiveSse;
1640
+ if (__privateGet(this, _isUpToDate) && useSse && !__privateGet(this, _isRefreshing) && !opts.resumingFromPause && !__privateGet(this, _sseFallbackToLongPolling)) {
1641
+ opts.fetchUrl.searchParams.set(EXPERIMENTAL_LIVE_SSE_QUERY_PARAM, `true`);
1642
+ opts.fetchUrl.searchParams.set(LIVE_SSE_QUERY_PARAM, `true`);
1643
+ return __privateMethod(this, _ShapeStream_instances, requestShapeSSE_fn).call(this, opts);
1644
+ }
1645
+ return __privateMethod(this, _ShapeStream_instances, requestShapeLongPoll_fn).call(this, opts);
1691
1646
  };
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);
1647
+ requestShapeLongPoll_fn = async function(opts) {
1648
+ const { fetchUrl, requestAbortController, headers } = opts;
1649
+ const response = await __privateGet(this, _fetchClient2).call(this, fetchUrl.toString(), {
1650
+ signal: requestAbortController.signal,
1651
+ headers
1706
1652
  });
1653
+ __privateSet(this, _connected, true);
1654
+ await __privateMethod(this, _ShapeStream_instances, onInitialResponse_fn).call(this, response);
1655
+ const schema = __privateGet(this, _schema);
1656
+ const res = await response.text();
1657
+ const messages = res || `[]`;
1658
+ const batch = __privateGet(this, _messageParser).parse(messages, schema);
1659
+ await __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, batch);
1707
1660
  };
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))
1661
+ requestShapeSSE_fn = async function(opts) {
1662
+ const { fetchUrl, requestAbortController, headers } = opts;
1663
+ const fetch2 = __privateGet(this, _sseFetchClient);
1664
+ __privateSet(this, _lastSseConnectionStartTime, Date.now());
1665
+ const sseHeaders = __spreadProps(__spreadValues({}, headers), {
1666
+ Accept: `text/event-stream`
1667
+ });
1668
+ try {
1669
+ let buffer = [];
1670
+ await (0, import_fetch_event_source.fetchEventSource)(fetchUrl.toString(), {
1671
+ headers: sseHeaders,
1672
+ fetch: fetch2,
1673
+ onopen: async (response) => {
1674
+ __privateSet(this, _connected, true);
1675
+ await __privateMethod(this, _ShapeStream_instances, onInitialResponse_fn).call(this, response);
1676
+ },
1677
+ onmessage: (event) => {
1678
+ if (event.data) {
1679
+ const schema = __privateGet(this, _schema);
1680
+ const message = __privateGet(this, _messageParser).parse(
1681
+ event.data,
1682
+ schema
1763
1683
  );
1764
- const delayMs = Math.floor(Math.random() * maxDelay);
1765
- yield new Promise((resolve) => setTimeout(resolve, delayMs));
1684
+ buffer.push(message);
1685
+ if (isUpToDateMessage(message)) {
1686
+ __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, buffer, true);
1687
+ buffer = [];
1688
+ }
1766
1689
  }
1767
- } else if (connectionDuration >= __privateGet(this, _minSseConnectionDuration)) {
1768
- __privateSet(this, _consecutiveShortSseConnections, 0);
1690
+ },
1691
+ onerror: (error) => {
1692
+ throw error;
1693
+ },
1694
+ signal: requestAbortController.signal
1695
+ });
1696
+ } catch (error) {
1697
+ if (requestAbortController.signal.aborted) {
1698
+ throw new FetchBackoffAbortError();
1699
+ }
1700
+ throw error;
1701
+ } finally {
1702
+ const connectionDuration = Date.now() - __privateGet(this, _lastSseConnectionStartTime);
1703
+ const wasAborted = requestAbortController.signal.aborted;
1704
+ if (connectionDuration < __privateGet(this, _minSseConnectionDuration) && !wasAborted) {
1705
+ __privateWrapper(this, _consecutiveShortSseConnections)._++;
1706
+ if (__privateGet(this, _consecutiveShortSseConnections) >= __privateGet(this, _maxShortSseConnections)) {
1707
+ __privateSet(this, _sseFallbackToLongPolling, true);
1708
+ console.warn(
1709
+ `[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.`
1710
+ );
1711
+ } else {
1712
+ const maxDelay = Math.min(
1713
+ __privateGet(this, _sseBackoffMaxDelay),
1714
+ __privateGet(this, _sseBackoffBaseDelay) * Math.pow(2, __privateGet(this, _consecutiveShortSseConnections))
1715
+ );
1716
+ const delayMs = Math.floor(Math.random() * maxDelay);
1717
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
1769
1718
  }
1719
+ } else if (connectionDuration >= __privateGet(this, _minSseConnectionDuration)) {
1720
+ __privateSet(this, _consecutiveShortSseConnections, 0);
1770
1721
  }
1771
- });
1722
+ }
1772
1723
  };
1773
1724
  pause_fn = function() {
1774
1725
  var _a;
@@ -1778,65 +1729,63 @@ pause_fn = function() {
1778
1729
  }
1779
1730
  };
1780
1731
  resume_fn = function() {
1732
+ var _a;
1781
1733
  if (__privateGet(this, _started) && (__privateGet(this, _state) === `paused` || __privateGet(this, _state) === `pause-requested`)) {
1734
+ if ((_a = this.options.signal) == null ? void 0 : _a.aborted) {
1735
+ return;
1736
+ }
1782
1737
  if (__privateGet(this, _state) === `pause-requested`) {
1783
1738
  __privateSet(this, _state, `active`);
1784
1739
  }
1785
1740
  __privateMethod(this, _ShapeStream_instances, start_fn).call(this);
1786
1741
  }
1787
1742
  };
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
- });
1743
+ nextTick_fn = async function() {
1744
+ if (__privateGet(this, _tickPromise)) {
1802
1745
  return __privateGet(this, _tickPromise);
1746
+ }
1747
+ __privateSet(this, _tickPromise, new Promise((resolve, reject) => {
1748
+ __privateSet(this, _tickPromiseResolver, resolve);
1749
+ __privateSet(this, _tickPromiseRejecter, reject);
1750
+ }));
1751
+ __privateGet(this, _tickPromise).finally(() => {
1752
+ __privateSet(this, _tickPromise, void 0);
1753
+ __privateSet(this, _tickPromiseResolver, void 0);
1754
+ __privateSet(this, _tickPromiseRejecter, void 0);
1803
1755
  });
1756
+ return __privateGet(this, _tickPromise);
1804
1757
  };
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
- });
1758
+ waitForStreamEnd_fn = async function() {
1759
+ if (!__privateGet(this, _isMidStream)) {
1760
+ return;
1761
+ }
1762
+ if (__privateGet(this, _midStreamPromise)) {
1820
1763
  return __privateGet(this, _midStreamPromise);
1764
+ }
1765
+ __privateSet(this, _midStreamPromise, new Promise((resolve) => {
1766
+ __privateSet(this, _midStreamPromiseResolver, resolve);
1767
+ }));
1768
+ __privateGet(this, _midStreamPromise).finally(() => {
1769
+ __privateSet(this, _midStreamPromise, void 0);
1770
+ __privateSet(this, _midStreamPromiseResolver, void 0);
1821
1771
  });
1772
+ return __privateGet(this, _midStreamPromise);
1822
1773
  };
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
- });
1774
+ publish_fn = async function(messages) {
1775
+ __privateSet(this, _messageChain, __privateGet(this, _messageChain).then(
1776
+ () => Promise.all(
1777
+ Array.from(__privateGet(this, _subscribers).values()).map(async ([callback, __]) => {
1778
+ try {
1779
+ await callback(messages);
1780
+ } catch (err) {
1781
+ queueMicrotask(() => {
1782
+ throw err;
1783
+ });
1784
+ }
1785
+ })
1786
+ )
1787
+ ));
1788
+ return __privateGet(this, _messageChain);
1840
1789
  };
1841
1790
  sendErrorToSubscribers_fn = function(error) {
1842
1791
  __privateGet(this, _subscribers).forEach(([_, errorFn]) => {
@@ -2008,13 +1957,11 @@ var Shape = class {
2008
1957
  * Request a snapshot for subset of data. Only available when mode is changes_only.
2009
1958
  * Returns void; data will be emitted via the stream and processed by this Shape.
2010
1959
  */
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
- });
1960
+ async requestSnapshot(params) {
1961
+ const key = JSON.stringify(params);
1962
+ __privateGet(this, _requestedSubSnapshots).add(key);
1963
+ await __privateMethod(this, _Shape_instances, awaitUpToDate_fn).call(this);
1964
+ await this.stream.requestSnapshot(params);
2018
1965
  }
2019
1966
  subscribe(callback) {
2020
1967
  const subscriptionId = Math.random();
@@ -2096,38 +2043,34 @@ process_fn = function(messages) {
2096
2043
  });
2097
2044
  if (shouldNotify) __privateMethod(this, _Shape_instances, notify_fn).call(this);
2098
2045
  };
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
- });
2046
+ reexecuteSnapshots_fn = async function() {
2047
+ await __privateMethod(this, _Shape_instances, awaitUpToDate_fn).call(this);
2048
+ await Promise.all(
2049
+ Array.from(__privateGet(this, _requestedSubSnapshots)).map(async (jsonParams) => {
2050
+ try {
2051
+ const snapshot = JSON.parse(jsonParams);
2052
+ await this.stream.requestSnapshot(snapshot);
2053
+ } catch (_) {
2054
+ }
2055
+ })
2056
+ );
2112
2057
  };
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
- });
2058
+ awaitUpToDate_fn = async function() {
2059
+ if (this.stream.isUpToDate) return;
2060
+ await new Promise((resolve) => {
2061
+ const check = () => {
2062
+ if (this.stream.isUpToDate) {
2063
+ clearInterval(interval);
2064
+ unsub();
2065
+ resolve();
2066
+ }
2067
+ };
2068
+ const interval = setInterval(check, 10);
2069
+ const unsub = this.stream.subscribe(
2070
+ () => check(),
2071
+ () => check()
2072
+ );
2073
+ check();
2131
2074
  });
2132
2075
  };
2133
2076
  updateShapeStatus_fn = function(status) {