@gjermundgaraba/clankermux 0.6.0 → 0.8.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.
@@ -30,10 +30,9 @@ export declare class Attachment {
30
30
  private cursor;
31
31
  private incarnation;
32
32
  private continuation;
33
- private pendingServer;
34
33
  session: Session | undefined;
35
34
  constructor(engine: Engine);
36
- openOn(welcome: Welcome): void;
35
+ open(welcome: Welcome): Resume | undefined;
37
36
  resume(): Resume | undefined;
38
37
  ended(): boolean;
39
38
  closed(): boolean;
@@ -10,21 +10,20 @@ function mustHoldUntilSnapshotEnd(held, type) {
10
10
  export class Attachment {
11
11
  engine;
12
12
  state = { kind: "opening" };
13
- cursor = { offset: 0, marker: 0 };
13
+ cursor = { incarnation: "", offset: 0, marker: 0 };
14
14
  incarnation = "";
15
15
  continuation = 0;
16
- pendingServer;
17
16
  session;
18
17
  constructor(engine) {
19
18
  this.engine = engine;
20
19
  }
21
- openOn(welcome) {
22
- this.pendingServer = { incarnation: welcome.incarnation, continuation: welcome.limits.continuation };
20
+ open(welcome) {
21
+ this.incarnation = welcome.incarnation;
22
+ this.continuation = welcome.limits.continuation;
23
+ return this.resume();
23
24
  }
24
25
  resume() {
25
- return this.state.kind === "streaming"
26
- ? { incarnation: this.incarnation, offset: this.cursor.offset, marker: this.cursor.marker }
27
- : undefined;
26
+ return this.state.kind === "streaming" ? { ...this.cursor } : undefined;
28
27
  }
29
28
  ended() {
30
29
  return this.state.kind === "ended";
@@ -68,16 +67,9 @@ export class Attachment {
68
67
  switch (type) {
69
68
  case FrameType.Attached: {
70
69
  const attached = this.json(frame);
71
- if (this.pendingServer) {
72
- this.incarnation = this.pendingServer.incarnation;
73
- this.continuation = this.pendingServer.continuation;
74
- this.pendingServer = undefined;
75
- }
76
- this.cursor = { offset: attached.offset, marker: attached.marker };
70
+ this.cursor = { incarnation: this.incarnation, offset: attached.offset, marker: attached.marker };
77
71
  this.session = attached.session;
78
- if (attached.mode === "snapshot") {
79
- if (attached.snapshot_bytes === undefined)
80
- throw ClientError.protocol("ATTACHED without snapshot_bytes");
72
+ if (attached.snapshot_bytes !== undefined) {
81
73
  this.discard();
82
74
  const decoder = new Decoder(this.engine, attached.snapshot_bytes);
83
75
  try {
@@ -1,6 +1,6 @@
1
1
  import { type Catalog, type Description, type ErrorCode, type ErrorMessage, type Event, type Frame, FrameType, type Hello, type Peer, type Welcome } from "../proto/index.ts";
2
2
  export interface Transport {
3
- send(bytes: Uint8Array): void;
3
+ send(bytes: Uint8Array<ArrayBuffer>): void;
4
4
  close(): void;
5
5
  onMessage: ((bytes: Uint8Array) => void) | undefined;
6
6
  onClose: (() => void) | undefined;
@@ -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,14 +1,3 @@
1
- /**
2
- * Finds OSC 52 clipboard writes in output bytes, across chunk boundaries,
3
- * ending them as the engine's parser does: BEL or any byte after ESC
4
- * dispatches the sequence (ESC `\` is the string terminator; ESC `]`
5
- * begins the next one), CAN and SUB abandon it. The engine raises the
6
- * clipboard effect through a callback, which from wasm would need a JS
7
- * function in the module's function table (a generated wrapper per
8
- * signature); the scan is less machinery. OSC 1337 and 5522 clipboard
9
- * forms are not scanned for, and an OSC 52 inside another string (a DCS
10
- * passthrough) is taken as one, where the engine would not.
11
- */
12
1
  const ESC = 0x1b;
13
2
  const BEL = 0x07;
14
3
  const CAN = 0x18;
@@ -1,42 +1,8 @@
1
1
  import { type CellPoint, type PackedFrame, type KeyInput } from "../engine/index.ts";
2
- import { type ErrorCode, type ErrorMessage, type Event, type Exit, type Peer, type Profile, type Session, type Size } from "../proto/index.ts";
2
+ import { type ErrorMessage, type Event, type Peer, type Profile, type Session, type Size } from "../proto/index.ts";
3
3
  import { type ErrorKind } from "./connection.ts";
4
- /** Why the link to the session ended. `closed`: the connection closed
5
- * (not a `CLOSED` frame, which the worker does not wait for when it
6
- * detaches); `removed`, `stopping` and `detached` (another client's
7
- * `detach`): the server's `CLOSED`; `exited`:
8
- * the session's program exited; `no_such_session` and `engine_mismatch`:
9
- * an `OPEN` refused; `error`: any other error, the server's or the
10
- * client's own. */
11
- export type LinkEnd = "closed" | "removed" | "stopping" | "detached" | "exited" | "no_such_session" | "engine_mismatch" | "error";
12
- /** A terminal's link to its session, through the client's connection and
13
- * the terminal's attachment. */
14
- export type Link =
15
- /** Opening a connection; `attempt` counts from 1 and starts again once
16
- * a server answers. */
17
- {
18
- state: "connecting";
19
- attempt: number;
20
- }
21
- /** Connected, and showing `session` as `ATTACHED` described it (its
22
- * `status` may be `restored` or `exited`), or attached to none. */
23
- | {
24
- state: "connected";
25
- session: Session | undefined;
26
- }
27
- /** The link ended. `code` and `message` are the error's, when there is
28
- * one, and `message` the reason a `detach` gave; `exit` is the
29
- * program's, for `exited`. `retry`: the worker
30
- * reconnects or attaches again by itself, so `connecting` or
31
- * `connected` follows; otherwise nothing does until the page attaches. */
32
- | {
33
- state: "ended";
34
- reason: LinkEnd;
35
- code?: ErrorCode;
36
- message?: string;
37
- exit?: Exit;
38
- retry: boolean;
39
- };
4
+ import { type Link } from "./link.ts";
5
+ export type { Link, LinkEnd } from "./link.ts";
40
6
  /** A failed request, as it crosses to the page. */
41
7
  export interface Failure {
42
8
  kind: ErrorKind;