Skip to content

Commit

Permalink
feat: AudioContext baseLatency entropy source (#1004)
Browse files Browse the repository at this point in the history
  • Loading branch information
dj-stormtrooper committed May 30, 2024
1 parent 703ea10 commit c975bbf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/sources/audio_base_latency.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { getBrowserVersion, isGecko, isWebKit } from '../../tests/utils'
import getAudioBaseLatency from './audio_base_latency'

describe('Sources', () => {
describe('audioBaseLatency', () => {
it("doesn't fail", () => {
const result = getAudioBaseLatency()
if (!hasBaseLatencySupport()) {
expect(result).toBe(undefined)
return
}

if (isGecko()) {
expect(result).toBe(0)
} else {
expect(result).toBeGreaterThan(0)
}
})

it('returns a stable value', () => {
const first = getAudioBaseLatency()
const second = getAudioBaseLatency()
expect(second).toBe(first)
})
})
})

function hasBaseLatencySupport() {
if (isWebKit()) {
const { major, minor } = getBrowserVersion() || { major: 0, minor: 0 }
if (major < 14) {
return false
} else if (major == 14) {
return minor >= 1
}
}
return true
}
7 changes: 7 additions & 0 deletions src/sources/audio_base_latency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function getAudioContextBaseLatency(): number | undefined {
if (!window.AudioContext) {
return undefined
}

return new AudioContext().baseLatency
}
2 changes: 2 additions & 0 deletions src/sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import getArchitecture from './architecture'
import getApplePayState from './apple_pay'
import getPrivateClickMeasurement from './private_click_measurement'
import { getWebGlBasics, getWebGlExtensions } from './webgl'
import getAudioContextBaseLatency from './audio_base_latency'

/**
* The list of entropy sources used to make visitor identifiers.
Expand Down Expand Up @@ -92,6 +93,7 @@ export const sources = {
architecture: getArchitecture,
applePay: getApplePayState,
privateClickMeasurement: getPrivateClickMeasurement,
audioBaseLatency: getAudioContextBaseLatency,

// Some sources can affect other sources (e.g. WebGL can affect canvas), so it's important to run these sources
// after other sources.
Expand Down

0 comments on commit c975bbf

Please sign in to comment.