ludicord 2.2.0 → 2.2.4
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/README.md +8 -6
- package/bin/ludicord.js +0 -0
- package/dist/api/runtime.d.ts +4 -0
- package/dist/api/runtime.js +61 -10
- package/dist/auth/client.d.ts +10 -0
- package/dist/auth/client.js +81 -8
- package/dist/auth/index.d.ts +1 -1
- package/dist/auth/index.js +1 -1
- package/dist/auth/server.js +175 -32
- package/dist/auth/types.d.ts +5 -0
- package/dist/cli/index.js +14 -13
- package/dist/cli/project-commands.js +49 -34
- package/dist/compiler/build.js +37 -127
- package/dist/compiler/client-entry.d.ts +4 -0
- package/dist/compiler/client-entry.js +35 -0
- package/dist/compiler/client-tooling.d.ts +5 -0
- package/dist/compiler/client-tooling.js +97 -0
- package/dist/compiler/scanner.js +8 -0
- package/dist/compiler/server-bundle.d.ts +11 -0
- package/dist/compiler/server-bundle.js +31 -0
- package/dist/compiler/type-generation.js +11 -2
- package/dist/config/defaults.js +28 -2
- package/dist/config/environment.d.ts +2 -0
- package/dist/config/environment.js +18 -0
- package/dist/discord/data.d.ts +21 -0
- package/dist/discord/data.js +91 -0
- package/dist/discord/events.d.ts +42 -4
- package/dist/discord/events.js +120 -41
- package/dist/discord/index.d.ts +4 -2
- package/dist/discord/index.js +2 -1
- package/dist/discord/runtime.d.ts +2 -2
- package/dist/discord/runtime.js +9 -13
- package/dist/discord/testing.d.ts +1 -0
- package/dist/discord/testing.js +10 -0
- package/dist/manifests/types.d.ts +4 -0
- package/dist/navigation/index.d.ts +2 -0
- package/dist/runtime/client/dev-panel.d.ts +2 -0
- package/dist/runtime/client/dev-panel.js +63 -0
- package/dist/runtime/client/embed-router.d.ts +4 -1
- package/dist/runtime/client/embed-router.js +69 -17
- package/dist/runtime/client/files.d.ts +23 -0
- package/dist/runtime/client/files.js +12 -0
- package/dist/runtime/client/minimize-card.js +4 -4
- package/dist/runtime/client/minimize.d.ts +1 -0
- package/dist/runtime/client/minimize.js +1 -0
- package/dist/runtime/client/root.d.ts +7 -0
- package/dist/runtime/client/root.js +19 -0
- package/dist/runtime/index.d.ts +8 -4
- package/dist/runtime/index.js +14 -2
- package/dist/runtime/server/dev-server.d.ts +1 -0
- package/dist/runtime/server/dev-server.js +294 -282
- package/dist/runtime/server/diagnostics.d.ts +17 -0
- package/dist/runtime/server/diagnostics.js +87 -0
- package/dist/runtime/server/index.d.ts +5 -0
- package/dist/runtime/server/index.js +3 -0
- package/dist/runtime/server/production-server.js +14 -1
- package/dist/security/discord.d.ts +9 -0
- package/dist/security/discord.js +37 -7
- package/dist/security/secret-scan.d.ts +3 -0
- package/dist/security/secret-scan.js +40 -0
- package/dist/server/discord-rest.d.ts +45 -0
- package/dist/server/discord-rest.js +46 -0
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.js +1 -0
- package/dist/sessions/cookies.d.ts +4 -1
- package/dist/sessions/cookies.js +17 -4
- package/dist/sessions/crypto.js +6 -0
- package/dist/testing/index.d.ts +13 -0
- package/dist/testing/index.js +24 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/ws/client.d.ts +6 -0
- package/dist/ws/client.js +46 -10
- package/dist/ws/server.d.ts +4 -0
- package/dist/ws/server.js +36 -15
- package/package.json +31 -4
- package/dist/api/runtime.d.ts.map +0 -1
- package/dist/api/runtime.js.map +0 -1
- package/dist/auth/client.d.ts.map +0 -1
- package/dist/auth/client.js.map +0 -1
- package/dist/auth/index.d.ts.map +0 -1
- package/dist/auth/index.js.map +0 -1
- package/dist/auth/server.d.ts.map +0 -1
- package/dist/auth/server.js.map +0 -1
- package/dist/auth/types.d.ts.map +0 -1
- package/dist/auth/types.js.map +0 -1
- package/dist/cli/index.d.ts.map +0 -1
- package/dist/cli/index.js.map +0 -1
- package/dist/cli/project-commands.d.ts.map +0 -1
- package/dist/cli/project-commands.js.map +0 -1
- package/dist/compiler/build.d.ts.map +0 -1
- package/dist/compiler/build.js.map +0 -1
- package/dist/compiler/index.d.ts.map +0 -1
- package/dist/compiler/index.js.map +0 -1
- package/dist/compiler/scanner.d.ts.map +0 -1
- package/dist/compiler/scanner.js.map +0 -1
- package/dist/compiler/type-generation.d.ts.map +0 -1
- package/dist/compiler/type-generation.js.map +0 -1
- package/dist/compiler/write-manifests.d.ts.map +0 -1
- package/dist/compiler/write-manifests.js.map +0 -1
- package/dist/config/defaults.d.ts.map +0 -1
- package/dist/config/defaults.js.map +0 -1
- package/dist/config/index.d.ts.map +0 -1
- package/dist/config/index.js.map +0 -1
- package/dist/config/loader.d.ts.map +0 -1
- package/dist/config/loader.js.map +0 -1
- package/dist/config/project-root.d.ts.map +0 -1
- package/dist/config/project-root.js.map +0 -1
- package/dist/config/types.d.ts.map +0 -1
- package/dist/config/types.js.map +0 -1
- package/dist/discord/events.d.ts.map +0 -1
- package/dist/discord/events.js.map +0 -1
- package/dist/discord/index.d.ts.map +0 -1
- package/dist/discord/index.js.map +0 -1
- package/dist/discord/runtime.d.ts.map +0 -1
- package/dist/discord/runtime.js.map +0 -1
- package/dist/discord/safe-area.d.ts.map +0 -1
- package/dist/discord/safe-area.js.map +0 -1
- package/dist/discord/testing.d.ts.map +0 -1
- package/dist/discord/testing.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/manifests/types.d.ts.map +0 -1
- package/dist/manifests/types.js.map +0 -1
- package/dist/navigation/index.d.ts.map +0 -1
- package/dist/navigation/index.js.map +0 -1
- package/dist/router/match.d.ts.map +0 -1
- package/dist/router/match.js.map +0 -1
- package/dist/runtime/client/embed-router.d.ts.map +0 -1
- package/dist/runtime/client/embed-router.js.map +0 -1
- package/dist/runtime/client/minimize-card.d.ts.map +0 -1
- package/dist/runtime/client/minimize-card.js.map +0 -1
- package/dist/runtime/client/minimize.d.ts.map +0 -1
- package/dist/runtime/client/minimize.js.map +0 -1
- package/dist/runtime/index.d.ts.map +0 -1
- package/dist/runtime/index.js.map +0 -1
- package/dist/runtime/server/dev-server.d.ts.map +0 -1
- package/dist/runtime/server/dev-server.js.map +0 -1
- package/dist/runtime/server/production-server.d.ts.map +0 -1
- package/dist/runtime/server/production-server.js.map +0 -1
- package/dist/security/discord.d.ts.map +0 -1
- package/dist/security/discord.js.map +0 -1
- package/dist/security/index.d.ts.map +0 -1
- package/dist/security/index.js.map +0 -1
- package/dist/server/index.d.ts.map +0 -1
- package/dist/server/index.js.map +0 -1
- package/dist/server/session.d.ts.map +0 -1
- package/dist/server/session.js.map +0 -1
- package/dist/server/types.d.ts.map +0 -1
- package/dist/server/types.js.map +0 -1
- package/dist/sessions/cookies.d.ts.map +0 -1
- package/dist/sessions/cookies.js.map +0 -1
- package/dist/sessions/crypto.d.ts.map +0 -1
- package/dist/sessions/crypto.js.map +0 -1
- package/dist/shared/errors.d.ts.map +0 -1
- package/dist/shared/errors.js.map +0 -1
- package/dist/testing/index.d.ts.map +0 -1
- package/dist/testing/index.js.map +0 -1
- package/dist/version.d.ts.map +0 -1
- package/dist/version.js.map +0 -1
- package/dist/ws/client.d.ts.map +0 -1
- package/dist/ws/client.js.map +0 -1
- package/dist/ws/index.d.ts.map +0 -1
- package/dist/ws/index.js.map +0 -1
- package/dist/ws/server.d.ts.map +0 -1
- package/dist/ws/server.js.map +0 -1
package/dist/compiler/build.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync, } from "node:fs";
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync, } from "node:fs";
|
|
3
2
|
import path from "node:path";
|
|
4
|
-
import {
|
|
5
|
-
import react from "@vitejs/plugin-react";
|
|
6
|
-
import { build as esbuild } from "esbuild";
|
|
7
|
-
import { build as viteBuild, normalizePath, } from "vite";
|
|
3
|
+
import { build as viteBuild, } from "vite";
|
|
8
4
|
import { loadLudicordConfig } from "../config/loader.js";
|
|
9
|
-
import { LudicordError } from "../shared/errors.js";
|
|
10
5
|
import { LUDICORD_VERSION } from "../version.js";
|
|
11
6
|
import { generateManifests } from "./scanner.js";
|
|
12
7
|
import { writeManifests } from "./write-manifests.js";
|
|
13
8
|
import { generateLudicordTypes } from "./type-generation.js";
|
|
14
|
-
|
|
9
|
+
import { clientEntry } from "./client-entry.js";
|
|
10
|
+
import { clientAliases as ludicordClientAliases, clientPlugins, ludicordLogger } from "./client-tooling.js";
|
|
11
|
+
import { bundleServerGraph } from "./server-bundle.js";
|
|
12
|
+
import { loadProjectEnvironment } from "../config/environment.js";
|
|
13
|
+
import { clientContainsSecret } from "../security/secret-scan.js";
|
|
14
|
+
import { checkProjectTypes } from "../runtime/server/diagnostics.js";
|
|
15
15
|
const VIRTUAL_BUILD_ENTRY = "virtual:ludicord-production-entry";
|
|
16
16
|
const RESOLVED_BUILD_ENTRY = "\0virtual:ludicord-production-entry";
|
|
17
17
|
function safeOutputDirectory(projectRoot) {
|
|
@@ -21,31 +21,6 @@ function safeOutputDirectory(projectRoot) {
|
|
|
21
21
|
}
|
|
22
22
|
return output;
|
|
23
23
|
}
|
|
24
|
-
function clientEntrySource(projectRoot, manifests, config) {
|
|
25
|
-
const activityImport = normalizePath(path.join(projectRoot, manifests.activity.root));
|
|
26
|
-
const registry = manifests.embeds
|
|
27
|
-
.map(({ route, file }) => {
|
|
28
|
-
const importPath = normalizePath(path.join(projectRoot, file));
|
|
29
|
-
return `${JSON.stringify(route)}: () => import(${JSON.stringify(importPath)})`;
|
|
30
|
-
})
|
|
31
|
-
.join(",\n");
|
|
32
|
-
const publicConfig = {
|
|
33
|
-
discordClientId: config.discord.clientId,
|
|
34
|
-
outsideDiscord: config.activity.outsideDiscord,
|
|
35
|
-
scopes: config.discord.scopes,
|
|
36
|
-
authRequired: config.discord.auth.required,
|
|
37
|
-
};
|
|
38
|
-
return `
|
|
39
|
-
import React from "react";
|
|
40
|
-
import { createRoot } from "react-dom/client";
|
|
41
|
-
import Pages from ${JSON.stringify(activityImport)};
|
|
42
|
-
globalThis.__LUDICORD_PUBLIC_CONFIG__ = ${JSON.stringify(publicConfig)};
|
|
43
|
-
globalThis.__LUDICORD_EMBEDS__ = { ${registry} };
|
|
44
|
-
const container = document.getElementById("ludicord-root");
|
|
45
|
-
if (!container) throw new Error("LUDICORD2003: Missing #ludicord-root container.");
|
|
46
|
-
createRoot(container).render(React.createElement(Pages));
|
|
47
|
-
`;
|
|
48
|
-
}
|
|
49
24
|
function buildPlugin(projectRoot, manifests, config) {
|
|
50
25
|
return {
|
|
51
26
|
name: "ludicord:production-entry",
|
|
@@ -54,60 +29,22 @@ function buildPlugin(projectRoot, manifests, config) {
|
|
|
54
29
|
},
|
|
55
30
|
load(id) {
|
|
56
31
|
return id === RESOLVED_BUILD_ENTRY
|
|
57
|
-
?
|
|
32
|
+
? clientEntry(projectRoot, manifests, config, false)
|
|
58
33
|
: undefined;
|
|
59
34
|
},
|
|
60
35
|
};
|
|
61
36
|
}
|
|
62
|
-
function clientAliases() {
|
|
63
|
-
return [
|
|
64
|
-
{ find: /^react$/, replacement: require.resolve("react") },
|
|
65
|
-
{
|
|
66
|
-
find: /^react\/jsx-runtime$/,
|
|
67
|
-
replacement: require.resolve("react/jsx-runtime"),
|
|
68
|
-
},
|
|
69
|
-
{
|
|
70
|
-
find: /^react\/jsx-dev-runtime$/,
|
|
71
|
-
replacement: require.resolve("react/jsx-dev-runtime"),
|
|
72
|
-
},
|
|
73
|
-
{
|
|
74
|
-
find: /^react-dom$/,
|
|
75
|
-
replacement: require.resolve("react-dom"),
|
|
76
|
-
},
|
|
77
|
-
{
|
|
78
|
-
find: /^react-dom\/client$/,
|
|
79
|
-
replacement: require.resolve("react-dom/client"),
|
|
80
|
-
},
|
|
81
|
-
{
|
|
82
|
-
find: /^ludicord$/,
|
|
83
|
-
replacement: fileURLToPath(new URL("../index.js", import.meta.url)),
|
|
84
|
-
},
|
|
85
|
-
{
|
|
86
|
-
find: /^ludicord\/navigation$/,
|
|
87
|
-
replacement: fileURLToPath(new URL("../navigation/index.js", import.meta.url)),
|
|
88
|
-
},
|
|
89
|
-
{
|
|
90
|
-
find: /^ludicord\/discord$/,
|
|
91
|
-
replacement: fileURLToPath(new URL("../discord/index.js", import.meta.url)),
|
|
92
|
-
},
|
|
93
|
-
{
|
|
94
|
-
find: /^ludicord\/auth$/,
|
|
95
|
-
replacement: fileURLToPath(new URL("../auth/index.js", import.meta.url)),
|
|
96
|
-
},
|
|
97
|
-
{
|
|
98
|
-
find: /^ludicord\/ws\/client$/,
|
|
99
|
-
replacement: fileURLToPath(new URL("../ws/client.js", import.meta.url)),
|
|
100
|
-
},
|
|
101
|
-
];
|
|
102
|
-
}
|
|
103
37
|
async function buildClient(projectRoot, outputDirectory, manifests, config) {
|
|
104
38
|
const result = (await viteBuild({
|
|
39
|
+
configFile: false,
|
|
40
|
+
cacheDir: path.join(projectRoot, ".ludicord", "cache", "client"),
|
|
41
|
+
customLogger: ludicordLogger(),
|
|
105
42
|
root: projectRoot,
|
|
106
43
|
publicDir: existsSync(path.join(projectRoot, "public"))
|
|
107
44
|
? path.join(projectRoot, "public")
|
|
108
45
|
: false,
|
|
109
|
-
plugins: [buildPlugin(projectRoot, manifests, config),
|
|
110
|
-
resolve: { alias:
|
|
46
|
+
plugins: [buildPlugin(projectRoot, manifests, config), ...await clientPlugins(projectRoot)],
|
|
47
|
+
resolve: { alias: ludicordClientAliases(projectRoot), dedupe: ["react", "react-dom"] },
|
|
111
48
|
build: {
|
|
112
49
|
outDir: path.join(outputDirectory, "client"),
|
|
113
50
|
emptyOutDir: true,
|
|
@@ -127,15 +64,28 @@ async function buildClient(projectRoot, outputDirectory, manifests, config) {
|
|
|
127
64
|
.flatMap((output) => output.output)
|
|
128
65
|
.find((item) => item.type === "chunk" && item.isEntry);
|
|
129
66
|
if (entry === undefined) {
|
|
130
|
-
throw new Error("LUDICORD7003:
|
|
67
|
+
throw new Error("LUDICORD7003: The compiler did not generate an Activity entry.");
|
|
131
68
|
}
|
|
132
69
|
const clientEntry = entry.fileName.split(path.sep).join("/");
|
|
70
|
+
const viteManifest = JSON.parse(readFileSync(path.join(outputDirectory, "client", "vite-manifest.json"), "utf8"));
|
|
71
|
+
const styles = new Set();
|
|
72
|
+
const visited = new Set();
|
|
73
|
+
const collect = (key) => {
|
|
74
|
+
if (visited.has(key))
|
|
75
|
+
return;
|
|
76
|
+
visited.add(key);
|
|
77
|
+
const chunk = viteManifest[key];
|
|
78
|
+
chunk?.css?.forEach((file) => styles.add(file));
|
|
79
|
+
chunk?.imports?.forEach(collect);
|
|
80
|
+
};
|
|
81
|
+
Object.keys(viteManifest).filter((key) => viteManifest[key]?.isEntry).forEach(collect);
|
|
133
82
|
writeFileSync(path.join(outputDirectory, "client", "index.html"), `<!doctype html>
|
|
134
83
|
<html>
|
|
135
84
|
<head>
|
|
136
85
|
<meta charset="UTF-8" />
|
|
137
86
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
138
87
|
<title>Ludicord Activity</title>
|
|
88
|
+
${[...styles].map((file) => `<link rel="stylesheet" href="/${file}" />`).join("\n ")}
|
|
139
89
|
</head>
|
|
140
90
|
<body>
|
|
141
91
|
<div id="ludicord-root"></div>
|
|
@@ -145,52 +95,6 @@ async function buildClient(projectRoot, outputDirectory, manifests, config) {
|
|
|
145
95
|
`, "utf8");
|
|
146
96
|
return clientEntry;
|
|
147
97
|
}
|
|
148
|
-
async function bundleRoutes(projectRoot, outputDirectory, entries, kind) {
|
|
149
|
-
const bundles = [];
|
|
150
|
-
const destination = path.join(outputDirectory, "server", kind);
|
|
151
|
-
mkdirSync(destination, { recursive: true });
|
|
152
|
-
for (const [index, entry] of entries.entries()) {
|
|
153
|
-
const bundle = path.join("server", kind, `${index}.mjs`).split(path.sep).join("/");
|
|
154
|
-
await esbuild({
|
|
155
|
-
entryPoints: [path.join(projectRoot, entry.file)],
|
|
156
|
-
outfile: path.join(outputDirectory, bundle),
|
|
157
|
-
platform: "node",
|
|
158
|
-
format: "esm",
|
|
159
|
-
target: "node20",
|
|
160
|
-
bundle: true,
|
|
161
|
-
sourcemap: true,
|
|
162
|
-
packages: "external",
|
|
163
|
-
logLevel: "silent",
|
|
164
|
-
});
|
|
165
|
-
bundles.push({ source: entry.file, bundle });
|
|
166
|
-
}
|
|
167
|
-
return bundles;
|
|
168
|
-
}
|
|
169
|
-
function walkFiles(directory) {
|
|
170
|
-
if (!existsSync(directory))
|
|
171
|
-
return [];
|
|
172
|
-
return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
|
|
173
|
-
const file = path.join(directory, entry.name);
|
|
174
|
-
return entry.isDirectory() ? walkFiles(file) : [file];
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
function scanForSecrets(clientDirectory) {
|
|
178
|
-
const secrets = [
|
|
179
|
-
process.env.LUDICORD_DISCORD_CLIENT_SECRET,
|
|
180
|
-
process.env.LUDICORD_SESSION_SECRET,
|
|
181
|
-
process.env.LUDICORD_DISCORD_BOT_TOKEN,
|
|
182
|
-
].filter((value) => value !== undefined && value !== "");
|
|
183
|
-
if (secrets.length === 0)
|
|
184
|
-
return;
|
|
185
|
-
for (const file of walkFiles(clientDirectory)) {
|
|
186
|
-
if (statSync(file).size > 10_000_000)
|
|
187
|
-
continue;
|
|
188
|
-
const contents = readFileSync(file);
|
|
189
|
-
if (secrets.some((secret) => contents.includes(Buffer.from(secret)))) {
|
|
190
|
-
throw new Error("LUDICORD7001: Server secret detected in client output.");
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
98
|
function buildMetadata(clientEntry, api, websocket, config) {
|
|
195
99
|
return {
|
|
196
100
|
frameworkVersion: LUDICORD_VERSION,
|
|
@@ -219,6 +123,7 @@ function buildMetadata(clientEntry, api, websocket, config) {
|
|
|
219
123
|
}
|
|
220
124
|
export async function buildLudicordProject(projectRoot) {
|
|
221
125
|
const root = path.resolve(projectRoot);
|
|
126
|
+
loadProjectEnvironment(root, "production");
|
|
222
127
|
const outputDirectory = safeOutputDirectory(root);
|
|
223
128
|
const manifests = generateManifests(root);
|
|
224
129
|
const { config } = await loadLudicordConfig(root);
|
|
@@ -226,14 +131,19 @@ export async function buildLudicordProject(projectRoot) {
|
|
|
226
131
|
mkdirSync(outputDirectory, { recursive: true });
|
|
227
132
|
try {
|
|
228
133
|
generateLudicordTypes(root, manifests, config);
|
|
134
|
+
const typeErrors = checkProjectTypes(root);
|
|
135
|
+
if (typeErrors.length > 0)
|
|
136
|
+
throw new Error(typeErrors.map((item) => `${item.file ?? "TypeScript"}:${item.line ?? 1}:${item.column ?? 1}\n${item.message}\n${item.frame ?? ""}`).join("\n\n"));
|
|
229
137
|
const clientEntry = await buildClient(root, outputDirectory, manifests, config);
|
|
230
|
-
const
|
|
231
|
-
const
|
|
138
|
+
const graph = await bundleServerGraph(root, path.join(outputDirectory, "server"), manifests);
|
|
139
|
+
const api = graph.bundles.filter((route) => route.kind === "api").map((route) => ({ source: route.source, bundle: `server/${route.bundle}` }));
|
|
140
|
+
const websocket = graph.bundles.filter((route) => route.kind === "ws").map((route) => ({ source: route.source, bundle: `server/${route.bundle}` }));
|
|
232
141
|
writeManifests(manifests, path.join(outputDirectory, "manifests"));
|
|
233
142
|
mkdirSync(path.join(outputDirectory, "types"), { recursive: true });
|
|
234
143
|
const metadata = buildMetadata(clientEntry, api, websocket, config);
|
|
235
144
|
writeFileSync(path.join(outputDirectory, "build.json"), `${JSON.stringify(metadata, null, 2)}\n`, "utf8");
|
|
236
|
-
|
|
145
|
+
if (clientContainsSecret(path.join(outputDirectory, "client")))
|
|
146
|
+
throw new Error("LUDICORD7001: Server secret detected in client output.");
|
|
237
147
|
return {
|
|
238
148
|
outputDirectory,
|
|
239
149
|
manifests,
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ResolvedLudicordConfig } from "../config/types.js";
|
|
2
|
+
import type { LudicordManifests } from "../manifests/types.js";
|
|
3
|
+
export declare function clientEntry(root: string, manifests: LudicordManifests, config: ResolvedLudicordConfig, development: boolean): string;
|
|
4
|
+
//# sourceMappingURL=client-entry.d.ts.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { normalizePath } from "vite";
|
|
3
|
+
import { LUDICORD_VERSION } from "../version.js";
|
|
4
|
+
export function clientEntry(root, manifests, config, development) {
|
|
5
|
+
const file = (relative) => `${development ? "/@fs/" : ""}${normalizePath(path.join(root, relative))}`;
|
|
6
|
+
const imports = [`import React from "react";`, `import { createRoot } from "react-dom/client";`,
|
|
7
|
+
`import { LudicordRoot } from "ludicord/internal";`, `import Pages from ${JSON.stringify(file(manifests.activity.root))};`];
|
|
8
|
+
const components = [];
|
|
9
|
+
for (const key of ["layout", "loading", "error", "minimize", "authLoading", "authError", "authDenied"]) {
|
|
10
|
+
const source = manifests.activity[key];
|
|
11
|
+
if (source !== undefined) {
|
|
12
|
+
const name = `File_${key}`;
|
|
13
|
+
imports.push(`import ${name} from ${JSON.stringify(file(source))};`);
|
|
14
|
+
components.push(`${key}: ${name}`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const publicConfig = {
|
|
18
|
+
discordServerData: Boolean(process.env.LUDICORD_DISCORD_BOT_TOKEN),
|
|
19
|
+
environment: development ? "development" : "production", version: LUDICORD_VERSION,
|
|
20
|
+
discordClientId: config.discord.clientId, outsideDiscord: config.activity.outsideDiscord,
|
|
21
|
+
scopes: config.discord.scopes, authRequired: config.discord.auth.required,
|
|
22
|
+
defaultEmbed: config.activity.defaultEmbed,
|
|
23
|
+
devFakeAuth: development && process.env.LUDICORD_DEV_FAKE_AUTH === "true",
|
|
24
|
+
websocket: { enabled: config.websocket.enabled, reconnect: config.websocket.reconnect },
|
|
25
|
+
};
|
|
26
|
+
return `${imports.join("\n")}
|
|
27
|
+
globalThis.__LUDICORD_PUBLIC_CONFIG__ = ${JSON.stringify(publicConfig)};
|
|
28
|
+
globalThis.__LUDICORD_EMBEDS__ = {${manifests.embeds.map((route) => `${JSON.stringify(route.route)}: () => import(${JSON.stringify(file(route.file))})`).join(",\n")}};
|
|
29
|
+
const container = document.getElementById("ludicord-root");
|
|
30
|
+
if (!container) throw new Error("LUDICORD2003: Missing #ludicord-root container.");
|
|
31
|
+
const root = createRoot(container);
|
|
32
|
+
root.render(React.createElement(LudicordRoot, { files: {${components.join(",")}} }, React.createElement(Pages)));
|
|
33
|
+
${development ? `if (import.meta.hot) import.meta.hot.dispose(() => root.unmount());` : ""}`;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=client-entry.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AliasOptions, Logger, PluginOption } from "vite";
|
|
2
|
+
export declare function clientAliases(projectRoot: string): AliasOptions;
|
|
3
|
+
export declare function ludicordLogger(onError?: (error: unknown) => void): Logger;
|
|
4
|
+
export declare function clientPlugins(projectRoot: string, onCompile?: (file: string, elapsed?: number) => void): Promise<PluginOption[]>;
|
|
5
|
+
//# sourceMappingURL=client-tooling.d.ts.map
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
5
|
+
import MagicString from "magic-string";
|
|
6
|
+
import ts from "typescript";
|
|
7
|
+
import react from "@vitejs/plugin-react";
|
|
8
|
+
import { createLogger, normalizePath } from "vite";
|
|
9
|
+
import { redact } from "../runtime/server/diagnostics.js";
|
|
10
|
+
const require = createRequire(import.meta.url);
|
|
11
|
+
export function clientAliases(projectRoot) {
|
|
12
|
+
const resolve = (name) => {
|
|
13
|
+
try {
|
|
14
|
+
return require.resolve(name, { paths: [projectRoot] });
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return require.resolve(name);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const entries = ["react", "react/jsx-runtime", "react/jsx-dev-runtime", "react-dom", "react-dom/client"].map((name) => ({ find: new RegExp(`^${name.replaceAll("/", "\\/")}$`), replacement: resolve(name) }));
|
|
21
|
+
const runtime = {
|
|
22
|
+
ludicord: "../index.js", "ludicord/internal": "../runtime/client/root.js", "ludicord/runtime": "../runtime/index.js",
|
|
23
|
+
"ludicord/navigation": "../navigation/index.js", "ludicord/discord": "../discord/index.js", "ludicord/auth": "../auth/index.js",
|
|
24
|
+
"ludicord/ws": "../ws/client.js", "ludicord/ws/client": "../ws/client.js",
|
|
25
|
+
};
|
|
26
|
+
return [...entries, ...Object.entries(runtime).map(([name, file]) => ({ find: new RegExp(`^${name.replaceAll("/", "\\/")}$`), replacement: fileURLToPath(new URL(file, import.meta.url)) }))];
|
|
27
|
+
}
|
|
28
|
+
export function ludicordLogger(onError) {
|
|
29
|
+
const base = createLogger("info", { prefix: "Ludicord", allowClearScreen: false });
|
|
30
|
+
return {
|
|
31
|
+
...base,
|
|
32
|
+
info() { },
|
|
33
|
+
warn(message, options) { base.warn(redact(message).replaceAll("[vite]", "[ludicord]"), options); },
|
|
34
|
+
warnOnce(message, options) { base.warnOnce(redact(message).replaceAll("[vite]", "[ludicord]"), options); },
|
|
35
|
+
error(message, options) {
|
|
36
|
+
const error = options?.error ?? new Error(message);
|
|
37
|
+
onError?.(error);
|
|
38
|
+
if (onError === undefined)
|
|
39
|
+
base.error(redact(message).replaceAll("[vite]", "[ludicord]"), options);
|
|
40
|
+
},
|
|
41
|
+
clearScreen() { },
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export async function clientPlugins(projectRoot, onCompile) {
|
|
45
|
+
const startTimes = new Map();
|
|
46
|
+
const appRoot = normalizePath(path.join(projectRoot, "app"));
|
|
47
|
+
const boundaries = {
|
|
48
|
+
name: "ludicord:client-boundaries", enforce: "pre",
|
|
49
|
+
resolveId(id, _importer, options) {
|
|
50
|
+
if (!options.ssr && /^ludicord\/(server|security|config|ws\/server|runtime\/server)(\/|$)/.test(id)) {
|
|
51
|
+
throw new Error(`LUDICORD7004: ${id} is server-only. Move this import to app/api or app/ws; use type-only imports for types.`);
|
|
52
|
+
}
|
|
53
|
+
return undefined;
|
|
54
|
+
},
|
|
55
|
+
transform(code, id, options) {
|
|
56
|
+
if (options?.ssr)
|
|
57
|
+
return undefined;
|
|
58
|
+
const file = normalizePath(id.split("?", 1)[0] ?? id);
|
|
59
|
+
if (file.startsWith(`${appRoot}/`)) {
|
|
60
|
+
startTimes.set(id, performance.now());
|
|
61
|
+
onCompile?.(path.relative(projectRoot, file).split(path.sep).join("/"));
|
|
62
|
+
}
|
|
63
|
+
if (!/\/embeds\/.*\/embed\.[jt]sx?$/.test(file))
|
|
64
|
+
return undefined;
|
|
65
|
+
// Source code remains `export default function embed`; React Refresh sees a component name.
|
|
66
|
+
const source = ts.createSourceFile(file, code, ts.ScriptTarget.Latest, true, file.endsWith("tsx") ? ts.ScriptKind.TSX : ts.ScriptKind.JSX);
|
|
67
|
+
const declaration = source.statements.find((node) => ts.isFunctionDeclaration(node) && node.name?.text === "embed" && node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.DefaultKeyword) === true);
|
|
68
|
+
if (!declaration?.name)
|
|
69
|
+
return undefined;
|
|
70
|
+
const result = new MagicString(code);
|
|
71
|
+
result.overwrite(declaration.name.getStart(source), declaration.name.end, "LudicordEmbed");
|
|
72
|
+
// Preserve self-references and other local references to the authored function name.
|
|
73
|
+
result.appendLeft(declaration.end, "\nconst embed = LudicordEmbed;\n");
|
|
74
|
+
return { code: result.toString(), map: result.generateMap({ source: file, includeContent: true, hires: true }) };
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
const finished = {
|
|
78
|
+
name: "ludicord:compile-timing", enforce: "post",
|
|
79
|
+
transform(_code, id) {
|
|
80
|
+
const start = startTimes.get(id);
|
|
81
|
+
if (start !== undefined) {
|
|
82
|
+
startTimes.delete(id);
|
|
83
|
+
onCompile?.(path.relative(projectRoot, id.split("?", 1)[0] ?? id).split(path.sep).join("/"), Math.round(performance.now() - start));
|
|
84
|
+
}
|
|
85
|
+
return undefined;
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
const plugins = [boundaries, react({ exclude: /[\\/]node_modules[\\/]|[\\/]\.ludicord[\\/]/ }), finished];
|
|
89
|
+
const manifest = path.join(projectRoot, "node_modules", "@tailwindcss", "vite", "package.json");
|
|
90
|
+
if (existsSync(manifest)) {
|
|
91
|
+
const file = require.resolve("@tailwindcss/vite", { paths: [projectRoot] });
|
|
92
|
+
const tailwind = await import(pathToFileURL(file).href);
|
|
93
|
+
plugins.push(tailwind.default());
|
|
94
|
+
}
|
|
95
|
+
return plugins;
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=client-tooling.js.map
|
package/dist/compiler/scanner.js
CHANGED
|
@@ -133,11 +133,19 @@ export function validateActivityRoot(projectRoot) {
|
|
|
133
133
|
const layout = optionalFile("layout.tsx");
|
|
134
134
|
const loading = optionalFile("loading.tsx");
|
|
135
135
|
const error = optionalFile("error.tsx");
|
|
136
|
+
const extras = Object.fromEntries([
|
|
137
|
+
["minimize", "minimize.tsx"], ["authLoading", "auth/loading.tsx"],
|
|
138
|
+
["authError", "auth/error.tsx"], ["authDenied", "auth/denied.tsx"],
|
|
139
|
+
].flatMap(([key, file]) => {
|
|
140
|
+
const value = optionalFile(file);
|
|
141
|
+
return value === undefined ? [] : [[key, value]];
|
|
142
|
+
}));
|
|
136
143
|
return {
|
|
137
144
|
root: "app/pages.tsx",
|
|
138
145
|
...(layout === undefined ? {} : { layout }),
|
|
139
146
|
...(loading === undefined ? {} : { loading }),
|
|
140
147
|
...(error === undefined ? {} : { error }),
|
|
148
|
+
...extras,
|
|
141
149
|
};
|
|
142
150
|
}
|
|
143
151
|
export function scanEmbeds(projectRoot) {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { LudicordManifests } from "../manifests/types.js";
|
|
2
|
+
export declare function bundleServerGraph(root: string, output: string, manifests: LudicordManifests): Promise<{
|
|
3
|
+
bundles: {
|
|
4
|
+
source: string;
|
|
5
|
+
bundle: string;
|
|
6
|
+
kind: "api" | "ws";
|
|
7
|
+
}[];
|
|
8
|
+
dependencies: Set<string>;
|
|
9
|
+
loadModule<T>(absoluteFile: string): Promise<T>;
|
|
10
|
+
}>;
|
|
11
|
+
//# sourceMappingURL=server-bundle.d.ts.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { pathToFileURL } from "node:url";
|
|
3
|
+
import { build } from "esbuild";
|
|
4
|
+
export async function bundleServerGraph(root, output, manifests) {
|
|
5
|
+
const routes = [...manifests.api.map((route, index) => ({ ...route, name: `api/${index}` })),
|
|
6
|
+
...manifests.websocket.map((route, index) => ({ ...route, name: `ws/${index}` }))];
|
|
7
|
+
const bundles = routes.map((route) => ({ source: route.file, bundle: `${route.name}.mjs`, kind: route.kind }));
|
|
8
|
+
const dependencies = new Set();
|
|
9
|
+
if (routes.length > 0) {
|
|
10
|
+
const result = await build({
|
|
11
|
+
absWorkingDir: root,
|
|
12
|
+
entryPoints: routes.map((route) => ({ in: path.join(root, route.file), out: route.name })),
|
|
13
|
+
outdir: output, outExtension: { ".js": ".mjs" },
|
|
14
|
+
chunkNames: "shared/[name]-[hash]", platform: "node", format: "esm", target: "node20",
|
|
15
|
+
bundle: true, splitting: true, sourcemap: true, packages: "external", logLevel: "silent", metafile: true,
|
|
16
|
+
});
|
|
17
|
+
for (const input of Object.keys(result.metafile.inputs))
|
|
18
|
+
dependencies.add(path.resolve(root, input));
|
|
19
|
+
}
|
|
20
|
+
const bySource = new Map(bundles.map((route) => [path.resolve(root, route.source), path.join(output, route.bundle)]));
|
|
21
|
+
return {
|
|
22
|
+
bundles, dependencies,
|
|
23
|
+
async loadModule(absoluteFile) {
|
|
24
|
+
const bundle = bySource.get(path.resolve(absoluteFile));
|
|
25
|
+
if (bundle === undefined)
|
|
26
|
+
throw new Error("LUDICORD3003: Server route has no compiled module.");
|
|
27
|
+
return import(pathToFileURL(bundle).href);
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=server-bundle.js.map
|
|
@@ -41,6 +41,7 @@ function generatedSource(manifests, config) {
|
|
|
41
41
|
const params = manifests.embeds
|
|
42
42
|
.map((entry) => ` readonly ${JSON.stringify(entry.route)}: ${paramsType(entry)};`)
|
|
43
43
|
.join("\n");
|
|
44
|
+
const routeParams = (entries) => entries.map((entry) => ` readonly ${JSON.stringify(entry.route)}: ${paramsType(entry)};`).join("\n");
|
|
44
45
|
const embedPatterns = manifests.embeds.map(({ route }) => JSON.stringify(route));
|
|
45
46
|
return `// Generated by Ludicord. Changes will be replaced by ludicord dev or ludicord build.
|
|
46
47
|
import "ludicord/navigation";
|
|
@@ -53,6 +54,12 @@ declare module "ludicord/navigation" {
|
|
|
53
54
|
readonly websocket: ${routeUnion(manifests.websocket)};
|
|
54
55
|
readonly embedParams: {
|
|
55
56
|
${params}
|
|
57
|
+
};
|
|
58
|
+
readonly apiParams: {
|
|
59
|
+
${routeParams(manifests.api)}
|
|
60
|
+
};
|
|
61
|
+
readonly websocketParams: {
|
|
62
|
+
${routeParams(manifests.websocket)}
|
|
56
63
|
};
|
|
57
64
|
readonly config: {
|
|
58
65
|
readonly defaultEmbed: ${JSON.stringify(config.activity.defaultEmbed)};
|
|
@@ -76,8 +83,10 @@ export function generateLudicordTypes(projectRoot, manifests, config) {
|
|
|
76
83
|
const publicFile = path.join(root, "ludicord.generated.d.ts");
|
|
77
84
|
const internalFile = path.join(root, ".ludicord", "types", "routes.d.ts");
|
|
78
85
|
mkdirSync(path.dirname(internalFile), { recursive: true });
|
|
79
|
-
|
|
80
|
-
|
|
86
|
+
if (!existsSync(publicFile) || readFileSync(publicFile, "utf8") !== source)
|
|
87
|
+
writeFileSync(publicFile, source, "utf8");
|
|
88
|
+
if (!existsSync(internalFile) || readFileSync(internalFile, "utf8") !== source)
|
|
89
|
+
writeFileSync(internalFile, source, "utf8");
|
|
81
90
|
ensureLudicordEnv(root);
|
|
82
91
|
return publicFile;
|
|
83
92
|
}
|
package/dist/config/defaults.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export function resolveConfig(config = {}) {
|
|
2
|
-
const clientId = config.discord?.clientId;
|
|
3
|
-
|
|
2
|
+
const clientId = config.discord?.clientId ?? process.env.LUDICORD_DISCORD_CLIENT_ID;
|
|
3
|
+
const resolved = {
|
|
4
4
|
discord: {
|
|
5
5
|
...(clientId === undefined ? {} : { clientId }),
|
|
6
6
|
scopes: [...(config.discord?.scopes ?? ["identify"])],
|
|
@@ -36,5 +36,31 @@ export function resolveConfig(config = {}) {
|
|
|
36
36
|
},
|
|
37
37
|
},
|
|
38
38
|
};
|
|
39
|
+
const fail = (field) => { throw new TypeError("LUDICORD0004: Invalid " + field + " in ludicord.config.mjs."); };
|
|
40
|
+
const integer = (value, min, max, field) => { if (!Number.isSafeInteger(value) || value < min || value > max)
|
|
41
|
+
fail(field); };
|
|
42
|
+
integer(resolved.server.port, 0, 65535, "server.port");
|
|
43
|
+
integer(resolved.websocket.heartbeatInterval, 1, 2_147_483_647, "websocket.heartbeatInterval");
|
|
44
|
+
integer(resolved.websocket.maxPayload, 1, 1_073_741_824, "websocket.maxPayload");
|
|
45
|
+
integer(resolved.websocket.reconnect.attempts, 0, 10000, "websocket.reconnect.attempts");
|
|
46
|
+
integer(resolved.websocket.reconnect.initialDelay, 0, 2_147_483_647, "websocket.reconnect.initialDelay");
|
|
47
|
+
integer(resolved.websocket.reconnect.maxDelay, resolved.websocket.reconnect.initialDelay, 2_147_483_647, "websocket.reconnect.maxDelay");
|
|
48
|
+
if (!["allow", "mock", "error"].includes(resolved.activity.outsideDiscord))
|
|
49
|
+
fail("activity.outsideDiscord");
|
|
50
|
+
if (typeof resolved.activity.defaultEmbed !== "string" || !resolved.activity.defaultEmbed.trim())
|
|
51
|
+
fail("activity.defaultEmbed");
|
|
52
|
+
if (typeof resolved.server.host !== "string" || !resolved.server.host.trim())
|
|
53
|
+
fail("server.host");
|
|
54
|
+
if (resolved.server.allowedHosts !== true && (!Array.isArray(resolved.server.allowedHosts) || resolved.server.allowedHosts.some((host) => typeof host !== "string" || !host.trim())))
|
|
55
|
+
fail("server.allowedHosts");
|
|
56
|
+
if (resolved.discord.auth.session !== "encrypted-cookie")
|
|
57
|
+
fail("discord.auth.session");
|
|
58
|
+
if (!Array.isArray(resolved.discord.scopes) || resolved.discord.scopes.some((scope) => typeof scope !== "string" || !scope.trim()))
|
|
59
|
+
fail("discord.scopes");
|
|
60
|
+
for (const [field, value] of Object.entries({ "discord.auth.required": resolved.discord.auth.required, "discord.auth.proxyVerification": resolved.discord.auth.proxyVerification, "discord.auth.activityInstanceVerification": resolved.discord.auth.activityInstanceVerification, "websocket.enabled": resolved.websocket.enabled, "websocket.compression": resolved.websocket.compression, "websocket.reconnect.enabled": resolved.websocket.reconnect.enabled })) {
|
|
61
|
+
if (typeof value !== "boolean")
|
|
62
|
+
fail(field);
|
|
63
|
+
}
|
|
64
|
+
return resolved;
|
|
39
65
|
}
|
|
40
66
|
//# sourceMappingURL=defaults.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { loadEnv } from "vite";
|
|
2
|
+
// Keep explicit shell values authoritative, while allowing .env edits between commands.
|
|
3
|
+
const loadedValues = new Map();
|
|
4
|
+
export function loadProjectEnvironment(projectRoot, mode) {
|
|
5
|
+
for (const [name, value] of loadedValues) {
|
|
6
|
+
if (process.env[name] === value)
|
|
7
|
+
delete process.env[name];
|
|
8
|
+
}
|
|
9
|
+
loadedValues.clear();
|
|
10
|
+
const environment = loadEnv(mode, projectRoot, "");
|
|
11
|
+
for (const [name, value] of Object.entries(environment)) {
|
|
12
|
+
if (process.env[name] === undefined) {
|
|
13
|
+
process.env[name] = value;
|
|
14
|
+
loadedValues.set(name, value);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=environment.js.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { DiscordSDK } from "@discord/embedded-app-sdk";
|
|
2
|
+
import type { LudicordGuildMember } from "./events.js";
|
|
3
|
+
type Commands = DiscordSDK["commands"];
|
|
4
|
+
export type LudicordDiscordCommand = Exclude<keyof Commands, "authorize" | "authenticate">;
|
|
5
|
+
export declare function useDiscordCommands(): {
|
|
6
|
+
supports(name: LudicordDiscordCommand): boolean;
|
|
7
|
+
call<T extends LudicordDiscordCommand>(name: T, ...args: Parameters<Commands[T]>): Promise<Awaited<ReturnType<Commands[T]>>>;
|
|
8
|
+
};
|
|
9
|
+
export declare function useDiscordGuildMembers(options?: {
|
|
10
|
+
readonly limit?: number;
|
|
11
|
+
readonly enabled?: boolean;
|
|
12
|
+
}): {
|
|
13
|
+
readonly members: readonly LudicordGuildMember[];
|
|
14
|
+
readonly status: "idle" | "ready" | "error" | "loading";
|
|
15
|
+
readonly error: Error | null;
|
|
16
|
+
readonly hasMore: boolean;
|
|
17
|
+
readonly refresh: () => void;
|
|
18
|
+
readonly loadMore: () => void;
|
|
19
|
+
};
|
|
20
|
+
export {};
|
|
21
|
+
//# sourceMappingURL=data.d.ts.map
|