toolroll 0.9.106 → 0.9.107
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 +18 -0
- package/README.md +1 -1
- package/dist/cli.js +18 -11
- package/dist/contracts/cli.js +1 -1
- package/dist/demo-sign-in.js +1 -55
- package/dist/easy-start-args.js +4 -2
- package/dist/easy-start.js +78 -24
- package/dist/first-run.js +4 -1
- package/dist/git-floor.js +9 -2
- package/dist/operate.js +28 -9
- package/dist/sandbox-runtime.js +1 -1
- package/dist/sign-in-page.js +197 -0
- package/dist/tui.js +48 -48
- package/dist/welcome-facts.js +5 -2
- package/package.json +1 -1
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { spawn as spawnChild } from "node:child_process";
|
|
2
|
+
import { chmodSync, lstatSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { basename, dirname, join } from "node:path";
|
|
4
|
+
import { SIGN_IN_LINK_MS } from "./server/session.js";
|
|
5
|
+
export const NO_SAFE_OPENER = "this computer can't open a browser for Toolroll safely. To use the browser, run `toolroll password set` and sign in with that password";
|
|
6
|
+
export function platformOpener(platform = process.platform) {
|
|
7
|
+
return platform === "darwin" ? "open" : platform === "linux" ? "xdg-open" : null;
|
|
8
|
+
}
|
|
9
|
+
const PAGE_FOLDER = /^sign-in-[A-Za-z0-9]{6}$/;
|
|
10
|
+
const PAGE_FILE = "open.html";
|
|
11
|
+
export function writeSignInPage(parent, link, now = Date.now()) {
|
|
12
|
+
sweepSignInPages(parent, now);
|
|
13
|
+
const dir = mkdtempSync(join(parent, "sign-in-"));
|
|
14
|
+
const file = join(dir, PAGE_FILE);
|
|
15
|
+
try {
|
|
16
|
+
chmodSync(dir, 0o700);
|
|
17
|
+
const attribute = link.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<");
|
|
18
|
+
writeFileSync(file, `<!doctype html><meta charset="utf-8"><meta name="referrer" content="no-referrer">`
|
|
19
|
+
+ `<meta http-equiv="refresh" content="0;url=${attribute}"><title>Toolroll</title>`
|
|
20
|
+
+ `<script>location.replace(${JSON.stringify(link).replace(/</g, "\\u003c")})</script><p>Opening Toolroll…</p>\n`, { mode: 0o600 });
|
|
21
|
+
chmodSync(file, 0o600);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
rmSync(dir, { recursive: true, force: true });
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
return file;
|
|
28
|
+
}
|
|
29
|
+
export function removeSignInPage(file) {
|
|
30
|
+
const dir = dirname(file);
|
|
31
|
+
if (basename(file) !== PAGE_FILE || !PAGE_FOLDER.test(basename(dir)))
|
|
32
|
+
throw Error(`${file} isn't a sign-in page`);
|
|
33
|
+
let info;
|
|
34
|
+
try {
|
|
35
|
+
info = lstatSync(dir);
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
if (error.code === "ENOENT")
|
|
39
|
+
return;
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
if (!info.isDirectory())
|
|
43
|
+
throw Error(`${dir} isn't a sign-in page's folder`);
|
|
44
|
+
rmSync(dir, { recursive: true, force: true });
|
|
45
|
+
}
|
|
46
|
+
function sweepSignInPages(parent, now) {
|
|
47
|
+
let names;
|
|
48
|
+
try {
|
|
49
|
+
names = readdirSync(parent);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const uid = process.getuid?.() ?? -1;
|
|
55
|
+
for (const name of names) {
|
|
56
|
+
if (!PAGE_FOLDER.test(name))
|
|
57
|
+
continue;
|
|
58
|
+
try {
|
|
59
|
+
const info = lstatSync(join(parent, name));
|
|
60
|
+
if (!info.isDirectory() || (uid >= 0 && info.uid !== uid) || now - info.mtimeMs < SIGN_IN_LINK_MS)
|
|
61
|
+
continue;
|
|
62
|
+
removeSignInPage(join(parent, name, PAGE_FILE));
|
|
63
|
+
}
|
|
64
|
+
catch { }
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export function openSignInPage(file, spawn = spawnChild, platform = process.platform, settleMs = 3_000) {
|
|
68
|
+
const command = platformOpener(platform);
|
|
69
|
+
if (command === null)
|
|
70
|
+
return Promise.resolve(false);
|
|
71
|
+
return new Promise(resolve => {
|
|
72
|
+
let settled = false;
|
|
73
|
+
let timer;
|
|
74
|
+
const end = (opened) => {
|
|
75
|
+
if (settled)
|
|
76
|
+
return;
|
|
77
|
+
settled = true;
|
|
78
|
+
if (timer !== undefined)
|
|
79
|
+
clearTimeout(timer);
|
|
80
|
+
resolve(opened);
|
|
81
|
+
};
|
|
82
|
+
let child;
|
|
83
|
+
try {
|
|
84
|
+
child = spawn(command, [file], { detached: true, stdio: "ignore" });
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
end(false);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
child.on("error", () => end(false));
|
|
91
|
+
child.once("exit", code => end(code === 0));
|
|
92
|
+
timer = setTimeout(() => end(true), settleMs);
|
|
93
|
+
child.unref();
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
export function ledgerSpent(store, outcome, account) {
|
|
97
|
+
const sinceRow = store.handle.prepare("SELECT coalesce(max(id), 0) AS id FROM action_ledger").get();
|
|
98
|
+
const since = Number(sinceRow?.["id"] ?? 0);
|
|
99
|
+
const spent = store.handle.prepare("SELECT actor FROM action_ledger WHERE id > ? AND source = 'sign-in' AND action = 'signed in' AND outcome = ?");
|
|
100
|
+
return () => spent.all(since, outcome).some(row => account === null || row["actor"] === account);
|
|
101
|
+
}
|
|
102
|
+
const held = new Set();
|
|
103
|
+
let exitHooked = false;
|
|
104
|
+
function holdUntilExit(finish) {
|
|
105
|
+
held.add(finish);
|
|
106
|
+
if (exitHooked)
|
|
107
|
+
return;
|
|
108
|
+
exitHooked = true;
|
|
109
|
+
process.once("exit", () => { for (const one of [...held])
|
|
110
|
+
one(); });
|
|
111
|
+
}
|
|
112
|
+
const REMOVE_RETRIES = 3;
|
|
113
|
+
const REMOVE_RETRY_MS = 250;
|
|
114
|
+
export function removeSignInPageWhenSpent(file, spent, options = {}) {
|
|
115
|
+
let done = false;
|
|
116
|
+
let resolveGone = () => { };
|
|
117
|
+
const gone = new Promise(resolve => { resolveGone = resolve; });
|
|
118
|
+
const removeOnce = options.remove ?? removeSignInPage;
|
|
119
|
+
const remove = (left) => {
|
|
120
|
+
try {
|
|
121
|
+
removeOnce(file);
|
|
122
|
+
}
|
|
123
|
+
catch {
|
|
124
|
+
if (left > 0) {
|
|
125
|
+
const again = setTimeout(() => remove(left - 1), REMOVE_RETRY_MS);
|
|
126
|
+
again.unref?.();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
const finish = () => {
|
|
131
|
+
if (done)
|
|
132
|
+
return;
|
|
133
|
+
done = true;
|
|
134
|
+
held.delete(finish);
|
|
135
|
+
clearInterval(timer);
|
|
136
|
+
clearTimeout(expiry);
|
|
137
|
+
try {
|
|
138
|
+
remove(REMOVE_RETRIES);
|
|
139
|
+
}
|
|
140
|
+
finally {
|
|
141
|
+
try {
|
|
142
|
+
options.onEnd?.();
|
|
143
|
+
}
|
|
144
|
+
catch { }
|
|
145
|
+
resolveGone();
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
const timer = setInterval(() => { try {
|
|
149
|
+
if (spent())
|
|
150
|
+
finish();
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
finish();
|
|
154
|
+
} }, options.everyMs ?? 500);
|
|
155
|
+
const expiry = setTimeout(finish, Math.max(0, (options.expiresAt ?? Date.now() + SIGN_IN_LINK_MS) - Date.now()));
|
|
156
|
+
timer.unref?.();
|
|
157
|
+
expiry.unref?.();
|
|
158
|
+
holdUntilExit(finish);
|
|
159
|
+
return { stop: finish, gone };
|
|
160
|
+
}
|
|
161
|
+
export async function openSignInLink(link, options) {
|
|
162
|
+
const platform = options.platform ?? process.platform;
|
|
163
|
+
const nothing = { opened: false, stop: () => { }, wait: async () => { } };
|
|
164
|
+
const end = () => { try {
|
|
165
|
+
options.onEnd?.();
|
|
166
|
+
}
|
|
167
|
+
catch { } };
|
|
168
|
+
if (platformOpener(platform) === null) {
|
|
169
|
+
end();
|
|
170
|
+
return nothing;
|
|
171
|
+
}
|
|
172
|
+
let file;
|
|
173
|
+
try {
|
|
174
|
+
file = writeSignInPage(options.parent, link);
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
end();
|
|
178
|
+
return nothing;
|
|
179
|
+
}
|
|
180
|
+
const watch = removeSignInPageWhenSpent(file, options.spent, {
|
|
181
|
+
expiresAt: options.expiresAt,
|
|
182
|
+
...(options.everyMs === undefined ? {} : { everyMs: options.everyMs }),
|
|
183
|
+
...(options.onEnd === undefined ? {} : { onEnd: options.onEnd }),
|
|
184
|
+
});
|
|
185
|
+
if (!await openSignInPage(file, options.opener ?? spawnChild, platform)) {
|
|
186
|
+
watch.stop();
|
|
187
|
+
return nothing;
|
|
188
|
+
}
|
|
189
|
+
const wait = async (ms) => {
|
|
190
|
+
let bound;
|
|
191
|
+
await Promise.race([watch.gone, new Promise(resolve => { bound = setTimeout(resolve, ms); })]);
|
|
192
|
+
if (bound !== undefined)
|
|
193
|
+
clearTimeout(bound);
|
|
194
|
+
watch.stop();
|
|
195
|
+
};
|
|
196
|
+
return { opened: true, stop: watch.stop, wait };
|
|
197
|
+
}
|