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.
JavaScript androidapple-ecosystemconnectelectronfile-sharingintel-unison-alternativeiosios-windowskde-connect-alternativekvmlinuxmacbookmacosmouse-sharingphone-link-alternativeproductivityscreen-sharingsidecar-alternativesynergy-alternativewindows
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Terminal task alerts (Local API, docs/local-api.md): a command run through twindesk-notify on
// this computer tells the connected phones that asked for it (task.alert.policy {on}) when it
// ends. Only the label and success or failure go to the phone, never the command or its output.
// Mac, Windows and Linux, one-to-one and Multi-device. Each phone's permission belongs to the
// socket it came on: a closed or replaced socket drops it, and with no phone left the watches go.
'use strict';
const { randomBytes } = require('node:crypto');
const { connectionOf } = require('./asked-send');
const { T, isPhoneOs } = require('./protocol');
const MAX_TASKS = 32;
const MAX_AGE = 24 * 60 * 60 * 1000;
const ID = /^[a-f0-9]{32}$/;
const failure = (code, error) => ({ code, body: { ok: false, error } });
const success = (fields) => ({ code: 200, body: { ok: true, ...fields } });
class TaskAlerts {
// source: the server the phones are on. It emits 'message' (msg, ctx, ws), or with typed: true
// `t:<type>` (msg, ctx, ws) as the Mac's one-to-one peer does, and 'closed' (ctx).
// send(ctx, msg) defaults to source.send. socketOf(ctx): the socket a permission must have come
// on (the Mac's peer.outbound); without it, the session's own socket or its fallback.
constructor(source = null, opts = {}) {
this.source = null;
this.owners = new Map(); // deviceId → {ctx, ws}
this.tasks = new Map();
if (source) this.attach(source, opts);
}
// The server can come later than the Local API (Windows and Linux make it once the identity is in).
attach(source, { typed = false, send, socketOf } = {}) {
this.source = source;
this.send = send || ((ctx, msg) => source.send(ctx, msg));
this.socketOf = socketOf || null;
const onPolicy = (msg, ctx, ws) => this._policy(msg, ctx, ws);
if (typed) source.on(`t:${T.TASK_ALERT_POLICY}`, onPolicy);
else source.on('message', (msg, ctx, ws) => { if (msg && msg.t === T.TASK_ALERT_POLICY) onPolicy(msg, ctx, ws); });
for (const event of ['client', 'closed', 'transport']) source.on(event, () => this._prune());
return this;
}
_policy(msg, ctx, ws) {
if (!msg || typeof msg.on !== 'boolean' || !this.live(ctx, ws)) return;
const had = this.owners.get(ctx.deviceId);
if (msg.on) this.owners.set(ctx.deviceId, { ctx, ws });
else this.owners.delete(ctx.deviceId);
if (had && (had.ws !== ws || !msg.on)) this._prune(true);
else this._prune();
}
// Still the authenticated phone session on the socket the permission came on.
live(ctx, ws) {
if (!ctx || !isPhoneOs(ctx.os) || !ws || ws.detached) return false;
const ok = this.socketOf ? ws === this.socketOf(ctx) : (ws === ctx.ws || ws === ctx.alt);
return ok && !!connectionOf(ctx, ws);
}
// A phone that went (or changed its mind, or came on another socket): with none left, no watch
// survives for a later session to inherit.
_prune(changed = false) {
for (const [id, o] of this.owners) if (!this.live(o.ctx, o.ws)) { this.owners.delete(id); changed = true; }
if (changed && !this.owners.size) this.tasks.clear();
}
get phones() { this._prune(); return this.owners.size; }
clear() { this.tasks.clear(); }
start(body) {
const title = typeof body.title === 'string' ? body.title.trim() : '';
if (!title || title.length > 80 || /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u.test(title)) {
return failure(400, 'Invalid task label');
}
if (!this.phones) return failure(409, 'Connect a phone and turn on Task alerts on it');
const now = Date.now();
for (const [id, task] of this.tasks) if (task.until <= now) this.tasks.delete(id);
if (this.tasks.size >= MAX_TASKS) return failure(429, 'Too many pending task alerts');
const id = randomBytes(16).toString('hex');
this.tasks.set(id, { title, until: now + MAX_AGE });
return success({ id });
}
finish(body) {
const { id, ok } = body;
if (typeof id !== 'string' || !ID.test(id) || typeof ok !== 'boolean') return failure(400, 'Invalid task alert');
const task = this.tasks.get(id);
this.tasks.delete(id);
if (!task || task.until <= Date.now() || !this.phones) return failure(409, 'Task alert session ended; start a new watch');
for (const o of this.owners.values()) this.send(o.ctx, { t: T.TASK_ALERT, id, title: task.title, ok });
return success();
}
cancel(body) {
const { id } = body;
if (typeof id !== 'string' || !ID.test(id)) return failure(400, 'Invalid task alert');
this.tasks.delete(id);
return success();
}
}
module.exports = { TaskAlerts };