| 76 | const UIDS = Symbol("uids"); |
| 77 | |
| 78 | export function setupPrimary() { |
| 79 | cluster.on("message", (worker, message) => { |
| 80 | const isValidSource = message?.source === MESSAGE_SOURCE; |
| 81 | if (!isValidSource) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | // store the requester's uids (one per namespace) so that the response can be sent specifically to them |
| 86 | worker[UIDS] = worker[UIDS] || new Set(); |
| 87 | worker[UIDS].add(message.uid); |
| 88 | |
| 89 | switch (message.type) { |
| 90 | case MessageType.FETCH_SOCKETS_RESPONSE: |
| 91 | case MessageType.SERVER_SIDE_EMIT_RESPONSE: |
| 92 | const requesterUid = message.requesterUid; |
| 93 | for (const workerId in cluster.workers) { |
| 94 | if ( |
| 95 | hasOwnProperty.call(cluster.workers, workerId) && |
| 96 | cluster.workers[workerId][UIDS]?.has(requesterUid) |
| 97 | ) { |
| 98 | cluster.workers[workerId].send(message, null, ignoreError); |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | break; |
| 103 | default: |
| 104 | const emitterIdAsString = String(worker.id); |
| 105 | // emit to all workers but the requester |
| 106 | for (const workerId in cluster.workers) { |
| 107 | if ( |
| 108 | hasOwnProperty.call(cluster.workers, workerId) && |
| 109 | workerId !== emitterIdAsString |
| 110 | ) { |
| 111 | cluster.workers[workerId].send(message, null, ignoreError); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | }); |
| 116 | } |