diff --git a/.env b/.env new file mode 100644 index 0000000..7440cf5 --- /dev/null +++ b/.env @@ -0,0 +1,4 @@ +PORT = 5000 +ADMIN_KEY = test +ACCESS_LOG = true +DEV = true \ No newline at end of file diff --git a/src/config.ts b/src/config.ts index d5571db..6d5659f 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,5 +1,7 @@ import Logging from "@hibas123/nodelogging"; import * as dotenv from "dotenv"; +dotenv.config() + interface IConfig { port: number; @@ -15,6 +17,4 @@ const config: IConfig = { dev: (process.env.DEV || "").toLowerCase() === "true" } -dotenv.config() - export default config; \ No newline at end of file diff --git a/src/connection.ts b/src/connection.ts index a29f22c..a9f7490 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -4,6 +4,7 @@ import { DatabaseManager } from "./database/database"; import Logging from "@hibas123/logging"; import Query from "./database/query"; import Session from "./database/session"; +import shortid = require("shortid"); type QueryTypes = "get" | "set" | "push" | "subscribe" | "unsubscribe"; @@ -16,6 +17,7 @@ export class ConnectionManager { } private static onConnection(socket: io.Socket) { + Logging.debug("New Connection:", socket.id); const reqMap = new Map(); const stored = new Map(); const session = new Session(); @@ -62,14 +64,19 @@ export class ConnectionManager { case "subscribe": if (!perms.read) throw noperm; + + let subscriptionID = shortid.generate(); + query.subscribe(data, (data) => { - answer(id, data); + socket.emit("event", subscriptionID, data); }); stored.set(id, query); + answer(id, subscriptionID); return; case "unsubscribe": query.unsubscribe(); stored.delete(id); + answer(id, true); return; } diff --git a/src/database/query.ts b/src/database/query.ts index 3615acb..1526305 100644 --- a/src/database/query.ts +++ b/src/database/query.ts @@ -230,7 +230,7 @@ export default class Query { if (type === change.type) { Logging.debug("Path", this.path, change.path); - if (this.path.length === change.path.length - (type === ChangeTypes.PUSH ? 1 : 0)) { + if (this.path.length <= change.path.length - (type === ChangeTypes.PUSH ? 1 : 0)) { let valid = true; for (let i = 0; i < this.path.length; i++) { if (this.path[i] !== change.path[i]) {