Skip to content

Commit cb8243e

Browse files
authored
Unify performanceIsometric implementation, simplify typing, and enable sha.js typing in client-utils and common-utils (#18253)
1 parent 200548e commit cb8243e

23 files changed

+38
-106
lines changed

common/lib/common-utils/src/indexBrowser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ export {
1111
Uint8ArrayToString,
1212
} from "./bufferBrowser";
1313
export { gitHashFile, hashFile } from "./hashFileBrowser";
14-
export { performance } from "./performanceBrowser";
14+
export { performance } from "./performanceIsomorphic";

common/lib/common-utils/src/indexNode.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ export {
1111
Uint8ArrayToString,
1212
} from "./bufferNode";
1313
export { gitHashFile, hashFile } from "./hashFileNode";
14-
export { performance } from "./performanceNode";
14+
export { performance } from "./performanceIsomorphic";

common/lib/common-utils/src/performanceBrowser.ts

-11
This file was deleted.

common/lib/common-utils/src/performanceIsomorphic.ts

+5
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@
1111
*/
1212
export type IsomorphicPerformance = Partial<Performance> &
1313
Pick<Performance, "clearMarks" | "mark" | "measure" | "now">;
14+
15+
/**
16+
* @deprecated Moved to the `@fluidframework-internal/client-utils` package.
17+
*/
18+
export const performance: IsomorphicPerformance = globalThis.performance;

common/lib/common-utils/src/performanceNode.ts

-13
This file was deleted.

common/lib/common-utils/tsconfig.json

-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
"rootDir": "./src",
66
"outDir": "./dist",
77
"baseUrl": ".",
8-
"paths": {
9-
"perf_hooks": ["types/perf_hooks.d.ts"],
10-
},
118
},
129
"include": ["src/**/*"],
1310
"exclude": ["src/test/**/*"],

common/lib/common-utils/types/perf_hooks.d.ts

-10
This file was deleted.

packages/common/client-utils/api-report/client-utils.api.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
55
```ts
66

7+
/// <reference types="node" />
8+
79
import { EventEmitter } from 'events';
810
import { IDisposable } from '@fluidframework/core-interfaces';
911
import { IEvent } from '@fluidframework/core-interfaces';
@@ -12,13 +14,14 @@ import { IEventTransformer } from '@fluidframework/core-interfaces';
1214
import { TransformedEvent } from '@fluidframework/core-interfaces';
1315

1416
// @internal
15-
export class Buffer extends Uint8Array {
17+
class Buffer_2 extends Uint8Array {
1618
static from(value: unknown, encodingOrOffset?: unknown, length?: unknown): IsoBuffer;
1719
// (undocumented)
18-
static isBuffer(obj: unknown): obj is Buffer;
20+
static isBuffer(obj: unknown): obj is Buffer_2;
1921
// (undocumented)
2022
toString(encoding?: "utf8" | "utf-8" | "base64"): string;
2123
}
24+
export { Buffer_2 as Buffer }
2225

2326
// @internal
2427
export const bufferToString: (blob: ArrayBufferLike, encoding: "utf8" | "utf-8" | "base64") => string;
@@ -56,10 +59,10 @@ export function gitHashFile(file: IsoBuffer): Promise<string>;
5659
export function hashFile(file: IsoBuffer, algorithm?: "SHA-1" | "SHA-256", hashEncoding?: "hex" | "base64"): Promise<string>;
5760

5861
// @internal (undocumented)
59-
export const IsoBuffer: typeof Buffer;
62+
export const IsoBuffer: typeof Buffer_2;
6063

6164
// @internal (undocumented)
62-
export type IsoBuffer = Buffer;
65+
export type IsoBuffer = Buffer_2;
6366

6467
// @internal
6568
export type IsomorphicPerformance = Partial<Performance> & Pick<Performance, "clearMarks" | "mark" | "measure" | "now">;
@@ -71,7 +74,7 @@ export interface ITraceEvent {
7174
readonly totalTimeElapsed: number;
7275
}
7376

74-
// @internal (undocumented)
77+
// @internal
7578
const performance_2: IsomorphicPerformance;
7679
export { performance_2 as performance }
7780

packages/common/client-utils/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
"@types/node": "^16.18.38",
9393
"@types/puppeteer": "1.3.0",
9494
"@types/rewire": "^2.5.28",
95-
"@types/sha.js": "^2.4.0",
95+
"@types/sha.js": "^2.4.4",
9696
"@types/sinon": "^7.0.13",
9797
"c8": "^7.7.1",
9898
"concurrently": "^8.2.1",

packages/common/client-utils/src/hashFileNode.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
* Licensed under the MIT License.
44
*/
55

6-
// eslint-disable-next-line import/no-internal-modules
7-
import sha1 from "sha.js/sha1";
8-
// eslint-disable-next-line import/no-internal-modules
9-
import sha256 from "sha.js/sha256";
6+
import { sha1, sha256 } from "sha.js";
107

118
import type { IsoBuffer } from "./bufferNode";
129

@@ -32,12 +29,10 @@ export async function hashFile(
3229
// eslint-disable-next-line default-case
3330
switch (algorithm) {
3431
case "SHA-1": {
35-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
3632
engine = new sha1();
3733
break;
3834
}
3935
case "SHA-256": {
40-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
4136
engine = new sha256();
4237
break;
4338
}
@@ -59,8 +54,6 @@ export async function gitHashFile(file: IsoBuffer): Promise<string> {
5954
const size = file.byteLength;
6055
// eslint-disable-next-line unicorn/prefer-code-point
6156
const filePrefix = `blob ${size.toString()}${String.fromCharCode(0)}`;
62-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
6357
const engine = new sha1();
64-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
65-
return engine.update(filePrefix).update(file).digest("hex") as string;
58+
return engine.update(filePrefix).update(file).digest("hex");
6659
}

packages/common/client-utils/src/indexBrowser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ export {
1111
Uint8ArrayToString,
1212
} from "./bufferBrowser";
1313
export { gitHashFile, hashFile } from "./hashFileBrowser";
14-
export { performance } from "./performanceBrowser";
14+
export { performance } from "./performanceIsomorphic";

packages/common/client-utils/src/indexNode.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ export {
1111
Uint8ArrayToString,
1212
} from "./bufferNode";
1313
export { gitHashFile, hashFile } from "./hashFileNode";
14-
export { performance } from "./performanceNode";
14+
export { performance } from "./performanceIsomorphic";

packages/common/client-utils/src/performanceBrowser.ts

-11
This file was deleted.

packages/common/client-utils/src/performanceIsomorphic.ts

+10
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,13 @@
1111
*/
1212
export type IsomorphicPerformance = Partial<Performance> &
1313
Pick<Performance, "clearMarks" | "mark" | "measure" | "now">;
14+
15+
/**
16+
* This exported "performance" member masks the built-in globalThis.performance object
17+
* as an IsomorphicPerformance, which hides all of its features that aren't compatible
18+
* between Node and browser implementations. Anything exposed on this performance object
19+
* is considered safe to use regarless of the environment it runs in.
20+
*
21+
* @internal
22+
*/
23+
export const performance: IsomorphicPerformance = globalThis.performance;

packages/common/client-utils/src/performanceNode.ts

-14
This file was deleted.

packages/common/client-utils/src/test/jest/gitHash.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ async function evaluateBrowserHash(
7878
)) as string;
7979

8080
// reconstruct the Uint8Array from the string
81-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
8281
const charCodes = Array.prototype.map.call([...hashCharCodeString], (char: string) => {
8382
return char.charCodeAt(0);
8483
}) as number[];

packages/common/client-utils/src/test/mocha/typedEventEmitter.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ describe("TypedEventEmitter", () => {
3030
const tee = new TypedEventEmitter<IErrorEvent>();
3131
let newListenerCalls = 0;
3232
let removeListenerCalls = 0;
33-
// eslint-disable-next-line unicorn/consistent-function-scoping
3433
const errListener = (): void => {};
3534
tee.on("removeListener", (event, listener) => {
3635
assert.equal(event, "error");

packages/common/client-utils/tsconfig.json

-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,5 @@
99
"rootDir": "./src",
1010
"outDir": "./dist",
1111
"baseUrl": ".",
12-
"paths": {
13-
"perf_hooks": ["types/perf_hooks.d.ts"],
14-
},
15-
"types": ["events"],
1612
},
1713
}

packages/common/client-utils/types/perf_hooks.d.ts

-10
This file was deleted.

packages/drivers/driver-base/src/documentDeltaConnection.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class DocumentDeltaConnection
7474

7575
private _details: IConnected | undefined;
7676

77-
private trackLatencyTimeout: number | undefined;
77+
private trackLatencyTimeout: ReturnType<typeof setTimeout> | undefined;
7878

7979
// Listeners only needed while the connection is in progress
8080
private readonly connectionListeners: Map<string, (...args: any[]) => void> = new Map();
@@ -132,7 +132,7 @@ export class DocumentDeltaConnection
132132
logger.sendErrorEvent(
133133
{
134134
eventName: "DeltaConnection:EventException",
135-
name,
135+
name: name as string,
136136
},
137137
error,
138138
);

packages/drivers/odsp-driver/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
"@types/mocha": "^9.1.1",
8787
"@types/node": "^16.18.38",
8888
"@types/node-fetch": "^2.5.10",
89-
"@types/sha.js": "^2.4.0",
9089
"@types/sinon": "^7.0.13",
9190
"c8": "^7.7.1",
9291
"cross-env": "^7.0.3",

packages/utils/telemetry-utils/api-report/telemetry-utils.api.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
55
```ts
66

7+
/// <reference types="node" />
8+
79
import { EventEmitter } from 'events';
810
import { EventEmitterEventType } from '@fluid-internal/client-utils';
911
import { IDisposable } from '@fluidframework/core-interfaces';

pnpm-lock.yaml

+4-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)