@@ -20,7 +20,6 @@ const {
2020} = primordials ;
2121
2222const {
23- kEmptyObject,
2423 kEnumerableProperty,
2524 setOwnProperty,
2625} = require ( 'internal/util' ) ;
@@ -38,7 +37,6 @@ const {
3837 moveMessagePortToContext,
3938 receiveMessageOnPort : receiveMessageOnPort_ ,
4039 stopMessagePort,
41- checkMessagePort,
4240 DOMException,
4341} = internalBinding ( 'messaging' ) ;
4442const {
@@ -59,25 +57,19 @@ const {
5957const { inspect } = require ( 'internal/util/inspect' ) ;
6058const {
6159 codes : {
62- ERR_INVALID_ARG_TYPE ,
6360 ERR_INVALID_THIS ,
6461 ERR_MISSING_ARGS ,
6562 } ,
6663} = require ( 'internal/errors' ) ;
6764
68- const kData = Symbol ( 'kData' ) ;
6965const kHandle = Symbol ( 'kHandle' ) ;
7066const kIncrementsPortRef = Symbol ( 'kIncrementsPortRef' ) ;
71- const kLastEventId = Symbol ( 'kLastEventId' ) ;
7267const kName = Symbol ( 'kName' ) ;
73- const kOrigin = Symbol ( 'kOrigin' ) ;
7468const kOnMessage = Symbol ( 'kOnMessage' ) ;
7569const kOnMessageError = Symbol ( 'kOnMessageError' ) ;
7670const kPort = Symbol ( 'kPort' ) ;
77- const kPorts = Symbol ( 'kPorts' ) ;
7871const kWaitingStreams = Symbol ( 'kWaitingStreams' ) ;
7972const kWritableCallbacks = Symbol ( 'kWritableCallbacks' ) ;
80- const kSource = Symbol ( 'kSource' ) ;
8173const kStartedReading = Symbol ( 'kStartedReading' ) ;
8274const kStdioWantsMoreDataCallback = Symbol ( 'kStdioWantsMoreDataCallback' ) ;
8375const kCurrentlyReceivingPorts =
@@ -93,6 +85,11 @@ const messageTypes = {
9385 LOAD_SCRIPT : 'loadScript' ,
9486} ;
9587
88+ let messageEvent ;
89+ function lazyMessageEvent ( ) {
90+ return messageEvent ??= require ( 'internal/deps/undici/undici' ) . MessageEvent ;
91+ }
92+
9693// We have to mess with the MessagePort prototype a bit, so that a) we can make
9794// it inherit from NodeEventTarget, even though it is a C++ class, and b) we do
9895// not provide methods that are not present in the Browser and not documented
@@ -119,95 +116,6 @@ MessagePort.prototype.hasRef = function() {
119116 return ! ! FunctionPrototypeCall ( MessagePortPrototype . hasRef , this ) ;
120117} ;
121118
122- function validateMessagePort ( port , name ) {
123- if ( ! checkMessagePort ( port ) )
124- throw new ERR_INVALID_ARG_TYPE ( name , 'MessagePort' , port ) ;
125- }
126-
127- function isMessageEvent ( value ) {
128- return value != null && kData in value ;
129- }
130-
131- class MessageEvent extends Event {
132- constructor ( type , {
133- data = null ,
134- origin = '' ,
135- lastEventId = '' ,
136- source = null ,
137- ports = [ ] ,
138- } = kEmptyObject ) {
139- super ( type ) ;
140- this [ kData ] = data ;
141- this [ kOrigin ] = `${ origin } ` ;
142- this [ kLastEventId ] = `${ lastEventId } ` ;
143- this [ kSource ] = source ;
144- this [ kPorts ] = [ ...ports ] ;
145-
146- if ( this [ kSource ] !== null )
147- validateMessagePort ( this [ kSource ] , 'init.source' ) ;
148- for ( let i = 0 ; i < this [ kPorts ] . length ; i ++ )
149- validateMessagePort ( this [ kPorts ] [ i ] , `init.ports[${ i } ]` ) ;
150- }
151- }
152-
153- ObjectDefineProperties ( MessageEvent . prototype , {
154- data : {
155- __proto__ : null ,
156- get ( ) {
157- if ( ! isMessageEvent ( this ) )
158- throw new ERR_INVALID_THIS ( 'MessageEvent' ) ;
159- return this [ kData ] ;
160- } ,
161- enumerable : true ,
162- configurable : true ,
163- set : undefined ,
164- } ,
165- origin : {
166- __proto__ : null ,
167- get ( ) {
168- if ( ! isMessageEvent ( this ) )
169- throw new ERR_INVALID_THIS ( 'MessageEvent' ) ;
170- return this [ kOrigin ] ;
171- } ,
172- enumerable : true ,
173- configurable : true ,
174- set : undefined ,
175- } ,
176- lastEventId : {
177- __proto__ : null ,
178- get ( ) {
179- if ( ! isMessageEvent ( this ) )
180- throw new ERR_INVALID_THIS ( 'MessageEvent' ) ;
181- return this [ kLastEventId ] ;
182- } ,
183- enumerable : true ,
184- configurable : true ,
185- set : undefined ,
186- } ,
187- source : {
188- __proto__ : null ,
189- get ( ) {
190- if ( ! isMessageEvent ( this ) )
191- throw new ERR_INVALID_THIS ( 'MessageEvent' ) ;
192- return this [ kSource ] ;
193- } ,
194- enumerable : true ,
195- configurable : true ,
196- set : undefined ,
197- } ,
198- ports : {
199- __proto__ : null ,
200- get ( ) {
201- if ( ! isMessageEvent ( this ) )
202- throw new ERR_INVALID_THIS ( 'MessageEvent' ) ;
203- return this [ kPorts ] ;
204- } ,
205- enumerable : true ,
206- configurable : true ,
207- set : undefined ,
208- } ,
209- } ) ;
210-
211119const originalCreateEvent = EventTarget . prototype [ kCreateEvent ] ;
212120ObjectDefineProperty (
213121 MessagePort . prototype ,
@@ -220,7 +128,7 @@ ObjectDefineProperty(
220128 }
221129 const ports = this [ kCurrentlyReceivingPorts ] ;
222130 this [ kCurrentlyReceivingPorts ] = undefined ;
223- return new MessageEvent ( type , { data, ports } ) ;
131+ return new ( lazyMessageEvent ( ) ) ( type , { data, ports } ) ;
224132 } ,
225133 configurable : false ,
226134 writable : false ,
@@ -413,7 +321,7 @@ function receiveMessageOnPort(port) {
413321}
414322
415323function onMessageEvent ( type , data ) {
416- this . dispatchEvent ( new MessageEvent ( type , { data } ) ) ;
324+ this . dispatchEvent ( new ( lazyMessageEvent ( ) ) ( type , { data } ) ) ;
417325}
418326
419327function isBroadcastChannel ( value ) {
@@ -546,7 +454,6 @@ module.exports = {
546454 moveMessagePortToContext,
547455 MessagePort,
548456 MessageChannel,
549- MessageEvent,
550457 receiveMessageOnPort,
551458 setupPortReferencing,
552459 ReadableWorkerStdio,
0 commit comments