Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add setBigUint64 polyfill #577

Merged
merged 3 commits into from Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 42 additions & 6 deletions packages/agent/src/agent/http/http.test.ts
Expand Up @@ -362,10 +362,46 @@ 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('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;
let usePolyfill = false;

beforeAll(() => {
jest.spyOn(Math, 'random').mockImplementation(() => 0.5);
jest.spyOn(globalThis, 'DataView').mockImplementation(buffer => {
const view: DataView = new DataViewConstructor(buffer);
view.setBigUint64 = usePolyfill ? undefined : view.setBigUint64;
spyOnSetUint32 = jest.spyOn(view, 'setUint32');
return view;
});
});

afterAll(() => {
jest.clearAllMocks();
jest.restoreAllMocks();
});

it('should create same value using polyfill', () => {
const originalNonce = toHexString(makeNonce());
expect(spyOnSetUint32).toBeCalledTimes(2);

usePolyfill = true;

const nonce = toHexString(makeNonce());
expect(spyOnSetUint32).toBeCalledTimes(4);

expect(nonce).toBe(originalNonce);
});
});
});
9 changes: 8 additions & 1 deletion packages/agent/src/agent/http/types.ts
Expand Up @@ -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);

Expand Down