@thi.ng/file-io 0.3.25 → 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**: 2022-12-29T20:56:59Z
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,19 @@ 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
+
18
+ ## [0.4.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/file-io@0.4.0) (2023-01-10)
19
+
20
+ #### 🚀 Features
21
+
22
+ - add readText() encoding opts ([22366c0](https://github.com/thi-ng/umbrella/commit/22366c0))
23
+ - add readBinary(), update pkg exports ([2c647ed](https://github.com/thi-ng/umbrella/commit/2c647ed))
24
+
12
25
  ### [0.3.25](https://github.com/thi-ng/umbrella/tree/@thi.ng/file-io@0.3.25) (2022-12-29)
13
26
 
14
27
  #### ♻️ Refactoring
package/README.md CHANGED
@@ -43,7 +43,7 @@ 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: 997 bytes
46
+ Package sizes (brotli'd, pre-treeshake): ESM: 1.03 KB
47
47
 
48
48
  ## Dependencies
49
49
 
@@ -75,4 +75,4 @@ If this project contributes to an academic publication, please cite it as:
75
75
 
76
76
  ## License
77
77
 
78
- © 2022 Karsten Schmidt // Apache License 2.0
78
+ © 2022 - 2023 Karsten Schmidt // Apache License 2.0
@@ -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,10 +1,12 @@
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";
7
8
  export * from "./mask.js";
9
+ export * from "./read.js";
8
10
  export * from "./temp.js";
9
11
  export * from "./text.js";
10
12
  export * from "./write.js";
package/index.js CHANGED
@@ -1,10 +1,12 @@
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";
7
8
  export * from "./mask.js";
9
+ export * from "./read.js";
8
10
  export * from "./temp.js";
9
11
  export * from "./text.js";
10
12
  export * from "./write.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/file-io",
3
- "version": "0.3.25",
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.2",
38
- "@thi.ng/checks": "^3.3.6",
39
- "@thi.ng/logger": "^1.4.6",
40
- "@thi.ng/random": "^3.3.20"
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.8",
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
  },
@@ -95,6 +99,9 @@
95
99
  "./mask": {
96
100
  "default": "./mask.js"
97
101
  },
102
+ "./read": {
103
+ "default": "./read.js"
104
+ },
98
105
  "./temp": {
99
106
  "default": "./temp.js"
100
107
  },
@@ -109,5 +116,5 @@
109
116
  "status": "stable",
110
117
  "year": 2022
111
118
  },
112
- "gitHead": "28bb74c67217a352d673b6efdab234921d4a370e\n"
119
+ "gitHead": "50ba9c87676fac60c46d2bc0e4d2c7711a374a68\n"
113
120
  }
package/read.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import type { ILogger } from "@thi.ng/logger";
2
+ /**
3
+ * Reads given file `path` into a byte array.
4
+ *
5
+ * @param path
6
+ * @param logger
7
+ */
8
+ export declare const readBinary: (path: string, logger?: ILogger) => Uint8Array;
9
+ //# sourceMappingURL=read.d.ts.map
package/read.js ADDED
@@ -0,0 +1,12 @@
1
+ import { readFileSync } from "fs";
2
+ /**
3
+ * Reads given file `path` into a byte array.
4
+ *
5
+ * @param path
6
+ * @param logger
7
+ */
8
+ export const readBinary = (path, logger) => {
9
+ logger && logger.debug("reading file:", path);
10
+ const buf = readFileSync(path);
11
+ return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
12
+ };
package/text.d.ts CHANGED
@@ -1,5 +1,14 @@
1
+ /// <reference types="node" />
1
2
  import type { ILogger } from "@thi.ng/logger";
2
- export declare const readText: (path: string, logger?: ILogger) => string;
3
+ /**
4
+ * Reads text from given file `path`, optionally with custom encoding (default:
5
+ * UTF-8).
6
+ *
7
+ * @param path
8
+ * @param logger
9
+ * @param encoding
10
+ */
11
+ export declare const readText: (path: string, logger?: ILogger, encoding?: Extract<BufferEncoding, "ascii" | "latin1" | "utf-8" | "utf-16le" | "ucs-2">) => string;
3
12
  /**
4
13
  * Writes `body` as UTF-8 file to given `path`. If `dryRun` is true (default:
5
14
  * false), the file WON'T be written, however if a `logger` is provided then at
package/text.js CHANGED
@@ -1,9 +1,17 @@
1
1
  import { isArray } from "@thi.ng/checks/is-array";
2
2
  import { readFileSync } from "fs";
3
3
  import { writeFile } from "./write.js";
4
- export const readText = (path, logger) => {
4
+ /**
5
+ * Reads text from given file `path`, optionally with custom encoding (default:
6
+ * UTF-8).
7
+ *
8
+ * @param path
9
+ * @param logger
10
+ * @param encoding
11
+ */
12
+ export const readText = (path, logger, encoding = "utf-8") => {
5
13
  logger && logger.debug("reading file:", path);
6
- return readFileSync(path, "utf-8");
14
+ return readFileSync(path, encoding);
7
15
  };
8
16
  /**
9
17
  * Writes `body` as UTF-8 file to given `path`. If `dryRun` is true (default: