Gathering detailed insights and metrics for strtok3
Gathering detailed insights and metrics for strtok3
Gathering detailed insights and metrics for strtok3
Gathering detailed insights and metrics for strtok3
npm install strtok3
Typescript
Module System
Min. Node Version
Node Version
NPM Version
99.4
Supply Chain
99.1
Quality
85.2
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
627,404,069
Last Day
1,275,678
Last Week
7,973,848
Last Month
31,421,134
Last Year
263,374,669
MIT License
41 Stars
1,334 Commits
15 Forks
2 Watchers
5 Branches
4 Contributors
Updated on Mar 09, 2025
Latest Version
10.2.2
Package Id
strtok3@10.2.2
Unpacked Size
50.52 kB
Size
11.00 kB
File Count
17
NPM Version
10.9.2
Node Version
22.13.0
Published on
Mar 09, 2025
Cumulative downloads
Total Downloads
Last Day
14.9%
1,275,678
Compared to previous day
Last Week
9.5%
7,973,848
Compared to previous week
Last Month
16.9%
31,421,134
Compared to previous month
Last Year
52.3%
263,374,669
Compared to previous year
A promise based streaming tokenizer for Node.js and browsers.
The strtok3
module provides several methods for creating a tokenizer from various input sources.
Designed for:
strtok3
can read from:
1npm install strtok3
Starting with version 7, the module has migrated from CommonJS to pure ECMAScript Module (ESM). The distributed JavaScript codebase is compliant with the ECMAScript 2020 (11th Edition) standard.
Requires a modern browser, Node.js (V8) ≥ 18 engine or Bun (JavaScriptCore) ≥ 1.2.
For TypeScript CommonJs backward compatibility, you can use load-esm.
[!NOTE] This module requires a Node.js ≥ 16 engine. It can also be used in a browser environment when bundled with a module bundler.
If you find this project useful and would like to support its development, consider sponsoring or contributing:
Buy me a coffee:
Use one of the methods to instantiate an abstract tokenizer:
NOTE: *
fromFile
andfromStream
only available when importing this module with Node.js
All methods return a Tokenizer
, either directly or via a promise.
fromFile
functionCreates a tokenizer from a local file.
1function fromFile(sourceFilePath: string): Promise<FileTokenizer>
Parameter | Type | Description |
---|---|---|
sourceFilePath | string | Path to file to read from |
[!NOTE]
- Only available for Node.js engines
fromFile
automatically embeds file-information
Returns, via a promise, a tokenizer which can be used to parse a file.
1import * as strtok3 from 'strtok3'; 2import * as Token from 'token-types'; 3 4(async () => { 5 6 const tokenizer = await strtok3.fromFile("somefile.bin"); 7 try { 8 const myNumber = await tokenizer.readToken(Token.UINT8); 9 console.log(`My number: ${myNumber}`); 10 } finally { 11 tokenizer.close(); // Close the file 12 } 13})();
fromStream
functionCreates a tokenizer from a Node.js readable stream.
1function fromStream(stream: Readable, options?: ITokenizerOptions): Promise<ReadStreamTokenizer>
Parameter | Optional | Type | Description |
---|---|---|---|
stream | no | Readable | Stream to read from |
fileInfo | yes | IFileInfo | Provide file information |
Returns a Promise providing a tokenizer.
[!NOTE]
- Only available for Node.js engines
fromWebStream
functionCreates tokenizer from a WHATWG ReadableStream.
1function fromWebStream(webStream: AnyWebByteStream, options?: ITokenizerOptions): ReadStreamTokenizer
Parameter | Optional | Type | Description |
---|---|---|---|
readableStream | no | ReadableStream | WHATWG ReadableStream to read from |
fileInfo | yes | IFileInfo | Provide file information |
Returns a Promise providing a tokenizer
1import strtok3 from 'strtok3'; 2import * as Token from 'token-types'; 3 4strtok3.fromWebStream(readableStream).then(tokenizer => { 5 return tokenizer.readToken(Token.UINT8).then(myUint8Number => { 6 console.log(`My number: ${myUint8Number}`); 7 }); 8});
fromBuffer()
functionCreate a tokenizer from memory (Uint8Array).
1function fromBuffer(uint8Array: Uint8Array, options?: ITokenizerOptions): BufferTokenizer
Parameter | Optional | Type | Description |
---|---|---|---|
uint8Array | no | Uint8Array | Uint8Array or Buffer to read from |
fileInfo | yes | IFileInfo | Provide file information |
Returns a Promise providing a tokenizer.
1import * as strtok3 from 'strtok3'; 2 3const tokenizer = strtok3.fromBuffer(buffer); 4 5tokenizer.readToken(Token.UINT8).then(myUint8Number => { 6 console.log(`My number: ${myUint8Number}`); 7});
Tokenizer
objectThe tokenizer is an abstraction of a stream, file or Uint8Array, allowing reading or peeking from the stream. It can also be translated in chunked reads, as done in @tokenizer/http;
tokenizer.ignore()
.peek
methods to preview data without advancing the read pointer.Read methods advance the stream pointer, while peek methods do not.
There are two kind of functions:
readBuffer
functionRead data from the tokenizer into provided "buffer" (Uint8Array
).
readBuffer(buffer, options?)
1readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
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 promise with number of bytes read. The number of bytes read maybe if less, mayBeLess flag was set.
peekBuffer
functionPeek (read ahead), from tokenizer, into the buffer without advancing the stream pointer.
1peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
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.
readToken
functionRead a token from the tokenizer-stream.
1readToken<Value>(token: IGetToken<Value>, position: number = this.position): Promise<Value>
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.
peek
functionPeek a token from the tokenizer.
1peekToken<Value>(token: IGetToken<Value>, position: number = this.position): Promise<Value>
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 a promise with the token value peeked from the tokenizer.
readNumber
functionPeek a numeric token from the tokenizer.
1readNumber(token: IToken<number>): Promise<number>
Parameter | Type | Description |
---|---|---|
token | IGetToken | Numeric token to read from the tokenizer-stream. |
Returns a promise with the decoded numeric value from the tokenizer-stream.
ignore
functionAdvance the offset pointer with the token number of bytes provided.
1ignore(length: number): Promise<number>
Parameter | Type | Description |
---|---|---|
ignore | number | Numeric of bytes to ignore. Will advance the tokenizer.position |
Returns a promise with the decoded numeric value from the tokenizer-stream.
close
functionClean up resources, such as closing a file pointer if applicable.
Tokenizer
attributesfileInfo
Optional attribute describing the file information, see IFileInfo
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.
IReadChunkOptions
interfaceEach attribute is optional:
Attribute | Type | Description |
---|---|---|
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 usage:
1 tokenizer.peekBuffer(buffer, {mayBeLess: true});
IFileInfo
interfaceProvides optional metadata about the file being tokenized.
Attribute | Type | Description |
---|---|---|
size | number | File size in bytes |
mimeType | number | MIME-type of file. |
path | number | File path |
url | boolean | File URL |
Token
objectThe 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:
1export interface IGetToken<T> { 2 3 /** 4 * Length in bytes of encoded value 5 */ 6 len: number; 7 8 /** 9 * Decode value from buffer at offset 10 * @param buf Buffer to read the decoded value from 11 * @param off Decode offset 12 */ 13 get(buf: Uint8Array, off: number): T; 14}
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.
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.
1import { fromWebStream } strtok3 from 'strtok3'; 2import { ReadableWebToNodeStream } from 'readable-web-to-node-stream'; 3 4(async () => { 5 6 const response = await fetch(url); 7 const readableWebStream = response.body; // Web-API readable stream 8 const webStream = new ReadableWebToNodeStream(readableWebStream); // convert to Node.js readable stream 9 10 const tokenizer = fromWebStream(webStream); // And we now have tokenizer in a web environment 11})();
The diagram below illustrates the primary dependencies of strtok3
:
1graph TD;
2 S(strtok3)-->P(peek-readable)
3 S(strtok3)-->TO("@tokenizer/token")
strtok3
for interpreting binary data.This project is licensed under the MIT License. Feel free to use, modify, and distribute as needed.
No vulnerabilities found.
Reason
14 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
SAST tool is run on all commits
Details
Reason
1 existing vulnerabilities detected
Details
Reason
Found 0/19 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2025-05-05
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More