@thi.ng/file-io 0.4.0 → 0.5.2
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/CHANGELOG.md +7 -1
- package/README.md +2 -1
- package/file-chunks.d.ts +42 -0
- package/file-chunks.js +48 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +15 -11
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2023-
|
|
3
|
+
- **Last updated**: 2023-02-10T14:03:10Z
|
|
4
4
|
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
5
|
|
|
6
6
|
All notable changes to this project will be documented in this file.
|
|
@@ -9,6 +9,12 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
9
9
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
10
10
|
and/or version bumps of transitive dependencies.
|
|
11
11
|
|
|
12
|
+
## [0.5.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/file-io@0.5.0) (2023-02-05)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- add fileChunks() ([bcff691](https://github.com/thi-ng/umbrella/commit/bcff691))
|
|
17
|
+
|
|
12
18
|
## [0.4.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/file-io@0.4.0) (2023-01-10)
|
|
13
19
|
|
|
14
20
|
#### 🚀 Features
|
package/README.md
CHANGED
|
@@ -43,12 +43,13 @@ For Node.js REPL:
|
|
|
43
43
|
const fileIo = await import("@thi.ng/file-io");
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
-
Package sizes (brotli'd, pre-treeshake): ESM: 1.
|
|
46
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 1.25 KB
|
|
47
47
|
|
|
48
48
|
## Dependencies
|
|
49
49
|
|
|
50
50
|
- [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api)
|
|
51
51
|
- [@thi.ng/checks](https://github.com/thi-ng/umbrella/tree/develop/packages/checks)
|
|
52
|
+
- [@thi.ng/hex](https://github.com/thi-ng/umbrella/tree/develop/packages/hex)
|
|
52
53
|
- [@thi.ng/logger](https://github.com/thi-ng/umbrella/tree/develop/packages/logger)
|
|
53
54
|
- [@thi.ng/random](https://github.com/thi-ng/umbrella/tree/develop/packages/random)
|
|
54
55
|
|
package/file-chunks.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { ILogger } from "@thi.ng/logger";
|
|
3
|
+
export interface FileChunkOpts {
|
|
4
|
+
/**
|
|
5
|
+
* Optional logger instance
|
|
6
|
+
*/
|
|
7
|
+
logger: ILogger;
|
|
8
|
+
/**
|
|
9
|
+
* Chunk size (in bytes).
|
|
10
|
+
*
|
|
11
|
+
* @defaultValue 1024
|
|
12
|
+
*/
|
|
13
|
+
size: number;
|
|
14
|
+
/**
|
|
15
|
+
* Read start position (in bytes)
|
|
16
|
+
*
|
|
17
|
+
* @defaultValue 0
|
|
18
|
+
*/
|
|
19
|
+
start: number;
|
|
20
|
+
/**
|
|
21
|
+
* Read end position (in bytes)
|
|
22
|
+
*
|
|
23
|
+
* @defaultValue Infinity
|
|
24
|
+
*/
|
|
25
|
+
end: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Async iterator. Yields chunks of byte buffers from given file. User
|
|
29
|
+
* configurable size and byte ranges.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* for await(let buf of fileChunks("file.bin", { start: 16*1024*1024 })) {
|
|
34
|
+
* // ...
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @param path
|
|
39
|
+
* @param opts
|
|
40
|
+
*/
|
|
41
|
+
export declare function fileChunks(path: string, opts?: Partial<FileChunkOpts>): AsyncGenerator<Buffer, void, unknown>;
|
|
42
|
+
//# sourceMappingURL=file-chunks.d.ts.map
|
package/file-chunks.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { U32 } from "@thi.ng/hex";
|
|
2
|
+
import { open } from "fs/promises";
|
|
3
|
+
/**
|
|
4
|
+
* Async iterator. Yields chunks of byte buffers from given file. User
|
|
5
|
+
* configurable size and byte ranges.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* for await(let buf of fileChunks("file.bin", { start: 16*1024*1024 })) {
|
|
10
|
+
* // ...
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @param path
|
|
15
|
+
* @param opts
|
|
16
|
+
*/
|
|
17
|
+
export async function* fileChunks(path, opts) {
|
|
18
|
+
let { logger, size, start, end } = {
|
|
19
|
+
size: 1024,
|
|
20
|
+
start: 0,
|
|
21
|
+
end: Infinity,
|
|
22
|
+
...opts,
|
|
23
|
+
};
|
|
24
|
+
logger &&
|
|
25
|
+
logger.debug(`start reading file chunks (size: 0x${size}): ${path}`);
|
|
26
|
+
let fd = undefined;
|
|
27
|
+
try {
|
|
28
|
+
fd = await open(path, "r");
|
|
29
|
+
while (start < end) {
|
|
30
|
+
logger &&
|
|
31
|
+
logger.debug(`reading chunk: 0x${U32(start)} - 0x${U32(start + size - 1)} (${path})`);
|
|
32
|
+
const { buffer, bytesRead } = await fd.read({
|
|
33
|
+
buffer: Buffer.alloc(size),
|
|
34
|
+
length: size,
|
|
35
|
+
position: start,
|
|
36
|
+
});
|
|
37
|
+
if (bytesRead === 0)
|
|
38
|
+
break;
|
|
39
|
+
yield buffer;
|
|
40
|
+
if (bytesRead < size)
|
|
41
|
+
break;
|
|
42
|
+
start += bytesRead;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
finally {
|
|
46
|
+
await fd?.close();
|
|
47
|
+
}
|
|
48
|
+
}
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/file-io",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "Assorted file I/O utils (with logging support) for NodeJS",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -34,18 +34,19 @@
|
|
|
34
34
|
"test": "testament test"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@thi.ng/api": "^8.
|
|
38
|
-
"@thi.ng/checks": "^3.3.
|
|
39
|
-
"@thi.ng/
|
|
40
|
-
"@thi.ng/
|
|
37
|
+
"@thi.ng/api": "^8.7.2",
|
|
38
|
+
"@thi.ng/checks": "^3.3.9",
|
|
39
|
+
"@thi.ng/hex": "^2.3.6",
|
|
40
|
+
"@thi.ng/logger": "^1.4.9",
|
|
41
|
+
"@thi.ng/random": "^3.3.24"
|
|
41
42
|
},
|
|
42
43
|
"devDependencies": {
|
|
43
|
-
"@microsoft/api-extractor": "^7.
|
|
44
|
-
"@thi.ng/testament": "^0.3.
|
|
45
|
-
"rimraf": "^
|
|
44
|
+
"@microsoft/api-extractor": "^7.34.2",
|
|
45
|
+
"@thi.ng/testament": "^0.3.11",
|
|
46
|
+
"rimraf": "^4.1.2",
|
|
46
47
|
"tools": "^0.0.1",
|
|
47
|
-
"typedoc": "^0.23.
|
|
48
|
-
"typescript": "^4.9.
|
|
48
|
+
"typedoc": "^0.23.24",
|
|
49
|
+
"typescript": "^4.9.5"
|
|
49
50
|
},
|
|
50
51
|
"keywords": [
|
|
51
52
|
"file",
|
|
@@ -83,6 +84,9 @@
|
|
|
83
84
|
"./ext": {
|
|
84
85
|
"default": "./ext.js"
|
|
85
86
|
},
|
|
87
|
+
"./file-chunks": {
|
|
88
|
+
"default": "./file-chunks.js"
|
|
89
|
+
},
|
|
86
90
|
"./files": {
|
|
87
91
|
"default": "./files.js"
|
|
88
92
|
},
|
|
@@ -112,5 +116,5 @@
|
|
|
112
116
|
"status": "stable",
|
|
113
117
|
"year": 2022
|
|
114
118
|
},
|
|
115
|
-
"gitHead": "
|
|
119
|
+
"gitHead": "cafa4ecea90fb681949dc3885a5bd6ddefa38b51\n"
|
|
116
120
|
}
|