@norniras/service-agent
Version:
Service agent made for handling incoming data and commands.
121 lines • 4.67 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ServiceAgent = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
class ServiceAgent {
constructor({ serviceUrl, token, ghostId, restartStream = true }) {
this.serviceUrl = serviceUrl,
this.token = token,
this.ghostId = ghostId,
this.restartStream = restartStream;
}
get domain() {
const regExp = /\w[\w-]+\w(?=\.)/i;
const result = regExp.exec(this.serviceUrl);
if (!result)
return;
return result[0];
}
get service() {
const regExp = /(?<=(\w[\w-]+\w\.\w+)\/)(\w[\w-]+\w)/i;
const result = regExp.exec(this.serviceUrl);
if (!result)
return;
return result[0];
}
get baseHeaders() {
return {
'Content-Type': 'application/x-www-form-urlencoded'
};
}
getQueryStringFromObject(obj) {
return Object.entries(obj)
.map(([key, value]) => `${key}=${value}`)
.join('&');
}
listen(cb) {
return __awaiter(this, void 0, void 0, function* () {
let readableStream;
try {
console.log('Agent is listening');
const { body } = yield (0, node_fetch_1.default)(this.serviceUrl, {
body: `token=${this.token}&objectID=${this.ghostId}&format=json`,
headers: Object.assign(Object.assign({}, this.baseHeaders), { 'Synx-Cat': '4' }),
method: 'POST'
});
readableStream = body;
}
catch (_e) {
readableStream = null;
console.log('External Error! Agent cannot listen');
}
;
if (!readableStream)
return;
readableStream.on('readable', () => {
const data = readableStream
.setEncoding('utf8')
.read();
if (!data || typeof data !== 'string')
return;
try {
const { RTW, CMD } = JSON.parse(data.trim());
cb({
data: RTW,
command: CMD
});
}
catch (_e) {
console.log('Received data is not a JSON');
}
});
readableStream.on('close', () => {
console.log('Agent is closed');
if (this.restartStream)
this.listen(cb);
});
});
}
sendData({ ghostId, dataString }) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield (0, node_fetch_1.default)(this.serviceUrl, {
body: `token=${this.token}&objectID=${ghostId}&${dataString}`,
headers: Object.assign(Object.assign({}, this.baseHeaders), { 'Synx-Cat': '1' }),
method: 'POST'
});
}
catch (e) {
console.error(e);
}
});
}
sendCommand({ targetUrl, commandString }) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield (0, node_fetch_1.default)(targetUrl, {
body: `token=${this.token}&refdomain=${this.domain}&refservice=${this.service}&${commandString}`,
headers: Object.assign(Object.assign({}, this.baseHeaders), { 'Synx-Cat': '0' }),
method: 'POST'
});
}
catch (e) {
console.error(e);
}
});
}
}
exports.ServiceAgent = ServiceAgent;
//# sourceMappingURL=index.js.map