Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pollyfill MessageEvent since Node doesn't initialize properties per the spec. #682

Closed
kalvenschraut opened this issue Dec 20, 2022 · 11 comments · Fixed by #765
Closed

Pollyfill MessageEvent since Node doesn't initialize properties per the spec. #682

kalvenschraut opened this issue Dec 20, 2022 · 11 comments · Fixed by #765
Assignees
Labels
enhancement New feature or request

Comments

@kalvenschraut
Copy link
Contributor

See my issue over on vitest

I could do a PR if all that would be done is what I did in its description.

@kalvenschraut kalvenschraut added the enhancement New feature or request label Dec 20, 2022
@capricorn86
Copy link
Owner

capricorn86 commented Dec 20, 2022

Thanks for reporting @kalvenschraut! 🙂

MessageEvent has never been implemented.

You are very welcome to implement it. All contributions are welcome.

The event classes are here (you should find many example of how they are implemented by looking at other events):
https://github.com/capricorn86/happy-dom/tree/master/packages/happy-dom/src/event/events

@kalvenschraut
Copy link
Contributor Author

kalvenschraut commented Dec 20, 2022

Can I extend the global MessageEvent and add the missing functionality? Node doesn't do anything with the second parameter like it is suppose to per the spec. Can see what I am doing currently locally from the vitest issue.

@capricorn86
Copy link
Owner

@kalvenschraut the events in the browsers are expected to inherit from the Event, and it is not possible to extend two classes. Also, I believe they would have different purposes as the MessageEvent is meant for very browser specific functionality.

Do you have any documentation for the Node.js MessageEvent? Because I can't find it.

@kalvenschraut
Copy link
Contributor Author

As far as I understood it, node is attempting to make MessageEvent available to behave like browser contexts, but it is missing the functionality that vitest is trying to use in their web worker package.

https://nodejs.org/api/globals.html#messageevent is the only documentation I can find.

Vlad at vitest talked that they could look at adding this themselves here

@capricorn86
Copy link
Owner

I think it is safest to implement this in Happy DOM without extending the NodeJS class to ensure that it will be compatible with the rest of the DOM functionality.

Example:

import Event from '../Event';
import IMessageEventInit from './IMessageEventInit';

export default class MessageEvent extends Event {
	public readonly data: string | null = null;
	...

	/**
	 * Constructor.
	 *
	 * @param type Event type.
	 * @param [eventInit] Event init.
	 */
	constructor(type: string, eventInit?: IMessageEventInit) {
		super(type, eventInit);
		this.data = eventInit?.data || null;

               ...
	}
}

@capricorn86
Copy link
Owner

@kalvenschraut
Copy link
Contributor Author

Apparently I was mistaken and Node's MessageEvent does work just fine as vlad reported and I just double checked.

image

The linked vitest issue only occurs with happy dom. No idea how happy-dom could be affecting this issue though.

@capricorn86
Copy link
Owner

It's probably because it has not been implemented in Happy DOM yet.

@kalvenschraut
Copy link
Contributor Author

kalvenschraut commented Dec 23, 2022

I guess I don't understand, if it hasn't been implemented in happy-dom then does it not fall back to Node's implementation?

node stackblitz

happy-dom stackblitz

The only difference between those two tests is the environment, one only has Node and other has happy-dom + Node.

Found Node's implementation here
Apparently Node is using symbols and then it defines a getter called data to get this symbol... interesting.

@kalvenschraut
Copy link
Contributor Author

kalvenschraut commented Dec 23, 2022

After looking into node's implementation a little bit more, they are defining those getters on the prototype specifically. Would happy dom be doing anything to overwrite the prototype to an empty object?

In node REPL
image

Then console.log'ing the prototype in happy-dom + vitest
image
image

@capricorn86
Copy link
Owner

@kalvenschraut I have made a fix which adds support for HTMLIframeElement, MessageEvent and Window.postMessage(). This should hopefully solve your issue 🙂

I have also added the logo for RTVision 🥳

You can read more about the release here:
#682

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants