@thi.ng/file-io 1.3.12 → 2.0.1
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 +13 -1
- package/README.md +10 -4
- package/hash.d.ts +34 -2
- package/hash.js +14 -10
- package/package.json +11 -8
- package/temp.d.ts +23 -2
- package/temp.js +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2024-
|
|
3
|
+
- **Last updated**: 2024-04-08T14:59:29Z
|
|
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
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
[](https://mastodon.thi.ng/@toxi)
|
|
8
8
|
|
|
9
9
|
> [!NOTE]
|
|
10
|
-
> This is one of
|
|
10
|
+
> This is one of 191 standalone projects, maintained as part
|
|
11
11
|
> of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
|
|
12
12
|
> and anti-framework.
|
|
13
13
|
>
|
|
@@ -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)
|
|
@@ -42,13 +42,19 @@ logging interface.
|
|
|
42
42
|
yarn add @thi.ng/file-io
|
|
43
43
|
```
|
|
44
44
|
|
|
45
|
+
ESM import:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import * as fio from "@thi.ng/file-io";
|
|
49
|
+
```
|
|
50
|
+
|
|
45
51
|
For Node.js REPL:
|
|
46
52
|
|
|
47
53
|
```js
|
|
48
|
-
const
|
|
54
|
+
const fio = await import("@thi.ng/file-io");
|
|
49
55
|
```
|
|
50
56
|
|
|
51
|
-
Package sizes (brotli'd, pre-treeshake): ESM:
|
|
57
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 2.04 KB
|
|
52
58
|
|
|
53
59
|
## Dependencies
|
|
54
60
|
|
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
|
-
|
|
4
|
-
|
|
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 {
|
|
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
|
-
|
|
9
|
+
for await (let chunk of src)
|
|
10
|
+
sum.update(chunk);
|
|
6
11
|
const hash = sum.digest("hex");
|
|
7
|
-
logger && logger.info(`${algo} hash
|
|
12
|
+
logger && logger.info(`${algo} hash: ${hash}`);
|
|
8
13
|
return hash;
|
|
9
14
|
};
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
|
|
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
|
-
|
|
23
|
+
streamHash
|
|
20
24
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/file-io",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Assorted file I/O utils (with logging support) for NodeJS",
|
|
3
|
+
"version": "2.0.1",
|
|
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",
|
|
@@ -36,11 +36,11 @@
|
|
|
36
36
|
"tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@thi.ng/api": "^8.
|
|
40
|
-
"@thi.ng/checks": "^3.
|
|
41
|
-
"@thi.ng/hex": "^2.3.
|
|
42
|
-
"@thi.ng/logger": "^3.0.
|
|
43
|
-
"@thi.ng/random": "^3.7.
|
|
39
|
+
"@thi.ng/api": "^8.10.0",
|
|
40
|
+
"@thi.ng/checks": "^3.6.0",
|
|
41
|
+
"@thi.ng/hex": "^2.3.42",
|
|
42
|
+
"@thi.ng/logger": "^3.0.8",
|
|
43
|
+
"@thi.ng/random": "^3.7.2"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@microsoft/api-extractor": "^7.43.0",
|
|
@@ -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
|
],
|
|
@@ -118,8 +120,9 @@
|
|
|
118
120
|
}
|
|
119
121
|
},
|
|
120
122
|
"thi.ng": {
|
|
123
|
+
"alias": "fio",
|
|
121
124
|
"status": "stable",
|
|
122
125
|
"year": 2022
|
|
123
126
|
},
|
|
124
|
-
"gitHead": "
|
|
127
|
+
"gitHead": "85ac4bd4d6d89f8e3689e2863d5bea0cecdb371c\n"
|
|
125
128
|
}
|
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
|
-
|
|
4
|
-
|
|
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
|