@supacloud/js 0.46.0 → 0.47.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -446,12 +446,13 @@ That table must expose the platform task `id`, `project_ref` and `status`,
446
446
  and have appropriate publication, SELECT privileges and row-level policies.
447
447
  The SDK does not create the table or grant access. Schema/table names must be
448
448
  simple identifiers of at most 63 characters. Explicit Realtime subscriptions
449
- retain Management polling on connection failure and periodic reconciliation.
449
+ retain Management polling on connection failure, retry Realtime while polling,
450
+ and perform periodic reconciliation after reconnecting.
450
451
  An omitted source no longer guesses `public.tasks`; malformed source options
451
452
  are rejected before opening a channel.
452
453
  Polling intervals must be integer milliseconds from 1 through 2,147,483,647.
453
- Realtime connection timeout and reconciliation intervals accept the same
454
- range plus 0 to disable that timer. Invalid explicit values, including null,
454
+ Realtime connection timeout, recovery, and reconciliation intervals accept the
455
+ same range plus 0 to disable that timer. Invalid explicit values, including null,
455
456
  are rejected before authentication or subscription work. The computed default
456
457
  reconciliation interval is capped at the same maximum.
457
458
  Subscription options are captured at creation. Callbacks must be functions,
package/dist/index.cjs CHANGED
@@ -4145,6 +4145,7 @@ function captureTaskSubscribeOptions(options) {
4145
4145
  "realtime",
4146
4146
  "pollingIntervalMs",
4147
4147
  "realtimeTimeoutMs",
4148
+ "realtimeRecoveryIntervalMs",
4148
4149
  "reconcileIntervalMs",
4149
4150
  "onUpdate",
4150
4151
  "onStateChange",
@@ -4487,10 +4488,12 @@ class SupaCloudTasksClient extends SupaCloudManagementClient {
4487
4488
  let channel = null;
4488
4489
  let pollTimer = null;
4489
4490
  let realtimeTimeoutTimer = null;
4491
+ let realtimeRecoveryTimer = null;
4490
4492
  let reconcileTimer = null;
4491
4493
  let mode = "connecting";
4492
4494
  const pollingIntervalMs = taskTimerMs(options.pollingIntervalMs === undefined ? this.options.pollingIntervalMs : options.pollingIntervalMs);
4493
4495
  const realtimeTimeoutMs = taskTimerMs(options.realtimeTimeoutMs === undefined ? 1e4 : options.realtimeTimeoutMs, true);
4496
+ const realtimeRecoveryIntervalMs = taskTimerMs(options.realtimeRecoveryIntervalMs === undefined ? Math.min(MAX_TASK_TIMER_MS, Math.max(30000, pollingIntervalMs * 10)) : options.realtimeRecoveryIntervalMs, true);
4494
4497
  const reconcileIntervalMs = taskTimerMs(options.reconcileIntervalMs === undefined ? Math.min(MAX_TASK_TIMER_MS, Math.max(30000, pollingIntervalMs * 10)) : options.reconcileIntervalMs, true);
4495
4498
  const stopOnTerminal = options.stopOnTerminal ?? true;
4496
4499
  const readController = new AbortController;
@@ -4534,6 +4537,12 @@ class SupaCloudTasksClient extends SupaCloudManagementClient {
4534
4537
  realtimeTimeoutTimer = null;
4535
4538
  }
4536
4539
  };
4540
+ const stopRealtimeRecovery = () => {
4541
+ if (realtimeRecoveryTimer) {
4542
+ clearTimeout(realtimeRecoveryTimer);
4543
+ realtimeRecoveryTimer = null;
4544
+ }
4545
+ };
4537
4546
  const stopReconcile = () => {
4538
4547
  if (reconcileTimer) {
4539
4548
  clearTimeout(reconcileTimer);
@@ -4558,6 +4567,7 @@ class SupaCloudTasksClient extends SupaCloudManagementClient {
4558
4567
  readController.abort();
4559
4568
  stopPolling();
4560
4569
  stopRealtimeTimeout();
4570
+ stopRealtimeRecovery();
4561
4571
  stopReconcile();
4562
4572
  try {
4563
4573
  await teardownRealtime();
@@ -4621,14 +4631,80 @@ class SupaCloudTasksClient extends SupaCloudManagementClient {
4621
4631
  reconcileOnce();
4622
4632
  }, reconcileIntervalMs);
4623
4633
  };
4634
+ const scheduleRealtimeRecovery = () => {
4635
+ if (closed || !realtime2 || mode !== "polling" || realtimeRecoveryIntervalMs <= 0)
4636
+ return;
4637
+ stopRealtimeRecovery();
4638
+ realtimeRecoveryTimer = setTimeout(() => {
4639
+ realtimeRecoveryTimer = null;
4640
+ startRealtime();
4641
+ }, realtimeRecoveryIntervalMs);
4642
+ };
4624
4643
  const startPolling = (error) => {
4625
4644
  if (closed)
4626
4645
  return;
4627
4646
  stopPolling();
4628
4647
  stopRealtimeTimeout();
4629
4648
  stopReconcile();
4649
+ stopRealtimeRecovery();
4630
4650
  setMode("polling", { error });
4631
4651
  pollOnce();
4652
+ scheduleRealtimeRecovery();
4653
+ };
4654
+ const startRealtime = () => {
4655
+ if (closed || !realtime2 || channel)
4656
+ return;
4657
+ stopRealtimeRecovery();
4658
+ channel = this.options.supabase.channel(`supacloud-task:${this.options.projectRef}:${taskId}`).on("postgres_changes", {
4659
+ event: "*",
4660
+ schema: realtime2.schema,
4661
+ table: realtime2.table,
4662
+ filter: `id=eq.${taskId}`
4663
+ }, (payload) => {
4664
+ const next = payload.new && Object.keys(payload.new).length > 0 ? payload.new : payload.old;
4665
+ if (next)
4666
+ emitTask(next);
4667
+ });
4668
+ channel.subscribe(async (status, error) => {
4669
+ if (closed)
4670
+ return;
4671
+ if (status === "SUBSCRIBED") {
4672
+ stopPolling();
4673
+ stopRealtimeTimeout();
4674
+ setMode("realtime");
4675
+ if (closed)
4676
+ return;
4677
+ try {
4678
+ emitTask(await readTask(readController.signal));
4679
+ } catch (err) {
4680
+ if (!closed)
4681
+ reportError(err);
4682
+ }
4683
+ startReconcile();
4684
+ return;
4685
+ }
4686
+ if (status === "TIMED_OUT" || status === "CHANNEL_ERROR" || status === "CLOSED") {
4687
+ await teardownRealtime();
4688
+ if (mode === "polling") {
4689
+ scheduleRealtimeRecovery();
4690
+ } else {
4691
+ startPolling(error);
4692
+ }
4693
+ }
4694
+ });
4695
+ if (!closed && channel && mode !== "realtime" && realtimeTimeoutMs > 0) {
4696
+ realtimeTimeoutTimer = setTimeout(() => {
4697
+ if (!closed && channel) {
4698
+ teardownRealtime().finally(() => {
4699
+ if (mode === "polling") {
4700
+ scheduleRealtimeRecovery();
4701
+ } else {
4702
+ startPolling(new Error("SupaCloud Realtime subscription timed out"));
4703
+ }
4704
+ });
4705
+ }
4706
+ }, realtimeTimeoutMs);
4707
+ }
4632
4708
  };
4633
4709
  const subscription = {
4634
4710
  get connectionState() {
@@ -4642,49 +4718,8 @@ class SupaCloudTasksClient extends SupaCloudManagementClient {
4642
4718
  startPolling();
4643
4719
  return subscription;
4644
4720
  }
4645
- channel = this.options.supabase.channel(`supacloud-task:${this.options.projectRef}:${taskId}`).on("postgres_changes", {
4646
- event: "*",
4647
- schema: realtime2.schema,
4648
- table: realtime2.table,
4649
- filter: `id=eq.${taskId}`
4650
- }, (payload) => {
4651
- const next = payload.new && Object.keys(payload.new).length > 0 ? payload.new : payload.old;
4652
- if (next)
4653
- emitTask(next);
4654
- });
4655
- channel.subscribe(async (status, error) => {
4656
- if (closed)
4657
- return;
4658
- if (status === "SUBSCRIBED") {
4659
- stopPolling();
4660
- stopRealtimeTimeout();
4661
- setMode("realtime");
4662
- if (closed)
4663
- return;
4664
- try {
4665
- emitTask(await readTask(readController.signal));
4666
- } catch (err) {
4667
- if (!closed)
4668
- reportError(err);
4669
- }
4670
- startReconcile();
4671
- return;
4672
- }
4673
- if (status === "TIMED_OUT" || status === "CHANNEL_ERROR" || status === "CLOSED") {
4674
- await teardownRealtime();
4675
- startPolling(error);
4676
- }
4677
- });
4678
- if (!closed && realtimeTimeoutMs > 0) {
4679
- realtimeTimeoutTimer = setTimeout(() => {
4680
- if (!closed && mode === "connecting") {
4681
- teardownRealtime().finally(() => {
4682
- startPolling(new Error("SupaCloud Realtime subscription timed out"));
4683
- });
4684
- }
4685
- }, realtimeTimeoutMs);
4686
- }
4687
4721
  setMode("connecting");
4722
+ startRealtime();
4688
4723
  return subscription;
4689
4724
  }
4690
4725
  }
@@ -4920,14 +4955,14 @@ function createSupaCloudClient(options) {
4920
4955
  invokeBackground: (functionName, submitOptions) => tasks.submit(functionName, submitOptions)
4921
4956
  }
4922
4957
  };
4923
- function withClient(apiClient) {
4958
+ const withClient = (apiClient) => {
4924
4959
  return {
4925
4960
  ...instance,
4926
4961
  procedures: createProcedureClient(apiClient),
4927
4962
  queryAdapter: (adapterOptions) => createQueryAdapter(apiClient, adapterOptions),
4928
4963
  withClient
4929
4964
  };
4930
- }
4965
+ };
4931
4966
  return {
4932
4967
  ...instance,
4933
4968
  procedures: options.apiClient === undefined ? undefined : createProcedureClient(options.apiClient),
package/dist/index.d.cts CHANGED
@@ -6,8 +6,8 @@ import { SupaCloudApiError } from "./api-error.cjs";
6
6
  import { type FetchTransport } from "./bounded-rpc-fetch.cjs";
7
7
  import { SupaCloudOAuthClientsClient } from "./oauth-clients.cjs";
8
8
  import { SupaCloudOAuthServerClient } from "./oauth-server.cjs";
9
- import { type RealtimeChannelConfig, type RealtimeTransport, type SupaCloudRealtimeChannel } from "./realtime.cjs";
10
- import { type ProcedureSource, type ProceduresOf, type QueryAdapterOptions } from "@supacloud/query";
9
+ import { type RealtimeChannelConfig, type RealtimeTransport, type SupaCloudRealtimeChannel, type SupaCloudRealtimeAdapter } from "./realtime.cjs";
10
+ import { createQueryAdapter, type ProcedureSource, type ProceduresOf, type QueryAdapterOptions } from "@supacloud/query";
11
11
  export { createProcedureClient, createQueryAdapter, createQueryKey, invalidateByTags, type QueryAdapter, type QueryAdapterOptions, } from "@supacloud/query";
12
12
  import { type SupaCloudQueueJson, type SupaCloudQueueMessage, type SupaCloudQueueSendResult, type SupaCloudQueueIdempotentSendResult, type SupaCloudQueueMutationResult } from "./queue-rpc.cjs";
13
13
  export { SupaCloudApiError } from "./api-error.cjs";
@@ -205,6 +205,7 @@ export type SupaCloudTaskSubscribeOptions<TResult = unknown> = {
205
205
  };
206
206
  pollingIntervalMs?: number;
207
207
  realtimeTimeoutMs?: number;
208
+ realtimeRecoveryIntervalMs?: number;
208
209
  reconcileIntervalMs?: number;
209
210
  onUpdate: (task: SupaCloudTaskSnapshot<TResult>) => void;
210
211
  onStateChange?: (state: SupaCloudTaskSubscribeState, details?: {
@@ -353,50 +354,31 @@ declare class SupaCloudQueuesClient<TClient extends SupabaseClient = SupabaseCli
353
354
  create(queueName: string, options?: SupaCloudQueueCreateOptions): Promise<SupaCloudQueueInfo>;
354
355
  drop(queueName: string): Promise<void>;
355
356
  }
356
- export declare function createSupaCloudClient<TClient extends SupabaseClient = SupabaseClient, TApiClient extends ProcedureSource | undefined = undefined>(options: SupaCloudClientOptions<TClient> & {
357
- apiClient?: TApiClient;
358
- }): {
359
- supabase: TClient;
360
- projectRef: string;
361
- managementApiUrl: string;
362
- auth: {
363
- oauthServer: SupaCloudOAuthServerClient;
364
- oauthClients: SupaCloudOAuthClientsClient;
357
+ type SupaCloudClientBase<TClient extends SupabaseClient> = {
358
+ readonly supabase: TClient;
359
+ readonly projectRef: string;
360
+ readonly managementApiUrl: string;
361
+ readonly auth: {
362
+ readonly oauthServer: SupaCloudOAuthServerClient;
363
+ readonly oauthClients: SupaCloudOAuthClientsClient;
365
364
  };
366
- tasks: SupaCloudTasksClient<TClient>;
367
- workflows: SupaCloudWorkflowsClient<TClient>;
368
- commands: SupaCloudCommandsClient<TClient>;
369
- artifacts: SupaCloudArtifactsClient<TClient>;
370
- queues: SupaCloudQueuesClient<TClient>;
371
- realtime: import("./realtime.cjs").SupaCloudRealtimeAdapter;
372
- channel: (topic: string, config?: RealtimeChannelConfig) => SupaCloudRealtimeChannel;
373
- queue: (name: string) => SupaCloudQueueClient<TClient>;
374
- functions: {
375
- invokeBackground: (functionName: string, submitOptions?: SupaCloudTaskSubmitOptions) => Promise<SupaCloudTaskReceipt<unknown>>;
376
- };
377
- procedures: TApiClient extends ProcedureSource ? ProceduresOf<TApiClient> : undefined;
378
- queryAdapter: (adapterOptions: QueryAdapterOptions) => import("@supacloud/query").QueryAdapter<TApiClient & {}>;
379
- withClient: <T extends ProcedureSource>(apiClient: T) => {
380
- supabase: TClient;
381
- projectRef: string;
382
- managementApiUrl: string;
383
- auth: {
384
- oauthServer: SupaCloudOAuthServerClient;
385
- oauthClients: SupaCloudOAuthClientsClient;
386
- };
387
- tasks: SupaCloudTasksClient<TClient>;
388
- workflows: SupaCloudWorkflowsClient<TClient>;
389
- commands: SupaCloudCommandsClient<TClient>;
390
- artifacts: SupaCloudArtifactsClient<TClient>;
391
- queues: SupaCloudQueuesClient<TClient>;
392
- realtime: import("./realtime.cjs").SupaCloudRealtimeAdapter;
393
- channel: (topic: string, config?: RealtimeChannelConfig) => SupaCloudRealtimeChannel;
394
- queue: (name: string) => SupaCloudQueueClient<TClient>;
395
- functions: {
396
- invokeBackground: (functionName: string, submitOptions?: SupaCloudTaskSubmitOptions) => Promise<SupaCloudTaskReceipt<unknown>>;
397
- };
398
- procedures: ProceduresOf<T>;
399
- queryAdapter: (adapterOptions: QueryAdapterOptions) => import("@supacloud/query").QueryAdapter<T>;
400
- withClient: /*elided*/ any;
365
+ readonly tasks: SupaCloudTasksClient<TClient>;
366
+ readonly workflows: SupaCloudWorkflowsClient<TClient>;
367
+ readonly commands: SupaCloudCommandsClient<TClient>;
368
+ readonly artifacts: SupaCloudArtifactsClient<TClient>;
369
+ readonly queues: SupaCloudQueuesClient<TClient>;
370
+ readonly realtime: SupaCloudRealtimeAdapter;
371
+ channel(topic: string, config?: RealtimeChannelConfig): SupaCloudRealtimeChannel;
372
+ queue(name: string): SupaCloudQueueClient<TClient>;
373
+ readonly functions: {
374
+ invokeBackground(functionName: string, submitOptions?: SupaCloudTaskSubmitOptions): Promise<SupaCloudTaskReceipt<unknown>>;
401
375
  };
402
376
  };
377
+ type SupaCloudClientWithProcedures<TClient extends SupabaseClient, TProcedures> = SupaCloudClientBase<TClient> & {
378
+ readonly procedures: TProcedures;
379
+ queryAdapter(options: QueryAdapterOptions): ReturnType<typeof createQueryAdapter>;
380
+ withClient<T extends ProcedureSource>(apiClient: T): SupaCloudClientWithProcedures<TClient, ProceduresOf<T>>;
381
+ };
382
+ export declare function createSupaCloudClient<TClient extends SupabaseClient = SupabaseClient, TApiClient extends ProcedureSource | undefined = undefined>(options: SupaCloudClientOptions<TClient> & {
383
+ apiClient?: TApiClient;
384
+ }): SupaCloudClientWithProcedures<TClient, TApiClient extends ProcedureSource ? ProceduresOf<TApiClient> : undefined>;
package/dist/index.d.mts CHANGED
@@ -6,8 +6,8 @@ import { SupaCloudApiError } from "./api-error.mjs";
6
6
  import { type FetchTransport } from "./bounded-rpc-fetch.mjs";
7
7
  import { SupaCloudOAuthClientsClient } from "./oauth-clients.mjs";
8
8
  import { SupaCloudOAuthServerClient } from "./oauth-server.mjs";
9
- import { type RealtimeChannelConfig, type RealtimeTransport, type SupaCloudRealtimeChannel } from "./realtime.mjs";
10
- import { type ProcedureSource, type ProceduresOf, type QueryAdapterOptions } from "@supacloud/query";
9
+ import { type RealtimeChannelConfig, type RealtimeTransport, type SupaCloudRealtimeChannel, type SupaCloudRealtimeAdapter } from "./realtime.mjs";
10
+ import { createQueryAdapter, type ProcedureSource, type ProceduresOf, type QueryAdapterOptions } from "@supacloud/query";
11
11
  export { createProcedureClient, createQueryAdapter, createQueryKey, invalidateByTags, type QueryAdapter, type QueryAdapterOptions, } from "@supacloud/query";
12
12
  import { type SupaCloudQueueJson, type SupaCloudQueueMessage, type SupaCloudQueueSendResult, type SupaCloudQueueIdempotentSendResult, type SupaCloudQueueMutationResult } from "./queue-rpc.mjs";
13
13
  export { SupaCloudApiError } from "./api-error.mjs";
@@ -205,6 +205,7 @@ export type SupaCloudTaskSubscribeOptions<TResult = unknown> = {
205
205
  };
206
206
  pollingIntervalMs?: number;
207
207
  realtimeTimeoutMs?: number;
208
+ realtimeRecoveryIntervalMs?: number;
208
209
  reconcileIntervalMs?: number;
209
210
  onUpdate: (task: SupaCloudTaskSnapshot<TResult>) => void;
210
211
  onStateChange?: (state: SupaCloudTaskSubscribeState, details?: {
@@ -353,50 +354,31 @@ declare class SupaCloudQueuesClient<TClient extends SupabaseClient = SupabaseCli
353
354
  create(queueName: string, options?: SupaCloudQueueCreateOptions): Promise<SupaCloudQueueInfo>;
354
355
  drop(queueName: string): Promise<void>;
355
356
  }
356
- export declare function createSupaCloudClient<TClient extends SupabaseClient = SupabaseClient, TApiClient extends ProcedureSource | undefined = undefined>(options: SupaCloudClientOptions<TClient> & {
357
- apiClient?: TApiClient;
358
- }): {
359
- supabase: TClient;
360
- projectRef: string;
361
- managementApiUrl: string;
362
- auth: {
363
- oauthServer: SupaCloudOAuthServerClient;
364
- oauthClients: SupaCloudOAuthClientsClient;
357
+ type SupaCloudClientBase<TClient extends SupabaseClient> = {
358
+ readonly supabase: TClient;
359
+ readonly projectRef: string;
360
+ readonly managementApiUrl: string;
361
+ readonly auth: {
362
+ readonly oauthServer: SupaCloudOAuthServerClient;
363
+ readonly oauthClients: SupaCloudOAuthClientsClient;
365
364
  };
366
- tasks: SupaCloudTasksClient<TClient>;
367
- workflows: SupaCloudWorkflowsClient<TClient>;
368
- commands: SupaCloudCommandsClient<TClient>;
369
- artifacts: SupaCloudArtifactsClient<TClient>;
370
- queues: SupaCloudQueuesClient<TClient>;
371
- realtime: import("./realtime.mjs").SupaCloudRealtimeAdapter;
372
- channel: (topic: string, config?: RealtimeChannelConfig) => SupaCloudRealtimeChannel;
373
- queue: (name: string) => SupaCloudQueueClient<TClient>;
374
- functions: {
375
- invokeBackground: (functionName: string, submitOptions?: SupaCloudTaskSubmitOptions) => Promise<SupaCloudTaskReceipt<unknown>>;
376
- };
377
- procedures: TApiClient extends ProcedureSource ? ProceduresOf<TApiClient> : undefined;
378
- queryAdapter: (adapterOptions: QueryAdapterOptions) => import("@supacloud/query").QueryAdapter<TApiClient & {}>;
379
- withClient: <T extends ProcedureSource>(apiClient: T) => {
380
- supabase: TClient;
381
- projectRef: string;
382
- managementApiUrl: string;
383
- auth: {
384
- oauthServer: SupaCloudOAuthServerClient;
385
- oauthClients: SupaCloudOAuthClientsClient;
386
- };
387
- tasks: SupaCloudTasksClient<TClient>;
388
- workflows: SupaCloudWorkflowsClient<TClient>;
389
- commands: SupaCloudCommandsClient<TClient>;
390
- artifacts: SupaCloudArtifactsClient<TClient>;
391
- queues: SupaCloudQueuesClient<TClient>;
392
- realtime: import("./realtime.mjs").SupaCloudRealtimeAdapter;
393
- channel: (topic: string, config?: RealtimeChannelConfig) => SupaCloudRealtimeChannel;
394
- queue: (name: string) => SupaCloudQueueClient<TClient>;
395
- functions: {
396
- invokeBackground: (functionName: string, submitOptions?: SupaCloudTaskSubmitOptions) => Promise<SupaCloudTaskReceipt<unknown>>;
397
- };
398
- procedures: ProceduresOf<T>;
399
- queryAdapter: (adapterOptions: QueryAdapterOptions) => import("@supacloud/query").QueryAdapter<T>;
400
- withClient: /*elided*/ any;
365
+ readonly tasks: SupaCloudTasksClient<TClient>;
366
+ readonly workflows: SupaCloudWorkflowsClient<TClient>;
367
+ readonly commands: SupaCloudCommandsClient<TClient>;
368
+ readonly artifacts: SupaCloudArtifactsClient<TClient>;
369
+ readonly queues: SupaCloudQueuesClient<TClient>;
370
+ readonly realtime: SupaCloudRealtimeAdapter;
371
+ channel(topic: string, config?: RealtimeChannelConfig): SupaCloudRealtimeChannel;
372
+ queue(name: string): SupaCloudQueueClient<TClient>;
373
+ readonly functions: {
374
+ invokeBackground(functionName: string, submitOptions?: SupaCloudTaskSubmitOptions): Promise<SupaCloudTaskReceipt<unknown>>;
401
375
  };
402
376
  };
377
+ type SupaCloudClientWithProcedures<TClient extends SupabaseClient, TProcedures> = SupaCloudClientBase<TClient> & {
378
+ readonly procedures: TProcedures;
379
+ queryAdapter(options: QueryAdapterOptions): ReturnType<typeof createQueryAdapter>;
380
+ withClient<T extends ProcedureSource>(apiClient: T): SupaCloudClientWithProcedures<TClient, ProceduresOf<T>>;
381
+ };
382
+ export declare function createSupaCloudClient<TClient extends SupabaseClient = SupabaseClient, TApiClient extends ProcedureSource | undefined = undefined>(options: SupaCloudClientOptions<TClient> & {
383
+ apiClient?: TApiClient;
384
+ }): SupaCloudClientWithProcedures<TClient, TApiClient extends ProcedureSource ? ProceduresOf<TApiClient> : undefined>;
package/dist/index.mjs CHANGED
@@ -4049,6 +4049,7 @@ function captureTaskSubscribeOptions(options) {
4049
4049
  "realtime",
4050
4050
  "pollingIntervalMs",
4051
4051
  "realtimeTimeoutMs",
4052
+ "realtimeRecoveryIntervalMs",
4052
4053
  "reconcileIntervalMs",
4053
4054
  "onUpdate",
4054
4055
  "onStateChange",
@@ -4391,10 +4392,12 @@ class SupaCloudTasksClient extends SupaCloudManagementClient {
4391
4392
  let channel = null;
4392
4393
  let pollTimer = null;
4393
4394
  let realtimeTimeoutTimer = null;
4395
+ let realtimeRecoveryTimer = null;
4394
4396
  let reconcileTimer = null;
4395
4397
  let mode = "connecting";
4396
4398
  const pollingIntervalMs = taskTimerMs(options.pollingIntervalMs === undefined ? this.options.pollingIntervalMs : options.pollingIntervalMs);
4397
4399
  const realtimeTimeoutMs = taskTimerMs(options.realtimeTimeoutMs === undefined ? 1e4 : options.realtimeTimeoutMs, true);
4400
+ const realtimeRecoveryIntervalMs = taskTimerMs(options.realtimeRecoveryIntervalMs === undefined ? Math.min(MAX_TASK_TIMER_MS, Math.max(30000, pollingIntervalMs * 10)) : options.realtimeRecoveryIntervalMs, true);
4398
4401
  const reconcileIntervalMs = taskTimerMs(options.reconcileIntervalMs === undefined ? Math.min(MAX_TASK_TIMER_MS, Math.max(30000, pollingIntervalMs * 10)) : options.reconcileIntervalMs, true);
4399
4402
  const stopOnTerminal = options.stopOnTerminal ?? true;
4400
4403
  const readController = new AbortController;
@@ -4438,6 +4441,12 @@ class SupaCloudTasksClient extends SupaCloudManagementClient {
4438
4441
  realtimeTimeoutTimer = null;
4439
4442
  }
4440
4443
  };
4444
+ const stopRealtimeRecovery = () => {
4445
+ if (realtimeRecoveryTimer) {
4446
+ clearTimeout(realtimeRecoveryTimer);
4447
+ realtimeRecoveryTimer = null;
4448
+ }
4449
+ };
4441
4450
  const stopReconcile = () => {
4442
4451
  if (reconcileTimer) {
4443
4452
  clearTimeout(reconcileTimer);
@@ -4462,6 +4471,7 @@ class SupaCloudTasksClient extends SupaCloudManagementClient {
4462
4471
  readController.abort();
4463
4472
  stopPolling();
4464
4473
  stopRealtimeTimeout();
4474
+ stopRealtimeRecovery();
4465
4475
  stopReconcile();
4466
4476
  try {
4467
4477
  await teardownRealtime();
@@ -4525,14 +4535,80 @@ class SupaCloudTasksClient extends SupaCloudManagementClient {
4525
4535
  reconcileOnce();
4526
4536
  }, reconcileIntervalMs);
4527
4537
  };
4538
+ const scheduleRealtimeRecovery = () => {
4539
+ if (closed || !realtime2 || mode !== "polling" || realtimeRecoveryIntervalMs <= 0)
4540
+ return;
4541
+ stopRealtimeRecovery();
4542
+ realtimeRecoveryTimer = setTimeout(() => {
4543
+ realtimeRecoveryTimer = null;
4544
+ startRealtime();
4545
+ }, realtimeRecoveryIntervalMs);
4546
+ };
4528
4547
  const startPolling = (error) => {
4529
4548
  if (closed)
4530
4549
  return;
4531
4550
  stopPolling();
4532
4551
  stopRealtimeTimeout();
4533
4552
  stopReconcile();
4553
+ stopRealtimeRecovery();
4534
4554
  setMode("polling", { error });
4535
4555
  pollOnce();
4556
+ scheduleRealtimeRecovery();
4557
+ };
4558
+ const startRealtime = () => {
4559
+ if (closed || !realtime2 || channel)
4560
+ return;
4561
+ stopRealtimeRecovery();
4562
+ channel = this.options.supabase.channel(`supacloud-task:${this.options.projectRef}:${taskId}`).on("postgres_changes", {
4563
+ event: "*",
4564
+ schema: realtime2.schema,
4565
+ table: realtime2.table,
4566
+ filter: `id=eq.${taskId}`
4567
+ }, (payload) => {
4568
+ const next = payload.new && Object.keys(payload.new).length > 0 ? payload.new : payload.old;
4569
+ if (next)
4570
+ emitTask(next);
4571
+ });
4572
+ channel.subscribe(async (status, error) => {
4573
+ if (closed)
4574
+ return;
4575
+ if (status === "SUBSCRIBED") {
4576
+ stopPolling();
4577
+ stopRealtimeTimeout();
4578
+ setMode("realtime");
4579
+ if (closed)
4580
+ return;
4581
+ try {
4582
+ emitTask(await readTask(readController.signal));
4583
+ } catch (err) {
4584
+ if (!closed)
4585
+ reportError(err);
4586
+ }
4587
+ startReconcile();
4588
+ return;
4589
+ }
4590
+ if (status === "TIMED_OUT" || status === "CHANNEL_ERROR" || status === "CLOSED") {
4591
+ await teardownRealtime();
4592
+ if (mode === "polling") {
4593
+ scheduleRealtimeRecovery();
4594
+ } else {
4595
+ startPolling(error);
4596
+ }
4597
+ }
4598
+ });
4599
+ if (!closed && channel && mode !== "realtime" && realtimeTimeoutMs > 0) {
4600
+ realtimeTimeoutTimer = setTimeout(() => {
4601
+ if (!closed && channel) {
4602
+ teardownRealtime().finally(() => {
4603
+ if (mode === "polling") {
4604
+ scheduleRealtimeRecovery();
4605
+ } else {
4606
+ startPolling(new Error("SupaCloud Realtime subscription timed out"));
4607
+ }
4608
+ });
4609
+ }
4610
+ }, realtimeTimeoutMs);
4611
+ }
4536
4612
  };
4537
4613
  const subscription = {
4538
4614
  get connectionState() {
@@ -4546,49 +4622,8 @@ class SupaCloudTasksClient extends SupaCloudManagementClient {
4546
4622
  startPolling();
4547
4623
  return subscription;
4548
4624
  }
4549
- channel = this.options.supabase.channel(`supacloud-task:${this.options.projectRef}:${taskId}`).on("postgres_changes", {
4550
- event: "*",
4551
- schema: realtime2.schema,
4552
- table: realtime2.table,
4553
- filter: `id=eq.${taskId}`
4554
- }, (payload) => {
4555
- const next = payload.new && Object.keys(payload.new).length > 0 ? payload.new : payload.old;
4556
- if (next)
4557
- emitTask(next);
4558
- });
4559
- channel.subscribe(async (status, error) => {
4560
- if (closed)
4561
- return;
4562
- if (status === "SUBSCRIBED") {
4563
- stopPolling();
4564
- stopRealtimeTimeout();
4565
- setMode("realtime");
4566
- if (closed)
4567
- return;
4568
- try {
4569
- emitTask(await readTask(readController.signal));
4570
- } catch (err) {
4571
- if (!closed)
4572
- reportError(err);
4573
- }
4574
- startReconcile();
4575
- return;
4576
- }
4577
- if (status === "TIMED_OUT" || status === "CHANNEL_ERROR" || status === "CLOSED") {
4578
- await teardownRealtime();
4579
- startPolling(error);
4580
- }
4581
- });
4582
- if (!closed && realtimeTimeoutMs > 0) {
4583
- realtimeTimeoutTimer = setTimeout(() => {
4584
- if (!closed && mode === "connecting") {
4585
- teardownRealtime().finally(() => {
4586
- startPolling(new Error("SupaCloud Realtime subscription timed out"));
4587
- });
4588
- }
4589
- }, realtimeTimeoutMs);
4590
- }
4591
4625
  setMode("connecting");
4626
+ startRealtime();
4592
4627
  return subscription;
4593
4628
  }
4594
4629
  }
@@ -4824,14 +4859,14 @@ function createSupaCloudClient(options) {
4824
4859
  invokeBackground: (functionName, submitOptions) => tasks.submit(functionName, submitOptions)
4825
4860
  }
4826
4861
  };
4827
- function withClient(apiClient) {
4862
+ const withClient = (apiClient) => {
4828
4863
  return {
4829
4864
  ...instance,
4830
4865
  procedures: createProcedureClient(apiClient),
4831
4866
  queryAdapter: (adapterOptions) => createQueryAdapter(apiClient, adapterOptions),
4832
4867
  withClient
4833
4868
  };
4834
- }
4869
+ };
4835
4870
  return {
4836
4871
  ...instance,
4837
4872
  procedures: options.apiClient === undefined ? undefined : createProcedureClient(options.apiClient),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supacloud/js",
3
- "version": "0.46.0",
3
+ "version": "0.47.1",
4
4
  "description": "Platform SDK for SupaCloud, built on top of supabase-js",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -116,14 +116,14 @@
116
116
  }
117
117
  },
118
118
  "dependencies": {
119
- "@supacloud/contracts": "0.15.2",
120
- "@supacloud/query": "0.5.3",
119
+ "@supacloud/contracts": "0.16.0",
120
+ "@supacloud/query": "0.6.0",
121
121
  "rxjs": "7.8.2"
122
122
  },
123
123
  "devDependencies": {
124
124
  "@types/bun": "^1.4.4",
125
125
  "@supabase/supabase-js": "^2.118.0",
126
- "typescript": "^7.0.2",
126
+ "typescript": "7.1.0-beta",
127
127
  "@tanstack/query-core": "5.105.0",
128
128
  "@tanstack/svelte-query": "6.3.6",
129
129
  "typebox": "1.4.3",