UNPKG

@rivetkit/redis

Version:

_Lightweight Libraries for Backends_

638 lines (627 loc) 21.1 kB
import { Redis } from 'ioredis'; import { z } from 'zod'; import { ActorKey, GenericConnGlobalState, AnyActorInstance, RegistryConfig, RunConfig, Registry, AnyClient, ActorRouter, Encoding, UniversalWebSocket } from '@rivetkit/core'; import { ActorDriver, ManagerDriver } from '@rivetkit/core/driver-helpers'; import { Client } from '@rivetkit/core/client'; import { Context } from 'hono'; declare const CoordinateDriverConfig: z.ZodObject<{ actorPeer: z.ZodObject<{ leaseDuration: z.ZodDefault<z.ZodNumber>; renewLeaseGrace: z.ZodDefault<z.ZodNumber>; checkLeaseInterval: z.ZodDefault<z.ZodNumber>; checkLeaseJitter: z.ZodDefault<z.ZodNumber>; messageAckTimeout: z.ZodDefault<z.ZodNumber>; }, "strip", z.ZodTypeAny, { leaseDuration: number; renewLeaseGrace: number; checkLeaseInterval: number; checkLeaseJitter: number; messageAckTimeout: number; }, { leaseDuration?: number | undefined; renewLeaseGrace?: number | undefined; checkLeaseInterval?: number | undefined; checkLeaseJitter?: number | undefined; messageAckTimeout?: number | undefined; }>; }, "strip", z.ZodTypeAny, { actorPeer: { leaseDuration: number; renewLeaseGrace: number; checkLeaseInterval: number; checkLeaseJitter: number; messageAckTimeout: number; }; }, { actorPeer: { leaseDuration?: number | undefined; renewLeaseGrace?: number | undefined; checkLeaseInterval?: number | undefined; checkLeaseJitter?: number | undefined; messageAckTimeout?: number | undefined; }; }>; type CoordinateDriverConfig = z.infer<typeof CoordinateDriverConfig>; declare const RedisDriverConfig: z.ZodObject<{ actorPeer: z.ZodObject<{ leaseDuration: z.ZodDefault<z.ZodNumber>; renewLeaseGrace: z.ZodDefault<z.ZodNumber>; checkLeaseInterval: z.ZodDefault<z.ZodNumber>; checkLeaseJitter: z.ZodDefault<z.ZodNumber>; messageAckTimeout: z.ZodDefault<z.ZodNumber>; }, "strip", z.ZodTypeAny, { leaseDuration: number; renewLeaseGrace: number; checkLeaseInterval: number; checkLeaseJitter: number; messageAckTimeout: number; }, { leaseDuration?: number | undefined; renewLeaseGrace?: number | undefined; checkLeaseInterval?: number | undefined; checkLeaseJitter?: number | undefined; messageAckTimeout?: number | undefined; }>; } & { redis: z.ZodDefault<z.ZodOptional<z.ZodType<Redis, z.ZodTypeDef, Redis>>>; keyPrefix: z.ZodDefault<z.ZodString>; }, "strip", z.ZodTypeAny, { actorPeer: { leaseDuration: number; renewLeaseGrace: number; checkLeaseInterval: number; checkLeaseJitter: number; messageAckTimeout: number; }; redis: Redis; keyPrefix: string; }, { actorPeer: { leaseDuration?: number | undefined; renewLeaseGrace?: number | undefined; checkLeaseInterval?: number | undefined; checkLeaseJitter?: number | undefined; messageAckTimeout?: number | undefined; }; redis?: Redis | undefined; keyPrefix?: string | undefined; }>; type RedisDriverConfig = z.infer<typeof RedisDriverConfig>; declare const NodeMessageSchema: z.ZodObject<{ n: z.ZodOptional<z.ZodString>; m: z.ZodOptional<z.ZodString>; b: z.ZodUnion<[z.ZodObject<{ a: z.ZodObject<{ m: z.ZodString; }, "strip", z.ZodTypeAny, { m: string; }, { m: string; }>; }, "strip", z.ZodTypeAny, { a: { m: string; }; }, { a: { m: string; }; }>, z.ZodObject<{ lf: z.ZodObject<{ ri: z.ZodString; ai: z.ZodString; method: z.ZodString; url: z.ZodString; headers: z.ZodRecord<z.ZodString, z.ZodString>; body: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodEffects<z.ZodAny, Uint8Array<ArrayBufferLike>, any>]>>; ad: z.ZodUnknown; }, "strip", z.ZodTypeAny, { ri: string; ai: string; method: string; url: string; headers: Record<string, string>; body?: string | Uint8Array<ArrayBufferLike> | undefined; ad?: unknown; }, { ri: string; ai: string; method: string; url: string; headers: Record<string, string>; body?: any; ad?: unknown; }>; }, "strip", z.ZodTypeAny, { lf: { ri: string; ai: string; method: string; url: string; headers: Record<string, string>; body?: string | Uint8Array<ArrayBufferLike> | undefined; ad?: unknown; }; }, { lf: { ri: string; ai: string; method: string; url: string; headers: Record<string, string>; body?: any; ad?: unknown; }; }>, z.ZodObject<{ ffr: z.ZodObject<{ ri: z.ZodString; status: z.ZodNumber; headers: z.ZodRecord<z.ZodString, z.ZodString>; body: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodEffects<z.ZodAny, Uint8Array<ArrayBufferLike>, any>]>>; error: z.ZodOptional<z.ZodString>; }, "strip", z.ZodTypeAny, { status: number; ri: string; headers: Record<string, string>; body?: string | Uint8Array<ArrayBufferLike> | undefined; error?: string | undefined; }, { status: number; ri: string; headers: Record<string, string>; body?: any; error?: string | undefined; }>; }, "strip", z.ZodTypeAny, { ffr: { status: number; ri: string; headers: Record<string, string>; body?: string | Uint8Array<ArrayBufferLike> | undefined; error?: string | undefined; }; }, { ffr: { status: number; ri: string; headers: Record<string, string>; body?: any; error?: string | undefined; }; }>, z.ZodObject<{ lwo: z.ZodObject<{ ai: z.ZodString; wi: z.ZodString; url: z.ZodString; e: z.ZodType<"json" | "cbor", z.ZodTypeDef, "json" | "cbor">; cp: z.ZodOptional<z.ZodUnknown>; ad: z.ZodOptional<z.ZodUnknown>; }, "strip", z.ZodTypeAny, { ai: string; url: string; wi: string; e: "json" | "cbor"; ad?: unknown; cp?: unknown; }, { ai: string; url: string; wi: string; e: "json" | "cbor"; ad?: unknown; cp?: unknown; }>; }, "strip", z.ZodTypeAny, { lwo: { ai: string; url: string; wi: string; e: "json" | "cbor"; ad?: unknown; cp?: unknown; }; }, { lwo: { ai: string; url: string; wi: string; e: "json" | "cbor"; ad?: unknown; cp?: unknown; }; }>, z.ZodObject<{ lwm: z.ZodObject<{ wi: z.ZodString; data: z.ZodUnion<[z.ZodString, z.ZodEffects<z.ZodAny, Uint8Array<ArrayBufferLike>, any>]>; binary: z.ZodBoolean; }, "strip", z.ZodTypeAny, { binary: boolean; wi: string; data: string | Uint8Array<ArrayBufferLike>; }, { binary: boolean; wi: string; data?: any; }>; }, "strip", z.ZodTypeAny, { lwm: { binary: boolean; wi: string; data: string | Uint8Array<ArrayBufferLike>; }; }, { lwm: { binary: boolean; wi: string; data?: any; }; }>, z.ZodObject<{ lwc: z.ZodObject<{ wi: z.ZodString; code: z.ZodOptional<z.ZodNumber>; reason: z.ZodOptional<z.ZodString>; }, "strip", z.ZodTypeAny, { wi: string; code?: number | undefined; reason?: string | undefined; }, { wi: string; code?: number | undefined; reason?: string | undefined; }>; }, "strip", z.ZodTypeAny, { lwc: { wi: string; code?: number | undefined; reason?: string | undefined; }; }, { lwc: { wi: string; code?: number | undefined; reason?: string | undefined; }; }>, z.ZodObject<{ fwo: z.ZodObject<{ wi: z.ZodString; }, "strip", z.ZodTypeAny, { wi: string; }, { wi: string; }>; }, "strip", z.ZodTypeAny, { fwo: { wi: string; }; }, { fwo: { wi: string; }; }>, z.ZodObject<{ fwm: z.ZodObject<{ wi: z.ZodString; data: z.ZodUnion<[z.ZodString, z.ZodEffects<z.ZodAny, Uint8Array<ArrayBufferLike>, any>]>; binary: z.ZodBoolean; }, "strip", z.ZodTypeAny, { binary: boolean; wi: string; data: string | Uint8Array<ArrayBufferLike>; }, { binary: boolean; wi: string; data?: any; }>; }, "strip", z.ZodTypeAny, { fwm: { binary: boolean; wi: string; data: string | Uint8Array<ArrayBufferLike>; }; }, { fwm: { binary: boolean; wi: string; data?: any; }; }>, z.ZodObject<{ fwc: z.ZodObject<{ wi: z.ZodString; code: z.ZodOptional<z.ZodNumber>; reason: z.ZodOptional<z.ZodString>; }, "strip", z.ZodTypeAny, { wi: string; code?: number | undefined; reason?: string | undefined; }, { wi: string; code?: number | undefined; reason?: string | undefined; }>; }, "strip", z.ZodTypeAny, { fwc: { wi: string; code?: number | undefined; reason?: string | undefined; }; }, { fwc: { wi: string; code?: number | undefined; reason?: string | undefined; }; }>]>; }, "strip", z.ZodTypeAny, { b: { a: { m: string; }; } | { lf: { ri: string; ai: string; method: string; url: string; headers: Record<string, string>; body?: string | Uint8Array<ArrayBufferLike> | undefined; ad?: unknown; }; } | { ffr: { status: number; ri: string; headers: Record<string, string>; body?: string | Uint8Array<ArrayBufferLike> | undefined; error?: string | undefined; }; } | { lwo: { ai: string; url: string; wi: string; e: "json" | "cbor"; ad?: unknown; cp?: unknown; }; } | { lwm: { binary: boolean; wi: string; data: string | Uint8Array<ArrayBufferLike>; }; } | { lwc: { wi: string; code?: number | undefined; reason?: string | undefined; }; } | { fwo: { wi: string; }; } | { fwm: { binary: boolean; wi: string; data: string | Uint8Array<ArrayBufferLike>; }; } | { fwc: { wi: string; code?: number | undefined; reason?: string | undefined; }; }; m?: string | undefined; n?: string | undefined; }, { b: { a: { m: string; }; } | { lf: { ri: string; ai: string; method: string; url: string; headers: Record<string, string>; body?: any; ad?: unknown; }; } | { ffr: { status: number; ri: string; headers: Record<string, string>; body?: any; error?: string | undefined; }; } | { lwo: { ai: string; url: string; wi: string; e: "json" | "cbor"; ad?: unknown; cp?: unknown; }; } | { lwm: { binary: boolean; wi: string; data?: any; }; } | { lwc: { wi: string; code?: number | undefined; reason?: string | undefined; }; } | { fwo: { wi: string; }; } | { fwm: { binary: boolean; wi: string; data?: any; }; } | { fwc: { wi: string; code?: number | undefined; reason?: string | undefined; }; }; m?: string | undefined; n?: string | undefined; }>; type NodeMessage = z.infer<typeof NodeMessageSchema>; type NodeMessageCallback = (message: NodeMessage) => void; interface GetActorLeaderOutput { /** Undefined if not initialized. */ actor?: { leaderNodeId?: string; }; } interface StartActorAndAcquireLeaseOutput { /** Undefined if not initialized. */ actor?: { name?: string; key?: ActorKey; leaderNodeId?: string; }; } interface ExtendLeaseOutput { leaseValid: boolean; } interface AttemptAcquireLease { newLeaderNodeId: string; } interface CoordinateDriver { createNodeSubscriber(selfNodeId: string, callback: NodeMessageCallback): Promise<void>; publishToNode(targetNodeId: string, message: NodeMessage): Promise<void>; getActorLeader(actorId: string): Promise<GetActorLeaderOutput>; startActorAndAcquireLease(actorId: string, selfNodeId: string, leaseDuration: number): Promise<StartActorAndAcquireLeaseOutput>; extendLease(actorId: string, selfNodeId: string, leaseDuration: number): Promise<ExtendLeaseOutput>; attemptAcquireLease(actorId: string, selfNodeId: string, leaseDuration: number): Promise<AttemptAcquireLease>; releaseLease(actorId: string, nodeId: string): Promise<void>; } declare class ActorPeer { #private; readonly genericConnGlobalState: GenericConnGlobalState; get isLeader(): boolean; get leaderNodeId(): string; get loadedActor(): AnyActorInstance | undefined; get loadedActorStartingPromise(): Promise<void> | undefined; constructor(registryConfig: RegistryConfig, runConfig: RunConfig, driverConfig: CoordinateDriverConfig, CoordinateDriver: CoordinateDriver, actorDriver: ActorDriver, inlineClient: any, // Client type globalState: GlobalState, actorId: string); /** Acquires a `ActorPeer` for a connection and includes the connection ID in the references. */ static acquire(registryConfig: RegistryConfig, runConfig: RunConfig, driverConfig: CoordinateDriverConfig, actorDriver: ActorDriver, inlineClient: any, // Client type CoordinateDriver: CoordinateDriver, globalState: GlobalState, actorId: string, connId: string): Promise<ActorPeer>; static getLeaderActorPeer(globalState: GlobalState, actorId: string): ActorPeer | undefined; static getLeaderActor(globalState: GlobalState, actorId: string): Promise<AnyActorInstance | undefined>; removeConnectionReference(connId: string): Promise<void>; } interface RelayConnDriver { /** Called on disconnect (regardless of source). */ disconnect(reason?: string): Promise<void>; } /** * This is different than `Connection`. `Connection` represents the data of the connection state on the actor itself, `RelayConnection` supports managing a connection for a actor running on another machine over pubsub. */ declare class RelayConn$1 { #private; get actorId(): string; get connId(): string; get connToken(): string; constructor(registryConfig: RegistryConfig, runConfig: RunConfig, driverConfig: CoordinateDriverConfig, actorDriver: ActorDriver, inlineClient: Client<Registry<any>>, coordinateDriver: CoordinateDriver, globalState: GlobalState, driver: RelayConnDriver, actorId: string); start(): Promise<void>; publishMessageToleader(message: NodeMessage, retry: boolean): Promise<void>; /** * Closes the connection and cleans it up. * * @param fromLeader - If this message is coming from the leader. This will prevent sending a close message back to the leader. */ disconnect(fromLeader: boolean, reason: string | undefined, disconnectMessageToleader: NodeMessage | undefined): Promise<void>; } declare class Node { #private; get inlineClient(): AnyClient; get actorDriver(): ActorDriver; constructor(registryConfig: RegistryConfig, runConfig: RunConfig, driverConfig: CoordinateDriverConfig, managerDriver: ManagerDriver, coordinateDriver: CoordinateDriver, globalState: GlobalState, inlineClient: AnyClient, actorDriver: ActorDriver, actorRouter: ActorRouter); get globalState(): GlobalState; get coordinateDriver(): CoordinateDriver; get registryConfig(): RegistryConfig; get runConfig(): RunConfig; get driverConfig(): CoordinateDriverConfig; start(): Promise<void>; sendRequest(actorId: string, actorRequest: Request, abortController?: AbortController): Promise<Response>; openWebSocket(path: string, actorId: string, encoding: Encoding, connParams: unknown): Promise<WebSocket>; proxyRequest(c: Context, actorRequest: Request, actorId: string): Promise<Response>; proxyWebSocket(c: Context, path: string, actorId: string, encoding: Encoding, connParams: unknown, authData: unknown): Promise<Response>; } declare class RelayWebSocketAdapter implements WebSocket { #private; constructor(node: Node, websocketId: string, relayConn: RelayConn$1); get openPromise(): Promise<void>; get readyState(): number; get bufferedAmount(): number; get binaryType(): "blob" | "arraybuffer"; set binaryType(value: "blob" | "arraybuffer"); get extensions(): string; get protocol(): string; get url(): string; get actorId(): string; get onopen(): ((this: WebSocket, ev: Event) => any) | null; set onopen(value: ((this: WebSocket, ev: Event) => any) | null); get onclose(): ((this: WebSocket, ev: any) => any) | null; set onclose(value: ((this: WebSocket, ev: any) => any) | null); get onerror(): ((this: WebSocket, ev: Event) => any) | null; set onerror(value: ((this: WebSocket, ev: Event) => any) | null); get onmessage(): ((this: WebSocket, ev: MessageEvent) => any) | null; set onmessage(value: ((this: WebSocket, ev: MessageEvent) => any) | null); send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void; close(code?: number, reason?: string): void; addEventListener(type: string, listener: any, options?: boolean | any): void; removeEventListener(type: string, listener: any, options?: boolean | any): void; dispatchEvent(event: Event): boolean; _handleMessage(data: string | Uint8Array, isBinary: boolean): void; _handleOpen(): void; _handleClose(code?: number, reason?: string): void; static readonly CONNECTING = 0; static readonly OPEN = 1; static readonly CLOSING = 2; static readonly CLOSED = 3; readonly CONNECTING = 0; readonly OPEN = 1; readonly CLOSING = 2; readonly CLOSED = 3; } type RelayConn = any; interface GlobalState { nodeId: string; /** Actors currently running on this instance. */ actorPeers: Map<string, ActorPeer>; /** Connections that are connected to this node. */ relayConns: Map<string, RelayConn>; /** Resolvers for when a message is acknowledged by the peer. */ messageAckResolvers: Map<string, () => void>; /** Resolvers for when an action response is received. */ actionResponseResolvers: Map<string, (result: { success: boolean; output?: unknown; error?: string; }) => void>; /** Resolvers for when a fetch response is received. */ fetchResponseResolvers: Map<string, (response: any) => void>; /** Raw WebSocket connections mapped by WebSocket ID. */ rawWebSockets: Map<string, UniversalWebSocket>; followerWebSockets: Map<string, { ws: any; relayConn: RelayConn; }>; relayWebSockets: Map<string, RelayWebSocketAdapter>; } export { type GlobalState as G, Node as N, RedisDriverConfig as R };