forked from forwardemail/superagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
107 lines (92 loc) · 2.07 KB
/
utils.js
File metadata and controls
107 lines (92 loc) · 2.07 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Return the mime type for the given `str`.
*
* @param {String} str
* @return {String}
* @api private
*/
exports.type = (string_) => string_.split(/ *; */).shift();
/**
* Return header field parameters.
*
* @param {String} str
* @return {Object}
* @api private
*/
exports.params = (value) => {
const object = {};
for (const string_ of value.split(/ *; */)) {
const parts = string_.split(/ *= */);
const key = parts.shift();
const value = parts.shift();
if (key && value) object[key] = value;
}
return object;
};
/**
* Parse Link header fields.
*
* @param {String} str
* @return {Object}
* @api private
*/
exports.parseLinks = (value) => {
const object = {};
for (const string_ of value.split(/ *, */)) {
const parts = string_.split(/ *; */);
const url = parts[0].slice(1, -1);
const rel = parts[1].split(/ *= */)[1].slice(1, -1);
object[rel] = url;
}
return object;
};
/**
* Strip content related fields from `header`.
*
* @param {Object} header
* @return {Object} header
* @api private
*/
exports.cleanHeader = (header, changesOrigin) => {
delete header['content-type'];
delete header['content-length'];
delete header['transfer-encoding'];
delete header.host;
// secuirty
if (changesOrigin) {
delete header.authorization;
delete header.cookie;
}
return header;
};
/**
* Check if `obj` is an object.
*
* @param {Object} object
* @return {Boolean}
* @api private
*/
exports.isObject = (object) => {
return object !== null && typeof object === 'object';
};
/**
* Object.hasOwn fallback/polyfill.
*
* @type {(object: object, property: string) => boolean} object
* @api private
*/
exports.hasOwn =
Object.hasOwn ||
function (object, property) {
if (object == null) {
throw new TypeError('Cannot convert undefined or null to object');
}
return Object.prototype.hasOwnProperty.call(new Object(object), property);
};
exports.mixin = (target, source) => {
for (const key in source) {
if (exports.hasOwn(source, key)) {
target[key] = source[key];
}
}
};