basant307/AI_Governance_Project
048
1"use strict";2module.exports = Service;3 4// extends Namespace5var Namespace = require("./namespace");6((Service.prototype = Object.create(Namespace.prototype)).constructor = Service).className = "Service";7 8var Method = require("./method"),9 util = require("./util"),10 rpc = require("./rpc");11 12/**13 * Constructs a new service instance.14 * @classdesc Reflected service.15 * @extends NamespaceBase16 * @constructor17 * @param {string} name Service name18 * @param {Object.<string,*>} [options] Service options19 * @throws {TypeError} If arguments are invalid20 */21function Service(name, options) {22 Namespace.call(this, name, options);23 24 /**25 * Service methods.26 * @type {Object.<string,Method>}27 */28 this.methods = {}; // toJSON, marker29 30 /**31 * Cached methods as an array.32 * @type {Method[]|null}33 * @private34 */35 this._methodsArray = null;36}37 38/**39 * Service descriptor.40 * @interface IService41 * @extends INamespace42 * @property {Object.<string,IMethod>} methods Method descriptors43 */44 45/**46 * Constructs a service from a service descriptor.47 * @param {string} name Service name48 * @param {IService} json Service descriptor49 * @param {number} [depth] Current nesting depth, defaults to `0`50 * @returns {Service} Created service51 * @throws {TypeError} If arguments are invalid52 */53Service.fromJSON = function fromJSON(name, json, depth) {54 depth = util.checkDepth(depth);55 var service = new Service(name, json.options);56 /* istanbul ignore else */57 if (json.methods)58 for (var names = Object.keys(json.methods), i = 0; i < names.length; ++i)59 service.add(Method.fromJSON(names[i], json.methods[names[i]]));60 if (json.nested)61 service.addJSON(json.nested, depth);62 if (json.edition)63 service._edition = json.edition;64 service.comment = json.comment;65 service._defaultEdition = "proto3"; // For backwards-compatibility.66 return service;67};68 69/**70 * Converts this service to a service descriptor.71 * @param {IToJSONOptions} [toJSONOptions] JSON conversion options72 * @returns {IService} Service descriptor73 */74Service.prototype.toJSON = function toJSON(toJSONOptions) {75 var inherited = Namespace.prototype.toJSON.call(this, toJSONOptions);76 var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;77 return util.toObject([78 "edition" , this._editionToJSON(),79 "options" , inherited && inherited.options || undefined,80 "methods" , Namespace.arrayToJSON(this.methodsArray, toJSONOptions) || /* istanbul ignore next */ {},81 "nested" , inherited && inherited.nested || undefined,82 "comment" , keepComments ? this.comment : undefined83 ]);84};85 86/**87 * Methods of this service as an array for iteration.88 * @name Service#methodsArray89 * @type {Method[]}90 * @readonly91 */92Object.defineProperty(Service.prototype, "methodsArray", {93 get: function() {94 return this._methodsArray || (this._methodsArray = util.toArray(this.methods));95 }96});97 98function clearCache(service) {99 service._methodsArray = null;100 return service;101}102 103/**104 * @override105 */106Service.prototype.get = function get(name) {107 return Object.prototype.hasOwnProperty.call(this.methods, name)108 ? this.methods[name]109 : Namespace.prototype.get.call(this, name);110};111 112/**113 * @override114 */115Service.prototype.resolveAll = function resolveAll() {116 if (!this._needsRecursiveResolve) return this;117 118 Namespace.prototype.resolve.call(this);119 var methods = this.methodsArray;120 for (var i = 0; i < methods.length; ++i)121 methods[i].resolve();122 return this;123};124 125/**126 * @override127 */128Service.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {129 if (!this._needsRecursiveFeatureResolution) return this;130 131 edition = this._edition || edition;132 133 Namespace.prototype._resolveFeaturesRecursive.call(this, edition);134 this.methodsArray.forEach(method => {135 method._resolveFeaturesRecursive(edition);136 });137 return this;138};139 140/**141 * @override142 */143Service.prototype.add = function add(object) {144 /* istanbul ignore if */145 if (this.get(object.name))146 throw Error("duplicate name '" + object.name + "' in " + this);147 148 if (object instanceof Method) {149 if (object.name === "__proto__")150 return this;151 this.methods[object.name] = object;152 object.parent = this;153 return clearCache(this);154 }155 return Namespace.prototype.add.call(this, object);156};157 158/**159 * @override160 */161Service.prototype.remove = function remove(object) {162 if (object instanceof Method) {163 164 /* istanbul ignore if */165 if (this.methods[object.name] !== object)166 throw Error(object + " is not a member of " + this);167 168 delete this.methods[object.name];169 object.parent = null;170 return clearCache(this);171 }172 return Namespace.prototype.remove.call(this, object);173};174 175/**176 * Creates a runtime service using the specified rpc implementation.177 * @param {RPCImpl} rpcImpl RPC implementation178 * @param {boolean} [requestDelimited=false] Whether requests are length-delimited179 * @param {boolean} [responseDelimited=false] Whether responses are length-delimited180 * @returns {rpc.Service} RPC service. Useful where requests and/or responses are streamed.181 */182Service.prototype.create = function create(rpcImpl, requestDelimited, responseDelimited) {183 var rpcService = new rpc.Service(rpcImpl, requestDelimited, responseDelimited);184 for (var i = 0, method; i < /* initializes */ this.methodsArray.length; ++i) {185 var methodName = util.lcFirst((method = this._methodsArray[i]).resolve().name).replace(/[^$\w_]/g, "");186 rpcService[methodName] = (function(method, requestType, responseType) {187 return function rpcMethod(request, callback) {188 return rpc.Service.prototype.rpcCall.call(this, method, requestType, responseType, request, callback);189 };190 })(method, method.resolvedRequestType.ctor, method.resolvedResponseType.ctor);191 }192 return rpcService;193};194 