@sparkvault/sdk-js 14.2.2 → 14.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -9693,33 +9693,71 @@ function getStyles(options) {
9693
9693
  }
9694
9694
 
9695
9695
  /**
9696
- * Modal shell shared by the SDK's widgets (identity and upload).
9696
+ * Modal shell shared by the SDK's widgets (identity, upload and the help desk
9697
+ * form).
9697
9698
  *
9698
- * One dialog behaviour for both: an overlay labelled as a modal dialog that
9699
- * dismisses on its backdrop and on Escape, a page scroll lock held for as long
9700
- * as any shell is open (and the page's own overflow given back when the last
9701
- * one closes), focus moved into the dialog on open and returned to whatever
9702
- * had it on close, and every listener removed on teardown. Each widget builds
9703
- * its own header, body and footer inside `modal`.
9704
- */
9705
- /** Shells currently open, and the page overflow the first of them replaced. */
9706
- let openShells = 0;
9699
+ * One dialog behaviour for all of them: an overlay labelled as a modal dialog;
9700
+ * a page scroll lock held for as long as any shell is open (and the page's own
9701
+ * overflow given back when the last one closes); focus moved into the dialog
9702
+ * on open and returned to whatever had it on close; and every listener
9703
+ * removed on teardown. Each widget builds its own header, body and footer
9704
+ * inside `modal`.
9705
+ *
9706
+ * The backdrop dismisses only a press and release both on it. A browser sends
9707
+ * `click` to the nearest common ancestor of the press and the release, so a
9708
+ * text selection dragged out of the dialog and released over the backdrop
9709
+ * clicks the overlay, and that is no dismissal.
9710
+ *
9711
+ * Open shells stack, and the keyboard belongs to the one opened last that is
9712
+ * still in the page, so a dialog opened over another (Identity's sign-in over
9713
+ * the help desk form) answers alone, and a shell whose overlay the host
9714
+ * removed without destroy() answers nothing. A dialog of the host's own that
9715
+ * holds focus (an `onAuthRequired` sign-in opened over the form) keeps the
9716
+ * keyboard too. Escape dismisses the shell, unless the key ends an IME
9717
+ * composition or another handler already took it (`defaultPrevented`). Tab
9718
+ * and Shift+Tab stay inside it, wrapping from its last control to its first
9719
+ * and back, as the WAI-ARIA dialog pattern asks; Escape still leaves, so it is
9720
+ * no keyboard trap.
9721
+ */
9722
+ /** What Tab can reach inside a dialog, before the rendered check. */
9723
+ const FOCUSABLE = 'a[href], button:not([disabled]), input:not([disabled]):not([type=hidden]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
9724
+ /** A dialog of any making: focus inside one that is no shell's leaves the keyboard to it. */
9725
+ const ANY_DIALOG = '[aria-modal="true"], [role="dialog"], [role="alertdialog"], dialog[open]';
9726
+ /** Shells currently open, the one opened last on top, and the page overflow the first of them replaced. */
9727
+ const openShells = [];
9707
9728
  let pageOverflow = '';
9708
- function lockPageScroll() {
9709
- if (openShells === 0) {
9729
+ /** The shell opened last whose overlay is still in the page. */
9730
+ function topShell() {
9731
+ for (let index = openShells.length - 1; index >= 0; index -= 1) {
9732
+ if (openShells[index].overlay.isConnected)
9733
+ return openShells[index];
9734
+ }
9735
+ return undefined;
9736
+ }
9737
+ /**
9738
+ * Whether a keydown is an IME's. Safari ends a composition before the keydown
9739
+ * that ends it, so that keydown reads `isComposing` false and only its
9740
+ * (deprecated) `keyCode`, the IME's 229, tells it apart.
9741
+ */
9742
+ function composing(event) {
9743
+ return event.isComposing || event.keyCode === 229;
9744
+ }
9745
+ function pushShell(shell) {
9746
+ if (openShells.length === 0) {
9710
9747
  pageOverflow = document.body.style.overflow;
9711
9748
  document.body.style.overflow = 'hidden';
9712
9749
  }
9713
- openShells += 1;
9750
+ openShells.push(shell);
9714
9751
  }
9715
- function unlockPageScroll() {
9716
- openShells -= 1;
9717
- if (openShells === 0) {
9752
+ function removeShell(shell) {
9753
+ openShells.splice(openShells.indexOf(shell), 1);
9754
+ if (openShells.length === 0) {
9718
9755
  document.body.style.overflow = pageOverflow;
9719
9756
  }
9720
9757
  }
9721
9758
  class ModalShell {
9722
9759
  constructor(options) {
9760
+ this.pressOnBackdrop = false;
9723
9761
  this.open = true;
9724
9762
  this.opener = document.activeElement instanceof HTMLElement ? document.activeElement : null;
9725
9763
  this.overlay = document.createElement('div');
@@ -9734,18 +9772,33 @@ class ModalShell {
9734
9772
  this.modal.className = `${options.classPrefix}-modal`;
9735
9773
  this.modal.tabIndex = -1;
9736
9774
  this.overlay.appendChild(this.modal);
9775
+ this.onOverlayPointerDown = (event) => {
9776
+ this.pressOnBackdrop = event.target === this.overlay;
9777
+ };
9737
9778
  this.onOverlayClick = (event) => {
9738
- if (event.target === this.overlay)
9779
+ const dismiss = event.target === this.overlay && this.pressOnBackdrop;
9780
+ this.pressOnBackdrop = false;
9781
+ if (dismiss)
9739
9782
  options.onDismiss();
9740
9783
  };
9741
9784
  this.onKeydown = (event) => {
9742
- if (event.key === 'Escape')
9785
+ if (!this.ownsKeyboard())
9786
+ return;
9787
+ if (event.key === 'Escape') {
9788
+ if (composing(event) || event.defaultPrevented)
9789
+ return;
9790
+ event.preventDefault();
9743
9791
  options.onDismiss();
9792
+ }
9793
+ else if (event.key === 'Tab' && !event.defaultPrevented) {
9794
+ this.keepFocusInside(event);
9795
+ }
9744
9796
  };
9797
+ this.overlay.addEventListener('pointerdown', this.onOverlayPointerDown);
9745
9798
  this.overlay.addEventListener('click', this.onOverlayClick);
9746
9799
  document.addEventListener('keydown', this.onKeydown);
9747
9800
  document.body.appendChild(this.overlay);
9748
- lockPageScroll();
9801
+ pushShell(this);
9749
9802
  this.modal.focus({ preventScroll: true });
9750
9803
  }
9751
9804
  /** Remove the dialog, release the scroll lock, and return focus to the opener. */
@@ -9754,13 +9807,59 @@ class ModalShell {
9754
9807
  return;
9755
9808
  this.open = false;
9756
9809
  document.removeEventListener('keydown', this.onKeydown);
9810
+ this.overlay.removeEventListener('pointerdown', this.onOverlayPointerDown);
9757
9811
  this.overlay.removeEventListener('click', this.onOverlayClick);
9758
9812
  this.overlay.remove();
9759
- unlockPageScroll();
9813
+ removeShell(this);
9760
9814
  if (this.opener?.isConnected) {
9761
9815
  this.opener.focus({ preventScroll: true });
9762
9816
  }
9763
9817
  }
9818
+ /**
9819
+ * Whether this shell answers the keyboard: it is the top shell still in the
9820
+ * page, and focus is not inside a dialog of the host's own, which the shell
9821
+ * stack does not know of. Focus in another shell is the stack's to settle.
9822
+ */
9823
+ ownsKeyboard() {
9824
+ if (topShell() !== this)
9825
+ return false;
9826
+ const hostDialog = document.activeElement?.closest(ANY_DIALOG);
9827
+ return !hostDialog || openShells.some((shell) => shell.overlay.contains(hostDialog));
9828
+ }
9829
+ /**
9830
+ * Wrap Tab from the dialog's last rendered control to its first, and
9831
+ * Shift+Tab from its first (or from anything before it, the dialog itself
9832
+ * included) to its last. Focus that has left the dialog comes back to its
9833
+ * first control. A step a widget hides (`hidden`) renders no box, so its
9834
+ * controls are never reached.
9835
+ */
9836
+ keepFocusInside(event) {
9837
+ const controls = Array.from(this.modal.querySelectorAll(FOCUSABLE))
9838
+ .filter((element) => element.getClientRects().length > 0);
9839
+ const active = document.activeElement;
9840
+ if (controls.length === 0) {
9841
+ event.preventDefault();
9842
+ this.modal.focus({ preventScroll: true });
9843
+ return;
9844
+ }
9845
+ const first = controls[0];
9846
+ const last = controls[controls.length - 1];
9847
+ if (!active || !this.modal.contains(active)) {
9848
+ event.preventDefault();
9849
+ first.focus();
9850
+ return;
9851
+ }
9852
+ const beforeFirst = active === first || (first.compareDocumentPosition(active) & Node.DOCUMENT_POSITION_PRECEDING) !== 0;
9853
+ const afterLast = active === last || ((last.compareDocumentPosition(active) & Node.DOCUMENT_POSITION_FOLLOWING) !== 0 && !last.contains(active));
9854
+ if (event.shiftKey && beforeFirst) {
9855
+ event.preventDefault();
9856
+ last.focus();
9857
+ }
9858
+ else if (!event.shiftKey && afterLast) {
9859
+ event.preventDefault();
9860
+ first.focus();
9861
+ }
9862
+ }
9764
9863
  }
9765
9864
 
9766
9865
  /**
@@ -14940,6 +15039,18 @@ function requestNumber(value) {
14940
15039
  function validToken(value) {
14941
15040
  return typeof value === 'string' && SESSION_TOKEN.test(value);
14942
15041
  }
15042
+ /** A session answer's lifetime: whole seconds from its landing, more than none. */
15043
+ function validLifetime(value) {
15044
+ return Number.isSafeInteger(value) && value > 0;
15045
+ }
15046
+ /**
15047
+ * When a session whose answer lands now ends, in seconds on this device's
15048
+ * clock: its `ttl_seconds` from now. Core's `expires_at` is never compared
15049
+ * with the device clock, which may run apart from Core's.
15050
+ */
15051
+ function landedDeadline(session) {
15052
+ return Date.now() / 1000 + session.ttl_seconds;
15053
+ }
14943
15054
  /** A stale result cannot be rendered as, or mistaken for a failure of, the new customer. */
14944
15055
  function sessionChanged() {
14945
15056
  return new SparkVaultError('The help desk session changed before this request completed. Reload for the current customer; reconcile any pending write before retrying.', 'HELP_DESK_SESSION_CHANGED', 409, undefined, null, 'HELP_DESK_SESSION_CHANGED');
@@ -14956,6 +15067,12 @@ class HelpDeskClient {
14956
15067
  this.renewedTokens = new Set();
14957
15068
  this.authorityRevision = 0;
14958
15069
  this.listeners = new Set();
15070
+ /**
15071
+ * When the installed session ends on this device's clock, in seconds: set
15072
+ * from `ttl_seconds` as an answer lands from Core. Null for a session whose
15073
+ * landing this client did not see (setSession), which ends at Core's 401.
15074
+ */
15075
+ this.deadline = null;
14959
15076
  this.token = null;
14960
15077
  this.revision = 0;
14961
15078
  this.desk = Object.freeze({ ...desk, branding: Object.freeze({ ...desk.branding }), identity: desk.identity ? Object.freeze({ ...desk.identity }) : null,
@@ -14994,19 +15111,23 @@ class HelpDeskClient {
14994
15111
  return new HelpDeskClient(data, http, new IngotsModule(customerConfig, http), options.getSessionToken, assertionProvider);
14995
15112
  }
14996
15113
  /**
14997
- * Install a customer session answer. A company provider verifies restored
14998
- * sessions again before use. Null stays signed out until refreshSession()
14999
- * or authenticate() explicitly resumes the provider.
15114
+ * Install a customer session answer the host holds, such as one restored
15115
+ * from its own storage. Its landing time is unknown, so this client sets it
15116
+ * no deadline of its own: it lasts until Core answers 401, which clears it.
15117
+ * A company provider verifies restored sessions again before use. Null
15118
+ * stays signed out until refreshSession() or authenticate() explicitly
15119
+ * resumes the provider.
15000
15120
  */
15001
15121
  setSession(session) {
15002
- this.installSession(session);
15122
+ this.installSession(session, null);
15003
15123
  }
15004
- installSession(session) {
15005
- if (session !== null && (!validToken(session?.token) || !Number.isFinite(session.expires_at) || session.expires_at <= Date.now() / 1000)) {
15006
- throw new ValidationError('session must be a live help desk session answer', { field: 'session' });
15124
+ installSession(session, deadline) {
15125
+ if (session !== null && (!validToken(session?.token) || !Number.isFinite(session.expires_at))) {
15126
+ throw new ValidationError('session must be a help desk session answer', { field: 'session' });
15007
15127
  }
15008
15128
  const installed = session ? { ...session, customer: { ...session.customer } } : null;
15009
15129
  this.session = installed;
15130
+ this.deadline = installed ? deadline : null;
15010
15131
  this.authentication = null;
15011
15132
  this.verifiedRequesterId = null;
15012
15133
  this.provedSession = false;
@@ -15028,6 +15149,7 @@ class HelpDeskClient {
15028
15149
  /** Explicitly refresh the host-managed session, for example after a backend handoff. */
15029
15150
  async refreshSession() {
15030
15151
  this.session = undefined;
15152
+ this.deadline = null;
15031
15153
  this.authentication = null;
15032
15154
  this.verifiedRequesterId = null;
15033
15155
  this.provedSession = false;
@@ -15046,12 +15168,12 @@ class HelpDeskClient {
15046
15168
  if (this.desk.auth_mode === 'external_unavailable')
15047
15169
  throw new SparkVaultError('Company-session authentication is unavailable.', 'HELP_DESK_EXTERNAL_UNAVAILABLE', 503);
15048
15170
  if (this.assertionProvider && this.desk.auth_mode === 'external_sso' &&
15049
- (this.session === undefined || (this.session && (!this.provedSession || (this.verifiedRequesterId !== null && this.session.expires_at <= Date.now() / 1000 + RENEW_BEFORE_SECONDS))))) {
15171
+ (this.session === undefined || (this.session && (!this.provedSession || (this.verifiedRequesterId !== null && this.deadline !== null && this.deadline <= Date.now() / 1000 + RENEW_BEFORE_SECONDS))))) {
15050
15172
  await this.authenticate();
15051
15173
  return this.session?.token ?? null;
15052
15174
  }
15053
15175
  if (this.session !== undefined) {
15054
- if (this.session && this.session.expires_at <= Date.now() / 1000)
15176
+ if (this.session && this.deadline !== null && this.deadline <= Date.now() / 1000)
15055
15177
  this.clearSession();
15056
15178
  return this.session?.token ?? null;
15057
15179
  }
@@ -15120,16 +15242,22 @@ class HelpDeskClient {
15120
15242
  const session = await this.send('POST', path, body, undefined, false);
15121
15243
  if (revision !== this.revision)
15122
15244
  throw new AuthenticationError('The help desk session changed during sign-in. Try again.');
15123
- const installed = this.installSession(session);
15245
+ if (!validLifetime(session?.ttl_seconds))
15246
+ throw new SparkVaultError('The help desk returned an invalid session.', 'HELP_DESK_INVALID_RESPONSE');
15247
+ const installed = this.installSession(session, landedDeadline(session));
15124
15248
  if (this.session !== installed || this.token !== session.token)
15125
15249
  throw sessionChanged();
15126
15250
  this.provedSession = true;
15127
15251
  return session;
15128
15252
  }
15129
- /** A public, short-lived challenge from the help desk's own Entropy service. */
15253
+ /**
15254
+ * A public, short-lived challenge from the help desk's own Entropy service.
15255
+ * Core refuses a nonce past its `expires_at` on its own clock, so this
15256
+ * device's clock never judges it.
15257
+ */
15130
15258
  async startExternalChallenge() {
15131
15259
  const challenge = await this.send('POST', apiPath `/v1/hd/desks/${this.desk.desk_id}/auth/external/challenge`, undefined, undefined, false);
15132
- if (!EXTERNAL_NONCE.test(challenge?.nonce) || !EXTERNAL_NONCE.test(challenge?.verifier) || !Number.isFinite(challenge.expires_at) || challenge.expires_at <= Date.now() / 1000) {
15260
+ if (!EXTERNAL_NONCE.test(challenge?.nonce) || !EXTERNAL_NONCE.test(challenge?.verifier) || !Number.isFinite(challenge.expires_at)) {
15133
15261
  throw new SparkVaultError('The help desk returned an invalid support challenge.', 'HELP_DESK_INVALID_RESPONSE');
15134
15262
  }
15135
15263
  return challenge;
@@ -15182,17 +15310,20 @@ class HelpDeskClient {
15182
15310
  this.authentication = null; }).catch(() => undefined);
15183
15311
  return operation;
15184
15312
  }
15313
+ /** Install a company session answer that just landed from Core, timed from its `ttl_seconds`. */
15185
15314
  acceptExternalSession(session, automatic = false) {
15186
- if (!validToken(session?.token) || !Number.isFinite(session.expires_at) || session.expires_at <= Date.now() / 1000 ||
15315
+ if (!validToken(session?.token) || !Number.isFinite(session.expires_at) || !validLifetime(session.ttl_seconds) ||
15187
15316
  !isResourceIdOf('requester', session.customer?.requester_id)) {
15188
15317
  throw new SparkVaultError('The help desk returned an invalid company session.', 'HELP_DESK_INVALID_RESPONSE');
15189
15318
  }
15319
+ const deadline = landedDeadline(session);
15190
15320
  const previous = this.verifiedRequesterId;
15191
15321
  const sameCustomer = previous !== null && previous === session.customer.requester_id;
15192
15322
  if (sameCustomer && this.token)
15193
15323
  this.renewedTokens.add(this.token);
15194
15324
  const installed = { ...session, customer: { ...session.customer } };
15195
15325
  this.session = installed;
15326
+ this.deadline = deadline;
15196
15327
  this.verifiedRequesterId = session.customer.requester_id;
15197
15328
  this.provedSession = true;
15198
15329
  this.rememberToken(session.token, sameCustomer ? 'renew' : 'external', true, {
@@ -15299,645 +15430,70 @@ class HelpDeskClient {
15299
15430
  }
15300
15431
  }
15301
15432
 
15302
- /** Scoped styles for the optional inline help desk form. */
15303
- function injectHelpDeskFormStyles() {
15304
- if (document.getElementById('sparkvault-help-desk-styles'))
15305
- return;
15306
- const style = document.createElement('style');
15307
- style.id = 'sparkvault-help-desk-styles';
15308
- style.textContent = `
15309
- .svhd-overlay{position:fixed;inset:0;z-index:2147483000;display:flex;align-items:center;justify-content:center;padding:16px;background:${DS.modalOverlay};overflow:auto}
15310
- .svhd-blur{backdrop-filter:blur(5px)}
15311
- .svhd-modal{width:100%;max-width:36rem;max-height:calc(100dvh - 32px);overflow:auto;border-radius:12px;background:${DS.white};box-shadow:${DS.shadowPopover}}
15312
- .svhd-modal-header{display:flex;justify-content:flex-end;padding:12px 16px 0}
15313
- .svhd-modal-close{border:0;border-radius:4px;background:transparent;color:${DS.slate600};cursor:pointer;font:600 14px/1.5 system-ui,sans-serif;padding:6px 8px}
15314
- .svhd-modal-close:hover{color:${DS.slate900}}
15315
- .svhd-modal-close:focus-visible{outline:3px solid ${DS.action};outline-offset:2px}
15316
- .svhd-modal .svhd-container{max-width:none;border:0;border-radius:0;padding-top:8px}
15317
- .svhd-container{box-sizing:border-box;max-width:36rem;border:1px solid ${DS.slate200};border-radius:12px;background:${DS.white};color:${DS.slate900};font:400 15px/1.5 system-ui,sans-serif;padding:24px}
15318
- .svhd-container *{box-sizing:border-box}
15319
- .svhd-container h2{font-size:22px;line-height:1.25;margin:0 0 20px}
15320
- .svhd-form{display:grid;gap:16px}
15321
- .svhd-field{display:grid;gap:6px}
15322
- .svhd-label{font-weight:600}
15323
- .svhd-input{width:100%;border:1px solid ${DS.slate300};border-radius:6px;padding:10px 12px;color:${DS.slate900};background:${DS.white};font:inherit}
15324
- .svhd-input:focus-visible,.svhd-button:focus-visible,.svhd-link:focus-visible{outline:3px solid ${DS.action};outline-offset:2px}
15325
- .svhd-input:disabled{background:${DS.slate50}}
15326
- .svhd-description{min-height:140px;resize:vertical}
15327
- .svhd-auth{display:grid;gap:16px;border:0;padding:0;margin:0}
15328
- .svhd-auth[hidden],.svhd-field[hidden],.svhd-status[hidden],.svhd-link[hidden]{display:none}
15329
- .svhd-actions{display:flex;align-items:center;gap:12px;flex-wrap:wrap}
15330
- .svhd-button{border:0;border-radius:6px;padding:10px 16px;font:600 15px/1.5 system-ui,sans-serif;background:${DS.action};color:${DS.white};cursor:pointer}
15331
- .svhd-button:hover{background:${DS.actionHover}}
15332
- .svhd-button:disabled{opacity:.6;cursor:default}
15333
- .svhd-secondary{background:transparent;color:${DS.slate600};padding:10px 4px}
15334
- .svhd-secondary:hover{background:transparent;color:${DS.slate900}}
15335
- .svhd-status{margin:0;color:${DS.slate600}}
15336
- .svhd-error{color:${DS.error}}
15337
- .svhd-link{color:${DS.action};text-decoration:underline}
15338
- .svhd-attachments[hidden],.svhd-attachments [hidden]{display:none}
15339
- .svhd-file-hint{font-size:13px;color:${DS.slate600};margin:0}
15340
- .svhd-file-list{display:grid;gap:12px;list-style:none;padding:0;margin:0}
15341
- .svhd-file{display:grid;grid-template-columns:minmax(0,1fr) auto auto;gap:4px 10px;align-items:center;border:1px solid ${DS.slate200};border-radius:6px;padding:10px}
15342
- .svhd-file-name{font-weight:600;overflow-wrap:anywhere;grid-column:1/-1}
15343
- .svhd-file [role=status]{grid-column:1/-1;font-size:13px}
15344
- .svhd-file progress{width:100%;min-width:0;accent-color:${DS.action}}
15345
- .svhd-file .svhd-button{font-size:13px;padding:6px}
15346
- .svhd-footer{margin-top:20px;font-size:12px;color:${DS.slate500}}
15347
- `;
15348
- document.head.appendChild(style);
15349
- }
15350
-
15351
15433
  /**
15352
- * The one rule for which characters a file name may hold, shared by every
15353
- * runtime that stores a name (Core refuses them at ingest, and clients check
15354
- * before they send) and every runtime that saves a stored name to a device
15355
- * (Forge's Content-Disposition, the web console's download, the x.sv reveal
15356
- * page, the mobile share sheet). A download saves under the file's current
15357
- * name by this rule, or under SAVED_FILENAME_FALLBACK when it has none.
15358
- *
15359
- * UNSAFE characters are never stored, and are replaced in any name saved to a
15360
- * device: path separators, control characters (C0, DEL and C1), and the
15361
- * invisible bidirectional controls that make a name display a different
15362
- * extension than it has (`invoice<U+202E>fdp.exe` shows as `invoiceexe.pdf`).
15363
- *
15364
- * NON-PORTABLE characters (`: * ? " < > |`) are refused by the file systems a
15365
- * name is saved to, so they are never stored either. A saved name keeps them:
15366
- * a stored name cannot hold one, and Forge carries a name's exact text in the
15367
- * RFC 5987 `filename*` parameter.
15434
+ * The products that send an organization's mail from a platform mail domain,
15435
+ * and the labels under those domains the platform keeps for itself.
15368
15436
  *
15369
- * Pure and import-free, so a browser, a container and a Lambda load it alike;
15370
- * sdk-mobile mirrors it at build time (scripts/generate-app-sdk-mirrors.mjs).
15371
- */
15372
-
15373
- /**
15374
- * Control characters (C0, DEL and C1) and the invisible bidirectional text
15375
- * controls, as the body of a character class: text holding one displays as
15376
- * other text than it holds.
15377
- */
15378
- const CONTROL_AND_BIDI_CHARACTERS = String.raw`\p{Cc}\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069`;
15379
-
15380
- /** Path separators, control characters and bidirectional text controls. */
15381
- const UNSAFE_FILENAME_CHARACTERS = new RegExp(String.raw`[\\/${CONTROL_AND_BIDI_CHARACTERS}]`, 'u');
15382
-
15383
- /** Characters the file systems a name is saved to refuse. */
15384
- const NON_PORTABLE_FILENAME_CHARACTERS = /[:*?"<>|]/;
15385
-
15386
- /**
15387
- * A character a save drops from a name's end. Windows drops trailing dots and
15388
- * spaces from a name it saves ("page.html." is saved as "page.html"); any
15389
- * other trailing white space is dropped alike, so a name never hides the
15390
- * extension a saved copy could open as.
15391
- */
15392
- const DROPPED_TRAILING_CHARACTER = /[\s.]/u;
15393
-
15394
- /**
15395
- * `name` without the trailing dots and white space a save drops: "page.html."
15396
- * and "page.html " are "page.html", "notes. ." is "notes", and a name of only
15397
- * dots and white space is ''. shownExtension and lastExtension read the
15398
- * extension in front of them, and Forge completes a saved name after them
15399
- * (./media-type-node.js withMediaTypeExtension: "notes." under text/plain
15400
- * saves as "notes.txt").
15437
+ * Each product sends an organization's mail from `{handle}.{domain}`, the
15438
+ * organization's own host under the product's mail domain, or from a domain
15439
+ * the organization verified once it is ready for what the product uses it
15440
+ * for. Core and the console read these facts here, so no surface names a
15441
+ * product's domain, capabilities or From address its own way.
15401
15442
  *
15402
- * @param {string} name
15403
- * @returns {string}
15443
+ * BROWSER-SAFE: imports only ./platform-domains.js, which imports nothing.
15404
15444
  */
15405
- function trimDroppedTrailing(name) {
15406
- let end = name.length;
15407
- while (end > 0 && DROPPED_TRAILING_CHARACTER.test(name[end - 1])) end -= 1;
15408
- return name.slice(0, end);
15409
- }
15410
15445
 
15411
- /**
15412
- * Invisible characters (Unicode's Default_Ignorable_Code_Point): the joiners,
15413
- * variation selectors, tags and bidirectional controls, which display as
15414
- * nothing, and the Hangul fillers, which display as blank space. None shows
15415
- * in a name, and a save keeps several (`Invoice.pdf<U+034F>` saves as itself
15416
- * in Chromium and Firefox, and shows as `Invoice.pdf`).
15417
- */
15418
- const INVISIBLE_CHARACTERS = /\p{Default_Ignorable_Code_Point}/gu;
15419
15446
 
15420
15447
  /**
15421
- * What `name` shows once saved: the name read past its invisible characters
15422
- * (INVISIBLE_CHARACTERS), without the trailing dots and white space a save
15423
- * drops (trimDroppedTrailing). The extension a name shows is read in it
15424
- * (shownExtension), and a name it leaves nothing of is one of only dots or
15425
- * spaces (isOnlyDotsOrSpaces).
15426
- *
15427
- * @param {string} name
15428
- * @returns {string}
15448
+ * Each product that sends an organization's mail from a platform mail domain:
15449
+ * its key (also its tiered-product key and the suffix of its SES tenant's
15450
+ * name), the domain (one SES identity, whose {handle}.{domain} subdomain is
15451
+ * each organization's address), what it uses an organization's own domain
15452
+ * for, what a domain needs ready before the product may select it, and the
15453
+ * fixed local part it writes from (null when it varies: a help desk team
15454
+ * writes from its alias). Every descriptor and array frozen.
15429
15455
  */
15430
- function nameAsShown(name) {
15431
- return trimDroppedTrailing(name.replace(INVISIBLE_CHARACTERS, ''));
15432
- }
15433
-
15434
- /** Why a name cannot be stored, as storableFilenameProblem reports it. */
15435
- const FILENAME_PROBLEMS = Object.freeze({
15436
- UNSAFE_CHARACTER: 'unsafe_character',
15437
- NON_PORTABLE_CHARACTER: 'non_portable_character',
15438
- ONLY_DOTS_OR_SPACES: 'only_dots_or_spaces'
15456
+ const MAIL_PRODUCTS = Object.freeze({
15457
+ help_desk: Object.freeze({
15458
+ key: 'help_desk',
15459
+ domain: SPARKVAULT_HELP_DESK_DOMAIN,
15460
+ capabilities: Object.freeze(['web', 'sending', 'receiving']),
15461
+ selectable: Object.freeze(['sending', 'web']),
15462
+ fromLocalPart: null
15463
+ }),
15464
+ notify: Object.freeze({
15465
+ key: 'notify',
15466
+ domain: SPARKVAULT_NOTIFY_SEND_DOMAIN,
15467
+ capabilities: Object.freeze(['sending']),
15468
+ selectable: Object.freeze(['sending']),
15469
+ fromLocalPart: 'notify'
15470
+ })
15439
15471
  });
15440
15472
 
15441
- /**
15442
- * Whether `name` is one of only dots or spaces: a name that is not empty yet
15443
- * shows nothing once saved (nameAsShown), every character in it a dot, white
15444
- * space or an invisible character (`...` and ` . `, and `..<U+034F>` and
15445
- * `.<U+200B>.`, which show as `..`). No such name is stored
15446
- * (storableFilenameProblem), and a file that has one saves under the
15447
- * fallback name (savedFilename).
15448
- *
15449
- * @param {string} name
15450
- * @returns {boolean}
15451
- */
15452
- function isOnlyDotsOrSpaces(name) {
15453
- return name !== '' && nameAsShown(name) === '';
15454
- }
15473
+ /** Every mail product's key, in MAIL_PRODUCTS order: the order every product-by-product decision runs in. */
15474
+ const MAIL_PRODUCT_KEYS = Object.freeze(Object.keys(MAIL_PRODUCTS));
15455
15475
 
15456
15476
  /**
15457
- * What stops `name` from being stored as a file name, or null when nothing
15458
- * does. Length and emptiness are the storing runtime's own limits.
15477
+ * GENERATED by infra/scripts/generate-organization-name-unicode.mjs from
15478
+ * Unicode 18.0.0 (confusables.txt, UnicodeData.txt, DerivedBidiClass.txt,
15479
+ * BidiBrackets.txt, DerivedAge.txt, DerivedNormalizationProps.txt). Do not
15480
+ * edit: rerun the generator.
15459
15481
  *
15460
- * @param {string} name
15461
- * @returns {string|null} One of FILENAME_PROBLEMS, or null
15462
- */
15463
- function storableFilenameProblem(name) {
15464
- if (UNSAFE_FILENAME_CHARACTERS.test(name)) return FILENAME_PROBLEMS.UNSAFE_CHARACTER;
15465
- if (NON_PORTABLE_FILENAME_CHARACTERS.test(name)) return FILENAME_PROBLEMS.NON_PORTABLE_CHARACTER;
15466
- if (isOnlyDotsOrSpaces(name)) return FILENAME_PROBLEMS.ONLY_DOTS_OR_SPACES;
15467
- return null;
15468
- }
15469
-
15470
- /** The longest name an ingot stores, in UTF-16 code units. */
15471
- const INGOT_NAME_MAX = 255;
15472
-
15473
- /**
15474
- * A run of the white space Firefox reads in a name it saves: the space
15475
- * separators and U+FEFF, and the line and paragraph separators, which it
15476
- * drops wherever they stand and which end no run.
15482
+ * The Unicode data @sparkvault/app-sdk/organization-name reads a text with.
15483
+ * Code points are hexadecimal, separated by spaces; `first-last` is a range.
15484
+ * The rule reads a text in NFC, so neither list names a character NFC
15485
+ * replaces on every engine the rule runs on (Unicode 15.1 or older).
15486
+ *
15487
+ * BROWSER-SAFE: data only, no imports.
15477
15488
  */
15478
- const WHITE_SPACE_RUN = /[\p{Zs}\uFEFF\u2028\u2029]+/gu;
15479
15489
 
15480
- /** A run whose first space is an ideographic space (U+3000), the one space Firefox keeps. */
15481
- const RUN_FIREFOX_KEEPS = /^[\u2028\u2029]*\u3000/;
15482
15490
 
15483
15491
  /**
15484
- * `name` with its white space closed up as Firefox closes it in what follows
15485
- * its last dot, under any type it is handed and in a blob's name alike
15486
- * (nsExternalHelperAppService::SanitizeFileName): it collapses a run of white
15487
- * space into the run's first character, an ideographic space kept as it is
15488
- * and any other made an ASCII space, then strips the ASCII spaces from what
15489
- * follows the last dot. So a run there is dropped whole unless it starts with
15490
- * an ideographic space: `evil.ex e` saves as `evil.exe`, `evil.ex<U+3000>e`
15491
- * as itself, `evil.ex <U+3000>e` as `evil.exe`. Closing up the whole name
15492
- * moves no dot, so what follows its last dot is what Firefox saves there.
15493
- *
15494
- * @param {string} name
15495
- * @returns {string}
15496
- */
15497
- function closeUpSpaces(name) {
15498
- return name.replace(WHITE_SPACE_RUN, (run) => (RUN_FIREFOX_KEEPS.test(run) ? '\u3000' : ''));
15499
- }
15500
-
15501
- /**
15502
- * The extension `name` shows, its spaces kept, lowercase and without its dot:
15503
- * what follows the last dot of what the name shows once saved (nameAsShown:
15504
- * read past its invisible characters, without the trailing dots and white
15505
- * space a save drops). "invoice.note.html" and "page.html." show 'html',
15506
- * "logs.tar.gz" 'gz', ".html" 'html' (Windows reads a leading dot's
15507
- * extension), "Invoice.pdf (1)" 'pdf (1)', and a name with no dot ''. It is
15508
- * what Chromium's copy shows, since Chromium saves a name as sent:
15509
- * "Invoice.pdf<U+034F>" shows 'pdf', and a character that draws as blank
15510
- * space or as another letter yet is no invisible character stays in it
15511
- * ("Invoice.pdf<U+2800>", a braille blank, shows 'pdf<U+2800>';
15512
- * "Invoice.<U+0440>df", a Cyrillic er, '<U+0440>df'). Only whether a saved
15513
- * name is completed reads it, by where its first word ends
15514
- * (./media-type-node.js withMediaTypeExtension), since completion ends a name
15515
- * in its type's own extension, which holds no space for Firefox to close up;
15516
- * every rule about what a name makes a file reads lastExtension.
15517
- *
15518
- * @param {string} name
15519
- * @returns {string}
15520
- */
15521
- function shownExtension(name) {
15522
- const shown = nameAsShown(name);
15523
- const dot = shown.lastIndexOf('.');
15524
- return dot === -1 ? '' : shown.slice(dot + 1).toLowerCase();
15525
- }
15526
-
15527
- /**
15528
- * The extension a saved copy of `name` opens as, lowercase and without its
15529
- * dot: the one it shows (shownExtension) once its spaces are closed up as
15530
- * Firefox, the browser that changes a saved name most, closes them
15531
- * (closeUpSpaces). "evil.ex e", "evil.ex<U+00A0>e" and "evil. exe" open as
15532
- * 'exe', "Scan. pdf" as 'pdf', "Invoice.pdf (1)" as 'pdf(1)' and "Acme Inc.
15533
- * Contract" as 'contract', though Chromium saves each as sent. Every rule
15534
- * about what a name makes a file (a refused extension, an inline
15535
- * disposition, the type Core stores for an untyped upload, the type Forge
15536
- * serves a download under: ./media-type-node.js) reads this, never
15537
- * splitExtension.
15538
- *
15539
- * @param {string} name
15540
- * @returns {string}
15541
- */
15542
- function lastExtension(name) {
15543
- // Closed up before its invisible characters are read past: Firefox counts
15544
- // U+FEFF, one of them, as white space (`evil.ex<U+FEFF><U+3000>e` saves as
15545
- // `evil.exe`).
15546
- return shownExtension(closeUpSpaces(name));
15547
- }
15548
-
15549
- /**
15550
- * What a help-desk attachment may be named and what its first bytes may not
15551
- * be: the one list of the files a ticket accepts, beside the one list of
15552
- * extensions Forge never completes a saved name with.
15553
- *
15554
- * Both sides of a ticket upload files: a customer through the portal, an
15555
- * operator through the console, and a customer's mail client through SES. The
15556
- * server never reads the plaintext of an uploaded ingot, so the policy splits
15557
- * by where the bytes are: the NAME rule runs on the server at initiate and in
15558
- * the browser before an upload starts; the SNIFF rule runs wherever the bytes
15559
- * are at hand — in the browser over the file's first slice, and on the server
15560
- * over an inbound email's parts. The type a ticket stores a file under, which
15561
- * the MIME registry decides, is server-only: ./attachment-policy-node.js.
15562
- *
15563
- * Built on the platform's one filename grammar (filename.js): nothing here
15564
- * spells a second version of it.
15565
- *
15566
- * BROWSER-SAFE: reads any Uint8Array (a Node Buffer is one).
15567
- */
15568
-
15569
-
15570
- /**
15571
- * The files a ticket accepts, by kind: each kind keyed by the word every
15572
- * surface names it with (./attachment-check.js ACCEPTED_ATTACHMENTS_COPY), and
15573
- * listing the extensions a name may end in, lowercase and without the dot, as
15574
- * lastExtension reads the one a saved copy opens as. A ticket takes no other
15575
- * file: a name of no extension, an archive, a page, a Markdown document, a
15576
- * drawing, a legacy or macro-enabled Office document, a message, and every
15577
- * program, script, installer, shortcut and certificate are refused, whatever
15578
- * their bytes say.
15579
- * attachment-policy.test.js gives each kind the reason the program a desktop
15580
- * opens it with runs no code from it, and holds that none of them, nor any
15581
- * extension the registry lists beside one for its type, is one of
15582
- * REFUSED_EXTENSIONS or one Outlook blocks.
15583
- */
15584
- const ACCEPTED_ATTACHMENT_KINDS = Object.freeze({
15585
- images: Object.freeze(['jpg', 'jpeg', 'png', 'gif', 'webp', 'heic', 'heif', 'bmp', 'tif', 'tiff', 'avif']),
15586
- documents: Object.freeze(['pdf', 'docx', 'xlsx', 'pptx', 'txt', 'csv', 'tsv', 'log', 'json']),
15587
- recordings: Object.freeze(['mp4', 'mov', 'webm', 'm4a', 'mp3', 'wav'])
15588
- });
15589
-
15590
- /** Every extension a ticket's file may end in: ACCEPTED_ATTACHMENT_KINDS's, in its order. */
15591
- const ACCEPTED_ATTACHMENT_EXTENSIONS = Object.freeze(Object.values(ACCEPTED_ATTACHMENT_KINDS).flat());
15592
-
15593
- const ACCEPTED_EXTENSION_SET = new Set(ACCEPTED_ATTACHMENT_EXTENSIONS);
15594
-
15595
- /**
15596
- * The extensions Forge never completes a saved name with (lowercase, no dot):
15597
- * a vault download's name that shows no extension the MIME registry knows is
15598
- * completed with its type's registered one only when that is none of these
15599
- * (./media-type-node.js withMediaTypeExtension), so no download saves as a
15600
- * program, a script, a page or any file a desktop acts on when it is opened.
15601
- * That completion is all that reads the list: a ticket takes only the
15602
- * extensions it accepts (ACCEPTED_ATTACHMENT_EXTENSIONS), none of them one of
15603
- * these. Every extension Outlook blocks as an attachment is one, held to
15604
- * Microsoft's list by attachment-policy.test.js. The list is ordered by kind.
15605
- */
15606
- const REFUSED_EXTENSIONS = Object.freeze([
15607
- 'exe', 'dll', 'scr', 'com', 'pif', 'cpl', 'msc', 'bat', 'cmd',
15608
- 'ps1', 'ps1xml', 'ps2', 'ps2xml', 'psc1', 'psc2', 'psd1', 'psdm1', 'pssc', 'cdxml',
15609
- 'msh', 'msh1', 'msh2', 'mshxml', 'msh1xml', 'msh2xml',
15610
- 'vb', 'vbs', 'vbe', 'js', 'jse', 'ws', 'wsf', 'wsh', 'wsc', 'sct',
15611
- 'msi', 'msp', 'mst', 'msu', 'cab', 'diagcab', 'inf', 'jar', 'hta', 'htc', 'reg',
15612
- 'lnk', 'url', 'website', 'shb', 'shs', 'scf', 'xnk', 'grp', 'library-ms', 'search-ms',
15613
- 'settingcontent-ms', 'appcontent-ms', 'theme', 'wsb', 'gadget', 'osd', 'bgi', 'udl', 'ins', 'isp',
15614
- 'webpnp', 'printerexport',
15615
- 'iso', 'img', 'vhd', 'vhdx', 'dmg', 'app',
15616
- 'sh', 'bash', 'zsh', 'ksh', 'csh', 'tcl', 'py', 'pyc', 'pyo', 'pyw', 'pyz', 'pyzw', 'pl', 'rb',
15617
- 'php', 'phtml', 'asp', 'aspx',
15618
- 'html', 'htm', 'xhtml', 'shtml', 'shtm', 'xht', 'xhtm', 'mht', 'mhtml', 'svg', 'svgz', 'xml', 'xsl', 'xslt',
15619
- 'rdf', 'xul',
15620
- 'docm', 'dotm', 'xlsm', 'xltm', 'xlam', 'pptm', 'potm', 'ppam', 'sldm', 'ppsm', 'xlsb', 'xla', 'xlm', 'xll',
15621
- 'application', 'appref-ms', 'xbap', 'appinstaller', 'appx', 'appxbundle', 'msix', 'msixbundle',
15622
- 'chm', 'its', 'hlp', 'cnt', 'hpj',
15623
- 'mdb', 'adp', 'ade', 'mag', 'mad', 'maf', 'mam', 'maq', 'mar', 'mas', 'mat', 'mav', 'maw', 'mda', 'mde',
15624
- 'mdt', 'mdw', 'mdz',
15625
- 'prf', 'ops', 'pst',
15626
- 'fxp', 'prg', 'bas', 'vbp', 'vsmacros', 'vsw', 'pcd', 'plg',
15627
- 'cer', 'der', 'crt', 'cert', 'pem', 'p12', 'pfx', 'pkcs12', 'p7b', 'p7c', 'p7r', 'spc',
15628
- 'jnlp', 'class', 'air', 'crx', 'xpi', 'oxt', 'vbox-extpack',
15629
- 'apk', 'pkg', 'mpkg', 'deb', 'udeb', 'rpm', 'run',
15630
- 'asx', 'wax', 'wvx', 'wmx', 'wmd',
15631
- 'mau', 'mcf', 'tmp'
15632
- ]);
15633
-
15634
- /**
15635
- * Why a name or a head slice is refused, as the two refusal functions report
15636
- * it. REFUSED_EXTENSION: the extension the name's saved copy opens as is none
15637
- * a ticket accepts, or the name shows none.
15638
- */
15639
- const ATTACHMENT_REFUSALS = Object.freeze({
15640
- EMPTY: 'empty',
15641
- TOO_LONG: 'too_long',
15642
- UNSAFE_NAME: 'unsafe_name',
15643
- REFUSED_EXTENSION: 'refused_extension',
15644
- EXECUTABLE: 'executable',
15645
- SCRIPT: 'script',
15646
- MARKUP: 'markup'
15647
- });
15648
-
15649
- /** How many leading bytes the sniff reads; a caller slices `file.slice(0, ATTACHMENT_SNIFF_BYTES)`. */
15650
- const ATTACHMENT_SNIFF_BYTES = 4096;
15651
-
15652
- new Set(REFUSED_EXTENSIONS);
15653
-
15654
- /** Leading byte sequences of code a desktop would run. */
15655
- const EXECUTABLE_SIGNATURES = Object.freeze([
15656
- [0x4d, 0x5a], // MZ: Windows PE
15657
- [0x7f, 0x45, 0x4c, 0x46], // ELF
15658
- [0xfe, 0xed, 0xfa, 0xce], // Mach-O 32-bit
15659
- [0xfe, 0xed, 0xfa, 0xcf], // Mach-O 64-bit
15660
- [0xce, 0xfa, 0xed, 0xfe], // Mach-O 32-bit, reversed
15661
- [0xcf, 0xfa, 0xed, 0xfe], // Mach-O 64-bit, reversed
15662
- [0xca, 0xfe, 0xba, 0xbe], // Mach-O universal / Java class
15663
- [0xbe, 0xba, 0xfe, 0xca] // Mach-O universal, reversed
15664
- ]);
15665
-
15666
- const SHEBANG = Object.freeze([0x23, 0x21]);
15667
-
15668
- /**
15669
- * A ZIP local file header's signature (`PK\x03\x04`): the first bytes of
15670
- * every Office Open XML package (.docx, .xlsx, .pptx), the only ZIP a ticket
15671
- * takes. Its parts are XML by definition, and a package whose ZIP stores them
15672
- * uncompressed, as common web exporters write one, shows its first part's
15673
- * `<?xml` declaration in the first kilobyte; no browser reads a file that
15674
- * opens with this signature as a page.
15675
- */
15676
- const ZIP_LOCAL_FILE_SIGNATURE = Object.freeze([0x50, 0x4b, 0x03, 0x04]);
15677
-
15678
- /** Document openers a browser would run as a page; matched case-insensitively in the first kilobyte. */
15679
- const MARKUP_OPENERS = Object.freeze(['<?xml', '<!doctype html', '<html', '<script', '<svg']);
15680
- const MARKUP_WINDOW = 1024;
15681
-
15682
- function startsWith(bytes, prefix) {
15683
- return bytes.length >= prefix.length && prefix.every((byte, index) => bytes[index] === byte);
15684
- }
15685
-
15686
- /**
15687
- * What stops `name` from naming a ticket attachment, or null when nothing does.
15688
- * Its extension is the one a saved copy opens as (lastExtension), and a ticket
15689
- * takes it only when that is one it accepts (ACCEPTED_ATTACHMENT_EXTENSIONS),
15690
- * so a name of no extension is refused, and neither a SparkVault document's
15691
- * two-part extension ("invoice.note.html"), trailing dots or spaces
15692
- * ("page.html."), an invisible character ("evil.ex<U+034F>e") nor a space
15693
- * Firefox closes up ("evil.ex e", which Firefox saves as "evil.exe") passes
15694
- * for an accepted one; a name that opens as one is taken however it is
15695
- * written ("Photo.JPG", "report.pdf.", "Scan. pdf").
15696
- *
15697
- * @param {unknown} name
15698
- * @returns {string|null} One of ATTACHMENT_REFUSALS, or null
15699
- */
15700
- function attachmentNameRefusal(name) {
15701
- if (typeof name !== 'string' || name.length === 0) return ATTACHMENT_REFUSALS.EMPTY;
15702
- if (name.length > INGOT_NAME_MAX) return ATTACHMENT_REFUSALS.TOO_LONG;
15703
- if (storableFilenameProblem(name) !== null) return ATTACHMENT_REFUSALS.UNSAFE_NAME;
15704
- if (!ACCEPTED_EXTENSION_SET.has(lastExtension(name))) return ATTACHMENT_REFUSALS.REFUSED_EXTENSION;
15705
- return null;
15706
- }
15707
-
15708
- /**
15709
- * What the first bytes of a file say it is that no ticket may carry, or null
15710
- * when they say nothing of the kind: a program, a script, or markup a browser
15711
- * would run as a page, which a ZIP package's head is never read for
15712
- * (ZIP_LOCAL_FILE_SIGNATURE). Reads at most ATTACHMENT_SNIFF_BYTES.
15713
- *
15714
- * @param {Uint8Array} headBytes
15715
- * @returns {string|null} One of ATTACHMENT_REFUSALS, or null
15716
- */
15717
- function attachmentSniffRefusal(headBytes) {
15718
- const bytes = headBytes.subarray(0, ATTACHMENT_SNIFF_BYTES);
15719
- if (EXECUTABLE_SIGNATURES.some((signature) => startsWith(bytes, signature))) return ATTACHMENT_REFUSALS.EXECUTABLE;
15720
- if (startsWith(bytes, SHEBANG)) return ATTACHMENT_REFUSALS.SCRIPT;
15721
- if (startsWith(bytes, ZIP_LOCAL_FILE_SIGNATURE)) return null;
15722
- // Latin-1 decoding keeps one byte per character, so a UTF-8 or Latin-1
15723
- // document's ASCII markup reads the same; a BOM or leading whitespace is
15724
- // skipped by searching the window instead of anchoring at offset 0.
15725
- const text = String.fromCharCode(...bytes.subarray(0, MARKUP_WINDOW)).toLowerCase();
15726
- if (MARKUP_OPENERS.some((opener) => text.includes(opener))) return ATTACHMENT_REFUSALS.MARKUP;
15727
- return null;
15728
- }
15729
-
15730
- /**
15731
- * A byte count as people read it: "500 B", "1 KB", "1.5 MB", "5 GB", in
15732
- * binary units (1 KB = 1024 B) up to TB, with at most `decimals` decimal
15733
- * places and no trailing zeros. The one size formatter every surface writes
15734
- * sizes with; a runtime that cannot import app-sdk runs a generated mirror of
15735
- * it (module-mirrors).
15736
- *
15737
- * A count that is missing, not a finite number, zero or negative reads
15738
- * "0 B"; a count past the largest unit is written in TB.
15739
- *
15740
- * BROWSER-SAFE: data only, no imports.
15741
- */
15742
-
15743
- const UNITS = Object.freeze(['B', 'KB', 'MB', 'GB', 'TB']);
15744
- const STEP = 1024;
15745
-
15746
- /**
15747
- * @param {number|null|undefined} bytes
15748
- * @param {number} [decimals=1] - The most decimal places shown
15749
- * @returns {string}
15750
- */
15751
- function formatBytes(bytes, decimals = 1) {
15752
- if (typeof bytes !== 'number' || !Number.isFinite(bytes) || bytes <= 0) return `0 ${UNITS[0]}`;
15753
- const unit = Math.min(Math.max(Math.floor(Math.log(bytes) / Math.log(STEP)), 0), UNITS.length - 1);
15754
- return `${parseFloat((bytes / STEP ** unit).toFixed(decimals))} ${UNITS[unit]}`;
15755
- }
15756
-
15757
- /**
15758
- * What stops a file from joining a message, decided in the browser before any
15759
- * upload starts: the one check the portal's editor and the console's
15760
- * composer run. The server never reads an uploaded file's plaintext, so the
15761
- * check on a file's first bytes can only run here, over the file itself; the
15762
- * name rule runs here and again on the server when the upload is initiated.
15763
- * Both are the platform's one attachment policy (./attachment-policy.js);
15764
- * this adds only what no upload can take: an empty file, one past the most
15765
- * its uploader may attach (a customer ./help-desk-contract.js
15766
- * CUSTOMER_ATTACHMENT_MAX_BYTES, an operator MAX_ATTACHMENT_BYTES), and one
15767
- * the browser can no longer read (changed or moved on disk after it was
15768
- * picked). Beside the check, the sentence each refusal reads, and the one
15769
- * phrase that names the files a ticket accepts.
15770
- *
15771
- * BROWSER-SAFE: reads the file it is handed through the Blob API, which
15772
- * Node's File has too.
15773
- */
15774
-
15775
-
15776
- /** The files refused beyond the policy's. */
15777
- const FILE_REFUSALS = Object.freeze({ EMPTY_FILE: 'empty_file', TOO_LARGE: 'too_large', UNREADABLE: 'unreadable' });
15778
-
15779
- /** The first bytes of `file`, or null when the browser cannot read them. */
15780
- async function headOf(file) {
15781
- try {
15782
- return new Uint8Array(await file.slice(0, ATTACHMENT_SNIFF_BYTES).arrayBuffer());
15783
- } catch {
15784
- return null;
15785
- }
15786
- }
15787
-
15788
- /** A ceiling a file is held to: a positive whole number of bytes, or a TypeError. */
15789
- function assertCeiling(maxBytes) {
15790
- if (!Number.isSafeInteger(maxBytes) || maxBytes < 1) throw new TypeError('A file is held to a ceiling of a positive whole number of bytes');
15791
- }
15792
-
15793
- /**
15794
- * Why `file` cannot be attached, or null when it may be: its name first, then
15795
- * its size against `maxBytes`, then its first bytes.
15796
- *
15797
- * @param {File} file
15798
- * @param {number} maxBytes - The most its uploader may attach: a customer
15799
- * CUSTOMER_ATTACHMENT_MAX_BYTES, an operator MAX_ATTACHMENT_BYTES
15800
- * @returns {Promise<string|null>} One of ATTACHMENT_REFUSALS or FILE_REFUSALS, or null
15801
- * @throws {TypeError} For a ceiling that is not a positive whole number of bytes
15802
- */
15803
- async function attachmentRefusal(file, maxBytes) {
15804
- assertCeiling(maxBytes);
15805
- const byName = attachmentNameRefusal(file.name);
15806
- if (byName) return byName;
15807
- if (file.size === 0) return FILE_REFUSALS.EMPTY_FILE;
15808
- if (file.size > maxBytes) return FILE_REFUSALS.TOO_LARGE;
15809
- const head = await headOf(file);
15810
- return head ? attachmentSniffRefusal(head) : FILE_REFUSALS.UNREADABLE;
15811
- }
15812
-
15813
- /**
15814
- * The words naming every kind of file a ticket accepts with its extensions,
15815
- * the last kind joined by "and": `images (.jpg, .jpeg, ...), documents
15816
- * (.pdf, ...) and recordings (.mp4, ...)`.
15817
- *
15818
- * @param {Readonly<Record<string, readonly string[]>>} kinds - ACCEPTED_ATTACHMENT_KINDS
15819
- * @returns {string}
15820
- */
15821
- function acceptedKindsCopy(kinds) {
15822
- const named = Object.entries(kinds).map(([kind, extensions]) => {
15823
- const listed = extensions.map((extension) => `.${extension}`).join(', ');
15824
- return `${kind} (${listed})`;
15825
- });
15826
- return `${named.slice(0, -1).join(', ')} and ${named.at(-1)}`;
15827
- }
15828
-
15829
- /**
15830
- * The files a ticket accepts, in the one phrase every surface names them by,
15831
- * built from the policy's own list (ACCEPTED_ATTACHMENT_KINDS): the sentence
15832
- * refusing any other file reads it, and so do the API reference
15833
- * (apps/api/openapi/overlay.mjs) and the help desk's page on the site
15834
- * (apps/www/_data/helpDeskAttachments.js).
15835
- */
15836
- const ACCEPTED_ATTACHMENTS_COPY = acceptedKindsCopy(ACCEPTED_ATTACHMENT_KINDS);
15837
-
15838
- const REFUSAL_COPY = Object.freeze({
15839
- [ATTACHMENT_REFUSALS.EMPTY]: 'This file has no name.',
15840
- [ATTACHMENT_REFUSALS.TOO_LONG]: "This file's name is too long. Rename it and try again.",
15841
- [ATTACHMENT_REFUSALS.UNSAFE_NAME]: "This file's name can't be used. Rename it and try again.",
15842
- [ATTACHMENT_REFUSALS.REFUSED_EXTENSION]: `Files of this type can't be attached. You can attach ${ACCEPTED_ATTACHMENTS_COPY}.`,
15843
- [ATTACHMENT_REFUSALS.EXECUTABLE]: "This file is a program, and programs can't be attached.",
15844
- [ATTACHMENT_REFUSALS.SCRIPT]: "This file is a script, and scripts can't be attached.",
15845
- [ATTACHMENT_REFUSALS.MARKUP]: "This file holds web page markup, which can't be attached.",
15846
- [FILE_REFUSALS.EMPTY_FILE]: 'This file is empty.',
15847
- [FILE_REFUSALS.UNREADABLE]: "This file couldn't be read. Remove it and pick it again."
15848
- });
15849
-
15850
- /**
15851
- * The sentence a person reads for a file larger than `maxBytes`, the ceiling
15852
- * it was held to: the browser's check's (FILE_REFUSALS.TOO_LARGE), and the
15853
- * server's, which names its own (`details.max_bytes` of a 413).
15854
- *
15855
- * @param {number} maxBytes
15856
- * @returns {string}
15857
- */
15858
- function tooLargeCopy(maxBytes) {
15859
- assertCeiling(maxBytes);
15860
- return `This file is larger than ${formatBytes(maxBytes)}.`;
15861
- }
15862
-
15863
- /**
15864
- * The sentence a person reads for a refusal.
15865
- *
15866
- * @param {string} refusal - One of ATTACHMENT_REFUSALS or FILE_REFUSALS
15867
- * @param {number} maxBytes - The ceiling attachmentRefusal held the file to
15868
- * @returns {string}
15869
- * @throws {TypeError} For a ceiling that is not a positive whole number of bytes
15870
- */
15871
- function refusalCopy(refusal, maxBytes) {
15872
- assertCeiling(maxBytes);
15873
- if (refusal === FILE_REFUSALS.TOO_LARGE) return tooLargeCopy(maxBytes);
15874
- return REFUSAL_COPY[refusal] ?? "This file can't be attached.";
15875
- }
15876
-
15877
- /**
15878
- * The products that send an organization's mail from a platform mail domain,
15879
- * and the labels under those domains the platform keeps for itself.
15880
- *
15881
- * Each product sends an organization's mail from `{handle}.{domain}`, the
15882
- * organization's own host under the product's mail domain, or from a domain
15883
- * the organization verified once it is ready for what the product uses it
15884
- * for. Core and the console read these facts here, so no surface names a
15885
- * product's domain, capabilities or From address its own way.
15886
- *
15887
- * BROWSER-SAFE: imports only ./platform-domains.js, which imports nothing.
15888
- */
15889
-
15890
-
15891
- /**
15892
- * Each product that sends an organization's mail from a platform mail domain:
15893
- * its key (also its tiered-product key and the suffix of its SES tenant's
15894
- * name), the domain (one SES identity, whose {handle}.{domain} subdomain is
15895
- * each organization's address), what it uses an organization's own domain
15896
- * for, what a domain needs ready before the product may select it, and the
15897
- * fixed local part it writes from (null when it varies: a help desk team
15898
- * writes from its alias). Every descriptor and array frozen.
15899
- */
15900
- const MAIL_PRODUCTS = Object.freeze({
15901
- help_desk: Object.freeze({
15902
- key: 'help_desk',
15903
- domain: SPARKVAULT_HELP_DESK_DOMAIN,
15904
- capabilities: Object.freeze(['web', 'sending', 'receiving']),
15905
- selectable: Object.freeze(['sending', 'web']),
15906
- fromLocalPart: null
15907
- }),
15908
- notify: Object.freeze({
15909
- key: 'notify',
15910
- domain: SPARKVAULT_NOTIFY_SEND_DOMAIN,
15911
- capabilities: Object.freeze(['sending']),
15912
- selectable: Object.freeze(['sending']),
15913
- fromLocalPart: 'notify'
15914
- })
15915
- });
15916
-
15917
- /** Every mail product's key, in MAIL_PRODUCTS order: the order every product-by-product decision runs in. */
15918
- const MAIL_PRODUCT_KEYS = Object.freeze(Object.keys(MAIL_PRODUCTS));
15919
-
15920
- /**
15921
- * GENERATED by infra/scripts/generate-organization-name-unicode.mjs from
15922
- * Unicode 18.0.0 (confusables.txt, UnicodeData.txt, DerivedBidiClass.txt,
15923
- * BidiBrackets.txt, DerivedAge.txt, DerivedNormalizationProps.txt). Do not
15924
- * edit: rerun the generator.
15925
- *
15926
- * The Unicode data @sparkvault/app-sdk/organization-name reads a text with.
15927
- * Code points are hexadecimal, separated by spaces; `first-last` is a range.
15928
- * The rule reads a text in NFC, so neither list names a character NFC
15929
- * replaces on every engine the rule runs on (Unicode 15.1 or older).
15930
- *
15931
- * BROWSER-SAFE: data only, no imports.
15932
- */
15933
-
15934
-
15935
- /**
15936
- * The Latin letters a character reads as, keyed by its reading: one group of
15937
- * letters per position, positions joined by '.' ('uv' is one letter, u or v;
15938
- * 'p.a' is two). A character NFKD already reads correctly on every engine the
15939
- * rule runs on is left out, as are the ASCII letters, which read as
15940
- * themselves.
15492
+ * The Latin letters a character reads as, keyed by its reading: one group of
15493
+ * letters per position, positions joined by '.' ('uv' is one letter, u or v;
15494
+ * 'p.a' is two). A character NFKD already reads correctly on every engine the
15495
+ * rule runs on is left out, as are the ASCII letters, which read as
15496
+ * themselves.
15941
15497
  */
15942
15498
  const LETTER_READINGS = {
15943
15499
  'a':
@@ -16250,450 +15806,1131 @@ const LETTER_READINGS = {
16250
15806
  };
16251
15807
 
16252
15808
  /**
16253
- * The characters that read as letters but that a reader may also pass over: a
16254
- * mark, drawn on the character before it; a superscript or subscript form,
16255
- * drawn small beside a word's letters; a character whose letters are a
16256
- * resemblance only (confusable with a character that is no ASCII letter, or
16257
- * with ASCII letters beside another character), and every hand-listed
16258
- * lookalike and character confusable with one, which a reader may take for
16259
- * the shape it is drawn as; a digit, punctuation mark or symbol that passes
16260
- * for a letter, or a letter UTS #39 makes confusable with one, which a reader
16261
- * may take for what it is drawn as; a bare vertical stroke (the vertical
16262
- * line, l, I, 1 and every character confusable with them), which a reader
16263
- * takes for a separator as readily as for a letter; and a character whose
16264
- * canonical decomposition a reader may pass over part by part (U+00CC, an I
16265
- * and a grave accent; a Hangul syllable of a bare stroke and jamo that read
16266
- * as no letter). Each is listed whether its reading is in LETTER_READINGS,
16267
- * comes from NFKD or is its own ASCII letter; a character that reads as no
16268
- * letter is passed over anyway.
15809
+ * The characters that read as letters but that a reader may also pass over: a
15810
+ * mark, drawn on the character before it; a superscript or subscript form,
15811
+ * drawn small beside a word's letters; a character whose letters are a
15812
+ * resemblance only (confusable with a character that is no ASCII letter, or
15813
+ * with ASCII letters beside another character), and every hand-listed
15814
+ * lookalike and character confusable with one, which a reader may take for
15815
+ * the shape it is drawn as; a digit, punctuation mark or symbol that passes
15816
+ * for a letter, or a letter UTS #39 makes confusable with one, which a reader
15817
+ * may take for what it is drawn as; a bare vertical stroke (the vertical
15818
+ * line, l, I, 1 and every character confusable with them), which a reader
15819
+ * takes for a separator as readily as for a letter; and a character whose
15820
+ * canonical decomposition a reader may pass over part by part (U+00CC, an I
15821
+ * and a grave accent; a Hangul syllable of a bare stroke and jamo that read
15822
+ * as no letter). Each is listed whether its reading is in LETTER_READINGS,
15823
+ * comes from NFKD or is its own ASCII letter; a character that reads as no
15824
+ * letter is passed over anyway.
15825
+ */
15826
+ const OPTIONAL_LETTERS =
15827
+ '21 24 25 2b 30 31 34 35 37 40 49 6c 7c a1-a3 a5 aa ae b5 b9 ba bc-be cc-cf d7 128 12a 12c 12e ' +
15828
+ '130 138 13a 13c 13e 140 142 190 196 197 19a 1bc 1c0 1c1 1c3 1cf 208 20a 245 25b 26b 26d 26e ' +
15829
+ '276 279-27b 28a 2a4 2a7 2a8 2b0-2b5 2b7 2b8 2e0-2e3 363-36f 376 377 37b 37d 38a 393 399 39b ' +
15830
+ '3aa 3ad 3b2 3b5 3ba 3bc 3c0 3c4 3d0 3d6 3f0 3f5 3fd 403 404 406 407 40d 413 414 418 419 41b ' +
15831
+ '426 42f 432 434 438-43a 43d 43f 442 446 44a 44b 44f 454 45c 45d 48a 48b 490 492 494 49b 49f ' +
15832
+ '4a3 4a7 4ad 4c0 4c5 4c8 4ca 4cf 4e2-4e5 4f6 4f9 4fa 4fe 50d 510-513 520 521 525 52e 52f 547 ' +
15833
+ '548 554 577 57a 586 58c 5c0 5d5 5df 5e7 5e9 5f0 5f1 609 60a 622 623 625 627 661 665 667 668 ' +
15834
+ '66a 672 673 675 6f1 6f5 6f7 6f8 7c0 7ca 964-966 9e6 a66 ae6 b66 be6 c02 c66 c82 ce6 d02 d5f ' +
15835
+ 'd66 d82 e04 e14 e15 e20 e22 e23 e26 e40 e41 e50 e8d ed0 1040 104a 104b 10a0 111b 111d 112b ' +
15836
+ '112c 1135 1141-1146 1148-114b 114d 1157 1162 1164 1166 1168 116b 116c 1170 1171 1174 1175 1180 ' +
15837
+ '1181 1185 1188 118a 118c 1190 1192 1194 1197-119d 11a1 11a7 11d5 11e2 11e6 11ec 11ed 11ef 11f1 ' +
15838
+ '11f2 11f4 1223 1260 13b1 13cb 13ce 13f0 13f8 13fb 13fc 1429 1431 143a-143d 144e 1457-145a 1467 ' +
15839
+ '1468 1476 1477 147a 147b 147e-1481 1486-1488 1498 1499 14a5 14ae 14af 14b6 14b7 14f0 14fa 14fb ' +
15840
+ '150e 1511 1515 1519 151a 1521 1522 152f 1530 1540 1575 157f 1581 1583 1584 1586 1591 1598 15b6 ' +
15841
+ '15e9 1609 166d 166e 1673 16c1 16c5 16d0 16ed 172a 17e0 18e8 1a80 1a90 1abf 1acc-1ace 1bea 1bec ' +
15842
+ '1c84 1d0b 1d0e 1d14 1d19 1d28 1d29 1d2c-1d2e 1d30 1d31 1d33-1d3a 1d3c 1d3e-1d43 1d45 1d47-1d49 ' +
15843
+ '1d4b 1d4d 1d4f-1d53 1d56-1d58 1d5a 1d5b 1d5d 1d5e 1d62-1d68 1d78 1d7a 1d7f 1d9c 1d9d 1da0-1da2 ' +
15844
+ '1da4-1db1 1db3 1db5-1db9 1dbb-1dbd 1dbf 1dca 1dd3-1dd8 1dda-1de9 1deb-1dee 1df0-1df4 1e2c 1e2e ' +
15845
+ '1e37 1e39 1e3b 1e3d 1ec8 1eca 1f10-1f15 1f38-1f3f 1f72 1fd8-1fda 2016 2020 2030 2031 203c 2048 ' +
15846
+ '2049 2052 2070 2071 2074 2075 2077 207a 207f-2081 2084 2085 2087 208a 2090-2093 2095-209f 20a1 ' +
15847
+ '20a4-20a6 20a8 20a9 20ab-20ae 20b1 20b3 20b4 20b6 20b8-20ba 20bd 20c1 2100 2101 2103 2105-2107 ' +
15848
+ '2109 2110 2111 2113 2116 2117 2120-2122 212e 213b 213c 213e 2150-2153 2155-215b 215d-217f 2183 ' +
15849
+ '2184 2189 218a 21bf 2205 2214 221a 221e 2223-2226 2228-222a 2296 229d 22a4 22c1-22c3 22f4 22ff ' +
15850
+ '2300 2361 2365 236c 2373 2374 2376-2378 237a 237f 239c 239f 23a2 23a5 23aa 23ae 23e8 23fd 2460 ' +
15851
+ '2463 2464 2466 2469-2474 2477 2478 247a 247d-2488 248b 248c 248e 2491-24ea 2502 2503 2573 2795 ' +
15852
+ '27d9 292b 292c 2963 296e 29b1-29b4 29e2 2a22-2a28 2a2f-2a31 2a40-2a45 2a52 2a5b 2a5d 2a62 2a63 ' +
15853
+ '2aee-2af1 2c6d 2c79 2c7c 2c7d 2c83 2c84 2c88 2c89 2c8f 2c92 2c95 2c9b 2ca1 2ca7 2cc0 2cd1 2ce8 ' +
15854
+ '2d00 2d37 2d4a 2d4f 2d51 2de1-2de3 2de6 2de8-2df0 2df2 2df4 2df6 2df7 2dfa 2dfb 2e36 2e37 2e85 ' +
15855
+ '2f01 2f0f 2f11 2f19 2f1b 2f59 3007 3012 3036 3072-3074 30a2 30a4 30ba 30e0 30ec 310e 3125 3129 ' +
15856
+ '3150 3152 3154 3156 3159 315a 315e 315f 3162 3163 3171 3178 3179 3182-3184 3188 3189 318b 318c ' +
15857
+ '318e 31d1 3207 3215 321d 321e 3250 3251 3254 3255 3257 325a 325b 325e 325f 3267 3275 327d 327e ' +
15858
+ '32b2 32b5-32c0 32c3 32c4 32c6 32c9-32d1 32f0 32f9 3300-3305 330a 330b 3315 3318-331a 331f 3320 ' +
15859
+ '332a 332d 332e 333d 3343 3344 3350 3355 3356 3358 3359 335c 335d 335f 3362-336d 3370-337a ' +
15860
+ '3380-33e0 33e3 33e4 33e6 33e9-33f4 33f7 33f8 33fa 33fd-33ff 4e02 4e28 4ebb 51e0 5200 5342 5369 ' +
15861
+ '53b6 5c3a 723f a00e a017 a018 a024 a072 a0f4 a224 a235 a263 a2d6 a2ea a2eb a317 a31a a34f a3dd ' +
15862
+ 'a407 a490 a492 a493 a4a4 a4a6 a4c4 a4c5 a4db a4e5 a4f2 a4f5 a516 a56f a613 a650 a651 a674-a679 ' +
15863
+ 'a67b a68d a69c a69d a6a1 a6ce a728-a72b a73f a742 a75a a75c a75d a768 a769 a772 a774-a776 a781 ' +
15864
+ 'a792 a793 a79d a7ae a7b5 a7da a7dc a7e2 a7f1-a7f4 a7f8 a7f9 a7fe a830 a8ce a8cf a96b a976 a977 ' +
15865
+ 'aa5d ab41 ab42 ab5d-ab5f ab62 ab68 ab6c ab70-ab72 ab7a-ab7c ab8b ab90 ab9b ab9e aba2 abae abb2 ' +
15866
+ 'abb6 ac1c-ac30 ac32-ac37 ac54-ac68 ac6a-ac6f ac8c-aca0 aca2-aca7 acc4-acd8 acda-acdf ad18-ad2c ' +
15867
+ 'ad2e-ad48 ad4a-ad4f ada4-adb8 adba-add4 add6-addb ae14-ae28 ae2a-ae44 ae46-ae4b ae68-ae7c ' +
15868
+ 'ae7e-ae83 aea0-aeb4 aeb6-aebb aed8-aeec aeee-aef3 af10-af24 af26-af2b af64-af78 af7a-af94 ' +
15869
+ 'af96-af9b aff0-b004 b006-b020 b022-b027 b060-b074 b076-b090 b092-b097 b0b4-b0c8 b0ca-b0cf ' +
15870
+ 'b0ec-b100 b102-b107 b124-b138 b13a-b13f b15c-b170 b172-b177 b1b0-b1c4 b1c6-b1e0 b1e2-b1e7 ' +
15871
+ 'b23c-b250 b252-b26c b26e-b273 b2ac-b2c0 b2c2-b2dc b2de-b2e3 b300-b314 b316-b31b b338-b34c ' +
15872
+ 'b34e-b353 b370-b384 b386-b38b b3a8-b3bc b3be-b3c3 b3fc-b410 b412-b42c b42e-b433 b488-b49c ' +
15873
+ 'b49e-b4b8 b4ba-b4bf b4f8-b50c b50e-b528 b52a-b52f b54c-b560 b562-b567 b584-b598 b59a-b59f ' +
15874
+ 'b5bc-b5d0 b5d2-b5d7 b5f4-b608 b60a-b60f b648-b65c b65e-b678 b67a-b67f b6d4-b6e8 b6ea-b704 ' +
15875
+ 'b706-b70b b744-b758 b75a-b774 b776-b77b b798-b7ac b7ae-b7b3 b7d0-b7e4 b7e6-b7eb b808-b81c ' +
15876
+ 'b81e-b823 b840-b854 b856-b85b b894-b8a8 b8aa-b8c4 b8c6-b8cb b920-b934 b936-b950 b952-b957 ' +
15877
+ 'b990-b9a4 b9a6-b9c0 b9c2-b9c7 b9e4-b9f8 b9fa-b9ff ba1c-ba30 ba32-ba37 ba54-ba68 ba6a-ba6f ' +
15878
+ 'ba8c-baa0 baa2-baa7 bae0-baf4 baf6-bb10 bb12-bb17 bb6c-bb80 bb82-bb9c bb9e-bba3 bbdc-bbf0 ' +
15879
+ 'bbf2-bc0c bc0e-bc13 bc30-bc44 bc46-bc4b bc68-bc7c bc7e-bc83 bca0-bcb4 bcb6-bcbb bcd8-bcec ' +
15880
+ 'bcee-bcf3 bd2c-bd40 bd42-bd5c bd5e-bd63 bdb8-bdcc bdce-bde8 bdea-bdef be28-be3c be3e-be58 ' +
15881
+ 'be5a-be5f be7c-be90 be92-be97 beb4-bec8 beca-becf beec-bf00 bf02-bf07 bf24-bf38 bf3a-bf3f ' +
15882
+ 'bf78-bf8c bf8e-bfa8 bfaa-bfaf c004-c018 c01a-c034 c036-c03b c074-c088 c08a-c0a4 c0a6-c0ab ' +
15883
+ 'c0c8-c0dc c0de-c0e3 c100-c114 c116-c11b c138-c14c c14e-c153 c170-c184 c186-c18b c1c4-c1d8 ' +
15884
+ 'c1da-c1f4 c1f6-c1fb c250-c264 c266-c280 c282-c287 c2c0-c2d4 c2d6-c2f0 c2f2-c2f7 c314-c328 ' +
15885
+ 'c32a-c32f c34c-c360 c362-c367 c384-c398 c39a-c39f c3bc-c3d0 c3d2-c3d7 c410-c424 c426-c440 ' +
15886
+ 'c442-c447 c49c-c4b0 c4b2-c4cc c4ce-c4d3 c50c-c520 c522-c53c c53e-c543 c7ac-c7c0 c7c2-c7c7 ' +
15887
+ 'c7e4-c7f8 c7fa-c7ff c81c-c830 c832-c837 c854-c868 c86a-c86f c8a8-c8bc c8be-c8d8 c8da-c8df ' +
15888
+ 'c934-c948 c94a-c964 c966-c96b c9a4-c9b8 c9ba-c9d4 c9d6-c9db c9f8-ca0c ca0e-ca13 ca30-ca44 ' +
15889
+ 'ca46-ca4b ca68-ca7c ca7e-ca83 caa0-cab4 cab6-cabb caf4-cb08 cb0a-cb24 cb26-cb2b cb80-cb94 ' +
15890
+ 'cb96-cbb0 cbb2-cbb7 cbf0-cc04 cc06-cc20 cc22-cc27 cc44-cc58 cc5a-cc5f cc7c-cc90 cc92-cc97 ' +
15891
+ 'ccb4-ccc8 ccca-cccf ccec-cd00 cd02-cd07 cd40-cd54 cd56-cd70 cd72-cd77 cdcc-cde0 cde2-cdfc ' +
15892
+ 'cdfe-ce03 ce3c-ce50 ce52-ce6c ce6e-ce73 ce90-cea4 cea6-ceab cec8-cedc cede-cee3 cf00-cf14 ' +
15893
+ 'cf16-cf1b cf38-cf4c cf4e-cf53 cf8c-cfa0 cfa2-cfbc cfbe-cfc3 d018-d02c d02e-d048 d04a-d04f ' +
15894
+ 'd088-d09c d09e-d0b8 d0ba-d0bf d0dc-d0f0 d0f2-d0f7 d114-d128 d12a-d12f d14c-d160 d162-d167 ' +
15895
+ 'd184-d198 d19a-d19f d1d8-d1ec d1ee-d208 d20a-d20f d264-d278 d27a-d294 d296-d29b d2d4-d2e8 ' +
15896
+ 'd2ea-d304 d306-d30b d328-d33c d33e-d343 d360-d374 d376-d37b d398-d3ac d3ae-d3b3 d3d0-d3e4 ' +
15897
+ 'd3e6-d3eb d424-d438 d43a-d454 d456-d45b d4b0-d4c4 d4c6-d4e0 d4e2-d4e7 d520-d534 d536-d550 ' +
15898
+ 'd552-d557 d574-d588 d58a-d58f d5ac-d5c0 d5c2-d5c7 d5e4-d5f8 d5fa-d5ff d61c-d630 d632-d637 ' +
15899
+ 'd670-d684 d686-d6a0 d6a2-d6a7 d6fc-d710 d712-d72c d72e-d733 d76c-d780 d782-d79c d79e-d7a3 d7b1 ' +
15900
+ 'd7b3 d7b6 d7b7 d7bb d7bd-d7c4 d7c6 d7dd d7eb d7f4 fb29 fbea fbeb fc37 fc80 fc88 fccd fd3c fd3d ' +
15901
+ 'fdf3 fdfc fe15 fe31 fe57 fe62 fe69-fe6b fe81-fe84 fe87 fe88 fe8d fe8e fef5-fefc ff01 ff04 ff05 ' +
15902
+ 'ff0b ff10 ff11 ff14 ff15 ff17 ff20 ff29 ff4c ff5c ff71 ff72 ff91 ff9a ffc3 ffc5 ffc7 ffcb ffce ' +
15903
+ 'ffcf ffd5 ffd6 ffdb ffdc ffe0 ffe1 ffe5 ffe6 ffe8 1017e 10182 1018b 1018e 10196-10199 101a0 ' +
15904
+ '1028a 1028d 1029b 102f5 10309 10320 10322 10401 10423 10425 10429 1042a 10443 1044b 1044d ' +
15905
+ '104b0 104bc 104d2 104e4 104fa 1050d-10510 10783-10785 10787-1078d 10790 10792-10796 ' +
15906
+ '10798-1079f 107a2 107a3 107a5-107b0 107b2 107b6 107b7 107b9 107ba 107bd 10926 10c3e 10ca5 ' +
15907
+ '10cfa 10cfc 11047 11048 11055 110c0 110c1 11141 11142 111c5 111c6 11302 113d4 113d5 1144b ' +
15908
+ '1144c 114d0 115c5 11641 11642 118af 118bb 118c6 118ce 118cf 118db 118e0 118e3 118e5 118e6 ' +
15909
+ '118e9 118ec 118ef 118f2 11c41 11c42 11dda 11de0 11de1 16869 16ad1 16ae4 16d63 16d69 16d6a ' +
15910
+ '16e80 16e82 16e8a 16eaa 16ec1 16f07 16f28 16f2d 16f3d 1cc90 1ccd6-1ccf1 1ccf4 1ccf5 1ccf7 ' +
15911
+ '1ce15 1cefc 1d100 1d134 1d1fe 1d202 1d207 1d20b-1d20d 1d212 1d213 1d216 1d21a 1d221 1d22a ' +
15912
+ '1d245 1d262 1d373 1d377 1d408 1d425 1d43c 1d459 1d470 1d48d 1d4c1 1d4d8 1d4f5 1d529 1d540 ' +
15913
+ '1d55d 1d574 1d591 1d5a8 1d5c5 1d5dc 1d5f9 1d610 1d62d 1d644 1d661 1d678 1d695 1d6aa 1d6b0 ' +
15914
+ '1d6b2 1d6c3 1d6c6 1d6cb 1d6cd 1d6d1 1d6d5 1d6dc 1d6de 1d6e1 1d6e4 1d6ea 1d6ec 1d6fd 1d700 ' +
15915
+ '1d705 1d707 1d70b 1d70f 1d716 1d718 1d71b 1d71e 1d724 1d726 1d737 1d73a 1d73f 1d741 1d745 ' +
15916
+ '1d749 1d750 1d752 1d755 1d758 1d75e 1d760 1d771 1d774 1d779 1d77b 1d77f 1d783 1d78a 1d78c ' +
15917
+ '1d78f 1d792 1d798 1d79a 1d7ab 1d7ae 1d7b3 1d7b5 1d7b9 1d7bd 1d7c4 1d7c6 1d7c9 1d7ce 1d7cf ' +
15918
+ '1d7d2 1d7d3 1d7d5 1d7d8 1d7d9 1d7dc 1d7dd 1d7df 1d7e2 1d7e3 1d7e6 1d7e7 1d7e9 1d7ec 1d7ed ' +
15919
+ '1d7f0 1d7f1 1d7f3 1d7f6 1d7f7 1d7fa 1d7fb 1d7fd 1df05 1df08 1df0a 1df12 1df15 1df17 1df19 ' +
15920
+ '1df1c 1df1f-1df23 1df2b 1df2c 1df3b 1df44 1df50 1df57 1df5b 1df5c 1df62 1df63 1df67 ' +
15921
+ '1df6c-1df6e 1dfcd-1dfd7 1dfd9-1dfdd 1dfe0 1dfe1 1dfe4 1dfe5 1dfe8-1dfea 1dfef-1dff2 ' +
15922
+ '1dff5-1dfff 1e030 1e032-1e035 1e038 1e039 1e03c-1e041 1e043 1e044 1e046 1e047 1e049 ' +
15923
+ '1e04c-1e051 1e053-1e056 1e059 1e05a 1e05c-1e05f 1e061 1e062 1e064-1e06d 1e08f 1e140 1e141 ' +
15924
+ '1e145 1e2f0 1e6e9 1e8c7 1ec75 1ec83 1ec86 1ec87 1ec89 1ec9e 1ec9f 1eca2 1eca3 1ecb1 1ecb2 ' +
15925
+ '1ed01 1ed13 1ed31 1ed32 1ed36 1ed37 1ee00 1ee61 1ee62 1ee67-1ee6a 1ee6c-1ee72 1ee74-1ee77 ' +
15926
+ '1ee79-1ee7c 1ee7e 1ee80 1f100-1f102 1f105 1f106 1f108 1f10f-1f12e 1f130-1f16e 1f170-1f198 ' +
15927
+ '1f19a 1f1a5-1f1a7 1f1a9-1f1ac 1f1e6-1f1ff 1f700 1f707 1f708 1f714 1f74c 1f75c 1f768 1f76b ' +
15928
+ '1f76c 1fbf0 1fbf1 1fbf4 1fbf5 1fbf7';
15929
+
15930
+ /**
15931
+ * Each bidirectional class other than L, keyed by its UAX #9 short name: the
15932
+ * code points that hold it. Every other code point is L. The explicit
15933
+ * formatting characters are left out: the rule refuses them before it lays a
15934
+ * text out.
15935
+ */
15936
+ const BIDI_CLASSES = {
15937
+ AL:
15938
+ '608 60b 60d 61b-64a 66d-66f 671-6d5 6e5-6e6 6ee-6ef 6fa-710 712-72f 74b-7a5 7b1-7bf 860-88f ' +
15939
+ '892-896 8a0-8c9 fb50-fbc2 fbd3-fd3d fd50-fd8f fd92-fdc7 fdf0-fdfc fe70-fefe 10d00-10d23 ' +
15940
+ '10d28-10d2f 10d3a-10d3f 10ec0-10eca 10ed9-10eef 10f30-10f45 10f51-10f6f 1ec70-1ecbf ' +
15941
+ '1ed00-1ed4f 1ee00-1eeef 1eef2-1eeff',
15942
+ AN:
15943
+ '600-605 660-669 66b-66c 6dd 890-891 8e2 10d30-10d39 10d40-10d49 10e60-10e7e',
15944
+ B:
15945
+ 'a d 1c-1e 85 2029',
15946
+ BN:
15947
+ '0-8 e-1b 7f-84 86-9f ad 180e 200b-200d 2060-2065 206a-206f fdd0-fdef feff fff0-fff8 ' +
15948
+ 'fffe-ffff 1bca0-1bca3 1d173-1d17a 1fffe-1ffff 2fffe-2ffff 3fffe-3ffff 4fffe-4ffff ' +
15949
+ '5fffe-5ffff 6fffe-6ffff 7fffe-7ffff 8fffe-8ffff 9fffe-9ffff afffe-affff bfffe-bffff ' +
15950
+ 'cfffe-cffff dfffe-e00ff e01f0-e0fff efffe-effff ffffe-fffff 10fffe-10ffff',
15951
+ CS:
15952
+ '2c 2e-2f 3a a0 60c 202f 2044 fe50 fe52 fe55 ff0c ff0e-ff0f ff1a',
15953
+ EN:
15954
+ '30-39 b2-b3 b9 6f0-6f9 2070 2074-2079 2080-2089 2488-249b ff10-ff19 102e1-102fb 1ccf0-1ccf9 ' +
15955
+ '1d7ce-1d7ff 1f100-1f10a 1fbf0-1fbf9',
15956
+ ES:
15957
+ '2b 2d 207a-207b 208a-208b 2212 fb29 fe62-fe63 ff0b ff0d',
15958
+ ET:
15959
+ '23-25 a2-a5 b0-b1 58f 609-60a 66a 9f2-9f3 9fb af1 bf9 e3f 17db 2030-2034 20a0-20cf 212e 2213 ' +
15960
+ 'a838-a839 fe5f fe69-fe6a ff03-ff05 ffe0-ffe1 ffe5-ffe6 11fdd-11fe0 1e2ff',
15961
+ NSM:
15962
+ '300-36f 483-489 591-5bd 5bf 5c1-5c2 5c4-5c5 5c7-5c9 610-61a 64b-65f 670 6d6-6dc 6df-6e4 ' +
15963
+ '6e7-6e8 6ea-6ed 711 730-74a 7a6-7b0 7eb-7f3 7fd 816-819 81b-823 825-827 829-82d 859-85b ' +
15964
+ '897-89f 8ca-8e1 8e3-902 93a 93c 941-948 94d 951-957 962-963 981 9bc 9c1-9c4 9cd 9e2-9e3 9fe ' +
15965
+ 'a01-a02 a3c a41-a42 a47-a48 a4b-a4d a51 a70-a71 a75 a81-a82 abc ac1-ac5 ac7-ac8 acd ae2-ae3 ' +
15966
+ 'afa-aff b01 b3c b3f b41-b44 b4d b53-b56 b62-b63 b82 bc0 bcd c00 c04 c3c c3e-c40 c46-c48 ' +
15967
+ 'c4a-c4d c55-c56 c62-c63 c81 cbc ccc-ccd ce2-ce3 d00-d01 d3b-d3c d41-d44 d4d d62-d63 d81 dca ' +
15968
+ 'dd2-dd4 dd6 e31 e34-e3a e47-e4e eb1 eb4-ebc ec8-ece f18-f19 f35 f37 f39 f71-f7e f80-f84 ' +
15969
+ 'f86-f87 f8d-f97 f99-fbc fc6 102d-1030 1032-1037 1039-103a 103d-103e 1058-1059 105e-1060 ' +
15970
+ '1071-1074 1082 1085-1086 108d 109d 135d-135f 1712-1714 1732-1733 1752-1753 1772-1773 ' +
15971
+ '17b4-17b5 17b7-17bd 17c6 17c9-17d3 17dd 180b-180d 180f 1885-1886 18a9 1920-1922 1927-1928 ' +
15972
+ '1932 1939-193b 1a17-1a18 1a1b 1a56 1a58-1a5e 1a60 1a62 1a65-1a6c 1a73-1a7c 1a7f 1ab0-1af0 ' +
15973
+ '1b00-1b03 1b34 1b36-1b39 1b6b-1b73 1b80-1b81 1ba2-1ba5 1ba8-1ba9 1bab-1bad 1be6 1be8-1be9 ' +
15974
+ '1bed 1bef-1bf1 1c2c-1c33 1c36-1c37 1cd0-1cd2 1cd4-1ce0 1ce2-1ce8 1ced 1cf4 1cf8-1cf9 ' +
15975
+ '1dc0-1dff 20d0-20f0 2cef-2cf1 2d7f 2de0-2dff 302a-302d 3099-309a a66f-a672 a674-a67d ' +
15976
+ 'a69e-a69f a6f0-a6f1 a802 a806 a80b a825-a826 a82c a8c4-a8c5 a8e0-a8f1 a8ff a926-a92d ' +
15977
+ 'a947-a951 a980-a982 a9b3 a9b6-a9b9 a9bc-a9bd a9e5 aa29-aa2e aa31-aa32 aa35-aa36 aa43 aa4c ' +
15978
+ 'aa7c aab0 aab2-aab4 aab7-aab8 aabe-aabf aac1 aaec-aaed aaf6 abe5 abe8 abed fb1e fe00-fe0f ' +
15979
+ 'fe20-fe2f 101fd 102e0 10376-1037a 10a01-10a03 10a05-10a06 10a0c-10a0f 10a38-10a3a 10a3f ' +
15980
+ '10ae5-10ae6 10d24-10d27 10d69-10d6d 10eab-10eac 10ecb-10ecf 10ef0-10eff 10f46-10f50 ' +
15981
+ '10f82-10f85 11001 11038-11046 11070 11073-11074 1107f-11081 110b3-110b6 110b9-110ba 110c2 ' +
15982
+ '11100-11102 11127-1112b 1112d-11134 11173 11180-11181 111b6-111be 111c9-111cc 111cf ' +
15983
+ '1122f-11231 11234 11236-11237 1123e 11241 112df 112e3-112ea 11300-11301 1133b-1133c 11340 ' +
15984
+ '11366-1136c 11370-11374 113bb-113c0 113ce 113d0 113d2 113e1-113e2 11438-1143f 11442-11444 ' +
15985
+ '11446 1145e 114b3-114b8 114ba 114bf-114c0 114c2-114c3 115b2-115b5 115bc-115bd 115bf-115c0 ' +
15986
+ '115dc-115dd 11633-1163a 1163d 1163f-11640 116ab 116ad 116b0-116b5 116b7 1171d 1171f ' +
15987
+ '11722-11725 11727-1172b 1182f-11837 11839-1183a 1193b-1193c 1193e 11943 119d4-119d7 ' +
15988
+ '119da-119db 119e0 11a01-11a06 11a09-11a0a 11a33-11a38 11a3b-11a3e 11a47 11a51-11a56 ' +
15989
+ '11a59-11a5b 11a8a-11a96 11a98-11a99 11b60 11b62-11b64 11b66 11c30-11c36 11c38-11c3d ' +
15990
+ '11c92-11ca7 11caa-11cb0 11cb2-11cb3 11cb5-11cb6 11d31-11d36 11d3a 11d3c-11d3d 11d3f-11d45 ' +
15991
+ '11d47 11d90-11d91 11d95 11d97 11df0 11ef3-11ef4 11f00-11f01 11f36-11f3a 11f40 11f42 11f5a ' +
15992
+ '13440 13447-13455 1611e-16129 1612d-1612f 16af0-16af4 16b30-16b36 16f4f 16f8f-16f92 16fe4 ' +
15993
+ '1bc9d-1bc9e 1cf00-1cf2d 1cf30-1cf46 1d127-1d128 1d167-1d169 1d17b-1d182 1d185-1d18b ' +
15994
+ '1d1aa-1d1ad 1d242-1d244 1d25b-1d25c 1da00-1da36 1da3b-1da6c 1da75 1da84 1da9b-1da9f ' +
15995
+ '1daa1-1daaf 1e000-1e006 1e008-1e018 1e01b-1e021 1e023-1e024 1e026-1e02a 1e08f 1e130-1e136 ' +
15996
+ '1e2ae 1e2ec-1e2ef 1e4ec-1e4ef 1e5ee-1e5ef 1e6e3 1e6e6 1e6ee-1e6ef 1e6f5 1e8d0-1e8d6 ' +
15997
+ '1e944-1e94a e0100-e01ef',
15998
+ ON:
15999
+ '21-22 26-2a 3b-40 5b-60 7b-7e a1 a6-a9 ab-ac ae-af b4 b6-b8 bb-bf d7 f7 2b9-2ba 2c2-2cf ' +
16000
+ '2d2-2df 2e5-2ed 2ef-2ff 374-375 37e 384-385 387 3f6 58a 58d-58e 606-607 60e-60f 6de 6e9 ' +
16001
+ '7f6-7f9 bf3-bf8 bfa c78-c7e f3a-f3d 1390-1399 1400 169b-169c 17f0-17f9 1800-180a 1940 ' +
16002
+ '1944-1945 19de-19ff 1fbd 1fbf-1fc1 1fcd-1fcf 1fdd-1fdf 1fed-1fef 1ffd-1ffe 2010-2027 ' +
16003
+ '2035-2043 2045-205e 207c-207e 208c-208f 2100-2101 2103-2106 2108-2109 2114 2116-2118 ' +
16004
+ '211e-2123 2125 2127 2129 213a-213b 2140-2144 214a-214d 2150-215f 2189-218b 2190-2211 ' +
16005
+ '2214-2335 237b-2394 2396-2429 2440-244a 2460-2487 24ea-26ab 26ad-27ff 2900-2b73 2b76-2bff ' +
16006
+ '2ce5-2cea 2cf9-2cff 2e00-2e5d 2e60-2e63 2e80-2e99 2e9b-2ef3 2f00-2fd5 2ff0-2fff 3001-3004 ' +
16007
+ '3008-3020 3030 3036-3037 303d-303f 309b-309c 30a0 30fb 31c0-31e5 31ef 321d-321e 3250-325f ' +
16008
+ '327c-327e 32b1-32bf 32cc-32cf 3377-337a 33de-33df 33ff 4dc0-4dff a490-a4c6 a60d-a60f a673 ' +
16009
+ 'a67e-a67f a700-a721 a788 a828-a82b a874-a877 ab6a-ab6b fbc3-fbd2 fd3e-fd4f fd90-fd91 ' +
16010
+ 'fdc8-fdcf fdfd-fdff fe10-fe19 fe30-fe4f fe51 fe54 fe56-fe5e fe60-fe61 fe64-fe66 fe68 fe6b ' +
16011
+ 'ff01-ff02 ff06-ff0a ff1b-ff20 ff3b-ff40 ff5b-ff65 ffe2-ffe4 ffe8-ffee fff9-fffd 10101 ' +
16012
+ '10140-1018c 10190-1019c 101a0 1091f 10b39-10b3f 10d6e 10ed0-10ed8 11052-11065 11660-1166c ' +
16013
+ '11fd5-11fdc 11fe1-11ff1 16fe2 1cc00-1ccd5 1ccfa-1ccfc 1cd00-1ceb3 1ceba-1ced0 1ced2-1ced4 ' +
16014
+ '1cedd-1cefd 1d1e9-1d1ea 1d200-1d241 1d245 1d300-1d356 1d6c1 1d6db 1d6fb 1d715 1d735 1d74f ' +
16015
+ '1d76f 1d789 1d7a9 1d7c3 1db00-1db1c 1eef0-1eef1 1f000-1f02b 1f030-1f093 1f0a0-1f0ae ' +
16016
+ '1f0b1-1f0bf 1f0c1-1f0cf 1f0d1-1f0f5 1f10b-1f10f 1f12f 1f16a-1f16f 1f1ad 1f260-1f265 ' +
16017
+ '1f300-1f6d9 1f6dc-1f6ec 1f6f0-1f6fc 1f700-1f7d9 1f7db 1f7e0-1f7eb 1f7f0-1f80b 1f810-1f847 ' +
16018
+ '1f850-1f859 1f860-1f887 1f890-1f8ad 1f8b0-1f8bb 1f8c0-1f8c1 1f8d0-1f8d8 1f900-1fa57 ' +
16019
+ '1fa60-1fa6d 1fa70-1fa7c 1fa80-1fac6 1fac8 1facc-1fadd 1fadf-1faeb 1faef-1fafa 1fb00-1fb92 ' +
16020
+ '1fb94-1fbef 1fbfa',
16021
+ R:
16022
+ '590 5be 5c0 5c3 5c6 5ca-5ff 7c0-7ea 7f4-7f5 7fa-7fc 7fe-815 81a 824 828 82e-858 85c-85f 200f ' +
16023
+ 'fb1d fb1f-fb28 fb2a-fb4f 10800-1091e 10920-10a00 10a04 10a07-10a0b 10a10-10a37 10a3b-10a3e ' +
16024
+ '10a40-10ae4 10ae7-10b38 10b40-10cff 10d4a-10d68 10d6f-10e5f 10e7f-10eaa 10ead-10ebf ' +
16025
+ '10f00-10f2f 10f70-10f81 10f86-10fff 1e800-1e8cf 1e8d7-1e943 1e94b-1ec6f 1ecc0-1ecff ' +
16026
+ '1ed50-1edff 1ef00-1efff',
16027
+ S:
16028
+ '9 b 1f',
16029
+ WS:
16030
+ 'c 20 1680 2000-200a 2028 205f 3000'
16031
+ };
16032
+
16033
+ /** UAX #9's paired brackets (rule N0), as `opening:closing`. */
16034
+ const BRACKET_PAIRS =
16035
+ '28:29 5b:5d 7b:7d f3a:f3b f3c:f3d 169b:169c 2045:2046 207d:207e 208d:208e 2308:2309 230a:230b ' +
16036
+ '2329:232a 2768:2769 276a:276b 276c:276d 276e:276f 2770:2771 2772:2773 2774:2775 27c5:27c6 ' +
16037
+ '27e6:27e7 27e8:27e9 27ea:27eb 27ec:27ed 27ee:27ef 2983:2984 2985:2986 2987:2988 2989:298a ' +
16038
+ '298b:298c 298d:2990 298f:298e 2991:2992 2993:2994 2995:2996 2997:2998 29d8:29d9 29da:29db ' +
16039
+ '29fc:29fd 2e22:2e23 2e24:2e25 2e26:2e27 2e28:2e29 2e55:2e56 2e57:2e58 2e59:2e5a 2e5b:2e5c ' +
16040
+ '2e62:2e63 3008:3009 300a:300b 300c:300d 300e:300f 3010:3011 3014:3015 3016:3017 3018:3019 ' +
16041
+ '301a:301b fe59:fe5a fe5b:fe5c fe5d:fe5e ff08:ff09 ff3b:ff3d ff5b:ff5d ff5f:ff60 ff62:ff63';
16042
+
16043
+ /**
16044
+ * Whether an organization's name, or any text, reads as SparkVault's own.
16045
+ *
16046
+ * An organization's name is the author a recipient reads on its mail:
16047
+ * Identity's sign-in codes leave the platform's sign-in mailbox under it, and
16048
+ * Notify relays an organization's message from "{name}"
16049
+ * <notify@{handle}.send.sv>. A name that reads as SparkVault or SparkLink
16050
+ * would author that mail as the platform, a name that carries an email
16051
+ * address would author it as whoever that address names, and a name that
16052
+ * reads as a web address would show a link where its author belongs.
16053
+ *
16054
+ * A reader reads letters, not code points, and reads them in the order they
16055
+ * are laid out, so a text is judged the way it is read:
16056
+ * - in each form a surface shows it in: a lone surrogate, a code unit no
16057
+ * other pairs with (a JSON escape such as \ud800 puts one in a string), is
16058
+ * shown as U+FFFD REPLACEMENT CHARACTER where the text is carried as UTF-8
16059
+ * (a From line's encoded-word), and as itself where a JSON answer carries
16060
+ * it (the answer writes the escape \ud800, and a JavaScript client's
16061
+ * JSON.parse restores the surrogate), which the client lays out as the
16062
+ * left-to-right character Unicode classes it as; U+FFFD is a neutral,
16063
+ * which joins the right-to-left run around it, and the surrogate splits
16064
+ * that run, so a text that holds one is read both ways (formsShown);
16065
+ * - in NFC: canonically equivalent texts show alike, so the rule reads one
16066
+ * text for all of them, a precomposed character for the character and marks
16067
+ * it decomposes to (U+00CC, an I with a grave accent, for I and U+0300
16068
+ * COMBINING GRAVE ACCENT) and a Hangul syllable for its jamo, each composed
16069
+ * character a reader may pass over wherever they may pass over its parts;
16070
+ * - each character as the Latin letters it passes for
16071
+ * (./organization-name-unicode.js LETTER_READINGS, derived from Unicode's
16072
+ * confusables, names and compatibility decompositions and a hand list of
16073
+ * lookalikes, the alphabets fancy-text generators substitute for Latin ones
16074
+ * among them; any other character through NFKD), a character at times
16075
+ * reading as either of two letters (U+028B, a v with a hook, is a u or a v),
16076
+ * and i and l as one letter, since a capital I and a small l are one glyph
16077
+ * in most sans-serif faces;
16078
+ * - a character that reads as no letter (a space, punctuation, an invisible
16079
+ * character) passed over, so it never breaks a word; the marks drawn on a
16080
+ * character, each read just before that character or just after it, in any
16081
+ * order, or not at all; and a character a reader may take for a letter or
16082
+ * pass over, read where it stands or not at all (OPTIONAL_LETTERS): a
16083
+ * superscript or subscript form, drawn small beside a word's letters; a
16084
+ * character whose letters are a resemblance only, a fancy-text generator's
16085
+ * letter among them (U+A317 YI SYLLABLE SOT, an s, is drawn as U+00A7
16086
+ * SECTION SIGN); a digit, punctuation mark or symbol that passes for a
16087
+ * letter (`1`, `$`, `+`), or a letter drawn as one; and a bare vertical
16088
+ * stroke (`|`, `l`, `I`), which reads as a separator as readily as an l; so
16089
+ * none of those breaks a word either, and a character breaks one only where
16090
+ * a reader must read it as letters: it is a form of them, or UTS #39 draws
16091
+ * it as Latin letters alone;
16092
+ * - in every order it can be laid out in: as stored, and as the Unicode
16093
+ * bidirectional algorithm (UAX #9) lays it out in a left-to-right and in a
16094
+ * right-to-left paragraph (an inbox in a right-to-left language, or a
16095
+ * client that takes the direction from the text's first letter), where a
16096
+ * right-to-left letter or mark, or an Arabic digit, between Latin words
16097
+ * reverses their order: "Vault", U+05BE HEBREW PUNCTUATION MAQAF, "Spark" is
16098
+ * laid out in a right-to-left paragraph as "Spark", the maqaf, "Vault".
16099
+ * An explicit direction control (holdsDirectionControl, ./display-text.js)
16100
+ * sets the direction of the text after it beyond what this lays out, so a
16101
+ * caller that judges what a reader sees refuses it as well, as
16102
+ * isReservedDisplayText, for any text an organization shows under its name,
16103
+ * and isReservedOrganizationName do. Both also judge a text at every cut of it
16104
+ * as stored, each cut read in this way, since the platform shows a text cut to
16105
+ * fit. The implicit marks are no such control: each is a strong character of
16106
+ * its direction, laid out as a letter of that direction is.
16107
+ *
16108
+ * BROWSER-SAFE: imports only ./display-text.js and
16109
+ * ./organization-name-unicode.js, neither of which imports anything, and needs
16110
+ * String.prototype.isWellFormed and toWellFormed (ES2024: Chrome 111,
16111
+ * Firefox 119, Safari 16.4).
16112
+ */
16113
+
16114
+ /** The words no organization's name may read as: the platform's brand and its link product. */
16115
+ const RESERVED_BRAND_TOKENS = Object.freeze(['sparkvault', 'sparklink']);
16116
+
16117
+ /** i and l read as one letter. */
16118
+ const foldLetter = (letter) => (letter === 'i' ? 'l' : letter);
16119
+
16120
+ /** The letters of the reserved words, each a bit of a position's mask. */
16121
+ const BRAND_BITS = new Map(
16122
+ [...new Set(Array.from(RESERVED_BRAND_TOKENS.join(''), foldLetter))].map((letter, index) => [letter, 1 << index])
16123
+ );
16124
+
16125
+ /** The brand letters a group of letters can read as, as a mask: 0 for a letter of no reserved word. */
16126
+ function lettersMask(letters) {
16127
+ let mask = 0;
16128
+ for (const letter of letters) mask |= BRAND_BITS.get(foldLetter(letter)) ?? 0;
16129
+ return mask;
16130
+ }
16131
+
16132
+ /** Each reserved word as the masks its letters need. */
16133
+ const TOKEN_MASKS = RESERVED_BRAND_TOKENS.map((token) => Array.from(token, lettersMask));
16134
+
16135
+ /** Each code point a generated list names: hexadecimal, separated by spaces, `first-last` a range. */
16136
+ function* codePointsOf(list) {
16137
+ for (const entry of list.split(' ')) {
16138
+ const [first, last = first] = entry.split('-');
16139
+ for (let codePoint = parseInt(first, 16); codePoint <= parseInt(last, 16); codePoint++) yield codePoint;
16140
+ }
16141
+ }
16142
+
16143
+ /** Each listed character's reading: one mask per letter position. */
16144
+ const LISTED_READINGS = new Map();
16145
+ for (const [reading, list] of Object.entries(LETTER_READINGS)) {
16146
+ const positions = Object.freeze(reading.split('.').map(lettersMask));
16147
+ for (const codePoint of codePointsOf(list)) LISTED_READINGS.set(codePoint, positions);
16148
+ }
16149
+
16150
+ /** The characters whose letters a reader may read or pass over. */
16151
+ new Set(codePointsOf(OPTIONAL_LETTERS));
16152
+
16153
+ /** Every bidirectional class other than L, as sorted [first, last, class] ranges. */
16154
+ Object.entries(BIDI_CLASSES)
16155
+ .flatMap(([bidiClass, list]) => list.split(' ').map((entry) => {
16156
+ const [first, last = first] = entry.split('-');
16157
+ return [parseInt(first, 16), parseInt(last, 16), bidiClass];
16158
+ }))
16159
+ .sort((a, b) => a[0] - b[0]);
16160
+
16161
+ /** Each opening paired bracket's closing bracket, and the reverse (UAX #9 BD16). */
16162
+ const CLOSING_BRACKET = new Map();
16163
+ const OPENING_BRACKET = new Map();
16164
+ for (const pair of BRACKET_PAIRS.split(' ')) {
16165
+ const [opening, closing] = pair.split(':').map((codePoint) => parseInt(codePoint, 16));
16166
+ CLOSING_BRACKET.set(opening, closing);
16167
+ OPENING_BRACKET.set(closing, opening);
16168
+ }
16169
+
16170
+ /**
16171
+ * A reading's progress, as one bit set (a bit-parallel search): for each
16172
+ * reserved word a bit per letter, set while the letters read so far end in the
16173
+ * word spelled up to that letter, the words' bits side by side from
16174
+ * WORD_OFFSETS; and SPELLED, set once any word has been spelled whole. Two
16175
+ * ways of reading a text merge as the union of their bits.
16176
+ */
16177
+ const WORD_OFFSETS = TOKEN_MASKS.map((_, word) =>
16178
+ TOKEN_MASKS.slice(0, word).reduce((offset, token) => offset + token.length, 0));
16179
+
16180
+ /** Each word's first letter, where a new spelling can start at any letter read. */
16181
+ WORD_OFFSETS.reduce((bits, offset) => bits | (1 << offset), 0);
16182
+
16183
+ /** Each word's last letter. */
16184
+ TOKEN_MASKS.reduce((bits, token, word) => bits | (1 << (WORD_OFFSETS[word] + token.length - 1)), 0);
16185
+
16186
+ 1 << TOKEN_MASKS.reduce((count, token) => count + token.length, 0);
16187
+
16188
+ /** For each mask of brand letters, the letters of the words a letter of it can be. */
16189
+ Array.from({ length: 1 << BRAND_BITS.size }, (_, mask) =>
16190
+ TOKEN_MASKS.reduce((bits, token, word) => token.reduce((wordBits, letters, position) =>
16191
+ ((letters & mask) === 0 ? wordBits : wordBits | (1 << (WORD_OFFSETS[word] + position))), bits), 0));
16192
+
16193
+ /**
16194
+ * The Secure Help Desk's shared vocabulary: the values Core's help-desk engine
16195
+ * stores and checks, and the console and the customer portal render, defined
16196
+ * once so the three cannot disagree about a status, a label, a limit, a timing
16197
+ * or an event's visibility.
16198
+ *
16199
+ * A ticket is `open`, `waiting_on_customer`, `on_hold` (until a time) or
16200
+ * `closed` (as resolved, a duplicate merged elsewhere, or spam). A customer
16201
+ * never sees those words: customerStatusLabel is the one translation into
16202
+ * theirs. Every timeline event type declares whether a customer may see it;
16203
+ * a customer-facing projection keeps only `public` types (an allowlist, so a
16204
+ * new type is hidden until it says otherwise). The portal's client name and
16205
+ * API prefix, team mail aliases, the attachment ceremony's steps, the
16206
+ * question a customer's new request asks before it is discarded, session,
16207
+ * sign-in code (its length, lifetime, sender, resend wait, countdown and
16208
+ * the code step's words),
16209
+ * merge and pending-attachment timings, hold presets, tag and size limits,
16210
+ * the operator display and desk mode choices and the response-time
16211
+ * histogram's bucket edges live here for the same reason. So do the one rule a tag is stored by
16212
+ * (ticketTag), which Core applies where it stores a tag and the console
16213
+ * applies where one is typed, and the one rule a typed or pasted sign-in
16214
+ * code is read by (helpDeskPinDigits), which every sign-in form applies.
16215
+ *
16216
+ * BROWSER-SAFE: data and pure functions; its imports are the platform's label
16217
+ * rule a tag is built on (./display-text.js), the mail products whose fixed
16218
+ * From local parts no team alias may take (./mail-products.js) and the brand
16219
+ * rule no team alias may read as (./organization-name.js).
16220
+ */
16221
+
16222
+
16223
+ /**
16224
+ * Mail local parts no team may take as its alias: the role mailboxes RFC 2142
16225
+ * names, the addresses mail systems and their operators keep for themselves,
16226
+ * the platform's own host labels, and the fixed local part every other mail
16227
+ * product writes from (MAIL_PRODUCTS fromLocalPart), so no team writes from
16228
+ * the address Notify writes from on a domain both products use.
16229
+ */
16230
+ Object.freeze([
16231
+ 'abuse', 'postmaster', 'hostmaster', 'webmaster', 'noc', 'security', 'info', 'marketing', 'sales',
16232
+ 'admin', 'administrator', 'root', 'mailer-daemon', 'no-reply', 'noreply', 'bounce', 'bounces',
16233
+ 'dmarc', 'spf', 'dkim', 'probe',
16234
+ 'www', 'api', 'mail', 'smtp', 'mx', 'help', 'sparkvault',
16235
+ ...MAIL_PRODUCT_KEYS.map((key) => MAIL_PRODUCTS[key].fromLocalPart).filter((localPart) => localPart !== null)
16236
+ ]);
16237
+
16238
+ /** The alias a team's mail goes out under until its admin chooses another. */
16239
+ const DEFAULT_TEAM_ALIAS = 'support';
16240
+
16241
+ /**
16242
+ * What a customer's new request asks before Cancel throws away what they
16243
+ * wrote and the files they added: the portal's New request page and the SDK's
16244
+ * support form ask it in place of their actions row, with Keep editing first,
16245
+ * as the primary button, focused as the question appears, and Discard after
16246
+ * it as the quieter one. Discard sends nothing, forgets the text and cancels
16247
+ * each file's upload, so `note` promises no more than both forms do.
16248
+ */
16249
+ const DISCARD_REQUEST_COPY = Object.freeze({
16250
+ question: 'Discard this request?',
16251
+ note: 'What you wrote and any files you added will not be sent.',
16252
+ keepEditing: 'Keep editing',
16253
+ discard: 'Discard'
16254
+ });
16255
+
16256
+ /**
16257
+ * What the emailed-code step of a customer's sign-in says, word for word in
16258
+ * the portal's sign-in page and the SDK's support form: the refusal of a code
16259
+ * that is wrong, spent or expired (Core's PIN_INVALID), and the note once a
16260
+ * new code is sent. Neither names who sent the email, so both read right on
16261
+ * the portal and on a company's own site.
16262
+ */
16263
+ const HELP_DESK_CODE_STEP_COPY = Object.freeze({
16264
+ codeRefused: 'That code is wrong or has expired. Check the newest email, or send a new code.',
16265
+ newCodeSent: 'We sent a new code. Use the newest one.'
16266
+ });
16267
+
16268
+ /** How long an emailed sign-in code stays good, in seconds. */
16269
+ const HELP_DESK_PIN_TTL_SECONDS = 600;
16270
+
16271
+ /** How long an emailed sign-in code stays good, in whole minutes: what the sign-in forms, the code's email and the API reference state. */
16272
+ const HELP_DESK_PIN_TTL_MINUTES = Math.round(HELP_DESK_PIN_TTL_SECONDS / 60);
16273
+
16274
+ /** How many digits an emailed sign-in code has. */
16275
+ const HELP_DESK_PIN_LENGTH = 6;
16276
+
16277
+ /** How long after a code is sent before a sign-in form offers to send another, in seconds. */
16278
+ const HELP_DESK_PIN_RESEND_AFTER_SECONDS = 60;
16279
+
16280
+ /**
16281
+ * The digits of a sign-in code as a person typed or pasted it, at most
16282
+ * HELP_DESK_PIN_LENGTH of them: "123 456" and "Your code: 123-456" both read
16283
+ * "123456", so a code pasted with separators is never cut short.
16284
+ *
16285
+ * @param {unknown} value
16286
+ * @returns {string}
16287
+ */
16288
+ function helpDeskPinDigits(value) {
16289
+ return String(value).replace(/\D/g, '').slice(0, HELP_DESK_PIN_LENGTH);
16290
+ }
16291
+
16292
+ /**
16293
+ * The address a desk's emailed sign-in codes come from: its default team
16294
+ * alias on the host the desk sends from (the desk lookup's `sending_host`).
16295
+ *
16296
+ * @param {string} sendingHost
16297
+ * @returns {string}
16298
+ */
16299
+ function helpDeskCodeSender(sendingHost) {
16300
+ return `${DEFAULT_TEAM_ALIAS}@${sendingHost}`;
16301
+ }
16302
+
16303
+ /**
16304
+ * A wait as the sign-in forms count it down before another code may be sent:
16305
+ * whole minutes and two-digit seconds, "0:42", "1:00". A part second rounds
16306
+ * down and a wait past its end reads "0:00".
16307
+ *
16308
+ * @param {number} seconds
16309
+ * @returns {string}
16310
+ */
16311
+ function helpDeskCountdown(seconds) {
16312
+ const whole = Math.max(0, Math.floor(seconds));
16313
+ return `${Math.floor(whole / 60)}:${String(whole % 60).padStart(2, '0')}`;
16314
+ }
16315
+
16316
+ /**
16317
+ * Why a customer's file is refused by the desk's upload allowance per
16318
+ * customer (`customer_upload_limit_bytes`), as the 402 QUOTA_EXCEEDED that
16319
+ * refuses it names in `details.reason`, so the portal tells it from the
16320
+ * organization's storage being full: the customer's files would pass their
16321
+ * allowance, or the allowance is 0, which turns customer uploads off. A file
16322
+ * an email carried that the allowance refused is listed by the same reason
16323
+ * among the files its message left out (`email.skipped_attachments`).
16324
+ */
16325
+ const CUSTOMER_UPLOAD_REFUSALS = Object.freeze({
16326
+ ALLOWANCE_REACHED: 'customer_upload_limit',
16327
+ UPLOADS_OFF: 'customer_uploads_off'
16328
+ });
16329
+
16330
+ /** The most attachments one message carries. */
16331
+ const MAX_ATTACHMENTS_PER_MESSAGE = 20;
16332
+
16333
+ /** The largest file a customer uploads through the portal, in bytes. */
16334
+ const CUSTOMER_ATTACHMENT_MAX_BYTES = 25 * 1024 * 1024;
16335
+
16336
+ /** Scoped styles for the optional inline help desk form. */
16337
+ function injectHelpDeskFormStyles() {
16338
+ if (document.getElementById('sparkvault-help-desk-styles'))
16339
+ return;
16340
+ const style = document.createElement('style');
16341
+ style.id = 'sparkvault-help-desk-styles';
16342
+ style.textContent = `
16343
+ .svhd-overlay{position:fixed;inset:0;z-index:2147483000;display:flex;align-items:center;justify-content:center;padding:16px;background:${DS.modalOverlay};overflow:auto}
16344
+ .svhd-blur{backdrop-filter:blur(5px)}
16345
+ .svhd-modal{width:100%;max-width:36rem;max-height:calc(100dvh - 32px);overflow:auto;border-radius:12px;background:${DS.white};box-shadow:${DS.shadowPopover}}
16346
+ .svhd-modal-header{display:flex;justify-content:flex-end;padding:12px 16px 0}
16347
+ .svhd-modal-close{border:0;border-radius:4px;background:transparent;color:${DS.slate600};cursor:pointer;font:600 14px/1.5 system-ui,sans-serif;padding:6px 8px;min-height:44px}
16348
+ .svhd-modal-close:hover{color:${DS.slate900}}
16349
+ .svhd-modal-close:focus-visible{outline:3px solid ${DS.action};outline-offset:2px}
16350
+ .svhd-modal .svhd-container{max-width:none;border:0;border-radius:0;padding-top:8px}
16351
+ .svhd-container{box-sizing:border-box;max-width:36rem;border:1px solid ${DS.slate200};border-radius:12px;background:${DS.white};color:${DS.slate900};font:400 15px/1.5 system-ui,sans-serif;padding:24px}
16352
+ .svhd-container *{box-sizing:border-box}
16353
+ .svhd-container h2{font-size:22px;line-height:1.25;margin:0 0 20px}
16354
+ .svhd-form{display:grid;gap:16px}
16355
+ .svhd-field{display:grid;gap:6px}
16356
+ .svhd-label{font-weight:600}
16357
+ .svhd-input{width:100%;border:1px solid ${DS.slate300};border-radius:6px;padding:10px 12px;color:${DS.slate900};background:${DS.white};font:inherit;font-size:16px}
16358
+ .svhd-input:focus-visible,.svhd-button:focus-visible,.svhd-link:focus-visible{outline:3px solid ${DS.action};outline-offset:2px}
16359
+ .svhd-input:disabled{background:${DS.slate50}}
16360
+ .svhd-description{min-height:140px;resize:vertical}
16361
+ .svhd-auth{display:grid;gap:16px;border:0;padding:0;margin:0}
16362
+ .svhd-form[hidden],.svhd-auth[hidden],.svhd-field[hidden],.svhd-field-error[hidden],.svhd-status[hidden],.svhd-link[hidden],.svhd-actions[hidden],.svhd-confirm[hidden]{display:none}
16363
+ .svhd-actions{display:flex;align-items:center;gap:12px;flex-wrap:wrap}
16364
+ .svhd-button{border:0;border-radius:6px;padding:10px 16px;min-height:44px;font:600 15px/1.5 system-ui,sans-serif;background:${DS.action};color:${DS.white};cursor:pointer}
16365
+ .svhd-button:hover{background:${DS.actionHover}}
16366
+ .svhd-button:disabled{opacity:.6;cursor:default}
16367
+ .svhd-secondary{background:transparent;color:${DS.slate600};padding:10px 4px}
16368
+ .svhd-secondary:hover{background:transparent;color:${DS.slate900}}
16369
+ .svhd-status{margin:0;color:${DS.slate600}}
16370
+ .svhd-error{color:${DS.error}}
16371
+ .svhd-field-error{margin:0;font-size:14px;color:${DS.error}}
16372
+ .svhd-code-links{display:flex;flex-wrap:wrap;gap:4px 16px}
16373
+ .svhd-confirm{display:grid;gap:8px}
16374
+ .svhd-confirm-title{font-weight:600;margin:0}
16375
+ .svhd-done{display:grid;gap:12px}
16376
+ .svhd-done-title{font-size:18px;line-height:1.3;margin:0}
16377
+ .svhd-done-title:focus{outline:none}
16378
+ .svhd-link{display:inline-flex;align-items:center;justify-self:start;min-height:44px;color:${DS.action};text-decoration:underline}
16379
+ .svhd-attachments[hidden],.svhd-attachments [hidden]{display:none}
16380
+ .svhd-file-hint{font-size:13px;color:${DS.slate600};margin:0}
16381
+ .svhd-file-list{display:grid;gap:12px;list-style:none;padding:0;margin:0}
16382
+ .svhd-file{display:grid;grid-template-columns:minmax(0,1fr) auto auto;gap:4px 10px;align-items:center;border:1px solid ${DS.slate200};border-radius:6px;padding:10px}
16383
+ .svhd-file-name{font-weight:600;overflow-wrap:anywhere;grid-column:1/-1}
16384
+ .svhd-file [role=status]{grid-column:1/-1;font-size:13px}
16385
+ .svhd-file progress{width:100%;min-width:0;accent-color:${DS.action}}
16386
+ .svhd-file .svhd-button{font-size:13px;padding:0 12px;min-height:44px}
16387
+ .svhd-footer{margin-top:20px;font-size:12px;color:${DS.slate500}}
16388
+ `;
16389
+ document.head.appendChild(style);
16390
+ }
16391
+
16392
+ /**
16393
+ * The one rule for which characters a file name may hold, shared by every
16394
+ * runtime that stores a name (Core refuses them at ingest, and clients check
16395
+ * before they send) and every runtime that saves a stored name to a device
16396
+ * (Forge's Content-Disposition, the web console's download, the x.sv reveal
16397
+ * page, the mobile share sheet). A download saves under the file's current
16398
+ * name by this rule, or under SAVED_FILENAME_FALLBACK when it has none.
16399
+ *
16400
+ * UNSAFE characters are never stored, and are replaced in any name saved to a
16401
+ * device: path separators, control characters (C0, DEL and C1), and the
16402
+ * invisible bidirectional controls that make a name display a different
16403
+ * extension than it has (`invoice<U+202E>fdp.exe` shows as `invoiceexe.pdf`).
16404
+ *
16405
+ * NON-PORTABLE characters (`: * ? " < > |`) are refused by the file systems a
16406
+ * name is saved to, so they are never stored either. A saved name keeps them:
16407
+ * a stored name cannot hold one, and Forge carries a name's exact text in the
16408
+ * RFC 5987 `filename*` parameter.
16409
+ *
16410
+ * Pure and import-free, so a browser, a container and a Lambda load it alike;
16411
+ * sdk-mobile mirrors it at build time (scripts/generate-app-sdk-mirrors.mjs).
16412
+ */
16413
+
16414
+ /**
16415
+ * Control characters (C0, DEL and C1) and the invisible bidirectional text
16416
+ * controls, as the body of a character class: text holding one displays as
16417
+ * other text than it holds.
16418
+ */
16419
+ const CONTROL_AND_BIDI_CHARACTERS = String.raw`\p{Cc}\u061C\u200E\u200F\u202A-\u202E\u2066-\u2069`;
16420
+
16421
+ /** Path separators, control characters and bidirectional text controls. */
16422
+ const UNSAFE_FILENAME_CHARACTERS = new RegExp(String.raw`[\\/${CONTROL_AND_BIDI_CHARACTERS}]`, 'u');
16423
+
16424
+ /** Characters the file systems a name is saved to refuse. */
16425
+ const NON_PORTABLE_FILENAME_CHARACTERS = /[:*?"<>|]/;
16426
+
16427
+ /**
16428
+ * A character a save drops from a name's end. Windows drops trailing dots and
16429
+ * spaces from a name it saves ("page.html." is saved as "page.html"); any
16430
+ * other trailing white space is dropped alike, so a name never hides the
16431
+ * extension a saved copy could open as.
16432
+ */
16433
+ const DROPPED_TRAILING_CHARACTER = /[\s.]/u;
16434
+
16435
+ /**
16436
+ * `name` without the trailing dots and white space a save drops: "page.html."
16437
+ * and "page.html " are "page.html", "notes. ." is "notes", and a name of only
16438
+ * dots and white space is ''. shownExtension and lastExtension read the
16439
+ * extension in front of them, and Forge completes a saved name after them
16440
+ * (./media-type-node.js withMediaTypeExtension: "notes." under text/plain
16441
+ * saves as "notes.txt").
16442
+ *
16443
+ * @param {string} name
16444
+ * @returns {string}
16445
+ */
16446
+ function trimDroppedTrailing(name) {
16447
+ let end = name.length;
16448
+ while (end > 0 && DROPPED_TRAILING_CHARACTER.test(name[end - 1])) end -= 1;
16449
+ return name.slice(0, end);
16450
+ }
16451
+
16452
+ /**
16453
+ * Invisible characters (Unicode's Default_Ignorable_Code_Point): the joiners,
16454
+ * variation selectors, tags and bidirectional controls, which display as
16455
+ * nothing, and the Hangul fillers, which display as blank space. None shows
16456
+ * in a name, and a save keeps several (`Invoice.pdf<U+034F>` saves as itself
16457
+ * in Chromium and Firefox, and shows as `Invoice.pdf`).
16458
+ */
16459
+ const INVISIBLE_CHARACTERS = /\p{Default_Ignorable_Code_Point}/gu;
16460
+
16461
+ /**
16462
+ * What `name` shows once saved: the name read past its invisible characters
16463
+ * (INVISIBLE_CHARACTERS), without the trailing dots and white space a save
16464
+ * drops (trimDroppedTrailing). The extension a name shows is read in it
16465
+ * (shownExtension), and a name it leaves nothing of is one of only dots or
16466
+ * spaces (isOnlyDotsOrSpaces).
16467
+ *
16468
+ * @param {string} name
16469
+ * @returns {string}
16470
+ */
16471
+ function nameAsShown(name) {
16472
+ return trimDroppedTrailing(name.replace(INVISIBLE_CHARACTERS, ''));
16473
+ }
16474
+
16475
+ /** Why a name cannot be stored, as storableFilenameProblem reports it. */
16476
+ const FILENAME_PROBLEMS = Object.freeze({
16477
+ UNSAFE_CHARACTER: 'unsafe_character',
16478
+ NON_PORTABLE_CHARACTER: 'non_portable_character',
16479
+ ONLY_DOTS_OR_SPACES: 'only_dots_or_spaces'
16480
+ });
16481
+
16482
+ /**
16483
+ * Whether `name` is one of only dots or spaces: a name that is not empty yet
16484
+ * shows nothing once saved (nameAsShown), every character in it a dot, white
16485
+ * space or an invisible character (`...` and ` . `, and `..<U+034F>` and
16486
+ * `.<U+200B>.`, which show as `..`). No such name is stored
16487
+ * (storableFilenameProblem), and a file that has one saves under the
16488
+ * fallback name (savedFilename).
16489
+ *
16490
+ * @param {string} name
16491
+ * @returns {boolean}
16492
+ */
16493
+ function isOnlyDotsOrSpaces(name) {
16494
+ return name !== '' && nameAsShown(name) === '';
16495
+ }
16496
+
16497
+ /**
16498
+ * What stops `name` from being stored as a file name, or null when nothing
16499
+ * does. Length and emptiness are the storing runtime's own limits.
16500
+ *
16501
+ * @param {string} name
16502
+ * @returns {string|null} One of FILENAME_PROBLEMS, or null
16503
+ */
16504
+ function storableFilenameProblem(name) {
16505
+ if (UNSAFE_FILENAME_CHARACTERS.test(name)) return FILENAME_PROBLEMS.UNSAFE_CHARACTER;
16506
+ if (NON_PORTABLE_FILENAME_CHARACTERS.test(name)) return FILENAME_PROBLEMS.NON_PORTABLE_CHARACTER;
16507
+ if (isOnlyDotsOrSpaces(name)) return FILENAME_PROBLEMS.ONLY_DOTS_OR_SPACES;
16508
+ return null;
16509
+ }
16510
+
16511
+ /** The longest name an ingot stores, in UTF-16 code units. */
16512
+ const INGOT_NAME_MAX = 255;
16513
+
16514
+ /**
16515
+ * A run of the white space Firefox reads in a name it saves: the space
16516
+ * separators and U+FEFF, and the line and paragraph separators, which it
16517
+ * drops wherever they stand and which end no run.
16518
+ */
16519
+ const WHITE_SPACE_RUN = /[\p{Zs}\uFEFF\u2028\u2029]+/gu;
16520
+
16521
+ /** A run whose first space is an ideographic space (U+3000), the one space Firefox keeps. */
16522
+ const RUN_FIREFOX_KEEPS = /^[\u2028\u2029]*\u3000/;
16523
+
16524
+ /**
16525
+ * `name` with its white space closed up as Firefox closes it in what follows
16526
+ * its last dot, under any type it is handed and in a blob's name alike
16527
+ * (nsExternalHelperAppService::SanitizeFileName): it collapses a run of white
16528
+ * space into the run's first character, an ideographic space kept as it is
16529
+ * and any other made an ASCII space, then strips the ASCII spaces from what
16530
+ * follows the last dot. So a run there is dropped whole unless it starts with
16531
+ * an ideographic space: `evil.ex e` saves as `evil.exe`, `evil.ex<U+3000>e`
16532
+ * as itself, `evil.ex <U+3000>e` as `evil.exe`. Closing up the whole name
16533
+ * moves no dot, so what follows its last dot is what Firefox saves there.
16534
+ *
16535
+ * @param {string} name
16536
+ * @returns {string}
16537
+ */
16538
+ function closeUpSpaces(name) {
16539
+ return name.replace(WHITE_SPACE_RUN, (run) => (RUN_FIREFOX_KEEPS.test(run) ? '\u3000' : ''));
16540
+ }
16541
+
16542
+ /**
16543
+ * The extension `name` shows, its spaces kept, lowercase and without its dot:
16544
+ * what follows the last dot of what the name shows once saved (nameAsShown:
16545
+ * read past its invisible characters, without the trailing dots and white
16546
+ * space a save drops). "invoice.note.html" and "page.html." show 'html',
16547
+ * "logs.tar.gz" 'gz', ".html" 'html' (Windows reads a leading dot's
16548
+ * extension), "Invoice.pdf (1)" 'pdf (1)', and a name with no dot ''. It is
16549
+ * what Chromium's copy shows, since Chromium saves a name as sent:
16550
+ * "Invoice.pdf<U+034F>" shows 'pdf', and a character that draws as blank
16551
+ * space or as another letter yet is no invisible character stays in it
16552
+ * ("Invoice.pdf<U+2800>", a braille blank, shows 'pdf<U+2800>';
16553
+ * "Invoice.<U+0440>df", a Cyrillic er, '<U+0440>df'). Only whether a saved
16554
+ * name is completed reads it, by where its first word ends
16555
+ * (./media-type-node.js withMediaTypeExtension), since completion ends a name
16556
+ * in its type's own extension, which holds no space for Firefox to close up;
16557
+ * every rule about what a name makes a file reads lastExtension.
16558
+ *
16559
+ * @param {string} name
16560
+ * @returns {string}
16561
+ */
16562
+ function shownExtension(name) {
16563
+ const shown = nameAsShown(name);
16564
+ const dot = shown.lastIndexOf('.');
16565
+ return dot === -1 ? '' : shown.slice(dot + 1).toLowerCase();
16566
+ }
16567
+
16568
+ /**
16569
+ * The extension a saved copy of `name` opens as, lowercase and without its
16570
+ * dot: the one it shows (shownExtension) once its spaces are closed up as
16571
+ * Firefox, the browser that changes a saved name most, closes them
16572
+ * (closeUpSpaces). "evil.ex e", "evil.ex<U+00A0>e" and "evil. exe" open as
16573
+ * 'exe', "Scan. pdf" as 'pdf', "Invoice.pdf (1)" as 'pdf(1)' and "Acme Inc.
16574
+ * Contract" as 'contract', though Chromium saves each as sent. Every rule
16575
+ * about what a name makes a file (a refused extension, an inline
16576
+ * disposition, the type Core stores for an untyped upload, the type Forge
16577
+ * serves a download under: ./media-type-node.js) reads this, never
16578
+ * splitExtension.
16579
+ *
16580
+ * @param {string} name
16581
+ * @returns {string}
16582
+ */
16583
+ function lastExtension(name) {
16584
+ // Closed up before its invisible characters are read past: Firefox counts
16585
+ // U+FEFF, one of them, as white space (`evil.ex<U+FEFF><U+3000>e` saves as
16586
+ // `evil.exe`).
16587
+ return shownExtension(closeUpSpaces(name));
16588
+ }
16589
+
16590
+ /**
16591
+ * What a help-desk attachment may be named and what its first bytes may not
16592
+ * be: the one list of the files a ticket accepts, beside the one list of
16593
+ * extensions Forge never completes a saved name with.
16594
+ *
16595
+ * Both sides of a ticket upload files: a customer through the portal, an
16596
+ * operator through the console, and a customer's mail client through SES. The
16597
+ * server never reads the plaintext of an uploaded ingot, so the policy splits
16598
+ * by where the bytes are: the NAME rule runs on the server at initiate and in
16599
+ * the browser before an upload starts; the SNIFF rule runs wherever the bytes
16600
+ * are at hand — in the browser over the file's first slice, and on the server
16601
+ * over an inbound email's parts. The type a ticket stores a file under, which
16602
+ * the MIME registry decides, is server-only: ./attachment-policy-node.js.
16603
+ *
16604
+ * Built on the platform's one filename grammar (filename.js): nothing here
16605
+ * spells a second version of it.
16606
+ *
16607
+ * BROWSER-SAFE: reads any Uint8Array (a Node Buffer is one).
16269
16608
  */
16270
- const OPTIONAL_LETTERS =
16271
- '21 24 25 2b 30 31 34 35 37 40 49 6c 7c a1-a3 a5 aa ae b5 b9 ba bc-be cc-cf d7 128 12a 12c 12e ' +
16272
- '130 138 13a 13c 13e 140 142 190 196 197 19a 1bc 1c0 1c1 1c3 1cf 208 20a 245 25b 26b 26d 26e ' +
16273
- '276 279-27b 28a 2a4 2a7 2a8 2b0-2b5 2b7 2b8 2e0-2e3 363-36f 376 377 37b 37d 38a 393 399 39b ' +
16274
- '3aa 3ad 3b2 3b5 3ba 3bc 3c0 3c4 3d0 3d6 3f0 3f5 3fd 403 404 406 407 40d 413 414 418 419 41b ' +
16275
- '426 42f 432 434 438-43a 43d 43f 442 446 44a 44b 44f 454 45c 45d 48a 48b 490 492 494 49b 49f ' +
16276
- '4a3 4a7 4ad 4c0 4c5 4c8 4ca 4cf 4e2-4e5 4f6 4f9 4fa 4fe 50d 510-513 520 521 525 52e 52f 547 ' +
16277
- '548 554 577 57a 586 58c 5c0 5d5 5df 5e7 5e9 5f0 5f1 609 60a 622 623 625 627 661 665 667 668 ' +
16278
- '66a 672 673 675 6f1 6f5 6f7 6f8 7c0 7ca 964-966 9e6 a66 ae6 b66 be6 c02 c66 c82 ce6 d02 d5f ' +
16279
- 'd66 d82 e04 e14 e15 e20 e22 e23 e26 e40 e41 e50 e8d ed0 1040 104a 104b 10a0 111b 111d 112b ' +
16280
- '112c 1135 1141-1146 1148-114b 114d 1157 1162 1164 1166 1168 116b 116c 1170 1171 1174 1175 1180 ' +
16281
- '1181 1185 1188 118a 118c 1190 1192 1194 1197-119d 11a1 11a7 11d5 11e2 11e6 11ec 11ed 11ef 11f1 ' +
16282
- '11f2 11f4 1223 1260 13b1 13cb 13ce 13f0 13f8 13fb 13fc 1429 1431 143a-143d 144e 1457-145a 1467 ' +
16283
- '1468 1476 1477 147a 147b 147e-1481 1486-1488 1498 1499 14a5 14ae 14af 14b6 14b7 14f0 14fa 14fb ' +
16284
- '150e 1511 1515 1519 151a 1521 1522 152f 1530 1540 1575 157f 1581 1583 1584 1586 1591 1598 15b6 ' +
16285
- '15e9 1609 166d 166e 1673 16c1 16c5 16d0 16ed 172a 17e0 18e8 1a80 1a90 1abf 1acc-1ace 1bea 1bec ' +
16286
- '1c84 1d0b 1d0e 1d14 1d19 1d28 1d29 1d2c-1d2e 1d30 1d31 1d33-1d3a 1d3c 1d3e-1d43 1d45 1d47-1d49 ' +
16287
- '1d4b 1d4d 1d4f-1d53 1d56-1d58 1d5a 1d5b 1d5d 1d5e 1d62-1d68 1d78 1d7a 1d7f 1d9c 1d9d 1da0-1da2 ' +
16288
- '1da4-1db1 1db3 1db5-1db9 1dbb-1dbd 1dbf 1dca 1dd3-1dd8 1dda-1de9 1deb-1dee 1df0-1df4 1e2c 1e2e ' +
16289
- '1e37 1e39 1e3b 1e3d 1ec8 1eca 1f10-1f15 1f38-1f3f 1f72 1fd8-1fda 2016 2020 2030 2031 203c 2048 ' +
16290
- '2049 2052 2070 2071 2074 2075 2077 207a 207f-2081 2084 2085 2087 208a 2090-2093 2095-209f 20a1 ' +
16291
- '20a4-20a6 20a8 20a9 20ab-20ae 20b1 20b3 20b4 20b6 20b8-20ba 20bd 20c1 2100 2101 2103 2105-2107 ' +
16292
- '2109 2110 2111 2113 2116 2117 2120-2122 212e 213b 213c 213e 2150-2153 2155-215b 215d-217f 2183 ' +
16293
- '2184 2189 218a 21bf 2205 2214 221a 221e 2223-2226 2228-222a 2296 229d 22a4 22c1-22c3 22f4 22ff ' +
16294
- '2300 2361 2365 236c 2373 2374 2376-2378 237a 237f 239c 239f 23a2 23a5 23aa 23ae 23e8 23fd 2460 ' +
16295
- '2463 2464 2466 2469-2474 2477 2478 247a 247d-2488 248b 248c 248e 2491-24ea 2502 2503 2573 2795 ' +
16296
- '27d9 292b 292c 2963 296e 29b1-29b4 29e2 2a22-2a28 2a2f-2a31 2a40-2a45 2a52 2a5b 2a5d 2a62 2a63 ' +
16297
- '2aee-2af1 2c6d 2c79 2c7c 2c7d 2c83 2c84 2c88 2c89 2c8f 2c92 2c95 2c9b 2ca1 2ca7 2cc0 2cd1 2ce8 ' +
16298
- '2d00 2d37 2d4a 2d4f 2d51 2de1-2de3 2de6 2de8-2df0 2df2 2df4 2df6 2df7 2dfa 2dfb 2e36 2e37 2e85 ' +
16299
- '2f01 2f0f 2f11 2f19 2f1b 2f59 3007 3012 3036 3072-3074 30a2 30a4 30ba 30e0 30ec 310e 3125 3129 ' +
16300
- '3150 3152 3154 3156 3159 315a 315e 315f 3162 3163 3171 3178 3179 3182-3184 3188 3189 318b 318c ' +
16301
- '318e 31d1 3207 3215 321d 321e 3250 3251 3254 3255 3257 325a 325b 325e 325f 3267 3275 327d 327e ' +
16302
- '32b2 32b5-32c0 32c3 32c4 32c6 32c9-32d1 32f0 32f9 3300-3305 330a 330b 3315 3318-331a 331f 3320 ' +
16303
- '332a 332d 332e 333d 3343 3344 3350 3355 3356 3358 3359 335c 335d 335f 3362-336d 3370-337a ' +
16304
- '3380-33e0 33e3 33e4 33e6 33e9-33f4 33f7 33f8 33fa 33fd-33ff 4e02 4e28 4ebb 51e0 5200 5342 5369 ' +
16305
- '53b6 5c3a 723f a00e a017 a018 a024 a072 a0f4 a224 a235 a263 a2d6 a2ea a2eb a317 a31a a34f a3dd ' +
16306
- 'a407 a490 a492 a493 a4a4 a4a6 a4c4 a4c5 a4db a4e5 a4f2 a4f5 a516 a56f a613 a650 a651 a674-a679 ' +
16307
- 'a67b a68d a69c a69d a6a1 a6ce a728-a72b a73f a742 a75a a75c a75d a768 a769 a772 a774-a776 a781 ' +
16308
- 'a792 a793 a79d a7ae a7b5 a7da a7dc a7e2 a7f1-a7f4 a7f8 a7f9 a7fe a830 a8ce a8cf a96b a976 a977 ' +
16309
- 'aa5d ab41 ab42 ab5d-ab5f ab62 ab68 ab6c ab70-ab72 ab7a-ab7c ab8b ab90 ab9b ab9e aba2 abae abb2 ' +
16310
- 'abb6 ac1c-ac30 ac32-ac37 ac54-ac68 ac6a-ac6f ac8c-aca0 aca2-aca7 acc4-acd8 acda-acdf ad18-ad2c ' +
16311
- 'ad2e-ad48 ad4a-ad4f ada4-adb8 adba-add4 add6-addb ae14-ae28 ae2a-ae44 ae46-ae4b ae68-ae7c ' +
16312
- 'ae7e-ae83 aea0-aeb4 aeb6-aebb aed8-aeec aeee-aef3 af10-af24 af26-af2b af64-af78 af7a-af94 ' +
16313
- 'af96-af9b aff0-b004 b006-b020 b022-b027 b060-b074 b076-b090 b092-b097 b0b4-b0c8 b0ca-b0cf ' +
16314
- 'b0ec-b100 b102-b107 b124-b138 b13a-b13f b15c-b170 b172-b177 b1b0-b1c4 b1c6-b1e0 b1e2-b1e7 ' +
16315
- 'b23c-b250 b252-b26c b26e-b273 b2ac-b2c0 b2c2-b2dc b2de-b2e3 b300-b314 b316-b31b b338-b34c ' +
16316
- 'b34e-b353 b370-b384 b386-b38b b3a8-b3bc b3be-b3c3 b3fc-b410 b412-b42c b42e-b433 b488-b49c ' +
16317
- 'b49e-b4b8 b4ba-b4bf b4f8-b50c b50e-b528 b52a-b52f b54c-b560 b562-b567 b584-b598 b59a-b59f ' +
16318
- 'b5bc-b5d0 b5d2-b5d7 b5f4-b608 b60a-b60f b648-b65c b65e-b678 b67a-b67f b6d4-b6e8 b6ea-b704 ' +
16319
- 'b706-b70b b744-b758 b75a-b774 b776-b77b b798-b7ac b7ae-b7b3 b7d0-b7e4 b7e6-b7eb b808-b81c ' +
16320
- 'b81e-b823 b840-b854 b856-b85b b894-b8a8 b8aa-b8c4 b8c6-b8cb b920-b934 b936-b950 b952-b957 ' +
16321
- 'b990-b9a4 b9a6-b9c0 b9c2-b9c7 b9e4-b9f8 b9fa-b9ff ba1c-ba30 ba32-ba37 ba54-ba68 ba6a-ba6f ' +
16322
- 'ba8c-baa0 baa2-baa7 bae0-baf4 baf6-bb10 bb12-bb17 bb6c-bb80 bb82-bb9c bb9e-bba3 bbdc-bbf0 ' +
16323
- 'bbf2-bc0c bc0e-bc13 bc30-bc44 bc46-bc4b bc68-bc7c bc7e-bc83 bca0-bcb4 bcb6-bcbb bcd8-bcec ' +
16324
- 'bcee-bcf3 bd2c-bd40 bd42-bd5c bd5e-bd63 bdb8-bdcc bdce-bde8 bdea-bdef be28-be3c be3e-be58 ' +
16325
- 'be5a-be5f be7c-be90 be92-be97 beb4-bec8 beca-becf beec-bf00 bf02-bf07 bf24-bf38 bf3a-bf3f ' +
16326
- 'bf78-bf8c bf8e-bfa8 bfaa-bfaf c004-c018 c01a-c034 c036-c03b c074-c088 c08a-c0a4 c0a6-c0ab ' +
16327
- 'c0c8-c0dc c0de-c0e3 c100-c114 c116-c11b c138-c14c c14e-c153 c170-c184 c186-c18b c1c4-c1d8 ' +
16328
- 'c1da-c1f4 c1f6-c1fb c250-c264 c266-c280 c282-c287 c2c0-c2d4 c2d6-c2f0 c2f2-c2f7 c314-c328 ' +
16329
- 'c32a-c32f c34c-c360 c362-c367 c384-c398 c39a-c39f c3bc-c3d0 c3d2-c3d7 c410-c424 c426-c440 ' +
16330
- 'c442-c447 c49c-c4b0 c4b2-c4cc c4ce-c4d3 c50c-c520 c522-c53c c53e-c543 c7ac-c7c0 c7c2-c7c7 ' +
16331
- 'c7e4-c7f8 c7fa-c7ff c81c-c830 c832-c837 c854-c868 c86a-c86f c8a8-c8bc c8be-c8d8 c8da-c8df ' +
16332
- 'c934-c948 c94a-c964 c966-c96b c9a4-c9b8 c9ba-c9d4 c9d6-c9db c9f8-ca0c ca0e-ca13 ca30-ca44 ' +
16333
- 'ca46-ca4b ca68-ca7c ca7e-ca83 caa0-cab4 cab6-cabb caf4-cb08 cb0a-cb24 cb26-cb2b cb80-cb94 ' +
16334
- 'cb96-cbb0 cbb2-cbb7 cbf0-cc04 cc06-cc20 cc22-cc27 cc44-cc58 cc5a-cc5f cc7c-cc90 cc92-cc97 ' +
16335
- 'ccb4-ccc8 ccca-cccf ccec-cd00 cd02-cd07 cd40-cd54 cd56-cd70 cd72-cd77 cdcc-cde0 cde2-cdfc ' +
16336
- 'cdfe-ce03 ce3c-ce50 ce52-ce6c ce6e-ce73 ce90-cea4 cea6-ceab cec8-cedc cede-cee3 cf00-cf14 ' +
16337
- 'cf16-cf1b cf38-cf4c cf4e-cf53 cf8c-cfa0 cfa2-cfbc cfbe-cfc3 d018-d02c d02e-d048 d04a-d04f ' +
16338
- 'd088-d09c d09e-d0b8 d0ba-d0bf d0dc-d0f0 d0f2-d0f7 d114-d128 d12a-d12f d14c-d160 d162-d167 ' +
16339
- 'd184-d198 d19a-d19f d1d8-d1ec d1ee-d208 d20a-d20f d264-d278 d27a-d294 d296-d29b d2d4-d2e8 ' +
16340
- 'd2ea-d304 d306-d30b d328-d33c d33e-d343 d360-d374 d376-d37b d398-d3ac d3ae-d3b3 d3d0-d3e4 ' +
16341
- 'd3e6-d3eb d424-d438 d43a-d454 d456-d45b d4b0-d4c4 d4c6-d4e0 d4e2-d4e7 d520-d534 d536-d550 ' +
16342
- 'd552-d557 d574-d588 d58a-d58f d5ac-d5c0 d5c2-d5c7 d5e4-d5f8 d5fa-d5ff d61c-d630 d632-d637 ' +
16343
- 'd670-d684 d686-d6a0 d6a2-d6a7 d6fc-d710 d712-d72c d72e-d733 d76c-d780 d782-d79c d79e-d7a3 d7b1 ' +
16344
- 'd7b3 d7b6 d7b7 d7bb d7bd-d7c4 d7c6 d7dd d7eb d7f4 fb29 fbea fbeb fc37 fc80 fc88 fccd fd3c fd3d ' +
16345
- 'fdf3 fdfc fe15 fe31 fe57 fe62 fe69-fe6b fe81-fe84 fe87 fe88 fe8d fe8e fef5-fefc ff01 ff04 ff05 ' +
16346
- 'ff0b ff10 ff11 ff14 ff15 ff17 ff20 ff29 ff4c ff5c ff71 ff72 ff91 ff9a ffc3 ffc5 ffc7 ffcb ffce ' +
16347
- 'ffcf ffd5 ffd6 ffdb ffdc ffe0 ffe1 ffe5 ffe6 ffe8 1017e 10182 1018b 1018e 10196-10199 101a0 ' +
16348
- '1028a 1028d 1029b 102f5 10309 10320 10322 10401 10423 10425 10429 1042a 10443 1044b 1044d ' +
16349
- '104b0 104bc 104d2 104e4 104fa 1050d-10510 10783-10785 10787-1078d 10790 10792-10796 ' +
16350
- '10798-1079f 107a2 107a3 107a5-107b0 107b2 107b6 107b7 107b9 107ba 107bd 10926 10c3e 10ca5 ' +
16351
- '10cfa 10cfc 11047 11048 11055 110c0 110c1 11141 11142 111c5 111c6 11302 113d4 113d5 1144b ' +
16352
- '1144c 114d0 115c5 11641 11642 118af 118bb 118c6 118ce 118cf 118db 118e0 118e3 118e5 118e6 ' +
16353
- '118e9 118ec 118ef 118f2 11c41 11c42 11dda 11de0 11de1 16869 16ad1 16ae4 16d63 16d69 16d6a ' +
16354
- '16e80 16e82 16e8a 16eaa 16ec1 16f07 16f28 16f2d 16f3d 1cc90 1ccd6-1ccf1 1ccf4 1ccf5 1ccf7 ' +
16355
- '1ce15 1cefc 1d100 1d134 1d1fe 1d202 1d207 1d20b-1d20d 1d212 1d213 1d216 1d21a 1d221 1d22a ' +
16356
- '1d245 1d262 1d373 1d377 1d408 1d425 1d43c 1d459 1d470 1d48d 1d4c1 1d4d8 1d4f5 1d529 1d540 ' +
16357
- '1d55d 1d574 1d591 1d5a8 1d5c5 1d5dc 1d5f9 1d610 1d62d 1d644 1d661 1d678 1d695 1d6aa 1d6b0 ' +
16358
- '1d6b2 1d6c3 1d6c6 1d6cb 1d6cd 1d6d1 1d6d5 1d6dc 1d6de 1d6e1 1d6e4 1d6ea 1d6ec 1d6fd 1d700 ' +
16359
- '1d705 1d707 1d70b 1d70f 1d716 1d718 1d71b 1d71e 1d724 1d726 1d737 1d73a 1d73f 1d741 1d745 ' +
16360
- '1d749 1d750 1d752 1d755 1d758 1d75e 1d760 1d771 1d774 1d779 1d77b 1d77f 1d783 1d78a 1d78c ' +
16361
- '1d78f 1d792 1d798 1d79a 1d7ab 1d7ae 1d7b3 1d7b5 1d7b9 1d7bd 1d7c4 1d7c6 1d7c9 1d7ce 1d7cf ' +
16362
- '1d7d2 1d7d3 1d7d5 1d7d8 1d7d9 1d7dc 1d7dd 1d7df 1d7e2 1d7e3 1d7e6 1d7e7 1d7e9 1d7ec 1d7ed ' +
16363
- '1d7f0 1d7f1 1d7f3 1d7f6 1d7f7 1d7fa 1d7fb 1d7fd 1df05 1df08 1df0a 1df12 1df15 1df17 1df19 ' +
16364
- '1df1c 1df1f-1df23 1df2b 1df2c 1df3b 1df44 1df50 1df57 1df5b 1df5c 1df62 1df63 1df67 ' +
16365
- '1df6c-1df6e 1dfcd-1dfd7 1dfd9-1dfdd 1dfe0 1dfe1 1dfe4 1dfe5 1dfe8-1dfea 1dfef-1dff2 ' +
16366
- '1dff5-1dfff 1e030 1e032-1e035 1e038 1e039 1e03c-1e041 1e043 1e044 1e046 1e047 1e049 ' +
16367
- '1e04c-1e051 1e053-1e056 1e059 1e05a 1e05c-1e05f 1e061 1e062 1e064-1e06d 1e08f 1e140 1e141 ' +
16368
- '1e145 1e2f0 1e6e9 1e8c7 1ec75 1ec83 1ec86 1ec87 1ec89 1ec9e 1ec9f 1eca2 1eca3 1ecb1 1ecb2 ' +
16369
- '1ed01 1ed13 1ed31 1ed32 1ed36 1ed37 1ee00 1ee61 1ee62 1ee67-1ee6a 1ee6c-1ee72 1ee74-1ee77 ' +
16370
- '1ee79-1ee7c 1ee7e 1ee80 1f100-1f102 1f105 1f106 1f108 1f10f-1f12e 1f130-1f16e 1f170-1f198 ' +
16371
- '1f19a 1f1a5-1f1a7 1f1a9-1f1ac 1f1e6-1f1ff 1f700 1f707 1f708 1f714 1f74c 1f75c 1f768 1f76b ' +
16372
- '1f76c 1fbf0 1fbf1 1fbf4 1fbf5 1fbf7';
16609
+
16373
16610
 
16374
16611
  /**
16375
- * Each bidirectional class other than L, keyed by its UAX #9 short name: the
16376
- * code points that hold it. Every other code point is L. The explicit
16377
- * formatting characters are left out: the rule refuses them before it lays a
16378
- * text out.
16612
+ * The files a ticket accepts, by kind: each kind keyed by the word every
16613
+ * surface names it with (./attachment-check.js ACCEPTED_ATTACHMENTS_COPY), and
16614
+ * listing the extensions a name may end in, lowercase and without the dot, as
16615
+ * lastExtension reads the one a saved copy opens as. A ticket takes no other
16616
+ * file: a name of no extension, an archive, a page, a Markdown document, a
16617
+ * drawing, a legacy or macro-enabled Office document, a message, and every
16618
+ * program, script, installer, shortcut and certificate are refused, whatever
16619
+ * their bytes say.
16620
+ * attachment-policy.test.js gives each kind the reason the program a desktop
16621
+ * opens it with runs no code from it, and holds that none of them, nor any
16622
+ * extension the registry lists beside one for its type, is one of
16623
+ * REFUSED_EXTENSIONS or one Outlook blocks.
16379
16624
  */
16380
- const BIDI_CLASSES = {
16381
- AL:
16382
- '608 60b 60d 61b-64a 66d-66f 671-6d5 6e5-6e6 6ee-6ef 6fa-710 712-72f 74b-7a5 7b1-7bf 860-88f ' +
16383
- '892-896 8a0-8c9 fb50-fbc2 fbd3-fd3d fd50-fd8f fd92-fdc7 fdf0-fdfc fe70-fefe 10d00-10d23 ' +
16384
- '10d28-10d2f 10d3a-10d3f 10ec0-10eca 10ed9-10eef 10f30-10f45 10f51-10f6f 1ec70-1ecbf ' +
16385
- '1ed00-1ed4f 1ee00-1eeef 1eef2-1eeff',
16386
- AN:
16387
- '600-605 660-669 66b-66c 6dd 890-891 8e2 10d30-10d39 10d40-10d49 10e60-10e7e',
16388
- B:
16389
- 'a d 1c-1e 85 2029',
16390
- BN:
16391
- '0-8 e-1b 7f-84 86-9f ad 180e 200b-200d 2060-2065 206a-206f fdd0-fdef feff fff0-fff8 ' +
16392
- 'fffe-ffff 1bca0-1bca3 1d173-1d17a 1fffe-1ffff 2fffe-2ffff 3fffe-3ffff 4fffe-4ffff ' +
16393
- '5fffe-5ffff 6fffe-6ffff 7fffe-7ffff 8fffe-8ffff 9fffe-9ffff afffe-affff bfffe-bffff ' +
16394
- 'cfffe-cffff dfffe-e00ff e01f0-e0fff efffe-effff ffffe-fffff 10fffe-10ffff',
16395
- CS:
16396
- '2c 2e-2f 3a a0 60c 202f 2044 fe50 fe52 fe55 ff0c ff0e-ff0f ff1a',
16397
- EN:
16398
- '30-39 b2-b3 b9 6f0-6f9 2070 2074-2079 2080-2089 2488-249b ff10-ff19 102e1-102fb 1ccf0-1ccf9 ' +
16399
- '1d7ce-1d7ff 1f100-1f10a 1fbf0-1fbf9',
16400
- ES:
16401
- '2b 2d 207a-207b 208a-208b 2212 fb29 fe62-fe63 ff0b ff0d',
16402
- ET:
16403
- '23-25 a2-a5 b0-b1 58f 609-60a 66a 9f2-9f3 9fb af1 bf9 e3f 17db 2030-2034 20a0-20cf 212e 2213 ' +
16404
- 'a838-a839 fe5f fe69-fe6a ff03-ff05 ffe0-ffe1 ffe5-ffe6 11fdd-11fe0 1e2ff',
16405
- NSM:
16406
- '300-36f 483-489 591-5bd 5bf 5c1-5c2 5c4-5c5 5c7-5c9 610-61a 64b-65f 670 6d6-6dc 6df-6e4 ' +
16407
- '6e7-6e8 6ea-6ed 711 730-74a 7a6-7b0 7eb-7f3 7fd 816-819 81b-823 825-827 829-82d 859-85b ' +
16408
- '897-89f 8ca-8e1 8e3-902 93a 93c 941-948 94d 951-957 962-963 981 9bc 9c1-9c4 9cd 9e2-9e3 9fe ' +
16409
- 'a01-a02 a3c a41-a42 a47-a48 a4b-a4d a51 a70-a71 a75 a81-a82 abc ac1-ac5 ac7-ac8 acd ae2-ae3 ' +
16410
- 'afa-aff b01 b3c b3f b41-b44 b4d b53-b56 b62-b63 b82 bc0 bcd c00 c04 c3c c3e-c40 c46-c48 ' +
16411
- 'c4a-c4d c55-c56 c62-c63 c81 cbc ccc-ccd ce2-ce3 d00-d01 d3b-d3c d41-d44 d4d d62-d63 d81 dca ' +
16412
- 'dd2-dd4 dd6 e31 e34-e3a e47-e4e eb1 eb4-ebc ec8-ece f18-f19 f35 f37 f39 f71-f7e f80-f84 ' +
16413
- 'f86-f87 f8d-f97 f99-fbc fc6 102d-1030 1032-1037 1039-103a 103d-103e 1058-1059 105e-1060 ' +
16414
- '1071-1074 1082 1085-1086 108d 109d 135d-135f 1712-1714 1732-1733 1752-1753 1772-1773 ' +
16415
- '17b4-17b5 17b7-17bd 17c6 17c9-17d3 17dd 180b-180d 180f 1885-1886 18a9 1920-1922 1927-1928 ' +
16416
- '1932 1939-193b 1a17-1a18 1a1b 1a56 1a58-1a5e 1a60 1a62 1a65-1a6c 1a73-1a7c 1a7f 1ab0-1af0 ' +
16417
- '1b00-1b03 1b34 1b36-1b39 1b6b-1b73 1b80-1b81 1ba2-1ba5 1ba8-1ba9 1bab-1bad 1be6 1be8-1be9 ' +
16418
- '1bed 1bef-1bf1 1c2c-1c33 1c36-1c37 1cd0-1cd2 1cd4-1ce0 1ce2-1ce8 1ced 1cf4 1cf8-1cf9 ' +
16419
- '1dc0-1dff 20d0-20f0 2cef-2cf1 2d7f 2de0-2dff 302a-302d 3099-309a a66f-a672 a674-a67d ' +
16420
- 'a69e-a69f a6f0-a6f1 a802 a806 a80b a825-a826 a82c a8c4-a8c5 a8e0-a8f1 a8ff a926-a92d ' +
16421
- 'a947-a951 a980-a982 a9b3 a9b6-a9b9 a9bc-a9bd a9e5 aa29-aa2e aa31-aa32 aa35-aa36 aa43 aa4c ' +
16422
- 'aa7c aab0 aab2-aab4 aab7-aab8 aabe-aabf aac1 aaec-aaed aaf6 abe5 abe8 abed fb1e fe00-fe0f ' +
16423
- 'fe20-fe2f 101fd 102e0 10376-1037a 10a01-10a03 10a05-10a06 10a0c-10a0f 10a38-10a3a 10a3f ' +
16424
- '10ae5-10ae6 10d24-10d27 10d69-10d6d 10eab-10eac 10ecb-10ecf 10ef0-10eff 10f46-10f50 ' +
16425
- '10f82-10f85 11001 11038-11046 11070 11073-11074 1107f-11081 110b3-110b6 110b9-110ba 110c2 ' +
16426
- '11100-11102 11127-1112b 1112d-11134 11173 11180-11181 111b6-111be 111c9-111cc 111cf ' +
16427
- '1122f-11231 11234 11236-11237 1123e 11241 112df 112e3-112ea 11300-11301 1133b-1133c 11340 ' +
16428
- '11366-1136c 11370-11374 113bb-113c0 113ce 113d0 113d2 113e1-113e2 11438-1143f 11442-11444 ' +
16429
- '11446 1145e 114b3-114b8 114ba 114bf-114c0 114c2-114c3 115b2-115b5 115bc-115bd 115bf-115c0 ' +
16430
- '115dc-115dd 11633-1163a 1163d 1163f-11640 116ab 116ad 116b0-116b5 116b7 1171d 1171f ' +
16431
- '11722-11725 11727-1172b 1182f-11837 11839-1183a 1193b-1193c 1193e 11943 119d4-119d7 ' +
16432
- '119da-119db 119e0 11a01-11a06 11a09-11a0a 11a33-11a38 11a3b-11a3e 11a47 11a51-11a56 ' +
16433
- '11a59-11a5b 11a8a-11a96 11a98-11a99 11b60 11b62-11b64 11b66 11c30-11c36 11c38-11c3d ' +
16434
- '11c92-11ca7 11caa-11cb0 11cb2-11cb3 11cb5-11cb6 11d31-11d36 11d3a 11d3c-11d3d 11d3f-11d45 ' +
16435
- '11d47 11d90-11d91 11d95 11d97 11df0 11ef3-11ef4 11f00-11f01 11f36-11f3a 11f40 11f42 11f5a ' +
16436
- '13440 13447-13455 1611e-16129 1612d-1612f 16af0-16af4 16b30-16b36 16f4f 16f8f-16f92 16fe4 ' +
16437
- '1bc9d-1bc9e 1cf00-1cf2d 1cf30-1cf46 1d127-1d128 1d167-1d169 1d17b-1d182 1d185-1d18b ' +
16438
- '1d1aa-1d1ad 1d242-1d244 1d25b-1d25c 1da00-1da36 1da3b-1da6c 1da75 1da84 1da9b-1da9f ' +
16439
- '1daa1-1daaf 1e000-1e006 1e008-1e018 1e01b-1e021 1e023-1e024 1e026-1e02a 1e08f 1e130-1e136 ' +
16440
- '1e2ae 1e2ec-1e2ef 1e4ec-1e4ef 1e5ee-1e5ef 1e6e3 1e6e6 1e6ee-1e6ef 1e6f5 1e8d0-1e8d6 ' +
16441
- '1e944-1e94a e0100-e01ef',
16442
- ON:
16443
- '21-22 26-2a 3b-40 5b-60 7b-7e a1 a6-a9 ab-ac ae-af b4 b6-b8 bb-bf d7 f7 2b9-2ba 2c2-2cf ' +
16444
- '2d2-2df 2e5-2ed 2ef-2ff 374-375 37e 384-385 387 3f6 58a 58d-58e 606-607 60e-60f 6de 6e9 ' +
16445
- '7f6-7f9 bf3-bf8 bfa c78-c7e f3a-f3d 1390-1399 1400 169b-169c 17f0-17f9 1800-180a 1940 ' +
16446
- '1944-1945 19de-19ff 1fbd 1fbf-1fc1 1fcd-1fcf 1fdd-1fdf 1fed-1fef 1ffd-1ffe 2010-2027 ' +
16447
- '2035-2043 2045-205e 207c-207e 208c-208f 2100-2101 2103-2106 2108-2109 2114 2116-2118 ' +
16448
- '211e-2123 2125 2127 2129 213a-213b 2140-2144 214a-214d 2150-215f 2189-218b 2190-2211 ' +
16449
- '2214-2335 237b-2394 2396-2429 2440-244a 2460-2487 24ea-26ab 26ad-27ff 2900-2b73 2b76-2bff ' +
16450
- '2ce5-2cea 2cf9-2cff 2e00-2e5d 2e60-2e63 2e80-2e99 2e9b-2ef3 2f00-2fd5 2ff0-2fff 3001-3004 ' +
16451
- '3008-3020 3030 3036-3037 303d-303f 309b-309c 30a0 30fb 31c0-31e5 31ef 321d-321e 3250-325f ' +
16452
- '327c-327e 32b1-32bf 32cc-32cf 3377-337a 33de-33df 33ff 4dc0-4dff a490-a4c6 a60d-a60f a673 ' +
16453
- 'a67e-a67f a700-a721 a788 a828-a82b a874-a877 ab6a-ab6b fbc3-fbd2 fd3e-fd4f fd90-fd91 ' +
16454
- 'fdc8-fdcf fdfd-fdff fe10-fe19 fe30-fe4f fe51 fe54 fe56-fe5e fe60-fe61 fe64-fe66 fe68 fe6b ' +
16455
- 'ff01-ff02 ff06-ff0a ff1b-ff20 ff3b-ff40 ff5b-ff65 ffe2-ffe4 ffe8-ffee fff9-fffd 10101 ' +
16456
- '10140-1018c 10190-1019c 101a0 1091f 10b39-10b3f 10d6e 10ed0-10ed8 11052-11065 11660-1166c ' +
16457
- '11fd5-11fdc 11fe1-11ff1 16fe2 1cc00-1ccd5 1ccfa-1ccfc 1cd00-1ceb3 1ceba-1ced0 1ced2-1ced4 ' +
16458
- '1cedd-1cefd 1d1e9-1d1ea 1d200-1d241 1d245 1d300-1d356 1d6c1 1d6db 1d6fb 1d715 1d735 1d74f ' +
16459
- '1d76f 1d789 1d7a9 1d7c3 1db00-1db1c 1eef0-1eef1 1f000-1f02b 1f030-1f093 1f0a0-1f0ae ' +
16460
- '1f0b1-1f0bf 1f0c1-1f0cf 1f0d1-1f0f5 1f10b-1f10f 1f12f 1f16a-1f16f 1f1ad 1f260-1f265 ' +
16461
- '1f300-1f6d9 1f6dc-1f6ec 1f6f0-1f6fc 1f700-1f7d9 1f7db 1f7e0-1f7eb 1f7f0-1f80b 1f810-1f847 ' +
16462
- '1f850-1f859 1f860-1f887 1f890-1f8ad 1f8b0-1f8bb 1f8c0-1f8c1 1f8d0-1f8d8 1f900-1fa57 ' +
16463
- '1fa60-1fa6d 1fa70-1fa7c 1fa80-1fac6 1fac8 1facc-1fadd 1fadf-1faeb 1faef-1fafa 1fb00-1fb92 ' +
16464
- '1fb94-1fbef 1fbfa',
16465
- R:
16466
- '590 5be 5c0 5c3 5c6 5ca-5ff 7c0-7ea 7f4-7f5 7fa-7fc 7fe-815 81a 824 828 82e-858 85c-85f 200f ' +
16467
- 'fb1d fb1f-fb28 fb2a-fb4f 10800-1091e 10920-10a00 10a04 10a07-10a0b 10a10-10a37 10a3b-10a3e ' +
16468
- '10a40-10ae4 10ae7-10b38 10b40-10cff 10d4a-10d68 10d6f-10e5f 10e7f-10eaa 10ead-10ebf ' +
16469
- '10f00-10f2f 10f70-10f81 10f86-10fff 1e800-1e8cf 1e8d7-1e943 1e94b-1ec6f 1ecc0-1ecff ' +
16470
- '1ed50-1edff 1ef00-1efff',
16471
- S:
16472
- '9 b 1f',
16473
- WS:
16474
- 'c 20 1680 2000-200a 2028 205f 3000'
16475
- };
16625
+ const ACCEPTED_ATTACHMENT_KINDS = Object.freeze({
16626
+ images: Object.freeze(['jpg', 'jpeg', 'png', 'gif', 'webp', 'heic', 'heif', 'bmp', 'tif', 'tiff', 'avif']),
16627
+ documents: Object.freeze(['pdf', 'docx', 'xlsx', 'pptx', 'txt', 'csv', 'tsv', 'log', 'json']),
16628
+ recordings: Object.freeze(['mp4', 'mov', 'webm', 'm4a', 'mp3', 'wav'])
16629
+ });
16476
16630
 
16477
- /** UAX #9's paired brackets (rule N0), as `opening:closing`. */
16478
- const BRACKET_PAIRS =
16479
- '28:29 5b:5d 7b:7d f3a:f3b f3c:f3d 169b:169c 2045:2046 207d:207e 208d:208e 2308:2309 230a:230b ' +
16480
- '2329:232a 2768:2769 276a:276b 276c:276d 276e:276f 2770:2771 2772:2773 2774:2775 27c5:27c6 ' +
16481
- '27e6:27e7 27e8:27e9 27ea:27eb 27ec:27ed 27ee:27ef 2983:2984 2985:2986 2987:2988 2989:298a ' +
16482
- '298b:298c 298d:2990 298f:298e 2991:2992 2993:2994 2995:2996 2997:2998 29d8:29d9 29da:29db ' +
16483
- '29fc:29fd 2e22:2e23 2e24:2e25 2e26:2e27 2e28:2e29 2e55:2e56 2e57:2e58 2e59:2e5a 2e5b:2e5c ' +
16484
- '2e62:2e63 3008:3009 300a:300b 300c:300d 300e:300f 3010:3011 3014:3015 3016:3017 3018:3019 ' +
16485
- '301a:301b fe59:fe5a fe5b:fe5c fe5d:fe5e ff08:ff09 ff3b:ff3d ff5b:ff5d ff5f:ff60 ff62:ff63';
16631
+ /** Every extension a ticket's file may end in: ACCEPTED_ATTACHMENT_KINDS's, in its order. */
16632
+ const ACCEPTED_ATTACHMENT_EXTENSIONS = Object.freeze(Object.values(ACCEPTED_ATTACHMENT_KINDS).flat());
16633
+
16634
+ const ACCEPTED_EXTENSION_SET = new Set(ACCEPTED_ATTACHMENT_EXTENSIONS);
16486
16635
 
16487
16636
  /**
16488
- * Whether an organization's name, or any text, reads as SparkVault's own.
16489
- *
16490
- * An organization's name is the author a recipient reads on its mail:
16491
- * Identity's sign-in codes leave the platform's sign-in mailbox under it, and
16492
- * Notify relays an organization's message from "{name}"
16493
- * <notify@{handle}.send.sv>. A name that reads as SparkVault or SparkLink
16494
- * would author that mail as the platform, a name that carries an email
16495
- * address would author it as whoever that address names, and a name that
16496
- * reads as a web address would show a link where its author belongs.
16497
- *
16498
- * A reader reads letters, not code points, and reads them in the order they
16499
- * are laid out, so a text is judged the way it is read:
16500
- * - in each form a surface shows it in: a lone surrogate, a code unit no
16501
- * other pairs with (a JSON escape such as \ud800 puts one in a string), is
16502
- * shown as U+FFFD REPLACEMENT CHARACTER where the text is carried as UTF-8
16503
- * (a From line's encoded-word), and as itself where a JSON answer carries
16504
- * it (the answer writes the escape \ud800, and a JavaScript client's
16505
- * JSON.parse restores the surrogate), which the client lays out as the
16506
- * left-to-right character Unicode classes it as; U+FFFD is a neutral,
16507
- * which joins the right-to-left run around it, and the surrogate splits
16508
- * that run, so a text that holds one is read both ways (formsShown);
16509
- * - in NFC: canonically equivalent texts show alike, so the rule reads one
16510
- * text for all of them, a precomposed character for the character and marks
16511
- * it decomposes to (U+00CC, an I with a grave accent, for I and U+0300
16512
- * COMBINING GRAVE ACCENT) and a Hangul syllable for its jamo, each composed
16513
- * character a reader may pass over wherever they may pass over its parts;
16514
- * - each character as the Latin letters it passes for
16515
- * (./organization-name-unicode.js LETTER_READINGS, derived from Unicode's
16516
- * confusables, names and compatibility decompositions and a hand list of
16517
- * lookalikes, the alphabets fancy-text generators substitute for Latin ones
16518
- * among them; any other character through NFKD), a character at times
16519
- * reading as either of two letters (U+028B, a v with a hook, is a u or a v),
16520
- * and i and l as one letter, since a capital I and a small l are one glyph
16521
- * in most sans-serif faces;
16522
- * - a character that reads as no letter (a space, punctuation, an invisible
16523
- * character) passed over, so it never breaks a word; the marks drawn on a
16524
- * character, each read just before that character or just after it, in any
16525
- * order, or not at all; and a character a reader may take for a letter or
16526
- * pass over, read where it stands or not at all (OPTIONAL_LETTERS): a
16527
- * superscript or subscript form, drawn small beside a word's letters; a
16528
- * character whose letters are a resemblance only, a fancy-text generator's
16529
- * letter among them (U+A317 YI SYLLABLE SOT, an s, is drawn as U+00A7
16530
- * SECTION SIGN); a digit, punctuation mark or symbol that passes for a
16531
- * letter (`1`, `$`, `+`), or a letter drawn as one; and a bare vertical
16532
- * stroke (`|`, `l`, `I`), which reads as a separator as readily as an l; so
16533
- * none of those breaks a word either, and a character breaks one only where
16534
- * a reader must read it as letters: it is a form of them, or UTS #39 draws
16535
- * it as Latin letters alone;
16536
- * - in every order it can be laid out in: as stored, and as the Unicode
16537
- * bidirectional algorithm (UAX #9) lays it out in a left-to-right and in a
16538
- * right-to-left paragraph (an inbox in a right-to-left language, or a
16539
- * client that takes the direction from the text's first letter), where a
16540
- * right-to-left letter or mark, or an Arabic digit, between Latin words
16541
- * reverses their order: "Vault", U+05BE HEBREW PUNCTUATION MAQAF, "Spark" is
16542
- * laid out in a right-to-left paragraph as "Spark", the maqaf, "Vault".
16543
- * An explicit direction control (holdsDirectionControl, ./display-text.js)
16544
- * sets the direction of the text after it beyond what this lays out, so a
16545
- * caller that judges what a reader sees refuses it as well, as
16546
- * isReservedDisplayText, for any text an organization shows under its name,
16547
- * and isReservedOrganizationName do. Both also judge a text at every cut of it
16548
- * as stored, each cut read in this way, since the platform shows a text cut to
16549
- * fit. The implicit marks are no such control: each is a strong character of
16550
- * its direction, laid out as a letter of that direction is.
16551
- *
16552
- * BROWSER-SAFE: imports only ./display-text.js and
16553
- * ./organization-name-unicode.js, neither of which imports anything, and needs
16554
- * String.prototype.isWellFormed and toWellFormed (ES2024: Chrome 111,
16555
- * Firefox 119, Safari 16.4).
16637
+ * The extensions Forge never completes a saved name with (lowercase, no dot):
16638
+ * a vault download's name that shows no extension the MIME registry knows is
16639
+ * completed with its type's registered one only when that is none of these
16640
+ * (./media-type-node.js withMediaTypeExtension), so no download saves as a
16641
+ * program, a script, a page or any file a desktop acts on when it is opened.
16642
+ * That completion is all that reads the list: a ticket takes only the
16643
+ * extensions it accepts (ACCEPTED_ATTACHMENT_EXTENSIONS), none of them one of
16644
+ * these. Every extension Outlook blocks as an attachment is one, held to
16645
+ * Microsoft's list by attachment-policy.test.js. The list is ordered by kind.
16556
16646
  */
16647
+ const REFUSED_EXTENSIONS = Object.freeze([
16648
+ 'exe', 'dll', 'scr', 'com', 'pif', 'cpl', 'msc', 'bat', 'cmd',
16649
+ 'ps1', 'ps1xml', 'ps2', 'ps2xml', 'psc1', 'psc2', 'psd1', 'psdm1', 'pssc', 'cdxml',
16650
+ 'msh', 'msh1', 'msh2', 'mshxml', 'msh1xml', 'msh2xml',
16651
+ 'vb', 'vbs', 'vbe', 'js', 'jse', 'ws', 'wsf', 'wsh', 'wsc', 'sct',
16652
+ 'msi', 'msp', 'mst', 'msu', 'cab', 'diagcab', 'inf', 'jar', 'hta', 'htc', 'reg',
16653
+ 'lnk', 'url', 'website', 'shb', 'shs', 'scf', 'xnk', 'grp', 'library-ms', 'search-ms',
16654
+ 'settingcontent-ms', 'appcontent-ms', 'theme', 'wsb', 'gadget', 'osd', 'bgi', 'udl', 'ins', 'isp',
16655
+ 'webpnp', 'printerexport',
16656
+ 'iso', 'img', 'vhd', 'vhdx', 'dmg', 'app',
16657
+ 'sh', 'bash', 'zsh', 'ksh', 'csh', 'tcl', 'py', 'pyc', 'pyo', 'pyw', 'pyz', 'pyzw', 'pl', 'rb',
16658
+ 'php', 'phtml', 'asp', 'aspx',
16659
+ 'html', 'htm', 'xhtml', 'shtml', 'shtm', 'xht', 'xhtm', 'mht', 'mhtml', 'svg', 'svgz', 'xml', 'xsl', 'xslt',
16660
+ 'rdf', 'xul',
16661
+ 'docm', 'dotm', 'xlsm', 'xltm', 'xlam', 'pptm', 'potm', 'ppam', 'sldm', 'ppsm', 'xlsb', 'xla', 'xlm', 'xll',
16662
+ 'application', 'appref-ms', 'xbap', 'appinstaller', 'appx', 'appxbundle', 'msix', 'msixbundle',
16663
+ 'chm', 'its', 'hlp', 'cnt', 'hpj',
16664
+ 'mdb', 'adp', 'ade', 'mag', 'mad', 'maf', 'mam', 'maq', 'mar', 'mas', 'mat', 'mav', 'maw', 'mda', 'mde',
16665
+ 'mdt', 'mdw', 'mdz',
16666
+ 'prf', 'ops', 'pst',
16667
+ 'fxp', 'prg', 'bas', 'vbp', 'vsmacros', 'vsw', 'pcd', 'plg',
16668
+ 'cer', 'der', 'crt', 'cert', 'pem', 'p12', 'pfx', 'pkcs12', 'p7b', 'p7c', 'p7r', 'spc',
16669
+ 'jnlp', 'class', 'air', 'crx', 'xpi', 'oxt', 'vbox-extpack',
16670
+ 'apk', 'pkg', 'mpkg', 'deb', 'udeb', 'rpm', 'run',
16671
+ 'asx', 'wax', 'wvx', 'wmx', 'wmd',
16672
+ 'mau', 'mcf', 'tmp'
16673
+ ]);
16557
16674
 
16558
- /** The words no organization's name may read as: the platform's brand and its link product. */
16559
- const RESERVED_BRAND_TOKENS = Object.freeze(['sparkvault', 'sparklink']);
16675
+ /**
16676
+ * Why a name or a head slice is refused, as the two refusal functions report
16677
+ * it. REFUSED_EXTENSION: the extension the name's saved copy opens as is none
16678
+ * a ticket accepts, or the name shows none.
16679
+ */
16680
+ const ATTACHMENT_REFUSALS = Object.freeze({
16681
+ EMPTY: 'empty',
16682
+ TOO_LONG: 'too_long',
16683
+ UNSAFE_NAME: 'unsafe_name',
16684
+ REFUSED_EXTENSION: 'refused_extension',
16685
+ EXECUTABLE: 'executable',
16686
+ SCRIPT: 'script',
16687
+ MARKUP: 'markup'
16688
+ });
16560
16689
 
16561
- /** i and l read as one letter. */
16562
- const foldLetter = (letter) => (letter === 'i' ? 'l' : letter);
16690
+ /** How many leading bytes the sniff reads; a caller slices `file.slice(0, ATTACHMENT_SNIFF_BYTES)`. */
16691
+ const ATTACHMENT_SNIFF_BYTES = 4096;
16563
16692
 
16564
- /** The letters of the reserved words, each a bit of a position's mask. */
16565
- const BRAND_BITS = new Map(
16566
- [...new Set(Array.from(RESERVED_BRAND_TOKENS.join(''), foldLetter))].map((letter, index) => [letter, 1 << index])
16567
- );
16693
+ new Set(REFUSED_EXTENSIONS);
16568
16694
 
16569
- /** The brand letters a group of letters can read as, as a mask: 0 for a letter of no reserved word. */
16570
- function lettersMask(letters) {
16571
- let mask = 0;
16572
- for (const letter of letters) mask |= BRAND_BITS.get(foldLetter(letter)) ?? 0;
16573
- return mask;
16574
- }
16695
+ /** Leading byte sequences of code a desktop would run. */
16696
+ const EXECUTABLE_SIGNATURES = Object.freeze([
16697
+ [0x4d, 0x5a], // MZ: Windows PE
16698
+ [0x7f, 0x45, 0x4c, 0x46], // ELF
16699
+ [0xfe, 0xed, 0xfa, 0xce], // Mach-O 32-bit
16700
+ [0xfe, 0xed, 0xfa, 0xcf], // Mach-O 64-bit
16701
+ [0xce, 0xfa, 0xed, 0xfe], // Mach-O 32-bit, reversed
16702
+ [0xcf, 0xfa, 0xed, 0xfe], // Mach-O 64-bit, reversed
16703
+ [0xca, 0xfe, 0xba, 0xbe], // Mach-O universal / Java class
16704
+ [0xbe, 0xba, 0xfe, 0xca] // Mach-O universal, reversed
16705
+ ]);
16575
16706
 
16576
- /** Each reserved word as the masks its letters need. */
16577
- const TOKEN_MASKS = RESERVED_BRAND_TOKENS.map((token) => Array.from(token, lettersMask));
16707
+ const SHEBANG = Object.freeze([0x23, 0x21]);
16578
16708
 
16579
- /** Each code point a generated list names: hexadecimal, separated by spaces, `first-last` a range. */
16580
- function* codePointsOf(list) {
16581
- for (const entry of list.split(' ')) {
16582
- const [first, last = first] = entry.split('-');
16583
- for (let codePoint = parseInt(first, 16); codePoint <= parseInt(last, 16); codePoint++) yield codePoint;
16584
- }
16709
+ /**
16710
+ * A ZIP local file header's signature (`PK\x03\x04`): the first bytes of
16711
+ * every Office Open XML package (.docx, .xlsx, .pptx), the only ZIP a ticket
16712
+ * takes. Its parts are XML by definition, and a package whose ZIP stores them
16713
+ * uncompressed, as common web exporters write one, shows its first part's
16714
+ * `<?xml` declaration in the first kilobyte; no browser reads a file that
16715
+ * opens with this signature as a page.
16716
+ */
16717
+ const ZIP_LOCAL_FILE_SIGNATURE = Object.freeze([0x50, 0x4b, 0x03, 0x04]);
16718
+
16719
+ /** Document openers a browser would run as a page; matched case-insensitively in the first kilobyte. */
16720
+ const MARKUP_OPENERS = Object.freeze(['<?xml', '<!doctype html', '<html', '<script', '<svg']);
16721
+ const MARKUP_WINDOW = 1024;
16722
+
16723
+ function startsWith(bytes, prefix) {
16724
+ return bytes.length >= prefix.length && prefix.every((byte, index) => bytes[index] === byte);
16585
16725
  }
16586
16726
 
16587
- /** Each listed character's reading: one mask per letter position. */
16588
- const LISTED_READINGS = new Map();
16589
- for (const [reading, list] of Object.entries(LETTER_READINGS)) {
16590
- const positions = Object.freeze(reading.split('.').map(lettersMask));
16591
- for (const codePoint of codePointsOf(list)) LISTED_READINGS.set(codePoint, positions);
16727
+ /**
16728
+ * What stops `name` from naming a ticket attachment, or null when nothing does.
16729
+ * Its extension is the one a saved copy opens as (lastExtension), and a ticket
16730
+ * takes it only when that is one it accepts (ACCEPTED_ATTACHMENT_EXTENSIONS),
16731
+ * so a name of no extension is refused, and neither a SparkVault document's
16732
+ * two-part extension ("invoice.note.html"), trailing dots or spaces
16733
+ * ("page.html."), an invisible character ("evil.ex<U+034F>e") nor a space
16734
+ * Firefox closes up ("evil.ex e", which Firefox saves as "evil.exe") passes
16735
+ * for an accepted one; a name that opens as one is taken however it is
16736
+ * written ("Photo.JPG", "report.pdf.", "Scan. pdf").
16737
+ *
16738
+ * @param {unknown} name
16739
+ * @returns {string|null} One of ATTACHMENT_REFUSALS, or null
16740
+ */
16741
+ function attachmentNameRefusal(name) {
16742
+ if (typeof name !== 'string' || name.length === 0) return ATTACHMENT_REFUSALS.EMPTY;
16743
+ if (name.length > INGOT_NAME_MAX) return ATTACHMENT_REFUSALS.TOO_LONG;
16744
+ if (storableFilenameProblem(name) !== null) return ATTACHMENT_REFUSALS.UNSAFE_NAME;
16745
+ if (!ACCEPTED_EXTENSION_SET.has(lastExtension(name))) return ATTACHMENT_REFUSALS.REFUSED_EXTENSION;
16746
+ return null;
16592
16747
  }
16593
16748
 
16594
- /** The characters whose letters a reader may read or pass over. */
16595
- new Set(codePointsOf(OPTIONAL_LETTERS));
16749
+ /**
16750
+ * What the first bytes of a file say it is that no ticket may carry, or null
16751
+ * when they say nothing of the kind: a program, a script, or markup a browser
16752
+ * would run as a page, which a ZIP package's head is never read for
16753
+ * (ZIP_LOCAL_FILE_SIGNATURE). Reads at most ATTACHMENT_SNIFF_BYTES.
16754
+ *
16755
+ * @param {Uint8Array} headBytes
16756
+ * @returns {string|null} One of ATTACHMENT_REFUSALS, or null
16757
+ */
16758
+ function attachmentSniffRefusal(headBytes) {
16759
+ const bytes = headBytes.subarray(0, ATTACHMENT_SNIFF_BYTES);
16760
+ if (EXECUTABLE_SIGNATURES.some((signature) => startsWith(bytes, signature))) return ATTACHMENT_REFUSALS.EXECUTABLE;
16761
+ if (startsWith(bytes, SHEBANG)) return ATTACHMENT_REFUSALS.SCRIPT;
16762
+ if (startsWith(bytes, ZIP_LOCAL_FILE_SIGNATURE)) return null;
16763
+ // Latin-1 decoding keeps one byte per character, so a UTF-8 or Latin-1
16764
+ // document's ASCII markup reads the same; a BOM or leading whitespace is
16765
+ // skipped by searching the window instead of anchoring at offset 0.
16766
+ const text = String.fromCharCode(...bytes.subarray(0, MARKUP_WINDOW)).toLowerCase();
16767
+ if (MARKUP_OPENERS.some((opener) => text.includes(opener))) return ATTACHMENT_REFUSALS.MARKUP;
16768
+ return null;
16769
+ }
16596
16770
 
16597
- /** Every bidirectional class other than L, as sorted [first, last, class] ranges. */
16598
- Object.entries(BIDI_CLASSES)
16599
- .flatMap(([bidiClass, list]) => list.split(' ').map((entry) => {
16600
- const [first, last = first] = entry.split('-');
16601
- return [parseInt(first, 16), parseInt(last, 16), bidiClass];
16602
- }))
16603
- .sort((a, b) => a[0] - b[0]);
16771
+ /**
16772
+ * A byte count as people read it: "500 B", "1 KB", "1.5 MB", "5 GB", in
16773
+ * binary units (1 KB = 1024 B) up to TB, with at most `decimals` decimal
16774
+ * places and no trailing zeros. The one size formatter every surface writes
16775
+ * sizes with; a runtime that cannot import app-sdk runs a generated mirror of
16776
+ * it (module-mirrors).
16777
+ *
16778
+ * A count that is missing, not a finite number, zero or negative reads
16779
+ * "0 B"; a count past the largest unit is written in TB.
16780
+ *
16781
+ * BROWSER-SAFE: data only, no imports.
16782
+ */
16604
16783
 
16605
- /** Each opening paired bracket's closing bracket, and the reverse (UAX #9 BD16). */
16606
- const CLOSING_BRACKET = new Map();
16607
- const OPENING_BRACKET = new Map();
16608
- for (const pair of BRACKET_PAIRS.split(' ')) {
16609
- const [opening, closing] = pair.split(':').map((codePoint) => parseInt(codePoint, 16));
16610
- CLOSING_BRACKET.set(opening, closing);
16611
- OPENING_BRACKET.set(closing, opening);
16784
+ const UNITS = Object.freeze(['B', 'KB', 'MB', 'GB', 'TB']);
16785
+ const STEP = 1024;
16786
+
16787
+ /**
16788
+ * @param {number|null|undefined} bytes
16789
+ * @param {number} [decimals=1] - The most decimal places shown
16790
+ * @returns {string}
16791
+ */
16792
+ function formatBytes(bytes, decimals = 1) {
16793
+ if (typeof bytes !== 'number' || !Number.isFinite(bytes) || bytes <= 0) return `0 ${UNITS[0]}`;
16794
+ const unit = Math.min(Math.max(Math.floor(Math.log(bytes) / Math.log(STEP)), 0), UNITS.length - 1);
16795
+ return `${parseFloat((bytes / STEP ** unit).toFixed(decimals))} ${UNITS[unit]}`;
16612
16796
  }
16613
16797
 
16614
16798
  /**
16615
- * A reading's progress, as one bit set (a bit-parallel search): for each
16616
- * reserved word a bit per letter, set while the letters read so far end in the
16617
- * word spelled up to that letter, the words' bits side by side from
16618
- * WORD_OFFSETS; and SPELLED, set once any word has been spelled whole. Two
16619
- * ways of reading a text merge as the union of their bits.
16799
+ * What stops a file from joining a message, decided in the browser before any
16800
+ * upload starts: the one check the portal's editor and the console's
16801
+ * composer run. The server never reads an uploaded file's plaintext, so the
16802
+ * check on a file's first bytes can only run here, over the file itself; the
16803
+ * name rule runs here and again on the server when the upload is initiated.
16804
+ * Both are the platform's one attachment policy (./attachment-policy.js);
16805
+ * this adds only what no upload can take: an empty file, one past the most
16806
+ * its uploader may attach (a customer ./help-desk-contract.js
16807
+ * CUSTOMER_ATTACHMENT_MAX_BYTES, an operator MAX_ATTACHMENT_BYTES), and one
16808
+ * the browser can no longer read (changed or moved on disk after it was
16809
+ * picked). Beside the check, the sentence each refusal reads, the one
16810
+ * phrase that names the files a ticket accepts, and what a customer's file
16811
+ * picker states and filters before any file is chosen.
16812
+ *
16813
+ * BROWSER-SAFE: reads the file it is handed through the Blob API, which
16814
+ * Node's File has too.
16620
16815
  */
16621
- const WORD_OFFSETS = TOKEN_MASKS.map((_, word) =>
16622
- TOKEN_MASKS.slice(0, word).reduce((offset, token) => offset + token.length, 0));
16623
16816
 
16624
- /** Each word's first letter, where a new spelling can start at any letter read. */
16625
- WORD_OFFSETS.reduce((bits, offset) => bits | (1 << offset), 0);
16626
16817
 
16627
- /** Each word's last letter. */
16628
- TOKEN_MASKS.reduce((bits, token, word) => bits | (1 << (WORD_OFFSETS[word] + token.length - 1)), 0);
16818
+ /** The files refused beyond the policy's. */
16819
+ const FILE_REFUSALS = Object.freeze({ EMPTY_FILE: 'empty_file', TOO_LARGE: 'too_large', UNREADABLE: 'unreadable' });
16629
16820
 
16630
- 1 << TOKEN_MASKS.reduce((count, token) => count + token.length, 0);
16821
+ /** The first bytes of `file`, or null when the browser cannot read them. */
16822
+ async function headOf(file) {
16823
+ try {
16824
+ return new Uint8Array(await file.slice(0, ATTACHMENT_SNIFF_BYTES).arrayBuffer());
16825
+ } catch {
16826
+ return null;
16827
+ }
16828
+ }
16631
16829
 
16632
- /** For each mask of brand letters, the letters of the words a letter of it can be. */
16633
- Array.from({ length: 1 << BRAND_BITS.size }, (_, mask) =>
16634
- TOKEN_MASKS.reduce((bits, token, word) => token.reduce((wordBits, letters, position) =>
16635
- ((letters & mask) === 0 ? wordBits : wordBits | (1 << (WORD_OFFSETS[word] + position))), bits), 0));
16830
+ /** A ceiling a file is held to: a positive whole number of bytes, or a TypeError. */
16831
+ function assertCeiling(maxBytes) {
16832
+ if (!Number.isSafeInteger(maxBytes) || maxBytes < 1) throw new TypeError('A file is held to a ceiling of a positive whole number of bytes');
16833
+ }
16636
16834
 
16637
16835
  /**
16638
- * The Secure Help Desk's shared vocabulary: the values Core's help-desk engine
16639
- * stores and checks, and the console and the customer portal render, defined
16640
- * once so the three cannot disagree about a status, a label, a limit, a timing
16641
- * or an event's visibility.
16836
+ * Why `file` cannot be attached, or null when it may be: its name first, then
16837
+ * its size against `maxBytes`, then its first bytes.
16642
16838
  *
16643
- * A ticket is `open`, `waiting_on_customer`, `on_hold` (until a time) or
16644
- * `closed` (as resolved, a duplicate merged elsewhere, or spam). A customer
16645
- * never sees those words: customerStatusLabel is the one translation into
16646
- * theirs. Every timeline event type declares whether a customer may see it;
16647
- * a customer-facing projection keeps only `public` types (an allowlist, so a
16648
- * new type is hidden until it says otherwise). The portal's client name and
16649
- * API prefix, team mail aliases, the attachment ceremony's steps, session,
16650
- * sign-in code, merge and pending-attachment timings, hold presets, tag and
16651
- * size limits, the operator display and desk mode choices and the
16652
- * response-time histogram's bucket edges live here for the same reason, and
16653
- * so does the one rule a tag is stored by (ticketTag), which Core applies
16654
- * where it stores a tag and the console applies where one is typed.
16839
+ * @param {File} file
16840
+ * @param {number} maxBytes - The most its uploader may attach: a customer
16841
+ * CUSTOMER_ATTACHMENT_MAX_BYTES, an operator MAX_ATTACHMENT_BYTES
16842
+ * @returns {Promise<string|null>} One of ATTACHMENT_REFUSALS or FILE_REFUSALS, or null
16843
+ * @throws {TypeError} For a ceiling that is not a positive whole number of bytes
16844
+ */
16845
+ async function attachmentRefusal(file, maxBytes) {
16846
+ assertCeiling(maxBytes);
16847
+ const byName = attachmentNameRefusal(file.name);
16848
+ if (byName) return byName;
16849
+ if (file.size === 0) return FILE_REFUSALS.EMPTY_FILE;
16850
+ if (file.size > maxBytes) return FILE_REFUSALS.TOO_LARGE;
16851
+ const head = await headOf(file);
16852
+ return head ? attachmentSniffRefusal(head) : FILE_REFUSALS.UNREADABLE;
16853
+ }
16854
+
16855
+ /**
16856
+ * The words naming every kind of file a ticket accepts with its extensions,
16857
+ * the last kind joined by "and": `images (.jpg, .jpeg, ...), documents
16858
+ * (.pdf, ...) and recordings (.mp4, ...)`.
16655
16859
  *
16656
- * BROWSER-SAFE: data and pure functions; its imports are the platform's label
16657
- * rule a tag is built on (./display-text.js), the mail products whose fixed
16658
- * From local parts no team alias may take (./mail-products.js) and the brand
16659
- * rule no team alias may read as (./organization-name.js).
16860
+ * @param {Readonly<Record<string, readonly string[]>>} kinds - ACCEPTED_ATTACHMENT_KINDS
16861
+ * @returns {string}
16660
16862
  */
16863
+ function acceptedKindsCopy(kinds) {
16864
+ const named = Object.entries(kinds).map(([kind, extensions]) => {
16865
+ const listed = extensions.map((extension) => `.${extension}`).join(', ');
16866
+ return `${kind} (${listed})`;
16867
+ });
16868
+ return `${named.slice(0, -1).join(', ')} and ${named.at(-1)}`;
16869
+ }
16661
16870
 
16871
+ /**
16872
+ * The files a ticket accepts, in the one phrase every surface names them by,
16873
+ * built from the policy's own list (ACCEPTED_ATTACHMENT_KINDS): the sentence
16874
+ * refusing any other file reads it, and so do the API reference
16875
+ * (apps/api/openapi/overlay.mjs) and the help desk's page on the site
16876
+ * (apps/www/_data/helpDeskAttachments.js).
16877
+ */
16878
+ const ACCEPTED_ATTACHMENTS_COPY = acceptedKindsCopy(ACCEPTED_ATTACHMENT_KINDS);
16662
16879
 
16663
16880
  /**
16664
- * Mail local parts no team may take as its alias: the role mailboxes RFC 2142
16665
- * names, the addresses mail systems and their operators keep for themselves,
16666
- * the platform's own host labels, and the fixed local part every other mail
16667
- * product writes from (MAIL_PRODUCTS fromLocalPart), so no team writes from
16668
- * the address Notify writes from on a domain both products use.
16881
+ * What a customer's file picker says before any file is chosen: what to add,
16882
+ * how many files a message takes and how large each may be. The portal's
16883
+ * editor and the SDK's support form show it under their pickers.
16669
16884
  */
16670
- Object.freeze([
16671
- 'abuse', 'postmaster', 'hostmaster', 'webmaster', 'noc', 'security', 'info', 'marketing', 'sales',
16672
- 'admin', 'administrator', 'root', 'mailer-daemon', 'no-reply', 'noreply', 'bounce', 'bounces',
16673
- 'dmarc', 'spf', 'dkim', 'probe',
16674
- 'www', 'api', 'mail', 'smtp', 'mx', 'help', 'sparkvault',
16675
- ...MAIL_PRODUCT_KEYS.map((key) => MAIL_PRODUCTS[key].fromLocalPart).filter((localPart) => localPart !== null)
16676
- ]);
16885
+ const CUSTOMER_ATTACHMENTS_HINT =
16886
+ `Add screenshots, documents or recordings. Up to ${MAX_ATTACHMENTS_PER_MESSAGE} files, ${formatBytes(CUSTOMER_ATTACHMENT_MAX_BYTES)} each.`;
16677
16887
 
16678
16888
  /**
16679
- * Why a customer's file is refused by the desk's upload allowance per
16680
- * customer (`customer_upload_limit_bytes`), as the 402 QUOTA_EXCEEDED that
16681
- * refuses it names in `details.reason`, so the portal tells it from the
16682
- * organization's storage being full: the customer's files would pass their
16683
- * allowance, or the allowance is 0, which turns customer uploads off. A file
16684
- * an email carried that the allowance refused is listed by the same reason
16685
- * among the files its message left out (`email.skipped_attachments`).
16889
+ * A file input's `accept`: every extension a ticket accepts
16890
+ * (ACCEPTED_ATTACHMENT_EXTENSIONS), each with its dot, so the system's file
16891
+ * chooser offers those files first. A filter, never the check: a person can
16892
+ * still pick any file, and attachmentRefusal decides.
16686
16893
  */
16687
- const CUSTOMER_UPLOAD_REFUSALS = Object.freeze({
16688
- ALLOWANCE_REACHED: 'customer_upload_limit',
16689
- UPLOADS_OFF: 'customer_uploads_off'
16894
+ const ATTACHMENT_INPUT_ACCEPT = ACCEPTED_ATTACHMENT_EXTENSIONS.map((extension) => `.${extension}`).join(',');
16895
+
16896
+ const REFUSAL_COPY = Object.freeze({
16897
+ [ATTACHMENT_REFUSALS.EMPTY]: 'This file has no name.',
16898
+ [ATTACHMENT_REFUSALS.TOO_LONG]: "This file's name is too long. Rename it and try again.",
16899
+ [ATTACHMENT_REFUSALS.UNSAFE_NAME]: "This file's name can't be used. Rename it and try again.",
16900
+ [ATTACHMENT_REFUSALS.REFUSED_EXTENSION]: `Files of this type can't be attached. You can attach ${ACCEPTED_ATTACHMENTS_COPY}.`,
16901
+ [ATTACHMENT_REFUSALS.EXECUTABLE]: "This file is a program, and programs can't be attached.",
16902
+ [ATTACHMENT_REFUSALS.SCRIPT]: "This file is a script, and scripts can't be attached.",
16903
+ [ATTACHMENT_REFUSALS.MARKUP]: "This file holds web page markup, which can't be attached.",
16904
+ [FILE_REFUSALS.EMPTY_FILE]: 'This file is empty.',
16905
+ [FILE_REFUSALS.UNREADABLE]: "This file couldn't be read. Remove it and pick it again."
16690
16906
  });
16691
16907
 
16692
- /** The most attachments one message carries. */
16693
- const MAX_ATTACHMENTS_PER_MESSAGE = 20;
16908
+ /**
16909
+ * The sentence a person reads for a file larger than `maxBytes`, the ceiling
16910
+ * it was held to: the browser's check's (FILE_REFUSALS.TOO_LARGE), and the
16911
+ * server's, which names its own (`details.max_bytes` of a 413).
16912
+ *
16913
+ * @param {number} maxBytes
16914
+ * @returns {string}
16915
+ */
16916
+ function tooLargeCopy(maxBytes) {
16917
+ assertCeiling(maxBytes);
16918
+ return `This file is larger than ${formatBytes(maxBytes)}.`;
16919
+ }
16694
16920
 
16695
- /** The largest file a customer uploads through the portal, in bytes. */
16696
- const CUSTOMER_ATTACHMENT_MAX_BYTES = 25 * 1024 * 1024;
16921
+ /**
16922
+ * The sentence a person reads for a refusal.
16923
+ *
16924
+ * @param {string} refusal - One of ATTACHMENT_REFUSALS or FILE_REFUSALS
16925
+ * @param {number} maxBytes - The ceiling attachmentRefusal held the file to
16926
+ * @returns {string}
16927
+ * @throws {TypeError} For a ceiling that is not a positive whole number of bytes
16928
+ */
16929
+ function refusalCopy(refusal, maxBytes) {
16930
+ assertCeiling(maxBytes);
16931
+ if (refusal === FILE_REFUSALS.TOO_LARGE) return tooLargeCopy(maxBytes);
16932
+ return REFUSAL_COPY[refusal] ?? "This file can't be attached.";
16933
+ }
16697
16934
 
16698
16935
  function createProgress() { return document.createElement('progress'); }
16699
16936
  /** A failed mint's quota/size refusal remains a visible file error, never a silently omitted file. */
@@ -16730,12 +16967,12 @@ function createFormAttachments(client, prefix, context) {
16730
16967
  input.name = 'attachments';
16731
16968
  input.id = label.htmlFor;
16732
16969
  input.multiple = true;
16733
- input.accept = ACCEPTED_ATTACHMENT_EXTENSIONS.map(extension => `.${extension}`).join(',');
16970
+ input.accept = ATTACHMENT_INPUT_ACCEPT;
16734
16971
  input.className = 'svhd-input';
16735
16972
  const hint = document.createElement('p');
16736
16973
  hint.id = `${prefix}-attachment-hint`;
16737
16974
  hint.className = 'svhd-file-hint';
16738
- hint.textContent = `Add screenshots, documents or recordings. Up to ${MAX_ATTACHMENTS_PER_MESSAGE} files, ${formatBytes(CUSTOMER_ATTACHMENT_MAX_BYTES)} each.`;
16975
+ hint.textContent = CUSTOMER_ATTACHMENTS_HINT;
16739
16976
  input.setAttribute('aria-describedby', hint.id);
16740
16977
  const notice = document.createElement('p');
16741
16978
  notice.className = 'svhd-error svhd-file-hint';
@@ -16946,6 +17183,7 @@ function createFormAttachments(client, prefix, context) {
16946
17183
  element,
16947
17184
  get attachmentIds() { return entries.filter(entry => entry.state === 'complete' && entry.attachmentId).map(entry => entry.attachmentId); },
16948
17185
  get unresolved() { return entries.some(entry => entry.state !== 'complete'); },
17186
+ get hasEntries() { return entries.length > 0; },
16949
17187
  get busy() { return entries.some(entry => !!entry.operation); },
16950
17188
  async start() { await Promise.all(entries.map(entry => entry.operation ?? (entry.state === 'queued' ? run(entry) : Promise.resolve()))); },
16951
17189
  lock(value) { locked = value; input.disabled = value || pickingDisabled; entries.forEach(controls); },
@@ -16983,18 +17221,24 @@ function notify(callback, value) {
16983
17221
  // The host owns its callback. A confirmed send must never become a retry.
16984
17222
  }
16985
17223
  }
16986
- function hostedFormUrl(host) {
17224
+ /** `https://{host}{path}` on the desk's own host, or null when the host is anything but a bare host name. */
17225
+ function hostedUrl(host, path) {
16987
17226
  try {
16988
17227
  const url = new URL(`https://${host}`);
16989
17228
  if (url.host !== host || url.username || url.password || url.pathname !== '/' || url.search || url.hash)
16990
17229
  return null;
16991
- url.pathname = '/requests/new';
17230
+ url.pathname = path;
16992
17231
  return url.href;
16993
17232
  }
16994
17233
  catch {
16995
17234
  return null;
16996
17235
  }
16997
17236
  }
17237
+ /** Whether a field holds text of the customer's own: anything but nothing or what the host prefilled. */
17238
+ function edited(input, prefill) {
17239
+ const text = input.value.trim();
17240
+ return text !== '' && text !== (prefill ?? '').trim();
17241
+ }
16998
17242
  function companySignInUrl(value) {
16999
17243
  try {
17000
17244
  const url = new URL(value ?? '');
@@ -17008,6 +17252,12 @@ function companySignInUrl(value) {
17008
17252
  * Inject a ticket form using only the requester's customer API. Authentication
17009
17253
  * is the desk's email-code flow or an explicit host-provided customer session.
17010
17254
  * Files use the same direct resumable Forge ceremony as the hosted portal.
17255
+ *
17256
+ * A check the form makes itself (an empty subject, a short code) and the
17257
+ * server's refusal of a code read under the field they concern; the status
17258
+ * line carries the flow (a code sent, a send in progress or uncertain, a
17259
+ * changed sign-in, a server error). Closing with unsent work asks first.
17260
+ * A confirmed send replaces the form with its confirmation.
17011
17261
  */
17012
17262
  function mountHelpDeskForm(client, options) {
17013
17263
  const target = resolveTarget(options.target);
@@ -17026,6 +17276,30 @@ function mountHelpDeskForm(client, options) {
17026
17276
  form.noValidate = true;
17027
17277
  form.setAttribute('aria-labelledby', heading.id);
17028
17278
  shell.body.append(heading, form);
17279
+ // A field's error is an alert only while it shows, so it is announced
17280
+ // even when focus already sits on the field it names.
17281
+ function setFieldError(entry, text) {
17282
+ entry.error.textContent = text;
17283
+ entry.error.setAttribute('role', 'alert');
17284
+ entry.error.hidden = false;
17285
+ entry.input.setAttribute('aria-invalid', 'true');
17286
+ const described = (entry.input.getAttribute('aria-describedby') ?? '').split(' ').filter(Boolean);
17287
+ if (!described.includes(entry.error.id))
17288
+ entry.input.setAttribute('aria-describedby', [...described, entry.error.id].join(' '));
17289
+ }
17290
+ function clearFieldError(entry) {
17291
+ if (entry.error.hidden)
17292
+ return;
17293
+ entry.error.textContent = '';
17294
+ entry.error.removeAttribute('role');
17295
+ entry.error.hidden = true;
17296
+ entry.input.removeAttribute('aria-invalid');
17297
+ const described = (entry.input.getAttribute('aria-describedby') ?? '').split(' ').filter((id) => id !== '' && id !== entry.error.id);
17298
+ if (described.length > 0)
17299
+ entry.input.setAttribute('aria-describedby', described.join(' '));
17300
+ else
17301
+ entry.input.removeAttribute('aria-describedby');
17302
+ }
17029
17303
  function field(labelText, name, textarea = false) {
17030
17304
  const wrapper = document.createElement('div');
17031
17305
  wrapper.className = 'svhd-field';
@@ -17037,23 +17311,46 @@ function mountHelpDeskForm(client, options) {
17037
17311
  input.id = label.htmlFor;
17038
17312
  input.name = name;
17039
17313
  input.className = textarea ? 'svhd-input svhd-description' : 'svhd-input';
17040
- wrapper.append(label, input);
17041
- return { wrapper, input };
17314
+ const error = document.createElement('p');
17315
+ error.id = `${input.id}-error`;
17316
+ error.className = 'svhd-field-error';
17317
+ error.hidden = true;
17318
+ wrapper.append(label, input, error);
17319
+ const created = { wrapper, input, error };
17320
+ input.addEventListener('input', () => clearFieldError(created));
17321
+ return created;
17042
17322
  }
17043
17323
  const subject = field('Subject', 'subject');
17044
17324
  const description = field('Description', 'description', true);
17045
17325
  const email = field('Email address', 'email');
17046
17326
  const code = field('Email code', 'code');
17047
17327
  const name = field('Your name', 'display_name');
17328
+ const fields = [subject, description, email, code, name];
17048
17329
  subject.input.maxLength = 200;
17049
17330
  subject.input.value = options.subject ?? '';
17050
17331
  description.input.value = options.body ?? '';
17051
17332
  email.input.type = 'email';
17052
17333
  email.input.autocomplete = 'email';
17053
17334
  code.input.inputMode = 'numeric';
17054
- code.input.maxLength = 6;
17055
17335
  code.input.autocomplete = 'one-time-code';
17056
17336
  name.input.autocomplete = 'name';
17337
+ const codeHint = document.createElement('p');
17338
+ codeHint.id = `${code.input.id}-hint`;
17339
+ codeHint.className = 'svhd-file-hint';
17340
+ codeHint.textContent = `It works for ${HELP_DESK_PIN_TTL_MINUTES} minutes. Not in your inbox? Look in spam for mail from ${helpDeskCodeSender(client.desk.sending_host)}.`;
17341
+ code.input.setAttribute('aria-describedby', codeHint.id);
17342
+ const resend = document.createElement('button');
17343
+ resend.type = 'button';
17344
+ resend.className = 'svhd-button svhd-secondary';
17345
+ resend.textContent = 'Send a new code';
17346
+ const changeEmail = document.createElement('button');
17347
+ changeEmail.type = 'button';
17348
+ changeEmail.className = 'svhd-button svhd-secondary';
17349
+ changeEmail.textContent = 'Use a different email';
17350
+ const codeLinks = document.createElement('div');
17351
+ codeLinks.className = 'svhd-code-links';
17352
+ codeLinks.append(resend, changeEmail);
17353
+ code.wrapper.append(codeHint, codeLinks);
17057
17354
  code.wrapper.hidden = true;
17058
17355
  name.wrapper.hidden = true;
17059
17356
  const auth = document.createElement('fieldset');
@@ -17071,14 +17368,9 @@ function mountHelpDeskForm(client, options) {
17071
17368
  hosted.target = '_blank';
17072
17369
  hosted.rel = 'noopener noreferrer';
17073
17370
  hosted.hidden = true;
17074
- const hostedUrl = hostedFormUrl(client.desk.canonical_host);
17075
- if (hostedUrl)
17076
- hosted.href = hostedUrl;
17077
- const changeEmail = document.createElement('button');
17078
- changeEmail.type = 'button';
17079
- changeEmail.className = 'svhd-button svhd-secondary';
17080
- changeEmail.textContent = 'Use another email';
17081
- changeEmail.hidden = true;
17371
+ const newRequestUrl = hostedUrl(client.desk.canonical_host, '/requests/new');
17372
+ if (newRequestUrl)
17373
+ hosted.href = newRequestUrl;
17082
17374
  const actions = document.createElement('div');
17083
17375
  actions.className = 'svhd-actions';
17084
17376
  const send = document.createElement('button');
@@ -17090,8 +17382,33 @@ function mountHelpDeskForm(client, options) {
17090
17382
  cancel.type = 'button';
17091
17383
  cancel.textContent = 'Cancel';
17092
17384
  cancel.hidden = !options.onCancel;
17093
- actions.append(send, changeEmail, cancel);
17094
- form.append(subject.wrapper, description.wrapper, auth, status, hosted, actions);
17385
+ actions.append(send, cancel);
17386
+ const confirm = document.createElement('div');
17387
+ confirm.className = 'svhd-confirm';
17388
+ confirm.setAttribute('role', 'group');
17389
+ confirm.hidden = true;
17390
+ const confirmTitle = document.createElement('p');
17391
+ confirmTitle.id = `${prefix}-confirm-title`;
17392
+ confirmTitle.className = 'svhd-confirm-title';
17393
+ confirmTitle.textContent = DISCARD_REQUEST_COPY.question;
17394
+ const confirmNote = document.createElement('p');
17395
+ confirmNote.id = `${prefix}-confirm-note`;
17396
+ confirmNote.className = 'svhd-status';
17397
+ confirm.setAttribute('aria-labelledby', confirmTitle.id);
17398
+ confirm.setAttribute('aria-describedby', confirmNote.id);
17399
+ const confirmActions = document.createElement('div');
17400
+ confirmActions.className = 'svhd-actions';
17401
+ const keepEditing = document.createElement('button');
17402
+ keepEditing.type = 'button';
17403
+ keepEditing.className = 'svhd-button';
17404
+ keepEditing.textContent = DISCARD_REQUEST_COPY.keepEditing;
17405
+ const discard = document.createElement('button');
17406
+ discard.type = 'button';
17407
+ discard.className = 'svhd-button svhd-secondary';
17408
+ discard.textContent = DISCARD_REQUEST_COPY.discard;
17409
+ confirmActions.append(keepEditing, discard);
17410
+ confirm.append(confirmTitle, confirmNote, confirmActions);
17411
+ form.append(subject.wrapper, description.wrapper, auth, status, hosted, actions, confirm);
17095
17412
  let state = 'editing';
17096
17413
  let destroyed = false;
17097
17414
  let pending = false;
@@ -17103,12 +17420,21 @@ function mountHelpDeskForm(client, options) {
17103
17420
  let focusAfter = null;
17104
17421
  let generation = 0;
17105
17422
  let challenge = null;
17423
+ // When the emailed code stops working, in milliseconds on this device's
17424
+ // clock from the start's landing: Core's `expires_at`, read against a device
17425
+ // clock set apart from Core's, would misjudge it.
17426
+ let challengeEndsAt = 0;
17106
17427
  let draftId = null;
17107
17428
  let draftPromise = null;
17108
17429
  let uncertainSubmission = null;
17109
17430
  let sessionToken = null;
17110
17431
  let sessionKnown = false;
17111
17432
  let attachments;
17433
+ let submittedCode = null;
17434
+ let resendAt = 0;
17435
+ let resendTimer = null;
17436
+ let confirmReturn = null;
17437
+ let done = null;
17112
17438
  function setState(next) {
17113
17439
  state = next;
17114
17440
  notify(options.onStateChange, next);
@@ -17119,13 +17445,163 @@ function mountHelpDeskForm(client, options) {
17119
17445
  status.classList.toggle('svhd-error', isError);
17120
17446
  status.setAttribute('role', isError ? 'alert' : 'status');
17121
17447
  }
17448
+ function renderResend() {
17449
+ const wait = Math.max(0, Math.ceil((resendAt - Date.now()) / 1000));
17450
+ resend.disabled = pending || completed || wait > 0;
17451
+ resend.textContent = wait > 0 ? `Send a new code in ${helpDeskCountdown(wait)}` : 'Send a new code';
17452
+ if (wait === 0)
17453
+ stopResendCountdown();
17454
+ }
17455
+ function stopResendCountdown() {
17456
+ if (resendTimer !== null)
17457
+ clearInterval(resendTimer);
17458
+ resendTimer = null;
17459
+ }
17460
+ function startResendCountdown() {
17461
+ stopResendCountdown();
17462
+ resendAt = Date.now() + HELP_DESK_PIN_RESEND_AFTER_SECONDS * 1000;
17463
+ resendTimer = setInterval(renderResend, 1000);
17464
+ renderResend();
17465
+ }
17122
17466
  function disable(disabled) {
17123
17467
  for (const input of [subject.input, description.input, email.input, code.input, name.input, send, cancel, changeEmail])
17124
17468
  input.disabled = disabled;
17469
+ renderResend();
17125
17470
  attachments?.disablePicking(disabled);
17126
17471
  attachments?.lock(completed || uncertainSubmission !== null);
17127
17472
  }
17473
+ function clearCode() {
17474
+ code.input.value = '';
17475
+ submittedCode = null;
17476
+ }
17477
+ /** Keep the challenge a start just answered, timing its code and the wait before another from now. */
17478
+ function holdChallenge(next) {
17479
+ challenge = next;
17480
+ challengeEndsAt = Date.now() + HELP_DESK_PIN_TTL_SECONDS * 1000;
17481
+ startResendCountdown();
17482
+ }
17483
+ /** Forget the emailed code's challenge: the code typed for it and the wait before another. */
17484
+ function dropChallenge() {
17485
+ challenge = null;
17486
+ challengeEndsAt = 0;
17487
+ clearCode();
17488
+ clearFieldError(code);
17489
+ stopResendCountdown();
17490
+ resendAt = 0;
17491
+ }
17492
+ function renderConfirmNote() {
17493
+ // Only the ticket write, in flight or unanswered, can have created the
17494
+ // request; the earlier steps (sign-in, files, the draft) create no ticket.
17495
+ confirmNote.textContent = uncertainSubmission !== null
17496
+ ? 'We may already have received this request. Closing now loses the way to check it.'
17497
+ : DISCARD_REQUEST_COPY.note;
17498
+ }
17499
+ /** The field of the step in view: the code, name or email the sign-in asks for, else the subject. */
17500
+ function stepField() {
17501
+ const asked = auth.hidden ? undefined : [code, name, email].find((entry) => !entry.wrapper.hidden);
17502
+ return (asked ?? subject).input;
17503
+ }
17504
+ function openConfirm() {
17505
+ // A press on the modal's backdrop leaves focus on the page body, and
17506
+ // Keep editing must never hand focus back outside the dialog.
17507
+ const active = document.activeElement;
17508
+ confirmReturn = active instanceof HTMLElement && shell.container.contains(active) ? active : stepField();
17509
+ renderConfirmNote();
17510
+ actions.hidden = true;
17511
+ confirm.hidden = false;
17512
+ keepEditing.focus();
17513
+ }
17514
+ /**
17515
+ * Move focus where a settled send or resend wants it. While the discard
17516
+ * question shows, focus stays on it and Keep editing goes there instead.
17517
+ */
17518
+ function focusSettled() {
17519
+ const target = focusAfter;
17520
+ focusAfter = null;
17521
+ if (confirm.hidden) {
17522
+ target?.focus();
17523
+ return;
17524
+ }
17525
+ renderConfirmNote();
17526
+ if (target)
17527
+ confirmReturn = target;
17528
+ }
17529
+ function closeConfirm(restoreFocus) {
17530
+ if (confirm.hidden)
17531
+ return;
17532
+ confirm.hidden = true;
17533
+ actions.hidden = false;
17534
+ const target = confirmReturn;
17535
+ confirmReturn = null;
17536
+ if (restoreFocus && target?.isConnected)
17537
+ target.focus();
17538
+ }
17539
+ /**
17540
+ * Whether closing now would lose something: a send under way or
17541
+ * unanswered, a code step, or text and files of the customer's own. The
17542
+ * silent company sign-in the form runs as it opens (`authenticating`) is
17543
+ * no work of the customer's.
17544
+ */
17545
+ function holdsUnsentWork() {
17546
+ if (completed)
17547
+ return false;
17548
+ return (pending && state !== 'authenticating') || uncertainSubmission !== null || challenge !== null
17549
+ || edited(subject.input, options.subject) || edited(description.input, options.body)
17550
+ || attachments?.hasEntries === true;
17551
+ }
17552
+ function showDone(ticket, customer) {
17553
+ const section = document.createElement('section');
17554
+ section.className = 'svhd-done';
17555
+ const title = document.createElement('h3');
17556
+ title.id = `${prefix}-done-title`;
17557
+ title.className = 'svhd-done-title';
17558
+ title.tabIndex = -1;
17559
+ title.textContent = `Request #${ticket.number} sent`;
17560
+ section.setAttribute('aria-labelledby', title.id);
17561
+ section.append(title);
17562
+ const requestUrl = Number.isSafeInteger(ticket.number) && ticket.number > 0
17563
+ ? hostedUrl(client.desk.canonical_host, `/requests/${ticket.number}`)
17564
+ : null;
17565
+ // GET /me carries email_updates only for a customer whose own mailbox
17566
+ // gets the desk's mail, so the form promises email only on that answer.
17567
+ const next = [
17568
+ ...(customer.email_updates === true && customer.email_verified !== false ? [`Replies will be emailed to ${customer.email}.`] : []),
17569
+ ...(requestUrl ? ['Follow it and read replies on your request page.'] : []),
17570
+ ];
17571
+ if (next.length > 0) {
17572
+ const note = document.createElement('p');
17573
+ note.className = 'svhd-status';
17574
+ note.textContent = next.join(' ');
17575
+ section.append(note);
17576
+ }
17577
+ if (requestUrl) {
17578
+ const link = document.createElement('a');
17579
+ link.className = 'svhd-link';
17580
+ link.href = requestUrl;
17581
+ link.target = '_blank';
17582
+ link.rel = 'noopener noreferrer';
17583
+ link.textContent = 'Open your request in a new tab';
17584
+ section.append(link);
17585
+ }
17586
+ if (options.onCancel) {
17587
+ const close = document.createElement('button');
17588
+ close.type = 'button';
17589
+ close.className = 'svhd-button svhd-secondary';
17590
+ close.textContent = 'Close';
17591
+ close.addEventListener('click', dismiss);
17592
+ section.append(close);
17593
+ }
17594
+ closeConfirm(false);
17595
+ form.hidden = true;
17596
+ form.after(section);
17597
+ done = section;
17598
+ title.focus();
17599
+ }
17128
17600
  function clearComposer() {
17601
+ done?.remove();
17602
+ done = null;
17603
+ form.hidden = false;
17604
+ closeConfirm(false);
17129
17605
  attachments?.clear(completed || uncertainSubmission !== null);
17130
17606
  attachments?.lock(false);
17131
17607
  if (attachments)
@@ -17134,14 +17610,14 @@ function mountHelpDeskForm(client, options) {
17134
17610
  description.input.value = '';
17135
17611
  email.input.value = '';
17136
17612
  email.input.readOnly = false;
17137
- changeEmail.hidden = true;
17138
- code.input.value = '';
17613
+ dropChallenge();
17139
17614
  name.input.value = '';
17615
+ for (const entry of fields)
17616
+ clearFieldError(entry);
17140
17617
  code.wrapper.hidden = true;
17141
17618
  name.wrapper.hidden = true;
17142
17619
  auth.hidden = true;
17143
17620
  hosted.hidden = true;
17144
- challenge = null;
17145
17621
  draftId = null;
17146
17622
  draftPromise = null;
17147
17623
  uncertainSubmission = null;
@@ -17184,7 +17660,7 @@ function mountHelpDeskForm(client, options) {
17184
17660
  description.input.value = '';
17185
17661
  name.input.value = '';
17186
17662
  email.input.value = '';
17187
- code.input.value = '';
17663
+ clearCode();
17188
17664
  if (attachments)
17189
17665
  attachments.element.hidden = true;
17190
17666
  return;
@@ -17253,9 +17729,8 @@ function mountHelpDeskForm(client, options) {
17253
17729
  email.wrapper.hidden = false;
17254
17730
  code.wrapper.hidden = challenge === null;
17255
17731
  email.input.readOnly = challenge !== null;
17256
- changeEmail.hidden = challenge === null;
17257
17732
  send.textContent = challenge ? 'Verify and send request' : 'Email me a code';
17258
- message(challenge ? 'Enter the six-digit code from your email to send this request.' : 'Verify your email to send this request.');
17733
+ message(challenge ? `We sent a six-digit code to ${email.input.value.trim()}.` : 'Verify your email to send this request.');
17259
17734
  focusAfter = challenge ? code.input : email.input;
17260
17735
  }
17261
17736
  function showHosted() {
@@ -17270,7 +17745,7 @@ function mountHelpDeskForm(client, options) {
17270
17745
  : 'Sign in to your account on this website to contact support.');
17271
17746
  return;
17272
17747
  }
17273
- hosted.hidden = hostedUrl === null;
17748
+ hosted.hidden = newRequestUrl === null;
17274
17749
  message(client.desk.auth_mode === 'identity_unavailable'
17275
17750
  ? 'This help desk cannot sign you in right now. Try again later.'
17276
17751
  : 'This help desk requires sign-in. Open it in a new tab to sign in and submit your request there.');
@@ -17278,6 +17753,11 @@ function mountHelpDeskForm(client, options) {
17278
17753
  async function submit() {
17279
17754
  if (pending || completed || destroyed)
17280
17755
  return;
17756
+ // Sending (Enter in a field, a code's last digit) answers the discard
17757
+ // question: the customer kept going.
17758
+ closeConfirm(false);
17759
+ for (const entry of fields)
17760
+ clearFieldError(entry);
17281
17761
  const recoveringSubmission = uncertainSubmission !== null;
17282
17762
  const subjectText = uncertainSubmission?.subject ?? subject.input.value.trim();
17283
17763
  const body = uncertainSubmission?.body ?? description.input.value;
@@ -17288,9 +17768,9 @@ function mountHelpDeskForm(client, options) {
17288
17768
  description.input.value = body;
17289
17769
  }
17290
17770
  if (!subjectText || subjectText.length > 200 || !body.trim()) {
17291
- const failure = new ValidationError(!subjectText ? 'Add a subject.' : subjectText.length > 200 ? 'Keep the subject to 200 characters.' : 'Describe what you need help with.');
17292
- message(failure.message, true);
17293
- (!subjectText || subjectText.length > 200 ? subject.input : description.input).focus();
17771
+ const refused = !subjectText || subjectText.length > 200 ? subject : description;
17772
+ setFieldError(refused, !subjectText ? 'Add a subject.' : subjectText.length > 200 ? 'Keep the subject to 200 characters.' : 'Describe what you need help with.');
17773
+ refused.input.focus();
17294
17774
  return;
17295
17775
  }
17296
17776
  const ownGeneration = generation;
@@ -17339,26 +17819,29 @@ function mountHelpDeskForm(client, options) {
17339
17819
  showEmail();
17340
17820
  return;
17341
17821
  }
17342
- challenge = await client.startPin(email.input.value.trim());
17822
+ const started = await client.startPin(email.input.value.trim());
17343
17823
  if (!alive(ownGeneration))
17344
17824
  return;
17825
+ holdChallenge(started);
17345
17826
  setState('verifying');
17346
17827
  showEmail();
17347
17828
  return;
17348
17829
  }
17349
- if (Date.now() >= challenge.expires_at * 1000) {
17350
- challenge = null;
17351
- code.input.value = '';
17830
+ if (Date.now() >= challengeEndsAt) {
17831
+ dropChallenge();
17352
17832
  setState('editing');
17353
17833
  showEmail();
17354
17834
  message('That code expired. Ask for another code.', true);
17355
17835
  return;
17356
17836
  }
17357
- if (!/^[0-9]{6}$/.test(code.input.value)) {
17358
- throw new ValidationError('Enter the six-digit code from your email.');
17837
+ const pin = helpDeskPinDigits(code.input.value);
17838
+ if (pin.length !== HELP_DESK_PIN_LENGTH) {
17839
+ setFieldError(code, 'Enter the six-digit code from your email.');
17840
+ focusAfter = code.input;
17841
+ return;
17359
17842
  }
17360
17843
  authenticating = true;
17361
- const verified = await client.verifyPin({ challengeId: challenge.challenge_id, bindingSecret: challenge.binding_secret, code: code.input.value });
17844
+ const verified = await client.verifyPin({ challengeId: challenge.challenge_id, bindingSecret: challenge.binding_secret, code: pin });
17362
17845
  if (!alive(ownGeneration))
17363
17846
  return;
17364
17847
  token = await client.getSessionToken();
@@ -17372,8 +17855,7 @@ function mountHelpDeskForm(client, options) {
17372
17855
  authenticating = false;
17373
17856
  if (attachments)
17374
17857
  attachments.element.hidden = false;
17375
- challenge = null;
17376
- changeEmail.hidden = true;
17858
+ dropChallenge();
17377
17859
  subject.input.value = subjectText;
17378
17860
  description.input.value = body;
17379
17861
  }
@@ -17393,7 +17875,7 @@ function mountHelpDeskForm(client, options) {
17393
17875
  name.wrapper.hidden = false;
17394
17876
  send.textContent = 'Send request';
17395
17877
  setState('editing');
17396
- message('Add your name before sending your request.');
17878
+ setFieldError(name, 'Add your name before sending your request.');
17397
17879
  focusAfter = name.input;
17398
17880
  return;
17399
17881
  }
@@ -17430,8 +17912,7 @@ function mountHelpDeskForm(client, options) {
17430
17912
  setState('success');
17431
17913
  if (!alive(ownGeneration))
17432
17914
  return;
17433
- message(`Request #${ticket.number} was sent.`);
17434
- send.textContent = 'Request sent';
17915
+ showDone(ticket, customer);
17435
17916
  notify(options.onSuccess, ticket);
17436
17917
  }
17437
17918
  catch (failure) {
@@ -17461,7 +17942,14 @@ function mountHelpDeskForm(client, options) {
17461
17942
  draftPromise = null;
17462
17943
  attachments?.clear();
17463
17944
  }
17464
- message(draftExpired ? 'Your saved draft expired. Your text is kept. Choose your files again before sending.' : error.message, true);
17945
+ if (error instanceof SparkVaultError && error.serviceCode === 'PIN_INVALID') {
17946
+ clearCode();
17947
+ setFieldError(code, HELP_DESK_CODE_STEP_COPY.codeRefused);
17948
+ focusAfter = code.input;
17949
+ }
17950
+ else {
17951
+ message(draftExpired ? 'Your saved draft expired. Your text is kept. Choose your files again before sending.' : error.message, true);
17952
+ }
17465
17953
  }
17466
17954
  setState(uncertainSubmission ? 'uncertain' : challenge ? 'verifying' : 'editing');
17467
17955
  if (!alive(ownGeneration))
@@ -17474,21 +17962,73 @@ function mountHelpDeskForm(client, options) {
17474
17962
  authenticating = false;
17475
17963
  pending = false;
17476
17964
  disable(completed);
17477
- focusAfter?.focus();
17478
- focusAfter = null;
17965
+ focusSettled();
17966
+ }
17967
+ }
17968
+ }
17969
+ /** Send a new code to the address the current one went to, replacing the challenge it answers. */
17970
+ async function resendCode() {
17971
+ if (pending || destroyed || challenge === null)
17972
+ return;
17973
+ closeConfirm(false);
17974
+ const ownGeneration = generation;
17975
+ pending = true;
17976
+ disable(true);
17977
+ clearFieldError(code);
17978
+ try {
17979
+ const next = await client.startPin(email.input.value.trim());
17980
+ if (!alive(ownGeneration))
17981
+ return;
17982
+ holdChallenge(next);
17983
+ clearCode();
17984
+ message(HELP_DESK_CODE_STEP_COPY.newCodeSent);
17985
+ focusAfter = code.input;
17986
+ }
17987
+ catch (failure) {
17988
+ if (!alive(ownGeneration))
17989
+ return;
17990
+ const error = failure instanceof Error ? failure : new Error('A new code could not be sent. Try again.');
17991
+ message(error instanceof SparkVaultError && error.statusCode === 429 ? 'Too many codes have been sent. Try again later.' : error.message, true);
17992
+ notify(options.onError, error);
17993
+ }
17994
+ finally {
17995
+ if (alive(ownGeneration)) {
17996
+ pending = false;
17997
+ disable(completed);
17998
+ focusSettled();
17479
17999
  }
17480
18000
  }
17481
18001
  }
18002
+ /** Take a typed, pasted or autofilled code as its digits, and send it the moment the last one lands. */
18003
+ function takeCode(value) {
18004
+ const digits = helpDeskPinDigits(value);
18005
+ if (code.input.value !== digits)
18006
+ code.input.value = digits;
18007
+ if (digits.length < HELP_DESK_PIN_LENGTH) {
18008
+ submittedCode = null;
18009
+ return;
18010
+ }
18011
+ if (pending || challenge === null || digits === submittedCode)
18012
+ return;
18013
+ submittedCode = digits;
18014
+ void submit();
18015
+ }
17482
18016
  const onSubmit = (event) => {
17483
18017
  event.preventDefault();
17484
18018
  void submit();
17485
18019
  };
17486
- const handle = {
18020
+ /** The Cancel path: remove the form, then tell the host the customer closed it. */
18021
+ function dismiss() {
18022
+ controller.destroy();
18023
+ notify(options.onCancel, undefined);
18024
+ }
18025
+ const controller = {
17487
18026
  destroy() {
17488
18027
  if (destroyed)
17489
18028
  return;
17490
18029
  destroyed = true;
17491
18030
  generation += 1;
18031
+ stopResendCountdown();
17492
18032
  attachments?.destroy(completed || uncertainSubmission !== null);
17493
18033
  unsubscribe();
17494
18034
  form.removeEventListener('submit', onSubmit);
@@ -17505,20 +18045,38 @@ function mountHelpDeskForm(client, options) {
17505
18045
  setState('editing');
17506
18046
  },
17507
18047
  getState: () => state,
18048
+ requestDismiss() {
18049
+ if (destroyed)
18050
+ return;
18051
+ if (!holdsUnsentWork())
18052
+ dismiss();
18053
+ else if (confirm.hidden)
18054
+ openConfirm();
18055
+ else
18056
+ closeConfirm(true);
18057
+ },
17508
18058
  };
18059
+ code.input.addEventListener('input', () => takeCode(code.input.value));
18060
+ code.input.addEventListener('paste', (event) => {
18061
+ const pasted = event.clipboardData?.getData('text');
18062
+ if (typeof pasted !== 'string')
18063
+ return;
18064
+ event.preventDefault();
18065
+ clearFieldError(code);
18066
+ takeCode(pasted);
18067
+ });
18068
+ resend.addEventListener('click', () => { void resendCode(); });
17509
18069
  changeEmail.addEventListener('click', () => {
17510
18070
  if (pending || destroyed)
17511
18071
  return;
17512
- challenge = null;
17513
- code.input.value = '';
18072
+ dropChallenge();
17514
18073
  setState('editing');
17515
18074
  showEmail();
17516
18075
  email.input.focus();
17517
18076
  });
17518
- cancel.addEventListener('click', () => {
17519
- handle.destroy();
17520
- notify(options.onCancel, undefined);
17521
- });
18077
+ cancel.addEventListener('click', () => controller.requestDismiss());
18078
+ keepEditing.addEventListener('click', () => closeConfirm(true));
18079
+ discard.addEventListener('click', dismiss);
17522
18080
  form.addEventListener('submit', onSubmit);
17523
18081
  notify(options.onStateChange, state);
17524
18082
  // Native company identity starts as the form opens, never from a Connect,
@@ -17561,7 +18119,7 @@ function mountHelpDeskForm(client, options) {
17561
18119
  }
17562
18120
  });
17563
18121
  }
17564
- return handle;
18122
+ return controller;
17565
18123
  }
17566
18124
 
17567
18125
  let modalSequence = 0;
@@ -17602,6 +18160,18 @@ function openHelpDeskForm(client, options = {}) {
17602
18160
  // A host callback must not leave the modal or scroll lock behind.
17603
18161
  }
17604
18162
  };
18163
+ // Escape, the backdrop and Close ask the form, which closes at once when
18164
+ // nothing is unsent and otherwise asks to discard first; its Cancel path
18165
+ // then reaches `cancel`. A state observer can dismiss while the form mounts,
18166
+ // before it hands back its controller, when nothing has been typed yet.
18167
+ const requestDismiss = () => {
18168
+ if (destroyed)
18169
+ return;
18170
+ if (inner)
18171
+ inner.requestDismiss();
18172
+ else
18173
+ cancel();
18174
+ };
17605
18175
  const prefix = `svhd-modal-${++modalSequence}`;
17606
18176
  const created = new ModalShell({
17607
18177
  overlayId: `${prefix}-overlay`,
@@ -17609,7 +18179,7 @@ function openHelpDeskForm(client, options = {}) {
17609
18179
  classPrefix: 'svhd',
17610
18180
  titleId: `${prefix}-title`,
17611
18181
  backdropBlur: options.backdropBlur !== false,
17612
- onDismiss: cancel,
18182
+ onDismiss: requestDismiss,
17613
18183
  });
17614
18184
  shell = created;
17615
18185
  // A host focus listener can dispatch a dismissal while the shell opens.
@@ -17629,7 +18199,7 @@ function openHelpDeskForm(client, options = {}) {
17629
18199
  close.className = 'svhd-modal-close';
17630
18200
  close.setAttribute('aria-label', 'Close support form');
17631
18201
  close.textContent = 'Close';
17632
- close.addEventListener('click', cancel);
18202
+ close.addEventListener('click', requestDismiss);
17633
18203
  header.append(title, close);
17634
18204
  const body = document.createElement('div');
17635
18205
  shell.modal.append(header, body);
@@ -21521,7 +22091,7 @@ class SparkVault {
21521
22091
  }
21522
22092
  /** Get the SDK version */
21523
22093
  static get version() {
21524
- return '14.2.2';
22094
+ return '14.2.3';
21525
22095
  }
21526
22096
  }
21527
22097
  if (typeof window !== 'undefined') {