refactor: migrate from clientSessionKey to clientId identity

This commit is contained in:
Mohamed Boudra
2026-02-21 15:33:29 +07:00
parent e906bffbb9
commit c3c8103563
56 changed files with 3289 additions and 941 deletions
@@ -5,31 +5,31 @@ import { homedir } from "node:os";
const CLIENT_SESSION_KEY_FILE = join(
process.env.PASEO_HOME ?? join(homedir(), ".paseo"),
"cli-client-session-key"
"cli-client-id"
);
let cachedClientSessionKey: string | null = null;
let cachedClientId: string | null = null;
function normalizeClientSessionKey(value: string): string | null {
function normalizeClientId(value: string): string | null {
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : null;
}
function generateClientSessionKey(): string {
return `clsk_${randomUUID().replace(/-/g, "")}`;
function generateClientId(): string {
return `cid_${randomUUID().replace(/-/g, "")}`;
}
export async function getOrCreateCliClientSessionKey(): Promise<string> {
if (cachedClientSessionKey) {
return cachedClientSessionKey;
export async function getOrCreateCliClientId(): Promise<string> {
if (cachedClientId) {
return cachedClientId;
}
try {
const existing = normalizeClientSessionKey(
const existing = normalizeClientId(
await readFile(CLIENT_SESSION_KEY_FILE, "utf8")
);
if (existing) {
cachedClientSessionKey = existing;
cachedClientId = existing;
return existing;
}
} catch (error) {
@@ -39,9 +39,9 @@ export async function getOrCreateCliClientSessionKey(): Promise<string> {
}
}
const nextValue = generateClientSessionKey();
const nextValue = generateClientId();
await mkdir(dirname(CLIENT_SESSION_KEY_FILE), { recursive: true });
await writeFile(CLIENT_SESSION_KEY_FILE, nextValue, { mode: 0o600 });
cachedClientSessionKey = nextValue;
cachedClientId = nextValue;
return nextValue;
}
+12 -9
View File
@@ -1,6 +1,6 @@
import { DaemonClient } from '@getpaseo/server'
import WebSocket from 'ws'
import { getOrCreateCliClientSessionKey } from './client-session-key.js'
import { getOrCreateCliClientId } from './client-id.js'
export interface ConnectOptions {
host?: string
@@ -40,15 +40,18 @@ function createNodeWebSocketFactory() {
export async function connectToDaemon(options?: ConnectOptions): Promise<DaemonClient> {
const host = getDaemonHost(options)
const timeout = options?.timeout ?? DEFAULT_TIMEOUT
const clientSessionKey = await getOrCreateCliClientSessionKey()
const encodedSessionKey = encodeURIComponent(clientSessionKey)
const url = `ws://${host}/ws?clientSessionKey=${encodedSessionKey}`
const clientId = await getOrCreateCliClientId()
const url = `ws://${host}/ws`
const client = new DaemonClient({
url,
webSocketFactory: createNodeWebSocketFactory(),
reconnect: { enabled: false },
})
const client = new DaemonClient(
{
url,
clientId,
clientType: 'cli',
webSocketFactory: createNodeWebSocketFactory(),
reconnect: { enabled: false },
} as unknown as ConstructorParameters<typeof DaemonClient>[0]
)
// Connect with timeout
const connectPromise = client.connect()