@tdxvolt/volt-client-grpc 0.18.5 → 0.18.7

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
 
@@ -96,7 +96,10 @@ const getDIDResolutionURL = (didIn, registryUrl = "") => {
96
96
  return "";
97
97
  }
98
98
 
99
- const lookupUrl = registryUrl || `https://${constants.defaultDIDHostName}`;
99
+ let lookupUrl = registryUrl || `https://${constants.defaultDIDHostName}`;
100
+ if (!lookupUrl.startsWith("http")) {
101
+ lookupUrl = `https://${lookupUrl}`;
102
+ }
100
103
  return `${lookupUrl}/did/${didIn}`;
101
104
  };
102
105
 
@@ -4250,206 +4253,6 @@ class GRPCCall extends EventEmitter__default["default"] {
4250
4253
  }
4251
4254
  }
4252
4255
 
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
4256
  const {
4454
4257
  aesDecrypt,
4455
4258
  aesEncrypt,
@@ -4466,7 +4269,7 @@ const {
4466
4269
  x25519
4467
4270
  } = js.voltUtils;
4468
4271
 
4469
- const log$2 = debug__default["default"]("volt-client-grpc:rpc-invocation");
4272
+ const log$3 = debug__default["default"]("volt-client-grpc:rpc-invocation");
4470
4273
 
4471
4274
  class RpcInvocation extends EventEmitter__default["default"] {
4472
4275
  #voltClient = null;
@@ -4520,7 +4323,7 @@ class RpcInvocation extends EventEmitter__default["default"] {
4520
4323
  this.#payload
4521
4324
  );
4522
4325
  } else {
4523
- log$2("unexpected: no method descriptor");
4326
+ log$3("unexpected: no method descriptor");
4524
4327
  throw new Error(
4525
4328
  "no method descriptor - set this before accessing payload"
4526
4329
  );
@@ -4547,7 +4350,7 @@ class RpcInvocation extends EventEmitter__default["default"] {
4547
4350
  this.#token = jwt__default["default"].decode(token);
4548
4351
 
4549
4352
  if (!this.#token) {
4550
- log$2("failed to decode token %s", token || "null");
4353
+ log$3("failed to decode token %s", token || "null");
4551
4354
  return Promise.reject(new Error("failed to decode token"));
4552
4355
  }
4553
4356
 
@@ -4608,7 +4411,7 @@ class RpcInvocation extends EventEmitter__default["default"] {
4608
4411
  if (!this.#derivedKey) {
4609
4412
  // We don't have the derived key yet, so we need to perform the key exchange.
4610
4413
  return this.#keyExchange(invoke_request.token).catch((err) => {
4611
- log$2("failure initialising invocation: %s", err.message);
4414
+ log$3("failure initialising invocation: %s", err.message);
4612
4415
  return Promise.reject(err);
4613
4416
  });
4614
4417
  } else {
@@ -4693,16 +4496,16 @@ class RpcInvocation extends EventEmitter__default["default"] {
4693
4496
  );
4694
4497
  this.emit("payload", this);
4695
4498
  } else if (wrappedPayload.method_end) {
4696
- log$2("method_end received");
4499
+ log$3("method_end received");
4697
4500
  if (!this.#endEmitted) {
4698
4501
  this.#endEmitted = true;
4699
4502
  this.emit("end", this);
4700
4503
  } else {
4701
- log$2("method_end: end already emitted");
4504
+ log$3("method_end: end already emitted");
4702
4505
  }
4703
4506
  }
4704
4507
  } catch (err) {
4705
- log$2("JSON.parse failure parsing json_payload: %s", err.message);
4508
+ log$3("JSON.parse failure parsing json_payload: %s", err.message);
4706
4509
  reject(err);
4707
4510
  }
4708
4511
 
@@ -4712,14 +4515,14 @@ class RpcInvocation extends EventEmitter__default["default"] {
4712
4515
  this.#endEmitted = true;
4713
4516
  this.emit("end", this);
4714
4517
  } else {
4715
- log$2("client_end: end already emitted");
4518
+ log$3("client_end: end already emitted");
4716
4519
  }
4717
4520
  } else {
4718
4521
  // Do we need to support json_payload here?
4719
4522
  reject(new Error("No payload in invoke request"));
4720
4523
  }
4721
4524
  }).catch((err) => {
4722
- log$2("failure parsing payload: %s", err.message);
4525
+ log$3("failure parsing payload: %s", err.message);
4723
4526
  return Promise.reject(err);
4724
4527
  });
4725
4528
  }
@@ -4807,6 +4610,206 @@ class RpcInvocation extends EventEmitter__default["default"] {
4807
4610
  }
4808
4611
  }
4809
4612
 
4613
+ /* eslint-disable no-use-before-define */
4614
+
4615
+ const log$2 = debug__default["default"]("volt-client-grpc:volt-connection");
4616
+
4617
+ function _cleanUp(immediateReconnect) {
4618
+ this._connectionId = "";
4619
+
4620
+ if (this._pingTimer) {
4621
+ clearTimeout(this._pingTimer);
4622
+ this._pingTimer = 0;
4623
+ }
4624
+
4625
+ if (this._pingTimeoutTimer) {
4626
+ clearTimeout(this._pingTimeoutTimer);
4627
+ this._pingTimeoutTimer = 0;
4628
+ }
4629
+
4630
+ if (this._dying) {
4631
+ clearTimeout(this._reconnectTimer);
4632
+ } else if (immediateReconnect || (this._autoRetry && !this._reconnectTimer)) {
4633
+ this._reconnectTimer = setTimeout(
4634
+ () => {
4635
+ this._reconnectTimer = 0;
4636
+ _doConnect.call(this, this._helloPayload);
4637
+ },
4638
+ immediateReconnect ? 0 : this._reconnectInterval
4639
+ );
4640
+ }
4641
+
4642
+ if (this.call?.writable) {
4643
+ this.call.end();
4644
+ }
4645
+ }
4646
+
4647
+ function _connectionTimeoutHandler() {
4648
+ log$2("server timed out");
4649
+ this.emit("connected", false);
4650
+ _cleanUp.call(this);
4651
+ }
4652
+
4653
+ function _doConnect(connectHello) {
4654
+ this.call = new GRPCCall(this._voltClient, "Connect", "METHOD_TYPE_BIDI");
4655
+
4656
+ const grpcClient = this._voltClient.getVoltAPIClient();
4657
+
4658
+ // Notify the Volt of our ping interval. If the Volt doesn't receive a ping from us
4659
+ // within this time, it will assume the connection is dead.
4660
+ const defaultHello = {
4661
+ ping_interval: this._pingInterval,
4662
+ timestamp: Date.now()
4663
+ };
4664
+
4665
+ const helloPayload = { ...connectHello } || {};
4666
+
4667
+ helloPayload.hello = { ...defaultHello, ...connectHello?.hello };
4668
+
4669
+ log$2("hello is %j", helloPayload);
4670
+
4671
+ this.call.on("error", (err) => {
4672
+ log$2("error on connection stream [%s]", err.message);
4673
+ _cleanUp.call(this);
4674
+ this.emit("error", err);
4675
+ });
4676
+
4677
+ this.call.on("data", (response) => {
4678
+ switch (response.payload) {
4679
+ case "ping": {
4680
+ log$2("pinged");
4681
+
4682
+ // Received ping response from server => clear timeout.
4683
+ if (this._pingTimeoutTimer) {
4684
+ clearTimeout(this._pingTimeoutTimer);
4685
+ }
4686
+
4687
+ this.emit("ping", response.ping.timestamp);
4688
+ break;
4689
+ }
4690
+ case "acknowledge": {
4691
+ if (!this._connectionId) {
4692
+ this._connectionId = response.acknowledge.connection_id;
4693
+
4694
+ // Set the ping timeout interval to the value returned by the Volt. If we don't
4695
+ // hear from the Volt in this time, we'll assume the connection is dead.
4696
+ this._timeoutInterval = response.acknowledge.ping_interval;
4697
+
4698
+ log$2("connection id is %s", this._connectionId);
4699
+ this.emit("connected", this._connectionId);
4700
+
4701
+ // Schedule the next ping.
4702
+ _startPingTimer.call(this);
4703
+ }
4704
+ break;
4705
+ }
4706
+ case "invoke_request": {
4707
+ log$2("received invoke request %s", response.invoke_request.invoke_id);
4708
+ // Pause the call while we process the invoke request to avoid getting
4709
+ // CANCELLED errors from grpc - we should perhaps leave this to the
4710
+ // client to manage as it may be overkill in some cases.
4711
+ this.call.pause();
4712
+ try {
4713
+ this.emit("invoke_request", response.invoke_request);
4714
+ } catch (err) {
4715
+ log$2("error processing invoke request [%s]", err.message);
4716
+ } finally {
4717
+ this.call.resume();
4718
+ }
4719
+ break;
4720
+ }
4721
+ case "evt": {
4722
+ log$2("received event %s", Object.keys(response.evt)[0]);
4723
+ this.emit("evt", response.evt);
4724
+ break;
4725
+ }
4726
+ default:
4727
+ log$2("unimplemented connect response payload: %s", response.payload);
4728
+ break;
4729
+ }
4730
+ });
4731
+
4732
+ this.call.on("end", () => {
4733
+ if (this._connectionId) {
4734
+ this.emit("connected", false);
4735
+ }
4736
+ _cleanUp.call(this);
4737
+ });
4738
+
4739
+ this.call.start(grpcClient, helloPayload);
4740
+ }
4741
+
4742
+ function _startPingTimer() {
4743
+ if (this._pingTimer) {
4744
+ log$2("ping timer running");
4745
+ return;
4746
+ }
4747
+
4748
+ this._pingTimer = setTimeout(() => {
4749
+ // Send next ping to server.
4750
+ const request = { ping: { timestamp: Date.now() } };
4751
+
4752
+ log$2("pinging Volt");
4753
+ this.call.write(request);
4754
+
4755
+ this._pingTimer = 0;
4756
+
4757
+ // Schedule a timeout if we don't hear back from the server.
4758
+ if (this._pingTimeoutTimer) {
4759
+ clearTimeout(this._pingTimeoutTimer);
4760
+ }
4761
+
4762
+ this._pingTimeoutTimer = setTimeout(
4763
+ _connectionTimeoutHandler.bind(this),
4764
+ this._timeoutInterval
4765
+ );
4766
+
4767
+ _startPingTimer.call(this);
4768
+ }, this._pingInterval);
4769
+ }
4770
+
4771
+ class VoltConnection extends EventEmitter__default["default"] {
4772
+ constructor(voltClient, grpc) {
4773
+ super();
4774
+
4775
+ const config = voltClient.config;
4776
+ this._grpc = grpc;
4777
+ this._autoRetry =
4778
+ config.autoReconnect === undefined ? true : !!config.autoReconnect;
4779
+ this._dying = false;
4780
+ this._voltClient = voltClient;
4781
+ this._pingTimer = 0;
4782
+ this._pingTimeoutTimer = 0;
4783
+ this._connectionId = "";
4784
+ this._pingInterval = config.pingInterval || 10000;
4785
+ this._reconnectInterval = config.reconnectInterval || 5000;
4786
+ this._timeoutInterval = config.timeoutInterval || 60000;
4787
+ this._helloPayload = undefined;
4788
+ }
4789
+
4790
+ get connectionId() {
4791
+ return this._connectionId;
4792
+ }
4793
+
4794
+ connect(helloPayload, throwOnFail) {
4795
+ this._helloPayload = helloPayload;
4796
+ if (throwOnFail) {
4797
+ return _doConnect.call(this, this._helloPayload);
4798
+ } else {
4799
+ return Promise.resolve(_cleanUp.call(this, true));
4800
+ }
4801
+ }
4802
+
4803
+ disconnect() {
4804
+ this._dying = true;
4805
+ _cleanUp.call(this);
4806
+ }
4807
+
4808
+ send(request) {
4809
+ this.call.write(request);
4810
+ }
4811
+ }
4812
+
4810
4813
  /* eslint-disable no-underscore-dangle */
4811
4814
 
4812
4815
  const { publicKeyToPem, removeCarriageReturn, signBase64 } = js.voltUtils;
@@ -5107,7 +5110,9 @@ function connectInternal(helloPayload) {
5107
5110
 
5108
5111
  if (rpcInvocation) {
5109
5112
  if (rpcInvocation.started) {
5110
- rpcInvocation.parsePayload(invoke_request);
5113
+ rpcInvocation.parsePayload(invoke_request).catch((err) => {
5114
+ log$1("invoke_request - error [%s]", err.message);
5115
+ });
5111
5116
  } else {
5112
5117
  rpcInvocation
5113
5118
  .initialise(invoke_request)
@@ -5327,7 +5332,7 @@ async function fetchVoltConfigFromDID(
5327
5332
  }
5328
5333
  }
5329
5334
 
5330
- throw new Error("Failed to fetch Volt config from any registry");
5335
+ throw new Error(`Failed to fetch Volt config from any of these registries: ${registryList.join(", ")}`);
5331
5336
  }
5332
5337
 
5333
5338
  const { readFile } = fs__default["default"].promises;
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.7",
7
7
  "description": "tdx Volt library for nodejs clients",
8
8
  "type": "module",
9
9
  "exports": {
package/src/utils.js CHANGED
@@ -47,6 +47,9 @@ export const getDIDResolutionURL = (didIn, registryUrl = "") => {
47
47
  return "";
48
48
  }
49
49
 
50
- const lookupUrl = registryUrl || `https://${constants.defaultDIDHostName}`;
50
+ let lookupUrl = registryUrl || `https://${constants.defaultDIDHostName}`;
51
+ if (!lookupUrl.startsWith("http")) {
52
+ lookupUrl = `https://${lookupUrl}`;
53
+ }
51
54
  return `${lookupUrl}/did/${didIn}`;
52
55
  };
@@ -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)
@@ -534,5 +536,5 @@ export async function fetchVoltConfigFromDID(
534
536
  }
535
537
  }
536
538
 
537
- throw new Error("Failed to fetch Volt config from any registry");
539
+ throw new Error(`Failed to fetch Volt config from any of these registries: ${registryList.join(", ")}`);
538
540
  }