@tdxvolt/volt-client-grpc 0.18.5 → 0.18.6

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
@@ -13,8 +13,8 @@ var Protobuf = require('protobufjs');
13
13
  var camelCase = require('lodash.camelcase');
14
14
  var descriptor = require('protobufjs/ext/descriptor/index.js');
15
15
  var bent = require('bent');
16
- var jwt = require('jsonwebtoken');
17
16
  var uuid = require('uuid');
17
+ var jwt = require('jsonwebtoken');
18
18
 
19
19
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
20
20
 
@@ -4250,206 +4250,6 @@ class GRPCCall extends EventEmitter__default["default"] {
4250
4250
  }
4251
4251
  }
4252
4252
 
4253
- /* eslint-disable no-use-before-define */
4254
-
4255
- const log$3 = debug__default["default"]("volt-client-grpc:volt-connection");
4256
-
4257
- function _cleanUp(immediateReconnect) {
4258
- this._connectionId = "";
4259
-
4260
- if (this._pingTimer) {
4261
- clearTimeout(this._pingTimer);
4262
- this._pingTimer = 0;
4263
- }
4264
-
4265
- if (this._pingTimeoutTimer) {
4266
- clearTimeout(this._pingTimeoutTimer);
4267
- this._pingTimeoutTimer = 0;
4268
- }
4269
-
4270
- if (this._dying) {
4271
- clearTimeout(this._reconnectTimer);
4272
- } else if (immediateReconnect || (this._autoRetry && !this._reconnectTimer)) {
4273
- this._reconnectTimer = setTimeout(
4274
- () => {
4275
- this._reconnectTimer = 0;
4276
- _doConnect.call(this, this._helloPayload);
4277
- },
4278
- immediateReconnect ? 0 : this._reconnectInterval
4279
- );
4280
- }
4281
-
4282
- if (this.call?.writable) {
4283
- this.call.end();
4284
- }
4285
- }
4286
-
4287
- function _connectionTimeoutHandler() {
4288
- log$3("server timed out");
4289
- this.emit("connected", false);
4290
- _cleanUp.call(this);
4291
- }
4292
-
4293
- function _doConnect(connectHello) {
4294
- this.call = new GRPCCall(this._voltClient, "Connect", "METHOD_TYPE_BIDI");
4295
-
4296
- const grpcClient = this._voltClient.getVoltAPIClient();
4297
-
4298
- // Notify the Volt of our ping interval. If the Volt doesn't receive a ping from us
4299
- // within this time, it will assume the connection is dead.
4300
- const defaultHello = {
4301
- ping_interval: this._pingInterval,
4302
- timestamp: Date.now()
4303
- };
4304
-
4305
- const helloPayload = { ...connectHello } || {};
4306
-
4307
- helloPayload.hello = { ...defaultHello, ...connectHello?.hello };
4308
-
4309
- log$3("hello is %j", helloPayload);
4310
-
4311
- this.call.on("error", (err) => {
4312
- log$3("error on connection stream [%s]", err.message);
4313
- _cleanUp.call(this);
4314
- this.emit("error", err);
4315
- });
4316
-
4317
- this.call.on("data", (response) => {
4318
- switch (response.payload) {
4319
- case "ping": {
4320
- log$3("pinged");
4321
-
4322
- // Received ping response from server => clear timeout.
4323
- if (this._pingTimeoutTimer) {
4324
- clearTimeout(this._pingTimeoutTimer);
4325
- }
4326
-
4327
- this.emit("ping", response.ping.timestamp);
4328
- break;
4329
- }
4330
- case "acknowledge": {
4331
- if (!this._connectionId) {
4332
- this._connectionId = response.acknowledge.connection_id;
4333
-
4334
- // Set the ping timeout interval to the value returned by the Volt. If we don't
4335
- // hear from the Volt in this time, we'll assume the connection is dead.
4336
- this._timeoutInterval = response.acknowledge.ping_interval;
4337
-
4338
- log$3("connection id is %s", this._connectionId);
4339
- this.emit("connected", this._connectionId);
4340
-
4341
- // Schedule the next ping.
4342
- _startPingTimer.call(this);
4343
- }
4344
- break;
4345
- }
4346
- case "invoke_request": {
4347
- log$3("received invoke request %s", response.invoke_request.invoke_id);
4348
- // Pause the call while we process the invoke request to avoid getting
4349
- // CANCELLED errors from grpc - we should perhaps leave this to the
4350
- // client to manage as it may be overkill in some cases.
4351
- this.call.pause();
4352
- try {
4353
- this.emit("invoke_request", response.invoke_request);
4354
- } catch (err) {
4355
- log$3("error processing invoke request [%s]", err.message);
4356
- } finally {
4357
- this.call.resume();
4358
- }
4359
- break;
4360
- }
4361
- case "evt": {
4362
- log$3("received event %s", Object.keys(response.evt)[0]);
4363
- this.emit("evt", response.evt);
4364
- break;
4365
- }
4366
- default:
4367
- log$3("unimplemented connect response payload: %s", response.payload);
4368
- break;
4369
- }
4370
- });
4371
-
4372
- this.call.on("end", () => {
4373
- if (this._connectionId) {
4374
- this.emit("connected", false);
4375
- }
4376
- _cleanUp.call(this);
4377
- });
4378
-
4379
- this.call.start(grpcClient, helloPayload);
4380
- }
4381
-
4382
- function _startPingTimer() {
4383
- if (this._pingTimer) {
4384
- log$3("ping timer running");
4385
- return;
4386
- }
4387
-
4388
- this._pingTimer = setTimeout(() => {
4389
- // Send next ping to server.
4390
- const request = { ping: { timestamp: Date.now() } };
4391
-
4392
- log$3("pinging Volt");
4393
- this.call.write(request);
4394
-
4395
- this._pingTimer = 0;
4396
-
4397
- // Schedule a timeout if we don't hear back from the server.
4398
- if (this._pingTimeoutTimer) {
4399
- clearTimeout(this._pingTimeoutTimer);
4400
- }
4401
-
4402
- this._pingTimeoutTimer = setTimeout(
4403
- _connectionTimeoutHandler.bind(this),
4404
- this._timeoutInterval
4405
- );
4406
-
4407
- _startPingTimer.call(this);
4408
- }, this._pingInterval);
4409
- }
4410
-
4411
- class VoltConnection extends EventEmitter__default["default"] {
4412
- constructor(voltClient, grpc) {
4413
- super();
4414
-
4415
- const config = voltClient.config;
4416
- this._grpc = grpc;
4417
- this._autoRetry =
4418
- config.autoReconnect === undefined ? true : !!config.autoReconnect;
4419
- this._dying = false;
4420
- this._voltClient = voltClient;
4421
- this._pingTimer = 0;
4422
- this._pingTimeoutTimer = 0;
4423
- this._connectionId = "";
4424
- this._pingInterval = config.pingInterval || 10000;
4425
- this._reconnectInterval = config.reconnectInterval || 5000;
4426
- this._timeoutInterval = config.timeoutInterval || 60000;
4427
- this._helloPayload = undefined;
4428
- }
4429
-
4430
- get connectionId() {
4431
- return this._connectionId;
4432
- }
4433
-
4434
- connect(helloPayload, throwOnFail) {
4435
- this._helloPayload = helloPayload;
4436
- if (throwOnFail) {
4437
- return _doConnect.call(this, this._helloPayload);
4438
- } else {
4439
- return Promise.resolve(_cleanUp.call(this, true));
4440
- }
4441
- }
4442
-
4443
- disconnect() {
4444
- this._dying = true;
4445
- _cleanUp.call(this);
4446
- }
4447
-
4448
- send(request) {
4449
- this.call.write(request);
4450
- }
4451
- }
4452
-
4453
4253
  const {
4454
4254
  aesDecrypt,
4455
4255
  aesEncrypt,
@@ -4466,7 +4266,7 @@ const {
4466
4266
  x25519
4467
4267
  } = js.voltUtils;
4468
4268
 
4469
- const log$2 = debug__default["default"]("volt-client-grpc:rpc-invocation");
4269
+ const log$3 = debug__default["default"]("volt-client-grpc:rpc-invocation");
4470
4270
 
4471
4271
  class RpcInvocation extends EventEmitter__default["default"] {
4472
4272
  #voltClient = null;
@@ -4520,7 +4320,7 @@ class RpcInvocation extends EventEmitter__default["default"] {
4520
4320
  this.#payload
4521
4321
  );
4522
4322
  } else {
4523
- log$2("unexpected: no method descriptor");
4323
+ log$3("unexpected: no method descriptor");
4524
4324
  throw new Error(
4525
4325
  "no method descriptor - set this before accessing payload"
4526
4326
  );
@@ -4547,7 +4347,7 @@ class RpcInvocation extends EventEmitter__default["default"] {
4547
4347
  this.#token = jwt__default["default"].decode(token);
4548
4348
 
4549
4349
  if (!this.#token) {
4550
- log$2("failed to decode token %s", token || "null");
4350
+ log$3("failed to decode token %s", token || "null");
4551
4351
  return Promise.reject(new Error("failed to decode token"));
4552
4352
  }
4553
4353
 
@@ -4608,7 +4408,7 @@ class RpcInvocation extends EventEmitter__default["default"] {
4608
4408
  if (!this.#derivedKey) {
4609
4409
  // We don't have the derived key yet, so we need to perform the key exchange.
4610
4410
  return this.#keyExchange(invoke_request.token).catch((err) => {
4611
- log$2("failure initialising invocation: %s", err.message);
4411
+ log$3("failure initialising invocation: %s", err.message);
4612
4412
  return Promise.reject(err);
4613
4413
  });
4614
4414
  } else {
@@ -4693,16 +4493,16 @@ class RpcInvocation extends EventEmitter__default["default"] {
4693
4493
  );
4694
4494
  this.emit("payload", this);
4695
4495
  } else if (wrappedPayload.method_end) {
4696
- log$2("method_end received");
4496
+ log$3("method_end received");
4697
4497
  if (!this.#endEmitted) {
4698
4498
  this.#endEmitted = true;
4699
4499
  this.emit("end", this);
4700
4500
  } else {
4701
- log$2("method_end: end already emitted");
4501
+ log$3("method_end: end already emitted");
4702
4502
  }
4703
4503
  }
4704
4504
  } catch (err) {
4705
- log$2("JSON.parse failure parsing json_payload: %s", err.message);
4505
+ log$3("JSON.parse failure parsing json_payload: %s", err.message);
4706
4506
  reject(err);
4707
4507
  }
4708
4508
 
@@ -4712,14 +4512,14 @@ class RpcInvocation extends EventEmitter__default["default"] {
4712
4512
  this.#endEmitted = true;
4713
4513
  this.emit("end", this);
4714
4514
  } else {
4715
- log$2("client_end: end already emitted");
4515
+ log$3("client_end: end already emitted");
4716
4516
  }
4717
4517
  } else {
4718
4518
  // Do we need to support json_payload here?
4719
4519
  reject(new Error("No payload in invoke request"));
4720
4520
  }
4721
4521
  }).catch((err) => {
4722
- log$2("failure parsing payload: %s", err.message);
4522
+ log$3("failure parsing payload: %s", err.message);
4723
4523
  return Promise.reject(err);
4724
4524
  });
4725
4525
  }
@@ -4807,6 +4607,206 @@ class RpcInvocation extends EventEmitter__default["default"] {
4807
4607
  }
4808
4608
  }
4809
4609
 
4610
+ /* eslint-disable no-use-before-define */
4611
+
4612
+ const log$2 = debug__default["default"]("volt-client-grpc:volt-connection");
4613
+
4614
+ function _cleanUp(immediateReconnect) {
4615
+ this._connectionId = "";
4616
+
4617
+ if (this._pingTimer) {
4618
+ clearTimeout(this._pingTimer);
4619
+ this._pingTimer = 0;
4620
+ }
4621
+
4622
+ if (this._pingTimeoutTimer) {
4623
+ clearTimeout(this._pingTimeoutTimer);
4624
+ this._pingTimeoutTimer = 0;
4625
+ }
4626
+
4627
+ if (this._dying) {
4628
+ clearTimeout(this._reconnectTimer);
4629
+ } else if (immediateReconnect || (this._autoRetry && !this._reconnectTimer)) {
4630
+ this._reconnectTimer = setTimeout(
4631
+ () => {
4632
+ this._reconnectTimer = 0;
4633
+ _doConnect.call(this, this._helloPayload);
4634
+ },
4635
+ immediateReconnect ? 0 : this._reconnectInterval
4636
+ );
4637
+ }
4638
+
4639
+ if (this.call?.writable) {
4640
+ this.call.end();
4641
+ }
4642
+ }
4643
+
4644
+ function _connectionTimeoutHandler() {
4645
+ log$2("server timed out");
4646
+ this.emit("connected", false);
4647
+ _cleanUp.call(this);
4648
+ }
4649
+
4650
+ function _doConnect(connectHello) {
4651
+ this.call = new GRPCCall(this._voltClient, "Connect", "METHOD_TYPE_BIDI");
4652
+
4653
+ const grpcClient = this._voltClient.getVoltAPIClient();
4654
+
4655
+ // Notify the Volt of our ping interval. If the Volt doesn't receive a ping from us
4656
+ // within this time, it will assume the connection is dead.
4657
+ const defaultHello = {
4658
+ ping_interval: this._pingInterval,
4659
+ timestamp: Date.now()
4660
+ };
4661
+
4662
+ const helloPayload = { ...connectHello } || {};
4663
+
4664
+ helloPayload.hello = { ...defaultHello, ...connectHello?.hello };
4665
+
4666
+ log$2("hello is %j", helloPayload);
4667
+
4668
+ this.call.on("error", (err) => {
4669
+ log$2("error on connection stream [%s]", err.message);
4670
+ _cleanUp.call(this);
4671
+ this.emit("error", err);
4672
+ });
4673
+
4674
+ this.call.on("data", (response) => {
4675
+ switch (response.payload) {
4676
+ case "ping": {
4677
+ log$2("pinged");
4678
+
4679
+ // Received ping response from server => clear timeout.
4680
+ if (this._pingTimeoutTimer) {
4681
+ clearTimeout(this._pingTimeoutTimer);
4682
+ }
4683
+
4684
+ this.emit("ping", response.ping.timestamp);
4685
+ break;
4686
+ }
4687
+ case "acknowledge": {
4688
+ if (!this._connectionId) {
4689
+ this._connectionId = response.acknowledge.connection_id;
4690
+
4691
+ // Set the ping timeout interval to the value returned by the Volt. If we don't
4692
+ // hear from the Volt in this time, we'll assume the connection is dead.
4693
+ this._timeoutInterval = response.acknowledge.ping_interval;
4694
+
4695
+ log$2("connection id is %s", this._connectionId);
4696
+ this.emit("connected", this._connectionId);
4697
+
4698
+ // Schedule the next ping.
4699
+ _startPingTimer.call(this);
4700
+ }
4701
+ break;
4702
+ }
4703
+ case "invoke_request": {
4704
+ log$2("received invoke request %s", response.invoke_request.invoke_id);
4705
+ // Pause the call while we process the invoke request to avoid getting
4706
+ // CANCELLED errors from grpc - we should perhaps leave this to the
4707
+ // client to manage as it may be overkill in some cases.
4708
+ this.call.pause();
4709
+ try {
4710
+ this.emit("invoke_request", response.invoke_request);
4711
+ } catch (err) {
4712
+ log$2("error processing invoke request [%s]", err.message);
4713
+ } finally {
4714
+ this.call.resume();
4715
+ }
4716
+ break;
4717
+ }
4718
+ case "evt": {
4719
+ log$2("received event %s", Object.keys(response.evt)[0]);
4720
+ this.emit("evt", response.evt);
4721
+ break;
4722
+ }
4723
+ default:
4724
+ log$2("unimplemented connect response payload: %s", response.payload);
4725
+ break;
4726
+ }
4727
+ });
4728
+
4729
+ this.call.on("end", () => {
4730
+ if (this._connectionId) {
4731
+ this.emit("connected", false);
4732
+ }
4733
+ _cleanUp.call(this);
4734
+ });
4735
+
4736
+ this.call.start(grpcClient, helloPayload);
4737
+ }
4738
+
4739
+ function _startPingTimer() {
4740
+ if (this._pingTimer) {
4741
+ log$2("ping timer running");
4742
+ return;
4743
+ }
4744
+
4745
+ this._pingTimer = setTimeout(() => {
4746
+ // Send next ping to server.
4747
+ const request = { ping: { timestamp: Date.now() } };
4748
+
4749
+ log$2("pinging Volt");
4750
+ this.call.write(request);
4751
+
4752
+ this._pingTimer = 0;
4753
+
4754
+ // Schedule a timeout if we don't hear back from the server.
4755
+ if (this._pingTimeoutTimer) {
4756
+ clearTimeout(this._pingTimeoutTimer);
4757
+ }
4758
+
4759
+ this._pingTimeoutTimer = setTimeout(
4760
+ _connectionTimeoutHandler.bind(this),
4761
+ this._timeoutInterval
4762
+ );
4763
+
4764
+ _startPingTimer.call(this);
4765
+ }, this._pingInterval);
4766
+ }
4767
+
4768
+ class VoltConnection extends EventEmitter__default["default"] {
4769
+ constructor(voltClient, grpc) {
4770
+ super();
4771
+
4772
+ const config = voltClient.config;
4773
+ this._grpc = grpc;
4774
+ this._autoRetry =
4775
+ config.autoReconnect === undefined ? true : !!config.autoReconnect;
4776
+ this._dying = false;
4777
+ this._voltClient = voltClient;
4778
+ this._pingTimer = 0;
4779
+ this._pingTimeoutTimer = 0;
4780
+ this._connectionId = "";
4781
+ this._pingInterval = config.pingInterval || 10000;
4782
+ this._reconnectInterval = config.reconnectInterval || 5000;
4783
+ this._timeoutInterval = config.timeoutInterval || 60000;
4784
+ this._helloPayload = undefined;
4785
+ }
4786
+
4787
+ get connectionId() {
4788
+ return this._connectionId;
4789
+ }
4790
+
4791
+ connect(helloPayload, throwOnFail) {
4792
+ this._helloPayload = helloPayload;
4793
+ if (throwOnFail) {
4794
+ return _doConnect.call(this, this._helloPayload);
4795
+ } else {
4796
+ return Promise.resolve(_cleanUp.call(this, true));
4797
+ }
4798
+ }
4799
+
4800
+ disconnect() {
4801
+ this._dying = true;
4802
+ _cleanUp.call(this);
4803
+ }
4804
+
4805
+ send(request) {
4806
+ this.call.write(request);
4807
+ }
4808
+ }
4809
+
4810
4810
  /* eslint-disable no-underscore-dangle */
4811
4811
 
4812
4812
  const { publicKeyToPem, removeCarriageReturn, signBase64 } = js.voltUtils;
@@ -5107,7 +5107,9 @@ function connectInternal(helloPayload) {
5107
5107
 
5108
5108
  if (rpcInvocation) {
5109
5109
  if (rpcInvocation.started) {
5110
- rpcInvocation.parsePayload(invoke_request);
5110
+ rpcInvocation.parsePayload(invoke_request).catch((err) => {
5111
+ log$1("invoke_request - error [%s]", err.message);
5112
+ });
5111
5113
  } else {
5112
5114
  rpcInvocation
5113
5115
  .initialise(invoke_request)
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.18.5",
6
+ "version": "0.18.6",
7
7
  "description": "tdx Volt library for nodejs clients",
8
8
  "type": "module",
9
9
  "exports": {
@@ -1,20 +1,20 @@
1
1
  /* eslint-disable no-underscore-dangle */
2
+ import { voltUtils as voltClientUtils } from "@tdxvolt/volt-client-web/js";
3
+ import bent from "bent";
2
4
  import debug from "debug";
3
5
  import ip from "ip";
4
- import bent from "bent";
5
6
  import lodash from "lodash";
6
- import VoltConnection from "./volt-connection.js";
7
- import {
8
- getServiceDescriptors,
9
- getBuiltInServiceDescriptors
10
- } from "./proto-utils.js";
11
- import { createClient as createGrpcClient } from "./grpc-utils.js";
12
- import * as voltUtils from "./utils.js";
7
+ import { v4 as generateId } from "uuid";
13
8
  import { constants } from "./constants.js";
14
9
  import GRPCCall from "./grpc-call.js";
10
+ import { createClient as createGrpcClient } from "./grpc-utils.js";
11
+ import {
12
+ getBuiltInServiceDescriptors,
13
+ getServiceDescriptors
14
+ } from "./proto-utils.js";
15
15
  import RpcInvocation from "./rpc-invocation.js";
16
- import { v4 as generateId } from "uuid";
17
- import { voltUtils as voltClientUtils } from "@tdxvolt/volt-client-web/js";
16
+ import * as voltUtils from "./utils.js";
17
+ import VoltConnection from "./volt-connection.js";
18
18
 
19
19
  const { publicKeyToPem, removeCarriageReturn, signBase64 } = voltClientUtils;
20
20
 
@@ -314,7 +314,9 @@ export function connectInternal(helloPayload) {
314
314
 
315
315
  if (rpcInvocation) {
316
316
  if (rpcInvocation.started) {
317
- rpcInvocation.parsePayload(invoke_request);
317
+ rpcInvocation.parsePayload(invoke_request).catch((err) => {
318
+ log("invoke_request - error [%s]", err.message);
319
+ });
318
320
  } else {
319
321
  rpcInvocation
320
322
  .initialise(invoke_request)