Skip to content

Commit

Permalink
feat: detect support for WebRTC data channels (#56)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
achingbrain authored Aug 12, 2020
1 parent 61c7fe2 commit 78ad2d2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/supports.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ const globalThis = require('./globalthis')
module.exports = {
supportsFileReader: typeof self !== 'undefined' && 'FileReader' in self,
supportsWebRTC: 'RTCPeerConnection' in globalThis &&
(typeof navigator !== 'undefined' && typeof navigator.mediaDevices !== 'undefined' && 'getUserMedia' in navigator.mediaDevices)
(typeof navigator !== 'undefined' && typeof navigator.mediaDevices !== 'undefined' && 'getUserMedia' in navigator.mediaDevices),
supportsWebRTCDataChannels: 'RTCPeerConnection' in globalThis
}
42 changes: 41 additions & 1 deletion test/supports.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('supports', function () {
}
})

it('supportsWebRTC should return true in Web Worker', function () {
it('supportsWebRTC should return false in Web Worker', function () {
if (env.isWebWorker) {
expect(supports.supportsWebRTC).to.be.false()
} else {
Expand All @@ -85,4 +85,44 @@ describe('supports', function () {
this.skip()
}
})

it('supportsWebRTCDataChannels should return false in node', function () {
if (env.isNode) {
expect(supports.supportsWebRTCDataChannels).to.be.false()
} else {
this.skip()
}
})

it('supportsWebRTCDataChannels should return true in browser', function () {
if (env.isBrowser) {
expect(supports.supportsWebRTCDataChannels).to.be.true()
} else {
this.skip()
}
})

it('supportsWebRTCDataChannels should return false in Web Worker', function () {
if (env.isWebWorker) {
expect(supports.supportsWebRTCDataChannels).to.be.false()
} else {
this.skip()
}
})

it('supportsWebRTCDataChannels should return false in Electron main', function () {
if (env.isElectron && !env.isElectronRenderer) {
expect(supports.supportsWebRTCDataChannels).to.be.false()
} else {
this.skip()
}
})

it('supportsWebRTCDataChannels should return true in Electron renderer', function () {
if (env.isElectronRenderer) {
expect(supports.supportsWebRTCDataChannels).to.be.true()
} else {
this.skip()
}
})
})

0 comments on commit 78ad2d2

Please sign in to comment.