This commit is contained in:
Eugene Pankov
2021-06-19 23:01:22 +02:00
parent f8f58a66ec
commit e74b44a9bc
5 changed files with 81 additions and 210 deletions

View File

@@ -66,7 +66,7 @@ export class MainComponent {
}
loadApp (version) {
this.iframe.nativeElement.src = `/terminal?${version.version}`
this.iframe.nativeElement.src = '/terminal'
this.activeVersion = version
}
@@ -78,6 +78,7 @@ export class MainComponent {
// TODO check config incompatibility
this.unloadApp()
setTimeout(() => {
this.appConnector.version = version
this.loadApp(version)
})
}

View File

@@ -1,11 +1,53 @@
import { Buffer } from 'buffer'
import { Subject } from 'rxjs'
import { debounceTime } from 'rxjs/operators'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
export class SocketProxy {
connect$ = new Subject<void>()
data$ = new Subject<Buffer>()
webSocket: WebSocket
initialBuffer: Buffer
constructor (...args) {
console.log('ws ctr', args)
this.initialBuffer = Buffer.from('')
}
connect (...args) {
console.log('ws connect', args)
this.webSocket = new WebSocket(`ws://${location.host}/api/1/gateway/tcp`)
this.webSocket.onopen = event => {
this.connect$.next()
this.connect$.complete()
this.webSocket.send(this.initialBuffer)
this.initialBuffer = Buffer.from('')
}
this.webSocket.onmessage = async event => {
this.data$.next(Buffer.from(await event.data.arrayBuffer()))
}
}
write (chunk: Buffer): void {
if (!this.webSocket.readyState) {
this.initialBuffer = Buffer.concat([this.initialBuffer, chunk])
} else {
this.webSocket.send(chunk)
}
}
close (error: Error): void {
console.warn('socket destroy', error)
}
}
@Injectable({ providedIn: 'root' })
export class AppConnectorService {
config: any
version: any
Socket = SocketProxy
private configUpdate = new Subject<string>()
constructor (private http: HttpClient) {
@@ -23,4 +65,8 @@ export class AppConnectorService {
this.configUpdate.next(content)
this.config.content = content
}
getAppVersion (): string {
return this.version.version
}
}

View File

@@ -1,6 +1,10 @@
import './terminal-styles.scss'
Object.assign(window, {
// Buffer,
process: {
env: { XWEB: 1, LOGNAME: 'root' },
env: { },
argv: ['terminus'],
platform: 'darwin',
on: () => null,
@@ -15,155 +19,28 @@ Object.assign(window, {
})
import { Duplex } from 'stream-browserify'
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'
import './terminal-styles.scss'
export class Socket extends Duplex {
webSocket: WebSocket
initialBuffer: Buffer
constructor () {
console.warn('socket constr', arguments)
super({
allowHalfOpen: false,
})
this.initialBuffer = Buffer.from('')
}
connect () {
this.webSocket = new WebSocket(`ws://${location.host}/api/1/gateway/tcp`)
this.webSocket.onopen = event => {
this['emit']('connect')
this.webSocket.send(this.initialBuffer)
this.initialBuffer = Buffer.from('')
}
this.webSocket.onmessage = async event => {
this['emit']('data', Buffer.from(await event.data.arrayBuffer()))
}
}
setNoDelay () {
}
setTimeout () {
}
_read (size: number): void {
}
_write (chunk: Buffer, _encoding: string, callback: (error?: Error | null) => void): void {
if (!this.webSocket.readyState) {
this.initialBuffer = Buffer.concat([this.initialBuffer, chunk])
} else {
this.webSocket.send(chunk)
}
callback()
}
_destroy (error: Error|null, callback: (error: Error|null) => void): void {
console.warn('socket destroy', error)
callback(error)
}
}
async function start () {
const mocks = {
'@electron/remote': {
app: {
getVersion: () => '1.0-web',
getPath: () => 'app-path',
getWindow: () => ({
reload: () => null,
setTrafficLightPosition: () => null,
}),
},
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: {
ipcRenderer: { // TODO remove
on: (e, c) => {
console.log('[ipc listen]', e)
},
once: (e, c) => {
console.log('[ipc listen once]', e)
},
send: msg => {
console.log('[ipc]', msg)
}
},
},
net: {
Socket,
},
// 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),
// },
}
let builtins = {
}
const modules = { }
Object.assign(window, {
require: (path) => {
if (mocks[path]) {
console.warn('requiring mock', path)
return mocks[path]
}
if (builtins[path]) {
return builtins[path]
if (modules[path]) {
return modules[path]
}
console.error('requiring real module', path)
},
})
const appVersion = location.search.substring(1)
await new Promise<void>(resolve => {
window.addEventListener('message', event => {
if (event.data === 'connector-ready') {
resolve()
}
})
window.parent.postMessage('request-connector', '*')
})
const appVersion = window['__connector__'].getAppVersion()
async function loadPlugin (name, file = 'index.js') {
const url = `../app-dist/${appVersion}/${name}/dist/${file}`
@@ -192,23 +69,21 @@ async function start () {
'terminus-web',
]) {
const mod = await loadPlugin(plugin)
builtins[`resources/builtin-plugins/${plugin}`] = builtins[plugin] = mod
modules[`resources/builtin-plugins/${plugin}`] = modules[plugin] = mod
pluginModules.push(mod)
}
document.querySelector('app-root')['style'].display = 'flex'
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()
window['bootstrapTerminus'](pluginModules, { config })
window['bootstrapTerminus'](pluginModules, {
config,
executable: 'web',
isFirstWindow: true,
windowID: 1,
installedPlugins: [],
userPluginsPath: '/',
})
}
start()