@zstackui/qiankun 2.6.3-beta-5
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/LICENSE +21 -0
- package/README.md +116 -0
- package/dist/index.umd.js +8681 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/index.umd.min.js +4 -0
- package/dist/index.umd.min.js.map +1 -0
- package/dist/src/__tests__/globalState.test.d.ts +5 -0
- package/dist/src/__tests__/utils.test.d.ts +1 -0
- package/dist/src/addons/engineFlag.d.ts +6 -0
- package/dist/src/addons/index.d.ts +6 -0
- package/dist/src/addons/runtimePublicPath.d.ts +6 -0
- package/dist/src/apis.d.ts +7 -0
- package/dist/src/effects.d.ts +3 -0
- package/dist/src/error.d.ts +3 -0
- package/dist/src/errorHandler.d.ts +7 -0
- package/dist/src/globalState.d.ts +7 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/interfaces.d.ts +107 -0
- package/dist/src/loader.d.ts +8 -0
- package/dist/src/prefetch.d.ts +14 -0
- package/dist/src/sandbox/__tests__/common.test.d.ts +5 -0
- package/dist/src/sandbox/__tests__/proxySandbox.test.d.ts +10 -0
- package/dist/src/sandbox/common.d.ts +21 -0
- package/dist/src/sandbox/index.d.ts +40 -0
- package/dist/src/sandbox/legacy/__tests__/sandbox.test.d.ts +5 -0
- package/dist/src/sandbox/legacy/sandbox.d.ts +28 -0
- package/dist/src/sandbox/patchers/__tests__/css.test.d.ts +5 -0
- package/dist/src/sandbox/patchers/__tests__/interval.test.d.ts +5 -0
- package/dist/src/sandbox/patchers/css.d.ts +17 -0
- package/dist/src/sandbox/patchers/dynamicAppend/__tests__/common.test.d.ts +1 -0
- package/dist/src/sandbox/patchers/dynamicAppend/common.d.ts +22 -0
- package/dist/src/sandbox/patchers/dynamicAppend/forLooseSandbox.d.ts +17 -0
- package/dist/src/sandbox/patchers/dynamicAppend/forStrictSandbox.d.ts +12 -0
- package/dist/src/sandbox/patchers/dynamicAppend/index.d.ts +6 -0
- package/dist/src/sandbox/patchers/historyListener.d.ts +5 -0
- package/dist/src/sandbox/patchers/index.d.ts +9 -0
- package/dist/src/sandbox/patchers/interval.d.ts +5 -0
- package/dist/src/sandbox/patchers/windowListener.d.ts +5 -0
- package/dist/src/sandbox/proxySandbox.d.ts +23 -0
- package/dist/src/sandbox/snapshotSandbox.d.ts +20 -0
- package/dist/src/utils.d.ts +44 -0
- package/dist/src/version.d.ts +1 -0
- package/es/addons/engineFlag.d.ts +6 -0
- package/es/addons/engineFlag.js +50 -0
- package/es/addons/index.d.ts +6 -0
- package/es/addons/index.js +13 -0
- package/es/addons/runtimePublicPath.d.ts +6 -0
- package/es/addons/runtimePublicPath.js +57 -0
- package/es/apis.d.ts +7 -0
- package/es/apis.js +278 -0
- package/es/effects.d.ts +3 -0
- package/es/effects.js +33 -0
- package/es/error.d.ts +3 -0
- package/es/error.js +16 -0
- package/es/errorHandler.d.ts +7 -0
- package/es/errorHandler.js +13 -0
- package/es/globalState.d.ts +7 -0
- package/es/globalState.js +101 -0
- package/es/index.d.ts +11 -0
- package/es/index.js +11 -0
- package/es/interfaces.d.ts +107 -0
- package/es/interfaces.js +8 -0
- package/es/loader.d.ts +8 -0
- package/es/loader.js +600 -0
- package/es/prefetch.d.ts +14 -0
- package/es/prefetch.js +125 -0
- package/es/sandbox/common.d.ts +21 -0
- package/es/sandbox/common.js +157 -0
- package/es/sandbox/index.d.ts +40 -0
- package/es/sandbox/index.js +105 -0
- package/es/sandbox/legacy/sandbox.d.ts +28 -0
- package/es/sandbox/legacy/sandbox.js +142 -0
- package/es/sandbox/patchers/css.d.ts +17 -0
- package/es/sandbox/patchers/css.js +186 -0
- package/es/sandbox/patchers/dynamicAppend/common.d.ts +22 -0
- package/es/sandbox/patchers/dynamicAppend/common.js +306 -0
- package/es/sandbox/patchers/dynamicAppend/forLooseSandbox.d.ts +17 -0
- package/es/sandbox/patchers/dynamicAppend/forLooseSandbox.js +77 -0
- package/es/sandbox/patchers/dynamicAppend/forStrictSandbox.d.ts +12 -0
- package/es/sandbox/patchers/dynamicAppend/forStrictSandbox.js +103 -0
- package/es/sandbox/patchers/dynamicAppend/index.d.ts +6 -0
- package/es/sandbox/patchers/dynamicAppend/index.js +6 -0
- package/es/sandbox/patchers/historyListener.d.ts +5 -0
- package/es/sandbox/patchers/historyListener.js +54 -0
- package/es/sandbox/patchers/index.d.ts +9 -0
- package/es/sandbox/patchers/index.js +45 -0
- package/es/sandbox/patchers/interval.d.ts +5 -0
- package/es/sandbox/patchers/interval.js +34 -0
- package/es/sandbox/patchers/windowListener.d.ts +5 -0
- package/es/sandbox/patchers/windowListener.js +34 -0
- package/es/sandbox/proxySandbox.d.ts +23 -0
- package/es/sandbox/proxySandbox.js +315 -0
- package/es/sandbox/snapshotSandbox.d.ts +20 -0
- package/es/sandbox/snapshotSandbox.js +59 -0
- package/es/utils.d.ts +44 -0
- package/es/utils.js +215 -0
- package/es/version.d.ts +1 -0
- package/es/version.js +1 -0
- package/lib/addons/engineFlag.d.ts +6 -0
- package/lib/addons/engineFlag.js +55 -0
- package/lib/addons/index.d.ts +6 -0
- package/lib/addons/index.js +21 -0
- package/lib/addons/runtimePublicPath.d.ts +6 -0
- package/lib/addons/runtimePublicPath.js +63 -0
- package/lib/apis.d.ts +7 -0
- package/lib/apis.js +288 -0
- package/lib/effects.d.ts +3 -0
- package/lib/effects.js +42 -0
- package/lib/error.d.ts +3 -0
- package/lib/error.js +23 -0
- package/lib/errorHandler.d.ts +7 -0
- package/lib/errorHandler.js +33 -0
- package/lib/globalState.d.ts +7 -0
- package/lib/globalState.js +110 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.js +89 -0
- package/lib/interfaces.d.ts +107 -0
- package/lib/interfaces.js +14 -0
- package/lib/loader.d.ts +8 -0
- package/lib/loader.js +606 -0
- package/lib/prefetch.d.ts +14 -0
- package/lib/prefetch.js +132 -0
- package/lib/sandbox/common.d.ts +21 -0
- package/lib/sandbox/common.js +169 -0
- package/lib/sandbox/index.d.ts +40 -0
- package/lib/sandbox/index.js +123 -0
- package/lib/sandbox/legacy/sandbox.d.ts +28 -0
- package/lib/sandbox/legacy/sandbox.js +148 -0
- package/lib/sandbox/patchers/css.d.ts +17 -0
- package/lib/sandbox/patchers/css.js +193 -0
- package/lib/sandbox/patchers/dynamicAppend/common.d.ts +22 -0
- package/lib/sandbox/patchers/dynamicAppend/common.js +322 -0
- package/lib/sandbox/patchers/dynamicAppend/forLooseSandbox.d.ts +17 -0
- package/lib/sandbox/patchers/dynamicAppend/forLooseSandbox.js +84 -0
- package/lib/sandbox/patchers/dynamicAppend/forStrictSandbox.d.ts +12 -0
- package/lib/sandbox/patchers/dynamicAppend/forStrictSandbox.js +110 -0
- package/lib/sandbox/patchers/dynamicAppend/index.d.ts +6 -0
- package/lib/sandbox/patchers/dynamicAppend/index.js +19 -0
- package/lib/sandbox/patchers/historyListener.d.ts +5 -0
- package/lib/sandbox/patchers/historyListener.js +62 -0
- package/lib/sandbox/patchers/index.d.ts +9 -0
- package/lib/sandbox/patchers/index.js +57 -0
- package/lib/sandbox/patchers/interval.d.ts +5 -0
- package/lib/sandbox/patchers/interval.js +42 -0
- package/lib/sandbox/patchers/windowListener.d.ts +5 -0
- package/lib/sandbox/patchers/windowListener.js +42 -0
- package/lib/sandbox/proxySandbox.d.ts +23 -0
- package/lib/sandbox/proxySandbox.js +321 -0
- package/lib/sandbox/snapshotSandbox.d.ts +20 -0
- package/lib/sandbox/snapshotSandbox.js +65 -0
- package/lib/utils.d.ts +44 -0
- package/lib/utils.js +240 -0
- package/lib/version.d.ts +1 -0
- package/lib/version.js +7 -0
- package/package.json +137 -0
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.default = void 0;
|
|
8
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
9
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
10
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
11
|
+
var _interfaces = require("../interfaces");
|
|
12
|
+
var _utils = require("../utils");
|
|
13
|
+
var _common = require("./common");
|
|
14
|
+
/**
|
|
15
|
+
* fastest(at most time) unique array method
|
|
16
|
+
* @see https://jsperf.com/array-filter-unique/30
|
|
17
|
+
*/
|
|
18
|
+
function uniq(array) {
|
|
19
|
+
return array.filter(function filter(element) {
|
|
20
|
+
return element in this ? false : this[element] = true;
|
|
21
|
+
}, Object.create(null));
|
|
22
|
+
}
|
|
23
|
+
// zone.js will overwrite Object.defineProperty
|
|
24
|
+
var rawObjectDefineProperty = Object.defineProperty;
|
|
25
|
+
var variableWhiteListInDev = process.env.NODE_ENV === 'development' || window.__QIANKUN_DEVELOPMENT__ ? [
|
|
26
|
+
// for react hot reload
|
|
27
|
+
// see https://github.com/facebook/create-react-app/blob/66bf7dfc43350249e2f09d138a20840dae8a0a4a/packages/react-error-overlay/src/index.js#L180
|
|
28
|
+
'__REACT_ERROR_OVERLAY_GLOBAL_HOOK__'] : [];
|
|
29
|
+
// who could escape the sandbox
|
|
30
|
+
var variableWhiteList = [
|
|
31
|
+
// FIXME System.js used a indirect call with eval, which would make it scope escape to global
|
|
32
|
+
// To make System.js works well, we write it back to global window temporary
|
|
33
|
+
// see https://github.com/systemjs/systemjs/blob/457f5b7e8af6bd120a279540477552a07d5de086/src/evaluate.js#L106
|
|
34
|
+
'System',
|
|
35
|
+
// see https://github.com/systemjs/systemjs/blob/457f5b7e8af6bd120a279540477552a07d5de086/src/instantiate.js#L357
|
|
36
|
+
'__cjsWrapper'].concat(variableWhiteListInDev);
|
|
37
|
+
/*
|
|
38
|
+
variables who are impossible to be overwrite need to be escaped from proxy sandbox for performance reasons
|
|
39
|
+
*/
|
|
40
|
+
var unscopables = {
|
|
41
|
+
undefined: true,
|
|
42
|
+
Array: true,
|
|
43
|
+
Object: true,
|
|
44
|
+
String: true,
|
|
45
|
+
Boolean: true,
|
|
46
|
+
Math: true,
|
|
47
|
+
Number: true,
|
|
48
|
+
Symbol: true,
|
|
49
|
+
parseFloat: true,
|
|
50
|
+
Float32Array: true,
|
|
51
|
+
isNaN: true,
|
|
52
|
+
Infinity: true,
|
|
53
|
+
Reflect: true,
|
|
54
|
+
Float64Array: true,
|
|
55
|
+
Function: true,
|
|
56
|
+
Map: true,
|
|
57
|
+
NaN: true,
|
|
58
|
+
Promise: true,
|
|
59
|
+
Proxy: true,
|
|
60
|
+
Set: true,
|
|
61
|
+
parseInt: true,
|
|
62
|
+
requestAnimationFrame: true
|
|
63
|
+
};
|
|
64
|
+
var useNativeWindowForBindingsProps = new Map([['fetch', true], ['mockDomAPIInBlackList', process.env.NODE_ENV === 'test']]);
|
|
65
|
+
function createFakeWindow(globalContext) {
|
|
66
|
+
// map always has the fastest performance in has check scenario
|
|
67
|
+
// see https://jsperf.com/array-indexof-vs-set-has/23
|
|
68
|
+
var propertiesWithGetter = new Map();
|
|
69
|
+
var fakeWindow = {};
|
|
70
|
+
/*
|
|
71
|
+
copy the non-configurable property of global to fakeWindow
|
|
72
|
+
see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/getOwnPropertyDescriptor
|
|
73
|
+
> A property cannot be reported as non-configurable, if it does not exists as an own property of the target object or if it exists as a configurable own property of the target object.
|
|
74
|
+
*/
|
|
75
|
+
Object.getOwnPropertyNames(globalContext).filter(function (p) {
|
|
76
|
+
var descriptor = Object.getOwnPropertyDescriptor(globalContext, p);
|
|
77
|
+
return !(descriptor === null || descriptor === void 0 ? void 0 : descriptor.configurable);
|
|
78
|
+
}).forEach(function (p) {
|
|
79
|
+
var descriptor = Object.getOwnPropertyDescriptor(globalContext, p);
|
|
80
|
+
if (descriptor) {
|
|
81
|
+
var hasGetter = Object.prototype.hasOwnProperty.call(descriptor, 'get');
|
|
82
|
+
/*
|
|
83
|
+
make top/self/window property configurable and writable, otherwise it will cause TypeError while get trap return.
|
|
84
|
+
see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/get
|
|
85
|
+
> The value reported for a property must be the same as the value of the corresponding target object property if the target object property is a non-writable, non-configurable data property.
|
|
86
|
+
*/
|
|
87
|
+
if (p === 'top' || p === 'parent' || p === 'self' || p === 'window' || process.env.NODE_ENV === 'test' && (p === 'mockTop' || p === 'mockSafariTop')) {
|
|
88
|
+
descriptor.configurable = true;
|
|
89
|
+
/*
|
|
90
|
+
The descriptor of window.window/window.top/window.self in Safari/FF are accessor descriptors, we need to avoid adding a data descriptor while it was
|
|
91
|
+
Example:
|
|
92
|
+
Safari/FF: Object.getOwnPropertyDescriptor(window, 'top') -> {get: function, set: undefined, enumerable: true, configurable: false}
|
|
93
|
+
Chrome: Object.getOwnPropertyDescriptor(window, 'top') -> {value: Window, writable: false, enumerable: true, configurable: false}
|
|
94
|
+
*/
|
|
95
|
+
if (!hasGetter) {
|
|
96
|
+
descriptor.writable = true;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (hasGetter) propertiesWithGetter.set(p, true);
|
|
100
|
+
// freeze the descriptor to avoid being modified by zone.js
|
|
101
|
+
// see https://github.com/angular/zone.js/blob/a5fe09b0fac27ac5df1fa746042f96f05ccb6a00/lib/browser/define-property.ts#L71
|
|
102
|
+
rawObjectDefineProperty(fakeWindow, p, Object.freeze(descriptor));
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
return {
|
|
106
|
+
fakeWindow: fakeWindow,
|
|
107
|
+
propertiesWithGetter: propertiesWithGetter
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
var activeSandboxCount = 0;
|
|
111
|
+
/**
|
|
112
|
+
* 基于 Proxy 实现的沙箱
|
|
113
|
+
*/
|
|
114
|
+
var ProxySandbox = exports.default = /*#__PURE__*/function () {
|
|
115
|
+
function ProxySandbox(name) {
|
|
116
|
+
var _this = this;
|
|
117
|
+
var globalContext = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window;
|
|
118
|
+
var transformStorage = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
119
|
+
(0, _classCallCheck2.default)(this, ProxySandbox);
|
|
120
|
+
/** window 值变更记录 */
|
|
121
|
+
this.updatedValueSet = new Set();
|
|
122
|
+
this.sandboxRunning = true;
|
|
123
|
+
this.latestSetProp = null;
|
|
124
|
+
this.name = name;
|
|
125
|
+
this.globalContext = globalContext;
|
|
126
|
+
this.type = _interfaces.SandBoxType.Proxy;
|
|
127
|
+
var updatedValueSet = this.updatedValueSet;
|
|
128
|
+
var _createFakeWindow = createFakeWindow(globalContext),
|
|
129
|
+
fakeWindow = _createFakeWindow.fakeWindow,
|
|
130
|
+
propertiesWithGetter = _createFakeWindow.propertiesWithGetter;
|
|
131
|
+
var descriptorTargetMap = new Map();
|
|
132
|
+
var hasOwnProperty = function hasOwnProperty(key) {
|
|
133
|
+
return fakeWindow.hasOwnProperty(key) || globalContext.hasOwnProperty(key);
|
|
134
|
+
};
|
|
135
|
+
var proxy = new Proxy(fakeWindow, {
|
|
136
|
+
set: function set(target, p, value) {
|
|
137
|
+
if (_this.sandboxRunning) {
|
|
138
|
+
_this.registerRunningApp(name, proxy);
|
|
139
|
+
// We must kept its description while the property existed in globalContext before
|
|
140
|
+
if (!target.hasOwnProperty(p) && globalContext.hasOwnProperty(p)) {
|
|
141
|
+
var descriptor = Object.getOwnPropertyDescriptor(globalContext, p);
|
|
142
|
+
var writable = descriptor.writable,
|
|
143
|
+
configurable = descriptor.configurable,
|
|
144
|
+
enumerable = descriptor.enumerable;
|
|
145
|
+
if (writable) {
|
|
146
|
+
Object.defineProperty(target, p, {
|
|
147
|
+
configurable: configurable,
|
|
148
|
+
enumerable: enumerable,
|
|
149
|
+
writable: writable,
|
|
150
|
+
value: value
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
} else {
|
|
154
|
+
// @ts-ignore
|
|
155
|
+
target[p] = value;
|
|
156
|
+
}
|
|
157
|
+
if (variableWhiteList.indexOf(p) !== -1) {
|
|
158
|
+
// @ts-ignore
|
|
159
|
+
globalContext[p] = value;
|
|
160
|
+
}
|
|
161
|
+
updatedValueSet.add(p);
|
|
162
|
+
_this.latestSetProp = p;
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
if (process.env.NODE_ENV === 'development') {
|
|
166
|
+
console.warn("[qiankun] Set window.".concat(p.toString(), " while sandbox destroyed or inactive in ").concat(name, "!"));
|
|
167
|
+
}
|
|
168
|
+
// 在 strict-mode 下,Proxy 的 handler.set 返回 false 会抛出 TypeError,在沙箱卸载的情况下应该忽略错误
|
|
169
|
+
return true;
|
|
170
|
+
},
|
|
171
|
+
get: function get(target, p) {
|
|
172
|
+
_this.registerRunningApp(name, proxy);
|
|
173
|
+
if (p === Symbol.unscopables) return unscopables;
|
|
174
|
+
// avoid who using window.window or window.self to escape the sandbox environment to touch the really window
|
|
175
|
+
// see https://github.com/eligrey/FileSaver.js/blob/master/src/FileSaver.js#L13
|
|
176
|
+
if (p === 'window' || p === 'self') {
|
|
177
|
+
return proxy;
|
|
178
|
+
}
|
|
179
|
+
// hijack globalWindow accessing with globalThis keyword
|
|
180
|
+
if (p === 'globalThis') {
|
|
181
|
+
return proxy;
|
|
182
|
+
}
|
|
183
|
+
if (p === 'top' || p === 'parent' || process.env.NODE_ENV === 'test' && (p === 'mockTop' || p === 'mockSafariTop')) {
|
|
184
|
+
// if your master app in an iframe context, allow these props escape the sandbox
|
|
185
|
+
if (globalContext === globalContext.parent) {
|
|
186
|
+
return proxy;
|
|
187
|
+
}
|
|
188
|
+
return globalContext[p];
|
|
189
|
+
}
|
|
190
|
+
// proxy.hasOwnProperty would invoke getter firstly, then its value represented as globalContext.hasOwnProperty
|
|
191
|
+
if (p === 'hasOwnProperty') {
|
|
192
|
+
return hasOwnProperty;
|
|
193
|
+
}
|
|
194
|
+
if (p === 'document') {
|
|
195
|
+
return document;
|
|
196
|
+
}
|
|
197
|
+
if (p === 'eval') {
|
|
198
|
+
return eval;
|
|
199
|
+
}
|
|
200
|
+
if (transformStorage && p === 'localStorage') {
|
|
201
|
+
return (0, _common.getZsLocalStorage)(name);
|
|
202
|
+
}
|
|
203
|
+
if (transformStorage && p === 'sessionStorage') {
|
|
204
|
+
return (0, _common.getZsSessionStorage)(name);
|
|
205
|
+
}
|
|
206
|
+
var value = propertiesWithGetter.has(p) ? globalContext[p] : p in target ? target[p] : globalContext[p];
|
|
207
|
+
/* Some dom api must be bound to native window, otherwise it would cause exception like 'TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation'
|
|
208
|
+
See this code:
|
|
209
|
+
const proxy = new Proxy(window, {});
|
|
210
|
+
const proxyFetch = fetch.bind(proxy);
|
|
211
|
+
proxyFetch('https://qiankun.com');
|
|
212
|
+
*/
|
|
213
|
+
var boundTarget = useNativeWindowForBindingsProps.get(p) ? _utils.nativeGlobal : globalContext;
|
|
214
|
+
return (0, _common.getTargetValue)(boundTarget, value);
|
|
215
|
+
},
|
|
216
|
+
// trap in operator
|
|
217
|
+
// see https://github.com/styled-components/styled-components/blob/master/packages/styled-components/src/constants.js#L12
|
|
218
|
+
has: function has(target, p) {
|
|
219
|
+
return p in unscopables || p in target || p in globalContext;
|
|
220
|
+
},
|
|
221
|
+
getOwnPropertyDescriptor: function getOwnPropertyDescriptor(target, p) {
|
|
222
|
+
/*
|
|
223
|
+
as the descriptor of top/self/window/mockTop in raw window are configurable but not in proxy target, we need to get it from target to avoid TypeError
|
|
224
|
+
see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/getOwnPropertyDescriptor
|
|
225
|
+
> A property cannot be reported as non-configurable, if it does not exists as an own property of the target object or if it exists as a configurable own property of the target object.
|
|
226
|
+
*/
|
|
227
|
+
if (target.hasOwnProperty(p)) {
|
|
228
|
+
var descriptor = Object.getOwnPropertyDescriptor(target, p);
|
|
229
|
+
descriptorTargetMap.set(p, 'target');
|
|
230
|
+
return descriptor;
|
|
231
|
+
}
|
|
232
|
+
if (globalContext.hasOwnProperty(p)) {
|
|
233
|
+
var _descriptor = Object.getOwnPropertyDescriptor(globalContext, p);
|
|
234
|
+
descriptorTargetMap.set(p, 'globalContext');
|
|
235
|
+
// A property cannot be reported as non-configurable, if it does not exists as an own property of the target object
|
|
236
|
+
if (_descriptor && !_descriptor.configurable) {
|
|
237
|
+
_descriptor.configurable = true;
|
|
238
|
+
}
|
|
239
|
+
return _descriptor;
|
|
240
|
+
}
|
|
241
|
+
return undefined;
|
|
242
|
+
},
|
|
243
|
+
// trap to support iterator with sandbox
|
|
244
|
+
ownKeys: function ownKeys(target) {
|
|
245
|
+
return uniq(Reflect.ownKeys(globalContext).concat(Reflect.ownKeys(target)));
|
|
246
|
+
},
|
|
247
|
+
defineProperty: function defineProperty(target, p, attributes) {
|
|
248
|
+
var from = descriptorTargetMap.get(p);
|
|
249
|
+
/*
|
|
250
|
+
Descriptor must be defined to native window while it comes from native window via Object.getOwnPropertyDescriptor(window, p),
|
|
251
|
+
otherwise it would cause a TypeError with illegal invocation.
|
|
252
|
+
*/
|
|
253
|
+
switch (from) {
|
|
254
|
+
case 'globalContext':
|
|
255
|
+
return Reflect.defineProperty(globalContext, p, attributes);
|
|
256
|
+
default:
|
|
257
|
+
return Reflect.defineProperty(target, p, attributes);
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
deleteProperty: function deleteProperty(target, p) {
|
|
261
|
+
_this.registerRunningApp(name, proxy);
|
|
262
|
+
if (target.hasOwnProperty(p)) {
|
|
263
|
+
// @ts-ignore
|
|
264
|
+
delete target[p];
|
|
265
|
+
updatedValueSet.delete(p);
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
return true;
|
|
269
|
+
},
|
|
270
|
+
// makes sure `window instanceof Window` returns truthy in micro app
|
|
271
|
+
getPrototypeOf: function getPrototypeOf() {
|
|
272
|
+
return Reflect.getPrototypeOf(globalContext);
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
this.proxy = proxy;
|
|
276
|
+
activeSandboxCount++;
|
|
277
|
+
}
|
|
278
|
+
return (0, _createClass2.default)(ProxySandbox, [{
|
|
279
|
+
key: "registerRunningApp",
|
|
280
|
+
value: function registerRunningApp(name, proxy) {
|
|
281
|
+
if (this.sandboxRunning) {
|
|
282
|
+
var currentRunningApp = (0, _common.getCurrentRunningApp)();
|
|
283
|
+
if (!currentRunningApp || currentRunningApp.name !== name) {
|
|
284
|
+
(0, _common.setCurrentRunningApp)({
|
|
285
|
+
name: name,
|
|
286
|
+
window: proxy
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
// FIXME if you have any other good ideas
|
|
290
|
+
// remove the mark in next tick, thus we can identify whether it in micro app or not
|
|
291
|
+
// this approach is just a workaround, it could not cover all complex cases, such as the micro app runs in the same task context with master in some case
|
|
292
|
+
(0, _utils.nextTask)(function () {
|
|
293
|
+
(0, _common.setCurrentRunningApp)(null);
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}, {
|
|
298
|
+
key: "active",
|
|
299
|
+
value: function active() {
|
|
300
|
+
if (!this.sandboxRunning) activeSandboxCount++;
|
|
301
|
+
this.sandboxRunning = true;
|
|
302
|
+
}
|
|
303
|
+
}, {
|
|
304
|
+
key: "inactive",
|
|
305
|
+
value: function inactive() {
|
|
306
|
+
var _this2 = this;
|
|
307
|
+
if (process.env.NODE_ENV === 'development') {
|
|
308
|
+
console.info("[qiankun:sandbox] ".concat(this.name, " modified global properties restore..."), (0, _toConsumableArray2.default)(this.updatedValueSet.keys()));
|
|
309
|
+
}
|
|
310
|
+
if (--activeSandboxCount === 0) {
|
|
311
|
+
variableWhiteList.forEach(function (p) {
|
|
312
|
+
if (_this2.proxy.hasOwnProperty(p)) {
|
|
313
|
+
// @ts-ignore
|
|
314
|
+
delete _this2.globalContext[p];
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
this.sandboxRunning = false;
|
|
319
|
+
}
|
|
320
|
+
}]);
|
|
321
|
+
}();
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Hydrogen
|
|
3
|
+
* @since 2020-3-8
|
|
4
|
+
*/
|
|
5
|
+
import type { SandBox } from '../interfaces';
|
|
6
|
+
import { SandBoxType } from '../interfaces';
|
|
7
|
+
/**
|
|
8
|
+
* 基于 diff 方式实现的沙箱,用于不支持 Proxy 的低版本浏览器
|
|
9
|
+
*/
|
|
10
|
+
export default class SnapshotSandbox implements SandBox {
|
|
11
|
+
proxy: WindowProxy;
|
|
12
|
+
name: string;
|
|
13
|
+
type: SandBoxType;
|
|
14
|
+
sandboxRunning: boolean;
|
|
15
|
+
private windowSnapshot;
|
|
16
|
+
private modifyPropsMap;
|
|
17
|
+
constructor(name: string);
|
|
18
|
+
active(): void;
|
|
19
|
+
inactive(): void;
|
|
20
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.default = void 0;
|
|
8
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
9
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
10
|
+
var _interfaces = require("../interfaces");
|
|
11
|
+
function iter(obj, callbackFn) {
|
|
12
|
+
// eslint-disable-next-line guard-for-in, no-restricted-syntax
|
|
13
|
+
for (var prop in obj) {
|
|
14
|
+
// patch for clearInterval for compatible reason, see #1490
|
|
15
|
+
if (obj.hasOwnProperty(prop) || prop === 'clearInterval') {
|
|
16
|
+
callbackFn(prop);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 基于 diff 方式实现的沙箱,用于不支持 Proxy 的低版本浏览器
|
|
22
|
+
*/
|
|
23
|
+
var SnapshotSandbox = exports.default = /*#__PURE__*/function () {
|
|
24
|
+
function SnapshotSandbox(name) {
|
|
25
|
+
(0, _classCallCheck2.default)(this, SnapshotSandbox);
|
|
26
|
+
this.sandboxRunning = true;
|
|
27
|
+
this.modifyPropsMap = {};
|
|
28
|
+
this.name = name;
|
|
29
|
+
this.proxy = window;
|
|
30
|
+
this.type = _interfaces.SandBoxType.Snapshot;
|
|
31
|
+
}
|
|
32
|
+
return (0, _createClass2.default)(SnapshotSandbox, [{
|
|
33
|
+
key: "active",
|
|
34
|
+
value: function active() {
|
|
35
|
+
var _this = this;
|
|
36
|
+
// 记录当前快照
|
|
37
|
+
this.windowSnapshot = {};
|
|
38
|
+
iter(window, function (prop) {
|
|
39
|
+
_this.windowSnapshot[prop] = window[prop];
|
|
40
|
+
});
|
|
41
|
+
// 恢复之前的变更
|
|
42
|
+
Object.keys(this.modifyPropsMap).forEach(function (p) {
|
|
43
|
+
window[p] = _this.modifyPropsMap[p];
|
|
44
|
+
});
|
|
45
|
+
this.sandboxRunning = true;
|
|
46
|
+
}
|
|
47
|
+
}, {
|
|
48
|
+
key: "inactive",
|
|
49
|
+
value: function inactive() {
|
|
50
|
+
var _this2 = this;
|
|
51
|
+
this.modifyPropsMap = {};
|
|
52
|
+
iter(window, function (prop) {
|
|
53
|
+
if (window[prop] !== _this2.windowSnapshot[prop]) {
|
|
54
|
+
// 记录变更,恢复环境
|
|
55
|
+
_this2.modifyPropsMap[prop] = window[prop];
|
|
56
|
+
window[prop] = _this2.windowSnapshot[prop];
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
if (process.env.NODE_ENV === 'development') {
|
|
60
|
+
console.info("[qiankun:sandbox] ".concat(this.name, " origin window restore..."), Object.keys(this.modifyPropsMap));
|
|
61
|
+
}
|
|
62
|
+
this.sandboxRunning = false;
|
|
63
|
+
}
|
|
64
|
+
}]);
|
|
65
|
+
}();
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author Kuitos
|
|
3
|
+
* @since 2019-05-15
|
|
4
|
+
*/
|
|
5
|
+
import type { FrameworkConfiguration } from './interfaces';
|
|
6
|
+
export declare function toArray<T>(array: T | T[]): T[];
|
|
7
|
+
export declare function sleep(ms: number): Promise<unknown>;
|
|
8
|
+
/**
|
|
9
|
+
* Run a callback before next task executing, and the invocation is idempotent in every singular task
|
|
10
|
+
* That means even we called nextTask multi times in one task, only the first callback will be pushed to nextTick to be invoked.
|
|
11
|
+
* @param cb
|
|
12
|
+
*/
|
|
13
|
+
export declare function nextTask(cb: () => void): void;
|
|
14
|
+
export declare function isConstructable(fn: () => any | FunctionConstructor): any;
|
|
15
|
+
export declare const isCallable: (fn: any) => boolean;
|
|
16
|
+
export declare function isBoundedFunction(fn: CallableFunction): boolean | undefined;
|
|
17
|
+
export declare function getDefaultTplWrapper(name: string): (tpl: string) => string;
|
|
18
|
+
export declare function getWrapperId(name: string): string;
|
|
19
|
+
export declare const nativeGlobal: any;
|
|
20
|
+
/**
|
|
21
|
+
* Get app instance name with the auto-increment approach
|
|
22
|
+
* @param appName
|
|
23
|
+
*/
|
|
24
|
+
export declare const genAppInstanceIdByName: (appName: string) => string;
|
|
25
|
+
/** 校验子应用导出的 生命周期 对象是否正确 */
|
|
26
|
+
export declare function validateExportLifecycle(exports: any): boolean;
|
|
27
|
+
export declare class Deferred<T> {
|
|
28
|
+
promise: Promise<T>;
|
|
29
|
+
resolve: (value: T | PromiseLike<T>) => void;
|
|
30
|
+
reject: (reason?: any) => void;
|
|
31
|
+
constructor();
|
|
32
|
+
}
|
|
33
|
+
export declare function performanceGetEntriesByName(markName: string, type?: string): PerformanceEntryList | null;
|
|
34
|
+
export declare function performanceMark(markName: string): void;
|
|
35
|
+
export declare function performanceMeasure(measureName: string, markName: string): void;
|
|
36
|
+
export declare function isEnableScopedCSS(sandbox: FrameworkConfiguration['sandbox']): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* copy from https://developer.mozilla.org/zh-CN/docs/Using_XPath
|
|
39
|
+
* @param el
|
|
40
|
+
* @param document
|
|
41
|
+
*/
|
|
42
|
+
export declare function getXPathForElement(el: Node, document: Document): string | void;
|
|
43
|
+
export declare function getContainer(container: string | HTMLElement): HTMLElement | null;
|
|
44
|
+
export declare function getContainerXPath(container?: string | HTMLElement): string | void;
|