Skip to content

Commit

Permalink
Merge pull request #41 from floodfx/middleware
Browse files Browse the repository at this point in the history
Middleware
  • Loading branch information
floodfx authored Feb 21, 2022
2 parents 0383063 + eeed380 commit 4645e8a
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 96 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ node_modules
.env
dist/
.parcel-cache/
.DS_Store
.DS_Store
coverage/lcov-report/
coverage/lcov.info
170 changes: 87 additions & 83 deletions coverage/clover.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<coverage generated="1645286671104" clover="3.2.0">
<project timestamp="1645286671104" name="All files">
<metrics statements="469" coveredstatements="469" conditionals="272" coveredconditionals="258" methods="90" coveredmethods="89" elements="831" coveredelements="816" complexity="0" loc="469" ncloc="469" packages="8" files="21" classes="21"/>
<coverage generated="1645452508201" clover="3.2.0">
<project timestamp="1645452508202" name="All files">
<metrics statements="473" coveredstatements="473" conditionals="276" coveredconditionals="266" methods="90" coveredmethods="89" elements="839" coveredelements="828" complexity="0" loc="473" ncloc="473" packages="8" files="21" classes="21"/>
<package name="examples.autocomplete">
<metrics statements="5" coveredstatements="5" conditionals="1" coveredconditionals="1" methods="2" coveredmethods="2"/>
<file name="data.ts" path="/Users/donnie/src/github.com/floodfx/liveviewjs/src/examples/autocomplete/data.ts">
Expand Down Expand Up @@ -56,7 +56,7 @@
</file>
</package>
<package name="server">
<metrics statements="76" coveredstatements="76" conditionals="40" coveredconditionals="31" methods="13" coveredmethods="12"/>
<metrics statements="80" coveredstatements="80" conditionals="44" coveredconditionals="39" methods="13" coveredmethods="12"/>
<file name="index.ts" path="/Users/donnie/src/github.com/floodfx/liveviewjs/src/server/index.ts">
<metrics statements="5" coveredstatements="5" conditionals="0" coveredconditionals="0" methods="0" coveredmethods="0"/>
<line num="1" count="4" type="stmt"/>
Expand All @@ -66,7 +66,7 @@
<line num="5" count="4" type="stmt"/>
</file>
<file name="live_view_server.ts" path="/Users/donnie/src/github.com/floodfx/liveviewjs/src/server/live_view_server.ts">
<metrics statements="71" coveredstatements="71" conditionals="40" coveredconditionals="31" methods="13" coveredmethods="12"/>
<metrics statements="75" coveredstatements="75" conditionals="44" coveredconditionals="39" methods="13" coveredmethods="12"/>
<line num="2" count="4" type="stmt"/>
<line num="3" count="4" type="stmt"/>
<line num="4" count="4" type="stmt"/>
Expand All @@ -76,68 +76,72 @@
<line num="8" count="4" type="stmt"/>
<line num="9" count="4" type="stmt"/>
<line num="10" count="4" type="stmt"/>
<line num="36" count="4" type="stmt"/>
<line num="38" count="4" type="stmt"/>
<line num="39" count="8" type="stmt"/>
<line num="40" count="8" type="stmt"/>
<line num="42" count="8" type="stmt"/>
<line num="45" count="8" type="stmt"/>
<line num="47" count="8" type="stmt"/>
<line num="48" count="8" type="stmt"/>
<line num="49" count="8" type="stmt"/>
<line num="56" count="8" type="cond" truecount="3" falsecount="1"/>
<line num="57" count="8" type="cond" truecount="3" falsecount="1"/>
<line num="58" count="8" type="cond" truecount="3" falsecount="1"/>
<line num="59" count="8" type="cond" truecount="1" falsecount="1"/>
<line num="60" count="8" type="cond" truecount="3" falsecount="1"/>
<line num="61" count="8" type="stmt"/>
<line num="62" count="8" type="stmt"/>
<line num="63" count="8" type="stmt"/>
<line num="64" count="8" type="stmt"/>
<line num="67" count="8" type="stmt"/>
<line num="71" count="4" type="stmt"/>
<line num="75" count="6" type="stmt"/>
<line num="79" count="1" type="stmt"/>
<line num="83" count="3" type="stmt"/>
<line num="87" count="10" type="cond" truecount="2" falsecount="0"/>
<line num="88" count="2" type="stmt"/>
<line num="89" count="2" type="stmt"/>
<line num="91" count="8" type="stmt"/>
<line num="95" count="8" type="stmt"/>
<line num="98" count="8" type="stmt"/>
<line num="100" count="2" type="stmt"/>
<line num="102" count="2" type="stmt"/>
<line num="103" count="1" type="stmt"/>
<line num="107" count="8" type="stmt"/>
<line num="108" count="4" type="stmt"/>
<line num="114" count="12" type="cond" truecount="2" falsecount="0"/>
<line num="115" count="4" type="stmt"/>
<line num="116" count="4" type="stmt"/>
<line num="118" count="8" type="stmt"/>
<line num="120" count="8" type="stmt"/>
<line num="121" count="8" type="stmt"/>
<line num="125" count="8" type="stmt"/>
<line num="127" count="8" type="stmt"/>
<line num="129" count="8" type="stmt"/>
<line num="130" count="8" type="stmt"/>
<line num="132" count="8" type="stmt"/>
<line num="142" count="8" type="stmt"/>
<line num="144" count="8" type="stmt"/>
<line num="145" count="2" type="stmt"/>
<line num="147" count="2" type="stmt"/>
<line num="150" count="2" type="stmt"/>
<line num="151" count="2" type="stmt"/>
<line num="161" count="2" type="stmt"/>
<line num="162" count="2" type="cond" truecount="1" falsecount="0"/>
<line num="165" count="1" type="stmt"/>
<line num="166" count="1" type="stmt"/>
<line num="170" count="1" type="cond" truecount="1" falsecount="0"/>
<line num="171" count="1" type="stmt"/>
<line num="37" count="4" type="stmt"/>
<line num="39" count="4" type="stmt"/>
<line num="40" count="11" type="stmt"/>
<line num="41" count="11" type="stmt"/>
<line num="43" count="11" type="stmt"/>
<line num="46" count="11" type="stmt"/>
<line num="47" count="11" type="stmt"/>
<line num="48" count="11" type="stmt"/>
<line num="49" count="11" type="stmt"/>
<line num="51" count="11" type="stmt"/>
<line num="58" count="11" type="cond" truecount="3" falsecount="1"/>
<line num="59" count="11" type="cond" truecount="3" falsecount="1"/>
<line num="60" count="11" type="cond" truecount="3" falsecount="1"/>
<line num="61" count="11" type="cond" truecount="1" falsecount="1"/>
<line num="62" count="11" type="cond" truecount="3" falsecount="1"/>
<line num="63" count="11" type="cond" truecount="4" falsecount="0"/>
<line num="64" count="11" type="stmt"/>
<line num="65" count="11" type="stmt"/>
<line num="66" count="11" type="stmt"/>
<line num="69" count="11" type="stmt"/>
<line num="70" count="11" type="stmt"/>
<line num="74" count="4" type="stmt"/>
<line num="78" count="6" type="stmt"/>
<line num="82" count="1" type="stmt"/>
<line num="86" count="5" type="stmt"/>
<line num="90" count="13" type="cond" truecount="2" falsecount="0"/>
<line num="91" count="2" type="stmt"/>
<line num="92" count="2" type="stmt"/>
<line num="94" count="11" type="stmt"/>
<line num="98" count="11" type="stmt"/>
<line num="101" count="11" type="stmt"/>
<line num="103" count="2" type="stmt"/>
<line num="105" count="2" type="stmt"/>
<line num="106" count="1" type="stmt"/>
<line num="110" count="11" type="stmt"/>
<line num="111" count="7" type="stmt"/>
<line num="117" count="15" type="cond" truecount="2" falsecount="0"/>
<line num="118" count="4" type="stmt"/>
<line num="119" count="4" type="stmt"/>
<line num="121" count="11" type="stmt"/>
<line num="123" count="11" type="stmt"/>
<line num="124" count="11" type="stmt"/>
<line num="128" count="11" type="stmt"/>
<line num="130" count="11" type="stmt"/>
<line num="132" count="11" type="stmt"/>
<line num="133" count="11" type="stmt"/>
<line num="136" count="11" type="stmt"/>
<line num="146" count="11" type="stmt"/>
<line num="147" count="1" type="stmt"/>
<line num="151" count="11" type="stmt"/>
<line num="153" count="11" type="stmt"/>
<line num="154" count="4" type="stmt"/>
<line num="156" count="4" type="stmt"/>
<line num="159" count="4" type="stmt"/>
<line num="160" count="4" type="stmt"/>
<line num="170" count="4" type="stmt"/>
<line num="171" count="4" type="cond" truecount="1" falsecount="0"/>
<line num="174" count="1" type="stmt"/>
<line num="179" count="1" type="stmt"/>
<line num="180" count="1" type="stmt"/>
<line num="183" count="1" type="stmt"/>
<line num="195" count="8" type="stmt"/>
<line num="175" count="1" type="stmt"/>
<line num="179" count="3" type="cond" truecount="1" falsecount="0"/>
<line num="180" count="3" type="stmt"/>
<line num="183" count="3" type="stmt"/>
<line num="188" count="3" type="stmt"/>
<line num="189" count="3" type="stmt"/>
<line num="192" count="3" type="stmt"/>
<line num="204" count="11" type="stmt"/>
</file>
</package>
<package name="server.component">
Expand Down Expand Up @@ -302,8 +306,8 @@
<metrics statements="59" coveredstatements="59" conditionals="24" coveredconditionals="24" methods="6" coveredmethods="6"/>
<line num="4" count="4" type="stmt"/>
<line num="6" count="4" type="stmt"/>
<line num="8" count="22" type="stmt"/>
<line num="9" count="22" type="stmt"/>
<line num="8" count="25" type="stmt"/>
<line num="9" count="25" type="stmt"/>
<line num="13" count="20" type="stmt"/>
<line num="16" count="20" type="cond" truecount="5" falsecount="0"/>
<line num="17" count="19" type="stmt"/>
Expand Down Expand Up @@ -434,16 +438,16 @@
<line num="25" count="1" type="stmt"/>
<line num="28" count="6" type="stmt"/>
<line num="31" count="12" type="stmt"/>
<line num="32" count="26" type="stmt"/>
<line num="32" count="30" type="stmt"/>
<line num="35" count="12" type="stmt"/>
<line num="36" count="284" type="cond" truecount="1" falsecount="0"/>
<line num="37" count="42" type="stmt"/>
<line num="39" count="242" type="cond" truecount="1" falsecount="0"/>
<line num="36" count="294" type="cond" truecount="1" falsecount="0"/>
<line num="37" count="46" type="stmt"/>
<line num="39" count="248" type="cond" truecount="1" falsecount="0"/>
<line num="40" count="1" type="stmt"/>
<line num="42" count="241" type="stmt"/>
<line num="42" count="247" type="stmt"/>
<line num="59" count="12" type="stmt"/>
<line num="65" count="178" type="stmt"/>
<line num="66" count="178" type="stmt"/>
<line num="65" count="186" type="stmt"/>
<line num="66" count="186" type="stmt"/>
<line num="71" count="103" type="cond" truecount="1" falsecount="0"/>
<line num="72" count="49" type="cond" truecount="1" falsecount="0"/>
<line num="73" count="1" type="stmt"/>
Expand All @@ -462,11 +466,11 @@
<line num="123" count="54" type="cond" truecount="1" falsecount="0"/>
<line num="124" count="35" type="stmt"/>
<line num="126" count="54" type="stmt"/>
<line num="130" count="82" type="stmt"/>
<line num="131" count="187" type="stmt"/>
<line num="132" count="187" type="stmt"/>
<line num="130" count="90" type="stmt"/>
<line num="131" count="197" type="stmt"/>
<line num="132" count="197" type="stmt"/>
<line num="138" count="12" type="stmt"/>
<line num="139" count="143" type="stmt"/>
<line num="139" count="147" type="stmt"/>
</file>
</package>
<package name="server.templates.helpers">
Expand Down Expand Up @@ -526,10 +530,10 @@
<metrics statements="6" coveredstatements="6" conditionals="16" coveredconditionals="16" methods="1" coveredmethods="1"/>
<line num="1" count="7" type="stmt"/>
<line num="8" count="7" type="stmt"/>
<line num="9" count="4" type="cond" truecount="4" falsecount="0"/>
<line num="10" count="4" type="cond" truecount="2" falsecount="0"/>
<line num="11" count="4" type="cond" truecount="2" falsecount="0"/>
<line num="12" count="4" type="cond" truecount="8" falsecount="0"/>
<line num="9" count="6" type="cond" truecount="4" falsecount="0"/>
<line num="10" count="6" type="cond" truecount="2" falsecount="0"/>
<line num="11" count="6" type="cond" truecount="2" falsecount="0"/>
<line num="12" count="6" type="cond" truecount="8" falsecount="0"/>
</file>
<file name="options_for_select.ts" path="/Users/donnie/src/github.com/floodfx/liveviewjs/src/server/templates/helpers/options_for_select.ts">
<metrics statements="16" coveredstatements="16" conditionals="11" coveredconditionals="11" methods="8" coveredmethods="8"/>
Expand Down
8 changes: 4 additions & 4 deletions coverage/coverage-final.json

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions docs/server_options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## `LiveViewServer` Options

The `LiveViewServer` constructor takes an a `LiveViewServerOptions` object which can be used to customize the server. The only required option is `signingSecret` which is used to sign the session cookie as well as the JWT tokens sent to the client.

```ts
export interface LiveViewServerOptions {
port?: number;
rootView?: string;
viewsPath?: string;
publicPath?: string;
sessionStore?: session.Store;
pageTitleDefaults?: PageTitleDefaults;
middleware?: express.Handler[];
signingSecret: string;
}
```
### Details on each option:
* `port`: `number` - The port to listen on. Defaults to `4444`.
* `rootView`: `string` - The [ejs](https://ejs.co/) template that wraps each `LiveViewComponent`s render output. Defaults to `src/server/web/views/root.html.ejs`.
* `viewsPath`: `string` - The path to the directory containing views (ejs templates) for your application. The webserver will look for views here including your `rootView` if that has been specified.
* `publicPath`: `string` - The path to the directory containing static files for your application. The webserver will serve static files from here.
* `sessionStore`: `session.Store` - The session store to use. Defaults to `express-session`'s in-memory store. See [express store implementations](https://github.com/expressjs/session#session-store-implementation) for more details.
* `pageTitleDefaults`: `PageTitleDefaults` - The default page title to use for each `LiveViewComponent`. You can set a `prefix`, `suffix`, and default title. The default is empty strings for each.
* `middleware`: `express.Handler[]` - An array of middleware to add to the express server. This is useful for adding logging, authentication, or other middleware. See [express middleware](https://expressjs.com/en/guide/using-middleware.html) for more details.


1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module.exports = {
coverageDirectory: 'coverage',
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
coveragePathIgnorePatterns: ['/node_modules/', '/dist/'],
coverageReporters: ['json', 'clover'],
roots: ['<rootDir>/src'],
verbose: true,
coverageThreshold: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "liveviewjs",
"version": "0.0.7",
"version": "0.0.8",
"description": "LiveViewJS brings the power of Phoenix LiveView to Typescript and Javascript developers and applications.",
"targets": {
"client": {
Expand Down
9 changes: 8 additions & 1 deletion src/examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ const lvServer = new LiveViewServer({
pageTitleDefaults: {
title: "Examples",
suffix: " · LiveViewJS"
}
},
middleware: [
// debugging middleware example
(req, res, next) => {
console.log(`${req.method} ${req.url} - ${new Date().toISOString()}`);
next()
}
]
});


Expand Down
41 changes: 39 additions & 2 deletions src/server/live_view_server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ describe("test live view server", () => {
beforeEach(() => {
lvServer = new LiveViewServer({
signingSecret: "MY_VERY_SECRET_KEY",
port: 7654
})
port: 7654,
pageTitleDefaults: {
prefix: "TitlePrefix - ",
suffix: " - TitleSuffix",
title: "Title",
}
});
httpServer = lvServer.httpServer;
lvServer.start();
})
Expand Down Expand Up @@ -66,6 +71,16 @@ describe("test live view server", () => {
})
})

it("http request contains live title components", (done) => {
const lvComponent = new LiveViewComponent()
lvServer.registerLiveViewRoute("/test", lvComponent)
request(lvServer.httpServer).get('/test').expect(200).then(res => {
console.log("test title", res.text)
expect(res.text).toContain("<title data-prefix=\"TitlePrefix - \" data-suffix=\" - TitleSuffix\">TitlePrefix - Title - TitleSuffix</title>")
done();
})
})

it("http 404s on unknown route", (done) => {
lvServer.start();
request(lvServer.httpServer).get('/unknwon').then(res => {
Expand Down Expand Up @@ -111,6 +126,28 @@ describe("test live view server", () => {
.expectClosed()
})

it("middleware is applied", (done) => {
let middlewareCalled = false;
const testLVServer = new LiveViewServer({
middleware: [(req, res, next) => {
middlewareCalled = true;
next();
}],
signingSecret: "test",
port: 7655
})

const lvComponent = new LiveViewComponent()
testLVServer.registerLiveViewRoute("/test", lvComponent)
testLVServer.start()
request(testLVServer.httpServer).get('/test').expect(200).then(res => {
// expect(res.text).toContain(lvComponent.render().toString())
expect(middlewareCalled).toBe(true);
testLVServer.shutdown();
done();
})
})

})

class LiveViewComponent extends BaseLiveViewComponent<{}, {}> {
Expand Down
Loading

0 comments on commit 4645e8a

Please sign in to comment.