From cc4e8671a1ef72ad1eeebe95f330f47f6e690bae Mon Sep 17 00:00:00 2001 From: mstrasinskis Date: Thu, 2 Jun 2022 11:50:49 +0200 Subject: [PATCH 1/3] fix: add setBigUint64 polyfill --- packages/agent/src/agent/http/types.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/agent/src/agent/http/types.ts b/packages/agent/src/agent/http/types.ts index ff8e3da92..e0dcfc489 100644 --- a/packages/agent/src/agent/http/types.ts +++ b/packages/agent/src/agent/http/types.ts @@ -110,7 +110,14 @@ export function makeNonce(): Nonce { const now = BigInt(+Date.now()); const randHi = Math.floor(Math.random() * 0xffffffff); const randLo = Math.floor(Math.random() * 0xffffffff); - view.setBigUint64(0, now); + // Fix for IOS < 14.8 setBigUint64 absence + if (typeof view.setBigUint64 === 'function') { + view.setBigUint64(0, now); + } else { + const TWO_TO_THE_32 = BigInt(1) << BigInt(32); + view.setUint32(0, Number(now >> BigInt(32))); + view.setUint32(4, Number(now % TWO_TO_THE_32)); + } view.setUint32(8, randHi); view.setUint32(12, randLo); From 38d43d620e211b470575e6a2fdc79bb28d008a2c Mon Sep 17 00:00:00 2001 From: mstrasinskis Date: Thu, 2 Jun 2022 17:03:15 +0200 Subject: [PATCH 2/3] test: makeNonce setBigUint64 polyfill --- packages/agent/src/agent/http/http.test.ts | 41 ++++++++++++++++++---- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/packages/agent/src/agent/http/http.test.ts b/packages/agent/src/agent/http/http.test.ts index e68ddfd43..a7de9a905 100644 --- a/packages/agent/src/agent/http/http.test.ts +++ b/packages/agent/src/agent/http/http.test.ts @@ -362,10 +362,39 @@ describe('replace identity', () => { expect(mockFetch).toBeCalledTimes(1); }); }); -describe('makeNonce should create unique values', () => { - const nonces = new Set(); - for (let i = 0; i < 100; i++) { - nonces.add(toHexString(makeNonce())); - } - expect(nonces.size).toBe(100); + +describe.only('makeNonce', () => { + it('should create unique values', () => { + const nonces = new Set(); + for (let i = 0; i < 100; i++) { + nonces.add(toHexString(makeNonce())); + } + expect(nonces.size).toBe(100); + }); + + describe('setBigUint64 polyfill', () => { + const DataViewConstructor = DataView; + let spyOnSetUint32: jest.SpyInstance; + + beforeAll(() => + jest.spyOn(globalThis, 'DataView').mockImplementation(buffer => { + const view: DataView = new DataViewConstructor(buffer); + view.setBigUint64 = undefined; + spyOnSetUint32 = jest.spyOn(view, 'setUint32'); + return view; + }), + ); + afterAll(jest.restoreAllMocks); + + it('should create unique values using polyfill', () => { + const nonces = new Set(); + + for (let i = 0; i < 100; i++) { + nonces.add(toHexString(makeNonce())); + expect(spyOnSetUint32).toBeCalledTimes(4); + } + + expect(nonces.size).toBe(100); + }); + }); }); From f000c4df3d2db87fdcf7ad7334c2116167269724 Mon Sep 17 00:00:00 2001 From: mstrasinskis Date: Thu, 2 Jun 2022 22:25:29 +0200 Subject: [PATCH 3/3] test: update makeNonce polyfill test (should create same value) --- packages/agent/src/agent/http/http.test.ts | 33 +++++++++++++--------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/packages/agent/src/agent/http/http.test.ts b/packages/agent/src/agent/http/http.test.ts index a7de9a905..21cf8d002 100644 --- a/packages/agent/src/agent/http/http.test.ts +++ b/packages/agent/src/agent/http/http.test.ts @@ -363,7 +363,7 @@ describe('replace identity', () => { }); }); -describe.only('makeNonce', () => { +describe('makeNonce', () => { it('should create unique values', () => { const nonces = new Set(); for (let i = 0; i < 100; i++) { @@ -375,26 +375,33 @@ describe.only('makeNonce', () => { describe('setBigUint64 polyfill', () => { const DataViewConstructor = DataView; let spyOnSetUint32: jest.SpyInstance; + let usePolyfill = false; - beforeAll(() => + beforeAll(() => { + jest.spyOn(Math, 'random').mockImplementation(() => 0.5); jest.spyOn(globalThis, 'DataView').mockImplementation(buffer => { const view: DataView = new DataViewConstructor(buffer); - view.setBigUint64 = undefined; + view.setBigUint64 = usePolyfill ? undefined : view.setBigUint64; spyOnSetUint32 = jest.spyOn(view, 'setUint32'); return view; - }), - ); - afterAll(jest.restoreAllMocks); + }); + }); + + afterAll(() => { + jest.clearAllMocks(); + jest.restoreAllMocks(); + }); + + it('should create same value using polyfill', () => { + const originalNonce = toHexString(makeNonce()); + expect(spyOnSetUint32).toBeCalledTimes(2); - it('should create unique values using polyfill', () => { - const nonces = new Set(); + usePolyfill = true; - for (let i = 0; i < 100; i++) { - nonces.add(toHexString(makeNonce())); - expect(spyOnSetUint32).toBeCalledTimes(4); - } + const nonce = toHexString(makeNonce()); + expect(spyOnSetUint32).toBeCalledTimes(4); - expect(nonces.size).toBe(100); + expect(nonce).toBe(originalNonce); }); }); });