yaver-feedback-react-native 0.7.8 → 0.7.9
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/Discovery.js +46 -31
- package/dist/_core/device.d.ts +79 -0
- package/dist/_core/device.js +310 -0
- package/dist/_core/index.d.ts +1 -0
- package/dist/_core/index.js +1 -0
- package/dist/deviceDedup.d.ts +13 -37
- package/dist/deviceDedup.js +19 -169
- package/package.json +1 -1
- package/src/Discovery.ts +13 -30
- package/src/_core/device.ts +352 -0
- package/src/_core/index.ts +1 -0
- package/src/deviceDedup.ts +26 -166
package/dist/deviceDedup.js
CHANGED
|
@@ -1,185 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
3
|
+
* Re-export the canonical dedup / freshness / race-probe helpers from
|
|
4
|
+
* @yaver/client-core (mirrored into `./_core/`).
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* The SDK previously carried its own ~180-line copy of
|
|
7
|
+
* `collapseAliasDevices` ported by hand from mobile's
|
|
8
|
+
* DeviceContext.tsx. The copies drifted on `hwid` strong-identity,
|
|
9
|
+
* `runners` / `local` field preservation, and a few other subtle
|
|
10
|
+
* merge rules. Canonicalising via client-core eliminates that drift
|
|
11
|
+
* class — mobile's test case automatically covers the SDK's dedup
|
|
12
|
+
* behaviour too.
|
|
10
13
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* mint a new hwid
|
|
15
|
-
* 3. Endpoint key (host:port)
|
|
16
|
-
*
|
|
17
|
-
* When collapsing, `mergeDeviceEntries` prefers: authenticated over
|
|
18
|
-
* needsAuth, online over offline, freshest lastHeartbeat.
|
|
14
|
+
* The shared module operates on a `CoreDevice` shape. The SDK's
|
|
15
|
+
* `RemoteDevice` is structurally compatible (same field names, same
|
|
16
|
+
* types), so the casts here are a no-op at runtime.
|
|
19
17
|
*/
|
|
20
18
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
19
|
exports.HEARTBEAT_STALE_MS = void 0;
|
|
22
20
|
exports.collapseRemoteDevices = collapseRemoteDevices;
|
|
23
21
|
exports.isDeviceFresh = isDeviceFresh;
|
|
24
22
|
exports.pickTargetDevice = pickTargetDevice;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
function normalizedHost(host) {
|
|
29
|
-
return String(host || '').trim().toLowerCase().replace(/\.local$/i, '');
|
|
30
|
-
}
|
|
31
|
-
function identityKey(d) {
|
|
32
|
-
if (d.hwid)
|
|
33
|
-
return `hwid:${d.hwid}`;
|
|
34
|
-
if (d.publicKey)
|
|
35
|
-
return `pub:${d.publicKey}`;
|
|
36
|
-
if (d.isGuest) {
|
|
37
|
-
const scope = d.hostEmail || d.hostName || 'guest';
|
|
38
|
-
return `guest:${scope}:${d.deviceId || d.name}`;
|
|
39
|
-
}
|
|
40
|
-
const n = normalizedName(d.name);
|
|
41
|
-
const os = String(d.platform || '').trim().toLowerCase();
|
|
42
|
-
if (n && os)
|
|
43
|
-
return `host:${os}:${n}`;
|
|
44
|
-
if (d.deviceId)
|
|
45
|
-
return `id:${d.deviceId}`;
|
|
46
|
-
return `name:${d.name}`;
|
|
47
|
-
}
|
|
48
|
-
function aliasKey(d) {
|
|
49
|
-
if (d.isGuest)
|
|
50
|
-
return null;
|
|
51
|
-
const n = normalizedName(d.name);
|
|
52
|
-
const os = String(d.platform || '').trim().toLowerCase();
|
|
53
|
-
if (!n || !os)
|
|
54
|
-
return null;
|
|
55
|
-
return `${os}:${n}`;
|
|
56
|
-
}
|
|
57
|
-
function endpointKey(d) {
|
|
58
|
-
if (d.isGuest)
|
|
59
|
-
return null;
|
|
60
|
-
const h = normalizedHost(d.quicHost);
|
|
61
|
-
if (!h)
|
|
62
|
-
return null;
|
|
63
|
-
return `${h}:${d.quicPort || 0}`;
|
|
64
|
-
}
|
|
65
|
-
function mergeEntries(existing, incoming) {
|
|
66
|
-
const incomingWins = (!!existing.needsAuth && !incoming.needsAuth) ||
|
|
67
|
-
(incoming.lastHeartbeat || 0) > (existing.lastHeartbeat || 0) ||
|
|
68
|
-
(!!incoming.isOnline && !existing.isOnline);
|
|
69
|
-
const base = incomingWins ? incoming : existing;
|
|
70
|
-
const other = incomingWins ? existing : incoming;
|
|
71
|
-
return {
|
|
72
|
-
...other,
|
|
73
|
-
...base,
|
|
74
|
-
quicHost: base.quicHost || other.quicHost,
|
|
75
|
-
quicPort: base.quicPort || other.quicPort,
|
|
76
|
-
isOnline: base.isOnline || other.isOnline,
|
|
77
|
-
runnerDown: base.runnerDown && other.runnerDown,
|
|
78
|
-
publicKey: base.publicKey || other.publicKey,
|
|
79
|
-
lastHeartbeat: Math.max(existing.lastHeartbeat || 0, incoming.lastHeartbeat || 0),
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
// When two rows share the same alias key (hostname + OS) but differ on
|
|
83
|
-
// hwid/publicKey, pick the active one over the stale needs-auth leftover.
|
|
84
|
-
function pickActiveOverStaleNeedsAuth(a, b) {
|
|
85
|
-
const aDead = a.needsAuth && !a.isOnline;
|
|
86
|
-
const bDead = b.needsAuth && !b.isOnline;
|
|
87
|
-
const aLive = !a.needsAuth && a.isOnline;
|
|
88
|
-
const bLive = !b.needsAuth && b.isOnline;
|
|
89
|
-
if (aDead && bLive)
|
|
90
|
-
return b;
|
|
91
|
-
if (bDead && aLive)
|
|
92
|
-
return a;
|
|
93
|
-
return null;
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Collapse a Convex device list so each physical machine appears once.
|
|
97
|
-
* Safe on an empty list; idempotent on an already-deduped list.
|
|
98
|
-
*/
|
|
23
|
+
const device_1 = require("./_core/device");
|
|
24
|
+
var constants_1 = require("./_core/constants");
|
|
25
|
+
Object.defineProperty(exports, "HEARTBEAT_STALE_MS", { enumerable: true, get: function () { return constants_1.HEARTBEAT_STALE_MS; } });
|
|
99
26
|
function collapseRemoteDevices(devices) {
|
|
100
|
-
|
|
101
|
-
return [];
|
|
102
|
-
const byIdentity = new Map();
|
|
103
|
-
for (const d of devices) {
|
|
104
|
-
const k = identityKey(d);
|
|
105
|
-
const prev = byIdentity.get(k);
|
|
106
|
-
byIdentity.set(k, prev ? mergeEntries(prev, d) : d);
|
|
107
|
-
}
|
|
108
|
-
const byAlias = new Map();
|
|
109
|
-
for (const d of byIdentity.values()) {
|
|
110
|
-
const k = aliasKey(d);
|
|
111
|
-
if (!k) {
|
|
112
|
-
byAlias.set(`id:${d.deviceId}`, d);
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
const prev = byAlias.get(k);
|
|
116
|
-
if (!prev) {
|
|
117
|
-
byAlias.set(k, d);
|
|
118
|
-
continue;
|
|
119
|
-
}
|
|
120
|
-
const strongIdentityConflict = !!prev.publicKey && !!d.publicKey && prev.publicKey !== d.publicKey;
|
|
121
|
-
if (strongIdentityConflict) {
|
|
122
|
-
const winner = pickActiveOverStaleNeedsAuth(prev, d);
|
|
123
|
-
if (winner) {
|
|
124
|
-
byAlias.set(k, winner);
|
|
125
|
-
continue;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
byAlias.set(k, mergeEntries(prev, d));
|
|
129
|
-
}
|
|
130
|
-
const byEndpoint = new Map();
|
|
131
|
-
for (const d of byAlias.values()) {
|
|
132
|
-
const k = endpointKey(d);
|
|
133
|
-
if (!k) {
|
|
134
|
-
byEndpoint.set(`id:${d.deviceId}`, d);
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
const prev = byEndpoint.get(k);
|
|
138
|
-
byEndpoint.set(k, prev ? mergeEntries(prev, d) : d);
|
|
139
|
-
}
|
|
140
|
-
return [...byEndpoint.values()];
|
|
27
|
+
return (0, device_1.collapseDevices)(devices);
|
|
141
28
|
}
|
|
142
|
-
/**
|
|
143
|
-
* Threshold for "online" based on heartbeat age, in milliseconds.
|
|
144
|
-
* Re-exported from `@yaver/client-core` so mobile + SDK + backend
|
|
145
|
-
* agree on one number at all times.
|
|
146
|
-
*/
|
|
147
|
-
const constants_1 = require("./_core/constants");
|
|
148
|
-
Object.defineProperty(exports, "HEARTBEAT_STALE_MS", { enumerable: true, get: function () { return constants_1.HEARTBEAT_STALE_MS; } });
|
|
149
|
-
/**
|
|
150
|
-
* Returns a freshness flag consistent with the mobile app. A device is
|
|
151
|
-
* "fresh" when it was online per Convex AND its heartbeat is within
|
|
152
|
-
* `HEARTBEAT_STALE_MS`.
|
|
153
|
-
*/
|
|
154
29
|
function isDeviceFresh(d) {
|
|
155
|
-
|
|
156
|
-
return false;
|
|
157
|
-
if (!d.lastHeartbeat)
|
|
158
|
-
return true;
|
|
159
|
-
return Date.now() - d.lastHeartbeat < constants_1.HEARTBEAT_STALE_MS;
|
|
30
|
+
return (0, device_1.isDeviceFresh)(d);
|
|
160
31
|
}
|
|
161
|
-
/**
|
|
162
|
-
* Pick the best candidate for an auto-connect attempt. Preference:
|
|
163
|
-
* 1. matches the preferred deviceId when supplied + still fresh
|
|
164
|
-
* 2. fresh (online + recent heartbeat) + has a quicHost
|
|
165
|
-
* 3. online + has a quicHost
|
|
166
|
-
* 4. first with a quicHost
|
|
167
|
-
*/
|
|
168
32
|
function pickTargetDevice(devices, preferredDeviceId) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
if (preferredDeviceId) {
|
|
172
|
-
const preferred = devices.find((d) => d.deviceId === preferredDeviceId && d.quicHost);
|
|
173
|
-
if (preferred && isDeviceFresh(preferred))
|
|
174
|
-
return preferred;
|
|
175
|
-
if (preferred)
|
|
176
|
-
return preferred;
|
|
177
|
-
}
|
|
178
|
-
const fresh = devices.find((d) => isDeviceFresh(d) && d.quicHost);
|
|
179
|
-
if (fresh)
|
|
180
|
-
return fresh;
|
|
181
|
-
const online = devices.find((d) => d.isOnline && d.quicHost);
|
|
182
|
-
if (online)
|
|
183
|
-
return online;
|
|
184
|
-
return devices.find((d) => d.quicHost) || devices[0] || null;
|
|
33
|
+
const pick = (0, device_1.pickTargetDevice)(devices, preferredDeviceId);
|
|
34
|
+
return (pick ?? null);
|
|
185
35
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yaver-feedback-react-native",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.9",
|
|
4
4
|
"description": "Visual feedback SDK for Yaver — bug reports, screen recording, voice annotations, and local-first developer workflows",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
package/src/Discovery.ts
CHANGED
|
@@ -256,36 +256,19 @@ export class YaverDiscovery {
|
|
|
256
256
|
* does on same-LAN.
|
|
257
257
|
*/
|
|
258
258
|
static async raceProbe(urls: string[]): Promise<DiscoveryResult | null> {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
);
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
attempts.forEach((p) => {
|
|
273
|
-
p.then((r) => {
|
|
274
|
-
if (settled) return;
|
|
275
|
-
settled = true;
|
|
276
|
-
resolve(r);
|
|
277
|
-
}).catch(() => {
|
|
278
|
-
remaining -= 1;
|
|
279
|
-
if (remaining <= 0 && !settled) {
|
|
280
|
-
settled = true;
|
|
281
|
-
resolve(null);
|
|
282
|
-
}
|
|
283
|
-
});
|
|
284
|
-
});
|
|
285
|
-
});
|
|
286
|
-
} catch {
|
|
287
|
-
return null;
|
|
288
|
-
}
|
|
259
|
+
// Delegate to the canonical client-core implementation so mobile +
|
|
260
|
+
// SDK run the same race logic. Returned shape is `ProbeResult` —
|
|
261
|
+
// structurally compatible with DiscoveryResult minus the required
|
|
262
|
+
// hostname/version fields, which we backfill with sane defaults.
|
|
263
|
+
const { raceHealthProbes } = await import('./_core/device');
|
|
264
|
+
const res = await raceHealthProbes(urls, { timeoutMs: PROBE_TIMEOUT_MS });
|
|
265
|
+
if (!res) return null;
|
|
266
|
+
return {
|
|
267
|
+
url: res.url,
|
|
268
|
+
hostname: res.hostname ?? 'Unknown',
|
|
269
|
+
version: res.version ?? 'unknown',
|
|
270
|
+
latency: res.latency ?? 0,
|
|
271
|
+
};
|
|
289
272
|
}
|
|
290
273
|
|
|
291
274
|
/**
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
// AUTO-SYNCED from shared/client-core/src/device.ts.
|
|
2
|
+
// DO NOT EDIT IN PLACE. Edit the source and re-run
|
|
3
|
+
// scripts/sync-client-core.sh. CI checks drift via `--check`.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Device dedup, merging, freshness, and target-picking.
|
|
7
|
+
*
|
|
8
|
+
* Phase-2 extract of the Yaver-mobile logic that lives at
|
|
9
|
+
* mobile/src/context/DeviceContext.tsx:193-413. Ported faithfully —
|
|
10
|
+
* mobile's collapseAliasDevices has been battle-tested through
|
|
11
|
+
* multiple Convex schema changes and the re-pair edge cases; this
|
|
12
|
+
* file is byte-identical in behaviour, only renamed for the shared
|
|
13
|
+
* RemoteDevice shape. The Feedback SDK used to carry its own copy
|
|
14
|
+
* (sdk/feedback/react-native/src/deviceDedup.ts) that drifted on
|
|
15
|
+
* `hwid` strong-identity and `runners`/`local` field preservation.
|
|
16
|
+
*
|
|
17
|
+
* Canonical device shape shared across every surface:
|
|
18
|
+
* - Mobile app: mobile/src/context/DeviceContext.tsx::Device (adapts
|
|
19
|
+
* into this shape at the Convex /devices/list fetch
|
|
20
|
+
* site).
|
|
21
|
+
* - Feedback SDK: src/auth.ts::RemoteDevice re-exports this type.
|
|
22
|
+
* - Web / Desktop: same.
|
|
23
|
+
*
|
|
24
|
+
* The mobile app may have extra view-model fields it wants to keep on
|
|
25
|
+
* its own Device interface (edgeProfile, sessionBinding, etc.) — those
|
|
26
|
+
* stay as local extensions; the core operates only on the fields
|
|
27
|
+
* declared here.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { HEARTBEAT_STALE_MS } from './constants';
|
|
31
|
+
|
|
32
|
+
export interface CoreDevice {
|
|
33
|
+
/** Convex-issued device id. */
|
|
34
|
+
deviceId: string;
|
|
35
|
+
/** Display name — usually hostname. */
|
|
36
|
+
name: string;
|
|
37
|
+
/** OS family — "darwin", "linux", "windows". */
|
|
38
|
+
platform: string;
|
|
39
|
+
isOnline: boolean;
|
|
40
|
+
needsAuth: boolean;
|
|
41
|
+
runnerDown: boolean;
|
|
42
|
+
/** Unix ms of the latest heartbeat the agent sent to Convex. */
|
|
43
|
+
lastHeartbeat: number;
|
|
44
|
+
isGuest: boolean;
|
|
45
|
+
hostName?: string;
|
|
46
|
+
hostEmail?: string;
|
|
47
|
+
accessScope?: 'owner' | 'shared-scoped' | 'shared-legacy';
|
|
48
|
+
/** Primary LAN IP (or tunnel host) the agent advertised. */
|
|
49
|
+
quicHost: string;
|
|
50
|
+
quicPort: number;
|
|
51
|
+
/** HTTP port the agent listens on (usually 18080). */
|
|
52
|
+
httpPort?: number;
|
|
53
|
+
publicKey?: string;
|
|
54
|
+
/** Stable hardware identifier — dedup key for re-pair events. */
|
|
55
|
+
hwid?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Every LAN IP the agent reported in its last heartbeat. Used by
|
|
58
|
+
* Discovery.raceProbe to try all interfaces in parallel.
|
|
59
|
+
*/
|
|
60
|
+
localIps?: string[];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// ── Normalisers ───────────────────────────────────────────────────────
|
|
64
|
+
|
|
65
|
+
function normName(name: string | undefined): string {
|
|
66
|
+
return String(name || '').trim().toLowerCase().replace(/\.local$/i, '');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function normHost(host: string | undefined): string {
|
|
70
|
+
return String(host || '').trim().toLowerCase().replace(/\.local$/i, '');
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ── Keys ──────────────────────────────────────────────────────────────
|
|
74
|
+
|
|
75
|
+
export function deviceIdentityKey(d: CoreDevice): string {
|
|
76
|
+
if (d.hwid) return `hwid:${d.hwid}`;
|
|
77
|
+
if (d.publicKey) return `pub:${d.publicKey}`;
|
|
78
|
+
if (d.isGuest) {
|
|
79
|
+
const scope = d.hostEmail || d.hostName || 'guest';
|
|
80
|
+
return `guest:${scope}:${d.deviceId || d.name}`;
|
|
81
|
+
}
|
|
82
|
+
const n = normName(d.name);
|
|
83
|
+
const os = String(d.platform || '').trim().toLowerCase();
|
|
84
|
+
if (n && os) return `host:${os}:${n}`;
|
|
85
|
+
if (d.deviceId) return `id:${d.deviceId}`;
|
|
86
|
+
return `name:${d.name}`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function deviceAliasKey(d: CoreDevice): string | null {
|
|
90
|
+
if (d.isGuest) return null;
|
|
91
|
+
const n = normName(d.name);
|
|
92
|
+
const os = String(d.platform || '').trim().toLowerCase();
|
|
93
|
+
if (!n || !os) return null;
|
|
94
|
+
return `${os}:${n}`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function deviceEndpointKey(d: CoreDevice): string | null {
|
|
98
|
+
if (d.isGuest) return null;
|
|
99
|
+
const h = normHost(d.quicHost);
|
|
100
|
+
if (!h) return null;
|
|
101
|
+
return `${h}:${d.quicPort || 0}`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ── Merge rules ───────────────────────────────────────────────────────
|
|
105
|
+
|
|
106
|
+
export function mergeDeviceEntries(a: CoreDevice, b: CoreDevice): CoreDevice {
|
|
107
|
+
const incomingWins =
|
|
108
|
+
(!!a.needsAuth && !b.needsAuth) ||
|
|
109
|
+
(b.lastHeartbeat || 0) > (a.lastHeartbeat || 0) ||
|
|
110
|
+
(!!b.isOnline && !a.isOnline);
|
|
111
|
+
const base = incomingWins ? b : a;
|
|
112
|
+
const other = incomingWins ? a : b;
|
|
113
|
+
return {
|
|
114
|
+
...other,
|
|
115
|
+
...base,
|
|
116
|
+
quicHost: base.quicHost || other.quicHost,
|
|
117
|
+
quicPort: base.quicPort || other.quicPort,
|
|
118
|
+
httpPort: base.httpPort || other.httpPort,
|
|
119
|
+
isOnline: base.isOnline || other.isOnline,
|
|
120
|
+
runnerDown: base.runnerDown && other.runnerDown,
|
|
121
|
+
publicKey: base.publicKey || other.publicKey,
|
|
122
|
+
hwid: base.hwid || other.hwid,
|
|
123
|
+
lastHeartbeat: Math.max(a.lastHeartbeat || 0, b.lastHeartbeat || 0),
|
|
124
|
+
localIps: (() => {
|
|
125
|
+
const set = new Set<string>();
|
|
126
|
+
for (const ip of a.localIps || []) if (ip) set.add(ip);
|
|
127
|
+
for (const ip of b.localIps || []) if (ip) set.add(ip);
|
|
128
|
+
return set.size > 0 ? [...set] : undefined;
|
|
129
|
+
})(),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// When two rows share the same alias (hostname + OS) but differ on
|
|
134
|
+
// hwid / publicKey, prefer the authenticated + online row over a
|
|
135
|
+
// stale "needsAuth + offline" leftover. That leftover pattern is
|
|
136
|
+
// what re-pair / wipe-and-reinstall produces on Convex.
|
|
137
|
+
function pickActiveOverStaleNeedsAuth(
|
|
138
|
+
a: CoreDevice,
|
|
139
|
+
b: CoreDevice,
|
|
140
|
+
): CoreDevice | null {
|
|
141
|
+
const aDead = a.needsAuth && !a.isOnline;
|
|
142
|
+
const bDead = b.needsAuth && !b.isOnline;
|
|
143
|
+
const aLive = !a.needsAuth && a.isOnline;
|
|
144
|
+
const bLive = !b.needsAuth && b.isOnline;
|
|
145
|
+
if (aDead && bLive) return b;
|
|
146
|
+
if (bDead && aLive) return a;
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ── Collapse (three-pass dedup) ───────────────────────────────────────
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Collapse duplicate Convex rows so each physical machine appears
|
|
154
|
+
* exactly once. Three passes — identity key → alias key → endpoint key.
|
|
155
|
+
* Safe on empty lists; idempotent on already-deduped input.
|
|
156
|
+
*/
|
|
157
|
+
export function collapseDevices(devices: CoreDevice[]): CoreDevice[] {
|
|
158
|
+
if (!Array.isArray(devices) || devices.length === 0) return [];
|
|
159
|
+
|
|
160
|
+
// Pass 1: identity key (hwid / publicKey / name+os).
|
|
161
|
+
const byIdentity = new Map<string, CoreDevice>();
|
|
162
|
+
for (const d of devices) {
|
|
163
|
+
const k = deviceIdentityKey(d);
|
|
164
|
+
const prev = byIdentity.get(k);
|
|
165
|
+
byIdentity.set(k, prev ? mergeDeviceEntries(prev, d) : d);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Pass 2: alias key (os + normalised hostname), with strong-identity
|
|
169
|
+
// conflict resolution so two genuinely-different machines sharing a
|
|
170
|
+
// hostname don't silently merge.
|
|
171
|
+
const byAlias = new Map<string, CoreDevice>();
|
|
172
|
+
for (const d of byIdentity.values()) {
|
|
173
|
+
const k = deviceAliasKey(d);
|
|
174
|
+
if (!k) {
|
|
175
|
+
byAlias.set(`id:${d.deviceId}`, d);
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
const prev = byAlias.get(k);
|
|
179
|
+
if (!prev) {
|
|
180
|
+
byAlias.set(k, d);
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
const strongConflict =
|
|
184
|
+
(!!prev.hwid && !!d.hwid && prev.hwid !== d.hwid) ||
|
|
185
|
+
(!!prev.publicKey && !!d.publicKey && prev.publicKey !== d.publicKey);
|
|
186
|
+
if (strongConflict) {
|
|
187
|
+
const winner = pickActiveOverStaleNeedsAuth(prev, d);
|
|
188
|
+
if (winner) {
|
|
189
|
+
byAlias.set(k, winner);
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
byAlias.set(k, mergeDeviceEntries(prev, d));
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Pass 3: endpoint key (host:port) — last-chance dedup for rows
|
|
197
|
+
// that share a LAN address but slipped through identity + alias.
|
|
198
|
+
const byEndpoint = new Map<string, CoreDevice>();
|
|
199
|
+
for (const d of byAlias.values()) {
|
|
200
|
+
const k = deviceEndpointKey(d);
|
|
201
|
+
if (!k) {
|
|
202
|
+
byEndpoint.set(`id:${d.deviceId}`, d);
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
const prev = byEndpoint.get(k);
|
|
206
|
+
byEndpoint.set(k, prev ? mergeDeviceEntries(prev, d) : d);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return [...byEndpoint.values()];
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// ── Freshness + target pick ───────────────────────────────────────────
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* "Fresh" matches the mobile app: online + heartbeat < 90 s. Clients
|
|
216
|
+
* read Convex's `isOnline` first (backend already applies its own 90 s
|
|
217
|
+
* gate from the server clock), then use this helper when they need the
|
|
218
|
+
* phone-side freshness opinion too — e.g. for auto-connect picks.
|
|
219
|
+
*/
|
|
220
|
+
export function isDeviceFresh(d: CoreDevice, now = Date.now()): boolean {
|
|
221
|
+
if (!d.isOnline) return false;
|
|
222
|
+
if (!d.lastHeartbeat) return true;
|
|
223
|
+
return now - d.lastHeartbeat < HEARTBEAT_STALE_MS;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Choose the best candidate for an auto-connect attempt. Preference:
|
|
228
|
+
* 1. explicit `preferredDeviceId` that's still fresh
|
|
229
|
+
* 2. fresh (online + recent heartbeat) + has a quicHost
|
|
230
|
+
* 3. online + has a quicHost
|
|
231
|
+
* 4. first with a quicHost
|
|
232
|
+
*/
|
|
233
|
+
export function pickTargetDevice(
|
|
234
|
+
devices: CoreDevice[],
|
|
235
|
+
preferredDeviceId?: string,
|
|
236
|
+
): CoreDevice | null {
|
|
237
|
+
if (!devices.length) return null;
|
|
238
|
+
if (preferredDeviceId) {
|
|
239
|
+
const preferred = devices.find(
|
|
240
|
+
(d) => d.deviceId === preferredDeviceId && d.quicHost,
|
|
241
|
+
);
|
|
242
|
+
if (preferred && isDeviceFresh(preferred)) return preferred;
|
|
243
|
+
if (preferred) return preferred;
|
|
244
|
+
}
|
|
245
|
+
const fresh = devices.find((d) => isDeviceFresh(d) && d.quicHost);
|
|
246
|
+
if (fresh) return fresh;
|
|
247
|
+
const online = devices.find((d) => d.isOnline && d.quicHost);
|
|
248
|
+
if (online) return online;
|
|
249
|
+
return devices.find((d) => d.quicHost) || devices[0] || null;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// ── Probe candidate assembly ──────────────────────────────────────────
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Build the set of `/health` candidate URLs for a target device —
|
|
256
|
+
* `quicHost` plus every LAN IP the agent reported in `localIps`,
|
|
257
|
+
* uniqued and formatted. This is the thing that makes direct LAN
|
|
258
|
+
* reloads "just work" on multi-homed hosts (en0 + utun tailscale +
|
|
259
|
+
* docker0 etc.) — the mobile app races them in parallel via
|
|
260
|
+
* Promise.any.
|
|
261
|
+
*/
|
|
262
|
+
export function buildProbeCandidates(
|
|
263
|
+
target: CoreDevice,
|
|
264
|
+
defaultHttpPort = 18080,
|
|
265
|
+
): string[] {
|
|
266
|
+
const port = target.httpPort ?? target.quicPort ?? defaultHttpPort;
|
|
267
|
+
const ips = new Set<string>();
|
|
268
|
+
if (target.quicHost) ips.add(target.quicHost);
|
|
269
|
+
for (const ip of target.localIps ?? []) {
|
|
270
|
+
if (ip) ips.add(ip);
|
|
271
|
+
}
|
|
272
|
+
return [...ips].map((ip) => `http://${ip}:${port}`);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// ── Parallel /health race (Promise.any polyfill baked in) ─────────────
|
|
276
|
+
|
|
277
|
+
export interface ProbeResult {
|
|
278
|
+
url: string;
|
|
279
|
+
hostname?: string;
|
|
280
|
+
version?: string;
|
|
281
|
+
latency?: number;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Race `/health` probes across N URLs. First 200 wins; everything else
|
|
286
|
+
* is abandoned. Older Hermes doesn't have Promise.any, so we hand-roll
|
|
287
|
+
* the same semantic.
|
|
288
|
+
*/
|
|
289
|
+
export async function raceHealthProbes(
|
|
290
|
+
urls: string[],
|
|
291
|
+
opts: { timeoutMs?: number; headers?: Record<string, string> } = {},
|
|
292
|
+
): Promise<ProbeResult | null> {
|
|
293
|
+
if (!urls || urls.length === 0) return null;
|
|
294
|
+
const timeoutMs = opts.timeoutMs ?? 2500;
|
|
295
|
+
|
|
296
|
+
const probeOne = async (url: string): Promise<ProbeResult | null> => {
|
|
297
|
+
const base = url.replace(/\/$/, '');
|
|
298
|
+
const start = Date.now();
|
|
299
|
+
try {
|
|
300
|
+
const controller = new AbortController();
|
|
301
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
302
|
+
const res = await fetch(`${base}/health`, {
|
|
303
|
+
method: 'GET',
|
|
304
|
+
headers: opts.headers,
|
|
305
|
+
signal: controller.signal,
|
|
306
|
+
});
|
|
307
|
+
clearTimeout(timer);
|
|
308
|
+
if (!res.ok) return null;
|
|
309
|
+
const latency = Date.now() - start;
|
|
310
|
+
let hostname: string | undefined;
|
|
311
|
+
let version: string | undefined;
|
|
312
|
+
try {
|
|
313
|
+
const data = await res.json();
|
|
314
|
+
hostname = data?.hostname ?? data?.name;
|
|
315
|
+
version = data?.version;
|
|
316
|
+
} catch {
|
|
317
|
+
// /health may return plain text
|
|
318
|
+
}
|
|
319
|
+
return { url: base, hostname, version, latency };
|
|
320
|
+
} catch {
|
|
321
|
+
return null;
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
return new Promise<ProbeResult | null>((resolve) => {
|
|
326
|
+
let remaining = urls.length;
|
|
327
|
+
let settled = false;
|
|
328
|
+
for (const url of urls) {
|
|
329
|
+
probeOne(url)
|
|
330
|
+
.then((r) => {
|
|
331
|
+
if (settled) return;
|
|
332
|
+
if (r) {
|
|
333
|
+
settled = true;
|
|
334
|
+
resolve(r);
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
remaining -= 1;
|
|
338
|
+
if (remaining <= 0 && !settled) {
|
|
339
|
+
settled = true;
|
|
340
|
+
resolve(null);
|
|
341
|
+
}
|
|
342
|
+
})
|
|
343
|
+
.catch(() => {
|
|
344
|
+
remaining -= 1;
|
|
345
|
+
if (remaining <= 0 && !settled) {
|
|
346
|
+
settled = true;
|
|
347
|
+
resolve(null);
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
}
|
package/src/_core/index.ts
CHANGED