Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions apps/server/src/actions/cluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { GlideClusterClient } from "@valkey/valkey-glide"
import { type Deps, withDeps } from "./utils.ts"
import { setClusterDashboardData } from "../setDashboardData.ts"

export const setClusterData = withDeps<Deps, void>(
async ({ ws, clients, connectionId, action }) => {
const client = clients.get(connectionId)

if (client instanceof GlideClusterClient) {
const { clusterId } = action.payload as unknown as { clusterId: string }
await setClusterDashboardData(clusterId, client, ws)
}
},
)
28 changes: 28 additions & 0 deletions apps/server/src/actions/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { sendValkeyRunCommand } from "../sendCommand.ts"
import { VALKEY } from "../../../../common/src/constants.ts"
import { type Deps, withDeps } from "./utils.ts"

type CommandAction = {
command: string
connectionId: string
}

export const sendRequested = withDeps<Deps, void>(
async ({ ws, clients, connectionId, action }) => {
const client = clients.get(connectionId!)

if (client) {
await sendValkeyRunCommand(client, ws, action.payload as CommandAction)
return
}

ws.send(
JSON.stringify({
type: VALKEY.COMMAND.sendFailed,
payload: {
error: "Invalid connection Id",
},
}),
)
},
)
28 changes: 28 additions & 0 deletions apps/server/src/actions/connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { GlideClusterClient } from "@valkey/valkey-glide"
import { connectToValkey } from "../connection.ts"
import { type Deps, withDeps } from "./utils.ts"
import { setClusterDashboardData } from "../setDashboardData.ts"

type ConnectPayload = {
host: string
port: number
connectionId: string
}

export const connectPending = withDeps<Deps, void>(
async ({ ws, clients, action }) => {
await connectToValkey(ws, action.payload as ConnectPayload, clients)
},
)

export const resetConnection = withDeps<Deps, void>(
async ({ ws, connectionId, clients, action }) => {
const client = clients.get(connectionId)

const { clusterId } = action.payload as unknown as { clusterId: string }

if (client instanceof GlideClusterClient) {
await setClusterDashboardData(clusterId, client, ws)
}
},
)
143 changes: 143 additions & 0 deletions apps/server/src/actions/keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { addKey, deleteKey, getKeyInfoSingle, getKeys, updateKey } from "../keys-browser.ts"
import { VALKEY } from "../../../../common/src/constants.ts"
import { type Deps, withDeps } from "./utils.ts"

type GetKeysPayload = {
connectionId: string;
pattern?: string | undefined;
count?: number | undefined;
}

export const getKeysRequested = withDeps<Deps, void>(
async ({ ws, clients, connectionId, action }) => {
const client = clients.get(connectionId)

if (client) {
await getKeys(client, ws, action.payload as GetKeysPayload)
} else {
ws.send(
JSON.stringify({
type: VALKEY.KEYS.getKeysFailed,
payload: {
connectionId,
error: "Invalid connection Id",
},
}),
)
}
},
)

interface KeyPayload {
connectionId: string;
key: string;
}

export const getKeyTypeRequested = withDeps<Deps, void>(
async ({ ws, clients, connectionId, action }) => {
const { key } = action.payload as unknown as KeyPayload

console.log("Handling getKeyTypeRequested for key:", key)
const client = clients.get(connectionId)

if (client) {
await getKeyInfoSingle(client, ws, action.payload as unknown as KeyPayload)
} else {
console.log("No client found for connectionId:", connectionId)
ws.send(
JSON.stringify({
type: VALKEY.KEYS.getKeyTypeFailed,
payload: {
connectionId,
key,
error: "Invalid connection Id",
},
}),
)
}
},
)

export const deleteKeyRequested = withDeps<Deps, void>(
async ({ ws, clients, connectionId, action }) => {
const { key } = action.payload as unknown as KeyPayload

console.log("Handling deleteKeyRequested for key:", key)
const client = clients.get(connectionId)

if (client) {
await deleteKey(client, ws, action.payload as unknown as KeyPayload)
} else {
console.log("No client found for connectionId:", connectionId)
ws.send(
JSON.stringify({
type: VALKEY.KEYS.deleteKeyFailed,
payload: {
connectionId,
key,
error: "Invalid connection Id",
},
}),
)
}
},
)

interface AddKeyRequestedPayload extends KeyPayload {
keyType: string;
value?: string | undefined;
fields?: {
field: string;
value: string;
}[] | undefined;
values?: string[] | undefined;
ttl?: number | undefined;
}

export const addKeyRequested = withDeps<Deps, void>(
async ({ ws, clients, connectionId, action }) => {
const { key } = action.payload as unknown as KeyPayload

console.log("Handling addKeyRequested for key:", key)
const client = clients.get(connectionId)
if (client) {
await addKey(client, ws, action.payload as unknown as AddKeyRequestedPayload)
} else {
console.log("No client found for connectionId:", connectionId)
ws.send(
JSON.stringify({
type: VALKEY.KEYS.addKeyFailed,
payload: {
connectionId,
key,
error: "Invalid connection Id",
},
}),
)
}
},
)

export const updateKeyRequested = withDeps<Deps, void>(
async ({ ws, clients, connectionId, action }) => {
const { key } = action.payload as unknown as KeyPayload

console.log("Handling updateKeyRequested for key:", key)
const client = clients.get(connectionId)
if (client) {
await updateKey(client, ws, action.payload as unknown as AddKeyRequestedPayload)
} else {
console.log("No client found for connectionId:", connectionId)
ws.send(
JSON.stringify({
type: VALKEY.KEYS.addKeyFailed,
payload: {
connectionId,
key,
error: "Invalid connection Id",
},
}),
)
}
},
)
12 changes: 12 additions & 0 deletions apps/server/src/actions/stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { GlideClient } from "@valkey/valkey-glide"
import { type Deps, withDeps } from "./utils.ts"
import { setDashboardData } from "../setDashboardData.ts"

export const setData = withDeps<Deps, void>(
async ({ ws, clients, connectionId }) => {
const client = clients.get(connectionId)

if (client instanceof GlideClient)
await setDashboardData(connectionId, client, ws)
},
)
37 changes: 37 additions & 0 deletions apps/server/src/actions/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { type GlideClient, type GlideClusterClient } from "@valkey/valkey-glide"
import type WebSocket from "ws"

export type Deps = {
ws: WebSocket
clients: Map<string, GlideClient | GlideClusterClient>
connectionId: string
}

export type ReduxAction = {
type: string
payload: {
connectionId: string
[k: string]: unknown
},
meta: unknown
}

export type WsActionMessage = {
payload: { connectionId: string },
type: string
}

// most actions need ws, clients, connectionId before they can process a redux action
export const withDeps =
<D, R>(fn: (ctx: D & { action: ReduxAction }) => R | Promise<R>) =>
(deps: D) =>
async (action: ReduxAction): Promise<Awaited<R>> => {
return await fn({ ...deps, action })
}

export type Handler = (deps: Deps) => (action: ReduxAction) => Promise<void>

export const unknownHandler: Handler = () =>
async (action: { type: string }) => {
console.log("Unknown action type:", action.type)
}
Loading
Loading