Skip to content

Commit

Permalink
fix: deno
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien Ringrose committed Jul 18, 2024
1 parent 732f782 commit 12a1d5b
Show file tree
Hide file tree
Showing 15 changed files with 853 additions and 33 deletions.
7 changes: 4 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "@sejori/peko",
"description": "Featherweight apps on the edge, built with Web Standards 🐣⚡",
"version": "2.2.0",
"exports": "./mod.ts",
"imports": {
"htm": "^3.1.1",
"preact": "^10.19.6",
"preact-render-to-string": "^6.4.0"
"esbuild": "https://deno.land/x/[email protected]/mod.js",
"htm": "https://ga.jspm.io/npm:[email protected]/dist/htm.mjs",
"preact": "https://npm.reversehttp.com/preact,preact/hooks,htm/preact,preact-render-to-string"
}
}
810 changes: 810 additions & 0 deletions deno.lock

Large diffs are not rendered by default.

11 changes: 5 additions & 6 deletions example/preactSSR/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ import {
ssr,
file,
} from "../../mod.ts"; //"https://deno.land/x/peko/mod.ts"
import { renderToString } from "preact-render-to-string";
import { renderToString } from "preact";

import Home from "./src/pages/Home.ts";
import About from "./src/pages/About.ts";
import htmlTemplate from "./document.ts";

// note: bundling is inefficient and should be done in a build step
import * as esbuild from "esbuild";

const router = new Router();
router.use(logger(console.log));

router.get("/", {
middleware: cacher(),
handler: (ctx) =>
ssr(
handler: (ctx) => {
return ssr(
() => {
const ssrHTML = renderToString(Home(), null);
return htmlTemplate({
Expand All @@ -40,7 +38,8 @@ router.get("/", {
: "no-store",
}),
}
)(ctx),
)(ctx);
},
});

router.get(
Expand Down
3 changes: 1 addition & 2 deletions example/preactSSR/src/components/App.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useState, useEffect } from "preact/hooks";
import { html } from "htm/preact";
import { html, useState, useEffect } from "preact";
import List from "./List.ts";
import { useLocalState } from "../hooks/localstate.ts";

Expand Down
4 changes: 2 additions & 2 deletions example/preactSSR/src/components/Layout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { html } from "htm/preact";
import { html } from "preact";

const Layout = ({
navColor,
Expand All @@ -16,7 +16,7 @@ const Layout = ({
height="200px"
width="1000px"
style="max-width:100%; margin: 1rem;"
src="https://raw.githubusercontent.com/sejori/peko/examples/preact/assets/logo_dark_alpha.webp"
src="https://raw.githubusercontent.com/sejori/peko/main/example/preactSSR/assets/logo_dark_alpha.webp"
alt="peko-logo"
/>
<h1 style="text-align: center;">
Expand Down
3 changes: 1 addition & 2 deletions example/preactSSR/src/components/List.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useState } from "preact/hooks";
import { html } from "htm/preact";
import { html, useState } from "preact";

const List = ({ data }: { data: string[] }) => {
// takes a data prop
Expand Down
2 changes: 1 addition & 1 deletion example/preactSSR/src/hooks/localstate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect } from "preact/hooks";
import { useState, useEffect } from "preact";

// INITIAL STATE
const initialState: Record<string, unknown> = {
Expand Down
2 changes: 1 addition & 1 deletion example/preactSSR/src/pages/About.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { html } from "htm/preact";
import { html } from "preact";

import Layout from "../components/Layout.ts";
import App from "../components/App.ts";
Expand Down
2 changes: 1 addition & 1 deletion example/preactSSR/src/pages/Home.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { html } from "htm/preact";
import { html } from "preact";

import Layout from "../components/Layout.ts";

Expand Down
8 changes: 4 additions & 4 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ export interface Route {
handler: Handler;
}

export type Result = void | Response | undefined;
export type Next = () => Promise<Result> | Result;

export type Middleware = (
ctx: RequestContext,
next: Next
) => Promise<unknown> | unknown;
export type Next = () => Promise<Result> | Result;
export type Result = void | Response | undefined;

) => Promise<Result> | Result;
export type Handler = (ctx: RequestContext) => Promise<Response> | Response;
export type HandlerOptions = { headers?: Headers };

Expand Down
4 changes: 2 additions & 2 deletions lib/utils/Cascade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Middleware, Result, Next } from "../types.ts";
export type PromiseMiddleware = (
ctx: RequestContext,
next: Next
) => Promise<unknown>;
) => Promise<Result>;

/**
* Utility class for running middleware functions in a cascade
Expand Down Expand Up @@ -34,7 +34,7 @@ export class Cascade {
const res = await this.middleware[this.called++](this.ctx, () =>
this.run.call(this)
);
if (res instanceof Response) this.result = res;
if (res) this.result = res;
else return await this.run();
} catch (error) {
throw error;
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "peko",
"name": "@sejori/peko",
"version": "2.2.0",
"description": "Featherweight apps on the edge, built with Web Standards 🐣⚡",
"main": "mod.ts",
Expand All @@ -10,13 +10,13 @@
},
"type": "module",
"scripts": {
"start": "deno run --allow-net --allow-read --allow-env --unstable-sloppy-imports scripts/deno/main.ts",
"test": "deno test --allow-read --allow-net --unstable-sloppy-imports",
"profile:deno": "deno run --allow-read --allow-net --unstable-sloppy-imports scripts/deno/profile.ts",
"start": "deno run --allow-net --allow-read --allow-env ----allow-run scripts/deno/main.ts",
"test": "deno test --allow-read --allow-net",
"profile:deno": "deno run --allow-read --allow-net scripts/deno/profile.ts",
"profile:bun": "bun run scripts/bun/profile.ts",
"profile:start:wrangler": "wrangler dev scripts/wrangler/testApp.ts",
"profile:wrangler": "node --loader ts-node/esm scripts/wrangler/profile.ts",
"start:dev:deno": "deno run --allow-net --allow-read --allow-env --watch --unstable-sloppy-imports scripts/deno/main.ts",
"start:dev:deno": "deno run -A --watch scripts/deno/main.ts",
"start:dev:bun": "bun run --watch scripts/bun/main.ts",
"start:dev:wrangler": "wrangler dev scripts/wrangler/main.ts"
},
Expand Down
6 changes: 5 additions & 1 deletion scripts/bun/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import router from "../../example/preactSSR/router.ts";

router.middleware.unshift((ctx) => {
ctx.state.env = process.env
});

Bun.serve({
port: 8080,
fetch(req) {
return router.use((ctx) => ctx.state.env = process.env).handle(req);
return router.handle(req);
});

console.log("Bun server running with Peko router <3");
8 changes: 6 additions & 2 deletions scripts/deno/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import router from "../../example/preactSSR/router.ts";

router.middleware.unshift((ctx) => {
console.log("heelo");
ctx.state.env = Deno.env.toObject();
});

// Start Deno server with Peko router :^)
Deno.serve(
{
port: 7777,
},
(req) =>
router.use((ctx) => (ctx.state.env = Deno.env.toObject())).handle(req)
(req) => router.handle(req)
);

console.log("Deno server running with Peko router <3");
6 changes: 5 additions & 1 deletion scripts/wrangler/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import router from "../../example/preactSSR/router.ts";

router.middleware.unshift((ctx) => {
ctx.state.env = env;
});

export default {
fetch(request: Request) {
return router.use((ctx) => (ctx.state.env = env)).handle(request);
return router.handle(request);
},
} satisfies ExportedHandler;

Expand Down

0 comments on commit 12a1d5b

Please sign in to comment.