@bun-native/protocol 0.33.1 → 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/processes.ts CHANGED
@@ -1,24 +1,25 @@
1
1
  import { ProtocolError } from './framing.ts'
2
2
  import { parseChunk, parseFilePath, parseRootId, parseRootPath } from './files.ts'
3
- import { fields, id, integer, keys, object } from './values.ts'
3
+ import { id, integer, keys, object } from './values.ts'
4
+ import {
5
+ MAX_PROCESS_ARGS,
6
+ MAX_PROCESS_ARG_BYTES,
7
+ MAX_PROCESS_CPU_SECONDS,
8
+ MAX_PROCESS_ENV,
9
+ MAX_PROCESS_ENV_NAME_BYTES,
10
+ MAX_PROCESS_ENV_VALUE_BYTES,
11
+ MAX_PROCESS_EXECUTABLES,
12
+ MAX_PROCESS_LAUNCH_BYTES,
13
+ MAX_PROCESS_MEMORY_BYTES,
14
+ MAX_PROCESS_OPEN_FILES,
15
+ MAX_TERMINAL_DIMENSION,
16
+ MIN_PROCESS_MEMORY_BYTES,
17
+ MIN_PROCESS_OPEN_FILES,
18
+ PAYLOAD_KEYS,
19
+ } from './wire-schema.ts'
4
20
 
5
- export const MAX_PROCESS_EXECUTABLES = 8
6
- export const MAX_PROCESSES = 8
7
- export const MAX_PROCESS_ARGS = 64
8
- export const MAX_PROCESS_ENV = 64
9
- export const MAX_PROCESS_OUTPUT_CREDIT = 4
10
- const MAX_PROCESS_ARG_BYTES = 4_096
11
- const MAX_PROCESS_ENV_NAME_BYTES = 128
12
- const MAX_PROCESS_ENV_VALUE_BYTES = 4_096
13
- const MAX_PROCESS_LAUNCH_BYTES = 32_768
14
- const MAX_TERMINAL_DIMENSION = 1_000
15
21
  const PROCESS_ARGUMENT = '--bun-native-process='
16
22
  const PROCESS_LIMITS_ARGUMENT = '--bun-native-process-limits='
17
- const MAX_PROCESS_CPU_SECONDS = 604_800
18
- const MIN_PROCESS_MEMORY_BYTES = 2 ** 20
19
- const MAX_PROCESS_MEMORY_BYTES = 2 ** 48
20
- const MIN_PROCESS_OPEN_FILES = 3
21
- const MAX_PROCESS_OPEN_FILES = 2 ** 20
22
23
  const encoder = new TextEncoder()
23
24
  const bytes = (value: string) => encoder.encode(value).byteLength
24
25
 
@@ -48,11 +49,9 @@ const LIMIT_BOUNDS = {
48
49
  open_files: [MIN_PROCESS_OPEN_FILES, MAX_PROCESS_OPEN_FILES],
49
50
  } as const
50
51
 
51
- const PROCESS_LIMITS_KEYS = fields(...LIMIT_NAMES)
52
-
53
52
  function parseProcessLimits(value: unknown): ProcessLimits {
54
53
  const data = object(value)
55
- keys(data, PROCESS_LIMITS_KEYS)
54
+ keys(data, PAYLOAD_KEYS.process_limits)
56
55
  const limits: ProcessLimits = {}
57
56
  for (const name of LIMIT_NAMES)
58
57
  if (name in data) {
@@ -105,11 +104,9 @@ export type ProcessSignal = (typeof PROCESS_SIGNALS)[number]
105
104
  export type ProcessStream = 'stdout' | 'stderr' | 'terminal'
106
105
  export type ProcessExit = { code: number } | { signal: number }
107
106
 
108
- const EXECUTABLE_KEYS = fields('id', 'executable', 'limits')
109
-
110
107
  function executable(value: unknown): ProcessExecutable {
111
108
  const data = object(value)
112
- keys(data, EXECUTABLE_KEYS)
109
+ keys(data, PAYLOAD_KEYS.process_executable)
113
110
  const path = parseRootPath(data['executable'])
114
111
  // `/` and `X:\` are the only normalized paths ending in a separator.
115
112
  if (path.endsWith('/') || path.endsWith('\\'))
@@ -173,11 +170,9 @@ function plain(value: unknown, limit: number, field: string): string {
173
170
  return value
174
171
  }
175
172
 
176
- const TERMINAL_SIZE_KEYS = fields('columns', 'rows')
177
-
178
173
  export function parseTerminalSize(value: unknown): TerminalSize {
179
174
  const data = object(value)
180
- keys(data, TERMINAL_SIZE_KEYS)
175
+ keys(data, PAYLOAD_KEYS.terminal_size)
181
176
  return {
182
177
  columns: integer(data['columns'], 'terminal columns', 1, MAX_TERMINAL_DIMENSION),
183
178
  rows: integer(data['rows'], 'terminal rows', 1, MAX_TERMINAL_DIMENSION),
@@ -192,12 +187,9 @@ function parseEnvName(value: string): string {
192
187
  return value
193
188
  }
194
189
 
195
- const PROCESS_LAUNCH_KEYS = fields('executable', 'args', 'env', 'cwd', 'terminal')
196
- const PROCESS_CWD_KEYS = fields('grant_id', 'path')
197
-
198
190
  export function parseProcessLaunch(value: unknown): ProcessLaunch {
199
191
  const data = object(value)
200
- keys(data, PROCESS_LAUNCH_KEYS)
192
+ keys(data, PAYLOAD_KEYS.process_launch)
201
193
  const args = data['args']
202
194
  if (!Array.isArray(args) || args.length > MAX_PROCESS_ARGS)
203
195
  throw new ProtocolError('Process arguments must be an array of at most ' + MAX_PROCESS_ARGS)
@@ -225,7 +217,7 @@ export function parseProcessLaunch(value: unknown): ProcessLaunch {
225
217
  }
226
218
  if ('cwd' in data) {
227
219
  const cwd = object(data['cwd'])
228
- keys(cwd, PROCESS_CWD_KEYS)
220
+ keys(cwd, PAYLOAD_KEYS.process_directory)
229
221
  launch.cwd = { grant_id: id(cwd['grant_id']), path: parseFilePath(cwd['path']) }
230
222
  }
231
223
  if ('terminal' in data) launch.terminal = parseTerminalSize(data['terminal'])
@@ -250,11 +242,9 @@ export function parseProcessOutput(value: unknown): string {
250
242
  return data
251
243
  }
252
244
 
253
- const PROCESS_EXIT_KEYS = fields('code', 'signal')
254
-
255
245
  export function parseProcessExit(value: unknown): ProcessExit {
256
246
  const data = object(value)
257
- keys(data, PROCESS_EXIT_KEYS)
247
+ keys(data, PAYLOAD_KEYS.process_exit)
258
248
  if ('code' in data && !('signal' in data))
259
249
  return { code: integer(data['code'], 'exit code', 0, 255) }
260
250
  if ('signal' in data && !('code' in data))
package/src/services.ts CHANGED
@@ -1,15 +1,14 @@
1
1
  import { ProtocolError } from './framing.ts'
2
- import { fields, keys, number, object, string } from './values.ts'
2
+ import { keys, number, object, string } from './values.ts'
3
3
  import { parseFilesystemRoots, type FilesystemRoot } from './files.ts'
4
4
  import { parseProcessExecutables, type ProcessExecutable } from './processes.ts'
5
-
6
- // Worst-case JSON escaping still leaves room within the 64KiB frame.
7
- export const MAX_CLIPBOARD_BYTES = 8_192
8
- export const MAX_SERVICE_REQUESTS = 8
9
- export const MAX_GLOBAL_SHORTCUTS = 16
10
- export const MAX_NOTIFICATIONS = 32
11
- export const MAX_NOTIFICATION_TITLE_BYTES = 256
12
- export const MAX_NOTIFICATION_BODY_BYTES = 2_048
5
+ import {
6
+ MAX_CLIPBOARD_BYTES,
7
+ MAX_DISPLAY_COORDINATE,
8
+ MAX_NOTIFICATION_BODY_BYTES,
9
+ MAX_NOTIFICATION_TITLE_BYTES,
10
+ PAYLOAD_KEYS,
11
+ } from './wire-schema.ts'
13
12
 
14
13
  export interface ServicePolicy {
15
14
  window_display: boolean
@@ -53,11 +52,9 @@ export interface ShortcutModifiers {
53
52
  platform: boolean
54
53
  }
55
54
 
56
- const SHORTCUT_MODIFIERS_KEYS = fields('control', 'alt', 'shift', 'platform')
57
-
58
55
  export function parseShortcutModifiers(value: unknown): ShortcutModifiers {
59
56
  const data = object(value)
60
- keys(data, SHORTCUT_MODIFIERS_KEYS)
57
+ keys(data, PAYLOAD_KEYS.shortcut_modifiers)
61
58
  const flag = (key: keyof ShortcutModifiers) => {
62
59
  if (typeof data[key] !== 'boolean')
63
60
  throw new ProtocolError('Global shortcut modifiers must state each modifier explicitly')
@@ -105,16 +102,13 @@ export function parseNotificationDismissal(value: unknown): NotificationDismissa
105
102
 
106
103
  export type ClipboardText = { kind: 'empty' } | { kind: 'text'; text: string }
107
104
 
108
- const CLIPBOARD_TEXT_EMPTY_KEYS = fields('kind')
109
- const CLIPBOARD_TEXT_READ_KEYS = fields('kind', 'text')
110
-
111
105
  export function parseClipboardText(value: unknown): ClipboardText {
112
106
  const data = object(value)
113
107
  if (data['kind'] === 'empty') {
114
- keys(data, CLIPBOARD_TEXT_EMPTY_KEYS)
108
+ keys(data, PAYLOAD_KEYS.clipboard_text.empty)
115
109
  return { kind: 'empty' }
116
110
  }
117
- keys(data, CLIPBOARD_TEXT_READ_KEYS)
111
+ keys(data, PAYLOAD_KEYS.clipboard_text.text)
118
112
  if (data['kind'] !== 'text') throw new ProtocolError('Unknown clipboard text result')
119
113
  return { kind: 'text', text: string(data['text'], 'clipboard text', MAX_CLIPBOARD_BYTES) }
120
114
  }
@@ -137,36 +131,20 @@ export interface DisplaySnapshot {
137
131
 
138
132
  const DISPLAY_UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
139
133
 
140
- const DISPLAY_BOUNDS_KEYS = fields('x', 'y', 'width', 'height')
141
-
142
134
  function displayBounds(value: unknown, name: string): DisplayBounds {
143
135
  const bounds = object(value)
144
- keys(bounds, DISPLAY_BOUNDS_KEYS)
136
+ keys(bounds, PAYLOAD_KEYS.display_bounds)
145
137
  return {
146
- x: number(bounds['x'], name + ' x', -1_048_576, 1_048_576),
147
- y: number(bounds['y'], name + ' y', -1_048_576, 1_048_576),
148
- width: number(bounds['width'], name + ' width', Number.MIN_VALUE, 1_048_576),
149
- height: number(bounds['height'], name + ' height', Number.MIN_VALUE, 1_048_576),
138
+ x: number(bounds['x'], name + ' x', -MAX_DISPLAY_COORDINATE, MAX_DISPLAY_COORDINATE),
139
+ y: number(bounds['y'], name + ' y', -MAX_DISPLAY_COORDINATE, MAX_DISPLAY_COORDINATE),
140
+ width: number(bounds['width'], name + ' width', Number.MIN_VALUE, MAX_DISPLAY_COORDINATE),
141
+ height: number(bounds['height'], name + ' height', Number.MIN_VALUE, MAX_DISPLAY_COORDINATE),
150
142
  }
151
143
  }
152
144
 
153
- const SERVICE_POLICY_KEYS = fields(
154
- 'window_display',
155
- 'clipboard_read',
156
- 'clipboard_write',
157
- 'global_shortcuts',
158
- 'notifications',
159
- 'drag_drop',
160
- 'filesystem_roots',
161
- 'processes',
162
- 'file_dialogs',
163
- 'app_menu',
164
- 'task_dialogs'
165
- )
166
-
167
145
  export function parseServicePolicy(value: unknown): ServicePolicy {
168
146
  const data = object(value)
169
- keys(data, SERVICE_POLICY_KEYS)
147
+ keys(data, PAYLOAD_KEYS.service_policy)
170
148
  const grant = (key: keyof ServicePolicy) => {
171
149
  if (typeof data[key] !== 'boolean')
172
150
  throw new ProtocolError('Native service policy must explicitly grant or deny each operation')
@@ -187,11 +165,9 @@ export function parseServicePolicy(value: unknown): ServicePolicy {
187
165
  }
188
166
  }
189
167
 
190
- const DISPLAY_SNAPSHOT_KEYS = fields('bounds', 'scale_factor', 'visible_bounds', 'display_uuid')
191
-
192
168
  export function parseDisplaySnapshot(value: unknown): DisplaySnapshot {
193
169
  const data = object(value)
194
- keys(data, DISPLAY_SNAPSHOT_KEYS)
170
+ keys(data, PAYLOAD_KEYS.display_snapshot)
195
171
  const snapshot: DisplaySnapshot = {
196
172
  bounds: displayBounds(data['bounds'], 'display'),
197
173
  scale_factor: number(data['scale_factor'], 'display scale factor', 0.125, 16),
package/src/values.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { ProtocolError } from './framing.ts'
2
+ import { MAX_TEXT_BYTES } from './wire-schema.ts'
2
3
 
3
- export const MAX_TEXT_BYTES = 16_384
4
- export const MAX_TITLE_BYTES = 256
5
4
  const encoder = new TextEncoder()
6
5
 
7
6
  export function object(value: unknown): Record<string, unknown> {
@@ -10,11 +9,6 @@ export function object(value: unknown): Record<string, unknown> {
10
9
  return value as Record<string, unknown>
11
10
  }
12
11
 
13
- /** A message's accepted property names, hoisted to module scope so validation allocates nothing. */
14
- export function fields(...names: readonly string[]): ReadonlySet<string> {
15
- return new Set(names)
16
- }
17
-
18
12
  /** Reject own enumerable properties outside `allowed`; inherited keys are ignored. */
19
13
  export function keys(value: Record<string, unknown>, allowed: ReadonlySet<string>): void {
20
14
  for (const key of Object.keys(value))
@@ -39,6 +33,8 @@ export function integer(
39
33
  }
40
34
 
41
35
  export function id(value: unknown): number {
36
+ // The common valid case without the generic range check's arguments.
37
+ if (Number.isSafeInteger(value) && (value as number) >= 1) return value as number
42
38
  return integer(value, 'Identifier', 1)
43
39
  }
44
40
 
@@ -59,3 +55,9 @@ export function string(value: unknown, field: string, limit = MAX_TEXT_BYTES): s
59
55
  if (value.includes('\0')) throw new ProtocolError(field + ' contains a NUL character')
60
56
  return value
61
57
  }
58
+
59
+ /** One of a value list's members, compared by identity. */
60
+ export function choice<T extends string>(value: unknown, field: string, values: readonly T[]): T {
61
+ for (const candidate of values) if (candidate === value) return candidate
62
+ throw new ProtocolError(field + ' must be one of ' + values.join(', '))
63
+ }