@air-jam/server 0.1.4 → 0.9.0

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.
@@ -0,0 +1,121 @@
1
+ import {
2
+ getAirJamDevProxyOptions,
3
+ getAirJamHttpsServerOptions,
4
+ } from "./vite-https.mjs";
5
+
6
+ export const AIR_JAM_IFRAME_HEADERS = {
7
+ // Air Jam games must be embeddable inside the local Arcade shell.
8
+ "Content-Security-Policy": "frame-ancestors *;",
9
+ };
10
+
11
+ const AIR_JAM_VITE_PROFILES = new Set(["default", "three"]);
12
+ const DEFAULT_CHUNK_WARNING_LIMIT_KB = 1000;
13
+ const THREE_PROFILE_CHUNK_WARNING_LIMIT_KB = 2500;
14
+
15
+ const resolveThreeProfileManualChunk = (id) => {
16
+ if (id.includes("/zod/") || id.includes("/packages/sdk/src/")) {
17
+ // Keep SDK runtime dependencies with the SDK chunk so schema evaluation
18
+ // cannot race a separate vendor chunk during embedded production startup.
19
+ return "airjam-sdk";
20
+ }
21
+
22
+ if (!id.includes("node_modules")) {
23
+ return undefined;
24
+ }
25
+
26
+ if (
27
+ id.includes("@dimforge/rapier3d-compat") ||
28
+ id.includes("@react-three/rapier")
29
+ ) {
30
+ return "rapier-runtime";
31
+ }
32
+
33
+ if (
34
+ id.includes("@react-three/drei") ||
35
+ id.includes("three-stdlib") ||
36
+ id.includes("@use-gesture") ||
37
+ id.includes("@react-spring")
38
+ ) {
39
+ return "drei-runtime";
40
+ }
41
+
42
+ if (id.includes("@react-three/fiber")) {
43
+ return "fiber-runtime";
44
+ }
45
+
46
+ if (id.includes("/three/")) {
47
+ return "three-core";
48
+ }
49
+
50
+ if (
51
+ id.includes("/react/") ||
52
+ id.includes("/react-dom/") ||
53
+ id.includes("react-router-dom")
54
+ ) {
55
+ // Keep the core React/app runtime in one chunk for the three profile.
56
+ // Splitting React away from state/router/runtime helpers created a
57
+ // circular startup edge in built embeds.
58
+ return "app-runtime";
59
+ }
60
+
61
+ if (
62
+ id.includes("/zustand/") ||
63
+ id.includes("/howler/") ||
64
+ id.includes("/socket.io-client/") ||
65
+ id.includes("/qrcode/")
66
+ ) {
67
+ return "app-runtime";
68
+ }
69
+
70
+ return undefined;
71
+ };
72
+
73
+ const resolveBuildConfig = (profile) => {
74
+ if (profile === "three") {
75
+ return {
76
+ chunkSizeWarningLimit: THREE_PROFILE_CHUNK_WARNING_LIMIT_KB,
77
+ rollupOptions: {
78
+ output: {
79
+ manualChunks: resolveThreeProfileManualChunk,
80
+ },
81
+ },
82
+ };
83
+ }
84
+
85
+ return {
86
+ chunkSizeWarningLimit: DEFAULT_CHUNK_WARNING_LIMIT_KB,
87
+ };
88
+ };
89
+
90
+ const createSharedDevServerConfig = ({ env, port }) => ({
91
+ host: true,
92
+ allowedHosts: true,
93
+ https: getAirJamHttpsServerOptions(env),
94
+ port,
95
+ strictPort: true,
96
+ headers: AIR_JAM_IFRAME_HEADERS,
97
+ cors: true,
98
+ });
99
+
100
+ export const createAirJamViteConfig = ({
101
+ env = process.env,
102
+ port = Number(env.VITE_PORT || 5173),
103
+ profile = "default",
104
+ } = {}) => {
105
+ if (!AIR_JAM_VITE_PROFILES.has(profile)) {
106
+ throw new Error(
107
+ `Unsupported Air Jam Vite profile "${profile}". Use "default" or "three".`,
108
+ );
109
+ }
110
+
111
+ const build = resolveBuildConfig(profile);
112
+
113
+ return {
114
+ build,
115
+ server: {
116
+ ...createSharedDevServerConfig({ env, port }),
117
+ proxy: getAirJamDevProxyOptions(env),
118
+ },
119
+ preview: createSharedDevServerConfig({ env, port }),
120
+ };
121
+ };
@@ -0,0 +1,14 @@
1
+ import type { ServerOptions } from "node:https";
2
+ import type { ProxyOptions } from "vite";
3
+
4
+ export const DEFAULT_AIR_JAM_DEV_BACKEND_URL: string;
5
+
6
+ export function getAirJamHttpsServerOptions(
7
+ env?: NodeJS.ProcessEnv,
8
+ ): ServerOptions | undefined;
9
+
10
+ export function getAirJamDevBackendUrl(env?: NodeJS.ProcessEnv): string;
11
+
12
+ export function getAirJamDevProxyOptions(
13
+ env?: NodeJS.ProcessEnv,
14
+ ): Record<string, ProxyOptions>;
@@ -0,0 +1,39 @@
1
+ import fs from "node:fs";
2
+
3
+ export const DEFAULT_AIR_JAM_DEV_BACKEND_URL = "http://127.0.0.1:4000";
4
+
5
+ export const getAirJamHttpsServerOptions = (env = process.env) => {
6
+ const certFile = env.AIR_JAM_DEV_CERT_FILE;
7
+ const keyFile = env.AIR_JAM_DEV_KEY_FILE;
8
+ if (!certFile || !keyFile) {
9
+ return undefined;
10
+ }
11
+
12
+ if (!fs.existsSync(certFile) || !fs.existsSync(keyFile)) {
13
+ return undefined;
14
+ }
15
+
16
+ return {
17
+ cert: fs.readFileSync(certFile),
18
+ key: fs.readFileSync(keyFile),
19
+ };
20
+ };
21
+
22
+ export const getAirJamDevBackendUrl = (env = process.env) =>
23
+ env.AIR_JAM_DEV_PROXY_BACKEND_URL?.trim() || DEFAULT_AIR_JAM_DEV_BACKEND_URL;
24
+
25
+ export const getAirJamDevProxyOptions = (env = process.env) => {
26
+ const target = getAirJamDevBackendUrl(env);
27
+
28
+ return {
29
+ "/socket.io": {
30
+ target,
31
+ ws: true,
32
+ changeOrigin: true,
33
+ },
34
+ "/__airjam": {
35
+ target,
36
+ changeOrigin: true,
37
+ },
38
+ };
39
+ };