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

3 lines (2 loc) 2.84 kB
import{SignJWT as t}from"jose";import{z as e}from"zod";const s=e.object({corporateProxy:e.boolean(),verifiedBot:e.boolean(),jsDetection:e.object({passed:e.boolean()}),staticResource:e.boolean(),detectionIds:e.record(e.any()),score:e.number()});e.object({ACTOR_KIT_SECRET:e.string(),ACTOR_KIT_HOST:e.string()}),e.object({longitude:e.string(),latitude:e.string(),continent:e.string(),country:e.string(),city:e.string(),timezone:e.string(),postalCode:e.string(),region:e.string(),regionCode:e.string(),metroCode:e.string(),botManagement:s});const r=e.object({id:e.string(),type:e.enum(["client","system","service"])});e.object({type:e.string()}),e.discriminatedUnion("type",[e.object({type:e.literal("INITIALIZE"),caller:e.object({type:e.literal("system"),id:e.string()})}),e.object({type:e.literal("CONNECT"),caller:e.object({type:e.literal("system"),id:e.string()}),connectingCaller:r}),e.object({type:e.literal("DISCONNECT"),caller:e.object({type:e.literal("system"),id:e.string()}),disconnectingCaller:r}),e.object({type:e.literal("RESUME"),caller:e.object({type:e.literal("system"),id:e.string()})}),e.object({type:e.literal("MIGRATE"),caller:e.object({type:e.literal("system"),id:e.string()}),operations:e.array(e.any())})]);const n=e.enum(["client","service","system"]),o=e.string().transform(((t,s)=>{if("anonymous"===t)return{type:"client",id:"anonymous"};const r=n.safeParse(t.split("-")[0]);if(!r.success)return r.error.issues.forEach(s.addIssue),e.NEVER;const o=r.data,i=t.substring(t.indexOf("-")+1);return e.string().uuid().safeParse(i).success?{type:o,id:i}:(s.addIssue({code:e.ZodIssueCode.custom,message:`Must be a valid uuid or 'anonymous'. Received '${i}' on value '${t}'.`}),e.NEVER)})),i=async({signingKey:e,actorId:s,actorType:r,callerId:n,callerType:i})=>{const a=`${i}-${n}`;o.parse(a);return await new t({}).setProtectedHeader({alg:"HS256"}).setJti(s).setSubject(a).setAudience(r).setExpirationTime("30d").sign((new TextEncoder).encode(e))},a=e.object({snapshot:e.record(e.any()),checksum:e.string()});function c({actorType:t,host:e}){return async function(s,r){const n=s.input??{};if(!e)throw new Error("Actor Kit host is not defined");const o=function(t,e){return`/api/${t}/${e}`}(t,s.actorId),i=function(t){return function(t){return t.startsWith("localhost")||t.startsWith("127.0.0.1")||t.startsWith("0.0.0.0")}(t)?"http":"https"}(e),c=new URL(`${i}://${e}${o}`);c.searchParams.append("input",JSON.stringify(n)),s.waitFor&&c.searchParams.append("waitFor",s.waitFor);const l=await fetch(c.toString(),{...r,headers:{...r?.headers,Authorization:`Bearer ${s.accessToken}`}});if(!l.ok)throw new Error(`Failed to fetch actor: ${l.statusText}`);const d=await l.json(),{checksum:u,snapshot:p}=a.parse(d);return{snapshot:p,checksum:u}}}export{i as createAccessToken,c as createActorFetch}; //# sourceMappingURL=server.js.map