unjs / unenv

๐Ÿ•Š๏ธ Convert javaScript code to be runtime agnostic
MIT License
342 stars 18 forks source link

feat: polyfill `node:perf_hooks` module and performance polyfill for nodeless #209

Closed jculvey closed 1 month ago

jculvey commented 1 month ago

๐Ÿ”— Linked issue

โ“ Type of change

๐Ÿ“š Description

Adds a polyfill for the node:perf_hooks module.

Replaces the current auto-mocking of 'perf_hooks' to support destructured ESM imports and allow for functional polyfill coverage in the future.

๐Ÿ“ Checklist

IgorMinar commented 1 month ago

Hmm... digging into this a bit more. I just remembered that we implemented bits and pieces of this in workerd in the past:

So it would be good if the polyfill could try to preserve the native globalThis.performance if it already exists in the runtime and only monkey patch the native object with methods and properties if the object doesn't have these APIs already defined. In other words, could we preserve workerd's performance.now method, and patch onto this object other methods like mark if they are not already defined?

@jsnell - this is an interesting one, and very similar to globalThis.process where we would like to keep the original native implementation if present, and fill in any missing APIs with mocks and thus creating a hybrid polyfill at an object instance level.

Ideally we build these unenv polyfills in a way where if/when we add more API surface to workerd's process or performance APIs, the polyfill will simply fill in less and less of the surface. All of this should be dynamic and guided via feature detection in unenv polyfills.

pi0 commented 1 month ago

Thanks @jculvey and @IgorMinar again.

I've rebased this PR with partial web implementation of performances API, and extending the Node.js additions (some are mainly for type mismatches..). See #213 and #214.