@electric-sql/client 1.0.3 → 1.0.5
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/cjs/index.cjs +301 -140
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +29 -8
- package/dist/index.browser.mjs +5 -5
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.d.ts +29 -8
- package/dist/index.legacy-esm.js +287 -140
- package/dist/index.legacy-esm.js.map +1 -1
- package/dist/index.mjs +303 -140
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -3
- package/src/client.ts +432 -207
- package/src/constants.ts +2 -0
- package/src/fetch.ts +12 -2
- package/src/helpers.ts +13 -1
- package/src/types.ts +4 -1
package/dist/cjs/index.cjs
CHANGED
|
@@ -296,6 +296,12 @@ function isControlMessage(message) {
|
|
|
296
296
|
function isUpToDateMessage(message) {
|
|
297
297
|
return isControlMessage(message) && message.headers.control === `up-to-date`;
|
|
298
298
|
}
|
|
299
|
+
function getOffset(message) {
|
|
300
|
+
const lsn = Number(message.headers.global_last_seen_lsn);
|
|
301
|
+
if (lsn && !isNaN(lsn)) {
|
|
302
|
+
return `${lsn}_0`;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
299
305
|
|
|
300
306
|
// src/constants.ts
|
|
301
307
|
var LIVE_CACHE_BUSTER_HEADER = `electric-cursor`;
|
|
@@ -312,7 +318,9 @@ var TABLE_QUERY_PARAM = `table`;
|
|
|
312
318
|
var WHERE_QUERY_PARAM = `where`;
|
|
313
319
|
var REPLICA_PARAM = `replica`;
|
|
314
320
|
var WHERE_PARAMS_PARAM = `params`;
|
|
321
|
+
var EXPERIMENTAL_LIVE_SSE_QUERY_PARAM = `experimental_live_sse`;
|
|
315
322
|
var FORCE_DISCONNECT_AND_REFRESH = `force-disconnect-and-refresh`;
|
|
323
|
+
var PAUSE_STREAM = `pause-stream`;
|
|
316
324
|
|
|
317
325
|
// src/fetch.ts
|
|
318
326
|
var HTTP_RETRY_STATUS_CODES = [429];
|
|
@@ -321,7 +329,7 @@ var BackoffDefaults = {
|
|
|
321
329
|
maxDelay: 1e4,
|
|
322
330
|
multiplier: 1.3
|
|
323
331
|
};
|
|
324
|
-
function createFetchWithBackoff(fetchClient, backoffOptions = BackoffDefaults) {
|
|
332
|
+
function createFetchWithBackoff(fetchClient, backoffOptions = BackoffDefaults, sseMode = false) {
|
|
325
333
|
const {
|
|
326
334
|
initialDelay,
|
|
327
335
|
maxDelay,
|
|
@@ -339,7 +347,11 @@ function createFetchWithBackoff(fetchClient, backoffOptions = BackoffDefaults) {
|
|
|
339
347
|
try {
|
|
340
348
|
const result = yield fetchClient(...args);
|
|
341
349
|
if (result.ok) return result;
|
|
342
|
-
|
|
350
|
+
const err = yield FetchError.fromResponse(result, url.toString());
|
|
351
|
+
if (err.status === 409 && sseMode) {
|
|
352
|
+
err.json = [err.json];
|
|
353
|
+
}
|
|
354
|
+
throw err;
|
|
343
355
|
} catch (e) {
|
|
344
356
|
onFailedAttempt == null ? void 0 : onFailedAttempt();
|
|
345
357
|
if ((_a = options == null ? void 0 : options.signal) == null ? void 0 : _a.aborted) {
|
|
@@ -537,6 +549,7 @@ function noop() {
|
|
|
537
549
|
}
|
|
538
550
|
|
|
539
551
|
// src/client.ts
|
|
552
|
+
var import_fetch_event_source = require("@microsoft/fetch-event-source");
|
|
540
553
|
var RESERVED_PARAMS = /* @__PURE__ */ new Set([
|
|
541
554
|
LIVE_CACHE_BUSTER_QUERY_PARAM,
|
|
542
555
|
SHAPE_HANDLE_QUERY_PARAM,
|
|
@@ -581,15 +594,18 @@ function resolveHeaders(headers) {
|
|
|
581
594
|
return Object.fromEntries(resolvedEntries);
|
|
582
595
|
});
|
|
583
596
|
}
|
|
584
|
-
var _error, _fetchClient2, _messageParser, _subscribers, _started, _lastOffset, _liveCacheBuster, _lastSyncedAt, _isUpToDate, _connected, _shapeHandle, _schema, _onError, _requestAbortController, _isRefreshing, _tickPromise, _tickPromiseResolver, _tickPromiseRejecter, _ShapeStream_instances, start_fn, nextTick_fn, publish_fn, sendErrorToSubscribers_fn, reset_fn;
|
|
597
|
+
var _error, _fetchClient2, _sseFetchClient, _messageParser, _subscribers, _started, _state, _lastOffset, _liveCacheBuster, _lastSyncedAt, _isUpToDate, _connected, _shapeHandle, _schema, _onError, _requestAbortController, _isRefreshing, _tickPromise, _tickPromiseResolver, _tickPromiseRejecter, _messageChain, _ShapeStream_instances, start_fn, requestShape_fn, constructUrl_fn, createAbortListener_fn, onInitialResponse_fn, onMessages_fn, fetchShape_fn, requestShapeLongPoll_fn, requestShapeSSE_fn, pause_fn, resume_fn, nextTick_fn, publish_fn, sendErrorToSubscribers_fn, subscribeToVisibilityChanges_fn, reset_fn;
|
|
585
598
|
var ShapeStream = class {
|
|
599
|
+
// promise chain for incoming messages
|
|
586
600
|
constructor(options) {
|
|
587
601
|
__privateAdd(this, _ShapeStream_instances);
|
|
588
602
|
__privateAdd(this, _error, null);
|
|
589
603
|
__privateAdd(this, _fetchClient2);
|
|
604
|
+
__privateAdd(this, _sseFetchClient);
|
|
590
605
|
__privateAdd(this, _messageParser);
|
|
591
606
|
__privateAdd(this, _subscribers, /* @__PURE__ */ new Map());
|
|
592
607
|
__privateAdd(this, _started, false);
|
|
608
|
+
__privateAdd(this, _state, `active`);
|
|
593
609
|
__privateAdd(this, _lastOffset);
|
|
594
610
|
__privateAdd(this, _liveCacheBuster);
|
|
595
611
|
// Seconds since our Electric Epoch 😎
|
|
@@ -605,6 +621,7 @@ var ShapeStream = class {
|
|
|
605
621
|
__privateAdd(this, _tickPromise);
|
|
606
622
|
__privateAdd(this, _tickPromiseResolver);
|
|
607
623
|
__privateAdd(this, _tickPromiseRejecter);
|
|
624
|
+
__privateAdd(this, _messageChain, Promise.resolve([]));
|
|
608
625
|
var _a, _b, _c;
|
|
609
626
|
this.options = __spreadValues({ subscribe: true }, options);
|
|
610
627
|
validateOptions(this.options);
|
|
@@ -614,18 +631,31 @@ var ShapeStream = class {
|
|
|
614
631
|
__privateSet(this, _messageParser, new MessageParser(options.parser));
|
|
615
632
|
__privateSet(this, _onError, this.options.onError);
|
|
616
633
|
const baseFetchClient = (_b = options.fetchClient) != null ? _b : (...args) => fetch(...args);
|
|
617
|
-
const
|
|
634
|
+
const backOffOpts = __spreadProps(__spreadValues({}, (_c = options.backoffOptions) != null ? _c : BackoffDefaults), {
|
|
618
635
|
onFailedAttempt: () => {
|
|
619
636
|
var _a2, _b2;
|
|
620
637
|
__privateSet(this, _connected, false);
|
|
621
638
|
(_b2 = (_a2 = options.backoffOptions) == null ? void 0 : _a2.onFailedAttempt) == null ? void 0 : _b2.call(_a2);
|
|
622
639
|
}
|
|
623
|
-
})
|
|
640
|
+
});
|
|
641
|
+
const fetchWithBackoffClient = createFetchWithBackoff(
|
|
642
|
+
baseFetchClient,
|
|
643
|
+
backOffOpts
|
|
644
|
+
);
|
|
624
645
|
__privateSet(this, _fetchClient2, createFetchWithConsumedMessages(
|
|
625
646
|
createFetchWithResponseHeadersCheck(
|
|
626
647
|
createFetchWithChunkBuffer(fetchWithBackoffClient)
|
|
627
648
|
)
|
|
628
649
|
));
|
|
650
|
+
const sseFetchWithBackoffClient = createFetchWithBackoff(
|
|
651
|
+
baseFetchClient,
|
|
652
|
+
backOffOpts,
|
|
653
|
+
true
|
|
654
|
+
);
|
|
655
|
+
__privateSet(this, _sseFetchClient, createFetchWithResponseHeadersCheck(
|
|
656
|
+
createFetchWithChunkBuffer(sseFetchWithBackoffClient)
|
|
657
|
+
));
|
|
658
|
+
__privateMethod(this, _ShapeStream_instances, subscribeToVisibilityChanges_fn).call(this);
|
|
629
659
|
}
|
|
630
660
|
get shapeHandle() {
|
|
631
661
|
return __privateGet(this, _shapeHandle);
|
|
@@ -671,6 +701,9 @@ var ShapeStream = class {
|
|
|
671
701
|
hasStarted() {
|
|
672
702
|
return __privateGet(this, _started);
|
|
673
703
|
}
|
|
704
|
+
isPaused() {
|
|
705
|
+
return __privateGet(this, _state) === `paused`;
|
|
706
|
+
}
|
|
674
707
|
/**
|
|
675
708
|
* Refreshes the shape stream.
|
|
676
709
|
* This preemptively aborts any ongoing long poll and reconnects without
|
|
@@ -691,9 +724,11 @@ var ShapeStream = class {
|
|
|
691
724
|
};
|
|
692
725
|
_error = new WeakMap();
|
|
693
726
|
_fetchClient2 = new WeakMap();
|
|
727
|
+
_sseFetchClient = new WeakMap();
|
|
694
728
|
_messageParser = new WeakMap();
|
|
695
729
|
_subscribers = new WeakMap();
|
|
696
730
|
_started = new WeakMap();
|
|
731
|
+
_state = new WeakMap();
|
|
697
732
|
_lastOffset = new WeakMap();
|
|
698
733
|
_liveCacheBuster = new WeakMap();
|
|
699
734
|
_lastSyncedAt = new WeakMap();
|
|
@@ -707,134 +742,14 @@ _isRefreshing = new WeakMap();
|
|
|
707
742
|
_tickPromise = new WeakMap();
|
|
708
743
|
_tickPromiseResolver = new WeakMap();
|
|
709
744
|
_tickPromiseRejecter = new WeakMap();
|
|
745
|
+
_messageChain = new WeakMap();
|
|
710
746
|
_ShapeStream_instances = new WeakSet();
|
|
711
747
|
start_fn = function() {
|
|
712
748
|
return __async(this, null, function* () {
|
|
713
|
-
var _a
|
|
714
|
-
if (__privateGet(this, _started)) throw new Error(`Cannot start stream twice`);
|
|
749
|
+
var _a;
|
|
715
750
|
__privateSet(this, _started, true);
|
|
716
751
|
try {
|
|
717
|
-
|
|
718
|
-
const { url, signal } = this.options;
|
|
719
|
-
const [requestHeaders, params] = yield Promise.all([
|
|
720
|
-
resolveHeaders(this.options.headers),
|
|
721
|
-
this.options.params ? toInternalParams(convertWhereParamsToObj(this.options.params)) : void 0
|
|
722
|
-
]);
|
|
723
|
-
if (params) {
|
|
724
|
-
validateParams(params);
|
|
725
|
-
}
|
|
726
|
-
const fetchUrl = new URL(url);
|
|
727
|
-
if (params) {
|
|
728
|
-
if (params.table)
|
|
729
|
-
setQueryParam(fetchUrl, TABLE_QUERY_PARAM, params.table);
|
|
730
|
-
if (params.where)
|
|
731
|
-
setQueryParam(fetchUrl, WHERE_QUERY_PARAM, params.where);
|
|
732
|
-
if (params.columns)
|
|
733
|
-
setQueryParam(fetchUrl, COLUMNS_QUERY_PARAM, params.columns);
|
|
734
|
-
if (params.replica)
|
|
735
|
-
setQueryParam(fetchUrl, REPLICA_PARAM, params.replica);
|
|
736
|
-
if (params.params)
|
|
737
|
-
setQueryParam(fetchUrl, WHERE_PARAMS_PARAM, params.params);
|
|
738
|
-
const customParams = __spreadValues({}, params);
|
|
739
|
-
delete customParams.table;
|
|
740
|
-
delete customParams.where;
|
|
741
|
-
delete customParams.columns;
|
|
742
|
-
delete customParams.replica;
|
|
743
|
-
delete customParams.params;
|
|
744
|
-
for (const [key, value] of Object.entries(customParams)) {
|
|
745
|
-
setQueryParam(fetchUrl, key, value);
|
|
746
|
-
}
|
|
747
|
-
}
|
|
748
|
-
fetchUrl.searchParams.set(OFFSET_QUERY_PARAM, __privateGet(this, _lastOffset));
|
|
749
|
-
if (__privateGet(this, _isUpToDate)) {
|
|
750
|
-
if (!__privateGet(this, _isRefreshing)) {
|
|
751
|
-
fetchUrl.searchParams.set(LIVE_QUERY_PARAM, `true`);
|
|
752
|
-
}
|
|
753
|
-
fetchUrl.searchParams.set(
|
|
754
|
-
LIVE_CACHE_BUSTER_QUERY_PARAM,
|
|
755
|
-
__privateGet(this, _liveCacheBuster)
|
|
756
|
-
);
|
|
757
|
-
}
|
|
758
|
-
if (__privateGet(this, _shapeHandle)) {
|
|
759
|
-
fetchUrl.searchParams.set(
|
|
760
|
-
SHAPE_HANDLE_QUERY_PARAM,
|
|
761
|
-
__privateGet(this, _shapeHandle)
|
|
762
|
-
);
|
|
763
|
-
}
|
|
764
|
-
fetchUrl.searchParams.sort();
|
|
765
|
-
__privateSet(this, _requestAbortController, new AbortController());
|
|
766
|
-
let abortListener;
|
|
767
|
-
if (signal) {
|
|
768
|
-
abortListener = () => {
|
|
769
|
-
var _a2;
|
|
770
|
-
(_a2 = __privateGet(this, _requestAbortController)) == null ? void 0 : _a2.abort(signal.reason);
|
|
771
|
-
};
|
|
772
|
-
signal.addEventListener(`abort`, abortListener, { once: true });
|
|
773
|
-
if (signal.aborted) {
|
|
774
|
-
(_b = __privateGet(this, _requestAbortController)) == null ? void 0 : _b.abort(signal.reason);
|
|
775
|
-
}
|
|
776
|
-
}
|
|
777
|
-
let response;
|
|
778
|
-
try {
|
|
779
|
-
response = yield __privateGet(this, _fetchClient2).call(this, fetchUrl.toString(), {
|
|
780
|
-
signal: __privateGet(this, _requestAbortController).signal,
|
|
781
|
-
headers: requestHeaders
|
|
782
|
-
});
|
|
783
|
-
__privateSet(this, _connected, true);
|
|
784
|
-
} catch (e) {
|
|
785
|
-
if ((e instanceof FetchError || e instanceof FetchBackoffAbortError) && __privateGet(this, _requestAbortController).signal.aborted && __privateGet(this, _requestAbortController).signal.reason === FORCE_DISCONNECT_AND_REFRESH) {
|
|
786
|
-
continue;
|
|
787
|
-
}
|
|
788
|
-
if (e instanceof FetchBackoffAbortError) break;
|
|
789
|
-
if (!(e instanceof FetchError)) throw e;
|
|
790
|
-
if (e.status == 409) {
|
|
791
|
-
const newShapeHandle = e.headers[SHAPE_HANDLE_HEADER];
|
|
792
|
-
__privateMethod(this, _ShapeStream_instances, reset_fn).call(this, newShapeHandle);
|
|
793
|
-
yield __privateMethod(this, _ShapeStream_instances, publish_fn).call(this, e.json);
|
|
794
|
-
continue;
|
|
795
|
-
} else {
|
|
796
|
-
__privateMethod(this, _ShapeStream_instances, sendErrorToSubscribers_fn).call(this, e);
|
|
797
|
-
throw e;
|
|
798
|
-
}
|
|
799
|
-
} finally {
|
|
800
|
-
if (abortListener && signal) {
|
|
801
|
-
signal.removeEventListener(`abort`, abortListener);
|
|
802
|
-
}
|
|
803
|
-
__privateSet(this, _requestAbortController, void 0);
|
|
804
|
-
}
|
|
805
|
-
const { headers, status } = response;
|
|
806
|
-
const shapeHandle = headers.get(SHAPE_HANDLE_HEADER);
|
|
807
|
-
if (shapeHandle) {
|
|
808
|
-
__privateSet(this, _shapeHandle, shapeHandle);
|
|
809
|
-
}
|
|
810
|
-
const lastOffset = headers.get(CHUNK_LAST_OFFSET_HEADER);
|
|
811
|
-
if (lastOffset) {
|
|
812
|
-
__privateSet(this, _lastOffset, lastOffset);
|
|
813
|
-
}
|
|
814
|
-
const liveCacheBuster = headers.get(LIVE_CACHE_BUSTER_HEADER);
|
|
815
|
-
if (liveCacheBuster) {
|
|
816
|
-
__privateSet(this, _liveCacheBuster, liveCacheBuster);
|
|
817
|
-
}
|
|
818
|
-
const getSchema = () => {
|
|
819
|
-
const schemaHeader = headers.get(SHAPE_SCHEMA_HEADER);
|
|
820
|
-
return schemaHeader ? JSON.parse(schemaHeader) : {};
|
|
821
|
-
};
|
|
822
|
-
__privateSet(this, _schema, (_c = __privateGet(this, _schema)) != null ? _c : getSchema());
|
|
823
|
-
if (status === 204) {
|
|
824
|
-
__privateSet(this, _lastSyncedAt, Date.now());
|
|
825
|
-
}
|
|
826
|
-
const messages = (yield response.text()) || `[]`;
|
|
827
|
-
const batch = __privateGet(this, _messageParser).parse(messages, __privateGet(this, _schema));
|
|
828
|
-
if (batch.length > 0) {
|
|
829
|
-
const lastMessage = batch[batch.length - 1];
|
|
830
|
-
if (isUpToDateMessage(lastMessage)) {
|
|
831
|
-
__privateSet(this, _lastSyncedAt, Date.now());
|
|
832
|
-
__privateSet(this, _isUpToDate, true);
|
|
833
|
-
}
|
|
834
|
-
yield __privateMethod(this, _ShapeStream_instances, publish_fn).call(this, batch);
|
|
835
|
-
}
|
|
836
|
-
(_d = __privateGet(this, _tickPromiseResolver)) == null ? void 0 : _d.call(this);
|
|
837
|
-
}
|
|
752
|
+
yield __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
|
|
838
753
|
} catch (err) {
|
|
839
754
|
__privateSet(this, _error, err);
|
|
840
755
|
if (__privateGet(this, _onError)) {
|
|
@@ -855,10 +770,241 @@ start_fn = function() {
|
|
|
855
770
|
throw err;
|
|
856
771
|
} finally {
|
|
857
772
|
__privateSet(this, _connected, false);
|
|
858
|
-
(
|
|
773
|
+
(_a = __privateGet(this, _tickPromiseRejecter)) == null ? void 0 : _a.call(this);
|
|
774
|
+
}
|
|
775
|
+
});
|
|
776
|
+
};
|
|
777
|
+
requestShape_fn = function() {
|
|
778
|
+
return __async(this, null, function* () {
|
|
779
|
+
var _a, _b;
|
|
780
|
+
if (__privateGet(this, _state) === `pause-requested`) {
|
|
781
|
+
__privateSet(this, _state, `paused`);
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
784
|
+
if (!this.options.subscribe && (((_a = this.options.signal) == null ? void 0 : _a.aborted) || __privateGet(this, _isUpToDate))) {
|
|
785
|
+
return;
|
|
786
|
+
}
|
|
787
|
+
const resumingFromPause = __privateGet(this, _state) === `paused`;
|
|
788
|
+
__privateSet(this, _state, `active`);
|
|
789
|
+
const { url, signal } = this.options;
|
|
790
|
+
const { fetchUrl, requestHeaders } = yield __privateMethod(this, _ShapeStream_instances, constructUrl_fn).call(this, url, resumingFromPause);
|
|
791
|
+
const abortListener = yield __privateMethod(this, _ShapeStream_instances, createAbortListener_fn).call(this, signal);
|
|
792
|
+
const requestAbortController = __privateGet(this, _requestAbortController);
|
|
793
|
+
try {
|
|
794
|
+
yield __privateMethod(this, _ShapeStream_instances, fetchShape_fn).call(this, {
|
|
795
|
+
fetchUrl,
|
|
796
|
+
requestAbortController,
|
|
797
|
+
headers: requestHeaders,
|
|
798
|
+
resumingFromPause: true
|
|
799
|
+
});
|
|
800
|
+
} catch (e) {
|
|
801
|
+
if ((e instanceof FetchError || e instanceof FetchBackoffAbortError) && requestAbortController.signal.aborted && requestAbortController.signal.reason === FORCE_DISCONNECT_AND_REFRESH) {
|
|
802
|
+
return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
|
|
803
|
+
}
|
|
804
|
+
if (e instanceof FetchBackoffAbortError) {
|
|
805
|
+
if (requestAbortController.signal.aborted && requestAbortController.signal.reason === PAUSE_STREAM) {
|
|
806
|
+
__privateSet(this, _state, `paused`);
|
|
807
|
+
}
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
if (!(e instanceof FetchError)) throw e;
|
|
811
|
+
if (e.status == 409) {
|
|
812
|
+
const newShapeHandle = e.headers[SHAPE_HANDLE_HEADER];
|
|
813
|
+
__privateMethod(this, _ShapeStream_instances, reset_fn).call(this, newShapeHandle);
|
|
814
|
+
yield __privateMethod(this, _ShapeStream_instances, publish_fn).call(this, e.json);
|
|
815
|
+
return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
|
|
816
|
+
} else {
|
|
817
|
+
__privateMethod(this, _ShapeStream_instances, sendErrorToSubscribers_fn).call(this, e);
|
|
818
|
+
throw e;
|
|
819
|
+
}
|
|
820
|
+
} finally {
|
|
821
|
+
if (abortListener && signal) {
|
|
822
|
+
signal.removeEventListener(`abort`, abortListener);
|
|
823
|
+
}
|
|
824
|
+
__privateSet(this, _requestAbortController, void 0);
|
|
825
|
+
}
|
|
826
|
+
(_b = __privateGet(this, _tickPromiseResolver)) == null ? void 0 : _b.call(this);
|
|
827
|
+
return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
|
|
828
|
+
});
|
|
829
|
+
};
|
|
830
|
+
constructUrl_fn = function(url, resumingFromPause) {
|
|
831
|
+
return __async(this, null, function* () {
|
|
832
|
+
const [requestHeaders, params] = yield Promise.all([
|
|
833
|
+
resolveHeaders(this.options.headers),
|
|
834
|
+
this.options.params ? toInternalParams(convertWhereParamsToObj(this.options.params)) : void 0
|
|
835
|
+
]);
|
|
836
|
+
if (params) {
|
|
837
|
+
validateParams(params);
|
|
838
|
+
}
|
|
839
|
+
const fetchUrl = new URL(url);
|
|
840
|
+
if (params) {
|
|
841
|
+
if (params.table) setQueryParam(fetchUrl, TABLE_QUERY_PARAM, params.table);
|
|
842
|
+
if (params.where) setQueryParam(fetchUrl, WHERE_QUERY_PARAM, params.where);
|
|
843
|
+
if (params.columns)
|
|
844
|
+
setQueryParam(fetchUrl, COLUMNS_QUERY_PARAM, params.columns);
|
|
845
|
+
if (params.replica) setQueryParam(fetchUrl, REPLICA_PARAM, params.replica);
|
|
846
|
+
if (params.params)
|
|
847
|
+
setQueryParam(fetchUrl, WHERE_PARAMS_PARAM, params.params);
|
|
848
|
+
const customParams = __spreadValues({}, params);
|
|
849
|
+
delete customParams.table;
|
|
850
|
+
delete customParams.where;
|
|
851
|
+
delete customParams.columns;
|
|
852
|
+
delete customParams.replica;
|
|
853
|
+
delete customParams.params;
|
|
854
|
+
for (const [key, value] of Object.entries(customParams)) {
|
|
855
|
+
setQueryParam(fetchUrl, key, value);
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
fetchUrl.searchParams.set(OFFSET_QUERY_PARAM, __privateGet(this, _lastOffset));
|
|
859
|
+
if (__privateGet(this, _isUpToDate)) {
|
|
860
|
+
if (!__privateGet(this, _isRefreshing) && !resumingFromPause) {
|
|
861
|
+
fetchUrl.searchParams.set(LIVE_QUERY_PARAM, `true`);
|
|
862
|
+
}
|
|
863
|
+
fetchUrl.searchParams.set(
|
|
864
|
+
LIVE_CACHE_BUSTER_QUERY_PARAM,
|
|
865
|
+
__privateGet(this, _liveCacheBuster)
|
|
866
|
+
);
|
|
867
|
+
}
|
|
868
|
+
if (__privateGet(this, _shapeHandle)) {
|
|
869
|
+
fetchUrl.searchParams.set(SHAPE_HANDLE_QUERY_PARAM, __privateGet(this, _shapeHandle));
|
|
870
|
+
}
|
|
871
|
+
fetchUrl.searchParams.sort();
|
|
872
|
+
return {
|
|
873
|
+
fetchUrl,
|
|
874
|
+
requestHeaders
|
|
875
|
+
};
|
|
876
|
+
});
|
|
877
|
+
};
|
|
878
|
+
createAbortListener_fn = function(signal) {
|
|
879
|
+
return __async(this, null, function* () {
|
|
880
|
+
var _a;
|
|
881
|
+
__privateSet(this, _requestAbortController, new AbortController());
|
|
882
|
+
if (signal) {
|
|
883
|
+
const abortListener = () => {
|
|
884
|
+
var _a2;
|
|
885
|
+
(_a2 = __privateGet(this, _requestAbortController)) == null ? void 0 : _a2.abort(signal.reason);
|
|
886
|
+
};
|
|
887
|
+
signal.addEventListener(`abort`, abortListener, { once: true });
|
|
888
|
+
if (signal.aborted) {
|
|
889
|
+
(_a = __privateGet(this, _requestAbortController)) == null ? void 0 : _a.abort(signal.reason);
|
|
890
|
+
}
|
|
891
|
+
return abortListener;
|
|
892
|
+
}
|
|
893
|
+
});
|
|
894
|
+
};
|
|
895
|
+
onInitialResponse_fn = function(response) {
|
|
896
|
+
return __async(this, null, function* () {
|
|
897
|
+
var _a;
|
|
898
|
+
const { headers, status } = response;
|
|
899
|
+
const shapeHandle = headers.get(SHAPE_HANDLE_HEADER);
|
|
900
|
+
if (shapeHandle) {
|
|
901
|
+
__privateSet(this, _shapeHandle, shapeHandle);
|
|
902
|
+
}
|
|
903
|
+
const lastOffset = headers.get(CHUNK_LAST_OFFSET_HEADER);
|
|
904
|
+
if (lastOffset) {
|
|
905
|
+
__privateSet(this, _lastOffset, lastOffset);
|
|
906
|
+
}
|
|
907
|
+
const liveCacheBuster = headers.get(LIVE_CACHE_BUSTER_HEADER);
|
|
908
|
+
if (liveCacheBuster) {
|
|
909
|
+
__privateSet(this, _liveCacheBuster, liveCacheBuster);
|
|
910
|
+
}
|
|
911
|
+
const getSchema = () => {
|
|
912
|
+
const schemaHeader = headers.get(SHAPE_SCHEMA_HEADER);
|
|
913
|
+
return schemaHeader ? JSON.parse(schemaHeader) : {};
|
|
914
|
+
};
|
|
915
|
+
__privateSet(this, _schema, (_a = __privateGet(this, _schema)) != null ? _a : getSchema());
|
|
916
|
+
if (status === 204) {
|
|
917
|
+
__privateSet(this, _lastSyncedAt, Date.now());
|
|
918
|
+
}
|
|
919
|
+
});
|
|
920
|
+
};
|
|
921
|
+
onMessages_fn = function(messages, schema, isSseMessage = false) {
|
|
922
|
+
return __async(this, null, function* () {
|
|
923
|
+
const batch = __privateGet(this, _messageParser).parse(messages, schema);
|
|
924
|
+
if (batch.length > 0) {
|
|
925
|
+
const lastMessage = batch[batch.length - 1];
|
|
926
|
+
if (isUpToDateMessage(lastMessage)) {
|
|
927
|
+
if (isSseMessage) {
|
|
928
|
+
const offset = getOffset(lastMessage);
|
|
929
|
+
if (offset) {
|
|
930
|
+
__privateSet(this, _lastOffset, offset);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
__privateSet(this, _lastSyncedAt, Date.now());
|
|
934
|
+
__privateSet(this, _isUpToDate, true);
|
|
935
|
+
}
|
|
936
|
+
yield __privateMethod(this, _ShapeStream_instances, publish_fn).call(this, batch);
|
|
937
|
+
}
|
|
938
|
+
});
|
|
939
|
+
};
|
|
940
|
+
fetchShape_fn = function(opts) {
|
|
941
|
+
return __async(this, null, function* () {
|
|
942
|
+
if (__privateGet(this, _isUpToDate) && this.options.experimentalLiveSse && !__privateGet(this, _isRefreshing) && !opts.resumingFromPause) {
|
|
943
|
+
opts.fetchUrl.searchParams.set(EXPERIMENTAL_LIVE_SSE_QUERY_PARAM, `true`);
|
|
944
|
+
return __privateMethod(this, _ShapeStream_instances, requestShapeSSE_fn).call(this, opts);
|
|
945
|
+
}
|
|
946
|
+
return __privateMethod(this, _ShapeStream_instances, requestShapeLongPoll_fn).call(this, opts);
|
|
947
|
+
});
|
|
948
|
+
};
|
|
949
|
+
requestShapeLongPoll_fn = function(opts) {
|
|
950
|
+
return __async(this, null, function* () {
|
|
951
|
+
const { fetchUrl, requestAbortController, headers } = opts;
|
|
952
|
+
const response = yield __privateGet(this, _fetchClient2).call(this, fetchUrl.toString(), {
|
|
953
|
+
signal: requestAbortController.signal,
|
|
954
|
+
headers
|
|
955
|
+
});
|
|
956
|
+
__privateSet(this, _connected, true);
|
|
957
|
+
yield __privateMethod(this, _ShapeStream_instances, onInitialResponse_fn).call(this, response);
|
|
958
|
+
const schema = __privateGet(this, _schema);
|
|
959
|
+
const res = yield response.text();
|
|
960
|
+
const messages = res || `[]`;
|
|
961
|
+
yield __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, messages, schema);
|
|
962
|
+
});
|
|
963
|
+
};
|
|
964
|
+
requestShapeSSE_fn = function(opts) {
|
|
965
|
+
return __async(this, null, function* () {
|
|
966
|
+
const { fetchUrl, requestAbortController, headers } = opts;
|
|
967
|
+
const fetch2 = __privateGet(this, _sseFetchClient);
|
|
968
|
+
try {
|
|
969
|
+
yield (0, import_fetch_event_source.fetchEventSource)(fetchUrl.toString(), {
|
|
970
|
+
headers,
|
|
971
|
+
fetch: fetch2,
|
|
972
|
+
onopen: (response) => __async(this, null, function* () {
|
|
973
|
+
__privateSet(this, _connected, true);
|
|
974
|
+
yield __privateMethod(this, _ShapeStream_instances, onInitialResponse_fn).call(this, response);
|
|
975
|
+
}),
|
|
976
|
+
onmessage: (event) => {
|
|
977
|
+
if (event.data) {
|
|
978
|
+
const messages = `[${event.data}]`;
|
|
979
|
+
const schema = __privateGet(this, _schema);
|
|
980
|
+
__privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, messages, schema, true);
|
|
981
|
+
}
|
|
982
|
+
},
|
|
983
|
+
onerror: (error) => {
|
|
984
|
+
throw error;
|
|
985
|
+
},
|
|
986
|
+
signal: requestAbortController.signal
|
|
987
|
+
});
|
|
988
|
+
} catch (error) {
|
|
989
|
+
if (requestAbortController.signal.aborted) {
|
|
990
|
+
throw new FetchBackoffAbortError();
|
|
991
|
+
}
|
|
992
|
+
throw error;
|
|
859
993
|
}
|
|
860
994
|
});
|
|
861
995
|
};
|
|
996
|
+
pause_fn = function() {
|
|
997
|
+
var _a;
|
|
998
|
+
if (__privateGet(this, _started) && __privateGet(this, _state) === `active`) {
|
|
999
|
+
__privateSet(this, _state, `pause-requested`);
|
|
1000
|
+
(_a = __privateGet(this, _requestAbortController)) == null ? void 0 : _a.abort(PAUSE_STREAM);
|
|
1001
|
+
}
|
|
1002
|
+
};
|
|
1003
|
+
resume_fn = function() {
|
|
1004
|
+
if (__privateGet(this, _started) && __privateGet(this, _state) === `paused`) {
|
|
1005
|
+
__privateMethod(this, _ShapeStream_instances, start_fn).call(this);
|
|
1006
|
+
}
|
|
1007
|
+
};
|
|
862
1008
|
nextTick_fn = function() {
|
|
863
1009
|
return __async(this, null, function* () {
|
|
864
1010
|
if (__privateGet(this, _tickPromise)) {
|
|
@@ -878,17 +1024,20 @@ nextTick_fn = function() {
|
|
|
878
1024
|
};
|
|
879
1025
|
publish_fn = function(messages) {
|
|
880
1026
|
return __async(this, null, function* () {
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
1027
|
+
__privateSet(this, _messageChain, __privateGet(this, _messageChain).then(
|
|
1028
|
+
() => Promise.all(
|
|
1029
|
+
Array.from(__privateGet(this, _subscribers).values()).map((_0) => __async(this, [_0], function* ([callback, __]) {
|
|
1030
|
+
try {
|
|
1031
|
+
yield callback(messages);
|
|
1032
|
+
} catch (err) {
|
|
1033
|
+
queueMicrotask(() => {
|
|
1034
|
+
throw err;
|
|
1035
|
+
});
|
|
1036
|
+
}
|
|
1037
|
+
}))
|
|
1038
|
+
)
|
|
1039
|
+
));
|
|
1040
|
+
return __privateGet(this, _messageChain);
|
|
892
1041
|
});
|
|
893
1042
|
};
|
|
894
1043
|
sendErrorToSubscribers_fn = function(error) {
|
|
@@ -896,6 +1045,18 @@ sendErrorToSubscribers_fn = function(error) {
|
|
|
896
1045
|
errorFn == null ? void 0 : errorFn(error);
|
|
897
1046
|
});
|
|
898
1047
|
};
|
|
1048
|
+
subscribeToVisibilityChanges_fn = function() {
|
|
1049
|
+
if (typeof document === `object` && typeof document.hidden === `boolean` && typeof document.addEventListener === `function`) {
|
|
1050
|
+
const visibilityHandler = () => {
|
|
1051
|
+
if (document.hidden) {
|
|
1052
|
+
__privateMethod(this, _ShapeStream_instances, pause_fn).call(this);
|
|
1053
|
+
} else {
|
|
1054
|
+
__privateMethod(this, _ShapeStream_instances, resume_fn).call(this);
|
|
1055
|
+
}
|
|
1056
|
+
};
|
|
1057
|
+
document.addEventListener(`visibilitychange`, visibilityHandler);
|
|
1058
|
+
}
|
|
1059
|
+
};
|
|
899
1060
|
/**
|
|
900
1061
|
* Resets the state of the stream, optionally with a provided
|
|
901
1062
|
* shape handle
|