yaver-feedback-react-native 0.7.0 → 0.7.2

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/src/auth.ts CHANGED
@@ -67,8 +67,13 @@ const TOKEN_KEY = 'yaver_feedback_auth_token';
67
67
  const USER_KEY = 'yaver_feedback_user';
68
68
  const DEVICE_KEY = 'yaver_feedback_selected_device';
69
69
 
70
+ // Source of truth: mobile/src/lib/constants.ts → CONVEX_SITE_URL.
71
+ // The yaver-io Convex deployment was migrated from shocking-echidna-394
72
+ // to perceptive-minnow-557; sessions minted against the old deployment
73
+ // don't validate on agents that point at the new one, producing a 403
74
+ // "invalid token" from the agent's authSDK middleware.
70
75
  export const DEFAULT_CONVEX_SITE_URL =
71
- 'https://shocking-echidna-394.eu-west-1.convex.site';
76
+ 'https://perceptive-minnow-557.eu-west-1.convex.site';
72
77
  export const DEFAULT_WEB_BASE_URL = 'https://yaver.io';
73
78
 
74
79
  let convexSiteUrl = DEFAULT_CONVEX_SITE_URL;
@@ -409,7 +414,17 @@ export interface RemoteDevice {
409
414
  accessScope: 'owner' | 'shared-scoped' | 'shared-legacy';
410
415
  quicHost: string;
411
416
  quicPort: number;
417
+ /** Agent HTTP port — preferred over quicPort when present. */
418
+ httpPort?: number;
412
419
  publicKey?: string;
420
+ /** Hardware identifier — used for dedup across re-pair events. */
421
+ hwid?: string;
422
+ /**
423
+ * Every LAN IP the agent reported in its last heartbeat. Useful on
424
+ * multi-homed hosts — probing all of them in parallel is the same
425
+ * trick the Yaver mobile app uses to "just work" on the same Wi-Fi.
426
+ */
427
+ localIps?: string[];
413
428
  }
414
429
 
415
430
  export interface DeviceList {
@@ -420,6 +435,10 @@ export interface DeviceList {
420
435
  /**
421
436
  * Fetch the set of remote dev machines this user can reach. Splits into
422
437
  * owned (user is the host) vs shared (host invited them as a guest).
438
+ *
439
+ * Collapses duplicate rows before splitting — Convex can return multiple
440
+ * rows per physical machine after a re-pair or hostname change, and the
441
+ * raw list used to render as "Kvancs-MacBook-Air.local ×3" in the picker.
423
442
  */
424
443
  export async function listReachableDevices(
425
444
  token: string,
@@ -430,10 +449,39 @@ export async function listReachableDevices(
430
449
  });
431
450
  if (!res.ok) return { owned: [], shared: [] };
432
451
  const data = await res.json();
433
- const all = (data.devices ?? []) as RemoteDevice[];
452
+ const raw = (data.devices ?? []) as any[];
453
+ // Normalise Convex field names → SDK's RemoteDevice shape. The
454
+ // backend returns `localIps`, sometimes the mobile-side mapping
455
+ // surfaces `lanIps` — accept either so the field survives.
456
+ const normalised: RemoteDevice[] = raw.map((d) => ({
457
+ deviceId: d.deviceId ?? d.id,
458
+ name: d.name ?? '',
459
+ platform: d.platform ?? d.os ?? '',
460
+ isOnline: !!d.isOnline,
461
+ needsAuth: !!d.needsAuth,
462
+ runnerDown: !!d.runnerDown,
463
+ lastHeartbeat: d.lastHeartbeat ?? 0,
464
+ isGuest: !!d.isGuest,
465
+ hostName: d.hostName,
466
+ hostEmail: d.hostEmail,
467
+ accessScope: d.accessScope ?? 'owner',
468
+ quicHost: d.quicHost ?? d.host ?? '',
469
+ quicPort: d.quicPort ?? 0,
470
+ httpPort: d.httpPort ?? d.quicPort,
471
+ publicKey: d.publicKey,
472
+ hwid: d.hardwareId ?? d.hwid,
473
+ localIps: Array.isArray(d.localIps)
474
+ ? d.localIps
475
+ : Array.isArray(d.lanIps)
476
+ ? d.lanIps
477
+ : undefined,
478
+ }));
479
+ // Lazy require so Jest + tree-shakers don't choke on a circular import.
480
+ const { collapseRemoteDevices } = require('./deviceDedup') as typeof import('./deviceDedup');
481
+ const deduped = collapseRemoteDevices(normalised);
434
482
  return {
435
- owned: all.filter((d) => !d.isGuest),
436
- shared: all.filter((d) => d.isGuest),
483
+ owned: deduped.filter((d) => !d.isGuest),
484
+ shared: deduped.filter((d) => d.isGuest),
437
485
  };
438
486
  } catch {
439
487
  return { owned: [], shared: [] };
@@ -0,0 +1,184 @@
1
+ /**
2
+ * Device deduplication + online-signal merging, ported from the Yaver
3
+ * mobile app's DeviceContext.collapseAliasDevices.
4
+ *
5
+ * Convex stores one row per pair (device, re-install). After a re-pair
6
+ * or hostname change the list can contain 2-3 rows for the same
7
+ * physical machine, with different hwid/publicKey values. The picker
8
+ * then shows duplicates and the user can't tell which one is live.
9
+ *
10
+ * This file collapses rows in three passes:
11
+ * 1. Identity key (hwid → publicKey → "host:os:name" → id → name)
12
+ * 2. Alias key (os + normalized-hostname) — catches re-pairs that
13
+ * mint a new hwid
14
+ * 3. Endpoint key (host:port)
15
+ *
16
+ * When collapsing, `mergeDeviceEntries` prefers: authenticated over
17
+ * needsAuth, online over offline, freshest lastHeartbeat.
18
+ */
19
+
20
+ import type { RemoteDevice } from './auth';
21
+
22
+ function normalizedName(name: string | undefined): string {
23
+ return String(name || '').trim().toLowerCase().replace(/\.local$/i, '');
24
+ }
25
+
26
+ function normalizedHost(host: string | undefined): string {
27
+ return String(host || '').trim().toLowerCase().replace(/\.local$/i, '');
28
+ }
29
+
30
+ function identityKey(d: RemoteDevice): string {
31
+ if (d.hwid) return `hwid:${d.hwid}`;
32
+ if (d.publicKey) return `pub:${d.publicKey}`;
33
+ if (d.isGuest) {
34
+ const scope = d.hostEmail || d.hostName || 'guest';
35
+ return `guest:${scope}:${d.deviceId || d.name}`;
36
+ }
37
+ const n = normalizedName(d.name);
38
+ const os = String(d.platform || '').trim().toLowerCase();
39
+ if (n && os) return `host:${os}:${n}`;
40
+ if (d.deviceId) return `id:${d.deviceId}`;
41
+ return `name:${d.name}`;
42
+ }
43
+
44
+ function aliasKey(d: RemoteDevice): string | null {
45
+ if (d.isGuest) return null;
46
+ const n = normalizedName(d.name);
47
+ const os = String(d.platform || '').trim().toLowerCase();
48
+ if (!n || !os) return null;
49
+ return `${os}:${n}`;
50
+ }
51
+
52
+ function endpointKey(d: RemoteDevice): string | null {
53
+ if (d.isGuest) return null;
54
+ const h = normalizedHost(d.quicHost);
55
+ if (!h) return null;
56
+ return `${h}:${d.quicPort || 0}`;
57
+ }
58
+
59
+ function mergeEntries(existing: RemoteDevice, incoming: RemoteDevice): RemoteDevice {
60
+ const incomingWins =
61
+ (!!existing.needsAuth && !incoming.needsAuth) ||
62
+ (incoming.lastHeartbeat || 0) > (existing.lastHeartbeat || 0) ||
63
+ (!!incoming.isOnline && !existing.isOnline);
64
+ const base = incomingWins ? incoming : existing;
65
+ const other = incomingWins ? existing : incoming;
66
+ return {
67
+ ...other,
68
+ ...base,
69
+ quicHost: base.quicHost || other.quicHost,
70
+ quicPort: base.quicPort || other.quicPort,
71
+ isOnline: base.isOnline || other.isOnline,
72
+ runnerDown: base.runnerDown && other.runnerDown,
73
+ publicKey: base.publicKey || other.publicKey,
74
+ lastHeartbeat: Math.max(existing.lastHeartbeat || 0, incoming.lastHeartbeat || 0),
75
+ };
76
+ }
77
+
78
+ // When two rows share the same alias key (hostname + OS) but differ on
79
+ // hwid/publicKey, pick the active one over the stale needs-auth leftover.
80
+ function pickActiveOverStaleNeedsAuth(a: RemoteDevice, b: RemoteDevice): RemoteDevice | null {
81
+ const aDead = a.needsAuth && !a.isOnline;
82
+ const bDead = b.needsAuth && !b.isOnline;
83
+ const aLive = !a.needsAuth && a.isOnline;
84
+ const bLive = !b.needsAuth && b.isOnline;
85
+ if (aDead && bLive) return b;
86
+ if (bDead && aLive) return a;
87
+ return null;
88
+ }
89
+
90
+ /**
91
+ * Collapse a Convex device list so each physical machine appears once.
92
+ * Safe on an empty list; idempotent on an already-deduped list.
93
+ */
94
+ export function collapseRemoteDevices(devices: RemoteDevice[]): RemoteDevice[] {
95
+ if (!Array.isArray(devices) || devices.length === 0) return [];
96
+
97
+ const byIdentity = new Map<string, RemoteDevice>();
98
+ for (const d of devices) {
99
+ const k = identityKey(d);
100
+ const prev = byIdentity.get(k);
101
+ byIdentity.set(k, prev ? mergeEntries(prev, d) : d);
102
+ }
103
+
104
+ const byAlias = new Map<string, RemoteDevice>();
105
+ for (const d of byIdentity.values()) {
106
+ const k = aliasKey(d);
107
+ if (!k) {
108
+ byAlias.set(`id:${d.deviceId}`, d);
109
+ continue;
110
+ }
111
+ const prev = byAlias.get(k);
112
+ if (!prev) {
113
+ byAlias.set(k, d);
114
+ continue;
115
+ }
116
+ const strongIdentityConflict =
117
+ !!prev.publicKey && !!d.publicKey && prev.publicKey !== d.publicKey;
118
+ if (strongIdentityConflict) {
119
+ const winner = pickActiveOverStaleNeedsAuth(prev, d);
120
+ if (winner) {
121
+ byAlias.set(k, winner);
122
+ continue;
123
+ }
124
+ }
125
+ byAlias.set(k, mergeEntries(prev, d));
126
+ }
127
+
128
+ const byEndpoint = new Map<string, RemoteDevice>();
129
+ for (const d of byAlias.values()) {
130
+ const k = endpointKey(d);
131
+ if (!k) {
132
+ byEndpoint.set(`id:${d.deviceId}`, d);
133
+ continue;
134
+ }
135
+ const prev = byEndpoint.get(k);
136
+ byEndpoint.set(k, prev ? mergeEntries(prev, d) : d);
137
+ }
138
+
139
+ return [...byEndpoint.values()];
140
+ }
141
+
142
+ /**
143
+ * Threshold for "online" based on heartbeat age, in milliseconds.
144
+ * Matches the mobile app (HEARTBEAT_STALE_MS = 90 s). The SDK used
145
+ * 60 s, which flashed yellow on single missed beats.
146
+ */
147
+ export const HEARTBEAT_STALE_MS = 90_000;
148
+
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
+ export function isDeviceFresh(d: RemoteDevice): boolean {
155
+ if (!d.isOnline) return false;
156
+ if (!d.lastHeartbeat) return true;
157
+ return Date.now() - d.lastHeartbeat < HEARTBEAT_STALE_MS;
158
+ }
159
+
160
+ /**
161
+ * Pick the best candidate for an auto-connect attempt. Preference:
162
+ * 1. matches the preferred deviceId when supplied + still fresh
163
+ * 2. fresh (online + recent heartbeat) + has a quicHost
164
+ * 3. online + has a quicHost
165
+ * 4. first with a quicHost
166
+ */
167
+ export function pickTargetDevice(
168
+ devices: RemoteDevice[],
169
+ preferredDeviceId?: string,
170
+ ): RemoteDevice | null {
171
+ if (!devices.length) return null;
172
+ if (preferredDeviceId) {
173
+ const preferred = devices.find(
174
+ (d) => d.deviceId === preferredDeviceId && d.quicHost,
175
+ );
176
+ if (preferred && isDeviceFresh(preferred)) return preferred;
177
+ if (preferred) return preferred;
178
+ }
179
+ const fresh = devices.find((d) => isDeviceFresh(d) && d.quicHost);
180
+ if (fresh) return fresh;
181
+ const online = devices.find((d) => d.isOnline && d.quicHost);
182
+ if (online) return online;
183
+ return devices.find((d) => d.quicHost) || devices[0] || null;
184
+ }