@expo/serve-sim 0.1.53 → 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/README.md +74 -3
- package/dist/middleware.js +123 -66
- package/dist/native/serve-sim-native.node +0 -0
- package/dist/serve-sim.js +160 -103
- package/dist/state.js +1 -1
- package/package.json +7 -3
- package/src/ax.ts +12 -0
- package/src/camera-helper.ts +6 -4
- package/src/middleware.ts +120 -128
- package/src/native.ts +10 -0
- package/src/state.ts +41 -14
package/src/state.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { tmpdir } from "os";
|
|
2
2
|
import { join } from "path";
|
|
3
|
-
import { readdirSync, mkdirSync, writeFileSync, renameSync } from "fs";
|
|
3
|
+
import { readdirSync, mkdirSync, writeFileSync, renameSync, readFileSync, unlinkSync } from "fs";
|
|
4
4
|
import type { StreamSettings } from "./stream-settings";
|
|
5
5
|
export type {
|
|
6
6
|
HttpStreamCodec,
|
|
@@ -9,16 +9,13 @@ export type {
|
|
|
9
9
|
WebRtcStreamCodec,
|
|
10
10
|
} from "./stream-settings";
|
|
11
11
|
|
|
12
|
-
/** Directory where serve-sim stores runtime state. */
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
* @deprecated Use `stateFileForDevice(udid)` for multi-device support. Kept for backward compat. */
|
|
17
|
-
export const STATE_FILE = join(STATE_DIR, "server.json");
|
|
12
|
+
/** Directory where serve-sim stores runtime state. Override with `SERVE_SIM_STATE_DIR`. */
|
|
13
|
+
export function stateDir(): string {
|
|
14
|
+
return process.env.SERVE_SIM_STATE_DIR || join(tmpdir(), "serve-sim");
|
|
15
|
+
}
|
|
18
16
|
|
|
19
|
-
/** Per-device state file: `/tmp/serve-sim/server-{udid}.json` */
|
|
20
17
|
export function stateFileForDevice(udid: string): string {
|
|
21
|
-
return join(
|
|
18
|
+
return join(stateDir(), `server-${udid}.json`);
|
|
22
19
|
}
|
|
23
20
|
|
|
24
21
|
/** Runtime record for a device streamed in-process by a preview server. */
|
|
@@ -30,6 +27,24 @@ export interface ServeSimDeviceState {
|
|
|
30
27
|
streamUrl: string;
|
|
31
28
|
wsUrl: string;
|
|
32
29
|
streamSettings?: StreamSettings;
|
|
30
|
+
/** Present only under `--require-token`, so local subcommands can reach the gated socket. */
|
|
31
|
+
token?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** `--quiet` startup payload. Carries the session token only when the gate is on. */
|
|
35
|
+
export function previewStartupPayload(
|
|
36
|
+
states: ServeSimDeviceState[],
|
|
37
|
+
token?: string,
|
|
38
|
+
): Record<string, unknown> {
|
|
39
|
+
const view = (s: ServeSimDeviceState) => ({
|
|
40
|
+
url: s.url,
|
|
41
|
+
streamUrl: s.streamUrl,
|
|
42
|
+
wsUrl: s.wsUrl,
|
|
43
|
+
port: s.port,
|
|
44
|
+
device: s.device,
|
|
45
|
+
});
|
|
46
|
+
const base = states.length === 1 ? view(states[0]!) : { devices: states.map(view) };
|
|
47
|
+
return token ? { ...base, token } : base;
|
|
33
48
|
}
|
|
34
49
|
|
|
35
50
|
/**
|
|
@@ -65,21 +80,33 @@ export function inProcessServeSimState(
|
|
|
65
80
|
* Writes atomically (temp file + rename) so a concurrent reader never observes
|
|
66
81
|
* a truncated or partially-written file. */
|
|
67
82
|
export function writeServeSimState(state: ServeSimDeviceState): void {
|
|
68
|
-
mkdirSync(
|
|
83
|
+
mkdirSync(stateDir(), { recursive: true });
|
|
69
84
|
const file = stateFileForDevice(state.device);
|
|
70
85
|
const tmp = `${file}.${process.pid}.tmp`;
|
|
71
|
-
//
|
|
72
|
-
// readable only by the account running serve-sim.
|
|
86
|
+
// Holds TURN credentials and, when gated, the session token.
|
|
73
87
|
writeFileSync(tmp, JSON.stringify(state, null, 2), { mode: 0o600 });
|
|
74
88
|
renameSync(tmp, file);
|
|
75
89
|
}
|
|
76
90
|
|
|
91
|
+
export function clearServeSimState(udid: string, ownerPid: number): void {
|
|
92
|
+
const file = stateFileForDevice(udid);
|
|
93
|
+
let state: ServeSimDeviceState;
|
|
94
|
+
try {
|
|
95
|
+
state = JSON.parse(readFileSync(file, "utf-8")) as ServeSimDeviceState;
|
|
96
|
+
} catch {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (state.pid !== ownerPid) return;
|
|
100
|
+
try { unlinkSync(file); } catch {}
|
|
101
|
+
}
|
|
102
|
+
|
|
77
103
|
/** List all per-device state files in the state directory. */
|
|
78
104
|
export function listStateFiles(): string[] {
|
|
79
105
|
try {
|
|
80
|
-
|
|
106
|
+
const dir = stateDir();
|
|
107
|
+
return readdirSync(dir)
|
|
81
108
|
.filter((f) => f.startsWith("server-") && f.endsWith(".json"))
|
|
82
|
-
.map((f) => join(
|
|
109
|
+
.map((f) => join(dir, f));
|
|
83
110
|
} catch {
|
|
84
111
|
return [];
|
|
85
112
|
}
|