egg 3.32.0 → 3.33.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/core/utils.js +116 -0
- package/lib/egg.js +18 -3
- package/package.json +1 -1
package/lib/core/utils.js
CHANGED
|
@@ -7,6 +7,7 @@ const URL = require('url').URL;
|
|
|
7
7
|
module.exports = {
|
|
8
8
|
convertObject,
|
|
9
9
|
safeParseURL,
|
|
10
|
+
createTransparentProxy,
|
|
10
11
|
};
|
|
11
12
|
|
|
12
13
|
function convertObject(obj, ignore, ignoreKeyPaths) {
|
|
@@ -90,3 +91,118 @@ function safeParseURL(url) {
|
|
|
90
91
|
return null;
|
|
91
92
|
}
|
|
92
93
|
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Create a Proxy that behaves like the real object, but remains transparent to
|
|
97
|
+
* monkeypatch libraries (e.g. defineProperty-based overrides).
|
|
98
|
+
*
|
|
99
|
+
* - Lazily creates the real object on first access.
|
|
100
|
+
* - Allows overriding properties on the proxy target (overlay) to take effect.
|
|
101
|
+
* - Delegates everything else to the real object.
|
|
102
|
+
*
|
|
103
|
+
* @param {Object} options
|
|
104
|
+
* @param {Function} options.createReal Create the real object (lazy)
|
|
105
|
+
* @param {boolean} [options.bindFunctions=true] Bind real methods to the real object
|
|
106
|
+
* @return {Proxy}
|
|
107
|
+
*/
|
|
108
|
+
function createTransparentProxy({ createReal, bindFunctions = true }) {
|
|
109
|
+
if (typeof createReal !== 'function') {
|
|
110
|
+
throw new TypeError('createReal must be a function');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
let real = null;
|
|
114
|
+
let error = null;
|
|
115
|
+
let initialized = false;
|
|
116
|
+
|
|
117
|
+
const init = () => {
|
|
118
|
+
if (initialized) {
|
|
119
|
+
if (error) throw error;
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
initialized = true;
|
|
123
|
+
try {
|
|
124
|
+
real = createReal();
|
|
125
|
+
} catch (err) {
|
|
126
|
+
error = err;
|
|
127
|
+
throw err;
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
return new Proxy({}, {
|
|
132
|
+
get(target, prop, receiver) {
|
|
133
|
+
init();
|
|
134
|
+
// Check if property is defined on proxy target (monkeypatch overlay)
|
|
135
|
+
if (Object.getOwnPropertyDescriptor(target, prop)) {
|
|
136
|
+
return Reflect.get(target, prop, receiver);
|
|
137
|
+
}
|
|
138
|
+
const value = real[prop];
|
|
139
|
+
if (bindFunctions && typeof value === 'function') {
|
|
140
|
+
return value.bind(real);
|
|
141
|
+
}
|
|
142
|
+
return value;
|
|
143
|
+
},
|
|
144
|
+
|
|
145
|
+
set(target, prop, value, receiver) {
|
|
146
|
+
init();
|
|
147
|
+
if (Object.getOwnPropertyDescriptor(target, prop)) {
|
|
148
|
+
return Reflect.set(target, prop, value, receiver);
|
|
149
|
+
}
|
|
150
|
+
return Reflect.set(real, prop, value);
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
has(target, prop) {
|
|
154
|
+
init();
|
|
155
|
+
return prop in target || prop in real;
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
ownKeys(target) {
|
|
159
|
+
init();
|
|
160
|
+
const keys = new Set([ ...Reflect.ownKeys(real), ...Reflect.ownKeys(target) ]);
|
|
161
|
+
return Array.from(keys);
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
getOwnPropertyDescriptor(target, prop) {
|
|
165
|
+
init();
|
|
166
|
+
return Object.getOwnPropertyDescriptor(target, prop)
|
|
167
|
+
|| Object.getOwnPropertyDescriptor(real, prop);
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
deleteProperty(target, prop) {
|
|
171
|
+
init();
|
|
172
|
+
if (Object.getOwnPropertyDescriptor(target, prop)) {
|
|
173
|
+
return delete target[prop];
|
|
174
|
+
}
|
|
175
|
+
return delete real[prop];
|
|
176
|
+
},
|
|
177
|
+
|
|
178
|
+
getPrototypeOf() {
|
|
179
|
+
init();
|
|
180
|
+
return Object.getPrototypeOf(real);
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
setPrototypeOf(_target, proto) {
|
|
184
|
+
init();
|
|
185
|
+
return Reflect.setPrototypeOf(real, proto);
|
|
186
|
+
},
|
|
187
|
+
|
|
188
|
+
isExtensible() {
|
|
189
|
+
init();
|
|
190
|
+
return Reflect.isExtensible(real);
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
preventExtensions(target) {
|
|
194
|
+
init();
|
|
195
|
+
// Must also prevent extensions on target to satisfy Proxy invariants
|
|
196
|
+
const result = Reflect.preventExtensions(real);
|
|
197
|
+
if (result) {
|
|
198
|
+
Reflect.preventExtensions(target);
|
|
199
|
+
}
|
|
200
|
+
return result;
|
|
201
|
+
},
|
|
202
|
+
|
|
203
|
+
defineProperty(target, prop, descriptor) {
|
|
204
|
+
// Used by monkeypatch libs: keep overrides on proxy target (overlay layer).
|
|
205
|
+
return Reflect.defineProperty(target, prop, descriptor);
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
}
|
package/lib/egg.js
CHANGED
|
@@ -297,18 +297,33 @@ class EggApplication extends EggCore {
|
|
|
297
297
|
*/
|
|
298
298
|
createHttpClient(options = {}) {
|
|
299
299
|
let httpClient;
|
|
300
|
-
options.lookup = options.lookup ?? this.config.httpclient.lookup;
|
|
301
|
-
|
|
302
300
|
if (this.config.httpclient.useHttpClientNext || this.config.httpclient.allowH2) {
|
|
303
|
-
httpClient =
|
|
301
|
+
httpClient = this._createHttpClientNextProxy(options);
|
|
304
302
|
} else if (this.config.httpclient?.enableDNSCache) {
|
|
305
303
|
httpClient = new DNSCacheHttpClient(this, options);
|
|
306
304
|
} else {
|
|
305
|
+
options.lookup = options.lookup ?? this.config.httpclient.lookup;
|
|
307
306
|
httpClient = new this.HttpClient(this, options);
|
|
308
307
|
}
|
|
309
308
|
return httpClient;
|
|
310
309
|
}
|
|
311
310
|
|
|
311
|
+
_createHttpClientNextProxy(options = {}) {
|
|
312
|
+
const self = this;
|
|
313
|
+
let realClient = null;
|
|
314
|
+
const init = () => {
|
|
315
|
+
if (realClient) return;
|
|
316
|
+
options.lookup = options.lookup ?? self.config.httpclient.lookup;
|
|
317
|
+
realClient = new self.HttpClientNext(self, options);
|
|
318
|
+
};
|
|
319
|
+
return utils.createTransparentProxy({
|
|
320
|
+
createReal() {
|
|
321
|
+
init();
|
|
322
|
+
return realClient;
|
|
323
|
+
},
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
|
|
312
327
|
/**
|
|
313
328
|
* HttpClient instance
|
|
314
329
|
* @see https://github.com/node-modules/urllib
|