Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions packages/utils/src/perf_hooks.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type {

Check warning on line 1 in packages/utils/src/perf_hooks.d.ts

View workflow job for this annotation

GitHub Actions / Standalone mode

<✓> ESLint | Enforce a case style for filenames.

Filename is not in kebab case. Rename it to `perf-hooks.d.ts`.
MarkerPayload,
TrackEntryPayload,
WithDevToolsPayload,
} from './lib/user-timing-extensibility-api.type';

export {};

type DetailPayloadWithDevtools = WithDevToolsPayload<
TrackEntryPayload | MarkerPayload
>;

declare module 'node:perf_hooks' {
export interface PerformanceMarkOptions {
detail?: DetailPayloadWithDevtools;
startTime?: DOMHighResTimeStamp;
}

export interface PerformanceMeasureOptions {
detail?: DetailPayloadWithDevtools;
start?: string | number;
end?: string | number;
duration?: number;
}

const performance: {
mark(
name: string,
options?: {
detail?: DetailPayloadWithDevtools;
},
): PerformanceMark;

Check failure on line 32 in packages/utils/src/perf_hooks.d.ts

View workflow job for this annotation

GitHub Actions / Standalone mode

<✓> ESLint | disallow unsupported Node.js built-in APIs on the specified version

The 'PerformanceMark' is still an experimental feature and is not supported until Node.js 19.0.0. The configured version range is '>=17.0.0'.

Check warning on line 32 in packages/utils/src/perf_hooks.d.ts

View workflow job for this annotation

GitHub Actions / Standalone mode

<✓> ESLint | Prefer property signatures over method signatures.

Use a property signature instead of a method signature

Check warning on line 32 in packages/utils/src/perf_hooks.d.ts

View workflow job for this annotation

GitHub Actions / Standalone mode

<✓> ESLint | Enforce using a particular method signature syntax

Shorthand method signature is forbidden. Use a function property instead.

measure(
name: string,
startOrOptions?:
| string
| number
| {
detail?: DetailPayloadWithDevtools;
start?: string | number;
end?: string | number;
duration?: number;
},
end?: string | number,
): PerformanceMeasure;

Check failure on line 46 in packages/utils/src/perf_hooks.d.ts

View workflow job for this annotation

GitHub Actions / Standalone mode

<✓> ESLint | disallow unsupported Node.js built-in APIs on the specified version

The 'PerformanceMeasure' is still an experimental feature and is not supported until Node.js 19.0.0. The configured version range is '>=17.0.0'.

Check warning on line 46 in packages/utils/src/perf_hooks.d.ts

View workflow job for this annotation

GitHub Actions / Standalone mode

<✓> ESLint | Prefer property signatures over method signatures.

Use a property signature instead of a method signature

Check warning on line 46 in packages/utils/src/perf_hooks.d.ts

View workflow job for this annotation

GitHub Actions / Standalone mode

<✓> ESLint | Enforce using a particular method signature syntax

Shorthand method signature is forbidden. Use a function property instead.
};
}
76 changes: 76 additions & 0 deletions packages/utils/src/perf_hooks.type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { type PerformanceMarkOptions, performance } from 'node:perf_hooks';

Check warning on line 1 in packages/utils/src/perf_hooks.type.test.ts

View workflow job for this annotation

GitHub Actions / Standalone mode

<✓> ESLint | Enforce a case style for filenames.

Filename is not in kebab case. Rename it to `perf-hooks.type.test.ts`.
import { describe, expectTypeOf, it } from 'vitest';

describe('perf_hooks.type', () => {
it('PerformanceMarkOptions should be type safe', () => {
expectTypeOf<{
startTime: number;
detail: {
devtools: {
dataType: 'marker';
color: 'error';
};
};
}>().toMatchTypeOf<PerformanceMarkOptions>();

expectTypeOf<{
startTime: number;
detail: {
devtools: {
dataType: 'markerr';
};
};
}>().not.toMatchTypeOf<PerformanceMarkOptions>();
});

it('perf_hooks.mark should be type safe', () => {
performance.mark('name', {
detail: {
devtools: {
dataType: 'marker',
color: 'error',
},
},
});

performance.mark('name', {
detail: {
devtools: {
/* @ts-expect-error - dataType should be marker | track */
dataType: 'markerrr',
color: 'error',
},
},
});
});

it('PerformanceMeasureOptions should be type safe', () => {
expectTypeOf<{
start: string;
end: string;
detail: {
devtools: {
dataType: 'track-entry';
track: 'test-track';
color: 'primary';
};
};
}>().toMatchTypeOf<PerformanceMeasureOptions>();
});

it('perf_hooks.measure should be type safe', () => {
performance.measure('measure-name', 'start-mark', 'end-mark');

performance.measure('measure-name', {
start: 'start-mark',
end: 'end-mark',
detail: {
/* @ts-expect-error - track is required */
devtools: {
dataType: 'track-entry',
color: 'primary',
},
},
});
});
});
2 changes: 1 addition & 1 deletion packages/utils/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"declaration": true,
"types": ["node"]
},
"include": ["src/**/*.ts"],
"include": ["src/**/*.{ts,d.ts}"],
"exclude": [
"vitest.unit.config.ts",
"vitest.int.config.ts",
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"src/**/*.test.js",
"src/**/*.test.jsx",
"src/**/*.d.ts",
"../../testing/test-setup/src/vitest.d.ts"
"../../testing/test-setup/src/vitest.d.ts",
"src/perf_hooks.type.ts"
]
}
Loading