strtok3 7.0.0-alpha.2 → 7.0.0-alpha.6

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,70 +1,50 @@
1
- import { AbstractTokenizer } from './AbstractTokenizer.js';
2
- import { EndOfStreamError } from 'peek-readable';
3
- import * as fs from './FsPromise.js';
4
- export class FileTokenizer extends AbstractTokenizer {
5
- constructor(fd, fileInfo) {
6
- super(fileInfo);
7
- this.fd = fd;
8
- }
9
- /**
10
- * Read buffer from file
11
- * @param buffer
12
- * @param options - Read behaviour options
13
- * @returns Promise number of bytes read
14
- */
15
- async readBuffer(buffer, options) {
16
- let offset = 0;
17
- let length = buffer.length;
18
- if (options) {
19
- if (options.position) {
20
- if (options.position < this.position) {
21
- throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
22
- }
23
- this.position = options.position;
24
- }
25
- if (Number.isInteger(options.length)) {
26
- length = options.length;
27
- }
28
- else {
29
- length -= options.offset || 0;
30
- }
31
- if (options.offset) {
32
- offset = options.offset;
33
- }
34
- }
35
- if (length === 0) {
36
- return Promise.resolve(0);
37
- }
38
- const res = await fs.read(this.fd, buffer, offset, length, this.position);
39
- this.position += res.bytesRead;
40
- if (res.bytesRead < length && (!options || !options.mayBeLess)) {
41
- throw new EndOfStreamError();
42
- }
43
- return res.bytesRead;
44
- }
45
- /**
46
- * Peek buffer from file
47
- * @param uint8Array - Uint8Array (or Buffer) to write data to
48
- * @param options - Read behaviour options
49
- * @returns Promise number of bytes read
50
- */
51
- async peekBuffer(uint8Array, options) {
52
- options = this.normalizeOptions(uint8Array, options);
53
- const res = await fs.read(this.fd, uint8Array, options.offset, options.length, options.position);
54
- if ((!options.mayBeLess) && res.bytesRead < options.length) {
55
- throw new EndOfStreamError();
56
- }
57
- return res.bytesRead;
58
- }
59
- async close() {
60
- return fs.close(this.fd);
61
- }
62
- }
63
- export async function fromFile(sourceFilePath) {
64
- const stat = await fs.stat(sourceFilePath);
65
- if (!stat.isFile) {
66
- throw new Error(`File not a file: ${sourceFilePath}`);
67
- }
68
- const fd = await fs.open(sourceFilePath, 'r');
69
- return new FileTokenizer(fd, { path: sourceFilePath, size: stat.size });
70
- }
1
+ import { AbstractTokenizer } from './AbstractTokenizer.js';
2
+ import { EndOfStreamError } from 'peek-readable';
3
+ import * as fs from './FsPromise.js';
4
+ export class FileTokenizer extends AbstractTokenizer {
5
+ constructor(fd, fileInfo) {
6
+ super(fileInfo);
7
+ this.fd = fd;
8
+ }
9
+ /**
10
+ * Read buffer from file
11
+ * @param uint8Array - Uint8Array to write result to
12
+ * @param options - Read behaviour options
13
+ * @returns Promise number of bytes read
14
+ */
15
+ async readBuffer(uint8Array, options) {
16
+ const normOptions = this.normalizeOptions(uint8Array, options);
17
+ this.position = normOptions.position;
18
+ const res = await fs.read(this.fd, uint8Array, normOptions.offset, normOptions.length, normOptions.position);
19
+ this.position += res.bytesRead;
20
+ if (res.bytesRead < normOptions.length && (!options || !options.mayBeLess)) {
21
+ throw new EndOfStreamError();
22
+ }
23
+ return res.bytesRead;
24
+ }
25
+ /**
26
+ * Peek buffer from file
27
+ * @param uint8Array - Uint8Array (or Buffer) to write data to
28
+ * @param options - Read behaviour options
29
+ * @returns Promise number of bytes read
30
+ */
31
+ async peekBuffer(uint8Array, options) {
32
+ const normOptions = this.normalizeOptions(uint8Array, options);
33
+ const res = await fs.read(this.fd, uint8Array, normOptions.offset, normOptions.length, normOptions.position);
34
+ if ((!normOptions.mayBeLess) && res.bytesRead < normOptions.length) {
35
+ throw new EndOfStreamError();
36
+ }
37
+ return res.bytesRead;
38
+ }
39
+ async close() {
40
+ return fs.close(this.fd);
41
+ }
42
+ }
43
+ export async function fromFile(sourceFilePath) {
44
+ const stat = await fs.stat(sourceFilePath);
45
+ if (!stat.isFile) {
46
+ throw new Error(`File not a file: ${sourceFilePath}`);
47
+ }
48
+ const fd = await fs.open(sourceFilePath, 'r');
49
+ return new FileTokenizer(fd, { path: sourceFilePath, size: stat.size });
50
+ }
@@ -1,18 +1,18 @@
1
- /**
2
- * Module convert fs functions to promise based functions
3
- */
4
- /// <reference types="node" />
5
- import * as fs from 'node:fs';
6
- export interface IReadResult {
7
- bytesRead: number;
8
- buffer: Uint8Array;
9
- }
10
- export declare const pathExists: typeof fs.existsSync;
11
- export declare const createReadStream: typeof fs.createReadStream;
12
- export declare function stat(path: fs.PathLike): Promise<fs.Stats>;
13
- export declare function close(fd: number): Promise<void>;
14
- export declare function open(path: fs.PathLike, mode?: string): Promise<number>;
15
- export declare function read(fd: number, buffer: Uint8Array, offset: number, length: number, position: number): Promise<IReadResult>;
16
- export declare function writeFile(path: fs.PathLike, data: Buffer | string): Promise<void>;
17
- export declare function writeFileSync(path: fs.PathLike, data: Buffer | string): void;
18
- export declare function readFile(path: fs.PathLike): Promise<Buffer>;
1
+ /**
2
+ * Module convert fs functions to promise based functions
3
+ */
4
+ /// <reference types="node" />
5
+ import fs from 'node:fs';
6
+ export interface IReadResult {
7
+ bytesRead: number;
8
+ buffer: Uint8Array;
9
+ }
10
+ export declare const pathExists: typeof fs.existsSync;
11
+ export declare const createReadStream: typeof fs.createReadStream;
12
+ export declare function stat(path: fs.PathLike): Promise<fs.Stats>;
13
+ export declare function close(fd: number): Promise<void>;
14
+ export declare function open(path: fs.PathLike, mode: fs.Mode): Promise<number>;
15
+ export declare function read(fd: number, buffer: Uint8Array, offset: number, length: number, position: number): Promise<IReadResult>;
16
+ export declare function writeFile(path: fs.PathLike, data: Buffer | string): Promise<void>;
17
+ export declare function writeFileSync(path: fs.PathLike, data: Buffer | string): void;
18
+ export declare function readFile(path: fs.PathLike): Promise<Buffer>;
package/lib/FsPromise.js CHANGED
@@ -1,69 +1,69 @@
1
- /**
2
- * Module convert fs functions to promise based functions
3
- */
4
- import * as fs from 'node:fs';
5
- export const pathExists = fs.existsSync;
6
- export const createReadStream = fs.createReadStream;
7
- export async function stat(path) {
8
- return new Promise((resolve, reject) => {
9
- fs.stat(path, (err, stats) => {
10
- if (err)
11
- reject(err);
12
- else
13
- resolve(stats);
14
- });
15
- });
16
- }
17
- export async function close(fd) {
18
- return new Promise((resolve, reject) => {
19
- fs.close(fd, err => {
20
- if (err)
21
- reject(err);
22
- else
23
- resolve();
24
- });
25
- });
26
- }
27
- export async function open(path, mode) {
28
- return new Promise((resolve, reject) => {
29
- fs.open(path, mode, (err, fd) => {
30
- if (err)
31
- reject(err);
32
- else
33
- resolve(fd);
34
- });
35
- });
36
- }
37
- export async function read(fd, buffer, offset, length, position) {
38
- return new Promise((resolve, reject) => {
39
- fs.read(fd, buffer, offset, length, position, (err, bytesRead, _buffer) => {
40
- if (err)
41
- reject(err);
42
- else
43
- resolve({ bytesRead, buffer: _buffer });
44
- });
45
- });
46
- }
47
- export async function writeFile(path, data) {
48
- return new Promise((resolve, reject) => {
49
- fs.writeFile(path, data, err => {
50
- if (err)
51
- reject(err);
52
- else
53
- resolve();
54
- });
55
- });
56
- }
57
- export function writeFileSync(path, data) {
58
- fs.writeFileSync(path, data);
59
- }
60
- export async function readFile(path) {
61
- return new Promise((resolve, reject) => {
62
- fs.readFile(path, (err, buffer) => {
63
- if (err)
64
- reject(err);
65
- else
66
- resolve(buffer);
67
- });
68
- });
69
- }
1
+ /**
2
+ * Module convert fs functions to promise based functions
3
+ */
4
+ import fs from 'node:fs';
5
+ export const pathExists = fs.existsSync;
6
+ export const createReadStream = fs.createReadStream;
7
+ export async function stat(path) {
8
+ return new Promise((resolve, reject) => {
9
+ fs.stat(path, (err, stats) => {
10
+ if (err)
11
+ reject(err);
12
+ else
13
+ resolve(stats);
14
+ });
15
+ });
16
+ }
17
+ export async function close(fd) {
18
+ return new Promise((resolve, reject) => {
19
+ fs.close(fd, err => {
20
+ if (err)
21
+ reject(err);
22
+ else
23
+ resolve();
24
+ });
25
+ });
26
+ }
27
+ export async function open(path, mode) {
28
+ return new Promise((resolve, reject) => {
29
+ fs.open(path, mode, (err, fd) => {
30
+ if (err)
31
+ reject(err);
32
+ else
33
+ resolve(fd);
34
+ });
35
+ });
36
+ }
37
+ export async function read(fd, buffer, offset, length, position) {
38
+ return new Promise((resolve, reject) => {
39
+ fs.read(fd, buffer, offset, length, position, (err, bytesRead, _buffer) => {
40
+ if (err)
41
+ reject(err);
42
+ else
43
+ resolve({ bytesRead, buffer: _buffer });
44
+ });
45
+ });
46
+ }
47
+ export async function writeFile(path, data) {
48
+ return new Promise((resolve, reject) => {
49
+ fs.writeFile(path, data, err => {
50
+ if (err)
51
+ reject(err);
52
+ else
53
+ resolve();
54
+ });
55
+ });
56
+ }
57
+ export function writeFileSync(path, data) {
58
+ fs.writeFileSync(path, data);
59
+ }
60
+ export async function readFile(path) {
61
+ return new Promise((resolve, reject) => {
62
+ fs.readFile(path, (err, buffer) => {
63
+ if (err)
64
+ reject(err);
65
+ else
66
+ resolve(buffer);
67
+ });
68
+ });
69
+ }
@@ -1,28 +1,28 @@
1
- /// <reference types="node" />
2
- import { AbstractTokenizer } from './AbstractTokenizer.js';
3
- import * as Stream from 'node:stream';
4
- import { IFileInfo, IReadChunkOptions } from './types';
5
- export declare class ReadStreamTokenizer extends AbstractTokenizer {
6
- private streamReader;
7
- constructor(stream: Stream.Readable, fileInfo?: IFileInfo);
8
- /**
9
- * Get file information, an HTTP-client may implement this doing a HEAD request
10
- * @return Promise with file information
11
- */
12
- getFileInfo(): Promise<IFileInfo>;
13
- /**
14
- * Read buffer from tokenizer
15
- * @param buffer - Target buffer to fill with data read from the tokenizer-stream
16
- * @param options - Read behaviour options
17
- * @returns Promise with number of bytes read
18
- */
19
- readBuffer(buffer: Uint8Array, options?: IReadChunkOptions): Promise<number>;
20
- /**
21
- * Peek (read ahead) buffer from tokenizer
22
- * @param uint8Array - Uint8Array (or Buffer) to write data to
23
- * @param options - Read behaviour options
24
- * @returns Promise with number of bytes peeked
25
- */
26
- peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
27
- ignore(length: number): Promise<number>;
28
- }
1
+ /// <reference types="node" />
2
+ import { AbstractTokenizer } from './AbstractTokenizer.js';
3
+ import { Readable } from 'node:stream';
4
+ import { IFileInfo, IReadChunkOptions } from './types.js';
5
+ export declare class ReadStreamTokenizer extends AbstractTokenizer {
6
+ private streamReader;
7
+ constructor(stream: Readable, fileInfo?: IFileInfo);
8
+ /**
9
+ * Get file information, an HTTP-client may implement this doing a HEAD request
10
+ * @return Promise with file information
11
+ */
12
+ getFileInfo(): Promise<IFileInfo>;
13
+ /**
14
+ * Read buffer from tokenizer
15
+ * @param uint8Array - Target Uint8Array to fill with data read from the tokenizer-stream
16
+ * @param options - Read behaviour options
17
+ * @returns Promise with number of bytes read
18
+ */
19
+ readBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
20
+ /**
21
+ * Peek (read ahead) buffer from tokenizer
22
+ * @param uint8Array - Uint8Array (or Buffer) to write data to
23
+ * @param options - Read behaviour options
24
+ * @returns Promise with number of bytes peeked
25
+ */
26
+ peekBuffer(uint8Array: Uint8Array, options?: IReadChunkOptions): Promise<number>;
27
+ ignore(length: number): Promise<number>;
28
+ }
@@ -1,110 +1,94 @@
1
- import { AbstractTokenizer } from './AbstractTokenizer.js';
2
- import { EndOfStreamError, StreamReader } from 'peek-readable';
3
- const maxBufferSize = 256000;
4
- export class ReadStreamTokenizer extends AbstractTokenizer {
5
- constructor(stream, fileInfo) {
6
- super(fileInfo);
7
- this.streamReader = new StreamReader(stream);
8
- }
9
- /**
10
- * Get file information, an HTTP-client may implement this doing a HEAD request
11
- * @return Promise with file information
12
- */
13
- async getFileInfo() {
14
- return this.fileInfo;
15
- }
16
- /**
17
- * Read buffer from tokenizer
18
- * @param buffer - Target buffer to fill with data read from the tokenizer-stream
19
- * @param options - Read behaviour options
20
- * @returns Promise with number of bytes read
21
- */
22
- async readBuffer(buffer, options) {
23
- // const _offset = position ? position : this.position;
24
- // debug(`readBuffer ${_offset}...${_offset + length - 1}`);
25
- let offset = 0;
26
- let length = buffer.length;
27
- if (options) {
28
- if (Number.isInteger(options.length)) {
29
- length = options.length;
30
- }
31
- else {
32
- length -= options.offset || 0;
33
- }
34
- if (options.position) {
35
- const skipBytes = options.position - this.position;
36
- if (skipBytes > 0) {
37
- await this.ignore(skipBytes);
38
- return this.readBuffer(buffer, options);
39
- }
40
- else if (skipBytes < 0) {
41
- throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
42
- }
43
- }
44
- if (options.offset) {
45
- offset = options.offset;
46
- }
47
- }
48
- if (length === 0) {
49
- return 0;
50
- }
51
- const bytesRead = await this.streamReader.read(buffer, offset, length);
52
- this.position += bytesRead;
53
- if ((!options || !options.mayBeLess) && bytesRead < length) {
54
- throw new EndOfStreamError();
55
- }
56
- return bytesRead;
57
- }
58
- /**
59
- * Peek (read ahead) buffer from tokenizer
60
- * @param uint8Array - Uint8Array (or Buffer) to write data to
61
- * @param options - Read behaviour options
62
- * @returns Promise with number of bytes peeked
63
- */
64
- async peekBuffer(uint8Array, options) {
65
- options = this.normalizeOptions(uint8Array, options);
66
- let bytesRead = 0;
67
- if (options.position) {
68
- const skipBytes = options.position - this.position;
69
- if (skipBytes > 0) {
70
- const skipBuffer = new Uint8Array(options.length + skipBytes);
71
- bytesRead = await this.peekBuffer(skipBuffer, { mayBeLess: options.mayBeLess });
72
- uint8Array.set(skipBuffer.subarray(skipBytes), options.offset);
73
- return bytesRead - skipBytes;
74
- }
75
- else if (skipBytes < 0) {
76
- throw new Error('Cannot peek from a negative offset in a stream');
77
- }
78
- }
79
- if (options.length > 0) {
80
- try {
81
- bytesRead = await this.streamReader.peek(uint8Array, options.offset, options.length);
82
- }
83
- catch (err) {
84
- if (options && options.mayBeLess && err instanceof EndOfStreamError) {
85
- return 0;
86
- }
87
- throw err;
88
- }
89
- if ((!options.mayBeLess) && bytesRead < options.length) {
90
- throw new EndOfStreamError();
91
- }
92
- }
93
- return bytesRead;
94
- }
95
- async ignore(length) {
96
- // debug(`ignore ${this.position}...${this.position + length - 1}`);
97
- const bufSize = Math.min(maxBufferSize, length);
98
- const buf = new Uint8Array(bufSize);
99
- let totBytesRead = 0;
100
- while (totBytesRead < length) {
101
- const remaining = length - totBytesRead;
102
- const bytesRead = await this.readBuffer(buf, { length: Math.min(bufSize, remaining) });
103
- if (bytesRead < 0) {
104
- return bytesRead;
105
- }
106
- totBytesRead += bytesRead;
107
- }
108
- return totBytesRead;
109
- }
110
- }
1
+ import { AbstractTokenizer } from './AbstractTokenizer.js';
2
+ import { EndOfStreamError, StreamReader } from 'peek-readable';
3
+ const maxBufferSize = 256000;
4
+ export class ReadStreamTokenizer extends AbstractTokenizer {
5
+ constructor(stream, fileInfo) {
6
+ super(fileInfo);
7
+ this.streamReader = new StreamReader(stream);
8
+ }
9
+ /**
10
+ * Get file information, an HTTP-client may implement this doing a HEAD request
11
+ * @return Promise with file information
12
+ */
13
+ async getFileInfo() {
14
+ return this.fileInfo;
15
+ }
16
+ /**
17
+ * Read buffer from tokenizer
18
+ * @param uint8Array - Target Uint8Array to fill with data read from the tokenizer-stream
19
+ * @param options - Read behaviour options
20
+ * @returns Promise with number of bytes read
21
+ */
22
+ async readBuffer(uint8Array, options) {
23
+ const normOptions = this.normalizeOptions(uint8Array, options);
24
+ const skipBytes = normOptions.position - this.position;
25
+ if (skipBytes > 0) {
26
+ await this.ignore(skipBytes);
27
+ return this.readBuffer(uint8Array, options);
28
+ }
29
+ else if (skipBytes < 0) {
30
+ throw new Error('`options.position` must be equal or greater than `tokenizer.position`');
31
+ }
32
+ if (normOptions.length === 0) {
33
+ return 0;
34
+ }
35
+ const bytesRead = await this.streamReader.read(uint8Array, normOptions.offset, normOptions.length);
36
+ this.position += bytesRead;
37
+ if ((!options || !options.mayBeLess) && bytesRead < normOptions.length) {
38
+ throw new EndOfStreamError();
39
+ }
40
+ return bytesRead;
41
+ }
42
+ /**
43
+ * Peek (read ahead) buffer from tokenizer
44
+ * @param uint8Array - Uint8Array (or Buffer) to write data to
45
+ * @param options - Read behaviour options
46
+ * @returns Promise with number of bytes peeked
47
+ */
48
+ async peekBuffer(uint8Array, options) {
49
+ const normOptions = this.normalizeOptions(uint8Array, options);
50
+ let bytesRead = 0;
51
+ if (normOptions.position) {
52
+ const skipBytes = normOptions.position - this.position;
53
+ if (skipBytes > 0) {
54
+ const skipBuffer = new Uint8Array(normOptions.length + skipBytes);
55
+ bytesRead = await this.peekBuffer(skipBuffer, { mayBeLess: normOptions.mayBeLess });
56
+ uint8Array.set(skipBuffer.subarray(skipBytes), normOptions.offset);
57
+ return bytesRead - skipBytes;
58
+ }
59
+ else if (skipBytes < 0) {
60
+ throw new Error('Cannot peek from a negative offset in a stream');
61
+ }
62
+ }
63
+ if (normOptions.length > 0) {
64
+ try {
65
+ bytesRead = await this.streamReader.peek(uint8Array, normOptions.offset, normOptions.length);
66
+ }
67
+ catch (err) {
68
+ if (options && options.mayBeLess && err instanceof EndOfStreamError) {
69
+ return 0;
70
+ }
71
+ throw err;
72
+ }
73
+ if ((!normOptions.mayBeLess) && bytesRead < normOptions.length) {
74
+ throw new EndOfStreamError();
75
+ }
76
+ }
77
+ return bytesRead;
78
+ }
79
+ async ignore(length) {
80
+ // debug(`ignore ${this.position}...${this.position + length - 1}`);
81
+ const bufSize = Math.min(maxBufferSize, length);
82
+ const buf = new Uint8Array(bufSize);
83
+ let totBytesRead = 0;
84
+ while (totBytesRead < length) {
85
+ const remaining = length - totBytesRead;
86
+ const bytesRead = await this.readBuffer(buf, { length: Math.min(bufSize, remaining) });
87
+ if (bytesRead < 0) {
88
+ return bytesRead;
89
+ }
90
+ totBytesRead += bytesRead;
91
+ }
92
+ return totBytesRead;
93
+ }
94
+ }
package/lib/core.d.ts CHANGED
@@ -1,23 +1,23 @@
1
- /// <reference types="node" />
2
- import { ReadStreamTokenizer } from './ReadStreamTokenizer.js';
3
- import * as Stream from 'node:stream';
4
- import { BufferTokenizer } from './BufferTokenizer.js';
5
- import { IFileInfo } from './types.js';
6
- export { EndOfStreamError } from 'peek-readable';
7
- export { ITokenizer, IFileInfo } from './types';
8
- export { IToken, IGetToken } from '@tokenizer/token';
9
- /**
10
- * Construct ReadStreamTokenizer from given Stream.
11
- * Will set fileSize, if provided given Stream has set the .path property/
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.
14
- * @returns ReadStreamTokenizer
15
- */
16
- export declare function fromStream(stream: Stream.Readable, fileInfo?: IFileInfo): ReadStreamTokenizer;
17
- /**
18
- * Construct ReadStreamTokenizer from given Buffer.
19
- * @param uint8Array - Uint8Array to tokenize
20
- * @param fileInfo - Pass additional file information to the tokenizer
21
- * @returns BufferTokenizer
22
- */
23
- export declare function fromBuffer(uint8Array: Uint8Array, fileInfo?: IFileInfo): BufferTokenizer;
1
+ /// <reference types="node" />
2
+ import { ReadStreamTokenizer } from './ReadStreamTokenizer.js';
3
+ import { Readable } from 'node:stream';
4
+ import { BufferTokenizer } from './BufferTokenizer.js';
5
+ import { IFileInfo } from './types.js';
6
+ export { EndOfStreamError } from 'peek-readable';
7
+ export { ITokenizer, IFileInfo } from './types.js';
8
+ export { IToken, IGetToken } from '@tokenizer/token';
9
+ /**
10
+ * Construct ReadStreamTokenizer from given Stream.
11
+ * Will set fileSize, if provided given Stream has set the .path property/
12
+ * @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
+ * @returns ReadStreamTokenizer
15
+ */
16
+ export declare function fromStream(stream: Readable, fileInfo?: IFileInfo): ReadStreamTokenizer;
17
+ /**
18
+ * Construct ReadStreamTokenizer from given Buffer.
19
+ * @param uint8Array - Uint8Array to tokenize
20
+ * @param fileInfo - Pass additional file information to the tokenizer
21
+ * @returns BufferTokenizer
22
+ */
23
+ export declare function fromBuffer(uint8Array: Uint8Array, fileInfo?: IFileInfo): BufferTokenizer;