Skip to content

Commit

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

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
50 changes: 50 additions & 0 deletions deploy/unstable-cloudflare.ts
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,
}),

});

}

0 comments on commit 4bc5381

Please sign in to comment.