@@ -20,7 +20,6 @@ const {
20
20
} = primordials ;
21
21
22
22
const {
23
- kEmptyObject,
24
23
kEnumerableProperty,
25
24
setOwnProperty,
26
25
} = require ( 'internal/util' ) ;
@@ -38,7 +37,6 @@ const {
38
37
moveMessagePortToContext,
39
38
receiveMessageOnPort : receiveMessageOnPort_ ,
40
39
stopMessagePort,
41
- checkMessagePort,
42
40
DOMException,
43
41
} = internalBinding ( 'messaging' ) ;
44
42
const {
@@ -59,25 +57,19 @@ const {
59
57
const { inspect } = require ( 'internal/util/inspect' ) ;
60
58
const {
61
59
codes : {
62
- ERR_INVALID_ARG_TYPE ,
63
60
ERR_INVALID_THIS ,
64
61
ERR_MISSING_ARGS ,
65
62
} ,
66
63
} = require ( 'internal/errors' ) ;
67
64
68
- const kData = Symbol ( 'kData' ) ;
69
65
const kHandle = Symbol ( 'kHandle' ) ;
70
66
const kIncrementsPortRef = Symbol ( 'kIncrementsPortRef' ) ;
71
- const kLastEventId = Symbol ( 'kLastEventId' ) ;
72
67
const kName = Symbol ( 'kName' ) ;
73
- const kOrigin = Symbol ( 'kOrigin' ) ;
74
68
const kOnMessage = Symbol ( 'kOnMessage' ) ;
75
69
const kOnMessageError = Symbol ( 'kOnMessageError' ) ;
76
70
const kPort = Symbol ( 'kPort' ) ;
77
- const kPorts = Symbol ( 'kPorts' ) ;
78
71
const kWaitingStreams = Symbol ( 'kWaitingStreams' ) ;
79
72
const kWritableCallbacks = Symbol ( 'kWritableCallbacks' ) ;
80
- const kSource = Symbol ( 'kSource' ) ;
81
73
const kStartedReading = Symbol ( 'kStartedReading' ) ;
82
74
const kStdioWantsMoreDataCallback = Symbol ( 'kStdioWantsMoreDataCallback' ) ;
83
75
const kCurrentlyReceivingPorts =
@@ -93,6 +85,11 @@ const messageTypes = {
93
85
LOAD_SCRIPT : 'loadScript' ,
94
86
} ;
95
87
88
+ let messageEvent ;
89
+ function lazyMessageEvent ( ) {
90
+ return messageEvent ??= require ( 'internal/deps/undici/undici' ) . MessageEvent ;
91
+ }
92
+
96
93
// We have to mess with the MessagePort prototype a bit, so that a) we can make
97
94
// it inherit from NodeEventTarget, even though it is a C++ class, and b) we do
98
95
// not provide methods that are not present in the Browser and not documented
@@ -119,95 +116,6 @@ MessagePort.prototype.hasRef = function() {
119
116
return ! ! FunctionPrototypeCall ( MessagePortPrototype . hasRef , this ) ;
120
117
} ;
121
118
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
-
211
119
const originalCreateEvent = EventTarget . prototype [ kCreateEvent ] ;
212
120
ObjectDefineProperty (
213
121
MessagePort . prototype ,
@@ -220,7 +128,7 @@ ObjectDefineProperty(
220
128
}
221
129
const ports = this [ kCurrentlyReceivingPorts ] ;
222
130
this [ kCurrentlyReceivingPorts ] = undefined ;
223
- return new MessageEvent ( type , { data, ports } ) ;
131
+ return new ( lazyMessageEvent ( ) ) ( type , { data, ports } ) ;
224
132
} ,
225
133
configurable : false ,
226
134
writable : false ,
@@ -413,7 +321,7 @@ function receiveMessageOnPort(port) {
413
321
}
414
322
415
323
function onMessageEvent ( type , data ) {
416
- this . dispatchEvent ( new MessageEvent ( type , { data } ) ) ;
324
+ this . dispatchEvent ( new ( lazyMessageEvent ( ) ) ( type , { data } ) ) ;
417
325
}
418
326
419
327
function isBroadcastChannel ( value ) {
@@ -546,7 +454,6 @@ module.exports = {
546
454
moveMessagePortToContext,
547
455
MessagePort,
548
456
MessageChannel,
549
- MessageEvent,
550
457
receiveMessageOnPort,
551
458
setupPortReferencing,
552
459
ReadableWorkerStdio,
0 commit comments