@thi.ng/file-io 1.3.12 → 2.0.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**: 2024-03-27T09:53:45Z
3
+ - **Last updated**: 2024-03-29T12:16:20Z
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,18 @@ 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
+ # [2.0.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/file-io@2.0.0) (2024-03-29)
13
+
14
+ #### 🛑 Breaking changes
15
+
16
+ - add `streamHash()`, update other hashing fns ([64a8cad](https://github.com/thi-ng/umbrella/commit/64a8cad))
17
+ - BREAKING CHANGE: `fileHash()` now async, rename `stringHash()` => `bufferHash()`
18
+
19
+ #### 🚀 Features
20
+
21
+ - update args for `createTempFile()` & `tempFilePath()` ([d944789](https://github.com/thi-ng/umbrella/commit/d944789))
22
+ - add docstrings
23
+
12
24
  ### [1.3.3](https://github.com/thi-ng/umbrella/tree/@thi.ng/file-io@1.3.3) (2024-02-22)
13
25
 
14
26
  #### ♻️ Refactoring
package/README.md CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  ## About
26
26
 
27
- Assorted file I/O utils (with logging support) for NodeJS.
27
+ Assorted file I/O utils (with logging support) for NodeJS/Bun.
28
28
 
29
29
  Most functions in this package have optional support for the
30
30
  [`ILogger`](https://github.com/thi-ng/umbrella/tree/develop/packages/logger)
@@ -48,7 +48,7 @@ For Node.js REPL:
48
48
  const fileIo = await import("@thi.ng/file-io");
49
49
  ```
50
50
 
51
- Package sizes (brotli'd, pre-treeshake): ESM: 1.99 KB
51
+ Package sizes (brotli'd, pre-treeshake): ESM: 2.04 KB
52
52
 
53
53
  ## Dependencies
54
54
 
package/hash.d.ts CHANGED
@@ -1,5 +1,37 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ import type { TypedArray } from "@thi.ng/api";
1
4
  import type { ILogger } from "@thi.ng/logger";
5
+ import type { Readable } from "node:stream";
2
6
  export type HashAlgo = "gost-mac" | "md4" | "md5" | "md_gost94" | "ripemd160" | "sha1" | "sha224" | "sha256" | "sha384" | "sha512" | "streebog256" | "streebog512" | "whirlpool";
3
- export declare const fileHash: (path: string, logger?: ILogger, algo?: HashAlgo) => string;
4
- export declare const stringHash: (src: string, logger?: ILogger, algo?: HashAlgo) => string;
7
+ /**
8
+ * Creates a readable stream for given file and computes its hash digest using
9
+ * {@link streamHash}.
10
+ *
11
+ * @param path
12
+ * @param logger
13
+ * @param algo
14
+ */
15
+ export declare const fileHash: (path: string, logger?: ILogger, algo?: HashAlgo) => Promise<string>;
16
+ /**
17
+ * Computes hash digest from given stream using chosen hash algorithm (default:
18
+ * "sha256"). If `logger` is given, the hash will be logged too.
19
+ *
20
+ * @remarks
21
+ * Also see {@link fileHash} and {@link stringHash}.
22
+ *
23
+ * @param src
24
+ * @param logger
25
+ * @param algo
26
+ */
27
+ export declare const streamHash: (src: Readable, logger?: ILogger, algo?: HashAlgo) => Promise<string>;
28
+ /**
29
+ * Computes hash digest from given string or buffer using chosen hash algorithm
30
+ * (default: "sha256"). If `logger` is given, the hash will be logged too.
31
+ *
32
+ * @param src
33
+ * @param logger
34
+ * @param algo
35
+ */
36
+ export declare const bufferHash: (src: TypedArray | Buffer | DataView | string, logger?: ILogger, algo?: HashAlgo) => string;
5
37
  //# sourceMappingURL=hash.d.ts.map
package/hash.js CHANGED
@@ -1,20 +1,24 @@
1
1
  import { createHash } from "node:crypto";
2
- import { readFileSync } from "node:fs";
3
- const fileHash = (path, logger, algo = "sha256") => {
2
+ import { createReadStream } from "node:fs";
3
+ const fileHash = async (path, logger, algo = "sha256") => {
4
+ logger && logger.info("reading file:", path);
5
+ return await streamHash(createReadStream(path), logger, algo);
6
+ };
7
+ const streamHash = async (src, logger, algo = "sha256") => {
4
8
  const sum = createHash(algo);
5
- sum.update(readFileSync(path));
9
+ for await (let chunk of src)
10
+ sum.update(chunk);
6
11
  const hash = sum.digest("hex");
7
- logger && logger.info(`${algo} hash for ${path}: ${hash}`);
12
+ logger && logger.info(`${algo} hash: ${hash}`);
8
13
  return hash;
9
14
  };
10
- const stringHash = (src, logger, algo = "sha256") => {
11
- const sum = createHash(algo);
12
- sum.update(src);
13
- const hash = sum.digest("hex");
14
- logger && logger.info(`${algo} hash for string: ${hash}`);
15
+ const bufferHash = (src, logger, algo = "sha256") => {
16
+ const hash = createHash(algo).update(src).digest("hex");
17
+ logger && logger.info(`${algo} hash: ${hash}`);
15
18
  return hash;
16
19
  };
17
20
  export {
21
+ bufferHash,
18
22
  fileHash,
19
- stringHash
23
+ streamHash
20
24
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@thi.ng/file-io",
3
- "version": "1.3.12",
4
- "description": "Assorted file I/O utils (with logging support) for NodeJS",
3
+ "version": "2.0.0",
4
+ "description": "Assorted file I/O utils (with logging support) for NodeJS/Bun",
5
5
  "type": "module",
6
6
  "module": "./index.js",
7
7
  "typings": "./index.d.ts",
@@ -50,11 +50,13 @@
50
50
  "typescript": "^5.4.3"
51
51
  },
52
52
  "keywords": [
53
+ "async",
53
54
  "file",
54
55
  "hash",
55
56
  "json",
56
57
  "logger",
57
58
  "nodejs",
59
+ "stream",
58
60
  "no-browser",
59
61
  "typescript"
60
62
  ],
@@ -121,5 +123,5 @@
121
123
  "status": "stable",
122
124
  "year": 2022
123
125
  },
124
- "gitHead": "feb3b24654f2c931cd3c3308c1c0c807ee14d0e4\n"
126
+ "gitHead": "ce5ae2a322d50a7ce8ecccbd94fa55c496ba04fd\n"
125
127
  }
package/temp.d.ts CHANGED
@@ -1,5 +1,26 @@
1
1
  import type { TypedArray } from "@thi.ng/api";
2
2
  import type { ILogger } from "@thi.ng/logger";
3
- export declare const createTempFile: (body: string | TypedArray, logger?: ILogger, name?: string) => string;
4
- export declare const tempFilePath: (name?: string) => string;
3
+ /**
4
+ * Constructs a temp file path using {@link tempFilePath} and writes `body` to
5
+ * this path, then returns path. If `name` is given and contains
6
+ * sub-directories, they will be created automatically.
7
+ *
8
+ * @param body
9
+ * @param logger
10
+ * @param name
11
+ * @param ext
12
+ */
13
+ export declare const createTempFile: (body: string | TypedArray, logger?: ILogger, name?: string, ext?: string) => string;
14
+ /**
15
+ * Constructs a file path in the system-defined temp directory, optionally using
16
+ * provided basename and/or file extension.
17
+ *
18
+ * @remarks
19
+ * If no `name` is given, constructs a random filename of `tmp-XXX` (16 random
20
+ * chars).
21
+ *
22
+ * @param name
23
+ * @param ext
24
+ */
25
+ export declare const tempFilePath: (name?: string, ext?: string) => string;
5
26
  //# sourceMappingURL=temp.d.ts.map
package/temp.js CHANGED
@@ -4,14 +4,14 @@ import { realpathSync, writeFileSync } from "node:fs";
4
4
  import { tmpdir } from "node:os";
5
5
  import { sep } from "node:path";
6
6
  import { ensureDirForFile } from "./dir.js";
7
- const createTempFile = (body, logger, name) => {
8
- const path = tempFilePath(name);
7
+ const createTempFile = (body, logger, name, ext) => {
8
+ const path = tempFilePath(name, ext);
9
9
  logger && logger.debug("creating temp file:", path);
10
10
  ensureDirForFile(path);
11
11
  writeFileSync(path, body, isString(body) ? "utf-8" : void 0);
12
12
  return path;
13
13
  };
14
- const tempFilePath = (name) => realpathSync(tmpdir()) + sep + (name || randomID(16, "tmp-"));
14
+ const tempFilePath = (name, ext = "") => realpathSync(tmpdir()) + sep + (name || randomID(16, "tmp-")) + ext;
15
15
  export {
16
16
  createTempFile,
17
17
  tempFilePath