Backend half
This commit is contained in:
Generated
Vendored
+36
@@ -0,0 +1,36 @@
|
||||
import { timing } from "./timing";
|
||||
const DEFER_EVENT_LISTENER_TIME = 1000;
|
||||
export const setConnectionTimeout = (request, reject, timeoutInMs = 0) => {
|
||||
if (!timeoutInMs) {
|
||||
return -1;
|
||||
}
|
||||
const registerTimeout = (offset) => {
|
||||
const timeoutId = timing.setTimeout(() => {
|
||||
request.destroy();
|
||||
reject(Object.assign(new Error(`Socket timed out without establishing a connection within ${timeoutInMs} ms`), {
|
||||
name: "TimeoutError",
|
||||
}));
|
||||
}, timeoutInMs - offset);
|
||||
const doWithSocket = (socket) => {
|
||||
if (socket?.connecting) {
|
||||
socket.on("connect", () => {
|
||||
timing.clearTimeout(timeoutId);
|
||||
});
|
||||
}
|
||||
else {
|
||||
timing.clearTimeout(timeoutId);
|
||||
}
|
||||
};
|
||||
if (request.socket) {
|
||||
doWithSocket(request.socket);
|
||||
}
|
||||
else {
|
||||
request.on("socket", doWithSocket);
|
||||
}
|
||||
};
|
||||
if (timeoutInMs < 2000) {
|
||||
registerTimeout(0);
|
||||
return 0;
|
||||
}
|
||||
return timing.setTimeout(registerTimeout.bind(null, DEFER_EVENT_LISTENER_TIME), DEFER_EVENT_LISTENER_TIME);
|
||||
};
|
||||
Reference in New Issue
Block a user