|
21 | 21 | * @module hfc
|
22 | 22 | */
|
23 | 23 |
|
24 |
| -var util = require('util'); |
25 |
| -var Chain = require('./lib/Chain.js'); |
26 |
| -var Config = require('./lib/Config.js'); |
27 |
| -var Peer = require('./lib/Peer.js'); |
28 |
| -var utils = require('./lib/utils.js'); |
29 |
| - |
30 |
| -var _chains = {}; |
31 |
| - |
32 |
| -/** |
33 |
| - * Create a new chain. If it already exists, throws an Error. |
34 |
| - * @param name {string} Name of the chain. It can be any name and has value only for the client. |
35 |
| - * |
36 |
| - * @returns [Chain]{@link module:api.Chain} a new instance of the Chain class |
37 |
| - */ |
38 |
| -module.exports.newChain = function(name) { |
39 |
| - var chain = _chains[name]; |
40 |
| - |
41 |
| - if (chain) |
42 |
| - throw new Error(util.format('Chain %s already exists', name)); |
43 |
| - |
44 |
| - chain = new Chain(name); |
45 |
| - |
46 |
| - _chains[name] = chain; |
47 |
| - return chain; |
48 |
| -}; |
49 |
| - |
50 |
| -/** |
51 |
| - * Get a chain. If it doesn't yet exist and 'create' is true, create it. |
52 |
| - * @param {string} chainName The name of the chain to get or create. |
53 |
| - * @param {boolean} create If the chain doesn't already exist, specifies whether to create it. |
54 |
| - * @returns {Chain} Returns the chain, or null if it doesn't exist and create is false. |
55 |
| - */ |
56 |
| -module.exports.getChain = function(chainName, create) { |
57 |
| - var chain = _chains[chainName]; |
58 |
| - |
59 |
| - if (!chain && create) { |
60 |
| - chain = this.newChain(chainName); |
61 |
| - } |
62 |
| - |
63 |
| - return chain; |
64 |
| -}; |
65 |
| - |
66 |
| -/** |
67 |
| - * Constructs and returns a Peer given its endpoint configuration settings. |
68 |
| - * |
69 |
| - * @param {string} url The URL with format of "grpcs://host:port". |
70 |
| - * @param {Object} opts The options for the connection to the peer. |
71 |
| - * @returns {Peer} Returns the new peer. |
72 |
| - */ |
73 |
| -module.exports.getPeer = function(url, opts) { |
74 |
| - var peer = new Peer(url, opts); |
75 |
| - |
76 |
| - return peer; |
77 |
| -}; |
78 |
| - |
79 |
| - |
80 |
| -/** |
81 |
| - * Obtains an instance of the [KeyValueStore]{@link module:api.KeyValueStore} class. By default |
82 |
| - * it returns the built-in implementation, which is based on files ([FileKeyValueStore]{@link module:api.FileKeyValueStore}). |
83 |
| - * This can be overriden with an environment variable KEY_VALUE_STORE, the value of which is the |
84 |
| - * full path of a CommonJS module for the alternative implementation. |
85 |
| - * |
86 |
| - * @param {Object} options is whatever the implementation requires for initializing the instance. For the built-in |
87 |
| - * file-based implementation, this requires a single property "path" to the top-level folder for the store |
88 |
| - * @returns [KeyValueStore]{@link module:api.KeyValueStore} an instance of the KeyValueStore implementation |
89 |
| - */ |
90 |
| -module.exports.newKeyValueStore = function(options) { |
91 |
| - return utils.newKeyValueStore(options); |
92 |
| -}; |
93 |
| - |
94 |
| -/** |
95 |
| - * Configures a logger for the entire HFC SDK to use and override the default logger. Unless this method is called, |
96 |
| - * HFC uses a default logger (based on winston). When using the built-in "winston" based logger, use the environment |
97 |
| - * variable HFC_LOGGING to pass in configurations in the following format: |
98 |
| - * |
99 |
| - * { |
100 |
| - * 'error': 'error.log', // 'error' logs are printed to file 'error.log' relative of the current working dir for node.js |
101 |
| - * 'debug': '/tmp/myapp/debug.log', // 'debug' and anything more critical ('info', 'warn', 'error') can also be an absolute path |
102 |
| - * 'info': 'console' // 'console' is a keyword for logging to console |
103 |
| - * } |
104 |
| - * |
105 |
| - * @param {Object} logger a logger instance that defines the following methods: debug(), info(), warn(), error() with |
106 |
| - * string interpolation methods like [util.format]{@link https://nodejs.org/api/util.html#util_util_format_format}. |
107 |
| - */ |
108 |
| -module.exports.setLogger = function(logger) { |
109 |
| - var err = ''; |
110 |
| - |
111 |
| - if (typeof logger.debug !== 'function') { |
112 |
| - err += 'debug() '; |
113 |
| - } |
114 |
| - |
115 |
| - if (typeof logger.info !== 'function') { |
116 |
| - err += 'info() '; |
117 |
| - } |
118 |
| - |
119 |
| - if (typeof logger.warn !== 'function') { |
120 |
| - err += 'warn() '; |
121 |
| - } |
122 |
| - |
123 |
| - if (typeof logger.error !== 'function' ) { |
124 |
| - err += 'error()'; |
125 |
| - } |
126 |
| - |
127 |
| - if (err !== '') { |
128 |
| - throw new Error('The "logger" parameter must be an object that implements the following methods, which are missing: ' + err); |
129 |
| - } |
130 |
| - |
131 |
| - if (global.hfc) { |
132 |
| - global.hfc.logger = logger; |
133 |
| - } else { |
134 |
| - global.hfc = { |
135 |
| - logger: logger |
136 |
| - }; |
137 |
| - } |
138 |
| -}; |
139 |
| - |
140 |
| -/** |
141 |
| - * Adds a file to the top of the list of configuration setting files that are |
142 |
| - * part of the hierarchical configuration. |
143 |
| - * These files will override the default settings and be overriden by environment, |
144 |
| - * command line arguments, and settings programmatically set into configuration settings. |
145 |
| - * |
146 |
| - * hierarchy search order: |
147 |
| - * 1. memory - all settings added with utils.setConfigSetting(name,value) |
148 |
| - * 2. Command-line arguments |
149 |
| - * 3. Environment variables (names will be change from AAA-BBB to aaa-bbb) |
150 |
| - * 4. Custom Files - all files added with the addConfigFile(path) |
151 |
| - * will be ordered by when added, were last one added will override previously added files |
152 |
| - * 5. The file located at 'config/default.json' with default settings |
153 |
| - * |
154 |
| - * @param {String} path - The path to the file to be added to the top of list of configuration files |
155 |
| - */ |
156 |
| -module.exports.addConfigFile = function(path) { |
157 |
| - |
158 |
| - utils.addConfigFile(path); |
159 |
| -}; |
160 |
| - |
161 |
| -/** |
162 |
| - * Adds a setting to override all settings that are |
163 |
| - * part of the hierarchical configuration. |
164 |
| - * |
165 |
| - * hierarchy search order: |
166 |
| - * 1. memory - settings added with this call |
167 |
| - * 2. Command-line arguments |
168 |
| - * 3. Environment variables (names will be change from AAA-BBB to aaa-bbb) |
169 |
| - * 4. Custom Files - all files added with the addConfigFile(path) |
170 |
| - * will be ordered by when added, were last one added will override previously added files |
171 |
| - * 5. The file located at 'config/default.json' with default settings |
172 |
| - * |
173 |
| - * @param {String} name - The name of a setting |
174 |
| - * @param {Object} value - The value of a setting |
175 |
| - */ |
176 |
| -module.exports.setConfigSetting = function(name, value) { |
177 |
| - |
178 |
| - utils.setConfigSetting(name, value); |
179 |
| -}; |
180 |
| - |
181 |
| -/** |
182 |
| - * Retrieves a setting from the hierarchical configuration and if not found |
183 |
| - * will return the provided default value. |
184 |
| - * |
185 |
| - * hierarchy search order: |
186 |
| - * 1. memory - settings added with utils.setConfigSetting(name,value) |
187 |
| - * 2. Command-line arguments |
188 |
| - * 3. Environment variables (names will be change from AAA-BBB to aaa-bbb) |
189 |
| - * 4. Custom Files - all files added with the addConfigFile(path) |
190 |
| - * will be ordered by when added, were last one added will override previously added files |
191 |
| - * 5. The file located at 'config/default.json' with default settings |
192 |
| - * |
193 |
| - * @param {String} name - The name of a setting |
194 |
| - * @param {Object} default_value - The value of a setting if not found in the hierarchical configuration |
195 |
| - */ |
196 |
| -module.exports.getConfigSetting = function(name, default_value) { |
197 |
| - |
198 |
| - return utils.getConfigSetting(name, default_value); |
199 |
| -}; |
200 |
| - |
201 |
| -/** |
202 |
| - * Builds an unique transaction ID based on the values in |
203 |
| - * the request object. |
204 |
| - * |
205 |
| - * @param {Object} request - An object with the values to be used |
206 |
| - * to build an unique transaction ID. |
207 |
| - * @returns {String} An unique transaction ID |
208 |
| - */ |
209 |
| -module.exports.buildTransactionID = function(request) { |
210 |
| - |
211 |
| - return utils.buildTransactionID(request); |
212 |
| -}; |
213 |
| - |
214 |
| -/** |
215 |
| - * Gets a random number for a one time use |
216 |
| - * |
217 |
| - * @param {int} length - Optional value to control the length of the number. |
218 |
| - * The configuration setting 'nonce-size' will be used if not |
219 |
| - * passed in. |
220 |
| - * @return {int} Random number of the specified length |
221 |
| - */ |
222 |
| -module.exports.getNonce = function(length) { |
223 |
| - |
224 |
| - return utils.buildTransactionID(length); |
225 |
| -}; |
226 |
| - |
| 24 | +module.exports = require('./lib/Client.js'); |
0 commit comments