termigan 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +304 -2
- package/assets/logo.svg +97 -0
- package/dist/termigan.wasm +0 -0
- package/docs/api.md +295 -0
- package/docs/architecture.md +102 -0
- package/package.json +62 -5
- package/types/index.d.ts +123 -0
- package/web/canvas-renderer.js +427 -0
- package/web/input-controller.js +181 -0
- package/web/pty-socket.js +125 -0
- package/web/termigan.js +870 -0
- package/web/viewport-controller.js +184 -0
- package/web/wasm-bridge.js +264 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
function defaultDecodeMessage(data) {
|
|
2
|
+
if (typeof data === "string") {
|
|
3
|
+
try {
|
|
4
|
+
const msg = JSON.parse(data);
|
|
5
|
+
if (
|
|
6
|
+
msg &&
|
|
7
|
+
typeof msg === "object" &&
|
|
8
|
+
msg.type === "size" &&
|
|
9
|
+
Number.isFinite(msg.cols) &&
|
|
10
|
+
Number.isFinite(msg.rows) &&
|
|
11
|
+
msg.cols > 0 &&
|
|
12
|
+
msg.rows > 0
|
|
13
|
+
) {
|
|
14
|
+
return { type: "size", cols: msg.cols, rows: msg.rows };
|
|
15
|
+
}
|
|
16
|
+
} catch {}
|
|
17
|
+
return { type: "output", bytes: data };
|
|
18
|
+
}
|
|
19
|
+
if (data instanceof ArrayBuffer) {
|
|
20
|
+
return { type: "output", bytes: new Uint8Array(data) };
|
|
21
|
+
}
|
|
22
|
+
if (ArrayBuffer.isView(data)) {
|
|
23
|
+
return {
|
|
24
|
+
type: "output",
|
|
25
|
+
bytes: new Uint8Array(data.buffer, data.byteOffset, data.byteLength),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function defaultEncodeResize(cols, rows) {
|
|
32
|
+
return JSON.stringify({ type: "resize", cols, rows });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function isSocketOpen(socket) {
|
|
36
|
+
const openState = typeof socket.OPEN === "number" ? socket.OPEN : 1;
|
|
37
|
+
return socket.readyState === openState;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function attachPtySocket(viewer, socket, options = {}) {
|
|
41
|
+
const bidirectional = options.bidirectional !== false;
|
|
42
|
+
const decodeMessage =
|
|
43
|
+
typeof options.decodeMessage === "function" ? options.decodeMessage : defaultDecodeMessage;
|
|
44
|
+
const encodeResize =
|
|
45
|
+
options.encodeResize === null
|
|
46
|
+
? null
|
|
47
|
+
: typeof options.encodeResize === "function"
|
|
48
|
+
? options.encodeResize
|
|
49
|
+
: defaultEncodeResize;
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
if ("binaryType" in socket) {
|
|
53
|
+
socket.binaryType = "arraybuffer";
|
|
54
|
+
}
|
|
55
|
+
} catch {}
|
|
56
|
+
|
|
57
|
+
let detached = false;
|
|
58
|
+
|
|
59
|
+
function dispatchDecoded(decoded) {
|
|
60
|
+
if (detached || !decoded) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (decoded.type === "size") {
|
|
64
|
+
viewer.setRemoteSize(decoded.cols, decoded.rows);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (decoded.type === "output" && decoded.bytes !== undefined && decoded.bytes !== null) {
|
|
68
|
+
viewer.write(decoded.bytes);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function onMessage(event) {
|
|
73
|
+
if (detached) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const data = event.data;
|
|
77
|
+
if (data && typeof data === "object" && typeof data.arrayBuffer === "function") {
|
|
78
|
+
data.arrayBuffer().then((buf) => {
|
|
79
|
+
if (detached) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
dispatchDecoded(decodeMessage(buf));
|
|
83
|
+
}).catch(() => {});
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
dispatchDecoded(decodeMessage(data));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
socket.addEventListener("message", onMessage);
|
|
90
|
+
|
|
91
|
+
const stopInput = bidirectional
|
|
92
|
+
? viewer.onInput((bytes) => {
|
|
93
|
+
if (!detached && isSocketOpen(socket)) {
|
|
94
|
+
socket.send(bytes);
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
: null;
|
|
98
|
+
|
|
99
|
+
const stopResize =
|
|
100
|
+
bidirectional && encodeResize
|
|
101
|
+
? viewer.onResize((cols, rows) => {
|
|
102
|
+
if (detached || !isSocketOpen(socket)) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const frame = encodeResize(cols, rows);
|
|
106
|
+
if (frame !== null && frame !== undefined) {
|
|
107
|
+
socket.send(frame);
|
|
108
|
+
}
|
|
109
|
+
})
|
|
110
|
+
: null;
|
|
111
|
+
|
|
112
|
+
return function detach() {
|
|
113
|
+
if (detached) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
detached = true;
|
|
117
|
+
socket.removeEventListener("message", onMessage);
|
|
118
|
+
if (stopInput) {
|
|
119
|
+
stopInput();
|
|
120
|
+
}
|
|
121
|
+
if (stopResize) {
|
|
122
|
+
stopResize();
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
}
|