emailengine-app 2.78.0 → 2.79.0

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 (142) hide show
  1. package/.ncurc.js +23 -13
  2. package/.release-please-manifest.json +1 -1
  3. package/CHANGELOG.md +29 -0
  4. package/config/e2e.toml +2 -1
  5. package/config/test.toml +2 -1
  6. package/data/google-crawlers.json +1 -1
  7. package/lib/account.js +70 -35
  8. package/lib/api-reference/format.js +91 -4
  9. package/lib/api-reference/model.js +144 -19
  10. package/lib/api-reference/schema-tree.js +282 -28
  11. package/lib/api-routes/account-routes.js +80 -34
  12. package/lib/api-routes/blocklist-routes.js +21 -47
  13. package/lib/api-routes/chat-routes.js +10 -12
  14. package/lib/api-routes/delivery-test-routes.js +6 -26
  15. package/lib/api-routes/export-routes.js +2 -0
  16. package/lib/api-routes/gateway-routes.js +52 -11
  17. package/lib/api-routes/license-routes.js +2 -0
  18. package/lib/api-routes/mailbox-routes.js +29 -26
  19. package/lib/api-routes/message-routes.js +27 -21
  20. package/lib/api-routes/oauth2-app-routes.js +11 -29
  21. package/lib/api-routes/operation-impact.js +80 -0
  22. package/lib/api-routes/outbox-routes.js +15 -5
  23. package/lib/api-routes/permission-map.js +397 -0
  24. package/lib/api-routes/route-helpers.js +223 -9
  25. package/lib/api-routes/route-metadata.js +28 -0
  26. package/lib/api-routes/settings-routes.js +30 -25
  27. package/lib/api-routes/stats-routes.js +2 -2
  28. package/lib/api-routes/submit-routes.js +53 -31
  29. package/lib/api-routes/template-routes.js +3 -0
  30. package/lib/api-routes/token-routes.js +319 -33
  31. package/lib/api-routes/webhook-route-routes.js +14 -4
  32. package/lib/auth-token.js +80 -8
  33. package/lib/consts.js +10 -0
  34. package/lib/db.js +20 -2
  35. package/lib/delivery-error.js +30 -1
  36. package/lib/email-client/base-client.js +27 -41
  37. package/lib/email-client/gmail-client.js +19 -10
  38. package/lib/email-client/imap/mailbox.js +23 -24
  39. package/lib/email-client/imap/subconnection.js +10 -7
  40. package/lib/email-client/imap/sync-operations.js +46 -46
  41. package/lib/email-client/imap-client.js +36 -26
  42. package/lib/email-client/message-builder.js +3 -3
  43. package/lib/email-client/notification-handler.js +1 -1
  44. package/lib/email-client/outlook/graph-api.js +10 -4
  45. package/lib/email-client/outlook-client.js +53 -54
  46. package/lib/email-client/smtp-pool-manager.js +1 -1
  47. package/lib/enum-descriptions.js +92 -2
  48. package/lib/export.js +1 -1
  49. package/lib/field-usage.js +133 -0
  50. package/lib/gateway.js +1 -1
  51. package/lib/imapproxy/imap-core/lib/commands/authenticate-plain.js +3 -3
  52. package/lib/imapproxy/imap-core/lib/commands/id.js +2 -13
  53. package/lib/imapproxy/imap-core/lib/commands/starttls.js +1 -1
  54. package/lib/imapproxy/imap-core/lib/imap-command.js +21 -5
  55. package/lib/imapproxy/imap-core/lib/imap-connection.js +15 -5
  56. package/lib/imapproxy/imap-server.js +27 -7
  57. package/lib/license-beacon.js +4 -9
  58. package/lib/lists.js +49 -1
  59. package/lib/llm-pre-process.js +5 -5
  60. package/lib/logger.js +5 -1
  61. package/lib/metrics-collector.js +2 -2
  62. package/lib/oauth/gmail.js +9 -12
  63. package/lib/oauth/mail-ru.js +5 -7
  64. package/lib/oauth/outlook.js +9 -9
  65. package/lib/oauth/pubsub/google.js +25 -10
  66. package/lib/oauth/scope-checker.js +1 -1
  67. package/lib/oauth2-apps.js +223 -294
  68. package/lib/openapi/build-document.js +15 -7
  69. package/lib/openapi/joi-schema.js +20 -0
  70. package/lib/outbox.js +85 -17
  71. package/lib/pre-process.js +8 -8
  72. package/lib/queue-stats.js +49 -0
  73. package/lib/read-env-value.js +1 -1
  74. package/lib/reconnection-manager.js +2 -2
  75. package/lib/rewrite-text-nodes.js +4 -3
  76. package/lib/routes-ui.js +4 -0
  77. package/lib/schemas.js +216 -160
  78. package/lib/settings.js +84 -5
  79. package/lib/sub-script.js +2 -1
  80. package/lib/swagger-options.js +2 -1
  81. package/lib/templates.js +4 -4
  82. package/lib/threads.js +2 -2
  83. package/lib/token-audit-log.js +208 -0
  84. package/lib/token-permission-view.js +219 -0
  85. package/lib/token-permissions.js +172 -0
  86. package/lib/tokens.js +99 -15
  87. package/lib/tools.js +142 -37
  88. package/lib/ui-routes/account-routes.js +31 -11
  89. package/lib/ui-routes/admin-config-routes.js +18 -10
  90. package/lib/ui-routes/admin-entities-routes.js +66 -22
  91. package/lib/ui-routes/auth-routes.js +30 -24
  92. package/lib/ui-routes/export-routes.js +10 -4
  93. package/lib/ui-routes/oauth-config-routes.js +5 -5
  94. package/lib/ui-routes/reference-routes.js +3 -3
  95. package/lib/ui-routes/route-helpers.js +51 -2
  96. package/lib/ui-routes/settings-page.js +1 -1
  97. package/lib/ui-routes/smtp-test-routes.js +1 -1
  98. package/lib/ui-routes/suppression-list-routes.js +294 -0
  99. package/lib/ui-routes/unsubscribe-routes.js +19 -29
  100. package/lib/utils/network.js +2 -2
  101. package/lib/web-safe-html.js +1 -1
  102. package/lib/webhook-routing.js +80 -0
  103. package/lib/webhooks.js +20 -14
  104. package/package.json +11 -10
  105. package/sbom.json +1 -1
  106. package/server.js +80 -41
  107. package/static/css/flyonui.css +1 -1
  108. package/static/css/src/app.tailwind.css +136 -9
  109. package/static/js/reference.js +456 -10
  110. package/static/js/ui.js +43 -0
  111. package/static/licenses.html +51 -41
  112. package/translations/messages.pot +59 -59
  113. package/views/accounts/account.hbs +69 -7
  114. package/views/accounts/browse.hbs +18 -35
  115. package/views/config/logging.hbs +6 -0
  116. package/views/config/oauth/app.hbs +0 -2
  117. package/views/config/security.hbs +13 -0
  118. package/views/partials/oauth_form.hbs +0 -15
  119. package/views/partials/reference/disclosure.hbs +8 -2
  120. package/views/partials/reference/operation.hbs +47 -13
  121. package/views/partials/reference/params.hbs +17 -1
  122. package/views/partials/reference/schema-node.hbs +110 -52
  123. package/views/partials/reference/schema-tools.hbs +25 -0
  124. package/views/partials/reference/try-it.hbs +84 -11
  125. package/views/partials/side_menu.hbs +1 -0
  126. package/views/partials/ui/checkbox.hbs +2 -2
  127. package/views/partials/ui/tabs.hbs +7 -1
  128. package/views/reference/token.hbs +4 -0
  129. package/views/suppression-lists/index.hbs +85 -0
  130. package/views/suppression-lists/list.hbs +87 -0
  131. package/views/templates/index.hbs +8 -2
  132. package/views/tokens/index.hbs +31 -4
  133. package/views/tokens/new.hbs +324 -2
  134. package/workers/api.js +146 -57
  135. package/workers/documents.js +46 -26
  136. package/workers/export.js +20 -8
  137. package/workers/imap-proxy.js +2 -2
  138. package/workers/imap.js +12 -6
  139. package/workers/smtp.js +13 -7
  140. package/workers/submit.js +40 -30
  141. package/workers/webhooks.js +59 -25
  142. package/lib/feature-flags.js +0 -48
@@ -74,7 +74,10 @@
74
74
  }
75
75
  };
76
76
 
77
- const minutesLeft = expires => Math.max(0, Math.round((expires - Date.now()) / 60000));
77
+ // Rounded up, so the label counts the minute that is still running rather than the one
78
+ // nearest: a token with 20 seconds of life left reads "1 min left", and "0" never shows
79
+ // for a token that still works.
80
+ const minutesLeft = expires => Math.max(0, Math.ceil((expires - Date.now()) / 60000));
78
81
 
79
82
  // The "no token set" banner sticks below the topbar (reference/token-alert.hbs), so it
80
83
  // adds to what a jump target has to clear. Its measured height is published to the
@@ -129,17 +132,64 @@
129
132
  }
130
133
  };
131
134
 
135
+ // A minted token's hour runs down while the page stays open, so the countdown below is
136
+ // repainted on a timer as well as on every change to the token. Not a poll: the repaint
137
+ // is scheduled for the moment the displayed minute actually changes, so a page left open
138
+ // does nothing at all in between. The handle doubles as "a countdown is running".
139
+ let countdownTimer = null;
140
+
141
+ const scheduleCountdown = expires => {
142
+ window.clearTimeout(countdownTimer);
143
+ countdownTimer = null;
144
+
145
+ if (!expires) {
146
+ return;
147
+ }
148
+
149
+ // The label (a ceil) rolls over on the minute boundaries counted back from the
150
+ // expiry, not on wall-clock ones. A timeout can fire a hair early, which would
151
+ // repaint the same number and then schedule another full minute from just short of
152
+ // the boundary, so aim past it. The store drops an entry the moment it expires, so
153
+ // what arrives here is always still ahead.
154
+ const remaining = expires - Date.now();
155
+ countdownTimer = window.setTimeout(() => paintTokenStatus(), (remaining % 60000 || 60000) + 250);
156
+ };
157
+
158
+ // Whether the "no token set" banner was last painted as shown. Only a change in that
159
+ // pays for publishAlertHeight, which measures: a forced layout, and one that follows the
160
+ // label write below, so the timer would be flushing layout for the whole page every
161
+ // minute for a height a minute rollover cannot have moved. The ResizeObserver in
162
+ // initAlertHeight still covers the band changing height on its own.
163
+ let alertShown = null;
164
+
132
165
  // Repaints the always-visible status in the sidebar, and shows the Clear control on
133
166
  // the overview page only while there is something to clear.
134
167
  const paintTokenStatus = () => {
135
168
  const entry = tokenStore.read();
169
+ const expires = entry && entry.expires;
170
+ const label = entry ? (expires ? `${minutesLeft(expires)} min left` : 'Active') : 'Not set';
136
171
 
137
172
  const state = document.getElementById('ref-token-state');
138
- if (state) {
139
- state.textContent = entry ? (entry.expires ? `${minutesLeft(entry.expires)} min left` : 'Active') : 'Not set';
173
+ // One guard for both: the class only ever changes along with the text
174
+ if (state && state.textContent !== label) {
175
+ state.textContent = label;
140
176
  state.className = entry ? 'text-success text-2xs' : 'text-base-content/50 text-2xs';
141
177
  }
142
178
 
179
+ // The access-token page's own copy of the countdown. It is there because the
180
+ // sidebar's is collapsed behind a disclosure below lg, on the one page where the
181
+ // token is the subject, and it is shown only for a minted token - a pasted one has
182
+ // no expiry known here.
183
+ const expiry = document.getElementById('ref-token-expiry');
184
+ if (expiry) {
185
+ expiry.classList.toggle('hidden', !expires);
186
+ if (expires) {
187
+ expiry.textContent = label;
188
+ }
189
+ }
190
+
191
+ scheduleCountdown(expires);
192
+
143
193
  const clearWrap = document.getElementById('ref-token-clear-wrap');
144
194
  if (clearWrap) {
145
195
  clearWrap.classList.toggle('hidden', !entry);
@@ -151,15 +201,22 @@
151
201
  const alert = document.getElementById('ref-token-alert');
152
202
  if (alert) {
153
203
  alert.classList.toggle('hidden', !!entry);
154
- publishAlertHeight(alert);
204
+ if (alertShown !== !entry) {
205
+ alertShown = !entry;
206
+ publishAlertHeight(alert);
207
+ }
155
208
  }
156
209
  };
157
210
 
158
211
  // A tag page carries up to ~150 code blocks, several of them tens of kilobytes,
159
212
  // so highlighting all of them up front stalls the page for seconds. Each block is
160
- // highlighted the first time it becomes visible instead. That also covers blocks
161
- // inside collapsed <details> and inactive tab panels for free: those have no
162
- // layout box until they open, so the observer fires exactly then.
213
+ // highlighted the first time it becomes visible instead - which for most of them is
214
+ // on scroll, since they are below the fold rather than hidden.
215
+ //
216
+ // Blocks with no layout box at all are covered for the same reason and for free: an
217
+ // inactive response tab panel, or a collapsed <details>, has none until it opens, so
218
+ // the observer fires exactly then. Example payloads used to be in that second group;
219
+ // they now render open, so only the non-active response tabs still are.
163
220
  const highlightBlock = block => {
164
221
  if (block.dataset.eeHighlighted) {
165
222
  return;
@@ -629,6 +686,147 @@
629
686
  paint();
630
687
  };
631
688
 
689
+ // Filter and expand/collapse for one large schema tree (reference/schema-tools).
690
+ //
691
+ // Separate from the sidebar filter above on purpose. That one searches operations and
692
+ // deliberately leaves response property names out of its corpus, because it ANDs plain
693
+ // substrings and the envelope fields repeat across most endpoints - adding them would
694
+ // blunt exactly the queries it exists for. The cost was that on a page rendering 418
695
+ // property rows there was no way to reach `oauth2.expires` except by scrolling and
696
+ // opening groups. This filter has its own corpus, its own control and its own scope, so
697
+ // that trade-off is untouched.
698
+ //
699
+ // Matching is on the property name alone, not the description: a name is what a caller
700
+ // has in hand when they come looking, and matching prose would light up most of a tree
701
+ // for a common word.
702
+ const SCHEMA_ROW = 'ee-ref-prop';
703
+ const SCHEMA_GROUP = 'details.ee-ref-disclosure';
704
+
705
+ // Everything about a schema tree that the filter needs and that never changes: the rows,
706
+ // their lowercased names, the parent row each one hangs off, and the disclosures on the
707
+ // way down to it. Walked once per tree, so a keystroke is a pass over arrays rather than
708
+ // ~130 selector queries and as many string allocations.
709
+ //
710
+ // querySelectorAll returns document order, and an ancestor row always precedes its
711
+ // descendants, so a parent's entry is already built by the time a child needs it.
712
+ const indexSchema = scope => {
713
+ const byElement = new Map();
714
+
715
+ return Array.from(scope.querySelectorAll(`.${SCHEMA_ROW}`)).map(element => {
716
+ const code = element.querySelector('.ee-ref-prop-rail code');
717
+ // The parent PROPERTY, not the closest ancestor carrying the class - that is the
718
+ // row itself, which is why the walk starts at parentElement.
719
+ const parentElement = element.parentElement ? element.parentElement.closest(`.${SCHEMA_ROW}`) : null;
720
+
721
+ const groups = [];
722
+ for (let details = element.closest(SCHEMA_GROUP); details && scope.contains(details); details = details.parentElement.closest(SCHEMA_GROUP)) {
723
+ groups.push(details);
724
+ }
725
+
726
+ const entry = { element, name: code ? code.textContent.toLowerCase() : '', parent: byElement.get(parentElement) || null, groups };
727
+ byElement.set(element, entry);
728
+ return entry;
729
+ });
730
+ };
731
+
732
+ const applySchemaFilter = (rows, disclosures, openOnLoad, term) => {
733
+ if (!term) {
734
+ for (const entry of rows) {
735
+ entry.element.classList.remove('hidden');
736
+ }
737
+ // Back to whatever the server decided, so clearing the box is a real undo rather
738
+ // than leaving the tree wherever the search happened to open it
739
+ for (const details of disclosures) {
740
+ details.open = openOnLoad.has(details);
741
+ }
742
+ return 0;
743
+ }
744
+
745
+ const matched = new Set(rows.filter(entry => entry.name.includes(term)));
746
+
747
+ // A row survives if it matches, contains a match, or sits inside one - so a hit on an
748
+ // object still shows the object's own children rather than an empty group.
749
+ const keep = new Set(matched);
750
+ for (const entry of matched) {
751
+ for (let parent = entry.parent; parent; parent = parent.parent) {
752
+ keep.add(parent);
753
+ }
754
+ }
755
+ for (const entry of rows) {
756
+ for (let parent = entry.parent; parent; parent = parent.parent) {
757
+ if (matched.has(parent)) {
758
+ keep.add(entry);
759
+ break;
760
+ }
761
+ }
762
+ }
763
+
764
+ // Only the groups on the way down to a surviving row stay open - otherwise the hits
765
+ // are inside a collapsed <details> and the filter looks like it found nothing, while
766
+ // unrelated groups the reader opened earlier stay in the way.
767
+ const open = new Set();
768
+ for (const entry of keep) {
769
+ entry.element.classList.remove('hidden');
770
+ for (const details of entry.groups) {
771
+ open.add(details);
772
+ }
773
+ }
774
+ for (const entry of rows) {
775
+ if (!keep.has(entry)) {
776
+ entry.element.classList.add('hidden');
777
+ }
778
+ }
779
+ for (const details of disclosures) {
780
+ details.open = open.has(details);
781
+ }
782
+
783
+ return matched.size;
784
+ };
785
+
786
+ const initSchemaTools = () => {
787
+ for (const tools of document.querySelectorAll('[data-schema-tools]')) {
788
+ const scope = document.getElementById(tools.getAttribute('data-schema-scope') || '');
789
+ if (!scope) {
790
+ continue;
791
+ }
792
+
793
+ const rows = indexSchema(scope);
794
+ const disclosures = Array.from(scope.querySelectorAll(SCHEMA_GROUP));
795
+ const openOnLoad = new Set(disclosures.filter(details => details.open));
796
+
797
+ // The input and the two buttons come from ui/search-input and ui/btn, so they
798
+ // carry no hooks of their own - reference/schema-tools renders exactly one
799
+ // input and exactly two buttons, in that order, and the container scopes both
800
+ // lookups.
801
+ const input = tools.querySelector('input');
802
+ const count = tools.querySelector('[data-schema-count]');
803
+ const [expand, collapse] = tools.querySelectorAll('button');
804
+
805
+ if (input) {
806
+ input.addEventListener('input', () => {
807
+ const term = input.value.trim().toLowerCase();
808
+ const hits = applySchemaFilter(rows, disclosures, openOnLoad, term);
809
+ if (count) {
810
+ count.textContent = term ? `${hits} ${hits === 1 ? 'match' : 'matches'}` : '';
811
+ }
812
+ });
813
+ }
814
+
815
+ const setAll = open => () => {
816
+ for (const details of disclosures) {
817
+ details.open = open;
818
+ }
819
+ };
820
+
821
+ if (expand) {
822
+ expand.addEventListener('click', setAll(true));
823
+ }
824
+ if (collapse) {
825
+ collapse.addEventListener('click', setAll(false));
826
+ }
827
+ }
828
+ };
829
+
632
830
  // Code sample language, remembered across operations and page loads.
633
831
  //
634
832
  // localStorage, not sessionStorage (which is what the try-it token uses): this is a
@@ -843,10 +1041,233 @@
843
1041
  }
844
1042
  };
845
1043
 
1044
+ // ---- Try it: JSON body editor ------------------------------------------------
1045
+ //
1046
+ // The request body textarea is upgraded to an ACE editor for JSON highlighting, the
1047
+ // gutter errors its worker reports as you type, and completion over the field names the
1048
+ // operation actually accepts. The textarea stays in the DOM and keeps holding the value,
1049
+ // so prepareRequest(), "Copy as curl" and the JSON validation are untouched and the
1050
+ // panel still works with no JS at all.
1051
+ //
1052
+ // Both halves are lazy. ace.js is 127 KB gzipped and nothing else on a reference page
1053
+ // wants it, so it is fetched the first time a Try it panel with a body is opened; and
1054
+ // each editor is mounted on its own panel's first open, because these disclosures start
1055
+ // closed and ACE cannot measure itself inside a container with no layout box. A tag page
1056
+ // has at most seven request bodies and in practice mounts one or two.
1057
+ const ACE_BASE = '/static/js/ace';
1058
+
1059
+ let acePromise = null;
1060
+
1061
+ const loadScript = src =>
1062
+ new Promise((resolve, reject) => {
1063
+ const element = document.createElement('script');
1064
+ element.src = src;
1065
+ element.onload = resolve;
1066
+ element.onerror = () => reject(new Error(`Failed to load ${src}`));
1067
+ document.head.appendChild(element);
1068
+ });
1069
+
1070
+ const loadAce = () => {
1071
+ if (!acePromise) {
1072
+ acePromise = loadScript(`${ACE_BASE}/ace.js`)
1073
+ .then(() => {
1074
+ // Modes, themes and the JSON worker are resolved against this. ACE infers
1075
+ // it from its own <script src> when the page carries one; injected at
1076
+ // runtime that inference is not worth relying on.
1077
+ window.ace.config.set('basePath', ACE_BASE);
1078
+ return loadScript(`${ACE_BASE}/ext-language_tools.js`);
1079
+ })
1080
+ .catch(err => {
1081
+ // Let a later open try again rather than leaving every panel stuck on a
1082
+ // one-off network failure
1083
+ acePromise = null;
1084
+ throw err;
1085
+ });
1086
+ }
1087
+ return acePromise;
1088
+ };
1089
+
1090
+ // Completion entries for one operation, read off the property tree already rendered on
1091
+ // the page rather than duplicated into the markup for the editor's benefit. Stock
1092
+ // language_tools completion is word-based - inside a JSON body that only ever suggests
1093
+ // words already typed - so this replaces the default completers rather than joining them.
1094
+ //
1095
+ // Deduped by name: the same field legitimately appears at several depths (`auth` under
1096
+ // both imap and smtp), and a completer needs the token, not where it came from.
1097
+ //
1098
+ // A row that carries an example completes to the whole pair, `"listId": "test-list"`,
1099
+ // rather than to a bare key. Completing a key alone left the reader with the harder half
1100
+ // of the problem still to solve, which is the same gap the rows themselves used to have.
1101
+ //
1102
+ // The value comes off the row's example chip, so it is the value documented right there.
1103
+ // A cut chip carries the whole value in data-example - reference/schema-node emits it for
1104
+ // exactly this - rather than the completion being reconstructed from the shortened text.
1105
+ const schemaCompleter = schemaId => {
1106
+ const scope = document.getElementById(schemaId || '');
1107
+ if (!scope) {
1108
+ return null;
1109
+ }
1110
+
1111
+ const fields = new Map();
1112
+ for (const row of scope.querySelectorAll('.ee-ref-prop')) {
1113
+ // The row's OWN rail. A row contains its nested rows, so an unanchored descendant
1114
+ // selector would read a child's name onto the parent.
1115
+ const rail = row.querySelector(':scope > .ee-ref-prop-row > .ee-ref-prop-rail');
1116
+ const name = rail && rail.querySelector('code');
1117
+ if (!name || !name.textContent || fields.has(name.textContent)) {
1118
+ continue;
1119
+ }
1120
+ const type = rail.querySelector('.text-primary');
1121
+ const example = row.querySelector(':scope > .ee-ref-prop-row > .ee-ref-prop-body .ee-ref-example-value');
1122
+ const value = example ? example.dataset.example || example.textContent : '';
1123
+
1124
+ fields.set(name.textContent, {
1125
+ caption: name.textContent,
1126
+ value: value ? `"${name.textContent}": ${value}` : name.textContent,
1127
+ meta: type ? type.textContent : '',
1128
+ score: 100
1129
+ });
1130
+ }
1131
+
1132
+ if (!fields.size) {
1133
+ return null;
1134
+ }
1135
+
1136
+ const list = [...fields.values()];
1137
+ return { getCompletions: (editor, session, pos, prefix, callback) => callback(null, list) };
1138
+ };
1139
+
1140
+ // Mounted editors, so the "Fill in every field" button can write into one. The editor is
1141
+ // created inside mountBodyEditor and nothing else needed a handle on it before.
1142
+ const bodyEditors = new WeakMap();
1143
+
1144
+ // Swaps the typical payload in the try-it field for the complete one, read back out of
1145
+ // the tab panel rendered above the form rather than from a second copy in the markup.
1146
+ const fillCompleteBody = field => {
1147
+ const source = document.getElementById(field.dataset.completeSource || '');
1148
+ const code = source && source.querySelector('code');
1149
+ const textarea = field.querySelector('[data-body]');
1150
+
1151
+ if (!code || !textarea) {
1152
+ return;
1153
+ }
1154
+
1155
+ textarea.value = code.textContent;
1156
+
1157
+ const editor = bodyEditors.get(field);
1158
+ if (editor) {
1159
+ editor.setValue(code.textContent, -1);
1160
+ }
1161
+ };
1162
+
1163
+ const mountBodyEditor = async field => {
1164
+ if (field.dataset.editorMounted) {
1165
+ return;
1166
+ }
1167
+ field.dataset.editorMounted = '1';
1168
+
1169
+ const textarea = field.querySelector('[data-body]');
1170
+ if (!textarea) {
1171
+ return;
1172
+ }
1173
+
1174
+ try {
1175
+ await loadAce();
1176
+ } catch (err) {
1177
+ // The textarea is still there and still holds the value, so the panel keeps
1178
+ // working exactly as it did before the upgrade existed
1179
+ delete field.dataset.editorMounted;
1180
+ return;
1181
+ }
1182
+
1183
+ const mount = document.createElement('div');
1184
+ mount.className = 'code-editor ee-ref-body-editor mt-1';
1185
+ textarea.after(mount);
1186
+ textarea.hidden = true;
1187
+
1188
+ const completer = schemaCompleter(field.dataset.schema);
1189
+ const editor = window.uiAceEditor(mount, 'json', textarea.value, {
1190
+ // Grows with the payload instead of scrolling inside a fixed box - a 62-property
1191
+ // account body is unreadable through an eight-line window - but stops before it
1192
+ // pushes the Send button off screen
1193
+ minLines: 8,
1194
+ maxLines: 24,
1195
+ // The 80-column rule means nothing in a JSON body
1196
+ showPrintMargin: false,
1197
+ // Only this operation's own field names, never the word-based default
1198
+ enableBasicAutocompletion: completer ? [completer] : false,
1199
+ enableLiveAutocompletion: completer ? [completer] : false
1200
+ });
1201
+
1202
+ bodyEditors.set(field, editor);
1203
+
1204
+ // The textarea remains what everything downstream reads
1205
+ editor.session.on('change', () => {
1206
+ textarea.value = editor.getValue();
1207
+ });
1208
+ };
1209
+
1210
+ const initTryItEditors = () => {
1211
+ for (const field of document.querySelectorAll('[data-body-field]')) {
1212
+ const details = field.closest('details');
1213
+ if (!details) {
1214
+ continue;
1215
+ }
1216
+
1217
+ if (details.open) {
1218
+ mountBodyEditor(field);
1219
+ continue;
1220
+ }
1221
+
1222
+ // Mounting is idempotent, so a panel opened and closed repeatedly still costs one
1223
+ details.addEventListener('toggle', () => {
1224
+ if (details.open) {
1225
+ mountBodyEditor(field);
1226
+ }
1227
+ });
1228
+ }
1229
+ };
1230
+
1231
+ // Confirmation step for the operations whose effect cannot be taken back (tryImpact.confirm
1232
+ // in lib/api-reference/model.js). The fields arrive prefilled from the schema example, so
1233
+ // Send on DELETE /v1/account/{account} is one click from a real deletion; this puts the
1234
+ // resolved request in front of the reader first - the actual method, path and body, with
1235
+ // the values currently in the form - and makes them press a second, differently labelled
1236
+ // button.
1237
+ //
1238
+ // The dialog mechanics are window.uiConfirmModal in static/js/ui.js, shared with the
1239
+ // message browser. All this adds is what to show.
1240
+ const confirmRequest = (form, request) => {
1241
+ const modal = document.getElementById(form.dataset.confirm);
1242
+ if (!modal) {
1243
+ return Promise.resolve(false);
1244
+ }
1245
+
1246
+ // Emitted unconditionally beside the button, by the same {{#if tryImpact.confirm}}
1247
+ // that sets the form's data-confirm, so these are there
1248
+ modal.querySelector('.ee-ref-confirm-request').textContent = `${request.method} ${request.url}`;
1249
+
1250
+ // Shown only when there is one, which among these operations means the bulk deletes
1251
+ // and the submits, where the body is what decides what actually happens
1252
+ modal.querySelector('.ee-ref-confirm-body').textContent = request.body || '';
1253
+ modal.querySelector('.ee-ref-confirm-body-wrap').classList.toggle('hidden', !request.body);
1254
+
1255
+ return window.uiConfirmModal(modal, '.ee-ref-confirm-run');
1256
+ };
1257
+
846
1258
  // One delegated listener for every try-it form on the page, matching how
847
1259
  // static/js/ui.js handles the copy buttons.
848
1260
  const initTryIt = () => {
849
1261
  document.addEventListener('click', event => {
1262
+ const fill = event.target.closest('.ee-ref-try-fill');
1263
+ if (fill) {
1264
+ const field = fill.closest('[data-body-field]');
1265
+ if (field) {
1266
+ fillCompleteBody(field);
1267
+ }
1268
+ return;
1269
+ }
1270
+
850
1271
  const button = event.target.closest('.ee-ref-try-curl');
851
1272
  if (!button) {
852
1273
  return;
@@ -874,10 +1295,22 @@
874
1295
  const status = form.querySelector('.ee-ref-try-status');
875
1296
  const button = form.querySelector('button[type="submit"]');
876
1297
 
1298
+ // Busy BEFORE the confirmation, not after it. uiButtonBusy disables the button,
1299
+ // which is what stops a second Send from queueing a second confirmation against
1300
+ // the same dialog - one "Run it" would then resolve both and delete twice.
877
1301
  window.uiButtonBusy(button, true);
878
- const started = performance.now();
879
1302
 
880
1303
  try {
1304
+ // Confirmed after prepareRequest, so a malformed body is reported rather than
1305
+ // confirmed, and the dialog can show the request that would actually run.
1306
+ // Inside the try so that however it ends, the finally re-enables the button.
1307
+ if (form.dataset.confirm && !(await confirmRequest(form, request))) {
1308
+ return;
1309
+ }
1310
+
1311
+ // Started after the dialog, so a reader thinking it over is not reported back
1312
+ // to them as request latency
1313
+ const started = performance.now();
881
1314
  const response = await fetch(request.url, {
882
1315
  method: request.method,
883
1316
  headers: request.headers,
@@ -964,10 +1397,11 @@
964
1397
  return;
965
1398
  }
966
1399
 
967
- // Reflect what is already held for this tab
1400
+ // Reflect what is already held for this tab. No minute count here - this line is
1401
+ // written once, and the badge beside the Clear button is the one that stays true.
968
1402
  const existing = tokenStore.read();
969
1403
  if (existing) {
970
- say(existing.expires ? `A temporary token is active, ${minutesLeft(existing.expires)} minutes left.` : 'A token is active.', true);
1404
+ say(existing.expires ? 'A temporary token is active.' : 'A token is active.', true);
971
1405
  }
972
1406
 
973
1407
  document.getElementById('ref-token-set').addEventListener('click', () => {
@@ -1003,8 +1437,10 @@
1003
1437
  initFilter();
1004
1438
  initTagRows();
1005
1439
  initScrollSpy();
1440
+ initSchemaTools();
1006
1441
  initSampleTabs();
1007
1442
  initTryIt();
1443
+ initTryItEditors();
1008
1444
  initMint();
1009
1445
  initTokenForm();
1010
1446
  initAlertHeight();
@@ -1015,6 +1451,16 @@
1015
1451
  whenComponentsReady(revealTarget);
1016
1452
  });
1017
1453
 
1454
+ // A background tab has its timers throttled to about one a minute, and a frozen one gets
1455
+ // none at all, so the countdown can be well behind by the time the reader comes back to
1456
+ // it. Repaint on the way back rather than trusting the schedule to have kept up - but
1457
+ // only with a countdown actually running, since nothing else here moves on its own.
1458
+ document.addEventListener('visibilitychange', () => {
1459
+ if (!document.hidden && countdownTimer) {
1460
+ paintTokenStatus();
1461
+ }
1462
+ });
1463
+
1018
1464
  // Same-page property links only change the fragment, so the load-time pass does not
1019
1465
  // run for them
1020
1466
  window.addEventListener('hashchange', revealTarget);
package/static/js/ui.js CHANGED
@@ -79,6 +79,49 @@ window.uiModal = {
79
79
  }
80
80
  };
81
81
 
82
+ // Promise-returning confirmation over a ui/modal. Resolves true only when the element matching
83
+ // `okSelector` was clicked before the dialog closed; every other way out - Cancel, the corner
84
+ // close, Escape, a backdrop click - resolves false, which is the safe answer for a
85
+ // confirmation. A dialog that is not on the page resolves false for the same reason: not
86
+ // running is recoverable, acting without being asked is not.
87
+ //
88
+ // Every button in the dialog must dismiss it through data-overlay, which is FlyonUI's own
89
+ // path. Nothing here closes the overlay, and callers must not either: HSOverlay defers parts
90
+ // of both open and close to timers and to transitionend, so a close driven from outside races
91
+ // with itself. One begun in the 50ms before `opened` is set is simply undone, and one begun
92
+ // mid-transition never re-adds `hidden` - either way the dialog is stranded on screen with no
93
+ // way out, which on a confirmation is the worst failure available.
94
+ //
95
+ // Callers fill in their own text first; only the answer is shared.
96
+ window.uiConfirmModal = (target, okSelector) => {
97
+ const modal = typeof target === 'string' ? document.querySelector(target) : target;
98
+ const ok = modal && modal.querySelector(okSelector);
99
+ if (!ok) {
100
+ return Promise.resolve(false);
101
+ }
102
+
103
+ return new Promise(resolve => {
104
+ let confirmed = false;
105
+ const onConfirm = () => {
106
+ confirmed = true;
107
+ };
108
+
109
+ // Removed by hand rather than with {once:true}: a dismissal leaves it attached, and
110
+ // one per call would accumulate on DOM that outlives the promise
111
+ ok.addEventListener('click', onConfirm);
112
+ modal.addEventListener(
113
+ 'close.overlay',
114
+ () => {
115
+ ok.removeEventListener('click', onConfirm);
116
+ resolve(confirmed);
117
+ },
118
+ { once: true }
119
+ );
120
+
121
+ window.uiModal.open(modal);
122
+ });
123
+ };
124
+
82
125
  // Re-initialize FlyonUI components inside dynamically injected markup
83
126
  window.uiAutoInit = () => {
84
127
  if (typeof HSStaticMethods !== 'undefined' && typeof HSStaticMethods.autoInit === 'function') {