glimpse-sdk 0.8.0 → 0.9.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 +24 -17
- package/PROTOCOL.md +536 -0
- package/README.md +40 -117
- package/dist/index.d.ts +174 -0
- package/dist/index.js +78 -0
- package/dist/jsx-runtime.d.ts +16 -0
- package/dist/jsx-runtime.js +7 -0
- package/dist/reconciler.d.ts +30 -0
- package/dist/reconciler.js +583 -0
- package/package.json +40 -39
- package/dist/src/app.d.ts +0 -66
- package/dist/src/app.js +0 -272
- package/dist/src/events.d.ts +0 -36
- package/dist/src/events.js +0 -37
- package/dist/src/index.d.ts +0 -5
- package/dist/src/index.js +0 -5
- package/dist/src/ipc.d.ts +0 -28
- package/dist/src/ipc.js +0 -214
- package/dist/src/protocol.d.ts +0 -23
- package/dist/src/protocol.js +0 -36
- package/dist/src/widgets.d.ts +0 -528
- package/dist/src/widgets.js +0 -619
- package/src/app.ts +0 -347
- package/src/events.ts +0 -85
- package/src/index.ts +0 -68
- package/src/ipc.ts +0 -233
- package/src/protocol.ts +0 -43
- package/src/widgets.ts +0 -491
package/src/ipc.ts
DELETED
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
// Minimal client for the Glimpse IPC socket.
|
|
2
|
-
//
|
|
3
|
-
// `ipc(service)` resolves a `Subscriber` (no I/O — the connection is opened
|
|
4
|
-
// lazily). `Subscriber.listen` subscribes to an event channel and yields
|
|
5
|
-
// decoded `Event`s; `Subscriber.dispatch` sends an action and awaits the
|
|
6
|
-
// server ack on a one-shot connection. The wire protocol matches the
|
|
7
|
-
// `glimpse-shell watch` / `dispatch` CLIs.
|
|
8
|
-
|
|
9
|
-
import { createConnection, type Socket } from "node:net";
|
|
10
|
-
import { createInterface, type Interface } from "node:readline";
|
|
11
|
-
import { join } from "node:path";
|
|
12
|
-
|
|
13
|
-
/** One decoded event line; `fields` values are unescaped. */
|
|
14
|
-
export interface Event {
|
|
15
|
-
name: string;
|
|
16
|
-
ts: number;
|
|
17
|
-
fields: Record<string, string>;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/** Thrown on connection failure or a rejected dispatch (`ok=false`). */
|
|
21
|
-
export class IpcError extends Error {}
|
|
22
|
-
|
|
23
|
-
interface Conn {
|
|
24
|
-
socket: Socket;
|
|
25
|
-
rl: Interface;
|
|
26
|
-
lines: AsyncIterator<string>;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** A resolved IPC endpoint. Cheap to create; holds only the socket path. */
|
|
30
|
-
export class Subscriber {
|
|
31
|
-
constructor(private readonly socketPath: string) {}
|
|
32
|
-
|
|
33
|
-
/** Subscribe to `channel` (an exact name, `prefix.*`, or `*`) and yield
|
|
34
|
-
* events until the server closes the connection. */
|
|
35
|
-
async *listen(channel: string): AsyncGenerator<Event> {
|
|
36
|
-
const conn = await this.connect();
|
|
37
|
-
try {
|
|
38
|
-
conn.socket.write(`subscribe ${channel}\n`);
|
|
39
|
-
for (;;) {
|
|
40
|
-
const next = await conn.lines.next();
|
|
41
|
-
if (next.done) return;
|
|
42
|
-
const line = next.value.trim();
|
|
43
|
-
if (line) yield parseEvent(line);
|
|
44
|
-
}
|
|
45
|
-
} finally {
|
|
46
|
-
close(conn);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** Dispatch `action` with `params` on a fresh connection and await the
|
|
51
|
-
* ack. Resolves with the extra ack fields; rejects with `IpcError` if the
|
|
52
|
-
* server replies `ok=false`. */
|
|
53
|
-
async dispatch(
|
|
54
|
-
action: string,
|
|
55
|
-
params: Record<string, string> = {},
|
|
56
|
-
): Promise<Record<string, string>> {
|
|
57
|
-
validateToken("action", action, false);
|
|
58
|
-
const entries = Object.entries(params);
|
|
59
|
-
for (const [k] of entries) validateToken("param key", k, true);
|
|
60
|
-
const conn = await this.connect();
|
|
61
|
-
try {
|
|
62
|
-
let line = action;
|
|
63
|
-
for (const [k, v] of entries) {
|
|
64
|
-
line += ` ${k}=${escape(v)}`;
|
|
65
|
-
}
|
|
66
|
-
conn.socket.write(line + "\n");
|
|
67
|
-
const next = await conn.lines.next();
|
|
68
|
-
if (next.done) {
|
|
69
|
-
throw new IpcError("IPC server closed connection without ack");
|
|
70
|
-
}
|
|
71
|
-
return parseAck(next.value.trim());
|
|
72
|
-
} finally {
|
|
73
|
-
close(conn);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
private connect(): Promise<Conn> {
|
|
78
|
-
return new Promise<Conn>((resolve, reject) => {
|
|
79
|
-
const socket = createConnection({ path: this.socketPath });
|
|
80
|
-
let connected = false;
|
|
81
|
-
// This handler stays attached for the socket's whole lifetime. Never
|
|
82
|
-
// remove it: an unhandled 'error' event on a Socket throws and crashes
|
|
83
|
-
// the process. `close()` only destroys the socket, it does not detach.
|
|
84
|
-
socket.on("error", (err: Error) => {
|
|
85
|
-
if (connected) {
|
|
86
|
-
// Post-connect transport error: end the stream cleanly so the
|
|
87
|
-
// line iterator completes instead of crashing the process.
|
|
88
|
-
socket.destroy();
|
|
89
|
-
} else {
|
|
90
|
-
reject(
|
|
91
|
-
new IpcError(
|
|
92
|
-
`cannot connect to IPC socket at ${this.socketPath}: ${err.message}`,
|
|
93
|
-
),
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
socket.once("connect", () => {
|
|
98
|
-
connected = true;
|
|
99
|
-
const rl = createInterface({ input: socket, crlfDelay: Infinity });
|
|
100
|
-
const lines = rl[Symbol.asyncIterator]();
|
|
101
|
-
void (async () => {
|
|
102
|
-
const hello = await lines.next();
|
|
103
|
-
if (hello.done || !hello.value.startsWith("hello")) {
|
|
104
|
-
rl.close();
|
|
105
|
-
socket.destroy();
|
|
106
|
-
reject(
|
|
107
|
-
new IpcError(
|
|
108
|
-
hello.done
|
|
109
|
-
? "IPC server closed connection before hello"
|
|
110
|
-
: `unexpected IPC greeting: ${hello.value}`,
|
|
111
|
-
),
|
|
112
|
-
);
|
|
113
|
-
return;
|
|
114
|
-
}
|
|
115
|
-
resolve({ socket, rl, lines });
|
|
116
|
-
})();
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/** Resolve the {@link Subscriber} for `service` (`"shell"` for the panel).
|
|
123
|
-
*
|
|
124
|
-
* The socket is `<dir>/<service>.sock` (`shell` maps to `ipc.sock`) where
|
|
125
|
-
* `<dir>` is `$GLIMPSE_IPC_DIR`, else `$XDG_RUNTIME_DIR/glimpse`. No
|
|
126
|
-
* connection is made here. */
|
|
127
|
-
export function ipc(service = "shell"): Subscriber {
|
|
128
|
-
return new Subscriber(socketPath(service));
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function socketPath(service: string): string {
|
|
132
|
-
const override = process.env.GLIMPSE_IPC_DIR;
|
|
133
|
-
let dir: string;
|
|
134
|
-
if (override) {
|
|
135
|
-
dir = override;
|
|
136
|
-
} else {
|
|
137
|
-
const runtime = process.env.XDG_RUNTIME_DIR;
|
|
138
|
-
if (!runtime) {
|
|
139
|
-
throw new IpcError(
|
|
140
|
-
"neither GLIMPSE_IPC_DIR nor XDG_RUNTIME_DIR is set; " +
|
|
141
|
-
"cannot locate the Glimpse IPC socket",
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
dir = join(runtime, "glimpse");
|
|
145
|
-
}
|
|
146
|
-
return join(dir, service === "shell" ? "ipc.sock" : `${service}.sock`);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
function close(conn: Conn): void {
|
|
150
|
-
conn.rl.close();
|
|
151
|
-
conn.socket.destroy();
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// The wire protocol splits client lines on whitespace and never unescapes
|
|
155
|
-
// the command name or a field key, so an `action`/key with whitespace would
|
|
156
|
-
// forge extra tokens or whole client lines. Values are safe (escaped).
|
|
157
|
-
function validateToken(label: string, token: string, forbidEq: boolean): void {
|
|
158
|
-
if (token.length === 0) {
|
|
159
|
-
throw new IpcError(`IPC ${label} must not be empty`);
|
|
160
|
-
}
|
|
161
|
-
if (/\s/.test(token)) {
|
|
162
|
-
throw new IpcError(`IPC ${label} "${token}" must not contain whitespace`);
|
|
163
|
-
}
|
|
164
|
-
if (forbidEq && token.includes("=")) {
|
|
165
|
-
throw new IpcError(`IPC param key "${token}" must not contain '='`);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function escape(s: string): string {
|
|
170
|
-
return s
|
|
171
|
-
.replaceAll("\\", "\\\\")
|
|
172
|
-
.replaceAll("\n", "\\n")
|
|
173
|
-
.replaceAll("\t", "\\t")
|
|
174
|
-
.replaceAll(" ", "\\s");
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
function unescape(s: string): string {
|
|
178
|
-
let out = "";
|
|
179
|
-
for (let i = 0; i < s.length; i++) {
|
|
180
|
-
if (s[i] !== "\\") {
|
|
181
|
-
out += s[i];
|
|
182
|
-
continue;
|
|
183
|
-
}
|
|
184
|
-
const next = s[++i];
|
|
185
|
-
if (next === "s") out += " ";
|
|
186
|
-
else if (next === "n") out += "\n";
|
|
187
|
-
else if (next === "t") out += "\t";
|
|
188
|
-
else if (next === "\\") out += "\\";
|
|
189
|
-
else if (next === undefined) out += "\\";
|
|
190
|
-
else out += "\\" + next;
|
|
191
|
-
}
|
|
192
|
-
return out;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
function parseEvent(line: string): Event {
|
|
196
|
-
const tokens = line.split(/\s+/);
|
|
197
|
-
const name = tokens[0] ?? "";
|
|
198
|
-
let ts = 0;
|
|
199
|
-
const fields: Record<string, string> = {};
|
|
200
|
-
for (const token of tokens.slice(1)) {
|
|
201
|
-
const eq = token.indexOf("=");
|
|
202
|
-
if (eq < 0) continue;
|
|
203
|
-
const key = token.slice(0, eq);
|
|
204
|
-
const value = unescape(token.slice(eq + 1));
|
|
205
|
-
if (key === "ts" && /^\d+$/.test(value)) {
|
|
206
|
-
ts = Number(value);
|
|
207
|
-
continue;
|
|
208
|
-
}
|
|
209
|
-
fields[key] = value;
|
|
210
|
-
}
|
|
211
|
-
return { name, ts, fields };
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
function parseAck(line: string): Record<string, string> {
|
|
215
|
-
const tokens = line.split(/\s+/);
|
|
216
|
-
if (tokens[0] !== "ack") {
|
|
217
|
-
throw new IpcError(`expected an ack, got: ${line}`);
|
|
218
|
-
}
|
|
219
|
-
let ok = false;
|
|
220
|
-
const fields: Record<string, string> = {};
|
|
221
|
-
for (const token of tokens.slice(1)) {
|
|
222
|
-
const eq = token.indexOf("=");
|
|
223
|
-
if (eq < 0) continue;
|
|
224
|
-
const key = token.slice(0, eq);
|
|
225
|
-
const value = unescape(token.slice(eq + 1));
|
|
226
|
-
if (key === "ok") ok = value === "true";
|
|
227
|
-
else fields[key] = value;
|
|
228
|
-
}
|
|
229
|
-
if (!ok) {
|
|
230
|
-
throw new IpcError(fields.error ?? "command failed");
|
|
231
|
-
}
|
|
232
|
-
return fields;
|
|
233
|
-
}
|
package/src/protocol.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
export class StatusItem {
|
|
2
|
-
constructor(
|
|
3
|
-
public readonly options: {
|
|
4
|
-
id?: string;
|
|
5
|
-
icon?: string;
|
|
6
|
-
label?: string;
|
|
7
|
-
tooltip?: string;
|
|
8
|
-
cssClasses?: string[];
|
|
9
|
-
} = {},
|
|
10
|
-
) {}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Return a copy with `cssClass` appended to `cssClasses`. Immutable-update
|
|
14
|
-
* form that mirrors the Rust SDK's `css_class()` builder so SDK-hopping
|
|
15
|
-
* users see equivalent ergonomics across languages.
|
|
16
|
-
*/
|
|
17
|
-
withCssClass(cssClass: string): StatusItem {
|
|
18
|
-
return new StatusItem({
|
|
19
|
-
...this.options,
|
|
20
|
-
cssClasses: [...(this.options.cssClasses ?? []), cssClass],
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
toProtocol(): Record<string, unknown> {
|
|
25
|
-
const payload: Record<string, unknown> = {};
|
|
26
|
-
if (this.options.id !== undefined) {
|
|
27
|
-
payload.id = this.options.id;
|
|
28
|
-
}
|
|
29
|
-
if (this.options.icon !== undefined) {
|
|
30
|
-
payload.icon = this.options.icon;
|
|
31
|
-
}
|
|
32
|
-
if (this.options.label !== undefined) {
|
|
33
|
-
payload.label = this.options.label;
|
|
34
|
-
}
|
|
35
|
-
if (this.options.tooltip !== undefined) {
|
|
36
|
-
payload.tooltip = this.options.tooltip;
|
|
37
|
-
}
|
|
38
|
-
if (this.options.cssClasses && this.options.cssClasses.length > 0) {
|
|
39
|
-
payload.css_classes = [...this.options.cssClasses];
|
|
40
|
-
}
|
|
41
|
-
return payload;
|
|
42
|
-
}
|
|
43
|
-
}
|