forked from vpulim/node-soap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
66 lines (54 loc) · 1.68 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import * as crypto from 'crypto';
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('|');
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;
}