@air-jam/sdk 0.1.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/README.md +87 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/chunk-XSXI3RIS.js +72 -0
- package/dist/chunk-XSXI3RIS.js.map +1 -0
- package/dist/index.cjs +3514 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1529 -0
- package/dist/index.d.ts +1529 -0
- package/dist/index.js +3366 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol.cjs +85 -0
- package/dist/protocol.cjs.map +1 -0
- package/dist/protocol.d.cts +242 -0
- package/dist/protocol.d.ts +242 -0
- package/dist/protocol.js +3 -0
- package/dist/protocol.js.map +1 -0
- package/package.json +81 -0
- package/src/AirJamClient.ts +775 -0
- package/src/audio/audio-manager.ts +395 -0
- package/src/audio/hooks.tsx +140 -0
- package/src/audio/index.ts +3 -0
- package/src/audio/volume-store.ts +77 -0
- package/src/components/air-jam-overlay.tsx +456 -0
- package/src/components/controller-shell.tsx +329 -0
- package/src/components/qr-scanner-dialog.tsx +373 -0
- package/src/components/ui/alert.tsx +66 -0
- package/src/components/ui/avatar.tsx +46 -0
- package/src/components/ui/badge.tsx +47 -0
- package/src/components/ui/button.tsx +61 -0
- package/src/components/ui/card.tsx +92 -0
- package/src/components/ui/slider.tsx +61 -0
- package/src/components/ui/tabs.tsx +64 -0
- package/src/components/volume-controls.tsx +93 -0
- package/src/constants.ts +45 -0
- package/src/context/air-jam-context.tsx +478 -0
- package/src/context/index.ts +2 -0
- package/src/context/socket-manager.ts +98 -0
- package/src/events.ts +151 -0
- package/src/hooks/use-air-jam-controller.ts +608 -0
- package/src/hooks/use-air-jam-host.ts +624 -0
- package/src/hooks/use-air-jam-shell.ts +343 -0
- package/src/hooks/use-fullscreen.ts +108 -0
- package/src/hooks/use-get-input.ts +97 -0
- package/src/hooks/use-send-signal.ts +164 -0
- package/src/index.ts +33 -0
- package/src/internal/input-manager.ts +362 -0
- package/src/protocol.ts +331 -0
- package/src/state/connection-store.ts +84 -0
- package/src/styles.css +128 -0
- package/src/types/browser.ts +42 -0
- package/src/utils/cn.ts +4 -0
- package/src/utils/ids.ts +31 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/mode.ts +13 -0
- package/src/utils/network-ip.ts +145 -0
- package/src/utils/url-builder.ts +170 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if an IP is from a virtual network interface (Docker, VMs, etc.)
|
|
3
|
+
*/
|
|
4
|
+
const isVirtualNetwork = (ip: string): boolean => {
|
|
5
|
+
// Docker Desktop on Mac uses 192.168.64.x and 192.168.65.x
|
|
6
|
+
if (ip.startsWith("192.168.64.") || ip.startsWith("192.168.65.")) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
// Docker default bridge network
|
|
10
|
+
if (ip.startsWith("172.17.")) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
// Other common virtual network ranges
|
|
14
|
+
if (ip.startsWith("172.18.") || ip.startsWith("172.19.")) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
// Link-local addresses
|
|
18
|
+
if (ip.startsWith("169.254.")) {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
return false;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Scores an IP address to determine preference.
|
|
26
|
+
* Higher score = more preferred.
|
|
27
|
+
*/
|
|
28
|
+
const scoreIp = (ip: string): number => {
|
|
29
|
+
// Prefer 192.168.0.x - 192.168.63.x (common home network ranges, excluding virtual networks)
|
|
30
|
+
if (ip.startsWith("192.168.")) {
|
|
31
|
+
const parts = ip.split(".");
|
|
32
|
+
const thirdOctet = parseInt(parts[2] || "0", 10);
|
|
33
|
+
// Prefer lower third octets (0-63) as they're more likely to be real networks
|
|
34
|
+
if (thirdOctet < 64) {
|
|
35
|
+
return 100 - thirdOctet; // Higher score for lower numbers
|
|
36
|
+
}
|
|
37
|
+
// 64+ are often virtual, but we'll still consider them if nothing else
|
|
38
|
+
return 10;
|
|
39
|
+
}
|
|
40
|
+
// Prefer 10.x.x.x networks
|
|
41
|
+
if (ip.startsWith("10.")) {
|
|
42
|
+
return 50;
|
|
43
|
+
}
|
|
44
|
+
// 172.16-31.x.x networks (but not 172.17.x.x which is Docker)
|
|
45
|
+
if (/^172\.(1[6-9]|2[0-9]|3[01])\./.test(ip)) {
|
|
46
|
+
return 30;
|
|
47
|
+
}
|
|
48
|
+
return 0;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Gets the local network IP address using WebRTC.
|
|
53
|
+
* Filters out virtual network interfaces and prefers real network IPs.
|
|
54
|
+
* Returns null if unable to determine the IP.
|
|
55
|
+
*/
|
|
56
|
+
export const getLocalNetworkIp = (): Promise<string | null> => {
|
|
57
|
+
return new Promise((resolve) => {
|
|
58
|
+
// If not in browser, return null
|
|
59
|
+
if (
|
|
60
|
+
typeof window === "undefined" ||
|
|
61
|
+
typeof RTCPeerConnection === "undefined"
|
|
62
|
+
) {
|
|
63
|
+
resolve(null);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const pc = new RTCPeerConnection({
|
|
68
|
+
iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const candidates: Array<{ ip: string; score: number }> = [];
|
|
72
|
+
let candidateGatheringComplete = false;
|
|
73
|
+
|
|
74
|
+
const resolveOnce = (ip: string | null) => {
|
|
75
|
+
pc.close();
|
|
76
|
+
resolve(ip);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// Timeout after 3 seconds
|
|
80
|
+
const timeout = setTimeout(() => {
|
|
81
|
+
if (candidateGatheringComplete) {
|
|
82
|
+
pickBestCandidate();
|
|
83
|
+
} else {
|
|
84
|
+
// Wait a bit more for candidates to arrive
|
|
85
|
+
setTimeout(() => {
|
|
86
|
+
pickBestCandidate();
|
|
87
|
+
}, 500);
|
|
88
|
+
}
|
|
89
|
+
}, 3000);
|
|
90
|
+
|
|
91
|
+
const pickBestCandidate = () => {
|
|
92
|
+
if (candidates.length === 0) {
|
|
93
|
+
resolveOnce(null);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Sort by score (highest first), then pick the best one
|
|
98
|
+
candidates.sort((a, b) => b.score - a.score);
|
|
99
|
+
const best = candidates[0];
|
|
100
|
+
clearTimeout(timeout);
|
|
101
|
+
resolveOnce(best.ip);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
pc.onicecandidate = (event) => {
|
|
105
|
+
if (event.candidate) {
|
|
106
|
+
const candidate = event.candidate.candidate;
|
|
107
|
+
const match = candidate.match(/([0-9]{1,3}(\.[0-9]{1,3}){3})/);
|
|
108
|
+
if (match) {
|
|
109
|
+
const ip = match[1];
|
|
110
|
+
// Filter out localhost
|
|
111
|
+
if (ip === "127.0.0.1") {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Check if it's a private network IP
|
|
116
|
+
const isPrivate =
|
|
117
|
+
ip.startsWith("192.168.") ||
|
|
118
|
+
ip.startsWith("10.") ||
|
|
119
|
+
/^172\.(1[6-9]|2[0-9]|3[01])\./.test(ip);
|
|
120
|
+
|
|
121
|
+
if (isPrivate && !isVirtualNetwork(ip)) {
|
|
122
|
+
const score = scoreIp(ip);
|
|
123
|
+
candidates.push({ ip, score });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
} else {
|
|
127
|
+
// No more candidates - gathering is complete
|
|
128
|
+
candidateGatheringComplete = true;
|
|
129
|
+
// Give it a moment for any last candidates to arrive, then pick the best
|
|
130
|
+
setTimeout(() => {
|
|
131
|
+
pickBestCandidate();
|
|
132
|
+
}, 200);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// Create a data channel to trigger candidate gathering
|
|
137
|
+
pc.createDataChannel("");
|
|
138
|
+
pc.createOffer()
|
|
139
|
+
.then((offer) => pc.setLocalDescription(offer))
|
|
140
|
+
.catch(() => {
|
|
141
|
+
clearTimeout(timeout);
|
|
142
|
+
resolveOnce(null);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { DEFAULT_CONTROLLER_PATH, DEFAULT_SERVER_PORT } from "../constants";
|
|
2
|
+
import type { RoomCode } from "../protocol";
|
|
3
|
+
import { getLocalNetworkIp } from "./network-ip";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Centralized URL builder service for Air Jam.
|
|
7
|
+
* Handles IP detection, URL normalization, and construction of all URL types.
|
|
8
|
+
*/
|
|
9
|
+
class UrlBuilder {
|
|
10
|
+
private cachedLocalIp: string | null = null;
|
|
11
|
+
private ipDetectionPromise: Promise<string | null> | null = null;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Get the local network IP address with caching.
|
|
15
|
+
* First call triggers detection, subsequent calls return cached value.
|
|
16
|
+
*/
|
|
17
|
+
async getLocalIp(): Promise<string | null> {
|
|
18
|
+
// Return cached value if available
|
|
19
|
+
if (this.cachedLocalIp !== null) {
|
|
20
|
+
return this.cachedLocalIp;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// If detection is already in progress, wait for it
|
|
24
|
+
if (this.ipDetectionPromise !== null) {
|
|
25
|
+
return this.ipDetectionPromise;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Start IP detection
|
|
29
|
+
this.ipDetectionPromise = getLocalNetworkIp().then((ip) => {
|
|
30
|
+
this.cachedLocalIp = ip;
|
|
31
|
+
this.ipDetectionPromise = null;
|
|
32
|
+
return ip;
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return this.ipDetectionPromise;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Clear the cached IP address.
|
|
40
|
+
* Useful for testing or when network changes are detected.
|
|
41
|
+
*/
|
|
42
|
+
clearCache(): void {
|
|
43
|
+
this.cachedLocalIp = null;
|
|
44
|
+
this.ipDetectionPromise = null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Normalize a URL for mobile access.
|
|
49
|
+
* Replaces localhost with the local network IP.
|
|
50
|
+
*/
|
|
51
|
+
async normalizeForMobile(url: string): Promise<string> {
|
|
52
|
+
if (typeof window === "undefined") return url;
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
const urlObj = new URL(url);
|
|
56
|
+
|
|
57
|
+
// If URL uses localhost, replace with local network IP
|
|
58
|
+
if (urlObj.hostname === "localhost" || urlObj.hostname === "127.0.0.1") {
|
|
59
|
+
const localIp = await this.getLocalIp();
|
|
60
|
+
|
|
61
|
+
if (localIp) {
|
|
62
|
+
urlObj.hostname = localIp;
|
|
63
|
+
return urlObj.toString();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Fallback to current hostname if IP detection fails
|
|
67
|
+
urlObj.hostname = window.location.hostname;
|
|
68
|
+
return urlObj.toString();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return url;
|
|
72
|
+
} catch {
|
|
73
|
+
return url;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Build a controller URL with room code.
|
|
79
|
+
* Automatically uses local IP when running on localhost.
|
|
80
|
+
*/
|
|
81
|
+
async buildControllerUrl(
|
|
82
|
+
roomId: RoomCode,
|
|
83
|
+
options?: {
|
|
84
|
+
path?: string;
|
|
85
|
+
host?: string;
|
|
86
|
+
},
|
|
87
|
+
): Promise<string> {
|
|
88
|
+
const path = options?.path || DEFAULT_CONTROLLER_PATH;
|
|
89
|
+
|
|
90
|
+
// If explicit host is provided, use it
|
|
91
|
+
if (options?.host) {
|
|
92
|
+
const url = new URL(path, this.normalizeOrigin(options.host));
|
|
93
|
+
url.searchParams.set("room", roomId);
|
|
94
|
+
return url.toString();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Determine base URL
|
|
98
|
+
let base: string;
|
|
99
|
+
if (typeof window === "undefined") {
|
|
100
|
+
base = `http://localhost:${DEFAULT_SERVER_PORT}`;
|
|
101
|
+
} else {
|
|
102
|
+
const currentUrl = new URL(window.location.href);
|
|
103
|
+
|
|
104
|
+
// If not localhost, use the current origin
|
|
105
|
+
if (!this.isLocalhost(currentUrl.hostname)) {
|
|
106
|
+
base = currentUrl.origin;
|
|
107
|
+
} else {
|
|
108
|
+
// If localhost, try to get the local network IP
|
|
109
|
+
const localIp = await this.getLocalIp();
|
|
110
|
+
if (localIp) {
|
|
111
|
+
// Preserve the port from the current URL
|
|
112
|
+
const port = currentUrl.port;
|
|
113
|
+
base = port ? `http://${localIp}:${port}` : `http://${localIp}`;
|
|
114
|
+
} else {
|
|
115
|
+
// Fallback to localhost if we can't determine the IP
|
|
116
|
+
base = currentUrl.origin;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const url = new URL(path, base);
|
|
122
|
+
url.searchParams.set("room", roomId);
|
|
123
|
+
return url.toString();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Resolve the server URL.
|
|
128
|
+
* Uses explicit URL if provided, otherwise infers from window location.
|
|
129
|
+
*/
|
|
130
|
+
resolveServerUrl(explicit?: string): string {
|
|
131
|
+
if (explicit) {
|
|
132
|
+
return this.normalizeOrigin(explicit);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (typeof window !== "undefined") {
|
|
136
|
+
const url = new URL(window.location.href);
|
|
137
|
+
url.port = String(DEFAULT_SERVER_PORT);
|
|
138
|
+
return url.origin;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return `http://localhost:${DEFAULT_SERVER_PORT}`;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Check if a hostname is localhost.
|
|
146
|
+
*/
|
|
147
|
+
private isLocalhost(hostname: string): boolean {
|
|
148
|
+
return (
|
|
149
|
+
hostname === "localhost" ||
|
|
150
|
+
hostname === "127.0.0.1" ||
|
|
151
|
+
hostname === "[::1]"
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Normalize an origin string to ensure it has a protocol.
|
|
157
|
+
*/
|
|
158
|
+
private normalizeOrigin(origin: string): string {
|
|
159
|
+
if (!origin.includes("://")) {
|
|
160
|
+
return `http://${origin}`;
|
|
161
|
+
}
|
|
162
|
+
return origin;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Export singleton instance
|
|
167
|
+
export const urlBuilder = new UrlBuilder();
|
|
168
|
+
|
|
169
|
+
// Export class for testing/advanced usage
|
|
170
|
+
export { UrlBuilder };
|