Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jldec committed Aug 21, 2024
1 parent fc734e7 commit d982792
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 229 deletions.
2 changes: 2 additions & 0 deletions .cursorignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public/
# Add directories or file patterns to ignore during indexing (e.g. foo/ or *.csv)
138 changes: 20 additions & 118 deletions packages/worker/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { Hono, Context } from 'hono'
import { Hono, type Context, type Bindings, type Content, type StatusCode } from './types'
import { jsxRenderer, useRequestContext } from 'hono/jsx-renderer'
import { type FC } from 'hono/jsx'
import { serveStatic } from 'hono/cloudflare-workers'
import { StatusCode } from 'hono/utils/http-status'
import { raw } from 'hono/html'
import { parseFrontmatter } from './markdown/parse-frontmatter'
import { parseMarkdown } from './markdown/parse-markdown'
import { extname } from '@std/path'
import { hash } from './markdown/hash'
import { routePartykitRequest } from 'partyserver'
import { getContent } from './markdown/get-content'

// PartyServer
export { Chat } from './chat'
// PartyServer durable object
export { Chat } from './partyserver'

// https://hono.dev/docs/middleware/builtin/jsx-renderer#extending-contextrenderer
declare module 'hono' {
Expand All @@ -26,58 +24,14 @@ declare module 'hono' {
// see also: https://github.com/honojs/hono/issues/1127
import manifest from '__STATIC_CONTENT_MANIFEST'

type Bindings = {
PAGE_CACHE: KVNamespace
IMAGES: R2Bucket
AI: any
GH_PAT: string
IMAGE_KEY: string
}

const app = new Hono<{ Bindings: Bindings }>()

const fileUrlPrefix = 'https://raw.githubusercontent.com/jldec/presskit/main/content'
const indexFile = 'index.md'

type Content = {
statusCode: StatusCode
attrs: any
html: string
summary?: AiSummarizationOutput
}

let homeContent: Content | null = null

async function getContent(
url: string,
c: Context<{
Bindings: Bindings
}>
): Promise<Content> {
try {
const response = await fetch(url)
const parsedContent = parseFrontmatter(await response.text())

return {
statusCode: response.status as StatusCode,
attrs: parsedContent.attrs,
html: parseMarkdown(parsedContent.body, { hashPrefix: c.env.IMAGE_KEY })
}
} catch (error) {
return {
statusCode: 500 as StatusCode,
attrs: {},
html: (error as any).message as string
}
}
}
const app = new Hono()

const NavItems: FC = async () => {
const c = useRequestContext()
if (homeContent === null) await getHomeContent(c)
const navItems = (await getContent('/', c))?.attrs?.nav || []
return (
<>
{homeContent?.attrs.nav?.map((item: any) => (
{navItems.map((item: any) => (
<li>
<a class="link px-2" href={item.link}>
{item.text ?? item.link}
Expand All @@ -94,11 +48,6 @@ const NavItems: FC = async () => {
Chat
</a>
</li>
<li>
<a class="link px-2 font-black" href="/test">
Test
</a>
</li>
</>
)
}
Expand Down Expand Up @@ -182,16 +131,6 @@ app.use(
})
)

async function getHomeContent(c: Context<{ Bindings: Bindings }>) {
homeContent = await getContent(`${fileUrlPrefix}/${indexFile}`, c)
console.log(JSON.stringify(homeContent.attrs))
}

app.get('/', async (c) => {
if (homeContent === null) await getHomeContent(c)
return c.render('', { htmlContent: homeContent?.html, title: homeContent?.attrs?.title })
})

app.get('/admin', async (c) => {
return c.render(
<>
Expand Down Expand Up @@ -222,28 +161,17 @@ app.get('/admin', async (c) => {
app.get('/chat', async (c) => {
return c.render(
<>
<div id="chat-root"></div>
<script src="/js/partychat.js" type="module"></script>
<div id="chat-root"></div>
<script src="/js/partychat.js" type="module"></script>
</>,
{}
)
})

app.get('/test', async (c) => {
return c.render(
<>
<h1>Test Page</h1>
<p>This is a test page to demonstrate rendering.</p>
</>,
{ title: "Test Page" }
)
})


// Formatted c.json()
function fjson(o: any) {
return new Response(JSON.stringify(o, null, 2), {
headers: { 'Content-Type': 'application/json; charset=UTF-8' }
headers: { 'Content-Type': 'application/json;charset=UTF-8' }
})
}

Expand Down Expand Up @@ -376,32 +304,15 @@ function storeHeaders(ogHeaders: Headers) {
}

// middleware fetches markdown content, fall through if not found
// only serves extensionless routes
// only serves extensionless route including root '/'
app.use(async (c, next) => {
let path = c.req.path // includes leading /
if (extname(path) !== '') return await next()
let content: Content
let cached = false
const cachedContent = await c.env.PAGE_CACHE.get(path)
if (cachedContent !== null) {
content = JSON.parse(cachedContent) as Content
cached = true
} else {
content = await getContent(`${fileUrlPrefix}${path}.md`, c)
}
if (content.statusCode === 200) {
if (!cached) {
c.executionCtx.waitUntil(summarizeAndCache(c.env, path, content))
}
const path = c.req.path // includes leading /
if (extname(path) !== '' || path.startsWith('/parties')) return await next()
const content = await getContent(path, c)
if (content) {
console.log('markdown', c.req.url, content.statusCode)
c.status(content.statusCode)
return c.render(
<>
<h2>AI Summary</h2>
{content.summary?.summary ?? ' No summary yet.'}
<hr />
</>,
{ htmlContent: content.html, title: content.attrs?.title }
)
return c.render('', { htmlContent: content.html, title: content.attrs?.title })
}
// else fall through
await next()
Expand All @@ -410,20 +321,11 @@ app.use(async (c, next) => {
// serve static from the root
app.get(serveStatic({ root: './', manifest }))

async function summarizeAndCache(env: Bindings, key: string, content: Content) {
content.summary = await env.AI.run('@cf/facebook/bart-large-cnn', {
input_text: content.html,
max_length: 50
})
console.log('summarized content', JSON.stringify(content, null, 2))
await env.PAGE_CACHE.put(key, JSON.stringify(content))
}

// listen for websocket (partySocket) requests
app.use(async (c, next) => {
const party = c.req.path.startsWith('/parties') && await routePartykitRequest(c.req.raw, c.env)
const party = c.req.path.startsWith('/parties') && (await routePartykitRequest(c.req.raw, c.env))
console.log('routePartykitRequest', c.req.url, party)
return (party || await next())
return party || (await next())
})

app.notFound((c) => {
Expand All @@ -436,4 +338,4 @@ app.notFound((c) => {
)
})

export default app
export default app
40 changes: 0 additions & 40 deletions packages/worker/src/markdown/crypto.test.ts

This file was deleted.

71 changes: 0 additions & 71 deletions packages/worker/src/markdown/crypto.ts

This file was deleted.

40 changes: 40 additions & 0 deletions packages/worker/src/markdown/get-content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { Content, Context, StatusCode } from '../types'
import { parseFrontmatter } from './parse-frontmatter'
import { parseMarkdown } from './parse-markdown'

const fileUrlPrefix = 'https://raw.githubusercontent.com/jldec/presskit/main/content'

// memoize to speed up homeContent().attrs for Nav
let homeContent: Content | null = null

function filePath(path: string): string {
if (path.endsWith('/')) {
path += 'index'
}
return `${fileUrlPrefix}${path}.md`
}

export async function getContent(path: string, c: Context): Promise<Content | null> {
try {
if (path === '/' && homeContent) return homeContent
const cachedContent = await c.env.PAGE_CACHE.get(path)
if (cachedContent !== null) return JSON.parse(cachedContent) as Content

const response = await fetch(filePath(path))
if (!response.ok) throw new Error(`${response.status} error fetching ${path}` )
const parsedFrontmatter = parseFrontmatter(await response.text())
const content = {
statusCode: response.status as StatusCode,
attrs: parsedFrontmatter.attrs,
html: parseMarkdown(parsedFrontmatter.body, { hashPrefix: c.env.IMAGE_KEY })
}
c.executionCtx.waitUntil(c.env.PAGE_CACHE.put(path, JSON.stringify(content)))
if (path === '/') {
homeContent = content
}
return content
} catch (error) {
console.error(error)
return null
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Original code copied from https://github.com/threepointone/durable-chat
// MIT License (c) 2024 Sunil Pai
import { type Connection, type ConnectionContext, Server, type WSMessage } from 'partyserver'
import { nanoid } from 'nanoid'
import { EventSourceParserStream } from 'eventsource-parser/stream'
Expand Down
Loading

0 comments on commit d982792

Please sign in to comment.