Socket
Socket
Sign inDemoInstall

token-types

Package Overview
Dependencies
2
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

token-types

Common token types for decoding and encoding numeric and string values


Version published
Maintainers
1
Weekly downloads
3,968,166
decreased by-1.23%
Install size
33.6 kB

Weekly downloads

Package description

What is token-types?

The token-types npm package provides a collection of token parsers for binary data types, facilitating the reading and interpretation of various data types from binary streams. This is particularly useful in applications dealing with multimedia files, where metadata and content need to be extracted or interpreted from binary formats.

What are token-types's main functionalities?

Reading integer values

This feature allows the reading of unsigned 32-bit integers from a buffer in little-endian format. It is useful for parsing low-level binary data structures.

const Token = require('token-types');

const uint32Token = Token.UINT32_LE;
const buffer = Buffer.from([0x01, 0x00, 0x00, 0x00]);
const value = uint32Token.get(buffer, 0);
console.log(value); // Outputs: 1

Reading string values

This feature enables the extraction of ASCII strings from binary data. It supports specifying the length of the string and the encoding, making it versatile for reading text data embedded in binary streams.

const Token = require('token-types');

const stringToken = new Token.StringType(5, 'ascii');
const buffer = Buffer.from('hello');
const value = stringToken.get(buffer, 0);
console.log(value); // Outputs: 'hello'

Other packages similar to token-types

Readme

Source

Node.js CI NPM version npm downloads coveralls Codacy Badge Language grade: JavaScript Total alerts DeepScan grade Known Vulnerabilities

token-types

A primitive token library used to read and write from a node Buffer. Although it is possible to use this module directly, it is primary designed to be used with strtok3 tokenizer.

Compatibility

Module: version 5 migrated from CommonJS to pure ECMAScript Module (ESM). JavaScript is compliant with ECMAScript 2019 (ES10).

Installation

npm install --save token-types

Usually in combination with strtok3:

npm install --save strtok3

Using TypeScript you should also install @tokenizer/token as a development dependency:

npm install --save-dev @tokenizer/token

Example

import * as strtok3 from 'strtok3';
import * as token from 'token-types';
    
(async () => {

  const tokenizer = await strtok3.fromFile("somefile.bin");
  try {
    const myNumber = await tokenizer.readToken(token.Float32_BE);
    console.log(`My number: ${myNumber}`);
  } finally {
    tokenizer.close(); // Close the file
  } 
})();

Tokens

Numeric tokens

node-strtok supports a wide variety of numerical tokens out of the box:

TokenNumberBitsEndianness
UINT8Unsigned integer8n/a
UINT16_BEUnsigned integer16big endian
UINT16_LEUnsigned integer16little endian
UINT24_BEUnsigned integer24big endian
UINT24_LEUnsigned integer24little endian
UINT32_BEUnsigned integer32big endian
UINT32_LEUnsigned integer32little endian
UINT64_BEUnsigned integer64big endian
UINT64_LE*Unsigned integer64little endian
INT8Signed integer8n/a
INT16_BESigned integer16big endian
INT16_LESigned integer16little endian
INT24_BESigned integer24big endian
INT24_LESigned integer24little endian
INT32_BESigned integer32big endian
INT32_LESigned integer32little endian
INT64_BESigned integer64big endian
INT64_LE*Signed integer64little endian
Float16_BEIEEE 754 float16big endian
Float16_LEIEEE 754 float16little endian
Float32_BEIEEE 754 float32big endian
Float32_LEIEEE 754 float32little endian
Float64_BEIEEE 754 float64big endian
Float64_LEIEEE 754 float64little endian
Float80_BE*IEEE 754 float80big endian
Float80_LE*IEEE 754 float80little endian

Other tokens

String types:

  • Windows-1252
  • ISO-8859-1

*) The tokens exceed the JavaScript IEEE 754 64-bit Floating Point precision, decoding and encoding is best effort based.

Custom token

Complex tokens can be added, which makes very suitable for reading binary files or network messages:

 ExtendedHeader = {
    len: 10,

    get: (buf, off) => {
      return {
        // Extended header size
        size: Token.UINT32_BE.get(buf, off),
        // Extended Flags
        extendedFlags: Token.UINT16_BE.get(buf, off + 4),
        // Size of padding
        sizeOfPadding: Token.UINT32_BE.get(buf, off + 6),
        // CRC data present
        crcDataPresent: common.strtokBITSET.get(buf, off + 4, 31)
      };
    }
  };

Keywords

FAQs

Last updated on 02 Aug 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc