154 lines · 5.8 KB
/* global window, document */
// Runs on the editor origin before VS Code loads. It waits for the forge page to hand over a ref-scoped
// session over postMessage, then boots the Code - OSS workbench with the gitfor-fs extension. The token
// only ever travels by postMessage between exact origins; there are no cookies on this origin.
(function boot() {
  "use strict";
  var config = JSON.parse(document.getElementById("gitfor-config").textContent);
  var session = null;
  var waiting = [];
  var load = window.crypto.randomUUID();

  function post(message) {
    window.parent.postMessage(message, config.forgeOrigin);
  }

  function sessionInfo() {
    return Object.assign({}, session, { forgeOrigin: config.forgeOrigin });
  }

  function refresh() {
    return new Promise(function (resolve) {
      waiting.push(resolve);
      post({ type: "gitfor-editor:refresh" });
    });
  }

  function loadScript(src) {
    return new Promise(function (resolve, reject) {
      var script = document.createElement("script");
      script.src = src;
      script.onload = resolve;
      script.onerror = function () {
        reject(new Error("Failed to load " + src));
      };
      document.body.appendChild(script);
    });
  }

  function start() {
    var base = config.assetBase;