strtok3 9.0.1 → 9.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,19 +10,20 @@ interface INormalizedReadChunkOptions extends IReadChunkOptions {
10
10
  * Core tokenizer
11
11
  */
12
12
  export declare abstract class AbstractTokenizer implements ITokenizer {
13
- fileInfo: IFileInfo;
14
13
  private onClose?;
14
+ private numBuffer;
15
+ abstract fileInfo: IFileInfo;
16
+ /**
17
+ * Tokenizer-stream position
18
+ */
19
+ position: number;
15
20
  /**
16
21
  * Constructor
17
22
  * @param options Tokenizer options
18
23
  * @protected
19
24
  */
20
25
  protected constructor(options?: ITokenizerOptions);
21
- /**
22
- * Tokenizer-stream position
23
- */
24
- position: number;
25
- private numBuffer;
26
+ abstract supportsRandomAccess(): boolean;
26
27
  /**
27
28
  * Read buffer from tokenizer
28
29
  * @param buffer - Target buffer to fill with data read from the tokenizer-stream
@@ -9,12 +9,11 @@ export class AbstractTokenizer {
9
9
  * @protected
10
10
  */
11
11
  constructor(options) {
12
+ this.numBuffer = new Uint8Array(8);
12
13
  /**
13
14
  * Tokenizer-stream position
14
15
  */
15
16
  this.position = 0;
16
- this.numBuffer = new Uint8Array(8);
17
- this.fileInfo = options?.fileInfo ?? {};
18
17
  this.onClose = options?.onClose;
19
18
  if (options?.abortSignal) {
20
19
  options.abortSignal.addEventListener('abort', () => {
@@ -1,7 +1,8 @@
1
- import type { ITokenizerOptions, IReadChunkOptions } from './types.js';
1
+ import type { ITokenizerOptions, IReadChunkOptions, IRandomAccessFileInfo, IRandomAccessTokenizer } from './types.js';
2
2
  import { AbstractTokenizer } from './AbstractTokenizer.js';
3
- export declare class BufferTokenizer extends AbstractTokenizer {
3
+ export declare class BufferTokenizer extends AbstractTokenizer implements IRandomAccessTokenizer {
4
4
  private uint8Array;
5
+ fileInfo: IRandomAccessFileInfo;
5
6
  /**
6
7
  * Construct BufferTokenizer
7
8
  * @param uint8Array - Uint8Array to tokenize
@@ -23,4 +24,6 @@ export declare class BufferTokenizer extends AbstractTokenizer {
23
24
  */
24
25
  peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
25
26
  close(): Promise<void>;
27
+ supportsRandomAccess(): boolean;
28
+ setPosition(position: number): void;
26
29
  }
@@ -9,7 +9,7 @@ export class BufferTokenizer extends AbstractTokenizer {
9
9
  constructor(uint8Array, options) {
10
10
  super(options);
11
11
  this.uint8Array = uint8Array;
12
- this.fileInfo.size = this.fileInfo.size ? this.fileInfo.size : uint8Array.length;
12
+ this.fileInfo = { ...options?.fileInfo ?? {}, ...{ size: uint8Array.length } };
13
13
  }
14
14
  /**
15
15
  * Read buffer from tokenizer
@@ -46,4 +46,10 @@ export class BufferTokenizer extends AbstractTokenizer {
46
46
  close() {
47
47
  return super.close();
48
48
  }
49
+ supportsRandomAccess() {
50
+ return true;
51
+ }
52
+ setPosition(position) {
53
+ this.position = position;
54
+ }
49
55
  }
@@ -1,9 +1,21 @@
1
1
  import { AbstractTokenizer } from './AbstractTokenizer.js';
2
- import type { IReadChunkOptions, ITokenizerOptions } from './types.js';
2
+ import type { IRandomAccessTokenizer, IRandomAccessFileInfo, IReadChunkOptions, ITokenizerOptions } from './types.js';
3
3
  import { type FileHandle } from 'node:fs/promises';
4
- export declare class FileTokenizer extends AbstractTokenizer {
4
+ interface IFileTokenizerOptions extends ITokenizerOptions {
5
+ /**
6
+ * Pass additional file information to the tokenizer
7
+ */
8
+ fileInfo: IRandomAccessFileInfo;
9
+ }
10
+ export declare class FileTokenizer extends AbstractTokenizer implements IRandomAccessTokenizer {
5
11
  private fileHandle;
6
- constructor(fileHandle: FileHandle, options: ITokenizerOptions);
12
+ fileInfo: IRandomAccessFileInfo;
13
+ /**
14
+ * Create tokenizer from provided file path
15
+ * @param sourceFilePath File path
16
+ */
17
+ static fromFile(sourceFilePath: string): Promise<FileTokenizer>;
18
+ protected constructor(fileHandle: FileHandle, options: IFileTokenizerOptions);
7
19
  /**
8
20
  * Read buffer from file
9
21
  * @param uint8Array - Uint8Array to write result to
@@ -19,5 +31,7 @@ export declare class FileTokenizer extends AbstractTokenizer {
19
31
  */
20
32
  peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
21
33
  close(): Promise<void>;
34
+ setPosition(position: number): void;
35
+ supportsRandomAccess(): boolean;
22
36
  }
23
- export declare function fromFile(sourceFilePath: string): Promise<FileTokenizer>;
37
+ export {};
@@ -2,9 +2,19 @@ import { AbstractTokenizer } from './AbstractTokenizer.js';
2
2
  import { EndOfStreamError } from 'peek-readable';
3
3
  import { open as fsOpen } from 'node:fs/promises';
4
4
  export class FileTokenizer extends AbstractTokenizer {
5
+ /**
6
+ * Create tokenizer from provided file path
7
+ * @param sourceFilePath File path
8
+ */
9
+ static async fromFile(sourceFilePath) {
10
+ const fileHandle = await fsOpen(sourceFilePath, 'r');
11
+ const stat = await fileHandle.stat();
12
+ return new FileTokenizer(fileHandle, { fileInfo: { path: sourceFilePath, size: stat.size } });
13
+ }
5
14
  constructor(fileHandle, options) {
6
15
  super(options);
7
16
  this.fileHandle = fileHandle;
17
+ this.fileInfo = options.fileInfo;
8
18
  }
9
19
  /**
10
20
  * Read buffer from file
@@ -42,9 +52,10 @@ export class FileTokenizer extends AbstractTokenizer {
42
52
  await this.fileHandle.close();
43
53
  return super.close();
44
54
  }
45
- }
46
- export async function fromFile(sourceFilePath) {
47
- const fileHandle = await fsOpen(sourceFilePath, 'r');
48
- const stat = await fileHandle.stat();
49
- return new FileTokenizer(fileHandle, { fileInfo: { path: sourceFilePath, size: stat.size } });
55
+ setPosition(position) {
56
+ this.position = position;
57
+ }
58
+ supportsRandomAccess() {
59
+ return true;
60
+ }
50
61
  }
@@ -1,8 +1,9 @@
1
1
  import { AbstractTokenizer } from './AbstractTokenizer.js';
2
2
  import { type IStreamReader } from 'peek-readable';
3
- import type { IReadChunkOptions, ITokenizerOptions } from './types.js';
3
+ import type { IFileInfo, IReadChunkOptions, ITokenizerOptions } from './types.js';
4
4
  export declare class ReadStreamTokenizer extends AbstractTokenizer {
5
5
  private streamReader;
6
+ fileInfo: IFileInfo;
6
7
  /**
7
8
  * Constructor
8
9
  * @param streamReader stream-reader to read from
@@ -25,4 +26,5 @@ export declare class ReadStreamTokenizer extends AbstractTokenizer {
25
26
  peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
26
27
  ignore(length: number): Promise<number>;
27
28
  abort(): Promise<void>;
29
+ supportsRandomAccess(): boolean;
28
30
  }
@@ -10,6 +10,7 @@ export class ReadStreamTokenizer extends AbstractTokenizer {
10
10
  constructor(streamReader, options) {
11
11
  super(options);
12
12
  this.streamReader = streamReader;
13
+ this.fileInfo = options?.fileInfo ?? {};
13
14
  }
14
15
  /**
15
16
  * Read buffer from tokenizer
@@ -92,4 +93,7 @@ export class ReadStreamTokenizer extends AbstractTokenizer {
92
93
  abort() {
93
94
  return this.streamReader.abort();
94
95
  }
96
+ supportsRandomAccess() {
97
+ return false;
98
+ }
95
99
  }
package/lib/core.d.ts CHANGED
@@ -4,7 +4,7 @@ import { ReadStreamTokenizer } from './ReadStreamTokenizer.js';
4
4
  import { BufferTokenizer } from './BufferTokenizer.js';
5
5
  import type { ITokenizerOptions } from './types.js';
6
6
  export { EndOfStreamError, type AnyWebByteStream } from 'peek-readable';
7
- export type { ITokenizer, IFileInfo, ITokenizerOptions, IReadChunkOptions, OnClose } from './types.js';
7
+ export type { ITokenizer, IRandomAccessTokenizer, IFileInfo, IRandomAccessFileInfo, ITokenizerOptions, IReadChunkOptions, OnClose } from './types.js';
8
8
  export type { IToken, IGetToken } from '@tokenizer/token';
9
9
  export { AbstractTokenizer } from './AbstractTokenizer.js';
10
10
  /**
package/lib/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import type { Readable } from 'node:stream';
2
2
  import type { ReadStreamTokenizer } from './ReadStreamTokenizer.js';
3
3
  import { type ITokenizerOptions } from './core.js';
4
- export { fromFile } from './FileTokenizer.js';
4
+ import { FileTokenizer } from "./FileTokenizer.js";
5
+ export { FileTokenizer } from './FileTokenizer.js';
5
6
  export * from './core.js';
6
7
  export type { IToken, IGetToken } from '@tokenizer/token';
7
8
  /**
@@ -12,3 +13,4 @@ export type { IToken, IGetToken } from '@tokenizer/token';
12
13
  * @returns Tokenizer
13
14
  */
14
15
  export declare function fromStream(stream: Readable, options?: ITokenizerOptions): Promise<ReadStreamTokenizer>;
16
+ export declare const fromFile: typeof FileTokenizer.fromFile;
package/lib/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { stat as fsStat } from 'node:fs/promises';
2
2
  import { fromStream as coreFromStream } from './core.js';
3
- export { fromFile } from './FileTokenizer.js';
3
+ import { FileTokenizer } from "./FileTokenizer.js";
4
+ export { FileTokenizer } from './FileTokenizer.js';
4
5
  export * from './core.js';
5
6
  /**
6
7
  * Construct ReadStreamTokenizer from given Stream.
@@ -10,12 +11,12 @@ export * from './core.js';
10
11
  * @returns Tokenizer
11
12
  */
12
13
  export async function fromStream(stream, options) {
13
- const augmentedOptions = options ?? {};
14
- augmentedOptions.fileInfo = augmentedOptions.fileInfo ?? {};
14
+ const rst = coreFromStream(stream, options);
15
15
  if (stream.path) {
16
16
  const stat = await fsStat(stream.path);
17
- augmentedOptions.fileInfo.path = stream.path;
18
- augmentedOptions.fileInfo.size = stat.size;
17
+ rst.fileInfo.path = stream.path;
18
+ rst.fileInfo.size = stat.size;
19
19
  }
20
- return coreFromStream(stream, augmentedOptions);
20
+ return rst;
21
21
  }
22
+ export const fromFile = FileTokenizer.fromFile;
package/lib/types.d.ts CHANGED
@@ -17,6 +17,12 @@ export interface IFileInfo {
17
17
  */
18
18
  url?: string;
19
19
  }
20
+ export interface IRandomAccessFileInfo extends IFileInfo {
21
+ /**
22
+ * File size in bytes
23
+ */
24
+ size: number;
25
+ }
20
26
  export interface IReadChunkOptions {
21
27
  /**
22
28
  * The offset in the buffer to start writing at; default is 0
@@ -29,14 +35,25 @@ export interface IReadChunkOptions {
29
35
  /**
30
36
  * Position where to begin reading from the file.
31
37
  * Default it is `tokenizer.position`.
32
- * Position may not be less then `tokenizer.position`.
38
+ * Position may not be less than `tokenizer.position`, unless `supportsRandomAccess()` returns `true`.
33
39
  */
34
40
  position?: number;
35
41
  /**
36
- * If set, will not throw an EOF error if not all of the requested data could be read
42
+ * If set, will not throw an EOF error if not all off the requested data could be read
37
43
  */
38
44
  mayBeLess?: boolean;
39
45
  }
46
+ export interface IRandomAccessTokenizer extends ITokenizer {
47
+ /**
48
+ * Provide access to information of the underlying information stream or file.
49
+ */
50
+ fileInfo: IRandomAccessFileInfo;
51
+ /**
52
+ * Change the position (offset) of the tokenizer
53
+ * @param position New position
54
+ */
55
+ setPosition(position: number): void;
56
+ }
40
57
  /**
41
58
  * The tokenizer allows us to read or peek from the tokenizer-stream.
42
59
  * The tokenizer-stream is an abstraction of a stream, file or Buffer.
@@ -45,11 +62,11 @@ export interface ITokenizer {
45
62
  /**
46
63
  * Provide access to information of the underlying information stream or file.
47
64
  */
48
- fileInfo: IFileInfo;
65
+ readonly fileInfo: IFileInfo;
49
66
  /**
50
67
  * Offset in bytes (= number of bytes read) since beginning of file or stream
51
68
  */
52
- position: number;
69
+ readonly position: number;
53
70
  /**
54
71
  * Peek (read ahead) buffer from tokenizer
55
72
  * @param buffer - Target buffer to fill with data peek from the tokenizer-stream
@@ -103,6 +120,10 @@ export interface ITokenizer {
103
120
  * Abort pending asynchronous operations
104
121
  */
105
122
  abort(): Promise<void>;
123
+ /**
124
+ * Returns true when the underlying file supports random access
125
+ */
126
+ supportsRandomAccess(): boolean;
106
127
  }
107
128
  export type OnClose = () => Promise<void>;
108
129
  export interface ITokenizerOptions {
package/package.json CHANGED
@@ -1,91 +1,91 @@
1
- {
2
- "name": "strtok3",
3
- "version": "9.0.1",
4
- "description": "A promise based streaming tokenizer",
5
- "author": {
6
- "name": "Borewit",
7
- "url": "https://github.com/Borewit"
8
- },
9
- "funding": {
10
- "type": "github",
11
- "url": "https://github.com/sponsors/Borewit"
12
- },
13
- "scripts": {
14
- "clean": "del-cli 'lib/**/*.js' 'lib/**/*.js.map' 'lib/**/*.d.ts' 'test/**/*.js' 'test/**/*.js.map'",
15
- "compile-src": "tsc -p lib",
16
- "compile-test": "tsc -p test",
17
- "compile": "yarn run compile-src && yarn run compile-test",
18
- "build": "yarn run clean && yarn run compile",
19
- "eslint": "eslint lib test",
20
- "lint-md": "remark -u preset-lint-recommended .",
21
- "lint-ts": "biome check",
22
- "lint": "yarn run lint-md && yarn run lint-ts",
23
- "fix": "yarn run biome lint --write",
24
- "test": "mocha",
25
- "test-coverage": "c8 yarn run test",
26
- "send-codacy": "c8 report --reporter=text-lcov | codacy-coverage",
27
- "start": "yarn run compile && yarn run lint && yarn run cover-test"
28
- },
29
- "engines": {
30
- "node": ">=16"
31
- },
32
- "repository": {
33
- "type": "git",
34
- "url": "https://github.com/Borewit/strtok3.git"
35
- },
36
- "license": "MIT",
37
- "type": "module",
38
- "exports": {
39
- ".": {
40
- "node": "./lib/index.js",
41
- "default": "./lib/core.js"
42
- },
43
- "./core": "./lib/core.js"
44
- },
45
- "types": "lib/index.d.ts",
46
- "files": [
47
- "lib/**/*.js",
48
- "lib/**/*.d.ts"
49
- ],
50
- "bugs": {
51
- "url": "https://github.com/Borewit/strtok3/issues"
52
- },
53
- "devDependencies": {
54
- "@biomejs/biome": "^1.9.3",
55
- "@types/chai": "^5.0.0",
56
- "@types/chai-as-promised": "^8.0.1",
57
- "@types/debug": "^4.1.12",
58
- "@types/mocha": "^10.0.9",
59
- "@types/node": "^22.7.5",
60
- "c8": "^10.1.2",
61
- "chai": "^5.1.1",
62
- "chai-as-promised": "^8.0.0",
63
- "del-cli": "^6.0.0",
64
- "mocha": "^10.7.3",
65
- "remark-cli": "^12.0.1",
66
- "remark-preset-lint-recommended": "^7.0.0",
67
- "token-types": "^6.0.0",
68
- "ts-node": "^10.9.2",
69
- "typescript": "^5.6.3",
70
- "uint8array-extras": "^1.4.0"
71
- },
72
- "dependencies": {
73
- "@tokenizer/token": "^0.3.0",
74
- "peek-readable": "^5.3.1"
75
- },
76
- "keywords": [
77
- "tokenizer",
78
- "reader",
79
- "token",
80
- "async",
81
- "promise",
82
- "parser",
83
- "decoder",
84
- "binary",
85
- "endian",
86
- "uint",
87
- "stream",
88
- "streaming"
89
- ],
90
- "packageManager": "yarn@4.3.1"
91
- }
1
+ {
2
+ "name": "strtok3",
3
+ "version": "9.1.1",
4
+ "description": "A promise based streaming tokenizer",
5
+ "author": {
6
+ "name": "Borewit",
7
+ "url": "https://github.com/Borewit"
8
+ },
9
+ "funding": {
10
+ "type": "github",
11
+ "url": "https://github.com/sponsors/Borewit"
12
+ },
13
+ "scripts": {
14
+ "clean": "del-cli 'lib/**/*.js' 'lib/**/*.js.map' 'lib/**/*.d.ts' 'test/**/*.js' 'test/**/*.js.map'",
15
+ "compile-src": "tsc -p lib",
16
+ "compile-test": "tsc -p test",
17
+ "compile": "yarn run compile-src && yarn run compile-test",
18
+ "build": "yarn run clean && yarn run compile",
19
+ "eslint": "eslint lib test",
20
+ "lint-md": "remark -u preset-lint-recommended .",
21
+ "lint-ts": "biome check",
22
+ "lint": "yarn run lint-md && yarn run lint-ts",
23
+ "fix": "yarn run biome lint --write",
24
+ "test": "mocha",
25
+ "test-coverage": "c8 yarn run test",
26
+ "send-codacy": "c8 report --reporter=text-lcov | codacy-coverage",
27
+ "start": "yarn run compile && yarn run lint && yarn run cover-test"
28
+ },
29
+ "engines": {
30
+ "node": ">=16"
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "https://github.com/Borewit/strtok3.git"
35
+ },
36
+ "license": "MIT",
37
+ "type": "module",
38
+ "exports": {
39
+ ".": {
40
+ "node": "./lib/index.js",
41
+ "default": "./lib/core.js"
42
+ },
43
+ "./core": "./lib/core.js"
44
+ },
45
+ "types": "lib/index.d.ts",
46
+ "files": [
47
+ "lib/**/*.js",
48
+ "lib/**/*.d.ts"
49
+ ],
50
+ "bugs": {
51
+ "url": "https://github.com/Borewit/strtok3/issues"
52
+ },
53
+ "devDependencies": {
54
+ "@biomejs/biome": "^1.9.4",
55
+ "@types/chai": "^5.0.1",
56
+ "@types/chai-as-promised": "^8.0.1",
57
+ "@types/debug": "^4.1.12",
58
+ "@types/mocha": "^10.0.9",
59
+ "@types/node": "^22.9.0",
60
+ "c8": "^10.1.2",
61
+ "chai": "^5.1.2",
62
+ "chai-as-promised": "^8.0.1",
63
+ "del-cli": "^6.0.0",
64
+ "mocha": "^10.8.2",
65
+ "remark-cli": "^12.0.1",
66
+ "remark-preset-lint-recommended": "^7.0.0",
67
+ "token-types": "^6.0.0",
68
+ "ts-node": "^10.9.2",
69
+ "typescript": "^5.7.2",
70
+ "uint8array-extras": "^1.4.0"
71
+ },
72
+ "dependencies": {
73
+ "@tokenizer/token": "^0.3.0",
74
+ "peek-readable": "^5.3.1"
75
+ },
76
+ "keywords": [
77
+ "tokenizer",
78
+ "reader",
79
+ "token",
80
+ "async",
81
+ "promise",
82
+ "parser",
83
+ "decoder",
84
+ "binary",
85
+ "endian",
86
+ "uint",
87
+ "stream",
88
+ "streaming"
89
+ ],
90
+ "packageManager": "yarn@4.3.1"
91
+ }