Skip to content

Commit

Permalink
Merge pull request #29 from digitaldesigndj/main
Browse files Browse the repository at this point in the history
update and upgrade with some tests added and an action for the tests, also I did a `deno fmt`

@digitaldesigndj san
Thank you.
  • Loading branch information
Octo8080X authored Jul 24, 2023
2 parents e13ef90 + c00b720 commit 84e3abc
Show file tree
Hide file tree
Showing 37 changed files with 393 additions and 198 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/deno.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This workflow will install Deno then run Deno fmt and test.
# For more information see: https://github.com/denoland/setup-deno

name: Deno Deploy & Test

on:
push:
branches: ["main","test"]
pull_request:
branches: ["main","test"]

permissions:
id-token: write # This is required to allow the GitHub Action to authenticate with Deno Deploy.
contents: read

jobs:
test:
runs-on: ubuntu-latest
env:
DENO_ENV: testing
BASE_URL: http://localhost:8000
DENO_DIR: deno_dir
APP_KEY: not-secret-at-all
steps:
- name: Setup repo
uses: actions/checkout@v3

- name: Setup Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.35.2

- name: Verify formatting
run: deno fmt --check

- name: Cache Deno dependencies
uses: actions/cache@v3
with:
key: denoDeps-${{ hashFiles('deno.lock') }}
path: ${{ env.DENO_DIR }}

- name: Run tests
run: deno test -A --quiet
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import {
Fresh Session currently uses [JSON Web Token](https://jwt.io/) under the hood to
create and manage session in the cookies.

JWT requires a secret key to sign new tokens. Fresh Session uses the
secret key from your [environment variable](https://deno.land/std/dotenv/load.ts)
JWT requires a secret key to sign new tokens. Fresh Session uses the secret key
from your [environment variable](https://deno.land/std/dotenv/load.ts)
`APP_KEY`.

If you don't know how to setup environment variable locally, I wrote
Expand Down Expand Up @@ -60,8 +60,6 @@ interacting with your session.

```tsx
// ./routes/dashboard.tsx
/** @jsx h */
import { h } from "preact";
import { Handlers, PageProps } from "$fresh/server.ts";
import { WithSession } from "https://deno.land/x/[email protected]/mod.ts";

Expand Down Expand Up @@ -91,7 +89,7 @@ export const handler: Handlers<
// /!\ This flashed data will disappear after accessing it one time.
session.flash("success");
// Session Key Rotation only kv store and redis store.
// Is not work in cookie store.
// Is not work in cookie store.

// Rotate the session key. Only supported by the kv store and redis store, not the cookie store.
// If using the session for authentication, with a kv or redis store, it is recommended to rotate the key at login to prevent session fixation attack.
Expand Down
10 changes: 8 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"tasks": {
"fixture": "deno run -A --watch=static/,routes/ tests/fixture/dev.ts"
"fixture": "deno run -A --watch=tests/fixture/static/,tests/fixture/routes/ tests/fixture/dev.ts",
"kv": "deno run -A --unstable --watch=example/use_kv_store/static/,example/use_kv_store/routes/ example/use_kv_store/dev.ts"
},
"importMap": "./tests/fixture/import_map.json"
"imports": {
"$fresh/": "https://deno.land/x/[email protected]/",
"$std/": "https://deno.land/[email protected]/",
"cookiejar": "https://deno.land/x/[email protected]/mod.ts"
},
"lock": false
}
2 changes: 1 addition & 1 deletion example/use_kv_store/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM denoland/deno:1.33.1
FROM denoland/deno:1.35.1

RUN apt-get update

Expand Down
4 changes: 2 additions & 2 deletions example/use_kv_store/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Use Redis store sample
# Use KV store sample

This project is fresh project used fresh-session with redis store.
This project is fresh project used fresh-session with a KV store

## Usage

Expand Down
12 changes: 0 additions & 12 deletions example/use_kv_store/components/Button.tsx

This file was deleted.

15 changes: 13 additions & 2 deletions example/use_kv_store/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@
"tasks": {
"start": "deno run -A --unstable --watch=static/,routes/ dev.ts"
},
"importMap": "./import_map.json",
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
},
"imports": {
"$fresh/": "https://deno.land/x/[email protected]/",
"preact": "https://esm.sh/[email protected]",
"preact/": "https://esm.sh/[email protected]/",
"preact-render-to-string": "https://esm.sh/*[email protected]",
"@preact/signals": "https://esm.sh/*@preact/[email protected]",
"@preact/signals-core": "https://esm.sh/*@preact/[email protected]",
"twind": "https://esm.sh/[email protected]",
"twind/": "https://esm.sh/[email protected]/",
"fresh-session/": "https://raw.githubusercontent.com/Octo8080X/fresh-session/main/"
},
"lock": false
}
13 changes: 3 additions & 10 deletions example/use_kv_store/fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.

import config from "./deno.json" assert { type: "json" };
import * as $0 from "./routes/_middleware.ts";
import * as $1 from "./routes/api/joke.ts";
import * as $2 from "./routes/index.tsx";
import * as $$0 from "./islands/Counter.tsx";
import * as $1 from "./routes/index.tsx";

const manifest = {
routes: {
"./routes/_middleware.ts": $0,
"./routes/api/joke.ts": $1,
"./routes/index.tsx": $2,
},
islands: {
"./islands/Counter.tsx": $$0,
"./routes/index.tsx": $1,
},
islands: {},
baseUrl: import.meta.url,
config,
};

export default manifest;
13 changes: 0 additions & 13 deletions example/use_kv_store/import_map.json

This file was deleted.

17 changes: 0 additions & 17 deletions example/use_kv_store/islands/Counter.tsx

This file was deleted.

4 changes: 3 additions & 1 deletion example/use_kv_store/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />

export const PORT = 8000;

import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";

import twindPlugin from "$fresh/plugins/twind.ts";
import twindConfig from "./twind.config.ts";

await start(manifest, { plugins: [twindPlugin(twindConfig)] });
await start(manifest, { plugins: [twindPlugin(twindConfig)], port: PORT });
4 changes: 4 additions & 0 deletions example/use_kv_store/routes/_middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MiddlewareHandlerContext } from "$fresh/server.ts";
import { kvSession, WithSession } from "fresh-session/mod.ts";
import { PORT } from "../main.ts";
export type State = WithSession;

async function sessionHundler(
Expand All @@ -14,6 +15,9 @@ async function sessionHundler(
if (req.url === `http://localhost:${ctx.localAddr?.port}/`) {
return session(req, ctx);
}
if (req.url === `http://localhost:${PORT}/`) {
return session(req, ctx);
}
return ctx.next();
}
export const handler = [sessionHundler];
21 changes: 0 additions & 21 deletions example/use_kv_store/routes/api/joke.ts

This file was deleted.

9 changes: 6 additions & 3 deletions example/use_kv_store/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type SessionData = { session: Record<string, string>; message?: string };
export const handler: Handlers<SessionData, WithSession> = {
GET(_req: Request, ctx: HandlerContext<SessionData, WithSession>) {
const { session } = ctx.state;
// console.log(session);
const message = session.flash("message");

return ctx.render({
Expand All @@ -27,7 +28,7 @@ export const handler: Handlers<SessionData, WithSession> = {
const text = form.get("new_session_text_value");
session.set("text", text);
session.flash("message", "Session value update!");
if(form.get("session_key_rotate")) {
if (form.get("session_key_rotate")) {
session.keyRotate();
}
}
Expand All @@ -43,9 +44,10 @@ export default function Index({ data }: PageProps<SessionData>) {
return (
<>
<Head>
<title>frash-session example[redis in use]</title>
<title>frash-session example[denoKV in use]</title>
</Head>
<div>
{/* <pre>{JSON.stringify(data, null, 2)}</pre> */}
<div>Flash Message: {data.message}</div>
<div>Now Session Value: {data.session.text}</div>
<div>
Expand All @@ -63,7 +65,8 @@ export default function Index({ data }: PageProps<SessionData>) {
id="session_key_rotate"
name="session_key_rotate"
placeholder="New session_text_value"
/><label for="session_key_rotate">Session key rotate</label>
/>
<label for="session_key_rotate">Session key rotate</label>
</div>
<div>
<button type="submit">Update Session Value!</button>
Expand Down
2 changes: 1 addition & 1 deletion example/use_redis_store/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM denoland/deno:1.25.1
FROM denoland/deno:1.35.2

RUN apt-get update
RUN mkdir /usr/src/app
Expand Down
14 changes: 12 additions & 2 deletions example/use_redis_store/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@
"tasks": {
"start": "deno run -A --watch=static/,routes/ dev.ts"
},
"importMap": "./import_map.json",
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
},
"imports": {
"$fresh/": "https://deno.land/x/[email protected]/",
"preact": "https://esm.sh/[email protected]",
"preact/": "https://esm.sh/[email protected]/",
"preact-render-to-string": "https://esm.sh/*[email protected]",
"@preact/signals": "https://esm.sh/*@preact/[email protected]",
"@preact/signals-core": "https://esm.sh/*@preact/[email protected]",
"redis/": "https://deno.land/x/[email protected]/",
"fresh-session/": "https://deno.land/x/[email protected]/"
},
"lock": false
}
2 changes: 0 additions & 2 deletions example/use_redis_store/fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.

import config from "./deno.json" assert { type: "json" };
import * as $0 from "./routes/_middleware.ts";
import * as $1 from "./routes/index.tsx";

Expand All @@ -13,7 +12,6 @@ const manifest = {
},
islands: {},
baseUrl: import.meta.url,
config,
};

export default manifest;
12 changes: 0 additions & 12 deletions example/use_redis_store/import_map.json

This file was deleted.

4 changes: 3 additions & 1 deletion example/use_redis_store/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />

export const PORT = 8000;

import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";

await start(manifest);
await start(manifest, { port: PORT });
4 changes: 4 additions & 0 deletions example/use_redis_store/routes/_middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MiddlewareHandlerContext } from "$fresh/server.ts";
import { redisSession, WithSession } from "fresh-session/mod.ts";
import { connect } from "redis/mod.ts";
import { PORT } from "../main.ts";
export type State = WithSession;

const redis = await connect({
Expand All @@ -17,6 +18,9 @@ function sessionHundler(req: Request, ctx: MiddlewareHandlerContext<State>) {
if (req.url === `http://localhost:${ctx.localAddr?.port}/`) {
return session(req, ctx);
}
if (req.url === `http://localhost:${PORT}/`) {
return session(req, ctx);
}
return ctx.next();
}
export const handler = [sessionHundler];
5 changes: 3 additions & 2 deletions example/use_redis_store/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const handler: Handlers<SessionData, WithSession> = {
const text = form.get("new_session_text_value");
session.set("text", text);
session.flash("message", "Session value update!");
if(form.get("session_key_rotate")) {
if (form.get("session_key_rotate")) {
session.keyRotate();
}
}
Expand Down Expand Up @@ -63,7 +63,8 @@ export default function Index({ data }: PageProps<SessionData>) {
id="session_key_rotate"
name="session_key_rotate"
placeholder="New session_text_value"
/><label for="session_key_rotate">Session key rotate</label>
/>
<label for="session_key_rotate">Session key rotate</label>
</div>
<div>
<button type="submit">Update Session Value!</button>
Expand Down
Loading

0 comments on commit 84e3abc

Please sign in to comment.