@botharness/core 1.5.2 → 1.6.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 +495 -1
- package/CHANGELOG.zh.md +495 -1
- package/cli/index.mjs +71086 -67555
- package/dist/folder-browse-worker-BfVjXz4j.d.mts +41 -0
- package/dist/folder-browse-worker.d.mts +2 -0
- package/dist/folder-browse-worker.mjs +102 -0
- package/dist/index.d.mts +1643 -62
- package/dist/index.mjs +104809 -96423
- package/package.json +2 -1
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//#region packages/core/src/workspaces/folder-browse-worker.d.ts
|
|
2
|
+
interface FolderReadRequest {
|
|
3
|
+
id: number;
|
|
4
|
+
path: string;
|
|
5
|
+
platform: NodeJS.Platform;
|
|
6
|
+
offset: number;
|
|
7
|
+
limit: number;
|
|
8
|
+
}
|
|
9
|
+
interface FolderReadEntry {
|
|
10
|
+
name: string;
|
|
11
|
+
hidden: boolean;
|
|
12
|
+
}
|
|
13
|
+
type FolderReadResult = {
|
|
14
|
+
ok: true;
|
|
15
|
+
entries: FolderReadEntry[];
|
|
16
|
+
total: number;
|
|
17
|
+
} | {
|
|
18
|
+
ok: false;
|
|
19
|
+
code: string;
|
|
20
|
+
message: string;
|
|
21
|
+
};
|
|
22
|
+
type FolderReadReply = FolderReadResult & {
|
|
23
|
+
id: number;
|
|
24
|
+
};
|
|
25
|
+
interface FolderReadFs {
|
|
26
|
+
readdir(path: string): Promise<FolderReadDirent[]>;
|
|
27
|
+
isDirectory(path: string): Promise<boolean>;
|
|
28
|
+
}
|
|
29
|
+
interface FolderReadDirent {
|
|
30
|
+
name: string;
|
|
31
|
+
isDirectory(): boolean;
|
|
32
|
+
isSymbolicLink(): boolean;
|
|
33
|
+
}
|
|
34
|
+
declare function readFolderLevel(target: string, platform: NodeJS.Platform, fs?: FolderReadFs): Promise<FolderReadEntry[]>;
|
|
35
|
+
declare function readFailure(error: unknown): {
|
|
36
|
+
code: string;
|
|
37
|
+
message: string;
|
|
38
|
+
};
|
|
39
|
+
declare function createFolderReader(fs?: FolderReadFs, now?: () => number): (request: FolderReadRequest) => Promise<FolderReadReply>;
|
|
40
|
+
//#endregion
|
|
41
|
+
export { FolderReadRequest as a, readFailure as c, FolderReadReply as i, readFolderLevel as l, FolderReadEntry as n, FolderReadResult as o, FolderReadFs as r, createFolderReader as s, FolderReadDirent as t };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as FolderReadRequest, c as readFailure, i as FolderReadReply, l as readFolderLevel, n as FolderReadEntry, o as FolderReadResult, r as FolderReadFs, s as createFolderReader, t as FolderReadDirent } from "./folder-browse-worker-BfVjXz4j.mjs";
|
|
2
|
+
export { FolderReadDirent, FolderReadEntry, FolderReadFs, FolderReadReply, FolderReadRequest, FolderReadResult, createFolderReader, readFailure, readFolderLevel };
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import nodePath from "node:path";
|
|
2
|
+
import { readdir, stat } from "node:fs/promises";
|
|
3
|
+
//#region packages/core/src/workspaces/folder-browse-worker.ts
|
|
4
|
+
const CACHE_MS = 3e4;
|
|
5
|
+
const CACHE_LEVELS = 8;
|
|
6
|
+
const LINK_CONCURRENCY = 8;
|
|
7
|
+
const nodeFs = {
|
|
8
|
+
readdir: (path) => readdir(path, { withFileTypes: true }),
|
|
9
|
+
async isDirectory(path) {
|
|
10
|
+
try {
|
|
11
|
+
return (await stat(path)).isDirectory();
|
|
12
|
+
} catch {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const collator = new Intl.Collator(void 0, { sensitivity: "base" });
|
|
18
|
+
function compare(left, right) {
|
|
19
|
+
return Number(left.hidden) - Number(right.hidden) || collator.compare(left.name, right.name);
|
|
20
|
+
}
|
|
21
|
+
async function readFolderLevel(target, platform, fs = nodeFs) {
|
|
22
|
+
const path = platform === "win32" ? nodePath.win32 : nodePath.posix;
|
|
23
|
+
const names = [];
|
|
24
|
+
const links = [];
|
|
25
|
+
for (const entry of await fs.readdir(target)) if (entry.isDirectory()) names.push(entry.name);
|
|
26
|
+
else if (entry.isSymbolicLink()) links.push(entry.name);
|
|
27
|
+
for (let at = 0; at < links.length; at += LINK_CONCURRENCY) {
|
|
28
|
+
const batch = links.slice(at, at + LINK_CONCURRENCY);
|
|
29
|
+
const kinds = await Promise.all(batch.map((name) => fs.isDirectory(path.join(target, name))));
|
|
30
|
+
batch.forEach((name, index) => {
|
|
31
|
+
if (kinds[index]) names.push(name);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return names.map((name) => ({
|
|
35
|
+
name,
|
|
36
|
+
hidden: name.startsWith(".")
|
|
37
|
+
})).toSorted(compare);
|
|
38
|
+
}
|
|
39
|
+
function errnoCode(error) {
|
|
40
|
+
return typeof error === "object" && error !== null && "code" in error ? String(error.code) : void 0;
|
|
41
|
+
}
|
|
42
|
+
function readFailure(error) {
|
|
43
|
+
const code = errnoCode(error);
|
|
44
|
+
if (code === "ENOENT") return {
|
|
45
|
+
code: "folder-browse/not-found",
|
|
46
|
+
message: "That folder does not exist."
|
|
47
|
+
};
|
|
48
|
+
if (code === "ENOTDIR") return {
|
|
49
|
+
code: "folder-browse/not-directory",
|
|
50
|
+
message: "That path is not a folder."
|
|
51
|
+
};
|
|
52
|
+
if (code === "EACCES" || code === "EPERM") return {
|
|
53
|
+
code: "folder-browse/denied",
|
|
54
|
+
message: "The computer does not allow reading that folder."
|
|
55
|
+
};
|
|
56
|
+
return {
|
|
57
|
+
code: "folder-browse/unreadable",
|
|
58
|
+
message: `The computer could not read that folder${code === void 0 ? "" : ` (${code})`}.`
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function createFolderReader(fs = nodeFs, now = Date.now) {
|
|
62
|
+
const levels = /* @__PURE__ */ new Map();
|
|
63
|
+
return async (request) => {
|
|
64
|
+
try {
|
|
65
|
+
const key = `${request.platform}:${request.path}`;
|
|
66
|
+
let level = levels.get(key);
|
|
67
|
+
if (level === void 0 || request.offset === 0 || now() - level.at > CACHE_MS) {
|
|
68
|
+
level = {
|
|
69
|
+
at: now(),
|
|
70
|
+
entries: await readFolderLevel(request.path, request.platform, fs)
|
|
71
|
+
};
|
|
72
|
+
levels.delete(key);
|
|
73
|
+
levels.set(key, level);
|
|
74
|
+
for (const stale of levels.keys()) {
|
|
75
|
+
if (levels.size <= CACHE_LEVELS) break;
|
|
76
|
+
levels.delete(stale);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
id: request.id,
|
|
81
|
+
ok: true,
|
|
82
|
+
entries: level.entries.slice(request.offset, request.offset + request.limit),
|
|
83
|
+
total: level.entries.length
|
|
84
|
+
};
|
|
85
|
+
} catch (error) {
|
|
86
|
+
return {
|
|
87
|
+
id: request.id,
|
|
88
|
+
ok: false,
|
|
89
|
+
...readFailure(error)
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
if (process.env["BOTHARNESS_FOLDER_BROWSE_WORKER"] === "1" && process.send !== void 0) {
|
|
95
|
+
const read = createFolderReader();
|
|
96
|
+
process.on("message", (request) => {
|
|
97
|
+
read(request).then((reply) => process.send?.(reply));
|
|
98
|
+
});
|
|
99
|
+
process.on("disconnect", () => process.exit(0));
|
|
100
|
+
}
|
|
101
|
+
//#endregion
|
|
102
|
+
export { createFolderReader, readFailure, readFolderLevel };
|