ber-tlv
Parsing and serializing TLV data
A streamlined library for creating, serializing and parsing TLV encoded data with focus on verbosity.
Usage
Importing the Module
import { ITlv, TlvType, TlvClass } from './node_modules/src/Tlv.ts';
import { TlvFactory, IParseError } from './node_modules/src/TlvFactory.ts';
var ITlv = require('ber-tlv').ITlv;
var TlvType = require('ber-tlv').TlvType;
var TlvClass = require('ber-tlv').TlvClass;
var TlvFactory = require('ber-tlv').TlvFactory;
var IParseError = require('ber-tlv').IParseError;
The ITlv object
var tlv = ...;
var tag = tlv.tag;
var type = tlv.type;
var class = tlv.class;
var buffer = tlv.value;
var array = tlv.items;
var string = TlvType[tlv.type];
var string = TlvClass[tlv.class]
The TlvFactory
var tlv = TlvFactory.primitiveTlv(tag, value);
var tlv = TlvFactory.constructedTlv(tag, value);
var tlv[] = TlvFactory.parse(data);
var tlv[] = error.partialTlv;
var buffer = TlvFactory.serialize(tlv);
Examples
Working with High Level Constructors
var tag = '57';
var value = '2030001020'
var primitive = TlvFactory.primitiveTlv(tag, value);
var tag = 'E0';
var constructed = TlvFactory.constructedTlv(tag, primitive);
Parsing a String
var string = '570101';
var tlv = [];
try {
tlv = TlvFactory.parse(string);
}
catch (error){
tlv = error.partialTlv;
console.log(error.name);
console.log(error.message);
}
Development
Prerequisites
The project is build on Typescript
for strong javascript typing and uses DefinitelyTyped
for downloading typescript definitions for development dependencies. In the end, everything is glued together by Gulp
.
//global dependencies
npm install gulp -g
npm install typescript -g
npm install tsd -g
//and now the local dependencies
npm install
Build
gulp build
Note: The ts.d file for the resulting javascript is currently created manually after the build.
Test
gulp test
License
The MIT License (MIT)
Copyright (c) 2015 Simon Eumes
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.