cmux 0.3.5 → 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/README.md +97 -51
- package/dist/src/base64.d.ts +4 -0
- package/dist/src/base64.js +38 -0
- package/dist/src/browser.d.ts +6 -0
- package/dist/src/browser.js +6 -0
- package/dist/src/client.d.ts +202 -0
- package/dist/src/client.js +799 -0
- package/dist/src/errors.d.ts +18 -0
- package/dist/src/errors.js +29 -0
- package/dist/src/index.d.ts +8 -0
- package/dist/src/index.js +8 -0
- package/dist/src/node-client.d.ts +18 -0
- package/dist/src/node-client.js +18 -0
- package/dist/src/node-transport.d.ts +27 -0
- package/dist/src/node-transport.js +99 -0
- package/dist/src/protocol/commands.d.ts +770 -0
- package/dist/src/protocol/commands.js +1 -0
- package/dist/src/protocol/common.d.ts +74 -0
- package/dist/src/protocol/common.js +1 -0
- package/dist/src/protocol/events.d.ts +349 -0
- package/dist/src/protocol/events.js +1 -0
- package/dist/src/protocol/index.d.ts +5 -0
- package/dist/src/protocol/index.js +5 -0
- package/dist/src/protocol/render.d.ts +49 -0
- package/dist/src/protocol/render.js +1 -0
- package/dist/src/protocol/tree.d.ts +95 -0
- package/dist/src/protocol/tree.js +1 -0
- package/dist/src/transport.d.ts +15 -0
- package/dist/src/transport.js +1 -0
- package/dist/src/types.d.ts +4 -0
- package/dist/src/types.js +2 -0
- package/dist/src/websocket-transport.d.ts +75 -0
- package/dist/src/websocket-transport.js +168 -0
- package/package.json +39 -31
- package/AGENTS.md +0 -87
- package/bin/cmux +0 -97
- package/lib/postinstall.js +0 -29
- package/llms.txt +0 -70
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { CmuxErrorDelivery } from "./protocol/common.js";
|
|
2
|
+
export declare class CmuxError extends Error {
|
|
3
|
+
constructor(message: string);
|
|
4
|
+
}
|
|
5
|
+
export declare class CmuxCommandError extends CmuxError {
|
|
6
|
+
readonly commandId: unknown;
|
|
7
|
+
readonly response: unknown;
|
|
8
|
+
readonly errorCode: string | undefined;
|
|
9
|
+
readonly delivery: CmuxErrorDelivery | undefined;
|
|
10
|
+
constructor(message: string, commandId?: unknown, response?: unknown, delivery?: CmuxErrorDelivery);
|
|
11
|
+
constructor(message: string, commandId?: unknown, response?: unknown, errorCode?: string, delivery?: CmuxErrorDelivery);
|
|
12
|
+
}
|
|
13
|
+
export declare class CmuxConnectionError extends CmuxError {
|
|
14
|
+
}
|
|
15
|
+
export declare class CmuxProtocolError extends CmuxError {
|
|
16
|
+
}
|
|
17
|
+
export declare class CmuxTimeoutError extends CmuxError {
|
|
18
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export class CmuxError extends Error {
|
|
2
|
+
constructor(message) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.name = new.target.name;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export class CmuxCommandError extends CmuxError {
|
|
8
|
+
commandId;
|
|
9
|
+
response;
|
|
10
|
+
errorCode;
|
|
11
|
+
delivery;
|
|
12
|
+
constructor(message, commandId, response, errorCode, delivery) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.commandId = commandId;
|
|
15
|
+
this.response = response;
|
|
16
|
+
const legacyDelivery = delivery === undefined
|
|
17
|
+
&& (errorCode === "known-not-delivered" || errorCode === "ambiguous")
|
|
18
|
+
? errorCode
|
|
19
|
+
: undefined;
|
|
20
|
+
this.errorCode = legacyDelivery === undefined ? errorCode : undefined;
|
|
21
|
+
this.delivery = delivery ?? legacyDelivery;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export class CmuxConnectionError extends CmuxError {
|
|
25
|
+
}
|
|
26
|
+
export class CmuxProtocolError extends CmuxError {
|
|
27
|
+
}
|
|
28
|
+
export class CmuxTimeoutError extends CmuxError {
|
|
29
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { CmuxClient, type ClientOptions } from "./node-client.js";
|
|
2
|
+
export { CmuxStream, TERMINAL_KEY_TEXT_MAX_BYTES, type AttachSurfaceOptions, type CmuxClientOptions, type NewBrowserTabOptions, type NewPaneRightOptions, type NewScreenOptions, type NewTabOptions, type NewWorkspaceOptions, type CreateTerminalOptions, type CreateWorkspaceOptions, type CloseWorkspaceOptions, type MoveWorkspaceOptions, type RenameWorkspaceOptions, type ResizeTransactionOptions, type SelectOptions, type SelectTabOptions, type SendOptions, type SplitOptions, type SubscribeOptions, } from "./client.js";
|
|
3
|
+
export * from "./base64.js";
|
|
4
|
+
export * from "./errors.js";
|
|
5
|
+
export * from "./node-transport.js";
|
|
6
|
+
export * from "./protocol/index.js";
|
|
7
|
+
export * from "./transport.js";
|
|
8
|
+
export * from "./websocket-transport.js";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { CmuxClient } from "./node-client.js";
|
|
2
|
+
export { CmuxStream, TERMINAL_KEY_TEXT_MAX_BYTES, } from "./client.js";
|
|
3
|
+
export * from "./base64.js";
|
|
4
|
+
export * from "./errors.js";
|
|
5
|
+
export * from "./node-transport.js";
|
|
6
|
+
export * from "./protocol/index.js";
|
|
7
|
+
export * from "./transport.js";
|
|
8
|
+
export * from "./websocket-transport.js";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { CmuxClient as TransportCmuxClient } from "./client.js";
|
|
2
|
+
import type { Transport } from "./transport.js";
|
|
3
|
+
/** Node.js client configuration, including Unix-socket defaults. */
|
|
4
|
+
export interface ClientOptions {
|
|
5
|
+
socketPath?: string;
|
|
6
|
+
session?: string;
|
|
7
|
+
timeoutMs?: number;
|
|
8
|
+
allowProtocolV6Attach?: boolean;
|
|
9
|
+
/** Overrides the default Unix transport. */
|
|
10
|
+
transport?: Transport;
|
|
11
|
+
/** Overrides dedicated transports used for subscribe and attach streams. */
|
|
12
|
+
streamTransportFactory?: () => Transport;
|
|
13
|
+
}
|
|
14
|
+
/** Node.js cmux client with backward-compatible Unix-socket defaults. */
|
|
15
|
+
export declare class CmuxClient extends TransportCmuxClient {
|
|
16
|
+
readonly socketPath: string;
|
|
17
|
+
constructor(options?: ClientOptions);
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { CmuxClient as TransportCmuxClient } from "./client.js";
|
|
2
|
+
import { defaultSocketPath, envSocketPath, UnixSocketTransport } from "./node-transport.js";
|
|
3
|
+
/** Node.js cmux client with backward-compatible Unix-socket defaults. */
|
|
4
|
+
export class CmuxClient extends TransportCmuxClient {
|
|
5
|
+
socketPath;
|
|
6
|
+
constructor(options = {}) {
|
|
7
|
+
const socketPath = options.socketPath ?? envSocketPath() ?? defaultSocketPath(options.session ?? "main");
|
|
8
|
+
const shared = {
|
|
9
|
+
transport: options.transport ?? new UnixSocketTransport(socketPath),
|
|
10
|
+
timeoutMs: options.timeoutMs,
|
|
11
|
+
allowProtocolV6Attach: options.allowProtocolV6Attach,
|
|
12
|
+
streamTransportFactory: options.streamTransportFactory
|
|
13
|
+
?? (options.transport ? undefined : () => new UnixSocketTransport(socketPath)),
|
|
14
|
+
};
|
|
15
|
+
super(shared);
|
|
16
|
+
this.socketPath = socketPath;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Transport, Unsubscribe } from "./transport.js";
|
|
2
|
+
/** Resolves the default Unix socket path for a session. */
|
|
3
|
+
export declare function defaultSocketPath(session?: string): string;
|
|
4
|
+
/** Reads the current or legacy cmux-tui socket environment variable. */
|
|
5
|
+
export declare function envSocketPath(): string | undefined;
|
|
6
|
+
/** Unix-socket JSON-lines transport for Node.js. */
|
|
7
|
+
export declare class UnixSocketTransport implements Transport {
|
|
8
|
+
readonly socketPath: string;
|
|
9
|
+
private readonly socket;
|
|
10
|
+
private readonly pending;
|
|
11
|
+
private readonly messageHandlers;
|
|
12
|
+
private readonly closeHandlers;
|
|
13
|
+
private readonly errorHandlers;
|
|
14
|
+
private buffer;
|
|
15
|
+
private connected;
|
|
16
|
+
private closed;
|
|
17
|
+
constructor(socketPath: string);
|
|
18
|
+
send(json: string): void;
|
|
19
|
+
onMessage(handler: (json: string) => void): Unsubscribe;
|
|
20
|
+
onClose(handler: () => void): Unsubscribe;
|
|
21
|
+
onError(handler: (error: Error) => void): Unsubscribe;
|
|
22
|
+
close(): void;
|
|
23
|
+
private write;
|
|
24
|
+
private receive;
|
|
25
|
+
private fail;
|
|
26
|
+
private finish;
|
|
27
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import * as net from "node:net";
|
|
2
|
+
import * as os from "node:os";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { CmuxConnectionError } from "./errors.js";
|
|
5
|
+
/** Resolves the default Unix socket path for a session. */
|
|
6
|
+
export function defaultSocketPath(session = "main") {
|
|
7
|
+
const base = process.env.TMPDIR || os.tmpdir();
|
|
8
|
+
return path.join(base, `cmux-tui-${process.getuid?.() ?? 0}`, `${session}.sock`);
|
|
9
|
+
}
|
|
10
|
+
/** Reads the current or legacy cmux-tui socket environment variable. */
|
|
11
|
+
export function envSocketPath() {
|
|
12
|
+
return process.env.CMUX_TUI_SOCKET || process.env.CMUX_MUX_SOCKET;
|
|
13
|
+
}
|
|
14
|
+
/** Unix-socket JSON-lines transport for Node.js. */
|
|
15
|
+
export class UnixSocketTransport {
|
|
16
|
+
socketPath;
|
|
17
|
+
socket;
|
|
18
|
+
pending = [];
|
|
19
|
+
messageHandlers = new Set();
|
|
20
|
+
closeHandlers = new Set();
|
|
21
|
+
errorHandlers = new Set();
|
|
22
|
+
buffer = "";
|
|
23
|
+
connected = false;
|
|
24
|
+
closed = false;
|
|
25
|
+
constructor(socketPath) {
|
|
26
|
+
this.socketPath = socketPath;
|
|
27
|
+
this.socket = net.createConnection({ path: socketPath });
|
|
28
|
+
this.socket.setEncoding("utf8");
|
|
29
|
+
this.socket.on("connect", () => {
|
|
30
|
+
this.connected = true;
|
|
31
|
+
while (this.pending.length > 0)
|
|
32
|
+
this.write(this.pending.shift());
|
|
33
|
+
});
|
|
34
|
+
this.socket.on("data", (chunk) => this.receive(chunk));
|
|
35
|
+
this.socket.on("error", (error) => {
|
|
36
|
+
const prefix = this.connected ? "socket error" : `cannot connect to session socket ${this.socketPath}`;
|
|
37
|
+
this.fail(new CmuxConnectionError(`${prefix}: ${error.message}`));
|
|
38
|
+
});
|
|
39
|
+
this.socket.on("close", () => this.finish());
|
|
40
|
+
}
|
|
41
|
+
send(json) {
|
|
42
|
+
if (this.closed)
|
|
43
|
+
throw new CmuxConnectionError("session socket closed");
|
|
44
|
+
if (this.connected)
|
|
45
|
+
this.write(json);
|
|
46
|
+
else
|
|
47
|
+
this.pending.push(json);
|
|
48
|
+
}
|
|
49
|
+
onMessage(handler) {
|
|
50
|
+
this.messageHandlers.add(handler);
|
|
51
|
+
return () => this.messageHandlers.delete(handler);
|
|
52
|
+
}
|
|
53
|
+
onClose(handler) {
|
|
54
|
+
this.closeHandlers.add(handler);
|
|
55
|
+
if (this.closed)
|
|
56
|
+
queueMicrotask(handler);
|
|
57
|
+
return () => this.closeHandlers.delete(handler);
|
|
58
|
+
}
|
|
59
|
+
onError(handler) {
|
|
60
|
+
this.errorHandlers.add(handler);
|
|
61
|
+
return () => this.errorHandlers.delete(handler);
|
|
62
|
+
}
|
|
63
|
+
close() {
|
|
64
|
+
if (!this.closed)
|
|
65
|
+
this.socket.destroy();
|
|
66
|
+
}
|
|
67
|
+
write(json) {
|
|
68
|
+
this.socket.write(`${json}\n`, "utf8", (error) => {
|
|
69
|
+
if (error)
|
|
70
|
+
this.fail(new CmuxConnectionError(`socket write failed: ${error.message}`));
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
receive(chunk) {
|
|
74
|
+
this.buffer += chunk;
|
|
75
|
+
for (;;) {
|
|
76
|
+
const index = this.buffer.indexOf("\n");
|
|
77
|
+
if (index < 0)
|
|
78
|
+
return;
|
|
79
|
+
const line = this.buffer.slice(0, index);
|
|
80
|
+
this.buffer = this.buffer.slice(index + 1);
|
|
81
|
+
if (line.trim() === "")
|
|
82
|
+
continue;
|
|
83
|
+
for (const handler of this.messageHandlers)
|
|
84
|
+
handler(line);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
fail(error) {
|
|
88
|
+
for (const handler of this.errorHandlers)
|
|
89
|
+
handler(error);
|
|
90
|
+
}
|
|
91
|
+
finish() {
|
|
92
|
+
if (this.closed)
|
|
93
|
+
return;
|
|
94
|
+
this.closed = true;
|
|
95
|
+
this.pending.length = 0;
|
|
96
|
+
for (const handler of this.closeHandlers)
|
|
97
|
+
handler();
|
|
98
|
+
}
|
|
99
|
+
}
|