File size: 2,486 Bytes
aacc29a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { pathToFileURL } from "node:url";

export function validateDeployEnv(env = process.env) {
  const errors = [];
  const parse = (name, { allowEmpty = false } = {}) => {
    const value = env[name]?.trim();
    if (!value) {
      if (!allowEmpty) errors.push(`${name} is required.`);
      return undefined;
    }
    try {
      const url = new URL(value);
      if (url.protocol !== "https:") {
        errors.push(`${name} must use HTTPS.`);
      }
      if (url.search || url.hash) {
        errors.push(`${name} must not include a query or fragment.`);
      }
      return url;
    } catch {
      errors.push(`${name} must be a valid URL.`);
      return undefined;
    }
  };

  // VITE_CONSOLE_URL was dropped with the interpreter console: it pointed at
  // /phien-dich-y-khoa/, which is now a deliberate 404.
  //
  // Empty is now valid and is the preferred production value: vercel.json
  // rewrites /api/* to the API host, so the browser calls its own origin and no
  // CORS allow-list participates. Requiring an absolute origin here is what let
  // the domain move to carepath-medicaltranslation.vercel.app while the Space
  // still allowed only carepath-omega.vercel.app, taking both tools down with
  // 400 Disallowed CORS origin. An absolute value stays valid for the combined
  // HF Space build and for local dev against a remote API.
  const apiBase = parse("VITE_API_BASE", { allowEmpty: true });
  if (apiBase && apiBase.pathname !== "/") {
    errors.push("VITE_API_BASE pathname must be /.");
  }

  // A websocket upgrade does not survive a Vercel rewrite to an external host,
  // so /ws/* goes direct and needs an absolute origin whenever the HTTP side is
  // same-origin. Empty is only valid when VITE_API_BASE names a host to inherit.
  const wsBase = parse("VITE_WS_BASE", { allowEmpty: true });
  if (wsBase && wsBase.pathname !== "/") {
    errors.push("VITE_WS_BASE pathname must be /.");
  }
  if (!wsBase && !apiBase) {
    errors.push(
      "VITE_WS_BASE is required when VITE_API_BASE is empty: the websocket cannot be proxied same-origin.",
    );
  }
  if (errors.length) {
    throw new Error(`Invalid Vercel deployment environment:\n- ${errors.join("\n- ")}`);
  }
}

if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
  try {
    validateDeployEnv();
    console.log("Vercel deployment environment is valid.");
  } catch (error) {
    console.error(error.message);
    process.exitCode = 1;
  }
}