From 15df0351a40209f795299c83a61f89ef887bf5ee Mon Sep 17 00:00:00 2001 From: Fabian Stamm Date: Fri, 11 Oct 2019 21:13:21 +0200 Subject: [PATCH] Adding keys request --- src/connection.ts | 7 ++++++- src/database/query.ts | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/connection.ts b/src/connection.ts index 84b34aa..19786ef 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -21,7 +21,7 @@ async function verifyJWT(token: string, publicKey: string) { import { URLSearchParams } from "url"; -type QueryTypes = "get" | "set" | "push" | "subscribe" | "unsubscribe"; +type QueryTypes = "keys" | "get" | "set" | "push" | "subscribe" | "unsubscribe"; export class ConnectionManager { static server: WebSocket.Server; @@ -96,6 +96,11 @@ export class ConnectionManager { const query = stored.get(id) || db.getQuery(path); switch (type) { + case "keys": + if (!perms.read) + throw noperm; + answer(id, await query.keys()); + return; case "get": if (!perms.read) throw noperm; diff --git a/src/database/query.ts b/src/database/query.ts index 1526305..b9c9dc0 100644 --- a/src/database/query.ts +++ b/src/database/query.ts @@ -74,6 +74,19 @@ export default class Query { }); } + async keys() { + const lock = await this.database.locks.lock(this.path); + try { + let obj = await this.getField(this.path); + if (!obj) + return []; + let fields = await this.getFields(this.path); + return fields.map(field => field.split("/").filter(e => e !== "")).filter(path => path.length === this.path.length + 1).map(path => path.pop()); + } finally { + lock() + } + } + async get() { const lock = await this.database.locks.lock(this.path); try { @@ -243,6 +256,7 @@ export default class Query { if (type === ChangeTypes.PUSH) { send({ id: change.path[change.path.length - 1], + path: change.path, data: await new Query(this.database, change.path).get() }) } else {