-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
111 lines (97 loc) · 4.28 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import * as TOML from '@ltd/j-toml';
import * as logger from "./logger.js";
import _ from "lodash";
import {defaultAppSpec,defaultUser} from "./defaults.js";
import crypto from "crypto";
import fs from "fs";
import os from "os";
// synchronous config load
export let config = {
appSpecs: [],
users: process.env.TEST ? [{
id: "test",
password: "1234"
}]:[],
secret: Buffer.from(crypto.getRandomValues(new Uint8Array(32)).buffer).toString("base64"),
managementOptions: {
pipewire: true && !process.env.PULSEAUDIO,
hyperwarpPath: "/opt/hyperwarp",
streamerPath: "/opt/streamerd",
hyperwarpTarget: "release",
procExitRequestTimeoutMs: 30 * 1000
},
sessionMaxLength: process.env.NODE_ENV == "production" ? "1d" : "7d", // new development default, will change my life
debug: false,
memoryDebug: false,
flameGraph: false,
valgrind: false,
valgrindChild: false,
port: 8001,
streamerdTargetHttpAddr: null,
encoder: "H264",
optimizations: "None",
resetLibva: false,
};
export function loadConfig(){
if(!fs.existsSync('config.toml')){
logger.warn("No config.toml found, generating a new one. ");
fs.writeFileSync('config.toml', TOML.stringify(config, {
newline: os.EOL
}));
}
let configStr = fs.readFileSync('config.toml', 'utf8');
let configDeserialized = TOML.parse(configStr, {
joiner: "\n",
multilineStringJoiner: "\n"
});
if(configDeserialized.appSpecs && configDeserialized.appSpecs.length > 0){
config.appSpecs = configDeserialized.appSpecs.map(appSpec => {
return _.defaultsDeep(appSpec, defaultAppSpec);
});
}else{
logger.warn("No appSpecs found in config.toml, server won't be able to do much. Consider checking the docs and adding an app. ");
}
if(process.env.SECRET){
config.secret = process.env.SECRET;
}else if(configDeserialized.secret){
config.secret = configDeserialized.secret;
}else{
logger.warn("No secret found in config.toml or in environment variables, generating a new one. This will invalidate all existing sessions every server restart. Consider specifying a secret in config.toml or in environment variables.");
}
if("debug" in configDeserialized) config.debug = configDeserialized.debug;
if(configDeserialized.users && configDeserialized.users.length > 0){
config.users = configDeserialized.users.filter((user, index) => {
if(!user.id){
logger.warn("No user id specified for user at index " + index + ", skipping. ");
return false;
}
if(!user.password && !user.passwordHash){
logger.warn("No user password or password hash specified for user at index " + index + ", skipping. ");
return false;
}
return true;
}).map(user => {
return _.defaults(user, defaultUser);
});
}else{
logger.warn("Config specifies no users. ");
}
config.managementOptions = (configDeserialized.managementOptions || config.managementOptions);
if(configDeserialized.streamerdTargetHttpAddr) config.streamerdTargetHttpAddr = configDeserialized.streamerdTargetHttpAddr;
// TODO: create our own section in the toml for configuring streamerd
if(!config.streamerdTargetHttpAddr){
// make a guess if this is not specified
config.streamerdTargetHttpAddr = "http://127.0.0.1:" + config.port;
}
if(configDeserialized.encoder) config.encoder = configDeserialized.encoder;
if(configDeserialized.optimizations) config.optimizations = configDeserialized.optimizations;
if("memoryDebug" in configDeserialized) config.memoryDebug = configDeserialized.memoryDebug;
if("flameGraph" in configDeserialized) config.flameGraph = configDeserialized.flameGraph;
if("valgrind" in configDeserialized) config.valgrind = configDeserialized.valgrind;
// allows reseting liva driver name
if("resetLibva" in configDeserialized) config.resetLibva = configDeserialized.resetLibva;
if("valgrindChild" in configDeserialized) config.valgrindChild = configDeserialized.valgrindChild;
// console.log(config.managementOptions);
}
loadConfig();
export default config;