@jsenv/plugin-toolbar 0.0.1 → 0.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 (165) hide show
  1. package/dist/css/{toolbar_main.css → toolbar.css} +285 -319
  2. package/dist/html/toolbar.html +91 -80
  3. package/dist/js/toolbar_core.js +1792 -0
  4. package/dist/js/toolbar_injector.js +60 -121
  5. package/dist/jsenv_plugin_toolbar.js +18 -12
  6. package/package.json +10 -3
  7. package/src/client/core/animation_actions.js +8 -0
  8. package/src/client/core/animation_effects.js +12 -0
  9. package/src/client/core/animation_signals.js +12 -0
  10. package/src/client/core/changes_actions.js +9 -0
  11. package/src/client/core/changes_signals.js +3 -0
  12. package/src/client/core/execution_actions.js +50 -0
  13. package/src/client/core/execution_effects.js +22 -0
  14. package/src/client/core/execution_signals.js +19 -0
  15. package/src/client/core/notification_actions.js +71 -0
  16. package/src/client/core/notification_context.js +1 -0
  17. package/src/client/core/notification_effects.js +11 -0
  18. package/src/client/core/notification_signals.js +13 -0
  19. package/src/client/{communication/parent_window_communication.js → core/parent_window_actions.js} +10 -0
  20. package/src/client/core/parent_window_context.js +7 -0
  21. package/src/client/core/parent_window_effects.js +20 -0
  22. package/src/client/core/parent_window_signals.js +34 -0
  23. package/src/client/core/server_actions.js +9 -0
  24. package/src/client/core/server_effects.js +11 -0
  25. package/src/client/core/server_signals.js +12 -0
  26. package/src/client/core/settings_actions.js +9 -0
  27. package/src/client/core/settings_signals.js +3 -0
  28. package/src/client/core/theme_actions.js +9 -0
  29. package/src/client/core/theme_effects.js +8 -0
  30. package/src/client/core/theme_signals.js +12 -0
  31. package/src/client/core/toolbar_logger.js +29 -0
  32. package/src/client/core/toolbar_open_actions.js +9 -0
  33. package/src/client/core/toolbar_open_effects.js +5 -0
  34. package/src/client/core/toolbar_open_signals.js +12 -0
  35. package/src/client/core/toolbar_state_context.js +5 -0
  36. package/src/client/core/toolbar_state_effects.js +10 -0
  37. package/src/client/core/toolbar_state_signals.js +20 -0
  38. package/src/client/core/tooltip_actions.js +7 -0
  39. package/src/client/toolbar.html +116 -89
  40. package/src/client/toolbar_core.js +8 -0
  41. package/src/client/toolbar_injector.js +64 -39
  42. package/src/client/ui/changes_indicator/changes_indicator.js +66 -0
  43. package/src/client/{execution/execution.css → ui/document_execution_indicator/document_execution_indicator.css} +10 -14
  44. package/src/client/ui/document_execution_indicator/document_execution_indicator.js +66 -0
  45. package/src/client/ui/document_index_link/document_index_link.js +8 -0
  46. package/src/client/ui/globals.css +103 -0
  47. package/src/client/{jsenv_logo.svg → ui/jsenv_logo.svg} +0 -0
  48. package/src/client/{eventsource/eventsource.css → ui/server_indicator/server_indicator.css} +14 -24
  49. package/src/client/ui/server_indicator/server_indicator.js +62 -0
  50. package/src/client/ui/toolbar.css +76 -0
  51. package/src/client/ui/toolbar_close_button/toolbar_close_button.js +15 -0
  52. package/src/client/{responsive/overflow_menu.css → ui/toolbar_menu_overflow/toolbar_menu_overflow.css} +10 -11
  53. package/src/client/{responsive/toolbar_responsive.js → ui/toolbar_menu_overflow/toolbar_menu_overflow.js} +25 -31
  54. package/src/client/ui/toolbar_opening/toolbar_opening.js +73 -0
  55. package/src/client/ui/toolbar_overlay/toolbar_overlay.js +76 -0
  56. package/src/client/{theme → ui/toolbar_settings}/jsenv_theme.css +6 -6
  57. package/src/client/{theme/light_theme.css → ui/toolbar_settings/theme_light.css} +9 -10
  58. package/src/client/ui/toolbar_settings/toolbar_animation_setting.js +27 -0
  59. package/src/client/ui/toolbar_settings/toolbar_autoreload_setting.js +79 -0
  60. package/src/client/ui/toolbar_settings/toolbar_notification_setting.js +88 -0
  61. package/src/client/{settings/settings.css → ui/toolbar_settings/toolbar_settings.css} +24 -23
  62. package/src/client/ui/toolbar_settings/toolbar_settings.js +54 -0
  63. package/src/client/ui/toolbar_settings/toolbar_theme_setting.js +17 -0
  64. package/src/client/ui/toolbar_ui.js +21 -0
  65. package/src/client/{tooltip/tooltip.css → ui/tooltips/tooltips.css} +13 -7
  66. package/src/client/{util → ui/util}/animation.js +0 -0
  67. package/src/client/ui/util/dom.js +57 -0
  68. package/src/client/{util → ui/util}/fetch_using_xhr.js +0 -0
  69. package/src/client/{util → ui/util}/fetching.js +0 -0
  70. package/src/client/{util → ui/util}/iframe_to_parent_href.js +0 -0
  71. package/src/client/{util → ui/util}/responsive.js +0 -0
  72. package/src/client/{util → ui/util}/util.js +0 -0
  73. package/src/client/{variant → ui}/variant.js +0 -0
  74. package/src/jsenv_plugin_toolbar.js +19 -7
  75. package/dist/js/toolbar_main.js +0 -900
  76. package/scripts/build.mjs +0 -20
  77. package/src/client/animation/toolbar_animation_core.js +0 -30
  78. package/src/client/animation/toolbar_animation_ui.js +0 -21
  79. package/src/client/eventsource/toolbar_eventsource.js +0 -57
  80. package/src/client/execution/toolbar_execution.js +0 -88
  81. package/src/client/focus/focus.css +0 -61
  82. package/src/client/focus/toolbar_focus.js +0 -19
  83. package/src/client/notification/toolbar_notification.js +0 -181
  84. package/src/client/settings/toolbar_settings.js +0 -47
  85. package/src/client/theme/toolbar_theme.js +0 -34
  86. package/src/client/toolbar_main.css +0 -173
  87. package/src/client/toolbar_main.js +0 -174
  88. package/src/client/toolbar_state.js +0 -16
  89. package/src/client/tooltip/tooltip.js +0 -39
  90. package/src/client/util/dom.js +0 -108
  91. package/src/client/util/jsenv_logger.js +0 -28
  92. package/src/client/util/preferences.js +0 -10
  93. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/ast/src/html/html_src_set.js +0 -18
  94. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/animation/toolbar_animation.js +0 -45
  95. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/animation/toolbar_animation.js.map +0 -12
  96. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/animation/toolbar_animation_core.js +0 -32
  97. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/animation/toolbar_animation_core.js.map +0 -12
  98. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/animation/toolbar_animation_ui.js +0 -23
  99. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/animation/toolbar_animation_ui.js.map +0 -12
  100. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/communication/parent_window_communication.js +0 -33
  101. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/eventsource/eventsource.css +0 -87
  102. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/eventsource/toolbar_eventsource.js +0 -59
  103. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/eventsource/toolbar_eventsource.js.map +0 -12
  104. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/execution/execution.css +0 -88
  105. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/execution/toolbar_execution.js +0 -90
  106. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/execution/toolbar_execution.js.map +0 -12
  107. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/focus/focus.css +0 -28
  108. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/focus/toolbar_focus.js +0 -19
  109. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/jsenv_logo.svg +0 -140
  110. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/notification/toolbar_notification.js +0 -183
  111. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/notification/toolbar_notification.js.map +0 -12
  112. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/responsive/overflow_menu.css +0 -60
  113. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/responsive/toolbar_responsive.js +0 -105
  114. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/responsive/toolbar_responsive.js.map +0 -12
  115. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/settings/settings.css +0 -200
  116. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/settings/toolbar_settings.js +0 -49
  117. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/settings/toolbar_settings.js.map +0 -12
  118. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/theme/light_theme.css +0 -110
  119. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/theme/toolbar_theme.js +0 -36
  120. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/theme/toolbar_theme.js.map +0 -12
  121. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/toolbar.html +0 -319
  122. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/toolbar_injector.js +0 -232
  123. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/toolbar_injector.js.map +0 -12
  124. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/toolbar_main.css +0 -161
  125. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/toolbar_main.css.map +0 -12
  126. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/toolbar_main.js +0 -176
  127. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/toolbar_main.js.map +0 -12
  128. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/toolbar_state.js +0 -18
  129. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/toolbar_state.js.map +0 -12
  130. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/tooltip/tooltip.css +0 -60
  131. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/tooltip/tooltip.js +0 -41
  132. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/tooltip/tooltip.js.map +0 -12
  133. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/util/animation.js +0 -307
  134. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/util/animation.js.map +0 -12
  135. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/util/dom.js +0 -108
  136. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/util/iframe_to_parent_href.js +0 -10
  137. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/util/preferences.js +0 -10
  138. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/util/responsive.js +0 -112
  139. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/packages/jsenv-plugin-toolbar/src/client/variant/variant.js +0 -74
  140. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/autoreload/client/autoreload.js +0 -201
  141. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/autoreload/client/autoreload.js.map +0 -12
  142. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/autoreload/client/autoreload_preference.js +0 -11
  143. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/autoreload/client/reload.js +0 -138
  144. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/autoreload/client/reload.js.map +0 -12
  145. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/autoreload/client/url_helpers.js +0 -20
  146. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/explorer/client/explorer.html +0 -590
  147. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/explorer/client/explorer.html@L15C5-L20C12.js +0 -7
  148. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/explorer/client/explorer.html@L15C5-L20C12.js.map +0 -12
  149. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/explorer/client/explorer.html@L368C5-L481C14.js +0 -114
  150. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/explorer/client/explorer.html@L482C5-L605C14.js +0 -124
  151. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/explorer/client/explorer.html@L69C5-L299C13.css +0 -227
  152. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/explorer/client/explorer.html@L8C5-L68C13.css +0 -28
  153. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/import_meta_hot/client/import_meta_hot.js +0 -82
  154. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/ribbon/client/ribbon.js +0 -51
  155. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/server_events/client/connection_manager.js +0 -165
  156. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/server_events/client/events_manager.js +0 -75
  157. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/server_events/client/server_events_client.js +0 -20
  158. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/server_events/client/server_events_client.js.map +0 -12
  159. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/server_events/client/web_socket_connection.js +0 -83
  160. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/server_events/client/web_socket_connection.js.map +0 -12
  161. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/supervisor/client/script_type_module_supervisor.js +0 -98
  162. package/tests/toolbar_basic/.jsenv/chrome@107.00/@fs/Users/d.maillard/dev/jsenv/jsenv-core/src/plugins/supervisor/client/supervisor.js +0 -966
  163. package/tests/toolbar_basic/main.html +0 -18
  164. package/tests/toolbar_basic/start_dev_server.mjs +0 -7
  165. package/todo.txt +0 -0
@@ -1,966 +0,0 @@
1
- window.__supervisor__ = (() => {
2
- const notImplemented = () => {
3
- throw new Error(`window.__supervisor__.setup() not called`)
4
- }
5
- const executionResults = {}
6
- const supervisor = {
7
- reportError: notImplemented,
8
- superviseScript: notImplemented,
9
- reloadSupervisedScript: notImplemented,
10
- getDocumentExecutionResult: notImplemented,
11
- executionResults,
12
- }
13
-
14
- let navigationStartTime
15
- try {
16
- navigationStartTime = window.performance.timing.navigationStart
17
- } catch (e) {
18
- navigationStartTime = Date.now()
19
- }
20
-
21
- supervisor.setupReportException = ({
22
- rootDirectoryUrl,
23
- errorNotification,
24
- errorOverlay,
25
- errorBaseUrl,
26
- openInEditor,
27
- }) => {
28
- const DYNAMIC_IMPORT_FETCH_ERROR = "dynamic_import_fetch_error"
29
- const DYNAMIC_IMPORT_EXPORT_MISSING = "dynamic_import_export_missing"
30
- const DYNAMIC_IMPORT_SYNTAX_ERROR = "dynamic_import_syntax_error"
31
-
32
- const createException = ({
33
- reason,
34
- reportedBy,
35
- url,
36
- line,
37
- column,
38
- } = {}) => {
39
- const exception = {
40
- reason,
41
- reportedBy,
42
- isError: false, // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/throw
43
- code: null,
44
- message: null,
45
- stack: null,
46
- stackFormatIsV8: null,
47
- stackSourcemapped: null,
48
- originalStack: null,
49
- meta: null,
50
- site: {
51
- isInline: null,
52
- url: null,
53
- line: null,
54
- column: null,
55
- originalUrl: null,
56
- },
57
- }
58
-
59
- const writeBasicProperties = () => {
60
- if (reason === undefined) {
61
- exception.message = "undefined"
62
- return
63
- }
64
- if (reason === null) {
65
- exception.message = "null"
66
- return
67
- }
68
- if (typeof reason === "string") {
69
- exception.message = reason
70
- return
71
- }
72
- if (reason instanceof Error) {
73
- const error = reason
74
- let message = error.message
75
- exception.isError = true
76
- if (Error.captureStackTrace) {
77
- // stackTrace formatted by V8
78
- exception.message = message
79
- exception.stack = getErrorStackWithoutErrorMessage(error)
80
- exception.stackFormatIsV8 = true
81
- exception.stackSourcemapped = true
82
- } else {
83
- exception.message = message
84
- exception.stack = error.stack ? ` ${error.stack}` : null
85
- exception.stackFormatIsV8 = false
86
- exception.stackSourcemapped = false
87
- }
88
- if (error.reportedBy) {
89
- exception.reportedBy = error.reportedBy
90
- }
91
- if (error.url) {
92
- Object.assign(exception.site, resolveUrlSite({ url: error.url }))
93
- }
94
- if (error.needsReport) {
95
- exception.needsReport = true
96
- }
97
- export_missing: {
98
- // chrome
99
- if (message.includes("does not provide an export named")) {
100
- exception.code = DYNAMIC_IMPORT_EXPORT_MISSING
101
- return
102
- }
103
- // firefox
104
- if (
105
- message.startsWith("import not found:") ||
106
- message.startsWith("ambiguous indirect export:")
107
- ) {
108
- exception.code = DYNAMIC_IMPORT_EXPORT_MISSING
109
- return
110
- }
111
- // safari
112
- if (message.startsWith("import binding name")) {
113
- exception.code = DYNAMIC_IMPORT_EXPORT_MISSING
114
- return
115
- }
116
- }
117
- js_syntax_error: {
118
- if (error.name === "SyntaxError" && typeof line === "number") {
119
- exception.code = DYNAMIC_IMPORT_SYNTAX_ERROR
120
- return
121
- }
122
- }
123
- return
124
- }
125
- if (typeof reason === "object") {
126
- exception.code = reason.code
127
- exception.message = reason.message
128
- exception.stack = reason.stack
129
- if (reason.reportedBy) {
130
- exception.reportedBy = reason.reportedBy
131
- }
132
- if (reason.url) {
133
- Object.assign(exception.site, resolveUrlSite({ url: reason.url }))
134
- }
135
- if (reason.needsReport) {
136
- exception.needsReport = true
137
- }
138
- return
139
- }
140
- exception.message = JSON.stringify(reason)
141
- }
142
- writeBasicProperties()
143
-
144
- // first create a version of the stack with file://
145
- // (and use it to locate exception url+line+column)
146
- if (exception.stack) {
147
- exception.originalStack = exception.stack
148
- exception.stack = replaceUrls(
149
- exception.originalStack,
150
- (serverUrlSite) => {
151
- const fileUrlSite = resolveUrlSite(serverUrlSite)
152
- if (exception.site.url === null) {
153
- Object.assign(exception.site, fileUrlSite)
154
- }
155
- return stringifyUrlSite(fileUrlSite)
156
- },
157
- )
158
- }
159
- // then if it fails, use url+line+column passed
160
- if (exception.site.url === null && url) {
161
- if (typeof line === "string") {
162
- line = parseInt(line)
163
- }
164
- if (typeof column === "string") {
165
- column = parseInt(column)
166
- }
167
- const fileUrlSite = resolveUrlSite({ url, line, column })
168
- if (
169
- fileUrlSite.isInline &&
170
- exception.code === DYNAMIC_IMPORT_SYNTAX_ERROR
171
- ) {
172
- // syntax error on inline script need line-1 for some reason
173
- if (Error.captureStackTrace) {
174
- fileUrlSite.line--
175
- } else {
176
- // firefox and safari need line-2
177
- fileUrlSite.line -= 2
178
- }
179
- }
180
- Object.assign(exception.site, fileUrlSite)
181
- }
182
- exception.text = stringifyMessageAndStack(exception)
183
- return exception
184
- }
185
-
186
- const stringifyMessageAndStack = ({ message, stack }) => {
187
- if (message && stack) {
188
- return `${message}\n${stack}`
189
- }
190
- if (stack) {
191
- return stack
192
- }
193
- return message
194
- }
195
-
196
- const stringifyUrlSite = ({ url, line, column }) => {
197
- if (typeof line === "number" && typeof column === "number") {
198
- return `${url}:${line}:${column}`
199
- }
200
- if (typeof line === "number") {
201
- return `${url}:${line}`
202
- }
203
- return url
204
- }
205
-
206
- const resolveUrlSite = ({ url, line, column }) => {
207
- const inlineUrlMatch = url.match(
208
- /@L([0-9]+)C([0-9]+)\-L([0-9]+)C([0-9]+)(\.[\w]+)$/,
209
- )
210
- if (inlineUrlMatch) {
211
- const htmlUrl = url.slice(0, inlineUrlMatch.index)
212
- const tagLineStart = parseInt(inlineUrlMatch[1])
213
- const tagColumnStart = parseInt(inlineUrlMatch[2])
214
- const tagLineEnd = parseInt(inlineUrlMatch[3])
215
- const tagColumnEnd = parseInt(inlineUrlMatch[4])
216
- const extension = inlineUrlMatch[5]
217
- url = htmlUrl
218
- line = tagLineStart + (typeof line === "number" ? line : 0)
219
- // stackTrace formatted by V8 (chrome)
220
- if (Error.captureStackTrace) {
221
- line--
222
- }
223
- column = tagColumnStart + (typeof column === "number" ? column : 0)
224
- const fileUrl = resolveFileUrl(url)
225
- return {
226
- isInline: true,
227
- serverUrl: url,
228
- originalUrl: `${fileUrl}@L${tagLineStart}C${tagColumnStart}-L${tagLineEnd}C${tagColumnEnd}${extension}`,
229
- url: fileUrl,
230
- line,
231
- column,
232
- }
233
- }
234
- return {
235
- isInline: false,
236
- serverUrl: url,
237
- url: resolveFileUrl(url),
238
- line,
239
- column,
240
- }
241
- }
242
-
243
- const getErrorStackWithoutErrorMessage = (error) => {
244
- let stack = error.stack
245
- if (!stack) return ""
246
- const messageInStack = `${error.name}: ${error.message}`
247
- if (stack.startsWith(messageInStack)) {
248
- stack = stack.slice(messageInStack.length)
249
- }
250
- const nextLineIndex = stack.indexOf("\n")
251
- if (nextLineIndex > -1) {
252
- stack = stack.slice(nextLineIndex + 1)
253
- }
254
- return stack
255
- }
256
-
257
- const resolveFileUrl = (url) => {
258
- let urlObject = new URL(url)
259
- if (urlObject.origin === window.origin) {
260
- urlObject = new URL(
261
- `${urlObject.pathname.slice(1)}${urlObject.search}`,
262
- rootDirectoryUrl,
263
- )
264
- }
265
- if (urlObject.href.startsWith("file:")) {
266
- const atFsIndex = urlObject.pathname.indexOf("/@fs/")
267
- if (atFsIndex > -1) {
268
- const afterAtFs = urlObject.pathname.slice(atFsIndex + "/@fs/".length)
269
- return new URL(afterAtFs, "file:///").href
270
- }
271
- }
272
- return urlObject.href
273
- }
274
-
275
- // `Error: yo
276
- // at Object.execute (http://127.0.0.1:57300/build/src/__test__/file-throw.js:9:13)
277
- // at doExec (http://127.0.0.1:3000/src/__test__/file-throw.js:452:38)
278
- // at postOrderExec (http://127.0.0.1:3000/src/__test__/file-throw.js:448:16)
279
- // at http://127.0.0.1:3000/src/__test__/file-throw.js:399:18`.replace(/(?:https?|ftp|file):\/\/(.*+)$/gm, (...args) => {
280
- // debugger
281
- // })
282
- const replaceUrls = (source, replace) => {
283
- return source.replace(/(?:https?|ftp|file):\/\/\S+/gm, (match) => {
284
- let replacement = ""
285
- const lastChar = match[match.length - 1]
286
-
287
- // hotfix because our url regex sucks a bit
288
- const endsWithSeparationChar = lastChar === ")" || lastChar === ":"
289
- if (endsWithSeparationChar) {
290
- match = match.slice(0, -1)
291
- }
292
-
293
- const lineAndColumnPattern = /:([0-9]+):([0-9]+)$/
294
- const lineAndColumMatch = match.match(lineAndColumnPattern)
295
- if (lineAndColumMatch) {
296
- const lineAndColumnString = lineAndColumMatch[0]
297
- const lineString = lineAndColumMatch[1]
298
- const columnString = lineAndColumMatch[2]
299
- replacement = replace({
300
- url: match.slice(0, -lineAndColumnString.length),
301
- line: lineString ? parseInt(lineString) : null,
302
- column: columnString ? parseInt(columnString) : null,
303
- })
304
- } else {
305
- const linePattern = /:([0-9]+)$/
306
- const lineMatch = match.match(linePattern)
307
- if (lineMatch) {
308
- const lineString = lineMatch[0]
309
- replacement = replace({
310
- url: match.slice(0, -lineString.length),
311
- line: lineString ? parseInt(lineString) : null,
312
- })
313
- } else {
314
- replacement = replace({
315
- url: match,
316
- })
317
- }
318
- }
319
- if (endsWithSeparationChar) {
320
- return `${replacement}${lastChar}`
321
- }
322
- return replacement
323
- })
324
- }
325
-
326
- let formatError
327
- error_formatter: {
328
- formatError = (exceptionInfo) => {
329
- const errorParts = {
330
- theme: "dark",
331
- title: "An error occured",
332
- text: "",
333
- tip: "",
334
- errorDetailsPromise: null,
335
- }
336
- const tips = []
337
- tips.push("Click outside to close.")
338
- errorParts.tip = tips.join(`\n <br />\n `)
339
-
340
- const generateClickableText = (text) => {
341
- const textWithHtmlLinks = makeLinksClickable(text, {
342
- createLink: ({ url, line, column }) => {
343
- const urlSite = resolveUrlSite({ url, line, column })
344
- if (errorBaseUrl) {
345
- if (urlSite.url.startsWith(rootDirectoryUrl)) {
346
- urlSite.url = `${errorBaseUrl}${urlSite.url.slice(
347
- rootDirectoryUrl.length,
348
- )}`
349
- } else {
350
- urlSite.url = "file:///mocked_for_snapshots"
351
- }
352
- }
353
- const urlWithLineAndColumn = stringifyUrlSite(urlSite)
354
- return {
355
- href:
356
- urlSite.url.startsWith("file:") && openInEditor
357
- ? `javascript:window.fetch('/__open_in_editor__/${encodeURIComponent(
358
- urlWithLineAndColumn,
359
- )}')`
360
- : urlSite.url,
361
- text: urlWithLineAndColumn,
362
- }
363
- },
364
- })
365
- return textWithHtmlLinks
366
- }
367
-
368
- errorParts.text = stringifyMessageAndStack({
369
- message: exceptionInfo.message
370
- ? generateClickableText(exceptionInfo.message)
371
- : "",
372
- stack: exceptionInfo.stack
373
- ? generateClickableText(exceptionInfo.stack)
374
- : "",
375
- })
376
- if (exceptionInfo.site.url) {
377
- errorParts.errorDetailsPromise = (async () => {
378
- try {
379
- if (
380
- exceptionInfo.code === DYNAMIC_IMPORT_FETCH_ERROR ||
381
- exceptionInfo.reportedBy === "script_error_event"
382
- ) {
383
- const response = await window.fetch(
384
- `/__get_error_cause__/${encodeURIComponent(
385
- exceptionInfo.site.isInline
386
- ? exceptionInfo.site.originalUrl
387
- : exceptionInfo.site.url,
388
- )}`,
389
- )
390
- if (response.status !== 200) {
391
- return null
392
- }
393
- const causeInfo = await response.json()
394
- if (!causeInfo) {
395
- return null
396
- }
397
- const causeText =
398
- causeInfo.code === "NOT_FOUND"
399
- ? stringifyMessageAndStack({
400
- message: generateClickableText(causeInfo.reason),
401
- stack: generateClickableText(causeInfo.codeFrame),
402
- })
403
- : stringifyMessageAndStack({
404
- message: generateClickableText(causeInfo.stack),
405
- stack: generateClickableText(causeInfo.codeFrame),
406
- })
407
- return {
408
- cause: causeText,
409
- }
410
- }
411
- if (
412
- exceptionInfo.site.line !== undefined &&
413
- // code frame showing internal window.reportError is pointless
414
- !exceptionInfo.site.url.endsWith(
415
- `script_type_module_supervisor.js`,
416
- )
417
- ) {
418
- const urlToFetch = new URL(
419
- `/__get_code_frame__/${encodeURIComponent(
420
- stringifyUrlSite(exceptionInfo.site),
421
- )}`,
422
- window.origin,
423
- )
424
- if (!exceptionInfo.stackSourcemapped) {
425
- urlToFetch.searchParams.set("remap", "")
426
- }
427
- const response = await window.fetch(urlToFetch)
428
- if (response.status !== 200) {
429
- return null
430
- }
431
- const codeFrame = await response.text()
432
- return {
433
- codeFrame: generateClickableText(codeFrame),
434
- }
435
- }
436
- } catch (e) {
437
- // happens if server is closed for instance
438
- return null
439
- }
440
- return null
441
- })()
442
- }
443
- return errorParts
444
- }
445
-
446
- const makeLinksClickable = (
447
- string,
448
- { createLink = ({ url }) => url },
449
- ) => {
450
- // normalize line breaks
451
- string = string.replace(/\n/g, "\n")
452
- string = escapeHtml(string)
453
- // render links
454
- string = replaceUrls(string, ({ url, line, column }) => {
455
- const { href, text } = createLink({ url, line, column })
456
- return link({ href, text })
457
- })
458
- return string
459
- }
460
-
461
- const escapeHtml = (string) => {
462
- return string
463
- .replace(/&/g, "&amp;")
464
- .replace(/</g, "&lt;")
465
- .replace(/>/g, "&gt;")
466
- .replace(/"/g, "&quot;")
467
- .replace(/'/g, "&#039;")
468
- }
469
-
470
- const link = ({ href, text = href }) => `<a href="${href}">${text}</a>`
471
- }
472
-
473
- let displayErrorNotification
474
- error_notification: {
475
- const { Notification } = window
476
- displayErrorNotification =
477
- typeof Notification === "function"
478
- ? ({ title, text, icon }) => {
479
- if (Notification.permission === "granted") {
480
- const notification = new Notification(title, {
481
- lang: "en",
482
- body: text,
483
- icon,
484
- })
485
- notification.onclick = () => {
486
- window.focus()
487
- }
488
- }
489
- }
490
- : () => {}
491
- }
492
-
493
- const JSENV_ERROR_OVERLAY_TAGNAME = "jsenv-error-overlay"
494
- let displayJsenvErrorOverlay
495
- error_overlay: {
496
- displayJsenvErrorOverlay = (params) => {
497
- let jsenvErrorOverlay = new JsenvErrorOverlay(params)
498
- document
499
- .querySelectorAll(JSENV_ERROR_OVERLAY_TAGNAME)
500
- .forEach((node) => {
501
- node.parentNode.removeChild(node)
502
- })
503
- document.body.appendChild(jsenvErrorOverlay)
504
- const removeErrorOverlay = () => {
505
- if (jsenvErrorOverlay && jsenvErrorOverlay.parentNode) {
506
- document.body.removeChild(jsenvErrorOverlay)
507
- jsenvErrorOverlay = null
508
- }
509
- }
510
- return removeErrorOverlay
511
- }
512
-
513
- class JsenvErrorOverlay extends HTMLElement {
514
- constructor({ theme, title, text, tip, errorDetailsPromise }) {
515
- super()
516
- this.root = this.attachShadow({ mode: "open" })
517
- this.root.innerHTML = `
518
- <style>
519
- ${overlayCSS}
520
- </style>
521
- <div class="backdrop"></div>
522
- <div class="overlay" data-theme=${theme}>
523
- <h1 class="title">
524
- ${title}
525
- </h1>
526
- <pre class="text">${text}</pre>
527
- <div class="tip">
528
- ${tip}
529
- </div>
530
- </div>`
531
- this.root.querySelector(".backdrop").onclick = () => {
532
- if (!this.parentNode) {
533
- // not in document anymore
534
- return
535
- }
536
- this.root.querySelector(".backdrop").onclick = null
537
- this.parentNode.removeChild(this)
538
- }
539
- if (errorDetailsPromise) {
540
- errorDetailsPromise.then((errorDetails) => {
541
- if (!errorDetails || !this.parentNode) {
542
- return
543
- }
544
- const { codeFrame, cause } = errorDetails
545
- if (codeFrame) {
546
- this.root.querySelector(".text").innerHTML += `\n\n${codeFrame}`
547
- }
548
- if (cause) {
549
- const causeIndented = prefixRemainingLines(cause, " ")
550
- this.root.querySelector(
551
- ".text",
552
- ).innerHTML += `\n [cause]: ${causeIndented}`
553
- }
554
- })
555
- }
556
- }
557
- }
558
-
559
- const prefixRemainingLines = (text, prefix) => {
560
- const lines = text.split(/\r?\n/)
561
- const firstLine = lines.shift()
562
- let result = firstLine
563
- let i = 0
564
- while (i < lines.length) {
565
- const line = lines[i]
566
- i++
567
- result += line.length ? `\n${prefix}${line}` : `\n`
568
- }
569
- return result
570
- }
571
-
572
- if (customElements && !customElements.get(JSENV_ERROR_OVERLAY_TAGNAME)) {
573
- customElements.define(JSENV_ERROR_OVERLAY_TAGNAME, JsenvErrorOverlay)
574
- }
575
-
576
- const overlayCSS = `
577
- :host {
578
- position: fixed;
579
- z-index: 99999;
580
- top: 0;
581
- left: 0;
582
- width: 100%;
583
- height: 100%;
584
- overflow-y: scroll;
585
- margin: 0;
586
- background: rgba(0, 0, 0, 0.66);
587
- }
588
-
589
- .backdrop {
590
- position: absolute;
591
- left: 0;
592
- right: 0;
593
- top: 0;
594
- bottom: 0;
595
- }
596
-
597
- .overlay {
598
- position: relative;
599
- background: rgba(0, 0, 0, 0.95);
600
- width: 800px;
601
- margin: 30px auto;
602
- padding: 25px 40px;
603
- padding-top: 0;
604
- overflow: hidden; /* for h1 margins */
605
- border-radius: 4px 8px;
606
- box-shadow: 0 20px 40px rgb(0 0 0 / 30%), 0 15px 12px rgb(0 0 0 / 20%);
607
- box-sizing: border-box;
608
- font-family: monospace;
609
- direction: ltr;
610
- }
611
-
612
- h1 {
613
- color: red;
614
- text-align: center;
615
- }
616
-
617
- pre {
618
- overflow: auto;
619
- max-width: 100%;
620
- /* padding is nice + prevents scrollbar from hiding the text behind it */
621
- /* does not work nicely on firefox though https://bugzilla.mozilla.org/show_bug.cgi?id=748518 */
622
- padding: 20px;
623
- }
624
-
625
- .tip {
626
- border-top: 1px solid #999;
627
- padding-top: 12px;
628
- }
629
-
630
- [data-theme="dark"] {
631
- color: #999;
632
- }
633
- [data-theme="dark"] pre {
634
- background: #111;
635
- border: 1px solid #333;
636
- color: #eee;
637
- }
638
-
639
- [data-theme="light"] {
640
- color: #EEEEEE;
641
- }
642
- [data-theme="light"] pre {
643
- background: #1E1E1E;
644
- border: 1px solid white;
645
- color: #EEEEEE;
646
- }
647
-
648
- pre a {
649
- color: inherit;
650
- }`
651
- }
652
-
653
- supervisor.createException = createException
654
- supervisor.reportException = (exception) => {
655
- const { theme, title, text, tip, errorDetailsPromise } =
656
- formatError(exception)
657
-
658
- if (errorOverlay) {
659
- const removeErrorOverlay = displayJsenvErrorOverlay({
660
- theme,
661
- title,
662
- text,
663
- tip,
664
- errorDetailsPromise,
665
- })
666
- if (window.__reloader__) {
667
- window.__reloader__.onstatuschange = () => {
668
- if (window.__reloader__.status === "reloading") {
669
- removeErrorOverlay()
670
- }
671
- }
672
- }
673
- }
674
- if (errorNotification) {
675
- displayErrorNotification({
676
- title,
677
- text,
678
- })
679
- }
680
- return exception
681
- }
682
- window.addEventListener("error", (errorEvent) => {
683
- if (!errorEvent.isTrusted) {
684
- // ignore custom error event (not sent by browser)
685
- return
686
- }
687
- const { error, message, filename, lineno, colno } = errorEvent
688
- const exception = supervisor.createException({
689
- // when error is reported within a worker error is null
690
- // but there is a message property on errorEvent
691
- reason: error || message,
692
- reportedBy: "window_error_event",
693
- url: filename,
694
- line: lineno,
695
- column: colno,
696
- })
697
- supervisor.reportException(exception)
698
- })
699
- window.addEventListener("unhandledrejection", (event) => {
700
- if (event.defaultPrevented) {
701
- return
702
- }
703
- const exception = supervisor.createException({
704
- reason: event.reason,
705
- reportedBy: "window_unhandledrejection_event",
706
- })
707
- supervisor.reportException(exception)
708
- })
709
- }
710
-
711
- supervisor.setup = ({
712
- rootDirectoryUrl,
713
- logs,
714
- errorOverlay,
715
- errorBaseUrl,
716
- openInEditor,
717
- }) => {
718
- supervisor.setupReportException({
719
- rootDirectoryUrl,
720
- errorOverlay,
721
- errorBaseUrl,
722
- openInEditor,
723
- })
724
-
725
- const supervisedScripts = []
726
- const pendingPromises = []
727
- // respect execution order
728
- // - wait for classic scripts to be done (non async)
729
- // - wait module script previous execution (non async)
730
- // see https://gist.github.com/jakub-g/385ee6b41085303a53ad92c7c8afd7a6#typemodule-vs-non-module-typetextjavascript-vs-script-nomodule
731
- const executionQueue = []
732
- let executing = false
733
- const addToExecutionQueue = async (execution) => {
734
- if (execution.async) {
735
- execution.start()
736
- return
737
- }
738
- if (executing) {
739
- executionQueue.push(execution)
740
- return
741
- }
742
- startThenDequeue(execution)
743
- }
744
- const startThenDequeue = async (execution) => {
745
- executing = true
746
- const promise = execution.start()
747
- await promise
748
- executing = false
749
- if (executionQueue.length) {
750
- const nextExecution = executionQueue.shift()
751
- startThenDequeue(nextExecution)
752
- }
753
- }
754
- supervisor.addExecution = ({ type, src, async, execute }) => {
755
- const execution = {
756
- type,
757
- src,
758
- async,
759
- execute,
760
- }
761
- execution.start = () => {
762
- return superviseExecution(execution, { isReload: false })
763
- }
764
- execution.reload = () => {
765
- return superviseExecution(execution, { isReload: true })
766
- }
767
- supervisedScripts.push(execution)
768
- return addToExecutionQueue(execution)
769
- }
770
- const superviseExecution = async (execution, { isReload }) => {
771
- if (logs) {
772
- console.group(`[jsenv] loading ${execution.type} ${execution.src}`)
773
- }
774
- const executionResult = {
775
- status: "pending",
776
- loadDuration: null,
777
- executionDuration: null,
778
- duration: null,
779
- exception: null,
780
- namespace: null,
781
- coverage: null,
782
- }
783
- executionResults[execution.src] = executionResult
784
-
785
- const monitorScriptLoad = () => {
786
- const loadStartTime = Date.now()
787
- let resolveScriptLoadPromise
788
- const scriptLoadPromise = new Promise((resolve) => {
789
- resolveScriptLoadPromise = resolve
790
- })
791
- pendingPromises.push(scriptLoadPromise)
792
- return () => {
793
- const loadEndTime = Date.now()
794
- executionResult.loadDuration = loadEndTime - loadStartTime
795
- resolveScriptLoadPromise()
796
- }
797
- }
798
- const monitorScriptExecution = () => {
799
- const executionStartTime = Date.now()
800
- let resolveExecutionPromise
801
- const executionPromise = new Promise((resolve) => {
802
- resolveExecutionPromise = resolve
803
- })
804
- pendingPromises.push(executionPromise)
805
- return () => {
806
- executionResult.coverage = window.__coverage__
807
- executionResult.executionDuration = Date.now() - executionStartTime
808
- executionResult.duration =
809
- executionResult.loadDuration + executionResult.executionDuration
810
- resolveExecutionPromise()
811
- }
812
- }
813
-
814
- const onError = (e) => {
815
- executionResult.status = "errored"
816
- const exception = supervisor.createException({ reason: e })
817
- if (exception.needsReport) {
818
- supervisor.reportException(exception)
819
- }
820
- executionResult.exception = exception
821
- }
822
-
823
- const scriptLoadDone = monitorScriptLoad()
824
- try {
825
- const result = await execution.execute({ isReload })
826
- if (logs) {
827
- console.log(`${execution.type} load ended`)
828
- console.groupEnd()
829
- }
830
- executionResult.status = "loaded"
831
- scriptLoadDone()
832
-
833
- const scriptExecutionDone = monitorScriptExecution()
834
- if (execution.type === "js_classic") {
835
- executionResult.status = "completed"
836
- scriptExecutionDone()
837
- } else if (execution.type === "js_module") {
838
- result.executionPromise.then(
839
- (namespace) => {
840
- executionResult.status = "completed"
841
- executionResult.namespace = namespace
842
- scriptExecutionDone()
843
- },
844
- (e) => {
845
- onError(e)
846
- scriptExecutionDone()
847
- },
848
- )
849
- }
850
- } catch (e) {
851
- if (logs) {
852
- console.groupEnd()
853
- }
854
- onError(e)
855
- scriptLoadDone()
856
- }
857
- }
858
-
859
- supervisor.superviseScript = async ({ src, async }) => {
860
- const { currentScript } = document
861
- const parentNode = currentScript.parentNode
862
- let nodeToReplace
863
- let currentScriptClone
864
- return supervisor.addExecution({
865
- src,
866
- type: "js_classic",
867
- async,
868
- execute: async ({ isReload }) => {
869
- const urlObject = new URL(src, window.location)
870
- const loadPromise = new Promise((resolve, reject) => {
871
- // do not use script.cloneNode()
872
- // bcause https://stackoverflow.com/questions/28771542/why-dont-clonenode-script-tags-execute
873
- currentScriptClone = document.createElement("script")
874
- // browsers set async by default when creating script(s)
875
- // we want an exact copy to preserves how code is executed
876
- currentScriptClone.async = false
877
- Array.from(currentScript.attributes).forEach((attribute) => {
878
- currentScriptClone.setAttribute(
879
- attribute.nodeName,
880
- attribute.nodeValue,
881
- )
882
- })
883
- if (isReload) {
884
- urlObject.searchParams.set("hmr", Date.now())
885
- nodeToReplace = currentScriptClone
886
- currentScriptClone.src = urlObject.href
887
- } else {
888
- currentScriptClone.removeAttribute("jsenv-cooked-by")
889
- currentScriptClone.removeAttribute("jsenv-inlined-by")
890
- currentScriptClone.removeAttribute("jsenv-injected-by")
891
- currentScriptClone.removeAttribute("inlined-from-src")
892
- currentScriptClone.removeAttribute("original-position")
893
- currentScriptClone.removeAttribute("original-src-position")
894
- nodeToReplace = currentScript
895
- currentScriptClone.src = src
896
- }
897
- currentScriptClone.addEventListener("error", reject)
898
- currentScriptClone.addEventListener("load", resolve)
899
- parentNode.replaceChild(currentScriptClone, nodeToReplace)
900
- })
901
- try {
902
- await loadPromise
903
- } catch (e) {
904
- // eslint-disable-next-line no-throw-literal
905
- throw {
906
- message: `Failed to fetch script: ${urlObject.href}`,
907
- reportedBy: "script_error_event",
908
- url: urlObject.href,
909
- // window.error won't be dispatched for this error
910
- needsReport: true,
911
- }
912
- }
913
- },
914
- })
915
- }
916
- supervisor.reloadSupervisedScript = ({ type, src }) => {
917
- const supervisedScript = supervisedScripts.find(
918
- (supervisedScriptCandidate) => {
919
- if (type && supervisedScriptCandidate.type !== type) {
920
- return false
921
- }
922
- return supervisedScriptCandidate.src === src
923
- },
924
- )
925
- if (supervisedScript) {
926
- supervisedScript.reload()
927
- }
928
- }
929
- supervisor.getDocumentExecutionResult = async () => {
930
- // just to be super safe and ensure any <script type="module"> got a chance to execute
931
- const documentReadyPromise = new Promise((resolve) => {
932
- if (document.readyState === "complete") {
933
- resolve()
934
- return
935
- }
936
- const loadCallback = () => {
937
- window.removeEventListener("load", loadCallback)
938
- resolve()
939
- }
940
- window.addEventListener("load", loadCallback)
941
- })
942
- await documentReadyPromise
943
- const waitScriptExecutions = async () => {
944
- const numberOfPromises = pendingPromises.length
945
- await Promise.all(pendingPromises)
946
- // new scripts added while the other where executing
947
- // (should happen only on webkit where
948
- // script might be added after window load event)
949
- await new Promise((resolve) => setTimeout(resolve))
950
- if (pendingPromises.length > numberOfPromises) {
951
- await waitScriptExecutions()
952
- }
953
- }
954
- await waitScriptExecutions()
955
-
956
- return {
957
- status: "completed",
958
- executionResults,
959
- startTime: navigationStartTime,
960
- endTime: Date.now(),
961
- }
962
- }
963
- }
964
-
965
- return supervisor
966
- })()