UNPKG

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

144 lines (143 loc) 3.62 kB
import { z } from "zod"; //#region src/schemas.ts const BotManagementSchema = z.object({ corporateProxy: z.boolean(), verifiedBot: z.boolean(), jsDetection: z.object({ passed: z.boolean() }), staticResource: z.boolean(), detectionIds: z.record(z.any()), score: z.number() }); const EnvironmentSchema = z.object({ ACTOR_KIT_SECRET: z.string(), ACTOR_KIT_HOST: z.string() }); const RequestInfoSchema = z.object({ longitude: z.string(), latitude: z.string(), continent: z.string(), country: z.string(), city: z.string(), timezone: z.string(), postalCode: z.string(), region: z.string(), regionCode: z.string(), metroCode: z.string(), botManagement: BotManagementSchema }); const CallerSchema = z.object({ id: z.string(), type: z.enum([ "client", "system", "service" ]) }); const AnyEventSchema = z.object({ type: z.string() }).passthrough(); const SystemEventSchema = z.discriminatedUnion("type", [ z.object({ type: z.literal("INITIALIZE"), caller: z.object({ type: z.literal("system"), id: z.string() }) }), z.object({ type: z.literal("CONNECT"), caller: z.object({ type: z.literal("system"), id: z.string() }), connectingCaller: CallerSchema }), z.object({ type: z.literal("DISCONNECT"), caller: z.object({ type: z.literal("system"), id: z.string() }), disconnectingCaller: CallerSchema }), z.object({ type: z.literal("RESUME"), caller: z.object({ type: z.literal("system"), id: z.string() }) }), z.object({ type: z.literal("MIGRATE"), caller: z.object({ type: z.literal("system"), id: z.string() }), operations: z.array(z.any()) }) ]); /** * Validates an emitted event from the actor-kit WebSocket protocol. * * The Zod schema validates `op` is a valid JSON Patch operation string, * then transforms the output to `Operation[]` from fast-json-patch. * This is safe because Zod has validated the `op` values match the * discriminated union variants — the transform bridges Zod's flat * object output to fast-json-patch's discriminated union type. */ const EmittedEventSchema = z.object({ operations: z.array(z.object({ op: z.enum([ "add", "remove", "replace", "move", "copy", "test", "_get" ]), path: z.string(), value: z.unknown().optional(), from: z.string().optional() })).transform((ops) => ops), checksum: z.string() }); const CallerIdTypeSchema = z.enum([ "client", "service", "system" ]); const CallerStringSchema = z.string().transform((val, ctx) => { if (val === "anonymous") return { type: "client", id: "anonymous" }; const parts = val.split("-"); if (parts.length < 2) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: `Caller string must be in the format 'type-id' or 'anonymous'. Received '${val}'.` }); return z.NEVER; } const typeStr = parts[0]; const id = parts.slice(1).join("-"); const callerTypeParseResult = CallerIdTypeSchema.safeParse(typeStr); if (!callerTypeParseResult.success) { callerTypeParseResult.error.issues.forEach(ctx.addIssue); return z.NEVER; } const type = callerTypeParseResult.data; if (id.length > 0) return { type, id }; else { ctx.addIssue({ code: z.ZodIssueCode.custom, message: `The ID part cannot be empty after the type prefix. Received '${id}' for value '${val}'.` }); return z.NEVER; } }); //#endregion export { CallerStringSchema as a, RequestInfoSchema as c, CallerSchema as i, SystemEventSchema as l, BotManagementSchema as n, EmittedEventSchema as o, CallerIdTypeSchema as r, EnvironmentSchema as s, AnyEventSchema as t }; //# sourceMappingURL=schemas-DTUwr6Qg.mjs.map