Changing the way Requests work

This commit is contained in:
Fabian Stamm 2019-10-11 09:12:24 +02:00
parent 8b6767c5f9
commit 715b392629
2 changed files with 36 additions and 15 deletions

22
package-lock.json generated
View File

@ -44,6 +44,7 @@
"version": "1.3.5", "version": "1.3.5",
"resolved": "https://registry.npmjs.org/@types/accepts/-/accepts-1.3.5.tgz", "resolved": "https://registry.npmjs.org/@types/accepts/-/accepts-1.3.5.tgz",
"integrity": "sha512-jOdnI/3qTpHABjM5cx1Hc0sKsPoYCp+DP/GJRGtDlPd7fiV9oXGGIcjW/ZOxLIvjGz8MA+uMZI9metHlgqbgwQ==", "integrity": "sha512-jOdnI/3qTpHABjM5cx1Hc0sKsPoYCp+DP/GJRGtDlPd7fiV9oXGGIcjW/ZOxLIvjGz8MA+uMZI9metHlgqbgwQ==",
"dev": true,
"requires": { "requires": {
"@types/node": "*" "@types/node": "*"
} }
@ -52,6 +53,7 @@
"version": "1.17.1", "version": "1.17.1",
"resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.17.1.tgz", "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.17.1.tgz",
"integrity": "sha512-RoX2EZjMiFMjZh9lmYrwgoP9RTpAjSHiJxdp4oidAQVO02T7HER3xj9UKue5534ULWeqVEkujhWcyvUce+d68w==", "integrity": "sha512-RoX2EZjMiFMjZh9lmYrwgoP9RTpAjSHiJxdp4oidAQVO02T7HER3xj9UKue5534ULWeqVEkujhWcyvUce+d68w==",
"dev": true,
"requires": { "requires": {
"@types/connect": "*", "@types/connect": "*",
"@types/node": "*" "@types/node": "*"
@ -61,6 +63,7 @@
"version": "3.4.32", "version": "3.4.32",
"resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.32.tgz", "resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.32.tgz",
"integrity": "sha512-4r8qa0quOvh7lGD0pre62CAb1oni1OO6ecJLGCezTmhQ8Fz50Arx9RUszryR8KlgK6avuSXvviL6yWyViQABOg==", "integrity": "sha512-4r8qa0quOvh7lGD0pre62CAb1oni1OO6ecJLGCezTmhQ8Fz50Arx9RUszryR8KlgK6avuSXvviL6yWyViQABOg==",
"dev": true,
"requires": { "requires": {
"@types/node": "*" "@types/node": "*"
} }
@ -69,6 +72,7 @@
"version": "0.7.4", "version": "0.7.4",
"resolved": "https://registry.npmjs.org/@types/cookies/-/cookies-0.7.4.tgz", "resolved": "https://registry.npmjs.org/@types/cookies/-/cookies-0.7.4.tgz",
"integrity": "sha512-oTGtMzZZAVuEjTwCjIh8T8FrC8n/uwy+PG0yTvQcdZ7etoel7C7/3MSd7qrukENTgQtotG7gvBlBojuVs7X5rw==", "integrity": "sha512-oTGtMzZZAVuEjTwCjIh8T8FrC8n/uwy+PG0yTvQcdZ7etoel7C7/3MSd7qrukENTgQtotG7gvBlBojuVs7X5rw==",
"dev": true,
"requires": { "requires": {
"@types/connect": "*", "@types/connect": "*",
"@types/express": "*", "@types/express": "*",
@ -94,6 +98,7 @@
"version": "4.17.1", "version": "4.17.1",
"resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.1.tgz", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.1.tgz",
"integrity": "sha512-VfH/XCP0QbQk5B5puLqTLEeFgR8lfCJHZJKkInZ9mkYd+u8byX0kztXEQxEk4wZXJs8HI+7km2ALXjn4YKcX9w==", "integrity": "sha512-VfH/XCP0QbQk5B5puLqTLEeFgR8lfCJHZJKkInZ9mkYd+u8byX0kztXEQxEk4wZXJs8HI+7km2ALXjn4YKcX9w==",
"dev": true,
"requires": { "requires": {
"@types/body-parser": "*", "@types/body-parser": "*",
"@types/express-serve-static-core": "*", "@types/express-serve-static-core": "*",
@ -104,6 +109,7 @@
"version": "4.16.9", "version": "4.16.9",
"resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.16.9.tgz", "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.16.9.tgz",
"integrity": "sha512-GqpaVWR0DM8FnRUJYKlWgyARoBUAVfRIeVDZQKOttLFp5SmhhF9YFIYeTPwMd/AXfxlP7xVO2dj1fGu0Q+krKQ==", "integrity": "sha512-GqpaVWR0DM8FnRUJYKlWgyARoBUAVfRIeVDZQKOttLFp5SmhhF9YFIYeTPwMd/AXfxlP7xVO2dj1fGu0Q+krKQ==",
"dev": true,
"requires": { "requires": {
"@types/node": "*", "@types/node": "*",
"@types/range-parser": "*" "@types/range-parser": "*"
@ -121,7 +127,8 @@
"@types/http-assert": { "@types/http-assert": {
"version": "1.5.1", "version": "1.5.1",
"resolved": "https://registry.npmjs.org/@types/http-assert/-/http-assert-1.5.1.tgz", "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": { "@types/ini": {
"version": "1.3.30", "version": "1.3.30",
@ -141,12 +148,14 @@
"@types/keygrip": { "@types/keygrip": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/@types/keygrip/-/keygrip-1.0.1.tgz", "resolved": "https://registry.npmjs.org/@types/keygrip/-/keygrip-1.0.1.tgz",
"integrity": "sha1-/1QEYtL7TQqIRBzq8n0oewHD2Hg=" "integrity": "sha1-/1QEYtL7TQqIRBzq8n0oewHD2Hg=",
"dev": true
}, },
"@types/koa": { "@types/koa": {
"version": "2.0.50", "version": "2.0.50",
"resolved": "https://registry.npmjs.org/@types/koa/-/koa-2.0.50.tgz", "resolved": "https://registry.npmjs.org/@types/koa/-/koa-2.0.50.tgz",
"integrity": "sha512-TcgOD2lh0EISSadAk1DOBYw7kNoY9XdeB3vEMOKiDDaTMYm+V54nyPsU7Ulb/htb5OBIR79RgTeCWntCcophLw==", "integrity": "sha512-TcgOD2lh0EISSadAk1DOBYw7kNoY9XdeB3vEMOKiDDaTMYm+V54nyPsU7Ulb/htb5OBIR79RgTeCWntCcophLw==",
"dev": true,
"requires": { "requires": {
"@types/accepts": "*", "@types/accepts": "*",
"@types/cookies": "*", "@types/cookies": "*",
@ -160,6 +169,7 @@
"version": "3.2.4", "version": "3.2.4",
"resolved": "https://registry.npmjs.org/@types/koa-compose/-/koa-compose-3.2.4.tgz", "resolved": "https://registry.npmjs.org/@types/koa-compose/-/koa-compose-3.2.4.tgz",
"integrity": "sha512-ioou0rxkuWL+yBQYsHUQAzRTfVxAg8Y2VfMftU+Y3RA03/MzuFL0x/M2sXXj3PkfnENbHsjeHR1aMdezLYpTeA==", "integrity": "sha512-ioou0rxkuWL+yBQYsHUQAzRTfVxAg8Y2VfMftU+Y3RA03/MzuFL0x/M2sXXj3PkfnENbHsjeHR1aMdezLYpTeA==",
"dev": true,
"requires": { "requires": {
"@types/koa": "*" "@types/koa": "*"
} }
@ -168,6 +178,7 @@
"version": "7.0.42", "version": "7.0.42",
"resolved": "https://registry.npmjs.org/@types/koa-router/-/koa-router-7.0.42.tgz", "resolved": "https://registry.npmjs.org/@types/koa-router/-/koa-router-7.0.42.tgz",
"integrity": "sha512-mggrNY7Ywwjt7QjaMAlbb1ixE+v7AFskOeyKdmZT/NvPVEAo48gYUxIcF8ILlMc3eg1bo6SxNoUcbxhTv7edrA==", "integrity": "sha512-mggrNY7Ywwjt7QjaMAlbb1ixE+v7AFskOeyKdmZT/NvPVEAo48gYUxIcF8ILlMc3eg1bo6SxNoUcbxhTv7edrA==",
"dev": true,
"requires": { "requires": {
"@types/koa": "*" "@types/koa": "*"
} }
@ -195,7 +206,8 @@
"@types/mime": { "@types/mime": {
"version": "2.0.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-2.0.1.tgz", "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": { "@types/node": {
"version": "12.7.12", "version": "12.7.12",
@ -205,12 +217,14 @@
"@types/range-parser": { "@types/range-parser": {
"version": "1.2.3", "version": "1.2.3",
"resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.3.tgz", "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": { "@types/serve-static": {
"version": "1.13.3", "version": "1.13.3",
"resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.3.tgz", "resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.13.3.tgz",
"integrity": "sha512-oprSwp094zOglVrXdlo/4bAHtKTAxX6VT8FOZlBKrmyLbNvE1zxZyJ6yikMVtHIvwP45+ZQGJn+FdXGKTozq0g==", "integrity": "sha512-oprSwp094zOglVrXdlo/4bAHtKTAxX6VT8FOZlBKrmyLbNvE1zxZyJ6yikMVtHIvwP45+ZQGJn+FdXGKTozq0g==",
"dev": true,
"requires": { "requires": {
"@types/express-serve-static-core": "*", "@types/express-serve-static-core": "*",
"@types/mime": "*" "@types/mime": "*"

View File

@ -33,7 +33,7 @@ export class ConnectionManager {
private static async onConnection(socket: WebSocket, req: IncomingMessage) { private static async onConnection(socket: WebSocket, req: IncomingMessage) {
Logging.log("New Connection:"); 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(); const session = new Session();
@ -78,12 +78,14 @@ export class ConnectionManager {
} }
const stored = new Map<string, Query>(); const stored = new Map<string, Query>();
const answer = (id: string, data: any, err: boolean = false) => { const answer = (id: string, data: any, error: boolean = false) => {
socket.send(JSON.stringify({ ns: "message", args: [id, err, data] })); socket.send(JSON.stringify({ ns: "message", data: { id, error, data } }));
} }
const handler = new Map<string, ((...args: any[]) => void)>(); const handler = new Map<string, ((data: any) => void)>();
handler.set("query", async (id: string, type: QueryTypes, path: string[], data: any) => { 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) Logging.debug(`Request with id '${id}' from type '${type}' and path '${path}' with data`, data)
try { try {
const perms = db.rules.hasPermission(path, session); const perms = db.rules.hasPermission(path, session);
@ -116,7 +118,7 @@ export class ConnectionManager {
let subscriptionID = shortid.generate(); let subscriptionID = shortid.generate();
query.subscribe(data, (data) => { 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); stored.set(id, query);
answer(id, subscriptionID); answer(id, subscriptionID);
@ -136,11 +138,16 @@ export class ConnectionManager {
} }
}) })
socket.on("message", (rawData: string) => { socket.on("message", async (rawData: string) => {
let data: { ns: string, args: any[] } = JSON.parse(rawData); try {
let h = handler.get(data.ns); let message: { ns: string, data: any } = JSON.parse(rawData);
if (h) { let h = handler.get(message.ns);
h(...data.args); if (h) {
h(message.data);
}
} catch (err) {
Logging.errorMessage("Unknown Error:");
Logging.error(err);
} }
}) })