-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
210 lines (167 loc) · 4.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"use strict";
const Parser = require("./lib/parser.js");
const Serializer = require("./lib/serializer.js");
const getType = require("./lib/type_functions.js");
const PRIMITIVE_TYPES = require("./lib/primitive_types.json");
const PRIMITIVES = Object.keys(PRIMITIVE_TYPES).map((key) => {
return key.toLowerCase();
});
// array with ints 1..24
const BIT_VALS = Array.apply(null, Array(32)).map((_, i) => {
return i + 1;
});
class Bin {
parser = new Parser();
serializer = new Serializer();
endian = "be";
bitRequests = [];
static start = () => {
return new Bin();
};
static Parser = Bin; // work as drop-in replacement for binary-parser
serialize(obj, buf) {
this._flushBitfield();
return this.serializer.serialize(obj, buf);
}
sizeOf(obj) {
if (obj == null) return this.parser.fixedSize;
return this.serializer.sizeFunc(obj);
}
choice(varName, options) {
this._flushBitfield();
const choices = options.choices;
const mappedChoices = {};
for (var key in choices) {
mappedChoices[key] = getType(choices[key]);
}
const defaultChoice =
options.defaultChoice && getType(options.defaultChoice);
const tag = options.tag;
const getTag =
typeof tag === "function"
? tag
: (obj) => {
if (!(tag in obj))
throw new Error(`tag \`${tag}\` not found in object`);
return obj[tag];
};
var getChoice = defaultChoice
? (obj) => {
var key = getTag(obj);
return key in mappedChoices ? mappedChoices[key] : defaultChoice;
}
: (obj) => {
var choice = mappedChoices[getTag(obj)];
if (!choice) throw new Error("invalid choice");
return choice;
};
this.parser.choice(varName, options, getChoice);
this.serializer.choice(varName, options, getChoice);
return this;
}
create(constructorFn) {
this.parser.create(constructorFn);
return this;
}
compile() {
/* do nothing */
}
getCode() {
throw new Error("not implemented");
}
parse(buffer, callback) {
this._flushBitfield();
return this.parser.parse(buffer, callback) || {};
}
stream() {
return this.parser.stream();
}
nest(varName, options) {
var type = getType(options.type, true);
var opts = { __proto__: options, type: type };
if (type.bitRequests.length) {
this.bitRequests = this.bitRequests.concat(
type.bitRequests.map((req) => {
return {
i: req.i,
vars: [varName].concat(req.vars),
options: req.options,
// TODO support constructors
};
}, this)
);
}
this.parser.nest(varName, opts);
this.serializer.nest(varName, opts);
return this;
}
_flushBitfield() {
var reqs = this.bitRequests;
if (!reqs.length) return;
if (this.endian === "le") reqs = reqs.reverse();
const length = reqs.reduce((sum, req) => {
return sum + req.i;
}, 0);
this.serializer._processBitfield(reqs, length);
this.parser.processBitfield(reqs, length);
this.bitRequests = [];
}
// copied from binary_parser.js
endianess(endianess) {
switch (endianess.toLowerCase()) {
case "little":
this.endian = "le";
break;
case "big":
this.endian = "be";
break;
default:
throw new Error("Invalid endianess: " + endianess);
}
return this;
}
// alias properties
get writeFunc() {
return this.serializer.writeFunc;
}
get sizeFunc() {
return this.serializer.sizeFunc;
}
get readFunc() {
return this.parser.readFunc;
}
get constructorFn() {
return this.parser.constructorFn;
}
get fixedSize() {
return this.parser.fixedSize;
}
}
["string", "buffer", "array", "fixedSizeNest", ...PRIMITIVES].forEach(
(name) => {
Bin.prototype[name] = function (varName, options) {
this._flushBitfield();
const type = options && options.type && getType(options.type);
this.parser[name](varName, options, type);
this.serializer[name](varName, options, type);
return this;
};
}
);
BIT_VALS.forEach((i) => {
Bin.prototype["bit" + i] = function (varName, options) {
// TODO support constructors
this.bitRequests.push({ i: i, vars: [varName], options: options });
return this;
};
});
Object.keys(PRIMITIVE_TYPES)
.filter((p) => p.endsWith("BE"))
.map((p) => p.toLowerCase())
.forEach((primitiveName) => {
var name = primitiveName.slice(0, -2).toLowerCase();
Bin.prototype[name] = function (varName, options) {
return this[name + this.endian](varName, options);
};
});
module.exports = Bin;