@leemour/max-cli 0.6.0 → 0.7.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/README.md +8 -0
- package/dist/cache/index.d.ts +1 -0
- package/dist/cache/index.d.ts.map +1 -1
- package/dist/cache/index.js +4 -4
- package/dist/cache/index.js.map +1 -1
- package/dist/client.d.ts +178 -9
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +764 -34
- package/dist/client.js.map +1 -1
- package/dist/commands/account.d.ts.map +1 -1
- package/dist/commands/account.js +71 -3
- package/dist/commands/account.js.map +1 -1
- package/dist/commands/chats.d.ts.map +1 -1
- package/dist/commands/chats.js +159 -0
- package/dist/commands/chats.js.map +1 -1
- package/dist/commands/complete.d.ts +10 -0
- package/dist/commands/complete.d.ts.map +1 -0
- package/dist/commands/complete.js +89 -0
- package/dist/commands/complete.js.map +1 -0
- package/dist/commands/config.d.ts.map +1 -1
- package/dist/commands/config.js +4 -2
- package/dist/commands/config.js.map +1 -1
- package/dist/commands/contacts.d.ts +3 -0
- package/dist/commands/contacts.d.ts.map +1 -1
- package/dist/commands/contacts.js +85 -0
- package/dist/commands/contacts.js.map +1 -1
- package/dist/commands/context.d.ts +8 -1
- package/dist/commands/context.d.ts.map +1 -1
- package/dist/commands/context.js +26 -6
- package/dist/commands/context.js.map +1 -1
- package/dist/commands/folders.d.ts +4 -0
- package/dist/commands/folders.d.ts.map +1 -0
- package/dist/commands/folders.js +76 -0
- package/dist/commands/folders.js.map +1 -0
- package/dist/commands/inbox.js +2 -2
- package/dist/commands/inbox.js.map +1 -1
- package/dist/commands/mcp.d.ts +3 -0
- package/dist/commands/mcp.d.ts.map +1 -0
- package/dist/commands/mcp.js +17 -0
- package/dist/commands/mcp.js.map +1 -0
- package/dist/commands/messages.d.ts.map +1 -1
- package/dist/commands/messages.js +84 -2
- package/dist/commands/messages.js.map +1 -1
- package/dist/commands/reactions.d.ts.map +1 -1
- package/dist/commands/reactions.js +17 -0
- package/dist/commands/reactions.js.map +1 -1
- package/dist/commands/serve.d.ts +10 -0
- package/dist/commands/serve.d.ts.map +1 -0
- package/dist/commands/serve.js +50 -0
- package/dist/commands/serve.js.map +1 -0
- package/dist/commands/session.d.ts.map +1 -1
- package/dist/commands/session.js +4 -1
- package/dist/commands/session.js.map +1 -1
- package/dist/commands/update.d.ts +7 -0
- package/dist/commands/update.d.ts.map +1 -0
- package/dist/commands/update.js +53 -0
- package/dist/commands/update.js.map +1 -0
- package/dist/commands/watch.d.ts +9 -0
- package/dist/commands/watch.d.ts.map +1 -0
- package/dist/commands/watch.js +68 -0
- package/dist/commands/watch.js.map +1 -0
- package/dist/config.d.ts +16 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +27 -4
- package/dist/config.js.map +1 -1
- package/dist/domain/map.d.ts +8 -1
- package/dist/domain/map.d.ts.map +1 -1
- package/dist/domain/map.js +42 -1
- package/dist/domain/map.js.map +1 -1
- package/dist/domain/models.d.ts +47 -0
- package/dist/domain/models.d.ts.map +1 -1
- package/dist/domain/models.js.map +1 -1
- package/dist/generated/client.generated.d.ts +26 -0
- package/dist/generated/client.generated.d.ts.map +1 -1
- package/dist/generated/client.generated.js +26 -0
- package/dist/generated/client.generated.js.map +1 -1
- package/dist/generated/opcodes.generated.d.ts +24 -5
- package/dist/generated/opcodes.generated.d.ts.map +1 -1
- package/dist/generated/opcodes.generated.js +24 -5
- package/dist/generated/opcodes.generated.js.map +1 -1
- package/dist/generated/operations.generated.d.ts +163 -6
- package/dist/generated/operations.generated.d.ts.map +1 -1
- package/dist/generated/operations.generated.js +27 -4
- package/dist/generated/operations.generated.js.map +1 -1
- package/dist/mcp/confirm.d.ts +24 -0
- package/dist/mcp/confirm.d.ts.map +1 -0
- package/dist/mcp/confirm.js +53 -0
- package/dist/mcp/confirm.js.map +1 -0
- package/dist/mcp/instructions.d.ts +10 -0
- package/dist/mcp/instructions.d.ts.map +1 -0
- package/dist/mcp/instructions.js +25 -0
- package/dist/mcp/instructions.js.map +1 -0
- package/dist/mcp/server.d.ts +23 -0
- package/dist/mcp/server.d.ts.map +1 -0
- package/dist/mcp/server.js +38 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/mcp/session.d.ts +28 -0
- package/dist/mcp/session.d.ts.map +1 -0
- package/dist/mcp/session.js +91 -0
- package/dist/mcp/session.js.map +1 -0
- package/dist/mcp/tools.d.ts +8 -0
- package/dist/mcp/tools.d.ts.map +1 -0
- package/dist/mcp/tools.js +274 -0
- package/dist/mcp/tools.js.map +1 -0
- package/dist/program.d.ts.map +1 -1
- package/dist/program.js +17 -0
- package/dist/program.js.map +1 -1
- package/dist/protocol/connection.d.ts +10 -8
- package/dist/protocol/connection.d.ts.map +1 -1
- package/dist/protocol/connection.js +64 -7
- package/dist/protocol/connection.js.map +1 -1
- package/dist/protocol/frame.d.ts +2 -1
- package/dist/protocol/frame.d.ts.map +1 -1
- package/dist/protocol/frame.js.map +1 -1
- package/dist/rendering/messages.d.ts.map +1 -1
- package/dist/rendering/messages.js +17 -9
- package/dist/rendering/messages.js.map +1 -1
- package/dist/sends/guard.d.ts +3 -2
- package/dist/sends/guard.d.ts.map +1 -1
- package/dist/sends/guard.js +14 -10
- package/dist/sends/guard.js.map +1 -1
- package/dist/sends/journal.d.ts +15 -2
- package/dist/sends/journal.d.ts.map +1 -1
- package/dist/sends/journal.js.map +1 -1
- package/dist/server/lines.d.ts +9 -0
- package/dist/server/lines.d.ts.map +1 -0
- package/dist/server/lines.js +25 -0
- package/dist/server/lines.js.map +1 -0
- package/dist/server/server-connection.d.ts +35 -0
- package/dist/server/server-connection.d.ts.map +1 -0
- package/dist/server/server-connection.js +195 -0
- package/dist/server/server-connection.js.map +1 -0
- package/dist/server/server.d.ts +62 -0
- package/dist/server/server.d.ts.map +1 -0
- package/dist/server/server.js +297 -0
- package/dist/server/server.js.map +1 -0
- package/dist/server/start.d.ts +12 -0
- package/dist/server/start.d.ts.map +1 -0
- package/dist/server/start.js +41 -0
- package/dist/server/start.js.map +1 -0
- package/dist/server/subscribe.d.ts +9 -0
- package/dist/server/subscribe.d.ts.map +1 -0
- package/dist/server/subscribe.js +26 -0
- package/dist/server/subscribe.js.map +1 -0
- package/dist/session/browser.d.ts +1 -1
- package/dist/session/browser.d.ts.map +1 -1
- package/dist/session/browser.js +86 -59
- package/dist/session/browser.js.map +1 -1
- package/dist/session/store.d.ts +2 -0
- package/dist/session/store.d.ts.map +1 -1
- package/dist/session/store.js +4 -0
- package/dist/session/store.js.map +1 -1
- package/dist/spec/define.d.ts.map +1 -1
- package/dist/spec/define.js +14 -1
- package/dist/spec/define.js.map +1 -1
- package/dist/spec/index.d.ts.map +1 -1
- package/dist/spec/index.js +28 -5
- package/dist/spec/index.js.map +1 -1
- package/dist/spec/operations/account.d.ts +20 -0
- package/dist/spec/operations/account.d.ts.map +1 -0
- package/dist/spec/operations/account.js +62 -0
- package/dist/spec/operations/account.js.map +1 -0
- package/dist/spec/operations/chats.d.ts +54 -0
- package/dist/spec/operations/chats.d.ts.map +1 -1
- package/dist/spec/operations/chats.js +121 -0
- package/dist/spec/operations/chats.js.map +1 -1
- package/dist/spec/operations/contacts.d.ts +19 -1
- package/dist/spec/operations/contacts.d.ts.map +1 -1
- package/dist/spec/operations/contacts.js +49 -6
- package/dist/spec/operations/contacts.js.map +1 -1
- package/dist/spec/operations/folders.d.ts +24 -0
- package/dist/spec/operations/folders.d.ts.map +1 -0
- package/dist/spec/operations/folders.js +64 -0
- package/dist/spec/operations/folders.js.map +1 -0
- package/dist/spec/operations/messages.d.ts +44 -6
- package/dist/spec/operations/messages.d.ts.map +1 -1
- package/dist/spec/operations/messages.js +86 -17
- package/dist/spec/operations/messages.js.map +1 -1
- package/dist/spec/operations/session.d.ts +7 -0
- package/dist/spec/operations/session.d.ts.map +1 -1
- package/dist/spec/operations/session.js +17 -0
- package/dist/spec/operations/session.js.map +1 -1
- package/dist/spec/operations/uploads.d.ts +18 -0
- package/dist/spec/operations/uploads.d.ts.map +1 -0
- package/dist/spec/operations/uploads.js +35 -0
- package/dist/spec/operations/uploads.js.map +1 -0
- package/dist/testing/mock-max.d.ts +12 -2
- package/dist/testing/mock-max.d.ts.map +1 -1
- package/dist/testing/mock-max.js +15 -2
- package/dist/testing/mock-max.js.map +1 -1
- package/dist/testing/sandbox.js +2 -0
- package/dist/testing/sandbox.js.map +1 -1
- package/dist/update.d.ts +36 -0
- package/dist/update.d.ts.map +1 -0
- package/dist/update.js +70 -0
- package/dist/update.js.map +1 -0
- package/dist/upload.d.ts +10 -0
- package/dist/upload.d.ts.map +1 -0
- package/dist/upload.js +61 -0
- package/dist/upload.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +12 -3
- package/skills/max-cli/SKILL.md +10 -1
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { connect } from "node:net";
|
|
2
|
+
import { Opcode } from "../generated/opcodes.generated.js";
|
|
3
|
+
import { Connection, ProtocolError } from "../protocol/connection.js";
|
|
4
|
+
import { asId } from "../protocol/frame.js";
|
|
5
|
+
import { startSession } from "../session/handshake.js";
|
|
6
|
+
import { buildRequest } from "../spec/index.js";
|
|
7
|
+
import { fromLine, lineReader, toLine } from "./lines.js";
|
|
8
|
+
/**
|
|
9
|
+
* **A command's connection that goes through `max serve`** when one is running (`MAX-16`).
|
|
10
|
+
*
|
|
11
|
+
* INIT and LOGIN are answered by the server's own login, so the command logs in without logging
|
|
12
|
+
* in. Reads go through the server's connection. **Anything else — a send, a reaction — makes this
|
|
13
|
+
* open a connection of its own and log in on it**, so it passes the send guards exactly as it does
|
|
14
|
+
* without a server (`NEED-185`). No server, a stale login, or a server that stops answering: the
|
|
15
|
+
* whole command falls back to its own connection before it has sent anything.
|
|
16
|
+
*/
|
|
17
|
+
export class ServerConnection {
|
|
18
|
+
#path;
|
|
19
|
+
#store;
|
|
20
|
+
#timeoutMs;
|
|
21
|
+
#direct;
|
|
22
|
+
#onUnreachable;
|
|
23
|
+
#pending = new Map();
|
|
24
|
+
#socket;
|
|
25
|
+
#id = 0;
|
|
26
|
+
#login;
|
|
27
|
+
/** What the client sent as INIT; a fallback at LOGIN has to send it first, or MAX refuses. */
|
|
28
|
+
#init;
|
|
29
|
+
#own;
|
|
30
|
+
#ownLoggedIn = false;
|
|
31
|
+
constructor({ path, store, timeoutMs = 30_000, direct, onUnreachable, }) {
|
|
32
|
+
this.#onUnreachable = onUnreachable;
|
|
33
|
+
this.#path = path;
|
|
34
|
+
this.#store = store;
|
|
35
|
+
this.#timeoutMs = timeoutMs;
|
|
36
|
+
this.#direct = direct ?? (() => new Connection({ timeoutMs }));
|
|
37
|
+
}
|
|
38
|
+
async open() { }
|
|
39
|
+
async invoke(opcode, payload = {}, watch) {
|
|
40
|
+
if (this.#own)
|
|
41
|
+
return this.#ownRequest(opcode, payload, watch);
|
|
42
|
+
if (opcode === Opcode.SESSION_INIT) {
|
|
43
|
+
this.#init = payload;
|
|
44
|
+
this.#login = await this.#ask({ login: true }).catch((error) => {
|
|
45
|
+
if (error.code === "ECONNREFUSED" || error.code === "ENOENT")
|
|
46
|
+
this.#onUnreachable?.();
|
|
47
|
+
return undefined;
|
|
48
|
+
});
|
|
49
|
+
if (!this.#login)
|
|
50
|
+
return this.#fallBack(opcode, payload, watch);
|
|
51
|
+
return {};
|
|
52
|
+
}
|
|
53
|
+
if (opcode === Opcode.LOGIN) {
|
|
54
|
+
// A token being tried out (`max session start`) is not the one the server logged in with, and
|
|
55
|
+
// a server still logged in as the account this profile had before is not this profile's.
|
|
56
|
+
if (!this.#login || payload.token !== this.#store.readToken() || !this.#sameAccount(this.#login)) {
|
|
57
|
+
return this.#fallBack(opcode, payload, watch);
|
|
58
|
+
}
|
|
59
|
+
return this.#login;
|
|
60
|
+
}
|
|
61
|
+
if (!READS.has(opcode))
|
|
62
|
+
return this.#ownRequest(opcode, payload, watch);
|
|
63
|
+
watch?.({ phase: "sent", seq: this.#id + 1, opcode, bytes: 0 });
|
|
64
|
+
const answer = await this.#ask({ opcode, payload });
|
|
65
|
+
watch?.({ phase: "received", seq: this.#id, opcode, bytes: 0 });
|
|
66
|
+
return answer;
|
|
67
|
+
}
|
|
68
|
+
#sameAccount(login) {
|
|
69
|
+
const profile = login.profile;
|
|
70
|
+
const viewerId = this.#store.readState().viewerId;
|
|
71
|
+
return viewerId === undefined || asId(profile?.contact?.id) === viewerId;
|
|
72
|
+
}
|
|
73
|
+
async close() {
|
|
74
|
+
for (const { reject, timer } of this.#pending.values()) {
|
|
75
|
+
clearTimeout(timer);
|
|
76
|
+
reject(new Error("the connection was closed before max serve answered"));
|
|
77
|
+
}
|
|
78
|
+
this.#pending.clear();
|
|
79
|
+
this.#socket?.destroy();
|
|
80
|
+
this.#socket = undefined;
|
|
81
|
+
await this.#own?.close();
|
|
82
|
+
}
|
|
83
|
+
/** Before anything was sent: the command simply runs as it would with no server. */
|
|
84
|
+
async #fallBack(opcode, payload, watch) {
|
|
85
|
+
this.#socket?.destroy();
|
|
86
|
+
this.#socket = undefined;
|
|
87
|
+
this.#own = this.#direct();
|
|
88
|
+
await this.#own.open();
|
|
89
|
+
if (opcode === Opcode.LOGIN && this.#init)
|
|
90
|
+
await this.#own.invoke(Opcode.SESSION_INIT, this.#init);
|
|
91
|
+
this.#ownLoggedIn = opcode === Opcode.LOGIN;
|
|
92
|
+
return this.#own.invoke(opcode, payload, watch);
|
|
93
|
+
}
|
|
94
|
+
/** A write mid-command: a connection of its own, logged in, then the request. */
|
|
95
|
+
async #ownRequest(opcode, payload, watch) {
|
|
96
|
+
if (!this.#own) {
|
|
97
|
+
this.#own = this.#direct();
|
|
98
|
+
await this.#own.open();
|
|
99
|
+
}
|
|
100
|
+
const own = this.#own;
|
|
101
|
+
if (!this.#ownLoggedIn && opcode !== Opcode.SESSION_INIT && opcode !== Opcode.LOGIN) {
|
|
102
|
+
const token = this.#store.readToken() ?? "";
|
|
103
|
+
await startSession((operation, request) => own.invoke(operation.opcode, buildRequest(operation, request)), {
|
|
104
|
+
token,
|
|
105
|
+
deviceId: this.#store.readState().deviceId,
|
|
106
|
+
});
|
|
107
|
+
this.#ownLoggedIn = true;
|
|
108
|
+
}
|
|
109
|
+
if (opcode === Opcode.LOGIN)
|
|
110
|
+
this.#ownLoggedIn = true;
|
|
111
|
+
return own.invoke(opcode, payload, watch);
|
|
112
|
+
}
|
|
113
|
+
async #ask(request) {
|
|
114
|
+
const socket = await this.#connected();
|
|
115
|
+
this.#id += 1;
|
|
116
|
+
const id = this.#id;
|
|
117
|
+
const answer = await new Promise((resolve, reject) => {
|
|
118
|
+
const timer = setTimeout(() => {
|
|
119
|
+
this.#pending.delete(id);
|
|
120
|
+
reject(new Error(`max serve did not answer within ${this.#timeoutMs}ms`));
|
|
121
|
+
}, this.#timeoutMs);
|
|
122
|
+
this.#pending.set(id, { resolve, reject, timer });
|
|
123
|
+
socket.write(toLine({ id, ...request }));
|
|
124
|
+
});
|
|
125
|
+
const error = answer.error;
|
|
126
|
+
if (error?.code === "refused") {
|
|
127
|
+
throw new ProtocolError(error.message ?? "MAX refused it", Number(request.opcode), error.payload ?? null);
|
|
128
|
+
}
|
|
129
|
+
if (error)
|
|
130
|
+
throw new Error(error.message ?? "max serve could not do it");
|
|
131
|
+
return (answer.payload ?? {});
|
|
132
|
+
}
|
|
133
|
+
async #connected() {
|
|
134
|
+
if (this.#socket)
|
|
135
|
+
return this.#socket;
|
|
136
|
+
const socket = connect(this.#path);
|
|
137
|
+
await new Promise((resolve, reject) => {
|
|
138
|
+
socket.once("connect", () => resolve());
|
|
139
|
+
socket.once("error", reject);
|
|
140
|
+
});
|
|
141
|
+
socket.on("data", lineReader((line) => {
|
|
142
|
+
const answer = fromLine(line);
|
|
143
|
+
const waiting = this.#pending.get(Number(answer.id));
|
|
144
|
+
if (!waiting)
|
|
145
|
+
return;
|
|
146
|
+
clearTimeout(waiting.timer);
|
|
147
|
+
this.#pending.delete(Number(answer.id));
|
|
148
|
+
waiting.resolve(answer);
|
|
149
|
+
}));
|
|
150
|
+
socket.on("error", () => { });
|
|
151
|
+
socket.on("close", () => {
|
|
152
|
+
for (const { reject, timer } of this.#pending.values()) {
|
|
153
|
+
clearTimeout(timer);
|
|
154
|
+
reject(new Error("max serve went away"));
|
|
155
|
+
}
|
|
156
|
+
this.#pending.clear();
|
|
157
|
+
this.#socket = undefined;
|
|
158
|
+
});
|
|
159
|
+
this.#socket = socket;
|
|
160
|
+
return socket;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* What a command may ask for through the server — the server allows exactly these. **Nothing here
|
|
165
|
+
* changes anything**: sending, reacting, marking read and deleting never come this way, so no
|
|
166
|
+
* guard can be walked around.
|
|
167
|
+
*/
|
|
168
|
+
export const READS = new Set([
|
|
169
|
+
Opcode.CHAT_HISTORY,
|
|
170
|
+
Opcode.CHATS_LIST,
|
|
171
|
+
Opcode.CONTACT_INFO,
|
|
172
|
+
Opcode.FILE_DOWNLOAD,
|
|
173
|
+
Opcode.VIDEO_PLAY,
|
|
174
|
+
Opcode.MSG_GET_REACTIONS,
|
|
175
|
+
]);
|
|
176
|
+
/** Asks a running server to stop. Resolves whether or not one was there. */
|
|
177
|
+
export const stopServer = (path) => new Promise((resolve) => {
|
|
178
|
+
const socket = connect(path);
|
|
179
|
+
const timer = setTimeout(() => {
|
|
180
|
+
socket.destroy();
|
|
181
|
+
resolve(false);
|
|
182
|
+
}, 2000);
|
|
183
|
+
socket.once("connect", () => socket.write(toLine({ id: 1, stop: true })));
|
|
184
|
+
// Nobody reads the answer, and a socket nobody reads never sees its end, so never closes.
|
|
185
|
+
socket.resume();
|
|
186
|
+
socket.once("error", () => {
|
|
187
|
+
clearTimeout(timer);
|
|
188
|
+
resolve(false);
|
|
189
|
+
});
|
|
190
|
+
socket.once("close", () => {
|
|
191
|
+
clearTimeout(timer);
|
|
192
|
+
resolve(true);
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
//# sourceMappingURL=server-connection.js.map
|
|
@@ -0,0 +1 @@
|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zE,0FAA0F;IAC1F,MAAM,CAAC,MAAM,EAAE,CAAA;IACf,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE;QACxB,YAAY,CAAC,KAAK,CAAC,CAAA;QACnB,OAAO,CAAC,KAAK,CAAC,CAAA;IAChB,CAAC,CAAC,CAAA;IACF,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE;QACxB,YAAY,CAAC,KAAK,CAAC,CAAA;QACnB,OAAO,CAAC,IAAI,CAAC,CAAA;IACf,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { CacheStore } from "../cache/store.js";
|
|
2
|
+
import { type MaxClientOptions } from "../client.js";
|
|
3
|
+
import type { MessageHit } from "../domain/models.js";
|
|
4
|
+
import { Connection, type ConnectionOptions } from "../protocol/connection.js";
|
|
5
|
+
import type { SessionStore } from "../session/store.js";
|
|
6
|
+
export type ServerEvent = {
|
|
7
|
+
event: "message";
|
|
8
|
+
message: MessageHit;
|
|
9
|
+
} | {
|
|
10
|
+
event: "status";
|
|
11
|
+
connected: boolean;
|
|
12
|
+
at: string;
|
|
13
|
+
};
|
|
14
|
+
export interface MaxServerOptions {
|
|
15
|
+
store: SessionStore;
|
|
16
|
+
cache?: CacheStore;
|
|
17
|
+
timeoutMs?: number;
|
|
18
|
+
/** One line for a person, on stderr. */
|
|
19
|
+
note: (line: string) => void;
|
|
20
|
+
/** Tests hand in a scripted MAX; the hooks must reach it. */
|
|
21
|
+
connection?: (hooks: Pick<ConnectionOptions, "onEvent" | "onClose">) => Connection;
|
|
22
|
+
pingEveryMs?: number;
|
|
23
|
+
/** The least time between two background logins after the snapshot went stale. */
|
|
24
|
+
refreshEveryMs?: number;
|
|
25
|
+
retryAfterMs?: (attempt: number) => number;
|
|
26
|
+
/**
|
|
27
|
+
* Stop after this long with nobody using it — no request, no `max watch`. Absent, it runs until
|
|
28
|
+
* stopped; a server started by a command gets one, or it would outlive every reason it had.
|
|
29
|
+
*/
|
|
30
|
+
idleMs?: number;
|
|
31
|
+
/** One event per request, for `--trace` and the run log — pings included. */
|
|
32
|
+
events?: MaxClientOptions["events"];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* **One logged-in connection per profile, kept open** (`MAX-16`), and a Unix socket beside the
|
|
36
|
+
* profile's state for local readers.
|
|
37
|
+
*
|
|
38
|
+
* It reopens REQUIREMENTS §3 and §18 on purpose and only here: every other command stays one-shot.
|
|
39
|
+
* What it does on the wire copies web.max.ru — a ping every 30 s, MAX's pings answered, each new
|
|
40
|
+
* message acknowledged (`Connection` with `live`). It never marks anything read and never sends.
|
|
41
|
+
*
|
|
42
|
+
* The socket answers `{"subscribe": true}` with a stream of `ServerEvent` lines and
|
|
43
|
+
* `{"status": true}` with one. For a command reusing the connection (`ServerConnection`) it answers
|
|
44
|
+
* `{"id", "login": true}` with the login it holds, and `{"id", "opcode", "payload"}` by passing the
|
|
45
|
+
* request to MAX — **reads only** (`NEED-185`). Anything that writes goes on the command's own
|
|
46
|
+
* connection, through the send guards, which live in the client and nowhere else.
|
|
47
|
+
*/
|
|
48
|
+
export declare class MaxServer {
|
|
49
|
+
#private;
|
|
50
|
+
/** Settles when the server stops — cleanly, or with the error that stopped it. */
|
|
51
|
+
readonly done: Promise<void>;
|
|
52
|
+
constructor(options: MaxServerOptions);
|
|
53
|
+
get connected(): boolean;
|
|
54
|
+
/** Logs in first: a token that does not work should fail here, before anything listens. */
|
|
55
|
+
start(): Promise<void>;
|
|
56
|
+
stop(error?: Error): Promise<void>;
|
|
57
|
+
}
|
|
58
|
+
/** Present while a server is being started in the background, so two commands do not start two. */
|
|
59
|
+
export declare const startingPath: (store: SessionStore) => string;
|
|
60
|
+
/** Whether something is listening on the socket — a live server, not a leftover file. */
|
|
61
|
+
export declare const answers: (path: string) => Promise<boolean>;
|
|
62
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|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,SAAS;;IAgBpB,kFAAkF;IAClF,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;gBAEhB,OAAO,EAAE,gBAAgB;IAOrC,IAAI,SAAS,IAAI,OAAO,CAEvB;IAED,2FAA2F;IACrF,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAqBtB,IAAI,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC;CAkNzC;AAKD,mGAAmG;AACnG,eAAO,MAAM,YAAY,GAAI,OAAO,YAAY,KAAG,MAA0C,CAAA;AAE7F,yFAAyF;AACzF,eAAO,MAAM,OAAO,GAAI,MAAM,MAAM,KAAG,OAAO,CAAC,OAAO,CAQlD,CAAA"}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { chmodSync, mkdirSync, rmSync } from "node:fs";
|
|
2
|
+
import { connect, createServer } from "node:net";
|
|
3
|
+
import { dirname } from "node:path";
|
|
4
|
+
import { CliError } from "@leemour/cli-core";
|
|
5
|
+
import { MaxClient } from "../client.js";
|
|
6
|
+
import { Connection, ProtocolError } from "../protocol/connection.js";
|
|
7
|
+
import { fromLine, lineReader, toLine } from "./lines.js";
|
|
8
|
+
import { READS } from "./server-connection.js";
|
|
9
|
+
/** The web client's keep-alive interval (web.max.ru bundle, 2026-09-24). A different one is a fingerprint. */
|
|
10
|
+
const PING_EVERY_MS = 30_000;
|
|
11
|
+
/** A login MAX counts; after one, the next background login waits at least this long. */
|
|
12
|
+
const REFRESH_EVERY_MS = 60_000;
|
|
13
|
+
/**
|
|
14
|
+
* **One logged-in connection per profile, kept open** (`MAX-16`), and a Unix socket beside the
|
|
15
|
+
* profile's state for local readers.
|
|
16
|
+
*
|
|
17
|
+
* It reopens REQUIREMENTS §3 and §18 on purpose and only here: every other command stays one-shot.
|
|
18
|
+
* What it does on the wire copies web.max.ru — a ping every 30 s, MAX's pings answered, each new
|
|
19
|
+
* message acknowledged (`Connection` with `live`). It never marks anything read and never sends.
|
|
20
|
+
*
|
|
21
|
+
* The socket answers `{"subscribe": true}` with a stream of `ServerEvent` lines and
|
|
22
|
+
* `{"status": true}` with one. For a command reusing the connection (`ServerConnection`) it answers
|
|
23
|
+
* `{"id", "login": true}` with the login it holds, and `{"id", "opcode", "payload"}` by passing the
|
|
24
|
+
* request to MAX — **reads only** (`NEED-185`). Anything that writes goes on the command's own
|
|
25
|
+
* connection, through the send guards, which live in the client and nowhere else.
|
|
26
|
+
*/
|
|
27
|
+
export class MaxServer {
|
|
28
|
+
#options;
|
|
29
|
+
#subscribers = new Set();
|
|
30
|
+
#listener;
|
|
31
|
+
#client;
|
|
32
|
+
#ping;
|
|
33
|
+
#retry;
|
|
34
|
+
#attempt = 0;
|
|
35
|
+
#stopped = false;
|
|
36
|
+
/** A push touched the chats in a way the login snapshot cannot follow; it is not handed out. */
|
|
37
|
+
#stale = false;
|
|
38
|
+
#refresh;
|
|
39
|
+
#lastRefresh = 0;
|
|
40
|
+
#lastUse = Date.now();
|
|
41
|
+
#idle;
|
|
42
|
+
#finish;
|
|
43
|
+
/** Settles when the server stops — cleanly, or with the error that stopped it. */
|
|
44
|
+
done;
|
|
45
|
+
constructor(options) {
|
|
46
|
+
this.#options = options;
|
|
47
|
+
this.done = new Promise((resolve, reject) => {
|
|
48
|
+
this.#finish = (error) => (error ? reject(error) : resolve());
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
get connected() {
|
|
52
|
+
return this.#client !== undefined;
|
|
53
|
+
}
|
|
54
|
+
/** Logs in first: a token that does not work should fail here, before anything listens. */
|
|
55
|
+
async start() {
|
|
56
|
+
try {
|
|
57
|
+
await this.#connect();
|
|
58
|
+
await this.#listen();
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
rmSync(startingPath(this.#options.store), { force: true });
|
|
62
|
+
}
|
|
63
|
+
const { idleMs } = this.#options;
|
|
64
|
+
if (idleMs !== undefined) {
|
|
65
|
+
this.#idle = setInterval(() => {
|
|
66
|
+
if (this.#subscribers.size === 0 && Date.now() - this.#lastUse >= idleMs) {
|
|
67
|
+
this.#options.note(`nobody has used it for ${Math.round(idleMs / 60_000)} min — stopping`);
|
|
68
|
+
void this.stop();
|
|
69
|
+
}
|
|
70
|
+
}, Math.min(idleMs, 30_000));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async stop(error) {
|
|
74
|
+
if (this.#stopped)
|
|
75
|
+
return;
|
|
76
|
+
this.#stopped = true;
|
|
77
|
+
clearInterval(this.#ping);
|
|
78
|
+
clearTimeout(this.#retry);
|
|
79
|
+
clearTimeout(this.#refresh);
|
|
80
|
+
clearInterval(this.#idle);
|
|
81
|
+
for (const socket of this.#subscribers)
|
|
82
|
+
socket.destroy();
|
|
83
|
+
this.#subscribers.clear();
|
|
84
|
+
await new Promise((resolve) => (this.#listener ? this.#listener.close(() => resolve()) : resolve()));
|
|
85
|
+
rmSync(this.#options.store.socketPath(), { force: true });
|
|
86
|
+
await this.#client?.close();
|
|
87
|
+
this.#client = undefined;
|
|
88
|
+
this.#finish?.(error);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Logs in on a new connection and, once that worked, makes it the server's — closing the one it
|
|
92
|
+
* replaces, if any. Pushes that arrive before the login finishes are held and replayed, not
|
|
93
|
+
* dropped: `Connection` has already acknowledged them, so MAX will not send them again.
|
|
94
|
+
*/
|
|
95
|
+
async #connect() {
|
|
96
|
+
const { store, cache, timeoutMs, events } = this.#options;
|
|
97
|
+
let mine;
|
|
98
|
+
const early = [];
|
|
99
|
+
const connection = (this.#options.connection ??
|
|
100
|
+
((hooks) => new Connection({ ...hooks, live: true, ...(timeoutMs ? { timeoutMs } : {}) })))({
|
|
101
|
+
onEvent: (frame) => {
|
|
102
|
+
if (mine)
|
|
103
|
+
this.#pushed(mine, frame.opcode, frame.payload ?? {});
|
|
104
|
+
else
|
|
105
|
+
early.push([frame.opcode, frame.payload ?? {}]);
|
|
106
|
+
},
|
|
107
|
+
onClose: (error) => {
|
|
108
|
+
if (mine && mine === this.#client)
|
|
109
|
+
this.#lost(error);
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
const client = new MaxClient({
|
|
113
|
+
store,
|
|
114
|
+
connection,
|
|
115
|
+
fullLogin: true,
|
|
116
|
+
warn: this.#options.note,
|
|
117
|
+
...(cache ? { cache } : {}),
|
|
118
|
+
...(events ? { events } : {}),
|
|
119
|
+
});
|
|
120
|
+
try {
|
|
121
|
+
await client.connect();
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
// A socket left open after a refused login keeps the process alive after it said it failed.
|
|
125
|
+
await client.close();
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
const replaced = this.#client;
|
|
129
|
+
mine = client;
|
|
130
|
+
this.#client = client;
|
|
131
|
+
this.#attempt = 0;
|
|
132
|
+
this.#stale = false;
|
|
133
|
+
this.#lastRefresh = Date.now();
|
|
134
|
+
clearInterval(this.#ping);
|
|
135
|
+
this.#ping = setInterval(() => {
|
|
136
|
+
client.live.ping().catch(() => { });
|
|
137
|
+
}, this.#options.pingEveryMs ?? PING_EVERY_MS);
|
|
138
|
+
if (replaced)
|
|
139
|
+
await replaced.close().catch(() => { });
|
|
140
|
+
else
|
|
141
|
+
this.#broadcast({ event: "status", connected: true, at: new Date().toISOString() });
|
|
142
|
+
for (const [opcode, payload] of early)
|
|
143
|
+
this.#pushed(client, opcode, payload);
|
|
144
|
+
}
|
|
145
|
+
#pushed(client, opcode, payload) {
|
|
146
|
+
if (!client.live.patch(opcode, payload))
|
|
147
|
+
this.#goneStale();
|
|
148
|
+
client.live
|
|
149
|
+
.message(opcode, payload)
|
|
150
|
+
.then((message) => message && this.#broadcast({ event: "message", message }))
|
|
151
|
+
.catch((error) => this.#options.note(`a pushed message could not be read: ${error.message}`));
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* The login can no longer be handed out. Rather than wait for a drop that may be hours away, log
|
|
155
|
+
* in again in the background — at most once a minute, since each is a login MAX counts.
|
|
156
|
+
*/
|
|
157
|
+
#goneStale() {
|
|
158
|
+
this.#stale = true;
|
|
159
|
+
if (this.#refresh || this.#stopped)
|
|
160
|
+
return;
|
|
161
|
+
const every = this.#options.refreshEveryMs ?? REFRESH_EVERY_MS;
|
|
162
|
+
const wait = Math.max(0, this.#lastRefresh + every - Date.now());
|
|
163
|
+
this.#refresh = setTimeout(() => {
|
|
164
|
+
this.#connect()
|
|
165
|
+
.catch((error) => this.#options.note(`could not log in again: ${error.message}`))
|
|
166
|
+
.finally(() => {
|
|
167
|
+
this.#refresh = undefined;
|
|
168
|
+
});
|
|
169
|
+
}, wait);
|
|
170
|
+
}
|
|
171
|
+
#lost(error) {
|
|
172
|
+
clearInterval(this.#ping);
|
|
173
|
+
const client = this.#client;
|
|
174
|
+
this.#client = undefined;
|
|
175
|
+
client?.close().catch(() => { });
|
|
176
|
+
if (this.#stopped)
|
|
177
|
+
return;
|
|
178
|
+
this.#broadcast({ event: "status", connected: false, at: new Date().toISOString() });
|
|
179
|
+
this.#reconnectLater(error);
|
|
180
|
+
}
|
|
181
|
+
#reconnectLater(error) {
|
|
182
|
+
const wait = (this.#options.retryAfterMs ?? backoff)(this.#attempt);
|
|
183
|
+
this.#attempt += 1;
|
|
184
|
+
this.#options.note(`${error.message} — connecting again in ${Math.round(wait / 1000)}s`);
|
|
185
|
+
this.#retry = setTimeout(() => {
|
|
186
|
+
this.#connect().catch((failure) => {
|
|
187
|
+
// A token MAX no longer takes will not start working on the next attempt.
|
|
188
|
+
if (failure instanceof CliError && failure.code === "authentication_error")
|
|
189
|
+
this.stop(failure);
|
|
190
|
+
else
|
|
191
|
+
this.#reconnectLater(failure);
|
|
192
|
+
});
|
|
193
|
+
}, wait);
|
|
194
|
+
}
|
|
195
|
+
#broadcast(event) {
|
|
196
|
+
const line = toLine(event);
|
|
197
|
+
for (const socket of this.#subscribers)
|
|
198
|
+
socket.write(line);
|
|
199
|
+
}
|
|
200
|
+
async #listen() {
|
|
201
|
+
const path = this.#options.store.socketPath();
|
|
202
|
+
if (await answers(path)) {
|
|
203
|
+
throw new CliError("validation_error", `a server is already running for profile "${this.#options.store.profile}"`);
|
|
204
|
+
}
|
|
205
|
+
// Nobody answers on it, so it is what a crashed server left behind.
|
|
206
|
+
rmSync(path, { force: true });
|
|
207
|
+
mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
|
|
208
|
+
const listener = createServer((socket) => this.#serve(socket));
|
|
209
|
+
await new Promise((resolve, reject) => {
|
|
210
|
+
listener.once("error", reject);
|
|
211
|
+
listener.listen(path, () => resolve());
|
|
212
|
+
});
|
|
213
|
+
chmodSync(path, 0o600);
|
|
214
|
+
this.#listener = listener;
|
|
215
|
+
}
|
|
216
|
+
#serve(socket) {
|
|
217
|
+
socket.on("error", () => this.#subscribers.delete(socket));
|
|
218
|
+
socket.on("close", () => {
|
|
219
|
+
this.#subscribers.delete(socket);
|
|
220
|
+
this.#lastUse = Date.now();
|
|
221
|
+
});
|
|
222
|
+
socket.on("data", lineReader((line) => {
|
|
223
|
+
this.#request(socket, line).catch(() => socket.destroy());
|
|
224
|
+
}));
|
|
225
|
+
}
|
|
226
|
+
async #request(socket, line) {
|
|
227
|
+
let request;
|
|
228
|
+
try {
|
|
229
|
+
request = fromLine(line);
|
|
230
|
+
}
|
|
231
|
+
catch {
|
|
232
|
+
socket.write(toLine({ error: { code: "bad_request", message: "one JSON object per line" } }));
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const status = { event: "status", connected: this.connected, at: new Date().toISOString() };
|
|
236
|
+
const { id } = request;
|
|
237
|
+
this.#lastUse = Date.now();
|
|
238
|
+
if (request.subscribe === true) {
|
|
239
|
+
this.#subscribers.add(socket);
|
|
240
|
+
socket.write(toLine(status));
|
|
241
|
+
}
|
|
242
|
+
else if (request.status === true) {
|
|
243
|
+
socket.write(toLine(status));
|
|
244
|
+
}
|
|
245
|
+
else if (request.stop === true) {
|
|
246
|
+
// Only the owner can reach this socket (mode 600); `max session end` asks, so a forgotten
|
|
247
|
+
// session is not kept alive by a server still logged in with it.
|
|
248
|
+
socket.end(toLine({ id, stopped: true }));
|
|
249
|
+
await this.stop();
|
|
250
|
+
}
|
|
251
|
+
else if (request.login === true) {
|
|
252
|
+
const client = this.#client;
|
|
253
|
+
socket.write(toLine(client && !this.#stale
|
|
254
|
+
? { id, payload: client.live.snapshot() }
|
|
255
|
+
: { id, error: { code: "unavailable", message: this.#stale ? "the login went stale" : "not connected" } }));
|
|
256
|
+
}
|
|
257
|
+
else if (typeof request.opcode === "number") {
|
|
258
|
+
socket.write(toLine({ id, ...(await this.#forward(request.opcode, request.payload)) }));
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
socket.write(toLine({ id, error: { code: "bad_request", message: "subscribe, status, login or an opcode" } }));
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
async #forward(opcode, payload) {
|
|
265
|
+
const client = this.#client;
|
|
266
|
+
if (!READS.has(opcode)) {
|
|
267
|
+
return {
|
|
268
|
+
error: { code: "not_allowed", message: `opcode ${opcode} is not a read; send it on your own connection` },
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
if (!client)
|
|
272
|
+
return { error: { code: "unavailable", message: "not connected" } };
|
|
273
|
+
try {
|
|
274
|
+
return { payload: await client.live.forward(opcode, (payload ?? {})) };
|
|
275
|
+
}
|
|
276
|
+
catch (error) {
|
|
277
|
+
if (error instanceof ProtocolError) {
|
|
278
|
+
return { error: { code: "refused", message: error.message, payload: error.payload } };
|
|
279
|
+
}
|
|
280
|
+
return { error: { code: "unavailable", message: error instanceof Error ? error.message : String(error) } };
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
/** 1 s, 2 s, 4 s … a minute at most — a server that hammers MAX after a drop looks like nothing MAX knows. */
|
|
285
|
+
const backoff = (attempt) => Math.min(60_000, 1000 * 2 ** attempt);
|
|
286
|
+
/** Present while a server is being started in the background, so two commands do not start two. */
|
|
287
|
+
export const startingPath = (store) => `${store.socketPath()}.starting`;
|
|
288
|
+
/** Whether something is listening on the socket — a live server, not a leftover file. */
|
|
289
|
+
export const answers = (path) => new Promise((resolve) => {
|
|
290
|
+
const probe = connect(path);
|
|
291
|
+
probe.once("connect", () => {
|
|
292
|
+
probe.destroy();
|
|
293
|
+
resolve(true);
|
|
294
|
+
});
|
|
295
|
+
probe.once("error", () => resolve(false));
|
|
296
|
+
});
|
|
297
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/server/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AACtD,OAAO,EAAE,OAAO,EAAE,YAAY,EAA4B,MAAM,UAAU,CAAA;AAC1E,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAA;AAE5C,OAAO,EAAE,SAAS,EAAyB,MAAM,cAAc,CAAA;AAE/D,OAAO,EAAE,UAAU,EAA0B,aAAa,EAAE,MAAM,2BAA2B,CAAA;AAE7F,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AACzD,OAAO,EAAE,KAAK,EAAE,MAAM,wBAAwB,CAAA;AA2B9C,8GAA8G;AAC9G,MAAM,aAAa,GAAG,MAAM,CAAA;AAE5B,yFAAyF;AACzF,MAAM,gBAAgB,GAAG,MAAM,CAAA;AAE/B;;;;;;;;;;;;;GAaG;AACH,MAAM,OAAO,SAAS;IACX,QAAQ,CAAkB;IAC1B,YAAY,GAAG,IAAI,GAAG,EAAU,CAAA;IACzC,SAAS,CAAoB;IAC7B,OAAO,CAAuB;IAC9B,KAAK,CAA4C;IACjD,MAAM,CAA2C;IACjD,QAAQ,GAAG,CAAC,CAAA;IACZ,QAAQ,G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G;AACnG,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,KAAmB,EAAU,EAAE,CAAC,GAAG,KAAK,CAAC,UAAU,EAAE,WAAW,CAAA;AAE7F,yFAAyF;AACzF,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,IAAY,EAAoB,EAAE,CACxD,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;IACtB,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC3B,KAAK,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,EAAE;QACzB,KAAK,CAAC,OAAO,EAAE,CAAA;QACf,OAAO,CAAC,IAAI,CAAC,CAAA;IACf,CAAC,CAAC,CAAA;IACF,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAA;AAC3C,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { SessionStore } from "../session/store.js";
|
|
2
|
+
/** A server a command started stops after this long unused. */
|
|
3
|
+
export declare const IDLE_MS: number;
|
|
4
|
+
/**
|
|
5
|
+
* Starts `max serve` for this profile in the background, detached, and returns at once — the
|
|
6
|
+
* command that asked goes on with its own connection and the next one finds the server.
|
|
7
|
+
*
|
|
8
|
+
* Its stderr goes to `<state>/profiles/<profile>.serve.log`, mode 600: nobody is watching a
|
|
9
|
+
* terminal for it, and that file is where a login that failed says so.
|
|
10
|
+
*/
|
|
11
|
+
export declare const startInBackground: (store: SessionStore, entry?: string | undefined) => boolean;
|
|
12
|
+
//# sourceMappingURL=start.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"start.d.ts","sourceRoot":"","sources":["../../src/server/start.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAGvD,+DAA+D;AAC/D,eAAO,MAAM,OAAO,QAAc,CAAA;AAKlC;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,GAAI,OAAO,YAAY,EAAE,0BAAuB,KAAG,OAuBhF,CAAA"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { closeSync, mkdirSync, openSync, rmSync, statSync } from "node:fs";
|
|
3
|
+
import { dirname } from "node:path";
|
|
4
|
+
import { startingPath } from "./server.js";
|
|
5
|
+
/** A server a command started stops after this long unused. */
|
|
6
|
+
export const IDLE_MS = 15 * 60_000;
|
|
7
|
+
/** A start that has not listened by now crashed; the next command may try again. */
|
|
8
|
+
const START_GRACE_MS = 30_000;
|
|
9
|
+
/**
|
|
10
|
+
* Starts `max serve` for this profile in the background, detached, and returns at once — the
|
|
11
|
+
* command that asked goes on with its own connection and the next one finds the server.
|
|
12
|
+
*
|
|
13
|
+
* Its stderr goes to `<state>/profiles/<profile>.serve.log`, mode 600: nobody is watching a
|
|
14
|
+
* terminal for it, and that file is where a login that failed says so.
|
|
15
|
+
*/
|
|
16
|
+
export const startInBackground = (store, entry = process.argv[1]) => {
|
|
17
|
+
if (!entry || store.readToken() === undefined)
|
|
18
|
+
return false;
|
|
19
|
+
const lock = startingPath(store);
|
|
20
|
+
mkdirSync(dirname(lock), { recursive: true, mode: 0o700 });
|
|
21
|
+
try {
|
|
22
|
+
closeSync(openSync(lock, "wx", 0o600));
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
const since = statSync(lock, { throwIfNoEntry: false })?.mtimeMs ?? 0;
|
|
26
|
+
if (Date.now() - since <= START_GRACE_MS)
|
|
27
|
+
return false;
|
|
28
|
+
rmSync(lock, { force: true });
|
|
29
|
+
closeSync(openSync(lock, "wx", 0o600));
|
|
30
|
+
}
|
|
31
|
+
const log = openSync(`${store.socketPath().replace(/\.sock$/, "")}.serve.log`, "a", 0o600);
|
|
32
|
+
const child = spawn(process.execPath, [entry, "--no-record", "serve", "--idle", `${IDLE_MS / 60_000}m`], {
|
|
33
|
+
detached: true,
|
|
34
|
+
stdio: ["ignore", "ignore", log],
|
|
35
|
+
env: { ...process.env, MAX_PROFILE: store.profile },
|
|
36
|
+
});
|
|
37
|
+
child.unref();
|
|
38
|
+
closeSync(log);
|
|
39
|
+
return true;
|
|
40
|
+
};
|
|
41
|
+
//# sourceMappingURL=start.js.map
|