Skip to content

Commit

Permalink
Merge pull request #58 from exposr/tunnelservice-refactor
Browse files Browse the repository at this point in the history
Refactor TunnelService
  • Loading branch information
fredriklindberg authored Sep 30, 2023
2 parents 8dfc1c1 + d627bef commit b13d2c7
Show file tree
Hide file tree
Showing 35 changed files with 2,455 additions and 1,468 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@
"devDependencies": {
"@rollup/plugin-commonjs": "^24.0.1",
"@rollup/plugin-json": "^6.0.0",
"@types/koa-joi-router": "^8.0.5",
"@types/mocha": "^10.0.1",
"@types/node": "^20.5.0",
"@types/sinon": "^10.0.17",
"@types/ws": "^8.5.5",
"commit-and-tag-version": "^11.2.1",
"mocha": "^10.2.0",
Expand Down
2 changes: 1 addition & 1 deletion src/cluster/cluster-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Node {

static address4 = Node._getIP(Node.interface, 'IPv4');
static address6 = Node._getIP(Node.interface, 'IPv6');
static address = Node.address4 || Node.address6;
static address = Node.address4 || Node.address6 || '0.0.0.0';

static getIP() {
return Node._getIP(Node.interface, 'IPv4') || Node._getIP(Node.interface, 'IPv6');
Expand Down
24 changes: 18 additions & 6 deletions src/cluster/eventbus.js → src/cluster/eventbus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@ import { EventEmitter } from 'events';
import { Logger } from '../logger.js';
import ClusterService from './index.js';

export type EmitMeta = {
node: {
id: string,
host: string,
ip: string,
},
ts: number,
}

class EventBus extends EventEmitter {
private logger: any;
private clusterService: ClusterService;

constructor() {
super();
this.logger = Logger("eventbus");
Expand All @@ -19,13 +31,13 @@ class EventBus extends EventEmitter {
clusterService.attach(this);
}

async destroy() {
public async destroy() {
this.removeAllListeners();
this.clusterService.detach(this);
return this.clusterService.destroy();
}

_emit(event, message, meta) {
public _emit(event: string, message: any, meta: EmitMeta) {
super.emit(event, message, meta);
this.logger.isTraceEnabled() &&
this.logger.trace({
Expand All @@ -36,14 +48,14 @@ class EventBus extends EventEmitter {
});
}

async publish(event, message) {
async publish(event: string, message: any) {
return this.clusterService.publish(event, message);
}

async waitFor(channel, predicate, timeout = undefined) {
async waitFor(channel: string, predicate: (message: any, meta: EmitMeta) => boolean, timeout: number | undefined) {
return new Promise((resolve, reject) => {
let timer;
const fun = (message, meta) => {
let timer: NodeJS.Timeout;
const fun = (message: any, meta: EmitMeta) => {
if (!predicate(message, meta)) {
return;
}
Expand Down
Loading

0 comments on commit b13d2c7

Please sign in to comment.