undici-thread-interceptor
Advanced tools
Comparing version 0.9.1 to 0.9.2
38
index.js
@@ -156,4 +156,4 @@ 'use strict' | ||
forwarded.get(port).add(port1) | ||
otherPort.postMessage({ type: 'route', url, port: port2 }, [port2]) | ||
port.postMessage({ type: 'route', url: key, port: port1 }, [port1]) | ||
otherPort.postMessage({ type: 'route', url, port: port2, threadId: port.threadId }, [port2]) | ||
port.postMessage({ type: 'route', url: key, port: port1, threadId: otherPort.threadId }, [port1]) | ||
} | ||
@@ -167,4 +167,9 @@ } | ||
const roundRobinIndex = routes.get(url).add(port) | ||
const roundRobin = routes.get(url) | ||
roundRobin.add(port) | ||
// We must copy the threadId outsise because it can be nulled | ||
// by Node.js | ||
const threadId = port.threadId | ||
function onClose () { | ||
@@ -185,3 +190,3 @@ const roundRobin = routes.get(url) | ||
// Notify other threads that any eventual network address for this route is no longer valid | ||
res.setAddress(url, roundRobinIndex) | ||
res.setAddress(url, threadId) | ||
} | ||
@@ -205,3 +210,12 @@ | ||
} else if (msg.type === 'address') { | ||
res.setAddress(url, roundRobinIndex, msg.address, forward) | ||
if (!msg.url) { | ||
res.setAddress(url, port.threadId, msg.address, forward) | ||
} else { | ||
const roundRobin = routes.get(msg.url) | ||
if (!roundRobin) { | ||
return | ||
} | ||
res.setAddress(msg.url, msg.threadId, msg.address, false) | ||
} | ||
} | ||
@@ -211,4 +225,4 @@ }) | ||
res.setAddress = (url, index, address, forward = true) => { | ||
const port = routes.get(url)?.get(index) | ||
res.setAddress = (url, threadId, address, forward = true) => { | ||
const port = routes.get(url)?.findByThreadId(threadId) | ||
@@ -225,3 +239,6 @@ if (port) { | ||
for (const otherPort of roundRobin) { | ||
otherPort.postMessage({ type: 'address', url, index, address }) | ||
// Avoid loops, do not send the message to the source | ||
if (otherPort.threadId !== threadId) { | ||
otherPort.postMessage({ type: 'address', url, address, threadId }) | ||
} | ||
} | ||
@@ -255,3 +272,3 @@ } | ||
if (typeof server === 'string') { | ||
parentPort.postMessage({ type: 'address', address: server }) | ||
parentPort.postMessage({ type: 'address', address: server, threadId }) | ||
} else { | ||
@@ -325,6 +342,7 @@ hasInject = typeof server?.inject === 'function' | ||
} else if (msg.type === 'route') { | ||
msg.port.threadId = msg.threadId | ||
interceptor.route(msg.url, msg.port, false) | ||
msg.port.on('message', onMessage) | ||
} else if (msg.type === 'address') { | ||
interceptor.setAddress(msg.url, msg.index, msg.address, false) | ||
interceptor.setAddress(msg.url, msg.threadId, msg.address, false) | ||
} | ||
@@ -331,0 +349,0 @@ } |
@@ -20,2 +20,6 @@ 'use strict' | ||
findByThreadId (threadId) { | ||
return this.ports.find((p) => p.threadId === threadId) | ||
} | ||
remove (port) { | ||
@@ -34,6 +38,2 @@ const index = this.ports.indexOf(port) | ||
get (index) { | ||
return this.ports[index] | ||
} | ||
get length () { | ||
@@ -40,0 +40,0 @@ return this.ports.length |
{ | ||
"name": "undici-thread-interceptor", | ||
"version": "0.9.1", | ||
"version": "0.9.2", | ||
"description": "An Undici interceptor that routes requests over a worker thread", | ||
@@ -19,3 +19,3 @@ "main": "index.js", | ||
"@fastify/static": "^8.0.2", | ||
"borp": "^0.18.0", | ||
"borp": "^0.19.0", | ||
"eslint": "^9.5.0", | ||
@@ -22,0 +22,0 @@ "express": "^4.19.2", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
64371
35
1685
2