@bun-native/protocol 0.33.0 → 0.33.2

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,61 +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
- id,
79
- integer,
80
- keys,
81
- 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,
82
83
  MAX_TITLE_BYTES,
83
- number,
84
- object,
85
- string,
86
- } from './values.ts'
87
-
88
- export const PROTOCOL_VERSION = 11
89
- export const MAX_OPERATIONS = 256
90
- const MAX_NODES_PER_WINDOW = 4_096
91
- const MAX_WINDOWS = 16
92
- const MAX_DEPTH = 64
93
- const MAX_TEXT_BYTES_PER_WINDOW = 4_194_304
94
-
95
- export interface Limits {
96
- max_frame_bytes: number
97
- max_operations: number
98
- max_nodes_per_window: number
99
- max_windows: number
100
- max_depth: number
101
- max_text_bytes: number
102
- max_text_bytes_per_window: number
103
- max_clipboard_bytes: number
104
- max_service_requests: number
105
- max_global_shortcuts: number
106
- max_notifications: number
107
- max_file_grants: number
108
- max_file_chunk_bytes: number
109
- max_processes: number
110
- max_drop_targets: number
111
- max_drop_items: number
112
- }
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'
113
96
 
114
- export const DEFAULT_LIMITS: Readonly<Limits> = Object.freeze({
115
- max_frame_bytes: MAX_FRAME_BYTES,
116
- max_operations: MAX_OPERATIONS,
117
- max_nodes_per_window: MAX_NODES_PER_WINDOW,
118
- max_windows: MAX_WINDOWS,
119
- max_depth: MAX_DEPTH,
120
- max_text_bytes: MAX_TEXT_BYTES,
121
- max_text_bytes_per_window: MAX_TEXT_BYTES_PER_WINDOW,
122
- max_clipboard_bytes: MAX_CLIPBOARD_BYTES,
123
- max_service_requests: MAX_SERVICE_REQUESTS,
124
- max_global_shortcuts: MAX_GLOBAL_SHORTCUTS,
125
- max_notifications: MAX_NOTIFICATIONS,
126
- max_file_grants: MAX_FILE_GRANTS,
127
- max_file_chunk_bytes: MAX_FILE_CHUNK_BYTES,
128
- max_processes: MAX_PROCESSES,
129
- max_drop_targets: MAX_DROP_TARGETS,
130
- max_drop_items: MAX_DROP_ITEMS,
131
- })
97
+ export type { Capability, DismissReason, ErrorCode, Limits, PointerButton, ResizeSource }
132
98
 
133
99
  export type Operation =
134
100
  | { type: 'create'; node_id: number; element: Element }
@@ -152,9 +118,6 @@ export type Operation =
152
118
  /** Requires `scroll_control_v1`. */
153
119
  | { type: 'scroll_into_view'; node_id: number }
154
120
 
155
- /** Largest programmatic scroll offset, in logical pixels. */
156
- export const MAX_SCROLL_OFFSET = 1_000_000
157
-
158
121
  export type Command =
159
122
  | { id: number; type: 'hello'; protocol: typeof PROTOCOL_VERSION }
160
123
  | {
@@ -316,65 +279,6 @@ export type Command =
316
279
  | { id: number; type: 'set_app_menu'; menu_id: number; menus: AppMenu[] }
317
280
  | { id: number; type: 'shutdown' }
318
281
 
319
- export const ERROR_CODES = [
320
- 'invalid_command',
321
- 'version_mismatch',
322
- 'invalid_state',
323
- 'invalid_handle',
324
- 'stale_revision',
325
- 'limit_exceeded',
326
- 'native_failure',
327
- 'permission_denied',
328
- 'service_unavailable',
329
- 'cancelled',
330
- 'operation_timed_out',
331
- 'payload_too_large',
332
- 'conflict',
333
- 'not_found',
334
- 'already_exists',
335
- 'path_denied',
336
- 'wrong_kind',
337
- 'not_empty',
338
- 'access_denied',
339
- ] as const
340
- export type ErrorCode = (typeof ERROR_CODES)[number]
341
- export const REQUIRED_CAPABILITIES = Object.freeze([
342
- 'tree',
343
- 'input_revisions',
344
- 'layout_v1',
345
- 'interactions_v1',
346
- 'window_display_v1',
347
- ] as const)
348
- const CAPABILITIES = [
349
- ...REQUIRED_CAPABILITIES,
350
- 'clipboard_text_v1',
351
- 'global_shortcuts_v1',
352
- 'notifications_v1',
353
- 'filesystem_grants_v1',
354
- 'processes_v1',
355
- 'split_v1',
356
- 'style_decoration_v1',
357
- 'display_details_v1',
358
- 'display_changes_v1',
359
- 'file_dialogs_v1',
360
- 'overlays_v1',
361
- 'scroll_anchor_v1',
362
- 'focus_v1',
363
- 'scroll_control_v1',
364
- 'overlay_keyboard_v1',
365
- 'popup_windows_v1',
366
- 'drag_drop_files_v1',
367
- 'input_states_v1',
368
- 'app_menu_v1',
369
- 'task_dialogs_v1',
370
- 'context_menus_v1',
371
- 'split_resize_v1',
372
- 'accessibility_v1',
373
- 'accessibility_lists_v1',
374
- 'display_identity_v1',
375
- ] as const
376
- export type Capability = (typeof CAPABILITIES)[number]
377
-
378
282
  export interface KeyModifiers {
379
283
  control: boolean
380
284
  alt: boolean
@@ -383,12 +287,6 @@ export interface KeyModifiers {
383
287
  function: boolean
384
288
  }
385
289
 
386
- export type PointerButton = 'left' | 'right' | 'middle' | 'back' | 'forward'
387
- /** Why the host asks the application to close a popup window overlay. */
388
- export type DismissReason = 'window_blur'
389
- /** What moved a split divider (`split_resize_v1`). */
390
- export type ResizeSource = 'pointer' | 'keyboard'
391
-
392
290
  interface NodeEventRoute {
393
291
  sequence: number
394
292
  window_id: number
@@ -614,57 +512,164 @@ export type NativeEvent =
614
512
  /** Application-scoped: a menu bar belongs to the application, not a window. */
615
513
  | { type: 'app_menu_item'; sequence: number; menu_id: number; item_id: string }
616
514
  | { type: 'error'; id?: number; code: ErrorCode; message: string }
515
+ /** Sequenced events owned by one window. */
516
+ export type WindowEvent = Extract<NativeEvent, { sequence: number; window_id: number }>
517
+ /** Sequenced events owned by the application rather than one window. */
518
+ export type ApplicationEvent = Extract<NativeEvent, { type: 'app_menu_item' }>
519
+ /** Unsolicited events, as opposed to responses that carry a request id. */
520
+ export type HostEvent = WindowEvent | ApplicationEvent
617
521
 
618
522
  function version(value: unknown): typeof PROTOCOL_VERSION {
619
523
  if (value !== PROTOCOL_VERSION) throw new ProtocolError('Native protocol version mismatch')
620
524
  return PROTOCOL_VERSION
621
525
  }
622
526
 
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
+ }
634
+
635
+ scrollIntoView(nodeId: number): void {
636
+ this.#push({ type: 'scroll_into_view', node_id: id(nodeId) })
637
+ }
638
+ }
639
+
623
640
  function operation(value: unknown): Operation {
624
641
  const data = object(value)
625
- switch (data['type']) {
642
+ const type = data['type']
643
+ switch (type) {
626
644
  case 'create':
627
- keys(data, ['type', 'node_id', 'element'])
628
- return {
629
- type: 'create',
630
- node_id: id(data['node_id']),
631
- element: parseElement(data['element']),
632
- }
633
- case 'set': {
634
- keys(data, ['type', 'node_id', 'element', 'expected_input_revision'])
635
- const node_id = id(data['node_id'])
636
- const element = parseElement(data['element'])
637
- if (element.kind === 'input')
638
- return {
639
- type: 'set',
640
- node_id,
641
- element,
642
- expected_input_revision: integer(data['expected_input_revision'], 'input revision'),
643
- }
644
- if ('expected_input_revision' in data)
645
- throw new ProtocolError('Only input elements accept an input revision')
646
- return { type: 'set', node_id, element }
647
- }
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
+ )
648
655
  case 'insert':
649
- keys(data, ['type', 'parent_id', 'child_id', 'index'])
650
- return {
651
- type: 'insert',
652
- parent_id: id(data['parent_id']),
653
- child_id: id(data['child_id']),
654
- index: integer(data['index'], 'index', 0, MAX_NODES_PER_WINDOW),
655
- }
656
+ keys(data, OPERATION_KEYS.insert)
657
+ return insertOperation(data['parent_id'], data['child_id'], data['index'])
656
658
  case 'delete':
657
659
  case 'focus':
658
660
  case 'scroll_into_view':
659
- keys(data, ['type', 'node_id'])
660
- return { type: data['type'], node_id: id(data['node_id']) }
661
- case 'scroll_to': {
662
- keys(data, ['type', 'node_id', 'x', 'y'])
663
- if (!('x' in data) && !('y' in data)) throw new ProtocolError('scroll_to requires x or y')
664
- const offset = (axis: 'x' | 'y') =>
665
- axis in data ? { [axis]: number(data[axis], 'scroll ' + axis, 0, MAX_SCROLL_OFFSET) } : {}
666
- return { type: 'scroll_to', node_id: id(data['node_id']), ...offset('x'), ...offset('y') }
667
- }
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)
668
673
  default:
669
674
  throw new ProtocolError('Unknown native operation')
670
675
  }
@@ -674,16 +679,17 @@ function operation(value: unknown): Operation {
674
679
  export function parseCommand(value: unknown): Command {
675
680
  const message = object(value)
676
681
  const request = id(message['id'])
677
- switch (message['type']) {
682
+ const type = message['type']
683
+ switch (type) {
678
684
  case 'hello':
679
- keys(message, ['id', 'type', 'protocol'])
685
+ keys(message, COMMAND_KEYS.hello)
680
686
  return {
681
687
  id: request,
682
688
  type: 'hello',
683
689
  protocol: version(message['protocol']),
684
690
  }
685
691
  case 'open': {
686
- keys(message, ['id', 'type', 'window_id', 'root_id', 'title', 'width', 'height'])
692
+ keys(message, COMMAND_KEYS.open)
687
693
  const title = string(message['title'], 'title', MAX_TITLE_BYTES)
688
694
  if (!title || /\p{Cc}/u.test(title))
689
695
  throw new ProtocolError('Window title must be nonempty and contain no control characters')
@@ -698,8 +704,10 @@ export function parseCommand(value: unknown): Command {
698
704
  }
699
705
  }
700
706
  case 'batch': {
701
- keys(message, ['id', 'type', 'window_id', 'base_revision', 'operations'])
702
- 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']
703
711
  if (!Array.isArray(operations) || operations.length < 1 || operations.length > MAX_OPERATIONS)
704
712
  throw new ProtocolError(
705
713
  'Batch must contain between 1 and ' + MAX_OPERATIONS + ' operations'
@@ -709,20 +717,20 @@ export function parseCommand(value: unknown): Command {
709
717
  type: 'batch',
710
718
  window_id: id(message['window_id']),
711
719
  base_revision: integer(message['base_revision'], 'tree revision'),
712
- operations: operations.map(operation),
720
+ operations: built ?? operations.map(operation),
713
721
  }
714
722
  }
715
723
  case 'close':
716
724
  case 'display_snapshot':
717
725
  case 'clipboard_read':
718
- keys(message, ['id', 'type', 'window_id'])
726
+ keys(message, COMMAND_KEYS[type])
719
727
  return {
720
728
  id: request,
721
- type: message['type'],
729
+ type,
722
730
  window_id: id(message['window_id']),
723
731
  }
724
732
  case 'clipboard_write':
725
- keys(message, ['id', 'type', 'window_id', 'text'])
733
+ keys(message, COMMAND_KEYS.clipboard_write)
726
734
  return {
727
735
  id: request,
728
736
  type: 'clipboard_write',
@@ -730,7 +738,7 @@ export function parseCommand(value: unknown): Command {
730
738
  text: string(message['text'], 'clipboard text', MAX_CLIPBOARD_BYTES),
731
739
  }
732
740
  case 'register_global_shortcut':
733
- keys(message, ['id', 'type', 'window_id', 'shortcut_id', 'key', 'modifiers'])
741
+ keys(message, COMMAND_KEYS.register_global_shortcut)
734
742
  return {
735
743
  id: request,
736
744
  type: 'register_global_shortcut',
@@ -740,7 +748,7 @@ export function parseCommand(value: unknown): Command {
740
748
  modifiers: parseShortcutModifiers(message['modifiers']),
741
749
  }
742
750
  case 'unregister_global_shortcut':
743
- keys(message, ['id', 'type', 'window_id', 'shortcut_id'])
751
+ keys(message, COMMAND_KEYS.unregister_global_shortcut)
744
752
  return {
745
753
  id: request,
746
754
  type: 'unregister_global_shortcut',
@@ -748,7 +756,7 @@ export function parseCommand(value: unknown): Command {
748
756
  shortcut_id: id(message['shortcut_id']),
749
757
  }
750
758
  case 'post_notification': {
751
- keys(message, ['id', 'type', 'window_id', 'notification_id', 'title', 'body'])
759
+ keys(message, COMMAND_KEYS.post_notification)
752
760
  const { title, body } = parseNotificationContent(message['title'], message['body'])
753
761
  return {
754
762
  id: request,
@@ -760,7 +768,7 @@ export function parseCommand(value: unknown): Command {
760
768
  }
761
769
  }
762
770
  case 'withdraw_notification':
763
- keys(message, ['id', 'type', 'window_id', 'notification_id'])
771
+ keys(message, COMMAND_KEYS.withdraw_notification)
764
772
  return {
765
773
  id: request,
766
774
  type: 'withdraw_notification',
@@ -768,7 +776,7 @@ export function parseCommand(value: unknown): Command {
768
776
  notification_id: id(message['notification_id']),
769
777
  }
770
778
  case 'open_file_grant':
771
- keys(message, ['id', 'type', 'window_id', 'grant_id', 'root', 'access'])
779
+ keys(message, COMMAND_KEYS.open_file_grant)
772
780
  return {
773
781
  id: request,
774
782
  type: 'open_file_grant',
@@ -778,7 +786,7 @@ export function parseCommand(value: unknown): Command {
778
786
  access: parseFileAccess(message['access']),
779
787
  }
780
788
  case 'close_file_grant':
781
- keys(message, ['id', 'type', 'window_id', 'grant_id'])
789
+ keys(message, COMMAND_KEYS.close_file_grant)
782
790
  return {
783
791
  id: request,
784
792
  type: 'close_file_grant',
@@ -786,7 +794,7 @@ export function parseCommand(value: unknown): Command {
786
794
  grant_id: id(message['grant_id']),
787
795
  }
788
796
  case 'file_request': {
789
- keys(message, ['id', 'type', 'window_id', 'grant_id', 'path', 'operation'])
797
+ keys(message, COMMAND_KEYS.file_request)
790
798
  const path = parseFilePath(message['path'])
791
799
  return {
792
800
  id: request,
@@ -798,7 +806,7 @@ export function parseCommand(value: unknown): Command {
798
806
  }
799
807
  }
800
808
  case 'spawn_process':
801
- keys(message, ['id', 'type', 'window_id', 'process_id', 'launch'])
809
+ keys(message, COMMAND_KEYS.spawn_process)
802
810
  return {
803
811
  id: request,
804
812
  type: 'spawn_process',
@@ -807,7 +815,7 @@ export function parseCommand(value: unknown): Command {
807
815
  launch: parseProcessLaunch(message['launch']),
808
816
  }
809
817
  case 'write_process':
810
- keys(message, ['id', 'type', 'window_id', 'process_id', 'data'])
818
+ keys(message, COMMAND_KEYS.write_process)
811
819
  return {
812
820
  id: request,
813
821
  type: 'write_process',
@@ -817,15 +825,15 @@ export function parseCommand(value: unknown): Command {
817
825
  }
818
826
  case 'close_process_input':
819
827
  case 'release_process':
820
- keys(message, ['id', 'type', 'window_id', 'process_id'])
828
+ keys(message, COMMAND_KEYS[type])
821
829
  return {
822
830
  id: request,
823
- type: message['type'],
831
+ type,
824
832
  window_id: id(message['window_id']),
825
833
  process_id: id(message['process_id']),
826
834
  }
827
835
  case 'resize_process':
828
- keys(message, ['id', 'type', 'window_id', 'process_id', 'size'])
836
+ keys(message, COMMAND_KEYS.resize_process)
829
837
  return {
830
838
  id: request,
831
839
  type: 'resize_process',
@@ -834,7 +842,7 @@ export function parseCommand(value: unknown): Command {
834
842
  size: parseTerminalSize(message['size']),
835
843
  }
836
844
  case 'signal_process':
837
- keys(message, ['id', 'type', 'window_id', 'process_id', 'signal'])
845
+ keys(message, COMMAND_KEYS.signal_process)
838
846
  return {
839
847
  id: request,
840
848
  type: 'signal_process',
@@ -843,7 +851,7 @@ export function parseCommand(value: unknown): Command {
843
851
  signal: parseProcessSignal(message['signal']),
844
852
  }
845
853
  case 'acknowledge_process_output':
846
- keys(message, ['id', 'type', 'window_id', 'process_id', 'chunk'])
854
+ keys(message, COMMAND_KEYS.acknowledge_process_output)
847
855
  return {
848
856
  id: request,
849
857
  type: 'acknowledge_process_output',
@@ -852,7 +860,7 @@ export function parseCommand(value: unknown): Command {
852
860
  chunk: id(message['chunk']),
853
861
  }
854
862
  case 'show_file_dialog':
855
- keys(message, ['id', 'type', 'window_id', 'dialog_id', 'options'])
863
+ keys(message, COMMAND_KEYS.show_file_dialog)
856
864
  return {
857
865
  id: request,
858
866
  type: 'show_file_dialog',
@@ -861,7 +869,7 @@ export function parseCommand(value: unknown): Command {
861
869
  options: parseFileDialogOptions(message['options']),
862
870
  }
863
871
  case 'close_file_dialog':
864
- keys(message, ['id', 'type', 'window_id', 'dialog_id'])
872
+ keys(message, COMMAND_KEYS.close_file_dialog)
865
873
  return {
866
874
  id: request,
867
875
  type: 'close_file_dialog',
@@ -869,7 +877,7 @@ export function parseCommand(value: unknown): Command {
869
877
  dialog_id: id(message['dialog_id']),
870
878
  }
871
879
  case 'show_message_dialog':
872
- keys(message, ['id', 'type', 'window_id', 'dialog_id', 'options'])
880
+ keys(message, COMMAND_KEYS.show_message_dialog)
873
881
  return {
874
882
  id: request,
875
883
  type: 'show_message_dialog',
@@ -878,7 +886,7 @@ export function parseCommand(value: unknown): Command {
878
886
  options: parseMessageDialogOptions(message['options']),
879
887
  }
880
888
  case 'close_message_dialog':
881
- keys(message, ['id', 'type', 'window_id', 'dialog_id'])
889
+ keys(message, COMMAND_KEYS.close_message_dialog)
882
890
  return {
883
891
  id: request,
884
892
  type: 'close_message_dialog',
@@ -886,18 +894,18 @@ export function parseCommand(value: unknown): Command {
886
894
  dialog_id: id(message['dialog_id']),
887
895
  }
888
896
  case 'show_context_menu':
889
- keys(message, ['id', 'type', 'window_id', 'menu_id', 'x', 'y', 'items'])
897
+ keys(message, COMMAND_KEYS.show_context_menu)
890
898
  return {
891
899
  id: request,
892
900
  type: 'show_context_menu',
893
901
  window_id: id(message['window_id']),
894
902
  menu_id: id(message['menu_id']),
895
- x: number(message['x'], 'context menu x', 0, 1_048_576),
896
- 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),
897
905
  items: parseContextMenuItems(message['items']),
898
906
  }
899
907
  case 'close_context_menu':
900
- keys(message, ['id', 'type', 'window_id', 'menu_id'])
908
+ keys(message, COMMAND_KEYS.close_context_menu)
901
909
  return {
902
910
  id: request,
903
911
  type: 'close_context_menu',
@@ -905,7 +913,7 @@ export function parseCommand(value: unknown): Command {
905
913
  menu_id: id(message['menu_id']),
906
914
  }
907
915
  case 'register_drop_target':
908
- keys(message, ['id', 'type', 'window_id', 'target_id', 'node_id'])
916
+ keys(message, COMMAND_KEYS.register_drop_target)
909
917
  return {
910
918
  id: request,
911
919
  type: 'register_drop_target',
@@ -914,7 +922,7 @@ export function parseCommand(value: unknown): Command {
914
922
  node_id: id(message['node_id']),
915
923
  }
916
924
  case 'unregister_drop_target':
917
- keys(message, ['id', 'type', 'window_id', 'target_id'])
925
+ keys(message, COMMAND_KEYS.unregister_drop_target)
918
926
  return {
919
927
  id: request,
920
928
  type: 'unregister_drop_target',
@@ -922,7 +930,7 @@ export function parseCommand(value: unknown): Command {
922
930
  target_id: id(message['target_id']),
923
931
  }
924
932
  case 'resolve_dropped_item':
925
- keys(message, ['id', 'type', 'window_id', 'target_id', 'drop_id', 'item', 'root'])
933
+ keys(message, COMMAND_KEYS.resolve_dropped_item)
926
934
  return {
927
935
  id: request,
928
936
  type: 'resolve_dropped_item',
@@ -933,7 +941,7 @@ export function parseCommand(value: unknown): Command {
933
941
  root: parseRootId(message['root']),
934
942
  }
935
943
  case 'set_app_menu':
936
- keys(message, ['id', 'type', 'menu_id', 'menus'])
944
+ keys(message, COMMAND_KEYS.set_app_menu)
937
945
  return {
938
946
  id: request,
939
947
  type: 'set_app_menu',
@@ -941,16 +949,18 @@ export function parseCommand(value: unknown): Command {
941
949
  menus: parseAppMenus(message['menus']),
942
950
  }
943
951
  case 'shutdown':
944
- keys(message, ['id', 'type'])
952
+ keys(message, COMMAND_KEYS.shutdown)
945
953
  return { id: request, type: 'shutdown' }
946
954
  default:
947
955
  throw new ProtocolError('Unknown native command')
948
956
  }
949
957
  }
950
958
 
959
+ const LIMITS_KEYS: ReadonlySet<string> = new Set(Object.keys(DEFAULT_LIMITS))
960
+
951
961
  function limits(value: unknown): Limits {
952
962
  const data = object(value)
953
- keys(data, Object.keys(DEFAULT_LIMITS))
963
+ keys(data, LIMITS_KEYS)
954
964
  const parse = (key: keyof Limits) => integer(data[key], key, 1, DEFAULT_LIMITS[key])
955
965
  return {
956
966
  max_frame_bytes: parse('max_frame_bytes'),
@@ -972,15 +982,23 @@ function limits(value: unknown): Limits {
972
982
  }
973
983
  }
974
984
 
975
- const ROUTE_KEYS = ['type', 'sequence', 'window_id', 'node_id', 'tree_revision', 'path'] as const
985
+ /** Short paths, the common case, compare pairwise instead of building a set. */
986
+ function unique(path: readonly number[]): boolean {
987
+ if (path.length > 16) return new Set(path).size === path.length
988
+ for (let index = 1; index < path.length; index++)
989
+ for (let other = 0; other < index; other++) if (path[index] === path[other]) return false
990
+ return true
991
+ }
976
992
 
977
993
  function route(message: Record<string, unknown>): NodeEventRoute {
978
994
  const node_id = id(message['node_id'])
979
995
  const value = message['path']
980
- if (!Array.isArray(value) || !value.length || value.length > MAX_DEPTH + 1)
981
- 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
+ )
982
1000
  const path = value.map(id)
983
- if (new Set(path).size !== path.length || path.at(-1) !== node_id)
1001
+ if (!unique(path) || path.at(-1) !== node_id)
984
1002
  throw new ProtocolError('Native event path must be unique and end at its target')
985
1003
  return {
986
1004
  sequence: id(message['sequence']),
@@ -993,7 +1011,7 @@ function route(message: Record<string, unknown>): NodeEventRoute {
993
1011
 
994
1012
  function modifiers(value: unknown): KeyModifiers {
995
1013
  const data = object(value)
996
- keys(data, ['control', 'alt', 'shift', 'platform', 'function'])
1014
+ keys(data, MODIFIER_KEYS)
997
1015
  const modifier = (name: keyof KeyModifiers) => {
998
1016
  if (typeof data[name] !== 'boolean') throw new ProtocolError('Invalid native key modifier')
999
1017
  return data[name]
@@ -1007,19 +1025,36 @@ function modifiers(value: unknown): KeyModifiers {
1007
1025
  }
1008
1026
  }
1009
1027
 
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
+ }
1039
+
1010
1040
  /** Native stdout is untrusted until its message shape and limits are checked. */
1011
1041
  export function parseEvent(value: unknown): NativeEvent {
1012
1042
  const message = object(value)
1013
- switch (message['type']) {
1043
+ const type = message['type']
1044
+ switch (type) {
1014
1045
  case 'ready': {
1015
- keys(message, ['type', 'protocol', 'capabilities', 'limits', 'service_policy'])
1046
+ keys(message, EVENT_KEYS.ready)
1016
1047
  const capabilities = message['capabilities']
1017
1048
  if (!Array.isArray(capabilities) || capabilities.length > CAPABILITIES.length)
1018
1049
  throw new ProtocolError('Invalid native capabilities')
1019
1050
  const parsed: Capability[] = []
1020
1051
  for (const value of capabilities) {
1021
- const capability = CAPABILITIES.find((candidate) => candidate === value)
1022
- 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))
1023
1058
  throw new ProtocolError('Unknown or duplicate native capability')
1024
1059
  parsed.push(capability)
1025
1060
  }
@@ -1032,10 +1067,10 @@ export function parseEvent(value: unknown): NativeEvent {
1032
1067
  }
1033
1068
  }
1034
1069
  case 'ack':
1035
- keys(message, ['type', 'id'])
1070
+ keys(message, EVENT_KEYS.ack)
1036
1071
  return { type: 'ack', id: id(message['id']) }
1037
1072
  case 'opened':
1038
- keys(message, ['type', 'id', 'window_id', 'root_id', 'tree_revision'])
1073
+ keys(message, EVENT_KEYS.opened)
1039
1074
  return {
1040
1075
  type: 'opened',
1041
1076
  id: id(message['id']),
@@ -1044,7 +1079,7 @@ export function parseEvent(value: unknown): NativeEvent {
1044
1079
  tree_revision: integer(message['tree_revision'], 'tree revision'),
1045
1080
  }
1046
1081
  case 'applied':
1047
- keys(message, ['type', 'id', 'window_id', 'tree_revision'])
1082
+ keys(message, EVENT_KEYS.applied)
1048
1083
  return {
1049
1084
  type: 'applied',
1050
1085
  id: id(message['id']),
@@ -1052,7 +1087,7 @@ export function parseEvent(value: unknown): NativeEvent {
1052
1087
  tree_revision: integer(message['tree_revision'], 'tree revision'),
1053
1088
  }
1054
1089
  case 'display_snapshot':
1055
- keys(message, ['type', 'id', 'window_id', 'snapshot'])
1090
+ keys(message, EVENT_KEYS.display_snapshot)
1056
1091
  return {
1057
1092
  type: 'display_snapshot',
1058
1093
  id: id(message['id']),
@@ -1060,7 +1095,7 @@ export function parseEvent(value: unknown): NativeEvent {
1060
1095
  snapshot: parseDisplaySnapshot(message['snapshot']),
1061
1096
  }
1062
1097
  case 'clipboard_text':
1063
- keys(message, ['type', 'id', 'window_id', 'result'])
1098
+ keys(message, EVENT_KEYS.clipboard_text)
1064
1099
  return {
1065
1100
  type: 'clipboard_text',
1066
1101
  id: id(message['id']),
@@ -1068,7 +1103,7 @@ export function parseEvent(value: unknown): NativeEvent {
1068
1103
  result: parseClipboardText(message['result']),
1069
1104
  }
1070
1105
  case 'clipboard_written':
1071
- keys(message, ['type', 'id', 'window_id'])
1106
+ keys(message, EVENT_KEYS.clipboard_written)
1072
1107
  return {
1073
1108
  type: 'clipboard_written',
1074
1109
  id: id(message['id']),
@@ -1076,15 +1111,15 @@ export function parseEvent(value: unknown): NativeEvent {
1076
1111
  }
1077
1112
  case 'global_shortcut_registered':
1078
1113
  case 'global_shortcut_unregistered':
1079
- keys(message, ['type', 'id', 'window_id', 'shortcut_id'])
1114
+ keys(message, EVENT_KEYS[type])
1080
1115
  return {
1081
- type: message['type'],
1116
+ type,
1082
1117
  id: id(message['id']),
1083
1118
  window_id: id(message['window_id']),
1084
1119
  shortcut_id: id(message['shortcut_id']),
1085
1120
  }
1086
1121
  case 'display_changed':
1087
- keys(message, ['type', 'sequence', 'window_id', 'snapshot'])
1122
+ keys(message, EVENT_KEYS.display_changed)
1088
1123
  return {
1089
1124
  type: 'display_changed',
1090
1125
  sequence: id(message['sequence']),
@@ -1092,7 +1127,7 @@ export function parseEvent(value: unknown): NativeEvent {
1092
1127
  snapshot: parseDisplaySnapshot(message['snapshot']),
1093
1128
  }
1094
1129
  case 'global_shortcut':
1095
- keys(message, ['type', 'sequence', 'window_id', 'shortcut_id'])
1130
+ keys(message, EVENT_KEYS.global_shortcut)
1096
1131
  return {
1097
1132
  type: 'global_shortcut',
1098
1133
  sequence: id(message['sequence']),
@@ -1100,7 +1135,7 @@ export function parseEvent(value: unknown): NativeEvent {
1100
1135
  shortcut_id: id(message['shortcut_id']),
1101
1136
  }
1102
1137
  case 'notification_posted':
1103
- keys(message, ['type', 'id', 'window_id', 'notification_id', 'activatable'])
1138
+ keys(message, EVENT_KEYS.notification_posted)
1104
1139
  if (typeof message['activatable'] !== 'boolean')
1105
1140
  throw new ProtocolError('Notification activatable must be a boolean')
1106
1141
  return {
@@ -1111,7 +1146,7 @@ export function parseEvent(value: unknown): NativeEvent {
1111
1146
  activatable: message['activatable'],
1112
1147
  }
1113
1148
  case 'notification_withdrawn':
1114
- keys(message, ['type', 'id', 'window_id', 'notification_id'])
1149
+ keys(message, EVENT_KEYS.notification_withdrawn)
1115
1150
  return {
1116
1151
  type: 'notification_withdrawn',
1117
1152
  id: id(message['id']),
@@ -1119,7 +1154,7 @@ export function parseEvent(value: unknown): NativeEvent {
1119
1154
  notification_id: id(message['notification_id']),
1120
1155
  }
1121
1156
  case 'notification_activated':
1122
- keys(message, ['type', 'sequence', 'window_id', 'notification_id'])
1157
+ keys(message, EVENT_KEYS.notification_activated)
1123
1158
  return {
1124
1159
  type: 'notification_activated',
1125
1160
  sequence: id(message['sequence']),
@@ -1127,7 +1162,7 @@ export function parseEvent(value: unknown): NativeEvent {
1127
1162
  notification_id: id(message['notification_id']),
1128
1163
  }
1129
1164
  case 'notification_dismissed':
1130
- keys(message, ['type', 'sequence', 'window_id', 'notification_id', 'reason'])
1165
+ keys(message, EVENT_KEYS.notification_dismissed)
1131
1166
  return {
1132
1167
  type: 'notification_dismissed',
1133
1168
  sequence: id(message['sequence']),
@@ -1137,15 +1172,15 @@ export function parseEvent(value: unknown): NativeEvent {
1137
1172
  }
1138
1173
  case 'file_grant_opened':
1139
1174
  case 'file_grant_closed':
1140
- keys(message, ['type', 'id', 'window_id', 'grant_id'])
1175
+ keys(message, EVENT_KEYS[type])
1141
1176
  return {
1142
- type: message['type'],
1177
+ type,
1143
1178
  id: id(message['id']),
1144
1179
  window_id: id(message['window_id']),
1145
1180
  grant_id: id(message['grant_id']),
1146
1181
  }
1147
1182
  case 'file_result':
1148
- keys(message, ['type', 'id', 'window_id', 'grant_id', 'result'])
1183
+ keys(message, EVENT_KEYS.file_result)
1149
1184
  return {
1150
1185
  type: 'file_result',
1151
1186
  id: id(message['id']),
@@ -1156,15 +1191,15 @@ export function parseEvent(value: unknown): NativeEvent {
1156
1191
  case 'process_spawned':
1157
1192
  case 'process_updated':
1158
1193
  case 'process_released':
1159
- keys(message, ['type', 'id', 'window_id', 'process_id'])
1194
+ keys(message, EVENT_KEYS[type])
1160
1195
  return {
1161
- type: message['type'],
1196
+ type,
1162
1197
  id: id(message['id']),
1163
1198
  window_id: id(message['window_id']),
1164
1199
  process_id: id(message['process_id']),
1165
1200
  }
1166
1201
  case 'process_output':
1167
- keys(message, ['type', 'sequence', 'window_id', 'process_id', 'chunk', 'stream', 'data'])
1202
+ keys(message, EVENT_KEYS.process_output)
1168
1203
  return {
1169
1204
  type: 'process_output',
1170
1205
  sequence: id(message['sequence']),
@@ -1175,7 +1210,7 @@ export function parseEvent(value: unknown): NativeEvent {
1175
1210
  data: parseProcessOutput(message['data']),
1176
1211
  }
1177
1212
  case 'process_exited':
1178
- keys(message, ['type', 'sequence', 'window_id', 'process_id', 'exit'])
1213
+ keys(message, EVENT_KEYS.process_exited)
1179
1214
  return {
1180
1215
  type: 'process_exited',
1181
1216
  sequence: id(message['sequence']),
@@ -1185,15 +1220,15 @@ export function parseEvent(value: unknown): NativeEvent {
1185
1220
  }
1186
1221
  case 'file_dialog_opened':
1187
1222
  case 'file_dialog_closed':
1188
- keys(message, ['type', 'id', 'window_id', 'dialog_id'])
1223
+ keys(message, EVENT_KEYS[type])
1189
1224
  return {
1190
- type: message['type'],
1225
+ type,
1191
1226
  id: id(message['id']),
1192
1227
  window_id: id(message['window_id']),
1193
1228
  dialog_id: id(message['dialog_id']),
1194
1229
  }
1195
1230
  case 'file_dialog_completed':
1196
- keys(message, ['type', 'sequence', 'window_id', 'dialog_id', 'result'])
1231
+ keys(message, EVENT_KEYS.file_dialog_completed)
1197
1232
  return {
1198
1233
  type: 'file_dialog_completed',
1199
1234
  sequence: id(message['sequence']),
@@ -1203,15 +1238,15 @@ export function parseEvent(value: unknown): NativeEvent {
1203
1238
  }
1204
1239
  case 'message_dialog_opened':
1205
1240
  case 'message_dialog_closed':
1206
- keys(message, ['type', 'id', 'window_id', 'dialog_id'])
1241
+ keys(message, EVENT_KEYS[type])
1207
1242
  return {
1208
- type: message['type'],
1243
+ type,
1209
1244
  id: id(message['id']),
1210
1245
  window_id: id(message['window_id']),
1211
1246
  dialog_id: id(message['dialog_id']),
1212
1247
  }
1213
1248
  case 'message_dialog_completed':
1214
- keys(message, ['type', 'sequence', 'window_id', 'dialog_id', 'result'])
1249
+ keys(message, EVENT_KEYS.message_dialog_completed)
1215
1250
  return {
1216
1251
  type: 'message_dialog_completed',
1217
1252
  sequence: id(message['sequence']),
@@ -1221,15 +1256,15 @@ export function parseEvent(value: unknown): NativeEvent {
1221
1256
  }
1222
1257
  case 'context_menu_opened':
1223
1258
  case 'context_menu_closed':
1224
- keys(message, ['type', 'id', 'window_id', 'menu_id'])
1259
+ keys(message, EVENT_KEYS[type])
1225
1260
  return {
1226
- type: message['type'],
1261
+ type,
1227
1262
  id: id(message['id']),
1228
1263
  window_id: id(message['window_id']),
1229
1264
  menu_id: id(message['menu_id']),
1230
1265
  }
1231
1266
  case 'context_menu_completed': {
1232
- keys(message, ['type', 'sequence', 'window_id', 'menu_id', 'item_id'])
1267
+ keys(message, EVENT_KEYS.context_menu_completed)
1233
1268
  const event: Extract<NativeEvent, { type: 'context_menu_completed' }> = {
1234
1269
  type: 'context_menu_completed',
1235
1270
  sequence: id(message['sequence']),
@@ -1241,15 +1276,15 @@ export function parseEvent(value: unknown): NativeEvent {
1241
1276
  }
1242
1277
  case 'drop_target_registered':
1243
1278
  case 'drop_target_unregistered':
1244
- keys(message, ['type', 'id', 'window_id', 'target_id'])
1279
+ keys(message, EVENT_KEYS[type])
1245
1280
  return {
1246
- type: message['type'],
1281
+ type,
1247
1282
  id: id(message['id']),
1248
1283
  window_id: id(message['window_id']),
1249
1284
  target_id: id(message['target_id']),
1250
1285
  }
1251
1286
  case 'dropped_item_resolved':
1252
- keys(message, ['type', 'id', 'window_id', 'target_id', 'drop_id', 'item', 'root', 'path'])
1287
+ keys(message, EVENT_KEYS.dropped_item_resolved)
1253
1288
  return {
1254
1289
  type: 'dropped_item_resolved',
1255
1290
  id: id(message['id']),
@@ -1261,7 +1296,7 @@ export function parseEvent(value: unknown): NativeEvent {
1261
1296
  path: parseFilePath(message['path']),
1262
1297
  }
1263
1298
  case 'drop_target_drag':
1264
- keys(message, ['type', 'sequence', 'window_id', 'target_id', 'state'])
1299
+ keys(message, EVENT_KEYS.drop_target_drag)
1265
1300
  return {
1266
1301
  type: 'drop_target_drag',
1267
1302
  sequence: id(message['sequence']),
@@ -1270,7 +1305,7 @@ export function parseEvent(value: unknown): NativeEvent {
1270
1305
  state: parseDragState(message['state']),
1271
1306
  }
1272
1307
  case 'drop_received':
1273
- keys(message, ['type', 'sequence', 'window_id', 'target_id', 'drop_id', 'items'])
1308
+ keys(message, EVENT_KEYS.drop_received)
1274
1309
  return {
1275
1310
  type: 'drop_received',
1276
1311
  sequence: id(message['sequence']),
@@ -1280,7 +1315,7 @@ export function parseEvent(value: unknown): NativeEvent {
1280
1315
  items: parseDroppedItems(message['items']),
1281
1316
  }
1282
1317
  case 'drop_rejected':
1283
- keys(message, ['type', 'sequence', 'window_id', 'target_id', 'reason'])
1318
+ keys(message, EVENT_KEYS.drop_rejected)
1284
1319
  return {
1285
1320
  type: 'drop_rejected',
1286
1321
  sequence: id(message['sequence']),
@@ -1291,69 +1326,68 @@ export function parseEvent(value: unknown): NativeEvent {
1291
1326
  case 'click':
1292
1327
  case 'focus':
1293
1328
  case 'blur':
1294
- keys(message, 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
+ )
1295
1335
  return {
1296
- type: message['type'],
1336
+ type,
1297
1337
  ...route(message),
1298
1338
  }
1299
1339
  case 'key_down':
1300
1340
  case 'key_up': {
1301
- keys(message, [...ROUTE_KEYS, 'key', 'key_char', 'modifiers'])
1302
- 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)
1303
1343
  if (!key) throw new ProtocolError('Native key must be nonempty')
1304
1344
  return {
1305
- type: message['type'],
1345
+ type,
1306
1346
  ...route(message),
1307
1347
  key,
1308
1348
  ...('key_char' in message
1309
- ? { key_char: string(message['key_char'], 'key_char', 256) }
1349
+ ? { key_char: string(message['key_char'], 'key_char', MAX_KEY_BYTES) }
1310
1350
  : {}),
1311
1351
  modifiers: modifiers(message['modifiers']),
1312
1352
  }
1313
1353
  }
1314
1354
  case 'pointer_down':
1315
1355
  case 'pointer_up': {
1316
- keys(message, [...ROUTE_KEYS, 'button', 'x', 'y', 'modifiers'])
1317
- const button = (['left', 'right', 'middle', 'back', 'forward'] as const).find(
1318
- (button) => button === message['button']
1319
- )
1320
- if (!button) throw new ProtocolError('Unknown native pointer button')
1356
+ keys(message, type === 'pointer_down' ? EVENT_KEYS.pointer_down : EVENT_KEYS.pointer_up)
1321
1357
  return {
1322
- type: message['type'],
1358
+ type,
1323
1359
  ...route(message),
1324
- button,
1325
- x: number(message['x'], 'pointer x', -1_048_576, 1_048_576),
1326
- 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),
1327
1363
  modifiers: modifiers(message['modifiers']),
1328
1364
  }
1329
1365
  }
1330
1366
  case 'dismiss':
1331
- keys(message, [...ROUTE_KEYS, 'reason'])
1332
- if (message['reason'] !== 'window_blur')
1333
- throw new ProtocolError('Unknown native dismissal reason')
1334
- 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
+ }
1335
1373
  case 'split_resize': {
1336
- keys(message, [...ROUTE_KEYS, 'ratio', 'source'])
1337
- const source = (['pointer', 'keyboard'] as const).find(
1338
- (source) => source === message['source']
1339
- )
1340
- if (!source) throw new ProtocolError('Unknown split resize source')
1374
+ keys(message, EVENT_KEYS.split_resize)
1341
1375
  return {
1342
1376
  type: 'split_resize',
1343
1377
  ...route(message),
1344
1378
  ratio: number(message['ratio'], 'split ratio', 0, 1),
1345
- source,
1379
+ source: member(SOURCES, message['source'], 'Unknown split resize source'),
1346
1380
  }
1347
1381
  }
1348
1382
  case 'closed':
1349
- keys(message, ['type', 'sequence', 'window_id'])
1383
+ keys(message, EVENT_KEYS.closed)
1350
1384
  return {
1351
1385
  type: 'closed',
1352
1386
  sequence: id(message['sequence']),
1353
1387
  window_id: id(message['window_id']),
1354
1388
  }
1355
1389
  case 'input':
1356
- keys(message, [...ROUTE_KEYS, 'value', 'input_revision'])
1390
+ keys(message, EVENT_KEYS.input)
1357
1391
  return {
1358
1392
  type: 'input',
1359
1393
  ...route(message),
@@ -1361,10 +1395,10 @@ export function parseEvent(value: unknown): NativeEvent {
1361
1395
  input_revision: integer(message['input_revision'], 'input revision'),
1362
1396
  }
1363
1397
  case 'app_menu_set':
1364
- keys(message, ['type', 'id', 'menu_id'])
1398
+ keys(message, EVENT_KEYS.app_menu_set)
1365
1399
  return { type: 'app_menu_set', id: id(message['id']), menu_id: id(message['menu_id']) }
1366
1400
  case 'app_menu_item':
1367
- keys(message, ['type', 'sequence', 'menu_id', 'item_id'])
1401
+ keys(message, EVENT_KEYS.app_menu_item)
1368
1402
  return {
1369
1403
  type: 'app_menu_item',
1370
1404
  sequence: id(message['sequence']),
@@ -1372,12 +1406,10 @@ export function parseEvent(value: unknown): NativeEvent {
1372
1406
  item_id: parseAppMenuItemId(message['item_id']),
1373
1407
  }
1374
1408
  case 'error': {
1375
- keys(message, ['type', 'id', 'code', 'message'])
1376
- const code = ERROR_CODES.find((code) => code === message['code'])
1377
- if (!code) throw new ProtocolError('Invalid native error code')
1409
+ keys(message, EVENT_KEYS.error)
1378
1410
  return {
1379
1411
  type: 'error',
1380
- code,
1412
+ code: member(CODES, message['code'], 'Invalid native error code'),
1381
1413
  message: string(message['message'], 'message'),
1382
1414
  ...('id' in message ? { id: id(message['id']) } : {}),
1383
1415
  }