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.

4.5 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// The full Android build (TwinDesk-<version>-full.apk, notification access), which the computers
// install over the phone's TwinDesk through wireless debugging. It isn't in the installers any more
// (30 MB in each of them, and every desktop build had to wait for Android's): Install downloads it
// from this version's release, as the iPhone's IPA is (shared/iossign.js), checks it against the
// SHA-256 GitHub gives for the file, and keeps it, so the next install needs no network.
// A copy next to the app (vendor/, an older installer or a build made with one) or the checkout's
// android/dist comes first.
'use strict';

const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const { digestOf } = require('./selfupdate.js');

const REPO = 'InukaAsith/TwinDesk';
const NAME = (version) => `TwinDesk-${version}-full.apk`;

// A copy that's already here: vendor/ (in the app) or the Android build's dist/ (a checkout).
function localApk(vendor, dist) {
  if (vendor && fs.existsSync(vendor)) return vendor;
  try {
    const f = fs.readdirSync(dist).filter((n) => /-full\.apk$/.test(n)).sort().pop();
    return f ? path.join(dist, f) : null;
  } catch {
    return null;
  }
}

// The APK's path: the local copy, the kept download, or a new download (onProgress(fraction)).
// Throws with a message to show when it can't be had.
async function fullApk({ vendor, dist, version, cacheDir, fetch = globalThis.fetch, onProgress } = {}) {
  const here = localApk(vendor, dist);
  if (here) return here;
  if (!version || !cacheDir) throw new Error('this build of TwinDesk doesn’t know its version');
  const name = NAME(version);
  const file = path.join(cacheDir, name);
  if (fs.existsSync(file)) return file; // checked when it was downloaded (renamed into place only then)
  const head = { 'User-Agent': 'TwinDesk', Accept: 'application/vnd.github+json' };
  const rel = await fetch(`https://api.github.com/repos/${REPO}/releases/tags/v${version}`, { headers: head });
  if (rel.status === 404) throw new Error(`TwinDesk ${version} isn’t released on GitHub yet`);
  if (!rel.ok) throw new Error(`GitHub didn’t answer (${rel.status})`);
  const asset = ((await rel.json()).assets || []).find((a) => a && a.name === name);
  if (!asset) throw new Error(`the ${version} release has no ${name}`);
  const want = digestOf(asset);
  const res = await fetch(asset.browser_download_url, { headers: { 'User-Agent': 'TwinDesk' }, redirect: 'follow' });
  if (!res.ok || !res.body) throw new Error(`the download failed (${res.status})`);
  fs.mkdirSync(cacheDir, { recursive: true });
  // Older versions' copies go: one at a time.
  for (const f of fs.readdirSync(cacheDir)) if (/-full\.apk(\.part)?$/.test(f)) { try { fs.rmSync(path.join(cacheDir, f), { force: true }); } catch {} }
  const part = `${file}.part`;
  const total = Number(res.headers.get('content-length')) || asset.size || 0;
  const out = fs.createWriteStream(part);
  const hash = crypto.createHash('sha256');
  let got = 0;
  let said = 0;
  try {
    const reader = res.body.getReader();
    for (;;) {
      const { done, value } = await reader.read();
      if (done) break;
      hash.update(value);
      got += value.length;
      if (!out.write(value)) await new Promise((r) => out.once('drain', r));
      if (total && onProgress && got - said > total / 100) { said = got; onProgress(Math.min(1, got / total)); }
    }
    await new Promise((resolve, reject) => out.end((e) => (e ? reject(e) : resolve())));
  } catch (e) {
    out.destroy();
    try { fs.rmSync(part, { force: true }); } catch {}
    throw e;
  }
  if ((want && hash.digest('hex') !== want) || (!want && asset.size && got !== asset.size)) {
    fs.rmSync(part, { force: true });
    throw new Error('the download didn’t match the release’s file');
  }
  fs.renameSync(part, file);
  return file;
}

// For the phone bridges: this app's version and where downloads are kept (Electron's userData).
// The version: Electron's (the app's package.json, as packaged), else the platform folder's.
function appContext(appDir) {
  let version = '';
  let cacheDir = '';
  try {
    const { app } = require('electron');
    version = app.getVersion();
    cacheDir = path.join(app.getPath('userData'), 'android');
  } catch {}
  if (!/^\d+\.\d+\.\d+/.test(version || '')) {
    try { version = require(path.join(appDir, 'package.json')).version; } catch {}
  }
  return { version, cacheDir };
}

module.exports = { fullApk, localApk, appContext, NAME };