Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refine encoder #24

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import { bodyLimit } from 'hono/body-limit';
import { validator } from 'hono/validator';
import { HTTPException } from 'hono/http-exception';

import type { Encoder } from './encoder/index.ts';
import type { AdapterGen } from './adapter/index.ts';

import { layout, Create, SigningPane, Go } from './components/index.tsx';
import { Show, scriptSrc } from './components/show.tsx';

import { collection } from './assets.ts';

import { noop, csp, cached, register, gen_fnv1a_hash,
import { noop, csp, cached, register,
parser, v_create, v_id_slugify, v_optional_signing_back, read_var,
UUIDv4, UUIDv5_URL, compose_signing_url, calc_fingerprint,
nmap, nothing, slugify, exception, challenge_, duration,
Expand All @@ -25,10 +26,14 @@ import { noop, csp, cached, register, gen_fnv1a_hash,



export async function create_app <E extends Env> (storage: AdapterGen, {
export async function create_app <E extends Env> (

hash: Encoder,
storage: AdapterGen,

{

auth = noop,
encoding = gen_fnv1a_hash(),
cache_name = 'assets-v1',
ttl_in_ms = nothing<number>(),
signing_nav = false,
Expand All @@ -38,7 +43,6 @@ export async function create_app <E extends Env> (storage: AdapterGen, {
} = {}) {

const db = await storage();
const hash = await encoding();

const extend = register(collection);

Expand Down
16 changes: 8 additions & 8 deletions deploy/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { MiddlewareHandler } from 'hono/types';

import { create_app } from '../app.tsx';
import { gen_deno_kv } from '../adapter/deno-kv.ts';
import { gen_fnv1a_hash } from '../utils.ts';
import { gen_fnv1a_hash } from '../encoder/jsr-std-wasm.ts';



Expand All @@ -26,19 +26,19 @@ export function make ({

} = {}): Promise<Deno.ServeDefaultExport> {

return create_app(gen_deno_kv(kv_path), {
const hash = gen_fnv1a_hash({
key: hash_seed,
large: hash_enlarge,
});

const storage = gen_deno_kv(kv_path);

return create_app(hash, storage, {
auth,
cache_name,
ttl_in_ms,
signing_nav,
signing_site,

encoding: gen_fnv1a_hash({
key: hash_seed,
large: hash_enlarge,
}),

});

}
Expand Down
2 changes: 2 additions & 0 deletions encoder/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export type Encoder = (message: string) => Promise<string>;

32 changes: 32 additions & 0 deletions encoder/jsr-std-wasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { encodeBase58 } from '@std/encoding/base58';
import { crypto as std_crypto } from '@std/crypto/crypto';

import type { Encoder } from './index.ts';

import { HMAC_SHA256 } from '../utils.ts';





export function gen_fnv1a_hash ({

key = `Cool URIs Don't Change`,
large = false,

} = {}): Encoder {

return async function (message) {

const entropy = await HMAC_SHA256({ key, message });

const algo = large ? 'FNV64A' : 'FNV32A';

const hash = await std_crypto.subtle.digest(algo, entropy);

return encodeBase58(hash);

};

}

21 changes: 12 additions & 9 deletions specs/smoking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ import { Hono } from 'hono';
import { testClient } from 'hono/testing';

import { create_app } from '../app.tsx';
import { gen_fnv1a_hash } from '../encoder/jsr-std-wasm.ts';
import { gen_deno_kv } from '../adapter/deno-kv.ts';
import { make_ttl_cache } from '../adapter/ttl-cache.ts';
import { make_map_object } from '../adapter/map-object.ts';
import { calc_fingerprint, gen_fnv1a_hash,
import { calc_fingerprint,
HMAC_SHA256, signingAuth, UUIDv4, webcrypto, challenge_, duration,
} from '../utils.ts';




const hashing = gen_fnv1a_hash();

const app = await create_app(gen_deno_kv(':memory:'), {
const app = await create_app(hashing, gen_deno_kv(':memory:'), {
insecure: true,
signing_nav: true,
});
Expand Down Expand Up @@ -137,8 +139,9 @@ Deno.test('throw on same code twice', async function () {

Deno.test('advanced config', async function () {

const other = await create_app(make_map_object, {
encoding: gen_fnv1a_hash({ large: true, key: 'ayb' }),
const hashing_local = gen_fnv1a_hash({ large: true, key: 'ayb' });

const other = await create_app(hashing_local, make_map_object, {
insecure: true,
});

Expand Down Expand Up @@ -194,7 +197,7 @@ Deno.test('advanced config', async function () {

Deno.test('default app', async function () {

await create_app(make_map_object);
await create_app(hashing, make_map_object);

});

Expand Down Expand Up @@ -305,7 +308,7 @@ Deno.test('signingAuth', async function () {

{ // ok on show page

const client = testClient(await create_app(make_map_object, {
const client = testClient(await create_app(hashing, make_map_object, {
auth: signingAuth([ fingerprint ]),
insecure: true,
}));
Expand All @@ -324,7 +327,7 @@ Deno.test('signingAuth', async function () {

const fingerprint_lookup = spy(() => true);

const client = testClient(await create_app(make_map_object, {
const client = testClient(await create_app(hashing, make_map_object, {
auth: signingAuth(fingerprint_lookup),
insecure: true,
}));
Expand Down Expand Up @@ -443,7 +446,7 @@ Deno.test('cache with TTL', async function () {
const url = 'https://example.com';
const code = 'foobar';

const app = await create_app(make_ttl_cache, {
const app = await create_app(hashing, make_ttl_cache, {
ttl_in_ms,
insecure: true,
});
Expand Down Expand Up @@ -502,7 +505,7 @@ Deno.test('take TTL from ctx.var', async function () {

using time = new FakeTime();

const app_local = await create_app(make_ttl_cache, {
const app_local = await create_app(hashing, make_ttl_cache, {
insecure: true,
ttl_in_ms: 500,
async auth (ctx, next) {
Expand Down
32 changes: 0 additions & 32 deletions utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,38 +50,6 @@ export const noop = createMiddleware(async function (_, next) {



export function gen_fnv1a_hash ({

key = `Cool URIs Don't Change`,
large = false,

} = {}) {

return async function () {

const { crypto: std_crypto } = await import('@std/crypto/crypto');
const { encodeBase58 } = await import('@std/encoding/base58');

return async function (message: string) {

const entropy = await HMAC_SHA256({ key, message });

const algo = large ? 'FNV64A' : 'FNV32A';

const hash = await std_crypto.subtle.digest(algo, entropy);

return encodeBase58(hash);

};

};

}





export function register (it: Iterable<{ href: string, remote: string }>, {

timeout = 5000,
Expand Down