diff --git a/build/esiur.js b/build/esiur.js index fd0474d..03fc5bd 100644 --- a/build/esiur.js +++ b/build/esiur.js @@ -1945,22 +1945,38 @@ var Codec = /*#__PURE__*/function () { } var end = offset + length; - var result = data.getUint8(offset++); - var previous = null; - var classId = null; - if (result == _RecordComparisonResult["default"].Null) previous = new _AsyncReply["default"](null);else if (result == _RecordComparisonResult["default"].Record) { - var cs = data.getUint32(offset); - var recordLength = cs - 16; - offset += 4; - classId = data.getGuid(offset); - offset += 16; - previous = Codec.parseRecord(data, offset, recordLength, connection, classId); - offset += recordLength; - } - reply.Add(previous); + var isTyped = (data.getUint8(offset) & 0x10) == 0x10; + var result = data.getUint8(offset++) & 0xF; - while (offset < end) { - result = data.getUint8(offset++); + if (isTyped) { + var classId = data.getGuid(offset); + offset += 16; + var template = Warehouse.getTemplateByClassId(classId, TemplateType.Record); + reply.arrayType = template.definedType; + var previous = null; + if (result == _RecordComparisonResult["default"].Null) previous = new _AsyncReply["default"](null);else if (result == _RecordComparisonResult["default"].Record || result == _RecordComparisonResult["default"].RecordSameType) { + var cs = data.getUint32(offset); + var recordLength = cs; + offset += 4; + previous = Codec.parseRecord(data, offset, recordLength, connection, classId); + offset += recordLength; + } + reply.add(previous); + + while (offset < end) { + result = data.getUint8(offset++); + if (result == _RecordComparisonResult["default"].Null) previous = new _AsyncReply["default"](null);else if (result == _RecordComparisonResult["default"].Record || result == _RecordComparisonResult["default"].RecordSameType) { + var cs = data.getUint32(offset); + offset += 4; + previous = Codec.parseRecord(data, offset, cs, connection, classId); + offset += cs; + } else if (result == _RecordComparisonResult["default"].Same) {// do nothing + } + reply.add(previous); + } + } else { + var previous = null; + var classId = null; if (result == _RecordComparisonResult["default"].Null) previous = new _AsyncReply["default"](null);else if (result == _RecordComparisonResult["default"].Record) { var cs = data.getUint32(offset); var recordLength = cs - 16; @@ -1969,18 +1985,84 @@ var Codec = /*#__PURE__*/function () { offset += 16; previous = Codec.parseRecord(data, offset, recordLength, connection, classId); offset += recordLength; - } else if (result == _RecordComparisonResult["default"].RecordSameType) { - var cs = data.getUint32(offset); - offset += 4; - previous = Codec.parseRecord(data, offset, cs, connection, classId); - offset += cs; - } else if (result == _RecordComparisonResult["default"].Same) {// do nothing } - reply.add(previous); + reply.Add(previous); + + while (offset < end) { + result = data.getUint8(offset++); + if (result == _RecordComparisonResult["default"].Null) previous = new _AsyncReply["default"](null);else if (result == _RecordComparisonResult["default"].Record) { + var cs = data.getUint32(offset); + var recordLength = cs - 16; + offset += 4; + classId = data.getGuid(offset); + offset += 16; + previous = Codec.parseRecord(data, offset, recordLength, connection, classId); + offset += recordLength; + } else if (result == _RecordComparisonResult["default"].RecordSameType) { + var cs = data.getUint32(offset); + offset += 4; + previous = ParseRecord(data, offset, cs, connection, classId); + offset += cs; + } else if (result == _RecordComparisonResult["default"].Same) {// do nothing + } + reply.add(previous); + } } - reply.seal(); - return reply; + reply.Seal(); + return reply; // var reply = new AsyncBag(); + // if (length == 0) + // { + // reply.seal(); + // return reply; + // } + // var end = offset + length; + // var result = data.getUint8(offset++); + // var previous = null; + // var classId = null; + // if (result == RecordComparisonResult.Null) + // previous = new AsyncReply(null); + // else if (result == RecordComparisonResult.Record) + // { + // var cs = data.getUint32(offset); + // var recordLength = cs - 16; + // offset += 4; + // classId = data.getGuid(offset); + // offset += 16; + // previous = Codec.parseRecord(data, offset, recordLength, connection, classId); + // offset += recordLength; + // } + // reply.Add(previous); + // while (offset < end) + // { + // result = data.getUint8(offset++); + // if (result == RecordComparisonResult.Null) + // previous = new AsyncReply(null); + // else if (result == RecordComparisonResult.Record) + // { + // var cs = data.getUint32(offset); + // var recordLength = cs - 16; + // offset += 4; + // classId = data.getGuid(offset); + // offset += 16; + // previous = Codec.parseRecord(data, offset, recordLength, connection, classId); + // offset += recordLength; + // } + // else if (result == RecordComparisonResult.RecordSameType) + // { + // var cs = data.getUint32(offset); + // offset += 4; + // previous = Codec.parseRecord(data, offset, cs, connection, classId); + // offset += cs; + // } + // else if (result == RecordComparisonResult.Same) + // { + // // do nothing + // } + // reply.add(previous); + // } + // reply.seal(); + // return reply; } }, { key: "parseRecord", @@ -1994,12 +2076,12 @@ var Codec = /*#__PURE__*/function () { length -= 16; } - var template = Warehouse.getTemplateByClassId(classId); + var template = Warehouse.getTemplateByClassId(classId, TemplateType.Record); if (template != null) { Codec.parseVarArray(data, offset, length, connection).then(function (ar) { - if (template.resourceType != null) { - var record = new template.resourceType(); + if (template.definedType != null) { + var record = new template.definedType(); for (var i = 0; i < template.properties.length; i++) { record[template.properties[i].name] = ar[i]; @@ -3747,7 +3829,7 @@ var _ExceptionCode = _interopRequireDefault(require("../../Core/ExceptionCode.js var _DistributedResource = _interopRequireDefault(require("./DistributedResource.js")); -var _ResourceTemplate = _interopRequireDefault(require("../../Resource/Template/ResourceTemplate.js")); +var _TypeTemplate = _interopRequireDefault(require("../../Resource/Template/TypeTemplate.js")); var _DistributedResourceQueueItem = _interopRequireDefault(require("./DistributedResourceQueueItem.js")); @@ -3771,6 +3853,8 @@ var _HostAuthentication = _interopRequireDefault(require("../../Security/Authori var _SocketState = _interopRequireDefault(require("../Sockets/SocketState.js")); +var _TemplateType = _interopRequireDefault(require("../../Resource/Template/TemplateType.js")); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } @@ -4074,7 +4158,7 @@ var DistributedConnection = /*#__PURE__*/function (_IStore) { case _IIPPacketAction["default"].TemplateFromClassName: case _IIPPacketAction["default"].TemplateFromClassId: case _IIPPacketAction["default"].TemplateFromResourceId: - this.IIPReply(packet.callbackId, _ResourceTemplate["default"].parse(packet.content)); + this.IIPReply(packet.callbackId, _TypeTemplate["default"].parse(packet.content)); break; case _IIPPacketAction["default"].QueryLink: @@ -5151,7 +5235,7 @@ var DistributedConnection = /*#__PURE__*/function (_IStore) { var templates = []; for (var i = 0; i < list.length; i++) { - templates = templates.concat(_ResourceTemplate["default"].getDependencies(list[i].instance.template).filter(function (x) { + templates = templates.concat(_TypeTemplate["default"].getDependencies(list[i].instance.template).filter(function (x) { return !templates.includes(x); })); } @@ -5173,12 +5257,14 @@ var DistributedConnection = /*#__PURE__*/function (_IStore) { value: function IIPRequestTemplateFromClassName(callback, className) { var self = this; - _Warehouse["default"].getTemplateByClassName(className).then(function (t) { - if (t != null) self.sendReply(_IIPPacketAction["default"].TemplateFromClassName, callback).addUint32(t.content.length).addUint8Array(t.content).done();else { - // reply failed - self.sendError(_ErrorType["default"].Management, callback, _ExceptionCode["default"].TemplateNotFound); - } - }); + var t = _Warehouse["default"].getTemplateByClassName(className); + + if (t != null) { + self.sendReply(_IIPPacketAction["default"].TemplateFromClassName, callback).addUint32(t.content.length).addUint8Array(t.content).done(); + } else { + // reply failed + self.sendError(_ErrorType["default"].Management, callback, _ExceptionCode["default"].TemplateNotFound); + } } }, { key: "IIPRequestTemplateFromClassId", @@ -5324,7 +5410,7 @@ var DistributedConnection = /*#__PURE__*/function (_IStore) { } if (fi instanceof Function) { - var pi = _ResourceTemplate["default"].getFunctionParameters(fi); + var pi = _TypeTemplate["default"].getFunctionParameters(fi); var args = new Array(pi.length); @@ -5719,7 +5805,7 @@ var DistributedConnection = /*#__PURE__*/function (_IStore) { for (var offset = 0; offset < data.length;) { var cs = data.getUint32(offset); offset += 4; - templates.push(_ResourceTemplate["default"].parse(data, offset, cs)); + templates.push(_TypeTemplate["default"].parse(data, offset, cs)); offset += cs; } @@ -5750,9 +5836,9 @@ var DistributedConnection = /*#__PURE__*/function (_IStore) { var dr; if (resource == null) { - var template = _Warehouse["default"].getTemplateByClassId(rt[0]); + var template = _Warehouse["default"].getTemplateByClassId(rt[0], _TemplateType["default"].Wrapper); - if ((template === null || template === void 0 ? void 0 : template.resourceType) != null) dr = new template.getDependencies(self, id, rt[1], rt[2]);else dr = new _DistributedResource["default"](self, id, rt[1], rt[2]); + if ((template === null || template === void 0 ? void 0 : template.definedType) != null) dr = new template.getDependencies(self, id, rt[1], rt[2]);else dr = new _DistributedResource["default"](self, id, rt[1], rt[2]); } else dr = resource; //let dr = resource || new DistributedResource(self, id, rt[1], rt[2]); @@ -6100,7 +6186,7 @@ var DistributedConnection = /*#__PURE__*/function (_IStore) { exports["default"] = DistributedConnection; -},{"../../Core/AsyncException.js":2,"../../Core/AsyncQueue.js":3,"../../Core/AsyncReply.js":4,"../../Core/ErrorType.js":5,"../../Core/ExceptionCode.js":6,"../../Core/ProgressType.js":9,"../../Data/Codec.js":12,"../../Data/DataConverter.js":13,"../../Data/KeyList.js":17,"../../Resource/IResource.js":45,"../../Resource/IStore.js":46,"../../Resource/Template/ResourceTemplate.js":54,"../../Resource/Warehouse.js":57,"../../Security/Authority/Authentication.js":58,"../../Security/Authority/AuthenticationMethod.js":59,"../../Security/Authority/AuthenticationType.js":60,"../../Security/Authority/ClientAuthentication.js":61,"../../Security/Authority/HostAuthentication.js":62,"../../Security/Authority/Session.js":63,"../../Security/Integrity/SHA256.js":64,"../../Security/Permissions/ActionType.js":65,"../../Security/Permissions/Ruling.js":67,"../NetworkBuffer.js":30,"../Packets//IIPPacketReport.js":38,"../Packets/IIPAuthPacket.js":31,"../Packets/IIPAuthPacketAction.js":32,"../Packets/IIPAuthPacketCommand.js":33,"../Packets/IIPPacket.js":34,"../Packets/IIPPacketAction.js":35,"../Packets/IIPPacketCommand.js":36,"../Packets/IIPPacketEvent.js":37,"../SendList.js":39,"../Sockets/SocketState.js":41,"../Sockets/WSSocket.js":42,"./DistributedPropertyContext.js":26,"./DistributedResource.js":27,"./DistributedResourceQueueItem.js":28,"./DistributedResourceQueueItemType.js":29}],26:[function(require,module,exports){ +},{"../../Core/AsyncException.js":2,"../../Core/AsyncQueue.js":3,"../../Core/AsyncReply.js":4,"../../Core/ErrorType.js":5,"../../Core/ExceptionCode.js":6,"../../Core/ProgressType.js":9,"../../Data/Codec.js":12,"../../Data/DataConverter.js":13,"../../Data/KeyList.js":17,"../../Resource/IResource.js":45,"../../Resource/IStore.js":46,"../../Resource/Template/TemplateType.js":55,"../../Resource/Template/TypeTemplate.js":56,"../../Resource/Warehouse.js":57,"../../Security/Authority/Authentication.js":58,"../../Security/Authority/AuthenticationMethod.js":59,"../../Security/Authority/AuthenticationType.js":60,"../../Security/Authority/ClientAuthentication.js":61,"../../Security/Authority/HostAuthentication.js":62,"../../Security/Authority/Session.js":63,"../../Security/Integrity/SHA256.js":64,"../../Security/Permissions/ActionType.js":65,"../../Security/Permissions/Ruling.js":67,"../NetworkBuffer.js":30,"../Packets//IIPPacketReport.js":38,"../Packets/IIPAuthPacket.js":31,"../Packets/IIPAuthPacketAction.js":32,"../Packets/IIPAuthPacketCommand.js":33,"../Packets/IIPPacket.js":34,"../Packets/IIPPacketAction.js":35,"../Packets/IIPPacketCommand.js":36,"../Packets/IIPPacketEvent.js":37,"../SendList.js":39,"../Sockets/SocketState.js":41,"../Sockets/WSSocket.js":42,"./DistributedPropertyContext.js":26,"./DistributedResource.js":27,"./DistributedResourceQueueItem.js":28,"./DistributedResourceQueueItemType.js":29}],26:[function(require,module,exports){ /* * Copyright (c) 2017-2018 Ahmed Kh. Zamil * @@ -8490,7 +8576,7 @@ var ArgumentTemplate = /*#__PURE__*/function () { exports["default"] = ArgumentTemplate; -},{"../../Data/BinaryList.js":11,"../../Data/DataConverter.js":13,"./TemplateDataType.js":55}],49:[function(require,module,exports){ +},{"../../Data/BinaryList.js":11,"../../Data/DataConverter.js":13,"./TemplateDataType.js":54}],49:[function(require,module,exports){ /* * Copyright (c) 2017 Ahmed Kh. Zamil * @@ -8924,6 +9010,140 @@ Object.defineProperty(exports, "__esModule", { }); exports["default"] = void 0; +var _DataConverter = require("../../Data/DataConverter.js"); + +var _DataType = _interopRequireDefault(require("../../Data/DataType.js")); + +var _Structure = _interopRequireDefault(require("../../Data/Structure.js")); + +var _IResource = _interopRequireDefault(require("../IResource.js")); + +var _TypeTemplate = _interopRequireDefault(require("./TypeTemplate.js")); + +var _IRecord = _interopRequireDefault(require("../../Data/IRecord.js")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } + +function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +var TemplateDataType = /*#__PURE__*/function () { + function TemplateDataType(type, guid) { + _classCallCheck(this, TemplateDataType); + + this.type = type; + this.typeGuid = guid; + } + + _createClass(TemplateDataType, [{ + key: "typeTemplate", + get: function get() { + return this.typeGuid == null ? null : Warehouse.getTemplateByType(this.typeGuid); + } //@TODO: implement this + + }, { + key: "compose", + value: function compose() { + if (this.type == _DataType["default"].Resource || this.type == _DataType["default"].ResourceArray || this.type == _DataType["default"].Record || this.type == _DataType["default"].RecordArray) { + return (0, _DataConverter.BL)().addUint8(this.type).addUint8Array(this.typeGuid).toDC(); + } else return _DataConverter.DC.from([this.type]); + } + }], [{ + key: "fromType", + value: function fromType(type) { + var _type, _type2; + + var isArray = type instanceof Array; + if (isArray) type = type[0]; + var dataType = 0; + var typeGuid = null; + if (!isNaN(type)) dataType = type;else if (type == _Structure["default"]) dataType = _DataType["default"].Structure;else if (typeof type == "string") { + var tIndex = this.typesDefinitions.indexOf(type); + dataType = tIndex > -1 ? tIndex : 0; + } else if (((_type = type) === null || _type === void 0 ? void 0 : _type.prototype) instanceof _IResource["default"]) { + dataType = _DataType["default"].Resource; + typeGuid = _TypeTemplate["default"].getTypeGuid(type); + } else if (((_type2 = type) === null || _type2 === void 0 ? void 0 : _type2.prototype) instanceof _IRecord["default"]) { + dataType = _DataType["default"].Record; + typeGuid = _TypeTemplate["default"].getTypeGuid(type); + } + if (isArray) dataType |= _DataType["default"].VarArray; + return new TemplateDataType(_DataType["default"].StructureArray); + } + }, { + key: "parse", + value: function parse(data, offset) { + var type = data.getUint8(offset++); + + if (type == _DataType["default"].Resource || type == _DataType["default"].ResourceArray || type == _DataType["default"].Record || type == _DataType["default"].RecordArray) { + var guid = data.getGuid(offset); + return { + size: 17, + value: new TemplateDataType(type, guid) + }; + } else return { + size: 1, + value: new TemplateDataType(type) + }; + } + }]); + + return TemplateDataType; +}(); + +exports["default"] = TemplateDataType; + +_defineProperty(TemplateDataType, "typesDefinitions", ["var", "bool", "sbyte", "byte", "char", "short", "ushort", "int", "uint", "long", "ulong", "float", "double", "decimal", "date", "resource", "DistributedResource", "ResourceLink", "string", "structure"]); + +},{"../../Data/DataConverter.js":13,"../../Data/DataType.js":14,"../../Data/IRecord.js":16,"../../Data/Structure.js":22,"../IResource.js":45,"./TypeTemplate.js":56}],55:[function(require,module,exports){ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports["default"] = void 0; +var _default = { + Unspecified: 0, + Resource: 1, + Record: 2, + Wrapper: 3 +}; +exports["default"] = _default; + +},{}],56:[function(require,module,exports){ +/* +* Copyright (c) 2017 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. +*/ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports["default"] = void 0; + var _FunctionTemplate = _interopRequireDefault(require("./FunctionTemplate.js")); var _PropertyTemplate = _interopRequireDefault(require("./PropertyTemplate.js")); @@ -8952,9 +9172,9 @@ function _defineProperties(target, props) { for (var i = 0; i < props.length; i+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } -var ResourceTemplate = /*#__PURE__*/function () { - function ResourceTemplate(type) { - _classCallCheck(this, ResourceTemplate); +var TypeTemplate = /*#__PURE__*/function () { + function TypeTemplate(type, addToWarehouse) { + _classCallCheck(this, TypeTemplate); this.properties = []; this.events = []; @@ -8962,11 +9182,12 @@ var ResourceTemplate = /*#__PURE__*/function () { this.members = []; if (type === undefined) return; if (type.prototype instanceof _IRecord["default"]) this.templateType = _TemplateType["default"].Record;else if (type.prototype instanceof _IResource["default"]) this.templateType = _TemplateType["default"].Resource;else throw new Error("Type is neither a resource nor a record."); - this.resourceType = type; + this.definedType = type; var template = type.template; // set guid this.className = template.namespace + "." + type.prototype.constructor.name; - this.classId = _SHA["default"].compute(_DataConverter.DC.stringToBytes(this.className)).getGuid(0); //byte currentIndex = 0; + this.classId = _SHA["default"].compute(_DataConverter.DC.stringToBytes(this.className)).getGuid(0); + if (addToWarehouse) addToWarehouse.putTemplate(this); //byte currentIndex = 0; if (template.properties != null) for (var i = 0; i < template.properties.length; i++) { var _pi$, _pi$2, _pi$3; @@ -9058,7 +9279,7 @@ var ResourceTemplate = /*#__PURE__*/function () { this.content = b.toArray(); } - _createClass(ResourceTemplate, [{ + _createClass(TypeTemplate, [{ key: "getEventTemplateByName", value: function getEventTemplateByName(eventName) { for (var i = 0; i < this.events.length; i++) { @@ -9142,7 +9363,7 @@ var ResourceTemplate = /*#__PURE__*/function () { var _getDependenciesFunc = null; _getDependenciesFunc = function getDependenciesFunc(tmp, bag) { - if (template.resourceType == null) return; // functions + if (template.definedType == null) return; // functions for (var i = 0; i < tmp.functions.length; i++) { f = tmp.functions[i]; @@ -9248,7 +9469,7 @@ var ResourceTemplate = /*#__PURE__*/function () { var ends = offset + contentLength; var oOffset = offset; // start parsing... - var od = new ResourceTemplate(); + var od = new TypeTemplate(); od.content = data.clip(offset, contentLength); od.templateType = data.getUint8(offset++); od.classId = data.getGuid(offset); @@ -9389,144 +9610,12 @@ var ResourceTemplate = /*#__PURE__*/function () { } }]); - return ResourceTemplate; + return TypeTemplate; }(); -exports["default"] = ResourceTemplate; +exports["default"] = TypeTemplate; -},{"../../Data/DataConverter.js":13,"../../Data/IRecord.js":16,"../../Security/Integrity/SHA256.js":64,"../IResource.js":45,"./ArgumentTemplate.js":48,"./EventTemplate.js":49,"./FunctionTemplate.js":50,"./PropertyTemplate.js":53,"./TemplateDataType.js":55,"./TemplateType.js":56}],55:[function(require,module,exports){ -/* -* Copyright (c) 2017 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. -*/ -"use strict"; - -Object.defineProperty(exports, "__esModule", { - value: true -}); -exports["default"] = void 0; - -var _DataConverter = require("../../Data/DataConverter.js"); - -var _DataType = _interopRequireDefault(require("../../Data/DataType.js")); - -var _Structure = _interopRequireDefault(require("../../Data/Structure.js")); - -var _IResource = _interopRequireDefault(require("../IResource.js")); - -var _ResourceTemplate = _interopRequireDefault(require("./ResourceTemplate.js")); - -var _IRecord = _interopRequireDefault(require("../../Data/IRecord.js")); - -function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } - -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } - -function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } - -function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } - -function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } - -var TemplateDataType = /*#__PURE__*/function () { - function TemplateDataType(type, guid) { - _classCallCheck(this, TemplateDataType); - - this.type = type; - this.typeGuid = guid; - } - - _createClass(TemplateDataType, [{ - key: "typeTemplate", - get: function get() { - return this.typeGuid == null ? null : Warehouse.getTemplateByType(this.typeGuid); - } //@TODO: implement this - - }, { - key: "compose", - value: function compose() { - if (this.type == _DataType["default"].Resource || this.type == _DataType["default"].ResourceArray || this.type == _DataType["default"].Record || this.type == _DataType["default"].RecordArray) { - return (0, _DataConverter.BL)().addUint8(this.type).addUint8Array(this.typeGuid).toDC(); - } else return _DataConverter.DC.from([this.type]); - } - }], [{ - key: "fromType", - value: function fromType(type) { - var _type, _type2; - - var isArray = type instanceof Array; - if (isArray) type = type[0]; - var dataType = 0; - var typeGuid = null; - if (!isNaN(type)) dataType = type;else if (type == _Structure["default"]) dataType = _DataType["default"].Structure;else if (typeof type == "string") { - var tIndex = this.typesDefinitions.indexOf(type); - dataType = tIndex > -1 ? tIndex : 0; - } else if (((_type = type) === null || _type === void 0 ? void 0 : _type.prototype) instanceof _IResource["default"]) { - dataType = _DataType["default"].Resource; - typeGuid = _ResourceTemplate["default"].getTypeGuid(type); - } else if (((_type2 = type) === null || _type2 === void 0 ? void 0 : _type2.prototype) instanceof _IRecord["default"]) { - dataType = _DataType["default"].Record; - typeGuid = _ResourceTemplate["default"].getTypeGuid(type); - } - if (isArray) dataType |= _DataType["default"].VarArray; - return new TemplateDataType(_DataType["default"].StructureArray); - } - }, { - key: "parse", - value: function parse(data, offset) { - var type = data.getUint8(offset++); - - if (type == _DataType["default"].Resource || type == _DataType["default"].ResourceArray || type == _DataType["default"].Record || type == _DataType["default"].RecordArray) { - var guid = data.getGuid(offset); - return { - size: 17, - value: new TemplateDataType(type, guid) - }; - } else return { - size: 1, - value: new TemplateDataType(type) - }; - } - }]); - - return TemplateDataType; -}(); - -exports["default"] = TemplateDataType; - -_defineProperty(TemplateDataType, "typesDefinitions", ["var", "bool", "sbyte", "byte", "char", "short", "ushort", "int", "uint", "long", "ulong", "float", "double", "decimal", "date", "resource", "DistributedResource", "ResourceLink", "string", "structure"]); - -},{"../../Data/DataConverter.js":13,"../../Data/DataType.js":14,"../../Data/IRecord.js":16,"../../Data/Structure.js":22,"../IResource.js":45,"./ResourceTemplate.js":54}],56:[function(require,module,exports){ -"use strict"; - -Object.defineProperty(exports, "__esModule", { - value: true -}); -exports["default"] = void 0; -var _default = { - Resource: 0, - Record: 1 -}; -exports["default"] = _default; - -},{}],57:[function(require,module,exports){ +},{"../../Data/DataConverter.js":13,"../../Data/IRecord.js":16,"../../Security/Integrity/SHA256.js":64,"../IResource.js":45,"./ArgumentTemplate.js":48,"./EventTemplate.js":49,"./FunctionTemplate.js":50,"./PropertyTemplate.js":53,"./TemplateDataType.js":54,"./TemplateType.js":55}],57:[function(require,module,exports){ /* * Copyright (c) 2017 Ahmed Kh. Zamil * @@ -9561,7 +9650,7 @@ exports["default"] = exports.WH = void 0; var _AsyncReply = _interopRequireDefault(require("../Core/AsyncReply.js")); -var _ResourceTemplate = _interopRequireDefault(require("../Resource/Template/ResourceTemplate.js")); +var _TypeTemplate = _interopRequireDefault(require("../Resource/Template/TypeTemplate.js")); var _IEventHandler2 = _interopRequireDefault(require("../Core/IEventHandler.js")); @@ -9587,6 +9676,8 @@ var _AsyncBag = _interopRequireDefault(require("../Core/AsyncBag.js")); var _IRecord = _interopRequireDefault(require("../Data/IRecord.js")); +var _TemplateType = _interopRequireDefault(require("./Template/TemplateType.js")); + function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } @@ -9630,7 +9721,15 @@ var WH = /*#__PURE__*/function (_IEventHandler) { _this.resources = new _KeyList["default"](); _this.resourceCounter = 0; _this.templates = new _KeyList["default"](); - _this.wrapperTemplates = new _KeyList["default"](); + + _this.templates.add(_TemplateType["default"].Unspecified, new _KeyList["default"]()); + + _this.templates.add(_TemplateType["default"].Resource, new _KeyList["default"]()); + + _this.templates.add(_TemplateType["default"].Record, new _KeyList["default"]()); + + _this.templates.add(_TemplateType["default"].Wrapper, new _KeyList["default"]()); + _this.protocols = new _KeyList["default"](); _this._register("connected"); @@ -9842,50 +9941,72 @@ var WH = /*#__PURE__*/function (_IEventHandler) { }, { key: "putTemplate", value: function putTemplate(template) { - var wrapper = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; - - if (wrapper) { - this.wrapperTemplates.add(template.classId.valueOf(), template); - } else { - this.templates.add(template.classId.valueOf(), template); - } + this.templates.get(template.type).add(template.classId, template); } }, { key: "getTemplateByType", value: function getTemplateByType(type) { + var templateType = _TemplateType["default"].Unspecified; + if (type.prototype instanceof DistributedResource) templateType = _TemplateType["default"].Wrapper; + if (type.prototype instanceof _IResource["default"]) templateType = _TemplateType["default"].Resource;else if (type.prototype instanceof _IRecord["default"]) templateType = _TemplateType["default"].Record;else return null; if (type == _IResource["default"] || type == _IRecord["default"]) return null; if (!(type.prototype instanceof _IResource["default"] || type.prototype instanceof _IRecord["default"])) return false; var className = type.prototype.constructor.name; if (className.startsWith("E_")) className = className.substr(2); - className = type.template.namespace + "." + className; // loaded ? + className = type.template.namespace + "." + className; + var templates = this.templates.get(templateType); // loaded ? - for (var i = 0; i < this.templates.length; i++) { - if (this.templates.at(i).className == className) return this.templates.at(i); + for (var i = 0; i < templates.length; i++) { + if (templates.at(i).className == className) return templates.at(i); } - var template = new _ResourceTemplate["default"](type); - this.templates.add(template.classId.valueOf(), template); + var template = new _TypeTemplate["default"](type, this); return template; } }, { key: "getTemplateByClassId", value: function getTemplateByClassId(classId) { - var wrapper = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; + var templateType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _TemplateType["default"].Unspecified; - if (wrapper) { - return this.wrapperTemplates.item(classId); - } else { - return this.templates.item(classId); - } + if (templateType == _TemplateType["default"].Unspecified) { + // look in resources + var template = templates.get(_TemplateType["default"].Resource).get(classId); + if (template != null) return template; // look in records + + template = templates.get(_TemplateType["default"].Record).get(classId); + if (template != null) return template; // look in wrappers + + template = templates.get(_TemplateType["default"].Wrapper).get(classId); + return template; + } else return templates.get(templateType).get(classId); } }, { key: "getTemplateByClassName", value: function getTemplateByClassName(className) { - for (var i = 0; i < this.templates.length; i++) { - if (this.templates.at(i).className == className) return new _AsyncReply["default"](this.templates.at(i)); - } + var templateType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _TemplateType["default"].Unspecified; - return new _AsyncReply["default"](null); + if (templateType == _TemplateType["default"].Unspecified) { + // look in resources + var template = templates[_TemplateType["default"].Resource].values.find(function (x) { + return x.className == className; + }); + + if (template != null) return template; // look in records + + template = templates[_TemplateType["default"].Record].values.find(function (x) { + return x.className == className; + }); + if (template != null) return template; // look in wrappers + + template = templates[_TemplateType["default"].Wrapper].values.find(function (x) { + return x.className == className; + }); + return template; + } else { + return templates[templateType].values.find(function (x) { + return x.className == className; + }); + } } }, { key: "_qureyIn", @@ -10030,7 +10151,7 @@ Warehouse.protocols.add("db", function (name, attributes) { var _default = Warehouse; exports["default"] = _default; -},{"../Core/AsyncBag.js":1,"../Core/AsyncReply.js":4,"../Core/IEventHandler.js":8,"../Data/AutoList.js":10,"../Data/IRecord.js":16,"../Data/KeyList.js":17,"../Net/IIP/DistributedConnection.js":25,"../Proxy/ResourceProxy.js":43,"../Resource/Instance.js":47,"../Resource/Template/ResourceTemplate.js":54,"../Stores/IndexedDBStore.js":68,"../Stores/MemoryStore.js":69,"./IResource.js":45,"./IStore.js":46}],58:[function(require,module,exports){ +},{"../Core/AsyncBag.js":1,"../Core/AsyncReply.js":4,"../Core/IEventHandler.js":8,"../Data/AutoList.js":10,"../Data/IRecord.js":16,"../Data/KeyList.js":17,"../Net/IIP/DistributedConnection.js":25,"../Proxy/ResourceProxy.js":43,"../Resource/Instance.js":47,"../Resource/Template/TypeTemplate.js":56,"../Stores/IndexedDBStore.js":68,"../Stores/MemoryStore.js":69,"./IResource.js":45,"./IStore.js":46,"./Template/TemplateType.js":55}],58:[function(require,module,exports){ /* * Copyright (c) 2017 Ahmed Kh. Zamil * diff --git a/package.json b/package.json index 2d88f1e..6570fcd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "esiur", - "version": "1.6.8", + "version": "1.7.1", "description": "Distributed Object Framework", "main": "esiur.js", "type": "module", diff --git a/src/Data/Codec.js b/src/Data/Codec.js index 7f8f759..8721ec1 100644 --- a/src/Data/Codec.js +++ b/src/Data/Codec.js @@ -710,7 +710,9 @@ export default class Codec { static parseRecordArray(data, offset, length, connection) { + var reply = new AsyncBag(); + if (length == 0) { reply.seal(); @@ -719,30 +721,61 @@ export default class Codec { var end = offset + length; - var result = data.getUint8(offset++); + var isTyped = (data.getUint8(offset) & 0x10) == 0x10; - var previous = null; - var classId = null; + var result = data.getUint8(offset++) & 0xF; - if (result == RecordComparisonResult.Null) - previous = new AsyncReply(null); - else if (result == RecordComparisonResult.Record) + if (isTyped) { - var cs = data.getUint32(offset); - var recordLength = cs - 16; - offset += 4; - classId = data.getGuid(offset); + var classId = data.getGuid(offset); offset += 16; - previous = Codec.parseRecord(data, offset, recordLength, connection, classId); - offset += recordLength; + + var template = Warehouse.getTemplateByClassId(classId, TemplateType.Record); + + reply.arrayType = template.definedType; + + var previous = null; + + if (result == RecordComparisonResult.Null) + previous = new AsyncReply(null); + else if (result == RecordComparisonResult.Record + || result == RecordComparisonResult.RecordSameType) + { + var cs = data.getUint32(offset); + var recordLength = cs; + offset += 4; + previous = Codec.parseRecord(data, offset, recordLength, connection, classId); + offset += recordLength; + } + + reply.add(previous); + + while (offset < end) + { + result = data.getUint8(offset++); + + if (result == RecordComparisonResult.Null) + previous = new AsyncReply(null); + else if (result == RecordComparisonResult.Record + || result == RecordComparisonResult.RecordSameType) + { + var cs = data.getUint32(offset); + offset += 4; + previous = Codec.parseRecord(data, offset, cs, connection, classId); + offset += cs; + } + else if (result == RecordComparisonResult.Same) + { + // do nothing + } + + reply.add(previous); + } } - - reply.Add(previous); - - - while (offset < end) + else { - result = data.getUint8(offset++); + var previous = null; + var classId = null; if (result == RecordComparisonResult.Null) previous = new AsyncReply(null); @@ -753,26 +786,113 @@ export default class Codec { offset += 4; classId = data.getGuid(offset); offset += 16; - previous = Codec.parseRecord(data, offset, recordLength, connection, classId); + previous = Codec.parseRecord(data, offset, recordLength, connection, classId); offset += recordLength; - } - else if (result == RecordComparisonResult.RecordSameType) - { - var cs = data.getUint32(offset); - offset += 4; - previous = Codec.parseRecord(data, offset, cs, connection, classId); - offset += cs; - } - else if (result == RecordComparisonResult.Same) - { - // do nothing + + } + + reply.Add(previous); + + + while (offset < end) + { + result = data.getUint8(offset++); + + if (result == RecordComparisonResult.Null) + previous = new AsyncReply(null); + else if (result == RecordComparisonResult.Record) + { + var cs = data.getUint32(offset); + var recordLength = cs - 16; + offset += 4; + classId = data.getGuid(offset); + offset += 16; + previous = Codec.parseRecord(data, offset, recordLength, connection, classId); + offset += recordLength; + } + else if (result == RecordComparisonResult.RecordSameType) + { + var cs = data.getUint32(offset); + offset += 4; + previous = ParseRecord(data, offset, cs, connection, classId); + offset += cs; + } + else if (result == RecordComparisonResult.Same) + { + // do nothing + } + + reply.add(previous); } - reply.add(previous); } - reply.seal(); + reply.Seal(); return reply; + + // var reply = new AsyncBag(); + // if (length == 0) + // { + // reply.seal(); + // return reply; + // } + + // var end = offset + length; + + // var result = data.getUint8(offset++); + + // var previous = null; + // var classId = null; + + // if (result == RecordComparisonResult.Null) + // previous = new AsyncReply(null); + // else if (result == RecordComparisonResult.Record) + // { + // var cs = data.getUint32(offset); + // var recordLength = cs - 16; + // offset += 4; + // classId = data.getGuid(offset); + // offset += 16; + // previous = Codec.parseRecord(data, offset, recordLength, connection, classId); + // offset += recordLength; + // } + + // reply.Add(previous); + + + // while (offset < end) + // { + // result = data.getUint8(offset++); + + // if (result == RecordComparisonResult.Null) + // previous = new AsyncReply(null); + // else if (result == RecordComparisonResult.Record) + // { + // var cs = data.getUint32(offset); + // var recordLength = cs - 16; + // offset += 4; + // classId = data.getGuid(offset); + // offset += 16; + // previous = Codec.parseRecord(data, offset, recordLength, connection, classId); + // offset += recordLength; + // } + // else if (result == RecordComparisonResult.RecordSameType) + // { + // var cs = data.getUint32(offset); + // offset += 4; + // previous = Codec.parseRecord(data, offset, cs, connection, classId); + // offset += cs; + // } + // else if (result == RecordComparisonResult.Same) + // { + // // do nothing + // } + + // reply.add(previous); + // } + + // reply.seal(); + // return reply; } static parseRecord(data, offset, length, connection, classId = null) @@ -787,15 +907,15 @@ export default class Codec { length -= 16; } - var template = Warehouse.getTemplateByClassId(classId); + var template = Warehouse.getTemplateByClassId(classId, TemplateType.Record); if (template != null) { Codec.parseVarArray(data, offset, length, connection).then(ar => { - if (template.resourceType != null) + if (template.definedType != null) { - var record = new template.resourceType(); + var record = new template.definedType(); for (var i = 0; i < template.properties.length; i++) record[template.properties[i].name] = ar[i]; diff --git a/src/Data/ResourceArrayType.js b/src/Data/ResourceArrayType.js new file mode 100644 index 0000000..2038ef2 --- /dev/null +++ b/src/Data/ResourceArrayType.js @@ -0,0 +1,6 @@ +export default +{ + Dynamic = 0x0, + Static = 0x10, + Wrapper = 0x20 +}; diff --git a/src/Net/IIP/DistributedConnection.js b/src/Net/IIP/DistributedConnection.js index dba0585..4b75b1e 100644 --- a/src/Net/IIP/DistributedConnection.js +++ b/src/Net/IIP/DistributedConnection.js @@ -58,7 +58,7 @@ import ProgressType from "../../Core/ProgressType.js"; import ExceptionCode from "../../Core/ExceptionCode.js"; import DistributedResource from './DistributedResource.js'; -import ResourceTemplate from '../../Resource/Template/ResourceTemplate.js'; +import TypeTemplate from '../../Resource/Template/TypeTemplate.js'; import DistributedResourceQueueItem from './DistributedResourceQueueItem.js'; import DistributedResourceQueueItemType from './DistributedResourceQueueItemType.js'; @@ -74,6 +74,7 @@ import WSSocket from '../Sockets/WSSocket.js'; import ClientAuthentication from "../../Security/Authority/ClientAuthentication.js"; import HostAuthentication from "../../Security/Authority/HostAuthentication.js"; import SocketState from "../Sockets/SocketState.js"; +import TemplateType from '../../Resource/Template/TemplateType.js'; export default class DistributedConnection extends IStore { @@ -315,7 +316,7 @@ export default class DistributedConnection extends IStore { case IIPPacketAction.TemplateFromClassName: case IIPPacketAction.TemplateFromClassId: case IIPPacketAction.TemplateFromResourceId: - this.IIPReply(packet.callbackId, ResourceTemplate.parse(packet.content)); + this.IIPReply(packet.callbackId, TypeTemplate.parse(packet.content)); break; case IIPPacketAction.QueryLink: @@ -1761,7 +1762,7 @@ export default class DistributedConnection extends IStore { var templates = []; for (var i = 0; i < list.length; i++) templates = templates - .concat(ResourceTemplate.getDependencies(list[i].instance.template) + .concat(TypeTemplate.getDependencies(list[i].instance.template) .filter(x => !templates.includes(x))); for(var i = 0; i < templates.length; i++) { @@ -1788,17 +1789,16 @@ export default class DistributedConnection extends IStore { var self = this; - Warehouse.getTemplateByClassName(className).then(function (t) { - if (t != null) - self.sendReply(IIPPacketAction.TemplateFromClassName, callback) - .addUint32(t.content.length) - .addUint8Array(t.content) - .done(); - else { - // reply failed - self.sendError(ErrorType.Management, callback, ExceptionCode.TemplateNotFound); - } - }); + var t = Warehouse.getTemplateByClassName(className); + if (t != null) { + self.sendReply(IIPPacketAction.TemplateFromClassName, callback) + .addUint32(t.content.length) + .addUint8Array(t.content) + .done(); + } else { + // reply failed + self.sendError(ErrorType.Management, callback, ExceptionCode.TemplateNotFound); + } } IIPRequestTemplateFromClassId(callback, classId) { @@ -1972,7 +1972,7 @@ export default class DistributedConnection extends IStore { if (fi instanceof Function) { - var pi = ResourceTemplate.getFunctionParameters(fi); + var pi = TypeTemplate.getFunctionParameters(fi); var args = new Array(pi.length); for (var i = 0; i < pi.length; i++) { @@ -2465,7 +2465,7 @@ export default class DistributedConnection extends IStore { { var cs = data.getUint32(offset); offset += 4; - templates.push(ResourceTemplate.parse(data, offset, cs)); + templates.push(TypeTemplate.parse(data, offset, cs)); offset += cs; } @@ -2512,8 +2512,8 @@ export default class DistributedConnection extends IStore { if (resource == null) { - var template = Warehouse.getTemplateByClassId(rt[0]); - if (template?.resourceType != null) + var template = Warehouse.getTemplateByClassId(rt[0], TemplateType.Wrapper); + if (template?.definedType != null) dr = new template.getDependencies(self, id, rt[1], rt[2]); else dr = new DistributedResource(self, id, rt[1], rt[2]); diff --git a/src/Resource/Template/TemplateDataType.js b/src/Resource/Template/TemplateDataType.js index 85b4138..0dbd47b 100644 --- a/src/Resource/Template/TemplateDataType.js +++ b/src/Resource/Template/TemplateDataType.js @@ -26,7 +26,7 @@ import {DC, BL} from '../../Data/DataConverter.js'; import DataType from "../../Data/DataType.js"; import Structure from '../../Data/Structure.js'; import IResource from '../IResource.js'; -import ResourceTemplate from './ResourceTemplate.js'; +import TypeTemplate from './TypeTemplate.js'; import IRecord from '../../Data/IRecord.js'; export default class TemplateDataType @@ -82,12 +82,12 @@ export default class TemplateDataType else if (type?.prototype instanceof IResource) { dataType = DataType.Resource; - typeGuid = ResourceTemplate.getTypeGuid(type); + typeGuid = TypeTemplate.getTypeGuid(type); } else if (type?.prototype instanceof IRecord) { dataType = DataType.Record; - typeGuid = ResourceTemplate.getTypeGuid(type); + typeGuid = TypeTemplate.getTypeGuid(type); } if (isArray) diff --git a/src/Resource/Template/TemplateType.js b/src/Resource/Template/TemplateType.js index 6041d5c..87afcf3 100644 --- a/src/Resource/Template/TemplateType.js +++ b/src/Resource/Template/TemplateType.js @@ -1,5 +1,7 @@ export default { - Resource: 0, - Record: 1 + Unspecified: 0, + Resource: 1, + Record: 2, + Wrapper: 3, } \ No newline at end of file diff --git a/src/Resource/Template/ResourceTemplate.js b/src/Resource/Template/TypeTemplate.js similarity index 98% rename from src/Resource/Template/ResourceTemplate.js rename to src/Resource/Template/TypeTemplate.js index 8179fa6..4cf83aa 100644 --- a/src/Resource/Template/ResourceTemplate.js +++ b/src/Resource/Template/TypeTemplate.js @@ -33,7 +33,7 @@ import IResource from '../IResource.js'; import IRecord from '../../Data/IRecord.js'; import TemplateType from './TemplateType.js' -export default class ResourceTemplate { +export default class TypeTemplate { getEventTemplateByName(eventName) { for (var i = 0; i < this.events.length; i++) @@ -115,7 +115,7 @@ export default class ResourceTemplate { getDependenciesFunc = (tmp, bag) => { - if (template.resourceType == null) + if (template.definedType == null) return; // functions @@ -204,7 +204,7 @@ export default class ResourceTemplate { return list; } - constructor(type) { + constructor(type, addToWarehouse) { this.properties = []; @@ -222,7 +222,7 @@ export default class ResourceTemplate { else throw new Error("Type is neither a resource nor a record."); - this.resourceType = type; + this.definedType = type; var template = type.template; @@ -231,6 +231,10 @@ export default class ResourceTemplate { this.classId = SHA256.compute(DC.stringToBytes(this.className)).getGuid(0); + + if (addToWarehouse) + addToWarehouse.putTemplate(this); + //byte currentIndex = 0; if (template.properties != null) @@ -356,7 +360,7 @@ export default class ResourceTemplate { // start parsing... - var od = new ResourceTemplate(); + var od = new TypeTemplate(); od.content = data.clip(offset, contentLength); od.templateType = data.getUint8(offset++); diff --git a/src/Resource/Warehouse.js b/src/Resource/Warehouse.js index 0df6d2d..8e7644a 100644 --- a/src/Resource/Warehouse.js +++ b/src/Resource/Warehouse.js @@ -27,7 +27,7 @@ "use strict"; import AsyncReply from '../Core/AsyncReply.js'; -import ResourceTemplate from '../Resource/Template/ResourceTemplate.js'; +import TypeTemplate from '../Resource/Template/TypeTemplate.js'; import IEventHandler from '../Core/IEventHandler.js'; import AutoList from '../Data/AutoList.js'; import KeyList from '../Data/KeyList.js'; @@ -40,7 +40,7 @@ import IndexedDBStore from '../Stores/IndexedDBStore.js'; import ResourceProxy from '../Proxy/ResourceProxy.js'; import AsyncBag from '../Core/AsyncBag.js'; import IRecord from '../Data/IRecord.js'; - +import TemplateType from './Template/TemplateType.js'; export class WH extends IEventHandler { @@ -52,7 +52,12 @@ export class WH extends IEventHandler this.resources = new KeyList(); this.resourceCounter = 0; this.templates = new KeyList(); - this.wrapperTemplates = new KeyList(); + + this.templates.add(TemplateType.Unspecified, new KeyList()); + this.templates.add(TemplateType.Resource, new KeyList()); + this.templates.add(TemplateType.Record, new KeyList()); + this.templates.add(TemplateType.Wrapper, new KeyList()); + this.protocols = new KeyList(); this._register("connected"); this._register("disconnected"); @@ -291,19 +296,24 @@ export class WH extends IEventHandler value.instance.parents.add(value); } - putTemplate(template, wrapper = false) + putTemplate(template) { - if (wrapper) { - this.wrapperTemplates.add(template.classId.valueOf(), template); - } - else { - this.templates.add(template.classId.valueOf(), template); - } + this.templates.get(template.type).add(template.classId, template); } getTemplateByType(type) { - + var templateType = TemplateType.Unspecified; + + if (type.prototype instanceof DistributedResource) + templateType = TemplateType.Wrapper; + if (type.prototype instanceof IResource) + templateType = TemplateType.Resource; + else if (type.prototype instanceof IRecord) + templateType = TemplateType.Record; + else + return null; + if (type == IResource || type == IRecord) return null; @@ -319,34 +329,63 @@ export class WH extends IEventHandler className = type.template.namespace + "." + className; - // loaded ? - for (var i = 0; i < this.templates.length; i++) - if (this.templates.at(i).className == className) - return this.templates.at(i); + var templates = this.templates.get(templateType); - var template = new ResourceTemplate(type); - this.templates.add(template.classId.valueOf(), template); + + // loaded ? + for(var i = 0; i < templates.length; i++) + if (templates.at(i).className == className) + return templates.at(i); + + var template = new TypeTemplate(type, this); return template; } - getTemplateByClassId(classId, wrapper = false) + getTemplateByClassId(classId, templateType = TemplateType.Unspecified) { - if (wrapper) { - return this.wrapperTemplates.item(classId); - } - else { - return this.templates.item(classId); + if (templateType == TemplateType.Unspecified) + { + // look in resources + var template = templates.get(TemplateType.Resource).get(classId); + if (template != null) + return template; + + // look in records + template = templates.get(TemplateType.Record).get(classId); + if (template != null) + return template; + + // look in wrappers + template = templates.get(TemplateType.Wrapper).get(classId); + return template; } + else + return templates.get(templateType).get(classId); } - getTemplateByClassName(className) + getTemplateByClassName(className, templateType = TemplateType.Unspecified) { - for(var i = 0; i < this.templates.length; i++) - if (this.templates.at(i).className == className) - return new AsyncReply(this.templates.at(i)); - - return new AsyncReply(null); + if (templateType == TemplateType.Unspecified) + { + // look in resources + var template = templates[TemplateType.Resource].values.find(x => x.className == className); + if (template != null) + return template; + + // look in records + template = templates[TemplateType.Record].values.find(x => x.className == className); + if (template != null) + return template; + + // look in wrappers + template = templates[TemplateType.Wrapper].values.find(x => x.className == className); + return template; + } + else + { + return templates[templateType].values.find(x => x.className == className); + } } _qureyIn(path, index, resources)