-
Notifications
You must be signed in to change notification settings - Fork 3
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
12 changed files
with
271 additions
and
111 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
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,85 @@ | ||
import { Hono } from 'hono' | ||
import { currentConfig } from './config.ts' | ||
import { get_doc_by_hpath } from './cache.ts' | ||
import { htmlTemplate } from './htmlTemplate.ts' | ||
import { renderHTML } from './render.ts' | ||
|
||
export function createHonoApp() { | ||
const app = new Hono() | ||
app.use(async (_, next) => { | ||
console.log('请求到达') | ||
try { | ||
await next() | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
}) | ||
app.get('/', (c) => c.redirect('/index.html')) | ||
app.get('/assets/*', async (c) => { | ||
// TODO 处于安全考虑应该防范 file 跳出 assets | ||
const file = c.req.path | ||
const r = await fetch(`${currentConfig.value.apiPrefix}${file}`, { | ||
headers: { | ||
Authorization: `Token ${currentConfig.value.authorized}`, | ||
}, | ||
method: 'GET', | ||
}) | ||
|
||
if (!r.body) { | ||
return c.text('Not Found', 404, { 'Content-Type': 'text/plain' }) | ||
} | ||
return c.stream( | ||
async (stream) => { | ||
const reader = r.body!.getReader() | ||
console.log(file) | ||
while (true) { | ||
const r = await reader.read() | ||
if (r.done) { | ||
stream.close() | ||
break | ||
} else { | ||
stream.write(r.value) | ||
} | ||
} | ||
}, | ||
200, | ||
{ | ||
'content-type': r.headers.get('content-type')!, | ||
}, | ||
) | ||
}) | ||
app.get('*', async (c) => { | ||
const path = decodeURIComponent(c.req.path) | ||
|
||
const r = await renderHtmlByPath(path).catch((err) => { | ||
console.log('err') | ||
|
||
return err.message | ||
}) | ||
|
||
if (r instanceof Error) throw r | ||
return c.html(r) | ||
}) | ||
return app | ||
} | ||
|
||
async function renderHtmlByPath(path: string): Promise<string | Error> { | ||
const hpath = decodeURIComponent(path) | ||
.replace(/\#(.*)?$/, '') | ||
.replace(/\.html$/, '') | ||
console.log(currentConfig.value) | ||
|
||
const doc = await get_doc_by_hpath(hpath) | ||
|
||
return await htmlTemplate( | ||
{ | ||
title: doc.Properties?.title || '', | ||
htmlContent: await renderHTML(doc), | ||
level: path.split('/').length - 1 /** 最开头有一个 / */, | ||
}, | ||
{ | ||
...currentConfig.value.cdn, | ||
embedCode: currentConfig.value.embedCode, | ||
}, | ||
) | ||
} |
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,51 @@ | ||
import { defineComponent, ref, watchEffect } from 'vue' | ||
import { useRoute } from 'vue-router' | ||
import { configs } from '~/core/config.ts' | ||
const preview = defineComponent({ | ||
setup() { | ||
const route = useRoute() | ||
const path = route.params.path as string | ||
const iframeHref = ref<string | null>(null) | ||
// firefox 不兼容 | ||
navigator.serviceWorker | ||
.register(import.meta.env.PROD ? 'sw.js' : '/sw.ts', { | ||
scope: '/', | ||
}) | ||
.then(async (_r) => { | ||
const configStatus = await ( | ||
await fetch('/preview/page/configs', { | ||
body: JSON.stringify(configs), | ||
method: 'POST', | ||
mode: 'cors', | ||
credentials: 'omit', | ||
}) | ||
).json() | ||
console.log('configStatus', configStatus) | ||
iframeHref.value = `/preview/page/${path}` | ||
}) | ||
.catch((e) => { | ||
console.log(e) | ||
}) | ||
const myElement = ref<HTMLIFrameElement | null>(null) | ||
watchEffect(async () => { | ||
if (!myElement.value) return | ||
// const honoApp = createHonoApp() | ||
|
||
// const r = honoApp.request(path) as Promise<Response> | ||
// const html = await (await r).text() | ||
// myElement.value.contentDocument!.body.innerHTML = html | ||
}) | ||
|
||
return () => ( | ||
<> | ||
<h4 style="color:#3c">预览界面</h4> | ||
<iframe | ||
ref={myElement} | ||
src={iframeHref.value ?? ''} | ||
style="width:100%;height:700px;border:none;" | ||
></iframe> | ||
</> | ||
) | ||
}, | ||
}) | ||
export default preview |
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
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
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,48 @@ | ||
import { storeDep } from '~/core/dependency.ts' | ||
import '~/core/render.api.dep' | ||
import { createHonoApp } from './src/core/hono_server.ts' | ||
import { Hono } from 'hono' | ||
import { loadConfigFile } from '~/core/config.ts' | ||
import { setCache } from '~/core/cache.ts' | ||
// 这个文件放在最外层是为了方便开发时调试,因为 sw 的 scop 规定 | ||
|
||
storeDep.getItem = getItem | ||
storeDep.setItem = setItem | ||
setCache(false) | ||
|
||
export function setItem(key: string, _value: string) { | ||
console.log(key) | ||
} | ||
export function getItem(_key: string): string | undefined { | ||
return | ||
} | ||
|
||
const renderServer = createHonoApp() | ||
const app = new Hono() | ||
|
||
app.use('*', async (c, next) => { | ||
await next() | ||
}) | ||
// 从主线程接收配置文件 | ||
app.post('/preview/page/configs', async (c) => { | ||
const r = await c.req.json() | ||
console.log('接收配置文件完毕', r) | ||
loadConfigFile(r) | ||
return c.json({ ok: true }) | ||
}) | ||
app.get('/preview/page/*', async (c) => { | ||
const url = c.req.path.substring('/preview/page'.length) | ||
// 转发到渲染服务器 | ||
return renderServer.request(url) | ||
}) | ||
app.notFound(async (c) => { | ||
// 请求转发给真实服务器 | ||
return fetch(c.req.raw) | ||
}) | ||
// | ||
app.fire() | ||
self.addEventListener('install', (event) => { | ||
console.log('Service Worker installed:', event) | ||
//@ts-ignore 跳过等待激活阶段,立即激活新的Service Worker | ||
// self.skipWaiting() | ||
}) |
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 |
---|---|---|
|
@@ -37,5 +37,5 @@ | |
"src/**/*.vue", | ||
"public/dev/http.js", | ||
"store/api_test.ts" | ||
], | ||
, "sw.ts" ], | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.