Skip to content

Commit 78ad2d2

Browse files
authored
feat: detect support for WebRTC data channels (#56)
The use of WebRTC in IPFS is only as a data channel, we don't use getUserMedia and sometimes the user has restricted access to this API so add a 'supports' test that is just for data channels. Fixes #50
1 parent 61c7fe2 commit 78ad2d2

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/supports.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ const globalThis = require('./globalthis')
55
module.exports = {
66
supportsFileReader: typeof self !== 'undefined' && 'FileReader' in self,
77
supportsWebRTC: 'RTCPeerConnection' in globalThis &&
8-
(typeof navigator !== 'undefined' && typeof navigator.mediaDevices !== 'undefined' && 'getUserMedia' in navigator.mediaDevices)
8+
(typeof navigator !== 'undefined' && typeof navigator.mediaDevices !== 'undefined' && 'getUserMedia' in navigator.mediaDevices),
9+
supportsWebRTCDataChannels: 'RTCPeerConnection' in globalThis
910
}

test/supports.spec.js

+41-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('supports', function () {
6262
}
6363
})
6464

65-
it('supportsWebRTC should return true in Web Worker', function () {
65+
it('supportsWebRTC should return false in Web Worker', function () {
6666
if (env.isWebWorker) {
6767
expect(supports.supportsWebRTC).to.be.false()
6868
} else {
@@ -85,4 +85,44 @@ describe('supports', function () {
8585
this.skip()
8686
}
8787
})
88+
89+
it('supportsWebRTCDataChannels should return false in node', function () {
90+
if (env.isNode) {
91+
expect(supports.supportsWebRTCDataChannels).to.be.false()
92+
} else {
93+
this.skip()
94+
}
95+
})
96+
97+
it('supportsWebRTCDataChannels should return true in browser', function () {
98+
if (env.isBrowser) {
99+
expect(supports.supportsWebRTCDataChannels).to.be.true()
100+
} else {
101+
this.skip()
102+
}
103+
})
104+
105+
it('supportsWebRTCDataChannels should return false in Web Worker', function () {
106+
if (env.isWebWorker) {
107+
expect(supports.supportsWebRTCDataChannels).to.be.false()
108+
} else {
109+
this.skip()
110+
}
111+
})
112+
113+
it('supportsWebRTCDataChannels should return false in Electron main', function () {
114+
if (env.isElectron && !env.isElectronRenderer) {
115+
expect(supports.supportsWebRTCDataChannels).to.be.false()
116+
} else {
117+
this.skip()
118+
}
119+
})
120+
121+
it('supportsWebRTCDataChannels should return true in Electron renderer', function () {
122+
if (env.isElectronRenderer) {
123+
expect(supports.supportsWebRTCDataChannels).to.be.true()
124+
} else {
125+
this.skip()
126+
}
127+
})
88128
})

0 commit comments

Comments
 (0)