butlerd
Version:
Node.js library for butlerd, the butler daemon
67 lines (66 loc) • 2.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const conversation_1 = require("./conversation");
var debug = require("debug")("butlerd:client");
class Client {
constructor(endpoint) {
this.idSeed = 1;
this.endpoint = endpoint;
{
const [host, port] = endpoint.tcp.address.split(":");
[this.host, this.port] = [host, parseInt(port, 10)];
}
{
let proxy = process.env.BUTLERD_PROXY;
if (proxy && proxy != "") {
const tokens = proxy.split(":");
if (tokens && tokens.length === 2) {
const [host, port] = tokens;
this.proxy = {
host: host,
port: parseInt(port, 10),
};
}
}
}
this.clientId = `client-${(Math.random() * 1024 * 1024).toFixed(0)}`;
debug(`Now speaking to ${this.host}:${this.port}`);
if (this.proxy) {
debug(`Through proxy ${this.proxy.host}:${this.proxy.port}`);
}
}
generateID() {
return this.idSeed++;
}
warn(msg) {
if (this.warningHandler) {
try {
this.warningHandler(msg);
return;
}
catch (e) { }
}
console.warn(msg);
}
onError(handler) {
this.errorHandler = handler;
}
onWarning(handler) {
this.warningHandler = handler;
}
async call(rc, params, setup) {
let conversation = new conversation_1.Conversation(this);
try {
if (setup) {
setup(conversation);
}
await conversation.connect();
let res = await conversation.call(rc, params);
return res;
}
finally {
conversation.close();
}
}
}
exports.Client = Client;