@celljs/ta-host 4.17.0 → 4.19.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 (153) hide show
  1. package/esm/node/account-service.d.ts +73 -0
  2. package/esm/node/account-service.js +304 -0
  3. package/esm/node/app-commands.d.ts +1 -1
  4. package/esm/node/app-commands.js +46 -29
  5. package/esm/node/credentials.d.ts +12 -0
  6. package/esm/node/credentials.js +35 -0
  7. package/esm/node/host-protocol.d.ts +2 -0
  8. package/esm/node/host-protocol.js +2 -0
  9. package/esm/node/index.d.ts +3 -0
  10. package/esm/node/index.js +3 -0
  11. package/esm/node/keep-awake.d.ts +3 -0
  12. package/esm/node/keep-awake.js +6 -1
  13. package/esm/node/package-hosts.d.ts +4 -0
  14. package/esm/node/package-hosts.js +18 -1
  15. package/esm/node/relay-bridge.js +1 -1
  16. package/esm/node/rooms-bridge.d.ts +10 -1
  17. package/esm/node/rooms-bridge.js +25 -0
  18. package/esm/node/screen-share/desktop-picture.d.ts +51 -0
  19. package/esm/node/screen-share/desktop-picture.js +133 -0
  20. package/esm/node/screen-share/desktop-session.d.ts +128 -0
  21. package/esm/node/screen-share/desktop-session.js +437 -0
  22. package/esm/node/screen-share/desktop-targets.d.ts +24 -0
  23. package/esm/node/screen-share/desktop-targets.js +80 -0
  24. package/esm/node/screen-share/desktop-viewer.d.ts +146 -0
  25. package/esm/node/screen-share/desktop-viewer.js +339 -0
  26. package/esm/node/screen-share/index.d.ts +8 -0
  27. package/esm/node/screen-share/index.js +9 -0
  28. package/esm/node/screen-share/rfb-client.d.ts +54 -0
  29. package/esm/node/screen-share/rfb-client.js +179 -0
  30. package/esm/node/screen-share/rfb-des.d.ts +11 -0
  31. package/esm/node/screen-share/rfb-des.js +143 -0
  32. package/esm/node/screen-share/rfb-encodings.d.ts +71 -0
  33. package/esm/node/screen-share/rfb-encodings.js +288 -0
  34. package/esm/node/screen-share/rfb-framebuffer.d.ts +16 -0
  35. package/esm/node/screen-share/rfb-framebuffer.js +53 -0
  36. package/esm/node/screen-share/rfb-protocol.d.ts +90 -0
  37. package/esm/node/screen-share/rfb-protocol.js +36 -0
  38. package/esm/node/screen-share/rfb-reader.d.ts +34 -0
  39. package/esm/node/screen-share/rfb-reader.js +110 -0
  40. package/esm/node/screen-share/rfb-security.d.ts +30 -0
  41. package/esm/node/screen-share/rfb-security.js +136 -0
  42. package/esm/node/screen-share/rfb-test-server.d.ts +37 -0
  43. package/esm/node/screen-share/rfb-test-server.js +116 -0
  44. package/esm/node/screen-share/screen-share-server-impl.d.ts +37 -0
  45. package/esm/node/screen-share/screen-share-server-impl.js +76 -0
  46. package/esm/node/screen-share/screen-shares.d.ts +123 -0
  47. package/esm/node/screen-share/screen-shares.js +463 -0
  48. package/esm/node/session-info-util.d.ts +10 -11
  49. package/esm/node/session-info-util.js +16 -94
  50. package/esm/node/settings-store.d.ts +1 -1
  51. package/esm/node/settings-store.js +7 -4
  52. package/esm/node/sign-in/access-store.d.ts +1 -1
  53. package/esm/node/sign-in/access-store.js +4 -1
  54. package/esm/node/sign-in/index.d.ts +2 -0
  55. package/esm/node/sign-in/index.js +2 -0
  56. package/esm/node/sign-in/polled-sign-in.d.ts +50 -0
  57. package/esm/node/sign-in/polled-sign-in.js +83 -0
  58. package/esm/node/sign-in/sign-in-devices.d.ts +55 -0
  59. package/esm/node/sign-in/sign-in-devices.js +113 -0
  60. package/esm/node/sign-in/sign-in-flow.d.ts +38 -4
  61. package/esm/node/sign-in/sign-in-flow.js +79 -4
  62. package/esm/node/sign-in/sign-in-pages.d.ts +8 -0
  63. package/esm/node/sign-in/sign-in-pages.js +137 -8
  64. package/esm/node/sign-in/sign-in-provider.d.ts +6 -0
  65. package/esm/node/sign-in/sign-in-provider.js +15 -0
  66. package/esm/node/sources-extension.d.ts +10 -0
  67. package/esm/node/sources-extension.js +18 -0
  68. package/esm/node/ta-server-impl.js +3 -3
  69. package/esm/node/ta-session-factory.js +2 -0
  70. package/esm/node/team/team-calls.js +35 -0
  71. package/esm/node/voice-bridge.d.ts +3 -1
  72. package/esm/node/voice-bridge.js +10 -3
  73. package/esm/node/voices-file.d.ts +5 -0
  74. package/esm/node/voices-file.js +14 -3
  75. package/esm/node/working-directory.d.ts +8 -2
  76. package/esm/node/working-directory.js +7 -0
  77. package/lib/node/account-service.d.ts +74 -0
  78. package/lib/node/account-service.js +307 -0
  79. package/lib/node/app-commands.d.ts +1 -1
  80. package/lib/node/app-commands.js +46 -29
  81. package/lib/node/credentials.d.ts +13 -0
  82. package/lib/node/credentials.js +38 -0
  83. package/lib/node/host-protocol.d.ts +2 -0
  84. package/lib/node/host-protocol.js +3 -1
  85. package/lib/node/index.d.ts +3 -0
  86. package/lib/node/index.js +3 -0
  87. package/lib/node/keep-awake.d.ts +3 -0
  88. package/lib/node/keep-awake.js +6 -1
  89. package/lib/node/package-hosts.d.ts +4 -0
  90. package/lib/node/package-hosts.js +17 -0
  91. package/lib/node/relay-bridge.js +1 -1
  92. package/lib/node/rooms-bridge.d.ts +10 -1
  93. package/lib/node/rooms-bridge.js +25 -0
  94. package/lib/node/screen-share/desktop-picture.d.ts +52 -0
  95. package/lib/node/screen-share/desktop-picture.js +136 -0
  96. package/lib/node/screen-share/desktop-session.d.ts +129 -0
  97. package/lib/node/screen-share/desktop-session.js +442 -0
  98. package/lib/node/screen-share/desktop-targets.d.ts +25 -0
  99. package/lib/node/screen-share/desktop-targets.js +84 -0
  100. package/lib/node/screen-share/desktop-viewer.d.ts +147 -0
  101. package/lib/node/screen-share/desktop-viewer.js +343 -0
  102. package/lib/node/screen-share/index.d.ts +9 -0
  103. package/lib/node/screen-share/index.js +25 -0
  104. package/lib/node/screen-share/rfb-client.d.ts +55 -0
  105. package/lib/node/screen-share/rfb-client.js +183 -0
  106. package/lib/node/screen-share/rfb-des.d.ts +12 -0
  107. package/lib/node/screen-share/rfb-des.js +146 -0
  108. package/lib/node/screen-share/rfb-encodings.d.ts +72 -0
  109. package/lib/node/screen-share/rfb-encodings.js +295 -0
  110. package/lib/node/screen-share/rfb-framebuffer.d.ts +17 -0
  111. package/lib/node/screen-share/rfb-framebuffer.js +57 -0
  112. package/lib/node/screen-share/rfb-protocol.d.ts +91 -0
  113. package/lib/node/screen-share/rfb-protocol.js +39 -0
  114. package/lib/node/screen-share/rfb-reader.d.ts +35 -0
  115. package/lib/node/screen-share/rfb-reader.js +115 -0
  116. package/lib/node/screen-share/rfb-security.d.ts +31 -0
  117. package/lib/node/screen-share/rfb-security.js +141 -0
  118. package/lib/node/screen-share/rfb-test-server.d.ts +38 -0
  119. package/lib/node/screen-share/rfb-test-server.js +120 -0
  120. package/lib/node/screen-share/screen-share-server-impl.d.ts +38 -0
  121. package/lib/node/screen-share/screen-share-server-impl.js +79 -0
  122. package/lib/node/screen-share/screen-shares.d.ts +124 -0
  123. package/lib/node/screen-share/screen-shares.js +464 -0
  124. package/lib/node/session-info-util.d.ts +10 -11
  125. package/lib/node/session-info-util.js +15 -93
  126. package/lib/node/settings-store.d.ts +1 -1
  127. package/lib/node/settings-store.js +6 -3
  128. package/lib/node/sign-in/access-store.d.ts +1 -1
  129. package/lib/node/sign-in/access-store.js +4 -1
  130. package/lib/node/sign-in/index.d.ts +2 -0
  131. package/lib/node/sign-in/index.js +2 -0
  132. package/lib/node/sign-in/polled-sign-in.d.ts +51 -0
  133. package/lib/node/sign-in/polled-sign-in.js +87 -0
  134. package/lib/node/sign-in/sign-in-devices.d.ts +56 -0
  135. package/lib/node/sign-in/sign-in-devices.js +117 -0
  136. package/lib/node/sign-in/sign-in-flow.d.ts +38 -4
  137. package/lib/node/sign-in/sign-in-flow.js +80 -5
  138. package/lib/node/sign-in/sign-in-pages.d.ts +8 -0
  139. package/lib/node/sign-in/sign-in-pages.js +136 -7
  140. package/lib/node/sign-in/sign-in-provider.d.ts +6 -0
  141. package/lib/node/sign-in/sign-in-provider.js +15 -0
  142. package/lib/node/sources-extension.d.ts +11 -0
  143. package/lib/node/sources-extension.js +22 -0
  144. package/lib/node/ta-server-impl.js +2 -2
  145. package/lib/node/ta-session-factory.js +2 -0
  146. package/lib/node/team/team-calls.js +35 -0
  147. package/lib/node/voice-bridge.d.ts +3 -1
  148. package/lib/node/voice-bridge.js +9 -2
  149. package/lib/node/voices-file.d.ts +5 -0
  150. package/lib/node/voices-file.js +14 -3
  151. package/lib/node/working-directory.d.ts +8 -2
  152. package/lib/node/working-directory.js +7 -0
  153. package/package.json +14 -10
@@ -0,0 +1,143 @@
1
+ /**
2
+ * DES of one block, as VNC Authentication needs it (RFB 3.8, 7.2.2): the challenge of 16 bytes encrypted with the
3
+ * password as the key, each byte of the key with its bits reversed. Written here because the OpenSSL of Node.js 17+
4
+ * keeps DES in its legacy provider, which is off; it encrypts 16 bytes once per sign-in, so speed does not matter.
5
+ */
6
+ export var RfbDes;
7
+ (function (RfbDes) {
8
+ const PC1 = [
9
+ 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46,
10
+ 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4,
11
+ ];
12
+ const PC2 = [
13
+ 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56,
14
+ 34, 53, 46, 42, 50, 36, 29, 32,
15
+ ];
16
+ const SHIFTS = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1];
17
+ const IP = [
18
+ 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9,
19
+ 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7,
20
+ ];
21
+ const FP = [
22
+ 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60,
23
+ 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25,
24
+ ];
25
+ const E = [
26
+ 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28,
27
+ 29, 28, 29, 30, 31, 32, 1,
28
+ ];
29
+ const P = [16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25];
30
+ const S = [
31
+ [
32
+ 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7,
33
+ 3, 10, 5, 0, 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
34
+ ],
35
+ [
36
+ 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6,
37
+ 9, 3, 2, 15, 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
38
+ ],
39
+ [
40
+ 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12,
41
+ 5, 10, 14, 7, 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
42
+ ],
43
+ [
44
+ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3,
45
+ 14, 5, 2, 8, 4, 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
46
+ ],
47
+ [
48
+ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5,
49
+ 6, 3, 0, 14, 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
50
+ ],
51
+ [
52
+ 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10,
53
+ 1, 13, 11, 6, 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
54
+ ],
55
+ [
56
+ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6,
57
+ 8, 0, 5, 9, 2, 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
58
+ ],
59
+ [
60
+ 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13,
61
+ 15, 3, 5, 8, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11,
62
+ ],
63
+ ];
64
+ /** The bits of a block, most significant first, as the tables of the standard number them from 1. */
65
+ function bits(bytes) {
66
+ const out = [];
67
+ for (const byte of bytes) {
68
+ for (let bit = 7; bit >= 0; bit--) {
69
+ out.push((byte >> bit) & 1);
70
+ }
71
+ }
72
+ return out;
73
+ }
74
+ function bytes(bits) {
75
+ const out = new Uint8Array(bits.length / 8);
76
+ bits.forEach((bit, index) => {
77
+ out[index >> 3] |= bit << (7 - (index & 7));
78
+ });
79
+ return out;
80
+ }
81
+ const permute = (input, table) => table.map(at => input[at - 1]);
82
+ function rotate(half, by) {
83
+ return [...half.slice(by), ...half.slice(0, by)];
84
+ }
85
+ /** The 16 keys of the rounds. */
86
+ function schedule(key) {
87
+ const permuted = permute(bits(key), PC1);
88
+ let left = permuted.slice(0, 28);
89
+ let right = permuted.slice(28);
90
+ return SHIFTS.map(shift => {
91
+ left = rotate(left, shift);
92
+ right = rotate(right, shift);
93
+ return permute([...left, ...right], PC2);
94
+ });
95
+ }
96
+ function feistel(right, key) {
97
+ const mixed = permute(right, E).map((bit, index) => bit ^ key[index]);
98
+ const out = [];
99
+ for (let box = 0; box < 8; box++) {
100
+ const six = mixed.slice(box * 6, box * 6 + 6);
101
+ const row = (six[0] << 1) | six[5];
102
+ const column = (six[1] << 3) | (six[2] << 2) | (six[3] << 1) | six[4];
103
+ const value = S[box][row * 16 + column];
104
+ out.push((value >> 3) & 1, (value >> 2) & 1, (value >> 1) & 1, value & 1);
105
+ }
106
+ return permute(out, P);
107
+ }
108
+ /** One block of 8 bytes encrypted with a key of 8 bytes (its parity bits ignored). */
109
+ function encryptBlock(key, block) {
110
+ const keys = schedule(key);
111
+ const permuted = permute(bits(block), IP);
112
+ let left = permuted.slice(0, 32);
113
+ let right = permuted.slice(32);
114
+ for (const round of keys) {
115
+ const next = feistel(right, round).map((bit, index) => bit ^ left[index]);
116
+ left = right;
117
+ right = next;
118
+ }
119
+ return bytes(permute([...right, ...left], FP));
120
+ }
121
+ RfbDes.encryptBlock = encryptBlock;
122
+ /** The answer to the challenge of VNC Authentication: the password, cut or padded to 8 bytes, with its bits reversed. */
123
+ function vncResponse(password, challenge) {
124
+ const key = new Uint8Array(8);
125
+ const text = new TextEncoder().encode(password);
126
+ for (let index = 0; index < 8 && index < text.length; index++) {
127
+ let byte = text[index];
128
+ let reversed = 0;
129
+ for (let bit = 0; bit < 8; bit++) {
130
+ reversed = (reversed << 1) | (byte & 1);
131
+ byte >>= 1;
132
+ }
133
+ key[index] = reversed;
134
+ }
135
+ const out = new Uint8Array(challenge.length);
136
+ for (let at = 0; at < challenge.length; at += 8) {
137
+ out.set(encryptBlock(key, challenge.subarray(at, at + 8)), at);
138
+ }
139
+ return out;
140
+ }
141
+ RfbDes.vncResponse = vncResponse;
142
+ })(RfbDes || (RfbDes = {}));
143
+ //# sourceMappingURL=rfb-des.js.map
@@ -0,0 +1,71 @@
1
+ import type { RfbFramebuffer } from './rfb-framebuffer.js';
2
+ import { type RfbCursor, type RfbRect, type RfbScreen } from './rfb-protocol.js';
3
+ import { RfbClosedError, type RfbReader } from './rfb-reader.js';
4
+ /**
5
+ * The zlib stream of ZRLE, one for the whole connection: each rectangle's bytes go on where the last ended. Inflated
6
+ * piece by piece as raw deflate, with the last 32 KB of what came out as the dictionary, so that nothing waits on a
7
+ * stream's events.
8
+ */
9
+ export declare class RfbInflater {
10
+ protected window: Uint8Array<ArrayBuffer>;
11
+ protected started: boolean;
12
+ /**
13
+ * Inflates the bytes of one rectangle, never more than `limit` (RfbZrle.maxBytes): a few KB of deflate can say
14
+ * gigabytes, and zlib stops at the limit instead of making them.
15
+ */
16
+ inflate(compressed: Uint8Array, limit: number): Uint8Array;
17
+ }
18
+ /** A ZRLE rectangle inflated to more than its size allows: a broken or hostile server, and the connection ends. */
19
+ export declare class RfbZrleTooLargeError extends RfbClosedError {
20
+ readonly limit: number;
21
+ constructor(limit: number);
22
+ }
23
+ /** Reads bytes already inflated, failing as a closed connection when they end early. */
24
+ export declare class RfbByteCursor {
25
+ readonly bytes: Uint8Array;
26
+ at: number;
27
+ constructor(bytes: Uint8Array);
28
+ u8(): number;
29
+ take(count: number): Uint8Array;
30
+ run(): number;
31
+ }
32
+ /** What the decoder of a rectangle tells the client it read. */
33
+ export type RfbRectResult = {
34
+ kind: 'pixels';
35
+ } | {
36
+ kind: 'size';
37
+ width: number;
38
+ height: number;
39
+ screens?: RfbScreen[];
40
+ } | {
41
+ kind: 'cursor';
42
+ cursor: RfbCursor | null;
43
+ } | {
44
+ kind: 'last';
45
+ };
46
+ /** The encodings of rectangles Ta asks for (docs/design/ta-desktop-remote.md 3.3). */
47
+ export declare class RfbDecoders {
48
+ protected readonly reader: RfbReader;
49
+ protected readonly framebuffer: RfbFramebuffer;
50
+ readonly inflater: RfbInflater;
51
+ constructor(reader: RfbReader, framebuffer: RfbFramebuffer);
52
+ read(rect: RfbRect, encoding: number): Promise<RfbRectResult>;
53
+ protected raw(rect: RfbRect): Promise<RfbRectResult>;
54
+ protected copyRect(rect: RfbRect): Promise<RfbRectResult>;
55
+ protected zrle(rect: RfbRect): Promise<RfbRectResult>;
56
+ protected cursor(rect: RfbRect): Promise<RfbRectResult>;
57
+ protected extendedSize(rect: RfbRect): Promise<RfbRectResult>;
58
+ }
59
+ /** ZRLE (RFC 6143 7.7.6): tiles of 64 by 64, each raw, one colour, a packed palette, or runs, with pixels of 3 bytes. */
60
+ export declare namespace RfbZrle {
61
+ /**
62
+ * The most bytes a rectangle may inflate to: each tile its kind byte, then at worst a run of one for every pixel (3
63
+ * bytes of colour and 1 of length: 4 a pixel), or the largest palette and a run of one for every pixel (381 bytes,
64
+ * then 2 a pixel); raw (3 a pixel) and packed palettes (at most 48 bytes and 1 a pixel) are less.
65
+ */
66
+ function maxBytes(rect: {
67
+ w: number;
68
+ h: number;
69
+ }): number;
70
+ function decode(cursor: RfbByteCursor, rect: RfbRect, framebuffer: RfbFramebuffer): void;
71
+ }
@@ -0,0 +1,288 @@
1
+ import { constants, inflateRawSync } from 'node:zlib';
2
+ import { Rfb } from './rfb-protocol.js';
3
+ import { RfbClosedError } from './rfb-reader.js';
4
+ /** The most a ZRLE rectangle may say it holds, compressed. */
5
+ const MAX_ZLIB = 64 * 1024 * 1024;
6
+ /** What deflate may refer back to. */
7
+ const WINDOW = 32 * 1024;
8
+ /**
9
+ * The zlib stream of ZRLE, one for the whole connection: each rectangle's bytes go on where the last ended. Inflated
10
+ * piece by piece as raw deflate, with the last 32 KB of what came out as the dictionary, so that nothing waits on a
11
+ * stream's events.
12
+ */
13
+ export class RfbInflater {
14
+ constructor() {
15
+ this.window = new Uint8Array(0);
16
+ this.started = false;
17
+ }
18
+ /**
19
+ * Inflates the bytes of one rectangle, never more than `limit` (RfbZrle.maxBytes): a few KB of deflate can say
20
+ * gigabytes, and zlib stops at the limit instead of making them.
21
+ */
22
+ inflate(compressed, limit) {
23
+ let input = compressed;
24
+ if (!this.started) {
25
+ // The header of zlib (two bytes) before the first deflate block.
26
+ input = input.subarray(2);
27
+ this.started = true;
28
+ }
29
+ let inflated;
30
+ try {
31
+ inflated = inflateRawSync(input, {
32
+ finishFlush: constants.Z_SYNC_FLUSH,
33
+ maxOutputLength: Math.max(1, limit),
34
+ ...(this.window.length ? { dictionary: this.window } : {}),
35
+ });
36
+ }
37
+ catch (error) {
38
+ if (error.code === 'ERR_BUFFER_TOO_LARGE') {
39
+ throw new RfbZrleTooLargeError(limit);
40
+ }
41
+ throw error;
42
+ }
43
+ const out = new Uint8Array(inflated);
44
+ if (out.length >= WINDOW) {
45
+ this.window = out.slice(out.length - WINDOW);
46
+ }
47
+ else {
48
+ const joined = new Uint8Array(Math.min(WINDOW, this.window.length + out.length));
49
+ const keep = joined.length - out.length;
50
+ joined.set(this.window.subarray(this.window.length - keep), 0);
51
+ joined.set(out, keep);
52
+ this.window = joined;
53
+ }
54
+ return out;
55
+ }
56
+ }
57
+ /** A ZRLE rectangle inflated to more than its size allows: a broken or hostile server, and the connection ends. */
58
+ export class RfbZrleTooLargeError extends RfbClosedError {
59
+ constructor(limit) {
60
+ super(`A ZRLE rectangle inflates to more than the ${limit} bytes its size allows.`);
61
+ this.limit = limit;
62
+ }
63
+ }
64
+ /** Reads bytes already inflated, failing as a closed connection when they end early. */
65
+ export class RfbByteCursor {
66
+ constructor(bytes) {
67
+ this.bytes = bytes;
68
+ this.at = 0;
69
+ }
70
+ u8() {
71
+ if (this.at >= this.bytes.length) {
72
+ throw new RfbClosedError('A ZRLE rectangle ended early.');
73
+ }
74
+ return this.bytes[this.at++];
75
+ }
76
+ take(count) {
77
+ if (this.at + count > this.bytes.length) {
78
+ throw new RfbClosedError('A ZRLE rectangle ended early.');
79
+ }
80
+ const out = this.bytes.subarray(this.at, this.at + count);
81
+ this.at += count;
82
+ return out;
83
+ }
84
+ run() {
85
+ let length = 1;
86
+ let byte;
87
+ do {
88
+ byte = this.u8();
89
+ length += byte;
90
+ } while (byte === 255);
91
+ return length;
92
+ }
93
+ }
94
+ /** The encodings of rectangles Ta asks for (docs/design/ta-desktop-remote.md 3.3). */
95
+ export class RfbDecoders {
96
+ constructor(reader, framebuffer) {
97
+ this.reader = reader;
98
+ this.framebuffer = framebuffer;
99
+ this.inflater = new RfbInflater();
100
+ }
101
+ async read(rect, encoding) {
102
+ switch (encoding) {
103
+ case Rfb.Encoding.RAW:
104
+ return this.raw(rect);
105
+ case Rfb.Encoding.COPY_RECT:
106
+ return this.copyRect(rect);
107
+ case Rfb.Encoding.ZRLE:
108
+ return this.zrle(rect);
109
+ case Rfb.Encoding.CURSOR:
110
+ return this.cursor(rect);
111
+ case Rfb.Encoding.DESKTOP_SIZE:
112
+ return { kind: 'size', width: rect.w, height: rect.h };
113
+ case Rfb.Encoding.EXTENDED_DESKTOP_SIZE:
114
+ return this.extendedSize(rect);
115
+ case Rfb.Encoding.LAST_RECT:
116
+ return { kind: 'last' };
117
+ default:
118
+ throw new RfbClosedError(`The desktop sent an encoding Ta did not ask for (${encoding}).`);
119
+ }
120
+ }
121
+ async raw(rect) {
122
+ const bytes = await this.reader.read(rect.w * rect.h * 4);
123
+ if (this.framebuffer.contains(rect)) {
124
+ this.framebuffer.putRaw(rect, bytes);
125
+ }
126
+ return { kind: 'pixels' };
127
+ }
128
+ async copyRect(rect) {
129
+ const fromX = await this.reader.u16();
130
+ const fromY = await this.reader.u16();
131
+ if (this.framebuffer.contains(rect) && this.framebuffer.contains({ x: fromX, y: fromY, w: rect.w, h: rect.h })) {
132
+ this.framebuffer.copy(rect, fromX, fromY);
133
+ }
134
+ return { kind: 'pixels' };
135
+ }
136
+ async zrle(rect) {
137
+ const length = await this.reader.u32();
138
+ if (length > MAX_ZLIB) {
139
+ throw new RfbClosedError(`A ZRLE rectangle of ${length} bytes is too big.`);
140
+ }
141
+ // Before inflating: the size of the rectangle is what bounds what it may inflate to.
142
+ if (!this.framebuffer.contains(rect)) {
143
+ throw new RfbClosedError(`A ZRLE rectangle of ${rect.w} by ${rect.h} at ${rect.x}, ${rect.y} lies outside the desktop.`);
144
+ }
145
+ const data = this.inflater.inflate(await this.reader.read(length), RfbZrle.maxBytes(rect));
146
+ RfbZrle.decode(new RfbByteCursor(data), rect, this.framebuffer);
147
+ return { kind: 'pixels' };
148
+ }
149
+ async cursor(rect) {
150
+ if (rect.w === 0 || rect.h === 0) {
151
+ return { kind: 'cursor', cursor: null };
152
+ }
153
+ if (rect.w > 256 || rect.h > 256) {
154
+ throw new RfbClosedError('The desktop sent a pointer too big to draw.');
155
+ }
156
+ const pixels = await this.reader.read(rect.w * rect.h * 4);
157
+ const rowMask = (rect.w + 7) >> 3;
158
+ const mask = await this.reader.read(rowMask * rect.h);
159
+ const rgba = new Uint8Array(rect.w * rect.h * 4);
160
+ for (let y = 0; y < rect.h; y++) {
161
+ for (let x = 0; x < rect.w; x++) {
162
+ const at = (y * rect.w + x) * 4;
163
+ rgba[at] = pixels[at];
164
+ rgba[at + 1] = pixels[at + 1];
165
+ rgba[at + 2] = pixels[at + 2];
166
+ rgba[at + 3] = mask[y * rowMask + (x >> 3)] & (0x80 >> (x & 7)) ? 255 : 0;
167
+ }
168
+ }
169
+ return { kind: 'cursor', cursor: { hotX: rect.x, hotY: rect.y, width: rect.w, height: rect.h, rgba } };
170
+ }
171
+ async extendedSize(rect) {
172
+ const count = await this.reader.u8();
173
+ await this.reader.read(3);
174
+ const screens = [];
175
+ for (let index = 0; index < count; index++) {
176
+ const id = await this.reader.u32();
177
+ const [x, y, width, height] = [await this.reader.u16(), await this.reader.u16(), await this.reader.u16(), await this.reader.u16()];
178
+ await this.reader.u32();
179
+ screens.push({ id, x, y, width, height });
180
+ }
181
+ // rect.y is the status of a change the client asked for: Ta asks for none, so every one is the server's.
182
+ return { kind: 'size', width: rect.w, height: rect.h, screens };
183
+ }
184
+ }
185
+ /** ZRLE (RFC 6143 7.7.6): tiles of 64 by 64, each raw, one colour, a packed palette, or runs, with pixels of 3 bytes. */
186
+ export var RfbZrle;
187
+ (function (RfbZrle) {
188
+ const TILE = 64;
189
+ /** The largest palette of a tile, 127 colours of 3 bytes. */
190
+ const MAX_PALETTE = 127 * 3;
191
+ /**
192
+ * The most bytes a rectangle may inflate to: each tile its kind byte, then at worst a run of one for every pixel (3
193
+ * bytes of colour and 1 of length: 4 a pixel), or the largest palette and a run of one for every pixel (381 bytes,
194
+ * then 2 a pixel); raw (3 a pixel) and packed palettes (at most 48 bytes and 1 a pixel) are less.
195
+ */
196
+ function maxBytes(rect) {
197
+ const tile = (width, height) => {
198
+ const pixels = width * height;
199
+ return pixels === 0 ? 0 : 1 + Math.max(4 * pixels, MAX_PALETTE + 2 * pixels);
200
+ };
201
+ const columns = Math.floor(rect.w / TILE);
202
+ const rows = Math.floor(rect.h / TILE);
203
+ const right = rect.w % TILE;
204
+ const bottom = rect.h % TILE;
205
+ return columns * rows * tile(TILE, TILE) + rows * tile(right, TILE) + columns * tile(TILE, bottom) + tile(right, bottom);
206
+ }
207
+ RfbZrle.maxBytes = maxBytes;
208
+ function decode(cursor, rect, framebuffer) {
209
+ const palette = new Uint8Array(128 * 3);
210
+ for (let top = 0; top < rect.h; top += TILE) {
211
+ for (let left = 0; left < rect.w; left += TILE) {
212
+ const tile = { x: rect.x + left, y: rect.y + top, w: Math.min(TILE, rect.w - left), h: Math.min(TILE, rect.h - top) };
213
+ tileOf(cursor, tile, framebuffer, palette);
214
+ }
215
+ }
216
+ }
217
+ RfbZrle.decode = decode;
218
+ function tileOf(cursor, tile, framebuffer, palette) {
219
+ const kind = cursor.u8();
220
+ const { rgba, width } = framebuffer;
221
+ const put = (index, r, g, b) => {
222
+ const at = ((tile.y + Math.floor(index / tile.w)) * width + tile.x + (index % tile.w)) * 4;
223
+ rgba[at] = r;
224
+ rgba[at + 1] = g;
225
+ rgba[at + 2] = b;
226
+ rgba[at + 3] = 255;
227
+ };
228
+ const count = tile.w * tile.h;
229
+ if (kind === 0) {
230
+ const bytes = cursor.take(count * 3);
231
+ for (let index = 0; index < count; index++) {
232
+ put(index, bytes[index * 3], bytes[index * 3 + 1], bytes[index * 3 + 2]);
233
+ }
234
+ return;
235
+ }
236
+ if (kind === 1) {
237
+ const [r, g, b] = cursor.take(3);
238
+ framebuffer.fill(tile, r, g, b);
239
+ return;
240
+ }
241
+ if (kind <= 16) {
242
+ palette.set(cursor.take(kind * 3));
243
+ const bits = kind === 2 ? 1 : kind <= 4 ? 2 : 4;
244
+ const mask = (1 << bits) - 1;
245
+ for (let row = 0; row < tile.h; row++) {
246
+ let byte = 0;
247
+ let left = 0;
248
+ for (let column = 0; column < tile.w; column++) {
249
+ if (left === 0) {
250
+ byte = cursor.u8();
251
+ left = 8;
252
+ }
253
+ left -= bits;
254
+ const index = ((byte >> left) & mask) * 3;
255
+ put(row * tile.w + column, palette[index], palette[index + 1], palette[index + 2]);
256
+ }
257
+ }
258
+ return;
259
+ }
260
+ if (kind === 128) {
261
+ let index = 0;
262
+ while (index < count) {
263
+ const [r, g, b] = cursor.take(3);
264
+ const end = Math.min(count, index + cursor.run());
265
+ for (; index < end; index++) {
266
+ put(index, r, g, b);
267
+ }
268
+ }
269
+ return;
270
+ }
271
+ if (kind >= 130) {
272
+ const size = kind - 128;
273
+ palette.set(cursor.take(size * 3));
274
+ let index = 0;
275
+ while (index < count) {
276
+ const byte = cursor.u8();
277
+ const colour = (byte & 127) * 3;
278
+ const end = byte & 128 ? Math.min(count, index + cursor.run()) : index + 1;
279
+ for (; index < end; index++) {
280
+ put(index, palette[colour], palette[colour + 1], palette[colour + 2]);
281
+ }
282
+ }
283
+ return;
284
+ }
285
+ throw new RfbClosedError(`A ZRLE tile of a kind Ta does not know (${kind}).`);
286
+ }
287
+ })(RfbZrle || (RfbZrle = {}));
288
+ //# sourceMappingURL=rfb-encodings.js.map
@@ -0,0 +1,16 @@
1
+ import type { RfbRect } from './rfb-protocol.js';
2
+ /** The pixels of a desktop, RGBA, one copy for every page that looks at it. */
3
+ export declare class RfbFramebuffer {
4
+ width: number;
5
+ height: number;
6
+ rgba: Uint8Array;
7
+ constructor(width: number, height: number);
8
+ resize(width: number, height: number): void;
9
+ /** Whether a rectangle a server named lies inside: one that does not is a broken server, and its bytes are read but not drawn. */
10
+ contains(rect: RfbRect): boolean;
11
+ /** Pixels of 4 bytes, R G B X, a row after another: the alpha set opaque. */
12
+ putRaw(rect: RfbRect, bytes: Uint8Array): void;
13
+ /** One colour, R G B in the low 24 bits of `rgb` as they are in memory (R lowest). */
14
+ fill(rect: RfbRect, r: number, g: number, b: number): void;
15
+ copy(rect: RfbRect, fromX: number, fromY: number): void;
16
+ }
@@ -0,0 +1,53 @@
1
+ /** The pixels of a desktop, RGBA, one copy for every page that looks at it. */
2
+ export class RfbFramebuffer {
3
+ constructor(width, height) {
4
+ this.width = width;
5
+ this.height = height;
6
+ this.rgba = new Uint8Array(width * height * 4);
7
+ }
8
+ resize(width, height) {
9
+ this.width = width;
10
+ this.height = height;
11
+ this.rgba = new Uint8Array(width * height * 4);
12
+ }
13
+ /** Whether a rectangle a server named lies inside: one that does not is a broken server, and its bytes are read but not drawn. */
14
+ contains(rect) {
15
+ return rect.x >= 0 && rect.y >= 0 && rect.w >= 0 && rect.h >= 0 && rect.x + rect.w <= this.width && rect.y + rect.h <= this.height;
16
+ }
17
+ /** Pixels of 4 bytes, R G B X, a row after another: the alpha set opaque. */
18
+ putRaw(rect, bytes) {
19
+ const { rgba, width } = this;
20
+ for (let row = 0; row < rect.h; row++) {
21
+ const to = ((rect.y + row) * width + rect.x) * 4;
22
+ rgba.set(bytes.subarray(row * rect.w * 4, (row + 1) * rect.w * 4), to);
23
+ for (let at = to + 3, end = to + rect.w * 4; at < end; at += 4) {
24
+ rgba[at] = 255;
25
+ }
26
+ }
27
+ }
28
+ /** One colour, R G B in the low 24 bits of `rgb` as they are in memory (R lowest). */
29
+ fill(rect, r, g, b) {
30
+ const { rgba, width } = this;
31
+ for (let row = 0; row < rect.h; row++) {
32
+ let at = ((rect.y + row) * width + rect.x) * 4;
33
+ for (let column = 0; column < rect.w; column++, at += 4) {
34
+ rgba[at] = r;
35
+ rgba[at + 1] = g;
36
+ rgba[at + 2] = b;
37
+ rgba[at + 3] = 255;
38
+ }
39
+ }
40
+ }
41
+ copy(rect, fromX, fromY) {
42
+ const { rgba, width } = this;
43
+ const rowBytes = rect.w * 4;
44
+ // Rows in the order that does not read a row already written over.
45
+ const down = fromY >= rect.y;
46
+ for (let index = 0; index < rect.h; index++) {
47
+ const row = down ? index : rect.h - 1 - index;
48
+ const from = ((fromY + row) * width + fromX) * 4;
49
+ rgba.copyWithin(((rect.y + row) * width + rect.x) * 4, from, from + rowBytes);
50
+ }
51
+ }
52
+ }
53
+ //# sourceMappingURL=rfb-framebuffer.js.map
@@ -0,0 +1,90 @@
1
+ /** What RFB (RFC 6143) calls things, as the client of Ta uses them (docs/design/ta-desktop-remote.md 3). */
2
+ export declare namespace Rfb {
3
+ const Security: {
4
+ readonly NONE: 1;
5
+ readonly VNC: 2;
6
+ readonly ARD: 30;
7
+ };
8
+ const Encoding: {
9
+ readonly RAW: 0;
10
+ readonly COPY_RECT: 1;
11
+ readonly ZRLE: 16;
12
+ readonly CURSOR: -239;
13
+ readonly DESKTOP_SIZE: -223;
14
+ readonly LAST_RECT: -224;
15
+ readonly EXTENDED_DESKTOP_SIZE: -308;
16
+ };
17
+ /** What Ta asks the server for, in order of preference. */
18
+ const ENCODINGS: readonly number[];
19
+ const ServerMessage: {
20
+ readonly FRAMEBUFFER_UPDATE: 0;
21
+ readonly COLOUR_MAP: 1;
22
+ readonly BELL: 2;
23
+ readonly CUT_TEXT: 3;
24
+ };
25
+ const ClientMessage: {
26
+ readonly SET_PIXEL_FORMAT: 0;
27
+ readonly SET_ENCODINGS: 2;
28
+ readonly UPDATE_REQUEST: 3;
29
+ readonly KEY: 4;
30
+ readonly POINTER: 5;
31
+ readonly CUT_TEXT: 6;
32
+ };
33
+ /**
34
+ * The pixels Ta asks for: 32 bits, little-endian, red in the lowest byte, so that a pixel in memory is R, G, B, X,
35
+ * the order of an RGBA buffer; ZRLE then sends three bytes a pixel (its CPIXEL), in the same order.
36
+ */
37
+ const PIXEL_FORMAT: readonly number[];
38
+ /** The biggest framebuffer Ta takes: 8K by 8K, whatever a server says. */
39
+ const MAX_SIDE = 8192;
40
+ /** The longest text of the clipboard or of a reason Ta reads. */
41
+ const MAX_TEXT: number;
42
+ }
43
+ /** One screen of a framebuffer (ExtendedDesktopSize); a framebuffer without them is one screen. */
44
+ export interface RfbScreen {
45
+ id: number;
46
+ x: number;
47
+ y: number;
48
+ width: number;
49
+ height: number;
50
+ }
51
+ export interface RfbRect {
52
+ x: number;
53
+ y: number;
54
+ w: number;
55
+ h: number;
56
+ }
57
+ /** The pointer's picture (Cursor pseudo-encoding), RGBA with the server's mask as alpha; drawn by the page. */
58
+ export interface RfbCursor {
59
+ hotX: number;
60
+ hotY: number;
61
+ width: number;
62
+ height: number;
63
+ rgba: Uint8Array;
64
+ }
65
+ /** What a client asks of whoever opened it while it signs in. */
66
+ export interface RfbCredentials {
67
+ username?: string;
68
+ password: string;
69
+ }
70
+ export interface RfbClientOptions {
71
+ /** Asked when the server wants a password (VNC Authentication) or an account (Apple Remote Desktop). */
72
+ credentials(need: {
73
+ username: boolean;
74
+ }): Promise<RfbCredentials>;
75
+ /** Shared with other clients of the server: true, so that Ta never ends the session of someone else. */
76
+ shared?: boolean;
77
+ }
78
+ /** What a client tells as the desktop changes. */
79
+ export interface RfbClientEvents {
80
+ /** The framebuffer has a new size (and maybe new screens); all of it follows as damage. */
81
+ size(width: number, height: number, screens: RfbScreen[]): void;
82
+ /** These pixels changed. */
83
+ damage(rect: RfbRect): void;
84
+ /** An update ended: what changed since the last is all there. */
85
+ update(): void;
86
+ cursor(cursor: RfbCursor | null): void;
87
+ clipboard(text: string): void;
88
+ bell(): void;
89
+ closed(error?: Error): void;
90
+ }