Greetings,
Here I am proposing below JavaScript API design for generating ASN.1 data structures.
JS Class definition will be dynamically provided based on supplied ASN.X data modules.
Consider below couple of examples:
1.. Supplied below ASN.1 Data Module for ticket.
<asnx:module>
<namedType name="ticket">
<type>
<sequence>
<element name="numero" type="asn:Integer"/>
<element name="class">
<type><enumerated>
<enumeration name="normal" number="0"/>
<enumeration name="gift" number="1"/>
<enumeration name="VIP" number="2"/>
</enumerated></type>
</element>
<optional>
<element name="end" type="asn:UTCTime"/>
</optional>
</sequence>
</type>
</namedType>
</asn:module>
This will dynamically generate the below JS API calls.
There can be two options
a. Generate by calling constructor
let ticket1 = new Ticket(20, {'normal':20, 'gift': 21, 'vip':22}, '20200629');
ticket1.setSequence();
let sequence_buffer = ticket1.encode();
b. Generate by calling class methods
let ticket2 = new Ticket();
console.log(ticket2.isTicketFinalised()); //returns false
ticket2.setNumero(21);
ticket2.setStartTime(new Date());
ticket2.setTicketClass({'normal':30, 'gift': 31, 'vip':32});
console.log(ticket2.isTicketFinalised()); //returns true
let ticket2_sequence_buffer = ticket2.encode();
As you can see here the endTime is optional, So the object can be encoded even if user does not set the optional elements and isTicketFinalised() will return true once we set all the required elements.
2. Supplied below ASN.1 Data Module for Time.
<namedType name="Time">
<type>
<choice>
<element name="utcTime" type="asnx:UTCTime"/>
<element name="generalizedTime" type="asnx:GeneralizedTime"/>
</choice>
</type>
</namedType>
This will dynamically generate the below JS API calls. Again there will two options as indicated above. Here I am mentioning the constructor one.
let time = new Time('20200704', '20200703');
time. setTimeChoice();
let time_sequence_buffer = time.encode();
console.log("time_sequence_buffer:"+time_sequence_buffer);
Likewise to start with we can design the JS API library for the fixed Data Module and dynamic generation of API calls for the application of the provided library.