@couch-kit/host 1.7.10 → 1.7.12
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 +55 -6
- package/lib/action-authorization.d.ts +28 -0
- package/lib/action-authorization.d.ts.map +1 -0
- package/lib/provider.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/action-authorization.ts +55 -0
- package/src/provider.tsx +14 -19
package/README.md
CHANGED
|
@@ -26,10 +26,10 @@ Then install the required peer dependencies:
|
|
|
26
26
|
|
|
27
27
|
```bash
|
|
28
28
|
npx expo install expo-file-system expo-network
|
|
29
|
-
bun add react-native-
|
|
29
|
+
bun add react-native-nitro-modules
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
> **Note:** This library requires Expo modules (`expo-file-system`, `expo-network`)
|
|
32
|
+
> **Note:** This library requires Expo modules (`expo-file-system`, `expo-network`) and `react-native-nitro-modules` as peer dependencies. These must be installed in your consumer app. React Native's autolinking will handle native setup automatically.
|
|
33
33
|
|
|
34
34
|
## Compatibility
|
|
35
35
|
|
|
@@ -39,9 +39,8 @@ bun add react-native-tcp-socket react-native-nitro-modules
|
|
|
39
39
|
| `react-native` | `>= 0.72.0` |
|
|
40
40
|
| `react-native-nitro-modules` | `>= 0.33.0` |
|
|
41
41
|
| `expo` | `>= 51.0.0` |
|
|
42
|
-
| `expo-file-system` | `>=
|
|
42
|
+
| `expo-file-system` | `>= 19.0.0` |
|
|
43
43
|
| `expo-network` | `>= 7.0.0` |
|
|
44
|
-
| `react-native-tcp-socket` | `>= 6.0.0` |
|
|
45
44
|
|
|
46
45
|
> **New Architecture:** This package supports React Native's New Architecture (Fabric/TurboModules) via React Native 0.83+.
|
|
47
46
|
|
|
@@ -58,6 +57,8 @@ Config:
|
|
|
58
57
|
- `staticDir?`: absolute path to the directory of static files to serve. **Required on Android** — APK assets live inside a zip archive and cannot be served directly, so use this to point to a writable filesystem path where you've extracted the `www/` assets at runtime. On iOS, defaults to the bundle directory + `/www`.
|
|
59
58
|
- `devMode?`: if true, do not start the TV static file server; instead point phones at `devServerUrl`
|
|
60
59
|
- `devServerUrl?`: URL of your laptop dev server (e.g. `http://192.168.1.50:5173`)
|
|
60
|
+
- `stateThrottleMs?`: minimum interval (ms) between state broadcasts (default `33` ≈ 30fps)
|
|
61
|
+
- `disconnectTimeout?`: time (ms) before a disconnected player is permanently removed (default `300000` = 5 minutes)
|
|
61
62
|
- `debug?`: enable verbose logs
|
|
62
63
|
|
|
63
64
|
### `useGameHost()`
|
|
@@ -183,8 +184,8 @@ function GameScreen() {
|
|
|
183
184
|
|
|
184
185
|
To iterate on your web controller without rebuilding the Android app constantly:
|
|
185
186
|
|
|
186
|
-
1.
|
|
187
|
-
2.
|
|
187
|
+
1. Start your web project locally (`vite dev` usually runs on `localhost:5173`).
|
|
188
|
+
2. Configure the Host to point to your laptop:
|
|
188
189
|
|
|
189
190
|
```tsx
|
|
190
191
|
<GameHostProvider
|
|
@@ -206,3 +207,51 @@ Important: when the controller is served from the laptop, the client-side hook c
|
|
|
206
207
|
In production, the host serves static controller assets from the iOS bundle directory + `/www` by default. On Android, `staticDir` must be provided since bundle assets live inside the APK.
|
|
207
208
|
|
|
208
209
|
The CLI `couch-kit bundle` copies your web build output into `android/app/src/main/assets/www` (default). Ensure your app packaging makes those assets available under the expected `www` folder.
|
|
210
|
+
|
|
211
|
+
## Additional Hooks
|
|
212
|
+
|
|
213
|
+
### `useActionRecorder()`
|
|
214
|
+
|
|
215
|
+
Records game actions with timestamps for session replay and debugging. Useful for testing, bug reproduction, and replay-based features.
|
|
216
|
+
|
|
217
|
+
```tsx
|
|
218
|
+
import { useActionRecorder, useGameHost } from "@couch-kit/host";
|
|
219
|
+
|
|
220
|
+
function GameScreen() {
|
|
221
|
+
const { state } = useGameHost();
|
|
222
|
+
const { isRecording, recordedCount, startRecording, stopRecording, recordAction } =
|
|
223
|
+
useActionRecorder();
|
|
224
|
+
|
|
225
|
+
const handleStartRecording = () => startRecording(state);
|
|
226
|
+
const handleStop = () => {
|
|
227
|
+
const recording = stopRecording();
|
|
228
|
+
// Save recording JSON for later replay via `couch-kit replay`
|
|
229
|
+
console.log(JSON.stringify(recording));
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
return (
|
|
233
|
+
<View>
|
|
234
|
+
<Text>Actions recorded: {recordedCount}</Text>
|
|
235
|
+
<Button title={isRecording ? "Stop" : "Record"} onPress={isRecording ? handleStop : handleStartRecording} />
|
|
236
|
+
</View>
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Returns:
|
|
242
|
+
|
|
243
|
+
- `isRecording`: whether a recording session is active
|
|
244
|
+
- `recordedCount`: number of actions captured so far
|
|
245
|
+
- `startRecording(currentState, metadata?)`: begin recording from the given state
|
|
246
|
+
- `stopRecording()`: end recording and return the `ActionRecording` object (or `null`)
|
|
247
|
+
- `recordAction(action, state)`: capture a single action + resulting state
|
|
248
|
+
|
|
249
|
+
### `getBestIpAddress()`
|
|
250
|
+
|
|
251
|
+
Returns the device's best LAN IPv4 address (or `null`). Used internally by the provider but exported for custom networking UI (e.g., displaying a manual connect URL).
|
|
252
|
+
|
|
253
|
+
```tsx
|
|
254
|
+
import { getBestIpAddress } from "@couch-kit/host";
|
|
255
|
+
|
|
256
|
+
const ip = await getBestIpAddress(); // e.g. "192.168.1.99"
|
|
257
|
+
```
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Error codes surfaced when a client ACTION is rejected before dispatch. */
|
|
2
|
+
export type ActionRejectionCode = "FORBIDDEN_ACTION" | "NOT_JOINED";
|
|
3
|
+
/** Outcome of authorizing an inbound client ACTION message. */
|
|
4
|
+
export type ActionAuthorization = {
|
|
5
|
+
kind: "allow";
|
|
6
|
+
playerId: string;
|
|
7
|
+
} | {
|
|
8
|
+
kind: "reject";
|
|
9
|
+
code: ActionRejectionCode;
|
|
10
|
+
message: string;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Decide whether an inbound client ACTION may be dispatched.
|
|
14
|
+
*
|
|
15
|
+
* Rejects, in order:
|
|
16
|
+
* - **Internal action types** — clients must not be able to inject framework
|
|
17
|
+
* actions (`__HYDRATE__`, `__PLAYER_JOINED__`, etc.) to forge state.
|
|
18
|
+
* - **Un-joined sockets** — a socket with no resolved player ID never completed
|
|
19
|
+
* a JOIN, so its actions have no owner and must not mutate state.
|
|
20
|
+
*
|
|
21
|
+
* The decision is pure so it can be unit-tested without a WebSocket or React.
|
|
22
|
+
*
|
|
23
|
+
* @param actionType - The `type` field of the client's action payload.
|
|
24
|
+
* @param resolvedPlayerId - The player ID cached for the socket at JOIN time, or
|
|
25
|
+
* `undefined` if the socket has not joined.
|
|
26
|
+
*/
|
|
27
|
+
export declare function authorizeClientAction(actionType: string, resolvedPlayerId: string | undefined): ActionAuthorization;
|
|
28
|
+
//# sourceMappingURL=action-authorization.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"action-authorization.d.ts","sourceRoot":"","sources":["../src/action-authorization.ts"],"names":[],"mappings":"AAEA,6EAA6E;AAC7E,MAAM,MAAM,mBAAmB,GAAG,kBAAkB,GAAG,YAAY,CAAC;AAEpE,+DAA+D;AAC/D,MAAM,MAAM,mBAAmB,GAC3B;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GACnC;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,IAAI,EAAE,mBAAmB,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAUnE;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,qBAAqB,CACnC,UAAU,EAAE,MAAM,EAClB,gBAAgB,EAAE,MAAM,GAAG,SAAS,GACnC,mBAAmB,CAkBrB"}
|
package/lib/provider.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.tsx"],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C,KAAK,IAAI,CAAC;IAC9B,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,WAAW,EAAE,KAAK,GAAG,IAAI,CAAC;CAC3B;AAQD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,UAAU,EAAE,CAAC,SAAS,OAAO,EAAE,EACxE,QAAQ,EACR,MAAM,GACP,EAAE;IACD,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,MAAM,EAAE,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;CAC9B,qBAqWA;AAED;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,UAAU,EAAE,CAAC,SAAS,OAAO,KAK/C,oBAAoB,CAAC,CAAC,EAAE,CAAC,CAAC,CAC7C"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@couch-kit/host",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.12",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"provenance": true
|
|
@@ -58,13 +58,13 @@
|
|
|
58
58
|
"prepublishOnly": "bun run build"
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@couch-kit/core": "0.9.
|
|
61
|
+
"@couch-kit/core": "0.9.2",
|
|
62
62
|
"buffer": "^6.0.3",
|
|
63
63
|
"js-sha1": "^0.7.0",
|
|
64
64
|
"react-native-nitro-http-server": "^1.6.1"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
|
-
"@types/react": "^19.
|
|
67
|
+
"@types/react": "^19.2.17",
|
|
68
68
|
"react": "19.0.4",
|
|
69
69
|
"react-native": "0.83.2",
|
|
70
70
|
"del-cli": "^5.1.0",
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { InternalActionTypes } from "@couch-kit/core";
|
|
2
|
+
|
|
3
|
+
/** Error codes surfaced when a client ACTION is rejected before dispatch. */
|
|
4
|
+
export type ActionRejectionCode = "FORBIDDEN_ACTION" | "NOT_JOINED";
|
|
5
|
+
|
|
6
|
+
/** Outcome of authorizing an inbound client ACTION message. */
|
|
7
|
+
export type ActionAuthorization =
|
|
8
|
+
| { kind: "allow"; playerId: string }
|
|
9
|
+
| { kind: "reject"; code: ActionRejectionCode; message: string };
|
|
10
|
+
|
|
11
|
+
const INTERNAL_ACTION_TYPES = new Set<string>([
|
|
12
|
+
InternalActionTypes.HYDRATE,
|
|
13
|
+
InternalActionTypes.PLAYER_JOINED,
|
|
14
|
+
InternalActionTypes.PLAYER_LEFT,
|
|
15
|
+
InternalActionTypes.PLAYER_RECONNECTED,
|
|
16
|
+
InternalActionTypes.PLAYER_REMOVED,
|
|
17
|
+
]);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Decide whether an inbound client ACTION may be dispatched.
|
|
21
|
+
*
|
|
22
|
+
* Rejects, in order:
|
|
23
|
+
* - **Internal action types** — clients must not be able to inject framework
|
|
24
|
+
* actions (`__HYDRATE__`, `__PLAYER_JOINED__`, etc.) to forge state.
|
|
25
|
+
* - **Un-joined sockets** — a socket with no resolved player ID never completed
|
|
26
|
+
* a JOIN, so its actions have no owner and must not mutate state.
|
|
27
|
+
*
|
|
28
|
+
* The decision is pure so it can be unit-tested without a WebSocket or React.
|
|
29
|
+
*
|
|
30
|
+
* @param actionType - The `type` field of the client's action payload.
|
|
31
|
+
* @param resolvedPlayerId - The player ID cached for the socket at JOIN time, or
|
|
32
|
+
* `undefined` if the socket has not joined.
|
|
33
|
+
*/
|
|
34
|
+
export function authorizeClientAction(
|
|
35
|
+
actionType: string,
|
|
36
|
+
resolvedPlayerId: string | undefined,
|
|
37
|
+
): ActionAuthorization {
|
|
38
|
+
if (INTERNAL_ACTION_TYPES.has(actionType)) {
|
|
39
|
+
return {
|
|
40
|
+
kind: "reject",
|
|
41
|
+
code: "FORBIDDEN_ACTION",
|
|
42
|
+
message: "Internal action types cannot be dispatched by clients",
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!resolvedPlayerId) {
|
|
47
|
+
return {
|
|
48
|
+
kind: "reject",
|
|
49
|
+
code: "NOT_JOINED",
|
|
50
|
+
message: "You must JOIN before dispatching actions",
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return { kind: "allow", playerId: resolvedPlayerId };
|
|
55
|
+
}
|
package/src/provider.tsx
CHANGED
|
@@ -27,6 +27,7 @@ import {
|
|
|
27
27
|
HostSessionManager,
|
|
28
28
|
type JoinSessionPayload,
|
|
29
29
|
} from "./session-manager";
|
|
30
|
+
import { authorizeClientAction } from "./action-authorization";
|
|
30
31
|
import {
|
|
31
32
|
BroadcastScheduler,
|
|
32
33
|
DEFAULT_STATE_THROTTLE_MS,
|
|
@@ -283,28 +284,25 @@ export function GameHostProvider<S extends IGameState, A extends IAction>({
|
|
|
283
284
|
}
|
|
284
285
|
|
|
285
286
|
case MessageTypes.ACTION: {
|
|
286
|
-
// Only accept actions with a user-defined type string,
|
|
287
|
-
// reject internal action types to prevent injection.
|
|
288
287
|
const actionPayload = message.payload as A;
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
288
|
+
|
|
289
|
+
// Authorize before doing any work: reject client-injected internal
|
|
290
|
+
// action types and actions from sockets that never completed a JOIN.
|
|
291
|
+
const resolvedPlayerId =
|
|
292
|
+
sessionManager.current.getPlayerIdForSocket(socketId);
|
|
293
|
+
const auth = authorizeClientAction(
|
|
294
|
+
actionPayload.type,
|
|
295
|
+
resolvedPlayerId,
|
|
296
|
+
);
|
|
297
|
+
if (auth.kind === "reject") {
|
|
296
298
|
if (configRef.current.debug)
|
|
297
299
|
console.warn(
|
|
298
|
-
`[GameHost] Rejected
|
|
300
|
+
`[GameHost] Rejected action from ${socketId} (${auth.code}):`,
|
|
299
301
|
actionPayload.type,
|
|
300
302
|
);
|
|
301
303
|
server.send(socketId, {
|
|
302
304
|
type: MessageTypes.ERROR,
|
|
303
|
-
payload: {
|
|
304
|
-
code: "FORBIDDEN_ACTION",
|
|
305
|
-
message:
|
|
306
|
-
"Internal action types cannot be dispatched by clients",
|
|
307
|
-
},
|
|
305
|
+
payload: { code: auth.code, message: auth.message },
|
|
308
306
|
});
|
|
309
307
|
return;
|
|
310
308
|
}
|
|
@@ -323,10 +321,7 @@ export function GameHostProvider<S extends IGameState, A extends IAction>({
|
|
|
323
321
|
return;
|
|
324
322
|
}
|
|
325
323
|
|
|
326
|
-
|
|
327
|
-
const resolvedPlayerId =
|
|
328
|
-
sessionManager.current.getPlayerIdForSocket(socketId);
|
|
329
|
-
dispatch({ ...actionPayload, playerId: resolvedPlayerId });
|
|
324
|
+
dispatch({ ...actionPayload, playerId: auth.playerId });
|
|
330
325
|
actionQueue.current.push(actionPayload);
|
|
331
326
|
break;
|
|
332
327
|
}
|