Skip to content

Commit

Permalink
add support for middleware in express; revert coverage but add html o…
Browse files Browse the repository at this point in the history
…utput to .gitignore; add test for live title helper
floodfx committed Feb 21, 2022

Verified

This commit was signed with the committer’s verified signature.
booc0mtaco Holloway
1 parent 0383063 commit ffab1d5
Showing 7 changed files with 153 additions and 95 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -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">
@@ -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"/>
@@ -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"/>
@@ -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">
@@ -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"/>
@@ -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"/>
@@ -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">
@@ -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"/>
8 changes: 4 additions & 4 deletions coverage/coverage-final.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -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: {
9 changes: 8 additions & 1 deletion src/examples/index.ts
Original file line number Diff line number Diff line change
@@ -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()
}
]
});


41 changes: 39 additions & 2 deletions src/server/live_view_server.test.ts
Original file line number Diff line number Diff line change
@@ -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();
})
@@ -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 => {
@@ -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<{}, {}> {
15 changes: 12 additions & 3 deletions src/server/live_view_server.ts
Original file line number Diff line number Diff line change
@@ -28,9 +28,10 @@ export interface LiveViewServerOptions {
rootView?: string;
viewsPath?: string;
publicPath?: string;
signingSecret: string;
sessionStore?: session.Store;
pageTitleDefaults?: PageTitleDefaults;
middleware?: express.Handler[];
signingSecret: string;
}

const MODULE_VIEWS_PATH = path.join(__dirname, "web", "views");
@@ -43,11 +44,12 @@ export class LiveViewServer {
private viewsPath: string[];
private signingSecret: string;
private sessionStore: session.Store = new MemoryStore();

private _router: LiveViewRouter = {};
private messageRouter = new MessageRouter()
private _isStarted = false;
private pageTitleDefaults?: PageTitleDefaults;
private middleware: express.Handler[] = [];

readonly httpServer: Server;
readonly socketServer: WebSocket.Server;
expressApp: express.Application;
@@ -58,13 +60,14 @@ export class LiveViewServer {
this.publicPath = options.publicPath ?? this.publicPath;
this.viewsPath = options.viewsPath ? [options.viewsPath, MODULE_VIEWS_PATH] : [MODULE_VIEWS_PATH];
this.sessionStore = options.sessionStore ?? this.sessionStore;
this.middleware = options.middleware ?? this.middleware
this.signingSecret = options.signingSecret;
this.expressApp = this.buildExpressApp();
this.httpServer = new Server();
this.socketServer = new WebSocket.Server({
server: this.httpServer
});
this.pageTitleDefaults = options.pageTitleDefaults;
this.expressApp = this.buildExpressApp();
}

get router(): LiveViewRouter {
@@ -129,6 +132,7 @@ export class LiveViewServer {
app.set('view engine', 'ejs');
app.set("views", this.viewsPath)

// setup session
app.use(session({
secret: this.signingSecret,
resave: false,
@@ -138,6 +142,11 @@ export class LiveViewServer {
store: this.sessionStore,
}))

// register middleware
for (const middleware of this.middleware) {
app.use(middleware)
}

// register live_title_tag helper
app.locals.live_title_tag = live_title_tag;

0 comments on commit ffab1d5

Please sign in to comment.