aboutsummaryrefslogtreecommitdiff
path: root/src/server
diff options
context:
space:
mode:
Diffstat (limited to 'src/server')
-rw-r--r--src/server/Client.ts1
-rw-r--r--src/server/Message.ts137
-rw-r--r--src/server/ServerUtil.ts99
-rw-r--r--src/server/database.ts21
-rw-r--r--src/server/index.ts20
5 files changed, 71 insertions, 207 deletions
diff --git a/src/server/Client.ts b/src/server/Client.ts
index b4c419438..e6f953712 100644
--- a/src/server/Client.ts
+++ b/src/server/Client.ts
@@ -8,5 +8,4 @@ export class Client {
}
@computed public get GUID(): string { return this._guid; }
-
} \ No newline at end of file
diff --git a/src/server/Message.ts b/src/server/Message.ts
index d22e5c17c..bbe4ffcad 100644
--- a/src/server/Message.ts
+++ b/src/server/Message.ts
@@ -1,146 +1,35 @@
import { Utils } from "../Utils";
export class Message<T> {
- private name: string;
- private guid: string;
-
- get Name(): string {
- return this.name;
- }
-
- get Message(): string {
- return this.guid;
- }
+ private _name: string;
+ private _guid: string;
constructor(name: string) {
- this.name = name;
- this.guid = Utils.GenerateDeterministicGuid(name);
- }
-
- GetValue() {
- return this.Name;
- }
-}
-
-class TestMessageArgs {
- hello: string = "";
-}
-
-export class SetFieldArgs {
- field: string;
- value: any;
-
- constructor(f: string, v: any) {
- this.field = f;
- this.value = v;
+ this._name = name;
+ this._guid = Utils.GenerateDeterministicGuid(name);
}
-}
-export class GetFieldArgs {
- field: string;
-
- constructor(f: string) {
- this.field = f;
- }
+ get Name(): string { return this._name; }
+ get Message(): string { return this._guid; }
}
export enum Types {
- Number,
- List,
- Key,
- Image,
- Web,
- Document,
- Text,
- RichText,
- DocumentReference,
- Html,
- Video,
- Audio,
- Ink,
- PDF,
- Tuple,
- HistogramOp,
- Boolean,
- Script,
-}
-
-export class DocumentTransfer implements Transferable {
- readonly type = Types.Document;
- _id: string;
-
- constructor(
- readonly obj: { type: Types; data: [string, string][]; _id: string }
- ) {
- this._id = obj._id;
- }
-}
-
-export class ImageTransfer implements Transferable {
- readonly type = Types.Image;
-
- constructor(readonly _id: string) { }
-}
-
-export class KeyTransfer implements Transferable {
- name: string;
- readonly _id: string;
- readonly type = Types.Key;
-
- constructor(i: string, n: string) {
- this.name = n;
- this._id = i;
- }
-}
-
-export class ListTransfer implements Transferable {
- type = Types.List;
-
- constructor(readonly _id: string) { }
-}
-
-export class NumberTransfer implements Transferable {
- readonly type = Types.Number;
-
- constructor(readonly value: number, readonly _id: string) { }
-}
-
-export class TextTransfer implements Transferable {
- value: string;
- readonly _id: string;
- readonly type = Types.Text;
-
- constructor(t: string, i: string) {
- this.value = t;
- this._id = i;
- }
-}
-
-export class RichTextTransfer implements Transferable {
- value: string;
- readonly _id: string;
- readonly type = Types.Text;
-
- constructor(t: string, i: string) {
- this.value = t;
- this._id = i;
- }
+ Number, List, Key, Image, Web, Document, Text, RichText, DocumentReference,
+ Html, Video, Audio, Ink, PDF, Tuple, HistogramOp, Boolean, Script,
}
export interface Transferable {
- readonly _id: string;
+ readonly id: string;
readonly type: Types;
+ readonly data?: any;
}
export namespace MessageStore {
export const Foo = new Message<string>("Foo");
export const Bar = new Message<string>("Bar");
- export const AddDocument = new Message<DocumentTransfer>("Add Document");
- export const SetField = new Message<{ _id: string; data: any; type: Types }>(
- "Set Field"
- );
- export const GetField = new Message<string>("Get Field");
- export const GetFields = new Message<string[]>("Get Fields");
+ export const SetField = new Message<Transferable>("Set Field"); // send Transferable (no reply)
+ export const GetField = new Message<string>("Get Field"); // send string 'id' get Transferable back
+ export const GetFields = new Message<string[]>("Get Fields"); // send string[] of 'id' get Transferable[] back
export const GetDocument = new Message<string>("Get Document");
export const DeleteAll = new Message<any>("Delete All");
}
diff --git a/src/server/ServerUtil.ts b/src/server/ServerUtil.ts
index dc8687b7e..818230c1a 100644
--- a/src/server/ServerUtil.ts
+++ b/src/server/ServerUtil.ts
@@ -1,83 +1,56 @@
-import { Field } from "./../fields/Field";
-import { TextField } from "./../fields/TextField";
-import { NumberField } from "./../fields/NumberField";
-import { RichTextField } from "./../fields/RichTextField";
-import { Key } from "./../fields/Key";
-import { ImageField } from "./../fields/ImageField";
-import { ListField } from "./../fields/ListField";
-import { Document } from "./../fields/Document";
-import { Server } from "./../client/Server";
-import { Types } from "./Message";
-import { Utils } from "../Utils";
-import { HtmlField } from "../fields/HtmlField";
-import { WebField } from "../fields/WebField";
+import { HistogramField } from "../client/northstar/dash-fields/HistogramField";
import { AudioField } from "../fields/AudioField";
-import { VideoField } from "../fields/VideoField";
+import { BooleanField } from "../fields/BooleanField";
+import { HtmlField } from "../fields/HtmlField";
import { InkField } from "../fields/InkField";
import { PDFField } from "../fields/PDFField";
-import { TupleField } from "../fields/TupleField";
-import { BooleanField } from "../fields/BooleanField";
-import { HistogramField } from "../client/northstar/dash-fields/HistogramField";
import { ScriptField } from "../fields/ScriptField";
+import { TupleField } from "../fields/TupleField";
+import { VideoField } from "../fields/VideoField";
+import { WebField } from "../fields/WebField";
+import { Utils } from "../Utils";
+import { Document } from "./../fields/Document";
+import { Field } from "./../fields/Field";
+import { ImageField } from "./../fields/ImageField";
+import { Key } from "./../fields/Key";
+import { ListField } from "./../fields/ListField";
+import { NumberField } from "./../fields/NumberField";
+import { RichTextField } from "./../fields/RichTextField";
+import { TextField } from "./../fields/TextField";
+import { Transferable, Types } from "./Message";
export class ServerUtils {
public static prepend(extension: string): string {
return window.location.origin + extension;
}
- public static FromJson(json: any): Field {
- let obj = json;
- let data: any = obj.data;
- let id: string = obj._id;
- let type: Types = obj.type;
+ public static FromJson(json: Transferable): Field {
- if (!(data !== undefined && id && type !== undefined)) {
+ if (!(json.data !== undefined && json.id && json.type !== undefined)) {
console.log(
"how did you manage to get an object that doesn't have a data or an id?"
);
return new TextField("Something to fill the space", Utils.GenerateGuid());
}
- switch (type) {
- case Types.Boolean:
- return new BooleanField(data, id, false);
- case Types.Number:
- return new NumberField(data, id, false);
- case Types.Text:
- return new TextField(data, id, false);
- case Types.Html:
- return new HtmlField(data, id, false);
- case Types.Web:
- return new WebField(new URL(data), id, false);
- case Types.RichText:
- return new RichTextField(data, id, false);
- case Types.Key:
- return new Key(data, id, false);
- case Types.Image:
- return new ImageField(new URL(data), id, false);
- case Types.HistogramOp:
- return HistogramField.FromJson(id, data);
- case Types.PDF:
- return new PDFField(new URL(data), id, false);
- case Types.List:
- return ListField.FromJson(id, data);
- case Types.Script:
- return ScriptField.FromJson(id, data);
- case Types.Audio:
- return new AudioField(new URL(data), id, false);
- case Types.Video:
- return new VideoField(new URL(data), id, false);
- case Types.Tuple:
- return new TupleField(data, id, false);
- case Types.Ink:
- return InkField.FromJson(id, data);
- case Types.Document:
- let doc: Document = new Document(id, false);
- let fields: { key: string, field: string }[] = data as { key: string, field: string }[];
- fields.forEach(element => {
- doc._proxies.set(element.key, element.field);
- });
- return doc;
+ switch (json.type) {
+ case Types.Boolean: return new BooleanField(json.data, json.id, false);
+ case Types.Number: return new NumberField(json.data, json.id, false);
+ case Types.Text: return new TextField(json.data, json.id, false);
+ case Types.Html: return new HtmlField(json.data, json.id, false);
+ case Types.Web: return new WebField(new URL(json.data), json.id, false);
+ case Types.RichText: return new RichTextField(json.data, json.id, false);
+ case Types.Key: return new Key(json.data, json.id, false);
+ case Types.Image: return new ImageField(new URL(json.data), json.id, false);
+ case Types.HistogramOp: return HistogramField.FromJson(json.id, json.data);
+ case Types.PDF: return new PDFField(new URL(json.data), json.id, false);
+ case Types.List: return ListField.FromJson(json.id, json.data);
+ case Types.Script: return ScriptField.FromJson(json.id, json.data);
+ case Types.Audio: return new AudioField(new URL(json.data), json.id, false);
+ case Types.Video: return new VideoField(new URL(json.data), json.id, false);
+ case Types.Tuple: return new TupleField(json.data, json.id, false);
+ case Types.Ink: return InkField.FromJson(json.id, json.data);
+ case Types.Document: return Document.FromJson(json.data, json.id, false);
default:
throw Error(
"Error, unrecognized field type received from server. If you just created a new field type, be sure to add it here"
diff --git a/src/server/database.ts b/src/server/database.ts
index 4011f26bd..5c70da931 100644
--- a/src/server/database.ts
+++ b/src/server/database.ts
@@ -1,20 +1,21 @@
import * as mongodb from 'mongodb';
+import { Transferable } from './Message';
export class Database {
public static DocumentsCollection = 'documents';
public static Instance = new Database();
private MongoClient = mongodb.MongoClient;
private url = 'mongodb://localhost:27017/Dash';
- private currentWrites: { [_id: string]: Promise<void> } = {};
+ private currentWrites: { [id: string]: Promise<void> } = {};
private db?: mongodb.Db;
constructor() {
this.MongoClient.connect(this.url, (err, client) => this.db = client.db());
}
- public update(id: string, value: any, callback: () => void, collectionName = Database.DocumentsCollection) {
+ public update(id: string, value: any, callback: () => void) {
if (this.db) {
- let collection = this.db.collection(collectionName);
+ let collection = this.db.collection('documents');
const prom = this.currentWrites[id];
let newProm: Promise<void>;
const run = (): Promise<void> => {
@@ -42,7 +43,7 @@ export class Database {
}
public delete(id: string, collectionName = Database.DocumentsCollection) {
- this.db && this.db.collection(collectionName).remove({ _id: id });
+ this.db && this.db.collection(collectionName).remove({ id: id });
}
public deleteAll(collectionName = Database.DocumentsCollection): Promise<any> {
@@ -56,18 +57,18 @@ export class Database {
);
}
- public getDocument(id: string, fn: (res: any) => void, collectionName = Database.DocumentsCollection) {
- this.db && this.db.collection(collectionName).findOne({ _id: id }, (err, result) =>
- fn(result ? result : undefined));
+ public getDocument(id: string, fn: (result?: Transferable) => void, collectionName = Database.DocumentsCollection) {
+ this.db && this.db.collection(collectionName).findOne({ id: id }, (err, result) =>
+ fn(result ? ({ id: result._id, type: result.type, data: result.data }) : undefined))
}
- public getDocuments(ids: string[], fn: (res: any) => void, collectionName = Database.DocumentsCollection) {
- this.db && this.db.collection(collectionName).find({ _id: { "$in": ids } }).toArray((err, docs) => {
+ public getDocuments(ids: string[], fn: (result: Transferable[]) => void, collectionName = Database.DocumentsCollection) {
+ this.db && this.db.collection(collectionName).find({ id: { "$in": ids } }).toArray((err, docs) => {
if (err) {
console.log(err.message);
console.log(err.errmsg);
}
- fn(docs);
+ fn(docs.map(doc => ({ id: doc._id, type: doc.type, data: doc.data })));
});
}
diff --git a/src/server/index.ts b/src/server/index.ts
index fef26f78a..bea84c6ed 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -36,7 +36,7 @@ import { Search } from './Search';
const MongoStore = require('connect-mongo')(session);
const mongoose = require('mongoose');
-const download = (url: string, dest: fs.PathLike) => request.get(url).pipe(fs.createWriteStream(dest));;
+const download = (url: string, dest: fs.PathLike) => request.get(url).pipe(fs.createWriteStream(dest));
const mongoUrl = 'mongodb://localhost:27017/Dash';
mongoose.connect(mongoUrl);
@@ -88,9 +88,11 @@ function addSecureRoute(method: Method,
...subscribers: string[]
) {
let abstracted = (req: express.Request, res: express.Response) => {
- const dashUser: DashUserModel = req.user;
- if (!dashUser) return onRejection(res);
- handler(dashUser, res, req);
+ if (req.user) {
+ handler(req.user, res, req);
+ } else {
+ onRejection(res);
+ }
};
subscribers.forEach(route => {
switch (method) {
@@ -253,20 +255,20 @@ function barReceived(guid: String) {
clients[guid.toString()] = new Client(guid.toString());
}
-function getField([id, callback]: [string, (result: any) => void]) {
- Database.Instance.getDocument(id, (result: any) =>
+function getField([id, callback]: [string, (result?: Transferable) => void]) {
+ Database.Instance.getDocument(id, (result?: Transferable) =>
callback(result ? result : undefined));
}
-function getFields([ids, callback]: [string[], (result: any) => void]) {
+function getFields([ids, callback]: [string[], (result: Transferable[]) => void]) {
Database.Instance.getDocuments(ids, callback);
}
function setField(socket: Socket, newValue: Transferable) {
- Database.Instance.update(newValue._id, newValue, () =>
+ Database.Instance.update(newValue.id, newValue, () =>
socket.broadcast.emit(MessageStore.SetField.Message, newValue));
if (newValue.type === Types.Text) {
- Search.Instance.updateDocument({ id: newValue._id, data: (newValue as any).data });
+ Search.Instance.updateDocument({ id: newValue.id, data: (newValue as any).data });
}
}