@celljs/ta-host 4.17.0 → 4.18.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 (143) hide show
  1. package/esm/node/app-commands.d.ts +1 -1
  2. package/esm/node/app-commands.js +46 -29
  3. package/esm/node/credentials.d.ts +12 -0
  4. package/esm/node/credentials.js +35 -0
  5. package/esm/node/index.d.ts +2 -0
  6. package/esm/node/index.js +2 -0
  7. package/esm/node/keep-awake.d.ts +3 -0
  8. package/esm/node/keep-awake.js +6 -1
  9. package/esm/node/package-hosts.d.ts +2 -0
  10. package/esm/node/package-hosts.js +6 -0
  11. package/esm/node/rooms-bridge.d.ts +10 -1
  12. package/esm/node/rooms-bridge.js +25 -0
  13. package/esm/node/screen-share/desktop-picture.d.ts +51 -0
  14. package/esm/node/screen-share/desktop-picture.js +133 -0
  15. package/esm/node/screen-share/desktop-session.d.ts +128 -0
  16. package/esm/node/screen-share/desktop-session.js +437 -0
  17. package/esm/node/screen-share/desktop-targets.d.ts +24 -0
  18. package/esm/node/screen-share/desktop-targets.js +80 -0
  19. package/esm/node/screen-share/desktop-viewer.d.ts +146 -0
  20. package/esm/node/screen-share/desktop-viewer.js +339 -0
  21. package/esm/node/screen-share/index.d.ts +8 -0
  22. package/esm/node/screen-share/index.js +9 -0
  23. package/esm/node/screen-share/rfb-client.d.ts +54 -0
  24. package/esm/node/screen-share/rfb-client.js +179 -0
  25. package/esm/node/screen-share/rfb-des.d.ts +11 -0
  26. package/esm/node/screen-share/rfb-des.js +143 -0
  27. package/esm/node/screen-share/rfb-encodings.d.ts +71 -0
  28. package/esm/node/screen-share/rfb-encodings.js +288 -0
  29. package/esm/node/screen-share/rfb-framebuffer.d.ts +16 -0
  30. package/esm/node/screen-share/rfb-framebuffer.js +53 -0
  31. package/esm/node/screen-share/rfb-protocol.d.ts +90 -0
  32. package/esm/node/screen-share/rfb-protocol.js +36 -0
  33. package/esm/node/screen-share/rfb-reader.d.ts +34 -0
  34. package/esm/node/screen-share/rfb-reader.js +110 -0
  35. package/esm/node/screen-share/rfb-security.d.ts +30 -0
  36. package/esm/node/screen-share/rfb-security.js +136 -0
  37. package/esm/node/screen-share/rfb-test-server.d.ts +37 -0
  38. package/esm/node/screen-share/rfb-test-server.js +116 -0
  39. package/esm/node/screen-share/screen-share-server-impl.d.ts +37 -0
  40. package/esm/node/screen-share/screen-share-server-impl.js +76 -0
  41. package/esm/node/screen-share/screen-shares.d.ts +123 -0
  42. package/esm/node/screen-share/screen-shares.js +463 -0
  43. package/esm/node/session-info-util.d.ts +10 -11
  44. package/esm/node/session-info-util.js +16 -94
  45. package/esm/node/settings-store.d.ts +1 -1
  46. package/esm/node/settings-store.js +7 -4
  47. package/esm/node/sign-in/access-store.d.ts +1 -1
  48. package/esm/node/sign-in/access-store.js +4 -1
  49. package/esm/node/sign-in/index.d.ts +2 -0
  50. package/esm/node/sign-in/index.js +2 -0
  51. package/esm/node/sign-in/polled-sign-in.d.ts +50 -0
  52. package/esm/node/sign-in/polled-sign-in.js +83 -0
  53. package/esm/node/sign-in/sign-in-devices.d.ts +55 -0
  54. package/esm/node/sign-in/sign-in-devices.js +113 -0
  55. package/esm/node/sign-in/sign-in-flow.d.ts +38 -4
  56. package/esm/node/sign-in/sign-in-flow.js +79 -4
  57. package/esm/node/sign-in/sign-in-pages.d.ts +8 -0
  58. package/esm/node/sign-in/sign-in-pages.js +137 -8
  59. package/esm/node/sign-in/sign-in-provider.d.ts +6 -0
  60. package/esm/node/sign-in/sign-in-provider.js +15 -0
  61. package/esm/node/sources-extension.d.ts +10 -0
  62. package/esm/node/sources-extension.js +18 -0
  63. package/esm/node/ta-server-impl.js +3 -3
  64. package/esm/node/ta-session-factory.js +2 -0
  65. package/esm/node/team/team-calls.js +27 -0
  66. package/esm/node/voice-bridge.d.ts +3 -1
  67. package/esm/node/voice-bridge.js +10 -3
  68. package/esm/node/voices-file.d.ts +5 -0
  69. package/esm/node/voices-file.js +14 -3
  70. package/esm/node/working-directory.d.ts +8 -2
  71. package/esm/node/working-directory.js +7 -0
  72. package/lib/node/app-commands.d.ts +1 -1
  73. package/lib/node/app-commands.js +46 -29
  74. package/lib/node/credentials.d.ts +13 -0
  75. package/lib/node/credentials.js +38 -0
  76. package/lib/node/index.d.ts +2 -0
  77. package/lib/node/index.js +2 -0
  78. package/lib/node/keep-awake.d.ts +3 -0
  79. package/lib/node/keep-awake.js +6 -1
  80. package/lib/node/package-hosts.d.ts +2 -0
  81. package/lib/node/package-hosts.js +6 -0
  82. package/lib/node/rooms-bridge.d.ts +10 -1
  83. package/lib/node/rooms-bridge.js +25 -0
  84. package/lib/node/screen-share/desktop-picture.d.ts +52 -0
  85. package/lib/node/screen-share/desktop-picture.js +136 -0
  86. package/lib/node/screen-share/desktop-session.d.ts +129 -0
  87. package/lib/node/screen-share/desktop-session.js +442 -0
  88. package/lib/node/screen-share/desktop-targets.d.ts +25 -0
  89. package/lib/node/screen-share/desktop-targets.js +84 -0
  90. package/lib/node/screen-share/desktop-viewer.d.ts +147 -0
  91. package/lib/node/screen-share/desktop-viewer.js +343 -0
  92. package/lib/node/screen-share/index.d.ts +9 -0
  93. package/lib/node/screen-share/index.js +25 -0
  94. package/lib/node/screen-share/rfb-client.d.ts +55 -0
  95. package/lib/node/screen-share/rfb-client.js +183 -0
  96. package/lib/node/screen-share/rfb-des.d.ts +12 -0
  97. package/lib/node/screen-share/rfb-des.js +146 -0
  98. package/lib/node/screen-share/rfb-encodings.d.ts +72 -0
  99. package/lib/node/screen-share/rfb-encodings.js +295 -0
  100. package/lib/node/screen-share/rfb-framebuffer.d.ts +17 -0
  101. package/lib/node/screen-share/rfb-framebuffer.js +57 -0
  102. package/lib/node/screen-share/rfb-protocol.d.ts +91 -0
  103. package/lib/node/screen-share/rfb-protocol.js +39 -0
  104. package/lib/node/screen-share/rfb-reader.d.ts +35 -0
  105. package/lib/node/screen-share/rfb-reader.js +115 -0
  106. package/lib/node/screen-share/rfb-security.d.ts +31 -0
  107. package/lib/node/screen-share/rfb-security.js +141 -0
  108. package/lib/node/screen-share/rfb-test-server.d.ts +38 -0
  109. package/lib/node/screen-share/rfb-test-server.js +120 -0
  110. package/lib/node/screen-share/screen-share-server-impl.d.ts +38 -0
  111. package/lib/node/screen-share/screen-share-server-impl.js +79 -0
  112. package/lib/node/screen-share/screen-shares.d.ts +124 -0
  113. package/lib/node/screen-share/screen-shares.js +464 -0
  114. package/lib/node/session-info-util.d.ts +10 -11
  115. package/lib/node/session-info-util.js +15 -93
  116. package/lib/node/settings-store.d.ts +1 -1
  117. package/lib/node/settings-store.js +6 -3
  118. package/lib/node/sign-in/access-store.d.ts +1 -1
  119. package/lib/node/sign-in/access-store.js +4 -1
  120. package/lib/node/sign-in/index.d.ts +2 -0
  121. package/lib/node/sign-in/index.js +2 -0
  122. package/lib/node/sign-in/polled-sign-in.d.ts +51 -0
  123. package/lib/node/sign-in/polled-sign-in.js +87 -0
  124. package/lib/node/sign-in/sign-in-devices.d.ts +56 -0
  125. package/lib/node/sign-in/sign-in-devices.js +117 -0
  126. package/lib/node/sign-in/sign-in-flow.d.ts +38 -4
  127. package/lib/node/sign-in/sign-in-flow.js +80 -5
  128. package/lib/node/sign-in/sign-in-pages.d.ts +8 -0
  129. package/lib/node/sign-in/sign-in-pages.js +136 -7
  130. package/lib/node/sign-in/sign-in-provider.d.ts +6 -0
  131. package/lib/node/sign-in/sign-in-provider.js +15 -0
  132. package/lib/node/sources-extension.d.ts +11 -0
  133. package/lib/node/sources-extension.js +22 -0
  134. package/lib/node/ta-server-impl.js +2 -2
  135. package/lib/node/ta-session-factory.js +2 -0
  136. package/lib/node/team/team-calls.js +27 -0
  137. package/lib/node/voice-bridge.d.ts +3 -1
  138. package/lib/node/voice-bridge.js +9 -2
  139. package/lib/node/voices-file.d.ts +5 -0
  140. package/lib/node/voices-file.js +14 -3
  141. package/lib/node/working-directory.d.ts +8 -2
  142. package/lib/node/working-directory.js +7 -0
  143. package/package.json +14 -10
@@ -0,0 +1,128 @@
1
+ import type { ExecSocket } from '@celljs/ta-exec';
2
+ import type { TaDesktopCapabilities, TaDesktopControl, TaDesktopFrames, TaDesktopInput, TaDesktopLink, TaDesktopScreen, TaDesktopWatcher } from '@celljs/ta-protocol';
3
+ import type { DesktopViewer } from './desktop-viewer.js';
4
+ import { RfbClient } from './rfb-client.js';
5
+ import type { RfbCredentials, RfbCursor, RfbRect, RfbScreen } from './rfb-protocol.js';
6
+ import { RfbAuthError } from './rfb-security.js';
7
+ /** How long a desktop stays connected once no page looks at it: a page that comes back within it has a picture at once. */
8
+ export declare const DESKTOP_LINGER_MS = 30000;
9
+ /** Control given back to no one after this long without input (docs/design/ta-desktop-remote.md 6.4). */
10
+ export declare const DESKTOP_CONTROL_IDLE_MS: number;
11
+ /**
12
+ * How long a desktop may keep Ta waiting while it connects (the TCP connection, then each step of signing in), the
13
+ * time the user takes to give the password aside: a server that never answers is lost, and says so.
14
+ */
15
+ export declare const DESKTOP_CONNECT_TIMEOUT_MS = 20000;
16
+ /** How many times a desktop that refuses a sign-in is asked again while a page waits for it. */
17
+ export declare const DESKTOP_SIGN_INS = 5;
18
+ /** The desktop refused the password (and account) it was given. */
19
+ export declare class RfbRefusedError extends RfbAuthError {
20
+ }
21
+ /** What a session needs of the service that made it. */
22
+ export interface DesktopSessionHost {
23
+ /** A TCP connection to the desktop's VNC server, through the ExecEnvironment of its machine. */
24
+ connect(): Promise<ExecSocket>;
25
+ /**
26
+ * The password (and an account) to sign in with, kept or asked of whoever is there; undefined when no one
27
+ * answered. `refused`: the desktop refused the last one, and it is asked again.
28
+ */
29
+ credentials(need: {
30
+ username: boolean;
31
+ refused: boolean;
32
+ }): Promise<RfbCredentials | undefined>;
33
+ /** Whether the desktop took the sign-in it was given last: told once signing in ends. */
34
+ signedIn(taken: boolean): void;
35
+ /** The display name of a principal. */
36
+ nameOf(principal: string): string;
37
+ /** The session has no viewer and lingered: the service drops it. */
38
+ idle(): void;
39
+ }
40
+ type PngCursor = {
41
+ hotX: number;
42
+ hotY: number;
43
+ width: number;
44
+ height: number;
45
+ png: string;
46
+ } | null;
47
+ /**
48
+ * One desktop connected (docs/design/ta-desktop-remote.md 5.1, 9): one RFB connection for every page that looks at
49
+ * it, each a DesktopViewer with its own view and frames, and one of them, at most, in control.
50
+ */
51
+ export declare class DesktopSession {
52
+ readonly id: string;
53
+ readonly name: string;
54
+ protected readonly host: DesktopSessionHost;
55
+ readonly viewers: Map<string, DesktopViewer>;
56
+ link: TaDesktopLink;
57
+ error?: string;
58
+ title: string;
59
+ protected controller?: {
60
+ viewerId: string;
61
+ since: number;
62
+ };
63
+ protected client?: RfbClient;
64
+ protected connecting?: Promise<void>;
65
+ protected cursor: PngCursor;
66
+ protected screens: RfbScreen[];
67
+ protected sawScreens: boolean;
68
+ protected sawCursor: boolean;
69
+ protected lingerTimer?: ReturnType<typeof setTimeout>;
70
+ protected idleTimer?: ReturnType<typeof setInterval>;
71
+ /** Where the pointer was put last: where held buttons are let go. */
72
+ protected pointerAt: {
73
+ x: number;
74
+ y: number;
75
+ };
76
+ /** The first pixels came: before them the framebuffer is black, and no tile of it goes. */
77
+ protected painted: boolean;
78
+ connectTimeoutMs: number;
79
+ constructor(id: string, name: string, host: DesktopSessionHost);
80
+ get framebuffer(): {
81
+ width: number;
82
+ height: number;
83
+ };
84
+ /** Connected, with a whole screen to show. */
85
+ get showing(): boolean;
86
+ /** Connects once; a session that was lost connects again when a page comes. */
87
+ start(): Promise<void>;
88
+ add(viewer: DesktopViewer): void;
89
+ remove(viewerId: string): void;
90
+ close(): void;
91
+ /** Frame `want` of a viewer (docs/design/ta-desktop-remote.md 5.4). */
92
+ read(viewer: DesktopViewer, want: number): Promise<TaDesktopFrames>;
93
+ /** Who works it, as a page sees it: `self` for the page of `viewer`. */
94
+ controlFor(viewerId?: string): TaDesktopControl | null;
95
+ watchers(viewerId?: string): TaDesktopWatcher[];
96
+ get controlled(): boolean;
97
+ /** The screens as the protocol says them. */
98
+ screensOf(): TaDesktopScreen[];
99
+ capabilities(): TaDesktopCapabilities;
100
+ /** The viewer takes control: from another page of the same person, or when no one has it. */
101
+ take(viewer: DesktopViewer, may: (holder: DesktopViewer) => boolean): TaDesktopControl | null;
102
+ release(viewer: DesktopViewer): void;
103
+ /** Input of the viewer in control, taken once by its seq. */
104
+ input(viewer: DesktopViewer, batch: {
105
+ seq: number;
106
+ events: TaDesktopInput[];
107
+ }): void;
108
+ /** Text into the clipboard of the desktop, then the keys that paste on its system when asked. */
109
+ paste(viewer: DesktopViewer, text: string, press: boolean, clientMessageId: string): void;
110
+ protected mustControl(viewer: DesktopViewer): void;
111
+ protected live(): RfbClient;
112
+ /** The rectangle of a screen of the framebuffer: the one named, else the primary one. */
113
+ screenOf(id?: string): RfbRect | undefined;
114
+ protected screenRect(id?: string): RfbRect | undefined;
115
+ protected frame(viewer: DesktopViewer, made: {
116
+ seq: number;
117
+ whole: boolean;
118
+ waited: number;
119
+ }): Promise<TaDesktopFrames>;
120
+ /** Connects, asking again while the desktop refuses what it is given and a page still waits for it. */
121
+ protected signIn(): Promise<void>;
122
+ protected connect(refused?: boolean): Promise<void>;
123
+ protected lose(error: Error): void;
124
+ protected tellAll(): void;
125
+ protected checkIdle(): void;
126
+ static cursorPng(cursor: RfbCursor | null): PngCursor;
127
+ }
128
+ export {};
@@ -0,0 +1,437 @@
1
+ import { PngThumbnail } from '../png-thumbnail.js';
2
+ import { TaErrors } from '../ta-errors.js';
3
+ import { DesktopPicture } from './desktop-picture.js';
4
+ import { RfbClient } from './rfb-client.js';
5
+ import { RfbAuthError } from './rfb-security.js';
6
+ /** How long a desktop stays connected once no page looks at it: a page that comes back within it has a picture at once. */
7
+ export const DESKTOP_LINGER_MS = 30000;
8
+ /** Control given back to no one after this long without input (docs/design/ta-desktop-remote.md 6.4). */
9
+ export const DESKTOP_CONTROL_IDLE_MS = 15 * 60000;
10
+ /**
11
+ * How long a desktop may keep Ta waiting while it connects (the TCP connection, then each step of signing in), the
12
+ * time the user takes to give the password aside: a server that never answers is lost, and says so.
13
+ */
14
+ export const DESKTOP_CONNECT_TIMEOUT_MS = 20000;
15
+ /** How many times a desktop that refuses a sign-in is asked again while a page waits for it. */
16
+ export const DESKTOP_SIGN_INS = 5;
17
+ /** The most frames a second a page gets (docs/design/ta-desktop-remote.md 8.3). */
18
+ const MAX_FPS = 30;
19
+ /** The desktop refused the password (and account) it was given. */
20
+ export class RfbRefusedError extends RfbAuthError {
21
+ }
22
+ /**
23
+ * One desktop connected (docs/design/ta-desktop-remote.md 5.1, 9): one RFB connection for every page that looks at
24
+ * it, each a DesktopViewer with its own view and frames, and one of them, at most, in control.
25
+ */
26
+ export class DesktopSession {
27
+ constructor(id, name, host) {
28
+ this.id = id;
29
+ this.name = name;
30
+ this.host = host;
31
+ this.viewers = new Map();
32
+ this.link = 'connecting';
33
+ this.title = '';
34
+ this.cursor = null;
35
+ this.screens = [];
36
+ this.sawScreens = false;
37
+ this.sawCursor = false;
38
+ /** Where the pointer was put last: where held buttons are let go. */
39
+ this.pointerAt = { x: 0, y: 0 };
40
+ /** The first pixels came: before them the framebuffer is black, and no tile of it goes. */
41
+ this.painted = false;
42
+ this.connectTimeoutMs = DESKTOP_CONNECT_TIMEOUT_MS;
43
+ }
44
+ get framebuffer() {
45
+ var _a;
46
+ var _b;
47
+ return (_b = (_a = this.client) === null || _a === void 0 ? void 0 : _a.framebuffer) !== null && _b !== void 0 ? _b : { width: 0, height: 0 };
48
+ }
49
+ /** Connected, with a whole screen to show. */
50
+ get showing() {
51
+ return this.link === 'live' && this.painted;
52
+ }
53
+ /** Connects once; a session that was lost connects again when a page comes. */
54
+ start() {
55
+ if (this.connecting && this.link !== 'lost') {
56
+ return this.connecting;
57
+ }
58
+ this.link = 'connecting';
59
+ this.error = undefined;
60
+ this.connecting = this.signIn().catch(error => this.lose(error));
61
+ return this.connecting;
62
+ }
63
+ add(viewer) {
64
+ var _a;
65
+ clearTimeout(this.lingerTimer);
66
+ this.lingerTimer = undefined;
67
+ this.viewers.set(viewer.id, viewer);
68
+ viewer.fit(this.framebuffer, this.screenRect(viewer.asked.screen));
69
+ void this.start();
70
+ (_a = this.idleTimer) !== null && _a !== void 0 ? _a : (this.idleTimer = setInterval(() => this.checkIdle(), 60000));
71
+ }
72
+ remove(viewerId) {
73
+ var _a;
74
+ const viewer = this.viewers.get(viewerId);
75
+ if (!viewer) {
76
+ return;
77
+ }
78
+ if (((_a = this.controller) === null || _a === void 0 ? void 0 : _a.viewerId) === viewerId) {
79
+ this.release(viewer);
80
+ }
81
+ viewer.stop();
82
+ this.viewers.delete(viewerId);
83
+ if (this.viewers.size > 0) {
84
+ return;
85
+ }
86
+ clearInterval(this.idleTimer);
87
+ this.idleTimer = undefined;
88
+ if (this.link !== 'live') {
89
+ // No one waits for it: a connection still signing in (a password asked) ends now.
90
+ this.close();
91
+ this.host.idle();
92
+ return;
93
+ }
94
+ this.lingerTimer = setTimeout(() => {
95
+ if (this.viewers.size === 0) {
96
+ this.close();
97
+ this.host.idle();
98
+ }
99
+ }, DESKTOP_LINGER_MS);
100
+ }
101
+ close() {
102
+ var _a;
103
+ clearTimeout(this.lingerTimer);
104
+ clearInterval(this.idleTimer);
105
+ for (const viewer of this.viewers.values()) {
106
+ viewer.stop();
107
+ }
108
+ this.viewers.clear();
109
+ this.controller = undefined;
110
+ (_a = this.client) === null || _a === void 0 ? void 0 : _a.end();
111
+ this.client = undefined;
112
+ }
113
+ /** Frame `want` of a viewer (docs/design/ta-desktop-remote.md 5.4). */
114
+ read(viewer, want) {
115
+ return viewer.claim(want, (one, frame) => this.frame(one, frame), { minGapMs: 1000 / MAX_FPS, showing: () => this.showing });
116
+ }
117
+ /** Who works it, as a page sees it: `self` for the page of `viewer`. */
118
+ controlFor(viewerId) {
119
+ const holder = this.controller && this.viewers.get(this.controller.viewerId);
120
+ if (!holder || !this.controller) {
121
+ return null;
122
+ }
123
+ return {
124
+ by: holder.principal,
125
+ name: this.host.nameOf(holder.principal),
126
+ device: holder.device,
127
+ self: holder.id === viewerId,
128
+ since: this.controller.since,
129
+ until: Math.max(holder.lastInputAt, this.controller.since) + DESKTOP_CONTROL_IDLE_MS,
130
+ };
131
+ }
132
+ watchers(viewerId) {
133
+ return [...this.viewers.values()].map(viewer => ({
134
+ by: viewer.principal,
135
+ name: this.host.nameOf(viewer.principal),
136
+ device: viewer.device,
137
+ self: viewer.id === viewerId,
138
+ since: viewer.since,
139
+ }));
140
+ }
141
+ get controlled() {
142
+ return !!this.controller;
143
+ }
144
+ /** The screens as the protocol says them. */
145
+ screensOf() {
146
+ var _a;
147
+ const { width, height } = this.framebuffer;
148
+ const scale = this.capabilities().system === 'mac' && width >= 2560 ? 2 : 1;
149
+ const list = this.sawScreens && this.screens.length ? this.screens : [{ id: -1, x: 0, y: 0, width, height }];
150
+ const primary = (_a = list.find(screen => screen.x === 0 && screen.y === 0)) !== null && _a !== void 0 ? _a : list[0];
151
+ return list.map(screen => ({
152
+ id: screen.id < 0 ? 'whole' : String(screen.id),
153
+ x: screen.x,
154
+ y: screen.y,
155
+ width: screen.width,
156
+ height: screen.height,
157
+ scale,
158
+ primary: screen === primary,
159
+ }));
160
+ }
161
+ capabilities() {
162
+ const title = this.title;
163
+ const system = /mac|apple/i.test(title) ? 'mac' : /windows/i.test(title) ? 'windows' : /:\d+$/.test(title) ? 'linux' : undefined;
164
+ return {
165
+ keyboard: 'keysym',
166
+ unicodeKeys: null,
167
+ clipboardWrite: true,
168
+ clipboardRead: false,
169
+ cursor: this.sawCursor,
170
+ screens: this.sawScreens,
171
+ ...(system ? { system } : {}),
172
+ };
173
+ }
174
+ /** The viewer takes control: from another page of the same person, or when no one has it. */
175
+ take(viewer, may) {
176
+ var _a;
177
+ const holder = this.controller && this.viewers.get(this.controller.viewerId);
178
+ if (holder && holder.id !== viewer.id) {
179
+ if (!may(holder)) {
180
+ throw TaErrors.invalidArgument(`${this.host.nameOf(holder.principal) || 'Someone'} works this desktop now.`, 'Ask them to let go of it first.');
181
+ }
182
+ holder.letGo(this.client, this.pointerAt);
183
+ }
184
+ if (((_a = this.controller) === null || _a === void 0 ? void 0 : _a.viewerId) !== viewer.id) {
185
+ this.controller = { viewerId: viewer.id, since: Date.now() };
186
+ }
187
+ viewer.lastInputAt = Date.now();
188
+ this.tellAll();
189
+ return this.controlFor(viewer.id);
190
+ }
191
+ release(viewer) {
192
+ var _a;
193
+ if (((_a = this.controller) === null || _a === void 0 ? void 0 : _a.viewerId) !== viewer.id) {
194
+ return;
195
+ }
196
+ viewer.letGo(this.client, this.pointerAt);
197
+ this.controller = undefined;
198
+ this.tellAll();
199
+ }
200
+ /** Input of the viewer in control, taken once by its seq. */
201
+ input(viewer, batch) {
202
+ this.mustControl(viewer);
203
+ if (typeof (batch === null || batch === void 0 ? void 0 : batch.seq) !== 'number' || batch.seq <= viewer.inputSeq) {
204
+ return;
205
+ }
206
+ viewer.inputSeq = batch.seq;
207
+ const client = this.live();
208
+ const events = Array.isArray(batch.events) ? batch.events.slice(0, 200) : [];
209
+ viewer.apply(client, events);
210
+ const last = [...events].reverse().find(event => event.kind !== 'key');
211
+ if (last) {
212
+ this.pointerAt = { x: Number(last.x) || 0, y: Number(last.y) || 0 };
213
+ }
214
+ }
215
+ /** Text into the clipboard of the desktop, then the keys that paste on its system when asked. */
216
+ paste(viewer, text, press, clientMessageId) {
217
+ this.mustControl(viewer);
218
+ if (viewer.pastes.has(clientMessageId)) {
219
+ return;
220
+ }
221
+ viewer.pastes.add(clientMessageId);
222
+ const client = this.live();
223
+ client.clipboard(String(text !== null && text !== void 0 ? text : ''));
224
+ if (press) {
225
+ // macOS pastes with the command key, the others with control.
226
+ const modifier = this.capabilities().system === 'mac' ? 0xffe7 : 0xffe3;
227
+ client.key(modifier, true);
228
+ client.key(0x76, true);
229
+ client.key(0x76, false);
230
+ client.key(modifier, false);
231
+ }
232
+ }
233
+ mustControl(viewer) {
234
+ var _a;
235
+ if (((_a = this.controller) === null || _a === void 0 ? void 0 : _a.viewerId) !== viewer.id) {
236
+ throw TaErrors.invalidArgument('This page does not work the desktop now.', 'Take control of it first (takeDesktopControl).');
237
+ }
238
+ }
239
+ live() {
240
+ if (!this.client || this.link !== 'live') {
241
+ throw TaErrors.invalidArgument('The desktop is not connected.', 'Wait until it is, or open it again.');
242
+ }
243
+ return this.client;
244
+ }
245
+ /** The rectangle of a screen of the framebuffer: the one named, else the primary one. */
246
+ screenOf(id) {
247
+ return this.screenRect(id);
248
+ }
249
+ screenRect(id) {
250
+ var _a;
251
+ const screens = this.screensOf();
252
+ const screen = (_a = screens.find(one => one.id === id)) !== null && _a !== void 0 ? _a : screens.find(one => one.primary);
253
+ return screen && screen.width > 0 ? { x: screen.x, y: screen.y, w: screen.width, h: screen.height } : undefined;
254
+ }
255
+ async frame(viewer, made) {
256
+ var _a;
257
+ const taken = viewer.take();
258
+ const control = this.controlFor(viewer.id);
259
+ const controlKey = JSON.stringify(control && { by: control.by, self: control.self, device: control.device });
260
+ const out = { seq: made.seq, base: made.whole ? 0 : made.seq - 1, waited: made.waited, tiles: [] };
261
+ if (made.whole || viewer.told.control !== controlKey) {
262
+ viewer.told.control = controlKey;
263
+ out.control = control;
264
+ }
265
+ if (made.whole || viewer.told.state !== this.link) {
266
+ viewer.told.state = this.link;
267
+ out.state = this.link;
268
+ if (this.error) {
269
+ out.error = this.error;
270
+ }
271
+ }
272
+ const framebuffer = (_a = this.client) === null || _a === void 0 ? void 0 : _a.framebuffer;
273
+ if (!framebuffer || framebuffer.width < 1 || !viewer.view || !this.showing) {
274
+ // Nothing to draw yet: what was to be told goes with the first frame once there is.
275
+ viewer.putBack({ ...taken, news: false });
276
+ return out;
277
+ }
278
+ if (made.whole || taken.size) {
279
+ out.size = { width: framebuffer.width, height: framebuffer.height, screens: this.screensOf() };
280
+ }
281
+ if (made.whole || taken.cursor) {
282
+ out.cursor = this.cursor;
283
+ }
284
+ const holder = this.controller && this.viewers.get(this.controller.viewerId);
285
+ if (holder && holder.id !== viewer.id) {
286
+ out.pointer = { ...this.pointerAt };
287
+ }
288
+ const view = viewer.view;
289
+ const { columns, rows } = DesktopPicture.grid(view);
290
+ const all = made.whole || taken.all ? Array.from({ length: columns * rows }, (_, index) => index) : [...taken.tiles];
291
+ out.view = { rect: view.rect, width: view.width, height: view.height };
292
+ out.tiles = await DesktopPicture.encode(framebuffer, view, all, viewer.qualityFor(all.length), () => viewer.view === view);
293
+ if (out.tiles.length < all.length) {
294
+ // The view changed while its tiles were made: all of the new one goes with the next frame.
295
+ viewer.putBack({ tiles: new Set(), all: true, size: false, cursor: false, news: false });
296
+ }
297
+ return out;
298
+ }
299
+ /** Connects, asking again while the desktop refuses what it is given and a page still waits for it. */
300
+ async signIn() {
301
+ for (let tries = 1;; tries++) {
302
+ try {
303
+ return await this.connect(tries > 1);
304
+ }
305
+ catch (error) {
306
+ if (!(error instanceof RfbRefusedError) || tries >= DESKTOP_SIGN_INS || this.viewers.size === 0) {
307
+ throw error;
308
+ }
309
+ }
310
+ }
311
+ }
312
+ async connect(refused = false) {
313
+ this.painted = false;
314
+ let asked = false;
315
+ const seconds = Math.round(this.connectTimeoutMs / 1000);
316
+ const timedOut = new Error(`The desktop did not answer in ${seconds} seconds: is its screen sharing on, and the address right?`);
317
+ let expired = false;
318
+ let timer;
319
+ const arm = (onExpire) => {
320
+ clearTimeout(timer);
321
+ timer = setTimeout(() => {
322
+ expired = true;
323
+ onExpire();
324
+ }, this.connectTimeoutMs);
325
+ };
326
+ const socket = await new Promise((resolve, reject) => {
327
+ arm(() => reject(timedOut));
328
+ this.host.connect().then(connected => (expired ? connected.end() : resolve(connected)), error => reject(error));
329
+ }).finally(() => clearTimeout(timer));
330
+ // Waiting on the server, not on the user: the clock stops while the password is asked.
331
+ const client = new RfbClient(socket, {
332
+ credentials: async (need) => {
333
+ clearTimeout(timer);
334
+ asked = true;
335
+ const given = await this.host.credentials({ ...need, refused });
336
+ if (!given) {
337
+ throw new Error('No one gave the password of the desktop.');
338
+ }
339
+ stall();
340
+ return given;
341
+ },
342
+ });
343
+ const stall = () => arm(() => client.end());
344
+ this.client = client;
345
+ stall();
346
+ try {
347
+ this.title = (await client.connect()).name;
348
+ this.host.signedIn(true);
349
+ }
350
+ catch (error) {
351
+ client.end();
352
+ if (error instanceof RfbAuthError && asked) {
353
+ this.host.signedIn(false);
354
+ throw new RfbRefusedError(error.message);
355
+ }
356
+ throw expired ? timedOut : error;
357
+ }
358
+ finally {
359
+ clearTimeout(timer);
360
+ }
361
+ this.link = 'live';
362
+ this.tellAll();
363
+ void client.run({
364
+ size: (width, height, screens) => {
365
+ this.screens = screens;
366
+ this.sawScreens || (this.sawScreens = client.extended);
367
+ for (const viewer of this.viewers.values()) {
368
+ viewer.sized();
369
+ viewer.fit({ width, height }, this.screenRect(viewer.asked.screen));
370
+ }
371
+ },
372
+ damage: rect => {
373
+ // The first update of a server may only tell the screens; pixels come in a later one.
374
+ this.painted = true;
375
+ for (const viewer of this.viewers.values()) {
376
+ viewer.damage(rect);
377
+ }
378
+ },
379
+ update: () => {
380
+ for (const viewer of this.viewers.values()) {
381
+ viewer.updated();
382
+ }
383
+ // The next update is asked once the last came: a server never sends more than Ta takes.
384
+ client.request();
385
+ },
386
+ cursor: cursor => {
387
+ this.sawCursor = true;
388
+ this.cursor = DesktopSession.cursorPng(cursor);
389
+ for (const viewer of this.viewers.values()) {
390
+ viewer.cursorChanged();
391
+ }
392
+ },
393
+ clipboard: () => undefined,
394
+ bell: () => undefined,
395
+ closed: error => {
396
+ if (this.client === client) {
397
+ this.client = undefined;
398
+ if (error) {
399
+ this.lose(error);
400
+ }
401
+ }
402
+ },
403
+ });
404
+ }
405
+ lose(error) {
406
+ this.link = 'lost';
407
+ this.error = error.message;
408
+ this.controller = undefined;
409
+ this.tellAll();
410
+ }
411
+ tellAll() {
412
+ for (const viewer of this.viewers.values()) {
413
+ viewer.news();
414
+ }
415
+ }
416
+ checkIdle() {
417
+ var _a;
418
+ var _b;
419
+ const holder = this.controller && this.viewers.get(this.controller.viewerId);
420
+ if (holder && Date.now() - Math.max(holder.lastInputAt, (_b = (_a = this.controller) === null || _a === void 0 ? void 0 : _a.since) !== null && _b !== void 0 ? _b : 0) > DESKTOP_CONTROL_IDLE_MS) {
421
+ this.release(holder);
422
+ }
423
+ }
424
+ static cursorPng(cursor) {
425
+ if (!cursor) {
426
+ return null;
427
+ }
428
+ return {
429
+ hotX: cursor.hotX,
430
+ hotY: cursor.hotY,
431
+ width: cursor.width,
432
+ height: cursor.height,
433
+ png: Buffer.from(PngThumbnail.encode(cursor.rgba, cursor.width, cursor.height)).toString('base64'),
434
+ };
435
+ }
436
+ }
437
+ //# sourceMappingURL=desktop-session.js.map
@@ -0,0 +1,24 @@
1
+ import { type TaDesktopInfo, type TaDesktopTarget } from '@celljs/ta-protocol';
2
+ import type { TaWorkingDirectory } from '../working-directory.js';
3
+ /** A desktop the user added, as ta.json keeps it under `desktops`. */
4
+ export interface DesktopEntry {
5
+ via: string;
6
+ host: string;
7
+ port: number;
8
+ name?: string;
9
+ }
10
+ /**
11
+ * Which desktops there are (docs/design/ta-desktop-remote.md 4.1): this computer's own, that of each host of ssh
12
+ * directories, and those the user added. Only these are ever connected to, so that a page cannot have ta serve open
13
+ * a port of any machine it names.
14
+ */
15
+ export declare class DesktopTargets {
16
+ protected readonly store: Pick<TaWorkingDirectory, 'desktops' | 'setDesktops'>;
17
+ constructor(store: Pick<TaWorkingDirectory, 'desktops' | 'setDesktops'>);
18
+ list(computer: string, sshHosts: string[]): TaDesktopInfo[];
19
+ add(target: TaDesktopTarget): TaDesktopInfo;
20
+ remove(desktopId: string): void;
21
+ /** A desktop added: of this computer (by loopback), of a host of ssh, or of the network. */
22
+ protected static info(entry: DesktopEntry): TaDesktopInfo;
23
+ protected entries(): DesktopEntry[];
24
+ }
@@ -0,0 +1,80 @@
1
+ import { TaDesktopUtil, TaLocation } from '@celljs/ta-protocol';
2
+ import { TaErrors } from '../ta-errors.js';
3
+ /** The most desktops kept. */
4
+ const MAX_DESKTOPS = 50;
5
+ /**
6
+ * Which desktops there are (docs/design/ta-desktop-remote.md 4.1): this computer's own, that of each host of ssh
7
+ * directories, and those the user added. Only these are ever connected to, so that a page cannot have ta serve open
8
+ * a port of any machine it names.
9
+ */
10
+ export class DesktopTargets {
11
+ constructor(store) {
12
+ this.store = store;
13
+ }
14
+ list(computer, sshHosts) {
15
+ const idle = { state: 'idle', control: null, viewers: [] };
16
+ const own = { id: TaDesktopUtil.id({ via: '' }), target: { via: '' }, name: computer, source: 'this', ...idle };
17
+ const hosts = sshHosts.map(host => {
18
+ const target = { via: TaLocation.ssh(host) };
19
+ return { id: TaDesktopUtil.id(target), target, name: TaLocation.hostLabel(host), source: 'ssh', ...idle };
20
+ });
21
+ const added = this.entries().map(entry => DesktopTargets.info(entry));
22
+ const seen = new Set();
23
+ return [own, ...added, ...hosts].filter(info => !seen.has(info.id) && seen.add(info.id));
24
+ }
25
+ add(target) {
26
+ const name = target === null || target === void 0 ? void 0 : target.name;
27
+ const via = typeof (target === null || target === void 0 ? void 0 : target.via) === 'string' ? target.via.trim() : '';
28
+ if (via && (!TaLocation.isRemote(via) || TaLocation.isCloud(TaLocation.host(via)) || TaLocation.path(via) !== '')) {
29
+ throw TaErrors.invalidArgument(`${JSON.stringify(via)} is not a machine.`, 'Leave it empty for this computer, or write ssh://<host>.');
30
+ }
31
+ const host = TaDesktopUtil.host(target);
32
+ const port = TaDesktopUtil.port(target);
33
+ if (!TaDesktopUtil.validHost(host)) {
34
+ throw TaErrors.invalidArgument(`${JSON.stringify(host)} is not a host name or an address.`, 'Write the name or the IP address of the computer.');
35
+ }
36
+ if (!TaDesktopUtil.validPort(port)) {
37
+ throw TaErrors.invalidArgument(`${port} is not a port of a VNC server.`, 'VNC listens on 5900, or 5900 + the number of the display.');
38
+ }
39
+ const entry = { via, host, port, ...(typeof name === 'string' && name.trim() ? { name: name.trim().slice(0, 80) } : {}) };
40
+ const id = TaDesktopUtil.id(entry);
41
+ const others = this.entries().filter(one => TaDesktopUtil.id(one) !== id);
42
+ if (others.length >= MAX_DESKTOPS) {
43
+ throw TaErrors.invalidArgument(`${MAX_DESKTOPS} desktops are kept already.`, 'Remove one first.');
44
+ }
45
+ this.store.setDesktops([entry, ...others]);
46
+ return DesktopTargets.info(entry);
47
+ }
48
+ remove(desktopId) {
49
+ const entries = this.entries();
50
+ const left = entries.filter(one => TaDesktopUtil.id(one) !== desktopId);
51
+ if (left.length === entries.length) {
52
+ throw TaErrors.invalidArgument(`${desktopId} was not added.`, 'Only the desktops added can be removed.');
53
+ }
54
+ this.store.setDesktops(left);
55
+ }
56
+ /** A desktop added: of this computer (by loopback), of a host of ssh, or of the network. */
57
+ static info(entry) {
58
+ const target = { via: entry.via, host: entry.host, port: entry.port, ...(entry.name ? { name: entry.name } : {}) };
59
+ const source = entry.via ? 'ssh' : TaDesktopUtil.loopback(entry.host) ? 'this' : 'lan';
60
+ return {
61
+ id: TaDesktopUtil.id(target),
62
+ target,
63
+ name: entry.name || `${entry.host}:${entry.port}`,
64
+ source,
65
+ state: 'idle',
66
+ control: null,
67
+ viewers: [],
68
+ };
69
+ }
70
+ entries() {
71
+ const kept = this.store.desktops();
72
+ return Array.isArray(kept)
73
+ ? kept.filter((one) => {
74
+ const entry = one;
75
+ return !!entry && typeof entry.via === 'string' && TaDesktopUtil.validHost(entry.host) && TaDesktopUtil.validPort(entry.port);
76
+ })
77
+ : [];
78
+ }
79
+ }
80
+ //# sourceMappingURL=desktop-targets.js.map