@lukoweb/apitogo 0.1.1 → 0.1.3
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.
- package/dist/cli/cli.js +168 -57
- package/dist/declarations/lib/ui/Command.d.ts +1 -1
- package/dist/declarations/lib/util/MdxComponents.d.ts +1 -1
- package/package.json +2 -1
- package/src/app/main.tsx +150 -150
- package/src/config/loader.ts +245 -245
- package/src/index.ts +33 -33
- package/src/lib/authentication/components/CallbackHandler.tsx +45 -45
- package/src/lib/authentication/components/SignIn.tsx +46 -46
- package/src/lib/authentication/components/SignUp.tsx +48 -48
- package/src/lib/authentication/providers/clerk.tsx +224 -224
- package/src/lib/authentication/ui/AuthCard.tsx +7 -7
- package/src/lib/authentication/ui/EmailLinkCallbackUi.tsx +129 -125
- package/src/lib/authentication/ui/EmailLinkSentUi.tsx +105 -101
- package/src/lib/authentication/ui/EmailLinkSignInUi.tsx +100 -96
- package/src/lib/authentication/ui/EmailVerificationUi.tsx +128 -124
- package/src/lib/authentication/ui/ZudokuAuthUi.tsx +620 -616
- package/src/lib/components/AiAssistantMenuItems.tsx +102 -102
- package/src/lib/components/Autocomplete.tsx +123 -123
- package/src/lib/components/DeveloperHint.tsx +39 -35
- package/src/lib/components/Header.tsx +260 -260
- package/src/lib/components/Layout.tsx +53 -53
- package/src/lib/components/Main.tsx +51 -51
- package/src/lib/components/Mermaid.tsx +74 -70
- package/src/lib/components/MobileTopNavigation.tsx +276 -276
- package/src/lib/components/MultiSelect.tsx +86 -82
- package/src/lib/components/TopNavigation.tsx +103 -103
- package/src/lib/components/index.ts +26 -26
- package/src/lib/components/navigation/NavigationCategory.tsx +157 -157
- package/src/lib/components/navigation/NavigationFilterInput.tsx +35 -35
- package/src/lib/components/navigation/NavigationItem.tsx +181 -177
- package/src/lib/core/RouteGuard.tsx +193 -193
- package/src/lib/errors/ErrorMessage.tsx +50 -46
- package/src/lib/plugins/api-catalog/Catalog.tsx +80 -80
- package/src/lib/plugins/api-keys/CreateApiKey.tsx +128 -124
- package/src/lib/plugins/api-keys/CreateApiKeyDialog.tsx +49 -49
- package/src/lib/plugins/api-keys/SettingsApiKeys.tsx +72 -72
- package/src/lib/plugins/api-keys/settings/ApiKeyItem.tsx +342 -342
- package/src/lib/plugins/api-keys/settings/RevealApiKey.tsx +127 -127
- package/src/lib/plugins/markdown/MdxPage.tsx +285 -285
- package/src/lib/plugins/openapi/ApiHeader.tsx +96 -96
- package/src/lib/plugins/openapi/CollapsibleCode.tsx +88 -88
- package/src/lib/plugins/openapi/DownloadSchemaButton.tsx +94 -94
- package/src/lib/plugins/openapi/GeneratedExampleSidecarBox.tsx +52 -52
- package/src/lib/plugins/openapi/OperationListItem.tsx +183 -183
- package/src/lib/plugins/openapi/RequestBodySidecarBox.tsx +63 -63
- package/src/lib/plugins/openapi/ResponsesSidecarBox.tsx +120 -117
- package/src/lib/plugins/openapi/SchemaInfo.tsx +344 -344
- package/src/lib/plugins/openapi/SchemaList.tsx +113 -113
- package/src/lib/plugins/openapi/Sidecar.tsx +312 -309
- package/src/lib/plugins/openapi/SidecarExamples.tsx +177 -174
- package/src/lib/plugins/openapi/components/EnumValues.tsx +58 -58
- package/src/lib/plugins/openapi/components/ResponseContent.tsx +117 -114
- package/src/lib/plugins/openapi/playground/BodyPanel.tsx +274 -271
- package/src/lib/plugins/openapi/playground/CollapsibleHeader.tsx +51 -51
- package/src/lib/plugins/openapi/playground/ExamplesDropdown.tsx +60 -60
- package/src/lib/plugins/openapi/playground/Headers.tsx +188 -181
- package/src/lib/plugins/openapi/playground/IdentityDialog.tsx +75 -75
- package/src/lib/plugins/openapi/playground/IdentitySelector.tsx +38 -38
- package/src/lib/plugins/openapi/playground/ParamsGrid.tsx +45 -45
- package/src/lib/plugins/openapi/playground/Playground.tsx +602 -599
- package/src/lib/plugins/openapi/playground/PlaygroundDialog.tsx +56 -56
- package/src/lib/plugins/openapi/playground/QueryParams.tsx +151 -148
- package/src/lib/plugins/openapi/playground/RequestLoginDialog.tsx +70 -70
- package/src/lib/plugins/openapi/playground/request-panel/MultipartField.tsx +91 -91
- package/src/lib/plugins/openapi/playground/result-panel/AudioPlayer.tsx +50 -50
- package/src/lib/plugins/openapi/playground/result-panel/ResponseTab.tsx +355 -355
- package/src/lib/plugins/openapi/playground/result-panel/ResultPanel.tsx +106 -102
- package/src/lib/plugins/openapi/schema/SchemaPropertyItem.tsx +189 -184
- package/src/lib/plugins/openapi/schema/SchemaView.tsx +299 -299
- package/src/lib/plugins/search-pagefind/IndexingDialog.tsx +162 -162
- package/src/lib/plugins/search-pagefind/PagefindSearch.tsx +202 -202
- package/src/lib/plugins/search-pagefind/ResultList.tsx +122 -118
- package/src/lib/ui/ActionButton.tsx +21 -21
- package/src/lib/ui/Command.tsx +191 -191
- package/src/lib/util/MdxComponents.tsx +150 -150
- package/src/vite/config.ts +234 -231
- package/src/vite/dev-server.ts +291 -291
- package/src/vite/plugin-api-keys.ts +50 -50
- package/src/vite/plugin-api.ts +320 -318
- package/src/vite/plugin-auth.ts +38 -38
- package/src/vite/plugin-component.ts +45 -39
- package/src/vite/plugin-config.ts +75 -75
- package/src/vite/plugin-custom-pages.ts +36 -36
- package/src/vite/plugin-docs.ts +202 -202
- package/src/vite/plugin-markdown-export.ts +214 -214
- package/src/vite/plugin-mdx.ts +149 -149
- package/src/vite/plugin-navigation.ts +106 -106
- package/src/vite/plugin-search.ts +47 -47
- package/src/vite/plugin.ts +42 -42
- package/src/vite/prerender/prerender.ts +233 -233
package/src/vite/dev-server.ts
CHANGED
|
@@ -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
|
+
}
|