Skip to content

Commit 8cb1d33

Browse files
committed
fix(QrcodeStream): wrong error for insecure context
For insecure context (HTTP rather than HTTPS) the component fails with the throws the *Stream API not supported error* even when the API is actually supported. fix #110
1 parent 467527b commit 8cb1d33

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/misc/camera.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import adapterFactory from "webrtc-adapter/src/js/adapter_factory.js";
2-
import { StreamApiNotSupportedError } from "./errors.js";
2+
import { StreamApiNotSupportedError, InsecureContextError } from "./errors.js";
33
import { imageDataFromVideo } from "./image-data.js";
44
import { eventOn } from "callforth";
55

@@ -18,15 +18,28 @@ class Camera {
1818
}
1919
}
2020

21-
const STREAM_API_SUPPORTED =
21+
const INSECURE_CONTEXT = window.isSecureContext !== true;
22+
23+
const STREAM_API_NOT_SUPPORTED = !(
2224
navigator &&
2325
(navigator.getUserMedia ||
24-
(navigator.mediaDevices && navigator.mediaDevices.getUserMedia));
26+
(navigator.mediaDevices && navigator.mediaDevices.getUserMedia))
27+
);
2528

2629
let streamApiShimApplied = false;
2730

2831
export default async function(constraints, videoEl) {
29-
if (!STREAM_API_SUPPORTED) {
32+
// At least in Chrome `navigator.mediaDevices` is undefined when the page is
33+
// loaded using HTTP rather than HTTPS. Thus `STREAM_API_NOT_SUPPORTED` is
34+
// initialized with `false` although the API might actually be supported.
35+
// So although `getUserMedia` already should have a build-in mechanism to
36+
// detect insecure context (by throwing `NotAllowedError`), we have to do a
37+
// manual check before even calling `getUserMedia`.
38+
if (INSECURE_CONTEXT) {
39+
throw new InsecureContextError();
40+
}
41+
42+
if (STREAM_API_NOT_SUPPORTED) {
3043
throw new StreamApiNotSupportedError();
3144
}
3245

src/misc/errors.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,13 @@ export class StreamApiNotSupportedError extends Error {
2121
this.name = "StreamApiNotSupportedError";
2222
}
2323
}
24+
25+
export class InsecureContextError extends Error {
26+
constructor() {
27+
super(
28+
"camera access is only permitted in secure context. Use HTTPS or localhost rather than HTTP."
29+
);
30+
31+
this.name = "InsecureContextError";
32+
}
33+
}

0 commit comments

Comments
 (0)