Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
imcotton committed Nov 21, 2024
1 parent b4b3900 commit 2ded6b7
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 2 deletions.
132 changes: 132 additions & 0 deletions adapter/unstable-cloudflare-kv.ts
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);

4 changes: 2 additions & 2 deletions app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export async function create_app <E extends Env> (

{

init = noop,
auth = noop,
async_local_storage = noop,
cache_name = 'assets-v1',
ttl_in_ms = nothing<number>(),
signing_nav = false,
Expand All @@ -49,7 +49,7 @@ export async function create_app <E extends Env> (

return extend(new Hono<E>().get('/static/*', cached(cache_name)))

.use(async_local_storage)
.use(init)

.use(layout)

Expand Down
1 change: 1 addition & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dev": "deno serve --port 3000 -N=esm.sh:443 -R=./db -W=./db --watch dev.ts"
},
"exports": {
"./deploy/unstable-cloudflare": "./deploy/unstable-cloudflare.ts",
"./deploy/deno": "./deploy/deno.ts",
"./helper": "./helper.ts",
"./app": "./app.tsx",
Expand Down
49 changes: 49 additions & 0 deletions deploy/unstable-cloudflare.ts
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,
});

}

0 comments on commit 2ded6b7

Please sign in to comment.