actor-kit
Version:
Actor Kit is a library for running state machines in Cloudflare Workers, leveraging XState for robust state management. It provides a framework for managing the logic, lifecycle, persistence, synchronization, and access control of actors in a distributed
522 lines (521 loc) • 15.9 kB
text/typescript
import { z } from "zod";
import { Operation } from "fast-json-patch";
import { AnyEventObject, AnyStateMachine, SnapshotFrom, StateMachine, StateValueFrom } from "xstate";
import { DurableObject } from "cloudflare:workers";
//#region src/schemas.d.ts
declare const BotManagementSchema: z.ZodObject<{
corporateProxy: z.ZodBoolean;
verifiedBot: z.ZodBoolean;
jsDetection: z.ZodObject<{
passed: z.ZodBoolean;
}, "strip", z.ZodTypeAny, {
passed: boolean;
}, {
passed: boolean;
}>;
staticResource: z.ZodBoolean;
detectionIds: z.ZodRecord<z.ZodString, z.ZodAny>;
score: z.ZodNumber;
}, "strip", z.ZodTypeAny, {
corporateProxy: boolean;
verifiedBot: boolean;
jsDetection: {
passed: boolean;
};
staticResource: boolean;
detectionIds: Record<string, any>;
score: number;
}, {
corporateProxy: boolean;
verifiedBot: boolean;
jsDetection: {
passed: boolean;
};
staticResource: boolean;
detectionIds: Record<string, any>;
score: number;
}>;
declare const EnvironmentSchema: z.ZodObject<{
ACTOR_KIT_SECRET: z.ZodString;
ACTOR_KIT_HOST: z.ZodString;
}, "strip", z.ZodTypeAny, {
ACTOR_KIT_SECRET: string;
ACTOR_KIT_HOST: string;
}, {
ACTOR_KIT_SECRET: string;
ACTOR_KIT_HOST: string;
}>;
declare const RequestInfoSchema: z.ZodObject<{
longitude: z.ZodString;
latitude: z.ZodString;
continent: z.ZodString;
country: z.ZodString;
city: z.ZodString;
timezone: z.ZodString;
postalCode: z.ZodString;
region: z.ZodString;
regionCode: z.ZodString;
metroCode: z.ZodString;
botManagement: z.ZodObject<{
corporateProxy: z.ZodBoolean;
verifiedBot: z.ZodBoolean;
jsDetection: z.ZodObject<{
passed: z.ZodBoolean;
}, "strip", z.ZodTypeAny, {
passed: boolean;
}, {
passed: boolean;
}>;
staticResource: z.ZodBoolean;
detectionIds: z.ZodRecord<z.ZodString, z.ZodAny>;
score: z.ZodNumber;
}, "strip", z.ZodTypeAny, {
corporateProxy: boolean;
verifiedBot: boolean;
jsDetection: {
passed: boolean;
};
staticResource: boolean;
detectionIds: Record<string, any>;
score: number;
}, {
corporateProxy: boolean;
verifiedBot: boolean;
jsDetection: {
passed: boolean;
};
staticResource: boolean;
detectionIds: Record<string, any>;
score: number;
}>;
}, "strip", z.ZodTypeAny, {
longitude: string;
latitude: string;
continent: string;
country: string;
city: string;
timezone: string;
postalCode: string;
region: string;
regionCode: string;
metroCode: string;
botManagement: {
corporateProxy: boolean;
verifiedBot: boolean;
jsDetection: {
passed: boolean;
};
staticResource: boolean;
detectionIds: Record<string, any>;
score: number;
};
}, {
longitude: string;
latitude: string;
continent: string;
country: string;
city: string;
timezone: string;
postalCode: string;
region: string;
regionCode: string;
metroCode: string;
botManagement: {
corporateProxy: boolean;
verifiedBot: boolean;
jsDetection: {
passed: boolean;
};
staticResource: boolean;
detectionIds: Record<string, any>;
score: number;
};
}>;
declare const CallerSchema: z.ZodObject<{
id: z.ZodString;
type: z.ZodEnum<["client", "system", "service"]>;
}, "strip", z.ZodTypeAny, {
type: "client" | "system" | "service";
id: string;
}, {
type: "client" | "system" | "service";
id: string;
}>;
declare const AnyEventSchema: z.ZodObject<{
type: z.ZodString;
}, "passthrough", z.ZodTypeAny, z.objectOutputType<{
type: z.ZodString;
}, z.ZodTypeAny, "passthrough">, z.objectInputType<{
type: z.ZodString;
}, z.ZodTypeAny, "passthrough">>;
declare const SystemEventSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
type: z.ZodLiteral<"INITIALIZE">;
caller: z.ZodObject<{
type: z.ZodLiteral<"system">;
id: z.ZodString;
}, "strip", z.ZodTypeAny, {
type: "system";
id: string;
}, {
type: "system";
id: string;
}>;
}, "strip", z.ZodTypeAny, {
type: "INITIALIZE";
caller: {
type: "system";
id: string;
};
}, {
type: "INITIALIZE";
caller: {
type: "system";
id: string;
};
}>, z.ZodObject<{
type: z.ZodLiteral<"CONNECT">;
caller: z.ZodObject<{
type: z.ZodLiteral<"system">;
id: z.ZodString;
}, "strip", z.ZodTypeAny, {
type: "system";
id: string;
}, {
type: "system";
id: string;
}>;
connectingCaller: z.ZodObject<{
id: z.ZodString;
type: z.ZodEnum<["client", "system", "service"]>;
}, "strip", z.ZodTypeAny, {
type: "client" | "system" | "service";
id: string;
}, {
type: "client" | "system" | "service";
id: string;
}>;
}, "strip", z.ZodTypeAny, {
type: "CONNECT";
caller: {
type: "system";
id: string;
};
connectingCaller: {
type: "client" | "system" | "service";
id: string;
};
}, {
type: "CONNECT";
caller: {
type: "system";
id: string;
};
connectingCaller: {
type: "client" | "system" | "service";
id: string;
};
}>, z.ZodObject<{
type: z.ZodLiteral<"DISCONNECT">;
caller: z.ZodObject<{
type: z.ZodLiteral<"system">;
id: z.ZodString;
}, "strip", z.ZodTypeAny, {
type: "system";
id: string;
}, {
type: "system";
id: string;
}>;
disconnectingCaller: z.ZodObject<{
id: z.ZodString;
type: z.ZodEnum<["client", "system", "service"]>;
}, "strip", z.ZodTypeAny, {
type: "client" | "system" | "service";
id: string;
}, {
type: "client" | "system" | "service";
id: string;
}>;
}, "strip", z.ZodTypeAny, {
type: "DISCONNECT";
caller: {
type: "system";
id: string;
};
disconnectingCaller: {
type: "client" | "system" | "service";
id: string;
};
}, {
type: "DISCONNECT";
caller: {
type: "system";
id: string;
};
disconnectingCaller: {
type: "client" | "system" | "service";
id: string;
};
}>, z.ZodObject<{
type: z.ZodLiteral<"RESUME">;
caller: z.ZodObject<{
type: z.ZodLiteral<"system">;
id: z.ZodString;
}, "strip", z.ZodTypeAny, {
type: "system";
id: string;
}, {
type: "system";
id: string;
}>;
}, "strip", z.ZodTypeAny, {
type: "RESUME";
caller: {
type: "system";
id: string;
};
}, {
type: "RESUME";
caller: {
type: "system";
id: string;
};
}>, z.ZodObject<{
type: z.ZodLiteral<"MIGRATE">;
caller: z.ZodObject<{
type: z.ZodLiteral<"system">;
id: z.ZodString;
}, "strip", z.ZodTypeAny, {
type: "system";
id: string;
}, {
type: "system";
id: string;
}>;
operations: z.ZodArray<z.ZodAny, "many">;
}, "strip", z.ZodTypeAny, {
type: "MIGRATE";
caller: {
type: "system";
id: string;
};
operations: any[];
}, {
type: "MIGRATE";
caller: {
type: "system";
id: string;
};
operations: any[];
}>]>;
declare const EmittedEventSchema: z.ZodObject<{
operations: z.ZodEffects<z.ZodArray<z.ZodObject<{
op: z.ZodEnum<["add", "remove", "replace", "move", "copy", "test", "_get"]>;
path: z.ZodString;
value: z.ZodOptional<z.ZodUnknown>;
from: z.ZodOptional<z.ZodString>;
}, "strip", z.ZodTypeAny, {
path: string;
op: "add" | "remove" | "replace" | "move" | "copy" | "test" | "_get";
value?: unknown;
from?: string | undefined;
}, {
path: string;
op: "add" | "remove" | "replace" | "move" | "copy" | "test" | "_get";
value?: unknown;
from?: string | undefined;
}>, "many">, Operation[], {
path: string;
op: "add" | "remove" | "replace" | "move" | "copy" | "test" | "_get";
value?: unknown;
from?: string | undefined;
}[]>;
checksum: z.ZodString;
}, "strip", z.ZodTypeAny, {
operations: Operation[];
checksum: string;
}, {
operations: {
path: string;
op: "add" | "remove" | "replace" | "move" | "copy" | "test" | "_get";
value?: unknown;
from?: string | undefined;
}[];
checksum: string;
}>;
declare const CallerIdTypeSchema: z.ZodEnum<["client", "service", "system"]>;
declare const CallerStringSchema: z.ZodEffects<z.ZodString, {
type: "client" | "system" | "service";
id: string;
}, string>;
//#endregion
//#region src/types.d.ts
type ActorKitStorage = DurableObjectStorage;
interface ActorKitEnv {
ACTOR_KIT_SECRET: string;
[key: string]: DurableObjectNamespace<AnyActorServer> | unknown;
}
type EnvWithDurableObjects = ActorKitEnv;
type AnyEvent = z.infer<typeof AnyEventSchema>;
interface ActorServerMethods<TMachine extends AnyActorKitStateMachine> {
fetch(request: Request): Promise<Response>;
spawn(props: {
actorType: string;
actorId: string;
caller: Caller;
input: Record<string, unknown>;
}): void;
send(event: ClientEventFrom<TMachine> | ServiceEventFrom<TMachine>): void;
getSnapshot(caller: Caller, options?: {
waitForEvent?: ClientEventFrom<TMachine>;
waitForState?: StateValueFrom<TMachine>;
timeout?: number;
errorOnWaitTimeout?: boolean;
}): Promise<{
checksum: string;
snapshot: CallerSnapshotFrom<TMachine>;
}>;
}
type ActorServer<TMachine extends AnyActorKitStateMachine> = DurableObject & ActorServerMethods<TMachine>;
type AnyActorServer = ActorServer<AnyActorKitStateMachine>;
type Caller = z.infer<typeof CallerSchema>;
type RequestInfo = z.infer<typeof RequestInfoSchema>;
type ActorKitInputProps = {
id: string;
caller: Caller;
storage: ActorKitStorage;
[key: string]: unknown;
};
type CallerType = "client" | "system" | "service";
type EventObject = {
type: string;
};
type EventSchemaUnion = z.ZodDiscriminatedUnion<"type", [z.ZodObject<z.ZodRawShape & {
type: z.ZodString;
}>, ...z.ZodObject<z.ZodRawShape & {
type: z.ZodString;
}>[]]>;
type EventSchemas = {
client: EventSchemaUnion;
service: EventSchemaUnion;
};
type BaseActorKitContext<TPublicProps extends {
[key: string]: unknown;
}, TPrivateProps extends {
[key: string]: unknown;
}> = {
public: TPublicProps;
private: Record<string, TPrivateProps>;
};
type ActorKitStateMachine<TEvent extends BaseActorKitEvent<ActorKitEnv>, TInput extends {
id: string;
caller: Caller;
storage: ActorKitStorage;
}, TContext extends BaseActorKitContext<any, any> & {
[key: string]: unknown;
}> = StateMachine<TContext, TEvent & EventObject, any, any, any, any, any, any, any, TInput, any, any, any, any>;
type BaseActorKitInput<TEnv extends ActorKitEnv = ActorKitEnv> = {
id: string;
caller: Caller;
env: TEnv;
storage: ActorKitStorage;
};
type WithActorKitInput<TInputProps extends {
[key: string]: unknown;
}, TEnv extends ActorKitEnv = ActorKitEnv> = TInputProps & BaseActorKitInput<TEnv>;
type AnyActorKitStateMachine = ActorKitStateMachine<any, any, any>;
type AnyActorKitEvent = (WithActorKitEvent<AnyEventObject, "client"> | WithActorKitEvent<AnyEventObject, "service"> | ActorKitSystemEvent) & BaseActorKitEvent<ActorKitEnv>;
type AnyActorKitInput = WithActorKitInput<{
[key: string]: unknown;
}, ActorKitEnv> & {
storage: ActorKitStorage;
};
type AnyActorKitContext = {
public: {
[key: string]: unknown;
};
private: Record<string, {
[key: string]: unknown;
}>;
};
type BaseActorKitStateMachine = ActorKitStateMachine<AnyActorKitEvent, AnyActorKitInput, AnyActorKitContext>;
type MachineServerOptions = {
persisted?: boolean;
};
type ExtraContext = {
requestId: string;
};
interface BaseActorKitEvent<TEnv extends ActorKitEnv = ActorKitEnv> {
caller: Caller;
storage: ActorKitStorage;
requestInfo?: RequestInfo;
env: TEnv;
}
type ActorKitSystemEvent = z.infer<typeof SystemEventSchema>;
type WithActorKitEvent<T extends {
type: string;
}, C extends CallerType> = T & BaseActorKitEvent<ActorKitEnv> & {
caller: {
type: C;
};
};
type WithActorKitContext<TExtraProps extends {
[key: string]: unknown;
}, TPrivateProps extends {
[key: string]: unknown;
}, TPublicProps extends {
[key: string]: unknown;
}> = TExtraProps & {
public: TPublicProps;
private: Record<string, TPrivateProps>;
};
type CallerSnapshotFrom<TMachine extends AnyStateMachine> = {
public: SnapshotFrom<TMachine> extends {
context: {
public: infer P;
};
} ? P : unknown;
private: SnapshotFrom<TMachine> extends {
context: {
private: Partial<Record<string, infer PR>>;
};
} ? PR : unknown;
value: SnapshotFrom<TMachine> extends {
value: infer V;
} ? V : unknown;
};
type ClientEventFrom<T extends AnyActorKitStateMachine> = T extends StateMachine<any, infer TEvent, any, any, any, any, any, any, any, any, any, any, any, any> ? TEvent extends WithActorKitEvent<infer E, "client"> ? Omit<E, keyof BaseActorKitEvent<ActorKitEnv>> : never : never;
type ServiceEventFrom<T extends AnyActorKitStateMachine> = T extends StateMachine<any, infer TEvent, any, any, any, any, any, any, any, any, any, any, any, any> ? TEvent extends WithActorKitEvent<infer E, "service"> ? Omit<E, keyof BaseActorKitEvent<ActorKitEnv>> : never : never;
type ScreamingSnakeToKebab<S extends string> = S extends `${infer T}_${infer U}` ? `${Lowercase<T>}-${ScreamingSnakeToKebab<U>}` : Lowercase<S>;
type DurableObjectActor<TMachine extends AnyActorKitStateMachine> = ActorServer<TMachine>;
type CamelToSnakeCase<S extends string> = S extends `${infer T}${infer U}` ? U extends Uncapitalize<U> ? `${Lowercase<T>}${CamelToSnakeCase<U>}` : `${Lowercase<T>}_${CamelToSnakeCase<U>}` : S;
type KebabToCamelCase<S extends string> = S extends `${infer T}-${infer U}` ? `${T}${Capitalize<KebabToCamelCase<U>>}` : S;
type KebabToScreamingSnake<S extends string> = Uppercase<CamelToSnakeCase<KebabToCamelCase<S>>>;
interface MatchesProps<TMachine extends AnyActorKitStateMachine> {
state: StateValueFrom<TMachine>;
and?: StateValueFrom<TMachine>;
or?: StateValueFrom<TMachine>;
not?: boolean;
initialValueOverride?: boolean;
}
type MachineFromServer<T> = T extends ActorServer<infer M> ? M : never;
type ActorKitEmittedEvent = {
operations: Operation[];
checksum: string;
};
type ActorKitClient<TMachine extends AnyActorKitStateMachine> = {
connect: () => Promise<void>;
disconnect: () => void;
send: (event: ClientEventFrom<TMachine>) => void;
getState: () => CallerSnapshotFrom<TMachine>;
subscribe: (listener: (state: CallerSnapshotFrom<TMachine>) => void) => () => void;
waitFor: (predicateFn: (state: CallerSnapshotFrom<TMachine>) => boolean, timeoutMs?: number) => Promise<void>;
};
type ExtractEventType<TMachine> = TMachine extends ActorKitStateMachine<infer TEvent, any, any> ? TEvent : never;
type ExtractEnvType<TEvent> = TEvent extends {
env: infer TEnv;
} ? TEnv : never;
type EnvFromMachine<TMachine extends AnyActorKitStateMachine> = ExtractEnvType<ExtractEventType<TMachine>> extends never ? ActorKitEnv : ExtractEnvType<ExtractEventType<TMachine>> & ActorKitEnv;
//#endregion
export { RequestInfo as A, CallerStringSchema as B, EnvWithDurableObjects as C, MachineFromServer as D, KebabToScreamingSnake as E, WithActorKitInput as F, EnvironmentSchema as H, AnyEventSchema as I, BotManagementSchema as L, ServiceEventFrom as M, WithActorKitContext as N, MachineServerOptions as O, WithActorKitEvent as P, CallerIdTypeSchema as R, EnvFromMachine as S, ExtraContext as T, RequestInfoSchema as U, EmittedEventSchema as V, SystemEventSchema as W, Caller as _, ActorKitStateMachine as a, ClientEventFrom as b, ActorServer as c, AnyActorServer as d, AnyEvent as f, BaseActorKitStateMachine as g, BaseActorKitInput as h, ActorKitInputProps as i, ScreamingSnakeToKebab as j, MatchesProps as k, ActorServerMethods as l, BaseActorKitEvent as m, ActorKitEmittedEvent as n, ActorKitStorage as o, BaseActorKitContext as p, ActorKitEnv as r, ActorKitSystemEvent as s, ActorKitClient as t, AnyActorKitStateMachine as u, CallerSnapshotFrom as v, EventSchemas as w, DurableObjectActor as x, CallerType as y, CallerSchema as z };
//# sourceMappingURL=types-C31KX9CW.d.mts.map