refactor: migrate from clientSessionKey to clientId identity
This commit is contained in:
+12
-12
@@ -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;
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user