From d356cb22606ae87e771602d555d2ab819e9d2e4e Mon Sep 17 00:00:00 2001 From: butlerx Date: Sun, 27 Mar 2022 11:43:34 +0100 Subject: [PATCH] Release 2.4.0 Fix compilation errors from dep upgrades --- package.json | 2 +- src/client/wetty/mobile.ts | 2 +- src/client/wetty/socket.ts | 3 +- src/client/wetty/term.ts | 13 +++++++- src/client/wetty/term/confiruragtion.ts | 31 +++++++++++-------- .../wetty/term/confiruragtion/clipboard.ts | 4 +-- .../wetty/term/confiruragtion/editor.ts | 12 +++---- src/client/wetty/term/confiruragtion/load.ts | 19 ++++++++++-- .../term/confiruragtion/shared/options.ts | 11 +++++++ src/server.ts | 2 +- tsconfig.browser.json | 7 +++-- 11 files changed, 73 insertions(+), 33 deletions(-) create mode 100644 src/client/wetty/term/confiruragtion/shared/options.ts diff --git a/package.json b/package.json index 6f6de0dc5..8f9b4cc15 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wetty", - "version": "2.3.0", + "version": "2.4.0", "description": "WeTTY = Web + TTY. Terminal access in browser over http/https", "homepage": "https://github.com/butlerx/wetty", "license": "MIT", diff --git a/src/client/wetty/mobile.ts b/src/client/wetty/mobile.ts index 014c79183..936f70c3d 100644 --- a/src/client/wetty/mobile.ts +++ b/src/client/wetty/mobile.ts @@ -1,7 +1,7 @@ import _ from 'lodash'; export function mobileKeyboard(): void { - const [screen] = document.getElementsByClassName('xterm-screen'); + const [screen] = Array.from(document.getElementsByClassName('xterm-screen')); if (_.isNull(screen)) return; screen.setAttribute('contenteditable', 'true'); screen.setAttribute('spellcheck', 'false'); diff --git a/src/client/wetty/socket.ts b/src/client/wetty/socket.ts index 4195c8d04..507d75917 100644 --- a/src/client/wetty/socket.ts +++ b/src/client/wetty/socket.ts @@ -1,9 +1,8 @@ import io from 'socket.io-client'; -const userRegex = new RegExp('ssh/[^/]+$'); export const trim = (str: string): string => str.replace(/\/*$/, ''); -const socketBase = trim(window.location.pathname).replace(userRegex, ''); +const socketBase = trim(window.location.pathname).replace('/ssh/[^/]+$/', ''); export const socket = io(window.location.origin, { path: `${trim(socketBase)}/socket.io`, }); diff --git a/src/client/wetty/term.ts b/src/client/wetty/term.ts index beb5b6de3..b04fe9178 100644 --- a/src/client/wetty/term.ts +++ b/src/client/wetty/term.ts @@ -4,6 +4,7 @@ import _ from 'lodash'; import { Terminal } from 'xterm'; import { FitAddon } from 'xterm-addon-fit'; import { WebLinksAddon } from 'xterm-addon-web-links'; +import type { Options } from './term/confiruragtion/shared/options'; import { configureTerm, shouldFitTerm } from './term/confiruragtion.js'; import { terminal as termElement } from '../shared/elements.js'; @@ -25,6 +26,16 @@ export class Term extends Terminal { } } +declare global { + interface Window { + wetty_term?: Term; + wetty_close_config?: () => void; + wetty_save_config?: (newConfig: Options) => void; + clipboardData: DataTransfer; + loadOptions: (conf: Options) => void; + } +} + export function terminal(socket: Socket): Term | undefined { const term = new Term(socket) as Term; if (_.isNull(termElement)) return undefined; @@ -32,6 +43,6 @@ export function terminal(socket: Socket): Term | undefined { term.open(termElement); configureTerm(term); window.onresize = term.resizeTerm; - (window as any).wetty_term = term; + window.wetty_term = term; return term; } diff --git a/src/client/wetty/term/confiruragtion.ts b/src/client/wetty/term/confiruragtion.ts index d9930e4b6..e01b64166 100644 --- a/src/client/wetty/term/confiruragtion.ts +++ b/src/client/wetty/term/confiruragtion.ts @@ -1,13 +1,12 @@ import type { Term } from '../term'; +import type { Options } from './confiruragtion/shared/options'; import { copySelected, copyShortcut } from './confiruragtion/clipboard'; import { onInput, setOptions } from './confiruragtion/editor'; import { editor } from '../../shared/elements'; import { loadOptions } from './confiruragtion/load'; export function configureTerm(term: Term): void { - let options = loadOptions(); - // Convert old options to new options - if (!('xterm' in options)) options = { xterm: options }; + const options = loadOptions(); try { setOptions(term, options); } catch { @@ -16,27 +15,33 @@ export function configureTerm(term: Term): void { const toggle = document.querySelector('#options .toggler'); const optionsElem = document.getElementById('options'); - if (editor == null || toggle == null || optionsElem == null) + if (editor == null || toggle == null || optionsElem == null) { throw new Error("Couldn't initialize configuration menu"); + } function editorOnLoad() { - (editor.contentWindow as any).loadOptions(loadOptions()); - (editor.contentWindow as any).wetty_close_config = () => { - optionsElem!.classList.toggle('opened'); + editor?.contentWindow?.loadOptions(loadOptions()); + editor.contentWindow!.wetty_close_config = () => { + optionsElem?.classList.toggle('opened'); }; - (editor.contentWindow as any).wetty_save_config = (newConfig: any) => { + editor.contentWindow!.wetty_save_config = (newConfig: Options) => { onInput(term, newConfig); }; } if ( - (editor.contentDocument || editor.contentWindow!.document).readyState === - 'complete' - ) + ( + editor.contentDocument || + (editor.contentWindow?.document ?? { + readyState: '', + }) + ).readyState === 'complete' + ) { editorOnLoad(); + } editor.addEventListener('load', editorOnLoad); toggle.addEventListener('click', e => { - (editor.contentWindow as any).loadOptions(loadOptions()); + editor?.contentWindow?.loadOptions(loadOptions()); optionsElem.classList.toggle('opened'); e.preventDefault(); }); @@ -53,5 +58,5 @@ export function configureTerm(term: Term): void { } export function shouldFitTerm(): boolean { - return (loadOptions() as any).wettyFitTerminal ?? true; + return loadOptions().wettyFitTerminal ?? true; } diff --git a/src/client/wetty/term/confiruragtion/clipboard.ts b/src/client/wetty/term/confiruragtion/clipboard.ts index a87d0b3a6..33cde21f0 100644 --- a/src/client/wetty/term/confiruragtion/clipboard.ts +++ b/src/client/wetty/term/confiruragtion/clipboard.ts @@ -4,8 +4,8 @@ @returns boolean to indicate success or failure */ export function copySelected(text: string): boolean { - if ((window as any).clipboardData?.setData) { - (window as any).clipboardData.setData('Text', text); + if (window.clipboardData?.setData) { + window.clipboardData.setData('Text', text); return true; } if ( diff --git a/src/client/wetty/term/confiruragtion/editor.ts b/src/client/wetty/term/confiruragtion/editor.ts index 5da764699..ad189a43d 100644 --- a/src/client/wetty/term/confiruragtion/editor.ts +++ b/src/client/wetty/term/confiruragtion/editor.ts @@ -1,7 +1,8 @@ import type { Term } from '../../term'; import { editor } from '../../../shared/elements'; +import type { Options } from './shared/options'; -export const onInput = (term: Term, updated: any) => { +export const onInput = (term: Term, updated: Options) => { try { const updatedConf = JSON.stringify(updated, null, 2); if (localStorage.options === updatedConf) return; @@ -16,16 +17,15 @@ export const onInput = (term: Term, updated: any) => { editor.classList.remove('error'); localStorage.options = updatedConf; } catch (e) { - console.error('Configuration Error'); - console.error(e); + console.error('Configuration Error', e); editor.classList.add('error'); } }; -export function setOptions(term: Term, options: any) { - Object.keys(options.xterm).forEach(key => { +export function setOptions(term: Term, { xterm }: Options) { + Object.keys(xterm).forEach(key => { if (key === 'cols' || key === 'rows') return; - const value = options.xterm[key]; + const value = xterm[key]; term.setOption(key, value); }); } diff --git a/src/client/wetty/term/confiruragtion/load.ts b/src/client/wetty/term/confiruragtion/load.ts index b72b6c712..5119ac946 100644 --- a/src/client/wetty/term/confiruragtion/load.ts +++ b/src/client/wetty/term/confiruragtion/load.ts @@ -1,11 +1,24 @@ import _ from 'lodash'; +import type { XTerm, Options } from './shared/options'; -export function loadOptions(): Record { - const defaultOptions = { xterm: { fontSize: 14 } }; +export const defaultOptions: Options = { + xterm: { fontSize: 14 }, + wettyVoid: 0, + wettyFitTerminal: true, +}; + +export function loadOptions(): Options { try { - return _.isUndefined(localStorage.options) + let options = _.isUndefined(localStorage.options) ? defaultOptions : JSON.parse(localStorage.options); + // Convert old options to new options + if (!('xterm' in options)) { + const xterm = options; + options = defaultOptions; + options.xterm = xterm as unknown as XTerm; + } + return options; } catch { return defaultOptions; } diff --git a/src/client/wetty/term/confiruragtion/shared/options.ts b/src/client/wetty/term/confiruragtion/shared/options.ts new file mode 100644 index 000000000..aaa54ed4e --- /dev/null +++ b/src/client/wetty/term/confiruragtion/shared/options.ts @@ -0,0 +1,11 @@ +export type XTerm = { + cols?: number; + rows?: number; + fontSize: number; +} & Record; + +export interface Options { + xterm: XTerm; + wettyFitTerminal: boolean; + wettyVoid: number; +} diff --git a/src/server.ts b/src/server.ts index b1cfe339e..733bd7627 100644 --- a/src/server.ts +++ b/src/server.ts @@ -33,7 +33,7 @@ export async function start( serverConf: Server = serverDefault, command: string = defaultCommand, forcessh: boolean = forceSSHDefault, - ssl?: SSL, + ssl: SSL | undefined = undefined, ): Promise { const logger = getLogger(); if (ssh.key) { diff --git a/tsconfig.browser.json b/tsconfig.browser.json index 034dcb6ac..ef1d68bf2 100644 --- a/tsconfig.browser.json +++ b/tsconfig.browser.json @@ -1,6 +1,7 @@ { "extends": "./tsconfig.json", - "include": [ - "src/client" - ] + "compilerOptions": { + "lib": ["DOM"] + }, + "include": ["src/client"] }