strtok3
A promise based streaming tokenizer
Last updated 2 years ago by borewit .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install strtok3 
SYNC missed versions from official npm registry.

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

strtok3

A promise based streaming tokenizer for Node.js and browsers. This node module is a successor of strtok2.

The strtok3 contains a few methods to turn different input into a tokenizer. Designed to

  • Support a streaming environment
  • Decoding of binary data, strings and numbers in mind
  • Read predefined or custom tokens.
  • Optimized tokenizers for reading from file, stream or buffer.

It can read from:

Installation

npm install strtok3

Compatibility

Module: version 7 migrated from CommonJS to pure ECMAScript Module (ESM). JavaScript is compliant with ECMAScript 2019 (ES10). Requires Node.js ≥ 14.16 engine.

API

Use one of the methods to instantiate an abstract tokenizer:

strtok3 methods

All of the strtok3 methods return a tokenizer, either directly or via a promise.

Method strtok3.fromFile()

Parameter Type Description
path Path to file (string) Path to file to read from

Note: that file-information is automatically added.

Returns, via a promise, a tokenizer which can be used to parse a file.

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.UINT8);
    console.log(`My number: ${myNumber}`);
  } finally {
    tokenizer.close(); // Close the file
  } 
})();

Method strtok3.fromStream()

Create tokenizer from a node.js readable stream.

Parameter Optional Type Description
stream no Readable Stream to read from
fileInfo yes IFileInfo Provide file information

Returns a tokenizer, via a Promise, which can be used to parse a buffer.

import strtok3 from 'strtok3';
import * as Token from 'token-types';

strtok3.fromStream(stream).then(tokenizer => {
  return tokenizer.readToken(Token.UINT8).then(myUint8Number => {
    console.log(`My number: ${myUint8Number}`);
  });
});

Method strtok3.fromBuffer()

Parameter Optional Type Description
uint8Array no Uint8Array Uint8Array or Buffer to read from
fileInfo yes IFileInfo Provide file information

Returns a tokenizer which can be used to parse the provided buffer.

import * as strtok3 from 'strtok3';
    
const tokenizer = strtok3.fromBuffer(buffer);

tokenizer.readToken(Token.UINT8).then(myUint8Number => {
  console.log(`My number: ${myUint8Number}`);
});

Tokenizer

The tokenizer allows us to read or peek from the tokenizer-stream. The tokenizer-stream is an abstraction of a stream, file or Buffer. It can also be translated in chunked reads, as done in @tokenizer/http;

What is the difference with Nodejs.js stream?

  • The tokenizer-stream supports jumping / seeking in a the tokenizer-stream using tokenizer.ignore()
  • In addition to read methods, it has peek methods, to read a ahead and check what is coming.

The tokenizer.position keeps tracks of the read position.

strtok3 attributes

Attribute tokenizer.fileInfo

Optional attribute describing the file information, see IFileInfo

Attribute tokenizer.position

Pointer to the current position in the tokenizer stream. If a position is provided to a read or peek method, is should be, at least, equal or greater than this value.

Tokenizer methods

There are two kind of methods:

  1. read methods: used to read a token of Buffer from the tokenizer. The position of the tokenizer-stream will advance with the size of the token.
  2. peek methods: same as the read, but it will not advance the pointer. It allows to read (peek) ahead.

Method tokenizer.readBuffer()

Read buffer from stream. readBuffer(buffer, options?)

Parameter Type Description
buffer Buffer | Uint8Array Target buffer to write the data read to
options IReadChunkOptions An integer specifying the number of bytes to read

Return value Promise<number> Promise with number of bytes read. The number of bytes read maybe if less, mayBeLess flag was set.

Method tokenizer.peekBuffer()

Peek (read ahead) buffer from tokenizer peekBuffer(buffer, options?)

Parameter Type Description
buffer Buffer | Uint8Array Target buffer to write the data read (peeked) to.
options IReadChunkOptions An integer specifying the number of bytes to read.

Return value Promise<number> Promise with number of bytes read. The number of bytes read maybe if less, mayBeLess flag was set.

Method tokenizer.readToken()

Read a token from the tokenizer-stream. readToken(token, position?)

Parameter Type Description
token IGetToken Token to read from the tokenizer-stream.
position? number Offset where to begin reading within the file. If position is null, data will be read from the current file position.

Return value Promise<number>. Promise with number of bytes read. The number of bytes read maybe if less, mayBeLess flag was set.

Method tokenizer.peekToken()

Peek a token from the tokenizer. peekToken(token, position?)

Parameter Type Description
token IGetToken<T> Token to read from the tokenizer-stream.
position? number Offset where to begin reading within the file. If position is null, data will be read from the current file position.

Return value Promise<T> Promise with token value peeked from the tokenizer.

Method tokenizer.readNumber()

Peek a numeric token from the tokenizer. readNumber(token)

Parameter Type Description
token IGetToken<number> Numeric token to read from the tokenizer-stream.

Return value Promise<number> Promise with number peeked from the tokenizer-stream.

Method tokenizer.ignore()

Advanse the offset pointer with the number of bytes provided. ignore(length)

Parameter Type Description
ignore number Numeric of bytes to ignore. Will advance the tokenizer.position

Return value Promise<number> Promise with number peeked from the tokenizer-stream.

Method tokenizer.close()

Clean up resources, such as closing a file pointer if applicable.

IReadChunkOptions

Each attribute is optional:

Attribute Type Description
offset number The offset in the buffer to start writing at; if not provided, start at 0
length number Requested number of bytes to read.
position number Position where to peek from the file. If position is null, data will be read from the current file position. Position may not be less then tokenizer.position
mayBeLess boolean If and only if set, will not throw an EOF error if less then the requested mayBeLess could be read.

Example:

  tokenizer.peekBuffer(buffer, {mayBeLess: true});

IFileInfo

File information interface which describes the underlying file, each attribute is optional.

Attribute Type Description
size number File size in bytes
mimeType number MIME-type of file.
path number File path
url boolean File URL

Token

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

A token is something which implements the following interface:

export interface IGetToken<T> {

  /**
   * Length in bytes of encoded value
   */
  len: number;

  /**
   * Decode value from buffer at offset
   * @param buf Buffer to read the decoded value from
   * @param off Decode offset
   */
  get(buf: Buffer, off: number): T;
}

The tokenizer reads token.len bytes from the tokenizer-stream into a Buffer. The token.get will be called with the Buffer. token.get is responsible for conversion from the buffer to the desired output type.

Browser compatibility

To exclude fs based dependencies, you can use a submodule-import from 'strtok3/lib/core'.

function 'strtok3' 'strtok3/lib/core'
parseBuffer
parseStream
fromFile

Working with Web-API readable stream

To convert a Web-API readable stream into a Node.js readable stream, you can use readable-web-to-node-stream to convert one in another.

Example submodule-import:

import * as strtok3core from 'strtok3/core'; // Submodule-import to prevent Node.js specific dependencies
import { ReadableWebToNodeStream } from 'readable-web-to-node-stream';

(async () => {

  const response = await fetch(url);
  const readableWebStream = response.body; // Web-API readable stream
  const nodeStream = new ReadableWebToNodeStream(readableWebStream); // convert to Node.js readable stream
  
  const tokenizer = strtok3core.fromStream(nodeStream); // And we now have tokenizer in a web environment
})();

Licence

(The MIT License)

Copyright (c) 2020 Borewit

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.

Current Tags

  • 7.0.0-alpha.9                                ...           esm (2 years ago)
  • 7.0.0                                ...           latest (2 years ago)

88 Versions

  • 7.0.0                                ...           2 years ago
  • 7.0.0-alpha.9                                ...           2 years ago
  • 6.3.0                                ...           3 years ago
  • 7.0.0-alpha.8                                ...           3 years ago
  • 7.0.0-alpha.7                                ...           3 years ago
  • 7.0.0-alpha.6                                ...           3 years ago
  • 7.0.0-alpha.5 [deprecated]           ...           3 years ago
  • 7.0.0-alpha.4                                ...           3 years ago
  • 6.2.4                                ...           3 years ago
  • 6.2.3                                ...           3 years ago
  • 7.0.0-alpha.3                                ...           3 years ago
  • 7.0.0-alpha.2                                ...           3 years ago
  • 7.0.0-alpha.1                                ...           3 years ago
  • 7.0.0-alpha.0                                ...           3 years ago
  • 6.2.2                                ...           3 years ago
  • 6.2.1                                ...           3 years ago
  • 6.2.0                                ...           3 years ago
  • 6.1.3                                ...           3 years ago
  • 6.1.2                                ...           3 years ago
  • 6.1.1                                ...           3 years ago
  • 6.1.0                                ...           3 years ago
  • 6.0.8                                ...           4 years ago
  • 6.0.7                                ...           4 years ago
  • 6.0.6                                ...           4 years ago
  • 6.0.5                                ...           4 years ago
  • 6.0.4                                ...           4 years ago
  • 6.0.3                                ...           4 years ago
  • 6.0.2                                ...           4 years ago
  • 6.0.1                                ...           4 years ago
  • 6.0.0                                ...           5 years ago
  • 5.0.1                                ...           5 years ago
  • 5.0.2                                ...           5 years ago
  • 5.0.0                                ...           5 years ago
  • 4.1.1                                ...           5 years ago
  • 4.1.0                                ...           5 years ago
  • 4.0.0                                ...           5 years ago
  • 3.1.7                                ...           5 years ago
  • 3.1.6                                ...           5 years ago
  • 3.1.5                                ...           5 years ago
  • 3.1.4                                ...           5 years ago
  • 3.1.3                                ...           5 years ago
  • 3.1.2                                ...           5 years ago
  • 3.1.1                                ...           5 years ago
  • 3.1.0                                ...           5 years ago
  • 3.0.6                                ...           5 years ago
  • 3.0.5                                ...           5 years ago
  • 3.0.4                                ...           5 years ago
  • 3.0.3                                ...           5 years ago
  • 3.0.1                                ...           5 years ago
  • 3.0.0                                ...           5 years ago
  • 2.3.0                                ...           6 years ago
  • 2.2.0                                ...           6 years ago
  • 2.1.1                                ...           6 years ago
  • 2.1.0                                ...           6 years ago
  • 2.0.0                                ...           6 years ago
  • 1.6.0                                ...           6 years ago
  • 1.5.4                                ...           6 years ago
  • 1.5.3                                ...           6 years ago
  • 1.5.2                                ...           6 years ago
  • 1.5.1                                ...           6 years ago
  • 1.5.0                                ...           6 years ago
  • 1.4.4                                ...           6 years ago
  • 1.4.3                                ...           6 years ago
  • 1.4.2                                ...           7 years ago
  • 1.4.1                                ...           7 years ago
  • 1.4.0                                ...           7 years ago
  • 1.3.4                                ...           7 years ago
  • 1.3.3                                ...           7 years ago
  • 1.3.2                                ...           7 years ago
  • 1.3.1                                ...           7 years ago
  • 1.3.0                                ...           7 years ago
  • 1.2.3                                ...           7 years ago
  • 1.2.2                                ...           7 years ago
  • 1.2.1                                ...           7 years ago
  • 1.2.0                                ...           7 years ago
  • 1.1.3                                ...           7 years ago
  • 1.1.2                                ...           7 years ago
  • 1.1.1                                ...           7 years ago
  • 1.1.0                                ...           7 years ago
  • 1.0.3                                ...           7 years ago
  • 1.0.1                                ...           7 years ago
  • 1.0.0                                ...           7 years ago
  • 0.2.0                                ...           7 years ago
  • 0.1.4                                ...           7 years ago
  • 0.1.3                                ...           7 years ago
  • 0.1.2                                ...           7 years ago
  • 0.1.1                                ...           7 years ago
  • 0.0.1                                ...           7 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |