node-health-agent
Advanced tools
Comparing version 0.1.0 to 0.3.0
69
index.js
@@ -5,3 +5,3 @@ const os = require('os') | ||
const heartbeat = (ws) => { | ||
const heartbeat = (ws, delay) => { | ||
clearTimeout(ws.pingTimeout) | ||
@@ -15,14 +15,16 @@ | ||
ws.terminate() | ||
}, 30000 + 1000) | ||
}, delay) | ||
} | ||
const connectToWSS = async (config, inspector) => { | ||
const ws = new WebSocket(`${config.serverUrl}/${os.hostname()}/${config.appName}`) | ||
const connectToWSS = (config, inspector, destroyed) => { | ||
const WSSConfig = {} | ||
const delay = config.heartbeatDelay || 31000 | ||
const autoReconnectDelay = config.autoReconnectDelay || 1000 | ||
if (config.token) WSSConfig.headers = { token: config.token } | ||
let ws = new WebSocket(`${config.serverUrl}/${os.hostname()}/${config.appName}`, WSSConfig) | ||
ws.on('ping', () => { heartbeat(ws) }) | ||
const ping = () => { heartbeat(ws, delay) } | ||
ws.on('open', () => { | ||
console.info('Connected to WS server.') | ||
heartbeat(ws) | ||
}) | ||
ws.on('ping', ping) | ||
.on('open', ping) | ||
@@ -34,11 +36,34 @@ ws.on('message', (msg) => { | ||
ws.on('close', async () => { | ||
ws.on('close', () => { | ||
ws.terminate() | ||
await connectToWSS(config, inspector) | ||
clearTimeout(ws.pingTimeout) | ||
if (!destroyed) { | ||
setTimeout(() => { | ||
ws.removeAllListeners() | ||
ws = (connectToWSS(config, inspector, destroyed)).ws | ||
}, autoReconnectDelay) | ||
} | ||
}) | ||
ws.on('error', async (err) => { | ||
ws.on('error', (err) => { | ||
if (err.code === 'ECONNREFUSED') { | ||
ws.removeAllListeners() | ||
ws = (connectToWSS(config, inspector, destroyed)).ws | ||
} | ||
ws.terminate() | ||
}) | ||
return { | ||
destroy: async () => { | ||
destroyed = true | ||
if (inspector) await inspector.destroy() | ||
clearTimeout(ws.pingTimeout) | ||
ws.terminate() | ||
}, | ||
ws: ws, | ||
addEvent: (event, fn) => { | ||
events[event] = fn | ||
} | ||
} | ||
} | ||
@@ -59,11 +84,15 @@ | ||
if (!config.appName) { | ||
console.error('Can\'t start node health agent, no app name provided!') | ||
return | ||
const msg = 'Can\'t start node health agent, no app name provided!' | ||
console.error(msg) | ||
return new Error(msg) | ||
} | ||
if (!config.serverUrl) { | ||
console.error('Can\'t start node health agent, no server url!') | ||
return | ||
const msg = 'Can\'t start node health agent, no server url!' | ||
console.error(msg) | ||
return new Error(msg) | ||
} | ||
const destroyed = false | ||
let inspector | ||
if (config.inspector) { | ||
@@ -74,9 +103,3 @@ inspector = new Inspector(config.inspector) | ||
connectToWSS(config, inspector) | ||
return { | ||
addEvent: (event, fn) => { | ||
events[event] = fn | ||
} | ||
} | ||
return connectToWSS(config, inspector, destroyed) | ||
} |
{ | ||
"name": "node-health-agent", | ||
"version": "0.1.0", | ||
"version": "0.3.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "NODE_ENV=test ./node_modules/.bin/jest", | ||
"lint": "./node_modules/.bin/eslint ./" | ||
@@ -25,4 +26,5 @@ }, | ||
"eslint-plugin-promise": "^4.2.1", | ||
"eslint-plugin-standard": "^4.0.1" | ||
"eslint-plugin-standard": "^4.0.1", | ||
"jest": "^24.9.0" | ||
} | ||
} |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
13777
7
283
7
1