Skip to content

Commit

Permalink
Changed to use ES6 class construct
Browse files Browse the repository at this point in the history
Based on discussions with Gari and others on slack, changed to
require node.js engine version 6.0 and later, which allows us
to use ES6 class support. Removed the self-rolled prototypal
based .extend() mechanism.

- Removed _account from Member.js
- Removed extraneous inline comments for types

Change-Id: I3086ae0e215bb58858d2544da01e199974b10ef1
Signed-off-by: Jim Zhang <[email protected]>
  • Loading branch information
jimthematrix committed Sep 29, 2016
1 parent 0b2d441 commit 869da76
Show file tree
Hide file tree
Showing 8 changed files with 348 additions and 433 deletions.
148 changes: 73 additions & 75 deletions lib/Chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
limitations under the License.
*/

'use strict';

/**
* This module contains the implementation of the [Chain]{@link module:api.Chain} abstract class.
*
Expand All @@ -34,178 +36,174 @@ var Member = require('./Member.js');
* @class Chain
* @memberof module:Chain
*/
module.exports = api.Chain.extend(/** @lends module:Chain.Chain.prototype */{

// Name of the chain is only meaningful to the client
_name: "",
module.exports = class extends api.Chain {

// The peers on this chain to which the client can connect
_peers: [], // Peer[]
/**
* @param {string} name to identify different chain instances. The naming of chain instances
* is completely at the client application's discretion.
*/
constructor(name) {
super();

// Security enabled flag
_securityEnabled: true,
// Name of the chain is only meaningful to the client
this._name = "";

// A member cache associated with this chain
// TODO: Make an LRU to limit size of member cache
_members: {}, // associated array of [name] <-> Member
// The peers on this chain to which the client can connect
this._peers = []; // Peer[]

// The number of tcerts to get in each batch
_tcertBatchSize: 200,
// Security enabled flag
this._securityEnabled = true;

// The registrar (if any) that registers & enrolls new members/users
_registrar: null, // Member
// A member cache associated with this chain
// TODO: Make an LRU to limit size of member cache
this._members = {}; // associated array of [name] <-> Member

// The member services used for this chain
_memberServices: null, // MemberServices
// The number of tcerts to get in each batch
this._tcertBatchSize = 200;

// The key-val store used for this chain
_keyValStore: null, // KeyValueStore;
// The registrar (if any) that registers & enrolls new members/users
this._registrar = null; // Member

// Is in dev mode or network mode
_devMode: false,
// The member services used for this chain
this._memberServices = null; // MemberServices

// If in prefetch mode, we prefetch tcerts from member services to help performance
_preFetchMode: true,
// The key-val store used for this chain
this._keyValStore = null; // KeyValueStore;

// Temporary variables to control how long to wait for deploy and invoke to complete before
// emitting events. This will be removed when the SDK is able to receive events from the
_deployWaitTime: 20,
_invokeWaitTime: 5,
// Is in dev mode or network mode
this._devMode = false;

// The crypto primitives object
cryptoPrimitives: utils.getCryptoSuite(),
// If in prefetch mode, we prefetch tcerts from member services to help performance
this._preFetchMode = true;

/**
* @param {string} name to identify different chain instances. The naming of chain instances
* is completely at the client application's discretion.
*/
constructor: function(name) {
this._name = name;
this._members = {};
},
// Temporary variables to control how long to wait for deploy and invoke to complete before
// emitting events. This will be removed when the SDK is able to receive events from the
this._deployWaitTime = 20;
this._invokeWaitTime = 5;
}

/**
* Get the chain name.
* @returns {string} The name of the chain.
*/
getName: function() {
getName() {
return this._name;
},
}

/**
* Get the member whose credentials are used to register and enroll other users, or undefined if not set.
* @returns [Member]{@link module:api.Member} The member whose credentials are used to perform registration, or undefined if not set.
*/
getRegistrar: function() {
getRegistrar() {
return this._registrar;
},
}

/**
* Set the member whose credentials are used to register and enroll other users.
* @param [Member]{@link module:api.Member} registrar The member whose credentials are used to perform registration.
*/
setRegistrar: function(registrar) {
setRegistrar(registrar) {
this._registrar = registrar;
},
}

/**
* Set the member services URL
* @param {string} url Member services URL of the form: "grpc://host:port" or "grpcs://host:port"
* @param {string} pem String value of the TLS certificate for the local client
*/
setMemberServicesUrl: function(url, pem) {
setMemberServicesUrl(url, pem) {
this.setMemberServices(new MemberServices(url, pem));
},
}

/**
* Get the member service associated this chain.
* @returns [MemberService]{@link module:api.MemberService} Return the current member service, or undefined if not set.
*/
getMemberServices: function() {
getMemberServices() {
return this._memberServices;
},
}

/**
* Set the member service associated this chain. This allows the default implementation of member service to be overridden.
* @param [MemberService]{@link module:api.MemberService} an instance of the MemberServices class
*/
setMemberServices: function(memberServices) {
setMemberServices(memberServices) {
this._memberServices = memberServices;
if (memberServices instanceof MemberServices) {
this.cryptoPrimitives = memberServices.getCrypto();
}
},
}

/**
* Determine if security is enabled.
*/
isSecurityEnabled: function() {
isSecurityEnabled() {
return this._memberServices !== undefined;
},
}

/**
* Determine if pre-fetch mode is enabled to prefetch tcerts.
*/
isPreFetchMode: function() {
isPreFetchMode() {
return this._preFetchMode;
},
}

/**
* Set prefetch mode to true or false.
*/
setPreFetchMode: function(preFetchMode) {
setPreFetchMode(preFetchMode) {
this._preFetchMode = preFetchMode;
},
}

/**
* Determine if dev mode is enabled.
*/
isDevMode: function() {
isDevMode() {
return this._devMode
},
}

/**
* Set dev mode to true or false.
*/
setDevMode: function(devMode) {
setDevMode(devMode) {
this._devMode = devMode;
},
}

/**
* Get the key val store implementation (if any) that is currently associated with this chain.
* @returns {KeyValueStore} Return the current KeyValueStore associated with this chain, or undefined if not set.
*/
getKeyValueStore: function() {
getKeyValueStore() {
return this._keyValStore;
},
}

/**
* Set the key value store implementation.
*/
setKeyValueStore: function(keyValStore) {
setKeyValueStore(keyValStore) {
this._keyValStore = keyValStore;
},
}

/**
* Get the tcert batch size.
*/
getTCertBatchSize: function() {
getTCertBatchSize() {
return this._tcertBatchSize;
},
}

/**
* Set the tcert batch size.
*/
setTCertBatchSize: function(batchSize) {
setTCertBatchSize(batchSize) {
this._tcertBatchSize = batchSize;
},
}

/**
* Get the user member named 'name' or create
* a new member if the member does not exist.
* @returns Promise for the Member object
*/
getMember: function(name) {
getMember(name) {
var self = this;
return new Promise(function(resolve, reject) {
if (!self._keyValStore) {
Expand All @@ -230,17 +228,17 @@ module.exports = api.Chain.extend(/** @lends module:Chain.Chain.prototype */{
}
);
});
},
}

/**
* Get a user.
* A user is a specific type of member.
* Another type of member is a peer.
* @returns Promise for the Member object
*/
getUser: function(name) {
getUser(name) {
return this.getMember(name);
},
}

// Try to get the member from cache.
// If not found, create a new one.
Expand Down Expand Up @@ -273,7 +271,7 @@ module.exports = api.Chain.extend(/** @lends module:Chain.Chain.prototype */{
}
);
});
},
}

/**
* Register a user or other member type with the chain.
Expand All @@ -297,7 +295,7 @@ module.exports = api.Chain.extend(/** @lends module:Chain.Chain.prototype */{
}
);
});
},
}

/**
* Enroll a user or other identity which has already been registered.
Expand Down Expand Up @@ -327,7 +325,7 @@ module.exports = api.Chain.extend(/** @lends module:Chain.Chain.prototype */{
}
);
});
},
}

/**
* Register and enroll a user or other member type.
Expand Down Expand Up @@ -362,5 +360,5 @@ module.exports = api.Chain.extend(/** @lends module:Chain.Chain.prototype */{
);
});
}
});
};

Loading

0 comments on commit 869da76

Please sign in to comment.