strtok3 6.2.1 → 6.3.0

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.
package/README.md CHANGED
@@ -30,6 +30,10 @@ It can read from:
30
30
  npm install strtok3
31
31
  ```
32
32
 
33
+ ### Compatibility
34
+
35
+ NPM module is compliant with [ECMAScript 2018 (ES9)](https://en.wikipedia.org/wiki/ECMAScript#9th_Edition_%E2%80%93_ECMAScript_2018).
36
+
33
37
  ## API
34
38
 
35
39
  Use one of the methods to instantiate an [*abstract tokenizer*](#tokenizer):
@@ -48,7 +52,7 @@ All of the strtok3 methods return a [*tokenizer*](#tokenizer), either directly o
48
52
  |-----------|-----------------------|----------------------------|
49
53
  | path | Path to file (string) | Path to file to read from |
50
54
 
51
- Note that [file-information](#file-information) is automatically added.
55
+ > __Note__: that [file-information](#file-information) is automatically added.
52
56
 
53
57
  Returns, via a promise, a [*tokenizer*](#tokenizer) which can be used to parse a file.
54
58
 
@@ -131,10 +135,9 @@ If a *position* is provided to a *read* or *peek* method, is should be, at least
131
135
 
132
136
  ### Tokenizer methods
133
137
 
134
- There are to groups of methods
135
-
136
- * *read* methods: used to read a *token* of [Buffer](https://nodejs.org/api/buffer.html) from the [*tokenizer*](#tokenizer). The position of the *tokenizer-stream* will advance with the size of the token.
137
- * *peek* methods: same as the read, but it will *not* advance the pointer. It allows to read (peek) ahead.
138
+ There are two kind of methods:
139
+ 1. *read* methods: used to read a *token* of [Buffer](https://nodejs.org/api/buffer.html) from the [*tokenizer*](#tokenizer). The position of the *tokenizer-stream* will advance with the size of the token.
140
+ 2. *peek* methods: same as the read, but it will *not* advance the pointer. It allows to read (peek) ahead.
138
141
 
139
142
  #### Method `tokenizer.readBuffer()`
140
143
 
@@ -1,5 +1,11 @@
1
1
  import { ITokenizer, IFileInfo, IReadChunkOptions } from './types';
2
2
  import { IGetToken, IToken } from '@tokenizer/token';
3
+ interface INormalizedReadChunkOptions extends IReadChunkOptions {
4
+ offset: number;
5
+ length: number;
6
+ position: number;
7
+ mayBeLess?: boolean;
8
+ }
3
9
  /**
4
10
  * Core tokenizer
5
11
  */
@@ -20,11 +26,11 @@ export declare abstract class AbstractTokenizer implements ITokenizer {
20
26
  abstract readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
21
27
  /**
22
28
  * Peek (read ahead) buffer from tokenizer
23
- * @param buffer - Target buffer to fill with data peek from the tokenizer-stream
29
+ * @param uint8Array- Target buffer to fill with data peek from the tokenizer-stream
24
30
  * @param options - Peek behaviour options
25
31
  * @returns Promise with number of bytes read
26
32
  */
27
- abstract peekBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
33
+ abstract peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
28
34
  /**
29
35
  * Read a token from the tokenizer-stream
30
36
  * @param token - The token to read
@@ -53,9 +59,11 @@ export declare abstract class AbstractTokenizer implements ITokenizer {
53
59
  peekNumber(token: IToken<number>): Promise<number>;
54
60
  /**
55
61
  * Ignore number of bytes, advances the pointer in under tokenizer-stream.
56
- * @param length - Number of bytes to skip (ignore)
57
- * @return actual number of bytes ignored
62
+ * @param length - Number of bytes to ignore
63
+ * @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
58
64
  */
59
- abstract ignore(length: number): Promise<number>;
65
+ ignore(length: number): Promise<number>;
60
66
  close(): Promise<void>;
67
+ protected normalizeOptions(uint8Array: Uint8Array, options?: IReadChunkOptions): INormalizedReadChunkOptions;
61
68
  }
69
+ export {};
@@ -20,7 +20,7 @@ class AbstractTokenizer {
20
20
  * @param position - If provided, the desired position in the tokenizer-stream
21
21
  * @returns Promise with token data
22
22
  */
23
- async readToken(token, position) {
23
+ async readToken(token, position = this.position) {
24
24
  const uint8Array = Buffer.alloc(token.len);
25
25
  const len = await this.readBuffer(uint8Array, { position });
26
26
  if (len < token.len)
@@ -62,8 +62,43 @@ class AbstractTokenizer {
62
62
  throw new peek_readable_1.EndOfStreamError();
63
63
  return token.get(this.numBuffer, 0);
64
64
  }
65
+ /**
66
+ * Ignore number of bytes, advances the pointer in under tokenizer-stream.
67
+ * @param length - Number of bytes to ignore
68
+ * @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
69
+ */
70
+ async ignore(length) {
71
+ if (this.fileInfo.size !== undefined) {
72
+ const bytesLeft = this.fileInfo.size - this.position;
73
+ if (length > bytesLeft) {
74
+ this.position += bytesLeft;
75
+ return bytesLeft;
76
+ }
77
+ }
78
+ this.position += length;
79
+ return length;
80
+ }
65
81
  async close() {
66
82
  // empty
67
83
  }
84
+ normalizeOptions(uint8Array, options) {
85
+ if (options && options.position !== undefined && options.position < this.position) {
86
+ throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
87
+ }
88
+ if (options) {
89
+ return {
90
+ mayBeLess: options.mayBeLess === true,
91
+ offset: options.offset ? options.offset : 0,
92
+ length: options.length ? options.length : (uint8Array.length - (options.offset ? options.offset : 0)),
93
+ position: options.position ? options.position : this.position
94
+ };
95
+ }
96
+ return {
97
+ mayBeLess: false,
98
+ offset: 0,
99
+ length: uint8Array.length,
100
+ position: this.position
101
+ };
102
+ }
68
103
  }
69
104
  exports.AbstractTokenizer = AbstractTokenizer;
@@ -1,22 +1,20 @@
1
- import { IFileInfo, IReadChunkOptions, ITokenizer } from './types';
2
- import { IGetToken, IToken } from '@tokenizer/token';
3
- export declare class BufferTokenizer implements ITokenizer {
1
+ import { IFileInfo, IReadChunkOptions } from './types';
2
+ import { AbstractTokenizer } from './AbstractTokenizer';
3
+ export declare class BufferTokenizer extends AbstractTokenizer {
4
4
  private uint8Array;
5
- fileInfo: IFileInfo;
6
- position: number;
7
5
  /**
8
6
  * Construct BufferTokenizer
9
- * @param buffer - Buffer to tokenize
7
+ * @param uint8Array - Uint8Array to tokenize
10
8
  * @param fileInfo - Pass additional file information to the tokenizer
11
9
  */
12
10
  constructor(uint8Array: Uint8Array, fileInfo?: IFileInfo);
13
11
  /**
14
12
  * Read buffer from tokenizer
15
- * @param buffer
13
+ * @param uint8Array - Uint8Array to tokenize
16
14
  * @param options - Read behaviour options
17
15
  * @returns {Promise<number>}
18
16
  */
19
- readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
17
+ readBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
20
18
  /**
21
19
  * Peek (read ahead) buffer from tokenizer
22
20
  * @param uint8Array
@@ -24,13 +22,5 @@ export declare class BufferTokenizer implements ITokenizer {
24
22
  * @returns {Promise<number>}
25
23
  */
26
24
  peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
27
- readToken<T>(token: IGetToken<T>, position?: number): Promise<T>;
28
- peekToken<T>(token: IGetToken<T>, position?: number): Promise<T>;
29
- readNumber(token: IToken<number>): Promise<number>;
30
- peekNumber(token: IToken<number>): Promise<number>;
31
- /**
32
- * @return actual number of bytes ignored
33
- */
34
- ignore(length: number): Promise<number>;
35
25
  close(): Promise<void>;
36
26
  }
@@ -2,35 +2,34 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BufferTokenizer = void 0;
4
4
  const peek_readable_1 = require("peek-readable");
5
- class BufferTokenizer {
5
+ const AbstractTokenizer_1 = require("./AbstractTokenizer");
6
+ class BufferTokenizer extends AbstractTokenizer_1.AbstractTokenizer {
6
7
  /**
7
8
  * Construct BufferTokenizer
8
- * @param buffer - Buffer to tokenize
9
+ * @param uint8Array - Uint8Array to tokenize
9
10
  * @param fileInfo - Pass additional file information to the tokenizer
10
11
  */
11
12
  constructor(uint8Array, fileInfo) {
13
+ super(fileInfo);
12
14
  this.uint8Array = uint8Array;
13
- this.position = 0;
14
- this.fileInfo = fileInfo ? fileInfo : {};
15
15
  this.fileInfo.size = this.fileInfo.size ? this.fileInfo.size : uint8Array.length;
16
16
  }
17
17
  /**
18
18
  * Read buffer from tokenizer
19
- * @param buffer
19
+ * @param uint8Array - Uint8Array to tokenize
20
20
  * @param options - Read behaviour options
21
21
  * @returns {Promise<number>}
22
22
  */
23
- async readBuffer(buffer, options) {
23
+ async readBuffer(uint8Array, options) {
24
24
  if (options && options.position) {
25
25
  if (options.position < this.position) {
26
26
  throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
27
27
  }
28
28
  this.position = options.position;
29
29
  }
30
- return this.peekBuffer(buffer, options).then(bytesRead => {
31
- this.position += bytesRead;
32
- return bytesRead;
33
- });
30
+ const bytesRead = await this.peekBuffer(uint8Array, options);
31
+ this.position += bytesRead;
32
+ return bytesRead;
34
33
  }
35
34
  /**
36
35
  * Peek (read ahead) buffer from tokenizer
@@ -39,74 +38,16 @@ class BufferTokenizer {
39
38
  * @returns {Promise<number>}
40
39
  */
41
40
  async peekBuffer(uint8Array, options) {
42
- let offset = 0;
43
- let length = uint8Array.length;
44
- let position = this.position;
45
- if (options) {
46
- if (options.position) {
47
- if (options.position < this.position) {
48
- throw new Error('`options.position` can be less than `tokenizer.position`');
49
- }
50
- position = options.position;
51
- }
52
- if (Number.isInteger(options.length)) {
53
- length = options.length;
54
- }
55
- else {
56
- length -= options.offset || 0;
57
- }
58
- if (options.offset) {
59
- offset = options.offset;
60
- }
61
- }
62
- if (length === 0) {
63
- return Promise.resolve(0);
64
- }
65
- position = position || this.position;
66
- if (!length) {
67
- length = uint8Array.length;
68
- }
69
- const bytes2read = Math.min(this.uint8Array.length - position, length);
70
- if ((!options || !options.mayBeLess) && bytes2read < length) {
41
+ const normOptions = this.normalizeOptions(uint8Array, options);
42
+ const bytes2read = Math.min(this.uint8Array.length - normOptions.position, normOptions.length);
43
+ if ((!normOptions.mayBeLess) && bytes2read < normOptions.length) {
71
44
  throw new peek_readable_1.EndOfStreamError();
72
45
  }
73
46
  else {
74
- uint8Array.set(this.uint8Array.subarray(position, position + bytes2read), offset);
47
+ uint8Array.set(this.uint8Array.subarray(normOptions.position, normOptions.position + bytes2read), normOptions.offset);
75
48
  return bytes2read;
76
49
  }
77
50
  }
78
- async readToken(token, position) {
79
- this.position = position || this.position;
80
- try {
81
- const tv = this.peekToken(token, this.position);
82
- this.position += token.len;
83
- return tv;
84
- }
85
- catch (err) {
86
- this.position += this.uint8Array.length - position;
87
- throw err;
88
- }
89
- }
90
- async peekToken(token, position = this.position) {
91
- if (this.uint8Array.length - position < token.len) {
92
- throw new peek_readable_1.EndOfStreamError();
93
- }
94
- return token.get(this.uint8Array, position);
95
- }
96
- async readNumber(token) {
97
- return this.readToken(token);
98
- }
99
- async peekNumber(token) {
100
- return this.peekToken(token);
101
- }
102
- /**
103
- * @return actual number of bytes ignored
104
- */
105
- async ignore(length) {
106
- const bytesIgnored = Math.min(this.uint8Array.length - this.position, length);
107
- this.position += bytesIgnored;
108
- return bytesIgnored;
109
- }
110
51
  async close() {
111
52
  // empty
112
53
  }
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { AbstractTokenizer } from './AbstractTokenizer';
3
2
  import { IFileInfo, IReadChunkOptions } from './types';
4
3
  export declare class FileTokenizer extends AbstractTokenizer {
@@ -6,23 +5,18 @@ export declare class FileTokenizer extends AbstractTokenizer {
6
5
  constructor(fd: number, fileInfo: IFileInfo);
7
6
  /**
8
7
  * Read buffer from file
9
- * @param buffer
8
+ * @param uint8Array - Uint8Array to write result to
10
9
  * @param options - Read behaviour options
11
10
  * @returns Promise number of bytes read
12
11
  */
13
- readBuffer(buffer: Buffer, options?: IReadChunkOptions): Promise<number>;
12
+ readBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
14
13
  /**
15
14
  * Peek buffer from file
16
- * @param buffer
15
+ * @param uint8Array - Uint8Array (or Buffer) to write data to
17
16
  * @param options - Read behaviour options
18
17
  * @returns Promise number of bytes read
19
18
  */
20
- peekBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
21
- /**
22
- * @param length - Number of bytes to ignore
23
- * @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
24
- */
25
- ignore(length: number): Promise<number>;
19
+ peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
26
20
  close(): Promise<void>;
27
21
  }
28
22
  export declare function fromFile(sourceFilePath: string): Promise<FileTokenizer>;
@@ -11,91 +11,34 @@ class FileTokenizer extends AbstractTokenizer_1.AbstractTokenizer {
11
11
  }
12
12
  /**
13
13
  * Read buffer from file
14
- * @param buffer
14
+ * @param uint8Array - Uint8Array to write result to
15
15
  * @param options - Read behaviour options
16
16
  * @returns Promise number of bytes read
17
17
  */
18
- async readBuffer(buffer, options) {
19
- let offset = 0;
20
- let length = buffer.length;
21
- if (options) {
22
- if (options.position) {
23
- if (options.position < this.position) {
24
- throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
25
- }
26
- this.position = options.position;
27
- }
28
- if (Number.isInteger(options.length)) {
29
- length = options.length;
30
- }
31
- else {
32
- length -= options.offset || 0;
33
- }
34
- if (options.offset) {
35
- offset = options.offset;
36
- }
37
- }
38
- if (length === 0) {
39
- return Promise.resolve(0);
40
- }
41
- const res = await fs.read(this.fd, buffer, offset, length, this.position);
18
+ async readBuffer(uint8Array, options) {
19
+ const normOptions = this.normalizeOptions(uint8Array, options);
20
+ this.position = normOptions.position;
21
+ const res = await fs.read(this.fd, uint8Array, normOptions.offset, normOptions.length, normOptions.position);
42
22
  this.position += res.bytesRead;
43
- if (res.bytesRead < length && (!options || !options.mayBeLess)) {
23
+ if (res.bytesRead < normOptions.length && (!options || !options.mayBeLess)) {
44
24
  throw new peek_readable_1.EndOfStreamError();
45
25
  }
46
26
  return res.bytesRead;
47
27
  }
48
28
  /**
49
29
  * Peek buffer from file
50
- * @param buffer
30
+ * @param uint8Array - Uint8Array (or Buffer) to write data to
51
31
  * @param options - Read behaviour options
52
32
  * @returns Promise number of bytes read
53
33
  */
54
- async peekBuffer(buffer, options) {
55
- let offset = 0;
56
- let length = buffer.length;
57
- let position = this.position;
58
- if (options) {
59
- if (options.position) {
60
- if (options.position < this.position) {
61
- throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
62
- }
63
- position = options.position;
64
- }
65
- if (Number.isInteger(options.length)) {
66
- length = options.length;
67
- }
68
- else {
69
- length -= options.offset || 0;
70
- }
71
- if (options.offset) {
72
- offset = options.offset;
73
- }
74
- }
75
- if (length === 0) {
76
- return Promise.resolve(0);
77
- }
78
- const res = await fs.read(this.fd, buffer, offset, length, position);
79
- if ((!options || !options.mayBeLess) && res.bytesRead < length) {
34
+ async peekBuffer(uint8Array, options) {
35
+ const normOptions = this.normalizeOptions(uint8Array, options);
36
+ const res = await fs.read(this.fd, uint8Array, normOptions.offset, normOptions.length, normOptions.position);
37
+ if ((!normOptions.mayBeLess) && res.bytesRead < normOptions.length) {
80
38
  throw new peek_readable_1.EndOfStreamError();
81
39
  }
82
40
  return res.bytesRead;
83
41
  }
84
- /**
85
- * @param length - Number of bytes to ignore
86
- * @return resolves the number of bytes ignored, equals length if this available, otherwise the number of bytes available
87
- */
88
- async ignore(length) {
89
- const bytesLeft = this.fileInfo.size - this.position;
90
- if (length <= bytesLeft) {
91
- this.position += length;
92
- return length;
93
- }
94
- else {
95
- this.position += bytesLeft;
96
- return bytesLeft;
97
- }
98
- }
99
42
  async close() {
100
43
  return fs.close(this.fd);
101
44
  }
@@ -11,7 +11,7 @@ export declare const pathExists: typeof fs.existsSync;
11
11
  export declare const createReadStream: typeof fs.createReadStream;
12
12
  export declare function stat(path: fs.PathLike): Promise<fs.Stats>;
13
13
  export declare function close(fd: number): Promise<void>;
14
- export declare function open(path: fs.PathLike, mode?: string): Promise<number>;
14
+ export declare function open(path: fs.PathLike, mode: fs.Mode): Promise<number>;
15
15
  export declare function read(fd: number, buffer: Uint8Array, offset: number, length: number, position: number): Promise<IReadResult>;
16
16
  export declare function writeFile(path: fs.PathLike, data: Buffer | string): Promise<void>;
17
17
  export declare function writeFileSync(path: fs.PathLike, data: Buffer | string): void;
@@ -1,10 +1,10 @@
1
1
  /// <reference types="node" />
2
2
  import { AbstractTokenizer } from './AbstractTokenizer';
3
- import * as Stream from 'stream';
3
+ import { Readable } from 'stream';
4
4
  import { IFileInfo, IReadChunkOptions } from './types';
5
5
  export declare class ReadStreamTokenizer extends AbstractTokenizer {
6
6
  private streamReader;
7
- constructor(stream: Stream.Readable, fileInfo?: IFileInfo);
7
+ constructor(stream: Readable, fileInfo?: IFileInfo);
8
8
  /**
9
9
  * Get file information, an HTTP-client may implement this doing a HEAD request
10
10
  * @return Promise with file information
@@ -12,17 +12,17 @@ export declare class ReadStreamTokenizer extends AbstractTokenizer {
12
12
  getFileInfo(): Promise<IFileInfo>;
13
13
  /**
14
14
  * Read buffer from tokenizer
15
- * @param buffer - Target buffer to fill with data read from the tokenizer-stream
15
+ * @param uint8Array - Target Uint8Array to fill with data read from the tokenizer-stream
16
16
  * @param options - Read behaviour options
17
17
  * @returns Promise with number of bytes read
18
18
  */
19
- readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
19
+ readBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
20
20
  /**
21
21
  * Peek (read ahead) buffer from tokenizer
22
- * @param buffer - Target buffer to write the data read to
22
+ * @param uint8Array - Uint8Array (or Buffer) to write data to
23
23
  * @param options - Read behaviour options
24
24
  * @returns Promise with number of bytes peeked
25
25
  */
26
- peekBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
26
+ peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
27
27
  ignore(length: number): Promise<number>;
28
28
  }
@@ -18,92 +18,64 @@ class ReadStreamTokenizer extends AbstractTokenizer_1.AbstractTokenizer {
18
18
  }
19
19
  /**
20
20
  * Read buffer from tokenizer
21
- * @param buffer - Target buffer to fill with data read from the tokenizer-stream
21
+ * @param uint8Array - Target Uint8Array to fill with data read from the tokenizer-stream
22
22
  * @param options - Read behaviour options
23
23
  * @returns Promise with number of bytes read
24
24
  */
25
- async readBuffer(buffer, options) {
26
- // const _offset = position ? position : this.position;
27
- // debug(`readBuffer ${_offset}...${_offset + length - 1}`);
28
- let offset = 0;
29
- let length = buffer.length;
30
- if (options) {
31
- if (Number.isInteger(options.length)) {
32
- length = options.length;
33
- }
34
- else {
35
- length -= options.offset || 0;
36
- }
37
- if (options.position) {
38
- const skipBytes = options.position - this.position;
39
- if (skipBytes > 0) {
40
- await this.ignore(skipBytes);
41
- return this.readBuffer(buffer, options);
42
- }
43
- else if (skipBytes < 0) {
44
- throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
45
- }
46
- }
47
- if (options.offset) {
48
- offset = options.offset;
49
- }
25
+ async readBuffer(uint8Array, options) {
26
+ const normOptions = this.normalizeOptions(uint8Array, options);
27
+ const skipBytes = normOptions.position - this.position;
28
+ if (skipBytes > 0) {
29
+ await this.ignore(skipBytes);
30
+ return this.readBuffer(uint8Array, options);
31
+ }
32
+ else if (skipBytes < 0) {
33
+ throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
50
34
  }
51
- if (length === 0) {
35
+ if (normOptions.length === 0) {
52
36
  return 0;
53
37
  }
54
- const bytesRead = await this.streamReader.read(buffer, offset, length);
38
+ const bytesRead = await this.streamReader.read(uint8Array, normOptions.offset, normOptions.length);
55
39
  this.position += bytesRead;
56
- if ((!options || !options.mayBeLess) && bytesRead < length) {
40
+ if ((!options || !options.mayBeLess) && bytesRead < normOptions.length) {
57
41
  throw new peek_readable_1.EndOfStreamError();
58
42
  }
59
43
  return bytesRead;
60
44
  }
61
45
  /**
62
46
  * Peek (read ahead) buffer from tokenizer
63
- * @param buffer - Target buffer to write the data read to
47
+ * @param uint8Array - Uint8Array (or Buffer) to write data to
64
48
  * @param options - Read behaviour options
65
49
  * @returns Promise with number of bytes peeked
66
50
  */
67
- async peekBuffer(buffer, options) {
68
- // const _offset = position ? position : this.position;
69
- // debug(`peek ${_offset}...${_offset + length - 1}`);
70
- let offset = 0;
71
- let bytesRead;
72
- let length = buffer.length;
73
- if (options) {
74
- if (options.offset) {
75
- offset = options.offset;
51
+ async peekBuffer(uint8Array, options) {
52
+ const normOptions = this.normalizeOptions(uint8Array, options);
53
+ let bytesRead = 0;
54
+ if (normOptions.position) {
55
+ const skipBytes = normOptions.position - this.position;
56
+ if (skipBytes > 0) {
57
+ const skipBuffer = new Uint8Array(normOptions.length + skipBytes);
58
+ bytesRead = await this.peekBuffer(skipBuffer, { mayBeLess: normOptions.mayBeLess });
59
+ uint8Array.set(skipBuffer.subarray(skipBytes), normOptions.offset);
60
+ return bytesRead - skipBytes;
76
61
  }
77
- if (Number.isInteger(options.length)) {
78
- length = options.length;
62
+ else if (skipBytes < 0) {
63
+ throw new Error('Cannot peek from a negative offset in a stream');
79
64
  }
80
- else {
81
- length -= options.offset || 0;
65
+ }
66
+ if (normOptions.length > 0) {
67
+ try {
68
+ bytesRead = await this.streamReader.peek(uint8Array, normOptions.offset, normOptions.length);
82
69
  }
83
- if (options.position) {
84
- const skipBytes = options.position - this.position;
85
- if (skipBytes > 0) {
86
- const skipBuffer = new Uint8Array(length + skipBytes);
87
- bytesRead = await this.peekBuffer(skipBuffer, { mayBeLess: options.mayBeLess });
88
- buffer.set(skipBuffer.subarray(skipBytes), offset);
89
- return bytesRead - skipBytes;
90
- }
91
- else if (skipBytes < 0) {
92
- throw new Error('Cannot peek from a negative offset in a stream');
70
+ catch (err) {
71
+ if (options && options.mayBeLess && err instanceof peek_readable_1.EndOfStreamError) {
72
+ return 0;
93
73
  }
74
+ throw err;
94
75
  }
95
- }
96
- try {
97
- bytesRead = await this.streamReader.peek(buffer, offset, length);
98
- }
99
- catch (err) {
100
- if (options && options.mayBeLess && err instanceof peek_readable_1.EndOfStreamError) {
101
- return 0;
76
+ if ((!normOptions.mayBeLess) && bytesRead < normOptions.length) {
77
+ throw new peek_readable_1.EndOfStreamError();
102
78
  }
103
- throw err;
104
- }
105
- if ((!options || !options.mayBeLess) && bytesRead < length) {
106
- throw new peek_readable_1.EndOfStreamError();
107
79
  }
108
80
  return bytesRead;
109
81
  }
package/lib/core.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import { ReadStreamTokenizer } from './ReadStreamTokenizer';
3
- import * as Stream from 'stream';
3
+ import { Readable } from 'stream';
4
4
  import { BufferTokenizer } from './BufferTokenizer';
5
5
  import { IFileInfo } from './types';
6
6
  export { EndOfStreamError } from 'peek-readable';
@@ -10,10 +10,10 @@ export { IToken, IGetToken } from '@tokenizer/token';
10
10
  * Construct ReadStreamTokenizer from given Stream.
11
11
  * Will set fileSize, if provided given Stream has set the .path property/
12
12
  * @param stream - Read from Node.js Stream.Readable
13
- * @param fileInfo - Pass the file information, like size and MIME-type of the correspnding stream.
13
+ * @param fileInfo - Pass the file information, like size and MIME-type of the corresponding stream.
14
14
  * @returns ReadStreamTokenizer
15
15
  */
16
- export declare function fromStream(stream: Stream.Readable, fileInfo?: IFileInfo): ReadStreamTokenizer;
16
+ export declare function fromStream(stream: Readable, fileInfo?: IFileInfo): ReadStreamTokenizer;
17
17
  /**
18
18
  * Construct ReadStreamTokenizer from given Buffer.
19
19
  * @param uint8Array - Uint8Array to tokenize
package/lib/core.js CHANGED
@@ -9,7 +9,7 @@ Object.defineProperty(exports, "EndOfStreamError", { enumerable: true, get: func
9
9
  * Construct ReadStreamTokenizer from given Stream.
10
10
  * Will set fileSize, if provided given Stream has set the .path property/
11
11
  * @param stream - Read from Node.js Stream.Readable
12
- * @param fileInfo - Pass the file information, like size and MIME-type of the correspnding stream.
12
+ * @param fileInfo - Pass the file information, like size and MIME-type of the corresponding stream.
13
13
  * @returns ReadStreamTokenizer
14
14
  */
15
15
  function fromStream(stream, fileInfo) {
package/lib/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import * as Stream from 'stream';
2
+ import { Readable } from 'stream';
3
3
  import { ReadStreamTokenizer } from './ReadStreamTokenizer';
4
4
  import * as core from './core';
5
5
  export { fromFile } from './FileTokenizer';
@@ -12,4 +12,4 @@ export { IToken, IGetToken } from '@tokenizer/token';
12
12
  * @param fileInfo - Pass additional file information to the tokenizer
13
13
  * @returns Tokenizer
14
14
  */
15
- export declare function fromStream(stream: Stream.Readable, fileInfo?: core.IFileInfo): Promise<ReadStreamTokenizer>;
15
+ export declare function fromStream(stream: Readable, fileInfo?: core.IFileInfo): Promise<ReadStreamTokenizer>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "strtok3",
3
- "version": "6.2.1",
3
+ "version": "6.3.0",
4
4
  "description": "A promise based streaming tokenizer",
5
5
  "author": {
6
6
  "name": "Borewit",
@@ -19,6 +19,7 @@
19
19
  "eslint": "eslint lib test --ext .ts --ignore-pattern *.d.ts",
20
20
  "lint-md": "remark -u preset-lint-recommended .",
21
21
  "lint": "npm run lint-md && npm run eslint",
22
+ "fix": "eslint lib test --ext .ts --ignore-pattern *.d.ts --fix",
22
23
  "test": "mocha --require ts-node/register --require source-map-support/register --full-trace test/test.ts",
23
24
  "test-coverage": "nyc npm run test",
24
25
  "send-coveralls": "nyc report --reporter=text-lcov | coveralls",
@@ -43,32 +44,34 @@
43
44
  "url": "https://github.com/Borewit/strtok3/issues"
44
45
  },
45
46
  "devDependencies": {
46
- "@tokenizer/token": "^0.3.0",
47
- "@types/chai": "^4.2.21",
48
- "@types/debug": "^4.1.6",
49
- "@types/mocha": "^8.2.3",
50
- "@types/node": "^16.3.2",
51
- "@typescript-eslint/eslint-plugin": "^2.34.0",
52
- "@typescript-eslint/eslint-plugin-tslint": "^4.28.3",
53
- "@typescript-eslint/parser": "^2.34.0",
54
- "chai": "^4.3.4",
47
+ "@types/chai": "^4.3.0",
48
+ "@types/debug": "^4.1.7",
49
+ "@types/mocha": "^9.1.0",
50
+ "@types/node": "^17.0.14",
51
+ "@typescript-eslint/eslint-plugin": "^5.10.2",
52
+ "@typescript-eslint/parser": "^5.10.2",
53
+ "chai": "^4.3.6",
55
54
  "coveralls": "^3.1.1",
56
55
  "del-cli": "^4.0.1",
57
- "eslint": "^6.8.0",
58
- "eslint-plugin-import": "^2.23.4",
59
- "eslint-plugin-prefer-arrow": "^1.2.3",
60
- "mocha": "^9.0.2",
56
+ "eslint": "^8.8.0",
57
+ "eslint-config-prettier": "^8.3.0",
58
+ "eslint-import-resolver-typescript": "^2.5.0",
59
+ "eslint-plugin-import": "^2.25.4",
60
+ "eslint-plugin-jsdoc": "^37.7.1",
61
+ "eslint-plugin-node": "^11.1.0",
62
+ "eslint-plugin-unicorn": "^40.1.0",
63
+ "mocha": "^9.2.0",
61
64
  "nyc": "^15.1.0",
62
- "remark-cli": "^9.0.0",
63
- "remark-preset-lint-recommended": "^5.0.0",
64
- "source-map-support": "^0.5.16",
65
- "token-types": "^4.0.1",
66
- "ts-node": "^10.1.0",
67
- "tslint": "^6.1.3",
68
- "typescript": "^4.3.5"
65
+ "remark-cli": "^10.0.1",
66
+ "remark-preset-lint-recommended": "^6.1.2",
67
+ "source-map-support": "^0.5.21",
68
+ "token-types": "^4.1.1",
69
+ "ts-node": "^10.4.0",
70
+ "typescript": "^4.5.5"
69
71
  },
70
72
  "dependencies": {
71
- "peek-readable": "^4.0.0"
73
+ "@tokenizer/token": "^0.3.0",
74
+ "peek-readable": "^4.1.0"
72
75
  },
73
76
  "keywords": [
74
77
  "tokenizer",