UNPKG

1.38 kBJavaScriptView Raw
1// Copyright 2011 Mark Cavage, Inc. All rights reserved.
2
3var assert = require('assert-plus');
4var util = require('util');
5
6var asn1 = require('asn1');
7
8var Protocol = require('../protocol');
9
10
11///--- Globals
12
13var Ber = asn1.Ber;
14
15
16///--- API
17
18function Control(options) {
19 assert.optionalObject(options);
20 options = options || {};
21 assert.optionalString(options.type);
22 assert.optionalBool(options.criticality);
23 if (options.value) {
24 assert.buffer(options.value);
25 }
26
27 this.type = options.type || '';
28 this.criticality = options.critical || options.criticality || false;
29 this.value = options.value || null;
30}
31Object.defineProperties(Control.prototype, {
32 json: {
33 get: function getJson() {
34 var obj = {
35 controlType: this.type,
36 criticality: this.criticality,
37 controlValue: this.value
38 };
39 return (typeof (this._json) === 'function' ? this._json(obj) : obj);
40 }
41 }
42});
43
44Control.prototype.toBer = function toBer(ber) {
45 assert.ok(ber);
46
47 ber.startSequence();
48 ber.writeString(this.type || '');
49 ber.writeBoolean(this.criticality);
50 if (typeof (this._toBer) === 'function') {
51 this._toBer(ber);
52 } else {
53 if (this.value)
54 ber.writeString(this.value);
55 }
56
57 ber.endSequence();
58 return;
59};
60
61Control.prototype.toString = function toString() {
62 return this.json;
63};
64
65
66///--- Exports
67module.exports = Control;