@@ -53,6 +53,7 @@ const { inspect } = require('internal/util/inspect');
53
53
const {
54
54
codes : {
55
55
ERR_INVALID_ARG_TYPE ,
56
+ ERR_INVALID_THIS ,
56
57
ERR_MISSING_ARGS ,
57
58
}
58
59
} = require ( 'internal/errors' ) ;
@@ -74,6 +75,7 @@ const kStartedReading = Symbol('kStartedReading');
74
75
const kStdioWantsMoreDataCallback = Symbol ( 'kStdioWantsMoreDataCallback' ) ;
75
76
const kCurrentlyReceivingPorts =
76
77
SymbolFor ( 'nodejs.internal.kCurrentlyReceivingPorts' ) ;
78
+ const kType = Symbol ( 'kType' ) ;
77
79
78
80
const messageTypes = {
79
81
UP_AND_RUNNING : 'upAndRunning' ,
@@ -349,11 +351,16 @@ function onMessageEvent(type, data) {
349
351
this . dispatchEvent ( new MessageEvent ( type , { data } ) ) ;
350
352
}
351
353
354
+ function isBroadcastChannel ( value ) {
355
+ return value ?. [ kType ] === 'BroadcastChannel' ;
356
+ }
357
+
352
358
class BroadcastChannel extends EventTarget {
353
359
constructor ( name ) {
354
360
if ( arguments . length === 0 )
355
361
throw new ERR_MISSING_ARGS ( 'name' ) ;
356
362
super ( ) ;
363
+ this [ kType ] = 'BroadcastChannel' ;
357
364
this [ kName ] = `${ name } ` ;
358
365
this [ kHandle ] = broadcastChannel ( this [ kName ] ) ;
359
366
this [ kOnMessage ] = FunctionPrototypeBind ( onMessageEvent , this , 'message' ) ;
@@ -364,6 +371,8 @@ class BroadcastChannel extends EventTarget {
364
371
}
365
372
366
373
[ inspect . custom ] ( depth , options ) {
374
+ if ( ! isBroadcastChannel ( this ) )
375
+ throw new ERR_INVALID_THIS ( 'BroadcastChannel' ) ;
367
376
if ( depth < 0 )
368
377
return 'BroadcastChannel' ;
369
378
@@ -378,9 +387,15 @@ class BroadcastChannel extends EventTarget {
378
387
} , opts ) } `;
379
388
}
380
389
381
- get name ( ) { return this [ kName ] ; }
390
+ get name ( ) {
391
+ if ( ! isBroadcastChannel ( this ) )
392
+ throw new ERR_INVALID_THIS ( 'BroadcastChannel' ) ;
393
+ return this [ kName ] ;
394
+ }
382
395
383
396
close ( ) {
397
+ if ( ! isBroadcastChannel ( this ) )
398
+ throw new ERR_INVALID_THIS ( 'BroadcastChannel' ) ;
384
399
if ( this [ kHandle ] === undefined )
385
400
return ;
386
401
this [ kHandle ] . off ( 'message' , this [ kOnMessage ] ) ;
@@ -392,6 +407,8 @@ class BroadcastChannel extends EventTarget {
392
407
}
393
408
394
409
postMessage ( message ) {
410
+ if ( ! isBroadcastChannel ( this ) )
411
+ throw new ERR_INVALID_THIS ( 'BroadcastChannel' ) ;
395
412
if ( arguments . length === 0 )
396
413
throw new ERR_MISSING_ARGS ( 'message' ) ;
397
414
if ( this [ kHandle ] === undefined )
@@ -400,19 +417,40 @@ class BroadcastChannel extends EventTarget {
400
417
throw new DOMException ( 'Message could not be posted.' ) ;
401
418
}
402
419
420
+ // The ref() method is Node.js specific and not part of the standard
421
+ // BroadcastChannel API definition. Typically we shouldn't extend Web
422
+ // Platform APIs with Node.js specific methods but ref and unref
423
+ // are a bit special.
403
424
ref ( ) {
425
+ if ( ! isBroadcastChannel ( this ) )
426
+ throw new ERR_INVALID_THIS ( 'BroadcastChannel' ) ;
404
427
if ( this [ kHandle ] )
405
428
this [ kHandle ] . ref ( ) ;
406
429
return this ;
407
430
}
408
431
432
+ // The unref() method is Node.js specific and not part of the standard
433
+ // BroadcastChannel API definition. Typically we shouldn't extend Web
434
+ // Platform APIs with Node.js specific methods but ref and unref
435
+ // are a bit special.
409
436
unref ( ) {
437
+ if ( ! isBroadcastChannel ( this ) )
438
+ throw new ERR_INVALID_THIS ( 'BroadcastChannel' ) ;
410
439
if ( this [ kHandle ] )
411
440
this [ kHandle ] . unref ( ) ;
412
441
return this ;
413
442
}
414
443
}
415
444
445
+ const kEnumerableProperty = ObjectCreate ( null ) ;
446
+ kEnumerableProperty . enumerable = true ;
447
+
448
+ ObjectDefineProperties ( BroadcastChannel . prototype , {
449
+ name : kEnumerableProperty ,
450
+ close : kEnumerableProperty ,
451
+ postMessage : kEnumerableProperty ,
452
+ } ) ;
453
+
416
454
defineEventHandler ( BroadcastChannel . prototype , 'message' ) ;
417
455
defineEventHandler ( BroadcastChannel . prototype , 'messageerror' ) ;
418
456
0 commit comments