-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
184 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { getContext } from 'hono/context-storage'; | ||
|
||
import * as v from 'valibot'; | ||
|
||
import type { Adapter, AdapterGen } from './index.ts'; | ||
|
||
|
||
|
||
|
||
|
||
export function gen_cloudflare_kv (ns: string): AdapterGen { | ||
|
||
return () => hook(ns); | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
function hook (ns: string): Adapter { | ||
|
||
return { | ||
|
||
async get (id) { | ||
|
||
const kv = get_kv(ns); | ||
const key = prefix_urls(id); | ||
|
||
const { success, output } = await kv.get(key).then(safe_url); | ||
|
||
if (success === true) { | ||
return output; | ||
} | ||
|
||
}, | ||
|
||
async put (id, link, { ttl } = {}) { | ||
|
||
const kv = get_kv(ns); | ||
const key = prefix_urls(id); | ||
|
||
const exist = await kv.get(key); | ||
|
||
if (exist != null) { | ||
return false; | ||
} | ||
|
||
const expirationTtl = ttl?.seconds(); | ||
|
||
const metadata = { | ||
create_date: new Date().toISOString(), | ||
}; | ||
|
||
await kv.put(key, link, { expirationTtl, metadata }); | ||
|
||
return true; | ||
|
||
}, | ||
|
||
async del (id) { | ||
|
||
const kv = get_kv(ns); | ||
const key = prefix_urls(id); | ||
|
||
await kv.delete(key); | ||
|
||
return true; | ||
|
||
}, | ||
|
||
[Symbol.dispose] () { | ||
// noop | ||
}, | ||
|
||
}; | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
interface KVNamespace { | ||
|
||
get (key: string): Promise<string | undefined>; | ||
|
||
put (key: string, value: string, opts?: { | ||
|
||
expirationTtl?: number, | ||
metadata?: object, | ||
|
||
}): Promise<void>; | ||
|
||
delete (key: string): Promise<void>; | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
function get_kv (ns: string): KVNamespace { | ||
|
||
type Env = { | ||
Bindings: Record<string, KVNamespace>, | ||
}; | ||
|
||
const { [ns]: kv } = getContext<Env>().env; | ||
|
||
v.assert(safe_kv, kv); | ||
|
||
return kv; | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
const safe_kv = v.object({ | ||
|
||
get: v.function(), | ||
put: v.function(), | ||
delete: v.function(), | ||
|
||
}, 'invalid worker kv'); | ||
|
||
const safe_url = v.safeParser(v.pipe(v.string(), v.url())); | ||
|
||
const prefix_urls = (id: string) => 'v1/urls:'.concat(id); | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { MiddlewareHandler } from 'hono/types'; | ||
import { contextStorage } from 'hono/context-storage'; | ||
|
||
import { create_app } from '../app.tsx'; | ||
import { gen_cloudflare_kv } from '../adapter/unstable-cloudflare-kv.ts'; | ||
import { gen_fnv1a_hash } from '../encoder/npm-pure.ts'; | ||
|
||
|
||
|
||
|
||
|
||
export function make ({ | ||
|
||
auth, kv_path, cache_name, ttl_in_ms, hash_seed, hash_enlarge, | ||
signing_nav, signing_site, | ||
|
||
}: { | ||
|
||
auth?: MiddlewareHandler, | ||
kv_path: string, | ||
cache_name?: string, | ||
ttl_in_ms?: number, | ||
hash_seed?: string, | ||
hash_enlarge?: boolean, | ||
signing_nav?: boolean, | ||
signing_site?: string, | ||
|
||
}): Promise<Deno.ServeDefaultExport> { | ||
|
||
const hash = gen_fnv1a_hash({ | ||
key: hash_seed, | ||
large: hash_enlarge, | ||
}); | ||
|
||
const storage = gen_cloudflare_kv(kv_path); | ||
|
||
const init = contextStorage(); | ||
|
||
return create_app(hash, storage, { | ||
init, | ||
auth, | ||
cache_name, | ||
ttl_in_ms, | ||
signing_nav, | ||
signing_site, | ||
}); | ||
|
||
} | ||
|