Skip to content

Commit

Permalink
Add export for decorating express server with wetty conf
Browse files Browse the repository at this point in the history
  • Loading branch information
butlerx committed Jun 10, 2023
1 parent c244f87 commit 6ecfe98
Show file tree
Hide file tree
Showing 28 changed files with 181 additions and 153 deletions.
95 changes: 95 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
module.exports = {
extends: ['airbnb-base', 'prettier', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'prettier'],
ignorePatterns: ['dist'],
root: true,
env: {
node: true,
browser: true,
},
rules: {
'@typescript-eslint/no-unused-vars': [
'error',
{ varsIgnorePattern: '^_', argsIgnorePattern: '^_' },
],
'@typescript-eslint/no-use-before-define': ['error', { functions: false }],
'func-style': ['error', 'declaration', { allowArrowFunctions: true }],
'import/extensions': [
'error',
'always',
{
ts: 'never',
js: 'ignorePackages',
mjs: 'ignorePackages',
jsx: 'never',
tsx: 'never',
},
],
'import/no-extraneous-dependencies': [
'error',
{ devDependencies: ['**/*.test.*', '**/*.spec.*'] },
],
'import/order': [
'error',
{
groups: [
'builtin',
'internal',
'external',
'parent',
'sibling',
'index',
'object',
'type',
],
pathGroups: [{ pattern: '@ev/**', group: 'internal' }],
distinctGroup: true,
alphabetize: { order: 'asc', caseInsensitive: false },
},
],
'import/prefer-default-export': 'off',
'import/prefer-default-export': 'off',
'linebreak-style': ['error', 'unix'],
'lines-between-class-members': [
'error',
'always',
{ exceptAfterSingleLine: true },
],
'no-param-reassign': ['error', { props: false }],
'no-use-before-define': ['error', { functions: false }],
},
settings: {
// Apply special parsing for TypeScript files
'import/parsers': { '@typescript-eslint/parser': ['.ts', '.tsx', '.d.ts'] },
'import/resolver': {
typescript: {
project: ['./tsconfig.browser.json', './tsconfig.node.json'],
},
node: { extensions: ['.mjs', '.js', '.json', '.ts', '.d.ts'] },
},
'import/extensions': ['.js', '.mjs', '.jsx', '.ts', '.tsx', '.d.ts'],
// Resolve type definition packages
'import/external-module-folders': ['node_modules', 'node_modules/@types'],
},
overrides: [
{ files: ['*.ts', '*.tsx'], rules: { 'import/no-unresolved': 'off' } },
{
files: ['*.js', '*.jsx'],
rules: {
'@typescript-eslint/no-var-requires': 'off',
'import/no-unresolved': 'off',
},
},
{
files: ['*.spec.*', '*.test.*'],
extends: ['plugin:mocha/recommended'],
plugins: ['mocha'],
rules: {
'import/no-extraneous-dependencies': ['off'],
'mocha/no-mocha-arrows': ['off'],
'no-unused-expressions': ['off'],
},
},
],
};
82 changes: 0 additions & 82 deletions .eslintrc.json

This file was deleted.

13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"dev": "NODE_ENV=development concurrently --kill-others --success first \"snowpack build --watch\" \"nodemon . --conf conf/config.json5\"",
"docker-compose-entrypoint": "ssh-keyscan -H wetty-ssh >> ~/.ssh/known_hosts; yarn start",
"lint": "eslint src",
"lint:fix": "eslint --fix src",
"start": "NODE_ENV=production node .",
"test": "mocha"
},
Expand Down Expand Up @@ -143,17 +144,17 @@
"@types/sinon": "^10.0.13",
"@types/toastify-js": "^1.9.2",
"@types/yargs": "^15.0.5",
"@typescript-eslint/eslint-plugin": "^5.32.0",
"@typescript-eslint/parser": "^5.32.0",
"all-contributors-cli": "^6.17.2",
"chai": "^4.3.6",
"concurrently": "^5.2.0",
"eslint": "^8.21.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-import-resolver-typescript": "^3.4.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-mocha": "^10.1.0",
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
"eslint": "^8.36.0",
"eslint-config-airbnb-base": "latest",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-prettier": "^4.2.1",
"git-authors-cli": "^1.0.42",
"husky": "^4.2.5",
Expand Down
10 changes: 5 additions & 5 deletions src/client/wetty.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import _ from 'lodash';
import { dom, library } from '@fortawesome/fontawesome-svg-core';
import { faCogs } from '@fortawesome/free-solid-svg-icons';
import _ from 'lodash';

import { FileDownloader } from './wetty/download';
import { disconnect } from './wetty/disconnect';
import { mobileKeyboard } from './wetty/mobile';
import { overlay } from './wetty/disconnect/elements';
import { socket } from './wetty/socket';
import { verifyPrompt } from './wetty/disconnect/verify';
import { terminal, Term } from './wetty/term';
import { FileDownloader } from './wetty/download';
import { FlowControlClient } from './wetty/flowcontrol';
import { mobileKeyboard } from './wetty/mobile';
import { socket } from './wetty/socket';
import { terminal, Term } from './wetty/term';

// Setup for fontawesome
library.add(faCogs);
Expand Down
2 changes: 1 addition & 1 deletion src/client/wetty/disconnect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';
import { verifyPrompt } from './disconnect/verify';
import { overlay } from './disconnect/elements';
import { verifyPrompt } from './disconnect/verify';

export function disconnect(reason: string): void {
if (_.isNull(overlay)) return;
Expand Down
2 changes: 1 addition & 1 deletion src/client/wetty/download.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { expect } from 'chai';
import 'mocha';
import { JSDOM } from 'jsdom';
import * as sinon from 'sinon';

import { JSDOM } from 'jsdom';
import { FileDownloader } from './download';

const noop = (): void => {}; // eslint-disable-line @typescript-eslint/no-empty-function
Expand Down
2 changes: 1 addition & 1 deletion src/client/wetty/download.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Toastify from 'toastify-js';
import fileType from 'file-type';
import Toastify from 'toastify-js';

const DEFAULT_FILE_BEGIN = '\u001b[5i';
const DEFAULT_FILE_END = '\u001b[4i';
Expand Down
10 changes: 5 additions & 5 deletions src/client/wetty/term.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { Socket } from 'socket.io-client';
import _ from 'lodash';
import { Terminal } from 'xterm';
import { FitAddon } from 'xterm-addon-fit';
import { WebLinksAddon } from 'xterm-addon-web-links';
import { ImageAddon } from 'xterm-addon-image';
import { WebLinksAddon } from 'xterm-addon-web-links';

import type { Options } from './term/options';
import { loadOptions } from './term/load';
import { configureTerm } from './term/confiruragtion';
import { terminal as termElement } from './disconnect/elements';
import { configureTerm } from './term/confiruragtion';
import { loadOptions } from './term/load';
import type { Options } from './term/options';
import type { Socket } from 'socket.io-client';

export class Term extends Terminal {
socket: Socket;
Expand Down
6 changes: 3 additions & 3 deletions src/client/wetty/term/confiruragtion.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Term } from '../term';
import type { Options } from './options';
import { editor } from '../disconnect/elements';
import { copySelected, copyShortcut } from './confiruragtion/clipboard';
import { onInput } from './confiruragtion/editor';
import { editor } from '../disconnect/elements';
import { loadOptions } from './load';
import type { Options } from './options';
import type { Term } from '../term';

export function configureTerm(term: Term): void {
const options = loadOptions();
Expand Down
2 changes: 1 addition & 1 deletion src/client/wetty/term/confiruragtion/editor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Term } from '../../term';
import { editor } from '../../disconnect/elements';
import type { Term } from '../../term';
import type { Options } from '../options';

export const onInput = (term: Term, updated: Options) => {
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
*
* This is the cli Interface for wetty.
*/
import yargs from 'yargs';
import { createRequire } from 'module';
import { setLevel, logger } from './shared/logger.js';
import yargs from 'yargs';
import { start } from './server.js';
import { loadConfigFile, mergeCliConf } from './shared/config.js';
import { setLevel, logger } from './shared/logger.js';

/* eslint-disable @typescript-eslint/no-var-requires */
const require = createRequire(import.meta.url);
Expand Down
32 changes: 23 additions & 9 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@
* Create WeTTY server
* @module WeTTy
*/
import type SocketIO from 'socket.io';
import { Gauge, collectDefaultMetrics } from 'prom-client';
import express from 'express';
import gc from 'gc-stats';

import type { SSH, SSL, Server } from './shared/interfaces.js';
import { Gauge, collectDefaultMetrics } from 'prom-client';
import { getCommand } from './server/command.js';
import { logger as getLogger } from './shared/logger.js';
import { login } from './server/login.js';
import { server } from './server/socketServer.js';
import { gcMetrics } from './server/metrics.js';
import { escapeShell } from './server/shared/shell.js';
import { server } from './server/socketServer.js';
import { spawn } from './server/spawn.js';
import {
sshDefault,
serverDefault,
forceSSHDefault,
defaultCommand,
} from './shared/defaults.js';
import { escapeShell } from './server/shared/shell.js';
import { logger as getLogger } from './shared/logger.js';
import type { SSH, SSL, Server } from './shared/interfaces.js';
import type { Application } from 'express';
import type SocketIO from 'socket.io';

export * from './shared/interfaces.js';
export { logger as getLogger } from './shared/logger.js';

const wettyConnections = new Gauge({
name: 'wetty_connections',
Expand All @@ -31,7 +35,17 @@ const wettyConnections = new Gauge({
* @name startServer
* @returns Promise that resolves SocketIO server
*/
export async function start(
export const start = (
ssh: SSH = sshDefault,
serverConf: Server = serverDefault,
command: string = defaultCommand,
forcessh: boolean = forceSSHDefault,
ssl: SSL | undefined = undefined,
): Promise<SocketIO.Server> =>
decorateServerWithSsh(express(), ssh, serverConf, command, forcessh, ssl);

export async function decorateServerWithSsh(
app: Application,
ssh: SSH = sshDefault,
serverConf: Server = serverDefault,
command: string = defaultCommand,
Expand All @@ -50,7 +64,7 @@ export async function start(
collectDefaultMetrics();
gc().on('stats', gcMetrics);

const io = await server(serverConf, ssl);
const io = await server(app, serverConf, ssl);
/**
* Wetty server connected too
* @fires WeTTy#connnection
Expand Down
4 changes: 2 additions & 2 deletions src/server/command.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import url from 'url';
import type { Socket } from 'socket.io';
import type { SSH } from '../shared/interfaces';
import { address } from './command/address.js';
import { loginOptions } from './command/login.js';
import { sshOptions } from './command/ssh.js';
import type { SSH } from '../shared/interfaces';
import type { Socket } from 'socket.io';

const localhost = (host: string): boolean =>
process.getuid() === 0 &&
Expand Down
2 changes: 1 addition & 1 deletion src/server/command/address.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IncomingHttpHeaders } from 'http';
import _ from 'lodash';
import { escapeShell } from '../shared/shell.js';
import type { IncomingHttpHeaders } from 'http';

export function address(
headers: IncomingHttpHeaders,
Expand Down
Loading

0 comments on commit 6ecfe98

Please sign in to comment.