node-pm2-events
Advanced tools
Comparing version 1.2.44 to 1.3.0
{ | ||
"name": "node-pm2-events", | ||
"version": "1.2.44", | ||
"version": "1.3.0", | ||
"description": "EventBus for local and decentralized instances of the both for individual nodejs applications and as parts of pm2", | ||
"main": "index.js", | ||
"types": "types/index.d.ts", | ||
"scripts": { | ||
"test": "npm run test:local && npm run test:transport", | ||
"test": "npx tsd && npm run test:local && npm run test:transport", | ||
"test:cluster": "node tests/eventbus-cluster-node.js.v.20.js", | ||
"test:local": "node tests/eventbus-local-process.js", | ||
"test:transport": "node tests/eventbus-transport-become-primary.js & node tests/eventbus-transport-become-primary.js" | ||
"test:transport": "node tests/eventbus-transport-become-primary.js & node tests/eventbus-transport-become-primary.js", | ||
"test:tsd": "npx tsd" | ||
}, | ||
@@ -32,3 +34,11 @@ "repository": { | ||
"ioredis": "^5.3.2" | ||
}, | ||
"devDependencies": { | ||
"dotenv": "^16.3.1", | ||
"mocha": "^10.2.0", | ||
"nyc": "^15.1.0", | ||
"typescript": "^5.2.2", | ||
"tsd": "^0.29.0", | ||
"@types/node": "^20.8.10" | ||
} | ||
} |
@@ -6,2 +6,3 @@ ## 🇺🇦Decentralized instances events | ||
[](http://www.npmtrends.com/node-pm2-events) | ||
[](https://snyk.io/test/npm/node-pm2-events) | ||
@@ -8,0 +9,0 @@ Data exchange between instances of pm2 |
@@ -15,4 +15,10 @@ /* | ||
#sendbox = false; | ||
/** | ||
* @type {EventBus} | ||
*/ | ||
static #instance = null; | ||
/** | ||
* @return {EventBus} | ||
*/ | ||
static get instance() { | ||
@@ -22,2 +28,5 @@ return this.getInstance() | ||
/** | ||
* @return {EventBus} | ||
*/ | ||
static getInstance() { | ||
@@ -33,2 +42,3 @@ if (!this.#instance) this.#instance = new EventBus() | ||
this.websocket.setSendbox(v) | ||
return this; | ||
} | ||
@@ -35,0 +45,0 @@ |
@@ -12,2 +12,5 @@ /* | ||
class Transport { | ||
/** | ||
* @type {{READY: string, subscriber: string, SIGINT: string, HANDSHAKE: string, publisher: string, type: {iamhere: string, bye: string}, message: string}} | ||
*/ | ||
constant = { | ||
@@ -39,4 +42,8 @@ SIGINT: 'SIGINT', | ||
setSendbox(v) { | ||
this.#sendbox = !!v; | ||
/** | ||
* Transport on ready state? | ||
* @return {boolean} | ||
*/ | ||
get isReady() { | ||
return this.#publisher && this.#publisher.status === this.constant.READY; | ||
} | ||
@@ -55,7 +62,18 @@ | ||
/** | ||
* Transport on ready state? | ||
* @return {null|boolean} | ||
* @return {{process_id: number, process_name: string, id: string, address: string; | ||
* netmask: string; | ||
* mac: string; | ||
* internal: boolean; | ||
* cidr: string | null; | ||
* family: string; | ||
* scopeid?: undefined; | ||
* }} | ||
*/ | ||
get isReady() { | ||
return this.#publisher && this.#publisher.status === this.constant.READY; | ||
get processInfo() { | ||
return { | ||
id: this.#id, | ||
process_name: this.#EventBus.process.process_name, | ||
process_id: this.#EventBus.process.process_id, | ||
...this.#EventBus.process.interface | ||
} | ||
} | ||
@@ -79,14 +97,6 @@ | ||
get processInfo() { | ||
return { | ||
id: this.#id, | ||
process_name: this.#EventBus.process.process_name, | ||
process_id: this.#EventBus.process.process_id, | ||
...this.#EventBus.process.interface | ||
} | ||
} | ||
/** | ||
* Set callback on transport ready state changed | ||
* @param {function(name<string>, state<string>)} callback | ||
* @param {function(name:string, state:string)} callback | ||
* @return {Transport} | ||
*/ | ||
@@ -99,4 +109,13 @@ set onStateChange(callback) { | ||
/** | ||
* @param {boolean} v | ||
* @return {Transport} | ||
*/ | ||
setSendbox(v) { | ||
this.#sendbox = !!v; | ||
return this; | ||
} | ||
/** | ||
* Establish that this is the main process | ||
* @param {string<EventBus.process.id>} id | ||
* @param {string:EventBus.process.id} id | ||
* @return {Transport} | ||
@@ -121,3 +140,4 @@ */ | ||
/** | ||
* @param {function(isPrimary<boolean>)} callback | ||
* @param {function(isPrimary:boolean)} callback | ||
* @return {Transport} | ||
*/ | ||
@@ -254,3 +274,3 @@ onPrimaryChange(callback) { | ||
* @param {string} channel | ||
* @param {function(channel, message<{channel, id, data, sender}>)} callback | ||
* @param {function(channel:string, message:{channel:string, id:string, data:object, sender:object})} callback | ||
* @description msg - {channel, id, data: message,} | ||
@@ -296,4 +316,7 @@ */ | ||
/** | ||
* @return {string} | ||
*/ | ||
toString() { | ||
return { | ||
return JSON.stringify({ | ||
...this.processInfo, | ||
@@ -304,3 +327,3 @@ excludeAddress: this.#excludeAddress, | ||
isPrimary: this.#isPrimary, | ||
} | ||
}) | ||
} | ||
@@ -307,0 +330,0 @@ |
@@ -7,5 +7,12 @@ /* | ||
/** | ||
* @param {object|string} msg | ||
* @return {object} | ||
*/ | ||
const parse = (msg) => msg.constructor === ''.constructor ? JSON.parse(msg) : msg; | ||
const getRandomUID = () => Math.random().toString(16).substring(2); | ||
/** | ||
* @param {number} ms | ||
* @return {Promise<number>} | ||
*/ | ||
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); | ||
@@ -12,0 +19,0 @@ |
@@ -13,4 +13,9 @@ /* | ||
/** | ||
* @param {boolean} v | ||
* @return {WebSocket} | ||
*/ | ||
setSendbox(v) { | ||
this.#sendbox = !!v; | ||
return this | ||
} | ||
@@ -17,0 +22,0 @@ |
@@ -50,3 +50,4 @@ /* | ||
console.log(text, v.name); | ||
process.exit(1); | ||
// can be error with free redis service access | ||
//process.exit(1); | ||
} | ||
@@ -53,0 +54,0 @@ } |
@@ -8,3 +8,2 @@ /* | ||
const {sleep} = require('../src/EventBus/utils') | ||
// const EventBus = require('node-pm2-events'); | ||
@@ -41,3 +40,3 @@ // Try using a free [Redis server](https://app.redislabs.com/) | ||
EventBus.transport.send(channelName, {awesomedata: 'some action'}); | ||
await EventBus.transport.send(channelName, {awesomedata: 'some action'}); | ||
@@ -53,5 +52,5 @@ EventBus.transport.onPrimaryChange(async (isPrimary) => { | ||
// example | ||
for (let i = 0; i < 2; i++) { | ||
for (let i = 0; i < 5; i++) { | ||
if (!EventBus.transport.isPrimary) break; // is it still the main one? | ||
EventBus.transport.send(channelName, { | ||
await EventBus.transport.send(channelName, { | ||
from: 'Primary', | ||
@@ -58,0 +57,0 @@ primary: EventBus.process.process_name, |
61929
36
1364
244
6