@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/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
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,9 +49,9 @@ const LIMIT_BOUNDS = {
48
49
  open_files: [MIN_PROCESS_OPEN_FILES, MAX_PROCESS_OPEN_FILES],
49
50
  } as const
50
51
 
51
- export function parseProcessLimits(value: unknown): ProcessLimits {
52
+ function parseProcessLimits(value: unknown): ProcessLimits {
52
53
  const data = object(value)
53
- keys(data, [...LIMIT_NAMES])
54
+ keys(data, PAYLOAD_KEYS.process_limits)
54
55
  const limits: ProcessLimits = {}
55
56
  for (const name of LIMIT_NAMES)
56
57
  if (name in data) {
@@ -105,7 +106,7 @@ export type ProcessExit = { code: number } | { signal: number }
105
106
 
106
107
  function executable(value: unknown): ProcessExecutable {
107
108
  const data = object(value)
108
- keys(data, ['id', 'executable', 'limits'])
109
+ keys(data, PAYLOAD_KEYS.process_executable)
109
110
  const path = parseRootPath(data['executable'])
110
111
  // `/` and `X:\` are the only normalized paths ending in a separator.
111
112
  if (path.endsWith('/') || path.endsWith('\\'))
@@ -171,7 +172,7 @@ function plain(value: unknown, limit: number, field: string): string {
171
172
 
172
173
  export function parseTerminalSize(value: unknown): TerminalSize {
173
174
  const data = object(value)
174
- keys(data, ['columns', 'rows'])
175
+ keys(data, PAYLOAD_KEYS.terminal_size)
175
176
  return {
176
177
  columns: integer(data['columns'], 'terminal columns', 1, MAX_TERMINAL_DIMENSION),
177
178
  rows: integer(data['rows'], 'terminal rows', 1, MAX_TERMINAL_DIMENSION),
@@ -188,7 +189,7 @@ function parseEnvName(value: string): string {
188
189
 
189
190
  export function parseProcessLaunch(value: unknown): ProcessLaunch {
190
191
  const data = object(value)
191
- keys(data, ['executable', 'args', 'env', 'cwd', 'terminal'])
192
+ keys(data, PAYLOAD_KEYS.process_launch)
192
193
  const args = data['args']
193
194
  if (!Array.isArray(args) || args.length > MAX_PROCESS_ARGS)
194
195
  throw new ProtocolError('Process arguments must be an array of at most ' + MAX_PROCESS_ARGS)
@@ -216,7 +217,7 @@ export function parseProcessLaunch(value: unknown): ProcessLaunch {
216
217
  }
217
218
  if ('cwd' in data) {
218
219
  const cwd = object(data['cwd'])
219
- keys(cwd, ['grant_id', 'path'])
220
+ keys(cwd, PAYLOAD_KEYS.process_directory)
220
221
  launch.cwd = { grant_id: id(cwd['grant_id']), path: parseFilePath(cwd['path']) }
221
222
  }
222
223
  if ('terminal' in data) launch.terminal = parseTerminalSize(data['terminal'])
@@ -243,7 +244,7 @@ export function parseProcessOutput(value: unknown): string {
243
244
 
244
245
  export function parseProcessExit(value: unknown): ProcessExit {
245
246
  const data = object(value)
246
- keys(data, ['code', 'signal'])
247
+ keys(data, PAYLOAD_KEYS.process_exit)
247
248
  if ('code' in data && !('signal' in data))
248
249
  return { code: integer(data['code'], 'exit code', 0, 255) }
249
250
  if ('signal' in data && !('code' in data))
package/src/services.ts CHANGED
@@ -2,14 +2,13 @@ import { ProtocolError } from './framing.ts'
2
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
@@ -55,7 +54,7 @@ export interface ShortcutModifiers {
55
54
 
56
55
  export function parseShortcutModifiers(value: unknown): ShortcutModifiers {
57
56
  const data = object(value)
58
- keys(data, ['control', 'alt', 'shift', 'platform'])
57
+ keys(data, PAYLOAD_KEYS.shortcut_modifiers)
59
58
  const flag = (key: keyof ShortcutModifiers) => {
60
59
  if (typeof data[key] !== 'boolean')
61
60
  throw new ProtocolError('Global shortcut modifiers must state each modifier explicitly')
@@ -106,10 +105,10 @@ export type ClipboardText = { kind: 'empty' } | { kind: 'text'; text: string }
106
105
  export function parseClipboardText(value: unknown): ClipboardText {
107
106
  const data = object(value)
108
107
  if (data['kind'] === 'empty') {
109
- keys(data, ['kind'])
108
+ keys(data, PAYLOAD_KEYS.clipboard_text.empty)
110
109
  return { kind: 'empty' }
111
110
  }
112
- keys(data, ['kind', 'text'])
111
+ keys(data, PAYLOAD_KEYS.clipboard_text.text)
113
112
  if (data['kind'] !== 'text') throw new ProtocolError('Unknown clipboard text result')
114
113
  return { kind: 'text', text: string(data['text'], 'clipboard text', MAX_CLIPBOARD_BYTES) }
115
114
  }
@@ -134,30 +133,18 @@ const DISPLAY_UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{
134
133
 
135
134
  function displayBounds(value: unknown, name: string): DisplayBounds {
136
135
  const bounds = object(value)
137
- keys(bounds, ['x', 'y', 'width', 'height'])
136
+ keys(bounds, PAYLOAD_KEYS.display_bounds)
138
137
  return {
139
- x: number(bounds['x'], name + ' x', -1_048_576, 1_048_576),
140
- y: number(bounds['y'], name + ' y', -1_048_576, 1_048_576),
141
- width: number(bounds['width'], name + ' width', Number.MIN_VALUE, 1_048_576),
142
- 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),
143
142
  }
144
143
  }
145
144
 
146
145
  export function parseServicePolicy(value: unknown): ServicePolicy {
147
146
  const data = object(value)
148
- keys(data, [
149
- 'window_display',
150
- 'clipboard_read',
151
- 'clipboard_write',
152
- 'global_shortcuts',
153
- 'notifications',
154
- 'drag_drop',
155
- 'filesystem_roots',
156
- 'processes',
157
- 'file_dialogs',
158
- 'app_menu',
159
- 'task_dialogs',
160
- ])
147
+ keys(data, PAYLOAD_KEYS.service_policy)
161
148
  const grant = (key: keyof ServicePolicy) => {
162
149
  if (typeof data[key] !== 'boolean')
163
150
  throw new ProtocolError('Native service policy must explicitly grant or deny each operation')
@@ -180,7 +167,7 @@ export function parseServicePolicy(value: unknown): ServicePolicy {
180
167
 
181
168
  export function parseDisplaySnapshot(value: unknown): DisplaySnapshot {
182
169
  const data = object(value)
183
- keys(data, ['bounds', 'scale_factor', 'visible_bounds', 'display_uuid'])
170
+ keys(data, PAYLOAD_KEYS.display_snapshot)
184
171
  const snapshot: DisplaySnapshot = {
185
172
  bounds: displayBounds(data['bounds'], 'display'),
186
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,9 +9,10 @@ export function object(value: unknown): Record<string, unknown> {
10
9
  return value as Record<string, unknown>
11
10
  }
12
11
 
13
- export function keys(value: Record<string, unknown>, allowed: readonly string[]): void {
12
+ /** Reject own enumerable properties outside `allowed`; inherited keys are ignored. */
13
+ export function keys(value: Record<string, unknown>, allowed: ReadonlySet<string>): void {
14
14
  for (const key of Object.keys(value))
15
- if (!allowed.includes(key)) throw new ProtocolError('Unknown message property: ' + key)
15
+ if (!allowed.has(key)) throw new ProtocolError('Unknown message property: ' + key)
16
16
  }
17
17
 
18
18
  export function number(value: unknown, field: string, min: number, max: number): number {
@@ -33,6 +33,8 @@ export function integer(
33
33
  }
34
34
 
35
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
36
38
  return integer(value, 'Identifier', 1)
37
39
  }
38
40
 
@@ -53,3 +55,9 @@ export function string(value: unknown, field: string, limit = MAX_TEXT_BYTES): s
53
55
  if (value.includes('\0')) throw new ProtocolError(field + ' contains a NUL character')
54
56
  return value
55
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
+ }