afield 0.0.0-stage → 0.1.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.
Files changed (43) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +165 -2
  3. package/afield +214 -0
  4. package/package.json +42 -4
  5. package/pi-extensions/afield-session.ts +59 -0
  6. package/pi-extensions/elastic-footer.ts +167 -0
  7. package/server/attach.js +264 -0
  8. package/server/auth.js +169 -0
  9. package/server/client.js +64 -0
  10. package/server/control.js +65 -0
  11. package/server/identity.js +45 -0
  12. package/server/index.js +375 -0
  13. package/server/mirror.js +159 -0
  14. package/server/origin.js +51 -0
  15. package/server/picker.js +187 -0
  16. package/server/places.js +348 -0
  17. package/server/protocol.js +51 -0
  18. package/server/quick-tunnel.js +40 -0
  19. package/server/registry.js +190 -0
  20. package/server/serve.js +100 -0
  21. package/server/session-main.js +27 -0
  22. package/server/session.js +112 -0
  23. package/server/supervisor.js +432 -0
  24. package/server/viewer.js +65 -0
  25. package/src/app.js +1683 -0
  26. package/src/apple-touch-icon.png +0 -0
  27. package/src/index.html +96 -0
  28. package/src/keys.js +59 -0
  29. package/src/pi-icon.svg +13 -0
  30. package/src/style.css +559 -0
  31. package/src/transport.js +153 -0
  32. package/src/ttyd.js +51 -0
  33. package/src/viewport.js +168 -0
  34. package/tsconfig.json +14 -0
  35. package/vendor/wterm/LICENSE +191 -0
  36. package/vendor/wterm/README.md +7 -0
  37. package/vendor/wterm/packages/@wterm/dom/src/debug.ts +347 -0
  38. package/vendor/wterm/packages/@wterm/dom/src/hyperlink.ts +8 -0
  39. package/vendor/wterm/packages/@wterm/dom/src/index.ts +13 -0
  40. package/vendor/wterm/packages/@wterm/dom/src/input.ts +662 -0
  41. package/vendor/wterm/packages/@wterm/dom/src/renderer.ts +725 -0
  42. package/vendor/wterm/packages/@wterm/dom/src/terminal.css +195 -0
  43. package/vendor/wterm/packages/@wterm/dom/src/wterm.ts +609 -0
package/src/style.css ADDED
@@ -0,0 +1,559 @@
1
+ /* The whole UI: a terminal that fills the visual viewport, a key bar under it,
2
+ and a sheet menu over both. */
3
+ :root {
4
+ --font: ui-monospace, "SF Mono", Menlo, monospace;
5
+ }
6
+
7
+ /* Color themes. `dark` is the default. The menu offers a switch that works
8
+ live (client-only), and the server can also bake one in with --theme. The
9
+ terminal's ANSI palette lives here rather than in wterm's defaults so one
10
+ set of variables drives both the terminal and the chrome around it. */
11
+ :root.theme-dark {
12
+ --bg: #0b0b0d;
13
+ --fg: #d8d8dc;
14
+ --chrome: #17171b;
15
+ --edge: #2c2c33;
16
+ --accent: #4c9aff;
17
+ --on-accent: #001;
18
+ --muted: #8a8a95;
19
+ --faint: #6f6f7a;
20
+ --inset: #0d0d11;
21
+ --raised: #22222a;
22
+ --pressed: #33333c;
23
+ --danger: #d46a5f;
24
+
25
+ --term-0: #1e1e1e; --term-1: #f44747; --term-2: #6a9955; --term-3: #d7ba7d;
26
+ --term-4: #569cd6; --term-5: #c586c0; --term-6: #4ec9b0; --term-7: #d4d4d4;
27
+ --term-8: #808080; --term-9: #f44747; --term-10: #6a9955; --term-11: #d7ba7d;
28
+ --term-12: #569cd6; --term-13: #c586c0; --term-14: #4ec9b0; --term-15: #ffffff;
29
+ --cursor: #5293ff;
30
+ }
31
+
32
+ :root.theme-light {
33
+ --bg: #fafafa;
34
+ --fg: #24262b;
35
+ --chrome: #f4f4f5;
36
+ --edge: #d4d4dc;
37
+ --accent: #3b6fe0;
38
+ --on-accent: #fff;
39
+ --muted: #76767e;
40
+ --faint: #9a9aa2;
41
+ --inset: #e9e9ec;
42
+ --raised: #e6e6ea;
43
+ --pressed: #d9d9df;
44
+ --danger: #b42318;
45
+
46
+ --term-0: #24262b; --term-1: #d13932; --term-2: #3a813d; --term-3: #a86a10;
47
+ --term-4: #3b6fe0; --term-5: #9b3f97; --term-6: #2f7b8f; --term-7: #c2c2cc;
48
+ --term-8: #9a9aa2; --term-9: #d13932; --term-10: #3a813d; --term-11: #a86a10;
49
+ --term-12: #3b6fe0; --term-13: #9b3f97; --term-14: #2f7b8f; --term-15: #5a5a63;
50
+ --cursor: #3b6fe0;
51
+ }
52
+
53
+ * { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
54
+
55
+ html, body {
56
+ margin: 0;
57
+ padding: 0;
58
+ background: var(--bg);
59
+ color: var(--fg);
60
+ font: 13px/1.2 var(--font);
61
+ /* The page itself never scrolls; only the terminal does. */
62
+ overflow: hidden;
63
+ overscroll-behavior: none;
64
+ /* overflow alone does not stop iOS panning the page up over the keyboard
65
+ from a drag that starts where no scroller claims it: the key bar's edges,
66
+ the gaps between keys, the side insets. touch-action only counts up to the
67
+ nearest scroller, so this reaches just the surfaces that do not scroll. */
68
+ touch-action: none;
69
+ }
70
+
71
+ #safe-probe {
72
+ position: fixed;
73
+ top: 0; left: 0;
74
+ width: 0; height: 0;
75
+ padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
76
+ pointer-events: none;
77
+ visibility: hidden;
78
+ }
79
+
80
+ #cell-probe {
81
+ position: fixed;
82
+ visibility: hidden;
83
+ white-space: pre;
84
+ font-family: var(--font);
85
+ line-height: 1.2;
86
+ }
87
+
88
+ /* The classic-scrollbar gutter: the inner div fills the scrollport, so the
89
+ width difference is the scrollbar's (0 when overlay). Read fractionally. */
90
+ #scroll-probe {
91
+ position: fixed;
92
+ visibility: hidden;
93
+ width: 100px;
94
+ height: 100px;
95
+ overflow-y: scroll;
96
+ pointer-events: none;
97
+ }
98
+ #scroll-probe > div { height: 200px; }
99
+
100
+ /* Sized from visualViewport in JS — never height:100%, which is the layout
101
+ viewport and hides the bottom of the screen behind the keyboard. The side
102
+ insets come from JS too, not env(): Chrome on Android reports a side cutout
103
+ it has already kept the page clear of (see deriveLayout). */
104
+ #app {
105
+ position: fixed;
106
+ top: 0;
107
+ left: 0;
108
+ width: 100%;
109
+ padding: 0 var(--inset-right) 0 var(--inset-left);
110
+ display: flex;
111
+ flex-direction: column;
112
+ }
113
+
114
+ #tabs {
115
+ position: absolute;
116
+ top: 0;
117
+ left: var(--inset-left);
118
+ right: var(--inset-right);
119
+ z-index: 7;
120
+ display: flex;
121
+ align-items: center;
122
+ gap: 4px;
123
+ height: calc(30px + env(safe-area-inset-top));
124
+ padding: env(safe-area-inset-top) 4px 0;
125
+ overflow-x: auto;
126
+ white-space: nowrap;
127
+ font: 600 11px/1.35 var(--font);
128
+ background: color-mix(in srgb, var(--chrome) 50%, transparent);
129
+ backdrop-filter: blur(6px);
130
+ }
131
+ #tabs .tab { flex: 0 0 auto; border: 0; border-radius: 6px; padding: 3px 8px; background: var(--raised); color: var(--fg); font: inherit; }
132
+ #tabs .tab[aria-current="true"] { background: var(--pressed); color: var(--accent); }
133
+ #tabs .tab.unread .tab-name::before { content: '● '; color: var(--accent); }
134
+ #tabs .tab.working .tab-name::before { content: '⏳️ '; }
135
+
136
+ .tabs-arrow {
137
+ position: absolute;
138
+ top: 0;
139
+ z-index: 7;
140
+ height: calc(30px + env(safe-area-inset-top));
141
+ padding: env(safe-area-inset-top) 6px 0;
142
+ border: 0;
143
+ background: color-mix(in srgb, var(--edge) 85%, transparent);
144
+ color: var(--fg);
145
+ font: 14px/1 var(--font);
146
+ }
147
+ #tabs-left { left: var(--inset-left); }
148
+ #tabs-right { right: var(--inset-right); }
149
+
150
+ #viewport {
151
+ flex: 1;
152
+ min-height: 0;
153
+ overflow-x: auto;
154
+ overflow-y: hidden;
155
+ overscroll-behavior: contain;
156
+ /* Sideways only: a vertical drag it let through would pan the page. */
157
+ touch-action: pan-x;
158
+ }
159
+
160
+ #stage { position: relative; }
161
+
162
+ /* The wterm mount. It owns vertical scroll, so scrollback gets native momentum
163
+ scrolling and selection handles. */
164
+ #screen {
165
+ /* scroll, not auto: a classic desktop scrollbar takes layout space inside
166
+ the width JS sets. Reserving it even with nothing to scroll keeps the
167
+ column count independent of whether scrollback has arrived yet. Overlay
168
+ scrollbars reserve nothing. */
169
+ overflow-y: scroll;
170
+ overflow-x: hidden;
171
+ display: flex;
172
+ flex-direction: column;
173
+ /* Say so explicitly: a vertical drag here scrolls, it is not a gesture for
174
+ the horizontal scroller wrapped around it to claim. */
175
+ touch-action: pan-y;
176
+ overscroll-behavior: contain;
177
+ /* The app owns scroll position wherever content moves -- follow-output,
178
+ keyboard pinning, resize restoration, and the renderer's window swaps,
179
+ which detach and re-insert scrollback rows on every render. UA anchoring
180
+ would be a second actor doing the same job off its own choice of anchor:
181
+ when rows are detached, WebKit picks a live grid row below the insertion
182
+ point, after which each appended history row pushes the anchor down and
183
+ walks the box with it -- a parked reader creeps one row per line of
184
+ output. */
185
+ overflow-anchor: none;
186
+ }
187
+
188
+ /* app.js hides the grid's blank tail; this puts what is left against the key bar
189
+ rather than at the top with the gap underneath. `margin-top` on the grid and
190
+ not `justify-content: flex-end` on the scroller, which would put the top of an
191
+ overflowing transcript out of reach. */
192
+ .term-grid { margin-top: auto; }
193
+
194
+ /* wterm ships desktop-window chrome — padding, rounding, a drop shadow. On a
195
+ phone the terminal is the whole screen. */
196
+ #screen.wterm {
197
+ --term-font-family: var(--font);
198
+ --term-bg: var(--bg);
199
+ --term-fg: var(--fg);
200
+ --term-cursor: var(--cursor);
201
+ --term-color-0: var(--term-0); --term-color-1: var(--term-1);
202
+ --term-color-2: var(--term-2); --term-color-3: var(--term-3);
203
+ --term-color-4: var(--term-4); --term-color-5: var(--term-5);
204
+ --term-color-6: var(--term-6); --term-color-7: var(--term-7);
205
+ --term-color-8: var(--term-8); --term-color-9: var(--term-9);
206
+ --term-color-10: var(--term-10); --term-color-11: var(--term-11);
207
+ --term-color-12: var(--term-12); --term-color-13: var(--term-13);
208
+ --term-color-14: var(--term-14); --term-color-15: var(--term-15);
209
+ padding: 0;
210
+ border-radius: 0;
211
+ box-shadow: none;
212
+ }
213
+
214
+ /* `relative` so the pad can hang off the bar's top edge wherever the insets
215
+ leave it. */
216
+ #bar {
217
+ position: relative;
218
+ height: var(--bar-h);
219
+ flex: 0 0 var(--bar-h);
220
+ display: flex;
221
+ flex-direction: column;
222
+ gap: 0;
223
+ /* The bar bleeds to the edge of the glass; the keys stay out of the corner
224
+ curve. Vertical clearance is the bar's own padding, set from the insets. */
225
+ padding-inline: 8px;
226
+ background: var(--edge);
227
+ border-top: 1px solid var(--edge);
228
+ }
229
+
230
+ #keys {
231
+ flex: 1 1 0;
232
+ min-height: 0;
233
+ display: flex;
234
+ align-items: stretch;
235
+ gap: 1px;
236
+ }
237
+
238
+ /* The arrow pad: a floating cluster over the terminal, anchored to the bar's
239
+ top edge. Out of flow, so opening it never reflows the grid the way a second
240
+ bar row would. Above the way-back button, which it would otherwise sit
241
+ under. Translucent and frosted: it hangs over live output, which stays
242
+ readable through it without competing with the arrows. */
243
+ #pad {
244
+ position: absolute;
245
+ right: 8px;
246
+ bottom: calc(100% + 8px);
247
+ z-index: 6;
248
+ display: flex;
249
+ gap: 1px;
250
+ padding: 4px;
251
+ background: color-mix(in srgb, var(--edge) 65%, transparent);
252
+ border-radius: 10px;
253
+ box-shadow: 0 4px 14px rgb(0 0 0 / .5);
254
+ -webkit-backdrop-filter: blur(10px);
255
+ backdrop-filter: blur(10px);
256
+ }
257
+
258
+ #pad button {
259
+ /* Narrower than an arrow's 52px so six keys still fit a phone's width with
260
+ the pad's padding and gaps; the ⇈/⇊ doubles read fine at 19px. */
261
+ width: 46px;
262
+ height: 52px;
263
+ border: 0;
264
+ border-radius: 6px;
265
+ background: color-mix(in srgb, var(--chrome) 65%, transparent);
266
+ color: var(--fg);
267
+ font: 19px/1 var(--font);
268
+ touch-action: none;
269
+ }
270
+
271
+ #pad button:active { background: color-mix(in srgb, var(--pressed) 65%, transparent); }
272
+
273
+ #keys button {
274
+ flex: 1 1 0;
275
+ min-width: 0;
276
+ padding: 0;
277
+ border: 0;
278
+ background: var(--chrome);
279
+ color: var(--fg);
280
+ font: 18px/1 var(--font);
281
+ touch-action: none;
282
+ }
283
+
284
+ #keys button:active { background: var(--pressed); }
285
+ #keys button.word { font-size: 14px; }
286
+ #keys button.wide { font-size: 22px; }
287
+ /* The ↑↓ toggle reads finer than the other ~22px glyphs at the same size. */
288
+ #keys button[name="arrow pad"] { font-size: 15px; }
289
+ #keys button.sticky { background: var(--accent); color: var(--on-accent); }
290
+
291
+ /* Only ever visible when the socket is not up. A frozen terminal otherwise
292
+ looks exactly like a busy one. Kept clear of the notch in landscape. */
293
+ #conn {
294
+ position: fixed;
295
+ top: calc(env(safe-area-inset-top) + 6px);
296
+ right: calc(var(--inset-right) + 8px);
297
+ z-index: 8;
298
+ width: 22px;
299
+ height: 22px;
300
+ display: flex;
301
+ align-items: center;
302
+ justify-content: center;
303
+ border-radius: 50%;
304
+ background: rgba(58, 36, 22, .9);
305
+ color: #f0c48a;
306
+ font-size: 12px;
307
+ line-height: 1;
308
+ animation: conn-pulse 1.4s ease-in-out infinite;
309
+ }
310
+
311
+ @keyframes conn-pulse { 50% { opacity: .35; } }
312
+
313
+ #to-bottom {
314
+ position: fixed;
315
+ left: 50%;
316
+ transform: translateX(-50%);
317
+ padding: 6px 14px;
318
+ border: 1px solid var(--edge);
319
+ border-radius: 14px;
320
+ background: var(--chrome);
321
+ color: var(--fg);
322
+ font: 12px var(--font);
323
+ z-index: 5;
324
+ }
325
+
326
+ /* Over the terminal while a session is reached, which for a pi starting up
327
+ takes seconds. Faded in late, so a session that draws at once never
328
+ flashes it. */
329
+ #loading {
330
+ position: fixed;
331
+ inset: 0 0 var(--bar-h);
332
+ display: flex;
333
+ align-items: center;
334
+ justify-content: center;
335
+ color: var(--muted);
336
+ font: 14px var(--font);
337
+ pointer-events: none;
338
+ z-index: 5;
339
+ animation: fade-in .2s .4s both;
340
+ }
341
+
342
+ @keyframes fade-in { from { opacity: 0; } }
343
+
344
+ /* `display` on an id selector beats the `hidden` attribute's UA rule, so an
345
+ invisible overlay would still swallow every tap — and so would `.term-row`,
346
+ which is what the grid's blank-tail trim rides on. */
347
+ [hidden] { display: none !important; }
348
+
349
+ /* Anchored to the top, because it exists to explain a broken bottom. */
350
+ #diag-overlay {
351
+ position: fixed;
352
+ top: 0;
353
+ left: 0;
354
+ right: 0;
355
+ z-index: 20;
356
+ margin: 0;
357
+ padding: 8px;
358
+ background: #2b1416;
359
+ border-bottom: 1px solid #7a2b32;
360
+ color: #e8c8cc;
361
+ font: 11px/1.45 var(--font);
362
+ white-space: pre-wrap;
363
+ }
364
+
365
+ #menu {
366
+ position: fixed;
367
+ inset: 0;
368
+ background: rgba(0, 0, 0, .55);
369
+ display: flex;
370
+ align-items: flex-end;
371
+ z-index: 10;
372
+ }
373
+
374
+ .menu-card {
375
+ width: 100%;
376
+ background: var(--chrome);
377
+ border-top: 1px solid var(--edge);
378
+ padding: 12px 12px calc(12px + env(safe-area-inset-bottom));
379
+ /* The sheet grows from the bottom, so without a ceiling a tall card runs off
380
+ the top of the screen taking `Done` with it — and the folder list is as
381
+ long as the number of projects you have. */
382
+ max-height: 88vh;
383
+ overflow-y: auto;
384
+ }
385
+
386
+ .menu-head { margin: 10px 0 5px; color: var(--muted); font-size: 11px; text-transform: uppercase; }
387
+ .menu-toggle { padding: 5px 0; }
388
+ /* The same caret, after the label, as the Diagnostics toggle under it. */
389
+ #advanced > summary { list-style: none; }
390
+ #advanced > summary::-webkit-details-marker { display: none; }
391
+ #advanced > summary::after { content: ' ▸'; }
392
+ #advanced[open] > summary::after { content: ' ▾'; }
393
+ .menu-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
394
+ /* Rows are usually separated by their heading. Where two run together the gap
395
+ has to come from somewhere, or the paste field sits straight on the buttons
396
+ under it. */
397
+ .menu-row + .menu-row { margin-top: 8px; }
398
+
399
+ /* The state line is the sheet's title bar, and Done sits at the end of it —
400
+ directly over Reload app, which is not what anyone reaching for Done meant
401
+ to hit. A rule and some room make them two different things. */
402
+ .menu-card > .menu-row:first-child {
403
+ padding-bottom: 10px;
404
+ margin-bottom: 2px;
405
+ border-bottom: 1px solid var(--edge);
406
+ }
407
+ .menu-row input[type=text] {
408
+ flex: 2;
409
+ min-width: 0;
410
+ padding: 10px 8px;
411
+ border: 1px solid var(--edge);
412
+ border-radius: 8px;
413
+ background: var(--inset);
414
+ color: var(--fg);
415
+ font: 13px var(--font);
416
+ }
417
+
418
+ .menu-row button {
419
+ flex: 1;
420
+ padding: 10px 8px;
421
+ border: 1px solid var(--edge);
422
+ border-radius: 8px;
423
+ background: var(--raised);
424
+ color: var(--fg);
425
+ font: 13px var(--font);
426
+ }
427
+
428
+ /* The theme switch shares the grid row; it never needs the full row width, so
429
+ it sizes to its label and reads as a secondary control next to the presets.
430
+ `sticky` is the same accent as the key bar's sticky modifiers. */
431
+ .menu-row button[data-theme] {
432
+ flex: 0 0 auto;
433
+ padding: 10px 14px;
434
+ font-size: 12px;
435
+ }
436
+ .menu-row button[data-theme].sticky { background: var(--accent); color: var(--on-accent); }
437
+
438
+ #menu-state { flex: 1; color: var(--muted); }
439
+
440
+ /* A first visit is only the session list, under its own title, with Reload
441
+ for a list that will not load. The menu cannot be dismissed from here:
442
+ there is nothing behind it yet. */
443
+ #menu:not(.first-visit) .first-visit-only,
444
+ #menu.first-visit :is(#menu-state, [data-act="close"], #menu-repair, #menu-main > :not(#places)) { display: none; }
445
+ span.first-visit-only { flex: 1; }
446
+ /* With nothing else on the sheet, the list can have most of it. */
447
+ #menu.first-visit #places { max-height: 70vh; }
448
+ #menu-notice { margin: 0 0 8px; color: var(--muted); font-size: 12px; }
449
+
450
+ /* Its own scroller: there is a folder here for every directory pi has ever run
451
+ in, which on a working machine is a hundred of them. The card must not grow
452
+ off the top of the screen to hold them. */
453
+ #places {
454
+ max-height: 34vh;
455
+ overflow-y: auto;
456
+ border: 1px solid var(--edge);
457
+ border-radius: 8px;
458
+ background: var(--inset);
459
+ }
460
+
461
+ .place + .place, .place + .place-row, .place-row + .place-row { border-top: 1px solid var(--edge); }
462
+
463
+ /* A row is a session, not a folder: tapping it joins straight away, so it is
464
+ a plain button rather than a two-step disclose-then-act control. Joining
465
+ never ends anything else, so there is no confirming second tap to design
466
+ for the way switching used to need. */
467
+ .place {
468
+ position: relative;
469
+ display: flex;
470
+ flex-direction: column;
471
+ gap: 2px;
472
+ width: 100%;
473
+ padding: 9px 10px;
474
+ border: 0;
475
+ background: none;
476
+ color: var(--fg);
477
+ font: 13px var(--font);
478
+ text-align: left;
479
+ }
480
+
481
+ .sr-only {
482
+ position: absolute;
483
+ width: 1px;
484
+ height: 1px;
485
+ margin: -1px;
486
+ padding: 0;
487
+ overflow: hidden;
488
+ clip-path: inset(50%);
489
+ white-space: nowrap;
490
+ border: 0;
491
+ }
492
+
493
+ /* The name is pi's folder; the path is what tells two `extensions` apart. */
494
+ .place-path { display: flex; align-items: center; gap: 6px; min-width: 0; color: var(--muted); font-size: 11px; }
495
+ .place-path-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
496
+ .place-watchers { margin-left: auto; white-space: nowrap; }
497
+
498
+ /* Where you already are, so a list of a hundred rows still says which is this. */
499
+ .place.here .place-name::after { content: ' ·'; color: var(--accent); }
500
+
501
+ /* Every session row keeps the same slot ahead of its name, so names line up
502
+ whatever it holds: working, unread, running, or nothing for a saved one. */
503
+ .place-row .place-name::before { content: ''; display: inline-block; width: 2ch; text-align: center; }
504
+ .place.running .place-name::before { content: '○'; color: var(--faint); }
505
+ .place.unread .place-name::before { content: '●'; color: var(--accent); }
506
+ .place.working .place-name::before { content: '⏳️'; }
507
+
508
+ /* The end action is a sibling button over the row's right edge, not a nested
509
+ control. Its width and the row's reserved space stay fixed while confirming. */
510
+ .place-row { position: relative; }
511
+ .place-row.has-end > .place { padding-right: 92px; }
512
+ .place-end {
513
+ position: absolute;
514
+ right: 0;
515
+ top: 50%;
516
+ transform: translateY(-50%);
517
+ min-width: 72px;
518
+ padding: 8px 10px;
519
+ border: 0;
520
+ background: none;
521
+ color: var(--muted);
522
+ font: 12px var(--font);
523
+ text-align: right;
524
+ }
525
+ .place-end.confirm { color: var(--danger); font-weight: 600; }
526
+
527
+ .place-more {
528
+ width: 100%;
529
+ padding: 9px 10px;
530
+ border: 0;
531
+ background: none;
532
+ color: var(--muted);
533
+ font: 12px var(--font);
534
+ text-align: center;
535
+ }
536
+
537
+ /* Starting options share a row; folder choices are one step of two. */
538
+ .place-starts { display: flex; border-bottom: 1px solid var(--edge); }
539
+ .place-starts .place { flex: 1; width: 50%; min-width: 0; }
540
+ .place-starts .place + .place { border-left: 1px solid var(--edge); }
541
+ .place.start .place-name { color: var(--accent); }
542
+ .place.dir .place-name { color: var(--fg); }
543
+ .place.dir .place-path { min-height: 0; }
544
+ .place-heading { padding: 8px 10px; color: var(--muted); font-size: 12px; }
545
+ .place-heading.pool { color: var(--accent); font-size: 13px; }
546
+
547
+ #place-now { text-transform: none; color: var(--faint); }
548
+
549
+ #diag {
550
+ margin: 0;
551
+ padding: 8px;
552
+ background: var(--inset);
553
+ border: 1px solid var(--edge);
554
+ border-radius: 6px;
555
+ color: var(--muted);
556
+ font: 11px/1.45 var(--font);
557
+ white-space: pre-wrap;
558
+ overflow-x: auto;
559
+ }
@@ -0,0 +1,153 @@
1
+ // The connection: framing and reconnect. The server owns the PTY size and says
2
+ // what it is, so there are no size games left here.
3
+ import { encodeInput, encodeResize, encodeHandshake, decodeFrame, OUTPUT, SET_TITLE, SET_SIZE, PROCESS } from './ttyd.js'
4
+
5
+ const BACKOFF_MIN = 500
6
+ const BACKOFF_MAX = 10_000
7
+
8
+ /**
9
+ * A connection that survives the phone sleeping.
10
+ *
11
+ * The terminal object outlives the socket, so a drop leaves the stale screen
12
+ * visible rather than blanking, and reconnecting replaces it with the screen the
13
+ * server kept.
14
+ */
15
+ export class TtydConnection {
16
+ constructor({ url, socketFactory, schedule, cancel = clearTimeout.bind(globalThis), onOutput, onTitle, onSize, onProcess, onState }) {
17
+ this.url = url
18
+ this.socketFactory = socketFactory
19
+ this.schedule = schedule
20
+ this.cancel = cancel
21
+ this.onOutput = onOutput
22
+ this.onTitle = onTitle
23
+ this.onSize = onSize
24
+ this.onProcess = onProcess
25
+ this.onState = onState
26
+
27
+ this.cols = 0
28
+ this.rows = 0
29
+ this.ws = null
30
+ this.queue = []
31
+ this.backoff = BACKOFF_MIN
32
+ this.retry = null
33
+ this.started = false
34
+ }
35
+
36
+ get connected() { return this.ws?.readyState === 1 }
37
+
38
+ connect({ cols, rows }) {
39
+ this.cols = cols
40
+ this.rows = rows
41
+ this.started = true
42
+ this._cancelRetry()
43
+ this._open()
44
+ }
45
+
46
+ _cancelRetry() {
47
+ if (!this.retry) return
48
+ this.cancel(this.retry.handle)
49
+ this.retry = null
50
+ }
51
+
52
+ _scheduleRetry() {
53
+ if (!this.started) return
54
+ this._cancelRetry()
55
+ const retry = {}
56
+ this.retry = retry
57
+ retry.handle = this.schedule(() => {
58
+ if (this.retry !== retry) return
59
+ this.retry = null
60
+ this._open()
61
+ }, this.backoff)
62
+ this.backoff = Math.min(BACKOFF_MAX, this.backoff * 2)
63
+ }
64
+
65
+ reconnectNow() {
66
+ if (!this.started) return
67
+ this._cancelRetry()
68
+ this.backoff = BACKOFF_MIN
69
+ const old = this.ws
70
+ this.ws = null
71
+ if (old && old.readyState !== 3) old.close()
72
+ this._open()
73
+ }
74
+
75
+ /**
76
+ * Point this connection at a different session and reconnect immediately.
77
+ *
78
+ * Joining a different (or the same, freshly resumed) session is not a frame
79
+ * on the wire any more — the session a socket shows is chosen by the URL it
80
+ * connects with, so switching is just this plus the ordinary reconnect path:
81
+ * a fresh screen arrives the same way it would after any other reconnect.
82
+ */
83
+ join(url) {
84
+ this.url = url
85
+ this.started = true
86
+ this.reconnectNow()
87
+ }
88
+
89
+ /** Cease dialing for good: the session we had is over. Until join() or
90
+ * connect() points somewhere new, a closed socket stays closed. */
91
+ stop() {
92
+ this.started = false
93
+ this._cancelRetry()
94
+ }
95
+
96
+ /** Forget input aimed at a place this connection never reached: flushing it
97
+ * at whatever is joined next would type it into the wrong session. */
98
+ discardInput() {
99
+ this.queue.length = 0
100
+ }
101
+
102
+ _open() {
103
+ this.onState?.('connecting')
104
+ const ws = this.socketFactory(this.url, ['tty'])
105
+ ws.binaryType = 'arraybuffer'
106
+ this.ws = ws
107
+
108
+ ws.onopen = () => {
109
+ if (this.ws !== ws) return
110
+ // The server holds the screen and sends it on connect, so attaching costs
111
+ // nothing and needs no prompting. It also answers with the grid the PTY
112
+ // actually has, which is not always the one asked for here.
113
+ ws.send(encodeHandshake(this.cols, this.rows))
114
+ this.backoff = BACKOFF_MIN
115
+ for (const frame of this.queue.splice(0)) ws.send(frame)
116
+ this.onState?.('connected')
117
+ }
118
+
119
+ ws.onmessage = ev => {
120
+ if (this.ws !== ws) return
121
+ const f = decodeFrame(ev.data)
122
+ if (f.cmd === OUTPUT) this.onOutput(f.payload)
123
+ else if (f.cmd === SET_TITLE) this.onTitle?.(f.text)
124
+ else if (f.cmd === SET_SIZE) this.onSize?.({ cols: f.json.columns, rows: f.json.rows })
125
+ else if (f.cmd === PROCESS) this.onProcess?.(f.json)
126
+ }
127
+
128
+ ws.onclose = ev => {
129
+ if (this.ws !== ws) return
130
+ this.onState?.('disconnected', ev.code, ev.reason)
131
+ this._scheduleRetry()
132
+ }
133
+ }
134
+
135
+ send(text) {
136
+ // Before any session is chosen there is nowhere for input to go, and
137
+ // holding it would type it into whichever one is picked later.
138
+ if (!this.started) return
139
+ const frame = encodeInput(text)
140
+ if (this.connected) this.ws.send(frame)
141
+ else this.queue.push(frame)
142
+ }
143
+
144
+ resize(cols, rows) {
145
+ if (cols === this.cols && rows === this.rows) return
146
+ this.cols = cols
147
+ this.rows = rows
148
+ // Nothing to send while disconnected: the next attach carries the size in
149
+ // its handshake.
150
+ if (this.connected) this.ws.send(encodeResize(cols, rows))
151
+ }
152
+
153
+ }