Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

2.4 KB
Raw Blame
import { execFileSync } from 'node:child_process';
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { ServerOptions } from 'node:https';
import type { Plugin } from 'vite';

/** Standalone browser development shares the local stack's stable CA. */
export function devHttps(
  env: Record<string, string | undefined> = process.env
): Plugin {
  let hostname: string;
  const caDir = fileURLToPath(
    new URL('../../../infra/local/certs/', import.meta.url)
  );

  return {
    name: 'macro-dev-https',
    apply: (_config, { command, isPreview }) =>
      command === 'serve' &&
      !isPreview &&
      !env.MACRO_LOCAL_BACKEND_PROXY &&
      !env.TAURI_ENV_PLATFORM &&
      !env.TAURI_DEV_HOST &&
      env.MACRO_DEV_HTTPS !== 'false',
    config() {
      hostname = execFileSync('hostname', { encoding: 'utf8' })
        .trim()
        .toLowerCase();
      const directory = mkdtempSync(join(tmpdir(), 'macro-vite-tls-'));
      try {
        execFileSync('bash', [
          join(caDir, 'issue-host.sh'),
          directory,
          hostname,
        ]);
        return {
          server: {
            https: {
              cert: readFileSync(join(directory, 'server.pem')),
              key: readFileSync(join(directory, 'server-key.pem')),
              // Reloading mid-load cancels thousands of module streams at
              // once. Node's HTTP/2 rapid-reset guard (1000 resets per burst)
              // then tears down the session and the new page loads blank.
              streamResetBurst: 1_000_000,
              streamResetRate: 1_000_000,
            } as ServerOptions,
            // Tailscale Serve proxies a remote devbox over HTTP/2 under its
            // MagicDNS name; Vite's HMR socket rejects hosts not listed here.
            allowedHosts: [hostname, '.ts.net'],
          },
        };
      } finally {
        rmSync(directory, { recursive: true, force: true });
      }
    },
    configureServer(server) {
      server.httpServer?.once('listening', () => {
        const address = server.httpServer?.address();
        if (!address || typeof address === 'string') return;
        server.config.logger.info(
          `\n  App: https://${hostname}:${address.port}/app/\n` +
            `  Trust ${join(caDir, 'ca.pem')} once in the visiting browser.\n`
        );
      });
    },
  };
}