-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from sejori/sejori-patch-1
feat: test ci runners
- Loading branch information
Showing
49 changed files
with
1,749 additions
and
1,753 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
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
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
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,162 @@ | ||
import { | ||
Router, | ||
RequestContext, | ||
logger, | ||
sse, | ||
cacher, | ||
ssr, | ||
file, | ||
} from "../../index.ts"; //"https://deno.land/x/peko/mod.ts" | ||
import { renderToString } from "preact-render-to-string"; | ||
|
||
// Preact page components and HTML template for ssrHandler render logic | ||
import Home from "./src/pages/Home.ts"; | ||
import About from "./src/pages/About.ts"; | ||
import htmlTemplate from "./document.ts"; | ||
|
||
// esbuild bundling for app TS files | ||
import * as esbuild from "esbuild"; | ||
|
||
const env: Record<string, string> = | ||
(typeof Deno !== "undefined" && Deno.env.toObject()) || | ||
(typeof process !== "undefined" && process.env) || | ||
(typeof env !== "undefined" && env) || | ||
{}; | ||
|
||
const router = new Router(); | ||
router.use(logger(console.log)); | ||
|
||
// FRONTEND PAGES | ||
router.get("/", { | ||
// use cacher to serve responses from cache in prod env | ||
middleware: env.ENVIRONMENT === "production" ? cacher() : [], | ||
handler: ssr( | ||
() => { | ||
const ssrHTML = renderToString(Home(), null); | ||
return htmlTemplate({ | ||
title: "Peko", | ||
ssrHTML, | ||
entrypoint: "/src/pages/Home.ts", | ||
}); | ||
}, | ||
{ | ||
headers: new Headers({ | ||
// instruct browser to cache page in prod env | ||
"Cache-Control": | ||
env.ENVIRONMENT === "production" | ||
? "max-age=86400, stale-while-revalidate=86400" | ||
: "no-store", | ||
}), | ||
} | ||
), | ||
}); | ||
|
||
router.get( | ||
"/about", | ||
(ctx) => { | ||
ctx.state = { | ||
request_time: `${Date.now()}`, | ||
...env, | ||
}; | ||
}, | ||
ssr((ctx) => { | ||
const ssrHTML = renderToString(About(ctx.state), null); | ||
return htmlTemplate({ | ||
title: "Peko | About", | ||
ssrHTML, | ||
entrypoint: "/src/pages/About.ts", | ||
}); | ||
}) | ||
); | ||
|
||
// STATIC FILES | ||
// dynamic URL param for filename, always cache | ||
router.get("/assets/:filename", cacher(), async (ctx) => | ||
( | ||
await file( | ||
new URL( | ||
`https://raw.githubusercontent.com/sejori/peko/main/example/preact/assets/${ctx.params.filename}` | ||
), | ||
{ | ||
headers: new Headers({ | ||
// instruct browser to cache file in prod env | ||
"Cache-Control": | ||
env.ENVIRONMENT === "production" | ||
? "max-age=86400, stale-while-revalidate=86400" | ||
: "no-store", | ||
}), | ||
} | ||
) | ||
)(ctx) | ||
); | ||
|
||
// BUNDLED TS FILES | ||
// dynamic URL param for filename, always cache | ||
router.get("/src/:filename", cacher(), async (ctx) => | ||
( | ||
await file( | ||
new URL( | ||
`https://raw.githubusercontent.com/sejori/peko/main/example/preact/src/${ctx.params.filename}` | ||
), | ||
{ | ||
transform: async (contents) => { | ||
const reader = contents.getReader(); | ||
let result = ""; | ||
let { done, value } = await reader.read(); | ||
while (!done) { | ||
result += new TextDecoder().decode(value); | ||
({ done, value } = await reader.read()); | ||
} | ||
const esbuildResult = await esbuild.build({ | ||
bundle: true, | ||
write: false, | ||
stdin: { | ||
contents: result, | ||
}, | ||
format: "esm", | ||
target: "es2022", | ||
loader: { | ||
".ts": "ts", | ||
}, | ||
}); | ||
|
||
const bundle = esbuildResult.outputFiles[0].text; | ||
console.log(bundle); | ||
return bundle; | ||
}, | ||
headers: new Headers({ | ||
"Content-Type": "application/javascript", | ||
// instruct browser to cache file in prod env | ||
"Cache-Control": | ||
env.ENVIRONMENT === "production" | ||
? "max-age=86400, stale-while-revalidate=86400" | ||
: "no-store", | ||
}), | ||
} | ||
) | ||
)(ctx) | ||
); | ||
|
||
// API FUNCTIONALITY | ||
const demoEventTarget = new EventTarget(); | ||
// shorthand route adding | ||
router.get("/sse", (ctx: RequestContext) => { | ||
setInterval(() => { | ||
demoEventTarget.dispatchEvent( | ||
new CustomEvent("send", { detail: Math.random() }) | ||
); | ||
}, 2500); | ||
|
||
return sse(demoEventTarget)(ctx); | ||
}); | ||
// full route object adding | ||
router.addRoute({ | ||
path: "/api/parrot", | ||
method: "POST", | ||
handler: async (ctx: RequestContext) => { | ||
const body = await ctx.request.text(); | ||
return new Response(`Parrot sqwarks: ${body}`); | ||
}, | ||
}); | ||
|
||
export default router; |
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
Oops, something went wrong.