@tdxvolt/volt-client-grpc 0.14.58 → 0.14.61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.cjs CHANGED
@@ -523,6 +523,14 @@ class VoltCredential {
523
523
  return this._cryptoCache;
524
524
  }
525
525
 
526
+ get clientId() {
527
+ return this._cryptoCache.client_id;
528
+ }
529
+
530
+ get publicKey() {
531
+ return pki$1.publicKeyToPem(this.getKey().publicKey);
532
+ }
533
+
526
534
  get voltFingerprint() {
527
535
  return this._voltFingerprint;
528
536
  }
@@ -603,6 +611,7 @@ class VoltCredential {
603
611
  aud: audience,
604
612
  iat: Math.floor(Date.now() / 1000) - ttl,
605
613
  exp: Math.floor(Date.now() / 1000) + ttl,
614
+ sub: this._cryptoCache.client_id,
606
615
  };
607
616
 
608
617
  let sharedKey;
@@ -1082,50 +1091,55 @@ class GRPCCall extends EventEmitter__default["default"] {
1082
1091
  const log$2 = debug__default["default"]("volt-client-grpc:volt-connection");
1083
1092
 
1084
1093
  function _cleanUp(immediateReconnect) {
1085
- this._session = false;
1086
-
1087
- if (this._pingTimer) {
1088
- clearTimeout(this._pingTimer);
1089
- this._pingTimer = 0;
1090
- }
1091
-
1092
- if (this._pingTimeoutTimer) {
1093
- clearTimeout(this._pingTimeoutTimer);
1094
- this._pingTimeoutTimer = 0;
1095
- }
1096
-
1097
- if (
1098
- !this._dying &&
1099
- (immediateReconnect || (this._autoRetry && !this._reconnectTimer))
1100
- ) {
1101
- this._reconnectTimer = setTimeout(
1102
- () => {
1103
- this._reconnectTimer = 0;
1104
- _doConnect.call(this);
1105
- },
1106
- immediateReconnect ? 0 : this._reconnectInterval,
1107
- );
1108
- }
1109
-
1110
- if (this.call?.writable) {
1111
- this.call.end();
1112
- }
1094
+ this._connectionId = "";
1095
+
1096
+ if (this._pingTimer) {
1097
+ clearTimeout(this._pingTimer);
1098
+ this._pingTimer = 0;
1099
+ }
1100
+
1101
+ if (this._pingTimeoutTimer) {
1102
+ clearTimeout(this._pingTimeoutTimer);
1103
+ this._pingTimeoutTimer = 0;
1104
+ }
1105
+
1106
+ if (
1107
+ !this._dying &&
1108
+ (immediateReconnect || (this._autoRetry && !this._reconnectTimer))
1109
+ ) {
1110
+ this._reconnectTimer = setTimeout(
1111
+ () => {
1112
+ this._reconnectTimer = 0;
1113
+ _doConnect.call(this, this._helloPayload);
1114
+ },
1115
+ immediateReconnect ? 0 : this._reconnectInterval,
1116
+ );
1117
+ }
1118
+
1119
+ if (this.call?.writable) {
1120
+ this.call.end();
1121
+ }
1113
1122
  }
1114
1123
 
1115
1124
  function _connectionTimeoutHandler() {
1116
- log$2("server timed out");
1117
- this.emit("connected", false);
1118
- _cleanUp.call(this);
1125
+ log$2("server timed out");
1126
+ this.emit("connected", false);
1127
+ _cleanUp.call(this);
1119
1128
  }
1120
1129
 
1121
- function _doConnect() {
1122
- this.call = new GRPCCall(this._voltClient, "Connect", "METHOD_TYPE_BIDI");
1130
+ function _doConnect(connectHello) {
1131
+ this.call = new GRPCCall(this._voltClient, "Connect", "METHOD_TYPE_BIDI");
1132
+
1133
+ const grpcClient = this._voltClient.getVoltAPIClient();
1134
+
1135
+ let helloPayload = connectHello;
1136
+ if (!connectHello) {
1137
+ log$2("defaulting hello payload");
1138
+ helloPayload = {
1139
+ hello: {},
1140
+ };
1141
+ }
1123
1142
 
1124
- const grpcClient = this._voltClient.getVoltAPIClient();
1125
- const helloPayload = {
1126
- hello: {},
1127
- };
1128
-
1129
1143
  this.call.on("error", (err) => {
1130
1144
  log$2("error on connection stream [%s]", err.message);
1131
1145
  _cleanUp.call(this);
@@ -1146,10 +1160,11 @@ function _doConnect() {
1146
1160
  _startPingTimer.call(this);
1147
1161
  break;
1148
1162
  }
1149
- case "hello": {
1150
- if (!this._session) {
1151
- this._session = true;
1152
- this.emit("connected", this._session);
1163
+ case "acknowledge": {
1164
+ if (!this._connectionId) {
1165
+ this._connectionId = response.acknowledge.connection_id;
1166
+ log$2("connection id is %s", this._connectionId);
1167
+ this.emit("connected", this._connectionId);
1153
1168
  }
1154
1169
  break;
1155
1170
  }
@@ -1165,74 +1180,76 @@ function _doConnect() {
1165
1180
  });
1166
1181
 
1167
1182
  this.call.on("end", () => {
1168
- if (this._session) {
1183
+ if (this._connectionId) {
1169
1184
  this.emit("connected", false);
1170
1185
  }
1171
1186
  _cleanUp.call(this);
1172
1187
  });
1173
1188
 
1174
- this.call.start(grpcClient, helloPayload);
1189
+ this.call.start(grpcClient, helloPayload);
1175
1190
 
1176
- _startPingTimer.call(this);
1191
+ _startPingTimer.call(this);
1177
1192
  }
1178
1193
 
1179
1194
  function _startPingTimer() {
1180
- if (this._pingTimer) {
1181
- log$2("ping timer running");
1182
- return;
1183
- }
1184
-
1185
- this._pingTimer = setTimeout(() => {
1186
- // Send next ping to server.
1187
- const request = { ping: { timestamp: Date.now() } };
1188
-
1189
- log$2("pinging Volt");
1190
- this.call.write(request);
1191
-
1192
- this._pingTimer = 0;
1193
-
1194
- // Schedule a timeout if we don't hear back from the server.
1195
- if (this._pingTimeoutTimer) {
1196
- clearTimeout(this._pingTimeoutTimer);
1197
- }
1198
-
1199
- this._pingTimeoutTimer = setTimeout(
1200
- _connectionTimeoutHandler.bind(this),
1201
- this._timeoutInterval,
1202
- );
1203
- }, this._pingInterval);
1195
+ if (this._pingTimer) {
1196
+ log$2("ping timer running");
1197
+ return;
1198
+ }
1199
+
1200
+ this._pingTimer = setTimeout(() => {
1201
+ // Send next ping to server.
1202
+ const request = { ping: { timestamp: Date.now() } };
1203
+
1204
+ log$2("pinging Volt");
1205
+ this.call.write(request);
1206
+
1207
+ this._pingTimer = 0;
1208
+
1209
+ // Schedule a timeout if we don't hear back from the server.
1210
+ if (this._pingTimeoutTimer) {
1211
+ clearTimeout(this._pingTimeoutTimer);
1212
+ }
1213
+
1214
+ this._pingTimeoutTimer = setTimeout(
1215
+ _connectionTimeoutHandler.bind(this),
1216
+ this._timeoutInterval,
1217
+ );
1218
+ }, this._pingInterval);
1204
1219
  }
1205
1220
 
1206
1221
  class VoltConnection extends EventEmitter__default["default"] {
1207
- constructor(voltClient, grpc) {
1208
- super();
1209
-
1210
- const config = voltClient.config;
1211
- this._grpc = grpc;
1212
- this._autoRetry =
1213
- config.autoReconnect === undefined ? true : !!config.autoReconnect;
1214
- this._dying = false;
1215
- this._voltClient = voltClient;
1216
- this._pingTimer = 0;
1217
- this._pingTimeoutTimer = 0;
1218
- this._session = false;
1219
- this._pingInterval = config.pingInterval || 10000;
1220
- this._reconnectInterval = config.reconnectInterval || 5000;
1221
- this._timeoutInterval = config.timeoutInterval || 60000;
1222
- }
1223
-
1224
- connect(throwOnFail) {
1225
- if (throwOnFail) {
1226
- return _doConnect.call(this);
1227
- } else {
1228
- return Promise.resolve(_cleanUp.call(this, true));
1229
- }
1230
- }
1231
-
1232
- disconnect() {
1233
- this._dying = true;
1234
- _cleanUp.call(this);
1235
- }
1222
+ constructor(voltClient, grpc) {
1223
+ super();
1224
+
1225
+ const config = voltClient.config;
1226
+ this._grpc = grpc;
1227
+ this._autoRetry =
1228
+ config.autoReconnect === undefined ? true : !!config.autoReconnect;
1229
+ this._dying = false;
1230
+ this._voltClient = voltClient;
1231
+ this._pingTimer = 0;
1232
+ this._pingTimeoutTimer = 0;
1233
+ this._connectionId = "";
1234
+ this._pingInterval = config.pingInterval || 10000;
1235
+ this._reconnectInterval = config.reconnectInterval || 5000;
1236
+ this._timeoutInterval = config.timeoutInterval || 60000;
1237
+ this._helloPayload = undefined;
1238
+ }
1239
+
1240
+ connect(helloPayload, throwOnFail) {
1241
+ this._helloPayload = helloPayload;
1242
+ if (throwOnFail) {
1243
+ return _doConnect.call(this, this._helloPayload);
1244
+ } else {
1245
+ return Promise.resolve(_cleanUp.call(this, true));
1246
+ }
1247
+ }
1248
+
1249
+ disconnect() {
1250
+ this._dying = true;
1251
+ _cleanUp.call(this);
1252
+ }
1236
1253
  }
1237
1254
 
1238
1255
  /* eslint-disable no-underscore-dangle */
@@ -1413,7 +1430,7 @@ async function bindInternal() {
1413
1430
  }
1414
1431
  }
1415
1432
 
1416
- function connectInternal() {
1433
+ function connectInternal(helloPayload) {
1417
1434
  try {
1418
1435
  if (this._voltConnection) {
1419
1436
  log$1("connectInternal - Volt connection already exists");
@@ -1443,7 +1460,7 @@ function connectInternal() {
1443
1460
  if (!this._connected) {
1444
1461
  onDisconnected();
1445
1462
  }
1446
- this.emit("connected", !!session);
1463
+ this.emit("connected", session);
1447
1464
  });
1448
1465
 
1449
1466
  this._voltConnection.on("error", (err) => {
@@ -1459,7 +1476,7 @@ function connectInternal() {
1459
1476
  this.emit("evt", evt);
1460
1477
  });
1461
1478
 
1462
- return this._voltConnection.connect();
1479
+ return this._voltConnection.connect(helloPayload);
1463
1480
  } catch (err) {
1464
1481
  log$1("connect - error [%s]", err.message);
1465
1482
  throw err;
@@ -1758,10 +1775,10 @@ class VoltClient extends EventEmitter__default["default"] {
1758
1775
  }
1759
1776
  }
1760
1777
 
1761
- initialiseAndConnect(configPath, extras = {}) {
1778
+ initialiseAndConnect(configPath, helloPayload = undefined, extras = {}) {
1762
1779
  return this.initialise(configPath, extras)
1763
1780
  .then(() => {
1764
- return connectInternal.call(this);
1781
+ return connectInternal.call(this, helloPayload);
1765
1782
  })
1766
1783
  .catch((err) => {
1767
1784
  log("initialiseAndConnect failure: %s", err.message);
@@ -1834,8 +1851,8 @@ class VoltClient extends EventEmitter__default["default"] {
1834
1851
  return this.isRemote && !this._voltConfig?.relay.cloud;
1835
1852
  }
1836
1853
 
1837
- connect() {
1838
- return connectInternal.call(this);
1854
+ connect(helloPayload) {
1855
+ return connectInternal.call(this, helloPayload);
1839
1856
  }
1840
1857
 
1841
1858
  disconnect() {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.14.58",
6
+ "version": "0.14.61",
7
7
  "description": "tdx Volt library for nodejs clients",
8
8
  "type": "module",
9
9
  "exports": {
@@ -48,4 +48,4 @@
48
48
  "node-forge": "^0.9.0",
49
49
  "uuid": "^8.1.0"
50
50
  }
51
- }
51
+ }
@@ -187,7 +187,7 @@ export async function bindInternal() {
187
187
  }
188
188
  }
189
189
 
190
- export function connectInternal() {
190
+ export function connectInternal(helloPayload) {
191
191
  try {
192
192
  if (this._voltConnection) {
193
193
  log("connectInternal - Volt connection already exists");
@@ -217,7 +217,7 @@ export function connectInternal() {
217
217
  if (!this._connected) {
218
218
  onDisconnected();
219
219
  }
220
- this.emit("connected", !!session);
220
+ this.emit("connected", session);
221
221
  });
222
222
 
223
223
  this._voltConnection.on("error", (err) => {
@@ -233,7 +233,7 @@ export function connectInternal() {
233
233
  this.emit("evt", evt);
234
234
  });
235
235
 
236
- return this._voltConnection.connect();
236
+ return this._voltConnection.connect(helloPayload);
237
237
  } catch (err) {
238
238
  log("connect - error [%s]", err.message);
239
239
  throw err;
@@ -189,10 +189,10 @@ export class VoltClient extends EventEmitter {
189
189
  }
190
190
  }
191
191
 
192
- initialiseAndConnect(configPath, extras = {}) {
192
+ initialiseAndConnect(configPath, helloPayload = undefined, extras = {}) {
193
193
  return this.initialise(configPath, extras)
194
194
  .then(() => {
195
- return connectInternal.call(this);
195
+ return connectInternal.call(this, helloPayload);
196
196
  })
197
197
  .catch((err) => {
198
198
  log("initialiseAndConnect failure: %s", err.message);
@@ -265,8 +265,8 @@ export class VoltClient extends EventEmitter {
265
265
  return this.isRemote && !this._voltConfig?.relay.cloud;
266
266
  }
267
267
 
268
- connect() {
269
- return connectInternal.call(this);
268
+ connect(helloPayload) {
269
+ return connectInternal.call(this, helloPayload);
270
270
  }
271
271
 
272
272
  disconnect() {
@@ -7,50 +7,55 @@ import GRPCCall from "./grpc-call.js";
7
7
  const log = debug("volt-client-grpc:volt-connection");
8
8
 
9
9
  function _cleanUp(immediateReconnect) {
10
- this._session = false;
11
-
12
- if (this._pingTimer) {
13
- clearTimeout(this._pingTimer);
14
- this._pingTimer = 0;
15
- }
16
-
17
- if (this._pingTimeoutTimer) {
18
- clearTimeout(this._pingTimeoutTimer);
19
- this._pingTimeoutTimer = 0;
20
- }
21
-
22
- if (
23
- !this._dying &&
24
- (immediateReconnect || (this._autoRetry && !this._reconnectTimer))
25
- ) {
26
- this._reconnectTimer = setTimeout(
27
- () => {
28
- this._reconnectTimer = 0;
29
- _doConnect.call(this);
30
- },
31
- immediateReconnect ? 0 : this._reconnectInterval,
32
- );
33
- }
34
-
35
- if (this.call?.writable) {
36
- this.call.end();
37
- }
10
+ this._connectionId = "";
11
+
12
+ if (this._pingTimer) {
13
+ clearTimeout(this._pingTimer);
14
+ this._pingTimer = 0;
15
+ }
16
+
17
+ if (this._pingTimeoutTimer) {
18
+ clearTimeout(this._pingTimeoutTimer);
19
+ this._pingTimeoutTimer = 0;
20
+ }
21
+
22
+ if (
23
+ !this._dying &&
24
+ (immediateReconnect || (this._autoRetry && !this._reconnectTimer))
25
+ ) {
26
+ this._reconnectTimer = setTimeout(
27
+ () => {
28
+ this._reconnectTimer = 0;
29
+ _doConnect.call(this, this._helloPayload);
30
+ },
31
+ immediateReconnect ? 0 : this._reconnectInterval,
32
+ );
33
+ }
34
+
35
+ if (this.call?.writable) {
36
+ this.call.end();
37
+ }
38
38
  }
39
39
 
40
40
  function _connectionTimeoutHandler() {
41
- log("server timed out");
42
- this.emit("connected", false);
43
- _cleanUp.call(this);
41
+ log("server timed out");
42
+ this.emit("connected", false);
43
+ _cleanUp.call(this);
44
44
  }
45
45
 
46
- function _doConnect() {
47
- this.call = new GRPCCall(this._voltClient, "Connect", "METHOD_TYPE_BIDI");
46
+ function _doConnect(connectHello) {
47
+ this.call = new GRPCCall(this._voltClient, "Connect", "METHOD_TYPE_BIDI");
48
+
49
+ const grpcClient = this._voltClient.getVoltAPIClient();
50
+
51
+ let helloPayload = connectHello;
52
+ if (!connectHello) {
53
+ log("defaulting hello payload");
54
+ helloPayload = {
55
+ hello: {},
56
+ };
57
+ }
48
58
 
49
- const grpcClient = this._voltClient.getVoltAPIClient();
50
- const helloPayload = {
51
- hello: {},
52
- };
53
-
54
59
  this.call.on("error", (err) => {
55
60
  log("error on connection stream [%s]", err.message);
56
61
  _cleanUp.call(this);
@@ -71,10 +76,11 @@ function _doConnect() {
71
76
  _startPingTimer.call(this);
72
77
  break;
73
78
  }
74
- case "hello": {
75
- if (!this._session) {
76
- this._session = true;
77
- this.emit("connected", this._session);
79
+ case "acknowledge": {
80
+ if (!this._connectionId) {
81
+ this._connectionId = response.acknowledge.connection_id;
82
+ log("connection id is %s", this._connectionId);
83
+ this.emit("connected", this._connectionId);
78
84
  }
79
85
  break;
80
86
  }
@@ -90,72 +96,74 @@ function _doConnect() {
90
96
  });
91
97
 
92
98
  this.call.on("end", () => {
93
- if (this._session) {
99
+ if (this._connectionId) {
94
100
  this.emit("connected", false);
95
101
  }
96
102
  _cleanUp.call(this);
97
103
  });
98
104
 
99
- this.call.start(grpcClient, helloPayload);
105
+ this.call.start(grpcClient, helloPayload);
100
106
 
101
- _startPingTimer.call(this);
107
+ _startPingTimer.call(this);
102
108
  }
103
109
 
104
110
  function _startPingTimer() {
105
- if (this._pingTimer) {
106
- log("ping timer running");
107
- return;
108
- }
109
-
110
- this._pingTimer = setTimeout(() => {
111
- // Send next ping to server.
112
- const request = { ping: { timestamp: Date.now() } };
113
-
114
- log("pinging Volt");
115
- this.call.write(request);
116
-
117
- this._pingTimer = 0;
118
-
119
- // Schedule a timeout if we don't hear back from the server.
120
- if (this._pingTimeoutTimer) {
121
- clearTimeout(this._pingTimeoutTimer);
122
- }
123
-
124
- this._pingTimeoutTimer = setTimeout(
125
- _connectionTimeoutHandler.bind(this),
126
- this._timeoutInterval,
127
- );
128
- }, this._pingInterval);
111
+ if (this._pingTimer) {
112
+ log("ping timer running");
113
+ return;
114
+ }
115
+
116
+ this._pingTimer = setTimeout(() => {
117
+ // Send next ping to server.
118
+ const request = { ping: { timestamp: Date.now() } };
119
+
120
+ log("pinging Volt");
121
+ this.call.write(request);
122
+
123
+ this._pingTimer = 0;
124
+
125
+ // Schedule a timeout if we don't hear back from the server.
126
+ if (this._pingTimeoutTimer) {
127
+ clearTimeout(this._pingTimeoutTimer);
128
+ }
129
+
130
+ this._pingTimeoutTimer = setTimeout(
131
+ _connectionTimeoutHandler.bind(this),
132
+ this._timeoutInterval,
133
+ );
134
+ }, this._pingInterval);
129
135
  }
130
136
 
131
137
  export default class VoltConnection extends EventEmitter {
132
- constructor(voltClient, grpc) {
133
- super();
134
-
135
- const config = voltClient.config;
136
- this._grpc = grpc;
137
- this._autoRetry =
138
- config.autoReconnect === undefined ? true : !!config.autoReconnect;
139
- this._dying = false;
140
- this._voltClient = voltClient;
141
- this._pingTimer = 0;
142
- this._pingTimeoutTimer = 0;
143
- this._session = false;
144
- this._pingInterval = config.pingInterval || 10000;
145
- this._reconnectInterval = config.reconnectInterval || 5000;
146
- this._timeoutInterval = config.timeoutInterval || 60000;
147
- }
148
-
149
- connect(throwOnFail) {
150
- if (throwOnFail) {
151
- return _doConnect.call(this);
152
- } else {
153
- return Promise.resolve(_cleanUp.call(this, true));
154
- }
155
- }
156
-
157
- disconnect() {
158
- this._dying = true;
159
- _cleanUp.call(this);
160
- }
138
+ constructor(voltClient, grpc) {
139
+ super();
140
+
141
+ const config = voltClient.config;
142
+ this._grpc = grpc;
143
+ this._autoRetry =
144
+ config.autoReconnect === undefined ? true : !!config.autoReconnect;
145
+ this._dying = false;
146
+ this._voltClient = voltClient;
147
+ this._pingTimer = 0;
148
+ this._pingTimeoutTimer = 0;
149
+ this._connectionId = "";
150
+ this._pingInterval = config.pingInterval || 10000;
151
+ this._reconnectInterval = config.reconnectInterval || 5000;
152
+ this._timeoutInterval = config.timeoutInterval || 60000;
153
+ this._helloPayload = undefined;
154
+ }
155
+
156
+ connect(helloPayload, throwOnFail) {
157
+ this._helloPayload = helloPayload;
158
+ if (throwOnFail) {
159
+ return _doConnect.call(this, this._helloPayload);
160
+ } else {
161
+ return Promise.resolve(_cleanUp.call(this, true));
162
+ }
163
+ }
164
+
165
+ disconnect() {
166
+ this._dying = true;
167
+ _cleanUp.call(this);
168
+ }
161
169
  }
@@ -69,6 +69,14 @@ export class VoltCredential {
69
69
  return this._cryptoCache;
70
70
  }
71
71
 
72
+ get clientId() {
73
+ return this._cryptoCache.client_id;
74
+ }
75
+
76
+ get publicKey() {
77
+ return pki.publicKeyToPem(this.getKey().publicKey);
78
+ }
79
+
72
80
  get voltFingerprint() {
73
81
  return this._voltFingerprint;
74
82
  }
@@ -149,6 +157,7 @@ export class VoltCredential {
149
157
  aud: audience,
150
158
  iat: Math.floor(Date.now() / 1000) - ttl,
151
159
  exp: Math.floor(Date.now() / 1000) + ttl,
160
+ sub: this._cryptoCache.client_id,
152
161
  };
153
162
 
154
163
  let sharedKey;