node-opcua-crypto 4.16.0 → 4.16.1

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/dist/index.js CHANGED
@@ -2209,7 +2209,7 @@ var BufferSourceConverter = class _BufferSourceConverter {
2209
2209
  }
2210
2210
  };
2211
2211
  var STRING_TYPE = "string";
2212
- var HEX_REGEX = /^[0-9a-f]+$/i;
2212
+ var HEX_REGEX = /^[0-9a-f\s]+$/i;
2213
2213
  var BASE64_REGEX = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
2214
2214
  var BASE64URL_REGEX = /^[a-zA-Z0-9-_]+$/;
2215
2215
  var Utf8Converter = class {
@@ -2657,19 +2657,19 @@ var BIT_STRING_NAME = "BIT STRING";
2657
2657
  function HexBlock(BaseClass) {
2658
2658
  var _a2;
2659
2659
  return _a2 = class Some extends BaseClass {
2660
- constructor(...args) {
2661
- var _a3;
2662
- super(...args);
2663
- const params = args[0] || {};
2664
- this.isHexOnly = (_a3 = params.isHexOnly) !== null && _a3 !== void 0 ? _a3 : false;
2665
- this.valueHexView = params.valueHex ? BufferSourceConverter.toUint8Array(params.valueHex) : EMPTY_VIEW;
2666
- }
2667
2660
  get valueHex() {
2668
2661
  return this.valueHexView.slice().buffer;
2669
2662
  }
2670
2663
  set valueHex(value) {
2671
2664
  this.valueHexView = new Uint8Array(value);
2672
2665
  }
2666
+ constructor(...args) {
2667
+ var _b;
2668
+ super(...args);
2669
+ const params = args[0] || {};
2670
+ this.isHexOnly = (_b = params.isHexOnly) !== null && _b !== void 0 ? _b : false;
2671
+ this.valueHexView = params.valueHex ? BufferSourceConverter.toUint8Array(params.valueHex) : EMPTY_VIEW;
2672
+ }
2673
2673
  fromBER(inputBuffer, inputOffset, inputLength) {
2674
2674
  const view = inputBuffer instanceof ArrayBuffer ? new Uint8Array(inputBuffer) : inputBuffer;
2675
2675
  if (!checkBufferParams(this, view, inputOffset, inputLength)) {
@@ -2704,12 +2704,6 @@ function HexBlock(BaseClass) {
2704
2704
  }, _a2.NAME = "hexBlock", _a2;
2705
2705
  }
2706
2706
  var LocalBaseBlock = class {
2707
- constructor({ blockLength = 0, error = EMPTY_STRING, warnings = [], valueBeforeDecode = EMPTY_VIEW } = {}) {
2708
- this.blockLength = blockLength;
2709
- this.error = error;
2710
- this.warnings = warnings;
2711
- this.valueBeforeDecodeView = BufferSourceConverter.toUint8Array(valueBeforeDecode);
2712
- }
2713
2707
  static blockName() {
2714
2708
  return this.NAME;
2715
2709
  }
@@ -2719,6 +2713,12 @@ var LocalBaseBlock = class {
2719
2713
  set valueBeforeDecode(value) {
2720
2714
  this.valueBeforeDecodeView = new Uint8Array(value);
2721
2715
  }
2716
+ constructor({ blockLength = 0, error = EMPTY_STRING, warnings = [], valueBeforeDecode = EMPTY_VIEW } = {}) {
2717
+ this.blockLength = blockLength;
2718
+ this.error = error;
2719
+ this.warnings = warnings;
2720
+ this.valueBeforeDecodeView = BufferSourceConverter.toUint8Array(valueBeforeDecode);
2721
+ }
2722
2722
  toJSON() {
2723
2723
  return {
2724
2724
  blockName: this.constructor.NAME,
@@ -2731,10 +2731,10 @@ var LocalBaseBlock = class {
2731
2731
  };
2732
2732
  LocalBaseBlock.NAME = "baseBlock";
2733
2733
  var ValueBlock = class extends LocalBaseBlock {
2734
- fromBER(inputBuffer, inputOffset, inputLength) {
2734
+ fromBER(_inputBuffer, _inputOffset, _inputLength) {
2735
2735
  throw TypeError("User need to make a specific function in a class which extends 'ValueBlock'");
2736
2736
  }
2737
- toBER(sizeOnly, writer) {
2737
+ toBER(_sizeOnly, _writer) {
2738
2738
  throw TypeError("User need to make a specific function in a class which extends 'ValueBlock'");
2739
2739
  }
2740
2740
  };
@@ -3075,7 +3075,9 @@ var BaseBlock = class extends LocalBaseBlock {
3075
3075
  return Convert.ToHex(this.toBER());
3076
3076
  }
3077
3077
  onAsciiEncoding() {
3078
- return `${this.constructor.NAME} : ${Convert.ToHex(this.valueBlock.valueBeforeDecodeView)}`;
3078
+ const name = this.constructor.NAME;
3079
+ const value = Convert.ToHex(this.valueBlock.valueBeforeDecodeView);
3080
+ return `${name} : ${value}`;
3079
3081
  }
3080
3082
  isEqual(other) {
3081
3083
  if (this === other) {
@@ -3091,6 +3093,7 @@ var BaseBlock = class extends LocalBaseBlock {
3091
3093
  };
3092
3094
  BaseBlock.NAME = "BaseBlock";
3093
3095
  function prepareIndefiniteForm(baseBlock) {
3096
+ var _a2;
3094
3097
  if (baseBlock instanceof typeStore.Constructed) {
3095
3098
  for (const value of baseBlock.valueBlock.value) {
3096
3099
  if (prepareIndefiniteForm(value)) {
@@ -3098,21 +3101,21 @@ function prepareIndefiniteForm(baseBlock) {
3098
3101
  }
3099
3102
  }
3100
3103
  }
3101
- return !!baseBlock.lenBlock.isIndefiniteForm;
3104
+ return !!((_a2 = baseBlock.lenBlock) === null || _a2 === void 0 ? void 0 : _a2.isIndefiniteForm);
3102
3105
  }
3103
3106
  var BaseStringBlock = class extends BaseBlock {
3104
- constructor({ value = EMPTY_STRING, ...parameters } = {}, stringValueBlockType) {
3105
- super(parameters, stringValueBlockType);
3106
- if (value) {
3107
- this.fromString(value);
3108
- }
3109
- }
3110
3107
  getValue() {
3111
3108
  return this.valueBlock.value;
3112
3109
  }
3113
3110
  setValue(value) {
3114
3111
  this.valueBlock.value = value;
3115
3112
  }
3113
+ constructor({ value = EMPTY_STRING, ...parameters } = {}, stringValueBlockType) {
3114
+ super(parameters, stringValueBlockType);
3115
+ if (value) {
3116
+ this.fromString(value);
3117
+ }
3118
+ }
3116
3119
  fromBER(inputBuffer, inputOffset, inputLength) {
3117
3120
  const resultOffset = this.valueBlock.fromBER(inputBuffer, inputOffset, this.lenBlock.isIndefiniteForm ? inputLength : this.lenBlock.length);
3118
3121
  if (resultOffset === -1) {
@@ -3471,10 +3474,10 @@ _a$v = Constructed;
3471
3474
  })();
3472
3475
  Constructed.NAME = "CONSTRUCTED";
3473
3476
  var LocalEndOfContentValueBlock = class extends ValueBlock {
3474
- fromBER(inputBuffer, inputOffset, inputLength) {
3477
+ fromBER(inputBuffer, inputOffset, _inputLength) {
3475
3478
  return inputOffset;
3476
3479
  }
3477
- toBER(sizeOnly) {
3480
+ toBER(_sizeOnly) {
3478
3481
  return EMPTY_BUFFER;
3479
3482
  }
3480
3483
  };
@@ -3535,17 +3538,6 @@ _a$t = Null;
3535
3538
  })();
3536
3539
  Null.NAME = "NULL";
3537
3540
  var LocalBooleanValueBlock = class extends HexBlock(ValueBlock) {
3538
- constructor({ value, ...parameters } = {}) {
3539
- super(parameters);
3540
- if (parameters.valueHex) {
3541
- this.valueHexView = BufferSourceConverter.toUint8Array(parameters.valueHex);
3542
- } else {
3543
- this.valueHexView = new Uint8Array(1);
3544
- }
3545
- if (value) {
3546
- this.value = value;
3547
- }
3548
- }
3549
3541
  get value() {
3550
3542
  for (const octet of this.valueHexView) {
3551
3543
  if (octet > 0) {
@@ -3557,6 +3549,17 @@ var LocalBooleanValueBlock = class extends HexBlock(ValueBlock) {
3557
3549
  set value(value) {
3558
3550
  this.valueHexView[0] = value ? 255 : 0;
3559
3551
  }
3552
+ constructor({ value, ...parameters } = {}) {
3553
+ super(parameters);
3554
+ if (parameters.valueHex) {
3555
+ this.valueHexView = BufferSourceConverter.toUint8Array(parameters.valueHex);
3556
+ } else {
3557
+ this.valueHexView = new Uint8Array(1);
3558
+ }
3559
+ if (value) {
3560
+ this.value = value;
3561
+ }
3562
+ }
3560
3563
  fromBER(inputBuffer, inputOffset, inputLength) {
3561
3564
  const inputView = BufferSourceConverter.toUint8Array(inputBuffer);
3562
3565
  if (!checkBufferParams(this, inputView, inputOffset, inputLength)) {
@@ -3583,17 +3586,17 @@ var LocalBooleanValueBlock = class extends HexBlock(ValueBlock) {
3583
3586
  LocalBooleanValueBlock.NAME = "BooleanValueBlock";
3584
3587
  var _a$s;
3585
3588
  var Boolean = class extends BaseBlock {
3586
- constructor(parameters = {}) {
3587
- super(parameters, LocalBooleanValueBlock);
3588
- this.idBlock.tagClass = 1;
3589
- this.idBlock.tagNumber = 1;
3590
- }
3591
3589
  getValue() {
3592
3590
  return this.valueBlock.value;
3593
3591
  }
3594
3592
  setValue(value) {
3595
3593
  this.valueBlock.value = value;
3596
3594
  }
3595
+ constructor(parameters = {}) {
3596
+ super(parameters, LocalBooleanValueBlock);
3597
+ this.idBlock.tagClass = 1;
3598
+ this.idBlock.tagNumber = 1;
3599
+ }
3597
3600
  onAsciiEncoding() {
3598
3601
  return `${this.constructor.NAME} : ${this.getValue}`;
3599
3602
  }
@@ -3651,7 +3654,7 @@ var LocalOctetStringValueBlock = class extends HexBlock(LocalConstructedValueBlo
3651
3654
  };
3652
3655
  LocalOctetStringValueBlock.NAME = "OctetStringValueBlock";
3653
3656
  var _a$r;
3654
- var OctetString = class _OctetString extends BaseBlock {
3657
+ var OctetString = class extends BaseBlock {
3655
3658
  constructor({ idBlock = {}, lenBlock = {}, ...parameters } = {}) {
3656
3659
  var _b, _c;
3657
3660
  (_b = parameters.isConstructed) !== null && _b !== void 0 ? _b : parameters.isConstructed = !!((_c = parameters.value) === null || _c === void 0 ? void 0 : _c.length);
@@ -3689,7 +3692,7 @@ var OctetString = class _OctetString extends BaseBlock {
3689
3692
  this.valueBlock.value = [asn.result];
3690
3693
  }
3691
3694
  }
3692
- } catch (e) {
3695
+ } catch {
3693
3696
  }
3694
3697
  }
3695
3698
  return super.fromBER(inputBuffer, inputOffset, inputLength);
@@ -3698,7 +3701,9 @@ var OctetString = class _OctetString extends BaseBlock {
3698
3701
  if (this.valueBlock.isConstructed || this.valueBlock.value && this.valueBlock.value.length) {
3699
3702
  return Constructed.prototype.onAsciiEncoding.call(this);
3700
3703
  }
3701
- return `${this.constructor.NAME} : ${Convert.ToHex(this.valueBlock.valueHexView)}`;
3704
+ const name = this.constructor.NAME;
3705
+ const value = Convert.ToHex(this.valueBlock.valueHexView);
3706
+ return `${name} : ${value}`;
3702
3707
  }
3703
3708
  getValue() {
3704
3709
  if (!this.idBlock.isConstructed) {
@@ -3706,7 +3711,7 @@ var OctetString = class _OctetString extends BaseBlock {
3706
3711
  }
3707
3712
  const array = [];
3708
3713
  for (const content of this.valueBlock.value) {
3709
- if (content instanceof _OctetString) {
3714
+ if (content instanceof _a$r) {
3710
3715
  array.push(content.valueBlock.valueHexView);
3711
3716
  }
3712
3717
  }
@@ -3776,7 +3781,7 @@ var LocalBitStringValueBlock = class extends HexBlock(LocalConstructedValueBlock
3776
3781
  this.value = [asn.result];
3777
3782
  }
3778
3783
  }
3779
- } catch (e) {
3784
+ } catch {
3780
3785
  }
3781
3786
  }
3782
3787
  this.valueHexView = intBuffer.subarray(1);
@@ -3791,7 +3796,9 @@ var LocalBitStringValueBlock = class extends HexBlock(LocalConstructedValueBlock
3791
3796
  return new ArrayBuffer(this.valueHexView.byteLength + 1);
3792
3797
  }
3793
3798
  if (!this.valueHexView.byteLength) {
3794
- return EMPTY_BUFFER;
3799
+ const empty = new Uint8Array(1);
3800
+ empty[0] = 0;
3801
+ return empty.buffer;
3795
3802
  }
3796
3803
  const retView = new Uint8Array(this.valueHexView.length + 1);
3797
3804
  retView[0] = this.unusedBits;
@@ -3841,7 +3848,9 @@ var BitString = class extends BaseBlock {
3841
3848
  bits.push(byte.toString(2).padStart(8, "0"));
3842
3849
  }
3843
3850
  const bitsStr = bits.join("");
3844
- return `${this.constructor.NAME} : ${bitsStr.substring(0, bitsStr.length - this.valueBlock.unusedBits)}`;
3851
+ const name = this.constructor.NAME;
3852
+ const value = bitsStr.substring(0, bitsStr.length - this.valueBlock.unusedBits);
3853
+ return `${name} : ${value}`;
3845
3854
  }
3846
3855
  }
3847
3856
  };
@@ -3938,16 +3947,6 @@ function viewSub(first, second) {
3938
3947
  return firstViewCopy.slice();
3939
3948
  }
3940
3949
  var LocalIntegerValueBlock = class extends HexBlock(ValueBlock) {
3941
- constructor({ value, ...parameters } = {}) {
3942
- super(parameters);
3943
- this._valueDec = 0;
3944
- if (parameters.valueHex) {
3945
- this.setValueHex();
3946
- }
3947
- if (value !== void 0) {
3948
- this.valueDec = value;
3949
- }
3950
- }
3951
3950
  setValueHex() {
3952
3951
  if (this.valueHexView.length >= 4) {
3953
3952
  this.warnings.push("Too big Integer for decoding, hex only");
@@ -3960,6 +3959,16 @@ var LocalIntegerValueBlock = class extends HexBlock(ValueBlock) {
3960
3959
  }
3961
3960
  }
3962
3961
  }
3962
+ constructor({ value, ...parameters } = {}) {
3963
+ super(parameters);
3964
+ this._valueDec = 0;
3965
+ if (parameters.valueHex) {
3966
+ this.setValueHex();
3967
+ }
3968
+ if (value !== void 0) {
3969
+ this.valueDec = value;
3970
+ }
3971
+ }
3963
3972
  set valueDec(v) {
3964
3973
  this._valueDec = v;
3965
3974
  this.isHexOnly = false;
@@ -4072,7 +4081,7 @@ LocalIntegerValueBlock.NAME = "IntegerValueBlock";
4072
4081
  });
4073
4082
  })();
4074
4083
  var _a$o;
4075
- var Integer = class _Integer extends BaseBlock {
4084
+ var Integer = class extends BaseBlock {
4076
4085
  constructor(parameters = {}) {
4077
4086
  super(parameters, LocalIntegerValueBlock);
4078
4087
  this.idBlock.tagClass = 1;
@@ -4102,18 +4111,16 @@ var Integer = class _Integer extends BaseBlock {
4102
4111
  }
4103
4112
  writer.write(view);
4104
4113
  }
4105
- const res = new _Integer({
4106
- valueHex: writer.final()
4107
- });
4114
+ const res = new _a$o({ valueHex: writer.final() });
4108
4115
  return res;
4109
4116
  }
4110
4117
  convertToDER() {
4111
- const integer = new _Integer({ valueHex: this.valueBlock.valueHexView });
4118
+ const integer = new _a$o({ valueHex: this.valueBlock.valueHexView });
4112
4119
  integer.valueBlock.toDER();
4113
4120
  return integer;
4114
4121
  }
4115
4122
  convertFromDER() {
4116
- return new _Integer({
4123
+ return new _a$o({
4117
4124
  valueHex: this.valueBlock.valueHexView[0] === 0 ? this.valueBlock.valueHexView.subarray(1) : this.valueBlock.valueHexView
4118
4125
  });
4119
4126
  }
@@ -4376,17 +4383,17 @@ var LocalObjectIdentifierValueBlock = class extends ValueBlock {
4376
4383
  LocalObjectIdentifierValueBlock.NAME = "ObjectIdentifierValueBlock";
4377
4384
  var _a$m;
4378
4385
  var ObjectIdentifier = class extends BaseBlock {
4379
- constructor(parameters = {}) {
4380
- super(parameters, LocalObjectIdentifierValueBlock);
4381
- this.idBlock.tagClass = 1;
4382
- this.idBlock.tagNumber = 6;
4383
- }
4384
4386
  getValue() {
4385
4387
  return this.valueBlock.toString();
4386
4388
  }
4387
4389
  setValue(value) {
4388
4390
  this.valueBlock.fromString(value);
4389
4391
  }
4392
+ constructor(parameters = {}) {
4393
+ super(parameters, LocalObjectIdentifierValueBlock);
4394
+ this.idBlock.tagClass = 1;
4395
+ this.idBlock.tagNumber = 6;
4396
+ }
4390
4397
  onAsciiEncoding() {
4391
4398
  return `${this.constructor.NAME} : ${this.valueBlock.toString() || "empty"}`;
4392
4399
  }
@@ -4506,7 +4513,7 @@ var LocalRelativeObjectIdentifierValueBlock = class extends ValueBlock {
4506
4513
  }
4507
4514
  return resultOffset;
4508
4515
  }
4509
- toBER(sizeOnly, writer) {
4516
+ toBER(sizeOnly, _writer) {
4510
4517
  const retBuffers = [];
4511
4518
  for (let i = 0; i < this.value.length; i++) {
4512
4519
  const valueBuf = this.value[i].toBER(sizeOnly);
@@ -4568,17 +4575,17 @@ var LocalRelativeObjectIdentifierValueBlock = class extends ValueBlock {
4568
4575
  LocalRelativeObjectIdentifierValueBlock.NAME = "RelativeObjectIdentifierValueBlock";
4569
4576
  var _a$l;
4570
4577
  var RelativeObjectIdentifier = class extends BaseBlock {
4571
- constructor(parameters = {}) {
4572
- super(parameters, LocalRelativeObjectIdentifierValueBlock);
4573
- this.idBlock.tagClass = 1;
4574
- this.idBlock.tagNumber = 13;
4575
- }
4576
4578
  getValue() {
4577
4579
  return this.valueBlock.toString();
4578
4580
  }
4579
4581
  setValue(value) {
4580
4582
  this.valueBlock.fromString(value);
4581
4583
  }
4584
+ constructor(parameters = {}) {
4585
+ super(parameters, LocalRelativeObjectIdentifierValueBlock);
4586
+ this.idBlock.tagClass = 1;
4587
+ this.idBlock.tagNumber = 13;
4588
+ }
4582
4589
  onAsciiEncoding() {
4583
4590
  return `${this.constructor.NAME} : ${this.valueBlock.toString() || "empty"}`;
4584
4591
  }
@@ -4975,7 +4982,8 @@ var GeneralizedTime = class extends UTCTime {
4975
4982
  this.millisecond = inputDate.getUTCMilliseconds();
4976
4983
  }
4977
4984
  toDate() {
4978
- return new Date(Date.UTC(this.year, this.month - 1, this.day, this.hour, this.minute, this.second, this.millisecond));
4985
+ const utcDate = Date.UTC(this.year, this.month - 1, this.day, this.hour, this.minute, this.second, this.millisecond);
4986
+ return new Date(utcDate);
4979
4987
  }
4980
4988
  fromString(inputString) {
4981
4989
  let isUTC = false;
@@ -5224,28 +5232,28 @@ var Repeated = class extends Any {
5224
5232
  }
5225
5233
  };
5226
5234
  var RawData = class {
5227
- constructor({ data = EMPTY_VIEW } = {}) {
5228
- this.dataView = BufferSourceConverter.toUint8Array(data);
5229
- }
5230
5235
  get data() {
5231
5236
  return this.dataView.slice().buffer;
5232
5237
  }
5233
5238
  set data(value) {
5234
5239
  this.dataView = BufferSourceConverter.toUint8Array(value);
5235
5240
  }
5241
+ constructor({ data = EMPTY_VIEW } = {}) {
5242
+ this.dataView = BufferSourceConverter.toUint8Array(data);
5243
+ }
5236
5244
  fromBER(inputBuffer, inputOffset, inputLength) {
5237
5245
  const endLength = inputOffset + inputLength;
5238
5246
  this.dataView = BufferSourceConverter.toUint8Array(inputBuffer).subarray(inputOffset, endLength);
5239
5247
  return endLength;
5240
5248
  }
5241
- toBER(sizeOnly) {
5249
+ toBER(_sizeOnly) {
5242
5250
  return this.dataView.slice().buffer;
5243
5251
  }
5244
5252
  };
5245
5253
  function compareSchema(root, inputData, inputSchema) {
5246
5254
  if (inputSchema instanceof Choice) {
5247
- for (let j = 0; j < inputSchema.value.length; j++) {
5248
- const result = compareSchema(root, inputData, inputSchema.value[j]);
5255
+ for (const element of inputSchema.value) {
5256
+ const result = compareSchema(root, inputData, element);
5249
5257
  if (result.verified) {
5250
5258
  return {
5251
5259
  verified: true,
@@ -5256,9 +5264,7 @@ function compareSchema(root, inputData, inputSchema) {
5256
5264
  {
5257
5265
  const _result = {
5258
5266
  verified: false,
5259
- result: {
5260
- error: "Wrong values for Choice type"
5261
- }
5267
+ result: { error: "Wrong values for Choice type" }
5262
5268
  };
5263
5269
  if (inputSchema.hasOwnProperty(NAME))
5264
5270
  _result.name = inputSchema.name;
@@ -5404,9 +5410,7 @@ function compareSchema(root, inputData, inputSchema) {
5404
5410
  let admission = 0;
5405
5411
  let result = {
5406
5412
  verified: false,
5407
- result: {
5408
- error: "Unknown error"
5409
- }
5413
+ result: { error: "Unknown error" }
5410
5414
  };
5411
5415
  let maxLength = inputSchema.valueBlock.value.length;
5412
5416
  if (maxLength > 0) {
@@ -5783,10 +5787,7 @@ var AsnSchemaStorage = class {
5783
5787
  }
5784
5788
  }
5785
5789
  createDefault(target) {
5786
- const schema = {
5787
- type: AsnTypeTypes.Sequence,
5788
- items: {}
5789
- };
5790
+ const schema = { type: AsnTypeTypes.Sequence, items: {} };
5790
5791
  const parentSchema = this.findParentSchema(target);
5791
5792
  if (parentSchema) {
5792
5793
  Object.assign(schema, parentSchema);
@@ -5828,26 +5829,14 @@ var AsnSchemaStorage = class {
5828
5829
  const Container = item.repeated === "set" ? Set : Sequence;
5829
5830
  asn1Item = new Container({
5830
5831
  name: "",
5831
- value: [
5832
- new Repeated({
5833
- name,
5834
- value: asn1Item
5835
- })
5836
- ]
5832
+ value: [new Repeated({ name, value: asn1Item })]
5837
5833
  });
5838
5834
  }
5839
5835
  if (item.context !== null && item.context !== void 0) {
5840
5836
  if (item.implicit) {
5841
5837
  if (typeof item.type === "number" || isConvertible(item.type)) {
5842
5838
  const Container = item.repeated ? Constructed : Primitive;
5843
- asn1Value.push(new Container({
5844
- name,
5845
- optional,
5846
- idBlock: {
5847
- tagClass: 3,
5848
- tagNumber: item.context
5849
- }
5850
- }));
5839
+ asn1Value.push(new Container({ name, optional, idBlock: { tagClass: 3, tagNumber: item.context } }));
5851
5840
  } else {
5852
5841
  this.cache(item.type);
5853
5842
  const isRepeated = !!item.repeated;
@@ -5856,20 +5845,14 @@ var AsnSchemaStorage = class {
5856
5845
  asn1Value.push(new Constructed({
5857
5846
  name: !isRepeated ? name : "",
5858
5847
  optional,
5859
- idBlock: {
5860
- tagClass: 3,
5861
- tagNumber: item.context
5862
- },
5848
+ idBlock: { tagClass: 3, tagNumber: item.context },
5863
5849
  value
5864
5850
  }));
5865
5851
  }
5866
5852
  } else {
5867
5853
  asn1Value.push(new Constructed({
5868
5854
  optional,
5869
- idBlock: {
5870
- tagClass: 3,
5871
- tagNumber: item.context
5872
- },
5855
+ idBlock: { tagClass: 3, tagNumber: item.context },
5873
5856
  value: [asn1Item]
5874
5857
  }));
5875
5858
  }
@@ -5925,7 +5908,6 @@ var AsnParser = class {
5925
5908
  return res;
5926
5909
  }
5927
5910
  static fromASN(asn1Schema, target) {
5928
- var _a2;
5929
5911
  try {
5930
5912
  if (isConvertible(target)) {
5931
5913
  const value = new target();
@@ -5934,104 +5916,259 @@ var AsnParser = class {
5934
5916
  const schema = schemaStorage.get(target);
5935
5917
  schemaStorage.cache(target);
5936
5918
  let targetSchema = schema.schema;
5937
- if (asn1Schema.constructor === Constructed && schema.type !== AsnTypeTypes.Choice) {
5938
- targetSchema = new Constructed({
5939
- idBlock: {
5940
- tagClass: 3,
5941
- tagNumber: asn1Schema.idBlock.tagNumber
5942
- },
5943
- value: schema.schema.valueBlock.value
5944
- });
5945
- for (const key in schema.items) {
5946
- delete asn1Schema[key];
5947
- }
5919
+ const choiceResult = this.handleChoiceTypes(asn1Schema, schema, target, targetSchema);
5920
+ if (choiceResult === null || choiceResult === void 0 ? void 0 : choiceResult.result) {
5921
+ return choiceResult.result;
5948
5922
  }
5949
- const asn1ComparedSchema = compareSchema({}, asn1Schema, targetSchema);
5950
- if (!asn1ComparedSchema.verified) {
5951
- throw new AsnSchemaValidationError(`Data does not match to ${target.name} ASN1 schema. ${asn1ComparedSchema.result.error}`);
5923
+ if (choiceResult === null || choiceResult === void 0 ? void 0 : choiceResult.targetSchema) {
5924
+ targetSchema = choiceResult.targetSchema;
5952
5925
  }
5926
+ const sequenceResult = this.handleSequenceTypes(asn1Schema, schema, target, targetSchema);
5953
5927
  const res = new target();
5954
5928
  if (isTypeOfArray(target)) {
5955
- if (!("value" in asn1Schema.valueBlock && Array.isArray(asn1Schema.valueBlock.value))) {
5956
- throw new Error(`Cannot get items from the ASN.1 parsed value. ASN.1 object is not constructed.`);
5957
- }
5958
- const itemType = schema.itemType;
5959
- if (typeof itemType === "number") {
5960
- const converter = defaultConverter(itemType);
5961
- if (!converter) {
5962
- throw new Error(`Cannot get default converter for array item of ${target.name} ASN1 schema`);
5963
- }
5964
- return target.from(asn1Schema.valueBlock.value, (element) => converter.fromASN(element));
5965
- } else {
5966
- return target.from(asn1Schema.valueBlock.value, (element) => this.fromASN(element, itemType));
5967
- }
5929
+ return this.handleArrayTypes(asn1Schema, schema, target);
5968
5930
  }
5931
+ this.processSchemaItems(schema, sequenceResult, res);
5932
+ return res;
5933
+ } catch (error) {
5934
+ if (error instanceof AsnSchemaValidationError) {
5935
+ error.schemas.push(target.name);
5936
+ }
5937
+ throw error;
5938
+ }
5939
+ }
5940
+ static handleChoiceTypes(asn1Schema, schema, target, targetSchema) {
5941
+ if (asn1Schema.constructor === Constructed && schema.type === AsnTypeTypes.Choice && asn1Schema.idBlock.tagClass === 3) {
5969
5942
  for (const key in schema.items) {
5970
- const asn1SchemaValue = asn1ComparedSchema.result[key];
5971
- if (!asn1SchemaValue) {
5972
- continue;
5973
- }
5974
5943
  const schemaItem = schema.items[key];
5975
- const schemaItemType = schemaItem.type;
5976
- if (typeof schemaItemType === "number" || isConvertible(schemaItemType)) {
5977
- const converter = (_a2 = schemaItem.converter) !== null && _a2 !== void 0 ? _a2 : isConvertible(schemaItemType) ? new schemaItemType() : null;
5978
- if (!converter) {
5979
- throw new Error("Converter is empty");
5980
- }
5981
- if (schemaItem.repeated) {
5982
- if (schemaItem.implicit) {
5983
- const Container = schemaItem.repeated === "sequence" ? Sequence : Set;
5984
- const newItem = new Container();
5985
- newItem.valueBlock = asn1SchemaValue.valueBlock;
5986
- const newItemAsn = fromBER(newItem.toBER(false));
5987
- if (newItemAsn.offset === -1) {
5988
- throw new Error(`Cannot parse the child item. ${newItemAsn.result.error}`);
5989
- }
5990
- if (!("value" in newItemAsn.result.valueBlock && Array.isArray(newItemAsn.result.valueBlock.value))) {
5991
- throw new Error("Cannot get items from the ASN.1 parsed value. ASN.1 object is not constructed.");
5992
- }
5993
- const value = newItemAsn.result.valueBlock.value;
5994
- res[key] = Array.from(value, (element) => converter.fromASN(element));
5995
- } else {
5996
- res[key] = Array.from(asn1SchemaValue, (element) => converter.fromASN(element));
5997
- }
5998
- } else {
5999
- let value = asn1SchemaValue;
6000
- if (schemaItem.implicit) {
6001
- let newItem;
6002
- if (isConvertible(schemaItemType)) {
6003
- newItem = new schemaItemType().toSchema("");
6004
- } else {
6005
- const Asn1TypeName = AsnPropTypes[schemaItemType];
6006
- const Asn1Type = index_es_exports[Asn1TypeName];
6007
- if (!Asn1Type) {
6008
- throw new Error(`Cannot get '${Asn1TypeName}' class from asn1js module`);
6009
- }
6010
- newItem = new Asn1Type();
5944
+ if (schemaItem.context === asn1Schema.idBlock.tagNumber && schemaItem.implicit) {
5945
+ if (typeof schemaItem.type === "function" && schemaStorage.has(schemaItem.type)) {
5946
+ const fieldSchema = schemaStorage.get(schemaItem.type);
5947
+ if (fieldSchema && fieldSchema.type === AsnTypeTypes.Sequence) {
5948
+ const newSeq = new Sequence();
5949
+ if ("value" in asn1Schema.valueBlock && Array.isArray(asn1Schema.valueBlock.value) && "value" in newSeq.valueBlock) {
5950
+ newSeq.valueBlock.value = asn1Schema.valueBlock.value;
5951
+ const fieldValue = this.fromASN(newSeq, schemaItem.type);
5952
+ const res = new target();
5953
+ res[key] = fieldValue;
5954
+ return { result: res };
6011
5955
  }
6012
- newItem.valueBlock = value.valueBlock;
6013
- value = fromBER(newItem.toBER(false)).result;
6014
5956
  }
6015
- res[key] = converter.fromASN(value);
6016
5957
  }
6017
- } else {
6018
- if (schemaItem.repeated) {
6019
- if (!Array.isArray(asn1SchemaValue)) {
6020
- throw new Error("Cannot get list of items from the ASN.1 parsed value. ASN.1 value should be iterable.");
6021
- }
6022
- res[key] = Array.from(asn1SchemaValue, (element) => this.fromASN(element, schemaItemType));
6023
- } else {
6024
- res[key] = this.fromASN(asn1SchemaValue, schemaItemType);
5958
+ }
5959
+ }
5960
+ } else if (asn1Schema.constructor === Constructed && schema.type !== AsnTypeTypes.Choice) {
5961
+ const newTargetSchema = new Constructed({
5962
+ idBlock: {
5963
+ tagClass: 3,
5964
+ tagNumber: asn1Schema.idBlock.tagNumber
5965
+ },
5966
+ value: schema.schema.valueBlock.value
5967
+ });
5968
+ for (const key in schema.items) {
5969
+ delete asn1Schema[key];
5970
+ }
5971
+ return { targetSchema: newTargetSchema };
5972
+ }
5973
+ return null;
5974
+ }
5975
+ static handleSequenceTypes(asn1Schema, schema, target, targetSchema) {
5976
+ if (schema.type === AsnTypeTypes.Sequence) {
5977
+ const asn1ComparedSchema = compareSchema({}, asn1Schema, targetSchema);
5978
+ if (!asn1ComparedSchema.verified) {
5979
+ throw new AsnSchemaValidationError(`Data does not match to ${target.name} ASN1 schema.${asn1ComparedSchema.result.error ? ` ${asn1ComparedSchema.result.error}` : ""}`);
5980
+ }
5981
+ return asn1ComparedSchema;
5982
+ } else {
5983
+ const asn1ComparedSchema = compareSchema({}, asn1Schema, targetSchema);
5984
+ if (!asn1ComparedSchema.verified) {
5985
+ throw new AsnSchemaValidationError(`Data does not match to ${target.name} ASN1 schema.${asn1ComparedSchema.result.error ? ` ${asn1ComparedSchema.result.error}` : ""}`);
5986
+ }
5987
+ return asn1ComparedSchema;
5988
+ }
5989
+ }
5990
+ static processRepeatedField(asn1Elements, asn1Index, schemaItem) {
5991
+ let elementsToProcess = asn1Elements.slice(asn1Index);
5992
+ if (elementsToProcess.length === 1 && elementsToProcess[0].constructor.name === "Sequence") {
5993
+ const seq = elementsToProcess[0];
5994
+ if (seq.valueBlock && seq.valueBlock.value && Array.isArray(seq.valueBlock.value)) {
5995
+ elementsToProcess = seq.valueBlock.value;
5996
+ }
5997
+ }
5998
+ if (typeof schemaItem.type === "number") {
5999
+ const converter = defaultConverter(schemaItem.type);
6000
+ if (!converter)
6001
+ throw new Error(`No converter for ASN.1 type ${schemaItem.type}`);
6002
+ return elementsToProcess.filter((el) => el && el.valueBlock).map((el) => {
6003
+ try {
6004
+ return converter.fromASN(el);
6005
+ } catch {
6006
+ return void 0;
6007
+ }
6008
+ }).filter((v) => v !== void 0);
6009
+ } else {
6010
+ return elementsToProcess.filter((el) => el && el.valueBlock).map((el) => {
6011
+ try {
6012
+ return this.fromASN(el, schemaItem.type);
6013
+ } catch {
6014
+ return void 0;
6015
+ }
6016
+ }).filter((v) => v !== void 0);
6017
+ }
6018
+ }
6019
+ static processPrimitiveField(asn1Element, schemaItem) {
6020
+ const converter = defaultConverter(schemaItem.type);
6021
+ if (!converter)
6022
+ throw new Error(`No converter for ASN.1 type ${schemaItem.type}`);
6023
+ return converter.fromASN(asn1Element);
6024
+ }
6025
+ static isOptionalChoiceField(schemaItem) {
6026
+ return schemaItem.optional && typeof schemaItem.type === "function" && schemaStorage.has(schemaItem.type) && schemaStorage.get(schemaItem.type).type === AsnTypeTypes.Choice;
6027
+ }
6028
+ static processOptionalChoiceField(asn1Element, schemaItem) {
6029
+ try {
6030
+ const value = this.fromASN(asn1Element, schemaItem.type);
6031
+ return { processed: true, value };
6032
+ } catch (err) {
6033
+ if (err instanceof AsnSchemaValidationError && /Wrong values for Choice type/.test(err.message)) {
6034
+ return { processed: false };
6035
+ }
6036
+ throw err;
6037
+ }
6038
+ }
6039
+ static handleArrayTypes(asn1Schema, schema, target) {
6040
+ if (!("value" in asn1Schema.valueBlock && Array.isArray(asn1Schema.valueBlock.value))) {
6041
+ throw new Error(`Cannot get items from the ASN.1 parsed value. ASN.1 object is not constructed.`);
6042
+ }
6043
+ const itemType = schema.itemType;
6044
+ if (typeof itemType === "number") {
6045
+ const converter = defaultConverter(itemType);
6046
+ if (!converter) {
6047
+ throw new Error(`Cannot get default converter for array item of ${target.name} ASN1 schema`);
6048
+ }
6049
+ return target.from(asn1Schema.valueBlock.value, (element) => converter.fromASN(element));
6050
+ } else {
6051
+ return target.from(asn1Schema.valueBlock.value, (element) => this.fromASN(element, itemType));
6052
+ }
6053
+ }
6054
+ static processSchemaItems(schema, asn1ComparedSchema, res) {
6055
+ for (const key in schema.items) {
6056
+ const asn1SchemaValue = asn1ComparedSchema.result[key];
6057
+ if (!asn1SchemaValue) {
6058
+ continue;
6059
+ }
6060
+ const schemaItem = schema.items[key];
6061
+ const schemaItemType = schemaItem.type;
6062
+ let parsedValue;
6063
+ if (typeof schemaItemType === "number" || isConvertible(schemaItemType)) {
6064
+ parsedValue = this.processPrimitiveSchemaItem(asn1SchemaValue, schemaItem, schemaItemType);
6065
+ } else {
6066
+ parsedValue = this.processComplexSchemaItem(asn1SchemaValue, schemaItem, schemaItemType);
6067
+ }
6068
+ if (parsedValue && typeof parsedValue === "object" && "value" in parsedValue && "raw" in parsedValue) {
6069
+ res[key] = parsedValue.value;
6070
+ res[`${key}Raw`] = parsedValue.raw;
6071
+ } else {
6072
+ res[key] = parsedValue;
6073
+ }
6074
+ }
6075
+ }
6076
+ static processPrimitiveSchemaItem(asn1SchemaValue, schemaItem, schemaItemType) {
6077
+ var _a2;
6078
+ const converter = (_a2 = schemaItem.converter) !== null && _a2 !== void 0 ? _a2 : isConvertible(schemaItemType) ? new schemaItemType() : null;
6079
+ if (!converter) {
6080
+ throw new Error("Converter is empty");
6081
+ }
6082
+ if (schemaItem.repeated) {
6083
+ return this.processRepeatedPrimitiveItem(asn1SchemaValue, schemaItem, converter);
6084
+ } else {
6085
+ return this.processSinglePrimitiveItem(asn1SchemaValue, schemaItem, schemaItemType, converter);
6086
+ }
6087
+ }
6088
+ static processRepeatedPrimitiveItem(asn1SchemaValue, schemaItem, converter) {
6089
+ if (schemaItem.implicit) {
6090
+ const Container = schemaItem.repeated === "sequence" ? Sequence : Set;
6091
+ const newItem = new Container();
6092
+ newItem.valueBlock = asn1SchemaValue.valueBlock;
6093
+ const newItemAsn = fromBER(newItem.toBER(false));
6094
+ if (newItemAsn.offset === -1) {
6095
+ throw new Error(`Cannot parse the child item. ${newItemAsn.result.error}`);
6096
+ }
6097
+ if (!("value" in newItemAsn.result.valueBlock && Array.isArray(newItemAsn.result.valueBlock.value))) {
6098
+ throw new Error("Cannot get items from the ASN.1 parsed value. ASN.1 object is not constructed.");
6099
+ }
6100
+ const value = newItemAsn.result.valueBlock.value;
6101
+ return Array.from(value, (element) => converter.fromASN(element));
6102
+ } else {
6103
+ return Array.from(asn1SchemaValue, (element) => converter.fromASN(element));
6104
+ }
6105
+ }
6106
+ static processSinglePrimitiveItem(asn1SchemaValue, schemaItem, schemaItemType, converter) {
6107
+ let value = asn1SchemaValue;
6108
+ if (schemaItem.implicit) {
6109
+ let newItem;
6110
+ if (isConvertible(schemaItemType)) {
6111
+ newItem = new schemaItemType().toSchema("");
6112
+ } else {
6113
+ const Asn1TypeName = AsnPropTypes[schemaItemType];
6114
+ const Asn1Type = index_es_exports[Asn1TypeName];
6115
+ if (!Asn1Type) {
6116
+ throw new Error(`Cannot get '${Asn1TypeName}' class from asn1js module`);
6117
+ }
6118
+ newItem = new Asn1Type();
6119
+ }
6120
+ newItem.valueBlock = value.valueBlock;
6121
+ value = fromBER(newItem.toBER(false)).result;
6122
+ }
6123
+ return converter.fromASN(value);
6124
+ }
6125
+ static processComplexSchemaItem(asn1SchemaValue, schemaItem, schemaItemType) {
6126
+ if (schemaItem.repeated) {
6127
+ if (!Array.isArray(asn1SchemaValue)) {
6128
+ throw new Error("Cannot get list of items from the ASN.1 parsed value. ASN.1 value should be iterable.");
6129
+ }
6130
+ return Array.from(asn1SchemaValue, (element) => this.fromASN(element, schemaItemType));
6131
+ } else {
6132
+ const valueToProcess = this.handleImplicitTagging(asn1SchemaValue, schemaItem, schemaItemType);
6133
+ if (this.isOptionalChoiceField(schemaItem)) {
6134
+ try {
6135
+ return this.fromASN(valueToProcess, schemaItemType);
6136
+ } catch (err) {
6137
+ if (err instanceof AsnSchemaValidationError && /Wrong values for Choice type/.test(err.message)) {
6138
+ return void 0;
6025
6139
  }
6140
+ throw err;
6026
6141
  }
6142
+ } else {
6143
+ const parsedValue = this.fromASN(valueToProcess, schemaItemType);
6144
+ if (schemaItem.raw) {
6145
+ return {
6146
+ value: parsedValue,
6147
+ raw: asn1SchemaValue.valueBeforeDecodeView
6148
+ };
6149
+ }
6150
+ return parsedValue;
6027
6151
  }
6028
- return res;
6029
- } catch (error) {
6030
- if (error instanceof AsnSchemaValidationError) {
6031
- error.schemas.push(target.name);
6152
+ }
6153
+ }
6154
+ static handleImplicitTagging(asn1SchemaValue, schemaItem, schemaItemType) {
6155
+ if (schemaItem.implicit && typeof schemaItem.context === "number") {
6156
+ const schema = schemaStorage.get(schemaItemType);
6157
+ if (schema.type === AsnTypeTypes.Sequence) {
6158
+ const newSeq = new Sequence();
6159
+ if ("value" in asn1SchemaValue.valueBlock && Array.isArray(asn1SchemaValue.valueBlock.value) && "value" in newSeq.valueBlock) {
6160
+ newSeq.valueBlock.value = asn1SchemaValue.valueBlock.value;
6161
+ return newSeq;
6162
+ }
6163
+ } else if (schema.type === AsnTypeTypes.Set) {
6164
+ const newSet = new Set();
6165
+ if ("value" in asn1SchemaValue.valueBlock && Array.isArray(asn1SchemaValue.valueBlock.value) && "value" in newSet.valueBlock) {
6166
+ newSet.valueBlock.value = asn1SchemaValue.valueBlock.value;
6167
+ return newSet;
6168
+ }
6032
6169
  }
6033
- throw error;
6034
6170
  }
6171
+ return asn1SchemaValue;
6035
6172
  }
6036
6173
  };
6037
6174
 
@@ -6481,7 +6618,7 @@ pvtsutils/build/index.es.js:
6481
6618
  (*!
6482
6619
  * MIT License
6483
6620
  *
6484
- * Copyright (c) 2017-2022 Peculiar Ventures, LLC
6621
+ * Copyright (c) 2017-2024 Peculiar Ventures, LLC
6485
6622
  *
6486
6623
  * Permission is hereby granted, free of charge, to any person obtaining a copy
6487
6624
  * of this software and associated documentation files (the "Software"), to deal