@pylonsync/functions 0.11.6 → 0.13.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.
package/src/types.ts CHANGED
@@ -827,6 +827,81 @@ export interface Files {
827
827
  signedUrl(fileId: string, opts?: { ttlSecs?: number }): Promise<string>;
828
828
  }
829
829
 
830
+ /**
831
+ * Shard tickets: short-lived, signed permission to join one realtime shard,
832
+ * with claims the shard's authorization hooks read without a database
833
+ * call. Check what the user may do here, then mint:
834
+ *
835
+ * ```ts
836
+ * export default mutation({
837
+ * args: { characterId: v.id("Character") },
838
+ * async handler(ctx, args) {
839
+ * const c = await ctx.db.get("Character", args.characterId);
840
+ * if (c?.ownerId !== ctx.auth.userId) throw ctx.error("FORBIDDEN", "not your character");
841
+ * const shard = `zone-${c.zone}`;
842
+ * const ticket = await ctx.shards.ticket(shard, {
843
+ * subscriberId: c.id,
844
+ * claims: { character: c.id, realm: c.realm },
845
+ * });
846
+ * return { shard, ticket };
847
+ * },
848
+ * });
849
+ * ```
850
+ *
851
+ * The client passes the ticket when it connects (`connectShard(shard, {
852
+ * ticket })`). The shard rejects a ticket for another shard, another
853
+ * subscriber id, or past its expiry.
854
+ */
855
+ export interface Shards {
856
+ /**
857
+ * Mint a ticket for `shardId`. `subscriberId` defaults to the calling
858
+ * user's id; `ttlSecs` defaults to 60 and is capped at 3600 by the host.
859
+ */
860
+ ticket(
861
+ shardId: string,
862
+ opts?: { subscriberId?: string; claims?: Record<string, unknown>; ttlSecs?: number },
863
+ ): Promise<string>;
864
+
865
+ /**
866
+ * Start shard `shardId` of a kind declared with `shard({...})` in app.ts.
867
+ * `params` reach the module's `init`. Actions only: a mutation's
868
+ * rollback cannot undo it.
869
+ *
870
+ * Throws `SHARD_EXISTS` when the id is running, `SHARD_LIMIT_REACHED`
871
+ * at the kind's `maxInstances`, `SHARD_KIND_NOT_FOUND`,
872
+ * `SHARD_ID_INVALID`, or `SHARD_INIT_FAILED` when `init` refuses.
873
+ */
874
+ create(kind: string, shardId: string, params?: unknown): Promise<ShardInfo>;
875
+
876
+ /** Stop a shard and close its subscribers' connections. Resolves to
877
+ * `false` when no shard has that id. Actions only. */
878
+ stop(shardId: string): Promise<boolean>;
879
+
880
+ /** A running shard, or `null`. */
881
+ get(shardId: string): Promise<ShardInfo | null>;
882
+
883
+ /** Every running shard. */
884
+ list(): Promise<ShardInfo[]>;
885
+ }
886
+
887
+ /** `ctx.shards` in a query or mutation: tickets and reads, no start or stop. */
888
+ export type ShardsReader = Pick<Shards, "ticket" | "get" | "list">;
889
+
890
+ /** A running shard, from `ctx.shards.create`, `get`, or `list`. */
891
+ export interface ShardInfo {
892
+ id: string;
893
+ /** The shard kind's name. */
894
+ kind: string;
895
+ /** Ticks run so far. */
896
+ tick: number;
897
+ subscribers: number;
898
+ /** False once the shard stopped (finished, idle, or failed) and before
899
+ * the host removes it. */
900
+ running: boolean;
901
+ /** Why the module stopped, when it trapped. */
902
+ error?: string;
903
+ }
904
+
830
905
  /** Context for query handlers (read-only).
831
906
  *
832
907
  * NOTE: `ctx.llm` is NOT exposed here. Queries are reactive: a
@@ -852,6 +927,8 @@ export interface QueryCtx<R extends AuthRequirement = "optional"> {
852
927
  requireMember: RequireMember;
853
928
  /** Signed file-download URLs — see {@link Files}. */
854
929
  files: Files;
930
+ /** Shard tickets and reads — see {@link Shards}. */
931
+ shards: ShardsReader;
855
932
  /**
856
933
  * Fires when the host cancels this call (idle timeout exceeded).
857
934
  * Thread it into `fetch(url, { signal: ctx.signal })` or SDK calls so
@@ -880,6 +957,8 @@ export interface MutationCtx<R extends AuthRequirement = "optional"> {
880
957
  workflows: Workflows;
881
958
  /** Signed file-download URLs — see {@link Files}. */
882
959
  files: Files;
960
+ /** Shard tickets and reads — see {@link Shards}. */
961
+ shards: ShardsReader;
883
962
  /** Create a typed error that triggers rollback. */
884
963
  error(code: string, message: string): Error;
885
964
  /** Assert org membership (optionally a role) — see {@link RequireMember}. */
@@ -1053,6 +1132,8 @@ export interface ActionCtx<R extends AuthRequirement = "optional"> {
1053
1132
  env: Record<string, string>;
1054
1133
  /** Signed file-download URLs — see {@link Files}. */
1055
1134
  files: Files;
1135
+ /** Shard tickets, reads, and start/stop — see {@link Shards}. */
1136
+ shards: Shards;
1056
1137
  /** Run a registered query within its own read transaction. */
1057
1138
  runQuery<T = unknown>(
1058
1139
  fnName: string,