-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
utils.js
139 lines (116 loc) · 2.67 KB
/
utils.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
let RE2;
let hasRE2 = true;
try {
RE2 = require('re2');
} catch {
hasRE2 = false;
}
const SafeRegExp = hasRE2 ? RE2 : RegExp;
/**
* 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];
}
}
};
/**
* Check if the response is compressed using Gzip or Deflate.
* @param {Object} res
* @return {Boolean}
*/
exports.isGzipOrDeflateEncoding = (res) => {
return new SafeRegExp(/^\s*(?:deflate|gzip)\s*$/).test(res.headers['content-encoding']);
};
/**
* Check if the response is compressed using Brotli.
* @param {Object} res
* @return {Boolean}
*/
exports.isBrotliEncoding = (res) => {
return new SafeRegExp(/^\s*(?:br)\s*$/).test(res.headers['content-encoding']);
};