-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuild.js
More file actions
322 lines (281 loc) · 9.99 KB
/
build.js
File metadata and controls
322 lines (281 loc) · 9.99 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
'use strict';
var fs = require('fs');
var Readable = require('stream').Readable;
var path = require('path');
var _ = require('lodash');
var sax = require('sax');
var Q = require('q');
var loki = require('lokijs');
var dictionary = {};
var dictionaryDbLocation = path.normalize(__dirname + '/loki_dictionary.json');
var db = new loki(dictionaryDbLocation);
var collections = [
'applications',
'commands',
'avps',
'typedefns',
'vendors'
];
var initDb = function() {
_.each(collections, function(collection) {
db.removeCollection(collection);
dictionary[collection] = db.addCollection(collection);
});
};
var parseDictionaryFile = function(dictionaryFile) {
var deferred = Q.defer();
var saxStream = sax.createStream(false, {lowercase: true});
saxStream.setMaxListeners(100);
var currentTags = {};
var insertOrFind = function(collection, item, searchQuery) {
var alreadyAdded = collection.findOne(searchQuery);
if (_.isEmpty(alreadyAdded)) {
collection.insert(item);
return item;
}
return alreadyAdded;
};
var tagHandlers = {
application: function(node) {
currentTags.application = insertOrFind(dictionary.applications, node.attributes,
{ 'id': { '$eq': node.attributes.id.toString() } });
},
command: function(node) {
var command = node.attributes;
command.applicationId = currentTags.application.id;
currentTags.command = insertOrFind(dictionary.commands, command,
{ '$and': [
{ 'applicationId': { '$eq': command.applicationId.toString() } },
{ 'code': { '$eq': command.code.toString() } }
] });
},
vendor: function(node) {
currentTags.vendors = insertOrFind(dictionary.vendors, node.attributes,
{ 'id': { '$eq': node.attributes['vendor-id'].toString() } });
},
avp: function(node) {
var avp = node.attributes;
if (currentTags.application) {
avp.applicationId = currentTags.application.id;
currentTags.avp = insertOrFind(dictionary.avps, avp,
{ '$and': [
{ 'applicationId': { '$eq': avp.applicationId.toString() } },
{ 'code': { '$eq': avp.code.toString() } }
] });
} else if (currentTags.vendors) {
if (currentTags.vendors instanceof Array) {
throw new Error('Expected vendor as parent element');
}
avp.applicationId = 0;
avp.vendorId = currentTags.vendors['vendor-id'];
currentTags.avp = insertOrFind(dictionary.avps, avp,
{ '$and': [
{ 'vendorId': { '$eq': avp.vendorId.toString() } },
{ 'code': { '$eq': avp.code.toString() } }
] });
} else {
throw new Error('Neither the application nor the vendor is known');
}
},
base: function() {
var baseApp = {
id: '0',
name: 'Diameter Common Messages'
};
currentTags.application = insertOrFind(dictionary.applications, baseApp,
{ 'id': { '$eq': baseApp.id.toString() } });
},
typedefn: function(node) {
var typedefn = node.attributes;
typedefn.applicationId = currentTags.application.id;
currentTags.typedefn = insertOrFind(dictionary.typedefns, typedefn,
{ '$and': [
{ 'applicationId': { '$eq': typedefn.applicationId.toString() } },
{ 'type-name': { '$eq': typedefn['type-name'].toString() } }
] });
},
type: function(node) {
var parent = currentTags.avp;
parent.type = node.attributes['type-name'];
dictionary.avps.update(parent);
},
'enum': function(node) {
var parent = currentTags.avp;
if (parent.enums == null) {
parent.enums = [];
}
parent.enums.push(node.attributes);
dictionary.avps.update(parent);
},
grouped: function(node) {
var parent = currentTags.avp;
parent.grouped = true;
dictionary.avps.update(parent);
},
gavp: function(node) {
var parent = currentTags.avp;
if (parent.gavps == null) {
parent.gavps = [];
}
parent.gavps.push(node.attributes.name);
dictionary.avps.update(parent);
}
};
saxStream.on('error', function(error) {
deferred.reject(error);
});
saxStream.on('opentag', function (node) {
currentTags[node.name] = node.attributes;
var tagHandler = tagHandlers[node.name];
if (tagHandler) {
tagHandler(node);
}
});
saxStream.on('closetag', function (tag) {
currentTags[tag] = null;
});
saxStream.on('end', function () {
db.save(function() {
deferred.resolve(dictionary);
});
});
var xml = fs.readFileSync(dictionaryFile, 'utf-8');
var entityMap = {};
var entityRe = /<!ENTITY\s+(.+?)\s+SYSTEM\s+"(.+)"\s*>/g;
var tokens;
while (tokens = entityRe.exec(xml)) {
var entityName = tokens[1];
var entityUri = tokens[2];
var entityPath = path.join(path.dirname(dictionaryFile), entityUri);
entityMap[entityName] = fs
.readFileSync(entityPath, 'utf-8')
.replace(/<\?xml.+\?>/, '');
}
xml = xml.replace(/\&([^;]+);/g, function(s, entityName) {
return entityMap[entityName] || s;
});
var xmlStream = new Readable();
xmlStream.push(xml);
xmlStream.push(null);
xmlStream.pipe(saxStream);
return deferred.promise;
};
var dictionaryDeferred = Q.defer();
var getDictionary = function() {
var dictionaryLocation = path.join(__dirname, 'node_modules', `wireshark.git#${process.argv[2] || ''}`, 'resources', 'protocols', 'diameter', 'dictionary.xml');
initDb();
parseDictionaryFile(dictionaryLocation).then(dictionaryDeferred.resolve,
dictionaryDeferred.reject);
return dictionaryDeferred.promise;
};
var getTypedefn = function(type, appId) {
var typedefn = dictionary.typedefns.find({
'$and': [{
'applicationId': {
'$eq': appId.toString()
}
}, {
'type-name': {
'$eq': type.toString()
}
}]
});
if (typedefn.length == 0 && appId !== '0') {
return getTypedefn(type, '0');
}
return typedefn[0];
};
var resolveToBaseType = function(type, appId) {
if (type == 'QoSFilterRule') return 'UTF8String';
if (type == 'Float32') return 'Unsigned32';
if (type == 'Float64') return 'Unsigned64';
if (type == 'Address') return 'OctetString';
if (type == 'DiameterIdentity') return 'UTF8String';
if (type == 'IPFilterRule') return 'UTF8String';
var parsableTypes = [
'OctetString',
'UTF8String',
'Unsigned32',
'Integer32',
'Unsigned64',
'Integer64',
'Time',
'IPAddress',
'AppId'
];
var typedefn = getTypedefn(type, appId);
if (_.includes(parsableTypes, typedefn['type-name'])) {
return typedefn['type-name'];
} else if (typedefn['type-parent'] !== undefined) {
return resolveToBaseType(typedefn['type-parent'], appId);
}
throw new Error('Unable to resolve type ' + type + ' for app ' + appId);
};
console.log('Parsing diameter dictionaries...');
getDictionary().then(function() {
console.log('Dictionaries parsed to loki_dictionary.json');
// This part stores it in plain JSON
var applications = dictionary.applications.find().map(function(app) {
return {
code: parseInt(app.id, 10),
name: app.name
};
});
var commands = dictionary.commands.find().map(function(com) {
var vendor = dictionary.vendors.findOne({
'vendor-id': {
'$eq': com['vendor-id']
}
});
var vendorId = vendor == null ? 0 : parseInt(vendor.code, 10);
return {
code: parseInt(com.code, 10),
name: com.name,
vendorId: vendorId
};
});
var avps = dictionary.avps.find().map(function(a) {
var vendor = dictionary.vendors.findOne({
'vendor-id': {
'$eq': a['vendor-id']
}
});
var vendorId = vendor == null ? 0 : parseInt(vendor.code, 10);
var avp = {
code: parseInt(a.code, 10),
name: a.name,
vendorId: vendorId,
type: a.type == null ? undefined : resolveToBaseType(a.type, a.applicationId),
flags: {
mandatory: a.mandatory == 'must',
'protected': a['protected'] == 'may',
mayEncrypt: a['may-encrypt'] == 'yes',
vendorBit: a['vendor-bit'] == 'must'
}
};
if (a.grouped) {
avp.groupedAvps = a.gavps;
avp.type = 'Grouped';
}
if (a.enums != null) {
avp.enums = _.map(a.enums, function(e) {
return {code: parseInt(e.code, 10), name: e.name};
});
}
return avp;
});
var dict = {
applications: _.sortBy(applications, 'code'),
commands: _.sortBy(commands, [ 'code', 'vendorId' ]),
avps: _.sortBy(avps, [ 'code', 'vendorId' ])
};
fs.writeFile('dist/dictionary.json', JSON.stringify(dict, null, 4), function(err) {
if(err) {
console.log(err);
} else {
console.log("JSON saved to " + 'dist/dictionary.json');
}
});
}, function(err) {
console.log('Error: ' + err);
}).done();