buildrush 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +84 -2
- package/SECURITY.md +57 -0
- package/bin/buildrush.js +6 -0
- package/dist/src/adapters.d.ts +23 -0
- package/dist/src/adapters.js +187 -0
- package/dist/src/adapters.js.map +1 -0
- package/dist/src/affected.d.ts +28 -0
- package/dist/src/affected.js +59 -0
- package/dist/src/affected.js.map +1 -0
- package/dist/src/cache.d.ts +94 -0
- package/dist/src/cache.js +362 -0
- package/dist/src/cache.js.map +1 -0
- package/dist/src/cli.d.ts +33 -0
- package/dist/src/cli.js +684 -0
- package/dist/src/cli.js.map +1 -0
- package/dist/src/config.d.ts +84 -0
- package/dist/src/config.js +394 -0
- package/dist/src/config.js.map +1 -0
- package/dist/src/daemon/cli.d.ts +36 -0
- package/dist/src/daemon/cli.js +202 -0
- package/dist/src/daemon/cli.js.map +1 -0
- package/dist/src/daemon/client.d.ts +106 -0
- package/dist/src/daemon/client.js +267 -0
- package/dist/src/daemon/client.js.map +1 -0
- package/dist/src/daemon/ops.d.ts +66 -0
- package/dist/src/daemon/ops.js +47 -0
- package/dist/src/daemon/ops.js.map +1 -0
- package/dist/src/daemon/paths.d.ts +62 -0
- package/dist/src/daemon/paths.js +192 -0
- package/dist/src/daemon/paths.js.map +1 -0
- package/dist/src/daemon/protocol.d.ts +60 -0
- package/dist/src/daemon/protocol.js +62 -0
- package/dist/src/daemon/protocol.js.map +1 -0
- package/dist/src/daemon/server.d.ts +68 -0
- package/dist/src/daemon/server.js +441 -0
- package/dist/src/daemon/server.js.map +1 -0
- package/dist/src/detect.d.ts +12 -0
- package/dist/src/detect.js +68 -0
- package/dist/src/detect.js.map +1 -0
- package/dist/src/doctor.d.ts +6 -0
- package/dist/src/doctor.js +94 -0
- package/dist/src/doctor.js.map +1 -0
- package/dist/src/engine.d.ts +70 -0
- package/dist/src/engine.js +655 -0
- package/dist/src/engine.js.map +1 -0
- package/dist/src/fsindex.d.ts +37 -0
- package/dist/src/fsindex.js +128 -0
- package/dist/src/fsindex.js.map +1 -0
- package/dist/src/fswatch.d.ts +30 -0
- package/dist/src/fswatch.js +141 -0
- package/dist/src/fswatch.js.map +1 -0
- package/dist/src/graph.d.ts +77 -0
- package/dist/src/graph.js +486 -0
- package/dist/src/graph.js.map +1 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/index.js +9 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/project.d.ts +45 -0
- package/dist/src/project.js +138 -0
- package/dist/src/project.js.map +1 -0
- package/dist/src/runner.d.ts +36 -0
- package/dist/src/runner.js +174 -0
- package/dist/src/runner.js.map +1 -0
- package/dist/src/scheduler.d.ts +30 -0
- package/dist/src/scheduler.js +143 -0
- package/dist/src/scheduler.js.map +1 -0
- package/dist/src/testcache.d.ts +60 -0
- package/dist/src/testcache.js +93 -0
- package/dist/src/testcache.js.map +1 -0
- package/dist/src/util/fsx.d.ts +11 -0
- package/dist/src/util/fsx.js +61 -0
- package/dist/src/util/fsx.js.map +1 -0
- package/dist/src/util/glob.d.ts +19 -0
- package/dist/src/util/glob.js +193 -0
- package/dist/src/util/glob.js.map +1 -0
- package/dist/src/util/hash.d.ts +14 -0
- package/dist/src/util/hash.js +57 -0
- package/dist/src/util/hash.js.map +1 -0
- package/dist/src/util/paths.d.ts +17 -0
- package/dist/src/util/paths.js +67 -0
- package/dist/src/util/paths.js.map +1 -0
- package/dist/src/watch.d.ts +101 -0
- package/dist/src/watch.js +289 -0
- package/dist/src/watch.js.map +1 -0
- package/dist/src/workspaces.d.ts +118 -0
- package/dist/src/workspaces.js +590 -0
- package/dist/src/workspaces.js.map +1 -0
- package/package.json +47 -4
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { RunReport } from '../engine.js';
|
|
2
|
+
import { DepGraph } from '../graph.js';
|
|
3
|
+
import { Project } from '../project.js';
|
|
4
|
+
/**
|
|
5
|
+
* Operations shared verbatim by the one-shot CLI and the daemon, so both produce byte-identical
|
|
6
|
+
* results from the same code (the daemon never has its own copy of the logic).
|
|
7
|
+
*/
|
|
8
|
+
/** Package -> packages it imports from, as observed in the module graph (sorted, for display). */
|
|
9
|
+
export declare function packageImportEdges(g: DepGraph): Map<string, string[]>;
|
|
10
|
+
export declare function analyze(p: Project): Promise<{
|
|
11
|
+
result: {
|
|
12
|
+
project: string;
|
|
13
|
+
root: string;
|
|
14
|
+
packageManager: "bun" | "npm" | "pnpm" | "yarn" | null;
|
|
15
|
+
tools: string[];
|
|
16
|
+
workspacePackages: {
|
|
17
|
+
[k: string]: string;
|
|
18
|
+
};
|
|
19
|
+
workspace: {
|
|
20
|
+
manager: import("../workspaces.js").WorkspaceManager;
|
|
21
|
+
source: string;
|
|
22
|
+
patterns: string[];
|
|
23
|
+
warnings: string[];
|
|
24
|
+
packages: {
|
|
25
|
+
name: string;
|
|
26
|
+
version: string | null;
|
|
27
|
+
dir: string;
|
|
28
|
+
dependsOn: string[];
|
|
29
|
+
dependents: string[];
|
|
30
|
+
}[];
|
|
31
|
+
importEdges: {
|
|
32
|
+
[k: string]: string[];
|
|
33
|
+
};
|
|
34
|
+
cycle: string[] | null;
|
|
35
|
+
} | null;
|
|
36
|
+
files: number;
|
|
37
|
+
modules: number;
|
|
38
|
+
edges: number;
|
|
39
|
+
tests: number;
|
|
40
|
+
importCycles: string[][];
|
|
41
|
+
uncertainModules: {
|
|
42
|
+
path: string;
|
|
43
|
+
reasons: string[];
|
|
44
|
+
}[];
|
|
45
|
+
skippedSymlinks: number;
|
|
46
|
+
tasks: {
|
|
47
|
+
[k: string]: {
|
|
48
|
+
kind: import("../config.js").TaskKind;
|
|
49
|
+
dependsOn: string[];
|
|
50
|
+
cache: boolean;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
det: import("../detect.js").Detection;
|
|
55
|
+
g: DepGraph;
|
|
56
|
+
uncertain: import("../graph.js").GraphModule[];
|
|
57
|
+
cycles: string[][];
|
|
58
|
+
edges: number;
|
|
59
|
+
}>;
|
|
60
|
+
export declare function cacheStats(p: Project): Promise<{
|
|
61
|
+
stats: import("../cache.js").CacheStats;
|
|
62
|
+
lifetime: import("../engine.js").LifetimeStats;
|
|
63
|
+
maxSizeMB: number;
|
|
64
|
+
}>;
|
|
65
|
+
/** What `buildrush explain` reads back; written after every run, in-process or via the daemon. */
|
|
66
|
+
export declare function writeLastRun(p: Project, requested: string[], report: RunReport): Promise<void>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as path from 'node:path';
|
|
2
|
+
import { detectProject } from '../detect.js';
|
|
3
|
+
import { readStats } from '../engine.js';
|
|
4
|
+
import { writeFileAtomic } from '../util/fsx.js';
|
|
5
|
+
/**
|
|
6
|
+
* Operations shared verbatim by the one-shot CLI and the daemon, so both produce byte-identical
|
|
7
|
+
* results from the same code (the daemon never has its own copy of the logic).
|
|
8
|
+
*/
|
|
9
|
+
/** Package -> packages it imports from, as observed in the module graph (sorted, for display). */
|
|
10
|
+
export function packageImportEdges(g) {
|
|
11
|
+
return new Map([...g.packageEdges()].sort(([a], [b]) => (a < b ? -1 : 1)).map(([k, v]) => [k, [...v].sort()]));
|
|
12
|
+
}
|
|
13
|
+
export async function analyze(p) {
|
|
14
|
+
const det = await detectProject(p.root);
|
|
15
|
+
const g = await p.graph();
|
|
16
|
+
await p.saveGraph(g);
|
|
17
|
+
const cycles = g.cycles();
|
|
18
|
+
const uncertain = [...g.modules.values()].filter((m) => m.uncertain.length);
|
|
19
|
+
const edges = [...g.deps.values()].reduce((n, m) => n + m.size, 0);
|
|
20
|
+
const result = {
|
|
21
|
+
project: p.config.project.name,
|
|
22
|
+
root: p.root,
|
|
23
|
+
packageManager: det.packageManager,
|
|
24
|
+
tools: det.tools,
|
|
25
|
+
workspacePackages: Object.fromEntries(g.workspacePackages),
|
|
26
|
+
workspace: p.workspace ? { ...p.workspace.summary(), importEdges: Object.fromEntries(packageImportEdges(g)), cycle: p.workspace.findCycle() } : null,
|
|
27
|
+
files: p.index.files.size,
|
|
28
|
+
modules: g.modules.size,
|
|
29
|
+
edges,
|
|
30
|
+
tests: g.testFiles().length,
|
|
31
|
+
importCycles: cycles,
|
|
32
|
+
uncertainModules: uncertain.map((m) => ({ path: m.path, reasons: m.uncertain })),
|
|
33
|
+
skippedSymlinks: p.index.skippedSymlinks.length,
|
|
34
|
+
tasks: Object.fromEntries(Object.values(p.config.tasks).map((t) => [t.name, { kind: t.kind, dependsOn: t.dependsOn, cache: t.cache }])),
|
|
35
|
+
};
|
|
36
|
+
return { result, det, g, uncertain, cycles, edges };
|
|
37
|
+
}
|
|
38
|
+
export async function cacheStats(p) {
|
|
39
|
+
const stats = await p.cache.stats();
|
|
40
|
+
const lifetime = await readStats(p);
|
|
41
|
+
return { stats, lifetime, maxSizeMB: p.config.cache.maxSizeMB };
|
|
42
|
+
}
|
|
43
|
+
/** What `buildrush explain` reads back; written after every run, in-process or via the daemon. */
|
|
44
|
+
export async function writeLastRun(p, requested, report) {
|
|
45
|
+
await writeFileAtomic(path.join(p.stateDir, 'last-run.json'), JSON.stringify({ at: Date.now(), requested, tasks: report.tasks.map((t) => ({ ...t, log: undefined })), selection: report.selection, packages: report.packages ?? null }));
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=ops.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ops.js","sourceRoot":"","sources":["../../../src/daemon/ops.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAa,MAAM,cAAc,CAAC;AAGpD,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEjD;;;GAGG;AAEH,kGAAkG;AAClG,MAAM,UAAU,kBAAkB,CAAC,CAAW;IAC5C,OAAO,IAAI,GAAG,CAAC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AAC,CAAU;IACzC,MAAM,KAAK,GAAG,MAAM,CAAC,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IACpC,MAAM,QAAQ,GAAG,MAAM,SAAS,CAAC,CAAC,CAAC,CAAC;IACpC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,CAAC;AAClE,CAAC;AAED,kGAAkG;AAClG,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,CAAU,EAAE,SAAmB,EAAE,MAAiB;IACnF,MAAM,eAAe,CACnB,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,QAAQ,EAAE,eAAe,CAAC,EACtC,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,CAAC,SAAS,EAAE,QAAQ,EAAE,MAAM,CAAC,QAAQ,IAAI,IAAI,EAAE,CAAC,CAC1K,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a project's daemon keeps its endpoint, token and metadata.
|
|
3
|
+
*
|
|
4
|
+
* Everything lives in a per-user directory OUTSIDE the project tree:
|
|
5
|
+
* POSIX $XDG_RUNTIME_DIR/buildrush (only if that directory is ours and mode 0700), else
|
|
6
|
+
* ${XDG_CACHE_HOME:-~/.cache}/buildrush/run (under the user's own home)
|
|
7
|
+
* Windows %LOCALAPPDATA%\BuildRush\daemon (per-user profile ACL)
|
|
8
|
+
* Never the shared temp directory: if no private per-user location exists the daemon is unavailable
|
|
9
|
+
* (callers fall back to one-shot), it is not relocated to /tmp.
|
|
10
|
+
* override: BUILDRUSH_DAEMON_DIR (tests, unusual setups); validated exactly like the defaults.
|
|
11
|
+
* Each project gets its own 0700 subdirectory keyed by a hash of its realpath.
|
|
12
|
+
*/
|
|
13
|
+
export interface DaemonDirs {
|
|
14
|
+
base: string;
|
|
15
|
+
/** Per-project directory (0700). */
|
|
16
|
+
dir: string;
|
|
17
|
+
key: string;
|
|
18
|
+
token: string;
|
|
19
|
+
meta: string;
|
|
20
|
+
log: string;
|
|
21
|
+
/** Held (as a directory) while a daemon is starting; serialises concurrent starters. */
|
|
22
|
+
lock: string;
|
|
23
|
+
/** POSIX: the Unix socket path (inside `dir`). Windows: unused (the pipe name is in meta). */
|
|
24
|
+
socket: string;
|
|
25
|
+
}
|
|
26
|
+
export interface DaemonMeta {
|
|
27
|
+
proto: number;
|
|
28
|
+
version: string;
|
|
29
|
+
pid: number;
|
|
30
|
+
root: string;
|
|
31
|
+
/** Unix socket path or Windows pipe name. */
|
|
32
|
+
endpoint: string;
|
|
33
|
+
startedAt: number;
|
|
34
|
+
node: string;
|
|
35
|
+
}
|
|
36
|
+
export declare function normalizeRoot(root: string): string;
|
|
37
|
+
export declare function projectKey(root: string): string;
|
|
38
|
+
export declare function resolveBaseDir(env?: NodeJS.ProcessEnv): string;
|
|
39
|
+
export declare function daemonDirs(root: string, env?: NodeJS.ProcessEnv): DaemonDirs;
|
|
40
|
+
export declare function assertSocketPathFits(p: string): void;
|
|
41
|
+
/** A fresh, unguessable Windows pipe name (pipes live in one global namespace). */
|
|
42
|
+
export declare function newPipeName(key: string): string;
|
|
43
|
+
/**
|
|
44
|
+
* Create `dir` if needed and make sure it is a real directory, owned by us and (POSIX) mode 0700.
|
|
45
|
+
* A pre-existing directory we own with loose permissions is tightened; anything else is refused.
|
|
46
|
+
* Windows: a directory we create is restricted to the current user via icacls (best effort).
|
|
47
|
+
*/
|
|
48
|
+
export declare function ensureSecureDir(dir: string): Promise<{
|
|
49
|
+
created: boolean;
|
|
50
|
+
acl: 'restricted' | 'inherited' | 'n/a';
|
|
51
|
+
}>;
|
|
52
|
+
/** Read-only variant used by clients: never creates or repairs anything. Returns a reason when unsafe. */
|
|
53
|
+
export declare function checkSecureDir(dir: string): Promise<string | null>;
|
|
54
|
+
/**
|
|
55
|
+
* Read a secret file through a single no-follow handle, verifying type, owner and mode on that same
|
|
56
|
+
* handle (no check-then-use race). Returns the text, 'absent', or { unsafe: reason }.
|
|
57
|
+
*/
|
|
58
|
+
export declare function readSecureFile(file: string): Promise<string | 'absent' | {
|
|
59
|
+
unsafe: string;
|
|
60
|
+
}>;
|
|
61
|
+
/** Write a secret: never follows an existing path, mode 0600 from the first byte. */
|
|
62
|
+
export declare function writeSecretFile(file: string, data: string): Promise<void>;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import * as fs from 'node:fs/promises';
|
|
2
|
+
import { constants as fsc, lstatSync } from 'node:fs';
|
|
3
|
+
import * as os from 'node:os';
|
|
4
|
+
import * as path from 'node:path';
|
|
5
|
+
import { createHash, randomBytes } from 'node:crypto';
|
|
6
|
+
import { execFile } from 'node:child_process';
|
|
7
|
+
const isWin = process.platform === 'win32';
|
|
8
|
+
export function normalizeRoot(root) {
|
|
9
|
+
return isWin ? root.toLowerCase() : root;
|
|
10
|
+
}
|
|
11
|
+
export function projectKey(root) {
|
|
12
|
+
return createHash('sha256').update('buildrush-daemon\0' + normalizeRoot(root)).digest('hex').slice(0, 16);
|
|
13
|
+
}
|
|
14
|
+
export function resolveBaseDir(env = process.env) {
|
|
15
|
+
if (env.BUILDRUSH_DAEMON_DIR)
|
|
16
|
+
return path.resolve(env.BUILDRUSH_DAEMON_DIR);
|
|
17
|
+
if (isWin) {
|
|
18
|
+
if (env.LOCALAPPDATA && path.isAbsolute(env.LOCALAPPDATA))
|
|
19
|
+
return path.join(env.LOCALAPPDATA, 'BuildRush', 'daemon');
|
|
20
|
+
throw new Error('no per-user location for daemon state (LOCALAPPDATA is not set)');
|
|
21
|
+
}
|
|
22
|
+
const rt = env.XDG_RUNTIME_DIR;
|
|
23
|
+
if (rt && path.isAbsolute(rt) && privateOwnDir(rt))
|
|
24
|
+
return path.join(rt, 'buildrush');
|
|
25
|
+
const cacheHome = env.XDG_CACHE_HOME && path.isAbsolute(env.XDG_CACHE_HOME) ? env.XDG_CACHE_HOME : env.HOME && path.isAbsolute(env.HOME) ? path.join(env.HOME, '.cache') : null;
|
|
26
|
+
if (!cacheHome)
|
|
27
|
+
throw new Error('no per-user location for daemon state (set XDG_RUNTIME_DIR, XDG_CACHE_HOME or HOME)');
|
|
28
|
+
return path.join(cacheHome, 'buildrush', 'run');
|
|
29
|
+
}
|
|
30
|
+
/** XDG_RUNTIME_DIR is only trusted if it is a real directory owned by us with no group/other access. */
|
|
31
|
+
function privateOwnDir(dir) {
|
|
32
|
+
try {
|
|
33
|
+
const st = lstatSync(dir);
|
|
34
|
+
return st.isDirectory() && (typeof process.getuid !== 'function' || st.uid === process.getuid()) && (st.mode & 0o077) === 0;
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** sun_path is 104 bytes on macOS/BSD and 108 on Linux; stay below the smaller. */
|
|
41
|
+
const MAX_SOCKET_PATH = 100;
|
|
42
|
+
export function daemonDirs(root, env = process.env) {
|
|
43
|
+
const base = resolveBaseDir(env);
|
|
44
|
+
const key = projectKey(root);
|
|
45
|
+
const dir = path.join(base, key);
|
|
46
|
+
let socket = path.join(dir, 's');
|
|
47
|
+
if (!isWin && Buffer.byteLength(socket) > MAX_SOCKET_PATH)
|
|
48
|
+
socket = path.join(base, `${key.slice(0, 8)}.s`); // shorter, still inside the 0700 base
|
|
49
|
+
if (!isWin)
|
|
50
|
+
assertSocketPathFits(socket); // too long even so: daemon unavailable, never relocated to /tmp
|
|
51
|
+
return {
|
|
52
|
+
base, dir, key,
|
|
53
|
+
token: path.join(dir, 'token'),
|
|
54
|
+
meta: path.join(dir, 'daemon.json'),
|
|
55
|
+
log: path.join(dir, 'log'),
|
|
56
|
+
lock: path.join(dir, 'starting'),
|
|
57
|
+
socket,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export function assertSocketPathFits(p) {
|
|
61
|
+
if (!isWin && Buffer.byteLength(p) > MAX_SOCKET_PATH) {
|
|
62
|
+
throw new Error(`daemon socket path is too long for this OS (${Buffer.byteLength(p)} > ${MAX_SOCKET_PATH} bytes): ${p}; the daemon is disabled for this project (set BUILDRUSH_DAEMON_DIR to a shorter private directory)`);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/** A fresh, unguessable Windows pipe name (pipes live in one global namespace). */
|
|
66
|
+
export function newPipeName(key) {
|
|
67
|
+
return `\\\\.\\pipe\\buildrush-${key}-${randomBytes(8).toString('hex')}`;
|
|
68
|
+
}
|
|
69
|
+
function posixOwnerCheck(st, what) {
|
|
70
|
+
if (typeof process.getuid === 'function' && st.uid !== process.getuid()) {
|
|
71
|
+
throw new Error(`${what} is not owned by the current user; refusing to use it`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Create `dir` if needed and make sure it is a real directory, owned by us and (POSIX) mode 0700.
|
|
76
|
+
* A pre-existing directory we own with loose permissions is tightened; anything else is refused.
|
|
77
|
+
* Windows: a directory we create is restricted to the current user via icacls (best effort).
|
|
78
|
+
*/
|
|
79
|
+
export async function ensureSecureDir(dir) {
|
|
80
|
+
await fs.mkdir(path.dirname(dir), { recursive: true });
|
|
81
|
+
let created = true;
|
|
82
|
+
try {
|
|
83
|
+
await fs.mkdir(dir, { mode: 0o700 });
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
if (e.code !== 'EEXIST')
|
|
87
|
+
throw e;
|
|
88
|
+
created = false;
|
|
89
|
+
}
|
|
90
|
+
if (isWin)
|
|
91
|
+
return { created, acl: created ? ((await restrictWindowsAcl(dir)) ? 'restricted' : 'inherited') : 'inherited' };
|
|
92
|
+
// Everything below goes through ONE directory handle opened without following symlinks, so the
|
|
93
|
+
// checks and the chmod apply to the very object that was checked (no check-then-use race).
|
|
94
|
+
let fh;
|
|
95
|
+
try {
|
|
96
|
+
fh = await fs.open(dir, fsc.O_RDONLY | (fsc.O_NOFOLLOW ?? 0) | (fsc.O_DIRECTORY ?? 0));
|
|
97
|
+
}
|
|
98
|
+
catch (e) {
|
|
99
|
+
throw new Error(`${dir} is not a plain directory; refusing to use it (${e.code})`);
|
|
100
|
+
}
|
|
101
|
+
try {
|
|
102
|
+
let st = await fh.stat();
|
|
103
|
+
if (!st.isDirectory())
|
|
104
|
+
throw new Error(`${dir} is not a plain directory; refusing to use it`);
|
|
105
|
+
posixOwnerCheck(st, dir);
|
|
106
|
+
if ((st.mode & 0o077) !== 0) {
|
|
107
|
+
await fh.chmod(0o700);
|
|
108
|
+
st = await fh.stat();
|
|
109
|
+
if ((st.mode & 0o077) !== 0)
|
|
110
|
+
throw new Error(`cannot restrict ${dir} to mode 0700`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
finally {
|
|
114
|
+
await fh.close();
|
|
115
|
+
}
|
|
116
|
+
return { created, acl: 'n/a' };
|
|
117
|
+
}
|
|
118
|
+
/** Read-only variant used by clients: never creates or repairs anything. Returns a reason when unsafe. */
|
|
119
|
+
export async function checkSecureDir(dir) {
|
|
120
|
+
try {
|
|
121
|
+
const st = await fs.lstat(dir);
|
|
122
|
+
if (st.isSymbolicLink() || !st.isDirectory())
|
|
123
|
+
return `${dir} is not a plain directory`;
|
|
124
|
+
if (!isWin) {
|
|
125
|
+
if (typeof process.getuid === 'function' && st.uid !== process.getuid())
|
|
126
|
+
return `${dir} is owned by another user`;
|
|
127
|
+
if ((st.mode & 0o077) !== 0)
|
|
128
|
+
return `${dir} is accessible by other users (mode ${(st.mode & 0o777).toString(8)})`;
|
|
129
|
+
}
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
catch (e) {
|
|
133
|
+
return e.code === 'ENOENT' ? 'absent' : e.message;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Read a secret file through a single no-follow handle, verifying type, owner and mode on that same
|
|
138
|
+
* handle (no check-then-use race). Returns the text, 'absent', or { unsafe: reason }.
|
|
139
|
+
*/
|
|
140
|
+
export async function readSecureFile(file) {
|
|
141
|
+
let fh;
|
|
142
|
+
try {
|
|
143
|
+
fh = await fs.open(file, fsc.O_RDONLY | (fsc.O_NOFOLLOW ?? 0));
|
|
144
|
+
}
|
|
145
|
+
catch (e) {
|
|
146
|
+
const code = e.code;
|
|
147
|
+
return code === 'ENOENT' ? 'absent' : { unsafe: `${file}: ${code}` };
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
const st = await fh.stat();
|
|
151
|
+
if (!st.isFile())
|
|
152
|
+
return { unsafe: `${file} is not a regular file` };
|
|
153
|
+
if (!isWin) {
|
|
154
|
+
if (typeof process.getuid === 'function' && st.uid !== process.getuid())
|
|
155
|
+
return { unsafe: `${file} is owned by another user` };
|
|
156
|
+
if ((st.mode & 0o077) !== 0)
|
|
157
|
+
return { unsafe: `${file} is accessible by other users` };
|
|
158
|
+
}
|
|
159
|
+
return await fh.readFile('utf8');
|
|
160
|
+
}
|
|
161
|
+
finally {
|
|
162
|
+
await fh.close();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/** Write a secret: never follows an existing path, mode 0600 from the first byte. */
|
|
166
|
+
export async function writeSecretFile(file, data) {
|
|
167
|
+
await fs.rm(file, { force: true });
|
|
168
|
+
await fs.writeFile(file, data, { mode: 0o600, flag: 'wx' });
|
|
169
|
+
}
|
|
170
|
+
function run(cmd, args) {
|
|
171
|
+
return new Promise((resolve) => {
|
|
172
|
+
try {
|
|
173
|
+
execFile(cmd, args, { timeout: 10_000, windowsHide: true }, (err) => resolve(!err));
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
resolve(false);
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Grant the current user full control, then drop inherited entries so only that user (plus the
|
|
182
|
+
* ACEs we do not remove explicitly: none) remains. The grant happens first and the inheritance
|
|
183
|
+
* change only if the grant worked, so a failure can never lock the user out of their own directory.
|
|
184
|
+
*/
|
|
185
|
+
async function restrictWindowsAcl(dir) {
|
|
186
|
+
const name = process.env.USERNAME || os.userInfo().username;
|
|
187
|
+
const account = process.env.USERDOMAIN ? `${process.env.USERDOMAIN}\\${name}` : name;
|
|
188
|
+
if (!(await run('icacls', [dir, '/grant:r', `${account}:(OI)(CI)F`])))
|
|
189
|
+
return false;
|
|
190
|
+
return run('icacls', [dir, '/inheritance:r']);
|
|
191
|
+
}
|
|
192
|
+
//# sourceMappingURL=paths.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paths.js","sourceRoot":"","sources":["../../../src/daemon/paths.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACvC,OAAO,EAAE,SAAS,IAAI,GAAG,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACtD,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAClC,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACtD,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAuC9C,MAAM,KAAK,GAAG,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC;AAE3C,MAAM,UAAU,aAAa,CAAC,IAAY;IACxC,OAAO,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AAC3C,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,oBAAoB,GAAG,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,KA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IAAI,EAAE,CAAC,CAAC;AAC9D,CAAC;AAED,SAAS,GAAG,CAAC,GAAW,EAAE,IAAc;IACtC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,IAAI,CAAC;YACH,QAAQ,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,IAAI,EAAE,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QACtF,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,CAAC,KAAK,CAAC,CAAC;QACjB,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;GAIG;AACH,KAAK,UAAU,kBAAkB,CAAC,GAAW;IAC3C,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC;IAC5D,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,UAAU,KAAK,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IACrF,IAAI,CAAC,CAAC,MAAM,GAAG,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,OAAO,YAAY,CAAC,CAAC,CAAC;QAAE,OAAO,KAAK,CAAC;IACpF,OAAO,GAAG,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,gBAAgB,CAAC,CAAC,CAAC;AAChD,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/** Bumped on ANY incompatible change to messages, op semantics or the engine contract. */
|
|
2
|
+
export declare const PROTO = 1;
|
|
3
|
+
export declare const VERSION: string;
|
|
4
|
+
export declare const MAX_REQUEST_CHARS: number;
|
|
5
|
+
export declare const MAX_ENV_CHARS: number;
|
|
6
|
+
/** Responses carry task logs (each already capped by the engine); the client allows a lot more. */
|
|
7
|
+
export declare const MAX_MESSAGE_CHARS: number;
|
|
8
|
+
export declare const AUTH_TIMEOUT_MS = 5000;
|
|
9
|
+
export declare const MAX_CONNECTIONS = 32;
|
|
10
|
+
export declare const MAX_UNAUTHENTICATED = 8;
|
|
11
|
+
/**
|
|
12
|
+
* The complete, closed set of operations. Nothing in a request is ever evaluated, spawned or
|
|
13
|
+
* used as a path/command: `run` names tasks that must already exist in buildrush.json.
|
|
14
|
+
*/
|
|
15
|
+
export declare const OPS: readonly ['ping', 'status', 'run', 'analyze', 'cacheStats', 'stop'];
|
|
16
|
+
export type Op = (typeof OPS)[number];
|
|
17
|
+
/** Served even when versions differ, so a client can always ask an old daemon to exit. */
|
|
18
|
+
export declare const VERSION_AGNOSTIC_OPS: readonly Op[];
|
|
19
|
+
export interface Request {
|
|
20
|
+
proto: number;
|
|
21
|
+
/** Client's BuildRush version. */
|
|
22
|
+
client: string;
|
|
23
|
+
token: string;
|
|
24
|
+
op: Op;
|
|
25
|
+
args?: Record<string, unknown>;
|
|
26
|
+
}
|
|
27
|
+
export type Message = {
|
|
28
|
+
type: 'queued';
|
|
29
|
+
ahead: number;
|
|
30
|
+
} | {
|
|
31
|
+
type: 'begin';
|
|
32
|
+
project: string;
|
|
33
|
+
tasks: number;
|
|
34
|
+
} | {
|
|
35
|
+
type: 'task';
|
|
36
|
+
report: unknown;
|
|
37
|
+
} | {
|
|
38
|
+
type: 'result';
|
|
39
|
+
data: unknown;
|
|
40
|
+
} | {
|
|
41
|
+
type: 'error';
|
|
42
|
+
code: ErrorCode;
|
|
43
|
+
message: string;
|
|
44
|
+
proto?: number;
|
|
45
|
+
version?: string;
|
|
46
|
+
};
|
|
47
|
+
export type ErrorCode = 'bad_request' | 'version_mismatch' | 'stale_daemon' | 'wrong_root' | 'failed' | 'shutting_down';
|
|
48
|
+
/** Constant-time token check (hashing first makes the compared buffers equal-length). */
|
|
49
|
+
export declare function tokenMatches(presented: unknown, expected: string): boolean;
|
|
50
|
+
/** Splits a utf8 stream into lines, refusing to buffer more than `max` characters per line. */
|
|
51
|
+
export declare class LineReader {
|
|
52
|
+
private readonly max;
|
|
53
|
+
private readonly onLine;
|
|
54
|
+
private readonly onOverflow;
|
|
55
|
+
private buf;
|
|
56
|
+
private dead;
|
|
57
|
+
constructor(max: number, onLine: (line: string) => void, onOverflow: () => void);
|
|
58
|
+
push(chunk: string): void;
|
|
59
|
+
private overflow;
|
|
60
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { createHash, timingSafeEqual } from 'node:crypto';
|
|
3
|
+
/** Bumped on ANY incompatible change to messages, op semantics or the engine contract. */
|
|
4
|
+
export const PROTO = 1;
|
|
5
|
+
export const VERSION = createRequire(import.meta.url)('../../../package.json').version;
|
|
6
|
+
export const MAX_REQUEST_CHARS = 1024 * 1024;
|
|
7
|
+
export const MAX_ENV_CHARS = 256 * 1024;
|
|
8
|
+
/** Responses carry task logs (each already capped by the engine); the client allows a lot more. */
|
|
9
|
+
export const MAX_MESSAGE_CHARS = 128 * 1024 * 1024;
|
|
10
|
+
export const AUTH_TIMEOUT_MS = 5000;
|
|
11
|
+
export const MAX_CONNECTIONS = 32;
|
|
12
|
+
export const MAX_UNAUTHENTICATED = 8;
|
|
13
|
+
/**
|
|
14
|
+
* The complete, closed set of operations. Nothing in a request is ever evaluated, spawned or
|
|
15
|
+
* used as a path/command: `run` names tasks that must already exist in buildrush.json.
|
|
16
|
+
*/
|
|
17
|
+
export const OPS = ['ping', 'status', 'run', 'analyze', 'cacheStats', 'stop'];
|
|
18
|
+
/** Served even when versions differ, so a client can always ask an old daemon to exit. */
|
|
19
|
+
export const VERSION_AGNOSTIC_OPS = ['ping', 'status', 'stop'];
|
|
20
|
+
/** Constant-time token check (hashing first makes the compared buffers equal-length). */
|
|
21
|
+
export function tokenMatches(presented, expected) {
|
|
22
|
+
if (typeof presented !== 'string')
|
|
23
|
+
return false;
|
|
24
|
+
const a = createHash('sha256').update(presented).digest();
|
|
25
|
+
const b = createHash('sha256').update(expected).digest();
|
|
26
|
+
return timingSafeEqual(a, b);
|
|
27
|
+
}
|
|
28
|
+
/** Splits a utf8 stream into lines, refusing to buffer more than `max` characters per line. */
|
|
29
|
+
export class LineReader {
|
|
30
|
+
max;
|
|
31
|
+
onLine;
|
|
32
|
+
onOverflow;
|
|
33
|
+
buf = '';
|
|
34
|
+
dead = false;
|
|
35
|
+
constructor(max, onLine, onOverflow) {
|
|
36
|
+
this.max = max;
|
|
37
|
+
this.onLine = onLine;
|
|
38
|
+
this.onOverflow = onOverflow;
|
|
39
|
+
}
|
|
40
|
+
push(chunk) {
|
|
41
|
+
if (this.dead)
|
|
42
|
+
return;
|
|
43
|
+
this.buf += chunk;
|
|
44
|
+
let i;
|
|
45
|
+
while (!this.dead && (i = this.buf.indexOf('\n')) !== -1) {
|
|
46
|
+
const line = this.buf.slice(0, i);
|
|
47
|
+
this.buf = this.buf.slice(i + 1);
|
|
48
|
+
if (line.length > this.max)
|
|
49
|
+
return this.overflow();
|
|
50
|
+
if (line.length > 0)
|
|
51
|
+
this.onLine(line);
|
|
52
|
+
}
|
|
53
|
+
if (!this.dead && this.buf.length > this.max)
|
|
54
|
+
this.overflow();
|
|
55
|
+
}
|
|
56
|
+
overflow() {
|
|
57
|
+
this.dead = true;
|
|
58
|
+
this.buf = '';
|
|
59
|
+
this.onOverflow();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=protocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.js","sourceRoot":"","sources":["../../../src/daemon/protocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE1D,0FAA0F;AAC1F,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,CAAC;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IAAI,GAAG,KAAK,CAAC;IACrB,YACmB,GAAW,EACX,MAA8B,EAC9B,UAAsB;mBAFtB,GAAG;sBACH,MAAM;0BACN,UAAU;IAC1B,CAAC;IAEJ,IAAI,CAAC,KAAa;QAChB,IAAI,IAAI,CAAC,IAAI;YAAE,OAAO;QACtB,IAAI,CAAC,GAAG,IAAI,KAAK,CAAC;QAClB,IAAI,CAAS,CAAC;QACd,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;YACzD,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;YAClC,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;YACjC,IAAI,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG;gBAAE,OAAO,IAAI,CAAC,QAAQ,EAAE,CAAC;YACnD,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC;gBAAE,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACzC,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG;YAAE,IAAI,CAAC,QAAQ,EAAE,CAAC;IAChE,CAAC;IAEO,QAAQ;QACd,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,GAAG,GAAG,EAAE,CAAC;QACd,IAAI,CAAC,UAAU,EAAE,CAAC;IACpB,CAAC;CACF"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { DaemonDirs, DaemonMeta } from './paths.js';
|
|
2
|
+
export declare class AlreadyRunningError extends Error {
|
|
3
|
+
readonly meta: DaemonMeta;
|
|
4
|
+
constructor(meta: DaemonMeta);
|
|
5
|
+
}
|
|
6
|
+
export interface ServerOptions {
|
|
7
|
+
root: string;
|
|
8
|
+
env?: NodeJS.ProcessEnv;
|
|
9
|
+
/** Exit after this long with no connection and no queued/running work. 0 disables. */
|
|
10
|
+
idleMs: number;
|
|
11
|
+
/** Test hooks: pretend to be a different build. */
|
|
12
|
+
versionOverride?: string;
|
|
13
|
+
protoOverride?: number;
|
|
14
|
+
/** How long a connection may take to deliver its first (authenticating) line. */
|
|
15
|
+
authTimeoutMs?: number;
|
|
16
|
+
/** Test hook: extra files whose change retires the daemon (like package.json does). */
|
|
17
|
+
extraWatch?: string[];
|
|
18
|
+
}
|
|
19
|
+
export declare class DaemonServer {
|
|
20
|
+
private readonly opts;
|
|
21
|
+
private readonly server;
|
|
22
|
+
readonly dirs: DaemonDirs;
|
|
23
|
+
readonly token: string;
|
|
24
|
+
readonly endpoint: string;
|
|
25
|
+
readonly closed: Promise<void>;
|
|
26
|
+
/** Warm, re-validated-on-every-use state (file index, dependency graph). */
|
|
27
|
+
private readonly warm;
|
|
28
|
+
private readonly conns;
|
|
29
|
+
private readonly runs;
|
|
30
|
+
private unauth;
|
|
31
|
+
private pending;
|
|
32
|
+
private queue;
|
|
33
|
+
private idleTimer;
|
|
34
|
+
private stopping;
|
|
35
|
+
private resolveClosed;
|
|
36
|
+
private runCount;
|
|
37
|
+
private readonly startedAt;
|
|
38
|
+
private readonly proto;
|
|
39
|
+
private readonly version;
|
|
40
|
+
private baseSig;
|
|
41
|
+
private staleCheckedAt;
|
|
42
|
+
private logBytes;
|
|
43
|
+
private constructor();
|
|
44
|
+
static start(opts: ServerOptions): Promise<DaemonServer>;
|
|
45
|
+
/**
|
|
46
|
+
* Identity of everything this daemon was loaded from: the Node binary, package.json, the newest
|
|
47
|
+
* mtime and file count across dist/src (so a partial rebuild also counts), plus test-injected files.
|
|
48
|
+
*/
|
|
49
|
+
private installSig;
|
|
50
|
+
private touch;
|
|
51
|
+
shutdown(reason: string): Promise<void>;
|
|
52
|
+
private log;
|
|
53
|
+
private onConnection;
|
|
54
|
+
private authenticate;
|
|
55
|
+
private send;
|
|
56
|
+
private fail;
|
|
57
|
+
private dispatch;
|
|
58
|
+
private status;
|
|
59
|
+
private isStale;
|
|
60
|
+
/** One project operation at a time: they share the warm index, the cache and the working tree. */
|
|
61
|
+
private enqueue;
|
|
62
|
+
private projectOp;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Windows environment names are case-insensitive (`Path` is `PATH`) but a plain object is not.
|
|
66
|
+
* Reads resolve through a case-folded index; enumeration still yields the names as the client spelled them.
|
|
67
|
+
*/
|
|
68
|
+
export declare function caseInsensitiveEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv;
|