main
anastasia Premiere Pro Bridge 0.1.0: Rust bridge, UXP plugin, CEP helper, skills, macOS manifest 32fb583 10d ago
// Adom Bridge CEP helper — invisible; dials the Rust bridge's WebSocket and runs
// ExtendScript (host.jsx) for the verbs UXP can't do. Same protocol as the UXP plugin.
(function () {
  "use strict";
  var HELPER_VERSION = "0.1.0";
  var nodeRequire = typeof require !== "undefined" ? require : window.cep_node && window.cep_node.require;
  var fs = nodeRequire("fs");
  var os = nodeRequire("os");
  var path = nodeRequire("path");

  var ws = null;
  var backoff = 1000;

  function evalScript(script) {
    return new Promise(function (resolve) {
      window.__adobe_cep__.evalScript(script, function (res) {
        resolve(res);
      });
    });
  }

  function readConnection() {
    try {
      var p = path.join(os.homedir(), ".adom", "premiere-bridge", "connection.json");
      var j = JSON.parse(fs.readFileSync(p, "utf8"));
      return j && j.port && j.token ? j : null;
    } catch (e) {
      return null;
    }
  }

  function send(obj) {
    if (ws && ws.readyState === 1) ws.send(JSON.stringify(obj));
  }

  function connect() {
    if (ws && ws.readyState <= 1) return;
    var conn = readConnection();
    if (!conn) return retry();
    var sock = new WebSocket("ws://127.0.0.1:" + conn.port + "/ws?token=" + encodeURIComponent(conn.token));
    ws = sock;
    sock.onopen = function () {
      backoff = 1000;
      evalScript("adom_hello()").then(function (res) {
        var info = {};
        try {
          info = JSON.parse(res);
        } catch (e) {}
        info.type = "hello";
        info.runtime = "cep";
        info.helperVersion = HELPER_VERSION;
        info.protocol = 1;
        send(info);
      });
    };
    sock.onmessage = function (ev) {
      var msg;
      try {
        msg = JSON.parse(ev.data);
      } catch (e) {
        return;
      }
      if (msg.type === "ping") return send({ type: "pong" });
      if (msg.type !== "call") return;
      var payload = JSON.stringify(JSON.stringify({ verb: msg.verb, args: msg.args || {} }));
      evalScript("adom_dispatch(" + payload + ")").then(function (res) {
        var out;
        try {
          out = JSON.parse(res);
        } catch (e) {
          out = { ok: false, error: "ExtendScript returned non-JSON: " + String(res).slice(0, 500), errorCode: "extendscript_error" };
        }
        out.type = "result";
        out.id = msg.id;
        send(out);
      });
    };
    sock.onclose = function () {
      if (ws === sock) ws = null;
      retry();
    };
  }

  function retry() {
    setTimeout(connect, backoff);
    backoff = Math.min(backoff * 2, 5000);
  }

  connect();
})();