@bun-native/protocol 0.33.1 → 0.33.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/src/messages.ts CHANGED
@@ -18,7 +18,7 @@ import {
18
18
  type MessageDialogOptions,
19
19
  type MessageDialogResult,
20
20
  } from './message-dialogs.ts'
21
- import { MAX_FRAME_BYTES, ProtocolError } from './framing.ts'
21
+ import { ProtocolError } from './framing.ts'
22
22
  import {
23
23
  parseDisplaySnapshot,
24
24
  parseClipboardText,
@@ -27,10 +27,6 @@ import {
27
27
  parseShortcutModifiers,
28
28
  parseNotificationContent,
29
29
  parseNotificationDismissal,
30
- MAX_CLIPBOARD_BYTES,
31
- MAX_GLOBAL_SHORTCUTS,
32
- MAX_NOTIFICATIONS,
33
- MAX_SERVICE_REQUESTS,
34
30
  type ClipboardText,
35
31
  type DisplaySnapshot,
36
32
  type NotificationDismissal,
@@ -38,8 +34,6 @@ import {
38
34
  type ShortcutModifiers,
39
35
  } from './services.ts'
40
36
  import {
41
- MAX_FILE_CHUNK_BYTES,
42
- MAX_FILE_GRANTS,
43
37
  parseFileAccess,
44
38
  parseFileOperation,
45
39
  parseFilePath,
@@ -50,7 +44,6 @@ import {
50
44
  type FileResult,
51
45
  } from './files.ts'
52
46
  import {
53
- MAX_PROCESSES,
54
47
  parseProcessExit,
55
48
  parseProcessLaunch,
56
49
  parseProcessOutput,
@@ -64,8 +57,6 @@ import {
64
57
  type TerminalSize,
65
58
  } from './processes.ts'
66
59
  import {
67
- MAX_DROP_ITEMS,
68
- MAX_DROP_TARGETS,
69
60
  parseDragState,
70
61
  parseDropItemIndex,
71
62
  parseDroppedItems,
@@ -74,63 +65,36 @@ import {
74
65
  type DroppedItem,
75
66
  type DropRejection,
76
67
  } from './drops.ts'
68
+ import { id, integer, keys, number, object, string } from './values.ts'
77
69
  import {
78
- fields,
79
- id,
80
- integer,
81
- keys,
82
- MAX_TEXT_BYTES,
70
+ CAPABILITIES,
71
+ COMMAND_KEYS,
72
+ DEFAULT_LIMITS,
73
+ DISMISS_REASONS,
74
+ ERROR_CODES,
75
+ EVENT_KEYS,
76
+ MAX_CLIPBOARD_BYTES,
77
+ MAX_EVENT_PATH_NODES,
78
+ MAX_KEY_BYTES,
79
+ MAX_NODES_PER_WINDOW,
80
+ MAX_OPERATIONS,
81
+ MAX_POINTER_COORDINATE,
82
+ MAX_SCROLL_OFFSET,
83
83
  MAX_TITLE_BYTES,
84
- number,
85
- object,
86
- string,
87
- } from './values.ts'
88
-
89
- export const PROTOCOL_VERSION = 12
90
- /** Four operations per node of a full window: a whole-window replacement fits one batch. */
91
- export const MAX_OPERATIONS = 16_384
92
- const MAX_NODES_PER_WINDOW = 4_096
93
- const MAX_WINDOWS = 16
94
- const MAX_DEPTH = 64
95
- const MAX_TEXT_BYTES_PER_WINDOW = 4_194_304
96
-
97
- export interface Limits {
98
- max_frame_bytes: number
99
- max_operations: number
100
- max_nodes_per_window: number
101
- max_windows: number
102
- max_depth: number
103
- max_text_bytes: number
104
- max_text_bytes_per_window: number
105
- max_clipboard_bytes: number
106
- max_service_requests: number
107
- max_global_shortcuts: number
108
- max_notifications: number
109
- max_file_grants: number
110
- max_file_chunk_bytes: number
111
- max_processes: number
112
- max_drop_targets: number
113
- max_drop_items: number
114
- }
84
+ MODIFIER_KEYS,
85
+ OPERATION_KEYS,
86
+ POINTER_BUTTONS,
87
+ PROTOCOL_VERSION,
88
+ RESIZE_SOURCES,
89
+ type Capability,
90
+ type DismissReason,
91
+ type ErrorCode,
92
+ type Limits,
93
+ type PointerButton,
94
+ type ResizeSource,
95
+ } from './wire-schema.ts'
115
96
 
116
- export const DEFAULT_LIMITS: Readonly<Limits> = Object.freeze({
117
- max_frame_bytes: MAX_FRAME_BYTES,
118
- max_operations: MAX_OPERATIONS,
119
- max_nodes_per_window: MAX_NODES_PER_WINDOW,
120
- max_windows: MAX_WINDOWS,
121
- max_depth: MAX_DEPTH,
122
- max_text_bytes: MAX_TEXT_BYTES,
123
- max_text_bytes_per_window: MAX_TEXT_BYTES_PER_WINDOW,
124
- max_clipboard_bytes: MAX_CLIPBOARD_BYTES,
125
- max_service_requests: MAX_SERVICE_REQUESTS,
126
- max_global_shortcuts: MAX_GLOBAL_SHORTCUTS,
127
- max_notifications: MAX_NOTIFICATIONS,
128
- max_file_grants: MAX_FILE_GRANTS,
129
- max_file_chunk_bytes: MAX_FILE_CHUNK_BYTES,
130
- max_processes: MAX_PROCESSES,
131
- max_drop_targets: MAX_DROP_TARGETS,
132
- max_drop_items: MAX_DROP_ITEMS,
133
- })
97
+ export type { Capability, DismissReason, ErrorCode, Limits, PointerButton, ResizeSource }
134
98
 
135
99
  export type Operation =
136
100
  | { type: 'create'; node_id: number; element: Element }
@@ -154,9 +118,6 @@ export type Operation =
154
118
  /** Requires `scroll_control_v1`. */
155
119
  | { type: 'scroll_into_view'; node_id: number }
156
120
 
157
- /** Largest programmatic scroll offset, in logical pixels. */
158
- export const MAX_SCROLL_OFFSET = 1_000_000
159
-
160
121
  export type Command =
161
122
  | { id: number; type: 'hello'; protocol: typeof PROTOCOL_VERSION }
162
123
  | {
@@ -318,65 +279,6 @@ export type Command =
318
279
  | { id: number; type: 'set_app_menu'; menu_id: number; menus: AppMenu[] }
319
280
  | { id: number; type: 'shutdown' }
320
281
 
321
- export const ERROR_CODES = [
322
- 'invalid_command',
323
- 'version_mismatch',
324
- 'invalid_state',
325
- 'invalid_handle',
326
- 'stale_revision',
327
- 'limit_exceeded',
328
- 'native_failure',
329
- 'permission_denied',
330
- 'service_unavailable',
331
- 'cancelled',
332
- 'operation_timed_out',
333
- 'payload_too_large',
334
- 'conflict',
335
- 'not_found',
336
- 'already_exists',
337
- 'path_denied',
338
- 'wrong_kind',
339
- 'not_empty',
340
- 'access_denied',
341
- ] as const
342
- export type ErrorCode = (typeof ERROR_CODES)[number]
343
- export const REQUIRED_CAPABILITIES = Object.freeze([
344
- 'tree',
345
- 'input_revisions',
346
- 'layout_v1',
347
- 'interactions_v1',
348
- 'window_display_v1',
349
- ] as const)
350
- const CAPABILITIES = [
351
- ...REQUIRED_CAPABILITIES,
352
- 'clipboard_text_v1',
353
- 'global_shortcuts_v1',
354
- 'notifications_v1',
355
- 'filesystem_grants_v1',
356
- 'processes_v1',
357
- 'split_v1',
358
- 'style_decoration_v1',
359
- 'display_details_v1',
360
- 'display_changes_v1',
361
- 'file_dialogs_v1',
362
- 'overlays_v1',
363
- 'scroll_anchor_v1',
364
- 'focus_v1',
365
- 'scroll_control_v1',
366
- 'overlay_keyboard_v1',
367
- 'popup_windows_v1',
368
- 'drag_drop_files_v1',
369
- 'input_states_v1',
370
- 'app_menu_v1',
371
- 'task_dialogs_v1',
372
- 'context_menus_v1',
373
- 'split_resize_v1',
374
- 'accessibility_v1',
375
- 'accessibility_lists_v1',
376
- 'display_identity_v1',
377
- ] as const
378
- export type Capability = (typeof CAPABILITIES)[number]
379
-
380
282
  export interface KeyModifiers {
381
283
  control: boolean
382
284
  alt: boolean
@@ -385,12 +287,6 @@ export interface KeyModifiers {
385
287
  function: boolean
386
288
  }
387
289
 
388
- export type PointerButton = 'left' | 'right' | 'middle' | 'back' | 'forward'
389
- /** Why the host asks the application to close a popup window overlay. */
390
- export type DismissReason = 'window_blur'
391
- /** What moved a split divider (`split_resize_v1`). */
392
- export type ResizeSource = 'pointer' | 'keyboard'
393
-
394
290
  interface NodeEventRoute {
395
291
  sequence: number
396
292
  window_id: number
@@ -628,111 +524,172 @@ function version(value: unknown): typeof PROTOCOL_VERSION {
628
524
  return PROTOCOL_VERSION
629
525
  }
630
526
 
631
- /** Property names of a window-scoped command, reply or unsolicited event. */
632
- const windowCommand = (...names: string[]) => fields('id', 'type', 'window_id', ...names)
633
- const windowReply = (...names: string[]) => fields('type', 'id', 'window_id', ...names)
634
- const windowNotice = (...names: string[]) => fields('type', 'sequence', 'window_id', ...names)
527
+ function createOperation(nodeId: unknown, element: unknown): Operation {
528
+ return { type: 'create', node_id: id(nodeId), element: parseElement(element) }
529
+ }
530
+
531
+ function setOperation(
532
+ nodeId: unknown,
533
+ value: unknown,
534
+ revised: boolean,
535
+ revision: unknown
536
+ ): Operation {
537
+ const node_id = id(nodeId)
538
+ const element = parseElement(value)
539
+ if (element.kind === 'input')
540
+ return {
541
+ type: 'set',
542
+ node_id,
543
+ element,
544
+ expected_input_revision: integer(revision, 'input revision'),
545
+ }
546
+ if (revised) throw new ProtocolError('Only input elements accept an input revision')
547
+ return { type: 'set', node_id, element }
548
+ }
549
+
550
+ function insertOperation(parentId: unknown, childId: unknown, index: unknown): Operation {
551
+ return {
552
+ type: 'insert',
553
+ parent_id: id(parentId),
554
+ child_id: id(childId),
555
+ index: integer(index, 'index', 0, MAX_NODES_PER_WINDOW),
556
+ }
557
+ }
558
+
559
+ function scrollOperation(
560
+ nodeId: unknown,
561
+ offset: { readonly x?: unknown; readonly y?: unknown }
562
+ ): Operation {
563
+ if (!('x' in offset) && !('y' in offset)) throw new ProtocolError('scroll_to requires x or y')
564
+ const axis = (name: 'x' | 'y') =>
565
+ name in offset ? { [name]: number(offset[name], 'scroll ' + name, 0, MAX_SCROLL_OFFSET) } : {}
566
+ return { type: 'scroll_to', node_id: id(nodeId), ...axis('x'), ...axis('y') }
567
+ }
568
+
569
+ /** The operations of an `OperationList`, copied, or `undefined` for any other value. */
570
+ let listed: (value: unknown) => Operation[] | undefined
571
+
572
+ /**
573
+ * One batch's operations, built by checked constructors for a trusted builder
574
+ * such as core's transaction. Each constructor checks its fields as
575
+ * `parseCommand` would and freezes the operation, and the list keeps them
576
+ * where no caller can replace them, so `parseCommand` accepts a list as its
577
+ * batch operations without parsing each one again. A plain operations array,
578
+ * including one read from a list, is parsed in full.
579
+ */
580
+ export class OperationList implements Iterable<Operation> {
581
+ #operations: Operation[] = []
582
+
583
+ static {
584
+ listed = (value) =>
585
+ value !== null && typeof value === 'object' && #operations in value
586
+ ? (value as OperationList).#operations.slice()
587
+ : undefined
588
+ }
589
+
590
+ /** One list of `lists`, in order, as one batch carries them. */
591
+ static join(lists: readonly OperationList[]): OperationList {
592
+ const joined = new OperationList()
593
+ joined.#operations = lists.flatMap((list) => list.#operations)
594
+ return joined
595
+ }
596
+
597
+ get length(): number {
598
+ return this.#operations.length
599
+ }
600
+
601
+ [Symbol.iterator](): Iterator<Operation> {
602
+ return this.#operations.values()
603
+ }
604
+
605
+ #push(operation: Operation): void {
606
+ this.#operations.push(Object.freeze(operation))
607
+ }
608
+
609
+ create(nodeId: number, element: Element): void {
610
+ this.#push(createOperation(nodeId, element))
611
+ }
612
+
613
+ set(nodeId: number, element: Element, expectedInputRevision?: number): void {
614
+ this.#push(
615
+ setOperation(nodeId, element, expectedInputRevision !== undefined, expectedInputRevision)
616
+ )
617
+ }
618
+
619
+ insert(parentId: number, childId: number, index: number): void {
620
+ this.#push(insertOperation(parentId, childId, index))
621
+ }
622
+
623
+ delete(nodeId: number): void {
624
+ this.#push({ type: 'delete', node_id: id(nodeId) })
625
+ }
626
+
627
+ focus(nodeId: number): void {
628
+ this.#push({ type: 'focus', node_id: id(nodeId) })
629
+ }
630
+
631
+ scrollTo(nodeId: number, offset: { readonly x?: number; readonly y?: number }): void {
632
+ this.#push(scrollOperation(nodeId, offset))
633
+ }
635
634
 
636
- const OPERATION_CREATE_KEYS = fields('type', 'node_id', 'element')
637
- const OPERATION_SET_KEYS = fields('type', 'node_id', 'element', 'expected_input_revision')
638
- const OPERATION_INSERT_KEYS = fields('type', 'parent_id', 'child_id', 'index')
639
- const OPERATION_DELETE_KEYS = fields('type', 'node_id')
640
- const OPERATION_SCROLL_TO_KEYS = fields('type', 'node_id', 'x', 'y')
635
+ scrollIntoView(nodeId: number): void {
636
+ this.#push({ type: 'scroll_into_view', node_id: id(nodeId) })
637
+ }
638
+ }
641
639
 
642
640
  function operation(value: unknown): Operation {
643
641
  const data = object(value)
644
- switch (data['type']) {
642
+ const type = data['type']
643
+ switch (type) {
645
644
  case 'create':
646
- keys(data, OPERATION_CREATE_KEYS)
647
- return {
648
- type: 'create',
649
- node_id: id(data['node_id']),
650
- element: parseElement(data['element']),
651
- }
652
- case 'set': {
653
- keys(data, OPERATION_SET_KEYS)
654
- const node_id = id(data['node_id'])
655
- const element = parseElement(data['element'])
656
- if (element.kind === 'input')
657
- return {
658
- type: 'set',
659
- node_id,
660
- element,
661
- expected_input_revision: integer(data['expected_input_revision'], 'input revision'),
662
- }
663
- if ('expected_input_revision' in data)
664
- throw new ProtocolError('Only input elements accept an input revision')
665
- return { type: 'set', node_id, element }
666
- }
645
+ keys(data, OPERATION_KEYS.create)
646
+ return createOperation(data['node_id'], data['element'])
647
+ case 'set':
648
+ keys(data, OPERATION_KEYS.set)
649
+ return setOperation(
650
+ data['node_id'],
651
+ data['element'],
652
+ 'expected_input_revision' in data,
653
+ data['expected_input_revision']
654
+ )
667
655
  case 'insert':
668
- keys(data, OPERATION_INSERT_KEYS)
669
- return {
670
- type: 'insert',
671
- parent_id: id(data['parent_id']),
672
- child_id: id(data['child_id']),
673
- index: integer(data['index'], 'index', 0, MAX_NODES_PER_WINDOW),
674
- }
656
+ keys(data, OPERATION_KEYS.insert)
657
+ return insertOperation(data['parent_id'], data['child_id'], data['index'])
675
658
  case 'delete':
676
659
  case 'focus':
677
660
  case 'scroll_into_view':
678
- keys(data, OPERATION_DELETE_KEYS)
679
- return { type: data['type'], node_id: id(data['node_id']) }
680
- case 'scroll_to': {
681
- keys(data, OPERATION_SCROLL_TO_KEYS)
682
- if (!('x' in data) && !('y' in data)) throw new ProtocolError('scroll_to requires x or y')
683
- const offset = (axis: 'x' | 'y') =>
684
- axis in data ? { [axis]: number(data[axis], 'scroll ' + axis, 0, MAX_SCROLL_OFFSET) } : {}
685
- return { type: 'scroll_to', node_id: id(data['node_id']), ...offset('x'), ...offset('y') }
686
- }
661
+ keys(
662
+ data,
663
+ type === 'delete'
664
+ ? OPERATION_KEYS.delete
665
+ : type === 'focus'
666
+ ? OPERATION_KEYS.focus
667
+ : OPERATION_KEYS.scroll_into_view
668
+ )
669
+ return { type, node_id: id(data['node_id']) }
670
+ case 'scroll_to':
671
+ keys(data, OPERATION_KEYS.scroll_to)
672
+ return scrollOperation(data['node_id'], data)
687
673
  default:
688
674
  throw new ProtocolError('Unknown native operation')
689
675
  }
690
676
  }
691
677
 
692
- const COMMAND_HELLO_KEYS = fields('id', 'type', 'protocol')
693
- const COMMAND_OPEN_KEYS = windowCommand('root_id', 'title', 'width', 'height')
694
- const COMMAND_BATCH_KEYS = windowCommand('base_revision', 'operations')
695
- const COMMAND_CLOSE_KEYS = windowCommand()
696
- const COMMAND_CLIPBOARD_WRITE_KEYS = windowCommand('text')
697
- const COMMAND_REGISTER_GLOBAL_SHORTCUT_KEYS = windowCommand('shortcut_id', 'key', 'modifiers')
698
- const COMMAND_UNREGISTER_GLOBAL_SHORTCUT_KEYS = windowCommand('shortcut_id')
699
- const COMMAND_POST_NOTIFICATION_KEYS = windowCommand('notification_id', 'title', 'body')
700
- const COMMAND_WITHDRAW_NOTIFICATION_KEYS = windowCommand('notification_id')
701
- const COMMAND_OPEN_FILE_GRANT_KEYS = windowCommand('grant_id', 'root', 'access')
702
- const COMMAND_CLOSE_FILE_GRANT_KEYS = windowCommand('grant_id')
703
- const COMMAND_FILE_REQUEST_KEYS = windowCommand('grant_id', 'path', 'operation')
704
- const COMMAND_SPAWN_PROCESS_KEYS = windowCommand('process_id', 'launch')
705
- const COMMAND_WRITE_PROCESS_KEYS = windowCommand('process_id', 'data')
706
- const COMMAND_CLOSE_PROCESS_INPUT_KEYS = windowCommand('process_id')
707
- const COMMAND_RESIZE_PROCESS_KEYS = windowCommand('process_id', 'size')
708
- const COMMAND_SIGNAL_PROCESS_KEYS = windowCommand('process_id', 'signal')
709
- const COMMAND_ACKNOWLEDGE_PROCESS_OUTPUT_KEYS = windowCommand('process_id', 'chunk')
710
- const COMMAND_SHOW_FILE_DIALOG_KEYS = windowCommand('dialog_id', 'options')
711
- const COMMAND_CLOSE_FILE_DIALOG_KEYS = windowCommand('dialog_id')
712
- const COMMAND_SHOW_MESSAGE_DIALOG_KEYS = windowCommand('dialog_id', 'options')
713
- const COMMAND_CLOSE_MESSAGE_DIALOG_KEYS = windowCommand('dialog_id')
714
- const COMMAND_SHOW_CONTEXT_MENU_KEYS = windowCommand('menu_id', 'x', 'y', 'items')
715
- const COMMAND_CLOSE_CONTEXT_MENU_KEYS = windowCommand('menu_id')
716
- const COMMAND_REGISTER_DROP_TARGET_KEYS = windowCommand('target_id', 'node_id')
717
- const COMMAND_UNREGISTER_DROP_TARGET_KEYS = windowCommand('target_id')
718
- const COMMAND_RESOLVE_DROPPED_ITEM_KEYS = windowCommand('target_id', 'drop_id', 'item', 'root')
719
- const COMMAND_SET_APP_MENU_KEYS = fields('id', 'type', 'menu_id', 'menus')
720
- const COMMAND_SHUTDOWN_KEYS = fields('id', 'type')
721
-
722
678
  /** Validate before crossing the native boundary, including JavaScript callers. */
723
679
  export function parseCommand(value: unknown): Command {
724
680
  const message = object(value)
725
681
  const request = id(message['id'])
726
- switch (message['type']) {
682
+ const type = message['type']
683
+ switch (type) {
727
684
  case 'hello':
728
- keys(message, COMMAND_HELLO_KEYS)
685
+ keys(message, COMMAND_KEYS.hello)
729
686
  return {
730
687
  id: request,
731
688
  type: 'hello',
732
689
  protocol: version(message['protocol']),
733
690
  }
734
691
  case 'open': {
735
- keys(message, COMMAND_OPEN_KEYS)
692
+ keys(message, COMMAND_KEYS.open)
736
693
  const title = string(message['title'], 'title', MAX_TITLE_BYTES)
737
694
  if (!title || /\p{Cc}/u.test(title))
738
695
  throw new ProtocolError('Window title must be nonempty and contain no control characters')
@@ -747,8 +704,10 @@ export function parseCommand(value: unknown): Command {
747
704
  }
748
705
  }
749
706
  case 'batch': {
750
- keys(message, COMMAND_BATCH_KEYS)
751
- const operations = message['operations']
707
+ keys(message, COMMAND_KEYS.batch)
708
+ // A list's operations were checked as it was built and cannot change since.
709
+ const built = listed(message['operations'])
710
+ const operations = built ?? message['operations']
752
711
  if (!Array.isArray(operations) || operations.length < 1 || operations.length > MAX_OPERATIONS)
753
712
  throw new ProtocolError(
754
713
  'Batch must contain between 1 and ' + MAX_OPERATIONS + ' operations'
@@ -758,20 +717,20 @@ export function parseCommand(value: unknown): Command {
758
717
  type: 'batch',
759
718
  window_id: id(message['window_id']),
760
719
  base_revision: integer(message['base_revision'], 'tree revision'),
761
- operations: operations.map(operation),
720
+ operations: built ?? operations.map(operation),
762
721
  }
763
722
  }
764
723
  case 'close':
765
724
  case 'display_snapshot':
766
725
  case 'clipboard_read':
767
- keys(message, COMMAND_CLOSE_KEYS)
726
+ keys(message, COMMAND_KEYS[type])
768
727
  return {
769
728
  id: request,
770
- type: message['type'],
729
+ type,
771
730
  window_id: id(message['window_id']),
772
731
  }
773
732
  case 'clipboard_write':
774
- keys(message, COMMAND_CLIPBOARD_WRITE_KEYS)
733
+ keys(message, COMMAND_KEYS.clipboard_write)
775
734
  return {
776
735
  id: request,
777
736
  type: 'clipboard_write',
@@ -779,7 +738,7 @@ export function parseCommand(value: unknown): Command {
779
738
  text: string(message['text'], 'clipboard text', MAX_CLIPBOARD_BYTES),
780
739
  }
781
740
  case 'register_global_shortcut':
782
- keys(message, COMMAND_REGISTER_GLOBAL_SHORTCUT_KEYS)
741
+ keys(message, COMMAND_KEYS.register_global_shortcut)
783
742
  return {
784
743
  id: request,
785
744
  type: 'register_global_shortcut',
@@ -789,7 +748,7 @@ export function parseCommand(value: unknown): Command {
789
748
  modifiers: parseShortcutModifiers(message['modifiers']),
790
749
  }
791
750
  case 'unregister_global_shortcut':
792
- keys(message, COMMAND_UNREGISTER_GLOBAL_SHORTCUT_KEYS)
751
+ keys(message, COMMAND_KEYS.unregister_global_shortcut)
793
752
  return {
794
753
  id: request,
795
754
  type: 'unregister_global_shortcut',
@@ -797,7 +756,7 @@ export function parseCommand(value: unknown): Command {
797
756
  shortcut_id: id(message['shortcut_id']),
798
757
  }
799
758
  case 'post_notification': {
800
- keys(message, COMMAND_POST_NOTIFICATION_KEYS)
759
+ keys(message, COMMAND_KEYS.post_notification)
801
760
  const { title, body } = parseNotificationContent(message['title'], message['body'])
802
761
  return {
803
762
  id: request,
@@ -809,7 +768,7 @@ export function parseCommand(value: unknown): Command {
809
768
  }
810
769
  }
811
770
  case 'withdraw_notification':
812
- keys(message, COMMAND_WITHDRAW_NOTIFICATION_KEYS)
771
+ keys(message, COMMAND_KEYS.withdraw_notification)
813
772
  return {
814
773
  id: request,
815
774
  type: 'withdraw_notification',
@@ -817,7 +776,7 @@ export function parseCommand(value: unknown): Command {
817
776
  notification_id: id(message['notification_id']),
818
777
  }
819
778
  case 'open_file_grant':
820
- keys(message, COMMAND_OPEN_FILE_GRANT_KEYS)
779
+ keys(message, COMMAND_KEYS.open_file_grant)
821
780
  return {
822
781
  id: request,
823
782
  type: 'open_file_grant',
@@ -827,7 +786,7 @@ export function parseCommand(value: unknown): Command {
827
786
  access: parseFileAccess(message['access']),
828
787
  }
829
788
  case 'close_file_grant':
830
- keys(message, COMMAND_CLOSE_FILE_GRANT_KEYS)
789
+ keys(message, COMMAND_KEYS.close_file_grant)
831
790
  return {
832
791
  id: request,
833
792
  type: 'close_file_grant',
@@ -835,7 +794,7 @@ export function parseCommand(value: unknown): Command {
835
794
  grant_id: id(message['grant_id']),
836
795
  }
837
796
  case 'file_request': {
838
- keys(message, COMMAND_FILE_REQUEST_KEYS)
797
+ keys(message, COMMAND_KEYS.file_request)
839
798
  const path = parseFilePath(message['path'])
840
799
  return {
841
800
  id: request,
@@ -847,7 +806,7 @@ export function parseCommand(value: unknown): Command {
847
806
  }
848
807
  }
849
808
  case 'spawn_process':
850
- keys(message, COMMAND_SPAWN_PROCESS_KEYS)
809
+ keys(message, COMMAND_KEYS.spawn_process)
851
810
  return {
852
811
  id: request,
853
812
  type: 'spawn_process',
@@ -856,7 +815,7 @@ export function parseCommand(value: unknown): Command {
856
815
  launch: parseProcessLaunch(message['launch']),
857
816
  }
858
817
  case 'write_process':
859
- keys(message, COMMAND_WRITE_PROCESS_KEYS)
818
+ keys(message, COMMAND_KEYS.write_process)
860
819
  return {
861
820
  id: request,
862
821
  type: 'write_process',
@@ -866,15 +825,15 @@ export function parseCommand(value: unknown): Command {
866
825
  }
867
826
  case 'close_process_input':
868
827
  case 'release_process':
869
- keys(message, COMMAND_CLOSE_PROCESS_INPUT_KEYS)
828
+ keys(message, COMMAND_KEYS[type])
870
829
  return {
871
830
  id: request,
872
- type: message['type'],
831
+ type,
873
832
  window_id: id(message['window_id']),
874
833
  process_id: id(message['process_id']),
875
834
  }
876
835
  case 'resize_process':
877
- keys(message, COMMAND_RESIZE_PROCESS_KEYS)
836
+ keys(message, COMMAND_KEYS.resize_process)
878
837
  return {
879
838
  id: request,
880
839
  type: 'resize_process',
@@ -883,7 +842,7 @@ export function parseCommand(value: unknown): Command {
883
842
  size: parseTerminalSize(message['size']),
884
843
  }
885
844
  case 'signal_process':
886
- keys(message, COMMAND_SIGNAL_PROCESS_KEYS)
845
+ keys(message, COMMAND_KEYS.signal_process)
887
846
  return {
888
847
  id: request,
889
848
  type: 'signal_process',
@@ -892,7 +851,7 @@ export function parseCommand(value: unknown): Command {
892
851
  signal: parseProcessSignal(message['signal']),
893
852
  }
894
853
  case 'acknowledge_process_output':
895
- keys(message, COMMAND_ACKNOWLEDGE_PROCESS_OUTPUT_KEYS)
854
+ keys(message, COMMAND_KEYS.acknowledge_process_output)
896
855
  return {
897
856
  id: request,
898
857
  type: 'acknowledge_process_output',
@@ -901,7 +860,7 @@ export function parseCommand(value: unknown): Command {
901
860
  chunk: id(message['chunk']),
902
861
  }
903
862
  case 'show_file_dialog':
904
- keys(message, COMMAND_SHOW_FILE_DIALOG_KEYS)
863
+ keys(message, COMMAND_KEYS.show_file_dialog)
905
864
  return {
906
865
  id: request,
907
866
  type: 'show_file_dialog',
@@ -910,7 +869,7 @@ export function parseCommand(value: unknown): Command {
910
869
  options: parseFileDialogOptions(message['options']),
911
870
  }
912
871
  case 'close_file_dialog':
913
- keys(message, COMMAND_CLOSE_FILE_DIALOG_KEYS)
872
+ keys(message, COMMAND_KEYS.close_file_dialog)
914
873
  return {
915
874
  id: request,
916
875
  type: 'close_file_dialog',
@@ -918,7 +877,7 @@ export function parseCommand(value: unknown): Command {
918
877
  dialog_id: id(message['dialog_id']),
919
878
  }
920
879
  case 'show_message_dialog':
921
- keys(message, COMMAND_SHOW_MESSAGE_DIALOG_KEYS)
880
+ keys(message, COMMAND_KEYS.show_message_dialog)
922
881
  return {
923
882
  id: request,
924
883
  type: 'show_message_dialog',
@@ -927,7 +886,7 @@ export function parseCommand(value: unknown): Command {
927
886
  options: parseMessageDialogOptions(message['options']),
928
887
  }
929
888
  case 'close_message_dialog':
930
- keys(message, COMMAND_CLOSE_MESSAGE_DIALOG_KEYS)
889
+ keys(message, COMMAND_KEYS.close_message_dialog)
931
890
  return {
932
891
  id: request,
933
892
  type: 'close_message_dialog',
@@ -935,18 +894,18 @@ export function parseCommand(value: unknown): Command {
935
894
  dialog_id: id(message['dialog_id']),
936
895
  }
937
896
  case 'show_context_menu':
938
- keys(message, COMMAND_SHOW_CONTEXT_MENU_KEYS)
897
+ keys(message, COMMAND_KEYS.show_context_menu)
939
898
  return {
940
899
  id: request,
941
900
  type: 'show_context_menu',
942
901
  window_id: id(message['window_id']),
943
902
  menu_id: id(message['menu_id']),
944
- x: number(message['x'], 'context menu x', 0, 1_048_576),
945
- y: number(message['y'], 'context menu y', 0, 1_048_576),
903
+ x: number(message['x'], 'context menu x', 0, MAX_POINTER_COORDINATE),
904
+ y: number(message['y'], 'context menu y', 0, MAX_POINTER_COORDINATE),
946
905
  items: parseContextMenuItems(message['items']),
947
906
  }
948
907
  case 'close_context_menu':
949
- keys(message, COMMAND_CLOSE_CONTEXT_MENU_KEYS)
908
+ keys(message, COMMAND_KEYS.close_context_menu)
950
909
  return {
951
910
  id: request,
952
911
  type: 'close_context_menu',
@@ -954,7 +913,7 @@ export function parseCommand(value: unknown): Command {
954
913
  menu_id: id(message['menu_id']),
955
914
  }
956
915
  case 'register_drop_target':
957
- keys(message, COMMAND_REGISTER_DROP_TARGET_KEYS)
916
+ keys(message, COMMAND_KEYS.register_drop_target)
958
917
  return {
959
918
  id: request,
960
919
  type: 'register_drop_target',
@@ -963,7 +922,7 @@ export function parseCommand(value: unknown): Command {
963
922
  node_id: id(message['node_id']),
964
923
  }
965
924
  case 'unregister_drop_target':
966
- keys(message, COMMAND_UNREGISTER_DROP_TARGET_KEYS)
925
+ keys(message, COMMAND_KEYS.unregister_drop_target)
967
926
  return {
968
927
  id: request,
969
928
  type: 'unregister_drop_target',
@@ -971,7 +930,7 @@ export function parseCommand(value: unknown): Command {
971
930
  target_id: id(message['target_id']),
972
931
  }
973
932
  case 'resolve_dropped_item':
974
- keys(message, COMMAND_RESOLVE_DROPPED_ITEM_KEYS)
933
+ keys(message, COMMAND_KEYS.resolve_dropped_item)
975
934
  return {
976
935
  id: request,
977
936
  type: 'resolve_dropped_item',
@@ -982,7 +941,7 @@ export function parseCommand(value: unknown): Command {
982
941
  root: parseRootId(message['root']),
983
942
  }
984
943
  case 'set_app_menu':
985
- keys(message, COMMAND_SET_APP_MENU_KEYS)
944
+ keys(message, COMMAND_KEYS.set_app_menu)
986
945
  return {
987
946
  id: request,
988
947
  type: 'set_app_menu',
@@ -990,14 +949,14 @@ export function parseCommand(value: unknown): Command {
990
949
  menus: parseAppMenus(message['menus']),
991
950
  }
992
951
  case 'shutdown':
993
- keys(message, COMMAND_SHUTDOWN_KEYS)
952
+ keys(message, COMMAND_KEYS.shutdown)
994
953
  return { id: request, type: 'shutdown' }
995
954
  default:
996
955
  throw new ProtocolError('Unknown native command')
997
956
  }
998
957
  }
999
958
 
1000
- const LIMITS_KEYS = fields(...Object.keys(DEFAULT_LIMITS))
959
+ const LIMITS_KEYS: ReadonlySet<string> = new Set(Object.keys(DEFAULT_LIMITS))
1001
960
 
1002
961
  function limits(value: unknown): Limits {
1003
962
  const data = object(value)
@@ -1023,8 +982,6 @@ function limits(value: unknown): Limits {
1023
982
  }
1024
983
  }
1025
984
 
1026
- const ROUTE_KEYS = ['type', 'sequence', 'window_id', 'node_id', 'tree_revision', 'path'] as const
1027
-
1028
985
  /** Short paths, the common case, compare pairwise instead of building a set. */
1029
986
  function unique(path: readonly number[]): boolean {
1030
987
  if (path.length > 16) return new Set(path).size === path.length
@@ -1036,8 +993,10 @@ function unique(path: readonly number[]): boolean {
1036
993
  function route(message: Record<string, unknown>): NodeEventRoute {
1037
994
  const node_id = id(message['node_id'])
1038
995
  const value = message['path']
1039
- if (!Array.isArray(value) || !value.length || value.length > MAX_DEPTH + 1)
1040
- throw new ProtocolError('Native event path must contain between 1 and 65 node identities')
996
+ if (!Array.isArray(value) || !value.length || value.length > MAX_EVENT_PATH_NODES)
997
+ throw new ProtocolError(
998
+ 'Native event path must contain between 1 and ' + MAX_EVENT_PATH_NODES + ' node identities'
999
+ )
1041
1000
  const path = value.map(id)
1042
1001
  if (!unique(path) || path.at(-1) !== node_id)
1043
1002
  throw new ProtocolError('Native event path must be unique and end at its target')
@@ -1050,11 +1009,9 @@ function route(message: Record<string, unknown>): NodeEventRoute {
1050
1009
  }
1051
1010
  }
1052
1011
 
1053
- const MODIFIERS_KEYS = fields('control', 'alt', 'shift', 'platform', 'function')
1054
-
1055
1012
  function modifiers(value: unknown): KeyModifiers {
1056
1013
  const data = object(value)
1057
- keys(data, MODIFIERS_KEYS)
1014
+ keys(data, MODIFIER_KEYS)
1058
1015
  const modifier = (name: keyof KeyModifiers) => {
1059
1016
  if (typeof data[name] !== 'boolean') throw new ProtocolError('Invalid native key modifier')
1060
1017
  return data[name]
@@ -1068,60 +1025,36 @@ function modifiers(value: unknown): KeyModifiers {
1068
1025
  }
1069
1026
  }
1070
1027
 
1071
- const EVENT_READY_KEYS = fields('type', 'protocol', 'capabilities', 'limits', 'service_policy')
1072
- const EVENT_ACK_KEYS = fields('type', 'id')
1073
- const EVENT_OPENED_KEYS = windowReply('root_id', 'tree_revision')
1074
- const EVENT_APPLIED_KEYS = windowReply('tree_revision')
1075
- const EVENT_DISPLAY_SNAPSHOT_KEYS = windowReply('snapshot')
1076
- const EVENT_CLIPBOARD_TEXT_KEYS = windowReply('result')
1077
- const EVENT_CLIPBOARD_WRITTEN_KEYS = windowReply()
1078
- const EVENT_GLOBAL_SHORTCUT_REGISTERED_KEYS = windowReply('shortcut_id')
1079
- const EVENT_DISPLAY_CHANGED_KEYS = windowNotice('snapshot')
1080
- const EVENT_GLOBAL_SHORTCUT_KEYS = windowNotice('shortcut_id')
1081
- const EVENT_NOTIFICATION_POSTED_KEYS = windowReply('notification_id', 'activatable')
1082
- const EVENT_NOTIFICATION_WITHDRAWN_KEYS = windowReply('notification_id')
1083
- const EVENT_NOTIFICATION_ACTIVATED_KEYS = windowNotice('notification_id')
1084
- const EVENT_NOTIFICATION_DISMISSED_KEYS = windowNotice('notification_id', 'reason')
1085
- const EVENT_FILE_GRANT_OPENED_KEYS = windowReply('grant_id')
1086
- const EVENT_FILE_RESULT_KEYS = windowReply('grant_id', 'result')
1087
- const EVENT_PROCESS_SPAWNED_KEYS = windowReply('process_id')
1088
- const EVENT_PROCESS_OUTPUT_KEYS = windowNotice('process_id', 'chunk', 'stream', 'data')
1089
- const EVENT_PROCESS_EXITED_KEYS = windowNotice('process_id', 'exit')
1090
- const EVENT_FILE_DIALOG_OPENED_KEYS = windowReply('dialog_id')
1091
- const EVENT_FILE_DIALOG_COMPLETED_KEYS = windowNotice('dialog_id', 'result')
1092
- const EVENT_MESSAGE_DIALOG_OPENED_KEYS = windowReply('dialog_id')
1093
- const EVENT_MESSAGE_DIALOG_COMPLETED_KEYS = windowNotice('dialog_id', 'result')
1094
- const EVENT_CONTEXT_MENU_OPENED_KEYS = windowReply('menu_id')
1095
- const EVENT_CONTEXT_MENU_COMPLETED_KEYS = windowNotice('menu_id', 'item_id')
1096
- const EVENT_DROP_TARGET_REGISTERED_KEYS = windowReply('target_id')
1097
- const EVENT_DROPPED_ITEM_RESOLVED_KEYS = windowReply('target_id', 'drop_id', 'item', 'root', 'path')
1098
- const EVENT_DROP_TARGET_DRAG_KEYS = windowNotice('target_id', 'state')
1099
- const EVENT_DROP_RECEIVED_KEYS = windowNotice('target_id', 'drop_id', 'items')
1100
- const EVENT_DROP_REJECTED_KEYS = windowNotice('target_id', 'reason')
1101
- const EVENT_ROUTE_KEYS = fields(...ROUTE_KEYS)
1102
- const EVENT_KEY_KEYS = fields(...ROUTE_KEYS, 'key', 'key_char', 'modifiers')
1103
- const EVENT_POINTER_KEYS = fields(...ROUTE_KEYS, 'button', 'x', 'y', 'modifiers')
1104
- const EVENT_DISMISS_KEYS = fields(...ROUTE_KEYS, 'reason')
1105
- const EVENT_SPLIT_RESIZE_KEYS = fields(...ROUTE_KEYS, 'ratio', 'source')
1106
- const EVENT_CLOSED_KEYS = windowNotice()
1107
- const EVENT_INPUT_KEYS = fields(...ROUTE_KEYS, 'value', 'input_revision')
1108
- const EVENT_APP_MENU_SET_KEYS = fields('type', 'id', 'menu_id')
1109
- const EVENT_APP_MENU_ITEM_KEYS = fields('type', 'sequence', 'menu_id', 'item_id')
1110
- const EVENT_ERROR_KEYS = fields('type', 'id', 'code', 'message')
1028
+ const BUTTONS: ReadonlySet<PointerButton> = new Set(POINTER_BUTTONS)
1029
+ const DISMISSALS: ReadonlySet<DismissReason> = new Set(DISMISS_REASONS)
1030
+ const SOURCES: ReadonlySet<ResizeSource> = new Set(RESIZE_SOURCES)
1031
+ const CODES: ReadonlySet<ErrorCode> = new Set(ERROR_CODES)
1032
+ const KNOWN_CAPABILITIES: ReadonlySet<Capability> = new Set(CAPABILITIES)
1033
+
1034
+ /** `value` when `values` holds it: one lookup, where scanning a frozen list is slower. */
1035
+ function member<T>(values: ReadonlySet<T>, value: unknown, message: string): T {
1036
+ if (!values.has(value as T)) throw new ProtocolError(message)
1037
+ return value as T
1038
+ }
1111
1039
 
1112
1040
  /** Native stdout is untrusted until its message shape and limits are checked. */
1113
1041
  export function parseEvent(value: unknown): NativeEvent {
1114
1042
  const message = object(value)
1115
- switch (message['type']) {
1043
+ const type = message['type']
1044
+ switch (type) {
1116
1045
  case 'ready': {
1117
- keys(message, EVENT_READY_KEYS)
1046
+ keys(message, EVENT_KEYS.ready)
1118
1047
  const capabilities = message['capabilities']
1119
1048
  if (!Array.isArray(capabilities) || capabilities.length > CAPABILITIES.length)
1120
1049
  throw new ProtocolError('Invalid native capabilities')
1121
1050
  const parsed: Capability[] = []
1122
1051
  for (const value of capabilities) {
1123
- const capability = CAPABILITIES.find((candidate) => candidate === value)
1124
- if (!capability || parsed.includes(capability))
1052
+ const capability = member(
1053
+ KNOWN_CAPABILITIES,
1054
+ value,
1055
+ 'Unknown or duplicate native capability'
1056
+ )
1057
+ if (parsed.includes(capability))
1125
1058
  throw new ProtocolError('Unknown or duplicate native capability')
1126
1059
  parsed.push(capability)
1127
1060
  }
@@ -1134,10 +1067,10 @@ export function parseEvent(value: unknown): NativeEvent {
1134
1067
  }
1135
1068
  }
1136
1069
  case 'ack':
1137
- keys(message, EVENT_ACK_KEYS)
1070
+ keys(message, EVENT_KEYS.ack)
1138
1071
  return { type: 'ack', id: id(message['id']) }
1139
1072
  case 'opened':
1140
- keys(message, EVENT_OPENED_KEYS)
1073
+ keys(message, EVENT_KEYS.opened)
1141
1074
  return {
1142
1075
  type: 'opened',
1143
1076
  id: id(message['id']),
@@ -1146,7 +1079,7 @@ export function parseEvent(value: unknown): NativeEvent {
1146
1079
  tree_revision: integer(message['tree_revision'], 'tree revision'),
1147
1080
  }
1148
1081
  case 'applied':
1149
- keys(message, EVENT_APPLIED_KEYS)
1082
+ keys(message, EVENT_KEYS.applied)
1150
1083
  return {
1151
1084
  type: 'applied',
1152
1085
  id: id(message['id']),
@@ -1154,7 +1087,7 @@ export function parseEvent(value: unknown): NativeEvent {
1154
1087
  tree_revision: integer(message['tree_revision'], 'tree revision'),
1155
1088
  }
1156
1089
  case 'display_snapshot':
1157
- keys(message, EVENT_DISPLAY_SNAPSHOT_KEYS)
1090
+ keys(message, EVENT_KEYS.display_snapshot)
1158
1091
  return {
1159
1092
  type: 'display_snapshot',
1160
1093
  id: id(message['id']),
@@ -1162,7 +1095,7 @@ export function parseEvent(value: unknown): NativeEvent {
1162
1095
  snapshot: parseDisplaySnapshot(message['snapshot']),
1163
1096
  }
1164
1097
  case 'clipboard_text':
1165
- keys(message, EVENT_CLIPBOARD_TEXT_KEYS)
1098
+ keys(message, EVENT_KEYS.clipboard_text)
1166
1099
  return {
1167
1100
  type: 'clipboard_text',
1168
1101
  id: id(message['id']),
@@ -1170,7 +1103,7 @@ export function parseEvent(value: unknown): NativeEvent {
1170
1103
  result: parseClipboardText(message['result']),
1171
1104
  }
1172
1105
  case 'clipboard_written':
1173
- keys(message, EVENT_CLIPBOARD_WRITTEN_KEYS)
1106
+ keys(message, EVENT_KEYS.clipboard_written)
1174
1107
  return {
1175
1108
  type: 'clipboard_written',
1176
1109
  id: id(message['id']),
@@ -1178,15 +1111,15 @@ export function parseEvent(value: unknown): NativeEvent {
1178
1111
  }
1179
1112
  case 'global_shortcut_registered':
1180
1113
  case 'global_shortcut_unregistered':
1181
- keys(message, EVENT_GLOBAL_SHORTCUT_REGISTERED_KEYS)
1114
+ keys(message, EVENT_KEYS[type])
1182
1115
  return {
1183
- type: message['type'],
1116
+ type,
1184
1117
  id: id(message['id']),
1185
1118
  window_id: id(message['window_id']),
1186
1119
  shortcut_id: id(message['shortcut_id']),
1187
1120
  }
1188
1121
  case 'display_changed':
1189
- keys(message, EVENT_DISPLAY_CHANGED_KEYS)
1122
+ keys(message, EVENT_KEYS.display_changed)
1190
1123
  return {
1191
1124
  type: 'display_changed',
1192
1125
  sequence: id(message['sequence']),
@@ -1194,7 +1127,7 @@ export function parseEvent(value: unknown): NativeEvent {
1194
1127
  snapshot: parseDisplaySnapshot(message['snapshot']),
1195
1128
  }
1196
1129
  case 'global_shortcut':
1197
- keys(message, EVENT_GLOBAL_SHORTCUT_KEYS)
1130
+ keys(message, EVENT_KEYS.global_shortcut)
1198
1131
  return {
1199
1132
  type: 'global_shortcut',
1200
1133
  sequence: id(message['sequence']),
@@ -1202,7 +1135,7 @@ export function parseEvent(value: unknown): NativeEvent {
1202
1135
  shortcut_id: id(message['shortcut_id']),
1203
1136
  }
1204
1137
  case 'notification_posted':
1205
- keys(message, EVENT_NOTIFICATION_POSTED_KEYS)
1138
+ keys(message, EVENT_KEYS.notification_posted)
1206
1139
  if (typeof message['activatable'] !== 'boolean')
1207
1140
  throw new ProtocolError('Notification activatable must be a boolean')
1208
1141
  return {
@@ -1213,7 +1146,7 @@ export function parseEvent(value: unknown): NativeEvent {
1213
1146
  activatable: message['activatable'],
1214
1147
  }
1215
1148
  case 'notification_withdrawn':
1216
- keys(message, EVENT_NOTIFICATION_WITHDRAWN_KEYS)
1149
+ keys(message, EVENT_KEYS.notification_withdrawn)
1217
1150
  return {
1218
1151
  type: 'notification_withdrawn',
1219
1152
  id: id(message['id']),
@@ -1221,7 +1154,7 @@ export function parseEvent(value: unknown): NativeEvent {
1221
1154
  notification_id: id(message['notification_id']),
1222
1155
  }
1223
1156
  case 'notification_activated':
1224
- keys(message, EVENT_NOTIFICATION_ACTIVATED_KEYS)
1157
+ keys(message, EVENT_KEYS.notification_activated)
1225
1158
  return {
1226
1159
  type: 'notification_activated',
1227
1160
  sequence: id(message['sequence']),
@@ -1229,7 +1162,7 @@ export function parseEvent(value: unknown): NativeEvent {
1229
1162
  notification_id: id(message['notification_id']),
1230
1163
  }
1231
1164
  case 'notification_dismissed':
1232
- keys(message, EVENT_NOTIFICATION_DISMISSED_KEYS)
1165
+ keys(message, EVENT_KEYS.notification_dismissed)
1233
1166
  return {
1234
1167
  type: 'notification_dismissed',
1235
1168
  sequence: id(message['sequence']),
@@ -1239,15 +1172,15 @@ export function parseEvent(value: unknown): NativeEvent {
1239
1172
  }
1240
1173
  case 'file_grant_opened':
1241
1174
  case 'file_grant_closed':
1242
- keys(message, EVENT_FILE_GRANT_OPENED_KEYS)
1175
+ keys(message, EVENT_KEYS[type])
1243
1176
  return {
1244
- type: message['type'],
1177
+ type,
1245
1178
  id: id(message['id']),
1246
1179
  window_id: id(message['window_id']),
1247
1180
  grant_id: id(message['grant_id']),
1248
1181
  }
1249
1182
  case 'file_result':
1250
- keys(message, EVENT_FILE_RESULT_KEYS)
1183
+ keys(message, EVENT_KEYS.file_result)
1251
1184
  return {
1252
1185
  type: 'file_result',
1253
1186
  id: id(message['id']),
@@ -1258,15 +1191,15 @@ export function parseEvent(value: unknown): NativeEvent {
1258
1191
  case 'process_spawned':
1259
1192
  case 'process_updated':
1260
1193
  case 'process_released':
1261
- keys(message, EVENT_PROCESS_SPAWNED_KEYS)
1194
+ keys(message, EVENT_KEYS[type])
1262
1195
  return {
1263
- type: message['type'],
1196
+ type,
1264
1197
  id: id(message['id']),
1265
1198
  window_id: id(message['window_id']),
1266
1199
  process_id: id(message['process_id']),
1267
1200
  }
1268
1201
  case 'process_output':
1269
- keys(message, EVENT_PROCESS_OUTPUT_KEYS)
1202
+ keys(message, EVENT_KEYS.process_output)
1270
1203
  return {
1271
1204
  type: 'process_output',
1272
1205
  sequence: id(message['sequence']),
@@ -1277,7 +1210,7 @@ export function parseEvent(value: unknown): NativeEvent {
1277
1210
  data: parseProcessOutput(message['data']),
1278
1211
  }
1279
1212
  case 'process_exited':
1280
- keys(message, EVENT_PROCESS_EXITED_KEYS)
1213
+ keys(message, EVENT_KEYS.process_exited)
1281
1214
  return {
1282
1215
  type: 'process_exited',
1283
1216
  sequence: id(message['sequence']),
@@ -1287,15 +1220,15 @@ export function parseEvent(value: unknown): NativeEvent {
1287
1220
  }
1288
1221
  case 'file_dialog_opened':
1289
1222
  case 'file_dialog_closed':
1290
- keys(message, EVENT_FILE_DIALOG_OPENED_KEYS)
1223
+ keys(message, EVENT_KEYS[type])
1291
1224
  return {
1292
- type: message['type'],
1225
+ type,
1293
1226
  id: id(message['id']),
1294
1227
  window_id: id(message['window_id']),
1295
1228
  dialog_id: id(message['dialog_id']),
1296
1229
  }
1297
1230
  case 'file_dialog_completed':
1298
- keys(message, EVENT_FILE_DIALOG_COMPLETED_KEYS)
1231
+ keys(message, EVENT_KEYS.file_dialog_completed)
1299
1232
  return {
1300
1233
  type: 'file_dialog_completed',
1301
1234
  sequence: id(message['sequence']),
@@ -1305,15 +1238,15 @@ export function parseEvent(value: unknown): NativeEvent {
1305
1238
  }
1306
1239
  case 'message_dialog_opened':
1307
1240
  case 'message_dialog_closed':
1308
- keys(message, EVENT_MESSAGE_DIALOG_OPENED_KEYS)
1241
+ keys(message, EVENT_KEYS[type])
1309
1242
  return {
1310
- type: message['type'],
1243
+ type,
1311
1244
  id: id(message['id']),
1312
1245
  window_id: id(message['window_id']),
1313
1246
  dialog_id: id(message['dialog_id']),
1314
1247
  }
1315
1248
  case 'message_dialog_completed':
1316
- keys(message, EVENT_MESSAGE_DIALOG_COMPLETED_KEYS)
1249
+ keys(message, EVENT_KEYS.message_dialog_completed)
1317
1250
  return {
1318
1251
  type: 'message_dialog_completed',
1319
1252
  sequence: id(message['sequence']),
@@ -1323,15 +1256,15 @@ export function parseEvent(value: unknown): NativeEvent {
1323
1256
  }
1324
1257
  case 'context_menu_opened':
1325
1258
  case 'context_menu_closed':
1326
- keys(message, EVENT_CONTEXT_MENU_OPENED_KEYS)
1259
+ keys(message, EVENT_KEYS[type])
1327
1260
  return {
1328
- type: message['type'],
1261
+ type,
1329
1262
  id: id(message['id']),
1330
1263
  window_id: id(message['window_id']),
1331
1264
  menu_id: id(message['menu_id']),
1332
1265
  }
1333
1266
  case 'context_menu_completed': {
1334
- keys(message, EVENT_CONTEXT_MENU_COMPLETED_KEYS)
1267
+ keys(message, EVENT_KEYS.context_menu_completed)
1335
1268
  const event: Extract<NativeEvent, { type: 'context_menu_completed' }> = {
1336
1269
  type: 'context_menu_completed',
1337
1270
  sequence: id(message['sequence']),
@@ -1343,15 +1276,15 @@ export function parseEvent(value: unknown): NativeEvent {
1343
1276
  }
1344
1277
  case 'drop_target_registered':
1345
1278
  case 'drop_target_unregistered':
1346
- keys(message, EVENT_DROP_TARGET_REGISTERED_KEYS)
1279
+ keys(message, EVENT_KEYS[type])
1347
1280
  return {
1348
- type: message['type'],
1281
+ type,
1349
1282
  id: id(message['id']),
1350
1283
  window_id: id(message['window_id']),
1351
1284
  target_id: id(message['target_id']),
1352
1285
  }
1353
1286
  case 'dropped_item_resolved':
1354
- keys(message, EVENT_DROPPED_ITEM_RESOLVED_KEYS)
1287
+ keys(message, EVENT_KEYS.dropped_item_resolved)
1355
1288
  return {
1356
1289
  type: 'dropped_item_resolved',
1357
1290
  id: id(message['id']),
@@ -1363,7 +1296,7 @@ export function parseEvent(value: unknown): NativeEvent {
1363
1296
  path: parseFilePath(message['path']),
1364
1297
  }
1365
1298
  case 'drop_target_drag':
1366
- keys(message, EVENT_DROP_TARGET_DRAG_KEYS)
1299
+ keys(message, EVENT_KEYS.drop_target_drag)
1367
1300
  return {
1368
1301
  type: 'drop_target_drag',
1369
1302
  sequence: id(message['sequence']),
@@ -1372,7 +1305,7 @@ export function parseEvent(value: unknown): NativeEvent {
1372
1305
  state: parseDragState(message['state']),
1373
1306
  }
1374
1307
  case 'drop_received':
1375
- keys(message, EVENT_DROP_RECEIVED_KEYS)
1308
+ keys(message, EVENT_KEYS.drop_received)
1376
1309
  return {
1377
1310
  type: 'drop_received',
1378
1311
  sequence: id(message['sequence']),
@@ -1382,7 +1315,7 @@ export function parseEvent(value: unknown): NativeEvent {
1382
1315
  items: parseDroppedItems(message['items']),
1383
1316
  }
1384
1317
  case 'drop_rejected':
1385
- keys(message, EVENT_DROP_REJECTED_KEYS)
1318
+ keys(message, EVENT_KEYS.drop_rejected)
1386
1319
  return {
1387
1320
  type: 'drop_rejected',
1388
1321
  sequence: id(message['sequence']),
@@ -1393,69 +1326,68 @@ export function parseEvent(value: unknown): NativeEvent {
1393
1326
  case 'click':
1394
1327
  case 'focus':
1395
1328
  case 'blur':
1396
- keys(message, EVENT_ROUTE_KEYS)
1329
+ // Hot input paths read their table entry statically; a keyed read costs
1330
+ // about a tenth of the parse.
1331
+ keys(
1332
+ message,
1333
+ type === 'click' ? EVENT_KEYS.click : type === 'focus' ? EVENT_KEYS.focus : EVENT_KEYS.blur
1334
+ )
1397
1335
  return {
1398
- type: message['type'],
1336
+ type,
1399
1337
  ...route(message),
1400
1338
  }
1401
1339
  case 'key_down':
1402
1340
  case 'key_up': {
1403
- keys(message, EVENT_KEY_KEYS)
1404
- const key = string(message['key'], 'key', 256)
1341
+ keys(message, type === 'key_down' ? EVENT_KEYS.key_down : EVENT_KEYS.key_up)
1342
+ const key = string(message['key'], 'key', MAX_KEY_BYTES)
1405
1343
  if (!key) throw new ProtocolError('Native key must be nonempty')
1406
1344
  return {
1407
- type: message['type'],
1345
+ type,
1408
1346
  ...route(message),
1409
1347
  key,
1410
1348
  ...('key_char' in message
1411
- ? { key_char: string(message['key_char'], 'key_char', 256) }
1349
+ ? { key_char: string(message['key_char'], 'key_char', MAX_KEY_BYTES) }
1412
1350
  : {}),
1413
1351
  modifiers: modifiers(message['modifiers']),
1414
1352
  }
1415
1353
  }
1416
1354
  case 'pointer_down':
1417
1355
  case 'pointer_up': {
1418
- keys(message, EVENT_POINTER_KEYS)
1419
- const button = (['left', 'right', 'middle', 'back', 'forward'] as const).find(
1420
- (button) => button === message['button']
1421
- )
1422
- if (!button) throw new ProtocolError('Unknown native pointer button')
1356
+ keys(message, type === 'pointer_down' ? EVENT_KEYS.pointer_down : EVENT_KEYS.pointer_up)
1423
1357
  return {
1424
- type: message['type'],
1358
+ type,
1425
1359
  ...route(message),
1426
- button,
1427
- x: number(message['x'], 'pointer x', -1_048_576, 1_048_576),
1428
- y: number(message['y'], 'pointer y', -1_048_576, 1_048_576),
1360
+ button: member(BUTTONS, message['button'], 'Unknown native pointer button'),
1361
+ x: number(message['x'], 'pointer x', -MAX_POINTER_COORDINATE, MAX_POINTER_COORDINATE),
1362
+ y: number(message['y'], 'pointer y', -MAX_POINTER_COORDINATE, MAX_POINTER_COORDINATE),
1429
1363
  modifiers: modifiers(message['modifiers']),
1430
1364
  }
1431
1365
  }
1432
1366
  case 'dismiss':
1433
- keys(message, EVENT_DISMISS_KEYS)
1434
- if (message['reason'] !== 'window_blur')
1435
- throw new ProtocolError('Unknown native dismissal reason')
1436
- return { type: 'dismiss', ...route(message), reason: 'window_blur' }
1367
+ keys(message, EVENT_KEYS.dismiss)
1368
+ return {
1369
+ type: 'dismiss',
1370
+ ...route(message),
1371
+ reason: member(DISMISSALS, message['reason'], 'Unknown native dismissal reason'),
1372
+ }
1437
1373
  case 'split_resize': {
1438
- keys(message, EVENT_SPLIT_RESIZE_KEYS)
1439
- const source = (['pointer', 'keyboard'] as const).find(
1440
- (source) => source === message['source']
1441
- )
1442
- if (!source) throw new ProtocolError('Unknown split resize source')
1374
+ keys(message, EVENT_KEYS.split_resize)
1443
1375
  return {
1444
1376
  type: 'split_resize',
1445
1377
  ...route(message),
1446
1378
  ratio: number(message['ratio'], 'split ratio', 0, 1),
1447
- source,
1379
+ source: member(SOURCES, message['source'], 'Unknown split resize source'),
1448
1380
  }
1449
1381
  }
1450
1382
  case 'closed':
1451
- keys(message, EVENT_CLOSED_KEYS)
1383
+ keys(message, EVENT_KEYS.closed)
1452
1384
  return {
1453
1385
  type: 'closed',
1454
1386
  sequence: id(message['sequence']),
1455
1387
  window_id: id(message['window_id']),
1456
1388
  }
1457
1389
  case 'input':
1458
- keys(message, EVENT_INPUT_KEYS)
1390
+ keys(message, EVENT_KEYS.input)
1459
1391
  return {
1460
1392
  type: 'input',
1461
1393
  ...route(message),
@@ -1463,10 +1395,10 @@ export function parseEvent(value: unknown): NativeEvent {
1463
1395
  input_revision: integer(message['input_revision'], 'input revision'),
1464
1396
  }
1465
1397
  case 'app_menu_set':
1466
- keys(message, EVENT_APP_MENU_SET_KEYS)
1398
+ keys(message, EVENT_KEYS.app_menu_set)
1467
1399
  return { type: 'app_menu_set', id: id(message['id']), menu_id: id(message['menu_id']) }
1468
1400
  case 'app_menu_item':
1469
- keys(message, EVENT_APP_MENU_ITEM_KEYS)
1401
+ keys(message, EVENT_KEYS.app_menu_item)
1470
1402
  return {
1471
1403
  type: 'app_menu_item',
1472
1404
  sequence: id(message['sequence']),
@@ -1474,12 +1406,10 @@ export function parseEvent(value: unknown): NativeEvent {
1474
1406
  item_id: parseAppMenuItemId(message['item_id']),
1475
1407
  }
1476
1408
  case 'error': {
1477
- keys(message, EVENT_ERROR_KEYS)
1478
- const code = ERROR_CODES.find((code) => code === message['code'])
1479
- if (!code) throw new ProtocolError('Invalid native error code')
1409
+ keys(message, EVENT_KEYS.error)
1480
1410
  return {
1481
1411
  type: 'error',
1482
- code,
1412
+ code: member(CODES, message['code'], 'Invalid native error code'),
1483
1413
  message: string(message['message'], 'message'),
1484
1414
  ...('id' in message ? { id: id(message['id']) } : {}),
1485
1415
  }