@bun-native/runtime 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/README.md +1 -1
- package/bun-native-source.json +14 -14
- package/package.json +2 -2
- package/src/exit-diagnostic.ts +19 -11
- package/src/index.ts +134 -334
- package/src/startup-trace.ts +14 -4
package/README.md
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
|
|
3
3
|
Bun Native host process lifecycle and bridge runtime.
|
|
4
4
|
|
|
5
|
-
Part of [Bun Native](https://github.com/0xPlayerOne/bun-native), an experimental developer preview. Version 0.33.
|
|
5
|
+
Part of [Bun Native](https://github.com/0xPlayerOne/bun-native), an experimental developer preview. Version 0.33.2 was built from [577f6fc5015b](https://github.com/0xPlayerOne/bun-native/tree/577f6fc5015b6eeef956efcc63f97e2a749d74ff) and published from GitHub Actions with npm provenance. Use the Bun revision pinned in that commit's toolchain.json. Start with the [getting-started guide](https://github.com/0xPlayerOne/bun-native/blob/577f6fc5015b6eeef956efcc63f97e2a749d74ff/docs/getting-started.md); `bun-native host install` fetches the matching release host. The license notice is in LICENSE.
|
package/bun-native-source.json
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schemaVersion": 1,
|
|
3
|
-
"commit": "
|
|
4
|
-
"tree": "
|
|
3
|
+
"commit": "577f6fc5015b6eeef956efcc63f97e2a749d74ff",
|
|
4
|
+
"tree": "c599e781af2ce0749d5e3a6d8700834690e872f4",
|
|
5
5
|
"files": {
|
|
6
6
|
"package.json": {
|
|
7
|
-
"bytes":
|
|
8
|
-
"sha256": "
|
|
7
|
+
"bytes": 2258,
|
|
8
|
+
"sha256": "38e2f0f89b5f6c4ba80b3baefcdf0806dd73c684294153df74ab470fda6bf3f9"
|
|
9
9
|
},
|
|
10
10
|
"packages/runtime/package.json": {
|
|
11
11
|
"bytes": 195,
|
|
12
12
|
"sha256": "30d8e6684ee94e266b3bc63d151f718a5d0a8804b2155c11f74de3e7189af63a"
|
|
13
13
|
},
|
|
14
14
|
"packages/runtime/src/exit-diagnostic.ts": {
|
|
15
|
-
"bytes":
|
|
16
|
-
"sha256": "
|
|
15
|
+
"bytes": 5059,
|
|
16
|
+
"sha256": "0c93ad077ad4cfcdf32a8092da1aff6cdc0f65322feadf538768439d67b11e06"
|
|
17
17
|
},
|
|
18
18
|
"packages/runtime/src/index.ts": {
|
|
19
|
-
"bytes":
|
|
20
|
-
"sha256": "
|
|
19
|
+
"bytes": 32606,
|
|
20
|
+
"sha256": "7f13b852fcc6e66d6b2c96374da757af8a97aea7f4710cd098363fd0ad1ef435"
|
|
21
21
|
},
|
|
22
22
|
"packages/runtime/src/installation.ts": {
|
|
23
23
|
"bytes": 1871,
|
|
24
24
|
"sha256": "a89c677fad56ace6839bda86891fafa088f3a37e0248c313a72d7918a0538a10"
|
|
25
25
|
},
|
|
26
26
|
"packages/runtime/src/startup-trace.ts": {
|
|
27
|
-
"bytes":
|
|
28
|
-
"sha256": "
|
|
27
|
+
"bytes": 3345,
|
|
28
|
+
"sha256": "19efe954374e1cd486277bc8df144d8dd711cffd12e2888d14021c871de840d6"
|
|
29
29
|
},
|
|
30
30
|
"LICENSE": {
|
|
31
31
|
"bytes": 1075,
|
|
@@ -33,11 +33,11 @@
|
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"toolchain": {
|
|
36
|
-
"bytes":
|
|
37
|
-
"sha256": "
|
|
36
|
+
"bytes": 3234,
|
|
37
|
+
"sha256": "9f4d9362cb4cc38c0f4e705295789a0cbfec46ffdd007e0541b9441a7c3003a7"
|
|
38
38
|
},
|
|
39
39
|
"dependencyLock": {
|
|
40
|
-
"bytes":
|
|
41
|
-
"sha256": "
|
|
40
|
+
"bytes": 28430,
|
|
41
|
+
"sha256": "9d321f65b9224958ec99286628d652232a16e3242ea9536a396bceaf72892460"
|
|
42
42
|
}
|
|
43
43
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bun-native/runtime",
|
|
3
|
-
"version": "0.33.
|
|
3
|
+
"version": "0.33.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"exports": "./src/index.ts",
|
|
16
16
|
"description": "Bun Native host process lifecycle and bridge runtime.",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@bun-native/protocol": "0.33.
|
|
18
|
+
"@bun-native/protocol": "0.33.2"
|
|
19
19
|
},
|
|
20
20
|
"repository": {
|
|
21
21
|
"type": "git",
|
package/src/exit-diagnostic.ts
CHANGED
|
@@ -46,10 +46,7 @@ function escapeRegExp(text: string): string {
|
|
|
46
46
|
* Removes inherited environment values, assignments and credential-shaped
|
|
47
47
|
* tokens from native output. Over-redaction is acceptable; leaks are not.
|
|
48
48
|
*/
|
|
49
|
-
function
|
|
50
|
-
text: string,
|
|
51
|
-
environment: Readonly<Record<string, string | undefined>>
|
|
52
|
-
): string {
|
|
49
|
+
function redact(text: string, environment: Readonly<Record<string, string | undefined>>): string {
|
|
53
50
|
let redacted = text.replace(/\p{Cc}/gu, (control) =>
|
|
54
51
|
control === '\n' || control === '\t' ? control : ''
|
|
55
52
|
)
|
|
@@ -72,6 +69,22 @@ function redactDiagnostic(
|
|
|
72
69
|
.replace(/\b[A-Za-z0-9+_-]{32,}={0,2}/g, REDACTED)
|
|
73
70
|
}
|
|
74
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Captured native stderr, safe to log or record. A clipped first line can hold
|
|
74
|
+
* the end of a secret that no longer matches its full environment value, so it
|
|
75
|
+
* is dropped before redaction.
|
|
76
|
+
*/
|
|
77
|
+
export function redactDiagnostic(
|
|
78
|
+
stderr: string,
|
|
79
|
+
clipped: boolean,
|
|
80
|
+
environment: Readonly<Record<string, string | undefined>>
|
|
81
|
+
): string {
|
|
82
|
+
return redact(
|
|
83
|
+
clipped ? stderr.slice(stderr.indexOf('\n') + 1 || stderr.length) : stderr,
|
|
84
|
+
environment
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
75
88
|
export function exitDiagnostic(input: {
|
|
76
89
|
exitCode: number | null
|
|
77
90
|
signal: string | null
|
|
@@ -88,17 +101,12 @@ export function exitDiagnostic(input: {
|
|
|
88
101
|
panicReport?: string | null
|
|
89
102
|
environment: Readonly<Record<string, string | undefined>>
|
|
90
103
|
}): Readonly<NativeHostExitDiagnostic> {
|
|
91
|
-
|
|
92
|
-
// its full environment value, so it is dropped before redaction.
|
|
93
|
-
const stderr = input.stderrClipped
|
|
94
|
-
? input.stderr.slice(input.stderr.indexOf('\n') + 1 || input.stderr.length)
|
|
95
|
-
: input.stderr
|
|
96
|
-
const redacted = redactDiagnostic(stderr, input.environment)
|
|
104
|
+
const redacted = redactDiagnostic(input.stderr, input.stderrClipped, input.environment)
|
|
97
105
|
let panic = PANIC_LOCATION.exec(redacted)
|
|
98
106
|
// The kept report leads the tail only when the captured stderr lost it.
|
|
99
107
|
const report =
|
|
100
108
|
!panic && input.panicReport
|
|
101
|
-
?
|
|
109
|
+
? redact(input.panicReport, input.environment)
|
|
102
110
|
.trimEnd()
|
|
103
111
|
.slice(0, MAX_DIAGNOSTIC_TAIL / 2)
|
|
104
112
|
: null
|
package/src/index.ts
CHANGED
|
@@ -6,18 +6,21 @@ import {
|
|
|
6
6
|
processArguments,
|
|
7
7
|
encodeFrame,
|
|
8
8
|
FrameDecoder,
|
|
9
|
+
MAX_EVENT_FRAME_BYTES,
|
|
9
10
|
parseCommand,
|
|
10
11
|
parseEvent,
|
|
11
12
|
PROTOCOL_VERSION,
|
|
12
13
|
REQUIRED_CAPABILITIES,
|
|
13
14
|
ProtocolError,
|
|
14
15
|
type Command,
|
|
16
|
+
type OperationList,
|
|
15
17
|
type NativeEvent,
|
|
16
18
|
type ErrorCode,
|
|
17
19
|
type Limits,
|
|
18
20
|
type ServicePolicy,
|
|
19
21
|
type Capability,
|
|
20
22
|
type FileAccess,
|
|
23
|
+
type HostEvent,
|
|
21
24
|
} from '@bun-native/protocol'
|
|
22
25
|
import { isAbsolute } from 'node:path'
|
|
23
26
|
import type { ReadableStreamDefaultReader } from 'node:stream/web'
|
|
@@ -31,82 +34,81 @@ export {
|
|
|
31
34
|
type NativeHostExitReason,
|
|
32
35
|
} from './exit-diagnostic.ts'
|
|
33
36
|
export { installedRelease, reportHealthy, type InstalledRelease } from './installation.ts'
|
|
37
|
+
export type { ApplicationEvent, HostEvent, WindowEvent } from '@bun-native/protocol'
|
|
34
38
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
{
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
| 'message_dialog_closed'
|
|
102
|
-
| 'context_menu_opened'
|
|
103
|
-
| 'context_menu_closed'
|
|
104
|
-
| 'drop_target_registered'
|
|
105
|
-
| 'drop_target_unregistered'
|
|
106
|
-
| 'dropped_item_resolved'
|
|
107
|
-
| 'app_menu_set'
|
|
39
|
+
/** A command as callers submit it; a batch may carry core's checked `OperationList`. */
|
|
40
|
+
type WithoutId<T> = T extends { id: number }
|
|
41
|
+
? T extends { type: 'batch'; operations: infer O }
|
|
42
|
+
? Omit<T, 'id' | 'operations'> & { operations: O | OperationList }
|
|
43
|
+
: Omit<T, 'id'>
|
|
44
|
+
: never
|
|
45
|
+
export type WindowCommand = WithoutId<Exclude<Command, { type: 'hello' | 'shutdown' }>>
|
|
46
|
+
|
|
47
|
+
type Field<C extends Command['type']> = Exclude<keyof Extract<Command, { type: C }>, 'type' | 'id'>
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Each command's one response type and the request fields the response must echo. `open`,
|
|
51
|
+
* `batch` and `file_request` also check the revision or result they answer (`matchesResponse`).
|
|
52
|
+
*/
|
|
53
|
+
const RESPONSES = {
|
|
54
|
+
hello: { type: 'ack', echoes: [] },
|
|
55
|
+
shutdown: { type: 'ack', echoes: [] },
|
|
56
|
+
open: { type: 'opened', echoes: ['window_id', 'root_id'] },
|
|
57
|
+
batch: { type: 'applied', echoes: ['window_id'] },
|
|
58
|
+
close: { type: 'ack', echoes: [] },
|
|
59
|
+
display_snapshot: { type: 'display_snapshot', echoes: ['window_id'] },
|
|
60
|
+
clipboard_read: { type: 'clipboard_text', echoes: ['window_id'] },
|
|
61
|
+
clipboard_write: { type: 'clipboard_written', echoes: ['window_id'] },
|
|
62
|
+
register_global_shortcut: {
|
|
63
|
+
type: 'global_shortcut_registered',
|
|
64
|
+
echoes: ['window_id', 'shortcut_id'],
|
|
65
|
+
},
|
|
66
|
+
unregister_global_shortcut: {
|
|
67
|
+
type: 'global_shortcut_unregistered',
|
|
68
|
+
echoes: ['window_id', 'shortcut_id'],
|
|
69
|
+
},
|
|
70
|
+
post_notification: { type: 'notification_posted', echoes: ['window_id', 'notification_id'] },
|
|
71
|
+
withdraw_notification: {
|
|
72
|
+
type: 'notification_withdrawn',
|
|
73
|
+
echoes: ['window_id', 'notification_id'],
|
|
74
|
+
},
|
|
75
|
+
open_file_grant: { type: 'file_grant_opened', echoes: ['window_id', 'grant_id'] },
|
|
76
|
+
close_file_grant: { type: 'file_grant_closed', echoes: ['window_id', 'grant_id'] },
|
|
77
|
+
file_request: { type: 'file_result', echoes: ['window_id', 'grant_id'] },
|
|
78
|
+
spawn_process: { type: 'process_spawned', echoes: ['window_id', 'process_id'] },
|
|
79
|
+
write_process: { type: 'process_updated', echoes: ['window_id', 'process_id'] },
|
|
80
|
+
close_process_input: { type: 'process_updated', echoes: ['window_id', 'process_id'] },
|
|
81
|
+
resize_process: { type: 'process_updated', echoes: ['window_id', 'process_id'] },
|
|
82
|
+
signal_process: { type: 'process_updated', echoes: ['window_id', 'process_id'] },
|
|
83
|
+
acknowledge_process_output: { type: 'process_updated', echoes: ['window_id', 'process_id'] },
|
|
84
|
+
release_process: { type: 'process_released', echoes: ['window_id', 'process_id'] },
|
|
85
|
+
show_file_dialog: { type: 'file_dialog_opened', echoes: ['window_id', 'dialog_id'] },
|
|
86
|
+
close_file_dialog: { type: 'file_dialog_closed', echoes: ['window_id', 'dialog_id'] },
|
|
87
|
+
show_message_dialog: { type: 'message_dialog_opened', echoes: ['window_id', 'dialog_id'] },
|
|
88
|
+
close_message_dialog: { type: 'message_dialog_closed', echoes: ['window_id', 'dialog_id'] },
|
|
89
|
+
show_context_menu: { type: 'context_menu_opened', echoes: ['window_id', 'menu_id'] },
|
|
90
|
+
close_context_menu: { type: 'context_menu_closed', echoes: ['window_id', 'menu_id'] },
|
|
91
|
+
register_drop_target: { type: 'drop_target_registered', echoes: ['window_id', 'target_id'] },
|
|
92
|
+
unregister_drop_target: {
|
|
93
|
+
type: 'drop_target_unregistered',
|
|
94
|
+
echoes: ['window_id', 'target_id'],
|
|
95
|
+
},
|
|
96
|
+
resolve_dropped_item: {
|
|
97
|
+
type: 'dropped_item_resolved',
|
|
98
|
+
echoes: ['window_id', 'target_id', 'drop_id', 'item', 'root'],
|
|
99
|
+
},
|
|
100
|
+
set_app_menu: { type: 'app_menu_set', echoes: ['menu_id'] },
|
|
101
|
+
} as const satisfies {
|
|
102
|
+
[C in Command['type']]: {
|
|
103
|
+
type: NativeEvent['type']
|
|
104
|
+
echoes: readonly Field<C>[]
|
|
108
105
|
}
|
|
109
|
-
|
|
106
|
+
}
|
|
107
|
+
type ResponseType<C extends Command['type']> = (typeof RESPONSES)[C]['type']
|
|
108
|
+
export type HostResponse = Extract<NativeEvent, { type: ResponseType<Command['type']> }>
|
|
109
|
+
const RESPONSE_TYPES: ReadonlySet<string> = new Set(
|
|
110
|
+
Object.values(RESPONSES).map((response) => response.type)
|
|
111
|
+
)
|
|
110
112
|
|
|
111
113
|
export interface ProcessLimitPermissions {
|
|
112
114
|
/** CPU seconds before the child is stopped (SIGXCPU on POSIX). */
|
|
@@ -308,6 +310,10 @@ function samePolicy(left: Readonly<ServicePolicy>, right: Readonly<ServicePolicy
|
|
|
308
310
|
)
|
|
309
311
|
}
|
|
310
312
|
|
|
313
|
+
function isResponse(event: NativeEvent): event is HostResponse {
|
|
314
|
+
return RESPONSE_TYPES.has(event.type)
|
|
315
|
+
}
|
|
316
|
+
|
|
311
317
|
type Pending = {
|
|
312
318
|
command: Command
|
|
313
319
|
resolve: (response: HostResponse) => void
|
|
@@ -351,6 +357,8 @@ export class NativeHost implements AsyncIterable<HostEvent> {
|
|
|
351
357
|
private readonly events: HostEvent[] = []
|
|
352
358
|
private readonly finished: Promise<void>
|
|
353
359
|
private waiting: WaitingReader | undefined
|
|
360
|
+
/** Queued events the consumer has taken; progress while reading is paused. */
|
|
361
|
+
private taken = 0
|
|
354
362
|
private failure: Error | undefined
|
|
355
363
|
private phase: 'starting' | 'running' | 'stopping' | 'closed' = 'starting'
|
|
356
364
|
private ready = false
|
|
@@ -390,6 +398,10 @@ export class NativeHost implements AsyncIterable<HostEvent> {
|
|
|
390
398
|
stdin: 'pipe',
|
|
391
399
|
stdout: 'pipe',
|
|
392
400
|
stderr: 'pipe',
|
|
401
|
+
// The host inherits the environment as this process holds it now. The
|
|
402
|
+
// pinned Bun otherwise passes the environment it started with, without
|
|
403
|
+
// later `process.env` changes (such as an opt-in host trace).
|
|
404
|
+
env: { ...process.env },
|
|
393
405
|
// A terminal's Ctrl-C signals its whole foreground process group. The
|
|
394
406
|
// application owns the host's lifetime and ends it through `shutdown`
|
|
395
407
|
// (or closed stdin), so the host runs in its own session on POSIX.
|
|
@@ -428,6 +440,11 @@ export class NativeHost implements AsyncIterable<HostEvent> {
|
|
|
428
440
|
}
|
|
429
441
|
}
|
|
430
442
|
|
|
443
|
+
/** Whether the transport is still open (false once it failed or shut down). */
|
|
444
|
+
get running(): boolean {
|
|
445
|
+
return this.phase === 'running'
|
|
446
|
+
}
|
|
447
|
+
|
|
431
448
|
get limits(): Readonly<Limits> {
|
|
432
449
|
if (!this.negotiatedLimits) throw new ProtocolError('Native limits are not negotiated')
|
|
433
450
|
return this.negotiatedLimits
|
|
@@ -446,95 +463,9 @@ export class NativeHost implements AsyncIterable<HostEvent> {
|
|
|
446
463
|
}
|
|
447
464
|
|
|
448
465
|
/** Resolves after the native UI thread acknowledges the operation. */
|
|
449
|
-
send(
|
|
450
|
-
command:
|
|
451
|
-
): Promise<Extract<HostResponse, { type: '
|
|
452
|
-
send(
|
|
453
|
-
command: Extract<WindowCommand, { type: 'batch' }>
|
|
454
|
-
): Promise<Extract<HostResponse, { type: 'applied' }>>
|
|
455
|
-
send(
|
|
456
|
-
command: Extract<WindowCommand, { type: 'close' }>
|
|
457
|
-
): Promise<Extract<HostResponse, { type: 'ack' }>>
|
|
458
|
-
send(
|
|
459
|
-
command: Extract<WindowCommand, { type: 'display_snapshot' }>
|
|
460
|
-
): Promise<Extract<HostResponse, { type: 'display_snapshot' }>>
|
|
461
|
-
send(
|
|
462
|
-
command: Extract<WindowCommand, { type: 'clipboard_read' }>
|
|
463
|
-
): Promise<Extract<HostResponse, { type: 'clipboard_text' }>>
|
|
464
|
-
send(
|
|
465
|
-
command: Extract<WindowCommand, { type: 'clipboard_write' }>
|
|
466
|
-
): Promise<Extract<HostResponse, { type: 'clipboard_written' }>>
|
|
467
|
-
send(
|
|
468
|
-
command: Extract<WindowCommand, { type: 'register_global_shortcut' }>
|
|
469
|
-
): Promise<Extract<HostResponse, { type: 'global_shortcut_registered' }>>
|
|
470
|
-
send(
|
|
471
|
-
command: Extract<WindowCommand, { type: 'unregister_global_shortcut' }>
|
|
472
|
-
): Promise<Extract<HostResponse, { type: 'global_shortcut_unregistered' }>>
|
|
473
|
-
send(
|
|
474
|
-
command: Extract<WindowCommand, { type: 'post_notification' }>
|
|
475
|
-
): Promise<Extract<HostResponse, { type: 'notification_posted' }>>
|
|
476
|
-
send(
|
|
477
|
-
command: Extract<WindowCommand, { type: 'withdraw_notification' }>
|
|
478
|
-
): Promise<Extract<HostResponse, { type: 'notification_withdrawn' }>>
|
|
479
|
-
send(
|
|
480
|
-
command: Extract<WindowCommand, { type: 'open_file_grant' }>
|
|
481
|
-
): Promise<Extract<HostResponse, { type: 'file_grant_opened' }>>
|
|
482
|
-
send(
|
|
483
|
-
command: Extract<WindowCommand, { type: 'close_file_grant' }>
|
|
484
|
-
): Promise<Extract<HostResponse, { type: 'file_grant_closed' }>>
|
|
485
|
-
send(
|
|
486
|
-
command: Extract<WindowCommand, { type: 'file_request' }>
|
|
487
|
-
): Promise<Extract<HostResponse, { type: 'file_result' }>>
|
|
488
|
-
send(
|
|
489
|
-
command: Extract<WindowCommand, { type: 'spawn_process' }>
|
|
490
|
-
): Promise<Extract<HostResponse, { type: 'process_spawned' }>>
|
|
491
|
-
send(
|
|
492
|
-
command: Extract<WindowCommand, { type: 'release_process' }>
|
|
493
|
-
): Promise<Extract<HostResponse, { type: 'process_released' }>>
|
|
494
|
-
send(
|
|
495
|
-
command: Extract<
|
|
496
|
-
WindowCommand,
|
|
497
|
-
{
|
|
498
|
-
type:
|
|
499
|
-
| 'write_process'
|
|
500
|
-
| 'close_process_input'
|
|
501
|
-
| 'resize_process'
|
|
502
|
-
| 'signal_process'
|
|
503
|
-
| 'acknowledge_process_output'
|
|
504
|
-
}
|
|
505
|
-
>
|
|
506
|
-
): Promise<Extract<HostResponse, { type: 'process_updated' }>>
|
|
507
|
-
send(
|
|
508
|
-
command: Extract<WindowCommand, { type: 'show_file_dialog' }>
|
|
509
|
-
): Promise<Extract<HostResponse, { type: 'file_dialog_opened' }>>
|
|
510
|
-
send(
|
|
511
|
-
command: Extract<WindowCommand, { type: 'close_file_dialog' }>
|
|
512
|
-
): Promise<Extract<HostResponse, { type: 'file_dialog_closed' }>>
|
|
513
|
-
send(
|
|
514
|
-
command: Extract<WindowCommand, { type: 'show_message_dialog' }>
|
|
515
|
-
): Promise<Extract<HostResponse, { type: 'message_dialog_opened' }>>
|
|
516
|
-
send(
|
|
517
|
-
command: Extract<WindowCommand, { type: 'close_message_dialog' }>
|
|
518
|
-
): Promise<Extract<HostResponse, { type: 'message_dialog_closed' }>>
|
|
519
|
-
send(
|
|
520
|
-
command: Extract<WindowCommand, { type: 'show_context_menu' }>
|
|
521
|
-
): Promise<Extract<HostResponse, { type: 'context_menu_opened' }>>
|
|
522
|
-
send(
|
|
523
|
-
command: Extract<WindowCommand, { type: 'close_context_menu' }>
|
|
524
|
-
): Promise<Extract<HostResponse, { type: 'context_menu_closed' }>>
|
|
525
|
-
send(
|
|
526
|
-
command: Extract<WindowCommand, { type: 'register_drop_target' }>
|
|
527
|
-
): Promise<Extract<HostResponse, { type: 'drop_target_registered' }>>
|
|
528
|
-
send(
|
|
529
|
-
command: Extract<WindowCommand, { type: 'unregister_drop_target' }>
|
|
530
|
-
): Promise<Extract<HostResponse, { type: 'drop_target_unregistered' }>>
|
|
531
|
-
send(
|
|
532
|
-
command: Extract<WindowCommand, { type: 'resolve_dropped_item' }>
|
|
533
|
-
): Promise<Extract<HostResponse, { type: 'dropped_item_resolved' }>>
|
|
534
|
-
send(
|
|
535
|
-
command: Extract<WindowCommand, { type: 'set_app_menu' }>
|
|
536
|
-
): Promise<Extract<HostResponse, { type: 'app_menu_set' }>>
|
|
537
|
-
send(command: WindowCommand): Promise<HostResponse>
|
|
466
|
+
send<C extends WindowCommand>(
|
|
467
|
+
command: C
|
|
468
|
+
): Promise<Extract<HostResponse, { type: ResponseType<C['type']> }>>
|
|
538
469
|
send(command: WindowCommand): Promise<HostResponse> {
|
|
539
470
|
if (this.phase !== 'running') return Promise.reject(this.closedError())
|
|
540
471
|
return this.request(command)
|
|
@@ -574,7 +505,10 @@ export class NativeHost implements AsyncIterable<HostEvent> {
|
|
|
574
505
|
next: () => {
|
|
575
506
|
if (this.failure) return Promise.reject(this.failure)
|
|
576
507
|
const event = this.events.shift()
|
|
577
|
-
if (event)
|
|
508
|
+
if (event) {
|
|
509
|
+
this.taken += 1
|
|
510
|
+
return Promise.resolve({ done: false, value: event })
|
|
511
|
+
}
|
|
578
512
|
if (this.phase === 'closed') return Promise.resolve({ done: true, value: undefined })
|
|
579
513
|
if (this.waiting)
|
|
580
514
|
return Promise.reject(new Error('Concurrent event reads are not supported'))
|
|
@@ -660,12 +594,13 @@ export class NativeHost implements AsyncIterable<HostEvent> {
|
|
|
660
594
|
}
|
|
661
595
|
|
|
662
596
|
private async readStdout(): Promise<void> {
|
|
663
|
-
const decoder = new FrameDecoder()
|
|
597
|
+
const decoder = new FrameDecoder(MAX_EVENT_FRAME_BYTES)
|
|
664
598
|
try {
|
|
665
599
|
for (;;) {
|
|
666
600
|
const { done, value } = await this.stdout.read()
|
|
667
601
|
if (done) break
|
|
668
602
|
for (const raw of decoder.push(value)) this.receive(parseEvent(raw))
|
|
603
|
+
if (this.events.length >= MAX_QUEUED_EVENTS) await this.drainEvents()
|
|
669
604
|
}
|
|
670
605
|
decoder.finish()
|
|
671
606
|
if (
|
|
@@ -679,6 +614,21 @@ export class NativeHost implements AsyncIterable<HostEvent> {
|
|
|
679
614
|
}
|
|
680
615
|
}
|
|
681
616
|
|
|
617
|
+
/**
|
|
618
|
+
* One pipe read can coalesce more frames than the cap while the consumer is
|
|
619
|
+
* keeping up. Over the cap, reading pauses for macrotask turns while the
|
|
620
|
+
* consumer takes events, and fails only a turn in which it took none, so
|
|
621
|
+
* queued events stay bounded by the cap plus one read.
|
|
622
|
+
*/
|
|
623
|
+
private async drainEvents(): Promise<void> {
|
|
624
|
+
while (this.events.length >= MAX_QUEUED_EVENTS) {
|
|
625
|
+
const taken = this.taken
|
|
626
|
+
await new Promise<void>((resolve) => setImmediate(resolve))
|
|
627
|
+
if (this.phase === 'closed') return
|
|
628
|
+
if (this.taken === taken) throw new ProtocolError('Native event limit exceeded')
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
|
|
682
632
|
private async readStderr(): Promise<void> {
|
|
683
633
|
const decoder = new TextDecoder()
|
|
684
634
|
try {
|
|
@@ -771,7 +721,12 @@ export class NativeHost implements AsyncIterable<HostEvent> {
|
|
|
771
721
|
clearTimeout(drainDeadline)
|
|
772
722
|
this.cancelOutput()
|
|
773
723
|
await Promise.allSettled(streams)
|
|
774
|
-
this.startupTrace?.finished(
|
|
724
|
+
this.startupTrace?.finished(
|
|
725
|
+
this.process.exitCode,
|
|
726
|
+
this.stderrTail,
|
|
727
|
+
this.stderrClipped,
|
|
728
|
+
this.process.pid
|
|
729
|
+
)
|
|
775
730
|
}
|
|
776
731
|
}
|
|
777
732
|
|
|
@@ -809,35 +764,7 @@ export class NativeHost implements AsyncIterable<HostEvent> {
|
|
|
809
764
|
this.ready = true
|
|
810
765
|
return
|
|
811
766
|
}
|
|
812
|
-
if (
|
|
813
|
-
event.type === 'ack' ||
|
|
814
|
-
event.type === 'opened' ||
|
|
815
|
-
event.type === 'applied' ||
|
|
816
|
-
event.type === 'display_snapshot' ||
|
|
817
|
-
event.type === 'clipboard_text' ||
|
|
818
|
-
event.type === 'clipboard_written' ||
|
|
819
|
-
event.type === 'global_shortcut_registered' ||
|
|
820
|
-
event.type === 'global_shortcut_unregistered' ||
|
|
821
|
-
event.type === 'notification_posted' ||
|
|
822
|
-
event.type === 'notification_withdrawn' ||
|
|
823
|
-
event.type === 'file_grant_opened' ||
|
|
824
|
-
event.type === 'file_grant_closed' ||
|
|
825
|
-
event.type === 'file_result' ||
|
|
826
|
-
event.type === 'process_spawned' ||
|
|
827
|
-
event.type === 'process_updated' ||
|
|
828
|
-
event.type === 'process_released' ||
|
|
829
|
-
event.type === 'file_dialog_opened' ||
|
|
830
|
-
event.type === 'file_dialog_closed' ||
|
|
831
|
-
event.type === 'message_dialog_opened' ||
|
|
832
|
-
event.type === 'message_dialog_closed' ||
|
|
833
|
-
event.type === 'context_menu_opened' ||
|
|
834
|
-
event.type === 'context_menu_closed' ||
|
|
835
|
-
event.type === 'drop_target_registered' ||
|
|
836
|
-
event.type === 'drop_target_unregistered' ||
|
|
837
|
-
event.type === 'dropped_item_resolved' ||
|
|
838
|
-
event.type === 'app_menu_set' ||
|
|
839
|
-
event.type === 'error'
|
|
840
|
-
) {
|
|
767
|
+
if (event.type === 'error' || isResponse(event)) {
|
|
841
768
|
if (event.type === 'error' && event.id === undefined)
|
|
842
769
|
throw new ProtocolError(`Native ${event.code}: ${event.message}`)
|
|
843
770
|
const requestId = event.id
|
|
@@ -864,149 +791,22 @@ export class NativeHost implements AsyncIterable<HostEvent> {
|
|
|
864
791
|
this.waiting.resolve({ done: false, value: event })
|
|
865
792
|
this.waiting = undefined
|
|
866
793
|
} else {
|
|
867
|
-
if (this.events.length >= MAX_QUEUED_EVENTS)
|
|
868
|
-
throw new ProtocolError('Native event limit exceeded')
|
|
869
794
|
this.events.push(event)
|
|
870
795
|
}
|
|
871
796
|
}
|
|
872
797
|
|
|
873
798
|
private matchesResponse(command: Command, response: HostResponse): boolean {
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
response.shortcut_id === command.shortcut_id
|
|
886
|
-
)
|
|
887
|
-
case 'unregister_global_shortcut':
|
|
888
|
-
return (
|
|
889
|
-
response.type === 'global_shortcut_unregistered' &&
|
|
890
|
-
response.window_id === command.window_id &&
|
|
891
|
-
response.shortcut_id === command.shortcut_id
|
|
892
|
-
)
|
|
893
|
-
case 'post_notification':
|
|
894
|
-
return (
|
|
895
|
-
response.type === 'notification_posted' &&
|
|
896
|
-
response.window_id === command.window_id &&
|
|
897
|
-
response.notification_id === command.notification_id
|
|
898
|
-
)
|
|
899
|
-
case 'withdraw_notification':
|
|
900
|
-
return (
|
|
901
|
-
response.type === 'notification_withdrawn' &&
|
|
902
|
-
response.window_id === command.window_id &&
|
|
903
|
-
response.notification_id === command.notification_id
|
|
904
|
-
)
|
|
905
|
-
case 'open_file_grant':
|
|
906
|
-
case 'close_file_grant':
|
|
907
|
-
return (
|
|
908
|
-
response.type ===
|
|
909
|
-
(command.type === 'open_file_grant' ? 'file_grant_opened' : 'file_grant_closed') &&
|
|
910
|
-
response.window_id === command.window_id &&
|
|
911
|
-
response.grant_id === command.grant_id
|
|
912
|
-
)
|
|
913
|
-
case 'file_request':
|
|
914
|
-
return (
|
|
915
|
-
response.type === 'file_result' &&
|
|
916
|
-
response.window_id === command.window_id &&
|
|
917
|
-
response.grant_id === command.grant_id &&
|
|
918
|
-
fileResultAnswers(command.operation, response.result)
|
|
919
|
-
)
|
|
920
|
-
case 'spawn_process':
|
|
921
|
-
case 'write_process':
|
|
922
|
-
case 'close_process_input':
|
|
923
|
-
case 'resize_process':
|
|
924
|
-
case 'signal_process':
|
|
925
|
-
case 'acknowledge_process_output':
|
|
926
|
-
case 'release_process':
|
|
927
|
-
return (
|
|
928
|
-
response.type ===
|
|
929
|
-
(command.type === 'spawn_process'
|
|
930
|
-
? 'process_spawned'
|
|
931
|
-
: command.type === 'release_process'
|
|
932
|
-
? 'process_released'
|
|
933
|
-
: 'process_updated') &&
|
|
934
|
-
response.window_id === command.window_id &&
|
|
935
|
-
response.process_id === command.process_id
|
|
936
|
-
)
|
|
937
|
-
case 'show_file_dialog':
|
|
938
|
-
return (
|
|
939
|
-
response.type === 'file_dialog_opened' &&
|
|
940
|
-
response.window_id === command.window_id &&
|
|
941
|
-
response.dialog_id === command.dialog_id
|
|
942
|
-
)
|
|
943
|
-
case 'close_file_dialog':
|
|
944
|
-
return (
|
|
945
|
-
response.type === 'file_dialog_closed' &&
|
|
946
|
-
response.window_id === command.window_id &&
|
|
947
|
-
response.dialog_id === command.dialog_id
|
|
948
|
-
)
|
|
949
|
-
case 'show_message_dialog':
|
|
950
|
-
return (
|
|
951
|
-
response.type === 'message_dialog_opened' &&
|
|
952
|
-
response.window_id === command.window_id &&
|
|
953
|
-
response.dialog_id === command.dialog_id
|
|
954
|
-
)
|
|
955
|
-
case 'close_message_dialog':
|
|
956
|
-
return (
|
|
957
|
-
response.type === 'message_dialog_closed' &&
|
|
958
|
-
response.window_id === command.window_id &&
|
|
959
|
-
response.dialog_id === command.dialog_id
|
|
960
|
-
)
|
|
961
|
-
case 'show_context_menu':
|
|
962
|
-
case 'close_context_menu':
|
|
963
|
-
return (
|
|
964
|
-
response.type ===
|
|
965
|
-
(command.type === 'show_context_menu'
|
|
966
|
-
? 'context_menu_opened'
|
|
967
|
-
: 'context_menu_closed') &&
|
|
968
|
-
response.window_id === command.window_id &&
|
|
969
|
-
response.menu_id === command.menu_id
|
|
970
|
-
)
|
|
971
|
-
case 'register_drop_target':
|
|
972
|
-
case 'unregister_drop_target':
|
|
973
|
-
return (
|
|
974
|
-
response.type ===
|
|
975
|
-
(command.type === 'register_drop_target'
|
|
976
|
-
? 'drop_target_registered'
|
|
977
|
-
: 'drop_target_unregistered') &&
|
|
978
|
-
response.window_id === command.window_id &&
|
|
979
|
-
response.target_id === command.target_id
|
|
980
|
-
)
|
|
981
|
-
case 'resolve_dropped_item':
|
|
982
|
-
return (
|
|
983
|
-
response.type === 'dropped_item_resolved' &&
|
|
984
|
-
response.window_id === command.window_id &&
|
|
985
|
-
response.target_id === command.target_id &&
|
|
986
|
-
response.drop_id === command.drop_id &&
|
|
987
|
-
response.item === command.item &&
|
|
988
|
-
response.root === command.root
|
|
989
|
-
)
|
|
990
|
-
case 'set_app_menu':
|
|
991
|
-
return response.type === 'app_menu_set' && response.menu_id === command.menu_id
|
|
992
|
-
case 'open':
|
|
993
|
-
return (
|
|
994
|
-
response.type === 'opened' &&
|
|
995
|
-
response.window_id === command.window_id &&
|
|
996
|
-
response.root_id === command.root_id &&
|
|
997
|
-
response.tree_revision === 0
|
|
998
|
-
)
|
|
999
|
-
case 'batch':
|
|
1000
|
-
return (
|
|
1001
|
-
response.type === 'applied' &&
|
|
1002
|
-
response.window_id === command.window_id &&
|
|
1003
|
-
response.tree_revision === command.base_revision + 1
|
|
1004
|
-
)
|
|
1005
|
-
case 'hello':
|
|
1006
|
-
case 'close':
|
|
1007
|
-
case 'shutdown':
|
|
1008
|
-
return response.type === 'ack'
|
|
1009
|
-
}
|
|
799
|
+
const expected = RESPONSES[command.type]
|
|
800
|
+
if (response.type !== expected.type) return false
|
|
801
|
+
const request = command as unknown as Readonly<Record<string, unknown>>
|
|
802
|
+
const answer = response as unknown as Readonly<Record<string, unknown>>
|
|
803
|
+
if (!expected.echoes.every((field: string) => answer[field] === request[field])) return false
|
|
804
|
+
if (command.type === 'open' && response.type === 'opened') return response.tree_revision === 0
|
|
805
|
+
if (command.type === 'batch' && response.type === 'applied')
|
|
806
|
+
return response.tree_revision === command.base_revision + 1
|
|
807
|
+
if (command.type === 'file_request' && response.type === 'file_result')
|
|
808
|
+
return fileResultAnswers(command.operation, response.result)
|
|
809
|
+
return true
|
|
1010
810
|
}
|
|
1011
811
|
|
|
1012
812
|
private closedError(): Error {
|
|
@@ -1016,7 +816,7 @@ export class NativeHost implements AsyncIterable<HostEvent> {
|
|
|
1016
816
|
private fail(cause: unknown): void {
|
|
1017
817
|
if (this.failure) return
|
|
1018
818
|
this.failure = cause instanceof Error ? cause : new Error(String(cause))
|
|
1019
|
-
this.startupTrace?.failure(this.failure, this.stderrTail, this.process.pid)
|
|
819
|
+
this.startupTrace?.failure(this.failure, this.stderrTail, this.stderrClipped, this.process.pid)
|
|
1020
820
|
this.phase = 'closed'
|
|
1021
821
|
for (const request of this.pending.values()) {
|
|
1022
822
|
clearTimeout(request.timer)
|
package/src/startup-trace.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Command, NativeEvent } from '@bun-native/protocol'
|
|
2
2
|
import { appendFileSync } from 'node:fs'
|
|
3
3
|
import { isAbsolute } from 'node:path'
|
|
4
|
+
import { redactDiagnostic } from './exit-diagnostic.ts'
|
|
4
5
|
|
|
5
6
|
const MAX_RECORDS = 256
|
|
6
7
|
|
|
@@ -51,18 +52,27 @@ export class StartupTrace {
|
|
|
51
52
|
})
|
|
52
53
|
}
|
|
53
54
|
|
|
54
|
-
|
|
55
|
+
/** `stderr` is the bounded capture; it is recorded redacted, like exit diagnostics. */
|
|
56
|
+
failure(error: Error, stderr: string, clipped: boolean, hostPid: number): void {
|
|
55
57
|
this.write(
|
|
56
58
|
'transport_failed',
|
|
57
|
-
{
|
|
59
|
+
{
|
|
60
|
+
host_pid: hostPid,
|
|
61
|
+
error: error.message.slice(-1_024),
|
|
62
|
+
stderr_tail: redactDiagnostic(stderr, clipped, process.env),
|
|
63
|
+
},
|
|
58
64
|
true
|
|
59
65
|
)
|
|
60
66
|
}
|
|
61
67
|
|
|
62
|
-
finished(exitCode: number | null, stderr: string, hostPid: number): void {
|
|
68
|
+
finished(exitCode: number | null, stderr: string, clipped: boolean, hostPid: number): void {
|
|
63
69
|
this.write(
|
|
64
70
|
'transport_finished',
|
|
65
|
-
{
|
|
71
|
+
{
|
|
72
|
+
host_pid: hostPid,
|
|
73
|
+
exit_code: exitCode,
|
|
74
|
+
stderr_tail: redactDiagnostic(stderr, clipped, process.env),
|
|
75
|
+
},
|
|
66
76
|
true
|
|
67
77
|
)
|
|
68
78
|
}
|