2022-01-11 01:29:33 +01:00
|
|
|
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
2021-06-02 19:16:27 +02:00
|
|
|
import { migrateSettings } from './lib/migration/settings'
|
2021-04-08 02:43:13 +02:00
|
|
|
import { initSettings } from './lib/settings'
|
2021-06-08 18:08:58 +02:00
|
|
|
import { initCustomFields } from './lib/custom-fields'
|
2021-04-09 19:29:44 +02:00
|
|
|
import { initRouters } from './lib/routers/index'
|
2021-04-13 17:13:41 +02:00
|
|
|
import { ensureProsodyRunning, ensureProsodyNotRunning } from './lib/prosody/ctl'
|
2021-04-09 11:22:46 +02:00
|
|
|
import decache from 'decache'
|
2021-04-08 03:47:55 +02:00
|
|
|
|
2021-06-02 12:48:24 +02:00
|
|
|
// FIXME: Peertube unregister don't have any parameter.
|
|
|
|
// Using this global variable to fix this, so we can use helpers to unregister.
|
2021-04-13 17:13:41 +02:00
|
|
|
let OPTIONS: RegisterServerOptions | undefined
|
2021-04-07 16:52:38 +02:00
|
|
|
|
2021-04-09 19:39:40 +02:00
|
|
|
async function register (options: RegisterServerOptions): Promise<any> {
|
2021-04-13 17:13:41 +02:00
|
|
|
OPTIONS = options
|
2021-04-08 03:47:55 +02:00
|
|
|
|
2021-06-03 11:53:18 +02:00
|
|
|
// This is a trick to check that peertube is at least in version 3.2.0
|
|
|
|
if (!options.peertubeHelpers.plugin) {
|
|
|
|
throw new Error('Your peertube version is not correct. This plugin is not compatible with Peertube < 3.2.0.')
|
|
|
|
}
|
|
|
|
|
2021-06-02 19:16:27 +02:00
|
|
|
await migrateSettings(options)
|
|
|
|
|
2021-04-09 19:39:40 +02:00
|
|
|
await initSettings(options)
|
2021-06-08 18:08:58 +02:00
|
|
|
await initCustomFields(options)
|
2021-04-09 19:39:40 +02:00
|
|
|
await initRouters(options)
|
2021-04-13 17:13:41 +02:00
|
|
|
|
2021-09-14 16:49:11 +02:00
|
|
|
try {
|
|
|
|
await ensureProsodyRunning(options)
|
|
|
|
} catch (error) {
|
2021-12-01 14:47:16 +01:00
|
|
|
options.peertubeHelpers.logger.error('Error when launching Prosody: ' + (error as string))
|
2021-09-14 16:49:11 +02:00
|
|
|
}
|
2019-07-16 16:39:36 +02:00
|
|
|
}
|
|
|
|
|
2021-04-07 17:20:28 +02:00
|
|
|
async function unregister (): Promise<any> {
|
2021-04-13 17:13:41 +02:00
|
|
|
if (OPTIONS) {
|
|
|
|
try {
|
|
|
|
await ensureProsodyNotRunning(OPTIONS)
|
|
|
|
} catch (error) {
|
|
|
|
OPTIONS.peertubeHelpers.logger.error('Error when trying to unload Prosody: ' + (error as string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-08 03:47:55 +02:00
|
|
|
const module = __filename
|
2021-04-13 17:13:41 +02:00
|
|
|
OPTIONS?.peertubeHelpers.logger.info(`Unloading module ${module}...`)
|
2021-06-02 12:48:24 +02:00
|
|
|
// Peertube calls decache(plugin) on register, not unregister.
|
|
|
|
// Will do here, to release memory.
|
2021-04-08 03:47:55 +02:00
|
|
|
decache(module)
|
2021-04-13 17:13:41 +02:00
|
|
|
OPTIONS?.peertubeHelpers.logger.info(`Successfully unloaded the module ${module}`)
|
|
|
|
OPTIONS = undefined
|
2019-07-16 16:39:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
register,
|
|
|
|
unregister
|
|
|
|
}
|