/
proxybusinessnetworkcardstore.js
203 lines (184 loc) · 7.05 KB
/
proxybusinessnetworkcardstore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const IdCard = require('composer-common').IdCard;
const BusinessNetworkCardStore = require('composer-common').BusinessNetworkCardStore;
const ProxyUtil = require('./proxyutil');
const socketIOClient = require('socket.io-client');
const Logger = require('composer-common').Logger;
const LOG = Logger.getLog('ProxyConnectionProfileStore');
let connectorServerURL = 'http://localhost:15699';
/**
* Manages persistence of business network cards by communicating with a real
* business network card store in the connector server over the socket.io transport.
*/
class ProxyBusinessNetworkCardStore extends BusinessNetworkCardStore {
/**
* Set the connector server URL to use.
* @param {string} url The connector server URL to use.
*/
static setConnectorServerURL (url) {
connectorServerURL = url;
}
/**
* Creates a new ProxyBusinessNetworkCardStore
*/
constructor () {
super();
this.connected = false;
this.socket = socketIOClient(connectorServerURL);
this.socket.on('connect', () => {
this.connected = true;
});
this.socket.on('disconnect', () => {
this.connected = false;
});
}
/**
* Ensure that we are connected to the connector server.
* @return {Promise} A promise that will be resolved when we
* are connected to the connector server, or rejected with an
* error.
*/
ensureConnected () {
if (this.connected) {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
this.socket.once('connect', () => {
resolve();
});
});
}
/**
* Gets a card from the store.
* @abstract
* @param {String} cardName The name of the card to get
* @return {Promise} A promise that is resolved with a {@link IdCard}.
*/
get (cardName) {
const method = 'get';
LOG.entry(method, cardName);
return this.ensureConnected()
.then(() => {
return new Promise((resolve, reject) => {
this.socket.emit('/api/businessNetworkCardStoreGet', cardName, (error, result) => {
if (error) {
return reject(ProxyUtil.inflaterr(error));
}
let cardProperties = result;
let idCard = new IdCard(cardProperties.metadata, cardProperties.connectionProfile);
idCard.setCredentials(cardProperties.credentials);
LOG.exit(method, idCard);
resolve(idCard);
});
});
});
}
/**
* Has returns a boolean indicating whether a card with the specified name exists or not.
* @abstract
* @param {String} cardName The name of the card to check
* @return {Promise} A promise resolved with true or false.
*/
has(cardName) {
const method = 'has';
LOG.entry(method, cardName);
return this.ensureConnected()
.then(() => {
return new Promise((resolve, reject) => {
this.socket.emit('/api/businessNetworkCardStoreHas', cardName, (error, result) => {
if (error) {
return reject(ProxyUtil.inflaterr(error));
}
LOG.exit(method, result);
resolve(result);
});
});
});
}
/**
* Puts a card in the store. It is an error to put a card name that already exists
* in the store.
* @param {String} cardName The name of the card to save
* @param {IdCard} card The card
* @return {Promise} A promise that resolves once the data is written
*/
put (cardName, card) {
const method = 'put';
LOG.entry(method, cardName, card);
return this.ensureConnected()
.then(() => {
return new Promise((resolve, reject) => {
this.socket.emit('/api/businessNetworkCardStorePut', cardName, card, (error, result) => {
if (error) {
return reject(ProxyUtil.inflaterr(error));
}
LOG.exit(method, result);
resolve(result);
});
});
});
}
/**
* Gets all cards from the store.
* @return {Promise} A promise that is resolved with a {@link Map} where
* the keys are identity card names and the values are {@link IdCard} objects.
*/
getAll () {
const method = 'getAll';
LOG.entry(method);
return this.ensureConnected()
.then(() => {
return new Promise((resolve, reject) => {
this.socket.emit('/api/businessNetworkCardStoreGetAll', (error, result) => {
if (error) {
return reject(ProxyUtil.inflaterr(error));
}
let resultMap = new Map();
Object.keys(result).forEach((key) => {
let cardProperties = result[key];
let idCard = new IdCard(cardProperties.metadata, cardProperties.connectionProfile);
idCard.setCredentials(cardProperties.credentials);
resultMap.set(key, idCard);
});
LOG.exit(method, resultMap);
resolve(resultMap);
});
});
});
}
/**
* Delete a specific card from the store.
* @param {String} cardName The name of the card to delete
* @return {Promise} A promise that resolves when the card is deleted.
*/
delete (cardName) {
const method = 'delete';
LOG.entry(method);
return this.ensureConnected()
.then(() => {
return new Promise((resolve, reject) => {
this.socket.emit('/api/businessNetworkCardStoreDelete', cardName, (error, result) => {
if (error) {
return reject(ProxyUtil.inflaterr(error));
}
LOG.exit(method, result);
resolve(result);
});
});
});
}
}
module.exports = ProxyBusinessNetworkCardStore;