@bun-native/protocol 0.33.1 → 0.33.3
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 +34 -30
- package/package.json +1 -1
- package/src/app-menu.ts +20 -57
- package/src/dialogs.ts +18 -34
- package/src/drops.ts +3 -11
- package/src/elements.ts +70 -174
- package/src/files.ts +27 -36
- package/src/framing.ts +2 -7
- package/src/index.ts +54 -62
- package/src/message-dialogs.ts +15 -29
- package/src/messages.ts +296 -366
- package/src/processes.ts +23 -33
- package/src/services.ts +18 -42
- package/src/values.ts +9 -7
- package/src/wire-schema.ts +789 -0
package/src/elements.ts
CHANGED
|
@@ -1,57 +1,25 @@
|
|
|
1
1
|
import { ProtocolError } from './framing.ts'
|
|
2
|
-
import {
|
|
2
|
+
import { choice, integer, keys, number, object, string } from './values.ts'
|
|
3
|
+
import {
|
|
4
|
+
ACCESSIBLE_ROLES,
|
|
5
|
+
ELEMENT_KEYS,
|
|
6
|
+
LIVE_POLITENESS,
|
|
7
|
+
MAX_ACCESSIBLE_SET_SIZE,
|
|
8
|
+
OVERLAY_ANCHORS,
|
|
9
|
+
OVERLAY_NAVIGATIONS,
|
|
10
|
+
OVERLAY_SURFACES,
|
|
11
|
+
STYLE_FIELDS,
|
|
12
|
+
type AccessibleRole,
|
|
13
|
+
type Live,
|
|
14
|
+
type Style,
|
|
15
|
+
} from './wire-schema.ts'
|
|
3
16
|
|
|
4
|
-
export
|
|
5
|
-
direction?: 'row' | 'column'
|
|
6
|
-
gap?: number
|
|
7
|
-
padding?: number
|
|
8
|
-
width?: number | 'fill'
|
|
9
|
-
height?: number | 'fill'
|
|
10
|
-
min_width?: number
|
|
11
|
-
max_width?: number
|
|
12
|
-
min_height?: number
|
|
13
|
-
max_height?: number
|
|
14
|
-
flex_grow?: number
|
|
15
|
-
flex_shrink?: number
|
|
16
|
-
align_items?: 'start' | 'center' | 'end' | 'stretch'
|
|
17
|
-
justify_content?: 'start' | 'center' | 'end' | 'space_between' | 'space_around' | 'space_evenly'
|
|
18
|
-
overflow?: 'visible' | 'hidden' | 'scroll_x' | 'scroll_y' | 'scroll'
|
|
19
|
-
/** Requires `scroll_y` or `scroll`. `end` keeps a viewport already at its end pinned there. */
|
|
20
|
-
scroll_anchor?: 'start' | 'end'
|
|
21
|
-
background?: number
|
|
22
|
-
color?: number
|
|
23
|
-
font_size?: number
|
|
24
|
-
/** `style_decoration_v1`: one uniform border width, 0–64 logical pixels. */
|
|
25
|
-
border_width?: number
|
|
26
|
-
/** `style_decoration_v1`: unsigned 24-bit RGB. */
|
|
27
|
-
border_color?: number
|
|
28
|
-
/** `style_decoration_v1`: one radius for every corner, 0–4,096 logical pixels. */
|
|
29
|
-
corner_radius?: number
|
|
30
|
-
/** `style_decoration_v1`: 0–1, multiplying descendants too. */
|
|
31
|
-
opacity?: number
|
|
32
|
-
/** `style_decoration_v1`: inherited 100–900 in steps of 100. */
|
|
33
|
-
font_weight?: 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900
|
|
34
|
-
}
|
|
17
|
+
export type { Style }
|
|
35
18
|
|
|
36
19
|
type LeafStyle = Omit<Style, 'overflow' | 'scroll_anchor'> & { overflow?: 'visible' | 'hidden' }
|
|
37
20
|
/** A split's `direction` selects its axis; gap, alignment and scrolling do not apply. */
|
|
38
21
|
export type SplitStyle = Omit<LeafStyle, 'gap' | 'align_items' | 'justify_content'>
|
|
39
22
|
|
|
40
|
-
/** Main-axis thickness of the host-drawn split divider, in logical pixels. */
|
|
41
|
-
export const SPLIT_DIVIDER = 6
|
|
42
|
-
|
|
43
|
-
/** Style fields that require `style_decoration_v1`. */
|
|
44
|
-
export const DECORATION_FIELDS = Object.freeze([
|
|
45
|
-
'border_width',
|
|
46
|
-
'border_color',
|
|
47
|
-
'corner_radius',
|
|
48
|
-
'opacity',
|
|
49
|
-
'font_weight',
|
|
50
|
-
] as const)
|
|
51
|
-
|
|
52
|
-
/** Overlays nested inside overlays, the outermost counting as one. */
|
|
53
|
-
export const MAX_OVERLAY_DEPTH = 8
|
|
54
|
-
|
|
55
23
|
/** Requires the `overlay_keyboard_v1` capability; defaults are omitted when normalized. */
|
|
56
24
|
export interface OverlayKeyboard {
|
|
57
25
|
/** Focus the layer's first enabled control when it opens. */
|
|
@@ -103,9 +71,6 @@ export interface AccessibleLabel {
|
|
|
103
71
|
accessible_label?: string
|
|
104
72
|
}
|
|
105
73
|
|
|
106
|
-
/** The largest `accessible_set_size` and `accessible_position`. */
|
|
107
|
-
const MAX_ACCESSIBLE_SET_SIZE = 2_147_483_647
|
|
108
|
-
|
|
109
74
|
/**
|
|
110
75
|
* Requires `accessibility_lists_v1`. A box without `accessible_role` is layout
|
|
111
76
|
* only and has no accessible node. A list's `accessible_set_size` counts every
|
|
@@ -113,7 +78,7 @@ const MAX_ACCESSIBLE_SET_SIZE = 2_147_483_647
|
|
|
113
78
|
* in that full set, so assistive technology knows a windowed list's extent.
|
|
114
79
|
*/
|
|
115
80
|
export interface BoxAccessibility {
|
|
116
|
-
accessible_role?:
|
|
81
|
+
accessible_role?: AccessibleRole
|
|
117
82
|
/** Only with a role: the list's or item's accessible name. */
|
|
118
83
|
accessible_label?: string
|
|
119
84
|
/** Only on a `list`. */
|
|
@@ -133,7 +98,7 @@ export type Element =
|
|
|
133
98
|
* Requires `accessibility_lists_v1`: announce the text when the node
|
|
134
99
|
* appears and whenever it changes.
|
|
135
100
|
*/
|
|
136
|
-
accessible_live?:
|
|
101
|
+
accessible_live?: Live
|
|
137
102
|
style?: LeafStyle
|
|
138
103
|
}
|
|
139
104
|
| { kind: 'button'; text: string; disabled?: boolean; style?: LeafStyle }
|
|
@@ -155,101 +120,55 @@ export type Element =
|
|
|
155
120
|
| ({ kind: 'split'; ratio?: number; min_size?: number; style?: SplitStyle } & AccessibleLabel)
|
|
156
121
|
| OverlayElement
|
|
157
122
|
|
|
158
|
-
function choice<T extends string>(value: unknown, name: string, values: readonly T[]): T {
|
|
159
|
-
for (const candidate of values) if (candidate === value) return candidate
|
|
160
|
-
throw new ProtocolError(name + ' must be one of ' + values.join(', '))
|
|
161
|
-
}
|
|
162
|
-
|
|
163
123
|
function requireLeafStyle(style: Style): asserts style is LeafStyle {
|
|
164
124
|
if (style.overflow !== undefined && style.overflow !== 'visible' && style.overflow !== 'hidden')
|
|
165
125
|
throw new ProtocolError('Scrolling is supported only on box and overlay elements')
|
|
166
126
|
}
|
|
167
127
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
'width',
|
|
173
|
-
'height',
|
|
174
|
-
'min_width',
|
|
175
|
-
'max_width',
|
|
176
|
-
'min_height',
|
|
177
|
-
'max_height',
|
|
178
|
-
'flex_grow',
|
|
179
|
-
'flex_shrink',
|
|
180
|
-
'align_items',
|
|
181
|
-
'justify_content',
|
|
182
|
-
'overflow',
|
|
183
|
-
'scroll_anchor',
|
|
184
|
-
'background',
|
|
185
|
-
'color',
|
|
186
|
-
'font_size',
|
|
187
|
-
...DECORATION_FIELDS
|
|
188
|
-
)
|
|
189
|
-
|
|
128
|
+
/**
|
|
129
|
+
* One pass over the members present, each checked by its generated field
|
|
130
|
+
* description. Inherited properties are ignored, as `keys` ignores them.
|
|
131
|
+
*/
|
|
190
132
|
function parseStyle(value: unknown): Style {
|
|
191
133
|
const data = object(value)
|
|
192
|
-
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
if (
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
134
|
+
const style: Record<string, unknown> = {}
|
|
135
|
+
for (const key of Object.keys(data)) {
|
|
136
|
+
const field = STYLE_FIELDS.get(key)
|
|
137
|
+
if (field === undefined) throw new ProtocolError('Unknown message property: ' + key)
|
|
138
|
+
const member = data[key]
|
|
139
|
+
switch (field.type) {
|
|
140
|
+
case 'number':
|
|
141
|
+
style[key] = number(member, key, field.min, field.max)
|
|
142
|
+
break
|
|
143
|
+
case 'dimension':
|
|
144
|
+
style[key] = member === 'fill' ? 'fill' : number(member, key, field.min, field.max)
|
|
145
|
+
break
|
|
146
|
+
case 'color':
|
|
147
|
+
style[key] = integer(member, key, 0, 0xff_ffff)
|
|
148
|
+
break
|
|
149
|
+
case 'enum':
|
|
150
|
+
style[key] = choice(member, key, field.values)
|
|
151
|
+
break
|
|
152
|
+
case 'weight':
|
|
153
|
+
style[key] = integer(member, key, field.min, field.max)
|
|
154
|
+
if ((style[key] as number) % field.step !== 0)
|
|
155
|
+
throw new ProtocolError(key + ' must be a multiple of ' + field.step)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const parsed = style as Style
|
|
200
159
|
for (const [minimum, maximum] of [
|
|
201
|
-
[
|
|
202
|
-
[
|
|
160
|
+
[parsed.min_width, parsed.max_width],
|
|
161
|
+
[parsed.min_height, parsed.max_height],
|
|
203
162
|
])
|
|
204
163
|
if (minimum !== undefined && maximum !== undefined && minimum > maximum)
|
|
205
164
|
throw new ProtocolError('Minimum size must not exceed maximum size')
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
style.align_items = choice(data['align_items'], 'align_items', [
|
|
214
|
-
'start',
|
|
215
|
-
'center',
|
|
216
|
-
'end',
|
|
217
|
-
'stretch',
|
|
218
|
-
])
|
|
219
|
-
if ('justify_content' in data)
|
|
220
|
-
style.justify_content = choice(data['justify_content'], 'justify_content', [
|
|
221
|
-
'start',
|
|
222
|
-
'center',
|
|
223
|
-
'end',
|
|
224
|
-
'space_between',
|
|
225
|
-
'space_around',
|
|
226
|
-
'space_evenly',
|
|
227
|
-
])
|
|
228
|
-
if ('overflow' in data)
|
|
229
|
-
style.overflow = choice(data['overflow'], 'overflow', [
|
|
230
|
-
'visible',
|
|
231
|
-
'hidden',
|
|
232
|
-
'scroll_x',
|
|
233
|
-
'scroll_y',
|
|
234
|
-
'scroll',
|
|
235
|
-
])
|
|
236
|
-
if ('scroll_anchor' in data) {
|
|
237
|
-
style.scroll_anchor = choice(data['scroll_anchor'], 'scroll_anchor', ['start', 'end'])
|
|
238
|
-
if (style.overflow !== 'scroll_y' && style.overflow !== 'scroll')
|
|
239
|
-
throw new ProtocolError('scroll_anchor requires vertical scrolling overflow')
|
|
240
|
-
}
|
|
241
|
-
if ('font_size' in data) style.font_size = number(data['font_size'], 'font_size', 1, 512)
|
|
242
|
-
if ('border_width' in data)
|
|
243
|
-
style.border_width = number(data['border_width'], 'border_width', 0, 64)
|
|
244
|
-
if ('corner_radius' in data)
|
|
245
|
-
style.corner_radius = number(data['corner_radius'], 'corner_radius', 0, 4_096)
|
|
246
|
-
if ('opacity' in data) style.opacity = number(data['opacity'], 'opacity', 0, 1)
|
|
247
|
-
if ('font_weight' in data) {
|
|
248
|
-
const weight = integer(data['font_weight'], 'font_weight', 100, 900)
|
|
249
|
-
if (weight % 100 !== 0) throw new ProtocolError('font_weight must be a multiple of 100')
|
|
250
|
-
style.font_weight = weight as NonNullable<Style['font_weight']>
|
|
251
|
-
}
|
|
252
|
-
return style
|
|
165
|
+
if (
|
|
166
|
+
parsed.scroll_anchor !== undefined &&
|
|
167
|
+
parsed.overflow !== 'scroll_y' &&
|
|
168
|
+
parsed.overflow !== 'scroll'
|
|
169
|
+
)
|
|
170
|
+
throw new ProtocolError('scroll_anchor requires vertical scrolling overflow')
|
|
171
|
+
return parsed
|
|
253
172
|
}
|
|
254
173
|
|
|
255
174
|
function accessibleLabel(element: Record<string, unknown>): AccessibleLabel {
|
|
@@ -270,7 +189,7 @@ function boxAccessibility(element: Record<string, unknown>): BoxAccessibility {
|
|
|
270
189
|
if (name in element) throw new ProtocolError(name + ' requires accessible_role')
|
|
271
190
|
return {}
|
|
272
191
|
}
|
|
273
|
-
const role = choice(element['accessible_role'], 'accessible_role',
|
|
192
|
+
const role = choice(element['accessible_role'], 'accessible_role', ACCESSIBLE_ROLES)
|
|
274
193
|
if (role === 'list') {
|
|
275
194
|
for (const name of ['accessible_position', 'accessible_selected'])
|
|
276
195
|
if (name in element) throw new ProtocolError(name + ' is only accepted on a list_item')
|
|
@@ -312,30 +231,6 @@ function boxAccessibility(element: Record<string, unknown>): BoxAccessibility {
|
|
|
312
231
|
}
|
|
313
232
|
}
|
|
314
233
|
|
|
315
|
-
const ELEMENT_BOX_KEYS = fields(
|
|
316
|
-
'kind',
|
|
317
|
-
'accessible_role',
|
|
318
|
-
'accessible_label',
|
|
319
|
-
'accessible_set_size',
|
|
320
|
-
'accessible_position',
|
|
321
|
-
'accessible_selected',
|
|
322
|
-
'style'
|
|
323
|
-
)
|
|
324
|
-
const ELEMENT_TEXT_KEYS = fields('kind', 'text', 'accessible_live', 'style')
|
|
325
|
-
const ELEMENT_BUTTON_KEYS = fields('kind', 'text', 'disabled', 'style')
|
|
326
|
-
const ELEMENT_INPUT_KEYS = fields('kind', 'value', 'placeholder', 'disabled', 'read_only', 'style')
|
|
327
|
-
const ELEMENT_SPLIT_KEYS = fields('kind', 'ratio', 'min_size', 'accessible_label', 'style')
|
|
328
|
-
const ELEMENT_OVERLAY_KEYS = fields(
|
|
329
|
-
'kind',
|
|
330
|
-
'anchor',
|
|
331
|
-
'modal',
|
|
332
|
-
'surface',
|
|
333
|
-
'autofocus',
|
|
334
|
-
'navigation',
|
|
335
|
-
'accessible_label',
|
|
336
|
-
'style'
|
|
337
|
-
)
|
|
338
|
-
|
|
339
234
|
/**
|
|
340
235
|
* Elements this module produced. Each is frozen with its style, so it is still
|
|
341
236
|
* exactly the normalized value that passed validation.
|
|
@@ -362,26 +257,27 @@ function validElement(value: unknown): Element {
|
|
|
362
257
|
const style = 'style' in element ? parseStyle(element['style']) : {}
|
|
363
258
|
switch (element['kind']) {
|
|
364
259
|
case 'box':
|
|
365
|
-
keys(element,
|
|
260
|
+
keys(element, ELEMENT_KEYS.box)
|
|
366
261
|
return { kind: 'box', ...boxAccessibility(element), style }
|
|
367
262
|
case 'text':
|
|
368
|
-
keys(element,
|
|
263
|
+
keys(element, ELEMENT_KEYS.text)
|
|
369
264
|
requireLeafStyle(style)
|
|
370
265
|
return {
|
|
371
266
|
kind: 'text',
|
|
372
267
|
text: string(element['text'], 'text'),
|
|
373
268
|
...('accessible_live' in element
|
|
374
269
|
? {
|
|
375
|
-
accessible_live: choice(
|
|
376
|
-
'
|
|
377
|
-
'
|
|
378
|
-
|
|
270
|
+
accessible_live: choice(
|
|
271
|
+
element['accessible_live'],
|
|
272
|
+
'accessible_live',
|
|
273
|
+
LIVE_POLITENESS
|
|
274
|
+
),
|
|
379
275
|
}
|
|
380
276
|
: {}),
|
|
381
277
|
style,
|
|
382
278
|
}
|
|
383
279
|
case 'button': {
|
|
384
|
-
keys(element,
|
|
280
|
+
keys(element, ELEMENT_KEYS.button)
|
|
385
281
|
requireLeafStyle(style)
|
|
386
282
|
if ('disabled' in element && typeof element['disabled'] !== 'boolean')
|
|
387
283
|
throw new ProtocolError('disabled must be a boolean')
|
|
@@ -393,7 +289,7 @@ function validElement(value: unknown): Element {
|
|
|
393
289
|
}
|
|
394
290
|
}
|
|
395
291
|
case 'input': {
|
|
396
|
-
keys(element,
|
|
292
|
+
keys(element, ELEMENT_KEYS.input)
|
|
397
293
|
requireLeafStyle(style)
|
|
398
294
|
for (const name of ['disabled', 'read_only'])
|
|
399
295
|
if (name in element && typeof element[name] !== 'boolean')
|
|
@@ -409,7 +305,7 @@ function validElement(value: unknown): Element {
|
|
|
409
305
|
}
|
|
410
306
|
}
|
|
411
307
|
case 'split': {
|
|
412
|
-
keys(element,
|
|
308
|
+
keys(element, ELEMENT_KEYS.split)
|
|
413
309
|
requireLeafStyle(style)
|
|
414
310
|
if (
|
|
415
311
|
style.gap !== undefined ||
|
|
@@ -426,10 +322,10 @@ function validElement(value: unknown): Element {
|
|
|
426
322
|
}
|
|
427
323
|
}
|
|
428
324
|
case 'overlay': {
|
|
429
|
-
keys(element,
|
|
325
|
+
keys(element, ELEMENT_KEYS.overlay)
|
|
430
326
|
const label = accessibleLabel(element)
|
|
431
327
|
const anchor =
|
|
432
|
-
'anchor' in element ? choice(element['anchor'], 'anchor',
|
|
328
|
+
'anchor' in element ? choice(element['anchor'], 'anchor', OVERLAY_ANCHORS) : 'window'
|
|
433
329
|
for (const flag of ['modal', 'autofocus'])
|
|
434
330
|
if (flag in element && typeof element[flag] !== 'boolean')
|
|
435
331
|
throw new ProtocolError(flag + ' must be a boolean')
|
|
@@ -437,12 +333,12 @@ function validElement(value: unknown): Element {
|
|
|
437
333
|
const keyboard: OverlayKeyboard = {
|
|
438
334
|
...(element['autofocus'] === true ? { autofocus: true } : {}),
|
|
439
335
|
...('navigation' in element &&
|
|
440
|
-
choice(element['navigation'], 'navigation',
|
|
336
|
+
choice(element['navigation'], 'navigation', OVERLAY_NAVIGATIONS) === 'menu'
|
|
441
337
|
? { navigation: 'menu' as const }
|
|
442
338
|
: {}),
|
|
443
339
|
}
|
|
444
340
|
const surface =
|
|
445
|
-
'surface' in element ? choice(element['surface'], 'surface',
|
|
341
|
+
'surface' in element ? choice(element['surface'], 'surface', OVERLAY_SURFACES) : 'layer'
|
|
446
342
|
if (surface === 'window') {
|
|
447
343
|
if (anchor !== 'parent')
|
|
448
344
|
throw new ProtocolError('A popup window overlay must be anchored to its parent')
|
package/src/files.ts
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { ProtocolError } from './framing.ts'
|
|
2
|
-
import {
|
|
2
|
+
import { integer, keys, object, string } from './values.ts'
|
|
3
|
+
import {
|
|
4
|
+
MAX_DIRECTORY_ENTRIES,
|
|
5
|
+
MAX_FILESYSTEM_ROOTS,
|
|
6
|
+
MAX_FILE_CHUNK_BYTES,
|
|
7
|
+
MAX_FILE_NAME_BYTES,
|
|
8
|
+
MAX_FILE_PATH_BYTES,
|
|
9
|
+
MAX_FILE_PATH_COMPONENTS,
|
|
10
|
+
MAX_ROOT_ID_BYTES,
|
|
11
|
+
MAX_ROOT_PATH_BYTES,
|
|
12
|
+
PAYLOAD_KEYS,
|
|
13
|
+
} from './wire-schema.ts'
|
|
3
14
|
|
|
4
|
-
export const MAX_FILESYSTEM_ROOTS = 8
|
|
5
|
-
export const MAX_FILE_GRANTS = 32
|
|
6
|
-
const MAX_FILE_PATH_BYTES = 1_024
|
|
7
|
-
const MAX_FILE_PATH_COMPONENTS = 32
|
|
8
|
-
const MAX_FILE_NAME_BYTES = 255
|
|
9
|
-
const MAX_ROOT_PATH_BYTES = 4_096
|
|
10
|
-
export const MAX_FILE_CHUNK_BYTES = 16_384
|
|
11
|
-
export const MAX_DIRECTORY_ENTRIES = 64
|
|
12
15
|
const MAX_FILE_CHUNK_BASE64 = Math.ceil(MAX_FILE_CHUNK_BYTES / 3) * 4
|
|
13
16
|
const FILESYSTEM_ROOT_ARGUMENT = '--bun-native-filesystem-root='
|
|
14
17
|
const encoder = new TextEncoder()
|
|
@@ -45,10 +48,12 @@ export type FileResult =
|
|
|
45
48
|
| { kind: 'written'; bytes: number }
|
|
46
49
|
| { kind: 'done' }
|
|
47
50
|
|
|
51
|
+
const ROOT_ID = new RegExp(`^[a-z][a-z0-9_-]{0,${MAX_ROOT_ID_BYTES - 1}}$`)
|
|
52
|
+
|
|
48
53
|
export function parseRootId(value: unknown): string {
|
|
49
|
-
if (typeof value !== 'string' ||
|
|
54
|
+
if (typeof value !== 'string' || !ROOT_ID.test(value))
|
|
50
55
|
throw new ProtocolError(
|
|
51
|
-
|
|
56
|
+
`Filesystem root ID must be 1-${MAX_ROOT_ID_BYTES} lowercase ASCII letters, digits, "-" or "_", starting with a letter`
|
|
52
57
|
)
|
|
53
58
|
return value
|
|
54
59
|
}
|
|
@@ -87,11 +92,9 @@ export function parseFileAccess(value: unknown): FileAccess {
|
|
|
87
92
|
return value as FileAccess
|
|
88
93
|
}
|
|
89
94
|
|
|
90
|
-
const ROOT_KEYS = fields('id', 'path', 'access')
|
|
91
|
-
|
|
92
95
|
function root(value: unknown): FilesystemRoot {
|
|
93
96
|
const data = object(value)
|
|
94
|
-
keys(data,
|
|
97
|
+
keys(data, PAYLOAD_KEYS.filesystem_root)
|
|
95
98
|
return {
|
|
96
99
|
id: parseRootId(data['id']),
|
|
97
100
|
path: parseRootPath(data['path']),
|
|
@@ -160,11 +163,6 @@ export function parseChunk(value: unknown): string {
|
|
|
160
163
|
const WRITE_MODES: readonly WriteMode[] = ['create', 'truncate', 'append']
|
|
161
164
|
const FILE_KINDS: readonly FileKind[] = ['file', 'directory', 'symlink', 'other']
|
|
162
165
|
|
|
163
|
-
const FILE_OPERATION_STAT_KEYS = fields('kind')
|
|
164
|
-
const FILE_OPERATION_LIST_KEYS = fields('kind', 'after')
|
|
165
|
-
const FILE_OPERATION_READ_KEYS = fields('kind', 'offset', 'length')
|
|
166
|
-
const FILE_OPERATION_WRITE_KEYS = fields('kind', 'mode', 'data')
|
|
167
|
-
|
|
168
166
|
export function parseFileOperation(value: unknown, path: string): FileOperation {
|
|
169
167
|
const data = object(value)
|
|
170
168
|
const writes = ['write', 'create_directory', 'remove'].includes(data['kind'] as string)
|
|
@@ -174,15 +172,15 @@ export function parseFileOperation(value: unknown, path: string): FileOperation
|
|
|
174
172
|
case 'stat':
|
|
175
173
|
case 'create_directory':
|
|
176
174
|
case 'remove':
|
|
177
|
-
keys(data,
|
|
175
|
+
keys(data, PAYLOAD_KEYS.file_operation.stat)
|
|
178
176
|
return { kind: data['kind'] }
|
|
179
177
|
case 'list':
|
|
180
|
-
keys(data,
|
|
178
|
+
keys(data, PAYLOAD_KEYS.file_operation.list)
|
|
181
179
|
return 'after' in data
|
|
182
180
|
? { kind: 'list', after: parseFileName(data['after']) }
|
|
183
181
|
: { kind: 'list' }
|
|
184
182
|
case 'read':
|
|
185
|
-
keys(data,
|
|
183
|
+
keys(data, PAYLOAD_KEYS.file_operation.read)
|
|
186
184
|
return {
|
|
187
185
|
kind: 'read',
|
|
188
186
|
offset: integer(
|
|
@@ -194,7 +192,7 @@ export function parseFileOperation(value: unknown, path: string): FileOperation
|
|
|
194
192
|
length: integer(data['length'], 'file read length', 1, MAX_FILE_CHUNK_BYTES),
|
|
195
193
|
}
|
|
196
194
|
case 'write': {
|
|
197
|
-
keys(data,
|
|
195
|
+
keys(data, PAYLOAD_KEYS.file_operation.write)
|
|
198
196
|
const mode = WRITE_MODES.find((item) => item === data['mode'])
|
|
199
197
|
if (!mode) throw new ProtocolError('Unknown file write mode')
|
|
200
198
|
return { kind: 'write', mode, data: parseChunk(data['data']) }
|
|
@@ -218,18 +216,11 @@ function compareBytes(left: string, right: string): number {
|
|
|
218
216
|
return a.length - b.length
|
|
219
217
|
}
|
|
220
218
|
|
|
221
|
-
const FILE_RESULT_STAT_KEYS = fields('kind', 'file_kind', 'size', 'modified_ms')
|
|
222
|
-
const FILE_RESULT_LIST_KEYS = fields('kind', 'entries', 'complete', 'omitted')
|
|
223
|
-
const FILE_LIST_ENTRY_KEYS = fields('name', 'kind')
|
|
224
|
-
const FILE_RESULT_READ_KEYS = fields('kind', 'data', 'end_of_file')
|
|
225
|
-
const FILE_RESULT_WRITTEN_KEYS = fields('kind', 'bytes')
|
|
226
|
-
const FILE_RESULT_DONE_KEYS = fields('kind')
|
|
227
|
-
|
|
228
219
|
export function parseFileResult(value: unknown): FileResult {
|
|
229
220
|
const data = object(value)
|
|
230
221
|
switch (data['kind']) {
|
|
231
222
|
case 'stat':
|
|
232
|
-
keys(data,
|
|
223
|
+
keys(data, PAYLOAD_KEYS.file_result.stat)
|
|
233
224
|
return {
|
|
234
225
|
kind: 'stat',
|
|
235
226
|
file_kind: fileKind(data['file_kind']),
|
|
@@ -241,13 +232,13 @@ export function parseFileResult(value: unknown): FileResult {
|
|
|
241
232
|
),
|
|
242
233
|
}
|
|
243
234
|
case 'list': {
|
|
244
|
-
keys(data,
|
|
235
|
+
keys(data, PAYLOAD_KEYS.file_result.list)
|
|
245
236
|
const entries = data['entries']
|
|
246
237
|
if (!Array.isArray(entries) || entries.length > MAX_DIRECTORY_ENTRIES)
|
|
247
238
|
throw new ProtocolError('Directory page exceeds ' + MAX_DIRECTORY_ENTRIES + ' entries')
|
|
248
239
|
const parsed = entries.map((entry) => {
|
|
249
240
|
const item = object(entry)
|
|
250
|
-
keys(item,
|
|
241
|
+
keys(item, PAYLOAD_KEYS.directory_entry)
|
|
251
242
|
return { name: parseFileName(item['name']), kind: fileKind(item['kind']) }
|
|
252
243
|
})
|
|
253
244
|
for (let index = 1; index < parsed.length; index++)
|
|
@@ -263,18 +254,18 @@ export function parseFileResult(value: unknown): FileResult {
|
|
|
263
254
|
}
|
|
264
255
|
}
|
|
265
256
|
case 'read':
|
|
266
|
-
keys(data,
|
|
257
|
+
keys(data, PAYLOAD_KEYS.file_result.read)
|
|
267
258
|
if (typeof data['end_of_file'] !== 'boolean')
|
|
268
259
|
throw new ProtocolError('File end-of-file flag must be a boolean')
|
|
269
260
|
return { kind: 'read', data: parseChunk(data['data']), end_of_file: data['end_of_file'] }
|
|
270
261
|
case 'written':
|
|
271
|
-
keys(data,
|
|
262
|
+
keys(data, PAYLOAD_KEYS.file_result.written)
|
|
272
263
|
return {
|
|
273
264
|
kind: 'written',
|
|
274
265
|
bytes: integer(data['bytes'], 'written bytes', 0, MAX_FILE_CHUNK_BYTES),
|
|
275
266
|
}
|
|
276
267
|
case 'done':
|
|
277
|
-
keys(data,
|
|
268
|
+
keys(data, PAYLOAD_KEYS.file_result.done)
|
|
278
269
|
return { kind: 'done' }
|
|
279
270
|
default:
|
|
280
271
|
throw new ProtocolError('Unknown file result')
|
package/src/framing.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* newline. One transaction is one batch, so this bounds a full-window mount.
|
|
4
|
-
*/
|
|
5
|
-
export const MAX_FRAME_BYTES = 16_777_216
|
|
6
|
-
/** Maximum event frame (host to runtime): one text value or service result. */
|
|
7
|
-
export const MAX_EVENT_FRAME_BYTES = 65_536
|
|
1
|
+
import { MAX_FRAME_BYTES } from './wire-schema.ts'
|
|
2
|
+
|
|
8
3
|
/** Decoder capacity kept between frames; a larger frame borrows memory while it is read. */
|
|
9
4
|
const RETAINED_FRAME_BYTES = 65_536
|
|
10
5
|
|