This commit is contained in:
commit
7fe18a0037
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,4 +1,7 @@
|
||||
<<<<<<< HEAD
|
||||
root=true
|
||||
=======
|
||||
>>>>>>> 0bfdbce908484560108e20cede6f9e7c46710818
|
||||
[*]
|
||||
charset = utf-8
|
||||
indent_size = 3
|
||||
|
720
package-lock.json
generated
720
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
32
package.json
32
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@hibas123/realtimedb",
|
||||
"version": "2.0.0-beta.9",
|
||||
"version": "2.0.0-beta.19",
|
||||
"description": "",
|
||||
"main": "lib/index.js",
|
||||
"private": true,
|
||||
@ -17,31 +17,31 @@
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/dotenv": "^8.2.0",
|
||||
"@types/jsonwebtoken": "^8.3.5",
|
||||
"@types/koa": "^2.11.0",
|
||||
"@types/koa-router": "^7.0.42",
|
||||
"@types/jsonwebtoken": "^8.3.8",
|
||||
"@types/koa": "^2.11.2",
|
||||
"@types/koa-router": "^7.4.0",
|
||||
"@types/leveldown": "^4.0.2",
|
||||
"@types/levelup": "^3.1.1",
|
||||
"@types/levelup": "^4.3.0",
|
||||
"@types/nanoid": "^2.1.0",
|
||||
"@types/node": "^12.12.14",
|
||||
"@types/ws": "^6.0.4",
|
||||
"concurrently": "^5.0.0",
|
||||
"nodemon": "^2.0.1",
|
||||
"typescript": "^3.7.2"
|
||||
"@types/node": "^13.9.3",
|
||||
"@types/ws": "^7.2.3",
|
||||
"concurrently": "^5.1.0",
|
||||
"nodemon": "^2.0.2",
|
||||
"typescript": "^3.8.3"
|
||||
},
|
||||
"dependencies": {
|
||||
"@hibas123/nodelogging": "^2.1.2",
|
||||
"@hibas123/nodelogging": "^2.1.5",
|
||||
"@hibas123/utils": "^2.2.3",
|
||||
"dotenv": "^8.2.0",
|
||||
"handlebars": "^4.5.3",
|
||||
"handlebars": "^4.7.3",
|
||||
"jsonwebtoken": "^8.5.1",
|
||||
"koa": "^2.11.0",
|
||||
"koa-body": "^4.1.1",
|
||||
"koa-router": "^7.4.0",
|
||||
"leveldown": "^5.4.1",
|
||||
"koa-router": "^8.0.8",
|
||||
"leveldown": "^5.5.1",
|
||||
"levelup": "^4.3.2",
|
||||
"nanoid": "^2.1.7",
|
||||
"nanoid": "^2.1.11",
|
||||
"what-the-pack": "^2.0.3",
|
||||
"ws": "^7.2.0"
|
||||
"ws": "^7.2.3"
|
||||
}
|
||||
}
|
@ -7,7 +7,12 @@ import Session from "./session";
|
||||
import { LevelUpChain } from "levelup";
|
||||
|
||||
export type IWriteQueries = "set" | "update" | "delete" | "add";
|
||||
export type ICollectionQueries = "get" | "add" | "keys" | "delete-collection" | "list";
|
||||
export type ICollectionQueries =
|
||||
| "get"
|
||||
| "add"
|
||||
| "keys"
|
||||
| "delete-collection"
|
||||
| "list";
|
||||
export type IDocumentQueries = "get" | "set" | "update" | "delete";
|
||||
|
||||
export interface ITypedQuery<T> {
|
||||
@ -17,21 +22,30 @@ export interface ITypedQuery<T> {
|
||||
options?: any;
|
||||
}
|
||||
|
||||
export type IQuery = ITypedQuery<ICollectionQueries | IDocumentQueries | "snapshot">;
|
||||
export type IQuery = ITypedQuery<
|
||||
ICollectionQueries | IDocumentQueries | "snapshot"
|
||||
>;
|
||||
|
||||
export const MP = MSGPack.initialize(2 ** 20);
|
||||
|
||||
const ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
const ALPHABET =
|
||||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
const { encode, decode } = MP;
|
||||
|
||||
type Runner = (collection: string, document: string, batch: LevelUpChain, collectionKey: string) => any;
|
||||
type Runner = (
|
||||
collection: string,
|
||||
document: string,
|
||||
batch: LevelUpChain,
|
||||
collectionKey: string
|
||||
) => any;
|
||||
|
||||
interface IPreparedQuery {
|
||||
createCollection: boolean;
|
||||
needDocument: boolean;
|
||||
batchCompatible: boolean;
|
||||
runner: Runner;
|
||||
permission: "write" | "read";
|
||||
additionalLock?: string[];
|
||||
}
|
||||
|
||||
@ -46,7 +60,9 @@ export abstract class Query {
|
||||
* @param path Path to be checked
|
||||
*/
|
||||
private validatePath(path: string[]) {
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
public changes: Change[] = [];
|
||||
@ -55,14 +71,24 @@ export abstract class Query {
|
||||
public readonly needDocument: boolean;
|
||||
public readonly batchCompatible: boolean;
|
||||
public readonly additionalLock?: string[];
|
||||
public readonly permission: string;
|
||||
private readonly _runner: Runner;
|
||||
|
||||
constructor(protected database: Database, protected session: Session, protected query: IQuery, snapshot = false) {
|
||||
constructor(
|
||||
protected database: Database,
|
||||
protected session: Session,
|
||||
protected query: IQuery,
|
||||
snapshot = false
|
||||
) {
|
||||
if (query.path.length > 10) {
|
||||
throw new QueryError("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(query.path)) {
|
||||
throw new QueryError("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 '>' "
|
||||
);
|
||||
}
|
||||
|
||||
if (!snapshot) {
|
||||
@ -80,75 +106,116 @@ export abstract class Query {
|
||||
protected getDoc(collection: string, document: string) {
|
||||
return this.database.data
|
||||
.get(Database.getKey(collection, document), { asBuffer: true })
|
||||
.then(res => decode<any>(res as Buffer)).catch(resNull);
|
||||
.then(res => decode<any>(res as Buffer))
|
||||
.catch(resNull);
|
||||
}
|
||||
|
||||
protected sendChange(collection: string, document: string, type: ChangeTypes, data: any) {
|
||||
protected sendChange(
|
||||
collection: string,
|
||||
document: string,
|
||||
type: ChangeTypes,
|
||||
data: any
|
||||
) {
|
||||
let change: Change = {
|
||||
type,
|
||||
document,
|
||||
collection,
|
||||
data,
|
||||
sender: this.session.id
|
||||
}
|
||||
};
|
||||
|
||||
this.changes.push(change);
|
||||
}
|
||||
|
||||
protected static getConstructorParams(query: Query): [Database, Session, IQuery] {
|
||||
protected static getConstructorParams(
|
||||
query: Query
|
||||
): [Database, Session, IQuery] {
|
||||
return [query.database, query.session, query.query];
|
||||
}
|
||||
|
||||
protected abstract checkChange(change: Change): boolean;
|
||||
protected abstract firstSend(collection: string, document: string): Promise<any>;
|
||||
protected abstract firstSend(
|
||||
collection: string,
|
||||
document: string
|
||||
): Promise<any>;
|
||||
|
||||
public run(collection: string, document: string, batch: LevelUpChain, collectionKey: string) {
|
||||
return this._runner.call(this, collection, document, batch, collectionKey);
|
||||
public run(
|
||||
collection: string,
|
||||
document: string,
|
||||
batch: LevelUpChain,
|
||||
collectionKey: string
|
||||
) {
|
||||
let perm = this.database.rules.hasPermission(
|
||||
this.query.path,
|
||||
this.session
|
||||
);
|
||||
if (this.permission === "read" && !perm.read) {
|
||||
throw new QueryError("No permission!");
|
||||
} else if (this.permission === "write" && !perm.write) {
|
||||
throw new QueryError("No permission!");
|
||||
}
|
||||
this.query.path = perm.path;
|
||||
return this._runner.call(
|
||||
this,
|
||||
collection,
|
||||
document,
|
||||
batch,
|
||||
collectionKey
|
||||
);
|
||||
}
|
||||
|
||||
public async snapshot(onChange: (change: (DocRes & { type: ChangeTypes })[]) => void) {
|
||||
public async snapshot(
|
||||
onChange: (change: (DocRes & { type: ChangeTypes })[]) => void
|
||||
) {
|
||||
let perm = this.database.rules.hasPermission(
|
||||
this.query.path,
|
||||
this.session
|
||||
);
|
||||
if (!perm.read) {
|
||||
throw new QueryError("No permission!");
|
||||
}
|
||||
|
||||
this.query.path = perm.path;
|
||||
|
||||
const receivedChanges = (changes: Change[]) => {
|
||||
let res = changes.filter(change => this.checkChange(change)).map(change => {
|
||||
let res = changes
|
||||
.filter(change => this.checkChange(change))
|
||||
.map(change => {
|
||||
return {
|
||||
id: change.document,
|
||||
data: change.data,
|
||||
type: change.type
|
||||
}
|
||||
})
|
||||
if (res.length > 0)
|
||||
onChange(res);
|
||||
};
|
||||
});
|
||||
if (res.length > 0) onChange(res);
|
||||
};
|
||||
|
||||
const unsub = this.database.collectionChangeListener.subscribe(change => {
|
||||
if (change.key === collectionKey) {
|
||||
if (change.type === "create")
|
||||
addSubscriber(change.id);
|
||||
else
|
||||
removeSubscriber(); // Send delete for all elements (Don't know how to do this...)
|
||||
if (change.type === "create") addSubscriber(change.id);
|
||||
else removeSubscriber(); // Send delete for all elements (Don't know how to do this...)
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
let { collection, document, collectionKey } = await this.database.resolve(this.query.path)
|
||||
let { collection, document, collectionKey } = await this.database.resolve(
|
||||
this.query.path
|
||||
);
|
||||
let oldKey: string = undefined;
|
||||
|
||||
const removeSubscriber = () => {
|
||||
if (!oldKey)
|
||||
return;
|
||||
if (!oldKey) return;
|
||||
let s = this.database.changeListener.get(oldKey);
|
||||
if (s) {
|
||||
s.delete(receivedChanges);
|
||||
if (s.size <= 0)
|
||||
this.database.changeListener.delete(oldKey);
|
||||
if (s.size <= 0) this.database.changeListener.delete(oldKey);
|
||||
}
|
||||
oldKey = undefined;
|
||||
}
|
||||
};
|
||||
|
||||
const addSubscriber = (collection: string) => {
|
||||
let key = Database.getKey(collection, document);
|
||||
if (oldKey !== key) {
|
||||
if (oldKey !== undefined)
|
||||
removeSubscriber();
|
||||
if (oldKey !== undefined) removeSubscriber();
|
||||
|
||||
let s = this.database.changeListener.get(key);
|
||||
if (!s) {
|
||||
@ -158,7 +225,7 @@ export abstract class Query {
|
||||
|
||||
s.add(receivedChanges);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (collection) {
|
||||
addSubscriber(collection);
|
||||
@ -170,7 +237,7 @@ export abstract class Query {
|
||||
removeSubscriber();
|
||||
},
|
||||
value: await this.firstSend(collection, document)
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -178,7 +245,7 @@ interface UpdateData {
|
||||
[path: string]: {
|
||||
type: "value" | "timestamp" | "increment" | "push";
|
||||
value: any;
|
||||
}
|
||||
};
|
||||
}
|
||||
export class DocumentQuery extends Query {
|
||||
prepare(query: IQuery): IPreparedQuery {
|
||||
@ -189,29 +256,33 @@ export class DocumentQuery extends Query {
|
||||
batchCompatible: false,
|
||||
createCollection: false,
|
||||
needDocument: false,
|
||||
permission: "read",
|
||||
runner: this.get
|
||||
}
|
||||
};
|
||||
case "set":
|
||||
return {
|
||||
batchCompatible: true,
|
||||
createCollection: true,
|
||||
needDocument: true,
|
||||
permission: "write",
|
||||
runner: this.set
|
||||
}
|
||||
};
|
||||
case "update":
|
||||
return {
|
||||
batchCompatible: true,
|
||||
createCollection: true,
|
||||
needDocument: true,
|
||||
permission: "write",
|
||||
runner: this.update
|
||||
}
|
||||
};
|
||||
case "delete":
|
||||
return {
|
||||
batchCompatible: true,
|
||||
createCollection: false,
|
||||
needDocument: true,
|
||||
permission: "write",
|
||||
runner: this.delete
|
||||
}
|
||||
};
|
||||
default:
|
||||
throw new Error("Invalid query type: " + type);
|
||||
}
|
||||
@ -225,22 +296,28 @@ export class DocumentQuery extends Query {
|
||||
return this.getDoc(collection, document);
|
||||
}
|
||||
|
||||
private async set(collection: string, document: string, batch?: LevelUpChain) {
|
||||
private async set(
|
||||
collection: string,
|
||||
document: string,
|
||||
batch?: LevelUpChain
|
||||
) {
|
||||
const { data, options } = this.query;
|
||||
if (data === null)
|
||||
return this.delete(collection, document, batch);
|
||||
if (data === null) return this.delete(collection, document, batch);
|
||||
|
||||
|
||||
let isNew = !(await this.getDoc(collection, document))
|
||||
batch.put(Database.getKey(collection, document), encode(data))
|
||||
this.sendChange(collection, document, isNew ? "added" : "modified", data)
|
||||
let isNew = !(await this.getDoc(collection, document));
|
||||
batch.put(Database.getKey(collection, document), encode(data));
|
||||
this.sendChange(collection, document, isNew ? "added" : "modified", data);
|
||||
}
|
||||
|
||||
private async update(collection: string, document: string, batch?: LevelUpChain) {
|
||||
private async update(
|
||||
collection: string,
|
||||
document: string,
|
||||
batch?: LevelUpChain
|
||||
) {
|
||||
const updateData: UpdateData = this.query.data;
|
||||
|
||||
let data = await this.getDoc(collection, document);
|
||||
let isNew = false
|
||||
let isNew = false;
|
||||
if (!data) {
|
||||
isNew = true;
|
||||
data = {};
|
||||
@ -252,8 +329,7 @@ export class DocumentQuery extends Query {
|
||||
let parts = path.split(".");
|
||||
while (parts.length > 1) {
|
||||
let seg = parts.shift();
|
||||
if (!data[seg])
|
||||
data[seg] = {}
|
||||
if (!data[seg]) data[seg] = {};
|
||||
d = data[seg];
|
||||
}
|
||||
|
||||
@ -290,23 +366,29 @@ export class DocumentQuery extends Query {
|
||||
}
|
||||
|
||||
if (batch) {
|
||||
batch.put(Database.getKey(collection, document), encode(data))
|
||||
batch.put(Database.getKey(collection, document), encode(data));
|
||||
} else {
|
||||
await this.database.data
|
||||
.put(Database.getKey(collection, document), encode(data))
|
||||
await this.database.data.put(
|
||||
Database.getKey(collection, document),
|
||||
encode(data)
|
||||
);
|
||||
}
|
||||
|
||||
this.sendChange(collection, document, isNew ? "added" : "modified", data)
|
||||
this.sendChange(collection, document, isNew ? "added" : "modified", data);
|
||||
}
|
||||
|
||||
private async delete(collection: string, document: string, batch?: LevelUpChain) {
|
||||
private async delete(
|
||||
collection: string,
|
||||
document: string,
|
||||
batch?: LevelUpChain
|
||||
) {
|
||||
if (batch) {
|
||||
batch.del(Database.getKey(collection, document))
|
||||
batch.del(Database.getKey(collection, document));
|
||||
} else {
|
||||
await this.database.data.del(Database.getKey(collection, document));
|
||||
}
|
||||
|
||||
this.sendChange(collection, document, "deleted", null)
|
||||
this.sendChange(collection, document, "deleted", null);
|
||||
}
|
||||
|
||||
checkChange(change: Change) {
|
||||
@ -324,19 +406,19 @@ export class DocumentQuery extends Query {
|
||||
|
||||
type FieldPath = string;
|
||||
type WhereFilterOp =
|
||||
| '<'
|
||||
| '<='
|
||||
| '=='
|
||||
| '>='
|
||||
| '>'
|
||||
| 'array-contains'
|
||||
| 'in'
|
||||
| 'array-contains-any';
|
||||
| "<"
|
||||
| "<="
|
||||
| "=="
|
||||
| ">="
|
||||
| ">"
|
||||
| "array-contains"
|
||||
| "in"
|
||||
| "array-contains-any";
|
||||
|
||||
interface IQueryWhereVerbose {
|
||||
fieldPath: FieldPath,
|
||||
opStr: WhereFilterOp,
|
||||
value: any
|
||||
fieldPath: FieldPath;
|
||||
opStr: WhereFilterOp;
|
||||
value: any;
|
||||
}
|
||||
|
||||
type IQueryWhereArray = [FieldPath, WhereFilterOp, any];
|
||||
@ -346,53 +428,55 @@ type IQueryWhere = IQueryWhereArray | IQueryWhereVerbose;
|
||||
export class CollectionQuery extends Query {
|
||||
private _addId: string;
|
||||
|
||||
|
||||
prepare(query): IPreparedQuery {
|
||||
switch (query.type as ICollectionQueries) {
|
||||
case "add":
|
||||
this._addId = nanoid(ALPHABET, 32)
|
||||
this._addId = nanoid(ALPHABET, 32);
|
||||
return {
|
||||
batchCompatible: true,
|
||||
createCollection: true,
|
||||
needDocument: false,
|
||||
runner: this.add,
|
||||
permission: "write",
|
||||
additionalLock: [...query.path, this._addId]
|
||||
}
|
||||
};
|
||||
case "get":
|
||||
const limit = (query.options || {}).limit;
|
||||
if (limit)
|
||||
this.limit = limit;
|
||||
if (limit) this.limit = limit;
|
||||
const where = (query.options || {}).where;
|
||||
if (where)
|
||||
this.where = where;
|
||||
if (where) this.where = where;
|
||||
|
||||
return {
|
||||
batchCompatible: false,
|
||||
createCollection: false,
|
||||
needDocument: false,
|
||||
permission: "read",
|
||||
runner: this.get
|
||||
}
|
||||
};
|
||||
case "keys":
|
||||
return {
|
||||
batchCompatible: false,
|
||||
createCollection: false,
|
||||
needDocument: false,
|
||||
permission: "read",
|
||||
runner: this.keys
|
||||
}
|
||||
};
|
||||
case "list":
|
||||
return {
|
||||
batchCompatible: false,
|
||||
createCollection: false,
|
||||
needDocument: false,
|
||||
permission: "read",
|
||||
runner: this.keys
|
||||
}
|
||||
};
|
||||
case "delete-collection":
|
||||
return {
|
||||
batchCompatible: false,
|
||||
createCollection: false,
|
||||
needDocument: false,
|
||||
permission: "write",
|
||||
runner: this.deleteCollection
|
||||
}
|
||||
};
|
||||
// run = () => q.deleteCollection();
|
||||
// break;
|
||||
default:
|
||||
@ -400,32 +484,40 @@ export class CollectionQuery extends Query {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private _where: IQueryWhereArray[] = [];
|
||||
public set where(value: IQueryWhere[]) {
|
||||
const invalidWhere = new QueryError("Invalid Where");
|
||||
if (!Array.isArray(value))
|
||||
throw invalidWhere;
|
||||
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;
|
||||
if (cond.length !== 3) throw invalidWhere;
|
||||
return cond;
|
||||
} else {
|
||||
if (cond && typeof cond === "object" && "fieldPath" in cond && "opStr" in cond && "value" in cond) {
|
||||
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(collection: string, document: string, batch: LevelUpChain, collectionKey: string) {
|
||||
public async add(
|
||||
collection: string,
|
||||
document: string,
|
||||
batch: LevelUpChain,
|
||||
collectionKey: string
|
||||
) {
|
||||
let q = new DocumentQuery(this.database, this.session, {
|
||||
type: "set",
|
||||
path: this.additionalLock,
|
||||
@ -442,28 +534,26 @@ export class CollectionQuery extends Query {
|
||||
|
||||
let lt = Buffer.alloc(gt.length);
|
||||
lt.set(gt);
|
||||
lt[gt.length - 1] = 0xFF;
|
||||
lt[gt.length - 1] = 0xff;
|
||||
|
||||
return {
|
||||
gt,
|
||||
lt
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public async keys(collection: string) {
|
||||
if (!collection)
|
||||
return []
|
||||
if (!collection) return [];
|
||||
|
||||
return new Promise<string[]>((yes, no) => {
|
||||
let keys = [];
|
||||
const stream = this.database.data.createKeyStream({
|
||||
...this.getStreamOptions(collection),
|
||||
keyAsBuffer: false
|
||||
})
|
||||
});
|
||||
stream.on("data", (key: string) => {
|
||||
let s = key.split("/", 2);
|
||||
if (s.length > 1)
|
||||
keys.push(s[1]);
|
||||
if (s.length > 1) keys.push(s[1]);
|
||||
});
|
||||
stream.on("end", () => yes(keys));
|
||||
stream.on("error", no);
|
||||
@ -477,8 +567,7 @@ export class CollectionQuery extends Query {
|
||||
let seg = parts.shift();
|
||||
|
||||
d = data[seg];
|
||||
if (d === undefined || d === null)
|
||||
break; // Undefined/Null has no other fields!
|
||||
if (d === undefined || d === null) break; // Undefined/Null has no other fields!
|
||||
}
|
||||
return d;
|
||||
}
|
||||
@ -513,21 +602,20 @@ export class CollectionQuery extends Query {
|
||||
default:
|
||||
throw new QueryError("Invalid where operation " + opStr);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
async get(collection: string) {
|
||||
if (!collection)
|
||||
return [];
|
||||
if (!collection) return [];
|
||||
|
||||
return new Promise<DocRes[]>((yes, no) => {
|
||||
const stream = this.database.data.iterator({
|
||||
...this.getStreamOptions(collection),
|
||||
keyAsBuffer: false,
|
||||
valueAsBuffer: true
|
||||
})
|
||||
});
|
||||
|
||||
let values: DocRes[] = [];
|
||||
|
||||
@ -535,16 +623,14 @@ export class CollectionQuery extends Query {
|
||||
if (err) {
|
||||
no(err);
|
||||
stream.end(err => Logging.error(err));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (!key && !value) {
|
||||
// END
|
||||
Logging.debug("Checked all!")
|
||||
Logging.debug("Checked all!");
|
||||
yes(values);
|
||||
} else {
|
||||
let s = key.split("/", 2);
|
||||
if (s.length <= 1)
|
||||
return;
|
||||
if (s.length <= 1) return;
|
||||
|
||||
const id = s[1];
|
||||
|
||||
@ -555,9 +641,8 @@ export class CollectionQuery extends Query {
|
||||
id,
|
||||
data
|
||||
});
|
||||
}
|
||||
else {
|
||||
stream.end((err) => err ? no(err) : yes(values))
|
||||
} else {
|
||||
stream.end(err => (err ? no(err) : yes(values)));
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -565,10 +650,10 @@ export class CollectionQuery extends Query {
|
||||
stream.next(onValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
stream.next(onValue);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
checkChange(change: Change) {
|
||||
@ -576,25 +661,30 @@ export class CollectionQuery extends Query {
|
||||
}
|
||||
|
||||
firstSend(collection: string) {
|
||||
return this.get(collection)
|
||||
return this.get(collection);
|
||||
}
|
||||
|
||||
public async collections() {
|
||||
if (!this.session.root)
|
||||
throw new QueryError("No Permission!");
|
||||
if (!this.session.root) throw new QueryError("No Permission!");
|
||||
|
||||
return new Promise<string[]>((yes, no) => {
|
||||
let keys = [];
|
||||
const stream = this.database.data.createKeyStream({ keyAsBuffer: false })
|
||||
const stream = this.database.data.createKeyStream({
|
||||
keyAsBuffer: false
|
||||
});
|
||||
stream.on("data", (key: string) => keys.push(key.split("/")));
|
||||
stream.on("end", () => yes(keys));
|
||||
stream.on("error", no);
|
||||
});
|
||||
}
|
||||
|
||||
public async deleteCollection(collection: string, document: string, _b: LevelUpChain, collectionKey: string) {
|
||||
if (!this.session.root)
|
||||
throw new QueryError("No Permission!");
|
||||
public async deleteCollection(
|
||||
collection: string,
|
||||
document: string,
|
||||
_b: LevelUpChain,
|
||||
collectionKey: string
|
||||
) {
|
||||
if (!this.session.root) throw new QueryError("No Permission!");
|
||||
|
||||
//TODO: Lock whole collection!
|
||||
let batch = this.database.data.batch();
|
||||
@ -615,8 +705,7 @@ export class CollectionQuery extends Query {
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
if (batch)
|
||||
batch.clear();
|
||||
if (batch) batch.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,13 +2,15 @@ import Session from "./session";
|
||||
import Logging from "@hibas123/nodelogging";
|
||||
|
||||
interface IRule<T> {
|
||||
".write"?: T
|
||||
".read"?: T
|
||||
".write"?: T;
|
||||
".read"?: T;
|
||||
}
|
||||
|
||||
type IRuleConfig<T> = {
|
||||
type IRuleConfig<T> =
|
||||
| IRule<T>
|
||||
| {
|
||||
[segment: string]: IRuleConfig<T>;
|
||||
} | IRule<T>;
|
||||
};
|
||||
|
||||
type IRuleRaw = IRuleConfig<string>;
|
||||
type IRuleParsed = IRuleConfig<boolean>;
|
||||
@ -17,17 +19,16 @@ const resolve = (value: any) => {
|
||||
if (value === true) {
|
||||
return true;
|
||||
} else if (typeof value === "string") {
|
||||
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
export class Rules {
|
||||
rules: IRuleParsed;
|
||||
constructor(private config: string) {
|
||||
let parsed: IRuleRaw = JSON.parse(config);
|
||||
|
||||
const analyze = (raw: IRuleRaw) => {
|
||||
const analyse = (raw: IRuleRaw) => {
|
||||
let r: IRuleParsed = {};
|
||||
|
||||
if (raw[".read"]) {
|
||||
@ -47,25 +48,34 @@ export class Rules {
|
||||
}
|
||||
|
||||
for (let segment in raw) {
|
||||
if (segment.startsWith("."))
|
||||
continue;
|
||||
if (segment.startsWith(".")) continue;
|
||||
|
||||
r[segment] = analyze(raw[segment]);
|
||||
r[segment] = analyse(raw[segment]);
|
||||
}
|
||||
return r;
|
||||
};
|
||||
|
||||
this.rules = analyse(parsed);
|
||||
}
|
||||
|
||||
this.rules = analyze(parsed);
|
||||
}
|
||||
|
||||
hasPermission(path: string[], session: Session): { read: boolean, write: boolean } {
|
||||
hasPermission(
|
||||
path: string[],
|
||||
session: Session
|
||||
): { read: boolean; write: boolean; path: string[] } {
|
||||
if (session.root)
|
||||
return {
|
||||
read: true,
|
||||
write: true,
|
||||
path: path
|
||||
};
|
||||
let read = this.rules[".read"] || false;
|
||||
let write = this.rules[".write"] || false;
|
||||
|
||||
let rules = this.rules;
|
||||
|
||||
for (let segment of path) {
|
||||
if (segment.startsWith("$") || segment.startsWith(".")) {
|
||||
for (let idx in path) {
|
||||
let segment = path[idx];
|
||||
if (segment.startsWith(".")) {
|
||||
read = false;
|
||||
write = false;
|
||||
Logging.log("Invalid query path (started with '$' or '.'):", path);
|
||||
@ -77,22 +87,25 @@ export class Rules {
|
||||
.find(e => {
|
||||
switch (e) {
|
||||
case "$uid":
|
||||
if (segment === session.uid)
|
||||
if (segment === "$uid") {
|
||||
path[idx] = session.uid;
|
||||
return true;
|
||||
}
|
||||
if (segment === session.uid) return true;
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
})
|
||||
});
|
||||
|
||||
rules = (k ? rules[k] : undefined) || rules[segment] || rules["*"];
|
||||
|
||||
if (rules) {
|
||||
if (rules[".read"]) {
|
||||
read = rules[".read"]
|
||||
read = rules[".read"];
|
||||
}
|
||||
|
||||
if (rules[".write"]) {
|
||||
read = rules[".write"]
|
||||
read = rules[".write"];
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
@ -101,8 +114,9 @@ export class Rules {
|
||||
|
||||
return {
|
||||
read: read as boolean,
|
||||
write: write as boolean
|
||||
}
|
||||
write: write as boolean,
|
||||
path
|
||||
};
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
|
@ -5,16 +5,26 @@ interface IFormConfigField {
|
||||
type: "text" | "number" | "boolean" | "textarea";
|
||||
label: 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 {
|
||||
let fields = Object.keys(fieldConfig).map(name => ({ name, ...fieldConfig[name] }))
|
||||
export default function getForm(
|
||||
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,
|
||||
title,
|
||||
fields
|
||||
});
|
||||
}));
|
||||
}
|
@ -2,7 +2,11 @@ import * as Router from "koa-router";
|
||||
import Settings from "../../settings";
|
||||
import getForm from "../helper/form";
|
||||
import getTable from "../helper/table";
|
||||
import { BadRequestError, NoPermissionError } from "../helper/errors";
|
||||
import {
|
||||
BadRequestError,
|
||||
NoPermissionError,
|
||||
NotFoundError
|
||||
} from "../helper/errors";
|
||||
import { DatabaseManager } from "../../database/database";
|
||||
import { MP } from "../../database/query";
|
||||
import config from "../../config";
|
||||
@ -13,10 +17,9 @@ const AdminRoute = new Router();
|
||||
|
||||
AdminRoute.use(async (ctx, next) => {
|
||||
const { key } = ctx.query;
|
||||
if (key !== config.admin)
|
||||
throw new NoPermissionError("No permission!");
|
||||
if (key !== config.admin) throw new NoPermissionError("No permission!");
|
||||
return next();
|
||||
})
|
||||
});
|
||||
|
||||
AdminRoute.get("/", async ctx => {
|
||||
//TODO: Main Interface
|
||||
@ -33,26 +36,24 @@ AdminRoute.get("/settings", async ctx => {
|
||||
let res = [["key", "value"]];
|
||||
stream.on("data", ({ key, value }) => {
|
||||
res.push([key, value]);
|
||||
})
|
||||
});
|
||||
|
||||
stream.on("error", no);
|
||||
stream.on("end", () => yes(res))
|
||||
})
|
||||
stream.on("end", () => yes(res));
|
||||
});
|
||||
|
||||
if (ctx.query.view) {
|
||||
return getTable("Settings", res, ctx);
|
||||
} else {
|
||||
ctx.body = res;
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
AdminRoute.get("/data", async ctx => {
|
||||
const { database } = ctx.query;
|
||||
let db = DatabaseManager.getDatabase(database);
|
||||
if (!db)
|
||||
throw new BadRequestError("Database not found");
|
||||
if (!db) throw new BadRequestError("Database not found");
|
||||
let res = await new Promise<string[][]>((yes, no) => {
|
||||
|
||||
const stream = db.data.createReadStream({
|
||||
keys: true,
|
||||
values: true,
|
||||
@ -61,28 +62,37 @@ AdminRoute.get("/data", async ctx => {
|
||||
limit: 1000
|
||||
});
|
||||
let res = [["key", "value"]];
|
||||
stream.on("data", ({ key, value }: { key: string, value: Buffer }) => {
|
||||
res.push([key, key.split("/").length > 2 ? value.toString() : JSON.stringify(MP.decode(value))]);
|
||||
})
|
||||
stream.on("data", ({ key, value }: { key: string; value: Buffer }) => {
|
||||
res.push([
|
||||
key,
|
||||
key.split("/").length > 2
|
||||
? value.toString()
|
||||
: JSON.stringify(MP.decode(value))
|
||||
]);
|
||||
});
|
||||
|
||||
stream.on("error", no);
|
||||
stream.on("end", () => yes(res))
|
||||
})
|
||||
stream.on("end", () => yes(res));
|
||||
});
|
||||
|
||||
if (ctx.query.view) {
|
||||
return getTable("Data from " + database, res, ctx);
|
||||
} else {
|
||||
ctx.body = res;
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
AdminRoute
|
||||
.get("/database", ctx => {
|
||||
AdminRoute.get("/database", ctx => {
|
||||
const isFull = ctx.query.full === "true" || ctx.query.full === "1";
|
||||
let res;
|
||||
if (isFull) {
|
||||
//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 {
|
||||
res = Array.from(DatabaseManager.databases.keys());
|
||||
}
|
||||
@ -92,42 +102,31 @@ AdminRoute
|
||||
} else {
|
||||
ctx.body = res;
|
||||
}
|
||||
})
|
||||
.post("/database", async ctx => {
|
||||
}).post("/database", async ctx => {
|
||||
const { name, rules, publickey, accesskey, rootkey } = ctx.request.body;
|
||||
|
||||
if (!name)
|
||||
throw new BadRequestError("Name must be set!");
|
||||
if (!name) throw new BadRequestError("Name must be set!");
|
||||
|
||||
let db = DatabaseManager.getDatabase(name);
|
||||
if (!db)
|
||||
db = await DatabaseManager.addDatabase(name);
|
||||
if (!db) db = await DatabaseManager.addDatabase(name);
|
||||
|
||||
if (publickey)
|
||||
await db.setPublicKey(publickey);
|
||||
if (publickey) await db.setPublicKey(publickey);
|
||||
|
||||
if (rules)
|
||||
await db.setRules(rules);
|
||||
if (rules) await db.setRules(rules);
|
||||
|
||||
if (accesskey)
|
||||
await db.setAccessKey(accesskey);
|
||||
|
||||
|
||||
if (rootkey)
|
||||
await db.setRootKey(rootkey);
|
||||
if (accesskey) await db.setAccessKey(accesskey);
|
||||
|
||||
if (rootkey) await db.setRootKey(rootkey);
|
||||
|
||||
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");
|
||||
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
|
||||
@ -135,24 +134,23 @@ AdminRoute.get("/collections", async ctx => {
|
||||
let res = [];
|
||||
stream.on("data", (key: string) => {
|
||||
res.push(key);
|
||||
})
|
||||
});
|
||||
|
||||
stream.on("error", no);
|
||||
stream.on("end", () => yes(res))
|
||||
})
|
||||
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");
|
||||
if (!db) throw new BadRequestError("Database not found");
|
||||
|
||||
let deleted = await db.runCleanup();
|
||||
if (ctx.query.view) {
|
||||
@ -160,14 +158,55 @@ AdminRoute.get("/collections/cleanup", async ctx => {
|
||||
} else {
|
||||
ctx.body = deleted;
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
AdminRoute.get("/database/new", getForm("/v1/admin/database", "New/Change Database", {
|
||||
name: { label: "Name", type: "text", },
|
||||
AdminRoute.get(
|
||||
"/database/new",
|
||||
getForm("/v1/admin/database", "New Database", {
|
||||
name: { label: "Name", type: "text" },
|
||||
accesskey: { label: "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" }
|
||||
}))
|
||||
})
|
||||
);
|
||||
|
||||
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;
|
@ -1,7 +1,11 @@
|
||||
import * as Router from "koa-router";
|
||||
import AdminRoute from "./admin";
|
||||
import { DatabaseManager } from "../../database/database";
|
||||
import { NotFoundError, NoPermissionError, BadRequestError } from "../helper/errors";
|
||||
import {
|
||||
NotFoundError,
|
||||
NoPermissionError,
|
||||
BadRequestError
|
||||
} from "../helper/errors";
|
||||
import Logging from "@hibas123/nodelogging";
|
||||
import Session from "../../database/session";
|
||||
import nanoid = require("nanoid");
|
||||
@ -28,7 +32,7 @@ V1.post("/db/:database/query", async ctx => {
|
||||
|
||||
if (db.accesskey) {
|
||||
if (!accesskey || accesskey !== db.accesskey) {
|
||||
throw new NoPermissionError("");
|
||||
throw new NoPermissionError("Invalid Access Key");
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,7 +40,6 @@ V1.post("/db/:database/query", async ctx => {
|
||||
let res = await verifyJWT(authkey, db.publickey);
|
||||
if (!res || !res.uid) {
|
||||
throw new BadRequestError("Invalid JWT");
|
||||
return;
|
||||
} else {
|
||||
session.uid = res.uid;
|
||||
}
|
||||
@ -54,6 +57,6 @@ V1.post("/db/:database/query", async ctx => {
|
||||
throw new BadRequestError(err.message);
|
||||
}
|
||||
throw err;
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
export default V1;
|
@ -7,11 +7,19 @@
|
||||
<title>Admin Interface</title>
|
||||
<link
|
||||
rel="stylesheet"
|
||||
<<<<<<< HEAD
|
||||
href="https://unpkg.com/@hibas123/theme@1/out/base.css"
|
||||
/>
|
||||
<link
|
||||
rel="stylesheet"
|
||||
href="https://unpkg.com/@hibas123/theme@1/out/light.css"
|
||||
=======
|
||||
href="https://unpkg.com/@hibas123/theme/out/base.css"
|
||||
/>
|
||||
<link
|
||||
rel="stylesheet"
|
||||
href="https://unpkg.com/@hibas123/theme/out/light.css"
|
||||
>>>>>>> 0bfdbce908484560108e20cede6f9e7c46710818
|
||||
/>
|
||||
|
||||
<script src="https://unpkg.com/handlebars/dist/handlebars.min.js"></script>
|
||||
@ -60,7 +68,11 @@
|
||||
style="margin: 1rem;"
|
||||
></div>
|
||||
</div>
|
||||
<<<<<<< HEAD
|
||||
<div style="position: relative;">
|
||||
=======
|
||||
<div style="position:relative;">
|
||||
>>>>>>> 0bfdbce908484560108e20cede6f9e7c46710818
|
||||
<iframe id="content"></iframe>
|
||||
</div>
|
||||
</div>
|
||||
@ -102,6 +114,7 @@
|
||||
<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>
|
||||
<button class=btn onclick="loadView('database/update', {database:'${database}'})">Change</button>
|
||||
</div>`
|
||||
)
|
||||
)
|
||||
|
@ -32,19 +32,19 @@
|
||||
<div class="input-group">
|
||||
<label>{{label}}</label>
|
||||
{{#ifCond type "===" "text"}}
|
||||
<input type="text" placeholder="{{label}}" name="{{name}}" value="{{value}}" />
|
||||
<input type="text" placeholder="{{label}}" name="{{name}}" value="{{value}}" {{disabled}} />
|
||||
{{/ifCond}}
|
||||
|
||||
{{#ifCond type "===" "number"}}
|
||||
<input type="number" placeholder="{{label}}" name="{{name}}" value="{{value}}" />
|
||||
<input type="number" placeholder="{{label}}" name="{{name}}" value="{{value}}" {{disabled}} />
|
||||
{{/ifCond}}
|
||||
|
||||
{{#ifCond type "===" "boolean"}}
|
||||
<input type="checkbox" name="{{name}}" checked="{{value}}" />
|
||||
<input type="checkbox" name="{{name}}" checked="{{value}}" {{disabled}} />
|
||||
{{/ifCond}}
|
||||
|
||||
{{#ifCond type "===" "textarea"}}
|
||||
<textarea class="inp" name="{{name}}" rows="20">{{value}}</textarea>
|
||||
<textarea class="inp" name="{{name}}" rows="20" {{disabled}}>{{value}}</textarea>
|
||||
{{/ifCond}}
|
||||
</div>
|
||||
{{/each}}
|
||||
|
Reference in New Issue
Block a user