mirror of
https://github.com/steelbrain/ffmpeg-over-ip.git
synced 2024-10-12 01:34:56 +03:00
🆕 Implement client
This commit is contained in:
@@ -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
93
src/client.ts
Normal 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)
|
||||
})
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user