@tdxvolt/volt-client-grpc 0.11.8 → 0.14.60

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.
@@ -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
  }
@@ -149,6 +149,7 @@ export class VoltCredential {
149
149
  aud: audience,
150
150
  iat: Math.floor(Date.now() / 1000) - ttl,
151
151
  exp: Math.floor(Date.now() / 1000) + ttl,
152
+ sub: this._cryptoCache.client_id,
152
153
  };
153
154
 
154
155
  let sharedKey;