@little-nebulae/node-fs 0.1.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/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ryan Luong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # Node fs library
@@ -0,0 +1,53 @@
1
+ import { UnexpectedError } from "@little-nebulae/error";
2
+ import { Result } from "@little-nebulae/result";
3
+ import { AccessDeniedSystemError, FileTooBigSystemError, IsDirectorySystemError, NameTooLongSystemError, NoEntrySystemError, NotDirectorySystemError, OutOfMemorySystemError, OverflowSystemError, PermissionDeniedSystemError } from "@little-nebulae/system-error";
4
+ //#region src/constants.d.ts
5
+ export declare const DEFAULT_CHARACTER_ENCODING = "utf-8";
6
+ //#endregion
7
+ //#region src/ops/dir/make.d.ts
8
+ export declare function makeDirectory({ path }: {
9
+ path: string;
10
+ }): Promise<Result<undefined, UnexpectedError>>;
11
+ //#endregion
12
+ //#region src/ops/dir/make-recursively.d.ts
13
+ export declare function makeDirectoryRecursively({ path }: {
14
+ path: string;
15
+ }): Promise<Result<string | undefined, UnexpectedError>>;
16
+ //#endregion
17
+ //#region src/ops/dir/read.d.ts
18
+ export declare function readDirectory({ path, recursive }: {
19
+ path: string;
20
+ recursive?: boolean;
21
+ }): Promise<Result<string[], UnexpectedError>>;
22
+ //#endregion
23
+ //#region src/ops/file/read-text.d.ts
24
+ export declare function readTextFile({ path, signal }: {
25
+ path: string;
26
+ signal?: AbortSignal;
27
+ }): Promise<Result<string, AccessDeniedSystemError | FileTooBigSystemError | IsDirectorySystemError | NameTooLongSystemError | NoEntrySystemError | NotDirectorySystemError | OutOfMemorySystemError | OverflowSystemError | PermissionDeniedSystemError | UnexpectedError>>;
28
+ //#endregion
29
+ //#region src/ops/file/write-text.d.ts
30
+ export declare function writeTextFile({ path, text }: {
31
+ path: string;
32
+ text: string;
33
+ }): Promise<Result<undefined, UnexpectedError>>;
34
+ //#endregion
35
+ //#region src/ops/remove-fs-entry.d.ts
36
+ export declare function removeFsEntry({ path, recursive, force, retry }: {
37
+ path: string;
38
+ recursive?: boolean;
39
+ force?: boolean;
40
+ retry?: {
41
+ enabled: false;
42
+ } | {
43
+ enabled: true;
44
+ maxTimes: number;
45
+ delay: number;
46
+ };
47
+ }): Promise<import("@little-nebulae/result").Failure<UnexpectedError<unknown, null>> | import("@little-nebulae/result").Success<undefined>>;
48
+ //#endregion
49
+ //#region src/ops/resolve-real-path.d.ts
50
+ export declare function resolveRealPath({ path }: {
51
+ path: string;
52
+ }): Promise<Result<string, UnexpectedError>>;
53
+ //#endregion
package/dist/index.mjs ADDED
@@ -0,0 +1,186 @@
1
+ import { UnexpectedError, composeErrorMessage } from "@little-nebulae/error";
2
+ import { fail, succeed } from "@little-nebulae/result";
3
+ import { mkdir, readFile, readdir, realpath, rm, writeFile } from "node:fs/promises";
4
+ import { AccessDeniedSystemError, FileTooBigSystemError, IsDirectorySystemError, NameTooLongSystemError, NoEntrySystemError, NotDirectorySystemError, OutOfMemorySystemError, OverflowSystemError, PermissionDeniedSystemError, SYSTEM_ERROR_RECORD, isErrnoException } from "@little-nebulae/system-error";
5
+ //#region src/constants.ts
6
+ const DEFAULT_CHARACTER_ENCODING = "utf-8";
7
+ //#endregion
8
+ //#region src/ops/dir/make.ts
9
+ async function makeDirectory({ path }) {
10
+ try {
11
+ await mkdir(path);
12
+ return succeed(void 0);
13
+ } catch (error) {
14
+ return fail(new UnexpectedError({
15
+ message: composeErrorMessage({
16
+ operation: "make directory",
17
+ reason: "some unexpected error"
18
+ }),
19
+ cause: error,
20
+ meta: null
21
+ }));
22
+ }
23
+ }
24
+ //#endregion
25
+ //#region src/ops/dir/make-recursively.ts
26
+ async function makeDirectoryRecursively({ path }) {
27
+ try {
28
+ const createdDirPath = await mkdir(path, { recursive: true });
29
+ return succeed(createdDirPath);
30
+ } catch (error) {
31
+ return fail(new UnexpectedError({
32
+ message: composeErrorMessage({
33
+ operation: "make directory recursively",
34
+ reason: "some unexpected error"
35
+ }),
36
+ cause: error,
37
+ meta: null
38
+ }));
39
+ }
40
+ }
41
+ //#endregion
42
+ //#region src/ops/dir/read.ts
43
+ async function readDirectory({ path, recursive = false }) {
44
+ try {
45
+ const entryPaths = await readdir(path, {
46
+ encoding: DEFAULT_CHARACTER_ENCODING,
47
+ recursive
48
+ });
49
+ return succeed(entryPaths);
50
+ } catch (error) {
51
+ return fail(new UnexpectedError({
52
+ message: composeErrorMessage({
53
+ operation: "read directory",
54
+ reason: "some unexpected error"
55
+ }),
56
+ cause: error,
57
+ meta: null
58
+ }));
59
+ }
60
+ }
61
+ //#endregion
62
+ //#region src/ops/file/read-text.ts
63
+ async function readTextFile({ path, signal }) {
64
+ try {
65
+ const text = await readFile(path, {
66
+ encoding: DEFAULT_CHARACTER_ENCODING,
67
+ signal
68
+ });
69
+ return succeed(text);
70
+ } catch (error) {
71
+ if (isErrnoException(error)) switch (-(error.errno ?? 0)) {
72
+ case SYSTEM_ERROR_RECORD.PERMISSION_DENIED_SYSTEM_ERROR.errno.number: return fail(new PermissionDeniedSystemError({
73
+ message: `The operation to read file at ${path} was prevented by a file seal.`,
74
+ cause: error,
75
+ meta: null
76
+ }));
77
+ case SYSTEM_ERROR_RECORD.NO_ENTRY_SYSTEM_ERROR.errno.number: return fail(new NoEntrySystemError({
78
+ message: `${path} does not exist or is a
79
+ dangling symbolic link.`,
80
+ cause: error,
81
+ meta: null
82
+ }));
83
+ case SYSTEM_ERROR_RECORD.OUT_OF_MEMORY_SYSTEM_ERROR.errno.number: return fail(new OutOfMemorySystemError({
84
+ message: "Insufficient kernel memory was available.",
85
+ cause: error,
86
+ meta: null
87
+ }));
88
+ case SYSTEM_ERROR_RECORD.ACCESS_DENIED_SYSTEM_ERROR.errno.number: return fail(new AccessDeniedSystemError({
89
+ message: `The requested access to the file is not allowed, or search permission is denied for one of the directories in the path prefix of ${path}`,
90
+ cause: error,
91
+ meta: null
92
+ }));
93
+ case SYSTEM_ERROR_RECORD.NOT_DIRECTORY_SYSTEM_ERROR.errno.number: return fail(new NotDirectorySystemError({
94
+ message: `A component used as a directory in ${path} is not a directory.`,
95
+ cause: error,
96
+ meta: null
97
+ }));
98
+ case SYSTEM_ERROR_RECORD.IS_DIRECTORY_SYSTEM_ERROR.errno.number: return fail(new IsDirectorySystemError({
99
+ message: `${path} is a directory, not a file.`,
100
+ cause: error,
101
+ meta: null
102
+ }));
103
+ case SYSTEM_ERROR_RECORD.FILE_TOO_BIG_SYSTEM_ERROR.errno.number: return fail(new FileTooBigSystemError({
104
+ message: `${path} refers to a file that is too big to be opened.`,
105
+ cause: error,
106
+ meta: null
107
+ }));
108
+ case SYSTEM_ERROR_RECORD.NAME_TOO_LONG_SYSTEM_ERROR.errno.number: return fail(new NameTooLongSystemError({
109
+ message: `${path} or one of its components is too long.`,
110
+ cause: error,
111
+ meta: null
112
+ }));
113
+ case SYSTEM_ERROR_RECORD.OVERFLOW_SYSTEM_ERROR.errno.number: return fail(new OverflowSystemError({
114
+ message: `${path} refers to a regular file that is too large to be opened.`,
115
+ cause: error,
116
+ meta: null
117
+ }));
118
+ }
119
+ return fail(new UnexpectedError({
120
+ message: composeErrorMessage({
121
+ operation: "read text file",
122
+ reason: "some unexpected error"
123
+ }),
124
+ cause: error,
125
+ meta: null
126
+ }));
127
+ }
128
+ }
129
+ //#endregion
130
+ //#region src/ops/file/write-text.ts
131
+ async function writeTextFile({ path, text }) {
132
+ try {
133
+ await writeFile(path, text, { encoding: DEFAULT_CHARACTER_ENCODING });
134
+ return succeed(void 0);
135
+ } catch (error) {
136
+ return fail(new UnexpectedError({
137
+ message: composeErrorMessage({
138
+ operation: "write text file",
139
+ reason: "some unexpected error"
140
+ }),
141
+ cause: error,
142
+ meta: null
143
+ }));
144
+ }
145
+ }
146
+ //#endregion
147
+ //#region src/ops/remove-fs-entry.ts
148
+ async function removeFsEntry({ path, recursive = false, force = false, retry = { enabled: false } }) {
149
+ try {
150
+ await rm(path, {
151
+ recursive,
152
+ force,
153
+ maxRetries: retry.enabled ? retry.maxTimes : void 0,
154
+ retryDelay: retry.enabled ? retry.delay : void 0
155
+ });
156
+ return succeed(void 0);
157
+ } catch (error) {
158
+ return fail(new UnexpectedError({
159
+ message: composeErrorMessage({
160
+ operation: `remove fs entry at ${path}`,
161
+ reason: "some unexpected error"
162
+ }),
163
+ cause: error,
164
+ meta: null
165
+ }));
166
+ }
167
+ }
168
+ //#endregion
169
+ //#region src/ops/resolve-real-path.ts
170
+ async function resolveRealPath({ path }) {
171
+ try {
172
+ const realPath = await realpath(path, { encoding: DEFAULT_CHARACTER_ENCODING });
173
+ return succeed(realPath);
174
+ } catch (error) {
175
+ return fail(new UnexpectedError({
176
+ message: composeErrorMessage({
177
+ operation: "resolve real path",
178
+ reason: "some unexpected error"
179
+ }),
180
+ cause: error,
181
+ meta: null
182
+ }));
183
+ }
184
+ }
185
+ //#endregion
186
+ export { DEFAULT_CHARACTER_ENCODING, makeDirectory, makeDirectoryRecursively, readDirectory, readTextFile, removeFsEntry, resolveRealPath, writeTextFile };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@little-nebulae/node-fs",
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "type": "module",
9
+ "exports": {
10
+ ".": "./dist/index.mjs",
11
+ "./package.json": "./package.json"
12
+ },
13
+ "devDependencies": {
14
+ "@arethetypeswrong/core": "0.18.5",
15
+ "@little-nebulae/error": "1.1.0",
16
+ "@little-nebulae/result": "1.0.0",
17
+ "@little-nebulae/system-error": "2.0.0",
18
+ "@little-nebulae/type-utils": "1.1.0",
19
+ "@types/node": "26.6.2",
20
+ "oxfmt": "0.68.0",
21
+ "oxlint": "1.83.0",
22
+ "oxlint-tsgolint": "7.0.2002",
23
+ "publint": "0.3.24",
24
+ "tsdown": "0.23.0",
25
+ "tsx": "4.23.13",
26
+ "typescript": "7.0.2",
27
+ "vitest": "5.0.1"
28
+ },
29
+ "peerDependencies": {
30
+ "@little-nebulae/error": "^1.0.0",
31
+ "@little-nebulae/result": "^1.0.0",
32
+ "@little-nebulae/system-error": "^1.0.0",
33
+ "@little-nebulae/type-utils": "^1.0.0"
34
+ },
35
+ "devEngines": {
36
+ "packageManager": {
37
+ "name": "pnpm",
38
+ "version": "12.4.2",
39
+ "onFail": "warn"
40
+ }
41
+ },
42
+ "scripts": {
43
+ "build": "tsdown",
44
+ "try": "tsx ./src/try.ts",
45
+ "fmt": "oxfmt --disable-nested-config",
46
+ "fmt:check": "oxfmt --check --disable-nested-config",
47
+ "lint": "oxlint --disable-nested-config",
48
+ "lint:fix": "oxlint --fix --disable-nested-config",
49
+ "check": "pnpm run lint && pnpm run fmt:check",
50
+ "fix": "pnpm run lint:fix && pnpm run fmt",
51
+ "test": "vitest run"
52
+ }
53
+ }