🆕 Implement client

This commit is contained in:
steelbrain
2024-02-15 06:37:52 +02:00
parent 9a703aa099
commit 7c9ca7fea9
3 changed files with 103 additions and 2 deletions

View File

@@ -6,7 +6,9 @@
"type": "module",
"scripts": {
"build:server": "esbuild src/server.ts --platform=node --bundle --outdir=lib",
"watch:server": "esbuild src/server.ts --platform=node --bundle --outdir=lib --watch"
"watch:server": "esbuild src/server.ts --platform=node --bundle --outdir=lib --watch",
"build:client": "esbuild src/client.ts --platform=node --bundle --outdir=lib",
"watch:client": "esbuild src/client.ts --platform=node --bundle --outdir=lib --watch"
},
"author": "",
"license": "MIT",

93
src/client.ts Normal file
View File

@@ -0,0 +1,93 @@
import http from 'node:http'
import { loadConfig } from './config.js'
import { CONFIG_FILE_SEARCH_PATHS_CLIENT, Runtime } from './constants.js'
import createLogger from './logger.js'
async function main() {
if (process.argv.includes('--debug-print-search-paths')) {
console.log(CONFIG_FILE_SEARCH_PATHS_CLIENT)
process.exit(1)
}
const config = await loadConfig(Runtime.Client)
if (process.argv.includes('--debug-print-config')) {
console.log(config)
process.exit(1)
}
const logger = createLogger(config.log)
const request = http.request(
{
hostname: config.connectAddress,
port: config.connectPort,
path: '/',
method: 'POST',
headers: {
Authorization: `Bearer ${config.authSecret}`,
},
},
response => {
if (response.statusCode !== 200) {
process.exitCode = 1
}
response.on('data', chunk => {
let parsed: unknown
try {
parsed = JSON.parse(chunk.toString())
} catch (err) {
console.error(chunk.toString())
return
}
if (
parsed != null &&
typeof parsed === 'object' &&
'stream' in parsed &&
typeof parsed.stream === 'string' &&
'data' in parsed &&
(parsed.data == null || typeof parsed.data === 'string')
) {
if (parsed.stream === 'stdout') {
if (parsed.data != null) {
process.stdout.write(parsed.data)
} else {
process.stdout.end()
}
} else {
if (parsed.data != null) {
process.stderr.write(parsed.data)
} else {
process.stderr.end()
}
}
return
}
if (
parsed != null &&
typeof parsed === 'object' &&
'exitCode' in parsed &&
typeof parsed.exitCode === 'number'
) {
process.exit(parsed.exitCode)
}
console.error(parsed)
})
}
)
request.write(JSON.stringify(process.argv.slice(2)))
request.end()
request.on('error', err => {
logger.error(`Error during request: ${err}`)
process.exitCode = 1
console.error('Failed to connect to server')
})
}
main().catch(err => {
console.error('Error during initialization', { err })
process.exit(1)
})

View File

@@ -10,5 +10,11 @@
// ^ This turns off logging completely
"log": "stdout",
"log": "stderr",
"log": "/var/log/messages.log"
"log": "/var/log/messages.log",
"connectAddress": "192.168.20.1", // type: string
"connectPort": 5050, // type: number
"authSecret": "YOUR-CLIENT-PASSWORD-HERE" // type: string
// ^ This MUST match what you have in the server
}