Skip to content

Commit

Permalink
Restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
matvp91 committed Nov 20, 2024
1 parent 8fe7a41 commit ae9cd8d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 47 deletions.
14 changes: 14 additions & 0 deletions packages/stitcher/src/adapters/kv/cloudflare-kv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { KVNamespace } from "@cloudflare/workers-types";

// Make sure wrangler.toml has a binding named "kv".
const kv = process.env["kv"] as unknown as KVNamespace;

export async function set(key: string, value: string, ttl: number) {
await kv.put(key, value, {
expirationTtl: ttl,
});
}

export async function get(key: string) {
return await kv.get(key);
}
14 changes: 14 additions & 0 deletions packages/stitcher/src/adapters/kv/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { env } from "../../env";

interface KeyValue {
set(key: string, value: string, ttl: number): Promise<void>;
get(key: string): Promise<string | null>;
}

export let kv: KeyValue;

if (env.SERVERLESS) {
kv = await import("./cloudflare-kv");
} else {
kv = await import("./redis");
}
23 changes: 23 additions & 0 deletions packages/stitcher/src/adapters/kv/redis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createClient } from "redis";
import { env } from "../../env";

const REDIS_PREFIX = "stitcher";

const client = createClient({
socket: {
host: env.REDIS_HOST,
port: env.REDIS_PORT,
},
});

await client.connect();

export async function set(key: string, value: string, ttl: number) {
await client.set(`${REDIS_PREFIX}:${key}`, value, {
EX: ttl,
});
}

export async function get(key: string) {
return await client.get(`${REDIS_PREFIX}:${key}`);
}
14 changes: 0 additions & 14 deletions packages/stitcher/src/kv/cloudflare.ts

This file was deleted.

10 changes: 0 additions & 10 deletions packages/stitcher/src/kv/index.ts

This file was deleted.

22 changes: 0 additions & 22 deletions packages/stitcher/src/kv/redis.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/stitcher/src/session.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { randomUUID } from "crypto";
import { DateTime } from "luxon";
import { kv } from "./kv";
import { kv } from "./adapters/kv";
import { JSON } from "./lib/json";
import { resolveUri } from "./lib/url";
import { fetchVmap } from "./vmap";
Expand Down

0 comments on commit ae9cd8d

Please sign in to comment.