From 78fbc3f3f695610083d07a55ef2a116e0bf04a10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 16 May 2023 05:16:24 +0200 Subject: [PATCH] fix(npm): add performance.markResourceTiming sham (#19123) This commit shams "performance.markResourceTiming" API by using a noop function. It is done to provide compatibility with "npm:undici" package. We should look into actually implementing this API properly, but I wanted to unblock support for "undici" and "astro" for now. Ref https://github.com/denoland/deno/issues/19065 --- ext/node/analyze.rs | 3 ++- ext/node/polyfills/01_require.js | 2 +- ext/node/polyfills/02_init.js | 1 + ext/node/polyfills/perf_hooks.ts | 3 +++ 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/ext/node/analyze.rs b/ext/node/analyze.rs index 6d32c68beb..2e5c2d15f5 100644 --- a/ext/node/analyze.rs +++ b/ext/node/analyze.rs @@ -32,6 +32,7 @@ static NODE_GLOBALS: &[&str] = &[ "setImmediate", "setInterval", "setTimeout", + "performance", ]; #[derive(Debug, Clone)] @@ -528,7 +529,7 @@ mod tests { "var clearTimeout = globalThis.clearTimeout;var console = globalThis.console;", "var global = globalThis.global;var process = globalThis.process;", "var setImmediate = globalThis.setImmediate;var setInterval = globalThis.setInterval;", - "var setTimeout = globalThis.setTimeout;\n", + "var setTimeout = globalThis.setTimeout;var performance = globalThis.performance;\n", "export const x = 1;" ), NODE_GLOBAL_THIS_NAME, diff --git a/ext/node/polyfills/01_require.js b/ext/node/polyfills/01_require.js index a8a70c2fca..7b91d12aa0 100644 --- a/ext/node/polyfills/01_require.js +++ b/ext/node/polyfills/01_require.js @@ -907,7 +907,7 @@ Module.prototype.require = function (id) { Module.wrapper = [ // We provide the non-standard APIs in the CommonJS wrapper // to avoid exposing them in global namespace. - "(function (exports, require, module, __filename, __dirname, globalThis) { const { Buffer, clearImmediate, clearInterval, clearTimeout, console, global, process, setImmediate, setInterval, setTimeout} = globalThis; var window = undefined; (function () {", + "(function (exports, require, module, __filename, __dirname, globalThis) { const { Buffer, clearImmediate, clearInterval, clearTimeout, console, global, process, setImmediate, setInterval, setTimeout, performance} = globalThis; var window = undefined; (function () {", "\n}).call(this); })", ]; Module.wrap = function (script) { diff --git a/ext/node/polyfills/02_init.js b/ext/node/polyfills/02_init.js index b8070d50f7..3aef000601 100644 --- a/ext/node/polyfills/02_init.js +++ b/ext/node/polyfills/02_init.js @@ -34,6 +34,7 @@ function initialize( nodeGlobals.setImmediate = nativeModuleExports["timers"].setImmediate; nodeGlobals.setInterval = nativeModuleExports["timers"].setInterval; nodeGlobals.setTimeout = nativeModuleExports["timers"].setTimeout; + nodeGlobals.performance = nativeModuleExports["perf_hooks"].performance; // add a hidden global for the esm code to use in order to reliably // get node's globalThis diff --git a/ext/node/polyfills/perf_hooks.ts b/ext/node/polyfills/perf_hooks.ts index ac74c10f81..30c50d3637 100644 --- a/ext/node/polyfills/perf_hooks.ts +++ b/ext/node/polyfills/perf_hooks.ts @@ -22,6 +22,8 @@ const performance: timerify: any; // deno-lint-ignore no-explicit-any timeOrigin: any; + // deno-lint-ignore no-explicit-any + markResourceTiming: any; } = { clearMarks: (markName: string) => shimPerformance.clearMarks(markName), eventLoopUtilization: () => @@ -50,6 +52,7 @@ const performance: timerify: () => notImplemented("timerify from performance"), // deno-lint-ignore no-explicit-any timeOrigin: (shimPerformance as any).timeOrigin, + markResourceTiming: () => {}, // @ts-ignore waiting on update in `deno`, but currently this is // a circular dependency toJSON: () => shimPerformance.toJSON(),