-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Removed shared enums * Removed getLangCode from shared * Updated kv
- Loading branch information
Showing
16 changed files
with
123 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
import type { Kv } from "."; | ||
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 class CloudflareKv implements Kv { | ||
async set(key: string, value: string, ttl: number) { | ||
const kv = this.getKv_(); | ||
await kv.put(key, value, { | ||
expirationTtl: ttl, | ||
}); | ||
} | ||
|
||
if (!kv) { | ||
throw new ReferenceError( | ||
'No kv found for Cloudflare, make sure you have "kv"' + | ||
" set as binding in wrangler.toml.", | ||
); | ||
} | ||
|
||
export async function set(key: string, value: string, ttl: number) { | ||
await kv.put(key, value, { | ||
expirationTtl: ttl, | ||
}); | ||
} | ||
async get(key: string) { | ||
const kv = this.getKv_(); | ||
return await kv.get(key); | ||
} | ||
|
||
export async function get(key: string) { | ||
return await kv.get(key); | ||
private getKv_() { | ||
// Make sure wrangler.toml has a binding named "kv". | ||
if ("kv" in process.env) { | ||
// @ts-expect-error Proper cast | ||
return process.env.kv as KVNamespace; | ||
} | ||
throw new Error("Cloudflare KV is not found in process.env"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import { CloudflareKv } from "./cloudflare-kv"; | ||
import { RedisKv } from "./redis-kv"; | ||
import { env } from "../../env"; | ||
|
||
interface KeyValue { | ||
export interface Kv { | ||
set(key: string, value: string, ttl: number): Promise<void>; | ||
get(key: string): Promise<string | null>; | ||
} | ||
|
||
export let kv: KeyValue; | ||
export let kv: Kv; | ||
|
||
// Map each KV adapter here to their corresponding import. | ||
// Map each KV adapter here to their corresponding constructor. | ||
if (env.KV === "cloudflare-kv") { | ||
kv = await import("./cloudflare-kv"); | ||
kv = new CloudflareKv(); | ||
} else if (env.KV === "redis") { | ||
kv = await import("./redis"); | ||
kv = new RedisKv(env.REDIS_HOST, env.REDIS_PORT); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { createClient } from "redis"; | ||
import type { Kv } from "."; | ||
|
||
const REDIS_PREFIX = "stitcher"; | ||
|
||
export class RedisKv implements Kv { | ||
private isConnected_ = false; | ||
|
||
private client_: ReturnType<typeof createClient>; | ||
|
||
constructor(host: string, port: number) { | ||
this.client_ = createClient({ | ||
socket: { host, port }, | ||
}); | ||
} | ||
|
||
async set(key: string, value: string, ttl: number) { | ||
await this.init_(); | ||
await this.client_.set(`${REDIS_PREFIX}:${key}`, value, { | ||
EX: ttl, | ||
}); | ||
} | ||
|
||
async get(key: string) { | ||
await this.init_(); | ||
return await this.client_.get(`${REDIS_PREFIX}:${key}`); | ||
} | ||
|
||
private async init_() { | ||
if (this.isConnected_) { | ||
return; | ||
} | ||
await this.client_.connect(); | ||
this.isConnected_ = true; | ||
} | ||
} |
Oops, something went wrong.