mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2024-05-16 20:02:40 +00:00
3a4992633e
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
141 lines
3.2 KiB
TypeScript
141 lines
3.2 KiB
TypeScript
import { parse, stringify } from '@iarna/toml'
|
|
import envPaths from 'env-paths'
|
|
import { ensureDir, pathExists, remove } from 'fs-extra/esm'
|
|
import { readFile, writeFile } from 'fs/promises'
|
|
import merge from 'lodash-es/merge.js'
|
|
import { dirname, join } from 'path'
|
|
import { logger } from '../shared/index.js'
|
|
|
|
const paths = envPaths('peertube-runner')
|
|
|
|
type Config = {
|
|
jobs: {
|
|
concurrency: number
|
|
}
|
|
|
|
ffmpeg: {
|
|
threads: number
|
|
nice: number
|
|
}
|
|
|
|
registeredInstances: {
|
|
url: string
|
|
runnerToken: string
|
|
runnerName: string
|
|
runnerDescription?: string
|
|
}[]
|
|
}
|
|
|
|
export class ConfigManager {
|
|
private static instance: ConfigManager
|
|
|
|
private config: Config = {
|
|
jobs: {
|
|
concurrency: 2
|
|
},
|
|
ffmpeg: {
|
|
threads: 2,
|
|
nice: 20
|
|
},
|
|
registeredInstances: []
|
|
}
|
|
|
|
private id: string
|
|
private configFilePath: string
|
|
|
|
private constructor () {}
|
|
|
|
init (id: string) {
|
|
this.id = id
|
|
this.configFilePath = join(this.getConfigDir(), 'config.toml')
|
|
}
|
|
|
|
async load () {
|
|
logger.info(`Using ${this.configFilePath} as configuration file`)
|
|
|
|
if (this.isTestInstance()) {
|
|
logger.info('Removing configuration file as we are using the "test" id')
|
|
await remove(this.configFilePath)
|
|
}
|
|
|
|
await ensureDir(dirname(this.configFilePath))
|
|
|
|
if (!await pathExists(this.configFilePath)) {
|
|
await this.save()
|
|
}
|
|
|
|
const file = await readFile(this.configFilePath, 'utf-8')
|
|
|
|
this.config = merge(this.config, parse(file))
|
|
}
|
|
|
|
save () {
|
|
return writeFile(this.configFilePath, stringify(this.config))
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
async setRegisteredInstances (registeredInstances: {
|
|
url: string
|
|
runnerToken: string
|
|
runnerName: string
|
|
runnerDescription?: string
|
|
}[]) {
|
|
this.config.registeredInstances = registeredInstances
|
|
|
|
await this.save()
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
getConfig () {
|
|
return this.deepFreeze(this.config)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
getTranscodingDirectory () {
|
|
return join(paths.cache, this.id, 'transcoding')
|
|
}
|
|
|
|
getSocketDirectory () {
|
|
return join(paths.data, this.id)
|
|
}
|
|
|
|
getSocketPath () {
|
|
return join(this.getSocketDirectory(), 'peertube-runner.sock')
|
|
}
|
|
|
|
getConfigDir () {
|
|
return join(paths.config, this.id)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
isTestInstance () {
|
|
return typeof this.id === 'string' && this.id.match(/^test-\d$/)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// Thanks: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
|
|
private deepFreeze <T extends object> (object: T) {
|
|
const propNames = Reflect.ownKeys(object)
|
|
|
|
// Freeze properties before freezing self
|
|
for (const name of propNames) {
|
|
const value = object[name]
|
|
|
|
if ((value && typeof value === 'object') || typeof value === 'function') {
|
|
this.deepFreeze(value)
|
|
}
|
|
}
|
|
|
|
return Object.freeze({ ...object })
|
|
}
|
|
|
|
static get Instance () {
|
|
return this.instance || (this.instance = new this())
|
|
}
|
|
}
|