UNPKG

4.32 kBJavaScriptView Raw
1/*
2Copyright 2011 Timothy J Fontaine <tjfontaine@gmail.com>
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
21*/
22
23"use strict";
24
25var assert = require('assert');
26
27var Message = function () {
28 this.initializeFields(this._fields);
29 if (this._sub_fields) {
30 this.initializeFields(this._sub_fields);
31 }
32};
33
34Message.prototype.initializeFields = function (fields) {
35 var self = this;
36 fields.forEach(function (field) {
37 self[field.name] = field.default;
38 });
39};
40
41Message.prototype.unpack = function (buff, pos) {
42 var ret;
43
44 this.record_position_ = pos;
45 this.raw_ = buff;
46
47 ret = this.unpackFields(this._fields, buff, pos);
48
49 if (this._sub_fields) {
50 this.unpackFields(this._sub_fields, buff, pos);
51 }
52
53 return ret;
54};
55
56Message.prototype.unpackFields = function (fields, buff, pos) {
57 var self = this, start = pos;
58
59 fields.forEach(function (field) {
60 var value;
61 if (field.get) {
62 value = field.get(self);
63 } else {
64 var ret, start;
65
66 start = pos;
67
68 try {
69 ret = field.unpack(buff, pos);
70 } catch (e) {
71 var err = new Error("Failed to unpack: " + field.name + " -- " + e + " [bufflen: " + buff.length + ", pos: " + pos + "]");
72 err.inner = e;
73 err.raw = buff;
74 err.pos = pos;
75 throw err;
76 }
77
78 pos += ret.read;
79 value = ret.value;
80
81 if (ret.field_position) {
82 field.position = ret.field_position;
83 } else {
84 field.position = start;
85 }
86 }
87 self[field.name] = value || field.default;
88 });
89
90 return pos - start;
91};
92
93Message.prototype.pack = function (buff, pos) {
94 if (this._sub_fields) {
95 this.packFields(this._sub_fields, buff, pos);
96 }
97 return this.packFields(this._fields, buff, pos);
98};
99
100Message.prototype.packFields = function(fields, buff, pos) {
101 var spos = pos, self = this;
102
103 fields.forEach(function (field) {
104 var value = self[field.name];
105
106 if (field.set) {
107 field.set(self, value);
108 } else {
109 try {
110 pos += field.pack.call(self, value, buff, pos);
111 } catch (e) {
112 var err = new Error("Failed to pack: " + field.name + " -- " + e + " [bufflen: " + buff.length + ", pos: " + pos + "]");
113 err.inner = e;
114 err.raw = buff;
115 err.pos = pos;
116 throw err;
117 }
118 }
119 });
120
121 return pos - spos;
122};
123
124Message.prototype.estimateSize = function () {
125 return this.estimateSizeFields(this._fields);
126};
127
128Message.prototype.estimateSizeFields = function (fields) {
129 var size = 0, self = this;
130 fields.forEach(function (field) {
131 var value = self[field.name];
132 if (!field.size.call) {
133 size += field.size;
134 } else {
135 size += field.size(value);
136 }
137 });
138 return size;
139};
140
141Message.prototype.compare = function (obj) {
142 var base, sub = true, rdata = true;
143
144 base = this.compareFields(this._fields, obj)
145
146 if (this._sub_fields)
147 sub = this.compareFields(this._sub_fields, obj);
148
149 if (this._rdata_fields)
150 rdata = this.compareFields(this._rdata_fields, obj);
151
152 return base && sub && rdata;
153};
154
155Message.prototype.compareFields = function (fields, obj) {
156 var same = true, self = this;
157 fields.forEach(function (field) {
158 if (same) {
159 try {
160 assert.deepEqual(self[field.name], obj[field.name]);
161 } catch (e) {
162 same = false;
163 }
164 }
165 });
166 return same;
167};
168
169module.exports = Message;