JSPM

  • Created
  • Published
  • Downloads 11202868
  • Score
    100M100P100Q222447F
  • License BSD-3-Clause

A promise based streaming tokenizer

Package Exports

  • strtok3

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (strtok3) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Build Status NPM version npm downloads

A promise based streaming tokenizer for NodeJS. This node module is a successor of strtok2.

Usage

The strtok3 contains one class, ReadStreamTokenizer, which is constructed with a a stream.Readable.

The ReadStreamTokenizer has one method readToken which takes a token as an argument and returns a Promise resolving the decoded token value.

The token is basically a description what to read form the stream. A basic set of token types can be found here: token-types.

Reading from a stream

Below is an example of parsing the the first byte from a readable stream as an unsigned-integer:

import {ReadStreamTokenizer} from "strtok3";
import * as stream from "stream";
import * as Token from "token-types";
    
let readableStream: stream.Readable;
// Assign readable

const streamTokenizer = new ReadStreamTokenizer(readableStream);

return streamTokenizer.readToken<number>(Token.UINT8).then((myUint8Number) => {
  console.log("My number: %s", myUint8Number);
});

Reading from a file

The same can be done from a file:

import * as fs from "fs-extra";
import {FileTokenizer} from "strtok3";
import * as Token from "token-types";
    
return fs.open("somefile.bin", "r").then((fd) => {
  const fileTokenizer = new FileTokenizer(fd);

  return fileTokenizer.readToken<number>(Token.UINT8).then((myUint8Number) => {
      console.log("My number: %s", myUint8Number);
    });
});