create-airjam 0.1.7 → 0.2.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.
- package/dist/index.js +12 -4
- package/package.json +10 -8
- package/templates/pong/.env.example +7 -0
- package/templates/pong/{AI_INSTRUCTIONS.md → AGENTS.md} +9 -2
- package/templates/pong/README.md +38 -3
- package/templates/pong/airjam-docs/for-agents/page.md +27 -0
- package/templates/pong/airjam-docs/getting-started/architecture/page.md +4 -1
- package/templates/pong/airjam-docs/getting-started/introduction/page.md +56 -38
- package/templates/pong/airjam-docs/getting-started/quick-start/page.md +108 -0
- package/templates/pong/airjam-docs/how-it-works/architecture/page.md +168 -0
- package/templates/pong/airjam-docs/how-it-works/host-system/page.md +20 -10
- package/templates/pong/airjam-docs/sdk/hooks/page.md +13 -11
- package/templates/pong/airjam-docs/sdk/networked-state/page.md +1 -1
- package/templates/pong/package.json +6 -3
- package/templates/pong/scripts/dev-secure.mjs +177 -0
- package/templates/pong/scripts/secure-init.mjs +134 -0
- package/templates/pong/src/App.tsx +2 -0
- package/templates/pong/src/controller-view.tsx +78 -99
- package/templates/pong/src/host-view.tsx +3 -18
- package/templates/pong/src/vite-env.d.ts +1 -0
- package/templates/pong/vite.config.ts +9 -0
- package/templates/pong/dist/assets/index-BwJ4uU20.js +0 -269
- package/templates/pong/dist/assets/index-D42jDbB5.css +0 -1
- package/templates/pong/dist/index.html +0 -19
- package/templates/pong/node_modules/.bin/air-jam-server +0 -17
- package/templates/pong/node_modules/.bin/eslint +0 -17
- package/templates/pong/node_modules/.bin/eslint-config-prettier +0 -17
- package/templates/pong/node_modules/.bin/tsc +0 -17
- package/templates/pong/node_modules/.bin/tsserver +0 -17
- package/templates/pong/node_modules/.bin/vite +0 -17
- package/templates/pong/node_modules/.vite/deps/@radix-ui_react-dialog.js +0 -1680
- package/templates/pong/node_modules/.vite/deps/@radix-ui_react-dialog.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/@radix-ui_react-slider.js +0 -638
- package/templates/pong/node_modules/.vite/deps/@radix-ui_react-slider.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/@radix-ui_react-slot.js +0 -129
- package/templates/pong/node_modules/.vite/deps/@radix-ui_react-slot.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/@radix-ui_react-tabs.js +0 -434
- package/templates/pong/node_modules/.vite/deps/@radix-ui_react-tabs.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/_metadata.json +0 -163
- package/templates/pong/node_modules/.vite/deps/chunk-6G72RLJL.js +0 -1004
- package/templates/pong/node_modules/.vite/deps/chunk-6G72RLJL.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/chunk-BHOBIW4J.js +0 -99
- package/templates/pong/node_modules/.vite/deps/chunk-BHOBIW4J.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/chunk-E44IQKDY.js +0 -49
- package/templates/pong/node_modules/.vite/deps/chunk-E44IQKDY.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/chunk-E5X4SHYG.js +0 -21
- package/templates/pong/node_modules/.vite/deps/chunk-E5X4SHYG.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/chunk-G3PMV62Z.js +0 -35
- package/templates/pong/node_modules/.vite/deps/chunk-G3PMV62Z.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/chunk-LI6KBZ3D.js +0 -292
- package/templates/pong/node_modules/.vite/deps/chunk-LI6KBZ3D.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/chunk-MDNXVKSA.js +0 -337
- package/templates/pong/node_modules/.vite/deps/chunk-MDNXVKSA.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/chunk-METQ356G.js +0 -280
- package/templates/pong/node_modules/.vite/deps/chunk-METQ356G.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/chunk-NPTAWNEA.js +0 -171
- package/templates/pong/node_modules/.vite/deps/chunk-NPTAWNEA.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/class-variance-authority.js +0 -51
- package/templates/pong/node_modules/.vite/deps/class-variance-authority.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/clsx.js +0 -9
- package/templates/pong/node_modules/.vite/deps/clsx.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/howler.js +0 -2241
- package/templates/pong/node_modules/.vite/deps/howler.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/html5-qrcode.js +0 -29350
- package/templates/pong/node_modules/.vite/deps/html5-qrcode.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/lucide-react.js +0 -28662
- package/templates/pong/node_modules/.vite/deps/lucide-react.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/package.json +0 -3
- package/templates/pong/node_modules/.vite/deps/qrcode.js +0 -2083
- package/templates/pong/node_modules/.vite/deps/qrcode.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/react-dom.js +0 -6
- package/templates/pong/node_modules/.vite/deps/react-dom.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/react-dom_client.js +0 -20217
- package/templates/pong/node_modules/.vite/deps/react-dom_client.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/react-router-dom.js +0 -13900
- package/templates/pong/node_modules/.vite/deps/react-router-dom.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/react.js +0 -5
- package/templates/pong/node_modules/.vite/deps/react.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/react_jsx-dev-runtime.js +0 -278
- package/templates/pong/node_modules/.vite/deps/react_jsx-dev-runtime.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/react_jsx-runtime.js +0 -6
- package/templates/pong/node_modules/.vite/deps/react_jsx-runtime.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/socket__io-client.js +0 -3422
- package/templates/pong/node_modules/.vite/deps/socket__io-client.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/tailwind-merge.js +0 -2534
- package/templates/pong/node_modules/.vite/deps/tailwind-merge.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/zod.js +0 -13810
- package/templates/pong/node_modules/.vite/deps/zod.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/zustand.js +0 -56
- package/templates/pong/node_modules/.vite/deps/zustand.js.map +0 -7
- package/templates/pong/node_modules/.vite/deps/zustand_react_shallow.js +0 -77
- package/templates/pong/node_modules/.vite/deps/zustand_react_shallow.js.map +0 -7
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# Architecture
|
|
2
|
+
|
|
3
|
+
Air Jam consists of four main components in a monorepo structure. This page explains how they work together to enable multiplayer gaming with smartphone controllers.
|
|
4
|
+
|
|
5
|
+
## System Overview
|
|
6
|
+
|
|
7
|
+
## Components
|
|
8
|
+
|
|
9
|
+
### 1. Platform (`apps/platform`)
|
|
10
|
+
|
|
11
|
+
**Role:** Central hub for the Air Jam ecosystem
|
|
12
|
+
|
|
13
|
+
**Technology:** Next.js 15, TypeScript, tRPC, BetterAuth, PostgreSQL (Drizzle ORM)
|
|
14
|
+
|
|
15
|
+
**Key Features:**
|
|
16
|
+
|
|
17
|
+
- **Developer Portal** - Account management, API key generation, analytics
|
|
18
|
+
- **Game Catalog** - Submit, manage, and discover Air Jam games
|
|
19
|
+
- **Arcade Mode** - Browse and launch games from a unified interface
|
|
20
|
+
- **Controller Shell** - Persistent mobile wrapper that loads game controllers
|
|
21
|
+
|
|
22
|
+
**Arcade Mode Flow:**
|
|
23
|
+
|
|
24
|
+
```
|
|
25
|
+
1. Player scans QR on arcade screen
|
|
26
|
+
2. Platform loads controller shell on phone
|
|
27
|
+
3. Player browses games using phone as remote
|
|
28
|
+
4. Game launches → controller switches to game's controller
|
|
29
|
+
5. Game ends → returns to arcade browser
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### 2. Server (`packages/server`)
|
|
33
|
+
|
|
34
|
+
**Role:** Real-time communication backbone
|
|
35
|
+
|
|
36
|
+
**Technology:** Node.js, Express, Socket.IO, PostgreSQL
|
|
37
|
+
|
|
38
|
+
**Core Services:**
|
|
39
|
+
|
|
40
|
+
**Socket Events:**
|
|
41
|
+
|
|
42
|
+
### 3. SDK (`packages/sdk`)
|
|
43
|
+
|
|
44
|
+
**Role:** Developer toolkit for building Air Jam games
|
|
45
|
+
|
|
46
|
+
**Technology:** React, TypeScript, Socket.IO Client, Zustand, Zod
|
|
47
|
+
|
|
48
|
+
**Architecture:**
|
|
49
|
+
|
|
50
|
+
**Key Design Decisions:**
|
|
51
|
+
|
|
52
|
+
1. **Provider Pattern** - Single provider for configuration, multiple hooks for access
|
|
53
|
+
2. **Lightweight Hooks** - `useGetInput`, `useSendSignal` don't trigger re-renders
|
|
54
|
+
3. **Input Latching** - Ensures rapid button taps are never missed
|
|
55
|
+
4. **Schema Validation** - Type-safe input with runtime validation
|
|
56
|
+
|
|
57
|
+
### 4. Prototype Game (`apps/prototype-game`)
|
|
58
|
+
|
|
59
|
+
**Role:** Reference implementation showcasing SDK capabilities
|
|
60
|
+
|
|
61
|
+
**Technology:** React, Vite, React Three Fiber, Rapier Physics
|
|
62
|
+
|
|
63
|
+
**Demonstrates:**
|
|
64
|
+
|
|
65
|
+
- Host-side game logic with physics
|
|
66
|
+
- Controller UI with joystick and buttons
|
|
67
|
+
- Player spawning/despawning on join/leave
|
|
68
|
+
- Haptic feedback on collisions
|
|
69
|
+
- Multiple game modes (CTF, survival)
|
|
70
|
+
|
|
71
|
+
## Run Modes
|
|
72
|
+
|
|
73
|
+
The SDK automatically detects and adapts to different deployment scenarios:
|
|
74
|
+
|
|
75
|
+
### Standalone Mode
|
|
76
|
+
|
|
77
|
+
Your game runs independently with direct WebSocket connection.
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
|
|
81
|
+
│ Your Game │◀───▶│ AirJam │◀───▶│ Controller │
|
|
82
|
+
│ (Host) │ │ Server │ │ (Phone) │
|
|
83
|
+
└──────────────┘ └──────────────┘ └──────────────┘
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
**Use case:** Self-hosted games, development, custom deployments
|
|
87
|
+
|
|
88
|
+
### Arcade Mode
|
|
89
|
+
|
|
90
|
+
Game runs inside an iframe on the Air Jam Platform.
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
┌─────────────────────────────────────────┐
|
|
94
|
+
│ Air Jam Platform (Parent) │
|
|
95
|
+
│ ┌───────────────────────────────────┐ │
|
|
96
|
+
│ │ Your Game (iframe) │ │
|
|
97
|
+
│ │ • Receives join token │ │
|
|
98
|
+
│ │ • Controlled player routing │ │
|
|
99
|
+
│ └───────────────────────────────────┘ │
|
|
100
|
+
└─────────────────────────────────────────┘
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
**Use case:** Featured on Air Jam arcade, game discovery
|
|
104
|
+
|
|
105
|
+
### Bridge Mode
|
|
106
|
+
|
|
107
|
+
Controller runs inside platform's shell (iframe communication).
|
|
108
|
+
|
|
109
|
+
```
|
|
110
|
+
┌─────────────────────────────────────────┐
|
|
111
|
+
│ Platform Controller Shell (Parent) │
|
|
112
|
+
│ ┌───────────────────────────────────┐ │
|
|
113
|
+
│ │ Your Controller UI (iframe) │ │
|
|
114
|
+
│ │ • Input via postMessage │ │
|
|
115
|
+
│ │ • Seamless game switching │ │
|
|
116
|
+
│ └───────────────────────────────────┘ │
|
|
117
|
+
└─────────────────────────────────────────┘
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
**Use case:** Arcade mode controllers, persistent session
|
|
121
|
+
|
|
122
|
+
## Data Flow
|
|
123
|
+
|
|
124
|
+
### Input Path (Controller → Game)
|
|
125
|
+
|
|
126
|
+
```
|
|
127
|
+
1. Player moves joystick on phone
|
|
128
|
+
2. Controller calls sendInput({ vector: {x, y}, action: false })
|
|
129
|
+
3. SDK sends controller:input event to server
|
|
130
|
+
4. Server routes to host socket
|
|
131
|
+
5. Host SDK's InputManager receives input
|
|
132
|
+
6. InputManager validates with Zod schema
|
|
133
|
+
7. InputManager applies latching if configured
|
|
134
|
+
8. Game calls getInput(playerId) in game loop
|
|
135
|
+
9. Returns typed, validated, latched input
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Signal Path (Game → Controller)
|
|
139
|
+
|
|
140
|
+
```
|
|
141
|
+
1. Game detects collision
|
|
142
|
+
2. Host calls sendSignal("HAPTIC", { pattern: "heavy" }, playerId)
|
|
143
|
+
3. SDK sends host:signal event to server
|
|
144
|
+
4. Server routes to target controller(s)
|
|
145
|
+
5. Controller SDK receives signal
|
|
146
|
+
6. SDK triggers navigator.vibrate() with pattern
|
|
147
|
+
7. Player feels haptic feedback
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Security
|
|
151
|
+
|
|
152
|
+
### API Key Authentication
|
|
153
|
+
|
|
154
|
+
- Games must provide valid API key for production
|
|
155
|
+
- Keys are validated against platform database
|
|
156
|
+
- Rate limiting prevents abuse
|
|
157
|
+
|
|
158
|
+
### Room Isolation
|
|
159
|
+
|
|
160
|
+
- Each room has unique 4-character code
|
|
161
|
+
- Controllers can only join with correct code
|
|
162
|
+
- Input is only routed to the designated host
|
|
163
|
+
|
|
164
|
+
### Input Validation
|
|
165
|
+
|
|
166
|
+
- Zod schemas validate all incoming input
|
|
167
|
+
- Invalid input is rejected with console warning
|
|
168
|
+
- Protects game logic from malformed data
|
|
@@ -11,18 +11,28 @@ The simplest mode—your game connects directly to the Air Jam server.
|
|
|
11
11
|
**Usage:**
|
|
12
12
|
|
|
13
13
|
```tsx filename="src/components/HostView.tsx"
|
|
14
|
-
|
|
15
|
-
onPlayerJoin: (player) => spawnPlayer(player),
|
|
16
|
-
onPlayerLeave: (id) => removePlayer(id),
|
|
17
|
-
});
|
|
14
|
+
import { HostShell, useAirJamHost } from "@air-jam/sdk";
|
|
18
15
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
processInput(p.id, input);
|
|
16
|
+
const HostView = () => {
|
|
17
|
+
const host = useAirJamHost({
|
|
18
|
+
onPlayerJoin: (player) => spawnPlayer(player),
|
|
19
|
+
onPlayerLeave: (id) => removePlayer(id),
|
|
24
20
|
});
|
|
25
|
-
|
|
21
|
+
|
|
22
|
+
// Host receives all input directly
|
|
23
|
+
useFrame(() => {
|
|
24
|
+
host.players.forEach((p) => {
|
|
25
|
+
const input = host.getInput(p.id);
|
|
26
|
+
processInput(p.id, input);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<HostShell>
|
|
32
|
+
<GameCanvas />
|
|
33
|
+
</HostShell>
|
|
34
|
+
);
|
|
35
|
+
};
|
|
26
36
|
```
|
|
27
37
|
|
|
28
38
|
### Arcade Mode (Two-Host Model)
|
|
@@ -54,7 +54,7 @@ The provider automatically reads from these environment variables if props aren'
|
|
|
54
54
|
The primary hook for game hosts. Connects to the server, manages players, and provides input access.
|
|
55
55
|
|
|
56
56
|
```tsx
|
|
57
|
-
import { useAirJamHost } from "@air-jam/sdk";
|
|
57
|
+
import { HostShell, useAirJamHost } from "@air-jam/sdk";
|
|
58
58
|
|
|
59
59
|
const HostView = () => {
|
|
60
60
|
const host = useAirJamHost({
|
|
@@ -91,13 +91,15 @@ const HostView = () => {
|
|
|
91
91
|
} = host;
|
|
92
92
|
|
|
93
93
|
return (
|
|
94
|
-
<
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
94
|
+
<HostShell>
|
|
95
|
+
{/* HostShell automatically provides:
|
|
96
|
+
- Top navbar with room ID and player avatars
|
|
97
|
+
- QR code overlay when paused
|
|
98
|
+
- Play/pause controls
|
|
99
|
+
- Settings panel
|
|
100
|
+
*/}
|
|
101
|
+
<GameCanvas />
|
|
102
|
+
</HostShell>
|
|
101
103
|
);
|
|
102
104
|
};
|
|
103
105
|
```
|
|
@@ -221,7 +223,7 @@ const Laser = ({ ownerId }: { ownerId: string }) => {
|
|
|
221
223
|
Hook for building mobile controllers that connect to game hosts.
|
|
222
224
|
|
|
223
225
|
```tsx
|
|
224
|
-
import { useAirJamController } from "@air-jam/sdk";
|
|
226
|
+
import { ControllerShell, useAirJamController } from "@air-jam/sdk";
|
|
225
227
|
|
|
226
228
|
const ControllerView = () => {
|
|
227
229
|
const controller = useAirJamController({
|
|
@@ -266,7 +268,7 @@ const ControllerView = () => {
|
|
|
266
268
|
}
|
|
267
269
|
|
|
268
270
|
return (
|
|
269
|
-
<
|
|
271
|
+
<ControllerShell forceOrientation="landscape">
|
|
270
272
|
<Joystick
|
|
271
273
|
onMove={(x, y) => {
|
|
272
274
|
controller.sendInput({
|
|
@@ -285,7 +287,7 @@ const ControllerView = () => {
|
|
|
285
287
|
});
|
|
286
288
|
}}
|
|
287
289
|
/>
|
|
288
|
-
</
|
|
290
|
+
</ControllerShell>
|
|
289
291
|
);
|
|
290
292
|
};
|
|
291
293
|
```
|
|
@@ -572,4 +572,4 @@ const useGameStore = createAirJamStore<GameState>((set) => ({
|
|
|
572
572
|
|
|
573
573
|
- [SDK Hooks](/docs/sdk/hooks) - Other SDK hooks and utilities
|
|
574
574
|
- [Input System](/docs/sdk/input-system) - Handling controller input
|
|
575
|
-
- [Architecture](/docs/
|
|
575
|
+
- [Architecture](/docs/how-it-works/architecture) - System overview
|
|
@@ -6,12 +6,14 @@
|
|
|
6
6
|
"scripts": {
|
|
7
7
|
"dev": "vite",
|
|
8
8
|
"dev:server": "air-jam-server",
|
|
9
|
+
"secure:init": "node ./scripts/secure-init.mjs",
|
|
10
|
+
"dev:secure": "node ./scripts/dev-secure.mjs",
|
|
9
11
|
"build": "tsc && vite build",
|
|
10
12
|
"preview": "vite preview",
|
|
11
13
|
"typecheck": "tsc --noEmit"
|
|
12
14
|
},
|
|
13
15
|
"dependencies": {
|
|
14
|
-
"@air-jam/sdk": "^0.1.
|
|
16
|
+
"@air-jam/sdk": "^0.1.4",
|
|
15
17
|
"react": "^19.0.0",
|
|
16
18
|
"react-dom": "^19.0.0",
|
|
17
19
|
"react-router-dom": "^7.0.0",
|
|
@@ -21,10 +23,10 @@
|
|
|
21
23
|
"@air-jam/server": "^0.1.3",
|
|
22
24
|
"@eslint/js": "^9.39.1",
|
|
23
25
|
"@tailwindcss/vite": "^4.0.0",
|
|
26
|
+
"@types/node": "^22.0.0",
|
|
24
27
|
"@types/react": "^19.0.0",
|
|
25
28
|
"@types/react-dom": "^19.0.0",
|
|
26
29
|
"@vitejs/plugin-react": "^4.3.0",
|
|
27
|
-
"@types/node": "^22.0.0",
|
|
28
30
|
"eslint": "^9.39.1",
|
|
29
31
|
"eslint-config-prettier": "^10.1.8",
|
|
30
32
|
"eslint-plugin-react-hooks": "^7.0.1",
|
|
@@ -33,6 +35,7 @@
|
|
|
33
35
|
"tailwindcss": "^4.0.0",
|
|
34
36
|
"typescript": "~5.6.0",
|
|
35
37
|
"typescript-eslint": "^8.46.4",
|
|
36
|
-
"vite": "^6.0.0"
|
|
38
|
+
"vite": "^6.0.0",
|
|
39
|
+
"cloudflared": "^0.7.1"
|
|
37
40
|
}
|
|
38
41
|
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { spawn } from "node:child_process";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import net from "node:net";
|
|
6
|
+
|
|
7
|
+
const SERVER_PORT = 4000;
|
|
8
|
+
const GAME_PORT = 5173;
|
|
9
|
+
const START_TIMEOUT_MS = 20_000;
|
|
10
|
+
|
|
11
|
+
const children = [];
|
|
12
|
+
|
|
13
|
+
function loadEnvFile(filePath) {
|
|
14
|
+
if (!fs.existsSync(filePath)) return;
|
|
15
|
+
const content = fs.readFileSync(filePath, "utf8");
|
|
16
|
+
for (const rawLine of content.split("\n")) {
|
|
17
|
+
const line = rawLine.trim();
|
|
18
|
+
if (!line || line.startsWith("#")) continue;
|
|
19
|
+
const eq = line.indexOf("=");
|
|
20
|
+
if (eq <= 0) continue;
|
|
21
|
+
const key = line.slice(0, eq).trim();
|
|
22
|
+
const value = line.slice(eq + 1).trim();
|
|
23
|
+
if (!process.env[key]) {
|
|
24
|
+
process.env[key] = value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function logPrefix(prefix, data) {
|
|
30
|
+
const text = data.toString();
|
|
31
|
+
for (const line of text.split("\n")) {
|
|
32
|
+
if (!line.trim()) continue;
|
|
33
|
+
console.log(`[${prefix}] ${line}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function run(name, command, args, options = {}) {
|
|
38
|
+
const { fatal = true, ...spawnOptions } = options;
|
|
39
|
+
const child = spawn(command, args, {
|
|
40
|
+
stdio: ["inherit", "pipe", "pipe"],
|
|
41
|
+
...spawnOptions,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
child.stdout.on("data", (data) => logPrefix(name, data));
|
|
45
|
+
child.stderr.on("data", (data) => logPrefix(name, data));
|
|
46
|
+
child.on("exit", (code) => {
|
|
47
|
+
if (fatal && code !== 0) {
|
|
48
|
+
console.error(`[${name}] exited with code ${code}`);
|
|
49
|
+
shutdown(code ?? 1);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
children.push(child);
|
|
54
|
+
return child;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function isPortOpen(port) {
|
|
58
|
+
return new Promise((resolve) => {
|
|
59
|
+
const socket = net.createConnection({ port, host: "127.0.0.1" });
|
|
60
|
+
const done = (value) => {
|
|
61
|
+
socket.removeAllListeners();
|
|
62
|
+
socket.destroy();
|
|
63
|
+
resolve(value);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
socket.setTimeout(400);
|
|
67
|
+
socket.on("connect", () => done(true));
|
|
68
|
+
socket.on("timeout", () => done(false));
|
|
69
|
+
socket.on("error", () => done(false));
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function waitForPort(port, timeoutMs) {
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
const start = Date.now();
|
|
76
|
+
|
|
77
|
+
const tryConnect = () => {
|
|
78
|
+
const socket = net.createConnection({ port, host: "127.0.0.1" });
|
|
79
|
+
|
|
80
|
+
socket.on("connect", () => {
|
|
81
|
+
socket.end();
|
|
82
|
+
resolve(true);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
socket.on("error", () => {
|
|
86
|
+
socket.destroy();
|
|
87
|
+
if (Date.now() - start > timeoutMs) {
|
|
88
|
+
reject(new Error(`Timed out waiting for localhost:${port}`));
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
setTimeout(tryConnect, 300);
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
tryConnect();
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
let isShuttingDown = false;
|
|
100
|
+
function shutdown(code = 0) {
|
|
101
|
+
if (isShuttingDown) return;
|
|
102
|
+
isShuttingDown = true;
|
|
103
|
+
|
|
104
|
+
for (const child of children) {
|
|
105
|
+
if (!child.killed) {
|
|
106
|
+
child.kill("SIGTERM");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
setTimeout(() => process.exit(code), 100);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
process.on("SIGINT", () => shutdown(0));
|
|
114
|
+
process.on("SIGTERM", () => shutdown(0));
|
|
115
|
+
|
|
116
|
+
async function main() {
|
|
117
|
+
console.log("Starting Air Jam secure dev mode (Cloudflare named tunnel)...");
|
|
118
|
+
loadEnvFile(".env");
|
|
119
|
+
loadEnvFile(".env.local");
|
|
120
|
+
|
|
121
|
+
const publicHost =
|
|
122
|
+
process.env.AIR_JAM_SECURE_PUBLIC_HOST ||
|
|
123
|
+
process.env.VITE_AIR_JAM_PUBLIC_HOST;
|
|
124
|
+
const tunnelName = process.env.CLOUDFLARE_TUNNEL_NAME;
|
|
125
|
+
if (!publicHost || !tunnelName) {
|
|
126
|
+
throw new Error(
|
|
127
|
+
"Missing AIR_JAM_SECURE_PUBLIC_HOST (or VITE_AIR_JAM_PUBLIC_HOST) or CLOUDFLARE_TUNNEL_NAME. Run `pnpm run secure:init` first.",
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const hasExistingServer = await isPortOpen(SERVER_PORT);
|
|
132
|
+
if (hasExistingServer) {
|
|
133
|
+
console.log(
|
|
134
|
+
`Detected existing local server on :${SERVER_PORT}, reusing it.`,
|
|
135
|
+
);
|
|
136
|
+
} else {
|
|
137
|
+
run("server", "pnpm", ["run", "dev:server"]);
|
|
138
|
+
await waitForPort(SERVER_PORT, START_TIMEOUT_MS);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const hasExistingGame = await isPortOpen(GAME_PORT);
|
|
142
|
+
if (hasExistingGame) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`Port ${GAME_PORT} is already in use. Stop existing Vite dev server and retry.`,
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
run("game", "pnpm", ["run", "dev"], {
|
|
149
|
+
env: {
|
|
150
|
+
...process.env,
|
|
151
|
+
VITE_AIR_JAM_PUBLIC_HOST: publicHost,
|
|
152
|
+
VITE_AIR_JAM_SERVER_URL: publicHost,
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
await waitForPort(GAME_PORT, START_TIMEOUT_MS);
|
|
156
|
+
|
|
157
|
+
run("cloudflared", "pnpm", [
|
|
158
|
+
"exec",
|
|
159
|
+
"cloudflared",
|
|
160
|
+
"tunnel",
|
|
161
|
+
"--config",
|
|
162
|
+
".cloudflared/config.yml",
|
|
163
|
+
"run",
|
|
164
|
+
tunnelName,
|
|
165
|
+
]);
|
|
166
|
+
|
|
167
|
+
console.log("\nAir Jam secure dev is running.");
|
|
168
|
+
console.log(`Open host: ${publicHost}`);
|
|
169
|
+
console.log(`Cloudflare tunnel: ${tunnelName}`);
|
|
170
|
+
console.log("Local server remains on http://localhost:4000 via Vite proxy.");
|
|
171
|
+
console.log("QR codes use the secure host above.");
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
main().catch((error) => {
|
|
175
|
+
console.error(error.message || error);
|
|
176
|
+
shutdown(1);
|
|
177
|
+
});
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { execSync } from "node:child_process";
|
|
4
|
+
import fs from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
|
|
7
|
+
function usage() {
|
|
8
|
+
console.log(
|
|
9
|
+
"Usage: pnpm run secure:init -- --hostname <dev.yourdomain.com> --tunnel <tunnel-name>",
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function parseArgs(argv) {
|
|
14
|
+
const args = { hostname: "", tunnel: "" };
|
|
15
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
16
|
+
const token = argv[i];
|
|
17
|
+
if (token === "--hostname") args.hostname = argv[i + 1] || "";
|
|
18
|
+
if (token === "--tunnel") args.tunnel = argv[i + 1] || "";
|
|
19
|
+
}
|
|
20
|
+
return args;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function run(command, options = {}) {
|
|
24
|
+
return execSync(command, {
|
|
25
|
+
stdio: options.silent ? "pipe" : "inherit",
|
|
26
|
+
encoding: "utf8",
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function upsertEnv(filePath, key, value) {
|
|
31
|
+
let content = "";
|
|
32
|
+
if (fs.existsSync(filePath)) {
|
|
33
|
+
content = fs.readFileSync(filePath, "utf8");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const line = `${key}=${value}`;
|
|
37
|
+
const pattern = new RegExp(`^${key}=.*$`, "m");
|
|
38
|
+
if (pattern.test(content)) {
|
|
39
|
+
content = content.replace(pattern, line);
|
|
40
|
+
} else {
|
|
41
|
+
content += `${content.endsWith("\n") || content.length === 0 ? "" : "\n"}${line}\n`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
fs.writeFileSync(filePath, content);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function main() {
|
|
48
|
+
const { hostname, tunnel } = parseArgs(process.argv.slice(2));
|
|
49
|
+
if (!hostname || !tunnel) {
|
|
50
|
+
usage();
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const certPath = path.join(
|
|
55
|
+
process.env.HOME || "",
|
|
56
|
+
".cloudflared",
|
|
57
|
+
"cert.pem",
|
|
58
|
+
);
|
|
59
|
+
if (!fs.existsSync(certPath)) {
|
|
60
|
+
console.error("Cloudflare login not found. Run: cloudflared tunnel login");
|
|
61
|
+
process.exit(1);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
run("pnpm exec cloudflared --version", { silent: true });
|
|
66
|
+
} catch {
|
|
67
|
+
console.error("cloudflared is required. Install it or run pnpm install.");
|
|
68
|
+
process.exit(1);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
let tunnelExists = false;
|
|
72
|
+
try {
|
|
73
|
+
run(`pnpm exec cloudflared tunnel info ${tunnel}`, { silent: true });
|
|
74
|
+
tunnelExists = true;
|
|
75
|
+
} catch {
|
|
76
|
+
tunnelExists = false;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (!tunnelExists) {
|
|
80
|
+
console.log(`Creating tunnel ${tunnel}...`);
|
|
81
|
+
run(`pnpm exec cloudflared tunnel create ${tunnel}`);
|
|
82
|
+
} else {
|
|
83
|
+
console.log(`Tunnel ${tunnel} already exists, reusing it.`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
console.log(`Routing DNS ${hostname} -> ${tunnel}...`);
|
|
87
|
+
run(`pnpm exec cloudflared tunnel route dns ${tunnel} ${hostname}`);
|
|
88
|
+
|
|
89
|
+
const tunnelInfoRaw = run(`pnpm exec cloudflared tunnel info ${tunnel}`, {
|
|
90
|
+
silent: true,
|
|
91
|
+
});
|
|
92
|
+
const idMatch = tunnelInfoRaw.match(/[0-9a-f-]{36}/i);
|
|
93
|
+
if (!idMatch) {
|
|
94
|
+
throw new Error(
|
|
95
|
+
"Could not resolve tunnel UUID from `cloudflared tunnel info`.",
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
const tunnelId = idMatch[0];
|
|
99
|
+
const credentialsFile = path.join(
|
|
100
|
+
process.env.HOME || "",
|
|
101
|
+
".cloudflared",
|
|
102
|
+
`${tunnelId}.json`,
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
fs.mkdirSync(".cloudflared", { recursive: true });
|
|
106
|
+
fs.writeFileSync(
|
|
107
|
+
".cloudflared/config.yml",
|
|
108
|
+
[
|
|
109
|
+
`tunnel: ${tunnel}`,
|
|
110
|
+
`credentials-file: ${credentialsFile}`,
|
|
111
|
+
"",
|
|
112
|
+
"ingress:",
|
|
113
|
+
` - hostname: ${hostname}`,
|
|
114
|
+
" service: http://127.0.0.1:5173",
|
|
115
|
+
" - service: http_status:404",
|
|
116
|
+
"",
|
|
117
|
+
].join("\n"),
|
|
118
|
+
"utf8",
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
const publicHost = `https://${hostname}`;
|
|
122
|
+
upsertEnv(".env.local", "AIR_JAM_SECURE_PUBLIC_HOST", publicHost);
|
|
123
|
+
upsertEnv(".env.local", "CLOUDFLARE_TUNNEL_NAME", tunnel);
|
|
124
|
+
|
|
125
|
+
console.log("\nSecure dev setup complete.");
|
|
126
|
+
console.log(`Public host: ${publicHost}`);
|
|
127
|
+
console.log(`Tunnel: ${tunnel}`);
|
|
128
|
+
console.log("Next: pnpm run dev:secure");
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
main().catch((error) => {
|
|
132
|
+
console.error(error?.message || error);
|
|
133
|
+
process.exit(1);
|
|
134
|
+
});
|