diff options
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/context.model.ts | 6 | ||||
-rw-r--r-- | server/src/operations.ts | 60 | ||||
-rw-r--r-- | server/src/server.test.ts | 36 | ||||
-rw-r--r-- | server/src/server.ts | 22 |
4 files changed, 62 insertions, 62 deletions
diff --git a/server/src/context.model.ts b/server/src/context.model.ts index d8419d4..cedd675 100644 --- a/server/src/context.model.ts +++ b/server/src/context.model.ts @@ -1,5 +1,5 @@ export interface Context { - httpPort: number; - wsPort: number; - logger: (e: any) => any; + httpPort: number + wsPort: number + logger: (e: any) => any } diff --git a/server/src/operations.ts b/server/src/operations.ts index 6802ee7..c0fec8d 100644 --- a/server/src/operations.ts +++ b/server/src/operations.ts @@ -1,56 +1,56 @@ -import { Context } from "./context.model"; -import { SubscriptionRequest } from "../../lib/subscription-req.model"; -import { WriteRequest } from "../../lib/write-req.model"; -import { DatabaseChange } from "../../lib/database-change.model"; +import { Context } from './context.model' +import { SubscriptionRequest } from '../../lib/subscription-req.model' +import { WriteRequest } from '../../lib/write-req.model' +import { DatabaseChange } from '../../lib/database-change.model' -import { DatabaseInterface } from "naive-core"; +import { DatabaseInterface } from 'naive-core' -import express, { RequestHandler } from "express"; -import bodyParser from "body-parser"; +import express, { RequestHandler } from 'express' +import bodyParser from 'body-parser' export const bindOperations = ( ctx: Context, db: DatabaseInterface, send: (e: DatabaseChange) => Promise<any> ) => { - const unsubs: { [key: string]: () => any } = {}; + const unsubs: { [key: string]: () => any } = {} const writeHandler: RequestHandler = async (req, res) => { - const { path, toWrite } = req.body as WriteRequest; - await db.write(path, toWrite); - res.status(200).end(); - }; + const { path, toWrite } = req.body as WriteRequest + await db.write(path, toWrite) + res.status(200).end() + } const addSubHandler: RequestHandler = async (req, res) => { - const { path } = req.body as SubscriptionRequest; + const { path } = req.body as SubscriptionRequest // only allow users to subscribe // to a given path once // TODO return a better error if (unsubs[path]) { - res.status(500).end(); - return; + res.status(500).end() + return } unsubs[path] = db.subscribe(path, async (change: Object) => { await send({ path, change - }); - }); - res.status(200).end(); - }; + }) + }) + res.status(200).end() + } const removeSubHandler: RequestHandler = async (req, res) => { - const { path } = req.body as SubscriptionRequest; - const unsub = unsubs[path]; - if (unsub) unsub(); - }; + const { path } = req.body as SubscriptionRequest + const unsub = unsubs[path] + if (unsub) unsub() + } - const router = express(); - router.use(bodyParser.json()); - router.post("/write", writeHandler); + const router = express() + router.use(bodyParser.json()) + router.post('/write', writeHandler) router - .route("/subscribe") + .route('/subscribe') .post(addSubHandler) - .delete(removeSubHandler); - router.listen(ctx.httpPort, () => ctx.logger("HTTPS server started")); -}; + .delete(removeSubHandler) + router.listen(ctx.httpPort, () => ctx.logger('HTTPS server started')) +} diff --git a/server/src/server.test.ts b/server/src/server.test.ts index 2bd2e45..b4a308f 100644 --- a/server/src/server.test.ts +++ b/server/src/server.test.ts @@ -1,31 +1,31 @@ -import { runServer } from "./server"; -import process from "process"; +import { runServer } from './server' +import process from 'process' // We test the server using the client. // It would be better to import this // from a linked npm package, // but that requires rebuilding each time // so fuck it for now -import { dbFactory, DatabaseConnection } from "../../client/src"; +import { dbFactory, DatabaseConnection } from '../../client/src' -const port = +(process.env.PORT || 5005); -const httpPort = port; -const wsPort = port + 1; +const port = +(process.env.PORT || 5005) +const httpPort = port +const wsPort = port + 1 -describe("Server module", async () => { - let db: DatabaseConnection; +describe('Server module', async () => { + let db: DatabaseConnection beforeAll(async () => { await runServer({ httpPort, wsPort, logger: console.log - }); - db = dbFactory({ wsPort, httpUrl: `http://localhost:${httpPort}` }); - await db.init(); - }); - test("it should work", async () => { - await db.write("/hello/world", { - my: "data" - }); - }); -}); + }) + db = dbFactory({ wsPort, httpUrl: `http://localhost:${httpPort}` }) + await db.init() + }) + test('it should work', async () => { + await db.write('/hello/world', { + my: 'data' + }) + }) +}) diff --git a/server/src/server.ts b/server/src/server.ts index f80bd2b..459bc69 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -1,18 +1,18 @@ -import { Context } from "./context.model"; -import { bindOperations } from "./operations"; -import { DatabaseChange } from "../../lib/database-change.model"; -import WebSocket from "ws"; +import { Context } from './context.model' +import { bindOperations } from './operations' +import { DatabaseChange } from '../../lib/database-change.model' +import WebSocket from 'ws' -import { Database, Context as CoreContext } from "naive-core"; +import { Database, Context as CoreContext } from 'naive-core' export const runServer = async (ctx: Context) => { - const db = new Database(); - await db.init(); - const wss = new WebSocket.Server({ port: ctx.wsPort }); + const db = new Database() + await db.init() + const wss = new WebSocket.Server({ port: ctx.wsPort }) bindOperations(ctx, db, async (dbChange: DatabaseChange) => { for (let client of wss.clients) { - client.send(dbChange); + client.send(dbChange) } - }); -}; + }) +} |