/
dimamir
/
node-soap
Обзор
Документация
Войти
/
dimamir
/
node-soap
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/utils.ts
141 строка
4 KB
millerdk12
Implement custom cache option (#1261)
03 ноя 2024, 21:54
Не верифицирован
03 ноя 2024, 21:54
2122afd
Код
Авторство
О чём код?
import * as crypto from 'crypto'; import { IMTOMAttachments, IWSDLCache } from './types'; import { WSDL } from './wsdl'; export function passwordDigest(nonce: string, created: string, password: string): string { // 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'); } export const 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 */ export function findPrefix(xmlnsMapping, nsURI) { for (const n in xmlnsMapping) { if (n === TNS_PREFIX) { continue; } if (xmlnsMapping[n] === nsURI) { return n; } } } export function splitQName<T>(nsName: T) { if (typeof nsName !== 'string') { return { prefix: TNS_PREFIX, name: nsName, }; } const [topLevelName] = nsName.split('|', 1); const prefixOffset = topLevelName.indexOf(':'); return { prefix: topLevelName.substring(0, prefixOffset) || TNS_PREFIX, name: topLevelName.substring(prefixOffset + 1), }; } export 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; } export function parseMTOMResp(payload: Buffer, boundary: string, callback: (err?: Error, resp?: IMTOMAttachments) => void) { return import('formidable') .then(({ MultipartParser }) => { const resp: IMTOMAttachments = { parts: [], }; let headerName = ''; let headerValue = ''; let data: Buffer; 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); } class DefaultWSDLCache implements IWSDLCache { private cache: { [key: string]: WSDL, }; constructor() { this.cache = {}; } public has(key: string): boolean { return !!this.cache[key]; } public get(key: string): WSDL { return this.cache[key]; } public set(key: string, wsdl: WSDL) { this.cache[key] = wsdl; } public clear() { this.cache = {}; } } export const wsdlCacheSingleton = new DefaultWSDLCache();