camstreamerlib
Version:
Helper library for CamStreamer ACAP applications.
68 lines (67 loc) • 2.96 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultAgent = void 0;
const HttpRequestSender_1 = require("./internal/HttpRequestSender");
class DefaultAgent {
constructor(opt = {}) {
var _a, _b, _c, _d, _e;
this.tls = (_a = opt.tls) !== null && _a !== void 0 ? _a : false;
this.ip = (_b = opt.ip) !== null && _b !== void 0 ? _b : '127.0.0.1';
this.port = (_c = opt.port) !== null && _c !== void 0 ? _c : (this.tls ? 443 : 80);
this.user = (_d = opt.user) !== null && _d !== void 0 ? _d : '';
this.pass = (_e = opt.pass) !== null && _e !== void 0 ? _e : '';
let agentOptions;
if (opt.tlsInsecure !== undefined || opt.keepAlive !== undefined) {
agentOptions = {
rejectUnaurhorized: !opt.tlsInsecure,
keepAlive: opt.keepAlive,
};
}
this.httpRequestSender = new HttpRequestSender_1.HttpRequestSender(agentOptions);
}
get(path, parameters = {}, headers) {
return __awaiter(this, void 0, void 0, function* () {
const options = this.getBaseConnectionParams('GET', path, parameters);
options.headers = headers;
return this.httpRequestSender.sendRequest(options);
});
}
post(path, data, parameters = {}, headers) {
return __awaiter(this, void 0, void 0, function* () {
const options = this.getBaseConnectionParams('POST', path, parameters);
options.headers = headers;
return this.httpRequestSender.sendRequest(options, data);
});
}
getBaseConnectionParams(method, path, params) {
if (path.indexOf('?') === -1) {
path += '?';
}
else {
path += '&';
}
for (const key in params) {
path += `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}&`;
}
path = path.slice(0, path.length - 1);
return {
method: method,
protocol: this.tls ? 'https:' : 'http:',
host: this.ip,
port: this.port,
path: path,
user: this.user,
pass: this.pass,
};
}
}
exports.DefaultAgent = DefaultAgent;