@anhlp/alp-daemon 0.0.0-stage → 0.0.1-beta.3
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 +202 -0
- package/NOTICE +7 -0
- package/README.md +10 -2
- package/dist/THIRD-PARTY-NOTICES.txt +431 -0
- package/dist/alp-templates.d.ts +5 -0
- package/dist/alpd.js +42558 -0
- package/dist/build-manifest.json +111 -0
- package/dist/files.d.ts +110 -0
- package/dist/files.js +350 -0
- package/dist/gitignore.d.ts +57 -0
- package/dist/gitignore.js +248 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +8 -0
- package/dist/lock.d.ts +21 -0
- package/dist/lock.js +42 -0
- package/dist/main.d.ts +1 -0
- package/dist/main.js +271 -0
- package/dist/operations.d.ts +23 -0
- package/dist/operations.js +275 -0
- package/dist/rpc-error.d.ts +6 -0
- package/dist/rpc-error.js +11 -0
- package/dist/server.d.ts +48 -0
- package/dist/server.js +1158 -0
- package/dist/store.d.ts +108 -0
- package/dist/store.js +279 -0
- package/package.json +100 -4
- package/src/alp-templates.d.ts +5 -0
- package/src/files.ts +438 -0
- package/src/gitignore.ts +267 -0
- package/src/index.ts +5 -0
- package/src/lock.ts +53 -0
- package/src/main.ts +286 -0
- package/src/operations.ts +300 -0
- package/src/rpc-error.ts +4 -0
- package/src/server.ts +1332 -0
- package/src/store.ts +336 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
// packages/daemon/src/gitignore.ts
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { accessSync, constants, lstatSync, realpathSync, statSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
var GIT_CHECK = Object.freeze({
|
|
6
|
+
/** Git gets this long per request, waiting for a turn included, then is killed. */
|
|
7
|
+
timeoutMs: 2e3,
|
|
8
|
+
/** Git processes alpd runs at once, and requests that may wait for one. */
|
|
9
|
+
maxConcurrent: 4,
|
|
10
|
+
maxQueued: 64,
|
|
11
|
+
maxErrorBytes: 64 * 1024
|
|
12
|
+
});
|
|
13
|
+
var IgnoreUnknown = class extends Error {
|
|
14
|
+
constructor(why) {
|
|
15
|
+
super(`git check-ignore failed: ${why}`);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
function gitEnvironment() {
|
|
19
|
+
const env = {};
|
|
20
|
+
for (const [key, value] of Object.entries(process.env)) if (!/^GIT_/i.test(key)) env[key] = value;
|
|
21
|
+
return { ...env, GIT_OPTIONAL_LOCKS: "0", GIT_TERMINAL_PROMPT: "0", LC_ALL: "C" };
|
|
22
|
+
}
|
|
23
|
+
var executable = (file) => {
|
|
24
|
+
try {
|
|
25
|
+
if (!statSync(file).isFile()) return false;
|
|
26
|
+
if (process.platform !== "win32") accessSync(file, constants.X_OK);
|
|
27
|
+
return true;
|
|
28
|
+
} catch {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
var identity = (file) => {
|
|
33
|
+
const { dev, ino } = statSync(file, { bigint: true });
|
|
34
|
+
return { dev, ino };
|
|
35
|
+
};
|
|
36
|
+
var foldCase = process.platform === "darwin" || process.platform === "win32";
|
|
37
|
+
var comparable = (file) => foldCase ? file.toLowerCase() : file;
|
|
38
|
+
function insideRoot(file, root) {
|
|
39
|
+
let directory;
|
|
40
|
+
try {
|
|
41
|
+
directory = realpathSync.native(file);
|
|
42
|
+
} catch {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
const rootName = comparable(root.path.endsWith(path.sep) && root.path.length > 1 ? root.path.slice(0, -1) : root.path);
|
|
46
|
+
for (; ; ) {
|
|
47
|
+
const name = comparable(directory);
|
|
48
|
+
if (name === rootName || name.startsWith(rootName.endsWith(path.sep) ? rootName : `${rootName}${path.sep}`)) return true;
|
|
49
|
+
let id;
|
|
50
|
+
try {
|
|
51
|
+
id = identity(directory);
|
|
52
|
+
} catch {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
if (root.id.ino !== 0n && id.dev === root.id.dev && id.ino === root.id.ino) return true;
|
|
56
|
+
const parent = path.dirname(directory);
|
|
57
|
+
if (parent === directory) return false;
|
|
58
|
+
directory = parent;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function rootOf(root) {
|
|
62
|
+
try {
|
|
63
|
+
const real = realpathSync.native(root);
|
|
64
|
+
return { path: real, id: identity(real) };
|
|
65
|
+
} catch {
|
|
66
|
+
return void 0;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function resolveGit(name = "git", searchPath = process.env.PATH ?? process.env.Path ?? "", root) {
|
|
70
|
+
const box = root === void 0 ? void 0 : rootOf(root);
|
|
71
|
+
if (root !== void 0 && !box) throw new IgnoreUnknown("the workspace cannot be examined");
|
|
72
|
+
const accept = (file) => {
|
|
73
|
+
if (!executable(file)) return void 0;
|
|
74
|
+
if (!box) return file;
|
|
75
|
+
let real;
|
|
76
|
+
try {
|
|
77
|
+
real = realpathSync.native(file);
|
|
78
|
+
} catch {
|
|
79
|
+
return void 0;
|
|
80
|
+
}
|
|
81
|
+
return insideRoot(path.dirname(real), box) || !executable(real) ? void 0 : real;
|
|
82
|
+
};
|
|
83
|
+
if (path.isAbsolute(name)) return accept(name);
|
|
84
|
+
if (name.includes("/") || name.includes("\\")) return void 0;
|
|
85
|
+
const names = process.platform === "win32" ? [`${name}.exe`, `${name}.com`] : [name];
|
|
86
|
+
for (const directory of searchPath.split(path.delimiter)) {
|
|
87
|
+
if (!directory || !path.isAbsolute(directory)) continue;
|
|
88
|
+
if (box && insideRoot(directory, box)) continue;
|
|
89
|
+
for (const candidate of names) {
|
|
90
|
+
const file = accept(path.join(directory, candidate));
|
|
91
|
+
if (file) return file;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return void 0;
|
|
95
|
+
}
|
|
96
|
+
function hasGitEntry(root) {
|
|
97
|
+
for (let directory = root; ; directory = path.dirname(directory)) {
|
|
98
|
+
try {
|
|
99
|
+
lstatSync(path.join(directory, ".git"));
|
|
100
|
+
return true;
|
|
101
|
+
} catch (error) {
|
|
102
|
+
if (error.code !== "ENOENT") throw new IgnoreUnknown("cannot examine .git");
|
|
103
|
+
}
|
|
104
|
+
if (path.dirname(directory) === directory) return false;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
var running = 0;
|
|
108
|
+
var waiting = [];
|
|
109
|
+
function acquire(deadline) {
|
|
110
|
+
const release = () => {
|
|
111
|
+
running--;
|
|
112
|
+
waiting.shift()?.();
|
|
113
|
+
};
|
|
114
|
+
if (running < GIT_CHECK.maxConcurrent) {
|
|
115
|
+
running++;
|
|
116
|
+
return Promise.resolve(release);
|
|
117
|
+
}
|
|
118
|
+
if (waiting.length >= GIT_CHECK.maxQueued) return Promise.reject(new IgnoreUnknown("too many waiting"));
|
|
119
|
+
return new Promise((resolve, reject) => {
|
|
120
|
+
const turn = () => {
|
|
121
|
+
clearTimeout(timer);
|
|
122
|
+
running++;
|
|
123
|
+
resolve(release);
|
|
124
|
+
};
|
|
125
|
+
const timer = setTimeout(() => {
|
|
126
|
+
const index = waiting.indexOf(turn);
|
|
127
|
+
if (index >= 0) waiting.splice(index, 1);
|
|
128
|
+
reject(new IgnoreUnknown("timed out waiting for a turn"));
|
|
129
|
+
}, Math.max(0, deadline - Date.now()));
|
|
130
|
+
waiting.push(turn);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
var gitChecksRunning = () => running;
|
|
134
|
+
async function gitIgnored(root, paths, { binary = "git", timeoutMs = GIT_CHECK.timeoutMs } = {}) {
|
|
135
|
+
if (!paths.length) return /* @__PURE__ */ new Set();
|
|
136
|
+
const git = resolveGit(binary, void 0, root);
|
|
137
|
+
if (!git) return null;
|
|
138
|
+
const deadline = Date.now() + timeoutMs;
|
|
139
|
+
const release = await acquire(deadline);
|
|
140
|
+
try {
|
|
141
|
+
return await check(git, root, paths, deadline, timeoutMs);
|
|
142
|
+
} finally {
|
|
143
|
+
release();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function check(git, root, paths, deadline, timeoutMs) {
|
|
147
|
+
const input = `${paths.map((entry) => `./${entry}`).join("\0")}\0`;
|
|
148
|
+
const maxOutputBytes = Buffer.byteLength(input) + 1024;
|
|
149
|
+
return new Promise((resolve, reject) => {
|
|
150
|
+
let settled = false;
|
|
151
|
+
let timer;
|
|
152
|
+
let child;
|
|
153
|
+
const group = process.platform !== "win32";
|
|
154
|
+
const kill = () => {
|
|
155
|
+
try {
|
|
156
|
+
if (group && child.pid) process.kill(-child.pid, "SIGKILL");
|
|
157
|
+
else child.kill("SIGKILL");
|
|
158
|
+
} catch {
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
const finish = (outcome) => {
|
|
162
|
+
if (settled) return;
|
|
163
|
+
settled = true;
|
|
164
|
+
if (timer) clearTimeout(timer);
|
|
165
|
+
outcome();
|
|
166
|
+
};
|
|
167
|
+
try {
|
|
168
|
+
child = spawn(git, ["-c", "core.fsmonitor=false", "check-ignore", "--no-index", "--stdin", "-z"], {
|
|
169
|
+
cwd: root,
|
|
170
|
+
env: gitEnvironment(),
|
|
171
|
+
shell: false,
|
|
172
|
+
windowsHide: true,
|
|
173
|
+
detached: group,
|
|
174
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
175
|
+
});
|
|
176
|
+
} catch (error) {
|
|
177
|
+
reject(new IgnoreUnknown(String(error.message)));
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
timer = setTimeout(() => finish(() => {
|
|
181
|
+
kill();
|
|
182
|
+
reject(new IgnoreUnknown("timed out"));
|
|
183
|
+
}), Math.max(1, Math.min(timeoutMs, deadline - Date.now())));
|
|
184
|
+
const out = [];
|
|
185
|
+
let outBytes = 0;
|
|
186
|
+
let err = "";
|
|
187
|
+
child.stdout.on("data", (chunk) => {
|
|
188
|
+
outBytes += chunk.length;
|
|
189
|
+
if (outBytes > maxOutputBytes) finish(() => {
|
|
190
|
+
kill();
|
|
191
|
+
reject(new IgnoreUnknown("more output than input"));
|
|
192
|
+
});
|
|
193
|
+
else out.push(chunk);
|
|
194
|
+
});
|
|
195
|
+
child.stderr.on("data", (chunk) => {
|
|
196
|
+
if (err.length < GIT_CHECK.maxErrorBytes) err += chunk.toString("utf8");
|
|
197
|
+
});
|
|
198
|
+
child.on("error", (error) => finish(() => {
|
|
199
|
+
reject(new IgnoreUnknown(error.message));
|
|
200
|
+
}));
|
|
201
|
+
child.on("close", (code) => finish(() => {
|
|
202
|
+
if (code === 128 && /not a git repository/i.test(err)) {
|
|
203
|
+
try {
|
|
204
|
+
if (hasGitEntry(root)) reject(new IgnoreUnknown("a .git Git cannot use"));
|
|
205
|
+
else resolve(null);
|
|
206
|
+
} catch (error) {
|
|
207
|
+
reject(error);
|
|
208
|
+
}
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (code !== 0 && code !== 1) {
|
|
212
|
+
reject(new IgnoreUnknown(`exit ${code}`));
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if (err.length || code === 1 && outBytes || code === 0 && !outBytes) {
|
|
216
|
+
reject(new IgnoreUnknown("inconsistent exit status or diagnostics"));
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const output = Buffer.concat(out).toString("utf8");
|
|
220
|
+
if (outBytes && !output.endsWith("\0")) {
|
|
221
|
+
reject(new IgnoreUnknown("unterminated answer"));
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const asked = new Set(paths);
|
|
225
|
+
const ignored = /* @__PURE__ */ new Set();
|
|
226
|
+
for (const entry of output.split("\0")) {
|
|
227
|
+
if (!entry) continue;
|
|
228
|
+
const relative = entry.startsWith("./") ? entry.slice(2) : void 0;
|
|
229
|
+
if (relative === void 0 || !asked.has(relative)) {
|
|
230
|
+
reject(new IgnoreUnknown("an answer for a path not asked"));
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
ignored.add(relative);
|
|
234
|
+
}
|
|
235
|
+
resolve(ignored);
|
|
236
|
+
}));
|
|
237
|
+
child.stdin.on("error", () => {
|
|
238
|
+
});
|
|
239
|
+
child.stdin.end(input);
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
export {
|
|
243
|
+
GIT_CHECK,
|
|
244
|
+
IgnoreUnknown,
|
|
245
|
+
gitChecksRunning,
|
|
246
|
+
gitIgnored,
|
|
247
|
+
resolveGit
|
|
248
|
+
};
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/lock.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type LockContents = {
|
|
2
|
+
pid: number;
|
|
3
|
+
startedAt: string;
|
|
4
|
+
bootTime: number;
|
|
5
|
+
uid: number;
|
|
6
|
+
version: string;
|
|
7
|
+
protocolVersion: number;
|
|
8
|
+
socket: string;
|
|
9
|
+
ready: boolean;
|
|
10
|
+
};
|
|
11
|
+
/** Single-instance lock (Paseo acquirePidLock, simplified): exclusive create, stale when its pid or boot is gone. */
|
|
12
|
+
export declare function acquireLock(home: string, contents: Omit<LockContents, 'pid' | 'startedAt' | 'bootTime' | 'uid' | 'ready'>): Promise<LockContents>;
|
|
13
|
+
/**
|
|
14
|
+
* Moves `temporary` over `file`. On Windows the move fails with EPERM, EACCES or EBUSY while
|
|
15
|
+
* another process has `file` open, such as a client reading the lock; it is tried again then.
|
|
16
|
+
*/
|
|
17
|
+
export declare function replaceFile(temporary: string, file: string): Promise<void>;
|
|
18
|
+
export declare function updateLock(home: string, lock: LockContents): Promise<void>;
|
|
19
|
+
/** True while this process still owns the lock; refreshes its mtime as a heartbeat. */
|
|
20
|
+
export declare function heartbeat(home: string): Promise<boolean>;
|
|
21
|
+
export declare function releaseLock(home: string): Promise<void>;
|
package/dist/lock.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// packages/daemon/src/lock.ts
|
|
2
|
+
import { unlink, utimes, writeFile, rename } from "node:fs/promises";
|
|
3
|
+
import { acquireDaemonLock, daemonPaths, readLock } from "@anhlp/alp-client/index";
|
|
4
|
+
async function acquireLock(home, contents) {
|
|
5
|
+
return acquireDaemonLock(home, contents);
|
|
6
|
+
}
|
|
7
|
+
async function replaceFile(temporary, file) {
|
|
8
|
+
for (let attempt = 0; ; attempt++) {
|
|
9
|
+
try {
|
|
10
|
+
return await rename(temporary, file);
|
|
11
|
+
} catch (error) {
|
|
12
|
+
if (process.platform !== "win32" || attempt >= 20 || !["EPERM", "EACCES", "EBUSY"].includes(error?.code)) throw error;
|
|
13
|
+
await new Promise((resolve) => setTimeout(resolve, 10 + attempt * 10));
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
async function updateLock(home, lock) {
|
|
18
|
+
const file = daemonPaths(home).lock;
|
|
19
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
20
|
+
await writeFile(temporary, JSON.stringify(lock), { mode: 384 });
|
|
21
|
+
await replaceFile(temporary, file);
|
|
22
|
+
}
|
|
23
|
+
async function heartbeat(home) {
|
|
24
|
+
const current = await readLock(home);
|
|
25
|
+
if (current?.pid !== process.pid) return false;
|
|
26
|
+
const now = /* @__PURE__ */ new Date();
|
|
27
|
+
await utimes(daemonPaths(home).lock, now, now).catch(() => {
|
|
28
|
+
});
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
async function releaseLock(home) {
|
|
32
|
+
const current = await readLock(home);
|
|
33
|
+
if (current?.pid === process.pid) await unlink(daemonPaths(home).lock).catch(() => {
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
export {
|
|
37
|
+
acquireLock,
|
|
38
|
+
heartbeat,
|
|
39
|
+
releaseLock,
|
|
40
|
+
replaceFile,
|
|
41
|
+
updateLock
|
|
42
|
+
};
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|