-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathutils.js
More file actions
208 lines (182 loc) · 5.3 KB
/
utils.js
File metadata and controls
208 lines (182 loc) · 5.3 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict';
const util = require('node:util');
const is = require('is-type-of');
const URL = require('node:url').URL;
module.exports = {
convertObject,
safeParseURL,
createTransparentProxy,
};
function convertObject(obj, ignore, ignoreKeyPaths) {
if (!is.array(ignore)) {
ignore = [ ignore ];
}
if (!is.array(ignoreKeyPaths)) {
ignoreKeyPaths = ignoreKeyPaths ? [ ignoreKeyPaths ] : [];
}
_convertObject(obj, ignore, ignoreKeyPaths, '');
}
function _convertObject(obj, ignore, ignoreKeyPaths, keyPath) {
for (const key of Object.keys(obj)) {
obj[key] = convertValue(key, obj[key], ignore, ignoreKeyPaths, keyPath ? `${keyPath}.${key}` : key);
}
return obj;
}
function convertValue(key, value, ignore, ignoreKeyPaths, keyPath) {
if (is.nullOrUndefined(value)) return value;
let hit = false;
let hitKeyPath = false;
for (const matchKey of ignore) {
if (is.string(matchKey) && matchKey === key) {
hit = true;
break;
} else if (is.regExp(matchKey) && matchKey.test(key)) {
hit = true;
break;
}
}
for (const matchKeyPath of ignoreKeyPaths) {
if (is.string(matchKeyPath) && keyPath === matchKeyPath) {
hitKeyPath = true;
break;
}
}
if (!hit && !hitKeyPath) {
if (is.symbol(value) || is.regExp(value)) return value.toString();
if (is.primitive(value) || is.array(value)) return value;
}
// only convert recursively when it's a plain object,
// o = {}
if (Object.getPrototypeOf(value) === Object.prototype) {
if (hitKeyPath) {
return '<Object>';
}
return _convertObject(value, ignore, ignoreKeyPaths, keyPath);
}
// support class
const name = value.name || 'anonymous';
if (is.class(value)) {
return `<Class ${name}>`;
}
// support generator function
if (is.function(value)) {
if (is.generatorFunction(value)) return `<GeneratorFunction ${name}>`;
if (is.asyncFunction(value)) return `<AsyncFunction ${name}>`;
return `<Function ${name}>`;
}
const typeName = value.constructor.name;
if (typeName) {
if (is.buffer(value) || is.string(value)) return `<${typeName} len: ${value.length}>`;
return `<${typeName}>`;
}
/* istanbul ignore next */
return util.format(value);
}
function safeParseURL(url) {
try {
return new URL(url);
} catch {
return null;
}
}
/**
* Create a Proxy that behaves like the real object, but remains transparent to
* monkeypatch libraries (e.g. defineProperty-based overrides).
*
* - Lazily creates the real object on first access.
* - Allows overriding properties on the proxy target (overlay) to take effect.
* - Delegates everything else to the real object.
*
* @param {Object} options
* @param {Function} options.createReal Create the real object (lazy)
* @param {boolean} [options.bindFunctions=true] Bind real methods to the real object
* @return {Proxy}
*/
function createTransparentProxy({ createReal, bindFunctions = true }) {
if (typeof createReal !== 'function') {
throw new TypeError('createReal must be a function');
}
let real = null;
let error = null;
let initialized = false;
const init = () => {
if (initialized) {
if (error) throw error;
return;
}
initialized = true;
try {
real = createReal();
} catch (err) {
error = err;
throw err;
}
};
return new Proxy({}, {
get(target, prop, receiver) {
init();
// Check if property is defined on proxy target (monkeypatch overlay)
if (Object.getOwnPropertyDescriptor(target, prop)) {
return Reflect.get(target, prop, receiver);
}
const value = real[prop];
if (bindFunctions && typeof value === 'function') {
return value.bind(real);
}
return value;
},
set(target, prop, value, receiver) {
init();
if (Object.getOwnPropertyDescriptor(target, prop)) {
return Reflect.set(target, prop, value, receiver);
}
return Reflect.set(real, prop, value);
},
has(target, prop) {
init();
return prop in target || prop in real;
},
ownKeys(target) {
init();
const keys = new Set([ ...Reflect.ownKeys(real), ...Reflect.ownKeys(target) ]);
return Array.from(keys);
},
getOwnPropertyDescriptor(target, prop) {
init();
return Object.getOwnPropertyDescriptor(target, prop)
|| Object.getOwnPropertyDescriptor(real, prop);
},
deleteProperty(target, prop) {
init();
if (Object.getOwnPropertyDescriptor(target, prop)) {
return delete target[prop];
}
return delete real[prop];
},
getPrototypeOf() {
init();
return Object.getPrototypeOf(real);
},
setPrototypeOf(_target, proto) {
init();
return Reflect.setPrototypeOf(real, proto);
},
isExtensible() {
init();
return Reflect.isExtensible(real);
},
preventExtensions(target) {
init();
// Must also prevent extensions on target to satisfy Proxy invariants
const result = Reflect.preventExtensions(real);
if (result) {
Reflect.preventExtensions(target);
}
return result;
},
defineProperty(target, prop, descriptor) {
// Used by monkeypatch libs: keep overrides on proxy target (overlay layer).
return Reflect.defineProperty(target, prop, descriptor);
},
});
}