-
-
Notifications
You must be signed in to change notification settings - Fork 20
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 #45 from floodfx/extract_handler
Extract handler
- Loading branch information
Showing
10 changed files
with
379 additions
and
205 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export * from "./live_view_server"; | ||
export * from "./component"; | ||
export * from "./live_view_route"; | ||
export * from "./live_view_server"; | ||
export * from "./templates"; | ||
export * from "./templates/helpers"; | ||
export * from "./templates/diff"; | ||
export * from "./templates/helpers"; | ||
|
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,93 @@ | ||
import { SessionData } from "express-session"; | ||
import { nanoid } from "nanoid"; | ||
import request from "superwstest"; | ||
import { configLiveViewHandler } from "."; | ||
import { BaseLiveViewComponent } from "./component/base_component"; | ||
import { LiveViewMountParams, LiveViewSocket } from "./component/types"; | ||
import { LiveViewServer } from "./live_view_server"; | ||
import { html } from "./templates"; | ||
|
||
|
||
describe("test live view route", () => { | ||
|
||
it("starting / stopping twice has no effect", async () => { | ||
const lvServer = new LiveViewServer({ | ||
signingSecret: "MY_VERY_SECRET_KEY", | ||
port: 7878, | ||
pageTitleDefaults: { | ||
prefix: "TitlePrefix - ", | ||
suffix: " - TitleSuffix", | ||
title: "Title", | ||
}, | ||
}); | ||
lvServer.start(); | ||
expect(lvServer.isStarted).toBe(true); | ||
lvServer.start(); | ||
expect(lvServer.isStarted).toBe(true); | ||
lvServer.shutdown(); | ||
expect(lvServer.isStarted).toBe(false); | ||
lvServer.shutdown(); | ||
expect(lvServer.isStarted).toBe(false); | ||
}) | ||
|
||
it("use configLiveViewHandler", (done) => { | ||
const lvServer = new LiveViewServer({ | ||
signingSecret: "MY_VERY_SECRET_KEY", | ||
port: 7878, | ||
pageTitleDefaults: { | ||
prefix: "TitlePrefix - ", | ||
suffix: " - TitleSuffix", | ||
title: "Title", | ||
}, | ||
}); | ||
|
||
lvServer.start(); | ||
expect(lvServer.isStarted).toBe(true); | ||
const lvComponent = new LiveViewComponent() | ||
lvServer.expressApp.get(...configLiveViewHandler( | ||
"/test/foo", | ||
lvComponent, | ||
"root.html.ejs", | ||
"my signing secret", | ||
(req) => { | ||
return { | ||
...req.session, // copy session data | ||
csrfToken: req.session.csrfToken || nanoid(), | ||
} | ||
}, | ||
{ | ||
title: "Default Title" | ||
} | ||
)) | ||
lvServer.start() | ||
setTimeout((() => { | ||
request(lvServer.httpServer).get('/test/foo').expect(200).then(res => { | ||
expect(res.text).toContain(lvComponent.render({ message: "test" }).toString()) | ||
done(); | ||
lvServer.shutdown() | ||
}) | ||
}), 100) | ||
}) | ||
|
||
|
||
|
||
}) | ||
|
||
declare module 'express-session' { | ||
interface SessionData { | ||
message: string; | ||
} | ||
} | ||
|
||
class LiveViewComponent extends BaseLiveViewComponent<{ message?: string }, {}> { | ||
|
||
mount(params: LiveViewMountParams, session: Partial<SessionData>, socket: LiveViewSocket<{}>): {} { | ||
return { message: session.message || "test" } | ||
} | ||
|
||
render(ctx: { message: string }) { | ||
const { message } = ctx | ||
return html`<div>${message}</div>`; | ||
} | ||
|
||
} |
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,62 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
import jwt from "jsonwebtoken"; | ||
import { nanoid } from "nanoid"; | ||
import { LiveViewComponent, LiveViewSocket, PageTitleDefaults } from "."; | ||
|
||
type SessionDataProvider<T extends {csrfToken: string}> = (req: Request) => T; | ||
|
||
const emptyVoid = () => {}; | ||
|
||
export const configLiveViewHandler = <T extends {csrfToken: string}>( | ||
getPath: string, | ||
component: LiveViewComponent<unknown,unknown>, | ||
rootView: string, | ||
signingSecret: string, | ||
sessionDataProvider: SessionDataProvider<T>, | ||
pageTitleDefaults: PageTitleDefaults | ||
): [string, (req:Request, res: Response, next: NextFunction) => Promise<void>] => { | ||
return [getPath, async (req:Request, res: Response, next: NextFunction) => { | ||
|
||
// new LiveViewId for each request | ||
const liveViewId = nanoid(); | ||
|
||
// mock socket | ||
const liveViewSocket: LiveViewSocket<T> = { | ||
id: liveViewId, | ||
connected: false, // ws socket not connected on http request | ||
context: {} as T, | ||
sendInternal: emptyVoid, | ||
repeat: emptyVoid, | ||
pageTitle: emptyVoid, | ||
subscribe: emptyVoid, | ||
pushPatch: emptyVoid, | ||
} | ||
|
||
// get session data from provider | ||
const session = sessionDataProvider(req); | ||
|
||
// mount and render component | ||
const ctx = await component.mount( | ||
{ | ||
_csrf_token: session.csrfToken, | ||
_mounts: -1 | ||
}, | ||
session, | ||
liveViewSocket | ||
); | ||
const view = component.render(ctx); | ||
|
||
// render the view with all the data | ||
res.render(rootView, { | ||
page_title: pageTitleDefaults?.title ?? "", | ||
page_title_prefix: pageTitleDefaults?.prefix, | ||
page_title_suffix: pageTitleDefaults?.suffix, | ||
csrf_meta_tag: req.session.csrfToken, | ||
liveViewId, | ||
session: jwt.sign(session, signingSecret), | ||
// TODO support static assets https://github.com/floodfx/liveviewjs/issues/42 | ||
statics: jwt.sign(JSON.stringify(view.statics), signingSecret), | ||
inner_content: view.toString() | ||
}) | ||
}] | ||
} |
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