You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.5 KiB
47 lines
1.5 KiB
2 years ago
|
import * as encoders from "./encoders";
|
||
|
import * as decoders from "./decoders";
|
||
|
import { inherits as inherits$0 } from "util";
|
||
|
'use strict';
|
||
|
const inherits = { inherits: inherits$0 }.inherits;
|
||
|
const api = exports;
|
||
|
api.define = function define(name, body) {
|
||
|
return new Entity(name, body);
|
||
|
};
|
||
|
function Entity(name, body) {
|
||
|
this.name = name;
|
||
|
this.body = body;
|
||
|
this.decoders = {};
|
||
|
this.encoders = {};
|
||
|
}
|
||
|
Entity.prototype._createNamed = function createNamed(Base) {
|
||
|
const name = this.name;
|
||
|
function Generated(entity) {
|
||
|
this._initNamed(entity, name);
|
||
|
}
|
||
|
inherits(Generated, Base);
|
||
|
Generated.prototype._initNamed = function _initNamed(entity, name) {
|
||
|
Base.call(this, entity, name);
|
||
|
};
|
||
|
return new Generated(this);
|
||
|
};
|
||
|
Entity.prototype._getDecoder = function _getDecoder(enc) {
|
||
|
enc = enc || 'der';
|
||
|
// Lazily create decoder
|
||
|
if (!this.decoders.hasOwnProperty(enc))
|
||
|
this.decoders[enc] = this._createNamed(decoders[enc]);
|
||
|
return this.decoders[enc];
|
||
|
};
|
||
|
Entity.prototype.decode = function decode(data, enc, options) {
|
||
|
return this._getDecoder(enc).decode(data, options);
|
||
|
};
|
||
|
Entity.prototype._getEncoder = function _getEncoder(enc) {
|
||
|
enc = enc || 'der';
|
||
|
// Lazily create encoder
|
||
|
if (!this.encoders.hasOwnProperty(enc))
|
||
|
this.encoders[enc] = this._createNamed(encoders[enc]);
|
||
|
return this.encoders[enc];
|
||
|
};
|
||
|
Entity.prototype.encode = function encode(data, enc, /* internal */ reporter) {
|
||
|
return this._getEncoder(enc).encode(data, reporter);
|
||
|
};
|