Skip to content

All your computers and your phone/iPhone, working as one desk. Screens, apps, files, clipboard, calls, texts, notifications, mouse and keyboard, shared between Mac, Windows, Linux , Android, Web and ios / ipados . Free and open source. No account, no cloud, and no shared network needed.

3.9 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Terminal nicknames in Multi-device: the name each computer of the mesh goes by in `ssh twindesk-<name>`,
// set on any computer's Terminal page and the same on all of them. The list is the mesh's
// (`term.names {list: {id: nickname}, at}` in a pool, the newest wins, kept in settings.termNames); a
// computer without a nickname goes by its own name (sshconfig.js aliasFor). Two computers never share an
// alias: a nickname another computer already goes by is refused.
//
//   const n = termNames({ bus, mesh, selfId, settings(), save(), isPhone(id), names() → {id: device name}, onChange(id[]) })
//   n.nick(id)          its nickname ('' none)
//   n.alias(id, name)   twindesk-<nickname, else its name>
//   n.set(id, nick)     → {ok, why?}  ('' clears it); why: taken, bad
//   n.view()            {id: nickname}
'use strict';

const { Pool } = require('./mesh.js');
const SshConfig = require('./sshconfig.js');

const T = 'term.names';
const MAX = 32;
const ID = /^[A-Za-z0-9_-]{1,64}$/;

// A nickname as kept: letters, digits, spaces, dots and dashes, at most 30.
function clean(v) {
  return typeof v === 'string' ? v.replace(/[^\p{L}\p{N} ._-]/gu, '').replace(/\s+/g, ' ').trim().slice(0, 30) : '';
}

function cleanList(list) {
  const out = {};
  if (!list || typeof list !== 'object') return out;
  for (const [id, v] of Object.entries(list)) {
    const n = clean(v);
    if (ID.test(id) && n) out[id] = n;
    if (Object.keys(out).length >= MAX) break;
  }
  return out;
}

class TermNames {
  constructor(o) {
    Object.assign(this, { onChange: () => {}, names: () => ({}) }, o);
    this.pool = new Pool({ name: 'termnames', selfId: this.selfId, peers: () => this.mesh.peers().filter((e) => e.mesh && !this.isPhone(e.id)).map((e) => ({ id: e.id, mesh: true, pool: true, send: (msg) => e.send(msg) })) });
    this.bus.on(T, (m, from) => this._heard(m, from));
    this.bus.onPeer('welcome', () => this._publish());
  }

  get cfg() {
    const s = this.settings();
    if (!s.termNames || typeof s.termNames !== 'object') s.termNames = { list: {}, at: 0 };
    return s.termNames;
  }

  nick(id) {
    return cleanList(this.cfg.list)[id] || '';
  }

  alias(id, name) {
    return SshConfig.aliasFor(this.nick(id) || name, id);
  }

  view() {
    return cleanList(this.cfg.list);
  }

  set(id, nick) {
    if (!ID.test(String(id || ''))) return { ok: false, why: 'bad' };
    const n = clean(nick);
    if (nick && !n) return { ok: false, why: 'bad' };
    const list = cleanList(this.cfg.list);
    if (n) {
      const want = SshConfig.aliasFor(n, id);
      const names = this.names() || {};
      const ids = new Set([...Object.keys(names), ...Object.keys(list)]);
      for (const other of ids) if (other !== id && SshConfig.aliasFor(list[other] || names[other], other) === want) return { ok: false, why: 'taken' };
      list[id] = n;
    } else delete list[id];
    const was = cleanList(this.cfg.list);
    this.settings().termNames = { list, at: Math.max(Date.now(), (this.cfg.at || 0) + 1) };
    this.save();
    this._publish();
    this.onChange(this._diff(was, list));
    return { ok: true };
  }

  _diff(a, b) {
    return [...new Set([...Object.keys(a), ...Object.keys(b)])].filter((id) => a[id] !== b[id]);
  }

  _publish() {
    const g = this.cfg;
    if (g.at) this.pool.publish({ t: T, list: cleanList(g.list), at: g.at });
  }

  _heard(m, from) {
    if (this.isPhone(from)) return;
    const r = this.pool.receive(from, m);
    if (!r.fresh || !r.msg || r.msg.o === this.selfId) return;
    const at = Number(m.at) || 0;
    const g = this.cfg;
    if (at <= (g.at || 0)) { if (at < g.at) this._publish(); return; } // ours is newer: theirs catches up
    const was = cleanList(g.list);
    const list = cleanList(m.list);
    this.settings().termNames = { list, at };
    this.save();
    this.onChange(this._diff(was, list));
  }
}

module.exports = { termNames: (o) => new TermNames(o), TermNames, clean, T };