Files
tabby-web/src/terminal.ts

215 lines
5.9 KiB
TypeScript
Raw Normal View History

2021-06-12 13:05:35 +02:00
Object.assign(window, {
process: {
env: { XWEB: 1, LOGNAME: 'root' },
argv: ['terminus'],
platform: 'darwin',
on: () => null,
stdout: {},
stderr: {},
resourcesPath: 'resources',
version: '14.0.0',
nextTick: (f, ...args) => setTimeout(() => f(...args)),
// cwd: () => '/',
},
global: window,
})
2021-06-15 23:43:23 +02:00
import { Duplex } from 'stream-browserify'
2021-06-12 13:05:35 +02:00
2021-05-22 15:52:23 +02:00
import 'core-js/proposals/reflect-metadata'
import '@fortawesome/fontawesome-free/css/solid.css'
import '@fortawesome/fontawesome-free/css/brands.css'
import '@fortawesome/fontawesome-free/css/fontawesome.css'
import 'source-code-pro/source-code-pro.css'
import 'source-sans-pro/source-sans-pro.css'
2021-05-31 20:13:09 +02:00
import './terminal-styles.scss'
2021-05-22 15:52:23 +02:00
export class Socket extends Duplex {
webSocket: WebSocket
2021-06-12 13:05:35 +02:00
initialBuffer: Buffer
2021-05-22 15:52:23 +02:00
constructor () {
console.warn('socket constr', arguments)
super({
allowHalfOpen: false,
})
2021-06-12 13:05:35 +02:00
this.initialBuffer = Buffer.from('')
2021-05-22 15:52:23 +02:00
}
connect () {
2021-06-12 13:05:35 +02:00
this.webSocket = new WebSocket(`ws://${location.host}/api/1/gateway/tcp`)
2021-05-22 15:52:23 +02:00
this.webSocket.onopen = event => {
2021-05-31 20:13:09 +02:00
this['emit']('connect')
2021-06-12 13:05:35 +02:00
this.webSocket.send(this.initialBuffer)
this.initialBuffer = Buffer.from('')
2021-05-22 15:52:23 +02:00
}
this.webSocket.onmessage = async event => {
2021-05-31 20:13:09 +02:00
this['emit']('data', Buffer.from(await event.data.arrayBuffer()))
2021-05-22 15:52:23 +02:00
}
}
setNoDelay () {
}
setTimeout () {
}
_read (size: number): void {
}
_write (chunk: Buffer, _encoding: string, callback: (error?: Error | null) => void): void {
2021-06-12 13:05:35 +02:00
if (!this.webSocket.readyState) {
this.initialBuffer = Buffer.concat([this.initialBuffer, chunk])
} else {
this.webSocket.send(chunk)
}
2021-05-22 15:52:23 +02:00
callback()
}
_destroy (error: Error|null, callback: (error: Error|null) => void): void {
console.warn('socket destroy', error)
callback(error)
}
}
async function start () {
const mocks = {
2021-06-15 23:43:23 +02:00
2021-05-22 15:52:23 +02:00
'@electron/remote': {
app: {
getVersion: () => '1.0-web',
getPath: () => 'app-path',
getWindow: () => ({
reload: () => null,
2021-05-31 20:13:09 +02:00
setTrafficLightPosition: () => null,
2021-05-22 15:52:23 +02:00
}),
},
screen: {
on: () => null,
getAllDisplays: () => [],
getPrimaryDisplay: () => ({}),
getCursorScreenPoint: () => ({}),
getDisplayNearestPoint: () => null,
},
globalShortcut: {
unregisterAll: () => null,
register: () => null,
},
autoUpdater: {
on: () => null,
once: () => null,
setFeedURL: () => null,
checkForUpdates: () => null,
},
BrowserWindow: {
fromId: () => ({
setOpacity: () => null,
setProgressBar: () => null,
}),
},
getGlobal: () => window['process'],
},
electron: {
2021-06-15 23:43:23 +02:00
ipcRenderer: { // TODO remove
2021-06-14 23:00:05 +02:00
on: (e, c) => {
console.log('[ipc listen]', e)
},
2021-05-22 15:52:23 +02:00
once: (e, c) => {
2021-06-14 23:00:05 +02:00
console.log('[ipc listen once]', e)
2021-05-22 15:52:23 +02:00
},
send: msg => {
console.log('[ipc]', msg)
}
},
},
net: {
Socket,
},
2021-06-15 23:43:23 +02:00
// winston: {
// Logger,
// transports: {
// File: Object,
// Console: Object,
// }
// },
// 'mz/child_process': {
// exec: (...x) => Promise.reject(),
// },
// 'mz/fs': {
// readFile: path => mocks.fs.readFileSync(path),
// exists: path => mocks.fs.existsSync(path),
// existsSync: path => mocks.fs.existsSync(path),
// },
}
2021-05-22 15:52:23 +02:00
let builtins = {
}
Object.assign(window, {
require: (path) => {
if (mocks[path]) {
2021-05-31 20:13:09 +02:00
console.warn('requiring mock', path)
2021-05-22 15:52:23 +02:00
return mocks[path]
}
if (builtins[path]) {
return builtins[path]
}
2021-05-31 20:13:09 +02:00
console.error('requiring real module', path)
2021-05-22 15:52:23 +02:00
},
})
2021-06-15 23:43:23 +02:00
const appVersion = location.search.substring(1)
2021-05-22 15:52:23 +02:00
2021-05-31 20:13:09 +02:00
async function loadPlugin (name, file = 'index.js') {
2021-06-15 23:43:23 +02:00
const url = `../app-dist/${appVersion}/${name}/dist/${file}`
console.log(`Loading ${url}`)
2021-06-12 13:05:35 +02:00
const e = document.createElement('script')
2021-06-14 23:00:05 +02:00
window['module'] = { exports: {} } as any
2021-06-12 13:05:35 +02:00
window['exports'] = window['module'].exports
await new Promise(resolve => {
e.onload = resolve
e.src = url
document.querySelector('head').appendChild(e)
})
return window['module'].exports
2021-05-22 15:52:23 +02:00
}
2021-06-15 23:43:23 +02:00
await loadPlugin('web', 'preload.js')
await loadPlugin('web', 'bundle.js')
2021-05-31 20:13:09 +02:00
const pluginModules = []
for (const plugin of [
'terminus-core',
'terminus-settings',
'terminus-terminal',
'terminus-ssh',
'terminus-community-color-schemes',
'terminus-web',
]) {
const mod = await loadPlugin(plugin)
builtins[`resources/builtin-plugins/${plugin}`] = builtins[plugin] = mod
pluginModules.push(mod)
2021-05-22 15:52:23 +02:00
}
document.querySelector('app-root')['style'].display = 'flex'
2021-06-14 23:00:05 +02:00
await new Promise<void>(resolve => {
window.addEventListener('message', event => {
if (event.data === 'connector-ready') {
resolve()
}
})
window.parent.postMessage('request-connector', '*')
})
const config = window['__connector__'].loadConfig()
2021-05-31 20:13:09 +02:00
window['bootstrapTerminus'](pluginModules, { config })
2021-05-22 15:52:23 +02:00
}
start()