strtok3 7.1.1 → 8.0.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.
@@ -1,5 +1,5 @@
1
- import { ITokenizer, IFileInfo, IReadChunkOptions } from './types.js';
2
- import { IGetToken, IToken } from '@tokenizer/token';
1
+ import type { ITokenizer, IFileInfo, IReadChunkOptions, ITokenizerOptions } from './types.js';
2
+ import type { IGetToken, IToken } from '@tokenizer/token';
3
3
  interface INormalizedReadChunkOptions extends IReadChunkOptions {
4
4
  offset: number;
5
5
  length: number;
@@ -11,7 +11,13 @@ interface INormalizedReadChunkOptions extends IReadChunkOptions {
11
11
  */
12
12
  export declare abstract class AbstractTokenizer implements ITokenizer {
13
13
  fileInfo: IFileInfo;
14
- protected constructor(fileInfo?: IFileInfo);
14
+ private onClose?;
15
+ /**
16
+ * Constructor
17
+ * @param options Tokenizer options
18
+ * @protected
19
+ */
20
+ protected constructor(options?: ITokenizerOptions);
15
21
  /**
16
22
  * Tokenizer-stream position
17
23
  */
@@ -26,7 +32,7 @@ export declare abstract class AbstractTokenizer implements ITokenizer {
26
32
  abstract readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
27
33
  /**
28
34
  * Peek (read ahead) buffer from tokenizer
29
- * @param uint8Array- Target buffer to fill with data peek from the tokenizer-stream
35
+ * @param uint8Array - Target buffer to fill with data peeked from the tokenizer-stream
30
36
  * @param options - Peek behaviour options
31
37
  * @returns Promise with number of bytes read
32
38
  */
@@ -3,13 +3,20 @@ import { EndOfStreamError } from 'peek-readable';
3
3
  * Core tokenizer
4
4
  */
5
5
  export class AbstractTokenizer {
6
- constructor(fileInfo) {
6
+ /**
7
+ * Constructor
8
+ * @param options Tokenizer options
9
+ * @protected
10
+ */
11
+ constructor(options) {
12
+ var _a;
7
13
  /**
8
14
  * Tokenizer-stream position
9
15
  */
10
16
  this.position = 0;
11
17
  this.numBuffer = new Uint8Array(8);
12
- this.fileInfo = fileInfo ? fileInfo : {};
18
+ this.fileInfo = (_a = options === null || options === void 0 ? void 0 : options.fileInfo) !== null && _a !== void 0 ? _a : {};
19
+ this.onClose = options === null || options === void 0 ? void 0 : options.onClose;
13
20
  }
14
21
  /**
15
22
  * Read a token from the tokenizer-stream
@@ -76,7 +83,8 @@ export class AbstractTokenizer {
76
83
  return length;
77
84
  }
78
85
  async close() {
79
- // empty
86
+ var _a;
87
+ await ((_a = this.onClose) === null || _a === void 0 ? void 0 : _a.call(this));
80
88
  }
81
89
  normalizeOptions(uint8Array, options) {
82
90
  if (options && options.position !== undefined && options.position < this.position) {
@@ -1,13 +1,13 @@
1
- import { IFileInfo, IReadChunkOptions } from './types.js';
1
+ import { ITokenizerOptions, IReadChunkOptions } from './types.js';
2
2
  import { AbstractTokenizer } from './AbstractTokenizer.js';
3
3
  export declare class BufferTokenizer extends AbstractTokenizer {
4
4
  private uint8Array;
5
5
  /**
6
6
  * Construct BufferTokenizer
7
7
  * @param uint8Array - Uint8Array to tokenize
8
- * @param fileInfo - Pass additional file information to the tokenizer
8
+ * @param options Tokenizer options
9
9
  */
10
- constructor(uint8Array: Uint8Array, fileInfo?: IFileInfo);
10
+ constructor(uint8Array: Uint8Array, options?: ITokenizerOptions);
11
11
  /**
12
12
  * Read buffer from tokenizer
13
13
  * @param uint8Array - Uint8Array to tokenize
@@ -4,10 +4,10 @@ export class BufferTokenizer extends AbstractTokenizer {
4
4
  /**
5
5
  * Construct BufferTokenizer
6
6
  * @param uint8Array - Uint8Array to tokenize
7
- * @param fileInfo - Pass additional file information to the tokenizer
7
+ * @param options Tokenizer options
8
8
  */
9
- constructor(uint8Array, fileInfo) {
10
- super(fileInfo);
9
+ constructor(uint8Array, options) {
10
+ super(options);
11
11
  this.uint8Array = uint8Array;
12
12
  this.fileInfo.size = this.fileInfo.size ? this.fileInfo.size : uint8Array.length;
13
13
  }
@@ -45,7 +45,7 @@ export class BufferTokenizer extends AbstractTokenizer {
45
45
  return bytes2read;
46
46
  }
47
47
  }
48
- async close() {
49
- // empty
48
+ close() {
49
+ return super.close();
50
50
  }
51
51
  }
@@ -1,9 +1,9 @@
1
1
  import { AbstractTokenizer } from './AbstractTokenizer.js';
2
- import { IFileInfo, IReadChunkOptions } from './types.js';
2
+ import type { IReadChunkOptions, ITokenizerOptions } from './types.js';
3
3
  import { FileHandle } from 'node:fs/promises';
4
4
  export declare class FileTokenizer extends AbstractTokenizer {
5
5
  private fileHandle;
6
- constructor(fileHandle: FileHandle, fileInfo: IFileInfo);
6
+ constructor(fileHandle: FileHandle, options: ITokenizerOptions);
7
7
  /**
8
8
  * Read buffer from file
9
9
  * @param uint8Array - Uint8Array to write result to
@@ -2,8 +2,8 @@ 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
- constructor(fileHandle, fileInfo) {
6
- super(fileInfo);
5
+ constructor(fileHandle, options) {
6
+ super(options);
7
7
  this.fileHandle = fileHandle;
8
8
  }
9
9
  /**
@@ -37,11 +37,12 @@ export class FileTokenizer extends AbstractTokenizer {
37
37
  return res.bytesRead;
38
38
  }
39
39
  async close() {
40
- return this.fileHandle.close();
40
+ await this.fileHandle.close();
41
+ return super.close();
41
42
  }
42
43
  }
43
44
  export async function fromFile(sourceFilePath) {
44
45
  const fileHandle = await fsOpen(sourceFilePath, 'r');
45
46
  const stat = await fileHandle.stat();
46
- return new FileTokenizer(fileHandle, { path: sourceFilePath, size: stat.size });
47
+ return new FileTokenizer(fileHandle, { fileInfo: { path: sourceFilePath, size: stat.size } });
47
48
  }
@@ -1,14 +1,14 @@
1
1
  import { AbstractTokenizer } from './AbstractTokenizer.js';
2
- import { IStreamReader } from 'peek-readable';
3
- import { IFileInfo, IReadChunkOptions } from './types.js';
2
+ import { type IStreamReader } from 'peek-readable';
3
+ import type { IReadChunkOptions, ITokenizerOptions } from './types.js';
4
4
  export declare class ReadStreamTokenizer extends AbstractTokenizer {
5
5
  private streamReader;
6
- constructor(streamReader: IStreamReader, fileInfo?: IFileInfo);
7
6
  /**
8
- * Get file information, an HTTP-client may implement this doing a HEAD request
9
- * @return Promise with file information
7
+ * Constructor
8
+ * @param streamReader stream-reader to read from
9
+ * @param options Tokenizer options
10
10
  */
11
- getFileInfo(): Promise<IFileInfo>;
11
+ constructor(streamReader: IStreamReader, options?: ITokenizerOptions);
12
12
  /**
13
13
  * Read buffer from tokenizer
14
14
  * @param uint8Array - Target Uint8Array to fill with data read from the tokenizer-stream
@@ -2,16 +2,14 @@ import { AbstractTokenizer } from './AbstractTokenizer.js';
2
2
  import { EndOfStreamError } from 'peek-readable';
3
3
  const maxBufferSize = 256000;
4
4
  export class ReadStreamTokenizer extends AbstractTokenizer {
5
- constructor(streamReader, fileInfo) {
6
- super(fileInfo);
7
- this.streamReader = streamReader;
8
- }
9
5
  /**
10
- * Get file information, an HTTP-client may implement this doing a HEAD request
11
- * @return Promise with file information
6
+ * Constructor
7
+ * @param streamReader stream-reader to read from
8
+ * @param options Tokenizer options
12
9
  */
13
- async getFileInfo() {
14
- return this.fileInfo;
10
+ constructor(streamReader, options) {
11
+ super(options);
12
+ this.streamReader = streamReader;
15
13
  }
16
14
  /**
17
15
  * Read buffer from tokenizer
package/lib/core.d.ts CHANGED
@@ -1,31 +1,32 @@
1
1
  import { ReadStreamTokenizer } from './ReadStreamTokenizer.js';
2
2
  import { BufferTokenizer } from './BufferTokenizer.js';
3
- import { IFileInfo } from './types.js';
3
+ import type { ITokenizerOptions } from './types.js';
4
4
  import type { ReadableStream } from 'node:stream/web';
5
5
  import type { Readable } from 'node:stream';
6
6
  export { EndOfStreamError } from 'peek-readable';
7
- export { ITokenizer, IFileInfo } from './types.js';
8
- export { IToken, IGetToken } from '@tokenizer/token';
7
+ export type { ITokenizer, IFileInfo, ITokenizerOptions, IReadChunkOptions, OnClose } from './types.js';
8
+ export type { IToken, IGetToken } from '@tokenizer/token';
9
+ export { AbstractTokenizer } from './AbstractTokenizer.js';
9
10
  /**
10
11
  * Construct ReadStreamTokenizer from given Stream.
11
12
  * Will set fileSize, if provided given Stream has set the .path property/
12
13
  * @param stream - Read from Node.js Stream.Readable
13
- * @param fileInfo - Pass the file information, like size and MIME-type of the corresponding stream.
14
+ * @param options - Tokenizer options
14
15
  * @returns ReadStreamTokenizer
15
16
  */
16
- export declare function fromStream(stream: Readable, fileInfo?: IFileInfo): ReadStreamTokenizer;
17
+ export declare function fromStream(stream: Readable, options?: ITokenizerOptions): ReadStreamTokenizer;
17
18
  /**
18
19
  * Construct ReadStreamTokenizer from given ReadableStream (WebStream API).
19
20
  * Will set fileSize, if provided given Stream has set the .path property/
20
21
  * @param webStream - Read from Node.js Stream.Readable
21
- * @param fileInfo - Pass the file information, like size and MIME-type of the corresponding stream.
22
+ * @param options - Tokenizer options
22
23
  * @returns ReadStreamTokenizer
23
24
  */
24
- export declare function fromWebStream(webStream: ReadableStream<Uint8Array>, fileInfo?: IFileInfo): ReadStreamTokenizer;
25
+ export declare function fromWebStream(webStream: ReadableStream<Uint8Array>, options?: ITokenizerOptions): ReadStreamTokenizer;
25
26
  /**
26
27
  * Construct ReadStreamTokenizer from given Buffer.
27
28
  * @param uint8Array - Uint8Array to tokenize
28
- * @param fileInfo - Pass additional file information to the tokenizer
29
+ * @param options - Tokenizer options
29
30
  * @returns BufferTokenizer
30
31
  */
31
- export declare function fromBuffer(uint8Array: Uint8Array, fileInfo?: IFileInfo): BufferTokenizer;
32
+ export declare function fromBuffer(uint8Array: Uint8Array, options?: ITokenizerOptions): BufferTokenizer;
package/lib/core.js CHANGED
@@ -2,34 +2,33 @@ import { ReadStreamTokenizer } from './ReadStreamTokenizer.js';
2
2
  import { BufferTokenizer } from './BufferTokenizer.js';
3
3
  import { StreamReader, WebStreamReader } from 'peek-readable';
4
4
  export { EndOfStreamError } from 'peek-readable';
5
+ export { AbstractTokenizer } from './AbstractTokenizer.js';
5
6
  /**
6
7
  * Construct ReadStreamTokenizer from given Stream.
7
8
  * Will set fileSize, if provided given Stream has set the .path property/
8
9
  * @param stream - Read from Node.js Stream.Readable
9
- * @param fileInfo - Pass the file information, like size and MIME-type of the corresponding stream.
10
+ * @param options - Tokenizer options
10
11
  * @returns ReadStreamTokenizer
11
12
  */
12
- export function fromStream(stream, fileInfo) {
13
- fileInfo = fileInfo ? fileInfo : {};
14
- return new ReadStreamTokenizer(new StreamReader(stream), fileInfo);
13
+ export function fromStream(stream, options) {
14
+ return new ReadStreamTokenizer(new StreamReader(stream), options);
15
15
  }
16
16
  /**
17
17
  * Construct ReadStreamTokenizer from given ReadableStream (WebStream API).
18
18
  * Will set fileSize, if provided given Stream has set the .path property/
19
19
  * @param webStream - Read from Node.js Stream.Readable
20
- * @param fileInfo - Pass the file information, like size and MIME-type of the corresponding stream.
20
+ * @param options - Tokenizer options
21
21
  * @returns ReadStreamTokenizer
22
22
  */
23
- export function fromWebStream(webStream, fileInfo) {
24
- fileInfo = fileInfo ? fileInfo : {};
25
- return new ReadStreamTokenizer(new WebStreamReader(webStream), fileInfo);
23
+ export function fromWebStream(webStream, options) {
24
+ return new ReadStreamTokenizer(new WebStreamReader(webStream), options);
26
25
  }
27
26
  /**
28
27
  * Construct ReadStreamTokenizer from given Buffer.
29
28
  * @param uint8Array - Uint8Array to tokenize
30
- * @param fileInfo - Pass additional file information to the tokenizer
29
+ * @param options - Tokenizer options
31
30
  * @returns BufferTokenizer
32
31
  */
33
- export function fromBuffer(uint8Array, fileInfo) {
34
- return new BufferTokenizer(uint8Array, fileInfo);
32
+ export function fromBuffer(uint8Array, options) {
33
+ return new BufferTokenizer(uint8Array, options);
35
34
  }
package/lib/index.d.ts CHANGED
@@ -1,14 +1,15 @@
1
1
  import { Readable } from 'node:stream';
2
2
  import { ReadStreamTokenizer } from './ReadStreamTokenizer.js';
3
- import * as core from './core.js';
3
+ import { type ITokenizerOptions } from './core.js';
4
4
  export { fromFile } from './FileTokenizer.js';
5
- export { ITokenizer, EndOfStreamError, fromBuffer, fromWebStream, IFileInfo } from './core.js';
6
- export { IToken, IGetToken } from '@tokenizer/token';
5
+ export { EndOfStreamError, fromBuffer, fromWebStream, AbstractTokenizer } from './core.js';
6
+ export type { ITokenizer, IFileInfo, ITokenizerOptions, IReadChunkOptions, OnClose } from './core.js';
7
+ export type { IToken, IGetToken } from '@tokenizer/token';
7
8
  /**
8
9
  * Construct ReadStreamTokenizer from given Stream.
9
10
  * Will set fileSize, if provided given Stream has set the .path property.
10
11
  * @param stream - Node.js Stream.Readable
11
- * @param fileInfo - Pass additional file information to the tokenizer
12
+ * @param options - Pass additional file information to the tokenizer
12
13
  * @returns Tokenizer
13
14
  */
14
- export declare function fromStream(stream: Readable, fileInfo?: core.IFileInfo): Promise<ReadStreamTokenizer>;
15
+ export declare function fromStream(stream: Readable, options?: ITokenizerOptions): Promise<ReadStreamTokenizer>;
package/lib/index.js CHANGED
@@ -1,20 +1,22 @@
1
1
  import { stat as fsStat } from 'node:fs/promises';
2
- import * as core from './core.js';
2
+ import { fromStream as coreFromStream } from './core.js';
3
3
  export { fromFile } from './FileTokenizer.js';
4
- export { EndOfStreamError, fromBuffer, fromWebStream } from './core.js';
4
+ export { EndOfStreamError, fromBuffer, fromWebStream, AbstractTokenizer } from './core.js';
5
5
  /**
6
6
  * Construct ReadStreamTokenizer from given Stream.
7
7
  * Will set fileSize, if provided given Stream has set the .path property.
8
8
  * @param stream - Node.js Stream.Readable
9
- * @param fileInfo - Pass additional file information to the tokenizer
9
+ * @param options - Pass additional file information to the tokenizer
10
10
  * @returns Tokenizer
11
11
  */
12
- export async function fromStream(stream, fileInfo) {
13
- fileInfo = fileInfo ? fileInfo : {};
12
+ export async function fromStream(stream, options) {
13
+ var _a;
14
+ const augmentedOptions = options !== null && options !== void 0 ? options : {};
15
+ augmentedOptions.fileInfo = (_a = augmentedOptions.fileInfo) !== null && _a !== void 0 ? _a : {};
14
16
  if (stream.path) {
15
17
  const stat = await fsStat(stream.path);
16
- fileInfo.path = stream.path;
17
- fileInfo.size = stat.size;
18
+ augmentedOptions.fileInfo.path = stream.path;
19
+ augmentedOptions.fileInfo.size = stat.size;
18
20
  }
19
- return core.fromStream(stream, fileInfo);
21
+ return coreFromStream(stream, augmentedOptions);
20
22
  }
package/lib/types.d.ts CHANGED
@@ -100,3 +100,14 @@ export interface ITokenizer {
100
100
  */
101
101
  close(): Promise<void>;
102
102
  }
103
+ export type OnClose = () => Promise<void>;
104
+ export interface ITokenizerOptions {
105
+ /**
106
+ * Pass additional file information to the tokenizer
107
+ */
108
+ fileInfo?: IFileInfo;
109
+ /**
110
+ * On tokenizer close handler
111
+ */
112
+ onClose?: OnClose;
113
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "strtok3",
3
- "version": "7.1.1",
3
+ "version": "8.0.1",
4
4
  "description": "A promise based streaming tokenizer",
5
5
  "author": {
6
6
  "name": "Borewit",
@@ -11,7 +11,7 @@
11
11
  "url": "https://github.com/sponsors/Borewit"
12
12
  },
13
13
  "scripts": {
14
- "clean": "del-cli lib/**/*.js lib/**/*.js.map lib/**/*.d.ts test/**/*.js test/**/*.js.map",
14
+ "clean": "del-cli 'lib/**/*.js' 'lib/**/*.js.map' 'lib/**/*.d.ts' 'test/**/*.js' 'test/**/*.js.map'",
15
15
  "compile-src": "tsc -p lib",
16
16
  "compile-test": "tsc -p test",
17
17
  "compile": "npm run compile-src && npm run compile-test",
@@ -56,7 +56,7 @@
56
56
  "@types/chai": "^4.3.16",
57
57
  "@types/debug": "^4.1.12",
58
58
  "@types/mocha": "^10.0.7",
59
- "@types/node": "^20.14.10",
59
+ "@types/node": "^20.14.11",
60
60
  "@typescript-eslint/eslint-plugin": "^7.16.1",
61
61
  "@typescript-eslint/parser": "^7.16.1",
62
62
  "c8": "^10.1.2",
@@ -66,17 +66,17 @@
66
66
  "eslint-config-prettier": "^9.1.0",
67
67
  "eslint-import-resolver-typescript": "^3.6.1",
68
68
  "eslint-plugin-import": "^2.29.1",
69
- "eslint-plugin-jsdoc": "^48.7.0",
69
+ "eslint-plugin-jsdoc": "^48.8.3",
70
70
  "eslint-plugin-node": "^11.1.0",
71
71
  "eslint-plugin-unicorn": "^54.0.0",
72
72
  "global": "^4.4.0",
73
- "mocha": "^10.6.0",
73
+ "mocha": "^10.7.0",
74
74
  "remark-cli": "^12.0.1",
75
75
  "remark-preset-lint-recommended": "^7.0.0",
76
76
  "token-types": "^6.0.0",
77
77
  "ts-node": "^10.9.2",
78
78
  "typescript": "^5.5.3",
79
- "uint8array-extras": "^1.3.0"
79
+ "uint8array-extras": "^1.4.0"
80
80
  },
81
81
  "dependencies": {
82
82
  "@tokenizer/token": "^0.3.0",