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

worker_threads: add brand checks for detached properties/methods #39763

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion lib/internal/worker/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const { inspect } = require('internal/util/inspect');
const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_THIS,
ERR_MISSING_ARGS,
}
} = require('internal/errors');
Expand All @@ -74,6 +75,7 @@ const kStartedReading = Symbol('kStartedReading');
const kStdioWantsMoreDataCallback = Symbol('kStdioWantsMoreDataCallback');
const kCurrentlyReceivingPorts =
SymbolFor('nodejs.internal.kCurrentlyReceivingPorts');
const kType = Symbol('kType');

const messageTypes = {
UP_AND_RUNNING: 'upAndRunning',
Expand Down Expand Up @@ -349,11 +351,16 @@ function onMessageEvent(type, data) {
this.dispatchEvent(new MessageEvent(type, { data }));
}

function isBroadcastChannel(value) {
return value?.[kType] === 'BroadcastChannel';
}
jasnell marked this conversation as resolved.
Show resolved Hide resolved

class BroadcastChannel extends EventTarget {
constructor(name) {
if (arguments.length === 0)
throw new ERR_MISSING_ARGS('name');
super();
this[kType] = 'BroadcastChannel';
this[kName] = `${name}`;
this[kHandle] = broadcastChannel(this[kName]);
this[kOnMessage] = FunctionPrototypeBind(onMessageEvent, this, 'message');
Expand All @@ -364,6 +371,8 @@ class BroadcastChannel extends EventTarget {
}

[inspect.custom](depth, options) {
if (!isBroadcastChannel(this))
throw new ERR_INVALID_THIS('BroadcastChannel');
if (depth < 0)
return 'BroadcastChannel';

Expand All @@ -378,9 +387,15 @@ class BroadcastChannel extends EventTarget {
}, opts)}`;
}

get name() { return this[kName]; }
get name() {
if (!isBroadcastChannel(this))
throw new ERR_INVALID_THIS('BroadcastChannel');
return this[kName];
}

close() {
if (!isBroadcastChannel(this))
throw new ERR_INVALID_THIS('BroadcastChannel');
if (this[kHandle] === undefined)
return;
this[kHandle].off('message', this[kOnMessage]);
Expand All @@ -392,6 +407,8 @@ class BroadcastChannel extends EventTarget {
}

postMessage(message) {
if (!isBroadcastChannel(this))
throw new ERR_INVALID_THIS('BroadcastChannel');
if (arguments.length === 0)
throw new ERR_MISSING_ARGS('message');
if (this[kHandle] === undefined)
Expand All @@ -400,19 +417,40 @@ class BroadcastChannel extends EventTarget {
throw new DOMException('Message could not be posted.');
}

// TODO(@jasnell): The ref() method is Node.js specific and not part
// of the standard BroadcastChannel API definition. Typically we shouldn't
// extend Web Platform APIs with Node.js specific methods but ref and unref
// are a bit special. We may want to revisit this, however.
jasnell marked this conversation as resolved.
Show resolved Hide resolved
ref() {
if (!isBroadcastChannel(this))
throw new ERR_INVALID_THIS('BroadcastChannel');
if (this[kHandle])
this[kHandle].ref();
return this;
}

// TODO(@jasnell): The unref() method is Node.js specific and not part
// of the standard BroadcastChannel API definition. Typically we shouldn't
// extend Web Platform APIs with Node.js specific methods but ref and unref
// are a bit special. We may want to revisit this, however.
unref() {
if (!isBroadcastChannel(this))
throw new ERR_INVALID_THIS('BroadcastChannel');
if (this[kHandle])
this[kHandle].unref();
return this;
}
}

const kEnumerableProperty = ObjectCreate(null);
kEnumerableProperty.enumerable = true;

ObjectDefineProperties(BroadcastChannel.prototype, {
name: kEnumerableProperty,
close: kEnumerableProperty,
postMessage: kEnumerableProperty,
});

defineEventHandler(BroadcastChannel.prototype, 'message');
defineEventHandler(BroadcastChannel.prototype, 'messageerror');

Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-worker-broadcastchannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,20 @@ assert.throws(() => new BroadcastChannel(), {
bc1.close();
bc2.close();
}

{
assert.throws(() => Reflect.get(BroadcastChannel.prototype, 'name', {}), {
code: 'ERR_INVALID_THIS',
});

[
'close',
'postMessage',
'ref',
'unref',
].forEach((i) => {
assert.throws(() => Reflect.apply(BroadcastChannel.prototype[i], [], {}), {
code: 'ERR_INVALID_THIS',
});
});
}