Compare commits
19 Commits
v2.0.0-0
...
v2.0.0-bet
Author | SHA1 | Date | |
---|---|---|---|
2ac9def153 | |||
d2621fdd3c | |||
4cee0048f5 | |||
3432ea5e23 | |||
0175822699 | |||
72e5c0dedd | |||
d48bf46231 | |||
e287890ca1 | |||
3e0dc06521 | |||
50268d05c5 | |||
10f3b4fa50 | |||
e42f0d3991 | |||
87985def82 | |||
1f193fd5a1 | |||
b3932aa54d | |||
3fa209c0cf | |||
d8c55d4389 | |||
51dc76464f | |||
c2b8d87195 |
16
package-lock.json
generated
16
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hibas123/realtimedb",
|
"name": "@hibas123/realtimedb",
|
||||||
"version": "2.0.0-beta.0",
|
"version": "2.0.0-beta.4",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -225,12 +225,6 @@
|
|||||||
"@types/mime": "*"
|
"@types/mime": "*"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@types/shortid": {
|
|
||||||
"version": "0.0.29",
|
|
||||||
"resolved": "https://registry.npmjs.org/@types/shortid/-/shortid-0.0.29.tgz",
|
|
||||||
"integrity": "sha1-gJPuBBam4r8qpjOBCRFLP7/6Dps=",
|
|
||||||
"dev": true
|
|
||||||
},
|
|
||||||
"@types/ws": {
|
"@types/ws": {
|
||||||
"version": "6.0.3",
|
"version": "6.0.3",
|
||||||
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-6.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-6.0.3.tgz",
|
||||||
@ -3290,14 +3284,6 @@
|
|||||||
"integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=",
|
"integrity": "sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"shortid": {
|
|
||||||
"version": "2.2.15",
|
|
||||||
"resolved": "https://registry.npmjs.org/shortid/-/shortid-2.2.15.tgz",
|
|
||||||
"integrity": "sha512-5EaCy2mx2Jgc/Fdn9uuDuNIIfWBpzY4XIlhoqtXF6qsf+/+SGZ+FxDdX/ZsMZiWupIWNqAEmiNY4RC+LSmCeOw==",
|
|
||||||
"requires": {
|
|
||||||
"nanoid": "^2.1.0"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"signal-exit": {
|
"signal-exit": {
|
||||||
"version": "3.0.2",
|
"version": "3.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.2.tgz",
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name": "@hibas123/realtimedb",
|
"name": "@hibas123/realtimedb",
|
||||||
"version": "2.0.0-beta.0",
|
"version": "2.0.0-beta.8",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node lib/index.js",
|
"start": "node lib/index.js",
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
@ -29,7 +30,6 @@
|
|||||||
"typescript": "^3.6.4"
|
"typescript": "^3.6.4"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@hibas123/logging": "^2.1.1",
|
|
||||||
"@hibas123/nodelogging": "^2.1.1",
|
"@hibas123/nodelogging": "^2.1.1",
|
||||||
"@hibas123/utils": "^2.1.1",
|
"@hibas123/utils": "^2.1.1",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import Logging from "@hibas123/nodelogging";
|
import Logging from "@hibas123/nodelogging";
|
||||||
import * as dotenv from "dotenv";
|
import * as dotenv from "dotenv";
|
||||||
|
import { LoggingTypes } from "@hibas123/logging";
|
||||||
dotenv.config()
|
dotenv.config()
|
||||||
|
|
||||||
|
|
||||||
@ -17,4 +18,8 @@ const config: IConfig = {
|
|||||||
dev: (process.env.DEV || "").toLowerCase() === "true"
|
dev: (process.env.DEV || "").toLowerCase() === "true"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (config.dev) {
|
||||||
|
Logging.logLevel = LoggingTypes.Log;
|
||||||
|
}
|
||||||
|
|
||||||
export default config;
|
export default config;
|
@ -1,43 +1,12 @@
|
|||||||
|
import Logging from "@hibas123/nodelogging";
|
||||||
|
import { IncomingMessage, Server } from "http";
|
||||||
import * as WebSocket from "ws";
|
import * as WebSocket from "ws";
|
||||||
import { Server, IncomingMessage } from "http";
|
import { DatabaseManager, IQuery, ITypedQuery } from "./database/database";
|
||||||
import { DatabaseManager } from "./database/database";
|
import { CollectionQuery, DocumentQuery } from "./database/query";
|
||||||
import Logging from "@hibas123/logging";
|
|
||||||
import Query from "./database/query";
|
|
||||||
import Session from "./database/session";
|
import Session from "./database/session";
|
||||||
|
import { verifyJWT } from "./helper/jwt";
|
||||||
import nanoid = require("nanoid");
|
import nanoid = require("nanoid");
|
||||||
|
|
||||||
import * as JWT from "jsonwebtoken";
|
|
||||||
|
|
||||||
async function verifyJWT(token: string, publicKey: string) {
|
|
||||||
return new Promise<any | undefined>((yes) => {
|
|
||||||
JWT.verify(token, publicKey, (err, decoded) => {
|
|
||||||
if (err)
|
|
||||||
yes(undefined);
|
|
||||||
else
|
|
||||||
yes(decoded);
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const StoreSym = Symbol("store");
|
|
||||||
function StoreQuery(result?: any) {
|
|
||||||
return {
|
|
||||||
[StoreSym]: true,
|
|
||||||
result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function DeleteQuery(result?: any) {
|
|
||||||
return {
|
|
||||||
[StoreSym]: false,
|
|
||||||
result
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
import { URLSearchParams } from "url";
|
|
||||||
|
|
||||||
type QueryTypes = "keys" | "get" | "set" | "update" | "delete" | "push" | "subscribe" | "unsubscribe";
|
|
||||||
|
|
||||||
export class ConnectionManager {
|
export class ConnectionManager {
|
||||||
static server: WebSocket.Server;
|
static server: WebSocket.Server;
|
||||||
|
|
||||||
@ -50,10 +19,9 @@ export class ConnectionManager {
|
|||||||
Logging.log("New Connection:");
|
Logging.log("New Connection:");
|
||||||
const sendError = (error: string) => socket.send(JSON.stringify({ ns: "error_msg", data: error }));
|
const sendError = (error: string) => socket.send(JSON.stringify({ ns: "error_msg", data: error }));
|
||||||
|
|
||||||
|
|
||||||
const session = new Session(nanoid());
|
const session = new Session(nanoid());
|
||||||
|
|
||||||
let query = new URLSearchParams(req.url.split("?").pop());
|
const query = new URL(req.url, "http://localhost").searchParams;
|
||||||
|
|
||||||
const database = query.get("database");
|
const database = query.get("database");
|
||||||
const db = DatabaseManager.getDatabase(database);
|
const db = DatabaseManager.getDatabase(database);
|
||||||
@ -92,111 +60,34 @@ export class ConnectionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const stored = new Map<string, Query>();
|
|
||||||
const answer = (id: string, data: any, error: boolean = false) => {
|
const answer = (id: string, data: any, error: boolean = false) => {
|
||||||
socket.send(JSON.stringify({ ns: "message", data: { id, error, data } }));
|
socket.send(JSON.stringify({ ns: "message", data: { id, error, data } }));
|
||||||
}
|
}
|
||||||
|
|
||||||
const handler = new Map<string, ((data: any) => void)>();
|
const handler = new Map<string, ((data: any) => void)>();
|
||||||
type QueryData = { id: string, type: QueryTypes, path: string[], data: any, options: any };
|
|
||||||
|
|
||||||
const queryHandler = new Map<string, (query: Query, permissions: { read: boolean, write: boolean }, data?: any) => Promise<any>>();
|
handler.set("v2", async ({ id, query }: { id: string, query: IQuery }) => db.run(query, session)
|
||||||
|
.then(res => answer(id, res))
|
||||||
|
.catch(err => answer(id, undefined, err)));
|
||||||
|
|
||||||
const noperm = new Error("No permisison!");
|
|
||||||
|
|
||||||
if (session.root) {
|
const SnapshotMap = new Map<string, string>();
|
||||||
queryHandler.set("collections", (query, perm, data) => {
|
handler.set("snapshot", async ({ id, query }: { id: string, query: ITypedQuery<"snapshot"> }) => {
|
||||||
return query.getCollections();
|
db.snapshot(query, session, (data => {
|
||||||
|
socket.send(JSON.stringify({
|
||||||
|
ns: "snapshot", data: { id, data }
|
||||||
|
}));
|
||||||
|
})).then(s => {
|
||||||
|
answer(id, s.snaphot);
|
||||||
|
SnapshotMap.set(id, s.id);
|
||||||
|
}).catch(err => answer(id, undefined, err));
|
||||||
})
|
})
|
||||||
|
|
||||||
queryHandler.set("delete-collection", (query, perm, data) => {
|
handler.set("unsubscribe", async ({ id }) => {
|
||||||
return query.deleteCollection();
|
let i = SnapshotMap.get(id);
|
||||||
})
|
if (i) {
|
||||||
}
|
db.unsubscribe(i, session);
|
||||||
|
SnapshotMap.delete(i);
|
||||||
queryHandler.set("keys", (query, perm, data) => {
|
|
||||||
if (!perm.read)
|
|
||||||
throw noperm;
|
|
||||||
return query.keys();
|
|
||||||
})
|
|
||||||
|
|
||||||
queryHandler.set("get", (query, perm, data) => {
|
|
||||||
if (!perm.read)
|
|
||||||
throw noperm;
|
|
||||||
return query.get();
|
|
||||||
})
|
|
||||||
|
|
||||||
queryHandler.set("set", (query, perm, data) => {
|
|
||||||
if (!perm.write)
|
|
||||||
throw noperm;
|
|
||||||
return query.set(data, {});
|
|
||||||
})
|
|
||||||
|
|
||||||
queryHandler.set("update", (query, perm, data) => {
|
|
||||||
if (!perm.write)
|
|
||||||
throw noperm;
|
|
||||||
return query.update(data);
|
|
||||||
})
|
|
||||||
|
|
||||||
queryHandler.set("push", (query, perm, data) => {
|
|
||||||
if (!perm.write)
|
|
||||||
throw noperm;
|
|
||||||
return query.push(data);
|
|
||||||
})
|
|
||||||
|
|
||||||
queryHandler.set("delete", (query, perm, data) => {
|
|
||||||
if (!perm.write)
|
|
||||||
throw noperm;
|
|
||||||
return query.delete();
|
|
||||||
})
|
|
||||||
|
|
||||||
queryHandler.set("subscribe", async (query, perm, data) => {
|
|
||||||
if (!perm.read)
|
|
||||||
throw noperm;
|
|
||||||
|
|
||||||
let subscriptionID = nanoid();
|
|
||||||
|
|
||||||
query.subscribe((data) => {
|
|
||||||
socket.send(JSON.stringify({ ns: "event", data: { id: subscriptionID, data } }));
|
|
||||||
}, data.types, data.options);
|
|
||||||
|
|
||||||
return StoreQuery(subscriptionID);
|
|
||||||
})
|
|
||||||
|
|
||||||
queryHandler.set("unsubscribe", async (query, perm, data) => {
|
|
||||||
query.unsubscribe();
|
|
||||||
return DeleteQuery(true);
|
|
||||||
})
|
|
||||||
|
|
||||||
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 {
|
|
||||||
if (!db)
|
|
||||||
throw new Error("Database not found!");
|
|
||||||
else {
|
|
||||||
let handler = queryHandler.get(type);
|
|
||||||
if (!handler) {
|
|
||||||
throw new Error("Invalid Request!");
|
|
||||||
} else {
|
|
||||||
const query = stored.get(id) || db.getQuery(path || [], session.sessionid);
|
|
||||||
const perms = db.rules.hasPermission(path, session);
|
|
||||||
|
|
||||||
let res = await handler(query, perms, data);
|
|
||||||
if (res && typeof res === "object" && res[StoreSym] !== undefined) {
|
|
||||||
if (res[StoreSym])
|
|
||||||
stored.set(id, query);
|
|
||||||
else
|
|
||||||
stored.delete(id);
|
|
||||||
}
|
|
||||||
answer(id, res);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
// Logging.error(err);
|
|
||||||
Logging.debug("Sending error:", err);
|
|
||||||
answer(id, err.message, true);
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -213,9 +104,12 @@ export class ConnectionManager {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
socket.on("disconnect", () => {
|
socket.on("close", () => {
|
||||||
stored.forEach(query => query.unsubscribe());
|
Logging.log(`${session.id} has disconnected!`);
|
||||||
stored.clear();
|
session.queries.forEach((query: DocumentQuery | CollectionQuery) => {
|
||||||
|
query.unsubscribe();
|
||||||
|
})
|
||||||
|
session.queries.clear();
|
||||||
socket.removeAllListeners();
|
socket.removeAllListeners();
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,28 @@ import { Rules } from "./rules";
|
|||||||
import Settings from "../settings";
|
import Settings from "../settings";
|
||||||
import getLevelDB, { LevelDB, deleteLevelDB } from "../storage";
|
import getLevelDB, { LevelDB, deleteLevelDB } from "../storage";
|
||||||
import DocumentLock from "./lock";
|
import DocumentLock from "./lock";
|
||||||
import Query from "./query";
|
import { DocumentQuery, CollectionQuery, Query, QueryError } from "./query";
|
||||||
|
import Logging from "@hibas123/nodelogging";
|
||||||
|
import Session from "./session";
|
||||||
|
import nanoid = require("nanoid");
|
||||||
|
|
||||||
|
type IWriteQueries = "set" | "update" | "delete" | "add";
|
||||||
|
type ICollectionQueries = "get" | "add" | "keys" | "delete-collection" | "list";
|
||||||
|
type IDocumentQueries = "get" | "set" | "update" | "delete";
|
||||||
|
|
||||||
|
export interface ITypedQuery<T> {
|
||||||
|
path: string[];
|
||||||
|
type: T;
|
||||||
|
data?: any;
|
||||||
|
options?: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ITransaction {
|
||||||
|
queries: ITypedQuery<IWriteQueries>[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export type IQuery = ITypedQuery<ICollectionQueries | IDocumentQueries>;
|
||||||
|
|
||||||
|
|
||||||
export class DatabaseManager {
|
export class DatabaseManager {
|
||||||
static databases = new Map<string, Database>();
|
static databases = new Map<string, Database>();
|
||||||
@ -104,11 +125,206 @@ export class Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
getQuery(path: string[], sender: string) {
|
getQuery(path: string[], session: Session, type: "document" | "collection" | "any") {
|
||||||
return new Query(this, path, sender);
|
if (type === "document")
|
||||||
|
return new DocumentQuery(this, path, session);
|
||||||
|
else if (type === "collection")
|
||||||
|
return new CollectionQuery(this, path, session);
|
||||||
|
else
|
||||||
|
return new Query(this, path, session);
|
||||||
|
}
|
||||||
|
|
||||||
|
private validate(query: ITypedQuery<any>) {
|
||||||
|
const inv = new QueryError("Malformed query!");
|
||||||
|
if (!query || typeof query !== "object")
|
||||||
|
throw inv;
|
||||||
|
|
||||||
|
if (!query.type)
|
||||||
|
throw inv;
|
||||||
|
|
||||||
|
if (!query.path)
|
||||||
|
throw inv;
|
||||||
|
}
|
||||||
|
|
||||||
|
async run(query: IQuery, session: Session) {
|
||||||
|
this.validate(query);
|
||||||
|
const isCollection = query.path.length % 2 === 1;
|
||||||
|
if (isCollection) {
|
||||||
|
const q = new CollectionQuery(this, query.path, session);
|
||||||
|
let type = query.type as ICollectionQueries;
|
||||||
|
switch (type) {
|
||||||
|
case "add":
|
||||||
|
return q.add(query.data);
|
||||||
|
case "get":
|
||||||
|
const limit = (query.options || {}).limit;
|
||||||
|
if (limit)
|
||||||
|
q.limit = limit;
|
||||||
|
const where = (query.options || {}).where;
|
||||||
|
if (where)
|
||||||
|
q.where = where;
|
||||||
|
return q.get();
|
||||||
|
case "keys":
|
||||||
|
return q.keys();
|
||||||
|
case "list":
|
||||||
|
return q.collections();
|
||||||
|
case "delete-collection":
|
||||||
|
return q.deleteCollection();
|
||||||
|
default:
|
||||||
|
return Promise.reject(new Error("Invalid query!"));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const q = new DocumentQuery(this, query.path, session);
|
||||||
|
let type = query.type as IDocumentQueries;
|
||||||
|
switch (type) {
|
||||||
|
case "get":
|
||||||
|
return q.get();
|
||||||
|
case "set":
|
||||||
|
return q.set(query.data, query.options || {});
|
||||||
|
case "update":
|
||||||
|
return q.update(query.data);
|
||||||
|
case "delete":
|
||||||
|
return q.delete();
|
||||||
|
default:
|
||||||
|
return Promise.reject(new Error("Invalid query!"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async snapshot(query: ITypedQuery<"snapshot">, session: Session, onchange: (change: any) => void) {
|
||||||
|
this.validate(query);
|
||||||
|
|
||||||
|
const isCollection = query.path.length % 2 === 1;
|
||||||
|
let q: DocumentQuery | CollectionQuery;
|
||||||
|
if (isCollection) {
|
||||||
|
q = new CollectionQuery(this, query.path, session);
|
||||||
|
const limit = (query.options || {}).limit;
|
||||||
|
if (limit)
|
||||||
|
q.limit = limit;
|
||||||
|
const where = (query.options || {}).where;
|
||||||
|
if (where)
|
||||||
|
q.where = where;
|
||||||
|
} else {
|
||||||
|
q = new DocumentQuery(this, query.path, session);
|
||||||
|
}
|
||||||
|
|
||||||
|
const id = nanoid(16);
|
||||||
|
session.queries.set(id, q);
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
snaphot: await q.snapshot(onchange)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async unsubscribe(id: string, session: Session) {
|
||||||
|
let query: CollectionQuery | DocumentQuery = session.queries.get(id) as any;
|
||||||
|
if (query) {
|
||||||
|
query.unsubscribe();
|
||||||
|
session.queries.delete(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async stop() {
|
async stop() {
|
||||||
await this.data.close();
|
await this.data.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async runCleanup() {
|
||||||
|
const should = await new Promise<Set<string>>((yes, no) => {
|
||||||
|
const stream = this.collections.iterator({
|
||||||
|
keyAsBuffer: false,
|
||||||
|
valueAsBuffer: false
|
||||||
|
})
|
||||||
|
|
||||||
|
const collections = new Set<string>();
|
||||||
|
const onValue = (err: Error, key: string, value: string) => {
|
||||||
|
if (err) {
|
||||||
|
Logging.error(err);
|
||||||
|
stream.end((err) => Logging.error(err))
|
||||||
|
no(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!key && !value) {
|
||||||
|
yes(collections);
|
||||||
|
} else {
|
||||||
|
collections.add(value)
|
||||||
|
stream.next(onValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stream.next(onValue);
|
||||||
|
})
|
||||||
|
|
||||||
|
const existing = await new Promise<Set<string>>((yes, no) => {
|
||||||
|
const stream = this.data.iterator({
|
||||||
|
keyAsBuffer: false,
|
||||||
|
values: false
|
||||||
|
})
|
||||||
|
|
||||||
|
const collections = new Set<string>();
|
||||||
|
const onValue = (err: Error, key: string, value: Buffer) => {
|
||||||
|
if (err) {
|
||||||
|
Logging.error(err);
|
||||||
|
stream.end((err) => Logging.error(err))
|
||||||
|
no(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!key && !value) {
|
||||||
|
yes(collections);
|
||||||
|
} else {
|
||||||
|
let coll = key.split("/")[0];
|
||||||
|
collections.add(coll)
|
||||||
|
stream.next(onValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stream.next(onValue);
|
||||||
|
})
|
||||||
|
|
||||||
|
const toDelete = new Set<string>();
|
||||||
|
existing.forEach(collection => {
|
||||||
|
if (!should.has(collection))
|
||||||
|
toDelete.add(collection);
|
||||||
|
})
|
||||||
|
|
||||||
|
for (let collection of toDelete) {
|
||||||
|
const batch = this.data.batch();
|
||||||
|
|
||||||
|
let gt = Buffer.from(collection + "/ ");
|
||||||
|
gt[gt.length - 1] = 0;
|
||||||
|
|
||||||
|
let lt = Buffer.alloc(gt.length);
|
||||||
|
lt.set(gt);
|
||||||
|
lt[gt.length - 1] = 0xFF;
|
||||||
|
|
||||||
|
await new Promise<void>((yes, no) => {
|
||||||
|
const stream = this.data.iterator({
|
||||||
|
keyAsBuffer: false,
|
||||||
|
values: false,
|
||||||
|
gt,
|
||||||
|
lt
|
||||||
|
})
|
||||||
|
|
||||||
|
const onValue = (err: Error, key: string, value: Buffer) => {
|
||||||
|
if (err) {
|
||||||
|
Logging.error(err);
|
||||||
|
stream.end((err) => Logging.error(err))
|
||||||
|
no(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!key && !value) {
|
||||||
|
yes();
|
||||||
|
} else {
|
||||||
|
batch.del(key);
|
||||||
|
stream.next(onValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stream.next(onValue);
|
||||||
|
})
|
||||||
|
|
||||||
|
await batch.write();
|
||||||
|
}
|
||||||
|
|
||||||
|
return Array.from(toDelete.values());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ export default class DocumentLock {
|
|||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
if (l.length > 0)
|
if (l.length > 0)
|
||||||
setImmediate(() => l.shift());
|
setImmediate(() => l.shift()());
|
||||||
else
|
else
|
||||||
this.locks.delete(key)
|
this.locks.delete(key)
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ import { resNull } from "../storage";
|
|||||||
import nanoid = require("nanoid/generate");
|
import nanoid = require("nanoid/generate");
|
||||||
import Logging from "@hibas123/nodelogging";
|
import Logging from "@hibas123/nodelogging";
|
||||||
import * as MSGPack from "what-the-pack";
|
import * as MSGPack from "what-the-pack";
|
||||||
|
import Session from "./session";
|
||||||
|
|
||||||
export const MP = MSGPack.initialize(2 ** 20);
|
export const MP = MSGPack.initialize(2 ** 20);
|
||||||
|
|
||||||
@ -10,11 +11,7 @@ const ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
|
|||||||
|
|
||||||
const { encode, decode } = MP;
|
const { encode, decode } = MP;
|
||||||
|
|
||||||
interface ISubscribeOptions {
|
export class Query {
|
||||||
existing: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class Query {
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the path only contains valid characters and false if it doesn't
|
* Returns true if the path only contains valid characters and false if it doesn't
|
||||||
* @param path Path to be checked
|
* @param path Path to be checked
|
||||||
@ -23,19 +20,17 @@ export default class Query {
|
|||||||
return path.every(e => (e.match(/[^a-zA-Z0-9_\-\<\>]/g) || []).length === 0);
|
return path.every(e => (e.match(/[^a-zA-Z0-9_\-\<\>]/g) || []).length === 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(private database: Database, private path: string[], private sender: string) {
|
constructor(protected database: Database, protected path: string[], protected session: Session) {
|
||||||
if (path.length > 10) {
|
if (path.length > 10) {
|
||||||
throw new Error("Path is to long. Path is only allowed to be 10 Layers deep!");
|
throw new QueryError("Path is to long. Path is only allowed to be 10 Layers deep!");
|
||||||
}
|
}
|
||||||
if (!this.validatePath(path)) {
|
if (!this.validatePath(path)) {
|
||||||
throw new Error("Path can only contain a-z A-Z 0-9 '-' '-' '<' and '>' ");
|
throw new QueryError("Path can only contain a-z A-Z 0-9 '-' '-' '<' and '>' ");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.onChange = this.onChange.bind(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private async resolve(path: string[], create = false): Promise<{ collection: string, document: string, collectionKey: string }> {
|
protected async resolve(path: string[], create = false): Promise<{ collection: string, document: string, collectionKey: string }> {
|
||||||
path = [...path]; // Create modifiable copy
|
path = [...path]; // Create modifiable copy
|
||||||
let collectionID: string = undefined;
|
let collectionID: string = undefined;
|
||||||
let documentKey = path.length % 2 === 0 ? path.pop() : undefined;
|
let documentKey = path.length % 2 === 0 ? path.pop() : undefined;
|
||||||
@ -60,23 +55,26 @@ export default class Query {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private getKey(collection: string, document?: string) {
|
protected getKey(collection: string, document?: string) {
|
||||||
return `${collection || ""}/${document || ""}`;
|
return `${collection || ""}/${document || ""}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private getDoc(collection: string, document: string) {
|
protected getDoc(collection: string, document: string) {
|
||||||
return this.database.data.get(this.getKey(collection, document), { asBuffer: true }).then(res => decode(res as Buffer)).catch(resNull);
|
return this.database.data
|
||||||
|
.get(this.getKey(collection, document), { asBuffer: true })
|
||||||
|
.then(res => decode<any>(res as Buffer)).catch(resNull);
|
||||||
}
|
}
|
||||||
|
|
||||||
private sendChange(collection: string, document: string, type: ChangeTypes, data: any) {
|
protected sendChange(collection: string, document: string, type: ChangeTypes, data: any) {
|
||||||
let change: Change = {
|
let change: Change = {
|
||||||
type,
|
type,
|
||||||
document,
|
document,
|
||||||
data,
|
data,
|
||||||
sender: this.sender
|
sender: this.session.id
|
||||||
}
|
}
|
||||||
|
|
||||||
let s = this.database.changes.get(this.getKey(collection, document))
|
let s = this.database.changes.get(this.getKey(collection, document))
|
||||||
|
|
||||||
if (s)
|
if (s)
|
||||||
s.forEach(e => setImmediate(() => e(change)))
|
s.forEach(e => setImmediate(() => e(change)))
|
||||||
s = this.database.changes.get(this.getKey(collection))
|
s = this.database.changes.get(this.getKey(collection))
|
||||||
@ -85,24 +83,269 @@ export default class Query {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected static getConstructorParams(query: Query): [Database, string[], Session] {
|
||||||
|
return [query.database, query.path, query.session];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface UpdateData {
|
||||||
|
[path: string]: {
|
||||||
|
type: "value" | "timestamp" | "increment" | "push";
|
||||||
|
value: any;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export class DocumentQuery extends Query {
|
||||||
|
constructor(database: Database, path: string[], session: Session) {
|
||||||
|
super(database, path, session);
|
||||||
|
this.onChange = this.onChange.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
public async get() {
|
public async get() {
|
||||||
let { collection, document } = await this.resolve(this.path);
|
let { collection, document } = await this.resolve(this.path);
|
||||||
|
|
||||||
if (!collection || !document) {
|
if (!collection || !document) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const lock = await this.database.locks.lock(collection, document);
|
|
||||||
return this.getDoc(collection, document).finally(() => lock())
|
return this.getDoc(collection, document)
|
||||||
}
|
}
|
||||||
|
|
||||||
public async keys() {
|
public async set(data: any, { merge = false }) {
|
||||||
|
if (data === null)
|
||||||
|
return this.delete();
|
||||||
|
let { collection, document } = await this.resolve(this.path, true);
|
||||||
|
if (!collection) {
|
||||||
|
throw new QueryError("There must be a collection!")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!document) {
|
||||||
|
throw new QueryError("There must be a document key!")
|
||||||
|
}
|
||||||
|
|
||||||
|
const lock = await this.database.locks.lock(collection, document);
|
||||||
|
|
||||||
|
let isNew = !(await this.getDoc(collection, document))
|
||||||
|
|
||||||
|
return this.database.data
|
||||||
|
.put(this.getKey(collection, document), encode(data))
|
||||||
|
.then(() => this.sendChange(collection, document, isNew ? "added" : "modified", data))
|
||||||
|
.finally(() => lock())
|
||||||
|
}
|
||||||
|
|
||||||
|
public async update(updateData: UpdateData) {
|
||||||
|
let { collection, document } = await this.resolve(this.path, true);
|
||||||
|
if (!collection) {
|
||||||
|
throw new QueryError("There must be a collection!")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!document) {
|
||||||
|
throw new QueryError("There must be a document key!")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logging.debug(updateData);
|
||||||
|
|
||||||
|
const lock = await this.database.locks.lock(collection, document);
|
||||||
|
try {
|
||||||
|
let data = await this.getDoc(collection, document);
|
||||||
|
let isNew = false
|
||||||
|
if (!data) {
|
||||||
|
isNew = true;
|
||||||
|
data = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let path in updateData) {
|
||||||
|
const toUpdate = updateData[path];
|
||||||
|
let d = data;
|
||||||
|
let parts = path.split(".");
|
||||||
|
while (parts.length > 1) {
|
||||||
|
let seg = parts.shift();
|
||||||
|
if (!data[seg])
|
||||||
|
data[seg] = {}
|
||||||
|
d = data[seg];
|
||||||
|
}
|
||||||
|
|
||||||
|
const last = parts[0];
|
||||||
|
|
||||||
|
// Logging.debug(parts, last, d)
|
||||||
|
|
||||||
|
switch (toUpdate.type) {
|
||||||
|
case "value":
|
||||||
|
d[last] = toUpdate.value;
|
||||||
|
break;
|
||||||
|
case "increment":
|
||||||
|
if (d[last] === undefined || d[last] === null)
|
||||||
|
d[last] = toUpdate.value;
|
||||||
|
else if (typeof d[last] !== "number") {
|
||||||
|
throw new QueryError("Field is no number!");
|
||||||
|
} else {
|
||||||
|
d[last] += toUpdate.value;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "timestamp":
|
||||||
|
d[last] = new Date().valueOf();
|
||||||
|
break;
|
||||||
|
case "push":
|
||||||
|
if (d[last] === undefined || d[last] === null)
|
||||||
|
d[last] = [toUpdate.value];
|
||||||
|
else if (Array.isArray(d[last])) {
|
||||||
|
d[last].push(toUpdate.value);
|
||||||
|
} else {
|
||||||
|
throw new QueryError("Field is not array!");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new QueryError("Invalid update type: " + toUpdate.type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.database.data
|
||||||
|
.put(this.getKey(collection, document), encode(data))
|
||||||
|
.then(() => this.sendChange(collection, document, isNew ? "added" : "modified", data))
|
||||||
|
} finally {
|
||||||
|
lock();
|
||||||
|
}
|
||||||
|
//TODO: Implement
|
||||||
|
}
|
||||||
|
|
||||||
|
public async delete() {
|
||||||
let { collection, document } = await this.resolve(this.path);
|
let { collection, document } = await this.resolve(this.path);
|
||||||
if (document)
|
|
||||||
throw new Error("Keys only works on collections!");
|
if (!collection) {
|
||||||
if (!collection)
|
throw new QueryError("There must be a collection!")
|
||||||
throw new Error("There must be a collection");
|
}
|
||||||
|
|
||||||
|
if (!document) {
|
||||||
|
throw new QueryError("There must be a document key!")
|
||||||
|
}
|
||||||
|
|
||||||
|
const lock = await this.database.locks.lock(collection, document);
|
||||||
|
|
||||||
|
return await this.database.data
|
||||||
|
.del(`${collection}/${document}`)
|
||||||
|
.then(() => this.sendChange(collection, document, "deleted", null))
|
||||||
|
.finally(() => lock())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private subscription: {
|
||||||
|
key: string,
|
||||||
|
onChange: (change: DocRes & { type: ChangeTypes }) => void
|
||||||
|
};
|
||||||
|
|
||||||
|
async snapshot(onChange: (change: DocRes & { type: ChangeTypes }) => void) {
|
||||||
|
if (this.subscription)
|
||||||
|
throw new QueryError("This query is already subscribed!");
|
||||||
|
let { collection, document } = await this.resolve(this.path);
|
||||||
|
|
||||||
|
let data = await this.getDoc(collection, document);
|
||||||
|
let key = this.getKey(collection, document);
|
||||||
|
this.subscription = {
|
||||||
|
key,
|
||||||
|
onChange
|
||||||
|
}
|
||||||
|
let s = this.database.changes.get(key);
|
||||||
|
if (!s) {
|
||||||
|
s = new Set();
|
||||||
|
this.database.changes.set(key, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
s.add(this.onChange);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
onChange(change: Change) {
|
||||||
|
// if(change.sender === this.sender)
|
||||||
|
// return
|
||||||
|
this.subscription.onChange({
|
||||||
|
id: change.document,
|
||||||
|
data: change.data,
|
||||||
|
type: change.type
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
unsubscribe() {
|
||||||
|
if (!this.subscription)
|
||||||
|
return;
|
||||||
|
let s = this.database.changes.get(this.subscription.key);
|
||||||
|
s.delete(this.onChange);
|
||||||
|
if (s.size <= 0)
|
||||||
|
this.database.changes.delete(this.subscription.key);
|
||||||
|
|
||||||
|
this.subscription = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static fromQuery(query: Query) {
|
||||||
|
return new DocumentQuery(...Query.getConstructorParams(query));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type FieldPath = string;
|
||||||
|
type WhereFilterOp =
|
||||||
|
| '<'
|
||||||
|
| '<='
|
||||||
|
| '=='
|
||||||
|
| '>='
|
||||||
|
| '>'
|
||||||
|
| 'array-contains'
|
||||||
|
| 'in'
|
||||||
|
| 'array-contains-any';
|
||||||
|
|
||||||
|
interface IQueryWhereVerbose {
|
||||||
|
fieldPath: FieldPath,
|
||||||
|
opStr: WhereFilterOp,
|
||||||
|
value: any
|
||||||
|
}
|
||||||
|
|
||||||
|
type IQueryWhereArray = [FieldPath, WhereFilterOp, any];
|
||||||
|
|
||||||
|
type IQueryWhere = IQueryWhereArray | IQueryWhereVerbose;
|
||||||
|
|
||||||
|
interface DocRes {
|
||||||
|
id: string;
|
||||||
|
data: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CollectionQuery extends Query {
|
||||||
|
constructor(database: Database, path: string[], session: Session) {
|
||||||
|
super(database, path, session);
|
||||||
|
this.onChange = this.onChange.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private _where: IQueryWhereArray[] = [];
|
||||||
|
public set where(value: IQueryWhere[]) {
|
||||||
|
const invalidWhere = new QueryError("Invalid Where");
|
||||||
|
if (!Array.isArray(value))
|
||||||
|
throw invalidWhere;
|
||||||
|
let c = [];
|
||||||
|
this._where = value.map(cond => {
|
||||||
|
Logging.debug("Query Condition", cond);
|
||||||
|
if (Array.isArray(cond)) {
|
||||||
|
if (cond.length !== 3)
|
||||||
|
throw invalidWhere;
|
||||||
|
return cond;
|
||||||
|
} else {
|
||||||
|
if (cond && typeof cond === "object" && "fieldPath" in cond && "opStr" in cond && "value" in cond) {
|
||||||
|
return [cond.fieldPath, cond.opStr, cond.value];
|
||||||
|
} else {
|
||||||
|
throw invalidWhere;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public limit: number = -1;
|
||||||
|
|
||||||
|
public async add(value: any) {
|
||||||
|
let id = nanoid(ALPHABET, 32);
|
||||||
|
let q = new DocumentQuery(this.database, [...this.path, id], this.session);
|
||||||
|
await q.set(value, {});
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
private getStreamOptions(collection: string) {
|
||||||
let gt = Buffer.from(this.getKey(collection) + " ");
|
let gt = Buffer.from(this.getKey(collection) + " ");
|
||||||
gt[gt.length - 1] = 0;
|
gt[gt.length - 1] = 0;
|
||||||
|
|
||||||
@ -110,11 +353,23 @@ export default class Query {
|
|||||||
lt.set(gt);
|
lt.set(gt);
|
||||||
lt[gt.length - 1] = 0xFF;
|
lt[gt.length - 1] = 0xFF;
|
||||||
|
|
||||||
|
return {
|
||||||
|
gt,
|
||||||
|
lt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async keys() {
|
||||||
|
let { collection, document } = await this.resolve(this.path);
|
||||||
|
if (document)
|
||||||
|
throw new QueryError("Keys only works on collections!");
|
||||||
|
if (!collection)
|
||||||
|
return []
|
||||||
|
|
||||||
return new Promise<string[]>((yes, no) => {
|
return new Promise<string[]>((yes, no) => {
|
||||||
let keys = [];
|
let keys = [];
|
||||||
const stream = this.database.data.createKeyStream({
|
const stream = this.database.data.createKeyStream({
|
||||||
gt,
|
...this.getStreamOptions(collection),
|
||||||
lt,
|
|
||||||
keyAsBuffer: false
|
keyAsBuffer: false
|
||||||
})
|
})
|
||||||
stream.on("data", (key: string) => {
|
stream.on("data", (key: string) => {
|
||||||
@ -127,60 +382,167 @@ export default class Query {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public async set(data: any, { merge = false }) {
|
private getFieldValue(data: any, path: FieldPath) {
|
||||||
if (data === null)
|
let parts = path.split(".");
|
||||||
return this.delete();
|
let d = data;
|
||||||
let { collection, document } = await this.resolve(this.path, true);
|
while (parts.length > 0) {
|
||||||
if (!collection) {
|
let seg = parts.shift();
|
||||||
throw new Error("There must be a collection!")
|
|
||||||
|
d = data[seg];
|
||||||
|
if (d === undefined || d === null)
|
||||||
|
break; // Undefined/Null has no other fields!
|
||||||
|
}
|
||||||
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!document) {
|
private fitsWhere(data: any): boolean {
|
||||||
throw new Error("There must be a document key!")
|
if (this._where.length > 0) {
|
||||||
|
return this._where.every(([fieldPath, opStr, value]) => {
|
||||||
|
let val = this.getFieldValue(data, fieldPath);
|
||||||
|
switch (opStr) {
|
||||||
|
case "<":
|
||||||
|
return val < value;
|
||||||
|
case "<=":
|
||||||
|
return val <= value;
|
||||||
|
case "==":
|
||||||
|
return val == value;
|
||||||
|
case ">=":
|
||||||
|
return val >= value;
|
||||||
|
case ">":
|
||||||
|
return val > value;
|
||||||
|
case "array-contains":
|
||||||
|
if (Array.isArray(val)) {
|
||||||
|
return val.some(e => e === value);
|
||||||
}
|
}
|
||||||
|
|
||||||
const lock = await this.database.locks.lock(collection, document);
|
return false;
|
||||||
|
// case "array-contains-any":
|
||||||
let isNew = await this.database.data.get(this.getKey(collection, document)).catch(resNull).then(e => e !== null);
|
case "in":
|
||||||
|
if (typeof val === "object") {
|
||||||
return this.database.data
|
return value in val;
|
||||||
.put(this.getKey(collection, document), encode(data))
|
}
|
||||||
.then(() => this.sendChange(collection, document, isNew ? "added" : "modified", data))
|
return false;
|
||||||
.finally(() => lock())
|
default:
|
||||||
|
throw new QueryError("Invalid where operation " + opStr);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async push(value: any) {
|
async get() {
|
||||||
let id = nanoid(ALPHABET, 32);
|
|
||||||
let q = new Query(this.database, [...this.path, id], this.sender);
|
|
||||||
await q.set(value, {});
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async delete() {
|
|
||||||
let { collection, document } = await this.resolve(this.path);
|
let { collection, document } = await this.resolve(this.path);
|
||||||
|
if (document)
|
||||||
|
throw new QueryError("Keys only works on collections!");
|
||||||
|
if (!collection)
|
||||||
|
return [];
|
||||||
|
|
||||||
if (!collection) {
|
return new Promise<DocRes[]>((yes, no) => {
|
||||||
throw new Error("There must be a collection!")
|
const stream = this.database.data.iterator({
|
||||||
|
...this.getStreamOptions(collection),
|
||||||
|
keyAsBuffer: false,
|
||||||
|
valueAsBuffer: true
|
||||||
|
})
|
||||||
|
|
||||||
|
let values: DocRes[] = [];
|
||||||
|
|
||||||
|
const onValue = (err: Error, key: string, value: Buffer) => {
|
||||||
|
if (err) {
|
||||||
|
no(err);
|
||||||
|
stream.end(err => Logging.error(err));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (!key && !value) {
|
||||||
|
// END
|
||||||
|
Logging.debug("Checked all!")
|
||||||
|
yes(values);
|
||||||
|
} else {
|
||||||
|
let s = key.split("/", 2);
|
||||||
|
if (s.length <= 1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const id = s[1];
|
||||||
|
|
||||||
|
let data = decode(value);
|
||||||
|
if (this.fitsWhere(data)) {
|
||||||
|
if (this.limit < 0 || values.length < this.limit) {
|
||||||
|
values.push({
|
||||||
|
id,
|
||||||
|
data
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
stream.end((err) => err ? no(err) : yes(values))
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!document) {
|
stream.next(onValue);
|
||||||
throw new Error("There must be a document key!")
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stream.next(onValue);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
private subscription: {
|
||||||
|
key: string,
|
||||||
|
onChange: (change: (DocRes & { type: ChangeTypes })[]) => void
|
||||||
|
};
|
||||||
|
|
||||||
|
async snapshot(onChange: (change: (DocRes & { type: ChangeTypes })[]) => void) {
|
||||||
|
if (this.subscription)
|
||||||
|
throw new QueryError("This query is already subscribed!");
|
||||||
|
let { collection, document } = await this.resolve(this.path, true);
|
||||||
|
|
||||||
|
let data = await this.get();
|
||||||
|
|
||||||
|
let key = this.getKey(collection, document);
|
||||||
|
this.subscription = {
|
||||||
|
key,
|
||||||
|
onChange
|
||||||
|
}
|
||||||
|
let s = this.database.changes.get(key);
|
||||||
|
if (!s) {
|
||||||
|
s = new Set();
|
||||||
|
this.database.changes.set(key, s);
|
||||||
|
}
|
||||||
|
|
||||||
|
s.add(this.onChange);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
onChange(change: Change) {
|
||||||
|
// if(change.sender === this.sender)
|
||||||
|
// return
|
||||||
|
|
||||||
|
if (this.fitsWhere(change.data)) {
|
||||||
|
this.subscription.onChange([{
|
||||||
|
id: change.document,
|
||||||
|
data: change.data,
|
||||||
|
type: change.type
|
||||||
|
}])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsubscribe() {
|
||||||
|
if (!this.subscription)
|
||||||
|
return;
|
||||||
|
let s = this.database.changes.get(this.subscription.key);
|
||||||
|
s.delete(this.onChange);
|
||||||
|
if (s.size <= 0)
|
||||||
|
this.database.changes.delete(this.subscription.key);
|
||||||
|
|
||||||
|
this.subscription = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const lock = await this.database.locks.lock(collection, document);
|
public async collections() {
|
||||||
|
if (!this.session.root)
|
||||||
|
throw new QueryError("No Permission!");
|
||||||
|
|
||||||
return await this.database.data
|
|
||||||
.del(`${collection}/${document}`)
|
|
||||||
.then(() => this.sendChange(collection, document, "deleted", null))
|
|
||||||
.finally(() => lock())
|
|
||||||
}
|
|
||||||
|
|
||||||
public async update(data: any) {
|
|
||||||
//TODO: Implement
|
|
||||||
}
|
|
||||||
|
|
||||||
public async getCollections() {
|
|
||||||
return new Promise<string[]>((yes, no) => {
|
return new Promise<string[]>((yes, no) => {
|
||||||
let keys = [];
|
let keys = [];
|
||||||
const stream = this.database.data.createKeyStream({ keyAsBuffer: false })
|
const stream = this.database.data.createKeyStream({ keyAsBuffer: false })
|
||||||
@ -191,18 +553,24 @@ export default class Query {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async deleteCollection() {
|
public async deleteCollection() {
|
||||||
|
if (!this.session.root)
|
||||||
|
throw new QueryError("No Permission!");
|
||||||
|
|
||||||
const { collection, document, collectionKey } = await this.resolve(this.path);
|
const { collection, document, collectionKey } = await this.resolve(this.path);
|
||||||
|
|
||||||
if (document) {
|
if (document) {
|
||||||
throw new Error("There can be no document defined on this operation");
|
throw new QueryError("There can be no document defined on this operation");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: Lock whole collection!
|
||||||
|
|
||||||
let batch = this.database.data.batch();
|
let batch = this.database.data.batch();
|
||||||
try {
|
try {
|
||||||
if (collection) {
|
if (collection) {
|
||||||
let keys = await this.keys();
|
let documents = await this.keys();
|
||||||
for (let key in keys) {
|
// Logging.debug("To delete:", documents)
|
||||||
batch.del(key);
|
for (let document of documents) {
|
||||||
|
batch.del(this.getKey(collection, document));
|
||||||
}
|
}
|
||||||
await batch.write();
|
await batch.write();
|
||||||
batch = undefined;
|
batch = undefined;
|
||||||
@ -214,80 +582,13 @@ export default class Query {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static fromQuery(query: Query) {
|
||||||
|
return new CollectionQuery(...Query.getConstructorParams(query));
|
||||||
private subscription: {
|
|
||||||
key: string;
|
|
||||||
type: Set<ChangeTypes>;
|
|
||||||
send: (data: Omit<Change, "sender">) => void;
|
|
||||||
};
|
|
||||||
async subscribe(send: (data: Omit<Change, "sender">) => void, type: ChangeTypes[] | undefined, _options: Partial<ISubscribeOptions>) {
|
|
||||||
let options: ISubscribeOptions = {
|
|
||||||
existing: true,
|
|
||||||
..._options
|
|
||||||
}
|
|
||||||
|
|
||||||
let { collection, document } = await this.resolve(this.path);
|
|
||||||
|
|
||||||
let key = this.getKey(collection, document);
|
|
||||||
let s = this.database.changes.get(key) || new Set();
|
|
||||||
s.add(this.onChange)
|
|
||||||
this.database.changes.set(key, s);
|
|
||||||
|
|
||||||
this.subscription = {
|
|
||||||
key,
|
|
||||||
send,
|
|
||||||
type: new Set(type || [])
|
|
||||||
};
|
|
||||||
|
|
||||||
Logging.debug("Existing?", options.existing)
|
|
||||||
|
|
||||||
if (options.existing) {
|
|
||||||
if (document) {
|
|
||||||
send({
|
|
||||||
document: document,
|
|
||||||
type: "added",
|
|
||||||
data: await this.get(),
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
await this.keys().then(async documents => {
|
|
||||||
for (let document of documents) {
|
|
||||||
const data = await this.getDoc(collection, document);
|
|
||||||
if (data)
|
|
||||||
send({
|
|
||||||
type: "added",
|
|
||||||
document,
|
|
||||||
data
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Logging.debug("Subscribed");
|
export class QueryError extends Error {
|
||||||
}
|
constructor(message: string) {
|
||||||
|
super(message);
|
||||||
async onChange(change: Change) {
|
|
||||||
const { type, send } = this.subscription;
|
|
||||||
|
|
||||||
// Events from the sender are handled locally
|
|
||||||
if (change.sender !== this.sender && type.has(change.type)) {
|
|
||||||
let c = { ...change };
|
|
||||||
delete c.sender;
|
|
||||||
send(c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsubscribe() {
|
|
||||||
if (!this.subscription) {
|
|
||||||
const { key } = this.subscription;
|
|
||||||
let s = this.database.changes.get(key);
|
|
||||||
if (s) {
|
|
||||||
s.delete(this.onChange);
|
|
||||||
if (s.size <= 0)
|
|
||||||
this.database.changes.delete(key);
|
|
||||||
}
|
|
||||||
this.subscription = undefined;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,8 +1,12 @@
|
|||||||
|
import { Query } from "./query";
|
||||||
|
|
||||||
export default class Session {
|
export default class Session {
|
||||||
constructor(private _sessionid: string) { }
|
constructor(private _sessionid: string) { }
|
||||||
get sessionid() {
|
get id() {
|
||||||
return this._sessionid;
|
return this._sessionid;
|
||||||
}
|
}
|
||||||
root: boolean = false;
|
root: boolean = false;
|
||||||
uid: string = undefined;
|
uid: string = undefined;
|
||||||
|
|
||||||
|
queries = new Map<string, Query>();
|
||||||
}
|
}
|
12
src/helper/jwt.ts
Normal file
12
src/helper/jwt.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import * as JWT from "jsonwebtoken";
|
||||||
|
|
||||||
|
export async function verifyJWT(token: string, publicKey: string) {
|
||||||
|
return new Promise<any | undefined>((yes) => {
|
||||||
|
JWT.verify(token, publicKey, (err, decoded) => {
|
||||||
|
if (err)
|
||||||
|
yes(undefined);
|
||||||
|
else
|
||||||
|
yes(decoded);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
@ -5,9 +5,14 @@ import { DatabaseManager } from "./database/database";
|
|||||||
import { createServer } from "http";
|
import { createServer } from "http";
|
||||||
import { ConnectionManager } from "./connection";
|
import { ConnectionManager } from "./connection";
|
||||||
import { LoggingTypes } from "@hibas123/logging";
|
import { LoggingTypes } from "@hibas123/logging";
|
||||||
|
import { readFileSync } from "fs";
|
||||||
|
|
||||||
Logging.logLevel = config.dev ? LoggingTypes.Debug : LoggingTypes.Log;
|
Logging.logLevel = config.dev ? LoggingTypes.Debug : LoggingTypes.Log;
|
||||||
|
|
||||||
|
const version = JSON.parse(readFileSync("./package.json").toString()).version;
|
||||||
|
|
||||||
|
Logging.log("Starting Database version:", version);
|
||||||
|
|
||||||
DatabaseManager.init().then(() => {
|
DatabaseManager.init().then(() => {
|
||||||
const http = createServer(Web.callback());
|
const http = createServer(Web.callback());
|
||||||
ConnectionManager.bind(http);
|
ConnectionManager.bind(http);
|
||||||
|
@ -398,6 +398,12 @@ export class NoPermissionError extends HttpError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class UnauthorizedError extends HttpError {
|
||||||
|
constructor(message: string) {
|
||||||
|
super(message, HttpStatusCode.UNAUTHORIZED)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export class BadRequestError extends HttpError {
|
export class BadRequestError extends HttpError {
|
||||||
constructor(message: string) {
|
constructor(message: string) {
|
||||||
super(message, HttpStatusCode.BAD_REQUEST)
|
super(message, HttpStatusCode.BAD_REQUEST)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import getTemplate from "./hb";
|
import { getTemplate } from "./hb";
|
||||||
import { Context } from "vm";
|
import { Context } from "vm";
|
||||||
|
|
||||||
interface IFormConfigField {
|
interface IFormConfigField {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import * as Handlebars from "handlebars";
|
import * as Handlebars from "handlebars";
|
||||||
import { readFileSync } from "fs";
|
import { readFileSync } from "fs";
|
||||||
import config from "../../config";
|
import config from "../../config";
|
||||||
import Logging from "@hibas123/logging";
|
import Logging from "@hibas123/nodelogging";
|
||||||
|
|
||||||
|
|
||||||
function checkCondition(v1, operator, v2) {
|
function checkCondition(v1, operator, v2) {
|
||||||
@ -37,8 +37,22 @@ Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
|
|||||||
|
|
||||||
|
|
||||||
const cache = new Map<string, Handlebars.TemplateDelegate>();
|
const cache = new Map<string, Handlebars.TemplateDelegate>();
|
||||||
|
const htmlCache = new Map<string, string>();
|
||||||
|
|
||||||
export default function getTemplate(name: string) {
|
export function getView(name: string) {
|
||||||
|
let tl: string;
|
||||||
|
if (!config.dev)
|
||||||
|
tl = htmlCache.get(name);
|
||||||
|
|
||||||
|
if (!tl) {
|
||||||
|
tl = readFileSync(`./views/${name}.html`).toString();
|
||||||
|
htmlCache.set(name, tl);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tl;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getTemplate(name: string) {
|
||||||
let tl: Handlebars.TemplateDelegate;
|
let tl: Handlebars.TemplateDelegate;
|
||||||
if (!config.dev)
|
if (!config.dev)
|
||||||
tl = cache.get(name);
|
tl = cache.get(name);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Context } from "koa";
|
import { Context } from "koa";
|
||||||
import getTemplate from "./hb";
|
import { getTemplate } from "./hb";
|
||||||
|
|
||||||
export default function getTable(title: string, data: any[], ctx: Context) {
|
export default function getTable(title: string, data: any[], ctx: Context) {
|
||||||
let table: string[][] = [];
|
let table: string[][] = [];
|
||||||
|
@ -6,7 +6,8 @@ import { BadRequestError, NoPermissionError } from "../helper/errors";
|
|||||||
import { DatabaseManager } from "../../database/database";
|
import { DatabaseManager } from "../../database/database";
|
||||||
import { MP } from "../../database/query";
|
import { MP } from "../../database/query";
|
||||||
import config from "../../config";
|
import config from "../../config";
|
||||||
import Logging from "@hibas123/logging";
|
import Logging from "@hibas123/nodelogging";
|
||||||
|
import { getView } from "../helper/hb";
|
||||||
|
|
||||||
const AdminRoute = new Router();
|
const AdminRoute = new Router();
|
||||||
|
|
||||||
@ -17,6 +18,11 @@ AdminRoute.use(async (ctx, next) => {
|
|||||||
return next();
|
return next();
|
||||||
})
|
})
|
||||||
|
|
||||||
|
AdminRoute.get("/", async ctx => {
|
||||||
|
//TODO: Main Interface
|
||||||
|
ctx.body = getView("admin");
|
||||||
|
});
|
||||||
|
|
||||||
AdminRoute.get("/settings", async ctx => {
|
AdminRoute.get("/settings", async ctx => {
|
||||||
let res = await new Promise<string[][]>((yes, no) => {
|
let res = await new Promise<string[][]>((yes, no) => {
|
||||||
const stream = Settings.db.createReadStream({
|
const stream = Settings.db.createReadStream({
|
||||||
@ -51,11 +57,11 @@ AdminRoute.get("/data", async ctx => {
|
|||||||
keys: true,
|
keys: true,
|
||||||
values: true,
|
values: true,
|
||||||
valueAsBuffer: true,
|
valueAsBuffer: true,
|
||||||
keyAsBuffer: false
|
keyAsBuffer: false,
|
||||||
|
limit: 1000
|
||||||
});
|
});
|
||||||
let res = [["key", "value"]];
|
let res = [["key", "value"]];
|
||||||
stream.on("data", ({ key, value }: { key: string, value: Buffer }) => {
|
stream.on("data", ({ key, value }: { key: string, value: Buffer }) => {
|
||||||
Logging.debug("Admin Key:", key);
|
|
||||||
res.push([key, key.split("/").length > 2 ? value.toString() : JSON.stringify(MP.decode(value))]);
|
res.push([key, key.split("/").length > 2 ? value.toString() : JSON.stringify(MP.decode(value))]);
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -114,6 +120,48 @@ AdminRoute
|
|||||||
ctx.body = "Success";
|
ctx.body = "Success";
|
||||||
})
|
})
|
||||||
|
|
||||||
|
AdminRoute.get("/collections", async ctx => {
|
||||||
|
const { database } = ctx.query;
|
||||||
|
let db = DatabaseManager.getDatabase(database);
|
||||||
|
if (!db)
|
||||||
|
throw new BadRequestError("Database not found");
|
||||||
|
|
||||||
|
let res = await new Promise<string[]>((yes, no) => {
|
||||||
|
|
||||||
|
const stream = db.collections.createKeyStream({
|
||||||
|
keyAsBuffer: false,
|
||||||
|
limit: 1000
|
||||||
|
});
|
||||||
|
let res = [];
|
||||||
|
stream.on("data", (key: string) => {
|
||||||
|
res.push(key);
|
||||||
|
})
|
||||||
|
|
||||||
|
stream.on("error", no);
|
||||||
|
stream.on("end", () => yes(res))
|
||||||
|
})
|
||||||
|
|
||||||
|
if (ctx.query.view) {
|
||||||
|
return getTable("Databases", res, ctx);
|
||||||
|
} else {
|
||||||
|
ctx.body = res;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
AdminRoute.get("/collections/cleanup", async ctx => {
|
||||||
|
const { database } = ctx.query;
|
||||||
|
let db = DatabaseManager.getDatabase(database);
|
||||||
|
if (!db)
|
||||||
|
throw new BadRequestError("Database not found");
|
||||||
|
|
||||||
|
let deleted = await db.runCleanup();
|
||||||
|
if (ctx.query.view) {
|
||||||
|
return getTable("Databases", deleted, ctx);
|
||||||
|
} else {
|
||||||
|
ctx.body = deleted;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
AdminRoute.get("/database/new", getForm("/v1/admin/database", "New/Change Database", {
|
AdminRoute.get("/database/new", getForm("/v1/admin/database", "New/Change Database", {
|
||||||
name: { label: "Name", type: "text", },
|
name: { label: "Name", type: "text", },
|
||||||
accesskey: { label: "Access Key", type: "text" },
|
accesskey: { label: "Access Key", type: "text" },
|
||||||
|
@ -1,5 +1,59 @@
|
|||||||
import * as Router from "koa-router";
|
import * as Router from "koa-router";
|
||||||
import AdminRoute from "./admin";
|
import AdminRoute from "./admin";
|
||||||
|
import { DatabaseManager } from "../../database/database";
|
||||||
|
import { NotFoundError, NoPermissionError, BadRequestError } from "../helper/errors";
|
||||||
|
import Logging from "@hibas123/nodelogging";
|
||||||
|
import Session from "../../database/session";
|
||||||
|
import nanoid = require("nanoid");
|
||||||
|
import { verifyJWT } from "../../helper/jwt";
|
||||||
|
import { QueryError } from "../../database/query";
|
||||||
const V1 = new Router({ prefix: "/v1" });
|
const V1 = new Router({ prefix: "/v1" });
|
||||||
|
|
||||||
V1.use("/admin", AdminRoute.routes(), AdminRoute.allowedMethods());
|
V1.use("/admin", AdminRoute.routes(), AdminRoute.allowedMethods());
|
||||||
|
|
||||||
|
V1.post("/db/:database/query", async ctx => {
|
||||||
|
const { database } = ctx.params;
|
||||||
|
const { accesskey, authkey, rootkey } = ctx.query;
|
||||||
|
|
||||||
|
const query = ctx.request.body;
|
||||||
|
if (!query) {
|
||||||
|
throw new BadRequestError("Query not defined!");
|
||||||
|
}
|
||||||
|
|
||||||
|
const session = new Session(nanoid());
|
||||||
|
const db = DatabaseManager.getDatabase(database);
|
||||||
|
if (!db) {
|
||||||
|
throw new NotFoundError("Database not found!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (db.accesskey) {
|
||||||
|
if (!accesskey || accesskey !== db.accesskey) {
|
||||||
|
throw new NoPermissionError("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (authkey && db.publickey) {
|
||||||
|
let res = await verifyJWT(authkey, db.publickey);
|
||||||
|
if (!res || !res.uid) {
|
||||||
|
throw new BadRequestError("Invalid JWT");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
session.uid = res.uid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rootkey && db.rootkey) {
|
||||||
|
if (rootkey === db.rootkey) {
|
||||||
|
session.root = true;
|
||||||
|
Logging.warning(`Somebody logged into ${database} via rootkey`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.body = await db.run(query, session).catch(err => {
|
||||||
|
if (err instanceof QueryError) {
|
||||||
|
throw new BadRequestError(err.message);
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
})
|
||||||
|
})
|
||||||
export default V1;
|
export default V1;
|
112
views/admin.html
Normal file
112
views/admin.html
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||||
|
<title>Admin Interface</title>
|
||||||
|
<link rel="stylesheet" href="https://unpkg.com/@hibas123/theme/out/base.css">
|
||||||
|
<link rel="stylesheet" href="https://unpkg.com/@hibas123/theme/out/light.css">
|
||||||
|
|
||||||
|
<script src="https://unpkg.com/handlebars/dist/handlebars.min.js"></script>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
#message {
|
||||||
|
visibility: hidden;
|
||||||
|
background-color: lightgreen;
|
||||||
|
border: 1px solid lime;
|
||||||
|
border-radius: .5rem;
|
||||||
|
padding: 1rem;
|
||||||
|
font-size: 1.5rem;
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.grid {
|
||||||
|
display: grid;
|
||||||
|
height: 100vh;
|
||||||
|
grid-template-columns: 360px auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
#content {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
border: 0;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div class="grid">
|
||||||
|
<div style="border-right: 1px solid darkgrey; padding: 1rem;">
|
||||||
|
<h2>Navigation: </h2>
|
||||||
|
<ul class="list list-clickable">
|
||||||
|
<li onclick="loadView('settings');">Settings</li>
|
||||||
|
<li onclick="loadView('database', {full:true});">Databases</li>
|
||||||
|
<li onclick="loadView('database/new');">New Database</li>
|
||||||
|
</ul>
|
||||||
|
Databases:
|
||||||
|
<div id="dbs" class="list list-clickable" style="margin: 1rem;"></div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
<div style="position:relative;">
|
||||||
|
<iframe id="content"></iframe>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
const key = new URL(window.location.href).searchParams.get("key");
|
||||||
|
const content = document.getElementById("content");
|
||||||
|
const base = new URL(window.location.href).host;
|
||||||
|
|
||||||
|
function getUrl(name, params, view = true) {
|
||||||
|
const url = new URL(window.location.href);
|
||||||
|
url.pathname = "/v1/admin/" + name;
|
||||||
|
for (let key in params || {})
|
||||||
|
url.searchParams.set(key, params[key]);
|
||||||
|
|
||||||
|
url.searchParams.set("key", key);
|
||||||
|
if (view)
|
||||||
|
url.searchParams.set("view", "true");
|
||||||
|
|
||||||
|
return url.href;
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadView(name, params) {
|
||||||
|
content.src = getUrl(name, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
loadView("settings")
|
||||||
|
|
||||||
|
const dbsul = document.getElementById("dbs");
|
||||||
|
function reloadDBs() {
|
||||||
|
fetch(getUrl("database", {}, false))
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(databases => databases.map(database => `
|
||||||
|
<div class="card margin elv-4">
|
||||||
|
<h3>${database}</h3>
|
||||||
|
<button class=btn onclick="loadView('data', {database:'${database}'})">Data</button>
|
||||||
|
<button class=btn onclick="loadView('collections', {database:'${database}'})">Collections</button>
|
||||||
|
<button class=btn onclick="loadView('collections/cleanup', {database:'${database}'})">Clean</button>
|
||||||
|
</div>`
|
||||||
|
))
|
||||||
|
.then(d => d.join("\n"))
|
||||||
|
.then(d => dbsul.innerHTML = d)
|
||||||
|
.catch(console.error)
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
reloadDBs();
|
||||||
|
setInterval(reloadDBs, 5000);
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
Reference in New Issue
Block a user