@electric-sql/client 1.5.6 → 1.5.7

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.
@@ -476,6 +476,12 @@ function getOffset(message) {
476
476
  const lsn = message.headers.global_last_seen_lsn;
477
477
  return lsn ? `${lsn}_0` : void 0;
478
478
  }
479
+ function bigintReplacer(_key, value) {
480
+ return typeof value === `bigint` ? value.toString() : value;
481
+ }
482
+ function bigintSafeStringify(value) {
483
+ return JSON.stringify(value, bigintReplacer);
484
+ }
479
485
  function isVisibleInSnapshot(txid, snapshot) {
480
486
  const xid = BigInt(txid);
481
487
  const xmin = BigInt(snapshot.xmin);
@@ -1741,6 +1747,13 @@ var PausedState = class _PausedState extends ShapeStreamState {
1741
1747
  get replayCursor() {
1742
1748
  return this.previousState.replayCursor;
1743
1749
  }
1750
+ handleResponseMetadata(input) {
1751
+ const transition = this.previousState.handleResponseMetadata(input);
1752
+ if (transition.action === `accepted`) {
1753
+ return { action: `accepted`, state: new _PausedState(transition.state) };
1754
+ }
1755
+ return transition;
1756
+ }
1744
1757
  withHandle(handle) {
1745
1758
  return new _PausedState(this.previousState.withHandle(handle));
1746
1759
  }
@@ -2131,7 +2144,7 @@ var ShapeStream = class {
2131
2144
  );
2132
2145
  }, 3e4);
2133
2146
  try {
2134
- const { metadata, data } = await this.fetchSnapshot(opts);
2147
+ const { metadata, data, responseOffset, responseHandle } = await this.fetchSnapshot(opts);
2135
2148
  const dataWithEndBoundary = data.concat([
2136
2149
  { headers: __spreadValues({ control: `snapshot-end` }, metadata) },
2137
2150
  { headers: __spreadValues({ control: `subset-end` }, opts) }
@@ -2141,6 +2154,25 @@ var ShapeStream = class {
2141
2154
  new Set(data.map((message) => message.key))
2142
2155
  );
2143
2156
  __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, dataWithEndBoundary, false);
2157
+ if (responseOffset !== null || responseHandle !== null) {
2158
+ const transition = __privateGet(this, _syncState).handleResponseMetadata({
2159
+ status: 200,
2160
+ responseHandle,
2161
+ responseOffset,
2162
+ responseCursor: null,
2163
+ expiredHandle: null,
2164
+ now: Date.now(),
2165
+ maxStaleCacheRetries: __privateGet(this, _maxStaleCacheRetries),
2166
+ createCacheBuster: () => `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`
2167
+ });
2168
+ if (transition.action === `accepted`) {
2169
+ __privateSet(this, _syncState, transition.state);
2170
+ } else {
2171
+ console.warn(
2172
+ `[Electric] Snapshot response metadata was not accepted by state "${__privateGet(this, _syncState).kind}" (action: ${transition.action}). Stream offset was not advanced from snapshot.`
2173
+ );
2174
+ }
2175
+ }
2144
2176
  return {
2145
2177
  metadata,
2146
2178
  data
@@ -2159,7 +2191,7 @@ var ShapeStream = class {
2159
2191
  * `subsetMethod: 'POST'` on the stream to send parameters in the request body instead.
2160
2192
  *
2161
2193
  * @param opts - The options for the snapshot request.
2162
- * @returns The metadata and the data for the snapshot.
2194
+ * @returns The metadata, data, and the response's offset/handle for state advancement.
2163
2195
  */
2164
2196
  async fetchSnapshot(opts) {
2165
2197
  var _a, _b, _c;
@@ -2175,7 +2207,7 @@ var ShapeStream = class {
2175
2207
  headers: __spreadProps(__spreadValues({}, result.requestHeaders), {
2176
2208
  "Content-Type": `application/json`
2177
2209
  }),
2178
- body: JSON.stringify(__privateMethod(this, _ShapeStream_instances, buildSubsetBody_fn).call(this, opts))
2210
+ body: bigintSafeStringify(__privateMethod(this, _ShapeStream_instances, buildSubsetBody_fn).call(this, opts))
2179
2211
  };
2180
2212
  } else {
2181
2213
  const result = await __privateMethod(this, _ShapeStream_instances, constructUrl_fn).call(this, this.options.url, true, opts);
@@ -2210,7 +2242,9 @@ var ShapeStream = class {
2210
2242
  rawData,
2211
2243
  schema
2212
2244
  );
2213
- return { metadata, data };
2245
+ const responseOffset = response.headers.get(CHUNK_LAST_OFFSET_HEADER) || null;
2246
+ const responseHandle = response.headers.get(SHAPE_HANDLE_HEADER);
2247
+ return { metadata, data, responseOffset, responseHandle };
2214
2248
  }
2215
2249
  };
2216
2250
  _error = new WeakMap();
@@ -2423,7 +2457,7 @@ constructUrl_fn = async function(url, resumingFromPause, subsetParams) {
2423
2457
  if (subsetParams.params)
2424
2458
  fetchUrl.searchParams.set(
2425
2459
  SUBSET_PARAM_WHERE_PARAMS,
2426
- JSON.stringify(subsetParams.params)
2460
+ bigintSafeStringify(subsetParams.params)
2427
2461
  );
2428
2462
  if (subsetParams.limit)
2429
2463
  setQueryParam(fetchUrl, SUBSET_PARAM_LIMIT, subsetParams.limit);
@@ -2602,7 +2636,7 @@ requestShapeLongPoll_fn = async function(opts) {
2602
2636
  const messages = res || `[]`;
2603
2637
  const batch = __privateGet(this, _messageParser).parse(messages, schema);
2604
2638
  if (!Array.isArray(batch)) {
2605
- const preview = (_a = JSON.stringify(batch)) == null ? void 0 : _a.slice(0, 200);
2639
+ const preview = (_a = bigintSafeStringify(batch)) == null ? void 0 : _a.slice(0, 200);
2606
2640
  throw new FetchError(
2607
2641
  response.status,
2608
2642
  `Received non-array response body from shape endpoint. This may indicate a proxy or CDN is returning an unexpected response. Expected a JSON array, got ${typeof batch}: ${preview}`,
@@ -2966,7 +3000,7 @@ var Shape = class {
2966
3000
  * Returns void; data will be emitted via the stream and processed by this Shape.
2967
3001
  */
2968
3002
  async requestSnapshot(params) {
2969
- const key = JSON.stringify(params);
3003
+ const key = bigintSafeStringify(params);
2970
3004
  __privateGet(this, _requestedSubSnapshots).add(key);
2971
3005
  await __privateMethod(this, _Shape_instances, awaitUpToDate_fn).call(this);
2972
3006
  await this.stream.requestSnapshot(params);