vsphere-infra
Version:
173 lines • 6.96 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomHttpClient = exports.sdkCaches = exports.CustomHttpClientOptions = void 0;
const url = __importStar(require("url"));
const fs = __importStar(require("fs"));
const axios_1 = __importDefault(require("axios"));
const request = __importStar(require("request"));
const httpNtlm = __importStar(require("httpntlm"));
class CustomHttpClientOptions {
constructor(init) {
this.sdkCache = true;
this.sdkCacheProfile = 'default';
if (init) {
Object.assign(this, init);
}
if (!this.sdkCacheProfile) {
this.sdkCacheProfile = 'default';
}
}
}
exports.CustomHttpClientOptions = CustomHttpClientOptions;
exports.sdkCaches = {
'core-types.xsd': 1,
'query-messagetypes.xsd': 1,
'query-types.xsd': 1,
'reflect-messagetypes.xsd': 1,
'reflect-types.xsd': 1,
'vim-messagetypes.xsd': 1,
'vim-types.xsd': 1,
'vim.wsdl': 1,
};
class CustomHttpClient {
constructor(options) {
this._request = request;
this.config = new CustomHttpClientOptions(options);
}
buildRequest(rurl, data, exheaders, exoptions) {
if (exoptions === void 0) {
exoptions = {};
}
const curl = url.parse(rurl);
const host = curl.hostname;
const port = parseInt(curl.port, 10);
const method = data ? 'POST' : 'GET';
const headers = {
'User-Agent': 'node-soap-custom-http-client',
Accept: 'text/html,application/xhtml+xml,application/xml,text/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'none',
'Accept-Charset': 'utf-8',
Connection: exoptions.forever ? 'keep-alive' : 'close',
Host: host + (isNaN(port) ? '' : ':' + port)
};
const mergeOptions = ['headers'];
const { attachments: _attachments } = exoptions, newExoptions = __rest(exoptions, ["attachments"]);
const attachments = _attachments || [];
if (typeof data === 'string' && attachments.length === 0 && !exoptions.forceMTOM) {
headers['Content-Length'] = Buffer.byteLength(data, 'utf8');
headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
exheaders = exheaders || {};
for (const attr of Object.keys(exheaders)) {
headers[attr] = exheaders[attr];
}
const options = {
uri: curl,
method, headers,
followAllRedirects: true
};
options.body = data;
for (const attr of Object.keys(newExoptions)) {
if (mergeOptions.indexOf(attr) !== -1) {
for (const header of Object.keys(exoptions[attr])) {
options[attr][header] = exoptions[attr][header];
}
}
else {
options[attr] = exoptions[attr];
}
}
return options;
}
handleResponse(reqObj, res, body) { return body; }
request(rurl, data, callback, exheaders, exoptions, caller) {
const options = this.buildRequest(rurl, data, exheaders, exoptions);
let reqObj;
if (exoptions !== undefined && exoptions.hasOwnProperty('ntlm')) {
options.url = rurl;
httpNtlm[options.method.toLowerCase()](options, (err, res) => {
if (err) {
return callback(err);
}
if (typeof res.body !== 'string') {
res.body = res.body.toString();
}
res.body = this.handleResponse(reqObj, res, res.body);
callback(null, res, res.body);
});
}
else {
const urlObj = options.url = options.uri;
options.data = options.body;
if (this.config.sdkCache && urlObj.path.startsWith('/sdk/')) {
const target = urlObj.path.split('/sdk/')[1];
if (exports.sdkCaches[target]) {
fs.readFile(`${__dirname}/sdk-cache/${this.config.sdkCacheProfile}/${target}`, (e, dataBuffer) => {
if (e) {
console.log(e);
return;
}
const dat = dataBuffer.toString('utf8');
const code = 200;
const res = { status: code, statusCode: code, body: dat, data: dat };
callback(null, res, res.body);
});
return;
}
}
if (typeof options.url === 'object') {
options.url = options.url.href;
}
reqObj = (0, axios_1.default)(options).then(res => {
res.body = res.data;
res.statusCode = res.status;
callback(null, res, res.data);
}).catch(e => {
callback(e);
});
}
}
requestStream(rurl, data, exheaders, exoptions, caller) {
const options = this.buildRequest(rurl, data, exheaders, exoptions);
return this._request(options);
}
}
exports.CustomHttpClient = CustomHttpClient;
//# sourceMappingURL=soap.request.js.map