@lukoweb/apitogo 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/dist/cli/cli.js +37 -147
  2. package/dist/declarations/lib/ui/Command.d.ts +1 -1
  3. package/dist/declarations/lib/util/MdxComponents.d.ts +1 -1
  4. package/package.json +1 -1
  5. package/src/app/main.tsx +150 -150
  6. package/src/config/loader.ts +245 -245
  7. package/src/index.ts +33 -33
  8. package/src/lib/authentication/components/CallbackHandler.tsx +45 -45
  9. package/src/lib/authentication/components/SignIn.tsx +46 -46
  10. package/src/lib/authentication/components/SignUp.tsx +48 -48
  11. package/src/lib/authentication/providers/clerk.tsx +224 -224
  12. package/src/lib/authentication/ui/AuthCard.tsx +7 -7
  13. package/src/lib/authentication/ui/EmailLinkCallbackUi.tsx +129 -125
  14. package/src/lib/authentication/ui/EmailLinkSentUi.tsx +105 -101
  15. package/src/lib/authentication/ui/EmailLinkSignInUi.tsx +100 -96
  16. package/src/lib/authentication/ui/EmailVerificationUi.tsx +128 -124
  17. package/src/lib/authentication/ui/ZudokuAuthUi.tsx +620 -616
  18. package/src/lib/components/AiAssistantMenuItems.tsx +102 -102
  19. package/src/lib/components/Autocomplete.tsx +123 -123
  20. package/src/lib/components/DeveloperHint.tsx +39 -35
  21. package/src/lib/components/Header.tsx +260 -260
  22. package/src/lib/components/Layout.tsx +53 -53
  23. package/src/lib/components/Main.tsx +51 -51
  24. package/src/lib/components/Mermaid.tsx +74 -70
  25. package/src/lib/components/MobileTopNavigation.tsx +276 -276
  26. package/src/lib/components/MultiSelect.tsx +86 -82
  27. package/src/lib/components/TopNavigation.tsx +103 -103
  28. package/src/lib/components/index.ts +26 -26
  29. package/src/lib/components/navigation/NavigationCategory.tsx +157 -157
  30. package/src/lib/components/navigation/NavigationFilterInput.tsx +35 -35
  31. package/src/lib/components/navigation/NavigationItem.tsx +181 -177
  32. package/src/lib/core/RouteGuard.tsx +193 -193
  33. package/src/lib/errors/ErrorMessage.tsx +50 -46
  34. package/src/lib/plugins/api-catalog/Catalog.tsx +80 -80
  35. package/src/lib/plugins/api-keys/CreateApiKey.tsx +128 -124
  36. package/src/lib/plugins/api-keys/CreateApiKeyDialog.tsx +49 -49
  37. package/src/lib/plugins/api-keys/SettingsApiKeys.tsx +72 -72
  38. package/src/lib/plugins/api-keys/settings/ApiKeyItem.tsx +342 -342
  39. package/src/lib/plugins/api-keys/settings/RevealApiKey.tsx +127 -127
  40. package/src/lib/plugins/markdown/MdxPage.tsx +285 -285
  41. package/src/lib/plugins/openapi/ApiHeader.tsx +96 -96
  42. package/src/lib/plugins/openapi/CollapsibleCode.tsx +88 -88
  43. package/src/lib/plugins/openapi/DownloadSchemaButton.tsx +94 -94
  44. package/src/lib/plugins/openapi/GeneratedExampleSidecarBox.tsx +52 -52
  45. package/src/lib/plugins/openapi/OperationListItem.tsx +183 -183
  46. package/src/lib/plugins/openapi/RequestBodySidecarBox.tsx +63 -63
  47. package/src/lib/plugins/openapi/ResponsesSidecarBox.tsx +120 -117
  48. package/src/lib/plugins/openapi/SchemaInfo.tsx +344 -344
  49. package/src/lib/plugins/openapi/SchemaList.tsx +113 -113
  50. package/src/lib/plugins/openapi/Sidecar.tsx +312 -309
  51. package/src/lib/plugins/openapi/SidecarExamples.tsx +177 -174
  52. package/src/lib/plugins/openapi/components/EnumValues.tsx +58 -58
  53. package/src/lib/plugins/openapi/components/ResponseContent.tsx +117 -114
  54. package/src/lib/plugins/openapi/playground/BodyPanel.tsx +274 -271
  55. package/src/lib/plugins/openapi/playground/CollapsibleHeader.tsx +51 -51
  56. package/src/lib/plugins/openapi/playground/ExamplesDropdown.tsx +60 -60
  57. package/src/lib/plugins/openapi/playground/Headers.tsx +188 -181
  58. package/src/lib/plugins/openapi/playground/IdentityDialog.tsx +75 -75
  59. package/src/lib/plugins/openapi/playground/IdentitySelector.tsx +38 -38
  60. package/src/lib/plugins/openapi/playground/ParamsGrid.tsx +45 -45
  61. package/src/lib/plugins/openapi/playground/Playground.tsx +602 -599
  62. package/src/lib/plugins/openapi/playground/PlaygroundDialog.tsx +56 -56
  63. package/src/lib/plugins/openapi/playground/QueryParams.tsx +151 -148
  64. package/src/lib/plugins/openapi/playground/RequestLoginDialog.tsx +70 -70
  65. package/src/lib/plugins/openapi/playground/request-panel/MultipartField.tsx +91 -91
  66. package/src/lib/plugins/openapi/playground/result-panel/AudioPlayer.tsx +50 -50
  67. package/src/lib/plugins/openapi/playground/result-panel/ResponseTab.tsx +355 -355
  68. package/src/lib/plugins/openapi/playground/result-panel/ResultPanel.tsx +106 -102
  69. package/src/lib/plugins/openapi/schema/SchemaPropertyItem.tsx +189 -184
  70. package/src/lib/plugins/openapi/schema/SchemaView.tsx +299 -299
  71. package/src/lib/plugins/search-pagefind/IndexingDialog.tsx +162 -162
  72. package/src/lib/plugins/search-pagefind/PagefindSearch.tsx +202 -202
  73. package/src/lib/plugins/search-pagefind/ResultList.tsx +122 -118
  74. package/src/lib/ui/ActionButton.tsx +21 -21
  75. package/src/lib/ui/Command.tsx +191 -191
  76. package/src/lib/util/MdxComponents.tsx +150 -150
  77. package/src/vite/config.ts +234 -231
  78. package/src/vite/dev-server.ts +291 -291
  79. package/src/vite/plugin-api-keys.ts +50 -50
  80. package/src/vite/plugin-api.ts +320 -318
  81. package/src/vite/plugin-auth.ts +38 -38
  82. package/src/vite/plugin-component.ts +45 -39
  83. package/src/vite/plugin-config.ts +75 -75
  84. package/src/vite/plugin-custom-pages.ts +36 -36
  85. package/src/vite/plugin-docs.ts +202 -202
  86. package/src/vite/plugin-markdown-export.ts +214 -214
  87. package/src/vite/plugin-mdx.ts +149 -149
  88. package/src/vite/plugin-navigation.ts +106 -106
  89. package/src/vite/plugin-search.ts +47 -47
  90. package/src/vite/plugin.ts +42 -42
  91. package/src/vite/prerender/prerender.ts +233 -233
@@ -1,291 +1,291 @@
1
- import fs from "node:fs/promises";
2
- import type { Server } from "node:http";
3
- import http from "node:http";
4
- import https from "node:https";
5
- import path from "node:path";
6
- import { createHttpTerminator, type HttpTerminator } from "http-terminator";
7
- import {
8
- createServer as createViteServer,
9
- isRunnableDevEnvironment,
10
- mergeConfig,
11
- type ViteDevServer,
12
- } from "vite";
13
- import { logger } from "../cli/common/logger.js";
14
- import { printDiagnosticsToConsole } from "../cli/common/output.js";
15
- import { findAvailablePort } from "../cli/common/utils/ports.js";
16
- import type { LoadedConfig } from "../config/config.js";
17
- import { loadZudokuConfig } from "../config/loader.js";
18
- import { createGraphQLServer } from "../lib/oas/graphql/index.js";
19
- import {
20
- getAppClientEntryPath,
21
- getAppServerEntryPath,
22
- getViteConfig,
23
- type ZudokuConfigEnv,
24
- } from "./config.js";
25
- import { getDevHtml } from "./html.js";
26
- import { buildPagefindDevIndex } from "./pagefind-dev-index.js";
27
-
28
- const DEFAULT_DEV_PORT = 3000;
29
-
30
- type EntryServerImport = typeof import("../app/entry.server.js");
31
-
32
- export class DevServer {
33
- private terminator: HttpTerminator | undefined;
34
- public resolvedPort = 0;
35
- public protocol = "http";
36
-
37
- constructor(
38
- private options: {
39
- dir: string;
40
- ssr?: boolean;
41
- open?: boolean;
42
- argPort?: number;
43
- },
44
- ) {}
45
-
46
- private async createNodeServer(config: LoadedConfig): Promise<Server> {
47
- if (!config.https) return http.createServer();
48
-
49
- this.protocol = "https";
50
- const { dir } = this.options;
51
-
52
- const [key, cert, ca] = await Promise.all([
53
- fs.readFile(path.resolve(dir, config.https.key)),
54
- fs.readFile(path.resolve(dir, config.https.cert)),
55
- config.https.ca
56
- ? fs.readFile(path.resolve(dir, config.https.ca))
57
- : undefined,
58
- ]);
59
-
60
- return https.createServer({ key, cert, ca });
61
- }
62
-
63
- async start(): Promise<{ vite: ViteDevServer }> {
64
- const configEnv: ZudokuConfigEnv = {
65
- mode: "development",
66
- command: "serve",
67
- isSsrBuild: this.options.ssr,
68
- };
69
- const viteConfig = await getViteConfig(this.options.dir, configEnv);
70
- const { config } = await loadZudokuConfig(configEnv, this.options.dir);
71
-
72
- this.resolvedPort = await findAvailablePort(
73
- this.options.argPort ?? config.port ?? DEFAULT_DEV_PORT,
74
- );
75
-
76
- const server = await this.createNodeServer(config);
77
-
78
- const mergedViteConfig = mergeConfig(viteConfig, {
79
- server: {
80
- hmr: { server },
81
- },
82
- plugins: [
83
- {
84
- // Serves the client entry via Vite's client transform pipeline.
85
- // Must be registered via configureServer so it runs before Vite's
86
- // built-in transform middleware which would treat the path as a static asset.
87
- name: "apitogo:entry-client",
88
- configureServer(server: ViteDevServer) {
89
- const entryPath = path.posix.join(
90
- server.config.base,
91
- "/__z/entry.client.tsx",
92
- );
93
- server.middlewares.use(entryPath, async (_req, res) => {
94
- const transformed =
95
- await server.environments.client.transformRequest(
96
- getAppClientEntryPath(),
97
- );
98
-
99
- if (!transformed) {
100
- res.writeHead(500);
101
- res.end("Error transforming client entry");
102
- return;
103
- }
104
- res.writeHead(200, { "Content-Type": "text/javascript" });
105
- res.end(transformed.code);
106
- });
107
- },
108
- },
109
- ],
110
- });
111
-
112
- const vite = await createViteServer(mergedViteConfig);
113
- const graphql = createGraphQLServer({ graphqlEndpoint: "/__z/graphql" });
114
-
115
- // Handle base path redirect
116
- vite.middlewares.use((req, res, next) => {
117
- if (
118
- req.method === "GET" &&
119
- req.originalUrl === "/" &&
120
- config.basePath &&
121
- config.basePath !== "/"
122
- ) {
123
- res.writeHead(307, { Location: config.basePath });
124
- res.end();
125
- return;
126
- }
127
- next();
128
- });
129
-
130
- vite.middlewares.use(graphql.graphqlEndpoint, graphql);
131
-
132
- // Pagefind reindex endpoint (SSE)
133
- vite.middlewares.use("/__z/pagefind-reindex", async (_req, res) => {
134
- res.writeHead(200, {
135
- "Content-Type": "text/event-stream",
136
- "Cache-Control": "no-cache",
137
- Connection: "keep-alive",
138
- });
139
-
140
- const { config: currentConfig } = await loadZudokuConfig(
141
- configEnv,
142
- this.options.dir,
143
- );
144
-
145
- const sendEvent = (data: unknown) =>
146
- res.write(`data: ${JSON.stringify(data)}\n\n`);
147
-
148
- if (currentConfig.search?.type !== "pagefind") {
149
- sendEvent({
150
- type: "complete",
151
- success: false,
152
- indexed: 0,
153
- error: "Pagefind search is not enabled",
154
- });
155
- res.end();
156
- return;
157
- }
158
-
159
- try {
160
- for await (const event of buildPagefindDevIndex(vite, currentConfig)) {
161
- sendEvent(event);
162
- }
163
- } catch (error) {
164
- const message =
165
- error instanceof Error ? error.message : "Unknown error";
166
-
167
- sendEvent({
168
- type: "complete",
169
- success: false,
170
- indexed: 0,
171
- error: message,
172
- });
173
- }
174
-
175
- res.end();
176
- });
177
-
178
- printDiagnosticsToConsole(
179
- `Server-side rendering ${this.options.ssr ? "enabled" : "disabled"}`,
180
- );
181
-
182
- if (config.search?.type === "pagefind") {
183
- const pagefindPath = path.join(
184
- vite.config.publicDir,
185
- "pagefind/pagefind.js",
186
- );
187
- const exists = await fs.stat(pagefindPath).catch(() => false);
188
- if (!exists) {
189
- await fs.mkdir(path.dirname(pagefindPath), { recursive: true });
190
- await fs.writeFile(pagefindPath, 'throw new Error("NOT_BUILT_YET");');
191
- }
192
- }
193
-
194
- vite.middlewares.use(async (req, res) => {
195
- const url = req.originalUrl ?? req.url ?? "/";
196
- const ssrEnvironment = vite.environments.ssr;
197
-
198
- if (!isRunnableDevEnvironment(ssrEnvironment)) {
199
- res.writeHead(500);
200
- res.end("SSR environment not available");
201
- return;
202
- }
203
-
204
- try {
205
- const { config: currentConfig } = await loadZudokuConfig(
206
- configEnv,
207
- this.options.dir,
208
- );
209
- const rawHtml = getDevHtml({
210
- jsEntry: "/__z/entry.client.tsx",
211
- dir: currentConfig.site?.dir,
212
- });
213
- const template = await vite.transformIndexHtml(url, rawHtml);
214
-
215
- if (this.options.ssr) {
216
- const entryServer =
217
- await ssrEnvironment.runner.import<EntryServerImport>(
218
- getAppServerEntryPath(),
219
- );
220
-
221
- const hasBody = req.method !== "GET" && req.method !== "HEAD";
222
- const request = new Request(
223
- `${this.protocol}://${req.headers.host}${url}`,
224
- {
225
- method: req.method,
226
- headers: req.headers as HeadersInit,
227
- body: hasBody ? (req as unknown as BodyInit) : undefined,
228
- // Required by Node when body is a readable stream
229
- // @ts-expect-error Missing type definition
230
- duplex: hasBody ? "half" : undefined,
231
- },
232
- );
233
-
234
- const response = await entryServer.handleRequest({
235
- template,
236
- request,
237
- routes: entryServer.getRoutesByConfig(currentConfig),
238
- basePath: currentConfig.basePath,
239
- });
240
-
241
- for (const [key, value] of response.headers) {
242
- res.appendHeader(key, value);
243
- }
244
- res.writeHead(response.status);
245
-
246
- for await (const chunk of response.body ?? []) {
247
- res.write(chunk);
248
- }
249
- res.end();
250
- } else {
251
- res.writeHead(200, { "Content-Type": "text/html" });
252
- res.end(template);
253
- }
254
- } catch (e) {
255
- logger.error(e);
256
- const html = `<!DOCTYPE html><html><body><script type="module">
257
- import { ErrorOverlay } from '/@vite/client';
258
- document.body.appendChild(new ErrorOverlay(${JSON.stringify({
259
- message: e instanceof Error ? e.message : String(e),
260
- stack: e instanceof Error ? e.stack : "",
261
- })}));
262
- </script></body></html>`;
263
- res.writeHead(500, { "Content-Type": "text/html" });
264
- res.end(html);
265
- }
266
- });
267
-
268
- server.on("request", vite.middlewares);
269
-
270
- await new Promise<void>((resolve) => {
271
- server.listen(this.resolvedPort, resolve);
272
- });
273
-
274
- this.terminator = createHttpTerminator({ server });
275
-
276
- if (this.options.open || process.env.ZUDOKU_OPEN_BROWSER) {
277
- const url = `${this.protocol}://localhost:${this.resolvedPort}`;
278
- vite.resolvedUrls = {
279
- local: [`${url}${vite.config.base || "/"}`],
280
- network: [],
281
- };
282
- vite.openBrowser();
283
- }
284
-
285
- return { vite };
286
- }
287
-
288
- async stop() {
289
- await this.terminator?.terminate();
290
- }
291
- }
1
+ import fs from "node:fs/promises";
2
+ import type { Server } from "node:http";
3
+ import http from "node:http";
4
+ import https from "node:https";
5
+ import path from "node:path";
6
+ import { createHttpTerminator, type HttpTerminator } from "http-terminator";
7
+ import {
8
+ createServer as createViteServer,
9
+ isRunnableDevEnvironment,
10
+ mergeConfig,
11
+ type ViteDevServer,
12
+ } from "vite";
13
+ import { logger } from "../cli/common/logger.js";
14
+ import { printDiagnosticsToConsole } from "../cli/common/output.js";
15
+ import { findAvailablePort } from "../cli/common/utils/ports.js";
16
+ import type { LoadedConfig } from "../config/config.js";
17
+ import { loadZudokuConfig } from "../config/loader.js";
18
+ import { createGraphQLServer } from "../lib/oas/graphql/index.js";
19
+ import {
20
+ getAppClientEntryPath,
21
+ getAppServerEntryPath,
22
+ getViteConfig,
23
+ type ZudokuConfigEnv,
24
+ } from "./config.js";
25
+ import { getDevHtml } from "./html.js";
26
+ import { buildPagefindDevIndex } from "./pagefind-dev-index.js";
27
+
28
+ const DEFAULT_DEV_PORT = 3000;
29
+
30
+ type EntryServerImport = typeof import("../app/entry.server.js");
31
+
32
+ export class DevServer {
33
+ private terminator: HttpTerminator | undefined;
34
+ public resolvedPort = 0;
35
+ public protocol = "http";
36
+
37
+ constructor(
38
+ private options: {
39
+ dir: string;
40
+ ssr?: boolean;
41
+ open?: boolean;
42
+ argPort?: number;
43
+ },
44
+ ) {}
45
+
46
+ private async createNodeServer(config: LoadedConfig): Promise<Server> {
47
+ if (!config.https) return http.createServer();
48
+
49
+ this.protocol = "https";
50
+ const { dir } = this.options;
51
+
52
+ const [key, cert, ca] = await Promise.all([
53
+ fs.readFile(path.resolve(dir, config.https.key)),
54
+ fs.readFile(path.resolve(dir, config.https.cert)),
55
+ config.https.ca
56
+ ? fs.readFile(path.resolve(dir, config.https.ca))
57
+ : undefined,
58
+ ]);
59
+
60
+ return https.createServer({ key, cert, ca });
61
+ }
62
+
63
+ async start(): Promise<{ vite: ViteDevServer }> {
64
+ const configEnv: ZudokuConfigEnv = {
65
+ mode: "development",
66
+ command: "serve",
67
+ isSsrBuild: this.options.ssr,
68
+ };
69
+ const viteConfig = await getViteConfig(this.options.dir, configEnv);
70
+ const { config } = await loadZudokuConfig(configEnv, this.options.dir);
71
+
72
+ this.resolvedPort = await findAvailablePort(
73
+ this.options.argPort ?? config.port ?? DEFAULT_DEV_PORT,
74
+ );
75
+
76
+ const server = await this.createNodeServer(config);
77
+
78
+ const mergedViteConfig = mergeConfig(viteConfig, {
79
+ server: {
80
+ hmr: { server },
81
+ },
82
+ plugins: [
83
+ {
84
+ // Serves the client entry via Vite's client transform pipeline.
85
+ // Must be registered via configureServer so it runs before Vite's
86
+ // built-in transform middleware which would treat the path as a static asset.
87
+ name: "apitogo:entry-client",
88
+ configureServer(server: ViteDevServer) {
89
+ const entryPath = path.posix.join(
90
+ server.config.base,
91
+ "/__z/entry.client.tsx",
92
+ );
93
+ server.middlewares.use(entryPath, async (_req, res) => {
94
+ const transformed =
95
+ await server.environments.client.transformRequest(
96
+ getAppClientEntryPath(),
97
+ );
98
+
99
+ if (!transformed) {
100
+ res.writeHead(500);
101
+ res.end("Error transforming client entry");
102
+ return;
103
+ }
104
+ res.writeHead(200, { "Content-Type": "text/javascript" });
105
+ res.end(transformed.code);
106
+ });
107
+ },
108
+ },
109
+ ],
110
+ });
111
+
112
+ const vite = await createViteServer(mergedViteConfig);
113
+ const graphql = createGraphQLServer({ graphqlEndpoint: "/__z/graphql" });
114
+
115
+ // Handle base path redirect
116
+ vite.middlewares.use((req, res, next) => {
117
+ if (
118
+ req.method === "GET" &&
119
+ req.originalUrl === "/" &&
120
+ config.basePath &&
121
+ config.basePath !== "/"
122
+ ) {
123
+ res.writeHead(307, { Location: config.basePath });
124
+ res.end();
125
+ return;
126
+ }
127
+ next();
128
+ });
129
+
130
+ vite.middlewares.use(graphql.graphqlEndpoint, graphql);
131
+
132
+ // Pagefind reindex endpoint (SSE)
133
+ vite.middlewares.use("/__z/pagefind-reindex", async (_req, res) => {
134
+ res.writeHead(200, {
135
+ "Content-Type": "text/event-stream",
136
+ "Cache-Control": "no-cache",
137
+ Connection: "keep-alive",
138
+ });
139
+
140
+ const { config: currentConfig } = await loadZudokuConfig(
141
+ configEnv,
142
+ this.options.dir,
143
+ );
144
+
145
+ const sendEvent = (data: unknown) =>
146
+ res.write(`data: ${JSON.stringify(data)}\n\n`);
147
+
148
+ if (currentConfig.search?.type !== "pagefind") {
149
+ sendEvent({
150
+ type: "complete",
151
+ success: false,
152
+ indexed: 0,
153
+ error: "Pagefind search is not enabled",
154
+ });
155
+ res.end();
156
+ return;
157
+ }
158
+
159
+ try {
160
+ for await (const event of buildPagefindDevIndex(vite, currentConfig)) {
161
+ sendEvent(event);
162
+ }
163
+ } catch (error) {
164
+ const message =
165
+ error instanceof Error ? error.message : "Unknown error";
166
+
167
+ sendEvent({
168
+ type: "complete",
169
+ success: false,
170
+ indexed: 0,
171
+ error: message,
172
+ });
173
+ }
174
+
175
+ res.end();
176
+ });
177
+
178
+ printDiagnosticsToConsole(
179
+ `Server-side rendering ${this.options.ssr ? "enabled" : "disabled"}`,
180
+ );
181
+
182
+ if (config.search?.type === "pagefind") {
183
+ const pagefindPath = path.join(
184
+ vite.config.publicDir,
185
+ "pagefind/pagefind.js",
186
+ );
187
+ const exists = await fs.stat(pagefindPath).catch(() => false);
188
+ if (!exists) {
189
+ await fs.mkdir(path.dirname(pagefindPath), { recursive: true });
190
+ await fs.writeFile(pagefindPath, 'throw new Error("NOT_BUILT_YET");');
191
+ }
192
+ }
193
+
194
+ vite.middlewares.use(async (req, res) => {
195
+ const url = req.originalUrl ?? req.url ?? "/";
196
+ const ssrEnvironment = vite.environments.ssr;
197
+
198
+ if (!isRunnableDevEnvironment(ssrEnvironment)) {
199
+ res.writeHead(500);
200
+ res.end("SSR environment not available");
201
+ return;
202
+ }
203
+
204
+ try {
205
+ const { config: currentConfig } = await loadZudokuConfig(
206
+ configEnv,
207
+ this.options.dir,
208
+ );
209
+ const rawHtml = getDevHtml({
210
+ jsEntry: "/__z/entry.client.tsx",
211
+ dir: currentConfig.site?.dir,
212
+ });
213
+ const template = await vite.transformIndexHtml(url, rawHtml);
214
+
215
+ if (this.options.ssr) {
216
+ const entryServer =
217
+ await ssrEnvironment.runner.import<EntryServerImport>(
218
+ getAppServerEntryPath(),
219
+ );
220
+
221
+ const hasBody = req.method !== "GET" && req.method !== "HEAD";
222
+ const request = new Request(
223
+ `${this.protocol}://${req.headers.host}${url}`,
224
+ {
225
+ method: req.method,
226
+ headers: req.headers as HeadersInit,
227
+ body: hasBody ? (req as unknown as BodyInit) : undefined,
228
+ // Required by Node when body is a readable stream
229
+ // @ts-expect-error Missing type definition
230
+ duplex: hasBody ? "half" : undefined,
231
+ },
232
+ );
233
+
234
+ const response = await entryServer.handleRequest({
235
+ template,
236
+ request,
237
+ routes: entryServer.getRoutesByConfig(currentConfig),
238
+ basePath: currentConfig.basePath,
239
+ });
240
+
241
+ for (const [key, value] of response.headers) {
242
+ res.appendHeader(key, value);
243
+ }
244
+ res.writeHead(response.status);
245
+
246
+ for await (const chunk of response.body ?? []) {
247
+ res.write(chunk);
248
+ }
249
+ res.end();
250
+ } else {
251
+ res.writeHead(200, { "Content-Type": "text/html" });
252
+ res.end(template);
253
+ }
254
+ } catch (e) {
255
+ logger.error(e);
256
+ const html = `<!DOCTYPE html><html><body><script type="module">
257
+ import { ErrorOverlay } from '/@vite/client';
258
+ document.body.appendChild(new ErrorOverlay(${JSON.stringify({
259
+ message: e instanceof Error ? e.message : String(e),
260
+ stack: e instanceof Error ? e.stack : "",
261
+ })}));
262
+ </script></body></html>`;
263
+ res.writeHead(500, { "Content-Type": "text/html" });
264
+ res.end(html);
265
+ }
266
+ });
267
+
268
+ server.on("request", vite.middlewares);
269
+
270
+ await new Promise<void>((resolve) => {
271
+ server.listen(this.resolvedPort, resolve);
272
+ });
273
+
274
+ this.terminator = createHttpTerminator({ server });
275
+
276
+ if (this.options.open || process.env.ZUDOKU_OPEN_BROWSER) {
277
+ const url = `${this.protocol}://localhost:${this.resolvedPort}`;
278
+ vite.resolvedUrls = {
279
+ local: [`${url}${vite.config.base || "/"}`],
280
+ network: [],
281
+ };
282
+ vite.openBrowser();
283
+ }
284
+
285
+ return { vite };
286
+ }
287
+
288
+ async stop() {
289
+ await this.terminator?.terminate();
290
+ }
291
+ }