peertube-plugin-livechat/server/lib/diagnostic/prosody.ts

98 lines
3.1 KiB
TypeScript
Raw Normal View History

import { getProsodyConfig, getWorkingDir } from '../prosody/config'
2021-04-14 14:14:56 +00:00
import { getProsodyAbout, testProsodyCorrectlyRunning } from '../prosody/ctl'
import { newResult, TestResult } from './utils'
2021-04-13 15:13:41 +00:00
import * as fs from 'fs'
2021-04-12 18:52:21 +00:00
export async function diagProsody (test: string, options: RegisterServerOptions): Promise<TestResult> {
const result = newResult(test)
result.label = 'Builtin Prosody and ConverseJS'
2021-04-12 18:52:21 +00:00
try {
const workingDir = await getWorkingDir(options)
result.messages.push('The working dir is: ' + workingDir.dir)
if (workingDir.permanent) {
result.messages.push('The working dir is permanent')
} else {
result.messages.push('The working dir is a temporary directory')
}
2021-04-12 18:52:21 +00:00
} catch (error) {
result.messages.push('Error when requiring the working dir: ' + (error as string))
return result
}
2021-04-14 16:15:43 +00:00
// FIXME: these tests are very similar to tests in testProsodyCorrectlyRunning. Remove from here?
2021-04-13 15:13:41 +00:00
// Testing the prosody config file.
try {
const wantedConfig = await getProsodyConfig(options)
const filePath = wantedConfig.paths.config
2021-04-16 13:23:08 +00:00
result.messages.push(`Prosody will run on port '${wantedConfig.port}'`)
result.messages.push(`Prosody modules path will be '${wantedConfig.paths.modules}'`)
2021-04-13 15:13:41 +00:00
await fs.promises.access(filePath, fs.constants.R_OK) // throw an error if file does not exist.
result.messages.push(`The prosody configuration file (${filePath}) exists`)
const actualContent = await fs.promises.readFile(filePath, {
encoding: 'utf-8'
})
2021-04-14 14:14:56 +00:00
result.debug.push({
title: 'Current prosody configuration',
message: actualContent
})
const wantedContent = wantedConfig.content
2021-04-13 15:13:41 +00:00
if (actualContent === wantedContent) {
result.messages.push('Prosody configuration file content is correct.')
} else {
result.messages.push('Prosody configuration file content is not correct.')
2021-04-14 14:14:56 +00:00
result.debug.push({
title: 'Prosody configuration should be',
message: wantedContent
})
2021-04-13 15:13:41 +00:00
return result
}
} catch (error) {
result.messages.push('Error when requiring the prosody config file: ' + (error as string))
return result
}
2021-04-13 16:00:45 +00:00
const isCorrectlyRunning = await testProsodyCorrectlyRunning(options)
if (isCorrectlyRunning.messages.length) {
result.messages.push(...isCorrectlyRunning.messages)
}
2021-04-14 14:14:56 +00:00
const about = await getProsodyAbout(options)
result.debug.push({
title: 'Prosody version',
message: about
})
2021-04-13 16:00:45 +00:00
if (!isCorrectlyRunning.ok) {
return result
}
2021-05-02 14:16:19 +00:00
const versionMatches = about.match(/^Prosody\s*(\d+)\.(\d+)\.(\d+)\s*$/mi)
if (!versionMatches) {
result.messages.push({
level: 'error',
message: 'Errors: cant find prosody version.'
})
return result
} else {
const major = versionMatches[1]
const minor = versionMatches[2]
const patch = versionMatches[3]
result.messages.push(`Prosody version is ${major}.${minor}.${patch}`)
if (major !== '0' && minor !== '11') {
result.messages.push({
level: parseInt(minor) < 11 ? 'error' : 'warning',
message: 'Warning: recommended Prosody version is 0.11.x'
})
}
}
2021-04-12 18:52:21 +00:00
result.ok = true
return result
}