@thi.ng/file-io 0.4.0 → 0.5.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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-01-10T15:20:18Z
3
+ - **Last updated**: 2023-02-05T14:42:21Z
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
@@ -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
@@ -1,6 +1,7 @@
1
1
  export * from "./delete.js";
2
2
  export * from "./dir.js";
3
3
  export * from "./ext.js";
4
+ export * from "./file-chunks.js";
4
5
  export * from "./files.js";
5
6
  export * from "./hash.js";
6
7
  export * from "./json.js";
package/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from "./delete.js";
2
2
  export * from "./dir.js";
3
3
  export * from "./ext.js";
4
+ export * from "./file-chunks.js";
4
5
  export * from "./files.js";
5
6
  export * from "./hash.js";
6
7
  export * from "./json.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/file-io",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
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.6.3",
38
- "@thi.ng/checks": "^3.3.7",
39
- "@thi.ng/logger": "^1.4.7",
40
- "@thi.ng/random": "^3.3.21"
37
+ "@thi.ng/api": "^8.7.0",
38
+ "@thi.ng/checks": "^3.3.8",
39
+ "@thi.ng/hex": "^2.3.5",
40
+ "@thi.ng/logger": "^1.4.8",
41
+ "@thi.ng/random": "^3.3.22"
41
42
  },
42
43
  "devDependencies": {
43
- "@microsoft/api-extractor": "^7.33.7",
44
- "@thi.ng/testament": "^0.3.9",
45
- "rimraf": "^3.0.2",
44
+ "@microsoft/api-extractor": "^7.34.2",
45
+ "@thi.ng/testament": "^0.3.10",
46
+ "rimraf": "^4.1.2",
46
47
  "tools": "^0.0.1",
47
- "typedoc": "^0.23.22",
48
- "typescript": "^4.9.4"
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": "3f0b3e2a7c82aefc7e46fb4338369836b5e1b8cf\n"
119
+ "gitHead": "50ba9c87676fac60c46d2bc0e4d2c7711a374a68\n"
116
120
  }