Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80622 views
1
var asn1 = require('../asn1');
2
var inherits = require('inherits');
3
4
var api = exports;
5
6
api.define = function define(name, body) {
7
return new Entity(name, body);
8
};
9
10
function Entity(name, body) {
11
this.name = name;
12
this.body = body;
13
14
this.decoders = {};
15
this.encoders = {};
16
};
17
18
Entity.prototype._createNamed = function createNamed(base) {
19
var named;
20
try {
21
named = require('vm').runInThisContext(
22
'(function ' + this.name + '(entity) {\n' +
23
' this._initNamed(entity);\n' +
24
'})'
25
);
26
} catch (e) {
27
named = function (entity) {
28
this._initNamed(entity);
29
};
30
}
31
inherits(named, base);
32
named.prototype._initNamed = function initnamed(entity) {
33
base.call(this, entity);
34
};
35
36
return new named(this);
37
};
38
39
Entity.prototype._getDecoder = function _getDecoder(enc) {
40
// Lazily create decoder
41
if (!this.decoders.hasOwnProperty(enc))
42
this.decoders[enc] = this._createNamed(asn1.decoders[enc]);
43
return this.decoders[enc];
44
};
45
46
Entity.prototype.decode = function decode(data, enc, options) {
47
return this._getDecoder(enc).decode(data, options);
48
};
49
50
Entity.prototype._getEncoder = function _getEncoder(enc) {
51
// Lazily create encoder
52
if (!this.encoders.hasOwnProperty(enc))
53
this.encoders[enc] = this._createNamed(asn1.encoders[enc]);
54
return this.encoders[enc];
55
};
56
57
Entity.prototype.encode = function encode(data, enc, /* internal */ reporter) {
58
return this._getEncoder(enc).encode(data, reporter);
59
};
60
61