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(client-utils): fix trace.js import of performance #21257

Merged
merged 10 commits into from
Jun 4, 2024
2 changes: 1 addition & 1 deletion packages/common/client-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"test:mocha": "npm run test:mocha:esm && npm run test:mocha:cjs",
"test:mocha:cjs": "mocha --recursive \"dist/test/mocha/**/*.spec.*js\" --exit -r node_modules/@fluid-internal/mocha-test-setup",
"test:mocha:esm": "mocha --recursive \"lib/test/mocha/**/*.spec.*js\" --exit -r node_modules/@fluid-internal/mocha-test-setup",
"tsc": "fluid-tsc commonjs --project ./tsconfig.cjs.json && copyfiles -f ./src/cjs/package.json ./dist",
"tsc": "fluid-tsc commonjs --project ./tsconfig.cjs.json && copyfiles -f ../../../common/build/build-common/src/cjs/package.json ./dist",
"typetests:gen": "flub generate typetests --dir . -v --level public",
"typetests:prepare": "flub typetests --dir . --reset --previous --normalize"
},
Expand Down
7 changes: 0 additions & 7 deletions packages/common/client-utils/src/cjs/package.json

This file was deleted.

33 changes: 33 additions & 0 deletions packages/common/client-utils/src/test/jest/trace.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/

import { strict as assert } from "node:assert";
import { Trace } from "../../index.js";

describe("Trace", () => {
test("measure 2ms timeout", async () => {
const trace = Trace.start();

return new Promise((resolve, reject) => {
setTimeout(() => {
try {
const event = trace.trace();

// While we specify a 2ms timeout, it's possible for performance.now() to measure a
// slightly smaller duration due to timing attack and fingerprinting mitigations.
// (See https://developer.mozilla.org/en-US/docs/Web/API/Performance/now#security_requirements)
//
// Therefore, we conservatively require that only 1ms has elapsed.
assert(event.duration >= 1);
assert(event.totalTimeElapsed >= 1);

resolve(undefined);
} catch (error) {
reject(error);
}
}, 2);
});
});
});
33 changes: 33 additions & 0 deletions packages/common/client-utils/src/test/mocha/trace.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/

import { strict as assert } from "node:assert";
import { Trace } from "../../index.js";
jason-ha marked this conversation as resolved.
Show resolved Hide resolved

describe("Trace", () => {
it("measure 2ms timeout", async () => {
const trace = Trace.start();

return new Promise((resolve, reject) => {
setTimeout(() => {
try {
const event = trace.trace();

// While we specify a 2ms timeout, it's possible for performance.now() to measure a
// slightly smaller duration due to timing attack and fingerprinting mitigations.
// (See https://developer.mozilla.org/en-US/docs/Web/API/Performance/now#security_requirements)
//
// Therefore, we conservatively require that only 1ms has elapsed.
assert(event.duration >= 1);
assert(event.totalTimeElapsed >= 1);

resolve(undefined);
} catch (error) {
reject(error);
}
}, 2);
});
});
});
2 changes: 1 addition & 1 deletion packages/common/client-utils/src/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License.
*/

import { performance } from "./indexNode.js";
import { performance } from "./performanceIsomorphic.js";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual fix. There is no node/browser specific code involved. PerformanceIsomorphic just narrows the type of the global performance object to the intersection of what browser/node support.


/**
* Helper class for tracing performance of events
Expand Down
Loading