@bun-native/core 0.0.0-bootstrap.0 → 0.33.0
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/LICENSE +21 -0
- package/README.md +5 -0
- package/bun-native-source.json +107 -0
- package/package.json +32 -1
- package/src/app-menu.ts +271 -0
- package/src/application.ts +2168 -0
- package/src/commit-plan.ts +233 -0
- package/src/context-menus.ts +76 -0
- package/src/dialogs.ts +153 -0
- package/src/display.ts +25 -0
- package/src/drops.ts +95 -0
- package/src/events.ts +331 -0
- package/src/files.ts +253 -0
- package/src/index.ts +88 -0
- package/src/message-dialogs.ts +487 -0
- package/src/notifications.ts +141 -0
- package/src/overlays.ts +55 -0
- package/src/processes.ts +218 -0
- package/src/services.ts +428 -0
- package/src/shortcuts.ts +129 -0
- package/src/splits.ts +34 -0
- package/src/theme.ts +96 -0
- package/src/tree.ts +672 -0
- package/src/types.ts +375 -0
package/src/events.ts
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
import type { WindowEvent } from '@bun-native/runtime'
|
|
2
|
+
import {
|
|
3
|
+
NativeStateError,
|
|
4
|
+
type Listener,
|
|
5
|
+
type ListenerOptions,
|
|
6
|
+
type NativeEventMap,
|
|
7
|
+
type NativeEventType,
|
|
8
|
+
type NativeNode,
|
|
9
|
+
type NativeUIEvent,
|
|
10
|
+
} from './types.ts'
|
|
11
|
+
|
|
12
|
+
export type RoutedEvent = Exclude<
|
|
13
|
+
WindowEvent,
|
|
14
|
+
{
|
|
15
|
+
type:
|
|
16
|
+
| 'closed'
|
|
17
|
+
| 'global_shortcut'
|
|
18
|
+
| 'notification_activated'
|
|
19
|
+
| 'notification_dismissed'
|
|
20
|
+
| 'process_output'
|
|
21
|
+
| 'process_exited'
|
|
22
|
+
| 'display_changed'
|
|
23
|
+
| 'file_dialog_completed'
|
|
24
|
+
| 'message_dialog_completed'
|
|
25
|
+
| 'context_menu_completed'
|
|
26
|
+
| 'drop_target_drag'
|
|
27
|
+
| 'drop_received'
|
|
28
|
+
| 'drop_rejected'
|
|
29
|
+
}
|
|
30
|
+
>
|
|
31
|
+
type BindingKey = `${NativeEventType}:${'capture' | 'bubble'}`
|
|
32
|
+
type BindingKeys = { readonly capture: BindingKey; readonly bubble: BindingKey }
|
|
33
|
+
const keys = (type: NativeEventType): BindingKeys => ({
|
|
34
|
+
capture: `${type}:capture`,
|
|
35
|
+
bubble: `${type}:bubble`,
|
|
36
|
+
})
|
|
37
|
+
/** Interned per-type keys: routing a native event builds no key strings. */
|
|
38
|
+
const bindingKeys: Readonly<Record<NativeEventType, BindingKeys>> = {
|
|
39
|
+
click: keys('click'),
|
|
40
|
+
input: keys('input'),
|
|
41
|
+
key_down: keys('key_down'),
|
|
42
|
+
key_up: keys('key_up'),
|
|
43
|
+
pointer_down: keys('pointer_down'),
|
|
44
|
+
pointer_up: keys('pointer_up'),
|
|
45
|
+
focus: keys('focus'),
|
|
46
|
+
blur: keys('blur'),
|
|
47
|
+
dismiss: keys('dismiss'),
|
|
48
|
+
split_resize: keys('split_resize'),
|
|
49
|
+
}
|
|
50
|
+
const eventTypes: readonly string[] = Object.keys(bindingKeys)
|
|
51
|
+
type Subscription = { remove: (() => void) | undefined }
|
|
52
|
+
type Binding = {
|
|
53
|
+
node: NativeNode
|
|
54
|
+
controller: AbortController
|
|
55
|
+
invoke: Listener<NativeUIEvent> | undefined
|
|
56
|
+
subscription: Subscription
|
|
57
|
+
}
|
|
58
|
+
type Delivery = { binding: Binding; phase: NativeUIEvent['phase'] }
|
|
59
|
+
|
|
60
|
+
/** Keep the typed listener outside the registration/removal closure scope. */
|
|
61
|
+
function invocation<T extends NativeEventType>(
|
|
62
|
+
listener: Listener<NativeEventMap[T]>
|
|
63
|
+
): Listener<NativeUIEvent> {
|
|
64
|
+
return (event) => listener(event as NativeEventMap[T])
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function revoke(binding: Binding): void {
|
|
68
|
+
// Abort handlers can reenter registration. Revoke both strong owner edges
|
|
69
|
+
// first, including bindings still present in a queued or active plan.
|
|
70
|
+
binding.invoke = undefined
|
|
71
|
+
binding.subscription.remove = undefined
|
|
72
|
+
binding.controller.abort()
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** A retained handle owns only a revocable cell, never the registry's scope. */
|
|
76
|
+
function unsubscribe(subscription: Subscription): () => void {
|
|
77
|
+
return () => subscription.remove?.()
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function payload(event: RoutedEvent, node: NativeNode, path: readonly NativeNode[]) {
|
|
81
|
+
const common = {
|
|
82
|
+
node,
|
|
83
|
+
path: Object.freeze([...path]),
|
|
84
|
+
sequence: event.sequence,
|
|
85
|
+
treeRevision: event.tree_revision,
|
|
86
|
+
}
|
|
87
|
+
switch (event.type) {
|
|
88
|
+
case 'click':
|
|
89
|
+
return { ...common, type: event.type, node: node as NativeNode<'button'> }
|
|
90
|
+
case 'input':
|
|
91
|
+
return {
|
|
92
|
+
...common,
|
|
93
|
+
type: event.type,
|
|
94
|
+
node: node as NativeNode<'input'>,
|
|
95
|
+
value: event.value,
|
|
96
|
+
revision: event.input_revision,
|
|
97
|
+
}
|
|
98
|
+
case 'key_down':
|
|
99
|
+
case 'key_up':
|
|
100
|
+
return {
|
|
101
|
+
...common,
|
|
102
|
+
type: event.type,
|
|
103
|
+
key: event.key,
|
|
104
|
+
...(event.key_char === undefined ? {} : { keyChar: event.key_char }),
|
|
105
|
+
modifiers: Object.freeze({ ...event.modifiers }),
|
|
106
|
+
}
|
|
107
|
+
case 'pointer_down':
|
|
108
|
+
case 'pointer_up':
|
|
109
|
+
return {
|
|
110
|
+
...common,
|
|
111
|
+
type: event.type,
|
|
112
|
+
button: event.button,
|
|
113
|
+
x: event.x,
|
|
114
|
+
y: event.y,
|
|
115
|
+
modifiers: Object.freeze({ ...event.modifiers }),
|
|
116
|
+
}
|
|
117
|
+
case 'focus':
|
|
118
|
+
case 'blur':
|
|
119
|
+
return { ...common, type: event.type }
|
|
120
|
+
case 'dismiss':
|
|
121
|
+
return {
|
|
122
|
+
...common,
|
|
123
|
+
type: event.type,
|
|
124
|
+
node: node as NativeNode<'overlay'>,
|
|
125
|
+
reason: event.reason,
|
|
126
|
+
}
|
|
127
|
+
case 'split_resize':
|
|
128
|
+
return {
|
|
129
|
+
...common,
|
|
130
|
+
type: event.type,
|
|
131
|
+
node: node as NativeNode<'split'>,
|
|
132
|
+
ratio: event.ratio,
|
|
133
|
+
source: event.source,
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export interface DispatchPlan {
|
|
139
|
+
readonly targetId: number
|
|
140
|
+
readonly cancellation: AbortController
|
|
141
|
+
readonly data: ReturnType<typeof payload>
|
|
142
|
+
deliveries: Delivery[]
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** A queued event never gains authority from a later replacement registration. */
|
|
146
|
+
export function prunePlan(plan: DispatchPlan): boolean {
|
|
147
|
+
const revoked = ({ binding }: Delivery) => binding.controller.signal.aborted
|
|
148
|
+
// Replace, never compact in place: an active dispatch may be iterating the old array.
|
|
149
|
+
if (plan.deliveries.some(revoked))
|
|
150
|
+
plan.deliveries = plan.deliveries.filter((delivery) => !revoked(delivery))
|
|
151
|
+
return !plan.cancellation.signal.aborted && plan.deliveries.length !== 0
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** One live binding per owned node, event type and phase. No global roots. */
|
|
155
|
+
export class EventBindings {
|
|
156
|
+
private readonly nodes = new Map<number, Map<BindingKey, Binding>>()
|
|
157
|
+
private readonly plans = new Set<DispatchPlan>()
|
|
158
|
+
|
|
159
|
+
constructor(private readonly changed: () => void) {}
|
|
160
|
+
|
|
161
|
+
get size(): number {
|
|
162
|
+
let size = 0
|
|
163
|
+
for (const bindings of this.nodes.values()) size += bindings.size
|
|
164
|
+
return size
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
register<T extends NativeEventType>(
|
|
168
|
+
id: number,
|
|
169
|
+
node: NativeNode,
|
|
170
|
+
type: T,
|
|
171
|
+
listener: Listener<NativeEventMap[T]>,
|
|
172
|
+
options: ListenerOptions = {}
|
|
173
|
+
): () => void {
|
|
174
|
+
if (
|
|
175
|
+
!eventTypes.includes(type) ||
|
|
176
|
+
typeof listener !== 'function' ||
|
|
177
|
+
options === null ||
|
|
178
|
+
typeof options !== 'object' ||
|
|
179
|
+
Array.isArray(options) ||
|
|
180
|
+
Object.keys(options).some((key) => key !== 'capture') ||
|
|
181
|
+
('capture' in options && typeof options.capture !== 'boolean')
|
|
182
|
+
)
|
|
183
|
+
throw new NativeStateError(
|
|
184
|
+
'invalid_state',
|
|
185
|
+
'Invalid native listener type, function or options'
|
|
186
|
+
)
|
|
187
|
+
const key = bindingKeys[type][options.capture ? 'capture' : 'bubble']
|
|
188
|
+
const bindings = this.nodes.get(id) ?? new Map<BindingKey, Binding>()
|
|
189
|
+
this.nodes.set(id, bindings)
|
|
190
|
+
const previous = bindings.get(key)
|
|
191
|
+
const subscription: Subscription = { remove: undefined }
|
|
192
|
+
const binding: Binding = {
|
|
193
|
+
node,
|
|
194
|
+
controller: new AbortController(),
|
|
195
|
+
// prepare selects only this registered type; invocation keeps the
|
|
196
|
+
// listener outside the registry-owned removal callback's scope.
|
|
197
|
+
invoke: invocation(listener),
|
|
198
|
+
subscription,
|
|
199
|
+
}
|
|
200
|
+
subscription.remove = () => {
|
|
201
|
+
if (this.nodes.get(id)?.get(key) !== binding) return
|
|
202
|
+
bindings.delete(key)
|
|
203
|
+
if (!bindings.size) this.nodes.delete(id)
|
|
204
|
+
revoke(binding)
|
|
205
|
+
this.changed()
|
|
206
|
+
}
|
|
207
|
+
bindings.set(key, binding)
|
|
208
|
+
// Publish first: an abort handler may install a newer binding reentrantly.
|
|
209
|
+
if (previous) revoke(previous)
|
|
210
|
+
this.changed()
|
|
211
|
+
return unsubscribe(subscription)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
retain(ids: ReadonlyMap<number, unknown>): void {
|
|
215
|
+
for (const [id, bindings] of this.nodes) {
|
|
216
|
+
if (ids.has(id)) continue
|
|
217
|
+
this.nodes.delete(id)
|
|
218
|
+
for (const binding of bindings.values()) revoke(binding)
|
|
219
|
+
}
|
|
220
|
+
for (const plan of this.plans) if (!ids.has(plan.targetId)) plan.cancellation.abort()
|
|
221
|
+
this.changed()
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
clear(): void {
|
|
225
|
+
const removed = [...this.nodes.values()]
|
|
226
|
+
this.nodes.clear()
|
|
227
|
+
for (const bindings of removed) for (const binding of bindings.values()) revoke(binding)
|
|
228
|
+
for (const plan of this.plans) plan.cancellation.abort()
|
|
229
|
+
this.plans.clear()
|
|
230
|
+
this.changed()
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
release(plan: DispatchPlan): void {
|
|
234
|
+
this.plans.delete(plan)
|
|
235
|
+
plan.deliveries.length = 0
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
prepare(event: RoutedEvent, path: readonly NativeNode[]): DispatchPlan | undefined {
|
|
239
|
+
// Events without a binding on their path allocate nothing here.
|
|
240
|
+
const { capture, bubble } = bindingKeys[event.type]
|
|
241
|
+
const target = path.length - 1
|
|
242
|
+
let deliveries: Delivery[] | undefined
|
|
243
|
+
for (let index = 0; index < path.length; index++) {
|
|
244
|
+
const binding = this.nodes.get(event.path[index]!)?.get(capture)
|
|
245
|
+
if (binding)
|
|
246
|
+
(deliveries ??= []).push({ binding, phase: index === target ? 'target' : 'capture' })
|
|
247
|
+
}
|
|
248
|
+
for (let index = target; index >= 0; index--) {
|
|
249
|
+
const binding = this.nodes.get(event.path[index]!)?.get(bubble)
|
|
250
|
+
if (binding)
|
|
251
|
+
(deliveries ??= []).push({ binding, phase: index === target ? 'target' : 'bubble' })
|
|
252
|
+
}
|
|
253
|
+
if (!deliveries) return
|
|
254
|
+
const plan = {
|
|
255
|
+
targetId: event.node_id,
|
|
256
|
+
cancellation: new AbortController(),
|
|
257
|
+
data: payload(event, path.at(-1)!, path),
|
|
258
|
+
deliveries,
|
|
259
|
+
}
|
|
260
|
+
this.plans.add(plan)
|
|
261
|
+
return plan
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Binding cancellation releases an uncooperative callback without blocking ingestion. */
|
|
266
|
+
export async function dispatchPlan(plan: DispatchPlan, targetIsLive: () => boolean): Promise<void> {
|
|
267
|
+
let stopped = false
|
|
268
|
+
let immediate = false
|
|
269
|
+
let previous: NativeNode | undefined
|
|
270
|
+
let previousPhase: NativeUIEvent['phase'] | undefined
|
|
271
|
+
try {
|
|
272
|
+
for (const { binding, phase } of plan.deliveries) {
|
|
273
|
+
if (
|
|
274
|
+
!targetIsLive() ||
|
|
275
|
+
plan.cancellation.signal.aborted ||
|
|
276
|
+
immediate ||
|
|
277
|
+
(stopped && (binding.node !== previous || phase !== 'target' || previousPhase !== 'target'))
|
|
278
|
+
)
|
|
279
|
+
break
|
|
280
|
+
if (binding.controller.signal.aborted) continue
|
|
281
|
+
previous = binding.node
|
|
282
|
+
previousPhase = phase
|
|
283
|
+
const signal = binding.controller.signal
|
|
284
|
+
let executing = true
|
|
285
|
+
const stop = (all: boolean) => {
|
|
286
|
+
if (!executing || signal.aborted) return
|
|
287
|
+
stopped = true
|
|
288
|
+
immediate ||= all
|
|
289
|
+
}
|
|
290
|
+
const event: NativeUIEvent = Object.freeze({
|
|
291
|
+
...plan.data,
|
|
292
|
+
currentTarget: binding.node,
|
|
293
|
+
phase,
|
|
294
|
+
signal,
|
|
295
|
+
get propagationStopped() {
|
|
296
|
+
return stopped
|
|
297
|
+
},
|
|
298
|
+
stopPropagation: () => stop(false),
|
|
299
|
+
stopImmediatePropagation: () => stop(true),
|
|
300
|
+
})
|
|
301
|
+
const planSignal = plan.cancellation.signal
|
|
302
|
+
let cancel: (() => void) | undefined
|
|
303
|
+
try {
|
|
304
|
+
const result = binding.invoke?.(event)
|
|
305
|
+
if (result === null || (typeof result !== 'object' && typeof result !== 'function')) {
|
|
306
|
+
// A synchronous listener needs no cancellation race. Two turns keep
|
|
307
|
+
// the microtask interleaving of the race path below.
|
|
308
|
+
await undefined
|
|
309
|
+
await undefined
|
|
310
|
+
} else {
|
|
311
|
+
const cancelled = new Promise<void>((resolve) => {
|
|
312
|
+
cancel = resolve
|
|
313
|
+
})
|
|
314
|
+
// The listener may have revoked its binding or the plan before returning.
|
|
315
|
+
if (signal.aborted || planSignal.aborted) cancel!()
|
|
316
|
+
signal.addEventListener('abort', cancel!, { once: true })
|
|
317
|
+
planSignal.addEventListener('abort', cancel!, { once: true })
|
|
318
|
+
await Promise.race([result, cancelled])
|
|
319
|
+
}
|
|
320
|
+
} finally {
|
|
321
|
+
executing = false
|
|
322
|
+
if (cancel) {
|
|
323
|
+
signal.removeEventListener('abort', cancel)
|
|
324
|
+
planSignal.removeEventListener('abort', cancel)
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
} finally {
|
|
329
|
+
plan.deliveries.length = 0
|
|
330
|
+
}
|
|
331
|
+
}
|
package/src/files.ts
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FILE_ACCESS,
|
|
3
|
+
MAX_FILE_CHUNK_BYTES,
|
|
4
|
+
parseFilePath,
|
|
5
|
+
parseRootId,
|
|
6
|
+
type FileAccess,
|
|
7
|
+
type FileKind,
|
|
8
|
+
type FileOperation,
|
|
9
|
+
type FileResult,
|
|
10
|
+
type WriteMode,
|
|
11
|
+
} from '@bun-native/protocol'
|
|
12
|
+
|
|
13
|
+
export interface FileGrantOptions {
|
|
14
|
+
/** At most the root's launch access. Defaults to `read`. */
|
|
15
|
+
readonly access?: FileAccess
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface NativeFileStat {
|
|
19
|
+
readonly kind: FileKind
|
|
20
|
+
readonly size: number
|
|
21
|
+
/** Milliseconds since the Unix epoch. */
|
|
22
|
+
readonly modifiedMs: number
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface NativeDirectoryPage {
|
|
26
|
+
/** At most 64 entries, sorted by UTF-8 name bytes. */
|
|
27
|
+
readonly entries: readonly Readonly<{ name: string; kind: FileKind }>[]
|
|
28
|
+
/** False when more entries follow; pass the last name as `after`. */
|
|
29
|
+
readonly complete: boolean
|
|
30
|
+
/** Names in the directory that are not valid UTF-8 or contain control characters. */
|
|
31
|
+
readonly omitted: number
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface NativeFileChunk {
|
|
35
|
+
readonly bytes: Uint8Array
|
|
36
|
+
readonly endOfFile: boolean
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Scoped authority over one launch root. Paths are relative names joined by
|
|
41
|
+
* `/`; symbolic links are never followed and `..` is rejected. Closing the
|
|
42
|
+
* grant, its window or the application revokes it; later results are withheld.
|
|
43
|
+
*/
|
|
44
|
+
export interface NativeFileGrant {
|
|
45
|
+
readonly root: string
|
|
46
|
+
readonly access: FileAccess
|
|
47
|
+
/** False once closed or revoked. */
|
|
48
|
+
readonly active: boolean
|
|
49
|
+
stat(path: string): Promise<NativeFileStat>
|
|
50
|
+
list(path?: string, options?: { readonly after?: string }): Promise<NativeDirectoryPage>
|
|
51
|
+
read(
|
|
52
|
+
path: string,
|
|
53
|
+
options?: { readonly offset?: number; readonly length?: number }
|
|
54
|
+
): Promise<NativeFileChunk>
|
|
55
|
+
/** Writes one chunk of at most 16384 bytes; resolves with the bytes written. */
|
|
56
|
+
write(path: string, bytes: Uint8Array, options?: { readonly mode?: WriteMode }): Promise<number>
|
|
57
|
+
createDirectory(path: string): Promise<void>
|
|
58
|
+
/** Removes a file, an empty directory or a link entry (never its target). */
|
|
59
|
+
remove(path: string): Promise<void>
|
|
60
|
+
/** Idempotent. Revokes locally first, then closes the native descriptor. */
|
|
61
|
+
close(): Promise<void>
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function fileGrantRequest(root: unknown, options: FileGrantOptions = {}) {
|
|
65
|
+
if (typeof root !== 'string') throw new TypeError('A filesystem root ID must be a string')
|
|
66
|
+
try {
|
|
67
|
+
parseRootId(root)
|
|
68
|
+
} catch {
|
|
69
|
+
throw new TypeError('Unknown filesystem root ID format: ' + JSON.stringify(root))
|
|
70
|
+
}
|
|
71
|
+
if (!options || typeof options !== 'object' || Array.isArray(options))
|
|
72
|
+
throw new TypeError('File grant options must be an object')
|
|
73
|
+
if (Object.keys(options).some((key) => key !== 'access'))
|
|
74
|
+
throw new TypeError('Unknown file grant option')
|
|
75
|
+
const access = Object.hasOwn(options, 'access') ? options.access : 'read'
|
|
76
|
+
if (!FILE_ACCESS.includes(access as FileAccess))
|
|
77
|
+
throw new TypeError('File grant access must be "read" or "read_write"')
|
|
78
|
+
return { root, access: access as FileAccess }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function path(value: unknown): string {
|
|
82
|
+
if (typeof value !== 'string') throw new TypeError('A grant path must be a string')
|
|
83
|
+
try {
|
|
84
|
+
return parseFilePath(value)
|
|
85
|
+
} catch (error) {
|
|
86
|
+
throw new TypeError((error as Error).message)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function options(value: unknown, allowed: readonly string[]): Record<string, unknown> {
|
|
91
|
+
if (value === undefined) return {}
|
|
92
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
93
|
+
throw new TypeError('File operation options must be an object')
|
|
94
|
+
if (Object.keys(value).some((key) => !allowed.includes(key)))
|
|
95
|
+
throw new TypeError('Unknown file operation option')
|
|
96
|
+
return value as Record<string, unknown>
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function safe(value: unknown, field: string, min: number, max: number, fallback: number): number {
|
|
100
|
+
if (value === undefined) return fallback
|
|
101
|
+
if (typeof value !== 'number' || !Number.isSafeInteger(value) || value < min || value > max)
|
|
102
|
+
throw new TypeError(field + ' must be an integer between ' + min + ' and ' + max)
|
|
103
|
+
return value
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export type FileCall = {
|
|
107
|
+
path: string
|
|
108
|
+
operation: FileOperation
|
|
109
|
+
finish: (result: FileResult) => unknown
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const fileCalls = {
|
|
113
|
+
stat(target: unknown): FileCall {
|
|
114
|
+
return {
|
|
115
|
+
path: path(target),
|
|
116
|
+
operation: { kind: 'stat' },
|
|
117
|
+
finish: (result) => {
|
|
118
|
+
if (result.kind !== 'stat') throw new TypeError('unreachable')
|
|
119
|
+
return Object.freeze({
|
|
120
|
+
kind: result.file_kind,
|
|
121
|
+
size: result.size,
|
|
122
|
+
modifiedMs: result.modified_ms,
|
|
123
|
+
})
|
|
124
|
+
},
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
list(target: unknown = '', value?: unknown): FileCall {
|
|
128
|
+
const { after } = options(value, ['after'])
|
|
129
|
+
if (after !== undefined && typeof after !== 'string')
|
|
130
|
+
throw new TypeError('List `after` must be a file name')
|
|
131
|
+
const operation: FileOperation =
|
|
132
|
+
after === undefined ? { kind: 'list' } : { kind: 'list', after: path(after) }
|
|
133
|
+
if (operation.kind === 'list' && (operation.after === '' || operation.after?.includes('/')))
|
|
134
|
+
throw new TypeError('List `after` must be a single file name')
|
|
135
|
+
return {
|
|
136
|
+
path: path(target),
|
|
137
|
+
operation,
|
|
138
|
+
finish: (result) => {
|
|
139
|
+
if (result.kind !== 'list') throw new TypeError('unreachable')
|
|
140
|
+
return Object.freeze({
|
|
141
|
+
entries: Object.freeze(result.entries.map((entry) => Object.freeze({ ...entry }))),
|
|
142
|
+
complete: result.complete,
|
|
143
|
+
omitted: result.omitted,
|
|
144
|
+
})
|
|
145
|
+
},
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
read(target: unknown, value?: unknown): FileCall {
|
|
149
|
+
const data = options(value, ['offset', 'length'])
|
|
150
|
+
return {
|
|
151
|
+
path: path(target),
|
|
152
|
+
operation: {
|
|
153
|
+
kind: 'read',
|
|
154
|
+
offset: safe(
|
|
155
|
+
data['offset'],
|
|
156
|
+
'Read offset',
|
|
157
|
+
0,
|
|
158
|
+
Number.MAX_SAFE_INTEGER - MAX_FILE_CHUNK_BYTES,
|
|
159
|
+
0
|
|
160
|
+
),
|
|
161
|
+
length: safe(data['length'], 'Read length', 1, MAX_FILE_CHUNK_BYTES, MAX_FILE_CHUNK_BYTES),
|
|
162
|
+
},
|
|
163
|
+
finish: (result) => {
|
|
164
|
+
if (result.kind !== 'read') throw new TypeError('unreachable')
|
|
165
|
+
return Object.freeze({
|
|
166
|
+
bytes: Uint8Array.fromBase64(result.data),
|
|
167
|
+
endOfFile: result.end_of_file,
|
|
168
|
+
})
|
|
169
|
+
},
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
write(target: unknown, bytes: unknown, value?: unknown): FileCall {
|
|
173
|
+
if (!(bytes instanceof Uint8Array)) throw new TypeError('File data must be a Uint8Array')
|
|
174
|
+
if (bytes.byteLength > MAX_FILE_CHUNK_BYTES)
|
|
175
|
+
throw new TypeError('File data exceeds ' + MAX_FILE_CHUNK_BYTES + ' bytes; write in chunks')
|
|
176
|
+
const { mode = 'create' } = options(value, ['mode'])
|
|
177
|
+
if (mode !== 'create' && mode !== 'truncate' && mode !== 'append')
|
|
178
|
+
throw new TypeError('Write mode must be "create", "truncate" or "append"')
|
|
179
|
+
const file = path(target)
|
|
180
|
+
if (!file) throw new TypeError('The grant root itself cannot be written')
|
|
181
|
+
return {
|
|
182
|
+
path: file,
|
|
183
|
+
operation: { kind: 'write', mode, data: bytes.toBase64() },
|
|
184
|
+
finish: (result) => {
|
|
185
|
+
if (result.kind !== 'written') throw new TypeError('unreachable')
|
|
186
|
+
return result.bytes
|
|
187
|
+
},
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
createDirectory(target: unknown): FileCall {
|
|
191
|
+
const file = path(target)
|
|
192
|
+
if (!file) throw new TypeError('The grant root itself cannot be created')
|
|
193
|
+
return { path: file, operation: { kind: 'create_directory' }, finish: () => undefined }
|
|
194
|
+
},
|
|
195
|
+
remove(target: unknown): FileCall {
|
|
196
|
+
const file = path(target)
|
|
197
|
+
if (!file) throw new TypeError('The grant root itself cannot be removed')
|
|
198
|
+
return { path: file, operation: { kind: 'remove' }, finish: () => undefined }
|
|
199
|
+
},
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Internal registry entry. Consumers only receive its frozen handle. */
|
|
203
|
+
export class FileGrantRegistration {
|
|
204
|
+
readonly handle: NativeFileGrant
|
|
205
|
+
private revoked = false
|
|
206
|
+
private closing: Promise<void> | undefined
|
|
207
|
+
|
|
208
|
+
constructor(
|
|
209
|
+
readonly id: number,
|
|
210
|
+
readonly windowId: number,
|
|
211
|
+
root: string,
|
|
212
|
+
access: FileAccess,
|
|
213
|
+
call: (registration: FileGrantRegistration, request: FileCall) => Promise<unknown>,
|
|
214
|
+
close: (registration: FileGrantRegistration) => Promise<void>
|
|
215
|
+
) {
|
|
216
|
+
const run = <T>(request: () => FileCall) => {
|
|
217
|
+
try {
|
|
218
|
+
return call(this, request()) as Promise<T>
|
|
219
|
+
} catch (error) {
|
|
220
|
+
return Promise.reject(error)
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
const live = () => this.live
|
|
224
|
+
this.handle = Object.freeze({
|
|
225
|
+
root,
|
|
226
|
+
access,
|
|
227
|
+
get active() {
|
|
228
|
+
return live()
|
|
229
|
+
},
|
|
230
|
+
stat: (target: string) => run<NativeFileStat>(() => fileCalls.stat(target)),
|
|
231
|
+
list: (target?: string, value?: { readonly after?: string }) =>
|
|
232
|
+
run<NativeDirectoryPage>(() => fileCalls.list(target, value)),
|
|
233
|
+
read: (target: string, value?: { readonly offset?: number; readonly length?: number }) =>
|
|
234
|
+
run<NativeFileChunk>(() => fileCalls.read(target, value)),
|
|
235
|
+
write: (target: string, bytes: Uint8Array, value?: { readonly mode?: WriteMode }) =>
|
|
236
|
+
run<number>(() => fileCalls.write(target, bytes, value)),
|
|
237
|
+
createDirectory: (target: string) => run<void>(() => fileCalls.createDirectory(target)),
|
|
238
|
+
remove: (target: string) => run<void>(() => fileCalls.remove(target)),
|
|
239
|
+
close: () => {
|
|
240
|
+
this.closing ??= this.revoked ? Promise.resolve() : close(this)
|
|
241
|
+
return this.closing
|
|
242
|
+
},
|
|
243
|
+
})
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
get live(): boolean {
|
|
247
|
+
return !this.revoked
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
revoke(): void {
|
|
251
|
+
this.revoked = true
|
|
252
|
+
}
|
|
253
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
export { createApplication } from './application.ts'
|
|
2
|
+
export { NativeStateError, UnsupportedFeatureError } from './types.ts'
|
|
3
|
+
export { ProtocolError } from '@bun-native/protocol'
|
|
4
|
+
export { NativeCommandError, NativeHostExitError } from '@bun-native/runtime'
|
|
5
|
+
export type { NativeHostExitDiagnostic, NativeHostExitReason } from '@bun-native/runtime'
|
|
6
|
+
export { NativeServiceError } from './services.ts'
|
|
7
|
+
export { DECORATION_FIELDS, SPLIT_DIVIDER } from '@bun-native/protocol'
|
|
8
|
+
export { defaultTheme, defineTheme } from './theme.ts'
|
|
9
|
+
export type { ResolvedStyle, Theme, ThemedStyle, ThemeTokens } from './theme.ts'
|
|
10
|
+
export { onDismiss, type DismissEvent } from './overlays.ts'
|
|
11
|
+
export { onSplitResize } from './splits.ts'
|
|
12
|
+
export { installedRelease, reportHealthy, type InstalledRelease } from '@bun-native/runtime'
|
|
13
|
+
export type {
|
|
14
|
+
DisplayChangeEvent,
|
|
15
|
+
NativeDisplaySnapshot,
|
|
16
|
+
NativeClipboardText,
|
|
17
|
+
ServiceCapabilities,
|
|
18
|
+
ServicePermissions,
|
|
19
|
+
ServiceRequestOptions,
|
|
20
|
+
} from './services.ts'
|
|
21
|
+
export type {
|
|
22
|
+
GlobalShortcutEvent,
|
|
23
|
+
GlobalShortcutKey,
|
|
24
|
+
GlobalShortcutOptions,
|
|
25
|
+
NativeGlobalShortcut,
|
|
26
|
+
} from './shortcuts.ts'
|
|
27
|
+
export type { NativeNotification, NotificationEvent, NotificationOptions } from './notifications.ts'
|
|
28
|
+
export type {
|
|
29
|
+
AppMenuItemEvent,
|
|
30
|
+
MenuEntryOptions,
|
|
31
|
+
MenuOptions,
|
|
32
|
+
MenuShortcutKey,
|
|
33
|
+
MenuShortcutOptions,
|
|
34
|
+
NativeMenuBar,
|
|
35
|
+
} from './app-menu.ts'
|
|
36
|
+
export type { ContextMenuPosition } from './context-menus.ts'
|
|
37
|
+
export type { DropTargetEvent, NativeDropTarget, NativeDroppedItem } from './drops.ts'
|
|
38
|
+
export type {
|
|
39
|
+
FileGrantOptions,
|
|
40
|
+
NativeDirectoryPage,
|
|
41
|
+
NativeFileChunk,
|
|
42
|
+
NativeFileGrant,
|
|
43
|
+
NativeFileStat,
|
|
44
|
+
} from './files.ts'
|
|
45
|
+
export type { NativeProcess, NativeProcessExit, ProcessEvent, ProcessOptions } from './processes.ts'
|
|
46
|
+
export type {
|
|
47
|
+
FileDialogFilter,
|
|
48
|
+
OpenDialogOptions,
|
|
49
|
+
OpenDialogResult,
|
|
50
|
+
SaveDialogOptions,
|
|
51
|
+
SaveDialogResult,
|
|
52
|
+
} from './dialogs.ts'
|
|
53
|
+
export type {
|
|
54
|
+
ConfirmDialogOptions,
|
|
55
|
+
MessageDialogButton,
|
|
56
|
+
MessageDialogOptions,
|
|
57
|
+
MessageDialogResult,
|
|
58
|
+
} from './message-dialogs.ts'
|
|
59
|
+
export type {
|
|
60
|
+
ApplicationOptions,
|
|
61
|
+
ClickEvent,
|
|
62
|
+
ContainerKind,
|
|
63
|
+
DismissRequestEvent,
|
|
64
|
+
Element,
|
|
65
|
+
ElementKind,
|
|
66
|
+
EventContext,
|
|
67
|
+
FocusEvent,
|
|
68
|
+
KeyEvent,
|
|
69
|
+
KeyModifiers,
|
|
70
|
+
ListenerOptions,
|
|
71
|
+
NativeEventMap,
|
|
72
|
+
NativeEventType,
|
|
73
|
+
NativeUIEvent,
|
|
74
|
+
PointerButton,
|
|
75
|
+
PointerEvent,
|
|
76
|
+
InputEvent,
|
|
77
|
+
InputState,
|
|
78
|
+
Listener,
|
|
79
|
+
NativeApplication,
|
|
80
|
+
NativeBatch,
|
|
81
|
+
NativeCapability,
|
|
82
|
+
NativeNode,
|
|
83
|
+
NativeWindow,
|
|
84
|
+
ScrollOffset,
|
|
85
|
+
SplitResizeEvent,
|
|
86
|
+
Style,
|
|
87
|
+
WindowOptions,
|
|
88
|
+
} from './types.ts'
|