diff --git a/types/node/test/worker_threads.ts b/types/node/test/worker_threads.ts index 35a5fe5ef017919..38c7648f246e01f 100644 --- a/types/node/test/worker_threads.ts +++ b/types/node/test/worker_threads.ts @@ -240,3 +240,11 @@ import { createContext } from "node:vm"; const arrayBuffer = new ArrayBuffer(0); structuredClone({ test: arrayBuffer }, { transfer: [arrayBuffer] }); // $ExpectType { test: ArrayBuffer; } } + +{ + const { port1 } = new workerThreads.MessageChannel(); + workerThreads.postMessageToThread(10, { port: port1 }, [port1], 1000); + workerThreads.postMessageToThread(10, { port: port1 }, [port1]); + workerThreads.postMessageToThread(10, { x: 100 }, 1000); + workerThreads.postMessageToThread(10, { x: 100 }); +} diff --git a/types/node/v20/test/worker_threads.ts b/types/node/v20/test/worker_threads.ts index 37948e1fba6067a..aeaac9f9e56293a 100644 --- a/types/node/v20/test/worker_threads.ts +++ b/types/node/v20/test/worker_threads.ts @@ -223,3 +223,11 @@ import { createContext } from "node:vm"; const arrayBuffer = new ArrayBuffer(0); structuredClone({ test: arrayBuffer }, { transfer: [arrayBuffer] }); // $ExpectType { test: ArrayBuffer; } } + +{ + const { port1 } = new workerThreads.MessageChannel(); + workerThreads.postMessageToThread(10, { port: port1 }, [port1], 1000); + workerThreads.postMessageToThread(10, { port: port1 }, [port1]); + workerThreads.postMessageToThread(10, { x: 100 }, 1000); + workerThreads.postMessageToThread(10, { x: 100 }); +} diff --git a/types/node/v20/worker_threads.d.ts b/types/node/v20/worker_threads.d.ts index dfe7b3431d1a252..89603a84c9166b9 100644 --- a/types/node/v20/worker_threads.d.ts +++ b/types/node/v20/worker_threads.d.ts @@ -676,6 +676,24 @@ declare module "worker_threads" { * for the `key` will be deleted. */ function setEnvironmentData(key: Serializable, value?: Serializable): void; + /** + * Sends a value to another worker, identified by its thread ID. + * @param threadId The target thread ID. If the thread ID is invalid, a `ERR_WORKER_MESSAGING_FAILED` error will be thrown. + * If the target thread ID is the current thread ID, a `ERR_WORKER_MESSAGING_SAME_THREAD` error will be thrown. + * @param value The value to send. + * @param transferList If one or more `MessagePort`-like objects are passed in value, a `transferList` is required for those items + * or `ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST` is thrown. See `port.postMessage()` for more information. + * @param timeout Time to wait for the message to be delivered in milliseconds. By default it's `undefined`, which means wait forever. + * If the operation times out, a `ERR_WORKER_MESSAGING_TIMEOUT` error is thrown. + * @since v22.5.0 + */ + function postMessageToThread(threadId: number, value: any, timeout?: number): Promise; + function postMessageToThread( + threadId: number, + value: any, + transferList: readonly Transferable[], + timeout?: number, + ): Promise; import { BroadcastChannel as _BroadcastChannel, diff --git a/types/node/v22/test/worker_threads.ts b/types/node/v22/test/worker_threads.ts index 35a5fe5ef017919..38c7648f246e01f 100644 --- a/types/node/v22/test/worker_threads.ts +++ b/types/node/v22/test/worker_threads.ts @@ -240,3 +240,11 @@ import { createContext } from "node:vm"; const arrayBuffer = new ArrayBuffer(0); structuredClone({ test: arrayBuffer }, { transfer: [arrayBuffer] }); // $ExpectType { test: ArrayBuffer; } } + +{ + const { port1 } = new workerThreads.MessageChannel(); + workerThreads.postMessageToThread(10, { port: port1 }, [port1], 1000); + workerThreads.postMessageToThread(10, { port: port1 }, [port1]); + workerThreads.postMessageToThread(10, { x: 100 }, 1000); + workerThreads.postMessageToThread(10, { x: 100 }); +} diff --git a/types/node/v22/worker_threads.d.ts b/types/node/v22/worker_threads.d.ts index 9cb2923ee6c4462..d48fbabf4d3c53e 100644 --- a/types/node/v22/worker_threads.d.ts +++ b/types/node/v22/worker_threads.d.ts @@ -738,6 +738,24 @@ declare module "worker_threads" { * for the `key` will be deleted. */ function setEnvironmentData(key: Serializable, value?: Serializable): void; + /** + * Sends a value to another worker, identified by its thread ID. + * @param threadId The target thread ID. If the thread ID is invalid, a `ERR_WORKER_MESSAGING_FAILED` error will be thrown. + * If the target thread ID is the current thread ID, a `ERR_WORKER_MESSAGING_SAME_THREAD` error will be thrown. + * @param value The value to send. + * @param transferList If one or more `MessagePort`-like objects are passed in value, a `transferList` is required for those items + * or `ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST` is thrown. See `port.postMessage()` for more information. + * @param timeout Time to wait for the message to be delivered in milliseconds. By default it's `undefined`, which means wait forever. + * If the operation times out, a `ERR_WORKER_MESSAGING_TIMEOUT` error is thrown. + * @since v22.5.0 + */ + function postMessageToThread(threadId: number, value: any, timeout?: number): Promise; + function postMessageToThread( + threadId: number, + value: any, + transferList: readonly Transferable[], + timeout?: number, + ): Promise; import { BroadcastChannel as _BroadcastChannel, diff --git a/types/node/worker_threads.d.ts b/types/node/worker_threads.d.ts index e14d367f1f9bba8..a31f7846178842b 100644 --- a/types/node/worker_threads.d.ts +++ b/types/node/worker_threads.d.ts @@ -739,6 +739,24 @@ declare module "worker_threads" { * for the `key` will be deleted. */ function setEnvironmentData(key: Serializable, value?: Serializable): void; + /** + * Sends a value to another worker, identified by its thread ID. + * @param threadId The target thread ID. If the thread ID is invalid, a `ERR_WORKER_MESSAGING_FAILED` error will be thrown. + * If the target thread ID is the current thread ID, a `ERR_WORKER_MESSAGING_SAME_THREAD` error will be thrown. + * @param value The value to send. + * @param transferList If one or more `MessagePort`-like objects are passed in value, a `transferList` is required for those items + * or `ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST` is thrown. See `port.postMessage()` for more information. + * @param timeout Time to wait for the message to be delivered in milliseconds. By default it's `undefined`, which means wait forever. + * If the operation times out, a `ERR_WORKER_MESSAGING_TIMEOUT` error is thrown. + * @since v22.5.0 + */ + function postMessageToThread(threadId: number, value: any, timeout?: number): Promise; + function postMessageToThread( + threadId: number, + value: any, + transferList: readonly Transferable[], + timeout?: number, + ): Promise; import { BroadcastChannel as _BroadcastChannel,