-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathbrowserAccess.ts
More file actions
126 lines (104 loc) · 4.12 KB
/
Copy pathbrowserAccess.ts
File metadata and controls
126 lines (104 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import type { TranslationKey } from "../i18n";
export type BrowserMediaPermissionKind = "camera" | "microphone";
export type BrowserPermissionState = "denied" | "granted" | "prompt" | "unsupported";
type BrowserPermissionsApi = Readonly<{
query: (descriptor: PermissionDescriptor) => Promise<Readonly<{ state: PermissionState }>>;
}>;
function getPermissionsApi(): BrowserPermissionsApi | null {
const permissionsValue = navigator.permissions;
if (permissionsValue === undefined) {
return null;
}
return permissionsValue as unknown as BrowserPermissionsApi;
}
export async function queryBrowserPermissionState(kind: BrowserMediaPermissionKind): Promise<BrowserPermissionState> {
const permissionsApi = getPermissionsApi();
if (permissionsApi === null) {
return "unsupported";
}
try {
const result = await permissionsApi.query({ name: kind as PermissionName });
if (result.state === "granted" || result.state === "denied" || result.state === "prompt") {
return result.state;
}
return "unsupported";
} catch {
return "unsupported";
}
}
export async function requestBrowserMediaPermission(kind: BrowserMediaPermissionKind): Promise<void> {
const mediaDevices = navigator.mediaDevices;
if (mediaDevices === undefined || typeof mediaDevices.getUserMedia !== "function") {
throw new Error("Media device access is unavailable in this browser.");
}
const isLocalHost = window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1";
if (window.isSecureContext === false && isLocalHost === false) {
throw new Error("Media permissions require HTTPS or localhost.");
}
const stream = await mediaDevices.getUserMedia(
kind === "camera"
? { audio: false, video: true }
: { audio: true, video: false },
);
for (const track of stream.getTracks()) {
track.stop();
}
}
export function isExpectedClipboardWriteError(error: unknown): boolean {
return error instanceof DOMException
&& (error.name === "NotAllowedError" || error.name === "SecurityError");
}
export function isExpectedBrowserMediaPermissionError(error: unknown): boolean {
if (error instanceof DOMException) {
return error.name === "NotAllowedError"
|| error.name === "NotFoundError"
|| error.name === "NotReadableError"
|| error.name === "SecurityError";
}
if (error instanceof Error) {
return error.message === "Media device access is unavailable in this browser."
|| error.message === "Media permissions require HTTPS or localhost.";
}
return false;
}
export function explainBrowserMediaPermissionError(
kind: BrowserMediaPermissionKind,
error: unknown,
permissionState: BrowserPermissionState,
t: (key: TranslationKey) => string,
): string {
if (error instanceof DOMException) {
if (error.name === "NotAllowedError") {
if (permissionState === "denied") {
return kind === "camera"
? t("accessSettings.permission.errorNotAllowedDeniedCamera")
: t("accessSettings.permission.errorNotAllowedDeniedMicrophone");
}
return kind === "camera"
? t("accessSettings.permission.errorNotAllowedCamera")
: t("accessSettings.permission.errorNotAllowedMicrophone");
}
if (error.name === "NotFoundError") {
return kind === "camera"
? t("accessSettings.permission.errorNotFoundCamera")
: t("accessSettings.permission.errorNotFoundMicrophone");
}
if (error.name === "NotReadableError") {
return kind === "camera"
? t("accessSettings.permission.errorNotReadableCamera")
: t("accessSettings.permission.errorNotReadableMicrophone");
}
if (error.name === "SecurityError") {
return t("accessSettings.permission.errorSecureContext");
}
}
if (error instanceof Error) {
if (error.message === "Media device access is unavailable in this browser.") {
return t("accessSettings.permission.errorMediaUnavailable");
}
if (error.message === "Media permissions require HTTPS or localhost.") {
return t("accessSettings.permission.errorSecureContext");
}
}
return error instanceof Error ? error.message : String(error);
}