From 7d1357162e3300fe7ff441ea9ad45310c2cc1759 Mon Sep 17 00:00:00 2001 From: Mihai Parparita Date: Thu, 25 Aug 2022 16:17:50 -0700 Subject: [PATCH] cmd/tsconnect: expose runSSHSession in npm package Move it to lib/ so that it can be used in both the app and the package. Updates #5415 Signed-off-by: Mihai Parparita --- cmd/tsconnect/src/app/ssh.tsx | 53 +---------------------------------- cmd/tsconnect/src/lib/ssh.ts | 52 ++++++++++++++++++++++++++++++++++ cmd/tsconnect/src/pkg/pkg.ts | 2 ++ 3 files changed, 55 insertions(+), 52 deletions(-) create mode 100644 cmd/tsconnect/src/lib/ssh.ts diff --git a/cmd/tsconnect/src/app/ssh.tsx b/cmd/tsconnect/src/app/ssh.tsx index 91ebee04f..36c85dddc 100644 --- a/cmd/tsconnect/src/app/ssh.tsx +++ b/cmd/tsconnect/src/app/ssh.tsx @@ -3,13 +3,7 @@ // license that can be found in the LICENSE file. import { useState, useCallback } from "preact/hooks" -import { Terminal } from "xterm" -import { FitAddon } from "xterm-addon-fit" - -type SSHSessionDef = { - username: string - hostname: string -} +import { runSSHSession, SSHSessionDef } from "../lib/ssh" export function SSH({ netMap, ipn }: { netMap: IPNNetMap; ipn: IPN }) { const [sshSessionDef, setSSHSessionDef] = useState(null) @@ -55,51 +49,6 @@ function SSHSession({ ) } -function runSSHSession( - termContainerNode: HTMLDivElement, - def: SSHSessionDef, - ipn: IPN, - onDone: () => void -) { - const term = new Terminal({ - cursorBlink: true, - }) - const fitAddon = new FitAddon() - term.loadAddon(fitAddon) - term.open(termContainerNode) - fitAddon.fit() - - let onDataHook: ((data: string) => void) | undefined - term.onData((e) => { - onDataHook?.(e) - }) - - term.focus() - - const sshSession = ipn.ssh(def.hostname, def.username, { - writeFn: (input) => term.write(input), - setReadFn: (hook) => (onDataHook = hook), - rows: term.rows, - cols: term.cols, - onDone: () => { - resizeObserver.disconnect() - term.dispose() - window.removeEventListener("beforeunload", handleBeforeUnload) - onDone() - }, - }) - - // Make terminal and SSH session track the size of the containing DOM node. - const resizeObserver = new ResizeObserver(() => fitAddon.fit()) - resizeObserver.observe(termContainerNode) - term.onResize(({ rows, cols }) => sshSession.resize(rows, cols)) - - // Close the session if the user closes the window without an explicit - // exit. - const handleBeforeUnload = () => sshSession.close() - window.addEventListener("beforeunload", handleBeforeUnload) -} - function NoSSHPeers() { return (
diff --git a/cmd/tsconnect/src/lib/ssh.ts b/cmd/tsconnect/src/lib/ssh.ts new file mode 100644 index 000000000..424687909 --- /dev/null +++ b/cmd/tsconnect/src/lib/ssh.ts @@ -0,0 +1,52 @@ +import { Terminal } from "xterm" +import { FitAddon } from "xterm-addon-fit" + +export type SSHSessionDef = { + username: string + hostname: string +} + +export function runSSHSession( + termContainerNode: HTMLDivElement, + def: SSHSessionDef, + ipn: IPN, + onDone: () => void +) { + const term = new Terminal({ + cursorBlink: true, + }) + const fitAddon = new FitAddon() + term.loadAddon(fitAddon) + term.open(termContainerNode) + fitAddon.fit() + + let onDataHook: ((data: string) => void) | undefined + term.onData((e) => { + onDataHook?.(e) + }) + + term.focus() + + const sshSession = ipn.ssh(def.hostname, def.username, { + writeFn: (input) => term.write(input), + setReadFn: (hook) => (onDataHook = hook), + rows: term.rows, + cols: term.cols, + onDone: () => { + resizeObserver.disconnect() + term.dispose() + window.removeEventListener("beforeunload", handleBeforeUnload) + onDone() + }, + }) + + // Make terminal and SSH session track the size of the containing DOM node. + const resizeObserver = new ResizeObserver(() => fitAddon.fit()) + resizeObserver.observe(termContainerNode) + term.onResize(({ rows, cols }) => sshSession.resize(rows, cols)) + + // Close the session if the user closes the window without an explicit + // exit. + const handleBeforeUnload = () => sshSession.close() + window.addEventListener("beforeunload", handleBeforeUnload) +} diff --git a/cmd/tsconnect/src/pkg/pkg.ts b/cmd/tsconnect/src/pkg/pkg.ts index 2a3806a1e..6c62da1ac 100644 --- a/cmd/tsconnect/src/pkg/pkg.ts +++ b/cmd/tsconnect/src/pkg/pkg.ts @@ -37,3 +37,5 @@ export async function createIPN(config: IPNPackageConfig): Promise { return newIPN(config) } + +export { runSSHSession } from "../lib/ssh"