-
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
3 changed files
with
178 additions
and
0 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,127 @@ | ||
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; | ||
} | ||
|
||
}, | ||
|
||
put (id, link, { ttl } = {}) { | ||
|
||
const kv = get_kv(ns); | ||
const key = prefix_urls(id); | ||
|
||
const expirationTtl = ttl?.seconds(); | ||
|
||
const metadata = { | ||
create_date: new Date().toISOString(), | ||
}; | ||
|
||
return kv.put(key, link, { expirationTtl, metadata }).then( | ||
() => true, | ||
() => false, | ||
); | ||
|
||
}, | ||
|
||
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>; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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_pure_js } from '../utils.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 async_local_storage = contextStorage(); | ||
|
||
return create_app(gen_cloudflare_kv(kv_path), { | ||
|
||
auth, | ||
async_local_storage, | ||
|
||
cache_name, | ||
ttl_in_ms, | ||
signing_nav, | ||
signing_site, | ||
|
||
encoding: gen_fnv1a_hash_pure_js({ | ||
key: hash_seed, | ||
large: hash_enlarge, | ||
}), | ||
|
||
}); | ||
|
||
} | ||
|