From 686ec85f5220327933d1dfae0e73dabbfc33bc45 Mon Sep 17 00:00:00 2001 From: await-ovo <13152410380@163.com> Date: Wed, 5 Jul 2023 01:19:18 +0800 Subject: [PATCH] fix(ext/node): Define performance.timeOrigin as getter property (#19714) --- cli/tests/unit_node/perf_hooks_test.ts | 16 +++++++++++++++- ext/node/polyfills/perf_hooks.ts | 6 ++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/cli/tests/unit_node/perf_hooks_test.ts b/cli/tests/unit_node/perf_hooks_test.ts index 78375d54dc..2249e62f82 100644 --- a/cli/tests/unit_node/perf_hooks_test.ts +++ b/cli/tests/unit_node/perf_hooks_test.ts @@ -1,7 +1,10 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. import * as perfHooks from "node:perf_hooks"; import { performance } from "node:perf_hooks"; -import { assertEquals } from "../../../test_util/std/testing/asserts.ts"; +import { + assertEquals, + assertThrows, +} from "../../../test_util/std/testing/asserts.ts"; Deno.test({ name: "[perf_hooks] performance", @@ -39,3 +42,14 @@ Deno.test({ assertEquals(perfHooks.PerformanceEntry, PerformanceEntry); }, }); + +Deno.test({ + name: "[perf_hooks] performance.timeOrigin", + fn() { + assertEquals(typeof performance.timeOrigin, "number"); + assertThrows(() => { + // @ts-expect-error: Cannot assign to 'timeOrigin' because it is a read-only property + performance.timeOrigin = 1; + }); + }, +}); diff --git a/ext/node/polyfills/perf_hooks.ts b/ext/node/polyfills/perf_hooks.ts index 059677c9bc..64c2c3e062 100644 --- a/ext/node/polyfills/perf_hooks.ts +++ b/ext/node/polyfills/perf_hooks.ts @@ -54,8 +54,10 @@ const performance: nodeTiming: {}, now: () => shimPerformance.now(), timerify: () => notImplemented("timerify from performance"), - // deno-lint-ignore no-explicit-any - timeOrigin: (shimPerformance as any).timeOrigin, + get timeOrigin() { + // deno-lint-ignore no-explicit-any + return (shimPerformance as any).timeOrigin; + }, markResourceTiming: () => {}, // @ts-ignore waiting on update in `deno`, but currently this is // a circular dependency