forked from kojibai/SigilMarkets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
149 lines (129 loc) · 4.42 KB
/
Copy pathvite.config.ts
File metadata and controls
149 lines (129 loc) · 4.42 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { defineConfig, type Connect, type Plugin } from "vite";
import react from "@vitejs/plugin-react";
import { VitePWA } from "vite-plugin-pwa";
import sigilProofHandler from "./api/proof/sigil.js";
import sigilSealHandler from "./api/sigils/seal.js";
import sigilIdHandler from "./api/sigils/[id].js";
const USE_EXTERNAL_PROOF_API = process.env.SIGIL_PROOF_API === "external";
const PROOF_API_TARGET = process.env.SIGIL_PROOF_API_URL ?? "http://localhost:8787";
const SIGIL_API_TARGET = process.env.SIGIL_API_URL ?? "http://localhost:8787";
const USE_LOCAL_SIGIL_API = !process.env.SIGIL_API_URL;
function sigilProofApi(): Plugin {
const handler: Connect.NextHandleFunction = (req, res, next) => {
void (async () => {
// allow preflight
if ((req.method ?? "GET").toUpperCase() === "OPTIONS") {
res.statusCode = 204;
res.end();
return;
}
if ((req.method ?? "GET").toUpperCase() !== "POST") {
res.statusCode = 405;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ error: "Method not allowed" }));
return;
}
try {
await sigilProofHandler(req as unknown, res as unknown);
if (!res.writableEnded) next();
} catch (error) {
if (!res.writableEnded) {
res.statusCode = 500;
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify({ error: "Proof handler failed" }));
}
next(error);
}
})().catch(next);
};
return {
name: "sigil-proof-api",
configureServer(server) {
server.middlewares.use("/api/proof/sigil", handler);
},
configurePreviewServer(server) {
server.middlewares.use("/api/proof/sigil", handler);
},
};
}
function localSigilApi(): Plugin {
const handler: Connect.NextHandleFunction = (req, res, next) => {
void (async () => {
const url = new URL(req.url ?? "/", "http://localhost");
const p = url.pathname;
// support both /sigils/* and /api/sigils/*
if (p === "/sigils/seal" || p === "/api/sigils/seal") {
await sigilSealHandler(req as unknown, res as unknown);
if (!res.writableEnded) next();
return;
}
const m = p.match(/^\/(?:api\/)?sigils\/([^/]+)\.svg$/);
if (m) {
(req as Connect.IncomingMessage & { query?: Record<string, string> }).query = { id: m[1] };
await sigilIdHandler(req as unknown, res as unknown);
if (!res.writableEnded) next();
return;
}
next();
})().catch(next);
};
return {
name: "local-sigil-api",
configureServer(server) {
server.middlewares.use(handler);
},
configurePreviewServer(server) {
server.middlewares.use(handler);
},
};
}
export default defineConfig(() => {
const proxy: Record<string, { target: string; changeOrigin: boolean; rewrite?: (path: string) => string }> = {};
// Only proxy sigils if we are NOT running local sigil api
if (!USE_LOCAL_SIGIL_API) {
proxy["/sigils"] = { target: SIGIL_API_TARGET, changeOrigin: true };
proxy["/api/sigils"] = { target: SIGIL_API_TARGET, changeOrigin: true };
}
// Only proxy proof if we are using external proof api
if (USE_EXTERNAL_PROOF_API) {
proxy["/api/proof/sigil"] = { target: PROOF_API_TARGET, changeOrigin: true };
}
const proxyConfig = Object.keys(proxy).length ? { proxy } : undefined;
return {
resolve: {
alias: {
html2canvas: "/src/shims/html2canvas.ts",
},
},
server: proxyConfig,
preview: proxyConfig,
plugins: [
react(),
VitePWA({
registerType: "autoUpdate",
includeAssets: ["verahai-icon.svg", "phi.svg"],
manifest: {
name: "Verahai · SigilMarkets",
short_name: "Verahai",
description: "SigilMarkets for Kairos glyph prophecy markets.",
theme_color: "#0f1115",
background_color: "#0f1115",
display: "standalone",
start_url: "/",
icons: [
{
src: "/verahai-icon.svg",
sizes: "any",
type: "image/svg+xml",
purpose: "maskable any",
},
],
},
}),
// Local proof endpoint unless explicitly external
...(USE_EXTERNAL_PROOF_API ? [] : [sigilProofApi()]),
// Local sigil endpoints unless SIGIL_API_URL is set
...(USE_LOCAL_SIGIL_API ? [localSigilApi()] : []),
],
};
});