Skip to content

Commit 47518fb

Browse files
committed
fix(huddle): log once when the mediaDevices API is unavailable
Review feedback on #3817: the guard left the mount-time path silent, so a locked-down environment produced an empty device list and no diagnostic at all. Support only had a signal once someone tried to join, and even then only the user-facing copy. Emit one `console.warn` from `availableMediaDevices()`, latched so it fires at most once per process. Three call sites reach it and mount-time effects run twice under `React.StrictMode`, so an unguarded log would emit five identical lines and bury the signal. The latch is environment state, not community state, so it is deliberately not wired into `resetCommunityState()`. Signed-off-by: Dmitry Petrakov <[email protected]>
1 parent 8c6e6e0 commit 47518fb

3 files changed

Lines changed: 94 additions & 2 deletions

File tree

‎desktop/src/features/huddle/lib/mediaDevices.test.mjs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@ import test from "node:test";
33

44
import { availableMediaDevices } from "./mediaDevices.ts";
55

6-
/** Swap `globalThis.navigator` for the duration of `run`. */
6+
/**
7+
* Swap `globalThis.navigator` for the duration of `run`, and swallow the
8+
* missing-API diagnostic so it does not pollute test output. The
9+
* once-per-process behaviour of that warning is covered separately in
10+
* `mediaDevicesWarning.test.mjs`, which needs its own file to see the latch
11+
* unset.
12+
*/
713
function withNavigator(value, run) {
814
const descriptor = Object.getOwnPropertyDescriptor(globalThis, "navigator");
15+
const originalWarn = console.warn;
916
Object.defineProperty(globalThis, "navigator", {
1017
value,
1118
configurable: true,
1219
writable: true,
1320
});
21+
console.warn = () => {};
1422
try {
1523
return run();
1624
} finally {
25+
console.warn = originalWarn;
1726
if (descriptor) {
1827
Object.defineProperty(globalThis, "navigator", descriptor);
1928
} else {

‎desktop/src/features/huddle/lib/mediaDevices.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,32 @@
88
* Mirrors the guard already used in
99
* `features/profile/lib/animatedAvatarCapture.ts`.
1010
*/
11+
/**
12+
* Latched so the diagnostic below is emitted at most once per process.
13+
*
14+
* This is environment state, not community state: whether the webview exposes
15+
* `mediaDevices` cannot change when the user switches communities. It is
16+
* therefore deliberately NOT wired into `resetCommunityState()`.
17+
*/
18+
let missingApiWarned = false;
19+
1120
export function availableMediaDevices(): MediaDevices | null {
1221
const media =
1322
typeof navigator === "undefined" ? undefined : navigator.mediaDevices;
14-
return typeof media?.enumerateDevices === "function" ? media : null;
23+
if (typeof media?.enumerateDevices === "function") {
24+
return media;
25+
}
26+
27+
// One line, once. Callers hit this from three sites and mount-time effects
28+
// run twice under `React.StrictMode`, so an unguarded log would emit five
29+
// identical lines and bury the signal it exists to provide.
30+
if (!missingApiWarned) {
31+
missingApiWarned = true;
32+
console.warn(
33+
"[mediaDevices] navigator.mediaDevices is unavailable (non-secure context); huddle audio is disabled in this window",
34+
);
35+
}
36+
return null;
1537
}
1638

1739
/** Raw error thrown when a huddle needs a mic but the API is unavailable. */
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
import { availableMediaDevices } from "./mediaDevices.ts";
5+
6+
// Deliberately its own file. The once-per-process latch lives in module scope,
7+
// and the node test runner gives each FILE its own process — so this is the
8+
// only place that observes the latch in its initial state. Folding these cases
9+
// into `mediaDevices.test.mjs` would make them depend on declaration order
10+
// there, because those tests consume the latch on their first null result.
11+
12+
/** Run `body` with `navigator` and `console.warn` swapped out. */
13+
function captureWarnings(navigatorValue, body) {
14+
const navigatorDescriptor = Object.getOwnPropertyDescriptor(
15+
globalThis,
16+
"navigator",
17+
);
18+
const originalWarn = console.warn;
19+
const warnings = [];
20+
21+
Object.defineProperty(globalThis, "navigator", {
22+
value: navigatorValue,
23+
configurable: true,
24+
writable: true,
25+
});
26+
console.warn = (...args) => warnings.push(args.join(" "));
27+
28+
try {
29+
body();
30+
} finally {
31+
console.warn = originalWarn;
32+
if (navigatorDescriptor) {
33+
Object.defineProperty(globalThis, "navigator", navigatorDescriptor);
34+
} else {
35+
delete globalThis.navigator;
36+
}
37+
}
38+
return warnings;
39+
}
40+
41+
test("warns exactly once no matter how many callers hit the missing API", () => {
42+
const warnings = captureWarnings({}, () => {
43+
// Three call sites, and StrictMode runs the two mount effects twice.
44+
for (let i = 0; i < 5; i += 1) {
45+
assert.equal(availableMediaDevices(), null);
46+
}
47+
});
48+
49+
assert.equal(warnings.length, 1, "expected a single diagnostic line");
50+
assert.match(warnings[0], /^\[mediaDevices\]/);
51+
assert.match(warnings[0], /non-secure context/);
52+
});
53+
54+
test("stays quiet on later calls once the API is present again", () => {
55+
const media = { enumerateDevices: () => Promise.resolve([]) };
56+
const warnings = captureWarnings({ mediaDevices: media }, () => {
57+
assert.equal(availableMediaDevices(), media);
58+
});
59+
60+
assert.deepEqual(warnings, []);
61+
});

0 commit comments

Comments
 (0)