@better-fs-tools/node 0.0.0-tegami-trusted-publish-setup → 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 +21 -0
- package/README.md +149 -6
- package/dist/command-runner.d.ts +16 -0
- package/dist/command-runner.js +159 -0
- package/dist/create-node-bash-tool.d.ts +10 -0
- package/dist/create-node-bash-tool.js +31 -0
- package/dist/create-node-fs-tools.d.ts +75 -0
- package/dist/create-node-fs-tools.js +112 -0
- package/dist/create-node-read-tool.d.ts +9 -0
- package/dist/create-node-read-tool.js +22 -0
- package/dist/create-node-write-tools.d.ts +19 -0
- package/dist/create-node-write-tools.js +42 -0
- package/dist/digest.d.ts +3 -0
- package/dist/digest.js +21 -0
- package/dist/filesystem.d.ts +15 -0
- package/dist/filesystem.js +226 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +7 -0
- package/dist/policy.d.ts +73 -0
- package/dist/policy.js +219 -0
- package/dist/stat.d.ts +3 -0
- package/dist/stat.js +121 -0
- package/dist/write.d.ts +20 -0
- package/dist/write.js +341 -0
- package/package.json +37 -2
package/dist/stat.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { lstat, realpath, stat as statPath } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { checkRequest, checkResolved, errorCode, fail, hasSymlinkComponent, mapError, nodeIdentity, notAFile, SYMLINK_REJECTED, targetPaths, } from "./policy.js";
|
|
4
|
+
const DANGLING = "the path is a symbolic link whose target does not exist";
|
|
5
|
+
export async function nodeStat(context, requested, options = {}) {
|
|
6
|
+
const { config } = context;
|
|
7
|
+
if (options.signal?.aborted)
|
|
8
|
+
return fail({ reason: "aborted" });
|
|
9
|
+
const checked = checkRequest(config, requested);
|
|
10
|
+
if ("error" in checked)
|
|
11
|
+
return fail(checked.error);
|
|
12
|
+
const { lexical } = checked;
|
|
13
|
+
let roots;
|
|
14
|
+
try {
|
|
15
|
+
roots = await context.roots();
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
return fail(mapError(error, "root-resolution"));
|
|
19
|
+
}
|
|
20
|
+
let target;
|
|
21
|
+
try {
|
|
22
|
+
if (config.symlinks === "reject" && (await hasSymlinkComponent(lexical))) {
|
|
23
|
+
return fail({ reason: "denied", detail: SYMLINK_REJECTED });
|
|
24
|
+
}
|
|
25
|
+
target = await realpath(lexical);
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
if (errorCode(error) !== "ENOENT")
|
|
29
|
+
return fail(mapError(error, "resolve"));
|
|
30
|
+
return statMissingPath(context, roots, lexical);
|
|
31
|
+
}
|
|
32
|
+
const refused = checkResolved(roots, target);
|
|
33
|
+
if (refused !== null)
|
|
34
|
+
return fail(refused);
|
|
35
|
+
const paths = targetPaths(config.cwd, target);
|
|
36
|
+
try {
|
|
37
|
+
const stats = await lstat(target, { bigint: true });
|
|
38
|
+
if (!stats.isFile())
|
|
39
|
+
return fail(notAFile(stats, paths));
|
|
40
|
+
if (stats.size > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
41
|
+
return fail({
|
|
42
|
+
reason: "denied",
|
|
43
|
+
detail: "the reported file size is outside the safe numeric range",
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
const version = nodeIdentity(stats);
|
|
47
|
+
return {
|
|
48
|
+
ok: true,
|
|
49
|
+
stat: {
|
|
50
|
+
exists: true,
|
|
51
|
+
...paths,
|
|
52
|
+
size: Number(stats.size),
|
|
53
|
+
mtimeMs: Number(stats.mtimeNs) / 1_000_000,
|
|
54
|
+
identity: version,
|
|
55
|
+
version,
|
|
56
|
+
mode: Number(stats.mode & 4095n),
|
|
57
|
+
hardLinks: Number(stats.nlink),
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
return fail(mapError(error, "stat", paths));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
async function statMissingPath(context, roots, lexical) {
|
|
66
|
+
const segments = [];
|
|
67
|
+
let current = lexical;
|
|
68
|
+
for (;;) {
|
|
69
|
+
const unresolvableEntry = await lstat(current).catch(() => null);
|
|
70
|
+
if (unresolvableEntry?.isSymbolicLink())
|
|
71
|
+
return fail({ reason: "denied", detail: DANGLING });
|
|
72
|
+
if (unresolvableEntry !== null) {
|
|
73
|
+
return fail({ reason: "io", detail: "the path changed while it was resolved" });
|
|
74
|
+
}
|
|
75
|
+
segments.unshift(path.basename(current));
|
|
76
|
+
const parent = path.dirname(current);
|
|
77
|
+
if (parent === current)
|
|
78
|
+
return fail({ reason: "not-found" });
|
|
79
|
+
current = parent;
|
|
80
|
+
let ancestor;
|
|
81
|
+
try {
|
|
82
|
+
ancestor = await realpath(current);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
if (errorCode(error) === "ENOENT")
|
|
86
|
+
continue;
|
|
87
|
+
return fail(mapError(error, "resolve"));
|
|
88
|
+
}
|
|
89
|
+
return statMissingUnderAncestor(context, roots, ancestor, segments);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async function statMissingUnderAncestor(context, roots, ancestor, segments) {
|
|
93
|
+
const refused = checkResolved(roots, ancestor);
|
|
94
|
+
if (refused !== null)
|
|
95
|
+
return fail(refused);
|
|
96
|
+
try {
|
|
97
|
+
const stats = await statPath(ancestor);
|
|
98
|
+
if (!stats.isDirectory()) {
|
|
99
|
+
return fail({ reason: "not-found", detail: "a parent is not a directory" });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
return fail(mapError(error, "stat-parent"));
|
|
104
|
+
}
|
|
105
|
+
const resolvedPath = path.join(ancestor, ...segments);
|
|
106
|
+
const deny = checkResolved(roots, resolvedPath);
|
|
107
|
+
if (deny !== null)
|
|
108
|
+
return fail(deny);
|
|
109
|
+
const missingDirectories = [];
|
|
110
|
+
for (let index = 1; index < segments.length; index += 1) {
|
|
111
|
+
missingDirectories.push(path.join(ancestor, ...segments.slice(0, index)));
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
ok: true,
|
|
115
|
+
stat: {
|
|
116
|
+
exists: false,
|
|
117
|
+
...targetPaths(context.config.cwd, resolvedPath),
|
|
118
|
+
missingDirectories,
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
package/dist/write.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { chmod, link, mkdir, open, rename, rmdir, unlink } from "node:fs/promises";
|
|
2
|
+
import type { MutateOptions, MutationError, MutationOutcome, StageOutcome, WriteOptions } from "@better-fs-tools/fs";
|
|
3
|
+
import type { NodeContext, TargetPaths } from "./policy.ts";
|
|
4
|
+
export interface NodeWriteIo {
|
|
5
|
+
readonly open: typeof open;
|
|
6
|
+
readonly link: typeof link;
|
|
7
|
+
readonly rename: typeof rename;
|
|
8
|
+
readonly unlink: typeof unlink;
|
|
9
|
+
readonly mkdir: typeof mkdir;
|
|
10
|
+
readonly rmdir: typeof rmdir;
|
|
11
|
+
readonly chmod: typeof chmod;
|
|
12
|
+
}
|
|
13
|
+
export declare const NODE_WRITE_IO: NodeWriteIo;
|
|
14
|
+
export interface NodeWrites {
|
|
15
|
+
write(path: string, bytes: Uint8Array, options: WriteOptions): Promise<MutationOutcome>;
|
|
16
|
+
stage(path: string, bytes: Uint8Array, options: WriteOptions): Promise<StageOutcome>;
|
|
17
|
+
remove(path: string, options: MutateOptions): Promise<MutationOutcome>;
|
|
18
|
+
}
|
|
19
|
+
export declare function nodeWrites(context: NodeContext, io?: NodeWriteIo): NodeWrites;
|
|
20
|
+
export declare function mapMutationError(error: unknown, phase: string, target?: TargetPaths): MutationError;
|
package/dist/write.js
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { constants } from "node:fs";
|
|
3
|
+
import { chmod, link, lstat, mkdir, open, rename, rmdir, unlink } from "node:fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { createModes, errorCode, fail, isMode, mapError, nodeIdentity, notAFile, } from "./policy.js";
|
|
6
|
+
import { nodeStat } from "./stat.js";
|
|
7
|
+
export const NODE_WRITE_IO = Object.freeze({
|
|
8
|
+
open,
|
|
9
|
+
link,
|
|
10
|
+
rename,
|
|
11
|
+
unlink,
|
|
12
|
+
mkdir,
|
|
13
|
+
rmdir,
|
|
14
|
+
chmod,
|
|
15
|
+
});
|
|
16
|
+
const NO_FOLLOW = typeof constants.O_NOFOLLOW === "number" ? constants.O_NOFOLLOW : 0;
|
|
17
|
+
const NON_BLOCK = typeof constants.O_NONBLOCK === "number" ? constants.O_NONBLOCK : 0;
|
|
18
|
+
const HARD_LINKED = "the file has more than one hard link";
|
|
19
|
+
export function nodeWrites(context, io = NODE_WRITE_IO) {
|
|
20
|
+
const { config } = context;
|
|
21
|
+
const stage = async (requested, bytes, options) => {
|
|
22
|
+
if (options.mode !== undefined && !isMode(options.mode)) {
|
|
23
|
+
return fail({ reason: "denied", detail: "mode must be an integer from 0 to 0o7777" });
|
|
24
|
+
}
|
|
25
|
+
const outcome = await nodeStat(context, requested, { signal: options.signal });
|
|
26
|
+
if (!outcome.ok)
|
|
27
|
+
return outcome;
|
|
28
|
+
const current = outcome.stat;
|
|
29
|
+
const conflict = preconditionConflict(current, options);
|
|
30
|
+
if (conflict !== null)
|
|
31
|
+
return fail(conflict);
|
|
32
|
+
const target = { resolvedPath: current.resolvedPath, displayPath: current.displayPath };
|
|
33
|
+
if (current.exists && (current.hardLinks ?? 1) > 1) {
|
|
34
|
+
if (config.hardLinks === "refuse")
|
|
35
|
+
return fail({ reason: "denied", detail: HARD_LINKED });
|
|
36
|
+
return { ok: true, staged: inPlaceStage(target, Uint8Array.from(bytes), options) };
|
|
37
|
+
}
|
|
38
|
+
const created = [];
|
|
39
|
+
let temp = null;
|
|
40
|
+
const cleanup = async () => {
|
|
41
|
+
if (temp !== null) {
|
|
42
|
+
await temp.handle.close().catch(() => { });
|
|
43
|
+
await io.unlink(temp.path).catch(() => { });
|
|
44
|
+
temp = null;
|
|
45
|
+
}
|
|
46
|
+
for (const directory of [...created].reverse()) {
|
|
47
|
+
// rmdir refuses a directory that is not empty, so only empty ones are removed.
|
|
48
|
+
await io.rmdir(directory).catch(() => { });
|
|
49
|
+
}
|
|
50
|
+
created.length = 0;
|
|
51
|
+
};
|
|
52
|
+
try {
|
|
53
|
+
if (!current.exists && current.missingDirectories.length > 0) {
|
|
54
|
+
if (!options.createParents) {
|
|
55
|
+
return fail({ reason: "not-found", detail: "the parent directory does not exist" });
|
|
56
|
+
}
|
|
57
|
+
const refused = await createParents(current.missingDirectories, created);
|
|
58
|
+
if (refused !== null) {
|
|
59
|
+
await cleanup();
|
|
60
|
+
return fail(refused);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const tempPath = tempPathFor(target.resolvedPath);
|
|
64
|
+
const handle = await io.open(tempPath, constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY | NO_FOLLOW, 0o600);
|
|
65
|
+
temp = { path: tempPath, handle };
|
|
66
|
+
await handle.writeFile(bytes);
|
|
67
|
+
await handle.chmod(current.exists && current.mode !== null
|
|
68
|
+
? current.mode
|
|
69
|
+
: (options.mode ?? createModes(config).file));
|
|
70
|
+
await handle.sync();
|
|
71
|
+
if (options.signal?.aborted) {
|
|
72
|
+
await cleanup();
|
|
73
|
+
return fail({ reason: "aborted" });
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
await cleanup();
|
|
78
|
+
return fail(mapMutationError(error, "stage", target));
|
|
79
|
+
}
|
|
80
|
+
const staged = temp;
|
|
81
|
+
return {
|
|
82
|
+
ok: true,
|
|
83
|
+
staged: renameStage(target, staged, [...created], options, cleanup),
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
const createParents = async (directories, created) => {
|
|
87
|
+
const mode = createModes(config).directory;
|
|
88
|
+
for (const directory of directories) {
|
|
89
|
+
try {
|
|
90
|
+
await io.mkdir(directory, mode);
|
|
91
|
+
created.push(directory);
|
|
92
|
+
// mkdir applies the umask; chmod makes the mode exact.
|
|
93
|
+
await io.chmod(directory, mode);
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
if (errorCode(error) !== "EEXIST")
|
|
97
|
+
return mapMutationError(error, "create-parent");
|
|
98
|
+
}
|
|
99
|
+
const stats = await lstat(directory).catch(() => null);
|
|
100
|
+
if (stats === null || !stats.isDirectory()) {
|
|
101
|
+
return { reason: "denied", detail: "a parent directory changed while it was created" };
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
};
|
|
106
|
+
const renameStage = (target, temp, createdDirectories, options, cleanup) => {
|
|
107
|
+
let used = false;
|
|
108
|
+
let settled = false;
|
|
109
|
+
return Object.freeze({
|
|
110
|
+
resolvedPath: target.resolvedPath,
|
|
111
|
+
async publish() {
|
|
112
|
+
if (used || settled)
|
|
113
|
+
throw new TypeError("node staged write: publish() is single-use");
|
|
114
|
+
used = true;
|
|
115
|
+
const outcome = await withInProcessPathLock(target.resolvedPath, async () => {
|
|
116
|
+
try {
|
|
117
|
+
const refused = await publishConflict(target, options);
|
|
118
|
+
if (refused !== null)
|
|
119
|
+
return fail(refused);
|
|
120
|
+
if (options.precondition.kind === "absent") {
|
|
121
|
+
// link() fails with EEXIST when another creator won the race.
|
|
122
|
+
await io.link(temp.path, target.resolvedPath);
|
|
123
|
+
await io.unlink(temp.path).catch(() => { });
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
await io.rename(temp.path, target.resolvedPath);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
return fail(mapMutationError(error, "publish", target));
|
|
131
|
+
}
|
|
132
|
+
await syncDirectory(path.dirname(target.resolvedPath));
|
|
133
|
+
const stats = await temp.handle.stat({ bigint: true }).catch(() => null);
|
|
134
|
+
await temp.handle.close().catch(() => { });
|
|
135
|
+
return { ok: true, file: mutated(target, stats, createdDirectories, true) };
|
|
136
|
+
});
|
|
137
|
+
settled = true;
|
|
138
|
+
if (!outcome.ok)
|
|
139
|
+
await cleanup();
|
|
140
|
+
return outcome;
|
|
141
|
+
},
|
|
142
|
+
async discard() {
|
|
143
|
+
if (settled)
|
|
144
|
+
return;
|
|
145
|
+
settled = true;
|
|
146
|
+
await cleanup();
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
const inPlaceStage = (target, bytes, options) => {
|
|
151
|
+
let used = false;
|
|
152
|
+
let settled = false;
|
|
153
|
+
return Object.freeze({
|
|
154
|
+
resolvedPath: target.resolvedPath,
|
|
155
|
+
async publish() {
|
|
156
|
+
if (used || settled)
|
|
157
|
+
throw new TypeError("node staged write: publish() is single-use");
|
|
158
|
+
used = true;
|
|
159
|
+
settled = true;
|
|
160
|
+
return withInProcessPathLock(target.resolvedPath, async () => {
|
|
161
|
+
let handle;
|
|
162
|
+
try {
|
|
163
|
+
handle = await io.open(target.resolvedPath, constants.O_WRONLY | NO_FOLLOW | NON_BLOCK);
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
if (errorCode(error) === "ENOENT" && options.precondition.kind === "version") {
|
|
167
|
+
return fail({ reason: "changed" });
|
|
168
|
+
}
|
|
169
|
+
return fail(mapMutationError(error, "publish", target));
|
|
170
|
+
}
|
|
171
|
+
try {
|
|
172
|
+
const before = await handle.stat({ bigint: true });
|
|
173
|
+
if (!before.isFile())
|
|
174
|
+
return fail(notAFile(before, target));
|
|
175
|
+
const { precondition } = options;
|
|
176
|
+
if (precondition.kind === "version" && nodeIdentity(before) !== precondition.version) {
|
|
177
|
+
return fail({ reason: "changed" });
|
|
178
|
+
}
|
|
179
|
+
if (precondition.kind === "absent")
|
|
180
|
+
return fail({ reason: "exists" });
|
|
181
|
+
await handle.truncate(0);
|
|
182
|
+
await handle.writeFile(bytes);
|
|
183
|
+
await handle.sync();
|
|
184
|
+
const after = await handle.stat({ bigint: true });
|
|
185
|
+
return { ok: true, file: mutated(target, after, [], false) };
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
return fail(mapMutationError(error, "publish", target));
|
|
189
|
+
}
|
|
190
|
+
finally {
|
|
191
|
+
await handle.close().catch(() => { });
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
},
|
|
195
|
+
async discard() {
|
|
196
|
+
settled = true;
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
};
|
|
200
|
+
const publishConflict = async (target, options) => {
|
|
201
|
+
const current = await lstat(target.resolvedPath, { bigint: true }).catch((error) => {
|
|
202
|
+
if (errorCode(error) === "ENOENT")
|
|
203
|
+
return null;
|
|
204
|
+
throw error;
|
|
205
|
+
});
|
|
206
|
+
const { precondition } = options;
|
|
207
|
+
if (precondition.kind === "absent")
|
|
208
|
+
return current === null ? null : { reason: "exists" };
|
|
209
|
+
if (precondition.kind === "version") {
|
|
210
|
+
return current !== null && current.isFile() && nodeIdentity(current) === precondition.version
|
|
211
|
+
? null
|
|
212
|
+
: { reason: "changed" };
|
|
213
|
+
}
|
|
214
|
+
if (current === null)
|
|
215
|
+
return null;
|
|
216
|
+
if (current.isSymbolicLink()) {
|
|
217
|
+
return { reason: "denied", detail: "the target became a symbolic link" };
|
|
218
|
+
}
|
|
219
|
+
if (!current.isFile())
|
|
220
|
+
return notAFile(current, target);
|
|
221
|
+
if (current.nlink > 1n && config.hardLinks === "refuse") {
|
|
222
|
+
return { reason: "denied", detail: HARD_LINKED };
|
|
223
|
+
}
|
|
224
|
+
return null;
|
|
225
|
+
};
|
|
226
|
+
const write = async (requested, bytes, options) => {
|
|
227
|
+
const staged = await stage(requested, bytes, options);
|
|
228
|
+
if (!staged.ok)
|
|
229
|
+
return staged;
|
|
230
|
+
try {
|
|
231
|
+
return await staged.staged.publish();
|
|
232
|
+
}
|
|
233
|
+
finally {
|
|
234
|
+
await staged.staged.discard();
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
const remove = async (requested, options) => {
|
|
238
|
+
const outcome = await nodeStat(context, requested, { signal: options.signal });
|
|
239
|
+
if (!outcome.ok)
|
|
240
|
+
return outcome;
|
|
241
|
+
const current = outcome.stat;
|
|
242
|
+
const { precondition } = options;
|
|
243
|
+
if (!current.exists) {
|
|
244
|
+
return fail({ reason: precondition.kind === "version" ? "changed" : "not-found" });
|
|
245
|
+
}
|
|
246
|
+
const conflict = preconditionConflict(current, { precondition });
|
|
247
|
+
if (conflict !== null)
|
|
248
|
+
return fail(conflict);
|
|
249
|
+
const target = { resolvedPath: current.resolvedPath, displayPath: current.displayPath };
|
|
250
|
+
return withInProcessPathLock(target.resolvedPath, async () => {
|
|
251
|
+
try {
|
|
252
|
+
const now = await lstat(target.resolvedPath, { bigint: true });
|
|
253
|
+
if (!now.isFile()) {
|
|
254
|
+
return fail(precondition.kind === "version" ? { reason: "changed" } : notAFile(now, target));
|
|
255
|
+
}
|
|
256
|
+
if (precondition.kind === "version" && nodeIdentity(now) !== precondition.version) {
|
|
257
|
+
return fail({ reason: "changed" });
|
|
258
|
+
}
|
|
259
|
+
await io.unlink(target.resolvedPath);
|
|
260
|
+
}
|
|
261
|
+
catch (error) {
|
|
262
|
+
if (errorCode(error) === "ENOENT" && precondition.kind === "version") {
|
|
263
|
+
return fail({ reason: "changed" });
|
|
264
|
+
}
|
|
265
|
+
return fail(mapMutationError(error, "remove", target));
|
|
266
|
+
}
|
|
267
|
+
await syncDirectory(path.dirname(target.resolvedPath));
|
|
268
|
+
return { ok: true, file: mutated(target, null, [], true) };
|
|
269
|
+
});
|
|
270
|
+
};
|
|
271
|
+
return { write, stage, remove };
|
|
272
|
+
}
|
|
273
|
+
function preconditionConflict(current, options) {
|
|
274
|
+
const { precondition } = options;
|
|
275
|
+
if (precondition.kind === "absent")
|
|
276
|
+
return current.exists ? { reason: "exists" } : null;
|
|
277
|
+
if (precondition.kind === "version") {
|
|
278
|
+
return current.exists && current.version === precondition.version
|
|
279
|
+
? null
|
|
280
|
+
: { reason: "changed" };
|
|
281
|
+
}
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
function mutated(target, stats, createdDirectories, atomic) {
|
|
285
|
+
const version = stats === null ? null : nodeIdentity(stats);
|
|
286
|
+
return {
|
|
287
|
+
...target,
|
|
288
|
+
version,
|
|
289
|
+
identity: version,
|
|
290
|
+
size: stats === null ? null : Number(stats.size),
|
|
291
|
+
createdDirectories,
|
|
292
|
+
atomic,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
const TEMP_NAME_MAX_CHARS = 48;
|
|
296
|
+
function tempPathFor(resolvedPath) {
|
|
297
|
+
const name = path.basename(resolvedPath).slice(0, TEMP_NAME_MAX_CHARS);
|
|
298
|
+
return path.join(path.dirname(resolvedPath), `.${name}.${randomBytes(6).toString("hex")}.tmp`);
|
|
299
|
+
}
|
|
300
|
+
async function syncDirectory(directory) {
|
|
301
|
+
// Best effort: some filesystems refuse fsync on a directory.
|
|
302
|
+
const handle = await open(directory, constants.O_RDONLY | constants.O_DIRECTORY).catch(() => null);
|
|
303
|
+
if (handle === null)
|
|
304
|
+
return;
|
|
305
|
+
await handle.sync().catch(() => { });
|
|
306
|
+
await handle.close().catch(() => { });
|
|
307
|
+
}
|
|
308
|
+
const inProcessPathLocks = new Map();
|
|
309
|
+
async function withInProcessPathLock(key, run) {
|
|
310
|
+
const previous = inProcessPathLocks.get(key) ?? Promise.resolve();
|
|
311
|
+
let release = () => { };
|
|
312
|
+
const held = new Promise((resolve) => {
|
|
313
|
+
release = resolve;
|
|
314
|
+
});
|
|
315
|
+
const tail = previous.then(() => held);
|
|
316
|
+
inProcessPathLocks.set(key, tail);
|
|
317
|
+
await previous;
|
|
318
|
+
try {
|
|
319
|
+
return await run();
|
|
320
|
+
}
|
|
321
|
+
finally {
|
|
322
|
+
release();
|
|
323
|
+
if (inProcessPathLocks.get(key) === tail)
|
|
324
|
+
inProcessPathLocks.delete(key);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
export function mapMutationError(error, phase, target) {
|
|
328
|
+
const code = errorCode(error);
|
|
329
|
+
const cause = { code: code ?? "UNKNOWN", phase };
|
|
330
|
+
switch (code) {
|
|
331
|
+
case "EROFS":
|
|
332
|
+
return { reason: "read-only", cause };
|
|
333
|
+
case "ENOSPC":
|
|
334
|
+
case "EDQUOT":
|
|
335
|
+
return { reason: "no-space", cause };
|
|
336
|
+
case "EEXIST":
|
|
337
|
+
return { reason: "exists", cause };
|
|
338
|
+
default:
|
|
339
|
+
return mapError(error, phase, target);
|
|
340
|
+
}
|
|
341
|
+
}
|
package/package.json
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-fs-tools/node",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The Node filesystem, a SHA-256 digest, a bash runner, and the zero-config read, edit, write, apply_patch, and bash tools for Node and Bun.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/gingerhendrix/better-fs-tools.git",
|
|
9
|
+
"directory": "packages/node"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"import": "./dist/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@better-fs-tools/fs": "^0.1.0",
|
|
33
|
+
"@better-fs-tools/read": "^0.1.0",
|
|
34
|
+
"@better-fs-tools/shell": "^0.1.0",
|
|
35
|
+
"@better-fs-tools/write": "^0.1.0"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=24"
|
|
39
|
+
}
|
|
5
40
|
}
|