diff options
author | mash-graz <mash-graz@users.noreply.github.com> | 2024-03-11 00:23:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-11 00:23:06 +0100 |
commit | 80dbcd3ddf589d20ae7786c716959c6d48ea1f1e (patch) | |
tree | 55967de66db8f4654539f462e6eea33385179aa3 /ext/node/polyfills/worker_threads.ts | |
parent | 16dbbfa64a5d2905580535c52c1db51d1cf5b89f (diff) |
fix(ext/node) implement receiveMessageOnPort for node:worker_threads (#22766)
Implementation of `receiveMessageOnPort` for `node:worker_threads`
Fixes: #22702
Diffstat (limited to 'ext/node/polyfills/worker_threads.ts')
-rw-r--r-- | ext/node/polyfills/worker_threads.ts | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/ext/node/polyfills/worker_threads.ts b/ext/node/polyfills/worker_threads.ts index fe3425972..74abf5bb5 100644 --- a/ext/node/polyfills/worker_threads.ts +++ b/ext/node/polyfills/worker_threads.ts @@ -8,21 +8,25 @@ import { op_host_recv_ctrl, op_host_recv_message, op_host_terminate_worker, + op_message_port_recv_message_sync, op_require_read_closest_package_json, } from "ext:core/ops"; -import { BroadcastChannel } from "ext:deno_broadcast_channel/01_broadcast_channel.js"; import { deserializeJsMessageData, MessageChannel, MessagePort, + MessagePortIdSymbol, + MessagePortPrototype, serializeJsMessageData, } from "ext:deno_web/13_message_port.js"; import * as webidl from "ext:deno_webidl/00_webidl.js"; import { log } from "ext:runtime/06_util.js"; import { notImplemented } from "ext:deno_node/_utils.ts"; import { EventEmitter, once } from "node:events"; +import { BroadcastChannel } from "ext:deno_broadcast_channel/01_broadcast_channel.js"; import { isAbsolute, resolve } from "node:path"; +const { ObjectPrototypeIsPrototypeOf } = primordials; const { Error, Symbol, @@ -496,9 +500,24 @@ export function markAsUntransferable() { export function moveMessagePortToContext() { notImplemented("moveMessagePortToContext"); } -export function receiveMessageOnPort() { - notImplemented("receiveMessageOnPort"); + +/** + * @param { MessagePort } port + * @returns {object | undefined} + */ +export function receiveMessageOnPort(port: MessagePort): object | undefined { + if (!(ObjectPrototypeIsPrototypeOf(MessagePortPrototype, port))) { + const err = new TypeError( + 'The "port" argument must be a MessagePort instance', + ); + err["code"] = "ERR_INVALID_ARG_TYPE"; + throw err; + } + const data = op_message_port_recv_message_sync(port[MessagePortIdSymbol]); + if (data === null) return undefined; + return { message: deserializeJsMessageData(data)[0] }; } + export { BroadcastChannel, MessageChannel, |