glimpse-sdk 0.3.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 +33 -1
- package/dist/src/app.d.ts +25 -1
- package/dist/src/app.js +96 -4
- package/dist/src/events.d.ts +1 -1
- package/dist/src/events.js +1 -1
- package/dist/src/index.d.ts +4 -3
- package/dist/src/index.js +4 -3
- package/dist/src/ipc.d.ts +28 -0
- package/dist/src/ipc.js +214 -0
- package/dist/src/protocol.d.ts +2 -15
- package/dist/src/protocol.js +1 -18
- package/dist/src/widgets.d.ts +415 -256
- package/dist/src/widgets.js +379 -457
- package/package.json +1 -1
- package/src/app.ts +122 -5
- package/src/events.ts +2 -2
- package/src/index.ts +39 -35
- package/src/ipc.ts +233 -0
- package/src/protocol.ts +2 -23
- package/src/widgets.ts +334 -787
package/README.md
CHANGED
|
@@ -10,6 +10,18 @@ Requires Node.js 20+.
|
|
|
10
10
|
npm install glimpse-sdk
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
+
## Develop
|
|
14
|
+
|
|
15
|
+
Create and live-run a TypeScript applet project with the Glimpse tooling:
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
glimpse-applet new counter --lang typescript
|
|
19
|
+
cd counter
|
|
20
|
+
glimpse-applet dev
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Read `docs/custom-applets/tooling.md` for project layout, `applet.toml`, dev applets, linking, and diagnostics.
|
|
24
|
+
|
|
13
25
|
## Goals
|
|
14
26
|
|
|
15
27
|
- typed protocol models
|
|
@@ -27,8 +39,8 @@ import {
|
|
|
27
39
|
Button,
|
|
28
40
|
Hero,
|
|
29
41
|
Icon,
|
|
30
|
-
Label,
|
|
31
42
|
StatusItem,
|
|
43
|
+
Label,
|
|
32
44
|
type TreeNode,
|
|
33
45
|
} from "glimpse-sdk";
|
|
34
46
|
|
|
@@ -92,3 +104,23 @@ Use explicit registration helpers instead of decorators:
|
|
|
92
104
|
|
|
93
105
|
The SDK owns the line transport. `status(state)` produces the panel items;
|
|
94
106
|
`popover(state)` produces the popover tree; both are pure functions of state.
|
|
107
|
+
|
|
108
|
+
## IPC client
|
|
109
|
+
|
|
110
|
+
Talk to a running Glimpse daemon: subscribe to event channels and dispatch
|
|
111
|
+
actions. `ipc(service)` only resolves the socket path — the connection is
|
|
112
|
+
opened lazily.
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import { ipc } from "glimpse-sdk";
|
|
116
|
+
|
|
117
|
+
const sub = ipc("shell"); // "shell" | "wallpaper" | "idle" | "lock"
|
|
118
|
+
|
|
119
|
+
// Fire an action; awaits the ack, throws IpcError if the server rejects it.
|
|
120
|
+
const ack = await sub.dispatch("open_uri", { uri: "https://example.com" });
|
|
121
|
+
|
|
122
|
+
// Stream events until the socket closes.
|
|
123
|
+
for await (const ev of sub.listen("audio.*")) {
|
|
124
|
+
console.log(ev.name, ev.fields);
|
|
125
|
+
}
|
|
126
|
+
```
|
package/dist/src/app.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type CallbackEvent, type ChangeEvent, type ClickEvent, type InitEvent, type InputEvent, type ScrollEvent, type ToggleEvent } from "./events.js";
|
|
1
|
+
import { type CallbackEvent, type ChangeEvent, type ClickEvent, type InitEvent, type InputEvent, type PopoverEvent, type ScrollEvent, type ToggleEvent } from "./events.js";
|
|
2
2
|
import { StatusItem } from "./protocol.js";
|
|
3
3
|
import { type TreeNode } from "./widgets.js";
|
|
4
4
|
type Handler<EventT> = (event: EventT) => void | Promise<void>;
|
|
@@ -7,9 +7,21 @@ interface OutgoingMessage {
|
|
|
7
7
|
data: unknown;
|
|
8
8
|
line: string;
|
|
9
9
|
}
|
|
10
|
+
interface ShowNotificationOptions {
|
|
11
|
+
body?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface CommandResult {
|
|
14
|
+
stdout: string;
|
|
15
|
+
stderr: string;
|
|
16
|
+
rc: number;
|
|
17
|
+
}
|
|
18
|
+
interface DismissNotificationArgs {
|
|
19
|
+
id: number;
|
|
20
|
+
}
|
|
10
21
|
export declare abstract class Applet<State extends object> {
|
|
11
22
|
state: State;
|
|
12
23
|
private readonly handlerMap;
|
|
24
|
+
private inlineHandlerMap;
|
|
13
25
|
private readonly outgoing;
|
|
14
26
|
private flushPromise;
|
|
15
27
|
private renderQueued;
|
|
@@ -18,6 +30,7 @@ export declare abstract class Applet<State extends object> {
|
|
|
18
30
|
private popoverOpen;
|
|
19
31
|
protected constructor();
|
|
20
32
|
protected abstract initialState(): State;
|
|
33
|
+
protected cssClass(): string | null;
|
|
21
34
|
protected onStart(): Promise<void>;
|
|
22
35
|
protected onInit(_event: InitEvent): Promise<void>;
|
|
23
36
|
protected onCallback(_event: CallbackEvent): Promise<void>;
|
|
@@ -29,7 +42,16 @@ export declare abstract class Applet<State extends object> {
|
|
|
29
42
|
onInput(id: string, handler: Handler<InputEvent>): void;
|
|
30
43
|
onChange(id: string, handler: Handler<ChangeEvent>): void;
|
|
31
44
|
onToggle(id: string, handler: Handler<ToggleEvent>): void;
|
|
45
|
+
onPopover(handler: Handler<PopoverEvent>): void;
|
|
32
46
|
isPopoverOpen(): boolean;
|
|
47
|
+
log(...args: unknown[]): void;
|
|
48
|
+
protected showNotification(summary: string, options?: ShowNotificationOptions): Promise<void>;
|
|
49
|
+
protected openUri(uri: string): Promise<void>;
|
|
50
|
+
protected copyToClipboard(text: string): Promise<void>;
|
|
51
|
+
protected runCommand(command: string[]): Promise<CommandResult>;
|
|
52
|
+
protected dismissNotification(args: DismissNotificationArgs): void;
|
|
53
|
+
protected closePopover(): void;
|
|
54
|
+
protected runDesktopCommand(command: string, args: string[], input?: string): Promise<void>;
|
|
33
55
|
run(): Promise<void>;
|
|
34
56
|
protected drainOutgoingForTest(): Promise<OutgoingMessage[]>;
|
|
35
57
|
private register;
|
|
@@ -38,5 +60,7 @@ export declare abstract class Applet<State extends object> {
|
|
|
38
60
|
private scheduleRender;
|
|
39
61
|
private flushRender;
|
|
40
62
|
private emit;
|
|
63
|
+
private emitAction;
|
|
41
64
|
}
|
|
65
|
+
export declare function runCommand(command: string[]): Promise<CommandResult>;
|
|
42
66
|
export {};
|
package/dist/src/app.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { createInterface } from "node:readline";
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
2
3
|
import { parseCallbackEvent, parseInitEvent, } from "./events.js";
|
|
4
|
+
import { InlineHandlerRegistry } from "./widgets.js";
|
|
3
5
|
export class Applet {
|
|
4
6
|
state;
|
|
5
7
|
handlerMap = new Map();
|
|
8
|
+
inlineHandlerMap = new Map();
|
|
6
9
|
outgoing = [];
|
|
7
10
|
flushPromise = null;
|
|
8
11
|
renderQueued = false;
|
|
@@ -12,6 +15,9 @@ export class Applet {
|
|
|
12
15
|
constructor() {
|
|
13
16
|
this.state = this.initialState();
|
|
14
17
|
}
|
|
18
|
+
cssClass() {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
15
21
|
async onStart() { }
|
|
16
22
|
async onInit(_event) { }
|
|
17
23
|
async onCallback(_event) { }
|
|
@@ -40,9 +46,41 @@ export class Applet {
|
|
|
40
46
|
onToggle(id, handler) {
|
|
41
47
|
this.register("toggle", id, handler);
|
|
42
48
|
}
|
|
49
|
+
onPopover(handler) {
|
|
50
|
+
this.register("open", "popover", handler);
|
|
51
|
+
this.register("close", "popover", handler);
|
|
52
|
+
}
|
|
43
53
|
isPopoverOpen() {
|
|
44
54
|
return this.popoverOpen;
|
|
45
55
|
}
|
|
56
|
+
log(...args) {
|
|
57
|
+
process.stderr.write(args.map(String).join(" ") + "\n");
|
|
58
|
+
}
|
|
59
|
+
async showNotification(summary, options = {}) {
|
|
60
|
+
const args = [summary];
|
|
61
|
+
if (options.body !== undefined) {
|
|
62
|
+
args.push(options.body);
|
|
63
|
+
}
|
|
64
|
+
await this.runDesktopCommand("notify-send", args);
|
|
65
|
+
}
|
|
66
|
+
async openUri(uri) {
|
|
67
|
+
await this.runDesktopCommand("xdg-open", [uri]);
|
|
68
|
+
}
|
|
69
|
+
async copyToClipboard(text) {
|
|
70
|
+
await this.runDesktopCommand("wl-copy", [], text);
|
|
71
|
+
}
|
|
72
|
+
async runCommand(command) {
|
|
73
|
+
return await runCommand(command);
|
|
74
|
+
}
|
|
75
|
+
dismissNotification(args) {
|
|
76
|
+
this.emitAction("dismiss_notification", args);
|
|
77
|
+
}
|
|
78
|
+
closePopover() {
|
|
79
|
+
this.emitAction("close_popover", {});
|
|
80
|
+
}
|
|
81
|
+
async runDesktopCommand(command, args, input) {
|
|
82
|
+
await runDesktopCommand([command, ...args], input);
|
|
83
|
+
}
|
|
46
84
|
async run() {
|
|
47
85
|
process.stdout.on("error", (err) => {
|
|
48
86
|
if (err.code === "EPIPE") {
|
|
@@ -50,6 +88,10 @@ export class Applet {
|
|
|
50
88
|
}
|
|
51
89
|
});
|
|
52
90
|
await this.onStart();
|
|
91
|
+
const cssClass = this.cssClass();
|
|
92
|
+
if (cssClass !== null) {
|
|
93
|
+
process.stdout.write(`class ${cssClass}\n`);
|
|
94
|
+
}
|
|
53
95
|
await this.scheduleRender();
|
|
54
96
|
const rl = createInterface({
|
|
55
97
|
input: process.stdin,
|
|
@@ -89,7 +131,13 @@ export class Applet {
|
|
|
89
131
|
this.handlerMap.set(`${event}:${id}`, handler);
|
|
90
132
|
}
|
|
91
133
|
async dispatchCallback(event) {
|
|
92
|
-
const
|
|
134
|
+
const key = `${event.event}:${event.id}`;
|
|
135
|
+
const inlineHandler = this.inlineHandlerMap.get(key);
|
|
136
|
+
if (inlineHandler !== undefined) {
|
|
137
|
+
await inlineHandler(event);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const handler = this.handlerMap.get(key);
|
|
93
141
|
if (handler !== undefined) {
|
|
94
142
|
await handler(event);
|
|
95
143
|
return;
|
|
@@ -136,10 +184,16 @@ export class Applet {
|
|
|
136
184
|
this.emit("status", { items: status });
|
|
137
185
|
}
|
|
138
186
|
const widget = await this.popover(this.state);
|
|
187
|
+
if (widget !== null) {
|
|
188
|
+
const registry = new InlineHandlerRegistry();
|
|
189
|
+
widget.bindHandlers?.(registry, []);
|
|
190
|
+
this.inlineHandlerMap = registry.handlers;
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
this.inlineHandlerMap = new Map();
|
|
194
|
+
}
|
|
139
195
|
const tree = { root: widget?.toProtocol() ?? null };
|
|
140
|
-
|
|
141
|
-
const shouldEmit = this.popoverOpen || this.lastTree === null || tree.root === null;
|
|
142
|
-
if (shouldEmit && !deepEqual(tree, this.lastTree)) {
|
|
196
|
+
if (!deepEqual(tree, this.lastTree)) {
|
|
143
197
|
this.lastTree = tree;
|
|
144
198
|
this.emit("popover", tree);
|
|
145
199
|
}
|
|
@@ -157,6 +211,9 @@ export class Applet {
|
|
|
157
211
|
}
|
|
158
212
|
}
|
|
159
213
|
}
|
|
214
|
+
emitAction(type, args) {
|
|
215
|
+
this.emit("action", { type, arguments: args });
|
|
216
|
+
}
|
|
160
217
|
}
|
|
161
218
|
function parseLine(line) {
|
|
162
219
|
const trimmed = line.trim();
|
|
@@ -178,3 +235,38 @@ function deepEqual(left, right) {
|
|
|
178
235
|
function isPopoverEvent(event) {
|
|
179
236
|
return event.event === "open" || event.event === "close";
|
|
180
237
|
}
|
|
238
|
+
export async function runCommand(command) {
|
|
239
|
+
return await runCommandWithInput(command);
|
|
240
|
+
}
|
|
241
|
+
async function runDesktopCommand(command, input) {
|
|
242
|
+
const result = await runCommandWithInput(command, input);
|
|
243
|
+
if (result.rc !== 0) {
|
|
244
|
+
throw new Error(`${command[0]} exited with status ${result.rc}`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
async function runCommandWithInput(command, input) {
|
|
248
|
+
if (command.length === 0) {
|
|
249
|
+
throw new Error("command must not be empty");
|
|
250
|
+
}
|
|
251
|
+
return await new Promise((resolve, reject) => {
|
|
252
|
+
const child = spawn(command[0], command.slice(1), { stdio: ["pipe", "pipe", "pipe"] });
|
|
253
|
+
const stdout = [];
|
|
254
|
+
const stderr = [];
|
|
255
|
+
child.on("error", reject);
|
|
256
|
+
child.stdout.on("data", (chunk) => stdout.push(chunk));
|
|
257
|
+
child.stderr.on("data", (chunk) => stderr.push(chunk));
|
|
258
|
+
child.on("close", (code) => {
|
|
259
|
+
resolve({
|
|
260
|
+
stdout: Buffer.concat(stdout).toString("utf8"),
|
|
261
|
+
stderr: Buffer.concat(stderr).toString("utf8"),
|
|
262
|
+
rc: code ?? -1,
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
if (input !== undefined) {
|
|
266
|
+
child.stdin.end(input);
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
child.stdin.end();
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
}
|
package/dist/src/events.d.ts
CHANGED
package/dist/src/events.js
CHANGED
|
@@ -8,7 +8,7 @@ export function parseCallbackEvent(payload) {
|
|
|
8
8
|
const event = String(payload.type ?? payload.event ?? "");
|
|
9
9
|
const id = String(payload.id ?? "");
|
|
10
10
|
if (event === "click") {
|
|
11
|
-
return { id, event, button: payload.button === undefined ?
|
|
11
|
+
return { id, event, button: payload.button === undefined ? null : String(payload.button) };
|
|
12
12
|
}
|
|
13
13
|
if (event === "scroll") {
|
|
14
14
|
return {
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { Applet } from "./app.js";
|
|
1
|
+
export { Applet, runCommand } from "./app.js";
|
|
2
2
|
export { type CallbackEvent, type ChangeEvent, type ClickEvent, type InitEvent, type InputEvent, type PopoverEvent, type ScrollEvent, type ToggleEvent, parseCallbackEvent, parseInitEvent, } from "./events.js";
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
3
|
+
export { ipc, IpcError, Subscriber } from "./ipc.js";
|
|
4
|
+
export { StatusItem } from "./protocol.js";
|
|
5
|
+
export { Badge, type BadgeKind, BatteryHero, BoxedList, ButtonRow, Calendar, CameraIndicator, Choice, ChoiceList, ChoiceTile, CircleBox, Column, Container, DateHero, EmptyState, EventItem, Events, ExpanderTile, Header, Hero, KeyValueGrid, Label, LocationIndicator, Meter, MicIndicator, MutedIndicator, type PagerAppearance, PagerItem, PagerStrip, PanelIndicator, PopoverShell, type PopoverSize, Row, ScreenCastIndicator, Scroll, SegmentedTile, Separator, SliderTile, Spinner, StatusDot, type StatusDotStatus, SwitchTile, Tile, type TreeNode, WeatherForecastItem, WeatherForecastList, WeatherHourlyItem, WeatherHourlyStrip, WidgetBase, WorldClock, WorldClockRow, } from "./widgets.js";
|
package/dist/src/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export { Applet } from "./app.js";
|
|
1
|
+
export { Applet, runCommand } from "./app.js";
|
|
2
2
|
export { parseCallbackEvent, parseInitEvent, } from "./events.js";
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
3
|
+
export { ipc, IpcError, Subscriber } from "./ipc.js";
|
|
4
|
+
export { StatusItem } from "./protocol.js";
|
|
5
|
+
export { Badge, BatteryHero, BoxedList, ButtonRow, Calendar, CameraIndicator, Choice, ChoiceList, ChoiceTile, CircleBox, Column, Container, DateHero, EmptyState, EventItem, Events, ExpanderTile, Header, Hero, KeyValueGrid, Label, LocationIndicator, Meter, MicIndicator, MutedIndicator, PagerItem, PagerStrip, PanelIndicator, PopoverShell, Row, ScreenCastIndicator, Scroll, SegmentedTile, Separator, SliderTile, Spinner, StatusDot, SwitchTile, Tile, WeatherForecastItem, WeatherForecastList, WeatherHourlyItem, WeatherHourlyStrip, WidgetBase, WorldClock, WorldClockRow, } from "./widgets.js";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** One decoded event line; `fields` values are unescaped. */
|
|
2
|
+
export interface Event {
|
|
3
|
+
name: string;
|
|
4
|
+
ts: number;
|
|
5
|
+
fields: Record<string, string>;
|
|
6
|
+
}
|
|
7
|
+
/** Thrown on connection failure or a rejected dispatch (`ok=false`). */
|
|
8
|
+
export declare class IpcError extends Error {
|
|
9
|
+
}
|
|
10
|
+
/** A resolved IPC endpoint. Cheap to create; holds only the socket path. */
|
|
11
|
+
export declare class Subscriber {
|
|
12
|
+
private readonly socketPath;
|
|
13
|
+
constructor(socketPath: string);
|
|
14
|
+
/** Subscribe to `channel` (an exact name, `prefix.*`, or `*`) and yield
|
|
15
|
+
* events until the server closes the connection. */
|
|
16
|
+
listen(channel: string): AsyncGenerator<Event>;
|
|
17
|
+
/** Dispatch `action` with `params` on a fresh connection and await the
|
|
18
|
+
* ack. Resolves with the extra ack fields; rejects with `IpcError` if the
|
|
19
|
+
* server replies `ok=false`. */
|
|
20
|
+
dispatch(action: string, params?: Record<string, string>): Promise<Record<string, string>>;
|
|
21
|
+
private connect;
|
|
22
|
+
}
|
|
23
|
+
/** Resolve the {@link Subscriber} for `service` (`"shell"` for the panel).
|
|
24
|
+
*
|
|
25
|
+
* The socket is `<dir>/<service>.sock` (`shell` maps to `ipc.sock`) where
|
|
26
|
+
* `<dir>` is `$GLIMPSE_IPC_DIR`, else `$XDG_RUNTIME_DIR/glimpse`. No
|
|
27
|
+
* connection is made here. */
|
|
28
|
+
export declare function ipc(service?: string): Subscriber;
|
package/dist/src/ipc.js
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
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
|
+
import { createConnection } from "node:net";
|
|
9
|
+
import { createInterface } from "node:readline";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
/** Thrown on connection failure or a rejected dispatch (`ok=false`). */
|
|
12
|
+
export class IpcError extends Error {
|
|
13
|
+
}
|
|
14
|
+
/** A resolved IPC endpoint. Cheap to create; holds only the socket path. */
|
|
15
|
+
export class Subscriber {
|
|
16
|
+
socketPath;
|
|
17
|
+
constructor(socketPath) {
|
|
18
|
+
this.socketPath = socketPath;
|
|
19
|
+
}
|
|
20
|
+
/** Subscribe to `channel` (an exact name, `prefix.*`, or `*`) and yield
|
|
21
|
+
* events until the server closes the connection. */
|
|
22
|
+
async *listen(channel) {
|
|
23
|
+
const conn = await this.connect();
|
|
24
|
+
try {
|
|
25
|
+
conn.socket.write(`subscribe ${channel}\n`);
|
|
26
|
+
for (;;) {
|
|
27
|
+
const next = await conn.lines.next();
|
|
28
|
+
if (next.done)
|
|
29
|
+
return;
|
|
30
|
+
const line = next.value.trim();
|
|
31
|
+
if (line)
|
|
32
|
+
yield parseEvent(line);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
finally {
|
|
36
|
+
close(conn);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** Dispatch `action` with `params` on a fresh connection and await the
|
|
40
|
+
* ack. Resolves with the extra ack fields; rejects with `IpcError` if the
|
|
41
|
+
* server replies `ok=false`. */
|
|
42
|
+
async dispatch(action, params = {}) {
|
|
43
|
+
validateToken("action", action, false);
|
|
44
|
+
const entries = Object.entries(params);
|
|
45
|
+
for (const [k] of entries)
|
|
46
|
+
validateToken("param key", k, true);
|
|
47
|
+
const conn = await this.connect();
|
|
48
|
+
try {
|
|
49
|
+
let line = action;
|
|
50
|
+
for (const [k, v] of entries) {
|
|
51
|
+
line += ` ${k}=${escape(v)}`;
|
|
52
|
+
}
|
|
53
|
+
conn.socket.write(line + "\n");
|
|
54
|
+
const next = await conn.lines.next();
|
|
55
|
+
if (next.done) {
|
|
56
|
+
throw new IpcError("IPC server closed connection without ack");
|
|
57
|
+
}
|
|
58
|
+
return parseAck(next.value.trim());
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
close(conn);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
connect() {
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
const socket = createConnection({ path: this.socketPath });
|
|
67
|
+
let connected = false;
|
|
68
|
+
// This handler stays attached for the socket's whole lifetime. Never
|
|
69
|
+
// remove it: an unhandled 'error' event on a Socket throws and crashes
|
|
70
|
+
// the process. `close()` only destroys the socket, it does not detach.
|
|
71
|
+
socket.on("error", (err) => {
|
|
72
|
+
if (connected) {
|
|
73
|
+
// Post-connect transport error: end the stream cleanly so the
|
|
74
|
+
// line iterator completes instead of crashing the process.
|
|
75
|
+
socket.destroy();
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
reject(new IpcError(`cannot connect to IPC socket at ${this.socketPath}: ${err.message}`));
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
socket.once("connect", () => {
|
|
82
|
+
connected = true;
|
|
83
|
+
const rl = createInterface({ input: socket, crlfDelay: Infinity });
|
|
84
|
+
const lines = rl[Symbol.asyncIterator]();
|
|
85
|
+
void (async () => {
|
|
86
|
+
const hello = await lines.next();
|
|
87
|
+
if (hello.done || !hello.value.startsWith("hello")) {
|
|
88
|
+
rl.close();
|
|
89
|
+
socket.destroy();
|
|
90
|
+
reject(new IpcError(hello.done
|
|
91
|
+
? "IPC server closed connection before hello"
|
|
92
|
+
: `unexpected IPC greeting: ${hello.value}`));
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
resolve({ socket, rl, lines });
|
|
96
|
+
})();
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/** Resolve the {@link Subscriber} for `service` (`"shell"` for the panel).
|
|
102
|
+
*
|
|
103
|
+
* The socket is `<dir>/<service>.sock` (`shell` maps to `ipc.sock`) where
|
|
104
|
+
* `<dir>` is `$GLIMPSE_IPC_DIR`, else `$XDG_RUNTIME_DIR/glimpse`. No
|
|
105
|
+
* connection is made here. */
|
|
106
|
+
export function ipc(service = "shell") {
|
|
107
|
+
return new Subscriber(socketPath(service));
|
|
108
|
+
}
|
|
109
|
+
function socketPath(service) {
|
|
110
|
+
const override = process.env.GLIMPSE_IPC_DIR;
|
|
111
|
+
let dir;
|
|
112
|
+
if (override) {
|
|
113
|
+
dir = override;
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
const runtime = process.env.XDG_RUNTIME_DIR;
|
|
117
|
+
if (!runtime) {
|
|
118
|
+
throw new IpcError("neither GLIMPSE_IPC_DIR nor XDG_RUNTIME_DIR is set; " +
|
|
119
|
+
"cannot locate the Glimpse IPC socket");
|
|
120
|
+
}
|
|
121
|
+
dir = join(runtime, "glimpse");
|
|
122
|
+
}
|
|
123
|
+
return join(dir, service === "shell" ? "ipc.sock" : `${service}.sock`);
|
|
124
|
+
}
|
|
125
|
+
function close(conn) {
|
|
126
|
+
conn.rl.close();
|
|
127
|
+
conn.socket.destroy();
|
|
128
|
+
}
|
|
129
|
+
// The wire protocol splits client lines on whitespace and never unescapes
|
|
130
|
+
// the command name or a field key, so an `action`/key with whitespace would
|
|
131
|
+
// forge extra tokens or whole client lines. Values are safe (escaped).
|
|
132
|
+
function validateToken(label, token, forbidEq) {
|
|
133
|
+
if (token.length === 0) {
|
|
134
|
+
throw new IpcError(`IPC ${label} must not be empty`);
|
|
135
|
+
}
|
|
136
|
+
if (/\s/.test(token)) {
|
|
137
|
+
throw new IpcError(`IPC ${label} "${token}" must not contain whitespace`);
|
|
138
|
+
}
|
|
139
|
+
if (forbidEq && token.includes("=")) {
|
|
140
|
+
throw new IpcError(`IPC param key "${token}" must not contain '='`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function escape(s) {
|
|
144
|
+
return s
|
|
145
|
+
.replaceAll("\\", "\\\\")
|
|
146
|
+
.replaceAll("\n", "\\n")
|
|
147
|
+
.replaceAll("\t", "\\t")
|
|
148
|
+
.replaceAll(" ", "\\s");
|
|
149
|
+
}
|
|
150
|
+
function unescape(s) {
|
|
151
|
+
let out = "";
|
|
152
|
+
for (let i = 0; i < s.length; i++) {
|
|
153
|
+
if (s[i] !== "\\") {
|
|
154
|
+
out += s[i];
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
const next = s[++i];
|
|
158
|
+
if (next === "s")
|
|
159
|
+
out += " ";
|
|
160
|
+
else if (next === "n")
|
|
161
|
+
out += "\n";
|
|
162
|
+
else if (next === "t")
|
|
163
|
+
out += "\t";
|
|
164
|
+
else if (next === "\\")
|
|
165
|
+
out += "\\";
|
|
166
|
+
else if (next === undefined)
|
|
167
|
+
out += "\\";
|
|
168
|
+
else
|
|
169
|
+
out += "\\" + next;
|
|
170
|
+
}
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
function parseEvent(line) {
|
|
174
|
+
const tokens = line.split(/\s+/);
|
|
175
|
+
const name = tokens[0] ?? "";
|
|
176
|
+
let ts = 0;
|
|
177
|
+
const fields = {};
|
|
178
|
+
for (const token of tokens.slice(1)) {
|
|
179
|
+
const eq = token.indexOf("=");
|
|
180
|
+
if (eq < 0)
|
|
181
|
+
continue;
|
|
182
|
+
const key = token.slice(0, eq);
|
|
183
|
+
const value = unescape(token.slice(eq + 1));
|
|
184
|
+
if (key === "ts" && /^\d+$/.test(value)) {
|
|
185
|
+
ts = Number(value);
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
fields[key] = value;
|
|
189
|
+
}
|
|
190
|
+
return { name, ts, fields };
|
|
191
|
+
}
|
|
192
|
+
function parseAck(line) {
|
|
193
|
+
const tokens = line.split(/\s+/);
|
|
194
|
+
if (tokens[0] !== "ack") {
|
|
195
|
+
throw new IpcError(`expected an ack, got: ${line}`);
|
|
196
|
+
}
|
|
197
|
+
let ok = false;
|
|
198
|
+
const fields = {};
|
|
199
|
+
for (const token of tokens.slice(1)) {
|
|
200
|
+
const eq = token.indexOf("=");
|
|
201
|
+
if (eq < 0)
|
|
202
|
+
continue;
|
|
203
|
+
const key = token.slice(0, eq);
|
|
204
|
+
const value = unescape(token.slice(eq + 1));
|
|
205
|
+
if (key === "ok")
|
|
206
|
+
ok = value === "true";
|
|
207
|
+
else
|
|
208
|
+
fields[key] = value;
|
|
209
|
+
}
|
|
210
|
+
if (!ok) {
|
|
211
|
+
throw new IpcError(fields.error ?? "command failed");
|
|
212
|
+
}
|
|
213
|
+
return fields;
|
|
214
|
+
}
|
package/dist/src/protocol.d.ts
CHANGED
|
@@ -1,26 +1,13 @@
|
|
|
1
|
-
export type IconKind = "name" | "path";
|
|
2
|
-
export declare class Icon {
|
|
3
|
-
readonly kind: IconKind;
|
|
4
|
-
readonly value: string;
|
|
5
|
-
constructor(kind: IconKind, value: string);
|
|
6
|
-
static name(value: string): Icon;
|
|
7
|
-
static path(value: string): Icon;
|
|
8
|
-
toProtocol(): {
|
|
9
|
-
name: string;
|
|
10
|
-
} | {
|
|
11
|
-
path: string;
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
1
|
export declare class StatusItem {
|
|
15
2
|
readonly options: {
|
|
16
3
|
id?: string;
|
|
17
|
-
icon?:
|
|
4
|
+
icon?: string;
|
|
18
5
|
label?: string;
|
|
19
6
|
tooltip?: string;
|
|
20
7
|
};
|
|
21
8
|
constructor(options?: {
|
|
22
9
|
id?: string;
|
|
23
|
-
icon?:
|
|
10
|
+
icon?: string;
|
|
24
11
|
label?: string;
|
|
25
12
|
tooltip?: string;
|
|
26
13
|
});
|
package/dist/src/protocol.js
CHANGED
|
@@ -1,20 +1,3 @@
|
|
|
1
|
-
export class Icon {
|
|
2
|
-
kind;
|
|
3
|
-
value;
|
|
4
|
-
constructor(kind, value) {
|
|
5
|
-
this.kind = kind;
|
|
6
|
-
this.value = value;
|
|
7
|
-
}
|
|
8
|
-
static name(value) {
|
|
9
|
-
return new Icon("name", value);
|
|
10
|
-
}
|
|
11
|
-
static path(value) {
|
|
12
|
-
return new Icon("path", value);
|
|
13
|
-
}
|
|
14
|
-
toProtocol() {
|
|
15
|
-
return this.kind === "name" ? { name: this.value } : { path: this.value };
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
1
|
export class StatusItem {
|
|
19
2
|
options;
|
|
20
3
|
constructor(options = {}) {
|
|
@@ -26,7 +9,7 @@ export class StatusItem {
|
|
|
26
9
|
payload.id = this.options.id;
|
|
27
10
|
}
|
|
28
11
|
if (this.options.icon !== undefined) {
|
|
29
|
-
payload.icon = this.options.icon
|
|
12
|
+
payload.icon = this.options.icon;
|
|
30
13
|
}
|
|
31
14
|
if (this.options.label !== undefined) {
|
|
32
15
|
payload.label = this.options.label;
|