mirror of
https://github.com/esiur/esiur-js.git
synced 2025-05-06 12:32:58 +00:00
1.6.1
This commit is contained in:
parent
a08519bde8
commit
f5365adec3
1730
build/esiur.js
1730
build/esiur.js
File diff suppressed because it is too large
Load Diff
@ -17,7 +17,7 @@ const WebSocket = require('ws');
|
||||
const http = require("http");
|
||||
const fs = require("fs");
|
||||
|
||||
const wss = new WebSocket.Server({port: 8081});
|
||||
const wss = new WebSocket.Server({port: 8001});
|
||||
|
||||
class MyMembership extends IMembership {
|
||||
userExists(username, domain) {
|
||||
@ -90,7 +90,7 @@ http.createServer(function (req, res) {
|
||||
res.end(data);
|
||||
}
|
||||
});
|
||||
}).listen(8080);
|
||||
}).listen(8000);
|
||||
|
||||
console.log(`HTTP Server running http://localhost:8080/demo/chat/index.html`);
|
||||
console.log(`IIP Server running iip://localhost:8081`);
|
||||
console.log(`HTTP Server running http://localhost:8000/demo/chat/index.html`);
|
||||
console.log(`IIP Server running iip://localhost:8001`);
|
||||
|
@ -15,7 +15,7 @@
|
||||
|
||||
try {
|
||||
status.innerHTML = "Connecting...";
|
||||
service = await wh.get("iip://localhost:8081/chat", {username, password: "1234"});
|
||||
service = await wh.get("iip://localhost:8001/chat", {username, password: "1234"});
|
||||
login.style.display = "none";
|
||||
service.on("message", appendMessage)
|
||||
.on(":title", updateTitle)
|
||||
@ -26,7 +26,7 @@
|
||||
service.users.forEach((x)=>appendUser(x, true));
|
||||
status.innerHTML = "Online";
|
||||
} catch (ex) {
|
||||
status.innerHTML = "Error " + ex.toString;
|
||||
status.innerHTML = "Error " + ex.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
32
demo/mongo/mongo.js
Normal file
32
demo/mongo/mongo.js
Normal file
@ -0,0 +1,32 @@
|
||||
import DistributedConnection from "../../src/Net/IIP/DistributedConnection.js";
|
||||
import Warehouse from "../../src/Resource/Warehouse.js";
|
||||
|
||||
import { createRequire } from 'module'
|
||||
import AsyncReply from "../../src/Core/AsyncReply.js";
|
||||
import DistributedServer from "../../src/Net/IIP/DistributedServer.js";
|
||||
import IMembership from "../../src/Security/Membership/IMembership.js";
|
||||
import WSSocket from "../../src/Net/Sockets/WSSocket.js";
|
||||
import MemoryStore from "../../src/Stores/MemoryStore.js";
|
||||
import DC from "../../src/Data/DataConverter.js";
|
||||
import IResource from "../../src/Resource/IResource.js";
|
||||
import Structure from "../../src/Data/Structure.js";
|
||||
import MongoDBStore from "../../src/Stores/MongoDBStore.js";
|
||||
|
||||
|
||||
class User extends IResource {
|
||||
static get template() {
|
||||
return {
|
||||
properties: [{name: "username"}, {name: "password"}]
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
let db = await Warehouse.new(MongoDBStore, "db");
|
||||
|
||||
await Warehouse.open();
|
||||
|
||||
let admin = await Warehouse.new(User, "admin", db, null, null, null, {username: "admin", password: "1234"});
|
||||
|
||||
let old = await db.get("id/6053437f389bee15089d8f85");
|
||||
|
||||
console.log("Count : " + db.resources.size);
|
1617
package-lock.json
generated
1617
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
13
package.json
13
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "esiur",
|
||||
"version": "1.6.0",
|
||||
"version": "1.6.1",
|
||||
"description": "Distributed Object Framework",
|
||||
"main": "esiur.js",
|
||||
"type": "module",
|
||||
@ -21,14 +21,15 @@
|
||||
"homepage": "https://github.com/esiur/esiur-js#readme",
|
||||
"dependencies": {
|
||||
"@babel/polyfill": "^7.12.1",
|
||||
"@babel/runtime": "^7.12.5",
|
||||
"@babel/runtime": "^7.13.10",
|
||||
"mongodb": "^3.6.6",
|
||||
"ws": "^6.2.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "^7.12.1",
|
||||
"@babel/core": "^7.12.3",
|
||||
"@babel/plugin-transform-runtime": "^7.12.1",
|
||||
"@babel/preset-env": "^7.12.1",
|
||||
"@babel/cli": "^7.13.14",
|
||||
"@babel/core": "^7.13.15",
|
||||
"@babel/plugin-transform-runtime": "^7.13.15",
|
||||
"@babel/preset-env": "^7.13.15",
|
||||
"babel-cli": "^6.0.0",
|
||||
"babelify": "^10.0.0",
|
||||
"browserify": "^16.5.2"
|
||||
|
@ -108,7 +108,7 @@ export default class ResourceTemplate {
|
||||
|
||||
//byte currentIndex = 0;
|
||||
|
||||
for (var i = 0; i < template.properties.length; i++) {
|
||||
for (var i = 0; i < template.properties?.length; i++) {
|
||||
var pt = new PropertyTemplate();
|
||||
pt.name = template.properties[i].name;
|
||||
pt.index = i;
|
||||
@ -118,7 +118,7 @@ export default class ResourceTemplate {
|
||||
this.properties.push(pt);
|
||||
}
|
||||
|
||||
for (var i = 0; i < template.events.length; i++) {
|
||||
for (var i = 0; i < template.events?.length; i++) {
|
||||
var et = new EventTemplate();
|
||||
et.name = template.events[i].name;
|
||||
et.index = i;
|
||||
@ -127,7 +127,7 @@ export default class ResourceTemplate {
|
||||
this.events.push(et);
|
||||
}
|
||||
|
||||
for (var i = 0; i < template.functions.length; i++) {
|
||||
for (var i = 0; i < template.functions?.length; i++) {
|
||||
var ft = new FunctionTemplate();
|
||||
ft.name = template.functions[i].name;
|
||||
ft.index = i;
|
||||
|
@ -152,7 +152,9 @@ export class WH extends IEventHandler
|
||||
|
||||
remove(resource)
|
||||
{
|
||||
|
||||
if (resource.instance == null)
|
||||
return;
|
||||
|
||||
if (this.resources.contains(resource.instance.id))
|
||||
this.resources.remove(resource.instance.id);
|
||||
else
|
||||
@ -430,7 +432,7 @@ export class WH extends IEventHandler
|
||||
|
||||
for(var i = 0; i < ar.length; i++)
|
||||
if (!ar[i])
|
||||
console.log(`Resource failed at Initialize ${self.resources.at(i).Instance.Name} [${self.resources.at(i).Instance.Template.ClassName}]`);
|
||||
console.log(`Resource failed at Initialize ${self.resources.at(i).instance.name} [${self.resources.at(i).instance.template.className}]`);
|
||||
|
||||
var sysBag = new AsyncBag();
|
||||
|
||||
@ -444,7 +446,7 @@ export class WH extends IEventHandler
|
||||
sysBag.then(ar2 => {
|
||||
for(var i = 0; i < ar2.length; i++)
|
||||
if (!ar2[i])
|
||||
console.log(`Resource failed at Initialize ${self.resources.at(i).Instance.Name} [${self.resources.at(i).Instance.Template.ClassName}]`);
|
||||
console.log(`Resource failed at Initialize ${self.resources.at(i).instance.name} [${self.resources.at(i).instance.template.className}]`);
|
||||
|
||||
self.warehouseIsOpen = true;
|
||||
rt.trigger(true);
|
||||
|
398
src/Stores/MongoDBStore.js
Normal file
398
src/Stores/MongoDBStore.js
Normal file
@ -0,0 +1,398 @@
|
||||
/*
|
||||
* Copyright (c) 2017-2021 Ahmed Kh. Zamil
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Created by Ahmed Zamil on 2/18/2021.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
import { ResourceTrigger } from '../Resource/IResource.js';
|
||||
import IStore from '../Resource/IStore.js'
|
||||
import AsyncReply from '../Core/AsyncReply.js';
|
||||
import Codec from '../Data/Codec.js';
|
||||
import Warehouse from '../Resource/Warehouse.js';
|
||||
import DataType from '../Data/DataType.js';
|
||||
import AsyncBag from '../Core/AsyncBag.js';
|
||||
import ErrorType from '../Core/ErrorType.js';
|
||||
import ExceptionCode from '../Core/ExceptionCode.js';
|
||||
import { createRequire } from 'module'
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
|
||||
export default class MongoDBStore extends IStore
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
super();
|
||||
this.client = require('mongodb').MongoClient;
|
||||
this.resources = new Map();
|
||||
this.classes = new Map();
|
||||
}
|
||||
|
||||
compose(value)
|
||||
{
|
||||
let type = Codec.getDataType(value, null);
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case DataType.Void:
|
||||
// nothing to do;
|
||||
return null;
|
||||
|
||||
case DataType.String:
|
||||
return value;
|
||||
|
||||
case DataType.Resource:
|
||||
case DataType.DistributedResource:
|
||||
return { "type": 0 , "link": value.instance.link };
|
||||
|
||||
case DataType.Structure:
|
||||
return this.composeStructure(value);
|
||||
|
||||
case DataType.VarArray:
|
||||
return this.composeVarArray(value);
|
||||
|
||||
case DataType.ResourceArray:
|
||||
return this.composeResourceArray(value);
|
||||
|
||||
case DataType.StructureArray:
|
||||
return this.composeStructureArray(value);
|
||||
|
||||
default:
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
parse(value)
|
||||
{
|
||||
if (value instanceof Array)
|
||||
{
|
||||
let bag = new AsyncBag();
|
||||
|
||||
for(let i = 0; i < value.length; i++)
|
||||
bag.add(this.parse(value[i]));
|
||||
|
||||
bag.seal();
|
||||
|
||||
return bag;
|
||||
}
|
||||
else if (value?.type !== undefined)
|
||||
{
|
||||
if (value.type == 0)
|
||||
{
|
||||
return Warehouse.get(value.link);
|
||||
} // structure
|
||||
else if (value.type == 1)
|
||||
{
|
||||
var bag = new AsyncBag();
|
||||
var rt = new AsyncReply();
|
||||
|
||||
let s = new Structure();
|
||||
|
||||
for (let i = 0; i < value.values.length; i++)
|
||||
bag.add(this.parse(value.values[i].value));
|
||||
|
||||
bag.seal();
|
||||
bag.then((x) =>
|
||||
{
|
||||
for (let i = 0; i < x.Length; i++)
|
||||
s[value.values[i].name] = x[i];
|
||||
|
||||
rt.trigger(s);
|
||||
});
|
||||
|
||||
return rt;
|
||||
}
|
||||
else
|
||||
return new AsyncReply(null);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new AsyncReply(value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
addClass(type)
|
||||
{
|
||||
let template = type.template;
|
||||
let className = template.namespace + "." + type.prototype.constructor.name;
|
||||
this.classes.set(className, type);
|
||||
}
|
||||
|
||||
fetch(id)
|
||||
{
|
||||
let self = this;
|
||||
|
||||
if (this.resources.has(id))
|
||||
return new AsyncReply(this.resources.get(id));
|
||||
|
||||
let rt = new AsyncReply();
|
||||
|
||||
|
||||
this.db.collection(this.collection).findOne({_id: id}, function(err, result) {
|
||||
if (err){
|
||||
rt.triggerError(err);
|
||||
} else {
|
||||
var doc = request.result;
|
||||
|
||||
if (doc == null)
|
||||
{
|
||||
//rt.triggerError(ErrorType.Management, ExceptionCode.ResourceNotFound);
|
||||
rt.trigger(null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!self.classes.has(doc.className))
|
||||
{
|
||||
rt.triggerError(ErrorType.Management, ExceptionCode.ClassNotFound);
|
||||
return;
|
||||
}
|
||||
|
||||
//let r = await Warehouse.new(, doc.name, this, null, null, this);
|
||||
let type = self.classes.get(doc.className);
|
||||
var proxyType = ResourceProxy.getProxy(type);
|
||||
var resource = new proxyType();
|
||||
self.resources.set(doc.id, resource);
|
||||
|
||||
resource._id = doc.id;
|
||||
|
||||
Warehouse.put(resource, doc.name, self, null, null, null, null).then(ok=>{
|
||||
self.parse(doc.attributes).then(attributes=>{
|
||||
|
||||
resource.instance.setAttributes(attributes);
|
||||
|
||||
// Apply store managers
|
||||
for (var i = 0; i < self.instance.managers.length; i++)
|
||||
resource.instance.managers.add(self.instance.managers[i]);
|
||||
|
||||
// Load values
|
||||
|
||||
var bag = new AsyncBag();
|
||||
|
||||
for(var i = 0; i < doc.values.length; i++)
|
||||
{
|
||||
let v = doc.values[i];
|
||||
|
||||
bag.add(self.parse(v.value));
|
||||
//var x = await this.parse(v.value);
|
||||
resource.instance.loadProperty(v.name, v.age, v.modification, x);
|
||||
}
|
||||
|
||||
bag.seal();
|
||||
|
||||
bag.then(ar=>{
|
||||
for(var i = 0; i < ar.length; i++)
|
||||
{
|
||||
let v = doc.values[i];
|
||||
resource.instance.loadProperty(v.name, v.age, v.modification, ar[i]);
|
||||
}
|
||||
|
||||
rt.trigger(resource);
|
||||
}).error(ex => rt.triggerError(ex));
|
||||
}).error(ex => rt.triggerError(ex));
|
||||
}).error(ex => rt.triggerError(ex));
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
put(resource)
|
||||
{
|
||||
if (resource == this)
|
||||
return;
|
||||
|
||||
let rt = new AsyncReply();
|
||||
|
||||
let self = this;
|
||||
|
||||
let attrs = resource.instance.getAttributes();
|
||||
|
||||
var record = {
|
||||
className: resource.instance.template.className,
|
||||
name: resource.instance.name,
|
||||
attributes: this.composeStructure(attrs),
|
||||
};
|
||||
|
||||
if (resource._id != null)
|
||||
record.id = resource._id;
|
||||
|
||||
// copy resource data
|
||||
let props = resource.instance.template.properties;
|
||||
|
||||
var snap = {};
|
||||
for(var i = 0; i < props.length; i++)
|
||||
{
|
||||
let pt = props[i];
|
||||
snap[pt.name] = { "age": resource.instance.getAge(pt.index),
|
||||
"modification": resource.instance.getModificationDate(pt.index),
|
||||
"value": this.compose(resource[pt.name])
|
||||
};
|
||||
}
|
||||
|
||||
record.values = snap;
|
||||
|
||||
this.db.collection("resources").insertOne(record, function(err, res) {
|
||||
if (err) {
|
||||
rt.trigger(false);
|
||||
} else {
|
||||
console.log("1 document inserted " + res.insertedId);
|
||||
resource["_id"] = res.insertedId;
|
||||
self.resources.set(res.insertedId, resource);
|
||||
rt.trigger(true);
|
||||
}
|
||||
});
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
// retrive(id)
|
||||
// {
|
||||
// let rt = new AsyncReply();
|
||||
|
||||
// var transaction = this.db.transaction(["resources"]);
|
||||
// var objectStore = transaction.objectStore("resources");
|
||||
// var request = objectStore.get(id);
|
||||
|
||||
// request.onerror = function(event) {
|
||||
// rt.trigger(null);
|
||||
// };
|
||||
|
||||
// request.onsuccess = function(event) {
|
||||
// rt.trigger(request.result);
|
||||
// };
|
||||
|
||||
// return rt;
|
||||
// }
|
||||
|
||||
get(path)
|
||||
{
|
||||
var p = path.split('/');
|
||||
if (p.length == 2)
|
||||
if (p[0] == "id")
|
||||
{
|
||||
// load from Id
|
||||
return this.fetch(p[1]);
|
||||
}
|
||||
|
||||
return new AsyncReply(null);
|
||||
}
|
||||
|
||||
|
||||
link(resource)
|
||||
{
|
||||
if (resource.instance.store == this)
|
||||
return this.instance.name + "/" + resource._id;
|
||||
}
|
||||
|
||||
trigger(trigger)
|
||||
{
|
||||
if (trigger == ResourceTrigger.Initialize)
|
||||
{
|
||||
let rt = new AsyncReply();
|
||||
|
||||
this.dbName = this.instance.attributes.item("database") ?? "ejs";
|
||||
this.collection = this.instance.attributes.item("collection") ?? "resources";
|
||||
this.connection = this.instance.attributes.item("connection") ?? "mongodb://localhost:27017";
|
||||
|
||||
let self = this;
|
||||
|
||||
this.client.connect(this.connection, function(err, client) {
|
||||
if (err) {
|
||||
rt.trigger(false);
|
||||
console.log(err);
|
||||
}
|
||||
else
|
||||
{
|
||||
console.log("Database created!");
|
||||
let db = client.db(self.dbName);
|
||||
db.createCollection(self.collection, function(err, res) {
|
||||
//if (err)
|
||||
// console.log(err);
|
||||
if (err == null)
|
||||
console.log("Collection created!");
|
||||
|
||||
rt.trigger(true);
|
||||
});
|
||||
|
||||
self.db = db;
|
||||
//db.close();
|
||||
}
|
||||
});
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
return new AsyncReply(true);
|
||||
}
|
||||
|
||||
record(resource, propertyName, value, age, dateTime)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
getRecord(resource, fromDate, toDate)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
composeStructure(value)
|
||||
{
|
||||
return {type: 1, values: value.toObject()};
|
||||
}
|
||||
|
||||
composeVarArray(array)
|
||||
{
|
||||
var rt = [];
|
||||
|
||||
for (var i = 0; i < array.length; i++)
|
||||
rt.push(this.compose(array[i]));
|
||||
return rt;
|
||||
}
|
||||
|
||||
composeStructureArray(structures)
|
||||
{
|
||||
var rt = [];
|
||||
|
||||
if (structures == null || structures.Length == 0)
|
||||
return rt;
|
||||
|
||||
for(var i = 0; i < structures.length; i++)
|
||||
rt.push(this.composeStructure(structures[s]));
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
composeResourceArray(array)
|
||||
{
|
||||
var rt = [];
|
||||
for (var i = 0; i < array.length; i++)
|
||||
rt.push({ "type": 0 , "link": array[i].instance.link });
|
||||
return rt;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user