Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broadcast proxy #126

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/Broadcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,21 @@ class Broadcast {
}

if (target.socket._prompted) {
target.socket.write('\r\n');
if (target.socket.broadcastProxy) {
target.socket.broadcastProxy(target, '\r\n', { wrapWidth, useColor, formatter });
} else {
target.socket.write('\r\n');
}
target.socket._prompted = false;
}

let targetMessage = formatter(target, message);
targetMessage = wrapWidth ? Broadcast.wrap(targetMessage, wrapWidth) : ansi.parse(targetMessage);
target.socket.write(targetMessage);
if (target.socket.broadcastProxy) {
target.socket.broadcastProxy(target, message, { wrapWidth, useColor, formatter } );
} else {
let targetMessage = formatter(target, message);
targetMessage = wrapWidth ? Broadcast.wrap(targetMessage, wrapWidth) : ansi.parse(targetMessage);
target.socket.write(targetMessage);
}
}
}

Expand Down
55 changes: 55 additions & 0 deletions src/BroadcastProxyRegistry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

module.exports = class BroadcastProxyRegistry extends Map {
load(requireFn, rootPath, config = {}) {
for (const [name, settings] of Object.entries(config)) {
if (!settings.hasOwnProperty('require')) {
throw new Error(`BroadcastProxyRegistry [${name}] does not specify a 'require'`);
}

if (typeof settings.require !== 'string') {
throw new TypeError(`BroadcastProxyRegistry [${name}] has an invalid 'require'`);
}

const sourceConfig = settings.config || {};

let proxy;

// relative path to require
if (settings.require[0] === '.') {
proxy = require(rootPath + '/' + settings.require);
} else if (!settings.require.includes('.')) {
proxy = require(settings.require);
} else {
const [moduleName, exportName] = settings.require.split('.');
proxy = requireFn(moduleName)[exportName];
}

const instance = new proxy();

if (typeof instance.configure !== 'function') {
throw new Error(`BroadcastProxyRegistry ${name} requires a 'proxy' method`);
}

this.set(name, instance);
}
}

attach(stream) {
const identifier = stream.identifier;
if (!stream.identifier) {
throw new Error("TransportStreams must define a 'identifier' getter.");
}

const proxyInstance = this.get(identifier);
if (!proxyInstance) {
throw new Error(`No BroadcastProxies are defined for identifier: ${identifier}`);
}

Object.assign(stream, {
broadcastProxy(target, message, options) {
proxyInstance.proxy(target, message, options);
}
});
}
};