trusted-user-agent 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +232 -2
- package/dist/acp/agent.js +719 -0
- package/dist/acp/agents/connection.js +298 -0
- package/dist/acp/agents/manager.js +363 -0
- package/dist/acp/agents/registry.js +201 -0
- package/dist/acp/agents/relay-session.js +371 -0
- package/dist/acp/agents/session-index.js +102 -0
- package/dist/acp/backend.js +2 -0
- package/dist/acp/config-options.js +26 -0
- package/dist/acp/mock-backend.js +277 -0
- package/dist/acp/sdk-backend.js +495 -0
- package/dist/browser/mcp-acp-client.js +81 -0
- package/dist/browser/provider.js +1049 -0
- package/dist/browser/schemas.js +221 -0
- package/dist/capability-registry.js +92 -0
- package/dist/client-heartbeat.js +118 -0
- package/dist/compose/schemas.js +90 -0
- package/dist/contacts/schemas.js +26 -0
- package/dist/installer/cli.js +70 -0
- package/dist/installer/common.js +93 -0
- package/dist/installer/index.js +366 -0
- package/dist/installer/mcp-clients.js +555 -0
- package/dist/installer/mcp.js +166 -0
- package/dist/installer/platforms.js +277 -0
- package/dist/logger.js +54 -0
- package/dist/mail/schemas.js +98 -0
- package/dist/mutation/schemas.js +31 -0
- package/dist/native-host/broker-ipc.js +344 -0
- package/dist/native-host/framing.js +76 -0
- package/dist/native-host/main.js +545 -0
- package/dist/native-host/mcp-server.js +333 -0
- package/dist/native-host/mcp-stdio-bridge.js +184 -0
- package/dist/native-host/mcp-stdio-main.js +13 -0
- package/dist/native-host/relay.js +84 -0
- package/dist/native-host/skills-startup.js +167 -0
- package/dist/native-host/transport.js +248 -0
- package/dist/permission-store.js +207 -0
- package/dist/repl/fs-sandbox.js +187 -0
- package/dist/repl/provider.js +252 -0
- package/dist/repl/runtime.js +436 -0
- package/dist/repl/types.js +10 -0
- package/dist/repl/worker.js +731 -0
- package/dist/skills/extension.js +99 -0
- package/dist/skills/grants-file.js +145 -0
- package/dist/skills/isolated-runner.js +259 -0
- package/dist/skills/mock-session-factory.js +49 -0
- package/dist/skills/package-roots.js +25 -0
- package/dist/skills/pi-bridge.js +8 -0
- package/dist/skills/pi-integration.js +110 -0
- package/dist/skills/planner.js +128 -0
- package/dist/skills/registry.js +104 -0
- package/dist/skills/skills-protocol.js +253 -0
- package/dist/skills/skills-service.js +195 -0
- package/dist/tool-bridge.js +8 -0
- package/dist/workspace.js +70 -0
- package/extensions/pi-browser-tools.ts +65 -0
- package/extensions/pi-browser.ts +60 -0
- package/extensions/pi-skills.ts +14 -0
- package/native/pi-agent-host +2 -0
- package/native/pi-browser-host +5 -0
- package/native/pi-browser-host.cmd +3 -0
- package/node_modules/@pi-browser/protocol/.bundled-by-trusted-user-agent +0 -0
- package/node_modules/@pi-browser/protocol/dist/acp.d.ts +10 -0
- package/node_modules/@pi-browser/protocol/dist/acp.js +9 -0
- package/node_modules/@pi-browser/protocol/dist/agents.d.ts +51 -0
- package/node_modules/@pi-browser/protocol/dist/agents.js +44 -0
- package/node_modules/@pi-browser/protocol/dist/browser-tools.d.ts +33 -0
- package/node_modules/@pi-browser/protocol/dist/browser-tools.js +478 -0
- package/node_modules/@pi-browser/protocol/dist/callers.d.ts +123 -0
- package/node_modules/@pi-browser/protocol/dist/callers.js +58 -0
- package/node_modules/@pi-browser/protocol/dist/compose-tools.d.ts +30 -0
- package/node_modules/@pi-browser/protocol/dist/compose-tools.js +175 -0
- package/node_modules/@pi-browser/protocol/dist/contacts-tools.d.ts +23 -0
- package/node_modules/@pi-browser/protocol/dist/contacts-tools.js +63 -0
- package/node_modules/@pi-browser/protocol/dist/control-tools.d.ts +32 -0
- package/node_modules/@pi-browser/protocol/dist/control-tools.js +201 -0
- package/node_modules/@pi-browser/protocol/dist/errors.d.ts +63 -0
- package/node_modules/@pi-browser/protocol/dist/errors.js +109 -0
- package/node_modules/@pi-browser/protocol/dist/index.d.ts +17 -0
- package/node_modules/@pi-browser/protocol/dist/index.js +17 -0
- package/node_modules/@pi-browser/protocol/dist/integration.d.ts +414 -0
- package/node_modules/@pi-browser/protocol/dist/integration.js +270 -0
- package/node_modules/@pi-browser/protocol/dist/jsonrpc.d.ts +33 -0
- package/node_modules/@pi-browser/protocol/dist/jsonrpc.js +31 -0
- package/node_modules/@pi-browser/protocol/dist/mail-mutation-tools.d.ts +27 -0
- package/node_modules/@pi-browser/protocol/dist/mail-mutation-tools.js +88 -0
- package/node_modules/@pi-browser/protocol/dist/mail-tools.d.ts +133 -0
- package/node_modules/@pi-browser/protocol/dist/mail-tools.js +245 -0
- package/node_modules/@pi-browser/protocol/dist/permission.d.ts +113 -0
- package/node_modules/@pi-browser/protocol/dist/permission.js +257 -0
- package/node_modules/@pi-browser/protocol/dist/skills.d.ts +304 -0
- package/node_modules/@pi-browser/protocol/dist/skills.js +473 -0
- package/node_modules/@pi-browser/protocol/dist/tool-capabilities.d.ts +18 -0
- package/node_modules/@pi-browser/protocol/dist/tool-capabilities.js +52 -0
- package/node_modules/@pi-browser/protocol/dist/transport.d.ts +17 -0
- package/node_modules/@pi-browser/protocol/dist/transport.js +17 -0
- package/node_modules/@pi-browser/protocol/package.json +14 -0
- package/package.json +69 -4
- package/skills/browser-walk/SKILL.md +98 -0
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Broker/relay private IPC (THUNDERBIRD-PLAN.md §26–27).
|
|
3
|
+
*
|
|
4
|
+
* One host process per app is spawned by Native Messaging. The FIRST process
|
|
5
|
+
* to start becomes the broker: it owns the ACP agent + Pi sessions and
|
|
6
|
+
* listens on a private OS IPC endpoint (POSIX: ~/.pi/run/agent-broker.sock —
|
|
7
|
+
* 0700 dir, 0600 socket; Windows: a per-user named pipe, see ipcEndpoint). Later
|
|
8
|
+
* processes detect the running broker via its state file (pid + token),
|
|
9
|
+
* connect, authenticate with the token, and run in relay mode (byte
|
|
10
|
+
* passthrough: app stdin → socket, socket → app stdout).
|
|
11
|
+
*
|
|
12
|
+
* Security: same-user only (socket in a 0700 dir under $HOME), per-broker
|
|
13
|
+
* random token, no localhost TCP. Native Messaging remains the externally
|
|
14
|
+
* visible Mozilla security boundary.
|
|
15
|
+
*
|
|
16
|
+
* Windows: Node's net API serves named pipes with the same stream semantics,
|
|
17
|
+
* so only the endpoint differs. Pipes carry no file mode; their default DACL
|
|
18
|
+
* grants other accounts READ only, which cannot send the handshake — the
|
|
19
|
+
* token (in the 0600 state file under the user's profile, ACL'd to the user)
|
|
20
|
+
* stays the same-user gate. A pipe vanishes with its owner, so there is no
|
|
21
|
+
* stale endpoint to unlink.
|
|
22
|
+
*/
|
|
23
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
24
|
+
import { chmodSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
25
|
+
import net from "node:net";
|
|
26
|
+
import os from "node:os";
|
|
27
|
+
import { Duplex } from "node:stream";
|
|
28
|
+
import path from "node:path";
|
|
29
|
+
import { PI_BROKER } from "@pi-browser/protocol";
|
|
30
|
+
import { encodeFrame, FrameDecoder } from "./framing.js";
|
|
31
|
+
/**
|
|
32
|
+
* The listening endpoint for `file` in run dir `dir`.
|
|
33
|
+
*
|
|
34
|
+
* POSIX: the socket file itself. Windows: a named pipe whose name is derived
|
|
35
|
+
* from the run dir (case-folded — Windows paths are case-insensitive), so it
|
|
36
|
+
* is per-user (the run dir lives in the user's profile) and every
|
|
37
|
+
* PI_BROWSER_BROKER_DIR override gets its own pipe, exactly like a socket
|
|
38
|
+
* file in that dir would.
|
|
39
|
+
*/
|
|
40
|
+
export function ipcEndpoint(dir, file, platform = process.platform) {
|
|
41
|
+
if (platform !== "win32")
|
|
42
|
+
return path.join(dir, file);
|
|
43
|
+
const key = path.win32.resolve(dir).toLowerCase();
|
|
44
|
+
const hash = createHash("sha256").update(key).digest("hex").slice(0, 16);
|
|
45
|
+
return String.raw `\\.\pipe\pi-agent-` + `${hash}-${file.replace(/\.sock$/, "")}`;
|
|
46
|
+
}
|
|
47
|
+
/** True for a Windows named-pipe endpoint (no file to chmod or unlink). */
|
|
48
|
+
export function isNamedPipe(endpoint) {
|
|
49
|
+
return /^\\\\[.?]\\pipe\\/i.test(endpoint);
|
|
50
|
+
}
|
|
51
|
+
/** Paths for the broker IPC (env override for tests). */
|
|
52
|
+
export function brokerPaths(homeDir = os.homedir()) {
|
|
53
|
+
const dir = process.env.PI_BROWSER_BROKER_DIR || path.join(homeDir, ".pi", PI_BROKER.runDir);
|
|
54
|
+
return {
|
|
55
|
+
dir,
|
|
56
|
+
socket: ipcEndpoint(dir, PI_BROKER.socketFile),
|
|
57
|
+
state: path.join(dir, PI_BROKER.stateFile),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/** True when the pid is alive (EPERM = alive but owned by another user). */
|
|
61
|
+
export function isPidAlive(pid) {
|
|
62
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
63
|
+
return false;
|
|
64
|
+
try {
|
|
65
|
+
process.kill(pid, 0);
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
return err.code === "EPERM";
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export function readBrokerState(statePath) {
|
|
73
|
+
try {
|
|
74
|
+
const raw = JSON.parse(readFileSync(statePath, "utf8"));
|
|
75
|
+
if (typeof raw.pid !== "number" || typeof raw.token !== "string" || typeof raw.socket !== "string") {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
return raw;
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Relay → broker duplex channel: readable = broker→relay bytes, writable =
|
|
86
|
+
* relay→broker bytes. The broker's framing dispatcher re-frames both
|
|
87
|
+
* directions, so this channel is raw bytes.
|
|
88
|
+
*/
|
|
89
|
+
class RelayChannel extends Duplex {
|
|
90
|
+
socket;
|
|
91
|
+
constructor(socket) {
|
|
92
|
+
super();
|
|
93
|
+
this.socket = socket;
|
|
94
|
+
}
|
|
95
|
+
_read() {
|
|
96
|
+
// Flow is push-driven from the socket's "data" events.
|
|
97
|
+
}
|
|
98
|
+
_write(chunk, _enc, cb) {
|
|
99
|
+
this.socket.write(chunk, (err) => cb(err ?? undefined));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** Reclaim only sockets whose broker and listener have both gone away. */
|
|
103
|
+
async function reclaimBrokerSocket(paths) {
|
|
104
|
+
if (existsSync(paths.socket)) {
|
|
105
|
+
const state = readBrokerState(paths.state);
|
|
106
|
+
if (state && isPidAlive(state.pid)) {
|
|
107
|
+
throw new Error(`another broker is already running (pid ${state.pid})`);
|
|
108
|
+
}
|
|
109
|
+
// A listener can exist before its state file is published. Never unlink
|
|
110
|
+
// a live socket during that startup window.
|
|
111
|
+
const probe = new net.Socket();
|
|
112
|
+
let listening = false;
|
|
113
|
+
try {
|
|
114
|
+
await connectWithTimeout(probe, paths.socket, 500);
|
|
115
|
+
listening = true;
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
// No listener remains at the stale socket.
|
|
119
|
+
}
|
|
120
|
+
finally {
|
|
121
|
+
probe.destroy();
|
|
122
|
+
}
|
|
123
|
+
if (listening)
|
|
124
|
+
throw new Error("another broker is starting");
|
|
125
|
+
if (!isNamedPipe(paths.socket))
|
|
126
|
+
rmSync(paths.socket, { force: true });
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Start the broker socket server. Creates the 0700 run dir, reclaims a
|
|
131
|
+
* stale socket when the previous broker is gone, writes the 0600 state
|
|
132
|
+
* file (pid + token), and listens. Throws if another live broker owns the
|
|
133
|
+
* socket (the caller should then attach as a relay instead).
|
|
134
|
+
*/
|
|
135
|
+
export async function startBrokerServer(opts) {
|
|
136
|
+
const paths = brokerPaths();
|
|
137
|
+
mkdirSync(paths.dir, { recursive: true, mode: 0o700 });
|
|
138
|
+
try {
|
|
139
|
+
chmodSync(paths.dir, 0o700); // mkdir mode is subject to umask
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
/* best-effort */
|
|
143
|
+
}
|
|
144
|
+
await reclaimBrokerSocket(paths);
|
|
145
|
+
const token = randomBytes(24).toString("hex");
|
|
146
|
+
const server = net.createServer((socket) => handleConnection(socket));
|
|
147
|
+
await new Promise((resolve, reject) => {
|
|
148
|
+
const onError = (err) => {
|
|
149
|
+
server.removeListener("error", onError);
|
|
150
|
+
reject(err);
|
|
151
|
+
};
|
|
152
|
+
server.once("error", onError);
|
|
153
|
+
server.listen(paths.socket, () => {
|
|
154
|
+
server.removeListener("error", onError);
|
|
155
|
+
resolve();
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
if (!isNamedPipe(paths.socket)) {
|
|
159
|
+
try {
|
|
160
|
+
chmodSync(paths.socket, 0o600);
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
/* best-effort */
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
const state = {
|
|
167
|
+
pid: process.pid,
|
|
168
|
+
token,
|
|
169
|
+
socket: paths.socket,
|
|
170
|
+
startedAt: Date.now(),
|
|
171
|
+
version: PI_BROKER.version,
|
|
172
|
+
};
|
|
173
|
+
writeFileSync(paths.state, JSON.stringify(state), { mode: 0o600 });
|
|
174
|
+
function handleConnection(socket) {
|
|
175
|
+
const chan = new RelayChannel(socket);
|
|
176
|
+
const decoder = new FrameDecoder();
|
|
177
|
+
let handshakeDone = false;
|
|
178
|
+
socket.on("data", (chunk) => {
|
|
179
|
+
if (handshakeDone) {
|
|
180
|
+
chan.push(chunk);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
try {
|
|
184
|
+
decoder.push(chunk);
|
|
185
|
+
for (let frame = decoder.read(); frame !== null; frame = decoder.read()) {
|
|
186
|
+
if (handshakeDone) {
|
|
187
|
+
chan.push(frame);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
handshakeDone = true;
|
|
191
|
+
let msg;
|
|
192
|
+
try {
|
|
193
|
+
msg = JSON.parse(frame.toString("utf8"));
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
msg = undefined;
|
|
197
|
+
}
|
|
198
|
+
if (msg?.type === PI_BROKER.handshake && typeof msg.token === "string" && msg.token === token) {
|
|
199
|
+
socket.write(encodeFrame({ type: PI_BROKER.handshakeAck, version: PI_BROKER.version }));
|
|
200
|
+
// Hand the pre-ack trailing bytes to the channel (re-framed there).
|
|
201
|
+
const rest = decoder.drain();
|
|
202
|
+
if (rest.length > 0)
|
|
203
|
+
chan.push(rest);
|
|
204
|
+
opts.log.info("broker: relay attached");
|
|
205
|
+
opts.onClient(chan);
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
opts.log.warn("broker: rejected relay connection (handshake/token mismatch)");
|
|
209
|
+
socket.destroy();
|
|
210
|
+
chan.destroy();
|
|
211
|
+
}
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
catch (err) {
|
|
216
|
+
opts.log.warn("broker: framing error on relay connection", err);
|
|
217
|
+
socket.destroy();
|
|
218
|
+
chan.destroy();
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
socket.on("end", () => {
|
|
222
|
+
if (!handshakeDone) {
|
|
223
|
+
socket.destroy();
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
chan.push(null);
|
|
227
|
+
// End the broker's writable side too: a half-open socket would keep
|
|
228
|
+
// the server from closing and leak the client slot.
|
|
229
|
+
socket.end();
|
|
230
|
+
});
|
|
231
|
+
socket.on("error", (err) => {
|
|
232
|
+
opts.log.debug("broker relay socket error", err);
|
|
233
|
+
chan.destroy(err);
|
|
234
|
+
});
|
|
235
|
+
chan.on("error", () => socket.destroy());
|
|
236
|
+
}
|
|
237
|
+
return {
|
|
238
|
+
socketPath: paths.socket,
|
|
239
|
+
close() {
|
|
240
|
+
if (!isNamedPipe(paths.socket))
|
|
241
|
+
rmSync(paths.socket, { force: true });
|
|
242
|
+
rmSync(paths.state, { force: true });
|
|
243
|
+
return new Promise((resolve) => server.close(() => resolve()));
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Try to attach to a running broker. Returns the attachment on success, or
|
|
249
|
+
* null when no live broker exists (the caller should become the broker
|
|
250
|
+
* instead).
|
|
251
|
+
*/
|
|
252
|
+
export async function tryRelayAttach(log, timeoutMs = 2_000) {
|
|
253
|
+
const paths = brokerPaths();
|
|
254
|
+
const state = readBrokerState(paths.state);
|
|
255
|
+
if (!state || !isPidAlive(state.pid))
|
|
256
|
+
return null;
|
|
257
|
+
const socket = new net.Socket();
|
|
258
|
+
socket.unref();
|
|
259
|
+
try {
|
|
260
|
+
await connectWithTimeout(socket, paths.socket, timeoutMs);
|
|
261
|
+
socket.write(encodeFrame({ type: PI_BROKER.handshake, token: state.token, version: PI_BROKER.version }));
|
|
262
|
+
const { remainder } = await readHandshakeAck(socket, timeoutMs);
|
|
263
|
+
log.debug("relay: attached to broker");
|
|
264
|
+
return { channel: socket, initial: remainder };
|
|
265
|
+
}
|
|
266
|
+
catch (err) {
|
|
267
|
+
log.debug("relay: broker attach failed", err);
|
|
268
|
+
socket.destroy();
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Retry tryRelayAttach until it succeeds or `totalMs` elapses — for the
|
|
274
|
+
* broker-election loser, whose winner may not have published its state
|
|
275
|
+
* file yet (listen succeeds first, the state write follows).
|
|
276
|
+
*/
|
|
277
|
+
export async function waitForRelayAttach(log, totalMs, stepMs = 50) {
|
|
278
|
+
const deadline = Date.now() + totalMs;
|
|
279
|
+
for (;;) {
|
|
280
|
+
const attachment = await tryRelayAttach(log, Math.min(1_000, Math.max(100, deadline - Date.now())));
|
|
281
|
+
if (attachment || Date.now() >= deadline)
|
|
282
|
+
return attachment;
|
|
283
|
+
await new Promise((r) => setTimeout(r, stepMs));
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/** Wait for the broker's handshake ack frame; returns its remainder bytes. */
|
|
287
|
+
async function readHandshakeAck(socket, timeoutMs) {
|
|
288
|
+
const decoder = new FrameDecoder();
|
|
289
|
+
await new Promise((resolve, reject) => {
|
|
290
|
+
let settled = false;
|
|
291
|
+
const timer = setTimeout(() => {
|
|
292
|
+
if (settled)
|
|
293
|
+
return;
|
|
294
|
+
settled = true;
|
|
295
|
+
socket.removeListener("data", onData);
|
|
296
|
+
socket.destroy();
|
|
297
|
+
reject(new Error("broker handshake ack timed out"));
|
|
298
|
+
}, timeoutMs);
|
|
299
|
+
function onData(chunk) {
|
|
300
|
+
decoder.push(chunk);
|
|
301
|
+
for (let frame = decoder.read(); frame !== null; frame = decoder.read()) {
|
|
302
|
+
let msg;
|
|
303
|
+
try {
|
|
304
|
+
msg = JSON.parse(frame.toString("utf8"));
|
|
305
|
+
}
|
|
306
|
+
catch {
|
|
307
|
+
msg = undefined;
|
|
308
|
+
}
|
|
309
|
+
if (settled)
|
|
310
|
+
continue;
|
|
311
|
+
settled = true;
|
|
312
|
+
clearTimeout(timer);
|
|
313
|
+
socket.removeListener("data", onData);
|
|
314
|
+
if (msg?.type !== PI_BROKER.handshakeAck) {
|
|
315
|
+
socket.destroy();
|
|
316
|
+
reject(new Error("broker handshake ack mismatch"));
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
resolve();
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
socket.on("data", onData);
|
|
324
|
+
});
|
|
325
|
+
return { remainder: decoder.drain() };
|
|
326
|
+
}
|
|
327
|
+
function connectWithTimeout(socket, path, timeoutMs) {
|
|
328
|
+
return new Promise((resolve, reject) => {
|
|
329
|
+
const timer = setTimeout(() => {
|
|
330
|
+
socket.destroy();
|
|
331
|
+
reject(new Error("broker connect timed out"));
|
|
332
|
+
}, timeoutMs);
|
|
333
|
+
socket.once("connect", () => {
|
|
334
|
+
clearTimeout(timer);
|
|
335
|
+
resolve();
|
|
336
|
+
});
|
|
337
|
+
socket.once("error", (err) => {
|
|
338
|
+
clearTimeout(timer);
|
|
339
|
+
reject(err);
|
|
340
|
+
});
|
|
341
|
+
socket.connect(path);
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
//# sourceMappingURL=broker-ipc.js.map
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Firefox Native Messaging framing (PRODUCT.md §16).
|
|
3
|
+
*
|
|
4
|
+
* Frame: [4-byte little-endian payload length][UTF-8 JSON payload].
|
|
5
|
+
* The reader must tolerate partial reads and multiple frames per chunk:
|
|
6
|
+
* never assume "one data event == one message".
|
|
7
|
+
*/
|
|
8
|
+
export const FIREFOX_MAX_FRAME_BYTES = 100 * 1024 * 1024; // 100 MB (screenshots are large)
|
|
9
|
+
export class FramingError extends Error {
|
|
10
|
+
reason;
|
|
11
|
+
constructor(reason, message) {
|
|
12
|
+
super(message ?? reason);
|
|
13
|
+
this.reason = reason;
|
|
14
|
+
this.name = "FramingError";
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Incremental frame decoder. Feed raw chunks; pull out complete frames.
|
|
19
|
+
*/
|
|
20
|
+
export class FrameDecoder {
|
|
21
|
+
buf = Buffer.alloc(0);
|
|
22
|
+
get bufferedBytes() {
|
|
23
|
+
return this.buf.length;
|
|
24
|
+
}
|
|
25
|
+
/** Feed a raw chunk from stdin. */
|
|
26
|
+
push(chunk) {
|
|
27
|
+
this.buf = this.buf.length === 0 ? chunk : Buffer.concat([this.buf, chunk]);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Try to read the next complete frame. Returns the payload, or null when
|
|
31
|
+
* more bytes are needed. Throws FramingError on protocol violations.
|
|
32
|
+
*/
|
|
33
|
+
read() {
|
|
34
|
+
// Need at least the 4-byte length header.
|
|
35
|
+
if (this.buf.length < 4)
|
|
36
|
+
return null;
|
|
37
|
+
const length = this.buf.readUInt32LE(0);
|
|
38
|
+
if (length > FIREFOX_MAX_FRAME_BYTES) {
|
|
39
|
+
throw new FramingError("frame-too-large", `frame length ${length} exceeds limit`);
|
|
40
|
+
}
|
|
41
|
+
if (this.buf.length < 4 + length)
|
|
42
|
+
return null;
|
|
43
|
+
const payload = this.buf.subarray(4, 4 + length);
|
|
44
|
+
this.buf = this.buf.subarray(4 + length);
|
|
45
|
+
return Buffer.from(payload);
|
|
46
|
+
}
|
|
47
|
+
/** Read as many complete frames as are currently buffered. */
|
|
48
|
+
readAll() {
|
|
49
|
+
const frames = [];
|
|
50
|
+
for (;;) {
|
|
51
|
+
const frame = this.read();
|
|
52
|
+
if (frame === null)
|
|
53
|
+
break;
|
|
54
|
+
frames.push(frame);
|
|
55
|
+
}
|
|
56
|
+
return frames;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Return and clear any incomplete trailing bytes. Used once a pre-protocol
|
|
60
|
+
* handshake (broker relay attach) has consumed the first frame: the
|
|
61
|
+
* remainder must flow onward, not stay stranded in this decoder.
|
|
62
|
+
*/
|
|
63
|
+
drain() {
|
|
64
|
+
const out = this.buf;
|
|
65
|
+
this.buf = Buffer.alloc(0);
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** Encode a message as a single Firefox frame. */
|
|
70
|
+
export function encodeFrame(message) {
|
|
71
|
+
const payload = Buffer.from(typeof message === "string" ? message : JSON.stringify(message), "utf8");
|
|
72
|
+
const header = Buffer.alloc(4);
|
|
73
|
+
header.writeUInt32LE(payload.length, 0);
|
|
74
|
+
return Buffer.concat([header, payload]);
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=framing.js.map
|