@sublang/spex 6.0.0 → 7.0.0-beta.2
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/README.md +419 -12
- package/dist/cli.js +12 -1
- package/dist/commands/add.d.ts +1 -1
- package/dist/commands/add.js +2 -2
- package/dist/commands/environment.d.ts +3 -3
- package/dist/commands/environment.js +5 -7
- package/dist/commands/export.js +2 -2
- package/dist/commands/home.d.ts +2 -2
- package/dist/commands/home.js +3 -3
- package/dist/commands/list.js +1 -1
- package/dist/commands/login.js +12 -7
- package/dist/commands/logout.js +2 -3
- package/dist/commands/publish.js +1 -1
- package/dist/commands/remove.js +1 -1
- package/dist/commands/run.d.ts +1 -1
- package/dist/commands/run.js +16 -0
- package/dist/commands/settle.d.ts +1 -1
- package/dist/commands/settle.js +1 -1
- package/dist/guards/approval.d.ts +56 -0
- package/dist/guards/approval.js +158 -0
- package/dist/guards/checks.d.ts +207 -0
- package/dist/guards/checks.js +681 -0
- package/dist/guards/commands.d.ts +15 -0
- package/dist/guards/commands.js +313 -0
- package/dist/guards/copy.d.ts +67 -0
- package/dist/guards/copy.js +306 -0
- package/dist/guards/declaration.d.ts +45 -0
- package/dist/guards/declaration.js +224 -0
- package/dist/guards/files.d.ts +137 -0
- package/dist/guards/files.js +544 -0
- package/dist/guards/harness.d.ts +58 -0
- package/dist/guards/harness.js +420 -0
- package/dist/guards/items.d.ts +61 -0
- package/dist/guards/items.js +161 -0
- package/dist/guards/patch.d.ts +55 -0
- package/dist/guards/patch.js +258 -0
- package/dist/guards/processes.d.ts +49 -0
- package/dist/guards/processes.js +233 -0
- package/dist/guards/proofs.d.ts +88 -0
- package/dist/guards/proofs.js +503 -0
- package/dist/guards/records.d.ts +176 -0
- package/dist/guards/records.js +513 -0
- package/dist/guards/report.d.ts +26 -0
- package/dist/guards/report.js +492 -0
- package/dist/guards/runner.d.ts +42 -0
- package/dist/guards/runner.js +167 -0
- package/dist/guards/tla.d.ts +51 -0
- package/dist/guards/tla.js +352 -0
- package/dist/guards/tlapm.d.ts +81 -0
- package/dist/guards/tlapm.js +172 -0
- package/dist/guards/tlc.d.ts +191 -0
- package/dist/guards/tlc.js +457 -0
- package/dist/guards/translation.d.ts +66 -0
- package/dist/guards/translation.js +202 -0
- package/dist/guards/types.d.ts +185 -0
- package/dist/guards/types.js +437 -0
- package/dist/guards/verify.d.ts +290 -0
- package/dist/guards/verify.js +1191 -0
- package/dist/guards/xml.d.ts +18 -0
- package/dist/guards/xml.js +105 -0
- package/dist/lib/environment/archive.d.ts +2 -2
- package/dist/lib/environment/archive.js +1 -1
- package/dist/lib/environment/builtins.d.ts +10 -12
- package/dist/lib/environment/builtins.js +17 -19
- package/dist/lib/environment/exports.d.ts +14 -13
- package/dist/lib/environment/exports.js +31 -16
- package/dist/lib/environment/format.d.ts +2 -2
- package/dist/lib/environment/format.js +3 -3
- package/dist/lib/environment/git-source.js +6 -3
- package/dist/lib/environment/install.d.ts +14 -16
- package/dist/lib/environment/install.js +66 -61
- package/dist/lib/environment/lock.d.ts +4 -3
- package/dist/lib/environment/lock.js +6 -5
- package/dist/lib/environment/marker.js +1 -2
- package/dist/lib/environment/own-paths.d.ts +7 -10
- package/dist/lib/environment/own-paths.js +10 -13
- package/dist/lib/environment/placed-record.d.ts +3 -2
- package/dist/lib/environment/placed-record.js +5 -4
- package/dist/lib/environment/registry.d.ts +1 -1
- package/dist/lib/environment/registry.js +1 -1
- package/dist/lib/environment/requests.d.ts +2 -2
- package/dist/lib/environment/requests.js +2 -2
- package/dist/lib/environment/resolve.d.ts +4 -4
- package/dist/lib/environment/resolve.js +3 -4
- package/dist/lib/environment/store.d.ts +34 -8
- package/dist/lib/environment/store.js +138 -43
- package/dist/lib/files.d.ts +72 -12
- package/dist/lib/files.js +200 -65
- package/dist/lib/git-host.d.ts +52 -258
- package/dist/lib/git-host.js +52 -265
- package/dist/lib/home-file.d.ts +13 -10
- package/dist/lib/home-file.js +28 -18
- package/dist/lib/instance.d.ts +1 -1
- package/dist/lib/instance.js +1 -1
- package/dist/lint.d.ts +22 -0
- package/dist/lint.js +36 -0
- package/dist/testing/standin-registry.js +1 -1
- package/package.json +7 -3
- package/scaffold/.file-history.json +14 -7
- package/scaffold/i18n/zh/source-pins.json +41 -0
- package/scaffold/i18n/zh/specs/decisions/000-spec-structure-format.md +0 -1
- package/scaffold/i18n/zh/specs/intents/000-spdx-headers.md +0 -1
- package/scaffold/i18n/zh/specs/map.md +0 -1
- package/scaffold/i18n/zh/specs/meta.md +18 -35
- package/scaffold/i18n/zh/specs/packages/git.md +0 -1
- package/scaffold/i18n/zh/specs/packages/licensing.md +0 -1
- package/scaffold/specs/meta.md +18 -3
- package/scripts/test-exit-guard.mjs +28 -0
package/dist/lib/files.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
// SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
|
|
3
|
-
// The primitives every Spex-owned file shares
|
|
3
|
+
// The primitives every Spex-owned file shares: closed
|
|
4
4
|
// encodings checked field by field, atomic same-directory replacement —
|
|
5
5
|
// plain, or under the version the writer read (DR-111) — the one
|
|
6
6
|
// storage fault a reader reports, and which of a clone's paths may leave
|
|
7
7
|
// the device. Their words are the host's (words.ts).
|
|
8
8
|
import { createHash, randomUUID } from "node:crypto";
|
|
9
|
-
import { closeSync, fchmodSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { closeSync, fchmodSync, fsyncSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, renameSync, rmdirSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
|
|
10
10
|
import { readOwnerAndDacl } from "@openclaw/fs-safe/permissions";
|
|
11
11
|
import { createDirectorySync, createFileSync, assertNoSymlinkParentsSync } from "@openclaw/fs-safe/advanced";
|
|
12
|
-
import { dirname, isAbsolute, parse, posix, resolve, win32 } from "node:path";
|
|
12
|
+
import { dirname, isAbsolute, join, parse, posix, resolve, win32 } from "node:path";
|
|
13
13
|
import { words } from "./words.js";
|
|
14
14
|
/** A canonical lowercase UUID. */
|
|
15
15
|
export const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
16
|
-
/** A list a language punctuates its own way
|
|
16
|
+
/** A list a language punctuates its own way. */
|
|
17
17
|
export const listed = (names) => names.join(words().listSeparator());
|
|
18
18
|
export class StorageFormatError extends Error {
|
|
19
19
|
file;
|
|
@@ -25,25 +25,52 @@ export class StorageFormatError extends Error {
|
|
|
25
25
|
this.name = "StorageFormatError";
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
|
-
|
|
29
|
-
|
|
28
|
+
// Windows access rights that let a principal change an entry, by their
|
|
29
|
+
// Win32 names. File and directory rights share these bits: adding a file
|
|
30
|
+
// or subdirectory is writing or appending data.
|
|
31
|
+
const FILE_WRITE_DATA = 0x0000_0002;
|
|
32
|
+
const FILE_APPEND_DATA = 0x0000_0004;
|
|
33
|
+
const FILE_WRITE_EA = 0x0000_0010;
|
|
34
|
+
const FILE_DELETE_CHILD = 0x0000_0040;
|
|
35
|
+
const FILE_WRITE_ATTRIBUTES = 0x0000_0100;
|
|
36
|
+
const DELETE = 0x0001_0000;
|
|
37
|
+
const WRITE_DAC = 0x0004_0000;
|
|
38
|
+
const WRITE_OWNER = 0x0008_0000;
|
|
39
|
+
const MAXIMUM_ALLOWED = 0x0200_0000;
|
|
40
|
+
const GENERIC_ALL = 0x1000_0000;
|
|
41
|
+
const GENERIC_WRITE = 0x4000_0000;
|
|
42
|
+
/** Rights a grant may not give another principal over a private entry
|
|
43
|
+
* (storage-57). Read, execute and synchronize grants hold none of them. */
|
|
44
|
+
const CHANGE_RIGHTS = FILE_WRITE_DATA | FILE_APPEND_DATA | FILE_WRITE_EA | FILE_DELETE_CHILD |
|
|
45
|
+
FILE_WRITE_ATTRIBUTES | DELETE | WRITE_DAC | WRITE_OWNER | MAXIMUM_ALLOWED | GENERIC_ALL | GENERIC_WRITE;
|
|
46
|
+
/** Spex's private-storage check (storage-57) over facts inspected by
|
|
47
|
+
* fs-safe, applied only where an entry's integrity is relied on — the
|
|
48
|
+
* root lease, a clone's lease before a move, and upload staging. POSIX admits the current user's entry with no group or
|
|
49
|
+
* other bit. Windows admits an entry the current user, LocalSystem or
|
|
50
|
+
* built-in Administrators own, as root is on POSIX, whose descriptor
|
|
51
|
+
* grants no other principal a right to change it; a read grant another
|
|
52
|
+
* tool added, such as the Codex sandbox's, is admitted (DR-223). */
|
|
30
53
|
export function hasPrivatePermissions(file, stat) {
|
|
31
54
|
if (process.platform !== "win32")
|
|
32
55
|
return (stat.mode & 0o077) === 0 &&
|
|
33
56
|
(typeof process.getuid !== "function" || stat.uid === process.getuid());
|
|
34
57
|
const facts = readOwnerAndDacl(file);
|
|
35
|
-
if (facts.status !== "supported" || !facts.complete || !facts.
|
|
58
|
+
if (facts.status !== "supported" || !facts.complete || !facts.daclPresent || facts.unsupportedAceTypes.length)
|
|
36
59
|
return false;
|
|
37
60
|
const trusted = new Set([facts.currentUserSid, "s-1-5-18", "s-1-5-32-544"]);
|
|
38
|
-
return trusted.has(facts.ownerSid) && facts.aces.every(ace => ace.aceType === "deny" || ace.mask === 0 || trusted.has(ace.sid));
|
|
61
|
+
return trusted.has(facts.ownerSid) && facts.aces.every(ace => ace.aceType === "deny" || (ace.mask & CHANGE_RIGHTS) === 0 || trusted.has(ace.sid));
|
|
39
62
|
}
|
|
40
|
-
/** Create a Windows state root privately from its first appearance
|
|
41
|
-
*
|
|
63
|
+
/** Create a Windows state root privately from its first appearance; an
|
|
64
|
+
* existing root is admitted whatever its permissions, as on POSIX
|
|
65
|
+
* (storage-57, DR-223). */
|
|
42
66
|
export async function prepareWindowsStorageDirectory(directory) {
|
|
43
67
|
if (process.platform === "win32")
|
|
44
68
|
makePrivateDirectory(directory);
|
|
45
69
|
}
|
|
46
|
-
/**
|
|
70
|
+
/** Make a folder owner-only where none stands — on Windows through
|
|
71
|
+
* fs-safe's protected creator, each missing parent the same way — and
|
|
72
|
+
* admit one that stands whatever its permissions, refusing only a link or
|
|
73
|
+
* a non-directory (storage-57). */
|
|
47
74
|
export function makePrivateDirectory(directory) {
|
|
48
75
|
if (process.platform !== "win32") {
|
|
49
76
|
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
@@ -64,17 +91,8 @@ export function makePrivateDirectory(directory) {
|
|
|
64
91
|
}
|
|
65
92
|
}
|
|
66
93
|
const current = lstatSync(directory);
|
|
67
|
-
if (current.isSymbolicLink() || !current.isDirectory()
|
|
68
|
-
throw new StorageFormatError(directory, "
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
function assertPrivateWindowsEntry(file) {
|
|
72
|
-
if (process.platform !== "win32")
|
|
73
|
-
return;
|
|
74
|
-
const info = lstatSync(file);
|
|
75
|
-
if (info.isSymbolicLink() || !hasPrivatePermissions(file, info)) {
|
|
76
|
-
throw new StorageFormatError(file, "private storage permissions cannot be verified");
|
|
77
|
-
}
|
|
94
|
+
if (current.isSymbolicLink() || !current.isDirectory())
|
|
95
|
+
throw new StorageFormatError(directory, "not a directory, or a link");
|
|
78
96
|
}
|
|
79
97
|
/** A directory reader never follows a linked root. Missing roots are
|
|
80
98
|
* empty; a present non-directory is a storage diagnostic. */
|
|
@@ -103,7 +121,7 @@ export function closed(value, required, optional, file) {
|
|
|
103
121
|
need(isObject(value), file, words().expectedObject());
|
|
104
122
|
need(required.every((key) => Object.hasOwn(value, key)) && Object.keys(value).every((key) => required.includes(key) || optional.includes(key)), file, words().expectedFields(listed(required), optional.length ? words().optionalFields(listed(optional)) : ""));
|
|
105
123
|
}
|
|
106
|
-
/** A reader refuses a layout version it does not know
|
|
124
|
+
/** A reader refuses a layout version it does not know: one
|
|
107
125
|
* format, or any of a list a reader reads. */
|
|
108
126
|
export function knownFormat(value, file, format = 1) {
|
|
109
127
|
const formats = typeof format === "number" ? [format] : format;
|
|
@@ -117,34 +135,23 @@ export function readJsonFile(file) {
|
|
|
117
135
|
throw new StorageFormatError(file, error.message);
|
|
118
136
|
}
|
|
119
137
|
}
|
|
120
|
-
|
|
138
|
+
// -- writes -------------------------------------------------------------------
|
|
139
|
+
//
|
|
140
|
+
// Every write is an atomic same-directory replacement through `publish`.
|
|
141
|
+
// Its caller chooses two things, never inferred from a mode:
|
|
142
|
+
//
|
|
143
|
+
// | | under a version (DR-111) | under none |
|
|
144
|
+
// | ------------------- | -------------------------- | --------------------- |
|
|
145
|
+
// | Spex-owned, private | writePrivateVersionedBytes | writeApplicationBytes |
|
|
146
|
+
// | ordinary file | writeVersionedBytes | writeWorkingBytes |
|
|
147
|
+
//
|
|
148
|
+
// A private file is created owner-only (storage-57); an ordinary one takes
|
|
149
|
+
// its folder's inherited permissions and keeps its mode (environments-49).
|
|
150
|
+
// No write checks the permissions of its folder or its file.
|
|
151
|
+
/** Atomic same-directory replacement of a Spex-owned file, created
|
|
152
|
+
* owner-only and synced before and after the rename (storage-57). */
|
|
121
153
|
export function writeApplicationBytes(file, bytes) {
|
|
122
|
-
|
|
123
|
-
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
124
|
-
const owned = process.platform === "win32" ? createFileSync(temporary, { private: true }) : undefined;
|
|
125
|
-
const fd = owned?.fd ?? openSync(temporary, "wx", 0o600);
|
|
126
|
-
try {
|
|
127
|
-
writeFileSync(fd, bytes);
|
|
128
|
-
fsyncSync(fd);
|
|
129
|
-
}
|
|
130
|
-
finally {
|
|
131
|
-
if (owned)
|
|
132
|
-
owned.close();
|
|
133
|
-
else
|
|
134
|
-
closeSync(fd);
|
|
135
|
-
}
|
|
136
|
-
assertPrivateWindowsEntry(temporary);
|
|
137
|
-
renameSync(temporary, file);
|
|
138
|
-
// Directory handles are not supported by Windows; the file is still synced.
|
|
139
|
-
if (process.platform !== "win32") {
|
|
140
|
-
const dir = openSync(dirname(file), "r");
|
|
141
|
-
try {
|
|
142
|
-
fsyncSync(dir);
|
|
143
|
-
}
|
|
144
|
-
finally {
|
|
145
|
-
closeSync(dir);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
154
|
+
publish(file, bytes, { privately: true });
|
|
148
155
|
}
|
|
149
156
|
export function writeApplicationFile(file, value) { writeApplicationBytes(file, JSON.stringify(value)); }
|
|
150
157
|
export const sha256 = (bytes) => createHash("sha256").update(bytes).digest("hex");
|
|
@@ -174,30 +181,57 @@ export function readVersioned(file) {
|
|
|
174
181
|
export const fileVersion = (file) => readVersioned(file).version;
|
|
175
182
|
/** Atomic same-directory replacement under a version: the file is
|
|
176
183
|
* checked at the instant before the rename and the write refused as a
|
|
177
|
-
* conflict where it no longer stands at `expected`.
|
|
178
|
-
* with `mode` exactly, the umask applying to none of it. No folder is made:
|
|
184
|
+
* conflict where it no longer stands at `expected`. No folder is made:
|
|
179
185
|
* a write whose folder is gone is refused the same way. The temporary
|
|
180
|
-
* file never outlives a refusal or a failure.
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
186
|
+
* file never outlives a refusal or a failure.
|
|
187
|
+
*
|
|
188
|
+
* This is an ordinary file of its folder (environments-49): it takes the
|
|
189
|
+
* folder's inherited permissions, none of them checked, and is published
|
|
190
|
+
* with `mode` exactly, else the mode the file holds, else `0644`, the
|
|
191
|
+
* umask applying to none of it. A Spex-owned file the home keeps private
|
|
192
|
+
* is written with `writePrivateVersionedBytes`. */
|
|
193
|
+
export function writeVersionedBytes(file, bytes, expected, mode) {
|
|
194
|
+
publish(file, bytes, { privately: false, expected, ...(mode !== undefined ? { mode } : {}) });
|
|
195
|
+
}
|
|
196
|
+
/** `writeVersionedBytes` for a Spex-owned file, created owner-only
|
|
197
|
+
* (storage-57). */
|
|
198
|
+
export function writePrivateVersionedBytes(file, bytes, expected) {
|
|
199
|
+
publish(file, bytes, { privately: true, expected });
|
|
200
|
+
}
|
|
201
|
+
/** Atomic same-directory replacement of an ordinary file of its folder,
|
|
202
|
+
* under no version (environments-49): the mode as `writeVersionedBytes`
|
|
203
|
+
* keeps it. No folder is made. */
|
|
204
|
+
export function writeWorkingBytes(file, bytes) {
|
|
205
|
+
publish(file, bytes, { privately: false });
|
|
206
|
+
}
|
|
207
|
+
/** The mode of the regular file at `file`, or undefined where none stands. */
|
|
208
|
+
function heldMode(file) {
|
|
209
|
+
const info = lstatSync(file, { throwIfNoEntry: false });
|
|
210
|
+
return info?.isFile() ? info.mode & 0o777 : undefined;
|
|
211
|
+
}
|
|
212
|
+
/** The one replacement every write above makes: written beside the file,
|
|
213
|
+
* synced, checked against `expected` where a version is given, renamed
|
|
214
|
+
* into place and the folder synced where the platform can. */
|
|
215
|
+
function publish(file, bytes, { privately, expected, mode }) {
|
|
216
|
+
const versioned = expected !== undefined;
|
|
217
|
+
const target = privately ? 0o600 : mode ?? heldMode(file) ?? 0o644;
|
|
184
218
|
const temporary = `${file}.${randomUUID()}.tmp`;
|
|
185
219
|
let fd;
|
|
186
220
|
let owned;
|
|
187
221
|
try {
|
|
188
|
-
if (process.platform === "win32"
|
|
222
|
+
if (privately && process.platform === "win32")
|
|
189
223
|
owned = createFileSync(temporary, { private: true });
|
|
190
|
-
fd = owned?.fd ?? openSync(temporary, "wx",
|
|
224
|
+
fd = owned?.fd ?? openSync(temporary, "wx", target);
|
|
191
225
|
}
|
|
192
226
|
catch (error) {
|
|
193
|
-
if (error.code === "ENOENT")
|
|
227
|
+
if (versioned && error.code === "ENOENT")
|
|
194
228
|
throw new VersionConflictError(file);
|
|
195
229
|
throw error;
|
|
196
230
|
}
|
|
197
231
|
try {
|
|
198
232
|
try {
|
|
199
233
|
writeFileSync(fd, bytes);
|
|
200
|
-
fchmodSync(fd,
|
|
234
|
+
fchmodSync(fd, target);
|
|
201
235
|
fsyncSync(fd);
|
|
202
236
|
}
|
|
203
237
|
finally {
|
|
@@ -206,9 +240,7 @@ export function writeVersionedBytes(file, bytes, expected, mode = 0o600) {
|
|
|
206
240
|
else
|
|
207
241
|
closeSync(fd);
|
|
208
242
|
}
|
|
209
|
-
if (
|
|
210
|
-
assertPrivateWindowsEntry(temporary);
|
|
211
|
-
if (fileVersion(file) !== expected)
|
|
243
|
+
if (versioned && fileVersion(file) !== expected)
|
|
212
244
|
throw new VersionConflictError(file);
|
|
213
245
|
renameSync(temporary, file);
|
|
214
246
|
}
|
|
@@ -216,6 +248,7 @@ export function writeVersionedBytes(file, bytes, expected, mode = 0o600) {
|
|
|
216
248
|
rmSync(temporary, { force: true });
|
|
217
249
|
throw error;
|
|
218
250
|
}
|
|
251
|
+
// Directory handles are not supported by Windows; the file is still synced.
|
|
219
252
|
if (process.platform !== "win32") {
|
|
220
253
|
const dir = openSync(dirname(file), "r");
|
|
221
254
|
try {
|
|
@@ -227,7 +260,109 @@ export function writeVersionedBytes(file, bytes, expected, mode = 0o600) {
|
|
|
227
260
|
}
|
|
228
261
|
}
|
|
229
262
|
/** Whether a path of a clone may leave the device: not one of the
|
|
230
|
-
* catalog's ignored families
|
|
231
|
-
export const portable = (file) => !/^(\.spex\/|packages\/|skills\/|local\/|\.spex-uploads\/|\.spex-apply\.json$|\.lock)/.test(file) &&
|
|
263
|
+
* catalog's ignored families. */
|
|
264
|
+
export const portable = (file) => !/^(\.spex\/|packages\/|skills\/|local\/|\.spex-uploads\/|\.spex-apply\.json$|\.lock|\.legacy-sessions\/)/.test(file) &&
|
|
232
265
|
!/\.(hints|spex)\.json$|(?<!^spex)\.lock(?:\.|\/|$)|\.tmp$|\.bak(?:\.|$)|\.backup(?:\.|$)/.test(file);
|
|
266
|
+
/** Remove a file or a tree as `rmSync` with `recursive` and `force`
|
|
267
|
+
* does, read-only files included, following no link: a link or Windows
|
|
268
|
+
* junction goes itself, its target untouched (environments-7). On
|
|
269
|
+
* Windows the Node the app's Electron ships refuses read-only files in
|
|
270
|
+
* `rmSync` and empties a junction's target, so the tree is walked here
|
|
271
|
+
* with `unlinkSync`, which clears a read-only mark, and `rmdirSync`. */
|
|
272
|
+
export function removeTree(path) {
|
|
273
|
+
if (process.platform !== "win32")
|
|
274
|
+
rmSync(path, { recursive: true, force: true });
|
|
275
|
+
else
|
|
276
|
+
removeEntry(path);
|
|
277
|
+
}
|
|
278
|
+
/** A folder's rename refused because something holds a file or folder
|
|
279
|
+
* beneath it open: Windows refuses to rename a folder while any handle
|
|
280
|
+
* inside it stands — a watch, an open file, a process's working folder —
|
|
281
|
+
* a rule no other platform has (moveFolder). */
|
|
282
|
+
export class FolderInUseError extends Error {
|
|
283
|
+
path;
|
|
284
|
+
code;
|
|
285
|
+
constructor(path, cause) {
|
|
286
|
+
super(cause.message, { cause });
|
|
287
|
+
this.path = path;
|
|
288
|
+
this.name = "FolderInUseError";
|
|
289
|
+
this.code = cause.code;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
/** Rename a folder as `renameSync` does; where Windows refuses it with
|
|
293
|
+
* `EPERM` or `EBUSY`, which it answers while something holds a file or
|
|
294
|
+
* folder beneath it open, the refusal is a FolderInUseError, which
|
|
295
|
+
* holds on no other platform. */
|
|
296
|
+
export function moveFolder(from, to) {
|
|
297
|
+
try {
|
|
298
|
+
renameSync(from, to);
|
|
299
|
+
}
|
|
300
|
+
catch (error) {
|
|
301
|
+
const { code } = error;
|
|
302
|
+
if (process.platform === "win32" && (code === "EPERM" || code === "EBUSY"))
|
|
303
|
+
throw new FolderInUseError(from, error);
|
|
304
|
+
throw error;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
/** How often, and how far apart, a step a held folder refuses is tried
|
|
308
|
+
* again (retryWhileInUse): counted in tries, not time, so an event loop
|
|
309
|
+
* busy meanwhile spends none of them. */
|
|
310
|
+
export const IN_USE_RETRIES = 20;
|
|
311
|
+
export const IN_USE_RETRY_WAIT_MS = 50;
|
|
312
|
+
/** Run `attempt` — one step at its instant, which undoes what it did
|
|
313
|
+
* before it throws — and run it again, whole, while a folder it renames
|
|
314
|
+
* is held (FolderInUseError) or `again` takes its refusal for one worth
|
|
315
|
+
* the same, up to IN_USE_RETRIES more times IN_USE_RETRY_WAIT_MS apart,
|
|
316
|
+
* so what holds it briefly — a process finishing, a file being closed —
|
|
317
|
+
* lets go; the last refusal then stands. The first try runs at once,
|
|
318
|
+
* within its caller's step. */
|
|
319
|
+
export async function retryWhileInUse(attempt, again = () => false) {
|
|
320
|
+
for (let retries = 0;; retries += 1) {
|
|
321
|
+
try {
|
|
322
|
+
return await attempt();
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
if (!(error instanceof FolderInUseError || again(error)) || retries >= IN_USE_RETRIES)
|
|
326
|
+
throw error;
|
|
327
|
+
await new Promise((resolve) => setTimeout(resolve, IN_USE_RETRY_WAIT_MS));
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
/** `removeTree` for garbage — a stage, or what a removal set aside —
|
|
332
|
+
* whose failure is said on the console, never in place of the outcome
|
|
333
|
+
* of the work that made it. */
|
|
334
|
+
export function discardTree(path, what) {
|
|
335
|
+
try {
|
|
336
|
+
removeTree(path);
|
|
337
|
+
}
|
|
338
|
+
catch (error) {
|
|
339
|
+
console.error(`spex: ${what} could not be removed at ${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
/** Remove one entry beneath a Windows tree. An entry gone meanwhile
|
|
343
|
+
* counts as removed. */
|
|
344
|
+
function removeEntry(path) {
|
|
345
|
+
const stat = gone(() => lstatSync(path));
|
|
346
|
+
if (!stat)
|
|
347
|
+
return;
|
|
348
|
+
// A link or junction reads as a link, never as a folder.
|
|
349
|
+
if (stat.isDirectory()) {
|
|
350
|
+
for (const name of readdirSync(path))
|
|
351
|
+
removeEntry(join(path, name));
|
|
352
|
+
gone(() => rmdirSync(path));
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
gone(() => unlinkSync(path));
|
|
356
|
+
}
|
|
357
|
+
/** Run a step on an entry, undefined where the entry is gone. */
|
|
358
|
+
function gone(step) {
|
|
359
|
+
try {
|
|
360
|
+
return step();
|
|
361
|
+
}
|
|
362
|
+
catch (error) {
|
|
363
|
+
if (error.code !== "ENOENT")
|
|
364
|
+
throw error;
|
|
365
|
+
return undefined;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
233
368
|
//# sourceMappingURL=files.js.map
|