@electric-sql/client 1.5.4 → 1.5.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.
@@ -426,10 +426,10 @@ function snakeCamelMapper(schema) {
426
426
 
427
427
  // src/helpers.ts
428
428
  function isChangeMessage(message) {
429
- return `key` in message;
429
+ return message != null && `key` in message;
430
430
  }
431
431
  function isControlMessage(message) {
432
- return !isChangeMessage(message);
432
+ return message != null && !isChangeMessage(message);
433
433
  }
434
434
  function isUpToDateMessage(message) {
435
435
  return isControlMessage(message) && message.headers.control === `up-to-date`;
@@ -2284,7 +2284,8 @@ requestShape_fn = async function() {
2284
2284
  }
2285
2285
  const newShapeHandle = e.headers[SHAPE_HANDLE_HEADER] || `${__privateGet(this, _syncState).handle}-next`;
2286
2286
  __privateMethod(this, _ShapeStream_instances, reset_fn).call(this, newShapeHandle);
2287
- await __privateMethod(this, _ShapeStream_instances, publish_fn).call(this, Array.isArray(e.json) ? e.json : [e.json]);
2287
+ const messages409 = Array.isArray(e.json) ? e.json : e.json != null ? [e.json] : [];
2288
+ await __privateMethod(this, _ShapeStream_instances, publish_fn).call(this, messages409);
2288
2289
  return __privateMethod(this, _ShapeStream_instances, requestShape_fn).call(this);
2289
2290
  } else {
2290
2291
  throw e;
@@ -2467,6 +2468,12 @@ onInitialResponse_fn = async function(response) {
2467
2468
  return true;
2468
2469
  };
2469
2470
  onMessages_fn = async function(batch, isSseMessage = false) {
2471
+ if (!Array.isArray(batch)) {
2472
+ console.warn(
2473
+ `[Electric] #onMessages called with non-array argument (${typeof batch}). This is a client bug \u2014 please report it.`
2474
+ );
2475
+ return;
2476
+ }
2470
2477
  if (batch.length === 0) return;
2471
2478
  const lastMessage = batch[batch.length - 1];
2472
2479
  const hasUpToDateMessage = isUpToDateMessage(lastMessage);
@@ -2523,6 +2530,7 @@ fetchShape_fn = async function(opts) {
2523
2530
  return __privateMethod(this, _ShapeStream_instances, requestShapeLongPoll_fn).call(this, opts);
2524
2531
  };
2525
2532
  requestShapeLongPoll_fn = async function(opts) {
2533
+ var _a;
2526
2534
  const { fetchUrl, requestAbortController, headers } = opts;
2527
2535
  const response = await __privateGet(this, _fetchClient2).call(this, fetchUrl.toString(), {
2528
2536
  signal: requestAbortController.signal,
@@ -2535,6 +2543,16 @@ requestShapeLongPoll_fn = async function(opts) {
2535
2543
  const res = await response.text();
2536
2544
  const messages = res || `[]`;
2537
2545
  const batch = __privateGet(this, _messageParser).parse(messages, schema);
2546
+ if (!Array.isArray(batch)) {
2547
+ const preview = (_a = JSON.stringify(batch)) == null ? void 0 : _a.slice(0, 200);
2548
+ throw new FetchError(
2549
+ response.status,
2550
+ `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}`,
2551
+ void 0,
2552
+ Object.fromEntries(response.headers.entries()),
2553
+ fetchUrl.toString()
2554
+ );
2555
+ }
2538
2556
  await __privateMethod(this, _ShapeStream_instances, onMessages_fn).call(this, batch);
2539
2557
  };
2540
2558
  requestShapeSSE_fn = async function(opts) {