TLV Parser for Node.js
The `node-tlv` library provides a robust parser and builder for Tag-Length-Value (TLV) encoded data, commonly found in smart card protocols like EMV and ISO7816. It supports parsing both simple TLV and BER-TLV structures, allowing developers to extract and manipulate data elements such as DGI, EMV, ISO7816, and DOL tags. Currently at stable version 1.5.14, the package does not explicitly state a release cadence, but its versioning and continuous updates suggest ongoing maintenance. Its key differentiator lies in its focused support for financial and smart card industry standards, offering both parsing of raw hexadecimal strings into structured TLV objects and the ability to programmatically construct complex TLV hierarchies, useful for building responses like PPSE FCI or PSE records. It is designed for Node.js environments, providing a programmatic interface to navigate and construct TLV data structures.
Common errors
-
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax in a Node.js environment or file configured for CommonJS, or without transpilation.fixChange `import TLV from 'node-tlv';` to `const TLV = require('node-tlv');`. If your `package.json` contains `"type": "module"`, consider removing it or explicitly configuring your file to be CommonJS (e.g., using a `.cjs` extension). -
TypeError: TLV.parse is not a function
cause Incorrectly importing the `node-tlv` module, leading to `TLV` not being the expected class with the static `parse` method, or attempting to destructure an incorrectly exported CommonJS module.fixVerify your import statement. For CommonJS, it should typically be `const TLV = require('node-tlv');`. Ensure you are not trying to destructure `TLV` (e.g., `const { parse } = require('node-tlv');`) as `parse` is a static method of the main `TLV` export. -
AssertionError [ERR_ASSERTION]: TLV length mismatch
cause The input hexadecimal string provided to `TLV.parse()` or the `TLV` constructor is malformed, has an incorrect length indicator, or contains non-hexadecimal characters.fixCarefully inspect the input string. Ensure it's a valid, clean hexadecimal representation of TLV data, where length fields correctly correspond to the subsequent value data length. Use a hex editor or online TLV decoder to validate the input structure.
Warnings
- gotcha The 'node-tlv' library is primarily distributed as a CommonJS module. Attempting to import it using ES module syntax (e.g., `import TLV from 'node-tlv';`) directly in an ESM project might lead to import errors or unexpected behavior.
- gotcha The parsing functions (`TLV.parse()` and `new TLV()`) expect valid hexadecimal string input for TLV data. Providing non-hexadecimal characters, odd-length strings, or malformed TLV structures (e.g., incorrect length fields) will result in parsing errors or incorrect data representation.
- gotcha When building TLV objects using `new TLV(tag, value)`, the `value` parameter expects the raw hexadecimal data content of that TLV tag, *not* the full TLV string including its own tag and length. The library automatically calculates and inserts the length field.
Install
-
npm install node-tlv -
yarn add node-tlv -
pnpm add node-tlv
Imports
- TLV
import TLV from 'node-tlv';
const TLV = require('node-tlv'); - TLV.parse
import { parse } from 'node-tlv';const TLV = require('node-tlv'); const parsedTlv = TLV.parse('770E82025800...'); - new TLV()
import { TLV } from 'node-tlv'; const newTlv = new TLV('84', '...');const TLV = require('node-tlv'); const newTlv = new TLV('84', '325041592E5359532E4444463031');
Quickstart
const TLV = require('node-tlv');
const assert = require('assert');
// A sample GPO (Get Processing Options) response in hex string format.
const resp = '770E8202580094080801010010010301';
// Parse the raw hexadecimal string into a TLV object tree.
const tlv = TLV.parse(resp);
// Assertions to verify the top-level TLV object's tag, length, and value.
assert(tlv.getTag() === '77');
assert(tlv.getLength() === 14); // Length in bytes for the value '8202580094080801010010010301'
assert(tlv.getValue() === '8202580094080801010010010301');
// Retrieve child TLV objects from the parsed structure.
const child = tlv.getChild();
assert(child.length === 2);
assert(child[0].getTag() === '82');
// Find a specific TLV tag using either a number or string representation.
const aip = tlv.find(0x82); // Application Interchange Profile (AIP)
assert(aip.getTag() === '82');
assert(aip.getLength() === 2);
assert(aip.getValue() === '5800');
const afl = tlv.find('94'); // Application File Locator (AFL)
assert(afl.getTag() === '94');
assert(afl.getLength() === 8);
assert(afl.getValue() === '0801010010010301');