diff --git a/package-lock.json b/package-lock.json index 7d052d4..b995491 100644 --- a/package-lock.json +++ b/package-lock.json @@ -44,6 +44,7 @@ "version": "1.3.5", "resolved": "https://registry.npmjs.org/@types/accepts/-/accepts-1.3.5.tgz", "integrity": "sha512-jOdnI/3qTpHABjM5cx1Hc0sKsPoYCp+DP/GJRGtDlPd7fiV9oXGGIcjW/ZOxLIvjGz8MA+uMZI9metHlgqbgwQ==", + "dev": true, "requires": { "@types/node": "*" } @@ -52,6 +53,7 @@ "version": "1.17.1", "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.17.1.tgz", "integrity": "sha512-RoX2EZjMiFMjZh9lmYrwgoP9RTpAjSHiJxdp4oidAQVO02T7HER3xj9UKue5534ULWeqVEkujhWcyvUce+d68w==", + "dev": true, "requires": { "@types/connect": "*", "@types/node": "*" @@ -61,6 +63,7 @@ "version": "3.4.32", "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.32.tgz", "integrity": "sha512-4r8qa0quOvh7lGD0pre62CAb1oni1OO6ecJLGCezTmhQ8Fz50Arx9RUszryR8KlgK6avuSXvviL6yWyViQABOg==", + "dev": true, "requires": { "@types/node": "*" } @@ -69,6 +72,7 @@ "version": "0.7.4", "resolved": "https://registry.npmjs.org/@types/cookies/-/cookies-0.7.4.tgz", "integrity": "sha512-oTGtMzZZAVuEjTwCjIh8T8FrC8n/uwy+PG0yTvQcdZ7etoel7C7/3MSd7qrukENTgQtotG7gvBlBojuVs7X5rw==", + "dev": true, "requires": { "@types/connect": "*", "@types/express": "*", @@ -94,6 +98,7 @@ "version": "4.17.1", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.1.tgz", "integrity": "sha512-VfH/XCP0QbQk5B5puLqTLEeFgR8lfCJHZJKkInZ9mkYd+u8byX0kztXEQxEk4wZXJs8HI+7km2ALXjn4YKcX9w==", + "dev": true, "requires": { "@types/body-parser": "*", "@types/express-serve-static-core": "*", @@ -104,6 +109,7 @@ "version": "4.16.9", "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.16.9.tgz", "integrity": "sha512-GqpaVWR0DM8FnRUJYKlWgyARoBUAVfRIeVDZQKOttLFp5SmhhF9YFIYeTPwMd/AXfxlP7xVO2dj1fGu0Q+krKQ==", + "dev": true, "requires": { "@types/node": "*", "@types/range-parser": "*" @@ -121,7 +127,8 @@ "@types/http-assert": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/@types/http-assert/-/http-assert-1.5.1.tgz", - "integrity": "sha512-PGAK759pxyfXE78NbKxyfRcWYA/KwW17X290cNev/qAsn9eQIxkH4shoNBafH37wewhDG/0p1cHPbK6+SzZjWQ==" + "integrity": "sha512-PGAK759pxyfXE78NbKxyfRcWYA/KwW17X290cNev/qAsn9eQIxkH4shoNBafH37wewhDG/0p1cHPbK6+SzZjWQ==", + "dev": true }, "@types/ini": { "version": "1.3.30", @@ -141,12 +148,14 @@ "@types/keygrip": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/@types/keygrip/-/keygrip-1.0.1.tgz", - "integrity": "sha1-/1QEYtL7TQqIRBzq8n0oewHD2Hg=" + "integrity": "sha1-/1QEYtL7TQqIRBzq8n0oewHD2Hg=", + "dev": true }, "@types/koa": { "version": "2.0.50", "resolved": "https://registry.npmjs.org/@types/koa/-/koa-2.0.50.tgz", "integrity": "sha512-TcgOD2lh0EISSadAk1DOBYw7kNoY9XdeB3vEMOKiDDaTMYm+V54nyPsU7Ulb/htb5OBIR79RgTeCWntCcophLw==", + "dev": true, "requires": { "@types/accepts": "*", "@types/cookies": "*", @@ -160,6 +169,7 @@ "version": "3.2.4", "resolved": "https://registry.npmjs.org/@types/koa-compose/-/koa-compose-3.2.4.tgz", "integrity": "sha512-ioou0rxkuWL+yBQYsHUQAzRTfVxAg8Y2VfMftU+Y3RA03/MzuFL0x/M2sXXj3PkfnENbHsjeHR1aMdezLYpTeA==", + "dev": true, "requires": { "@types/koa": "*" } @@ -168,6 +178,7 @@ "version": "7.0.42", "resolved": "https://registry.npmjs.org/@types/koa-router/-/koa-router-7.0.42.tgz", "integrity": "sha512-mggrNY7Ywwjt7QjaMAlbb1ixE+v7AFskOeyKdmZT/NvPVEAo48gYUxIcF8ILlMc3eg1bo6SxNoUcbxhTv7edrA==", + "dev": true, "requires": { "@types/koa": "*" } @@ -195,7 +206,8 @@ "@types/mime": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/@types/mime/-/mime-2.0.1.tgz", - "integrity": "sha512-FwI9gX75FgVBJ7ywgnq/P7tw+/o1GUbtP0KzbtusLigAOgIgNISRK0ZPl4qertvXSIE8YbsVJueQ90cDt9YYyw==" + "integrity": "sha512-FwI9gX75FgVBJ7ywgnq/P7tw+/o1GUbtP0KzbtusLigAOgIgNISRK0ZPl4qertvXSIE8YbsVJueQ90cDt9YYyw==", + "dev": true }, "@types/node": { "version": "12.7.12", @@ -205,12 +217,14 @@ "@types/range-parser": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.3.tgz", - "integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA==" + "integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA==", + "dev": true }, "@types/serve-static": { "version": "1.13.3", "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.3.tgz", "integrity": "sha512-oprSwp094zOglVrXdlo/4bAHtKTAxX6VT8FOZlBKrmyLbNvE1zxZyJ6yikMVtHIvwP45+ZQGJn+FdXGKTozq0g==", + "dev": true, "requires": { "@types/express-serve-static-core": "*", "@types/mime": "*" diff --git a/src/connection.ts b/src/connection.ts index a5a7710..5b7eeab 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -33,7 +33,7 @@ export class ConnectionManager { private static async onConnection(socket: WebSocket, req: IncomingMessage) { Logging.log("New Connection:"); - const sendError = (msg: string) => socket.send(JSON.stringify({ ns: "error_msg", args: [msg] })); + const sendError = (error: string) => socket.send(JSON.stringify({ ns: "error_msg", data: { error } })); const session = new Session(); @@ -78,12 +78,14 @@ export class ConnectionManager { } const stored = new Map(); - const answer = (id: string, data: any, err: boolean = false) => { - socket.send(JSON.stringify({ ns: "message", args: [id, err, data] })); + const answer = (id: string, data: any, error: boolean = false) => { + socket.send(JSON.stringify({ ns: "message", data: { id, error, data } })); } - const handler = new Map void)>(); - handler.set("query", async (id: string, type: QueryTypes, path: string[], data: any) => { + const handler = new Map void)>(); + type QueryData = { id: string, type: QueryTypes, path: string[], data: any }; + handler.set("query", async ({ id, type, path, data }: QueryData) => { + //TODO: Handle case with no id, type, path Logging.debug(`Request with id '${id}' from type '${type}' and path '${path}' with data`, data) try { const perms = db.rules.hasPermission(path, session); @@ -116,7 +118,7 @@ export class ConnectionManager { let subscriptionID = shortid.generate(); query.subscribe(data, (data) => { - socket.send(JSON.stringify({ ns: "event", args: [subscriptionID, data] })); + socket.send(JSON.stringify({ ns: "event", data: { id: subscriptionID, data } })); }); stored.set(id, query); answer(id, subscriptionID); @@ -136,11 +138,16 @@ export class ConnectionManager { } }) - socket.on("message", (rawData: string) => { - let data: { ns: string, args: any[] } = JSON.parse(rawData); - let h = handler.get(data.ns); - if (h) { - h(...data.args); + socket.on("message", async (rawData: string) => { + try { + let message: { ns: string, data: any } = JSON.parse(rawData); + let h = handler.get(message.ns); + if (h) { + h(message.data); + } + } catch (err) { + Logging.errorMessage("Unknown Error:"); + Logging.error(err); } })