( message: unknown, parentProcess = process, )
| 12 | import {packMessage} from './safeMessageTransferring'; |
| 13 | |
| 14 | export default function messageParent( |
| 15 | message: unknown, |
| 16 | parentProcess = process, |
| 17 | ): void { |
| 18 | if (!isMainThread && parentPort != null) { |
| 19 | try { |
| 20 | parentPort.postMessage([PARENT_MESSAGE_CUSTOM, message]); |
| 21 | } catch (error) { |
| 22 | // Try to handle https://html.spec.whatwg.org/multipage/structured-data.html#structuredserializeinternal |
| 23 | // for `symbols` and `functions` |
| 24 | if (isDataCloneError(error)) { |
| 25 | parentPort.postMessage([PARENT_MESSAGE_CUSTOM, packMessage(message)]); |
| 26 | } else { |
| 27 | throw error; |
| 28 | } |
| 29 | } |
| 30 | } else if (typeof parentProcess.send === 'function') { |
| 31 | try { |
| 32 | parentProcess.send([PARENT_MESSAGE_CUSTOM, message]); |
| 33 | } catch (error) { |
| 34 | if ( |
| 35 | isError(error) && |
| 36 | // if .send is a function, it's a serialization issue |
| 37 | !error.message.includes('.send is not a function') |
| 38 | ) { |
| 39 | // Apply specific serialization only in error cases |
| 40 | // to avoid affecting performance in regular cases. |
| 41 | parentProcess.send([PARENT_MESSAGE_CUSTOM, packMessage(message)]); |
| 42 | } else { |
| 43 | throw error; |
| 44 | } |
| 45 | } |
| 46 | } else { |
| 47 | throw new TypeError('"messageParent" can only be used inside a worker'); |
| 48 | } |
| 49 | } |
no test coverage detected