strtok3 6.2.0 → 6.2.4
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/LICENSE +14 -14
- package/README.md +300 -301
- package/lib/AbstractTokenizer.d.ts +69 -61
- package/lib/AbstractTokenizer.js +104 -69
- package/lib/BufferTokenizer.d.ts +26 -36
- package/lib/BufferTokenizer.js +55 -116
- package/lib/FileTokenizer.d.ts +22 -28
- package/lib/FileTokenizer.js +55 -112
- package/lib/FsPromise.d.ts +18 -18
- package/lib/FsPromise.js +79 -79
- package/lib/ReadStreamTokenizer.d.ts +28 -28
- package/lib/ReadStreamTokenizer.js +98 -126
- package/lib/core.d.ts +23 -23
- package/lib/core.js +29 -29
- package/lib/index.d.ts +15 -15
- package/lib/index.js +27 -27
- package/lib/types.d.ts +103 -103
- package/lib/types.js +2 -2
- package/package.json +101 -100
package/lib/FileTokenizer.js
CHANGED
|
@@ -1,112 +1,55 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.fromFile = exports.FileTokenizer = void 0;
|
|
4
|
-
const AbstractTokenizer_1 = require("./AbstractTokenizer");
|
|
5
|
-
const peek_readable_1 = require("peek-readable");
|
|
6
|
-
const fs = require("./FsPromise");
|
|
7
|
-
class FileTokenizer extends AbstractTokenizer_1.AbstractTokenizer {
|
|
8
|
-
constructor(fd, fileInfo) {
|
|
9
|
-
super(fileInfo);
|
|
10
|
-
this.fd = fd;
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Read buffer from file
|
|
14
|
-
* @param
|
|
15
|
-
* @param options - Read behaviour options
|
|
16
|
-
* @returns Promise number of bytes read
|
|
17
|
-
*/
|
|
18
|
-
async readBuffer(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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) {
|
|
80
|
-
throw new peek_readable_1.EndOfStreamError();
|
|
81
|
-
}
|
|
82
|
-
return res.bytesRead;
|
|
83
|
-
}
|
|
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
|
-
async close() {
|
|
100
|
-
return fs.close(this.fd);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
exports.FileTokenizer = FileTokenizer;
|
|
104
|
-
async function fromFile(sourceFilePath) {
|
|
105
|
-
const stat = await fs.stat(sourceFilePath);
|
|
106
|
-
if (!stat.isFile) {
|
|
107
|
-
throw new Error(`File not a file: ${sourceFilePath}`);
|
|
108
|
-
}
|
|
109
|
-
const fd = await fs.open(sourceFilePath, 'r');
|
|
110
|
-
return new FileTokenizer(fd, { path: sourceFilePath, size: stat.size });
|
|
111
|
-
}
|
|
112
|
-
exports.fromFile = fromFile;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.fromFile = exports.FileTokenizer = void 0;
|
|
4
|
+
const AbstractTokenizer_1 = require("./AbstractTokenizer");
|
|
5
|
+
const peek_readable_1 = require("peek-readable");
|
|
6
|
+
const fs = require("./FsPromise");
|
|
7
|
+
class FileTokenizer extends AbstractTokenizer_1.AbstractTokenizer {
|
|
8
|
+
constructor(fd, fileInfo) {
|
|
9
|
+
super(fileInfo);
|
|
10
|
+
this.fd = fd;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Read buffer from file
|
|
14
|
+
* @param uint8Array - Uint8Array to write result to
|
|
15
|
+
* @param options - Read behaviour options
|
|
16
|
+
* @returns Promise number of bytes read
|
|
17
|
+
*/
|
|
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);
|
|
22
|
+
this.position += res.bytesRead;
|
|
23
|
+
if (res.bytesRead < normOptions.length && (!options || !options.mayBeLess)) {
|
|
24
|
+
throw new peek_readable_1.EndOfStreamError();
|
|
25
|
+
}
|
|
26
|
+
return res.bytesRead;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Peek buffer from file
|
|
30
|
+
* @param uint8Array - Uint8Array (or Buffer) to write data to
|
|
31
|
+
* @param options - Read behaviour options
|
|
32
|
+
* @returns Promise number of bytes read
|
|
33
|
+
*/
|
|
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) {
|
|
38
|
+
throw new peek_readable_1.EndOfStreamError();
|
|
39
|
+
}
|
|
40
|
+
return res.bytesRead;
|
|
41
|
+
}
|
|
42
|
+
async close() {
|
|
43
|
+
return fs.close(this.fd);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.FileTokenizer = FileTokenizer;
|
|
47
|
+
async function fromFile(sourceFilePath) {
|
|
48
|
+
const stat = await fs.stat(sourceFilePath);
|
|
49
|
+
if (!stat.isFile) {
|
|
50
|
+
throw new Error(`File not a file: ${sourceFilePath}`);
|
|
51
|
+
}
|
|
52
|
+
const fd = await fs.open(sourceFilePath, 'r');
|
|
53
|
+
return new FileTokenizer(fd, { path: sourceFilePath, size: stat.size });
|
|
54
|
+
}
|
|
55
|
+
exports.fromFile = fromFile;
|
package/lib/FsPromise.d.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Module convert fs functions to promise based functions
|
|
3
|
-
*/
|
|
4
|
-
/// <reference types="node" />
|
|
5
|
-
import * as fs from '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
|
|
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 * as fs from '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,79 +1,79 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Module convert fs functions to promise based functions
|
|
4
|
-
*/
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.readFile = exports.writeFileSync = exports.writeFile = exports.read = exports.open = exports.close = exports.stat = exports.createReadStream = exports.pathExists = void 0;
|
|
7
|
-
const fs = require("fs");
|
|
8
|
-
exports.pathExists = fs.existsSync;
|
|
9
|
-
exports.createReadStream = fs.createReadStream;
|
|
10
|
-
async function stat(path) {
|
|
11
|
-
return new Promise((resolve, reject) => {
|
|
12
|
-
fs.stat(path, (err, stats) => {
|
|
13
|
-
if (err)
|
|
14
|
-
reject(err);
|
|
15
|
-
else
|
|
16
|
-
resolve(stats);
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
exports.stat = stat;
|
|
21
|
-
async function close(fd) {
|
|
22
|
-
return new Promise((resolve, reject) => {
|
|
23
|
-
fs.close(fd, err => {
|
|
24
|
-
if (err)
|
|
25
|
-
reject(err);
|
|
26
|
-
else
|
|
27
|
-
resolve();
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
exports.close = close;
|
|
32
|
-
async function open(path, mode) {
|
|
33
|
-
return new Promise((resolve, reject) => {
|
|
34
|
-
fs.open(path, mode, (err, fd) => {
|
|
35
|
-
if (err)
|
|
36
|
-
reject(err);
|
|
37
|
-
else
|
|
38
|
-
resolve(fd);
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
exports.open = open;
|
|
43
|
-
async function read(fd, buffer, offset, length, position) {
|
|
44
|
-
return new Promise((resolve, reject) => {
|
|
45
|
-
fs.read(fd, buffer, offset, length, position, (err, bytesRead, _buffer) => {
|
|
46
|
-
if (err)
|
|
47
|
-
reject(err);
|
|
48
|
-
else
|
|
49
|
-
resolve({ bytesRead, buffer: _buffer });
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
exports.read = read;
|
|
54
|
-
async function writeFile(path, data) {
|
|
55
|
-
return new Promise((resolve, reject) => {
|
|
56
|
-
fs.writeFile(path, data, err => {
|
|
57
|
-
if (err)
|
|
58
|
-
reject(err);
|
|
59
|
-
else
|
|
60
|
-
resolve();
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
exports.writeFile = writeFile;
|
|
65
|
-
function writeFileSync(path, data) {
|
|
66
|
-
fs.writeFileSync(path, data);
|
|
67
|
-
}
|
|
68
|
-
exports.writeFileSync = writeFileSync;
|
|
69
|
-
async function readFile(path) {
|
|
70
|
-
return new Promise((resolve, reject) => {
|
|
71
|
-
fs.readFile(path, (err, buffer) => {
|
|
72
|
-
if (err)
|
|
73
|
-
reject(err);
|
|
74
|
-
else
|
|
75
|
-
resolve(buffer);
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
exports.readFile = readFile;
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Module convert fs functions to promise based functions
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.readFile = exports.writeFileSync = exports.writeFile = exports.read = exports.open = exports.close = exports.stat = exports.createReadStream = exports.pathExists = void 0;
|
|
7
|
+
const fs = require("fs");
|
|
8
|
+
exports.pathExists = fs.existsSync;
|
|
9
|
+
exports.createReadStream = fs.createReadStream;
|
|
10
|
+
async function stat(path) {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
fs.stat(path, (err, stats) => {
|
|
13
|
+
if (err)
|
|
14
|
+
reject(err);
|
|
15
|
+
else
|
|
16
|
+
resolve(stats);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
exports.stat = stat;
|
|
21
|
+
async function close(fd) {
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
fs.close(fd, err => {
|
|
24
|
+
if (err)
|
|
25
|
+
reject(err);
|
|
26
|
+
else
|
|
27
|
+
resolve();
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
exports.close = close;
|
|
32
|
+
async function open(path, mode) {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
fs.open(path, mode, (err, fd) => {
|
|
35
|
+
if (err)
|
|
36
|
+
reject(err);
|
|
37
|
+
else
|
|
38
|
+
resolve(fd);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
exports.open = open;
|
|
43
|
+
async function read(fd, buffer, offset, length, position) {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
fs.read(fd, buffer, offset, length, position, (err, bytesRead, _buffer) => {
|
|
46
|
+
if (err)
|
|
47
|
+
reject(err);
|
|
48
|
+
else
|
|
49
|
+
resolve({ bytesRead, buffer: _buffer });
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
exports.read = read;
|
|
54
|
+
async function writeFile(path, data) {
|
|
55
|
+
return new Promise((resolve, reject) => {
|
|
56
|
+
fs.writeFile(path, data, err => {
|
|
57
|
+
if (err)
|
|
58
|
+
reject(err);
|
|
59
|
+
else
|
|
60
|
+
resolve();
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
exports.writeFile = writeFile;
|
|
65
|
+
function writeFileSync(path, data) {
|
|
66
|
+
fs.writeFileSync(path, data);
|
|
67
|
+
}
|
|
68
|
+
exports.writeFileSync = writeFileSync;
|
|
69
|
+
async function readFile(path) {
|
|
70
|
+
return new Promise((resolve, reject) => {
|
|
71
|
+
fs.readFile(path, (err, buffer) => {
|
|
72
|
+
if (err)
|
|
73
|
+
reject(err);
|
|
74
|
+
else
|
|
75
|
+
resolve(buffer);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
exports.readFile = readFile;
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import { AbstractTokenizer } from './AbstractTokenizer';
|
|
3
|
-
import
|
|
4
|
-
import { IFileInfo, IReadChunkOptions } from './types';
|
|
5
|
-
export declare class ReadStreamTokenizer extends AbstractTokenizer {
|
|
6
|
-
private streamReader;
|
|
7
|
-
constructor(stream:
|
|
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
|
|
16
|
-
* @param options - Read behaviour options
|
|
17
|
-
* @returns Promise with number of bytes read
|
|
18
|
-
*/
|
|
19
|
-
readBuffer(
|
|
20
|
-
/**
|
|
21
|
-
* Peek (read ahead) buffer from tokenizer
|
|
22
|
-
* @param
|
|
23
|
-
* @param options - Read behaviour options
|
|
24
|
-
* @returns Promise with number of bytes peeked
|
|
25
|
-
*/
|
|
26
|
-
peekBuffer(
|
|
27
|
-
ignore(length: number): Promise<number>;
|
|
28
|
-
}
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { AbstractTokenizer } from './AbstractTokenizer';
|
|
3
|
+
import { Readable } from 'stream';
|
|
4
|
+
import { IFileInfo, IReadChunkOptions } from './types';
|
|
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,126 +1,98 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ReadStreamTokenizer = void 0;
|
|
4
|
-
const AbstractTokenizer_1 = require("./AbstractTokenizer");
|
|
5
|
-
const peek_readable_1 = require("peek-readable");
|
|
6
|
-
const maxBufferSize = 256000;
|
|
7
|
-
class ReadStreamTokenizer extends AbstractTokenizer_1.AbstractTokenizer {
|
|
8
|
-
constructor(stream, fileInfo) {
|
|
9
|
-
super(fileInfo);
|
|
10
|
-
this.streamReader = new peek_readable_1.StreamReader(stream);
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Get file information, an HTTP-client may implement this doing a HEAD request
|
|
14
|
-
* @return Promise with file information
|
|
15
|
-
*/
|
|
16
|
-
async getFileInfo() {
|
|
17
|
-
return this.fileInfo;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Read buffer from tokenizer
|
|
21
|
-
* @param
|
|
22
|
-
* @param options - Read behaviour options
|
|
23
|
-
* @returns Promise with number of bytes read
|
|
24
|
-
*/
|
|
25
|
-
async readBuffer(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
catch (err) {
|
|
100
|
-
if (options && options.mayBeLess && err instanceof peek_readable_1.EndOfStreamError) {
|
|
101
|
-
return 0;
|
|
102
|
-
}
|
|
103
|
-
throw err;
|
|
104
|
-
}
|
|
105
|
-
if ((!options || !options.mayBeLess) && bytesRead < length) {
|
|
106
|
-
throw new peek_readable_1.EndOfStreamError();
|
|
107
|
-
}
|
|
108
|
-
return bytesRead;
|
|
109
|
-
}
|
|
110
|
-
async ignore(length) {
|
|
111
|
-
// debug(`ignore ${this.position}...${this.position + length - 1}`);
|
|
112
|
-
const bufSize = Math.min(maxBufferSize, length);
|
|
113
|
-
const buf = new Uint8Array(bufSize);
|
|
114
|
-
let totBytesRead = 0;
|
|
115
|
-
while (totBytesRead < length) {
|
|
116
|
-
const remaining = length - totBytesRead;
|
|
117
|
-
const bytesRead = await this.readBuffer(buf, { length: Math.min(bufSize, remaining) });
|
|
118
|
-
if (bytesRead < 0) {
|
|
119
|
-
return bytesRead;
|
|
120
|
-
}
|
|
121
|
-
totBytesRead += bytesRead;
|
|
122
|
-
}
|
|
123
|
-
return totBytesRead;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
exports.ReadStreamTokenizer = ReadStreamTokenizer;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReadStreamTokenizer = void 0;
|
|
4
|
+
const AbstractTokenizer_1 = require("./AbstractTokenizer");
|
|
5
|
+
const peek_readable_1 = require("peek-readable");
|
|
6
|
+
const maxBufferSize = 256000;
|
|
7
|
+
class ReadStreamTokenizer extends AbstractTokenizer_1.AbstractTokenizer {
|
|
8
|
+
constructor(stream, fileInfo) {
|
|
9
|
+
super(fileInfo);
|
|
10
|
+
this.streamReader = new peek_readable_1.StreamReader(stream);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Get file information, an HTTP-client may implement this doing a HEAD request
|
|
14
|
+
* @return Promise with file information
|
|
15
|
+
*/
|
|
16
|
+
async getFileInfo() {
|
|
17
|
+
return this.fileInfo;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Read buffer from tokenizer
|
|
21
|
+
* @param uint8Array - Target Uint8Array to fill with data read from the tokenizer-stream
|
|
22
|
+
* @param options - Read behaviour options
|
|
23
|
+
* @returns Promise with number of bytes read
|
|
24
|
+
*/
|
|
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`');
|
|
34
|
+
}
|
|
35
|
+
if (normOptions.length === 0) {
|
|
36
|
+
return 0;
|
|
37
|
+
}
|
|
38
|
+
const bytesRead = await this.streamReader.read(uint8Array, normOptions.offset, normOptions.length);
|
|
39
|
+
this.position += bytesRead;
|
|
40
|
+
if ((!options || !options.mayBeLess) && bytesRead < normOptions.length) {
|
|
41
|
+
throw new peek_readable_1.EndOfStreamError();
|
|
42
|
+
}
|
|
43
|
+
return bytesRead;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Peek (read ahead) buffer from tokenizer
|
|
47
|
+
* @param uint8Array - Uint8Array (or Buffer) to write data to
|
|
48
|
+
* @param options - Read behaviour options
|
|
49
|
+
* @returns Promise with number of bytes peeked
|
|
50
|
+
*/
|
|
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;
|
|
61
|
+
}
|
|
62
|
+
else if (skipBytes < 0) {
|
|
63
|
+
throw new Error('Cannot peek from a negative offset in a stream');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (normOptions.length > 0) {
|
|
67
|
+
try {
|
|
68
|
+
bytesRead = await this.streamReader.peek(uint8Array, normOptions.offset, normOptions.length);
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
if (options && options.mayBeLess && err instanceof peek_readable_1.EndOfStreamError) {
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
throw err;
|
|
75
|
+
}
|
|
76
|
+
if ((!normOptions.mayBeLess) && bytesRead < normOptions.length) {
|
|
77
|
+
throw new peek_readable_1.EndOfStreamError();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return bytesRead;
|
|
81
|
+
}
|
|
82
|
+
async ignore(length) {
|
|
83
|
+
// debug(`ignore ${this.position}...${this.position + length - 1}`);
|
|
84
|
+
const bufSize = Math.min(maxBufferSize, length);
|
|
85
|
+
const buf = new Uint8Array(bufSize);
|
|
86
|
+
let totBytesRead = 0;
|
|
87
|
+
while (totBytesRead < length) {
|
|
88
|
+
const remaining = length - totBytesRead;
|
|
89
|
+
const bytesRead = await this.readBuffer(buf, { length: Math.min(bufSize, remaining) });
|
|
90
|
+
if (bytesRead < 0) {
|
|
91
|
+
return bytesRead;
|
|
92
|
+
}
|
|
93
|
+
totBytesRead += bytesRead;
|
|
94
|
+
}
|
|
95
|
+
return totBytesRead;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
exports.ReadStreamTokenizer = ReadStreamTokenizer;
|