npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tlv.js

v1.0.0

Published

TLV de- and encoder

Downloads

3

Readme

tlv.js

This package en- and decodes TLV objects as they are used for BER (Basic Encoding Rules) data structures. (But don't expect BER to be included! This package could be used to implement a BER en- and decoder.)

This is basically the foundation if you want to read X.509 certificats by hand.

For verifacation purposes there exists a handy dandy online viewer for in- and outputs of this package: The ASN.1 JavaScript decoder by Lapo Luchini.

The ruleset is described in ISO/IEC 7816. This document is your deep dive, if you don't understand the terminology used in the API description down below.

API

const TLV = require('tlv');

Constructor

const obj = new TLV(opts);

Creates a new TLV object. opts states the properties the object shall be initialised with. Cf. the property description below for further details.

Furthermore opts can have the property inherit which states another TLV instance of which the properties class, tag and type are inherited.

Property: class

obj.class = 'context';
const class = obj.class;

Sets or gets the class of the TLV object. Valid classes are: 'universal', 'application', 'context' and 'private'.

Property: type

obj.type = 'primitive';
const type = obj.type;

Sets or gets the type of the TLV object. 'primitive' objects hold binary data. 'constructed' objects hold further TLV objects.

Property: tag

obj.tag = 0x10;
const tag = obj.tag;

Sets or gets the tag of obj.

Property: length and fullLength

const length = obj.length;
const fullLength = obj.fullLength;

Gets the length in bytes. length is the size of the object's value. fullLength also include the object's header size.

Property: value

const value = obj.value

Gets the hold value. For primitive objects value returns a Buffer containing its value. If the TLV object is constructed, value is an Array of TLV instances.

const int = new TLV({class: 'univeral', type: 'primitive', tag: 0x02, value: 0x42});
obj.value = int;

Stores int insided of obj. obj.type will change to constructed.

const data = Buffer.from('hello');
obj.value = data;

Stores data insided of obj. obj.type will change to primitive.

Property: next

const int = new TLV({class: 'univeral', type: 'primitive', tag: 0x02, value: 0x42});
obj.next = int;

Stores int after (and not inside of) obj;

const next = obj.next;

Returns the next TLV object after obj or null if obj is the last item.

Method: toBuffer()

const buf = obj.toBuffer();

Converts the TLV object into a Buffer.

Method: is()

const SEQ = new TLV({class: 'universal', type: 'constructed', tag: 0x10});
const isSeq = obj.is(SEQ);

isSeq is true, if obj has the same class, type and tag as SEQ. Value and length are not compared.

Method: assert()

const SEQ = new TLV({class: 'universal', type: 'constructed', tag: 0x10});
obj.assert(SEQ);

Throws an Error, if obj is not a BER SEQ.