#!/usr/bin/env node // E2E probe: open the DSH Remote stream WebSocket of a device THROUGH the local // /r//api/remote.mux proxy and check that the remote DSH answers. // node deploy/e2e-ws.mjs [port] import fs from 'node:fs' import os from 'node:os' import path from 'node:path' import { connect } from '../lib/ws.mjs' import { mintCookie } from '../lib/auth-cookie.mjs' const device = process.argv[2] || 'ai-pc' const port = process.argv[3] || process.env.DSH_PORT || '3081' const home = process.env.DSH_HOME || path.join(os.homedir(), '.dsh') const secret = fs.readFileSync(path.join(home, '.credentials.yaml'), 'utf8').match(/browser-session:[\s\S]*?secret:\s*([A-Za-z0-9_-]{43})/)[1] const authority = `127.0.0.1:${port}` const cookie = mintCookie(authority, secret, 1) const t0 = Date.now() const ws = await connect(`ws://${authority}/r/${device}/api/remote.mux`, { headers: { cookie, origin: `http://${authority}` } }) console.log(`ws open through /r/${device}/ in ${Date.now() - t0} ms`) const got = await new Promise((resolve) => { const timer = setTimeout(() => resolve('(no frame within 4s: connection stays open, server waits for client frames)'), 4000) ws.on('message', (m) => { clearTimeout(timer); resolve(`first frame: ${String(m).slice(0, 160)}`) }) ws.on('close', (c, r) => { clearTimeout(timer); resolve(`closed ${c} ${r}`) }) }) console.log(got) console.log(`still open: ${ws.open}`) ws.close() process.exit(0)