Compare commits
19 Commits
v2.0.0-bet
...
v2.0.0-bet
Author | SHA1 | Date | |
---|---|---|---|
0bfdbce908 | |||
68295c148d | |||
2a62c3d3ac | |||
1434036b42 | |||
88b0cb68d8 | |||
904b986e22 | |||
2ac9def153 | |||
d2621fdd3c | |||
4cee0048f5 | |||
3432ea5e23 | |||
0175822699 | |||
72e5c0dedd | |||
d48bf46231 | |||
e287890ca1 | |||
3e0dc06521 | |||
50268d05c5 | |||
10f3b4fa50 | |||
e42f0d3991 | |||
87985def82 |
21
.drone.yml
Normal file
21
.drone.yml
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
kind: pipeline
|
||||||
|
type: docker
|
||||||
|
name: default
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Build with node
|
||||||
|
image: node:12
|
||||||
|
commands:
|
||||||
|
- npm install
|
||||||
|
- npm run build
|
||||||
|
- name: Publish to docker
|
||||||
|
image: plugins/docker
|
||||||
|
settings:
|
||||||
|
username:
|
||||||
|
from_secret: docker_username
|
||||||
|
password:
|
||||||
|
from_secret: docker_password
|
||||||
|
auto_tag: true
|
||||||
|
repo: hibas123.azurecr.io/realtimedb
|
||||||
|
registry: hibas123.azurecr.io
|
||||||
|
debug: true
|
@ -1,3 +1,5 @@
|
|||||||
|
[*]
|
||||||
charset = utf-8
|
charset = utf-8
|
||||||
indent_size = 3
|
indent_size = 3
|
||||||
indent_style = space
|
indent_style = space
|
||||||
|
insert_final_newline = true
|
2458
package-lock.json
generated
2458
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
37
package.json
37
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@hibas123/realtimedb",
|
"name": "@hibas123/realtimedb",
|
||||||
"version": "2.0.0-beta.4",
|
"version": "2.0.0-beta.19",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"private": true,
|
"private": true,
|
||||||
@ -17,32 +17,31 @@
|
|||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/dotenv": "^8.2.0",
|
"@types/dotenv": "^8.2.0",
|
||||||
"@types/jsonwebtoken": "^8.3.5",
|
"@types/jsonwebtoken": "^8.3.8",
|
||||||
"@types/koa": "^2.0.51",
|
"@types/koa": "^2.11.2",
|
||||||
"@types/koa-router": "^7.0.42",
|
"@types/koa-router": "^7.4.0",
|
||||||
"@types/leveldown": "^4.0.1",
|
"@types/leveldown": "^4.0.2",
|
||||||
"@types/levelup": "^3.1.1",
|
"@types/levelup": "^4.3.0",
|
||||||
"@types/nanoid": "^2.1.0",
|
"@types/nanoid": "^2.1.0",
|
||||||
"@types/node": "^12.12.5",
|
"@types/node": "^13.9.3",
|
||||||
"@types/ws": "^6.0.3",
|
"@types/ws": "^7.2.3",
|
||||||
"concurrently": "^5.0.0",
|
"concurrently": "^5.1.0",
|
||||||
"nodemon": "^1.19.4",
|
"nodemon": "^2.0.2",
|
||||||
"typescript": "^3.6.4"
|
"typescript": "^3.8.3"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@hibas123/logging": "^2.1.1",
|
"@hibas123/nodelogging": "^2.1.5",
|
||||||
"@hibas123/nodelogging": "^2.1.1",
|
"@hibas123/utils": "^2.2.3",
|
||||||
"@hibas123/utils": "^2.1.1",
|
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"handlebars": "^4.5.1",
|
"handlebars": "^4.7.3",
|
||||||
"jsonwebtoken": "^8.5.1",
|
"jsonwebtoken": "^8.5.1",
|
||||||
"koa": "^2.11.0",
|
"koa": "^2.11.0",
|
||||||
"koa-body": "^4.1.1",
|
"koa-body": "^4.1.1",
|
||||||
"koa-router": "^7.4.0",
|
"koa-router": "^8.0.8",
|
||||||
"leveldown": "^5.4.1",
|
"leveldown": "^5.5.1",
|
||||||
"levelup": "^4.3.2",
|
"levelup": "^4.3.2",
|
||||||
"nanoid": "^2.1.6",
|
"nanoid": "^2.1.11",
|
||||||
"what-the-pack": "^2.0.3",
|
"what-the-pack": "^2.0.3",
|
||||||
"ws": "^7.2.0"
|
"ws": "^7.2.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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 } from "./database/database";
|
import { DatabaseManager } from "./database/database";
|
||||||
import Logging from "@hibas123/logging";
|
import { CollectionQuery, DocumentQuery, IQuery, ITypedQuery } from "./database/query";
|
||||||
import { Query, CollectionQuery, DocumentQuery } 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,53 +60,42 @@ 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) => {
|
||||||
|
if (error)
|
||||||
|
Logging.error(error as any);
|
||||||
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 };
|
|
||||||
|
|
||||||
handler.set("query", async ({ id, type, path, data }: QueryData) => {
|
handler.set("v2", async ({ id, query }) => db.run(Array.isArray(query) ? query : [query], session)
|
||||||
//TODO: Handle case with no id, type, path
|
.then(res => answer(id, res))
|
||||||
Logging.debug(`Request with id '${id}' from type '${type}' and path '${path.join("/")}' with data`, data)
|
.catch(err => answer(id, undefined, err))
|
||||||
|
);
|
||||||
|
|
||||||
try {
|
// handler.set("bulk", async ({ id, query }) => db.run(query, session)
|
||||||
if (!db)
|
// .then(res => answer(id, res))
|
||||||
throw new Error("Database not found!");
|
// .catch(err => answer(id, undefined, err))
|
||||||
else {
|
// );
|
||||||
let isDoc = path.length % 2 === 0;
|
|
||||||
let handler = isDoc ? documentHandler.get(type) : collectionHandler.get(type);
|
|
||||||
|
|
||||||
if (!handler && session.root)
|
|
||||||
handler = rootHandler.get(type);
|
|
||||||
|
|
||||||
if (!handler)
|
const SnapshotMap = new Map<string, string>();
|
||||||
throw new Error("Invalid Request!");
|
handler.set("snapshot", async ({ id, query }: { id: string, query: ITypedQuery<"snapshot"> }) => {
|
||||||
|
db.snapshot(query, session, (data => {
|
||||||
let query = db.getQuery(path || [], session.sessionid, isDoc ? "document" : "collection");
|
socket.send(JSON.stringify({
|
||||||
let res = await handler({
|
ns: "snapshot", data: { id, data }
|
||||||
id,
|
}));
|
||||||
data,
|
})).then(s => {
|
||||||
socket,
|
answer(id, s.snaphot);
|
||||||
query: query as any // We know it is the right one
|
SnapshotMap.set(id, s.id);
|
||||||
|
}).catch(err => answer(id, undefined, err));
|
||||||
})
|
})
|
||||||
|
|
||||||
if (res && typeof res === "object" && res[StoreSym] !== undefined) {
|
handler.set("unsubscribe", async ({ id }) => {
|
||||||
if (res[StoreSym])
|
let i = SnapshotMap.get(id);
|
||||||
stored.set(id, query);
|
if (i) {
|
||||||
else
|
db.unsubscribe(i, session);
|
||||||
stored.delete(id);
|
SnapshotMap.delete(i);
|
||||||
|
|
||||||
res = res.result;
|
|
||||||
}
|
|
||||||
answer(id, res);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
// Logging.error(err);
|
|
||||||
Logging.debug("Sending error:", err);
|
|
||||||
answer(id, err.message, true);
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -156,102 +113,10 @@ export class ConnectionManager {
|
|||||||
})
|
})
|
||||||
|
|
||||||
socket.on("close", () => {
|
socket.on("close", () => {
|
||||||
Logging.log(`${session.sessionid} has disconnected!`);
|
Logging.log(`${session.id} has disconnected!`);
|
||||||
Logging.debug("Clearing stored:", stored);
|
session.subscriptions.forEach(unsubscribe => unsubscribe());
|
||||||
stored.forEach(query => (query as DocumentQuery | CollectionQuery).unsubscribe());
|
session.subscriptions.clear();
|
||||||
stored.clear();
|
|
||||||
socket.removeAllListeners();
|
socket.removeAllListeners();
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type QueryHandler<T extends Query> = (api: {
|
|
||||||
id: string;
|
|
||||||
query: T;
|
|
||||||
// storedQuery(id: string): T | undefined;
|
|
||||||
socket: WebSocket;
|
|
||||||
data: any;
|
|
||||||
}) => any;
|
|
||||||
|
|
||||||
const NoPermissionError = new Error("No permisison!");
|
|
||||||
|
|
||||||
const rootHandler = new Map<string, QueryHandler<Query>>();
|
|
||||||
rootHandler.set("collections", ({ query }) => {
|
|
||||||
return CollectionQuery.fromQuery(query).collections();
|
|
||||||
})
|
|
||||||
|
|
||||||
rootHandler.set("delete-collection", ({ query }) => {
|
|
||||||
return CollectionQuery.fromQuery(query).deleteCollection();
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
const documentHandler = new Map<string, QueryHandler<DocumentQuery>>();
|
|
||||||
documentHandler.set("get", ({ query }) => {
|
|
||||||
return query.get();
|
|
||||||
})
|
|
||||||
|
|
||||||
documentHandler.set("set", ({ query, data }) => {
|
|
||||||
return query.set(data, {});
|
|
||||||
})
|
|
||||||
|
|
||||||
documentHandler.set("update", ({ query, data }) => {
|
|
||||||
return query.update(data);
|
|
||||||
})
|
|
||||||
|
|
||||||
documentHandler.set("delete", ({ query }) => {
|
|
||||||
return query.delete();
|
|
||||||
})
|
|
||||||
|
|
||||||
documentHandler.set("snapshot", async ({ query, data, id, socket }) => {
|
|
||||||
let res = await query.snapshot((data) => {
|
|
||||||
socket.send(JSON.stringify({
|
|
||||||
ns: "snapshot", data: { id, data }
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
return StoreQuery(res);
|
|
||||||
})
|
|
||||||
|
|
||||||
documentHandler.set("unsubscribe", async ({ query }) => {
|
|
||||||
query.unsubscribe();
|
|
||||||
return DeleteQuery(true);
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
const collectionHandler = new Map<string, QueryHandler<CollectionQuery>>();
|
|
||||||
collectionHandler.set("keys", ({ query }) => {
|
|
||||||
return query.keys();
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
collectionHandler.set("add", ({ query, data }) => {
|
|
||||||
return query.add(data);
|
|
||||||
})
|
|
||||||
|
|
||||||
collectionHandler.set("get", ({ query, data }) => {
|
|
||||||
if (data.where)
|
|
||||||
query.where = data.where;
|
|
||||||
if (data.limit)
|
|
||||||
query.limit = data.limit;
|
|
||||||
return query.get();
|
|
||||||
})
|
|
||||||
|
|
||||||
collectionHandler.set("snapshot", async ({ query, id, socket, data }) => {
|
|
||||||
if (data.where)
|
|
||||||
query.where = data.where;
|
|
||||||
if (data.limit)
|
|
||||||
query.limit = data.limit;
|
|
||||||
|
|
||||||
let res = await query.snapshot((data) => {
|
|
||||||
socket.send(JSON.stringify({
|
|
||||||
ns: "snapshot", data: { id, data }
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
|
|
||||||
return StoreQuery(res);
|
|
||||||
})
|
|
||||||
|
|
||||||
collectionHandler.set("unsubscribe", async ({ query }) => {
|
|
||||||
query.unsubscribe();
|
|
||||||
return DeleteQuery(true);
|
|
||||||
})
|
|
@ -1,9 +1,18 @@
|
|||||||
import { Rules } from "./rules";
|
import { Rules } from "./rules";
|
||||||
import Settings from "../settings";
|
import Settings from "../settings";
|
||||||
import getLevelDB, { LevelDB, deleteLevelDB } from "../storage";
|
import getLevelDB, { LevelDB, deleteLevelDB, resNull } from "../storage";
|
||||||
import DocumentLock from "./lock";
|
import DocumentLock from "./lock";
|
||||||
import { DocumentQuery, CollectionQuery, Query } from "./query";
|
import { DocumentQuery, CollectionQuery, Query, QueryError, ITypedQuery, IQuery } from "./query";
|
||||||
import Logging from "@hibas123/nodelogging";
|
import Logging from "@hibas123/nodelogging";
|
||||||
|
import Session from "./session";
|
||||||
|
import nanoid = require("nanoid/generate");
|
||||||
|
import { Observable } from "@hibas123/utils";
|
||||||
|
|
||||||
|
const ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||||
|
|
||||||
|
// interface ITransaction {
|
||||||
|
// queries: ITypedQuery<IWriteQueries>[];
|
||||||
|
// }
|
||||||
|
|
||||||
export class DatabaseManager {
|
export class DatabaseManager {
|
||||||
static databases = new Map<string, Database>();
|
static databases = new Map<string, Database>();
|
||||||
@ -46,12 +55,17 @@ export type ChangeTypes = "added" | "modified" | "deleted";
|
|||||||
export type Change = {
|
export type Change = {
|
||||||
data: any;
|
data: any;
|
||||||
document: string;
|
document: string;
|
||||||
|
collection: string;
|
||||||
type: ChangeTypes;
|
type: ChangeTypes;
|
||||||
sender: string;
|
sender: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export class Database {
|
export class Database {
|
||||||
|
public static getKey(collectionid: string, documentid?: string) {
|
||||||
|
return `${collectionid || ""}/${documentid || ""}`;
|
||||||
|
}
|
||||||
|
|
||||||
private level = getLevelDB(this.name);
|
private level = getLevelDB(this.name);
|
||||||
|
|
||||||
get data() {
|
get data() {
|
||||||
@ -64,10 +78,15 @@ export class Database {
|
|||||||
|
|
||||||
|
|
||||||
public rules: Rules;
|
public rules: Rules;
|
||||||
public locks = new DocumentLock()
|
private locks = new DocumentLock()
|
||||||
public collectionLocks = new DocumentLock()
|
public collectionLocks = new DocumentLock()
|
||||||
|
|
||||||
public changes = new Map<string, Set<(change: Change) => void>>();
|
public changeListener = new Map<string, Set<(change: Change[]) => void>>();
|
||||||
|
public collectionChangeListener = new Observable<{
|
||||||
|
key: string;
|
||||||
|
id: string;
|
||||||
|
type: "create" | "delete"
|
||||||
|
}>();
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
return {
|
return {
|
||||||
@ -104,14 +123,201 @@ export class Database {
|
|||||||
this.publickey = key;
|
this.publickey = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async resolve(path: string[], create = false): Promise<{ collection: string, document: string, collectionKey: string }> {
|
||||||
|
path = [...path]; // Create modifiable copy
|
||||||
|
let collectionID: string = undefined;
|
||||||
|
let documentKey = path.length % 2 === 0 ? path.pop() : undefined;
|
||||||
|
let key = path.join("/");
|
||||||
|
|
||||||
getQuery(path: string[], sender: string, type: "document" | "collection" | "any") {
|
const lock = await this.collectionLocks.lock(key);
|
||||||
if (type === "document")
|
|
||||||
return new DocumentQuery(this, path, sender);
|
try {
|
||||||
else if (type === "collection")
|
collectionID = await this.collections.get(key).then(r => r.toString()).catch(resNull);
|
||||||
return new CollectionQuery(this, path, sender);
|
if (!collectionID && create) {
|
||||||
|
collectionID = nanoid(ALPHABET, 32);
|
||||||
|
await this.collections.put(key, collectionID);
|
||||||
|
setImmediate(() => {
|
||||||
|
this.collectionChangeListener.send({
|
||||||
|
id: collectionID,
|
||||||
|
key,
|
||||||
|
type: "create"
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
lock();
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
collection: collectionID,
|
||||||
|
document: documentKey,
|
||||||
|
collectionKey: key
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private sendChanges(changes: Change[]) {
|
||||||
|
let col = new Map<string, Map<string, Change[]>>();
|
||||||
|
changes.forEach(change => {
|
||||||
|
let e = col.get(change.collection);
|
||||||
|
if (!e) {
|
||||||
|
e = new Map()
|
||||||
|
col.set(change.collection, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
let d = e.get(change.document);
|
||||||
|
if (!d) {
|
||||||
|
d = [];
|
||||||
|
e.set(change.document, d);
|
||||||
|
}
|
||||||
|
|
||||||
|
d.push(change);
|
||||||
|
})
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
for (let [collection, documents] of col.entries()) {
|
||||||
|
let collectionChanges = [];
|
||||||
|
for (let [document, documentChanges] of documents.entries()) {
|
||||||
|
let s = this.changeListener.get(Database.getKey(collection, document));
|
||||||
|
if (s)
|
||||||
|
s.forEach(e => setImmediate(() => e(documentChanges)));
|
||||||
|
|
||||||
|
collectionChanges.push(...documentChanges);
|
||||||
|
}
|
||||||
|
let s = this.changeListener.get(Database.getKey(collection))
|
||||||
|
if (s)
|
||||||
|
s.forEach(e => setImmediate(() => e(collectionChanges)))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
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(queries: IQuery[], session: Session) {
|
||||||
|
let resolve: { path: string[], create: boolean, resolved?: [string, string, string] }[] = [];
|
||||||
|
|
||||||
|
const addToResolve = (path: string[], create?: boolean) => {
|
||||||
|
let entry = resolve.find(e => { //TODO: Find may be slow...
|
||||||
|
if (e.path.length !== path.length)
|
||||||
|
return false;
|
||||||
|
for (let i = 0; i < e.path.length; i++) {
|
||||||
|
if (e.path[i] !== path[i])
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!entry) {
|
||||||
|
entry = {
|
||||||
|
path,
|
||||||
|
create
|
||||||
|
}
|
||||||
|
resolve.push(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
entry.create = entry.create || create;
|
||||||
|
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
const isBatch = queries.length > 1;
|
||||||
|
let parsed = queries.map(rawQuery => {
|
||||||
|
this.validate(rawQuery);
|
||||||
|
const isCollection = rawQuery.path.length % 2 === 1;
|
||||||
|
|
||||||
|
let query = isCollection
|
||||||
|
? new CollectionQuery(this, session, rawQuery)
|
||||||
|
: new DocumentQuery(this, session, rawQuery);
|
||||||
|
|
||||||
|
if (isBatch && !query.batchCompatible)
|
||||||
|
throw new Error("There are queries that are not batch compatible!");
|
||||||
|
|
||||||
|
let path = addToResolve(rawQuery.path, query.createCollection);
|
||||||
|
if (query.additionalLock)
|
||||||
|
addToResolve(query.additionalLock);
|
||||||
|
|
||||||
|
return {
|
||||||
|
path,
|
||||||
|
query
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
resolve = resolve.sort((a, b) => a.path.length - b.path.length);
|
||||||
|
|
||||||
|
let locks: (() => void)[] = [];
|
||||||
|
for (let e of resolve) {
|
||||||
|
let { collection, document, collectionKey } = await this.resolve(e.path, e.create);
|
||||||
|
e.resolved = [collection, document, collectionKey];
|
||||||
|
|
||||||
|
locks.push(
|
||||||
|
await this.locks.lock(collection, document)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = [];
|
||||||
|
try {
|
||||||
|
let batch = this.data.batch();
|
||||||
|
let changes: Change[] = [];
|
||||||
|
for (let e of parsed) {
|
||||||
|
result.push(
|
||||||
|
await e.query.run(e.path.resolved[0], e.path.resolved[1], batch, e.path.resolved[2])
|
||||||
|
);
|
||||||
|
changes.push(...e.query.changes);
|
||||||
|
}
|
||||||
|
if (batch.length > 0)
|
||||||
|
await batch.write();
|
||||||
|
|
||||||
|
this.sendChanges(changes);
|
||||||
|
} finally {
|
||||||
|
locks.forEach(lock => lock());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isBatch)
|
||||||
|
return result;
|
||||||
else
|
else
|
||||||
return new Query(this, path, sender);
|
return result[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
async snapshot(rawQuery: ITypedQuery<"snapshot">, session: Session, onchange: (change: any) => void) {
|
||||||
|
Logging.debug("Snaphot request:", rawQuery.path);
|
||||||
|
this.validate(rawQuery);
|
||||||
|
|
||||||
|
if (rawQuery.type !== "snapshot")
|
||||||
|
throw new Error("Invalid query type!");
|
||||||
|
|
||||||
|
const isCollection = rawQuery.path.length % 2 === 1;
|
||||||
|
let query = isCollection
|
||||||
|
? new CollectionQuery(this, session, rawQuery, true)
|
||||||
|
: new DocumentQuery(this, session, rawQuery, true);
|
||||||
|
|
||||||
|
const {
|
||||||
|
unsubscribe,
|
||||||
|
value
|
||||||
|
} = await query.snapshot(onchange);
|
||||||
|
|
||||||
|
const id = nanoid(ALPHABET, 16);
|
||||||
|
session.subscriptions.set(id, unsubscribe);
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
snaphot: value
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async unsubscribe(id: string, session: Session) {
|
||||||
|
let query = session.subscriptions.get(id);
|
||||||
|
if (query) {
|
||||||
|
query();
|
||||||
|
session.subscriptions.delete(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async stop() {
|
async stop() {
|
||||||
|
@ -8,6 +8,7 @@ export default class DocumentLock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async lock(collection: string = "", document: string = "") {
|
async lock(collection: string = "", document: string = "") {
|
||||||
|
//TODO: Check collection locks
|
||||||
let key = collection + "/" + document;
|
let key = collection + "/" + document;
|
||||||
let l = this.locks.get(key);
|
let l = this.locks.get(key);
|
||||||
if (l)
|
if (l)
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -2,13 +2,15 @@ import Session from "./session";
|
|||||||
import Logging from "@hibas123/nodelogging";
|
import Logging from "@hibas123/nodelogging";
|
||||||
|
|
||||||
interface IRule<T> {
|
interface IRule<T> {
|
||||||
".write"?: T
|
".write"?: T;
|
||||||
".read"?: T
|
".read"?: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
type IRuleConfig<T> = {
|
type IRuleConfig<T> =
|
||||||
|
| IRule<T>
|
||||||
|
| {
|
||||||
[segment: string]: IRuleConfig<T>;
|
[segment: string]: IRuleConfig<T>;
|
||||||
} | IRule<T>;
|
};
|
||||||
|
|
||||||
type IRuleRaw = IRuleConfig<string>;
|
type IRuleRaw = IRuleConfig<string>;
|
||||||
type IRuleParsed = IRuleConfig<boolean>;
|
type IRuleParsed = IRuleConfig<boolean>;
|
||||||
@ -17,17 +19,16 @@ const resolve = (value: any) => {
|
|||||||
if (value === true) {
|
if (value === true) {
|
||||||
return true;
|
return true;
|
||||||
} else if (typeof value === "string") {
|
} else if (typeof value === "string") {
|
||||||
|
|
||||||
}
|
}
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
};
|
||||||
|
|
||||||
export class Rules {
|
export class Rules {
|
||||||
rules: IRuleParsed;
|
rules: IRuleParsed;
|
||||||
constructor(private config: string) {
|
constructor(private config: string) {
|
||||||
let parsed: IRuleRaw = JSON.parse(config);
|
let parsed: IRuleRaw = JSON.parse(config);
|
||||||
|
|
||||||
const analyze = (raw: IRuleRaw) => {
|
const analyse = (raw: IRuleRaw) => {
|
||||||
let r: IRuleParsed = {};
|
let r: IRuleParsed = {};
|
||||||
|
|
||||||
if (raw[".read"]) {
|
if (raw[".read"]) {
|
||||||
@ -47,25 +48,34 @@ export class Rules {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (let segment in raw) {
|
for (let segment in raw) {
|
||||||
if (segment.startsWith("."))
|
if (segment.startsWith(".")) continue;
|
||||||
continue;
|
|
||||||
|
|
||||||
r[segment] = analyze(raw[segment]);
|
r[segment] = analyse(raw[segment]);
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
|
};
|
||||||
|
|
||||||
|
this.rules = analyse(parsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.rules = analyze(parsed);
|
hasPermission(
|
||||||
}
|
path: string[],
|
||||||
|
session: Session
|
||||||
hasPermission(path: string[], session: Session): { read: boolean, write: boolean } {
|
): { read: boolean; write: boolean; path: string[] } {
|
||||||
|
if (session.root)
|
||||||
|
return {
|
||||||
|
read: true,
|
||||||
|
write: true,
|
||||||
|
path: path
|
||||||
|
};
|
||||||
let read = this.rules[".read"] || false;
|
let read = this.rules[".read"] || false;
|
||||||
let write = this.rules[".write"] || false;
|
let write = this.rules[".write"] || false;
|
||||||
|
|
||||||
let rules = this.rules;
|
let rules = this.rules;
|
||||||
|
|
||||||
for (let segment of path) {
|
for (let idx in path) {
|
||||||
if (segment.startsWith("$") || segment.startsWith(".")) {
|
let segment = path[idx];
|
||||||
|
if (segment.startsWith(".")) {
|
||||||
read = false;
|
read = false;
|
||||||
write = false;
|
write = false;
|
||||||
Logging.log("Invalid query path (started with '$' or '.'):", path);
|
Logging.log("Invalid query path (started with '$' or '.'):", path);
|
||||||
@ -77,22 +87,25 @@ export class Rules {
|
|||||||
.find(e => {
|
.find(e => {
|
||||||
switch (e) {
|
switch (e) {
|
||||||
case "$uid":
|
case "$uid":
|
||||||
if (segment === session.uid)
|
if (segment === "$uid") {
|
||||||
|
path[idx] = session.uid;
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
if (segment === session.uid) return true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
})
|
});
|
||||||
|
|
||||||
rules = (k ? rules[k] : undefined) || rules[segment] || rules["*"];
|
rules = (k ? rules[k] : undefined) || rules[segment] || rules["*"];
|
||||||
|
|
||||||
if (rules) {
|
if (rules) {
|
||||||
if (rules[".read"]) {
|
if (rules[".read"]) {
|
||||||
read = rules[".read"]
|
read = rules[".read"];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rules[".write"]) {
|
if (rules[".write"]) {
|
||||||
read = rules[".write"]
|
read = rules[".write"];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@ -101,8 +114,9 @@ export class Rules {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
read: read as boolean,
|
read: read as boolean,
|
||||||
write: write as boolean
|
write: write as boolean,
|
||||||
}
|
path
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
|
|
||||||
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;
|
||||||
|
|
||||||
|
subscriptions = new Map<string, (() => void)>();
|
||||||
}
|
}
|
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)
|
||||||
|
@ -5,16 +5,26 @@ interface IFormConfigField {
|
|||||||
type: "text" | "number" | "boolean" | "textarea";
|
type: "text" | "number" | "boolean" | "textarea";
|
||||||
label: string;
|
label: string;
|
||||||
value?: string;
|
value?: string;
|
||||||
|
disabled?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
type IFormConfig = { [name: string]: IFormConfigField }
|
type IFormConfig = { [name: string]: IFormConfigField };
|
||||||
|
|
||||||
export default function getForm(url: string, title: string, fieldConfig: IFormConfig): (ctx: Context) => void {
|
export default function getForm(
|
||||||
let fields = Object.keys(fieldConfig).map(name => ({ name, ...fieldConfig[name] }))
|
url: string,
|
||||||
|
title: string,
|
||||||
|
fieldConfig: IFormConfig
|
||||||
|
): (ctx: Context) => void {
|
||||||
|
let fields = Object.keys(fieldConfig).map(name => ({
|
||||||
|
name,
|
||||||
|
...fieldConfig[name],
|
||||||
|
disabled: fieldConfig.disabled ? "disabled" : ""
|
||||||
|
}));
|
||||||
|
|
||||||
return ctx => ctx.body = getTemplate("forms")({
|
return ctx =>
|
||||||
|
(ctx.body = getTemplate("forms")({
|
||||||
url,
|
url,
|
||||||
title,
|
title,
|
||||||
fields
|
fields
|
||||||
});
|
}));
|
||||||
}
|
}
|
@ -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) {
|
||||||
|
@ -2,21 +2,24 @@ import * as Router from "koa-router";
|
|||||||
import Settings from "../../settings";
|
import Settings from "../../settings";
|
||||||
import getForm from "../helper/form";
|
import getForm from "../helper/form";
|
||||||
import getTable from "../helper/table";
|
import getTable from "../helper/table";
|
||||||
import { BadRequestError, NoPermissionError } from "../helper/errors";
|
import {
|
||||||
|
BadRequestError,
|
||||||
|
NoPermissionError,
|
||||||
|
NotFoundError
|
||||||
|
} 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";
|
import { getView } from "../helper/hb";
|
||||||
|
|
||||||
const AdminRoute = new Router();
|
const AdminRoute = new Router();
|
||||||
|
|
||||||
AdminRoute.use(async (ctx, next) => {
|
AdminRoute.use(async (ctx, next) => {
|
||||||
const { key } = ctx.query;
|
const { key } = ctx.query;
|
||||||
if (key !== config.admin)
|
if (key !== config.admin) throw new NoPermissionError("No permission!");
|
||||||
throw new NoPermissionError("No permission!");
|
|
||||||
return next();
|
return next();
|
||||||
})
|
});
|
||||||
|
|
||||||
AdminRoute.get("/", async ctx => {
|
AdminRoute.get("/", async ctx => {
|
||||||
//TODO: Main Interface
|
//TODO: Main Interface
|
||||||
@ -33,26 +36,24 @@ AdminRoute.get("/settings", async ctx => {
|
|||||||
let res = [["key", "value"]];
|
let res = [["key", "value"]];
|
||||||
stream.on("data", ({ key, value }) => {
|
stream.on("data", ({ key, value }) => {
|
||||||
res.push([key, value]);
|
res.push([key, value]);
|
||||||
})
|
});
|
||||||
|
|
||||||
stream.on("error", no);
|
stream.on("error", no);
|
||||||
stream.on("end", () => yes(res))
|
stream.on("end", () => yes(res));
|
||||||
})
|
});
|
||||||
|
|
||||||
if (ctx.query.view) {
|
if (ctx.query.view) {
|
||||||
return getTable("Settings", res, ctx);
|
return getTable("Settings", res, ctx);
|
||||||
} else {
|
} else {
|
||||||
ctx.body = res;
|
ctx.body = res;
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
AdminRoute.get("/data", async ctx => {
|
AdminRoute.get("/data", async ctx => {
|
||||||
const { database } = ctx.query;
|
const { database } = ctx.query;
|
||||||
let db = DatabaseManager.getDatabase(database);
|
let db = DatabaseManager.getDatabase(database);
|
||||||
if (!db)
|
if (!db) throw new BadRequestError("Database not found");
|
||||||
throw new BadRequestError("Database not found");
|
|
||||||
let res = await new Promise<string[][]>((yes, no) => {
|
let res = await new Promise<string[][]>((yes, no) => {
|
||||||
|
|
||||||
const stream = db.data.createReadStream({
|
const stream = db.data.createReadStream({
|
||||||
keys: true,
|
keys: true,
|
||||||
values: true,
|
values: true,
|
||||||
@ -61,28 +62,37 @@ AdminRoute.get("/data", async ctx => {
|
|||||||
limit: 1000
|
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 }) => {
|
||||||
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))
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
stream.on("error", no);
|
stream.on("error", no);
|
||||||
stream.on("end", () => yes(res))
|
stream.on("end", () => yes(res));
|
||||||
})
|
});
|
||||||
|
|
||||||
if (ctx.query.view) {
|
if (ctx.query.view) {
|
||||||
return getTable("Data from " + database, res, ctx);
|
return getTable("Data from " + database, res, ctx);
|
||||||
} else {
|
} else {
|
||||||
ctx.body = res;
|
ctx.body = res;
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
AdminRoute
|
AdminRoute.get("/database", ctx => {
|
||||||
.get("/database", ctx => {
|
|
||||||
const isFull = ctx.query.full === "true" || ctx.query.full === "1";
|
const isFull = ctx.query.full === "true" || ctx.query.full === "1";
|
||||||
let res;
|
let res;
|
||||||
if (isFull) {
|
if (isFull) {
|
||||||
//TODO: Better than JSON.parse / JSON.stringify
|
//TODO: Better than JSON.parse / JSON.stringify
|
||||||
res = Array.from(DatabaseManager.databases.entries()).map(([name, config]) => ({ name, ...(JSON.parse(JSON.stringify(config))) }));
|
res = Array.from(DatabaseManager.databases.entries()).map(
|
||||||
|
([name, config]) => ({
|
||||||
|
name,
|
||||||
|
...JSON.parse(JSON.stringify(config))
|
||||||
|
})
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
res = Array.from(DatabaseManager.databases.keys());
|
res = Array.from(DatabaseManager.databases.keys());
|
||||||
}
|
}
|
||||||
@ -92,42 +102,31 @@ AdminRoute
|
|||||||
} else {
|
} else {
|
||||||
ctx.body = res;
|
ctx.body = res;
|
||||||
}
|
}
|
||||||
})
|
}).post("/database", async ctx => {
|
||||||
.post("/database", async ctx => {
|
|
||||||
const { name, rules, publickey, accesskey, rootkey } = ctx.request.body;
|
const { name, rules, publickey, accesskey, rootkey } = ctx.request.body;
|
||||||
|
|
||||||
if (!name)
|
if (!name) throw new BadRequestError("Name must be set!");
|
||||||
throw new BadRequestError("Name must be set!");
|
|
||||||
|
|
||||||
let db = DatabaseManager.getDatabase(name);
|
let db = DatabaseManager.getDatabase(name);
|
||||||
if (!db)
|
if (!db) db = await DatabaseManager.addDatabase(name);
|
||||||
db = await DatabaseManager.addDatabase(name);
|
|
||||||
|
|
||||||
if (publickey)
|
if (publickey) await db.setPublicKey(publickey);
|
||||||
await db.setPublicKey(publickey);
|
|
||||||
|
|
||||||
if (rules)
|
if (rules) await db.setRules(rules);
|
||||||
await db.setRules(rules);
|
|
||||||
|
|
||||||
if (accesskey)
|
if (accesskey) await db.setAccessKey(accesskey);
|
||||||
await db.setAccessKey(accesskey);
|
|
||||||
|
|
||||||
|
|
||||||
if (rootkey)
|
|
||||||
await db.setRootKey(rootkey);
|
|
||||||
|
|
||||||
|
if (rootkey) await db.setRootKey(rootkey);
|
||||||
|
|
||||||
ctx.body = "Success";
|
ctx.body = "Success";
|
||||||
})
|
});
|
||||||
|
|
||||||
AdminRoute.get("/collections", async ctx => {
|
AdminRoute.get("/collections", async ctx => {
|
||||||
const { database } = ctx.query;
|
const { database } = ctx.query;
|
||||||
let db = DatabaseManager.getDatabase(database);
|
let db = DatabaseManager.getDatabase(database);
|
||||||
if (!db)
|
if (!db) throw new BadRequestError("Database not found");
|
||||||
throw new BadRequestError("Database not found");
|
|
||||||
|
|
||||||
let res = await new Promise<string[]>((yes, no) => {
|
let res = await new Promise<string[]>((yes, no) => {
|
||||||
|
|
||||||
const stream = db.collections.createKeyStream({
|
const stream = db.collections.createKeyStream({
|
||||||
keyAsBuffer: false,
|
keyAsBuffer: false,
|
||||||
limit: 1000
|
limit: 1000
|
||||||
@ -135,24 +134,23 @@ AdminRoute.get("/collections", async ctx => {
|
|||||||
let res = [];
|
let res = [];
|
||||||
stream.on("data", (key: string) => {
|
stream.on("data", (key: string) => {
|
||||||
res.push(key);
|
res.push(key);
|
||||||
})
|
});
|
||||||
|
|
||||||
stream.on("error", no);
|
stream.on("error", no);
|
||||||
stream.on("end", () => yes(res))
|
stream.on("end", () => yes(res));
|
||||||
})
|
});
|
||||||
|
|
||||||
if (ctx.query.view) {
|
if (ctx.query.view) {
|
||||||
return getTable("Databases", res, ctx);
|
return getTable("Databases", res, ctx);
|
||||||
} else {
|
} else {
|
||||||
ctx.body = res;
|
ctx.body = res;
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
AdminRoute.get("/collections/cleanup", async ctx => {
|
AdminRoute.get("/collections/cleanup", async ctx => {
|
||||||
const { database } = ctx.query;
|
const { database } = ctx.query;
|
||||||
let db = DatabaseManager.getDatabase(database);
|
let db = DatabaseManager.getDatabase(database);
|
||||||
if (!db)
|
if (!db) throw new BadRequestError("Database not found");
|
||||||
throw new BadRequestError("Database not found");
|
|
||||||
|
|
||||||
let deleted = await db.runCleanup();
|
let deleted = await db.runCleanup();
|
||||||
if (ctx.query.view) {
|
if (ctx.query.view) {
|
||||||
@ -160,14 +158,55 @@ AdminRoute.get("/collections/cleanup", async ctx => {
|
|||||||
} else {
|
} else {
|
||||||
ctx.body = deleted;
|
ctx.body = deleted;
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
AdminRoute.get("/database/new", getForm("/v1/admin/database", "New/Change Database", {
|
AdminRoute.get(
|
||||||
name: { label: "Name", type: "text", },
|
"/database/new",
|
||||||
|
getForm("/v1/admin/database", "New Database", {
|
||||||
|
name: { label: "Name", type: "text" },
|
||||||
accesskey: { label: "Access Key", type: "text" },
|
accesskey: { label: "Access Key", type: "text" },
|
||||||
rootkey: { label: "Root access key", type: "text" },
|
rootkey: { label: "Root access key", type: "text" },
|
||||||
rules: { label: "Rules", type: "textarea", value: `{\n ".write": true, \n ".read": true \n}` },
|
rules: {
|
||||||
|
label: "Rules",
|
||||||
|
type: "textarea",
|
||||||
|
value: `{\n ".write": true, \n ".read": true \n}`
|
||||||
|
},
|
||||||
publickey: { label: "Public Key", type: "textarea" }
|
publickey: { label: "Public Key", type: "textarea" }
|
||||||
}))
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
AdminRoute.get("/database/update", async ctx => {
|
||||||
|
const { database } = ctx.query;
|
||||||
|
let db = DatabaseManager.getDatabase(database);
|
||||||
|
if (!db) throw new NotFoundError("Database not found!");
|
||||||
|
getForm("/v1/admin/database", "Change Database", {
|
||||||
|
name: {
|
||||||
|
label: "Name",
|
||||||
|
type: "text",
|
||||||
|
value: db.name,
|
||||||
|
disabled: true
|
||||||
|
},
|
||||||
|
accesskey: {
|
||||||
|
label: "Access Key",
|
||||||
|
type: "text",
|
||||||
|
value: db.accesskey
|
||||||
|
},
|
||||||
|
rootkey: {
|
||||||
|
label: "Root access key",
|
||||||
|
type: "text",
|
||||||
|
value: db.rootkey
|
||||||
|
},
|
||||||
|
rules: {
|
||||||
|
label: "Rules",
|
||||||
|
type: "textarea",
|
||||||
|
value: db.rules.toJSON()
|
||||||
|
},
|
||||||
|
publickey: {
|
||||||
|
label: "Public Key",
|
||||||
|
type: "textarea",
|
||||||
|
value: db.publickey
|
||||||
|
}
|
||||||
|
})(ctx);
|
||||||
|
});
|
||||||
|
|
||||||
export default AdminRoute;
|
export default AdminRoute;
|
@ -1,5 +1,62 @@
|
|||||||
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("Invalid Access Key");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (authkey && db.publickey) {
|
||||||
|
let res = await verifyJWT(authkey, db.publickey);
|
||||||
|
if (!res || !res.uid) {
|
||||||
|
throw new BadRequestError("Invalid JWT");
|
||||||
|
} 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;
|
@ -1,13 +1,18 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
<head>
|
||||||
<head>
|
<meta charset="UTF-8" />
|
||||||
<meta charset="UTF-8">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
|
||||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
|
||||||
<title>Admin Interface</title>
|
<title>Admin Interface</title>
|
||||||
<link rel="stylesheet" href="https://unpkg.com/@hibas123/theme/out/base.css">
|
<link
|
||||||
<link rel="stylesheet" href="https://unpkg.com/@hibas123/theme/out/light.css">
|
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>
|
<script src="https://unpkg.com/handlebars/dist/handlebars.min.js"></script>
|
||||||
|
|
||||||
@ -16,7 +21,7 @@
|
|||||||
visibility: hidden;
|
visibility: hidden;
|
||||||
background-color: lightgreen;
|
background-color: lightgreen;
|
||||||
border: 1px solid lime;
|
border: 1px solid lime;
|
||||||
border-radius: .5rem;
|
border-radius: 0.5rem;
|
||||||
padding: 1rem;
|
padding: 1rem;
|
||||||
font-size: 1.5rem;
|
font-size: 1.5rem;
|
||||||
margin-bottom: 1rem;
|
margin-bottom: 1rem;
|
||||||
@ -37,29 +42,30 @@
|
|||||||
border: 0;
|
border: 0;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div class="grid">
|
<div class="grid">
|
||||||
<div style="border-right: 1px solid darkgrey; padding: 1rem;">
|
<div style="border-right: 1px solid darkgrey; padding: 1rem;">
|
||||||
<h2>Navigation: </h2>
|
<h2>Navigation:</h2>
|
||||||
<ul class="list list-clickable">
|
<ul class="list list-clickable">
|
||||||
<li onclick="loadView('settings');">Settings</li>
|
<li onclick="loadView('settings');">Settings</li>
|
||||||
<li onclick="loadView('database', {full:true});">Databases</li>
|
<li onclick="loadView('database', {full:true});">Databases</li>
|
||||||
<li onclick="loadView('database/new');">New Database</li>
|
<li onclick="loadView('database/new');">New Database</li>
|
||||||
</ul>
|
</ul>
|
||||||
Databases:
|
Databases:
|
||||||
<div id="dbs" class="list list-clickable" style="margin: 1rem;"></div>
|
<div
|
||||||
|
id="dbs"
|
||||||
|
class="list list-clickable"
|
||||||
|
style="margin: 1rem;"
|
||||||
|
></div>
|
||||||
</div>
|
</div>
|
||||||
<div style="position:relative;">
|
<div style="position:relative;">
|
||||||
<iframe id="content"></iframe>
|
<iframe id="content"></iframe>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<template>
|
<template> </template>
|
||||||
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
const key = new URL(window.location.href).searchParams.get("key");
|
const key = new URL(window.location.href).searchParams.get("key");
|
||||||
@ -73,8 +79,7 @@
|
|||||||
url.searchParams.set(key, params[key]);
|
url.searchParams.set(key, params[key]);
|
||||||
|
|
||||||
url.searchParams.set("key", key);
|
url.searchParams.set("key", key);
|
||||||
if (view)
|
if (view) url.searchParams.set("view", "true");
|
||||||
url.searchParams.set("view", "true");
|
|
||||||
|
|
||||||
return url.href;
|
return url.href;
|
||||||
}
|
}
|
||||||
@ -83,30 +88,31 @@
|
|||||||
content.src = getUrl(name, params);
|
content.src = getUrl(name, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
loadView("settings")
|
loadView("settings");
|
||||||
|
|
||||||
const dbsul = document.getElementById("dbs");
|
const dbsul = document.getElementById("dbs");
|
||||||
function reloadDBs() {
|
function reloadDBs() {
|
||||||
fetch(getUrl("database", {}, false))
|
fetch(getUrl("database", {}, false))
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(databases => databases.map(database => `
|
.then(databases =>
|
||||||
|
databases.map(
|
||||||
|
database => `
|
||||||
<div class="card margin elv-4">
|
<div class="card margin elv-4">
|
||||||
<h3>${database}</h3>
|
<h3>${database}</h3>
|
||||||
<button class=btn onclick="loadView('data', {database:'${database}'})">Data</button>
|
<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', {database:'${database}'})">Collections</button>
|
||||||
<button class=btn onclick="loadView('collections/cleanup', {database:'${database}'})">Clean</button>
|
<button class=btn onclick="loadView('collections/cleanup', {database:'${database}'})">Clean</button>
|
||||||
|
<button class=btn onclick="loadView('database/update', {database:'${database}'})">Change</button>
|
||||||
</div>`
|
</div>`
|
||||||
))
|
)
|
||||||
|
)
|
||||||
.then(d => d.join("\n"))
|
.then(d => d.join("\n"))
|
||||||
.then(d => dbsul.innerHTML = d)
|
.then(d => (dbsul.innerHTML = d))
|
||||||
.catch(console.error)
|
.catch(console.error);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
reloadDBs();
|
reloadDBs();
|
||||||
setInterval(reloadDBs, 5000);
|
setInterval(reloadDBs, 5000);
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
@ -32,19 +32,19 @@
|
|||||||
<div class="input-group">
|
<div class="input-group">
|
||||||
<label>{{label}}</label>
|
<label>{{label}}</label>
|
||||||
{{#ifCond type "===" "text"}}
|
{{#ifCond type "===" "text"}}
|
||||||
<input type="text" placeholder="{{label}}" name="{{name}}" value="{{value}}" />
|
<input type="text" placeholder="{{label}}" name="{{name}}" value="{{value}}" {{disabled}} />
|
||||||
{{/ifCond}}
|
{{/ifCond}}
|
||||||
|
|
||||||
{{#ifCond type "===" "number"}}
|
{{#ifCond type "===" "number"}}
|
||||||
<input type="number" placeholder="{{label}}" name="{{name}}" value="{{value}}" />
|
<input type="number" placeholder="{{label}}" name="{{name}}" value="{{value}}" {{disabled}} />
|
||||||
{{/ifCond}}
|
{{/ifCond}}
|
||||||
|
|
||||||
{{#ifCond type "===" "boolean"}}
|
{{#ifCond type "===" "boolean"}}
|
||||||
<input type="checkbox" name="{{name}}" checked="{{value}}" />
|
<input type="checkbox" name="{{name}}" checked="{{value}}" {{disabled}} />
|
||||||
{{/ifCond}}
|
{{/ifCond}}
|
||||||
|
|
||||||
{{#ifCond type "===" "textarea"}}
|
{{#ifCond type "===" "textarea"}}
|
||||||
<textarea class="inp" name="{{name}}" rows="20">{{value}}</textarea>
|
<textarea class="inp" name="{{name}}" rows="20" {{disabled}}>{{value}}</textarea>
|
||||||
{{/ifCond}}
|
{{/ifCond}}
|
||||||
</div>
|
</div>
|
||||||
{{/each}}
|
{{/each}}
|
||||||
|
Reference in New Issue
Block a user