Skip to content

Commit

Permalink
more wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Zubnix committed Oct 27, 2023
1 parent 2b385ee commit 77084a0
Show file tree
Hide file tree
Showing 20 changed files with 174 additions and 370 deletions.
3 changes: 2 additions & 1 deletion packages/compositor-proxy-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"scripts": {
"generate:config-types": "yarn run json2ts -i src/app-config-schema.json -o src/app-config.d.ts --style.singleQuote --no-style.semi",
"build": "yarn run rimraf dist && yarn run tsc",
"start": "yarn ts-node src/main.ts --applications=./example-applications.json",
"start": "yarn tsx src/main.ts --applications=./example-applications.json",
"package:copy-libs": "cp '../compositor-proxy/dist/addons/libwayland-server.so.0' '../compositor-proxy/dist/addons/libwestfield.so' '../compositor-proxy/dist/addons/libproxy-encoding.so' -t ./package",
"package": "yarn run pkg --compress Brotli --no-native-build package.json && yarn package:copy-libs"
},
Expand All @@ -39,6 +39,7 @@
"prettier": "^3.0.3",
"rimraf": "^5.0.5",
"ts-node": "^10.9.1",
"tsx": "^3.14.0",
"typescript": "^5.2.2"
},
"pkg": {
Expand Down
2 changes: 1 addition & 1 deletion packages/compositor-proxy-cli/src/main-args.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseArgs, ParseArgsConfig } from 'node:util'
import { AppConfigSchema } from './app-config'
import { AppConfigSchema } from './app-config.js'
import { readFileSync } from 'node:fs'
import Ajv from 'ajv'
import addFormats from 'ajv-formats'
Expand Down
14 changes: 7 additions & 7 deletions packages/compositor-proxy-cli/src/main-controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ToMainProcessMessage, ToSessionProcessMessage } from './SessionProcess'
import { ChildProcess } from 'child_process'
import { ToMainProcessMessage, ToSessionProcessMessage } from './SessionProcess.js'
import { ChildProcess } from 'node:child_process'
import { Configschema, createLogger } from '@gfld/compositor-proxy'
import { IncomingMessage, ServerResponse } from 'http'
import { args } from './main-args'
import { AppConfigSchema } from './app-config'
import { IncomingMessage, ServerResponse } from 'node:http'
import { args } from './main-args.js'
import { AppConfigSchema } from './app-config.js'

type RemoteAppContextAttributes = Readonly<{
baseURL: string
Expand Down Expand Up @@ -91,7 +91,7 @@ function sendMessageWithReply<T extends Extract<ToSessionProcessMessage, { type:
})
}

export function handleOptions(config: Configschema, request: IncomingMessage, response: ServerResponse, url: URL) {
export function handleOptions(config: Configschema, request: IncomingMessage, response: ServerResponse, _url: URL) {
const origin = request.headers['origin']
const accessControlRequestMethod = request.headers['access-control-request-method']
if (origin === '' || accessControlRequestMethod === '') {
Expand Down Expand Up @@ -183,7 +183,7 @@ export async function handleGET(
'Content-Type': 'application/json',
})
.end(JSON.stringify(reply))
} catch (e) {
} catch (e: any) {
logger.error(e)
response
.writeHead(500, 'Internal Server Error', {
Expand Down
8 changes: 4 additions & 4 deletions packages/compositor-proxy-cli/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Configschema, createLogger } from '@gfld/compositor-proxy'
import { createServer } from 'node:http'
import { ChildProcess, fork } from 'node:child_process'
import { ToSessionProcessMessage } from './SessionProcess'
import { Socket } from 'net'
import { authRequest, handleGET, handleOptions } from './main-controller'
import { args } from './main-args'
import { ToSessionProcessMessage } from './SessionProcess.js'
import { Socket } from 'node:net'
import { authRequest, handleGET, handleOptions } from './main-controller.js'
import { args } from './main-args.js'
import { inspect } from 'node:util'
import path from 'node:path'

Expand Down
2 changes: 0 additions & 2 deletions packages/compositor-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
},
"dependencies": {
"@gfld/xtsb": "workspace:^",
"pino": "^8.15.1",
"pino-pretty": "^10.2.0",
"ws": "^8.14.2"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/compositor-proxy/src/Channel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Kcp } from './kcp'
import { NativeAppContext } from './NativeAppContext'
import { Kcp } from './kcp.js'
import { NativeAppContext } from './NativeAppContext.js'
import { WebSocket } from 'ws'

const MTU = 64000
Expand Down
6 changes: 3 additions & 3 deletions packages/compositor-proxy/src/FrameFeedback.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { destroyWlResourceSilently, flush, sendEvents, WlClient } from './wayland-server'
import { performance } from 'perf_hooks'
import type { Channel } from './Channel'
import { destroyWlResourceSilently, flush, sendEvents, WlClient } from './wayland-server.js'
import { performance } from 'node:perf_hooks'
import type { Channel } from './Channel.js'

let tickInterval = 16.667
let nextTickInterval = tickInterval
Expand Down
50 changes: 38 additions & 12 deletions packages/compositor-proxy/src/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
import Logger, { LoggerOptions } from 'pino'

const loggerConfig: LoggerOptions = {
level: process.env.LOG_LEVEL || 'info',
enabled: true,
} as const

export function createLogger(name: string): Logger.Logger {
return Logger({
...loggerConfig,
name,
})
export interface Logger {
info(msg: string): void

error(msg: string): void

fatal(msg: string): void

debug(msg: string): void
}

export type LoggerFactory = (name: string) => Logger

class DefaultLogger implements Logger {
constructor(public readonly name: string) {}

info(msg: string) {
console.info(`{time:"${new Date().toISOString()}",name:"${this.name}",msg:"${msg}"}`)
}

debug(msg: string): void {
// console.debug(`{time:"${new Date().toISOString()}",name:"${this.name}",msg:"${msg}"}`)
}

error(msg: string): void {
console.error(`{time:"${new Date().toISOString()}",name:"${this.name}",msg:"${msg}"}`)
}

fatal(msg: string): void {
console.error(`{time:"${new Date().toISOString()}",name:"${this.name}",msg:"${msg}"}`)
}
}

function createDefaultLogger(name: string): Logger {
return new DefaultLogger(name)
}

export function createLogger(name: string, loggerFactory: LoggerFactory = createDefaultLogger): Logger {
return loggerFactory(name)
}
14 changes: 7 additions & 7 deletions packages/compositor-proxy/src/NativeAppContext.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { NativeWaylandClientSession } from './NativeWaylandClientSession'
import { randomBytes } from 'crypto'
import { ChannelDesc, WebSocketChannel } from './Channel'
import { createLogger } from './Logger'
import { spawn } from 'child_process'
import { Session } from './Session'
import { setTimeout } from 'timers'
import { NativeWaylandClientSession } from './NativeWaylandClientSession.js'
import { randomBytes } from 'node:crypto'
import { ChannelDesc, WebSocketChannel } from './Channel.js'
import { createLogger } from './Logger.js'
import { spawn } from 'node:child_process'
import { Session } from './Session.js'
import { setTimeout } from 'node:timers'
import { WebSocket } from 'ws'

export type RemoteAppContextAttributes = Readonly<{
Expand Down
28 changes: 14 additions & 14 deletions packages/compositor-proxy/src/NativeWaylandClientSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@
// You should have received a copy of the GNU Affero General Public License
// along with Greenfield. If not, see <https://www.gnu.org/licenses/>.

import { createLogger } from './Logger'
import { NativeWaylandCompositorSession } from './NativeWaylandCompositorSession'

// eslint-disable-next-line camelcase,@typescript-eslint/ban-ts-comment
// @ts-ignore
import wl_display_interceptor from './protocol/wl_display_interceptor'
import { createLogger } from './Logger.js'
import { NativeWaylandCompositorSession } from './NativeWaylandCompositorSession.js'
import wl_display_interceptor from './protocol/wl_display_interceptor.js'
import { ProxyFD } from './io/types'
import { TextDecoder, TextEncoder } from 'util'
import { TextDecoder, TextEncoder } from 'node:util'
import {
destroyClient,
destroyWlResourceSilently,
Expand All @@ -39,11 +36,11 @@ import {
setWireMessageEndCallback,
WlRegistry,
WlClient,
} from './wayland-server'
import { ProxyBuffer } from './ProxyBuffer'
import type { Channel } from './Channel'
import wl_surface_interceptor from './protocol/wl_surface_interceptor'
import { NativeAppContext } from './NativeAppContext'
} from './wayland-server.js'
import { ProxyBuffer } from './ProxyBuffer.js'
import type { Channel } from './Channel.js'
import wl_surface_interceptor from './protocol/wl_surface_interceptor.js'
import { NativeAppContext } from './NativeAppContext.js'

const logger = createLogger('native-client-session')

Expand Down Expand Up @@ -114,8 +111,11 @@ export function createNativeClientSession(
protocolChannel.onMessage = (event) => {
try {
nativeClientSession.onMessage(event)
} catch (e) {
logger.error('BUG? Error while processing event from compositor.', e)
} catch (e: any) {
logger.error('BUG? Error while processing event from compositor.')
logger.error(`\tname: ${e.name} message: ${e.message} text: ${e.text}`)
logger.error('error object stack: ')
logger.error(e.stack)
nativeClientSession.destroy()
}
}
Expand Down
18 changes: 9 additions & 9 deletions packages/compositor-proxy/src/NativeWaylandCompositorSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
// along with Greenfield. If not, see <https://www.gnu.org/licenses/>.

import { PollHandle, startPoll, stopPoll } from './addons/proxy-poll-addon'
import { createProxyInputOutput } from './io/ProxyInputOutput'
import { createLogger } from './Logger'
import { createNativeClientSession, NativeWaylandClientSession } from './NativeWaylandClientSession'
import { createProxyInputOutput } from './io/ProxyInputOutput.js'
import { createLogger } from './Logger.js'
import { createNativeClientSession, NativeWaylandClientSession } from './NativeWaylandClientSession.js'
import {
addSocketAuto,
createDisplay,
Expand All @@ -33,12 +33,12 @@ import {
nativeGlobalNames,
WlClient,
WlDisplay,
} from './wayland-server'
import { Channel, createProtocolChannel } from './Channel'
import { webcrypto } from 'crypto'
import { Session } from './Session'
import { readFileSync } from 'fs'
import { NativeAppContext } from './NativeAppContext'
} from './wayland-server.js'
import { Channel, createProtocolChannel } from './Channel.js'
import { webcrypto } from 'node:crypto'
import { Session } from './Session.js'
import { readFileSync } from 'node:fs'
import { NativeAppContext } from './NativeAppContext.js'
import { pid } from 'node:process'

const logger = createLogger('native-compositor-session')
Expand Down
2 changes: 1 addition & 1 deletion packages/compositor-proxy/src/ProxyBuffer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MessageDestination } from './wayland-server'
import { MessageDestination } from './wayland-server.js'

let bufferSerial = 0

Expand Down
10 changes: 5 additions & 5 deletions packages/compositor-proxy/src/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
// You should have received a copy of the GNU Affero General Public License
// along with Greenfield. If not, see <https://www.gnu.org/licenses/>.

import { createLogger } from './Logger'
import { createLogger } from './Logger.js'

import { createNativeCompositorSession, NativeWaylandCompositorSession } from './NativeWaylandCompositorSession'
import { createXWaylandSession, XWaylandSession } from './XWaylandSession'
import { NativeAppContext } from './NativeAppContext'
import { Configschema } from './config'
import { createNativeCompositorSession, NativeWaylandCompositorSession } from './NativeWaylandCompositorSession.js'
import { createXWaylandSession, XWaylandSession } from './XWaylandSession.js'
import { NativeAppContext } from './NativeAppContext.js'
import { Configschema } from './config.js'

// TODO create logger per proxy session instance
const logger = createLogger('compositor-proxy-session')
Expand Down
14 changes: 7 additions & 7 deletions packages/compositor-proxy/src/SessionController.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { createWebSocketStream, WebSocket, WebSocketServer } from 'ws'
import { Session } from './Session'
import { IncomingMessage } from 'http'
import { close, createReadStream, createWriteStream, read } from 'fs'
import { createLogger } from './Logger'
import wl_surface_interceptor from './protocol/wl_surface_interceptor'
import { isSignalingMessage, SignalingMessageType } from './NativeAppContext'
import { Socket } from 'net'
import { Session } from './Session.js'
import { IncomingMessage } from 'node:http'
import { close, createReadStream, createWriteStream, read } from 'node:fs'
import { createLogger } from './Logger.js'
import wl_surface_interceptor from './protocol/wl_surface_interceptor.js'
import { isSignalingMessage, SignalingMessageType } from './NativeAppContext.js'
import { Socket } from 'node:net'

// 64*1024=64kb
const TRANSFER_CHUNK_SIZE = 65792 as const
Expand Down
16 changes: 8 additions & 8 deletions packages/compositor-proxy/src/SurfaceBufferEncoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
// You should have received a copy of the GNU Affero General Public License
// along with Greenfield. If not, see <https://www.gnu.org/licenses/>.

import { unmarshallArgs } from './wayland-server'
import { createEncoder, Encoder } from './encoding/Encoder'

import { createLogger } from './Logger'
import wlSurfaceInterceptor from './protocol/wl_surface_interceptor'
import { FrameFeedback } from './FrameFeedback'
import { incrementAndGetNextBufferSerial, ProxyBuffer } from './ProxyBuffer'
import { Channel, createFeedbackChannel, createFrameDataChannel } from './Channel'
import { unmarshallArgs } from './wayland-server.js'
import { createEncoder, Encoder } from './encoding/Encoder.js'

import { createLogger } from './Logger.js'
import wlSurfaceInterceptor from './protocol/wl_surface_interceptor.js'
import { FrameFeedback } from './FrameFeedback.js'
import { incrementAndGetNextBufferSerial, ProxyBuffer } from './ProxyBuffer.js'
import { Channel, createFeedbackChannel, createFrameDataChannel } from './Channel.js'

const logger = createLogger('surface-buffer-encoding')

Expand Down
10 changes: 5 additions & 5 deletions packages/compositor-proxy/src/XWaylandSession.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createLogger } from './Logger'
import { createLogger } from './Logger.js'
import { nodeFDConnectionSetup } from '@gfld/xtsb'
import { ClientEntry, NativeWaylandCompositorSession } from './NativeWaylandCompositorSession'
import { equalValueExternal, setupXWayland, teardownXWayland, WlClient, XWaylandHandle } from './wayland-server'
import { createReadStream } from 'fs'
import { Channel, createXWMDataChannel } from './Channel'
import { ClientEntry, NativeWaylandCompositorSession } from './NativeWaylandCompositorSession.js'
import { equalValueExternal, setupXWayland, teardownXWayland, WlClient, XWaylandHandle } from './wayland-server.js'
import { createReadStream } from 'node:fs'
import { Channel, createXWMDataChannel } from './Channel.js'

const logger = createLogger('xwayland')
const textEncoder = new TextEncoder()
Expand Down
2 changes: 1 addition & 1 deletion packages/compositor-proxy/src/encoding/Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// along with Greenfield. If not, see <https://www.gnu.org/licenses/>.

import appEndpointNative from '../addons/proxy-encoding-addon'
import { Session } from '../Session'
import { Session } from '../Session.js'

export function createEncoder(session: Session, wlClient: unknown, drmContext: unknown): Encoder {
// TODO we could probably use a pool here?
Expand Down
12 changes: 6 additions & 6 deletions packages/compositor-proxy/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { createLogger } from './Logger'
export { initSurfaceBufferEncoding } from './SurfaceBufferEncoding'
export { createSessionController, SessionController } from './SessionController'
export { createSession, Session } from './Session'
export { launchApplication, NativeAppContext, RemoteAppContextAttributes } from './NativeAppContext'
export { Configschema } from './config'
export { createLogger } from './Logger.js'
export { initSurfaceBufferEncoding } from './SurfaceBufferEncoding.js'
export { createSessionController, SessionController } from './SessionController.js'
export { createSession, Session } from './Session.js'
export { launchApplication, NativeAppContext, RemoteAppContextAttributes } from './NativeAppContext.js'
export { Configschema } from './config.js'
10 changes: 5 additions & 5 deletions packages/compositor-proxy/src/io/ProxyInputOutput.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createReadStream } from 'fs'
import { ProxyFD } from './types'
import { createLogger } from '../Logger'
import { createMemoryMappedFile, makePipe } from '../wayland-server'
import { createReadStream } from 'node:fs'
import { ProxyFD } from './types.js'
import { createLogger } from '../Logger.js'
import { createMemoryMappedFile, makePipe } from '../wayland-server.js'
import { createWebSocketStream, WebSocket } from 'ws'
import { Session } from '../Session'
import { Session } from '../Session.js'

const logger = createLogger('webfs')

Expand Down
Loading

0 comments on commit 77084a0

Please sign in to comment.