soap
Version:
A minimal node SOAP client
313 lines • 9.9 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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.wsdlCacheSingleton = exports.TNS_PREFIX = void 0;
exports.isObject = isObject;
exports.isPlainObject = isPlainObject;
exports.merge = merge;
exports.mergeWith = mergeWith;
exports.defaults = defaults;
exports.defaultsDeep = defaultsDeep;
exports.pickBy = pickBy;
exports.once = once;
exports.getByPath = getByPath;
exports.passwordDigest = passwordDigest;
exports.findPrefix = findPrefix;
exports.splitQName = splitQName;
exports.xmlEscape = xmlEscape;
exports.parseMTOMResp = parseMTOMResp;
exports.streamToText = streamToText;
exports.stripBom = stripBom;
const crypto = __importStar(require("crypto"));
// ---------------------------------------------------------------------------
// Utility functions extracted from lodash (https://lodash.com/)
// MIT License – Copyright JS Foundation and other contributors
// ---------------------------------------------------------------------------
// --- START lodash replacement ---
function isObject(value) {
const type = typeof value;
return value != null && (type === 'object' || type === 'function');
}
function isPlainObject(value) {
if (value === null || typeof value !== 'object') {
return false;
}
const proto = Object.getPrototypeOf(value);
return proto === null || proto === Object.prototype;
}
function merge(target, ...sources) {
return mergeWith(target, ...sources, undefined);
}
function mergeWith(target, ...args) {
const customizer = args.pop();
const sources = args;
for (const source of sources) {
if (source == null) {
continue;
}
for (const key of Object.keys(source)) {
baseMergeValue(target, source, key, customizer);
}
}
return target;
}
function baseMergeValue(target, source, key, customizer) {
const srcValue = source[key];
const objValue = target[key];
if (customizer) {
const customResult = customizer(objValue, srcValue, key);
if (customResult !== undefined) {
target[key] = customResult;
return;
}
}
if (srcValue === undefined) {
return;
}
if (isPlainObject(srcValue)) {
if (isPlainObject(objValue)) {
for (const k of Object.keys(srcValue)) {
baseMergeValue(objValue, srcValue, k, customizer);
}
}
else {
target[key] = merge({}, srcValue);
}
}
else if (Array.isArray(srcValue)) {
if (Array.isArray(objValue)) {
for (let i = 0; i < srcValue.length; i++) {
if (isPlainObject(srcValue[i]) && isPlainObject(objValue[i])) {
merge(objValue[i], srcValue[i]);
}
else if (srcValue[i] !== undefined) {
objValue[i] = srcValue[i];
}
}
}
else {
target[key] = srcValue.slice();
}
}
else {
target[key] = srcValue;
}
}
function defaults(target, ...sources) {
for (const source of sources) {
if (source == null) {
continue;
}
for (const key of Object.keys(source)) {
if (target[key] === undefined) {
target[key] = source[key];
}
}
}
return target;
}
function defaultsDeep(target, ...sources) {
for (const source of sources) {
if (source == null) {
continue;
}
for (const key of Object.keys(source)) {
const tVal = target[key];
const sVal = source[key];
if (isPlainObject(tVal) && isPlainObject(sVal)) {
defaultsDeep(tVal, sVal);
}
else if (tVal === undefined) {
target[key] = sVal;
}
}
}
return target;
}
function pickBy(object, predicate) {
const result = {};
for (const key of Object.keys(object)) {
if (predicate(object[key], key)) {
result[key] = object[key];
}
}
return result;
}
function once(func) {
let called = false;
let result;
return function (...args) {
if (!called) {
called = true;
result = func.apply(this, args);
}
return result;
};
}
function getByPath(obj, path) {
return path.split('.').reduce(function (o, k) {
return o && o[k];
}, obj);
}
// --- END lodash replacement ---
function passwordDigest(nonce, created, password) {
// digest = base64 ( sha1 ( nonce + created + password ) )
const pwHash = crypto.createHash('sha1');
const NonceBytes = Buffer.from(nonce || '', 'base64');
const CreatedBytes = Buffer.from(created || '', 'utf8');
const PasswordBytes = Buffer.from(password || '', 'utf8');
const FullBytes = Buffer.concat([NonceBytes, CreatedBytes, PasswordBytes]);
pwHash.update(FullBytes);
return pwHash.digest('base64');
}
exports.TNS_PREFIX = '__tns__'; // Prefix for targetNamespace
/**
* Find a key from an object based on the value
* @param {Object} Namespace prefix/uri mapping
* @param {*} nsURI value
* @returns {String} The matching key
*/
function findPrefix(xmlnsMapping, nsURI) {
for (const n in xmlnsMapping) {
if (n === exports.TNS_PREFIX) {
continue;
}
if (xmlnsMapping[n] === nsURI) {
return n;
}
}
}
function splitQName(nsName) {
if (typeof nsName !== 'string') {
return {
prefix: exports.TNS_PREFIX,
name: nsName,
};
}
const [topLevelName] = nsName.split('|', 1);
const prefixOffset = topLevelName.indexOf(':');
return {
prefix: topLevelName.substring(0, prefixOffset) || exports.TNS_PREFIX,
name: topLevelName.substring(prefixOffset + 1),
};
}
function xmlEscape(obj) {
if (typeof obj === 'string') {
if (obj.substr(0, 9) === '<![CDATA[' && obj.substr(-3) === ']]>') {
return obj;
}
return obj.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
}
return obj;
}
function parseMTOMResp(payload, boundary, callback) {
return import('formidable')
.then(({ MultipartParser }) => {
const resp = {
parts: [],
};
let headerName = '';
let headerValue = '';
let data;
let partIndex = 0;
const parser = new MultipartParser();
parser.initWithBoundary(boundary);
parser.on('data', ({ name, buffer, start, end }) => {
switch (name) {
case 'partBegin':
resp.parts[partIndex] = {
body: null,
headers: {},
};
data = Buffer.from('');
break;
case 'headerField':
headerName = buffer.slice(start, end).toString();
break;
case 'headerValue':
headerValue = buffer.slice(start, end).toString();
break;
case 'headerEnd':
resp.parts[partIndex].headers[headerName.toLowerCase()] = headerValue;
break;
case 'partData':
data = Buffer.concat([data, buffer.slice(start, end)]);
break;
case 'partEnd':
resp.parts[partIndex].body = data;
partIndex++;
break;
}
});
parser.write(payload);
return callback(null, resp);
})
.catch(callback);
}
async function streamToText(stream) {
const chunks = [];
for await (const chunk of stream) {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
}
return Buffer.concat(chunks).toString('utf8');
}
/**
* Strip UTF-8 byte order mark (BOM) from a string.
*/
function stripBom(str) {
if (str.charCodeAt(0) === 0xfeff) {
return str.slice(1);
}
return str;
}
class DefaultWSDLCache {
constructor() {
this.cache = {};
}
has(key) {
return !!this.cache[key];
}
get(key) {
return this.cache[key];
}
set(key, wsdl) {
this.cache[key] = wsdl;
}
clear() {
this.cache = {};
}
}
exports.wsdlCacheSingleton = new DefaultWSDLCache();
//# sourceMappingURL=utils.js.map