Skip to content

Commit

Permalink
Revert audio fingerprint to the version from v4.1.0 (#1007)
Browse files Browse the repository at this point in the history
  • Loading branch information
Finesse committed May 31, 2024
1 parent 59401f4 commit ff6b204
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 245 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default { load, hashComponents, componentsToDebugString }
export const murmurX64Hash128 = x64hash128
export { prepareForSources } from './agent'
export { sources } from './sources'
export { getUnstableAudioFingerprint, doesBrowserSuspendAudioContext, renderAudio } from './sources/audio'
export { getUnstableAudioFingerprint } from './sources/audio'
export { getUnstableCanvasFingerprint } from './sources/canvas'
export { getUnstableScreenFrame } from './sources/screen_frame'
export { getUnstableScreenResolution } from './sources/screen_resolution'
Expand Down
77 changes: 29 additions & 48 deletions src/sources/audio.test.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,48 @@
import { getBrowserMajorVersion, isChromium, isGecko, isMobile, isSafari, isWebKit } from '../../tests/utils'
import getAudioFingerprint, { getMiddle, SpecialFingerprint, stabilize } from './audio'
import { getBrowserMajorVersion, isMobile, isSafari } from '../../tests/utils'
import getAudioFingerprint, { SpecialFingerprint } from './audio'

describe('Sources', () => {
describe('audio', () => {
it('returns expected value', async () => {
const result = (await getAudioFingerprint())()
it('returns expected value type depending on the browser', async () => {
const result = getAudioFingerprint()

if (doesBrowserSuspendAudioContext()) {
if (doesBrowserPerformAntifingerprinting()) {
expect(result).toBe(SpecialFingerprint.KnownForAntifingerprinting)
} else if (doesBrowserSuspendAudioContext()) {
expect(result).toBe(SpecialFingerprint.KnownForSuspending)
} else if (isGecko()) {
expect(result).toBeGreaterThan(0.000169)
expect(result).toBeLessThan(0.00017)
} else if (isWebKit()) {
if ('OfflineAudioContext' in window) {
expect(result).toBeGreaterThan(0.000058)
expect(result).toBeLessThan(0.000066)
} else {
expect(result).toBeGreaterThan(0.000113)
expect(result).toBeLessThan(0.000114)
}
} else if (isChromium()) {
expect(result).toBeGreaterThan(0.00006)
expect(result).toBeLessThan(0.00009)
} else {
throw new Error('Unexpected browser')
// A type guard
if (typeof result !== 'function') {
throw new Error('Expected to be a function')
}
const fingerprint = await result()
expect(fingerprint).toBeGreaterThanOrEqual(0)
const newFingerprint = await result()
expect(newFingerprint).toBe(newFingerprint)
}
})

it('returns a stable value', async () => {
const finishFirst = await getAudioFingerprint()
const first = finishFirst()
const finishSecond = await getAudioFingerprint()
const second = finishSecond()
expect(first).toBe(second)
expect(finishFirst()).toBe(first)
})
})
const first = getAudioFingerprint()
const second = getAudioFingerprint()

describe('getMiddle helper', () => {
it('calculates properly and ignores zeros', () => {
const signal = [
0, -5.92384345, -3.39578492, -4.39564854, 0, -3.89384529, -8.92384234, -5.01429423, -4.12045834, -3.93298453, 0,
]
expect(getMiddle(signal)).toEqual(-6.15981363)
})
})
if (first === second) {
return
}

if (typeof first !== 'function' || typeof second !== 'function') {
throw new Error('Expected to be a function')
}

describe('stabilize helper', () => {
it('calculates properly', () => {
expect(stabilize(0.000123456789, 2)).toBe(0.00012)
expect(stabilize(0.000000012345, 2)).toBe(0.000000012)
expect(stabilize(-0.000123456789, 2)).toBe(-0.00012)
expect(stabilize(0.000123456789, 7)).toBe(0.0001234568)
expect(stabilize(0.000123456789, 2.2)).toBe(0.000125)
expect(stabilize(0.000123456789, 2.5)).toBe(0.000124)
expect(stabilize(0.000123456789, 6.2)).toBe(0.000123457)
expect(stabilize(0.000123456789, 15.5)).toBe(0.000123456789)
expect(stabilize(0.000123456789, 5.2)).toBe(0.000123455)
expect(stabilize(0.000123456789, 5.5)).toBe(0.000123456)
expect(await second()).toBe(await first())
})
})
})

function doesBrowserPerformAntifingerprinting() {
return isSafari() && (getBrowserMajorVersion() ?? 0) >= 17
}

function doesBrowserSuspendAudioContext() {
// WebKit has stopped telling its real version in the user-agent string since version 605.1.15,
// therefore the browser version has to be checked instead of the engine version.
Expand Down
Loading

0 comments on commit ff6b204

Please sign in to comment.