serializer.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AsnSerializer = void 0;
  4. const tslib_1 = require("tslib");
  5. const asn1js = tslib_1.__importStar(require("asn1js"));
  6. const utils_1 = require("@peculiar/utils");
  7. const converters = tslib_1.__importStar(require("./converters"));
  8. const enums_1 = require("./enums");
  9. const helper_1 = require("./helper");
  10. const storage_1 = require("./storage");
  11. class AsnSerializer {
  12. static serialize(obj) {
  13. if (obj instanceof asn1js.BaseBlock) {
  14. return obj.toBER(false);
  15. }
  16. return this.toASN(obj).toBER(false);
  17. }
  18. static toASN(obj) {
  19. if (obj && typeof obj === "object" && (0, helper_1.isConvertible)(obj)) {
  20. return obj.toASN();
  21. }
  22. if (!(obj && typeof obj === "object")) {
  23. throw new TypeError("Parameter 1 should be type of Object.");
  24. }
  25. const target = obj.constructor;
  26. const schema = storage_1.schemaStorage.get(target);
  27. storage_1.schemaStorage.cache(target);
  28. let asn1Value = [];
  29. if (schema.itemType) {
  30. if (!Array.isArray(obj)) {
  31. throw new TypeError("Parameter 1 should be type of Array.");
  32. }
  33. if (typeof schema.itemType === "number") {
  34. const converter = converters.defaultConverter(schema.itemType);
  35. if (!converter) {
  36. throw new Error(`Cannot get default converter for array item of ${target.name} ASN1 schema`);
  37. }
  38. asn1Value = obj.map((o) => converter.toASN(o));
  39. }
  40. else {
  41. asn1Value = obj.map((o) => this.toAsnItem({ type: schema.itemType }, "[]", target, o));
  42. }
  43. }
  44. else {
  45. for (const key in schema.items) {
  46. const schemaItem = schema.items[key];
  47. const objProp = obj[key];
  48. if (objProp === undefined ||
  49. schemaItem.defaultValue === objProp ||
  50. (typeof schemaItem.defaultValue === "object" && typeof objProp === "object" && (0, helper_1.isArrayEqual)(this.serialize(schemaItem.defaultValue), this.serialize(objProp)))) {
  51. continue;
  52. }
  53. const asn1Item = AsnSerializer.toAsnItem(schemaItem, key, target, objProp);
  54. if (typeof schemaItem.context === "number") {
  55. if (schemaItem.implicit) {
  56. if (!schemaItem.repeated && (typeof schemaItem.type === "number" || (0, helper_1.isConvertible)(schemaItem.type))) {
  57. const value = {};
  58. value.valueHex = asn1Item instanceof asn1js.Null ? (0, utils_1.toArrayBuffer)(asn1Item.valueBeforeDecodeView) : asn1Item.valueBlock.toBER();
  59. asn1Value.push(new asn1js.Primitive({
  60. optional: schemaItem.optional,
  61. idBlock: {
  62. tagClass: 3,
  63. tagNumber: schemaItem.context,
  64. },
  65. ...value,
  66. }));
  67. }
  68. else {
  69. asn1Value.push(new asn1js.Constructed({
  70. optional: schemaItem.optional,
  71. idBlock: {
  72. tagClass: 3,
  73. tagNumber: schemaItem.context,
  74. },
  75. value: asn1Item.valueBlock.value,
  76. }));
  77. }
  78. }
  79. else {
  80. asn1Value.push(new asn1js.Constructed({
  81. optional: schemaItem.optional,
  82. idBlock: {
  83. tagClass: 3,
  84. tagNumber: schemaItem.context,
  85. },
  86. value: [asn1Item],
  87. }));
  88. }
  89. }
  90. else if (schemaItem.repeated) {
  91. asn1Value = asn1Value.concat(asn1Item);
  92. }
  93. else {
  94. asn1Value.push(asn1Item);
  95. }
  96. }
  97. }
  98. let asnSchema;
  99. switch (schema.type) {
  100. case enums_1.AsnTypeTypes.Sequence:
  101. asnSchema = new asn1js.Sequence({ value: asn1Value });
  102. break;
  103. case enums_1.AsnTypeTypes.Set:
  104. asnSchema = new asn1js.Set({ value: asn1Value });
  105. break;
  106. case enums_1.AsnTypeTypes.Choice:
  107. if (!asn1Value[0]) {
  108. throw new Error(`Schema '${target.name}' has wrong data. Choice cannot be empty.`);
  109. }
  110. asnSchema = asn1Value[0];
  111. break;
  112. }
  113. return asnSchema;
  114. }
  115. static toAsnItem(schemaItem, key, target, objProp) {
  116. let asn1Item;
  117. if (typeof schemaItem.type === "number") {
  118. const converter = schemaItem.converter;
  119. if (!converter) {
  120. throw new Error(`Property '${key}' doesn't have converter for type ${enums_1.AsnPropTypes[schemaItem.type]} in schema '${target.name}'`);
  121. }
  122. if (schemaItem.repeated) {
  123. if (!Array.isArray(objProp)) {
  124. throw new TypeError("Parameter 'objProp' should be type of Array.");
  125. }
  126. const items = Array.from(objProp, (element) => converter.toASN(element));
  127. const Container = schemaItem.repeated === "sequence" ? asn1js.Sequence : asn1js.Set;
  128. asn1Item = new Container({ value: items });
  129. }
  130. else {
  131. asn1Item = converter.toASN(objProp);
  132. }
  133. }
  134. else {
  135. if (schemaItem.repeated) {
  136. if (!Array.isArray(objProp)) {
  137. throw new TypeError("Parameter 'objProp' should be type of Array.");
  138. }
  139. const items = Array.from(objProp, (element) => this.toASN(element));
  140. const Container = schemaItem.repeated === "sequence" ? asn1js.Sequence : asn1js.Set;
  141. asn1Item = new Container({ value: items });
  142. }
  143. else {
  144. asn1Item = this.toASN(objProp);
  145. }
  146. }
  147. return asn1Item;
  148. }
  149. }
  150. exports.AsnSerializer = AsnSerializer;