@worktile/planet 10.0.0 → 12.1.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.
Files changed (256) hide show
  1. package/.all-contributorsrc +84 -0
  2. package/.circleci/config.yml +17 -0
  3. package/.coveralls.yml +1 -0
  4. package/.docgeni/public/assets/favicon.ico +0 -0
  5. package/.docgeni/public/index.html +13 -0
  6. package/.docgenirc.js +35 -0
  7. package/.dockerignore +1 -0
  8. package/.editorconfig +22 -0
  9. package/.github/FUNDING.yml +12 -0
  10. package/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
  11. package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  12. package/.prettierignore +7 -0
  13. package/.prettierrc +28 -0
  14. package/.travis.yml +26 -0
  15. package/.wpmrc.js +11 -0
  16. package/CHANGELOG.md +391 -0
  17. package/CODE_OF_CONDUCT.md +76 -0
  18. package/Dockerfile +11 -0
  19. package/LICENSE +21 -0
  20. package/README.md +348 -0
  21. package/README.zh-CN.md +307 -0
  22. package/SECURITY.md +21 -0
  23. package/angular.json +373 -0
  24. package/commitlint.config.js +6 -0
  25. package/docs/api/defineApplication.md +46 -0
  26. package/docs/api/globalEventDispatcher.md +51 -0
  27. package/docs/api/index.md +5 -0
  28. package/docs/api/planet.md +117 -0
  29. package/docs/guides/getting-started.md +174 -0
  30. package/docs/guides/index.md +5 -0
  31. package/docs/guides/intro.md +216 -0
  32. package/examples/app1/browserslist +11 -0
  33. package/examples/app1/extra-webpack.config.js +33 -0
  34. package/examples/app1/karma.conf.js +31 -0
  35. package/examples/app1/postcss.config.js +1 -0
  36. package/examples/app1/src/app/app.module.ts +44 -0
  37. package/examples/app1/src/app/app.routing.ts +49 -0
  38. package/examples/app1/src/app/counter.service.ts +16 -0
  39. package/examples/app1/src/app/dashboard/dashboard.component.html +51 -0
  40. package/examples/app1/src/app/dashboard/dashboard.component.ts +70 -0
  41. package/examples/app1/src/app/detail/detail.component.html +8 -0
  42. package/examples/app1/src/app/detail/detail.component.ts +18 -0
  43. package/examples/app1/src/app/overlay.ts +26 -0
  44. package/examples/app1/src/app/projects/dialog/projects-dialog.component.html +10 -0
  45. package/examples/app1/src/app/projects/dialog/projects-dialog.component.ts +41 -0
  46. package/examples/app1/src/app/projects/projects.component.ts +33 -0
  47. package/examples/app1/src/app/root/root.component.css +3 -0
  48. package/examples/app1/src/app/root/root.component.html +16 -0
  49. package/examples/app1/src/app/root/root.component.ts +35 -0
  50. package/examples/app1/src/app/services/initialized-data.resolver.ts +15 -0
  51. package/examples/app1/src/app/shared.module.ts +57 -0
  52. package/examples/app1/src/app/user/detail/user-detail.component.html +5 -0
  53. package/examples/app1/src/app/user/detail/user-detail.component.ts +20 -0
  54. package/examples/app1/src/app/user/user-list.component.html +7 -0
  55. package/examples/app1/src/app/user/user-list.component.ts +20 -0
  56. package/examples/app1/src/app/user/user.module.ts +31 -0
  57. package/examples/app1/src/assets/.gitkeep +0 -0
  58. package/examples/app1/src/assets/github.png +0 -0
  59. package/examples/app1/src/assets/main.css +3 -0
  60. package/examples/app1/src/environments/environment.prod.ts +3 -0
  61. package/examples/app1/src/environments/environment.ts +16 -0
  62. package/examples/app1/src/favicon.ico +0 -0
  63. package/examples/app1/src/index.html +14 -0
  64. package/examples/app1/src/main.ts +35 -0
  65. package/examples/app1/src/polyfills.ts +84 -0
  66. package/examples/app1/src/styles.scss +35 -0
  67. package/examples/app1/src/test.ts +14 -0
  68. package/examples/app1/temp.js +221 -0
  69. package/examples/app1/tsconfig.app.json +10 -0
  70. package/examples/app1/tsconfig.spec.json +18 -0
  71. package/examples/app1/tslint.json +17 -0
  72. package/examples/app1/webpack.config.js +127 -0
  73. package/examples/app2/browserslist +11 -0
  74. package/examples/app2/extra-webpack.config.js +32 -0
  75. package/examples/app2/karma.conf.js +31 -0
  76. package/examples/app2/src/app/app.module.ts +90 -0
  77. package/examples/app2/src/app/dashboard/dashboard.component.html +21 -0
  78. package/examples/app2/src/app/dashboard/dashboard.component.ts +27 -0
  79. package/examples/app2/src/app/overlay.ts +26 -0
  80. package/examples/app2/src/app/projects/detail/detail.component.html +16 -0
  81. package/examples/app2/src/app/projects/detail/detail.component.ts +21 -0
  82. package/examples/app2/src/app/projects/project-list.component.html +29 -0
  83. package/examples/app2/src/app/projects/project-list.component.ts +58 -0
  84. package/examples/app2/src/app/projects/project.resolver.ts +15 -0
  85. package/examples/app2/src/app/projects/projects.service.ts +28 -0
  86. package/examples/app2/src/app/projects/tasks/tasks.component.html +10 -0
  87. package/examples/app2/src/app/projects/tasks/tasks.component.ts +16 -0
  88. package/examples/app2/src/app/projects/view/view.component.html +1 -0
  89. package/examples/app2/src/app/projects/view/view.component.ts +20 -0
  90. package/examples/app2/src/app/root/root.component.html +14 -0
  91. package/examples/app2/src/app/root/root.component.scss +12 -0
  92. package/examples/app2/src/app/root/root.component.ts +28 -0
  93. package/examples/app2/src/app/shared.module.ts +59 -0
  94. package/examples/app2/src/assets/.gitkeep +0 -0
  95. package/examples/app2/src/environments/environment.prod.ts +3 -0
  96. package/examples/app2/src/environments/environment.ts +16 -0
  97. package/examples/app2/src/favicon.ico +0 -0
  98. package/examples/app2/src/index.html +14 -0
  99. package/examples/app2/src/main.ts +36 -0
  100. package/examples/app2/src/polyfills.ts +84 -0
  101. package/examples/app2/src/styles.scss +28 -0
  102. package/examples/app2/src/test.ts +14 -0
  103. package/examples/app2/tsconfig.app.json +10 -0
  104. package/examples/app2/tsconfig.spec.json +18 -0
  105. package/examples/app2/tslint.json +7 -0
  106. package/examples/app2/webpack.config.js +92 -0
  107. package/examples/common/app-root-context.ts +10 -0
  108. package/examples/common/cache.ts +82 -0
  109. package/examples/common/index.ts +13 -0
  110. package/examples/common/module.ts +10 -0
  111. package/examples/common/overlay-container.service.ts +28 -0
  112. package/examples/common/section-card/section-card.component.html +5 -0
  113. package/examples/common/section-card/section-card.component.scss +15 -0
  114. package/examples/common/section-card/section-card.component.ts +11 -0
  115. package/examples/common/utils.ts +0 -0
  116. package/examples/portal/src/app/a-detail/a-detail.component.html +8 -0
  117. package/examples/portal/src/app/a-detail/a-detail.component.ts +18 -0
  118. package/examples/portal/src/app/about/about.component.html +27 -0
  119. package/examples/portal/src/app/about/about.component.scss +0 -0
  120. package/examples/portal/src/app/about/about.component.spec.ts +26 -0
  121. package/examples/portal/src/app/about/about.component.ts +26 -0
  122. package/examples/portal/src/app/app-routing.module.ts +57 -0
  123. package/examples/portal/src/app/app.component.html +73 -0
  124. package/examples/portal/src/app/app.component.scss +58 -0
  125. package/examples/portal/src/app/app.component.spec.ts +33 -0
  126. package/examples/portal/src/app/app.component.ts +111 -0
  127. package/examples/portal/src/app/app.module.ts +64 -0
  128. package/examples/portal/src/app/custom-settings.service.ts +44 -0
  129. package/examples/portal/src/app/overlay.ts +26 -0
  130. package/examples/portal/src/app/settings/settings.component.html +31 -0
  131. package/examples/portal/src/app/settings/settings.component.scss +3 -0
  132. package/examples/portal/src/app/settings/settings.component.ts +43 -0
  133. package/examples/portal/src/assets/.gitkeep +0 -0
  134. package/examples/portal/src/assets/apps.json +29 -0
  135. package/examples/portal/src/assets/icons/sprite.defs.svg +1 -0
  136. package/examples/portal/src/assets/images/logo.svg +16 -0
  137. package/examples/portal/src/assets/ngx-planet-micro-front-end.gif +0 -0
  138. package/examples/portal/src/browserslist +11 -0
  139. package/examples/portal/src/environments/environment.prod.ts +3 -0
  140. package/examples/portal/src/environments/environment.ts +16 -0
  141. package/examples/portal/src/extra-webpack.config.js +32 -0
  142. package/examples/portal/src/favicon.ico +0 -0
  143. package/examples/portal/src/index.html +14 -0
  144. package/examples/portal/src/karma.conf.js +31 -0
  145. package/examples/portal/src/main.ts +18 -0
  146. package/examples/portal/src/polyfills.ts +84 -0
  147. package/examples/portal/src/reboot.scss +14 -0
  148. package/examples/portal/src/styles.scss +14 -0
  149. package/examples/portal/src/test.ts +14 -0
  150. package/examples/portal/src/tsconfig.app.json +10 -0
  151. package/examples/portal/src/tsconfig.spec.json +9 -0
  152. package/examples/portal/src/tslint.json +7 -0
  153. package/extra-webpack.config.js +5 -0
  154. package/nginx.conf +17 -0
  155. package/package.json +120 -15
  156. package/packages/planet/karma.conf.js +37 -0
  157. package/packages/planet/ng-package.json +7 -0
  158. package/packages/planet/package.json +10 -0
  159. package/packages/planet/src/application/planet-application-loader.spec.ts +1102 -0
  160. package/packages/planet/src/application/planet-application-loader.ts +549 -0
  161. package/packages/planet/src/application/planet-application-ref.spec.ts +233 -0
  162. package/packages/planet/src/application/planet-application-ref.ts +131 -0
  163. package/packages/planet/src/application/planet-application.service.spec.ts +145 -0
  164. package/packages/planet/src/application/planet-application.service.ts +88 -0
  165. package/packages/planet/src/application/portal-application.spec.ts +53 -0
  166. package/packages/planet/src/application/portal-application.ts +28 -0
  167. package/packages/planet/src/assets-loader.spec.ts +689 -0
  168. package/packages/planet/src/assets-loader.ts +247 -0
  169. package/packages/planet/src/component/planet-component-loader.spec.ts +187 -0
  170. package/packages/planet/src/component/planet-component-loader.ts +173 -0
  171. package/{component/planet-component-ref.d.ts → packages/planet/src/component/planet-component-ref.ts} +3 -2
  172. package/{component/plant-component.config.d.ts → packages/planet/src/component/plant-component.config.ts} +4 -3
  173. package/packages/planet/src/debug.spec.ts +58 -0
  174. package/packages/planet/src/debug.ts +66 -0
  175. package/packages/planet/src/empty/empty.component.spec.ts +34 -0
  176. package/packages/planet/src/empty/empty.component.ts +7 -0
  177. package/packages/planet/src/global-event-dispatcher.spec.ts +81 -0
  178. package/packages/planet/src/global-event-dispatcher.ts +82 -0
  179. package/packages/planet/src/global-planet.spec.ts +39 -0
  180. package/packages/planet/src/global-planet.ts +73 -0
  181. package/packages/planet/src/helpers.spec.ts +242 -0
  182. package/packages/planet/src/helpers.ts +125 -0
  183. package/packages/planet/src/module.spec.ts +79 -0
  184. package/packages/planet/src/module.ts +25 -0
  185. package/packages/planet/src/planet.class.ts +59 -0
  186. package/packages/planet/src/planet.spec.ts +226 -0
  187. package/packages/planet/src/planet.ts +113 -0
  188. package/{public-api.d.ts → packages/planet/src/public-api.ts} +4 -0
  189. package/packages/planet/src/sandbox/constants.ts +6 -0
  190. package/packages/planet/src/sandbox/exec.ts +22 -0
  191. package/packages/planet/src/sandbox/index.ts +24 -0
  192. package/packages/planet/src/sandbox/proxy/patches/document.ts +13 -0
  193. package/packages/planet/src/sandbox/proxy/patches/eventListener.ts +49 -0
  194. package/packages/planet/src/sandbox/proxy/patches/index.ts +13 -0
  195. package/packages/planet/src/sandbox/proxy/patches/storage.ts +53 -0
  196. package/packages/planet/src/sandbox/proxy/patches/timer.ts +50 -0
  197. package/packages/planet/src/sandbox/proxy/proxies/common.ts +137 -0
  198. package/packages/planet/src/sandbox/proxy/proxies/document.ts +140 -0
  199. package/packages/planet/src/sandbox/proxy/proxies/window.ts +176 -0
  200. package/packages/planet/src/sandbox/proxy/proxy-sandbox.ts +59 -0
  201. package/packages/planet/src/sandbox/proxy/types.ts +20 -0
  202. package/packages/planet/src/sandbox/sandbox.ts +20 -0
  203. package/packages/planet/src/sandbox/snapshot/snapshot-sandbox.ts +12 -0
  204. package/packages/planet/src/sandbox/types.ts +1 -0
  205. package/packages/planet/src/test.ts +16 -0
  206. package/packages/planet/src/testing/app1.module.ts +34 -0
  207. package/packages/planet/src/testing/app2.module.ts +24 -0
  208. package/packages/planet/src/testing/applications.ts +58 -0
  209. package/packages/planet/src/testing/index.ts +2 -0
  210. package/packages/planet/src/testing/utils.ts +31 -0
  211. package/packages/planet/tsconfig.lib.json +27 -0
  212. package/packages/planet/tsconfig.lib.prod.json +9 -0
  213. package/packages/planet/tsconfig.spec.json +17 -0
  214. package/packages/planet/tslint.json +7 -0
  215. package/postcss.config.js +3 -0
  216. package/proxy.conf.js +15 -0
  217. package/tsconfig.json +23 -0
  218. package/tslint.json +80 -0
  219. package/application/planet-application-loader.d.ts +0 -68
  220. package/application/planet-application-ref.d.ts +0 -32
  221. package/application/planet-application.service.d.ts +0 -18
  222. package/application/portal-application.d.ts +0 -14
  223. package/assets-loader.d.ts +0 -23
  224. package/bundles/worktile-planet.umd.js +0 -1670
  225. package/bundles/worktile-planet.umd.js.map +0 -1
  226. package/bundles/worktile-planet.umd.min.js +0 -16
  227. package/bundles/worktile-planet.umd.min.js.map +0 -1
  228. package/component/planet-component-loader.d.ts +0 -27
  229. package/empty/empty.component.d.ts +0 -2
  230. package/esm2015/application/planet-application-loader.js +0 -417
  231. package/esm2015/application/planet-application-ref.js +0 -86
  232. package/esm2015/application/planet-application.service.js +0 -87
  233. package/esm2015/application/portal-application.js +0 -16
  234. package/esm2015/assets-loader.js +0 -165
  235. package/esm2015/component/planet-component-loader.js +0 -157
  236. package/esm2015/component/planet-component-ref.js +0 -3
  237. package/esm2015/component/plant-component.config.js +0 -7
  238. package/esm2015/empty/empty.component.js +0 -10
  239. package/esm2015/global-event-dispatcher.js +0 -71
  240. package/esm2015/global-planet.js +0 -51
  241. package/esm2015/helpers.js +0 -116
  242. package/esm2015/module.js +0 -27
  243. package/esm2015/planet.class.js +0 -9
  244. package/esm2015/planet.js +0 -86
  245. package/esm2015/public-api.js +0 -18
  246. package/esm2015/worktile-planet.js +0 -6
  247. package/fesm2015/worktile-planet.js +0 -1257
  248. package/fesm2015/worktile-planet.js.map +0 -1
  249. package/global-event-dispatcher.d.ts +0 -19
  250. package/global-planet.d.ts +0 -22
  251. package/helpers.d.ts +0 -37
  252. package/module.d.ts +0 -5
  253. package/planet.class.d.ts +0 -34
  254. package/planet.d.ts +0 -24
  255. package/worktile-planet.d.ts +0 -5
  256. package/worktile-planet.metadata.json +0 -1
@@ -0,0 +1,13 @@
1
+ import { ProxyDocument } from '../proxies/document';
2
+ import { SandboxPatchHandler } from '../types';
3
+
4
+ export function documentPatch(): SandboxPatchHandler {
5
+ const proxyDocument = new ProxyDocument();
6
+ const { document, Document } = proxyDocument.create();
7
+ return {
8
+ rewrite: {
9
+ document,
10
+ Document
11
+ }
12
+ };
13
+ }
@@ -0,0 +1,49 @@
1
+ import { SandboxPatchHandler, ProxySandboxInstance } from '../types';
2
+ import { Observable, Subscription } from 'rxjs';
3
+
4
+ export function eventListenerPatch(sandbox: ProxySandboxInstance): SandboxPatchHandler {
5
+ const rawAddEventListener = window.addEventListener;
6
+ const rawRemoveEventListener = window.removeEventListener;
7
+ const listenerSubscriptions = new Map<EventListenerOrEventListenerObject, Subscription>();
8
+
9
+ function addEventListener(
10
+ type: string,
11
+ listener: EventListenerOrEventListenerObject,
12
+ options?: boolean | AddEventListenerOptions
13
+ ) {
14
+ const observable = new Observable(() => {
15
+ rawAddEventListener.call(this, type, listener, options);
16
+ return () => {
17
+ rawRemoveEventListener.call(this, type, listener, options);
18
+ };
19
+ });
20
+ const subscription = observable.subscribe(() => {});
21
+ listenerSubscriptions.set(listener, subscription);
22
+ }
23
+
24
+ function removeEventListener(type: string, listener: EventListenerOrEventListenerObject) {
25
+ const subscription = listenerSubscriptions.get(listener);
26
+ if (subscription) {
27
+ subscription.unsubscribe();
28
+ listenerSubscriptions.delete(listener);
29
+ }
30
+ }
31
+
32
+ return {
33
+ rewrite: {
34
+ addEventListener: addEventListener.bind(window),
35
+ removeEventListener: removeEventListener.bind(window)
36
+ },
37
+ init() {
38
+ const fakeDocument = sandbox.global.document;
39
+ if (fakeDocument) {
40
+ fakeDocument.addEventListener = addEventListener.bind(document);
41
+ fakeDocument.removeEventListener = removeEventListener.bind(document);
42
+ }
43
+ },
44
+ destroy() {
45
+ listenerSubscriptions.forEach(subscription => subscription.unsubscribe());
46
+ listenerSubscriptions.clear();
47
+ }
48
+ };
49
+ }
@@ -0,0 +1,13 @@
1
+ import { ProxySandboxInstance, SandboxPatch, SandboxPatchHandler } from '../types';
2
+ import { eventListenerPatch } from './eventListener';
3
+ import { documentPatch } from './document';
4
+ import { timerPatch } from './timer';
5
+ import { storagePatch } from './storage';
6
+
7
+ const sandboxPatches: SandboxPatch[] = [documentPatch, eventListenerPatch, timerPatch, storagePatch];
8
+
9
+ export function getSandboxPatchHandlers(sandbox: ProxySandboxInstance): SandboxPatchHandler[] {
10
+ return sandboxPatches.map(patch => {
11
+ return patch(sandbox);
12
+ });
13
+ }
@@ -0,0 +1,53 @@
1
+ import { ProxySandboxInstance, SandboxPatchHandler } from '../types';
2
+
3
+ const PLANET_STORAGE_PREFIX = '__planet-storage-';
4
+
5
+ export class RewriteStorage {
6
+ prefix: string;
7
+ rawStorage: Storage;
8
+
9
+ constructor(app: string, rawStorage: Storage) {
10
+ this.rawStorage = rawStorage;
11
+ this.prefix = `${PLANET_STORAGE_PREFIX}${app}__:`;
12
+ }
13
+
14
+ get length() {
15
+ return this.getKeys().length;
16
+ }
17
+
18
+ private getKeys() {
19
+ return Object.keys(this.rawStorage).filter(key => key.startsWith(this.prefix));
20
+ }
21
+
22
+ key(n: number) {
23
+ const key = this.getKeys()[n];
24
+ return key ? key.substring(this.prefix.length) : null;
25
+ }
26
+
27
+ getItem(key: string) {
28
+ return this.rawStorage.getItem(`${this.prefix + key}`);
29
+ }
30
+
31
+ setItem(key: string, value: string) {
32
+ this.rawStorage.setItem(`${this.prefix + key}`, value);
33
+ }
34
+
35
+ removeItem(key: string) {
36
+ this.rawStorage.removeItem(`${this.prefix + key}`);
37
+ }
38
+
39
+ clear() {
40
+ this.getKeys().forEach(key => {
41
+ this.rawStorage.removeItem(key);
42
+ });
43
+ }
44
+ }
45
+
46
+ export function storagePatch(sandbox: ProxySandboxInstance): SandboxPatchHandler {
47
+ return {
48
+ rewrite: {
49
+ localStorage: new RewriteStorage(sandbox.app, localStorage),
50
+ sessionStorage: new RewriteStorage(sandbox.app, sessionStorage)
51
+ }
52
+ };
53
+ }
@@ -0,0 +1,50 @@
1
+ import { SandboxPatchHandler } from '../types';
2
+
3
+ const rawSetTimeout = window.setTimeout;
4
+ const rawClearTimeout = window.clearTimeout;
5
+ const rawSetInterval = window.setInterval;
6
+ const rawClearInterval = window.clearInterval;
7
+
8
+ export function timerPatch(): SandboxPatchHandler {
9
+ const timeout = new Set<number>();
10
+ const interval = new Set<number>();
11
+
12
+ function rewriteSetTimeout(handler: TimerHandler, ms?: number, ...args: any[]) {
13
+ const timeoutId = rawSetTimeout(handler, ms, ...args);
14
+ timeout.add(timeoutId);
15
+ return timeoutId;
16
+ }
17
+
18
+ function rewriteClearTimeout(timeoutId: number) {
19
+ timeout.delete(timeoutId);
20
+ rawClearTimeout(timeoutId);
21
+ }
22
+
23
+ function rewriteSetInterval(handler: TimerHandler, ms: number, ...args: any[]) {
24
+ const intervalId = rawSetInterval(handler, ms, ...args);
25
+ interval.add(intervalId);
26
+ return intervalId;
27
+ }
28
+
29
+ function rewriteClearInterval(intervalId: number) {
30
+ interval.delete(intervalId);
31
+ rawClearInterval(intervalId);
32
+ }
33
+
34
+ return {
35
+ rewrite: {
36
+ setTimeout: rewriteSetTimeout,
37
+ clearTimeout: rewriteClearTimeout,
38
+ setInterval: rewriteSetInterval,
39
+ clearInterval: rewriteClearInterval
40
+ },
41
+ destroy: () => {
42
+ timeout.forEach(timeoutId => {
43
+ rawClearTimeout(timeoutId);
44
+ });
45
+ interval.forEach(intervalId => {
46
+ rawClearInterval(intervalId);
47
+ });
48
+ }
49
+ };
50
+ }
@@ -0,0 +1,137 @@
1
+ import { isObject } from '../../../helpers';
2
+ import { RAW_NODE } from '../../constants';
3
+
4
+ const rawHasOwnProperty = Object.prototype.hasOwnProperty;
5
+ export function hasOwnProp(obj: any, key: PropertyKey): boolean {
6
+ return rawHasOwnProperty.call(obj, key);
7
+ }
8
+
9
+ export function isValueDescriptor(desc?: PropertyDescriptor) {
10
+ if (desc === undefined) {
11
+ return false;
12
+ }
13
+ return 'value' in desc || 'writable' in desc;
14
+ }
15
+
16
+ export function isAccessorDescriptor(desc?: PropertyDescriptor) {
17
+ if (desc === undefined) {
18
+ return false;
19
+ }
20
+ return 'get' in desc || 'set' in desc;
21
+ }
22
+
23
+ export function isNonWriteableValue(desc?: PropertyDescriptor) {
24
+ return desc && desc.configurable === false && 'value' in desc && desc.writable === false;
25
+ }
26
+
27
+ export function isInvalidSetAccessor(desc?: PropertyDescriptor) {
28
+ if (desc && desc.configurable === false && 'set' in desc && desc.set === undefined) {
29
+ return true;
30
+ } else {
31
+ return false;
32
+ }
33
+ }
34
+
35
+ export function isInvalidGetAccessor(desc?: PropertyDescriptor) {
36
+ if (desc && desc.configurable === false && 'get' in desc && desc.get === undefined) {
37
+ return true;
38
+ } else {
39
+ return false;
40
+ }
41
+ }
42
+
43
+ function transferParams(args: IArguments | Array<any>) {
44
+ args = Array.isArray(args) ? args : Array.from(args);
45
+ return args.map(arg => {
46
+ return arg[RAW_NODE] ? arg[RAW_NODE] : arg;
47
+ });
48
+ }
49
+
50
+ const buildInProps: PropertyKey[] = ['length', 'caller', 'callee', 'arguments', 'prototype', Symbol.hasInstance];
51
+ function transferProps(o: Function, n: Function) {
52
+ for (const key of Reflect.ownKeys(o)) {
53
+ if (buildInProps.includes(key)) {
54
+ continue;
55
+ }
56
+ const desc = Object.getOwnPropertyDescriptor(n, key);
57
+ if (desc && desc.writable) {
58
+ n[key] = o[key];
59
+ }
60
+ }
61
+ }
62
+
63
+ export function bind(fn: any, context: any) {
64
+ const fNOP = function() {};
65
+ function bound() {
66
+ const args = transferParams(arguments);
67
+ if (this instanceof bound) {
68
+ const obj = new fn(...args);
69
+ Object.setPrototypeOf(obj, bound.prototype);
70
+ return obj;
71
+ } else {
72
+ return fn.apply(context, args);
73
+ }
74
+ }
75
+ bound.$native = fn;
76
+ transferProps(fn, bound);
77
+ if (fn.prototype) {
78
+ fNOP.prototype = fn.prototype;
79
+ }
80
+ bound.prototype = new fNOP();
81
+ if (Symbol.hasInstance) {
82
+ Object.defineProperty(bound, Symbol.hasInstance, {
83
+ configurable: true,
84
+ value(instance: any) {
85
+ const op = fn.prototype;
86
+ return isObject(op) || typeof op === 'function' ? instance instanceof fn : false;
87
+ }
88
+ });
89
+ }
90
+ return bound;
91
+ }
92
+
93
+ export function createFakeObject(target: Record<PropertyKey, any>, writableKeys?: PropertyKey[]) {
94
+ const fakeObject = {};
95
+ const propertyMap: Record<string, string | boolean> = {};
96
+ const storageBox = Object.create(null);
97
+ const propertyNames = Object.getOwnPropertyNames(target);
98
+ const def = (p: string) => {
99
+ const descriptor = Object.getOwnPropertyDescriptor(target, p);
100
+ if (descriptor?.configurable) {
101
+ const hasGetter = hasOwnProp(descriptor, 'get');
102
+ const hasSetter = hasOwnProp(descriptor, 'set');
103
+ const canWritable = writableKeys && writableKeys.length > 0 && writableKeys.includes(p);
104
+ if (hasGetter) {
105
+ descriptor.get = () => (hasOwnProp(storageBox, p) ? storageBox[p] : target[p]);
106
+ }
107
+ if (hasSetter) {
108
+ descriptor.set = val => {
109
+ storageBox[p] = val;
110
+ return true;
111
+ };
112
+ }
113
+ if (canWritable) {
114
+ if (descriptor.writable === false) {
115
+ descriptor.writable = true;
116
+ } else if (hasGetter) {
117
+ descriptor.set = val => {
118
+ storageBox[p] = val;
119
+ return true;
120
+ };
121
+ }
122
+ }
123
+ Object.defineProperty(fakeObject, p, Object.freeze(descriptor));
124
+ }
125
+ };
126
+ propertyNames.forEach(p => {
127
+ propertyMap[p] = true;
128
+ def(p);
129
+ });
130
+ // 再次循环是为了处理原型链中的属性
131
+ for (const prop in target) {
132
+ if (!propertyMap[prop]) {
133
+ def(prop);
134
+ }
135
+ }
136
+ return fakeObject as any;
137
+ }
@@ -0,0 +1,140 @@
1
+ import { isFunction } from '../../../helpers';
2
+ import { DOCUMENT_BIND_FN, PLANET_SANDBOX_DOCUMENT_WHITELIST, RAW_NODE } from '../../constants';
3
+ import {
4
+ hasOwnProp,
5
+ createFakeObject,
6
+ isInvalidSetAccessor,
7
+ isNonWriteableValue,
8
+ bind,
9
+ isInvalidGetAccessor
10
+ } from './common';
11
+
12
+ const rawDocument = Document;
13
+ const whitelist: PropertyKey[] = ['cookie', 'title', 'location', ...(PLANET_SANDBOX_DOCUMENT_WHITELIST || [])];
14
+
15
+ export class ProxyDocument {
16
+ constructor() {}
17
+
18
+ private createGetter() {
19
+ return (target: any, p: PropertyKey, receiver?: any) => {
20
+ const value = hasOwnProp(target, p) ? Reflect.get(target, p, receiver) : Reflect.get(document, p);
21
+ if (p === 'activeElement') {
22
+ return Reflect.get(document, p);
23
+ }
24
+ // TODO: 处理动态节点
25
+ // if (p === 'createElement') {
26
+ // return function(tagName: string, options: ElementCreationOptions) {
27
+ // const created = value.call(document, tagName, options);
28
+ // if (isObject(created)) {
29
+ // created[SANDBOX_INSTANCE] = sandbox;
30
+ // }
31
+ // return created;
32
+ // };
33
+ // }
34
+ if (isFunction(value)) {
35
+ let newValue = hasOwnProp(value, DOCUMENT_BIND_FN) ? value[DOCUMENT_BIND_FN] : null;
36
+ if (!newValue) {
37
+ newValue = bind(value, document);
38
+ }
39
+ const desc = Object.getOwnPropertyDescriptor(target, p);
40
+ if (desc && isNonWriteableValue(desc)) {
41
+ if (!Object.is(newValue, desc.value)) {
42
+ return value;
43
+ }
44
+ }
45
+ if (isInvalidGetAccessor(desc)) {
46
+ return undefined;
47
+ }
48
+ value[DOCUMENT_BIND_FN] = newValue;
49
+ return newValue;
50
+ }
51
+ return value;
52
+ };
53
+ }
54
+
55
+ private createSetter() {
56
+ return (target: any, p: PropertyKey, value: any, receiver: any) => {
57
+ const desc = Object.getOwnPropertyDescriptor(whitelist.includes(p) ? document : receiver || target, p);
58
+ if (desc && isNonWriteableValue(desc)) {
59
+ if (Object.is(value, desc.value)) {
60
+ return true;
61
+ } else {
62
+ return false;
63
+ }
64
+ }
65
+ if (isInvalidSetAccessor(desc)) {
66
+ return false;
67
+ }
68
+ return whitelist.includes(p) ? Reflect.set(document, p, value) : Reflect.set(target, p, value, receiver);
69
+ };
70
+ }
71
+
72
+ private createDefineProperty() {
73
+ return (target: any, p: PropertyKey, descriptor: PropertyDescriptor) => {
74
+ return whitelist.includes(p)
75
+ ? Reflect.defineProperty(document, p, descriptor)
76
+ : Reflect.defineProperty(target, p, descriptor);
77
+ };
78
+ }
79
+
80
+ create() {
81
+ let proxyDocument = null;
82
+ const fakeDocument = createFakeObject(document);
83
+ const getter = this.createGetter();
84
+ const fakeDocumentProto = new Proxy(fakeDocument, {
85
+ get: (...args) => {
86
+ return getter(...args);
87
+ }
88
+ });
89
+ const fakeDocumentCtor = function Document() {
90
+ if (!(this instanceof fakeDocumentCtor)) {
91
+ throw new TypeError(`Failed to construct 'Document': Please use the 'new' operator.`);
92
+ }
93
+ const docInstance = new rawDocument();
94
+ Object.setPrototypeOf(docInstance, fakeDocument);
95
+ return docInstance;
96
+ };
97
+ fakeDocumentCtor.prototype = fakeDocumentProto;
98
+ fakeDocumentCtor.prototype.constructor = fakeDocumentCtor;
99
+ if (Symbol.hasInstance) {
100
+ Object.defineProperty(fakeDocumentCtor, Symbol.hasInstance, {
101
+ configurable: true,
102
+ value(value: any) {
103
+ let proto = value;
104
+ if (proto === document) {
105
+ return true;
106
+ }
107
+ while ((proto = Object.getPrototypeOf(proto))) {
108
+ if (proto === fakeDocumentProto) {
109
+ return true;
110
+ }
111
+ }
112
+ const cloned = function() {};
113
+ cloned.prototype = fakeDocument;
114
+ return value instanceof cloned;
115
+ }
116
+ });
117
+ }
118
+ proxyDocument = new Proxy(
119
+ Object.create(fakeDocumentProto, {
120
+ currentScript: {
121
+ value: null,
122
+ writable: true
123
+ },
124
+ [RAW_NODE]: {
125
+ writable: false,
126
+ configurable: false,
127
+ value: document
128
+ }
129
+ }),
130
+ {
131
+ set: this.createSetter(),
132
+ defineProperty: this.createDefineProperty()
133
+ }
134
+ );
135
+ return {
136
+ document: proxyDocument,
137
+ Document: fakeDocumentCtor
138
+ };
139
+ }
140
+ }
@@ -0,0 +1,176 @@
1
+ import {
2
+ hasOwnProp,
3
+ createFakeObject,
4
+ bind,
5
+ isInvalidSetAccessor,
6
+ isNonWriteableValue,
7
+ isInvalidGetAccessor
8
+ } from './common';
9
+ import { PLANET_SANDBOX_WINDOW_WHITELIST, SANDBOX_INSTANCE, WINDOW_BIND_FN } from '../../constants';
10
+ import { isFunction } from '../../../helpers';
11
+ import { ProxySandboxInstance } from '../types';
12
+
13
+ const esGlobalFunctions: PropertyKey[] = (
14
+ 'eval,isFinite,isNaN,parseFloat,parseInt,' +
15
+ // URL handling functions
16
+ 'decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,' +
17
+ // Constructor properties of the global object
18
+ 'Array,ArrayBuffer,BigInt,BigInt64Array,BigUint64Array,Boolean,DataView,Date,Error,EvalError,' +
19
+ 'FinalizationRegistry,Float32Array,Float64Array,Function,Int8Array,Int16Array,Int32Array,Map,Number,' +
20
+ 'Object,Promise,Proxy,RangeError,ReferenceError,RegExp,Set,SharedArrayBuffer,String,Symbol,SyntaxError,' +
21
+ 'TypeError,Uint8Array,Uint8ClampedArray,Uint16Array,Uint32Array,URIError,WeakMap,WeakRef,WeakSet,' +
22
+ // Other Properties of the Global Object
23
+ 'Atomics,JSON,Math,Reflect'
24
+ ).split(',');
25
+
26
+ const whitelistVariables = PLANET_SANDBOX_WINDOW_WHITELIST || [];
27
+
28
+ function isConstructor(fn: () => void | FunctionConstructor) {
29
+ const fp = fn.prototype;
30
+ const hasConstructor = fp && fp.constructor === fn && Object.getOwnPropertyNames(fp).length > 1;
31
+ const functionStr = !hasConstructor && fn.toString();
32
+ return hasConstructor || /^function\s+[A-Z]/.test(functionStr as string) || /^class\b/.test(functionStr as string);
33
+ }
34
+
35
+ const unscopables = {
36
+ undefined: true,
37
+ Array: true,
38
+ Object: true,
39
+ String: true,
40
+ Boolean: true,
41
+ Math: true,
42
+ Number: true,
43
+ Symbol: true,
44
+ parseFloat: true,
45
+ Float32Array: true
46
+ };
47
+
48
+ export class ProxyWindow {
49
+ constructor(private sandbox: ProxySandboxInstance) {}
50
+
51
+ private definedVariables = new Set();
52
+
53
+ private createGetter() {
54
+ return (target: Window, key: PropertyKey, receiver: any) => {
55
+ if (key === Symbol.unscopables) {
56
+ return unscopables;
57
+ }
58
+ if (key === SANDBOX_INSTANCE) {
59
+ return this.sandbox;
60
+ }
61
+ let value = null;
62
+ if (whitelistVariables.includes(key)) {
63
+ return Reflect.get(window, key);
64
+ } else {
65
+ value = hasOwnProp(target, key) ? Reflect.get(target, key, receiver) : Reflect.get(window, key);
66
+ }
67
+ if (isFunction(value)) {
68
+ if (
69
+ esGlobalFunctions.includes(key) ||
70
+ whitelistVariables.includes(key) ||
71
+ this.sandbox.rewriteVariables.includes(key) ||
72
+ this.definedVariables.has(key) ||
73
+ isConstructor(value)
74
+ ) {
75
+ return value;
76
+ } else {
77
+ const newValue = hasOwnProp(value, WINDOW_BIND_FN) ? value[WINDOW_BIND_FN] : bind(value, window);
78
+ const desc = Object.getOwnPropertyDescriptor(target, key);
79
+ if (isNonWriteableValue(desc)) {
80
+ return value;
81
+ }
82
+ if (isInvalidGetAccessor(desc)) {
83
+ return undefined;
84
+ }
85
+ value[WINDOW_BIND_FN] = newValue;
86
+ return newValue;
87
+ }
88
+ } else {
89
+ return value;
90
+ }
91
+ };
92
+ }
93
+
94
+ private createSetter() {
95
+ return (target: Window, key: PropertyKey, value: unknown, receiver: any) => {
96
+ const desc = Object.getOwnPropertyDescriptor(
97
+ whitelistVariables.includes(key) ? window : receiver ? receiver : target,
98
+ key
99
+ );
100
+ if (desc && isNonWriteableValue(desc)) {
101
+ if (!Object.is(value, desc.value)) {
102
+ return false;
103
+ } else {
104
+ return true;
105
+ }
106
+ }
107
+ if (isInvalidSetAccessor(desc)) {
108
+ return false;
109
+ }
110
+ if (whitelistVariables.includes(key)) {
111
+ return Reflect.set(window, key, value);
112
+ } else {
113
+ const success = Reflect.set(target, key, value, receiver);
114
+ if (success) {
115
+ if (this.sandbox.running) {
116
+ this.definedVariables.add(key);
117
+ }
118
+ }
119
+ return success;
120
+ }
121
+ };
122
+ }
123
+
124
+ private createDefineProperty() {
125
+ return (target: Window, p: PropertyKey, descriptor: PropertyDescriptor) => {
126
+ if (whitelistVariables.includes(p)) {
127
+ return Reflect.defineProperty(window, p, descriptor);
128
+ } else {
129
+ const success = Reflect.defineProperty(target, p, descriptor);
130
+ if (this.sandbox.running && success) {
131
+ this.definedVariables.add(p);
132
+ }
133
+ return success;
134
+ }
135
+ };
136
+ }
137
+
138
+ private createDeleteProperty() {
139
+ return (target: Window, p: PropertyKey) => {
140
+ if (hasOwnProp(target, p)) {
141
+ delete (target as any)[p];
142
+ if (this.sandbox.running && this.definedVariables.has(p)) {
143
+ this.definedVariables.delete(p);
144
+ }
145
+ }
146
+ return true;
147
+ };
148
+ }
149
+
150
+ private createHas() {
151
+ return (_target: Window, key: PropertyKey) => {
152
+ return !whitelistVariables.includes(key);
153
+ };
154
+ }
155
+
156
+ create() {
157
+ const fakeWindow = createFakeObject(window, this.sandbox.rewriteVariables);
158
+ const baseHandlers = {
159
+ get: this.createGetter(),
160
+ set: this.createSetter(),
161
+ defineProperty: this.createDefineProperty(),
162
+ deleteProperty: this.createDeleteProperty()
163
+ };
164
+ const proxy = new Proxy(fakeWindow, {
165
+ ...baseHandlers,
166
+ has: this.createHas()
167
+ });
168
+ const subProxy = new Proxy(fakeWindow, baseHandlers);
169
+ proxy.self = subProxy;
170
+ proxy.window = subProxy;
171
+ proxy.globalThis = subProxy;
172
+ proxy.top = window.top === window ? subProxy : window.top;
173
+ proxy.parent = window.parent === window ? subProxy : window.top;
174
+ return proxy;
175
+ }
176
+ }
@@ -0,0 +1,59 @@
1
+ import { ProxyWindow } from './proxies/window';
2
+ import { getSandboxPatchHandlers } from './patches';
3
+ import { SandboxPatchHandler } from './types';
4
+ import { Global } from '../types';
5
+ import { Sandbox, SandboxOptions } from '../sandbox';
6
+
7
+ export class ProxySandbox extends Sandbox {
8
+ public running = false;
9
+
10
+ public global!: Global;
11
+
12
+ public rewriteVariables!: PropertyKey[];
13
+
14
+ private patchHandlers: SandboxPatchHandler[] = [];
15
+
16
+ constructor(public app: string, public options: SandboxOptions) {
17
+ super();
18
+ this.patchHandlers = getSandboxPatchHandlers(this);
19
+ this.start();
20
+ }
21
+
22
+ start() {
23
+ this.running = true;
24
+ this.rewriteVariables = this.getPatchRewriteVariables();
25
+ const proxyWindow = new ProxyWindow(this);
26
+ this.global = proxyWindow.create();
27
+ this.execPatchHandlers();
28
+ }
29
+
30
+ destroy() {
31
+ this.running = false;
32
+ this.patchHandlers.forEach(handler => {
33
+ if (handler.destroy) {
34
+ handler.destroy();
35
+ }
36
+ });
37
+ }
38
+
39
+ private getPatchRewriteVariables() {
40
+ return this.patchHandlers.reduce<string[]>((pre, cur) => {
41
+ return [...pre, ...(cur.rewrite ? Object.keys(cur.rewrite) : [])];
42
+ }, []);
43
+ }
44
+
45
+ private execPatchHandlers() {
46
+ this.patchHandlers.forEach(handler => {
47
+ if (handler.rewrite) {
48
+ for (const key in handler.rewrite) {
49
+ if (handler.rewrite[key]) {
50
+ this.global[key] = handler.rewrite[key];
51
+ }
52
+ }
53
+ }
54
+ if (handler.init) {
55
+ handler?.init();
56
+ }
57
+ });
58
+ }
59
+ }