@gjermundgaraba/clankermux 0.5.0 → 0.7.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 (40) hide show
  1. package/README.md +1 -1
  2. package/dist/client/attachment.d.ts +9 -29
  3. package/dist/client/attachment.js +28 -41
  4. package/dist/client/client.d.ts +0 -2
  5. package/dist/client/client.js +6 -11
  6. package/dist/client/connection.d.ts +1 -19
  7. package/dist/client/connection.js +0 -17
  8. package/dist/client/copy.d.ts +0 -14
  9. package/dist/client/copy.js +4 -25
  10. package/dist/client/keyboard.d.ts +1 -27
  11. package/dist/client/keyboard.js +18 -34
  12. package/dist/client/link.d.ts +143 -0
  13. package/dist/client/link.js +205 -0
  14. package/dist/client/listeners.d.ts +3 -1
  15. package/dist/client/listeners.js +20 -4
  16. package/dist/client/mount.d.ts +0 -5
  17. package/dist/client/mount.js +20 -72
  18. package/dist/client/osc52.d.ts +1 -4
  19. package/dist/client/osc52.js +20 -17
  20. package/dist/client/profile.d.ts +0 -6
  21. package/dist/client/profile.js +0 -6
  22. package/dist/client/worker.d.ts +3 -44
  23. package/dist/client/worker.js +185 -332
  24. package/dist/engine/decoder.d.ts +0 -20
  25. package/dist/engine/decoder.js +8 -27
  26. package/dist/engine/index.d.ts +2 -49
  27. package/dist/engine/index.js +7 -48
  28. package/dist/engine/keys.d.ts +0 -7
  29. package/dist/engine/keys.js +9 -15
  30. package/dist/engine/render.d.ts +7 -18
  31. package/dist/engine/render.js +32 -32
  32. package/dist/engine/selection.d.ts +0 -17
  33. package/dist/engine/selection.js +8 -30
  34. package/dist/proto/frame.d.ts +2 -23
  35. package/dist/proto/frame.js +1 -21
  36. package/dist/proto/messages.d.ts +49 -9
  37. package/dist/proto/messages.js +42 -1
  38. package/dist/renderer/index.d.ts +3 -4
  39. package/dist/renderer/index.js +38 -42
  40. package/package.json +5 -6
@@ -1,50 +1,34 @@
1
1
  import { isText } from "../engine/index.js";
2
- /** Whether the browser keeps the key: its own shortcuts with the platform's
3
- * command modifier, and Ctrl+Shift+V, the paste shortcut where Ctrl+V is
4
- * the terminal's. */
2
+ const IME_PROCESSING_KEY_CODE = 229;
3
+ function isTerminalPasteShortcut(e) {
4
+ return e.ctrlKey && e.shiftKey && e.code === "KeyV";
5
+ }
5
6
  export function browserKeeps(e) {
6
- return e.metaKey || (e.ctrlKey && e.shiftKey && e.code === "KeyV");
7
+ return e.metaKey || isTerminalPasteShortcut(e);
7
8
  }
8
- /** Whether the key belongs to an input method: it is composing, or
9
- * processing the key (`Process`, key code 229), or the key is a dead key
10
- * that starts a composition. Its text arrives at `compositionend`. */
11
9
  export function composing(e) {
12
- return e.isComposing || e.keyCode === 229 || e.key === "Process" || e.key === "Dead";
10
+ return e.isComposing || e.keyCode === IME_PROCESSING_KEY_CODE || e.key === "Process" || e.key === "Dead";
13
11
  }
14
- /** A key with no modifiers, pressed. */
15
12
  function pressed(code) {
16
13
  return { code, key: code, action: "press", shift: false, ctrl: false, alt: false, meta: false };
17
14
  }
18
- /**
19
- * Typing on the terminal's input element, a textarea: keys go to the key
20
- * encoder, and text that no key sent goes as text. Keys the browser keeps
21
- * and keys of a composition are left to the browser; a composition's text
22
- * is sent when it ends, and text inserted without a key (a dead key's
23
- * character, the emoji picker, dictation) as it is inserted.
24
- */
25
15
  export class Typing {
26
16
  keyboard;
27
- /** Keys whose press was sent, so their release is too: a key that
28
- * ended a composition sends no release of a press it never sent. */
29
- down = new Set();
17
+ pressesSent = new Set();
30
18
  constructor(keyboard) {
31
19
  this.keyboard = keyboard;
32
20
  }
33
- /** A key to send, or undefined to leave the event to the browser. */
34
21
  keydown(e) {
35
22
  if (browserKeeps(e) || composing(e))
36
23
  return undefined;
37
- this.down.add(e.code);
24
+ this.pressesSent.add(e.code);
38
25
  return keyInput(e, this.keyboard);
39
26
  }
40
27
  keyup(e) {
41
- if (!this.down.delete(e.code))
28
+ if (!this.pressesSent.delete(e.code))
42
29
  return undefined;
43
30
  return keyInput(e, this.keyboard);
44
31
  }
45
- /** `beforeinput`: what the element would insert or delete, when no key
46
- * sent it. A composition's own steps wait for `compositionend`; a paste
47
- * is the `paste` event's. */
48
32
  beforeinput(e) {
49
33
  if (e.isComposing)
50
34
  return undefined;
@@ -60,7 +44,6 @@ export class Typing {
60
44
  return undefined;
61
45
  }
62
46
  }
63
- /** `compositionend`: the composed text. */
64
47
  compositionend(data) {
65
48
  return data ? { text: data } : undefined;
66
49
  }
@@ -80,13 +63,14 @@ export function keyInput(e, keyboard) {
80
63
  const unshifted = keyboard.layout?.get(e.code);
81
64
  if (unshifted !== undefined)
82
65
  input.unshifted = unshifted;
83
- // Alt composed the text when it differs from the key's own character
84
- // (Option on macOS, AltGr elsewhere). Without a layout to compare
85
- // against, macOS Option composes and other platforms' Alt modifies.
86
- if (e.altKey && isText(e.key)) {
87
- const composed = unshifted !== undefined ? e.key.toLowerCase() !== unshifted.toLowerCase() : keyboard.mac;
88
- if (composed)
89
- input.altComposed = true;
90
- }
66
+ if (altComposedText(e, unshifted, keyboard))
67
+ input.altComposed = true;
91
68
  return input;
92
69
  }
70
+ function altComposedText(e, unshifted, keyboard) {
71
+ if (!e.altKey || !isText(e.key))
72
+ return false;
73
+ if (unshifted === undefined)
74
+ return keyboard.mac;
75
+ return e.key.toLowerCase() !== unshifted.toLowerCase();
76
+ }
@@ -0,0 +1,143 @@
1
+ import { type FSM, type FSMSnapshot } from "xstate/fsm";
2
+ import type { ClosedReason, ErrorCode, ErrorMessage, Exit, Session } from "../proto/index.ts";
3
+ import type { Attachment } from "./attachment.ts";
4
+ import { type Connection } from "./connection.ts";
5
+ /** Why the link to the session ended. `closed`: the connection closed
6
+ * (not a `CLOSED` frame, which the worker does not wait for when it
7
+ * detaches); `removed`, `stopping` and `detached` (another client's
8
+ * `detach`): the server's `CLOSED`; `exited`:
9
+ * the session's program exited; `no_such_session` and `engine_mismatch`:
10
+ * an `OPEN` refused; `error`: any other error, the server's or the
11
+ * client's own. */
12
+ export type LinkEnd = "closed" | "removed" | "stopping" | "detached" | "exited" | "no_such_session" | "engine_mismatch" | "error";
13
+ /** A terminal's link to its session, through the client's connection and
14
+ * the terminal's attachment. */
15
+ export type Link =
16
+ /** Opening a connection; `attempt` counts from 1 and starts again once
17
+ * a server answers. */
18
+ {
19
+ state: "connecting";
20
+ attempt: number;
21
+ }
22
+ /** Connected, and showing `session` as `ATTACHED` described it (its
23
+ * `status` may be `restored` or `exited`), or attached to none. */
24
+ | {
25
+ state: "connected";
26
+ session: Session | undefined;
27
+ }
28
+ /** The link ended. `code` and `message` are the error's, when there is
29
+ * one, and `message` the reason a `detach` gave; `exit` is the
30
+ * program's, for `exited`. `retry`: the worker
31
+ * reconnects or attaches again by itself, so `connecting` or
32
+ * `connected` follows; otherwise nothing does until the page attaches. */
33
+ | {
34
+ state: "ended";
35
+ reason: LinkEnd;
36
+ code?: ErrorCode;
37
+ message?: string;
38
+ exit?: Exit;
39
+ retry: boolean;
40
+ };
41
+ export declare function endedWith(err: unknown, retry: boolean): Link;
42
+ export interface Open {
43
+ conn: Connection;
44
+ channel: number;
45
+ fresh: Attachment;
46
+ }
47
+ export interface Line {
48
+ session: string;
49
+ attachment: Attachment;
50
+ channel: number;
51
+ conn: Connection;
52
+ }
53
+ declare const CLEARED: {
54
+ gaveUp: undefined;
55
+ live: undefined;
56
+ session: undefined;
57
+ attachment: undefined;
58
+ channel: undefined;
59
+ conn: undefined;
60
+ described: undefined;
61
+ exit: undefined;
62
+ reason: undefined;
63
+ message: undefined;
64
+ ended: undefined;
65
+ };
66
+ type Only<T> = T & {
67
+ [K in Exclude<keyof typeof CLEARED, keyof T>]?: undefined;
68
+ };
69
+ interface Contexts {
70
+ none: Only<{
71
+ gaveUp: Link | undefined;
72
+ live: boolean;
73
+ }>;
74
+ waiting: Only<{
75
+ session: string;
76
+ attachment: Attachment | undefined;
77
+ }>;
78
+ opening: Only<Line>;
79
+ attached: Only<Line & {
80
+ described: Session | undefined;
81
+ }>;
82
+ exited: Only<Line & {
83
+ exit: Exit | undefined;
84
+ }>;
85
+ closed: Only<Line & {
86
+ reason: "stopping" | "closed";
87
+ message: string | undefined;
88
+ }>;
89
+ backoff: Only<{
90
+ session: string;
91
+ ended: Link;
92
+ }>;
93
+ disposed: Only<{}>;
94
+ }
95
+ interface Events {
96
+ "hub.live": {
97
+ open: Open;
98
+ };
99
+ "hub.down": {};
100
+ "page.attach": {
101
+ session: string;
102
+ open: Open | undefined;
103
+ };
104
+ "page.detach": {
105
+ live: boolean;
106
+ };
107
+ "page.unmount": {};
108
+ "frame.screen": {
109
+ session: Session | undefined;
110
+ };
111
+ "frame.resumed": {
112
+ session: Session | undefined;
113
+ };
114
+ "frame.exited": {
115
+ exit: Exit | undefined;
116
+ };
117
+ "frame.closed": {
118
+ reason: ClosedReason;
119
+ message: string | undefined;
120
+ };
121
+ "frame.error": {
122
+ error: ErrorMessage;
123
+ endsAttachment: boolean;
124
+ };
125
+ "attachment.threw": {
126
+ error: unknown;
127
+ };
128
+ "reopen.due": {
129
+ open: Open | undefined;
130
+ };
131
+ }
132
+ export type MountSnapshot = {
133
+ [K in keyof Contexts]: FSMSnapshot<Contexts[K], K>;
134
+ }[keyof Contexts];
135
+ export type MountEvent = {
136
+ [K in keyof Events]: {
137
+ type: K;
138
+ } & Events[K];
139
+ }[keyof Events];
140
+ export declare const mountMachine: FSM<{}, MountEvent, keyof Contexts, MountSnapshot, unknown>;
141
+ export declare function lineOf(snapshot: MountSnapshot): Line | undefined;
142
+ export declare function linkOf(snapshot: MountSnapshot): Link | undefined;
143
+ export {};
@@ -0,0 +1,205 @@
1
+ import { setup, types } from "xstate/fsm";
2
+ import { ClientError } from "./connection.js";
3
+ const UNRETRYABLE_OPEN_ERRORS = new Set(["no_such_session", "engine_mismatch", "invalid_request"]);
4
+ function linkEnd(code) {
5
+ return code === "no_such_session" || code === "engine_mismatch" ? code : "error";
6
+ }
7
+ export function endedWith(err, retry) {
8
+ const server = err instanceof ClientError ? err.server : undefined;
9
+ return server
10
+ ? { state: "ended", reason: linkEnd(server.code), code: server.code, message: server.message, retry }
11
+ : { state: "ended", reason: "error", message: err instanceof Error ? err.message : String(err), retry };
12
+ }
13
+ function closedWith(reason, message) {
14
+ return { state: "ended", reason, ...(message !== undefined && { message }), retry: reason === "stopping" };
15
+ }
16
+ const CLEARED = {
17
+ gaveUp: undefined,
18
+ live: undefined,
19
+ session: undefined,
20
+ attachment: undefined,
21
+ channel: undefined,
22
+ conn: undefined,
23
+ described: undefined,
24
+ exit: undefined,
25
+ reason: undefined,
26
+ message: undefined,
27
+ ended: undefined,
28
+ };
29
+ const none = (gaveUp, live) => ({ ...CLEARED, gaveUp, live });
30
+ const waiting = (session, attachment) => ({ ...CLEARED, session, attachment });
31
+ const line = ({ session, attachment, channel, conn }) => ({ ...CLEARED, session, attachment, channel, conn });
32
+ const opening = (session, attachment, { conn, channel }) => line({ session, attachment, channel, conn });
33
+ const backoff = (session, ended) => ({ ...CLEARED, session, ended });
34
+ const attach = ({ event: { session, open } }) => open
35
+ ? { target: "opening", context: opening(session, open.fresh, open) }
36
+ : { target: "waiting", context: waiting(session, undefined) };
37
+ const detach = ({ event }) => ({ target: "none", context: none(undefined, event.live) });
38
+ const unmount = { target: "disposed", context: CLEARED };
39
+ const lost = ({ context }) => ({ target: "waiting", context: waiting(context.session, context.attachment) });
40
+ const screen = ({ context, event }) => ({
41
+ target: "attached",
42
+ context: { ...line(context), described: event.session },
43
+ });
44
+ const closed = ({ context, event: { reason, message } }) => reason === "removed" || reason === "detached"
45
+ ? { target: "none", context: none(closedWith(reason, message), true) }
46
+ : { target: "closed", context: { ...line(context), reason, message } };
47
+ const refused = ({ context, event }) => {
48
+ if (!event.endsAttachment)
49
+ return undefined;
50
+ const { code, message } = event.error;
51
+ const retry = !UNRETRYABLE_OPEN_ERRORS.has(code);
52
+ const ended = { state: "ended", reason: linkEnd(code), code, message, retry };
53
+ return retry
54
+ ? { target: "backoff", context: backoff(context.session, ended) }
55
+ : { target: "none", context: none(ended, true) };
56
+ };
57
+ const threw = ({ context, event }) => {
58
+ const desync = event.error instanceof ClientError && event.error.kind === "desync";
59
+ const ended = endedWith(event.error, desync);
60
+ return desync
61
+ ? { target: "backoff", context: backoff(context.session, ended) }
62
+ : { target: "none", context: none(ended, true) };
63
+ };
64
+ export const mountMachine = setup({
65
+ schemas: {
66
+ events: {
67
+ "hub.live": types(),
68
+ "hub.down": types(),
69
+ "page.attach": types(),
70
+ "page.detach": types(),
71
+ "page.unmount": types(),
72
+ "frame.screen": types(),
73
+ "frame.resumed": types(),
74
+ "frame.exited": types(),
75
+ "frame.closed": types(),
76
+ "frame.error": types(),
77
+ "attachment.threw": types(),
78
+ "reopen.due": types(),
79
+ },
80
+ },
81
+ states: {
82
+ none: { schemas: { context: types() } },
83
+ waiting: { schemas: { context: types() } },
84
+ opening: { schemas: { context: types() } },
85
+ attached: { schemas: { context: types() } },
86
+ exited: { schemas: { context: types() } },
87
+ closed: { schemas: { context: types() } },
88
+ backoff: { schemas: { context: types() } },
89
+ disposed: { schemas: { context: types() } },
90
+ },
91
+ }).createFSM({
92
+ initial: "none",
93
+ context: none(undefined, false),
94
+ states: {
95
+ none: {
96
+ on: {
97
+ "hub.live": { target: "none", context: none(undefined, true) },
98
+ "hub.down": { target: "none", context: none(undefined, false) },
99
+ "page.attach": attach,
100
+ "page.unmount": unmount,
101
+ },
102
+ },
103
+ waiting: {
104
+ on: {
105
+ "hub.live": ({ context, event: { open } }) => ({
106
+ target: "opening",
107
+ context: opening(context.session, context.attachment ?? open.fresh, open),
108
+ }),
109
+ "page.attach": attach,
110
+ "page.detach": detach,
111
+ "page.unmount": unmount,
112
+ },
113
+ },
114
+ opening: {
115
+ on: {
116
+ "frame.screen": screen,
117
+ "frame.resumed": screen,
118
+ "frame.closed": closed,
119
+ "frame.error": refused,
120
+ "attachment.threw": threw,
121
+ "hub.down": lost,
122
+ "page.attach": attach,
123
+ "page.detach": detach,
124
+ "page.unmount": unmount,
125
+ },
126
+ },
127
+ attached: {
128
+ on: {
129
+ "frame.screen": screen,
130
+ "frame.exited": ({ context, event: { exit } }) => ({ target: "exited", context: { ...line(context), exit } }),
131
+ "frame.closed": closed,
132
+ "frame.error": refused,
133
+ "attachment.threw": threw,
134
+ "hub.down": lost,
135
+ "page.attach": attach,
136
+ "page.detach": detach,
137
+ "page.unmount": unmount,
138
+ },
139
+ },
140
+ exited: {
141
+ on: {
142
+ "frame.screen": screen,
143
+ "frame.closed": closed,
144
+ "frame.error": refused,
145
+ "attachment.threw": threw,
146
+ "hub.down": lost,
147
+ "page.attach": attach,
148
+ "page.detach": detach,
149
+ "page.unmount": unmount,
150
+ },
151
+ },
152
+ closed: {
153
+ on: {
154
+ "hub.down": lost,
155
+ "page.attach": attach,
156
+ "page.detach": detach,
157
+ "page.unmount": unmount,
158
+ },
159
+ },
160
+ backoff: {
161
+ on: {
162
+ "reopen.due": ({ context, event: { open } }) => open
163
+ ? { target: "opening", context: opening(context.session, open.fresh, open) }
164
+ : { target: "waiting", context: waiting(context.session, undefined) },
165
+ "hub.live": ({ context, event: { open } }) => ({
166
+ target: "opening",
167
+ context: opening(context.session, open.fresh, open),
168
+ }),
169
+ "page.attach": attach,
170
+ "page.detach": detach,
171
+ "page.unmount": unmount,
172
+ },
173
+ },
174
+ disposed: {},
175
+ },
176
+ });
177
+ export function lineOf(snapshot) {
178
+ switch (snapshot.value) {
179
+ case "opening":
180
+ case "attached":
181
+ case "exited":
182
+ case "closed":
183
+ return snapshot.context;
184
+ default:
185
+ return undefined;
186
+ }
187
+ }
188
+ export function linkOf(snapshot) {
189
+ switch (snapshot.value) {
190
+ case "none":
191
+ return snapshot.context.gaveUp ?? (snapshot.context.live ? { state: "connected", session: undefined } : undefined);
192
+ case "attached":
193
+ return { state: "connected", session: snapshot.context.described };
194
+ case "exited": {
195
+ const { exit } = snapshot.context;
196
+ return { state: "ended", reason: "exited", ...(exit && { exit }), retry: false };
197
+ }
198
+ case "closed":
199
+ return closedWith(snapshot.context.reason, snapshot.context.message);
200
+ case "backoff":
201
+ return snapshot.context.ended;
202
+ default:
203
+ return undefined;
204
+ }
205
+ }
@@ -1,7 +1,9 @@
1
- /** Listeners of one kind, and the latest value they were told. */
1
+ export type ListenerMode = "replayLatest" | "isolateErrors";
2
2
  export declare class Listeners<T> {
3
3
  private readonly all;
4
4
  private latest;
5
+ private readonly mode;
6
+ constructor(mode?: ListenerMode);
5
7
  add(listener: (value: T) => void): () => void;
6
8
  emit(value: T): void;
7
9
  clear(): void;
@@ -1,7 +1,10 @@
1
- /** Listeners of one kind, and the latest value they were told. */
2
1
  export class Listeners {
3
2
  all = new Set();
4
3
  latest;
4
+ mode;
5
+ constructor(mode = "replayLatest") {
6
+ this.mode = mode;
7
+ }
5
8
  add(listener) {
6
9
  this.all.add(listener);
7
10
  if (this.latest)
@@ -9,9 +12,22 @@ export class Listeners {
9
12
  return () => void this.all.delete(listener);
10
13
  }
11
14
  emit(value) {
12
- this.latest = { value };
13
- for (const listener of this.all)
14
- listener(value);
15
+ if (this.mode === "replayLatest") {
16
+ this.latest = { value };
17
+ for (const listener of this.all)
18
+ listener(value);
19
+ return;
20
+ }
21
+ for (const listener of this.all) {
22
+ try {
23
+ listener(value);
24
+ }
25
+ catch (err) {
26
+ queueMicrotask(() => {
27
+ throw err;
28
+ });
29
+ }
30
+ }
15
31
  }
16
32
  clear() {
17
33
  this.all.clear();
@@ -75,19 +75,14 @@ export interface Terminal {
75
75
  * copy. A listener added later is not told of earlier copies. */
76
76
  onCopy(listener: (copy: Copy) => void): () => void;
77
77
  }
78
- /** The client's side of a terminal: its messages to the worker, and
79
- * word that it is disposed. */
80
78
  export interface MountHost {
81
79
  send(message: ToWorker): void;
82
80
  disposed(): void;
83
81
  }
84
82
  type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
85
- /** A message for one terminal, without its number. */
86
83
  export type ToMount = DistributiveOmit<Extract<ToWorker, {
87
84
  mount: number;
88
85
  }>, "mount">;
89
- /** Mounts a terminal as `id` of the client `host` serves, and the
90
- * handler of the worker's messages for it. */
91
86
  export declare function mountOn(host: MountHost, id: number, options: MountOptions): {
92
87
  terminal: Terminal;
93
88
  receive: (message: ToPage) => void;