diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/initializers/checker-after-init.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
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)
Diffstat (limited to 'server/initializers/checker-after-init.ts')
-rw-r--r-- | server/initializers/checker-after-init.ts | 325 |
1 files changed, 0 insertions, 325 deletions
diff --git a/server/initializers/checker-after-init.ts b/server/initializers/checker-after-init.ts deleted file mode 100644 index 4281e2bb5..000000000 --- a/server/initializers/checker-after-init.ts +++ /dev/null | |||
@@ -1,325 +0,0 @@ | |||
1 | import config from 'config' | ||
2 | import { readFileSync, writeFileSync } from 'fs-extra' | ||
3 | import { URL } from 'url' | ||
4 | import { uniqify } from '@shared/core-utils' | ||
5 | import { getFFmpegVersion } from '@shared/ffmpeg' | ||
6 | import { VideoRedundancyConfigFilter } from '@shared/models/redundancy/video-redundancy-config-filter.type' | ||
7 | import { RecentlyAddedStrategy } from '../../shared/models/redundancy' | ||
8 | import { isProdInstance, parseBytes, parseSemVersion } from '../helpers/core-utils' | ||
9 | import { isArray } from '../helpers/custom-validators/misc' | ||
10 | import { logger } from '../helpers/logger' | ||
11 | import { ApplicationModel, getServerActor } from '../models/application/application' | ||
12 | import { OAuthClientModel } from '../models/oauth/oauth-client' | ||
13 | import { UserModel } from '../models/user/user' | ||
14 | import { CONFIG, getLocalConfigFilePath, isEmailEnabled, reloadConfig } from './config' | ||
15 | import { WEBSERVER } from './constants' | ||
16 | |||
17 | async function checkActivityPubUrls () { | ||
18 | const actor = await getServerActor() | ||
19 | |||
20 | const parsed = new URL(actor.url) | ||
21 | if (WEBSERVER.HOST !== parsed.host) { | ||
22 | const NODE_ENV = config.util.getEnv('NODE_ENV') | ||
23 | const NODE_CONFIG_DIR = config.util.getEnv('NODE_CONFIG_DIR') | ||
24 | |||
25 | logger.warn( | ||
26 | 'It seems PeerTube was started (and created some data) with another domain name. ' + | ||
27 | 'This means you will not be able to federate! ' + | ||
28 | 'Please use %s %s npm run update-host to fix this.', | ||
29 | NODE_CONFIG_DIR ? `NODE_CONFIG_DIR=${NODE_CONFIG_DIR}` : '', | ||
30 | NODE_ENV ? `NODE_ENV=${NODE_ENV}` : '' | ||
31 | ) | ||
32 | } | ||
33 | } | ||
34 | |||
35 | // Some checks on configuration files or throw if there is an error | ||
36 | function checkConfig () { | ||
37 | |||
38 | const configFiles = config.util.getConfigSources().map(s => s.name).join(' -> ') | ||
39 | logger.info('Using following configuration file hierarchy: %s.', configFiles) | ||
40 | |||
41 | checkRemovedConfigKeys() | ||
42 | |||
43 | checkSecretsConfig() | ||
44 | checkEmailConfig() | ||
45 | checkNSFWPolicyConfig() | ||
46 | checkLocalRedundancyConfig() | ||
47 | checkRemoteRedundancyConfig() | ||
48 | checkStorageConfig() | ||
49 | checkTranscodingConfig() | ||
50 | checkImportConfig() | ||
51 | checkBroadcastMessageConfig() | ||
52 | checkSearchConfig() | ||
53 | checkLiveConfig() | ||
54 | checkObjectStorageConfig() | ||
55 | checkVideoStudioConfig() | ||
56 | } | ||
57 | |||
58 | // We get db by param to not import it in this file (import orders) | ||
59 | async function clientsExist () { | ||
60 | const totalClients = await OAuthClientModel.countTotal() | ||
61 | |||
62 | return totalClients !== 0 | ||
63 | } | ||
64 | |||
65 | // We get db by param to not import it in this file (import orders) | ||
66 | async function usersExist () { | ||
67 | const totalUsers = await UserModel.countTotal() | ||
68 | |||
69 | return totalUsers !== 0 | ||
70 | } | ||
71 | |||
72 | // We get db by param to not import it in this file (import orders) | ||
73 | async function applicationExist () { | ||
74 | const totalApplication = await ApplicationModel.countTotal() | ||
75 | |||
76 | return totalApplication !== 0 | ||
77 | } | ||
78 | |||
79 | async function checkFFmpegVersion () { | ||
80 | const version = await getFFmpegVersion() | ||
81 | const { major, minor, patch } = parseSemVersion(version) | ||
82 | |||
83 | if (major < 4 || (major === 4 && minor < 1)) { | ||
84 | logger.warn('Your ffmpeg version (%s) is outdated. PeerTube supports ffmpeg >= 4.1. Please upgrade ffmpeg.', version) | ||
85 | } | ||
86 | |||
87 | if (major === 4 && minor === 4 && patch === 0) { | ||
88 | logger.warn('There is a bug in ffmpeg 4.4.0 with HLS videos. Please upgrade ffmpeg.') | ||
89 | } | ||
90 | } | ||
91 | |||
92 | // --------------------------------------------------------------------------- | ||
93 | |||
94 | export { | ||
95 | checkConfig, | ||
96 | clientsExist, | ||
97 | checkFFmpegVersion, | ||
98 | usersExist, | ||
99 | applicationExist, | ||
100 | checkActivityPubUrls | ||
101 | } | ||
102 | |||
103 | // --------------------------------------------------------------------------- | ||
104 | |||
105 | function checkRemovedConfigKeys () { | ||
106 | // Moved configuration keys | ||
107 | if (config.has('services.csp-logger')) { | ||
108 | logger.warn('services.csp-logger configuration has been renamed to csp.report_uri. Please update your configuration file.') | ||
109 | } | ||
110 | |||
111 | if (config.has('transcoding.webtorrent.enabled')) { | ||
112 | const localConfigPath = getLocalConfigFilePath() | ||
113 | |||
114 | const content = readFileSync(localConfigPath, { encoding: 'utf-8' }) | ||
115 | if (!content.includes('"webtorrent"')) { | ||
116 | throw new Error('Please rename transcoding.webtorrent.enabled key to transcoding.web_videos.enabled in your configuration file') | ||
117 | } | ||
118 | |||
119 | try { | ||
120 | logger.info( | ||
121 | 'Replacing "transcoding.webtorrent.enabled" key to "transcoding.web_videos.enabled" in your local configuration ' + localConfigPath | ||
122 | ) | ||
123 | |||
124 | writeFileSync(localConfigPath, content.replace('"webtorrent"', '"web_videos"'), { encoding: 'utf-8' }) | ||
125 | |||
126 | reloadConfig() | ||
127 | } catch (err) { | ||
128 | logger.error('Cannot write new configuration to file ' + localConfigPath, { err }) | ||
129 | } | ||
130 | } | ||
131 | } | ||
132 | |||
133 | function checkSecretsConfig () { | ||
134 | if (!CONFIG.SECRETS.PEERTUBE) { | ||
135 | throw new Error('secrets.peertube is missing in config. Generate one using `openssl rand -hex 32`') | ||
136 | } | ||
137 | } | ||
138 | |||
139 | function checkEmailConfig () { | ||
140 | if (!isEmailEnabled()) { | ||
141 | if (CONFIG.SIGNUP.ENABLED && CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) { | ||
142 | throw new Error('SMTP is not configured but you require signup email verification.') | ||
143 | } | ||
144 | |||
145 | if (CONFIG.SIGNUP.ENABLED && CONFIG.SIGNUP.REQUIRES_APPROVAL) { | ||
146 | // eslint-disable-next-line max-len | ||
147 | logger.warn('SMTP is not configured but signup approval is enabled: PeerTube will not be able to send an email to the user upon acceptance/rejection of the registration request') | ||
148 | } | ||
149 | |||
150 | if (CONFIG.CONTACT_FORM.ENABLED) { | ||
151 | logger.warn('SMTP is not configured so the contact form will not work.') | ||
152 | } | ||
153 | } | ||
154 | } | ||
155 | |||
156 | function checkNSFWPolicyConfig () { | ||
157 | const defaultNSFWPolicy = CONFIG.INSTANCE.DEFAULT_NSFW_POLICY | ||
158 | |||
159 | const available = [ 'do_not_list', 'blur', 'display' ] | ||
160 | if (available.includes(defaultNSFWPolicy) === false) { | ||
161 | throw new Error('NSFW policy setting should be ' + available.join(' or ') + ' instead of ' + defaultNSFWPolicy) | ||
162 | } | ||
163 | } | ||
164 | |||
165 | function checkLocalRedundancyConfig () { | ||
166 | const redundancyVideos = CONFIG.REDUNDANCY.VIDEOS.STRATEGIES | ||
167 | |||
168 | if (isArray(redundancyVideos)) { | ||
169 | const available = [ 'most-views', 'trending', 'recently-added' ] | ||
170 | |||
171 | for (const r of redundancyVideos) { | ||
172 | if (available.includes(r.strategy) === false) { | ||
173 | throw new Error('Videos redundancy should have ' + available.join(' or ') + ' strategy instead of ' + r.strategy) | ||
174 | } | ||
175 | |||
176 | // Lifetime should not be < 10 hours | ||
177 | if (isProdInstance() && r.minLifetime < 1000 * 3600 * 10) { | ||
178 | throw new Error('Video redundancy minimum lifetime should be >= 10 hours for strategy ' + r.strategy) | ||
179 | } | ||
180 | } | ||
181 | |||
182 | const filtered = uniqify(redundancyVideos.map(r => r.strategy)) | ||
183 | if (filtered.length !== redundancyVideos.length) { | ||
184 | throw new Error('Redundancy video entries should have unique strategies') | ||
185 | } | ||
186 | |||
187 | const recentlyAddedStrategy = redundancyVideos.find(r => r.strategy === 'recently-added') as RecentlyAddedStrategy | ||
188 | if (recentlyAddedStrategy && isNaN(recentlyAddedStrategy.minViews)) { | ||
189 | throw new Error('Min views in recently added strategy is not a number') | ||
190 | } | ||
191 | } else { | ||
192 | throw new Error('Videos redundancy should be an array (you must uncomment lines containing - too)') | ||
193 | } | ||
194 | } | ||
195 | |||
196 | function checkRemoteRedundancyConfig () { | ||
197 | const acceptFrom = CONFIG.REMOTE_REDUNDANCY.VIDEOS.ACCEPT_FROM | ||
198 | const acceptFromValues = new Set<VideoRedundancyConfigFilter>([ 'nobody', 'anybody', 'followings' ]) | ||
199 | |||
200 | if (acceptFromValues.has(acceptFrom) === false) { | ||
201 | throw new Error('remote_redundancy.videos.accept_from has an incorrect value') | ||
202 | } | ||
203 | } | ||
204 | |||
205 | function checkStorageConfig () { | ||
206 | // Check storage directory locations | ||
207 | if (isProdInstance()) { | ||
208 | const configStorage = config.get<{ [ name: string ]: string }>('storage') | ||
209 | |||
210 | for (const key of Object.keys(configStorage)) { | ||
211 | if (configStorage[key].startsWith('storage/')) { | ||
212 | logger.warn( | ||
213 | 'Directory of %s should not be in the production directory of PeerTube. Please check your production configuration file.', | ||
214 | key | ||
215 | ) | ||
216 | } | ||
217 | } | ||
218 | } | ||
219 | |||
220 | if (CONFIG.STORAGE.WEB_VIDEOS_DIR === CONFIG.STORAGE.REDUNDANCY_DIR) { | ||
221 | logger.warn('Redundancy directory should be different than the videos folder.') | ||
222 | } | ||
223 | } | ||
224 | |||
225 | function checkTranscodingConfig () { | ||
226 | if (CONFIG.TRANSCODING.ENABLED) { | ||
227 | if (CONFIG.TRANSCODING.WEB_VIDEOS.ENABLED === false && CONFIG.TRANSCODING.HLS.ENABLED === false) { | ||
228 | throw new Error('You need to enable at least Web Video transcoding or HLS transcoding.') | ||
229 | } | ||
230 | |||
231 | if (CONFIG.TRANSCODING.CONCURRENCY <= 0) { | ||
232 | throw new Error('Transcoding concurrency should be > 0') | ||
233 | } | ||
234 | } | ||
235 | |||
236 | if (CONFIG.IMPORT.VIDEOS.HTTP.ENABLED || CONFIG.IMPORT.VIDEOS.TORRENT.ENABLED) { | ||
237 | if (CONFIG.IMPORT.VIDEOS.CONCURRENCY <= 0) { | ||
238 | throw new Error('Video import concurrency should be > 0') | ||
239 | } | ||
240 | } | ||
241 | } | ||
242 | |||
243 | function checkImportConfig () { | ||
244 | if (CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED && !CONFIG.IMPORT.VIDEOS.HTTP) { | ||
245 | throw new Error('You need to enable HTTP import to allow synchronization') | ||
246 | } | ||
247 | } | ||
248 | |||
249 | function checkBroadcastMessageConfig () { | ||
250 | if (CONFIG.BROADCAST_MESSAGE.ENABLED) { | ||
251 | const currentLevel = CONFIG.BROADCAST_MESSAGE.LEVEL | ||
252 | const available = [ 'info', 'warning', 'error' ] | ||
253 | |||
254 | if (available.includes(currentLevel) === false) { | ||
255 | throw new Error('Broadcast message level should be ' + available.join(' or ') + ' instead of ' + currentLevel) | ||
256 | } | ||
257 | } | ||
258 | } | ||
259 | |||
260 | function checkSearchConfig () { | ||
261 | if (CONFIG.SEARCH.SEARCH_INDEX.ENABLED === true) { | ||
262 | if (CONFIG.SEARCH.REMOTE_URI.USERS === false) { | ||
263 | throw new Error('You cannot enable search index without enabling remote URI search for users.') | ||
264 | } | ||
265 | } | ||
266 | } | ||
267 | |||
268 | function checkLiveConfig () { | ||
269 | if (CONFIG.LIVE.ENABLED === true) { | ||
270 | if (CONFIG.LIVE.ALLOW_REPLAY === true && CONFIG.TRANSCODING.ENABLED === false) { | ||
271 | throw new Error('Live allow replay cannot be enabled if transcoding is not enabled.') | ||
272 | } | ||
273 | |||
274 | if (CONFIG.LIVE.RTMP.ENABLED === false && CONFIG.LIVE.RTMPS.ENABLED === false) { | ||
275 | throw new Error('You must enable at least RTMP or RTMPS') | ||
276 | } | ||
277 | |||
278 | if (CONFIG.LIVE.RTMPS.ENABLED) { | ||
279 | if (!CONFIG.LIVE.RTMPS.KEY_FILE) { | ||
280 | throw new Error('You must specify a key file to enable RTMPS') | ||
281 | } | ||
282 | |||
283 | if (!CONFIG.LIVE.RTMPS.CERT_FILE) { | ||
284 | throw new Error('You must specify a cert file to enable RTMPS') | ||
285 | } | ||
286 | } | ||
287 | } | ||
288 | } | ||
289 | |||
290 | function checkObjectStorageConfig () { | ||
291 | if (CONFIG.OBJECT_STORAGE.ENABLED === true) { | ||
292 | |||
293 | if (!CONFIG.OBJECT_STORAGE.WEB_VIDEOS.BUCKET_NAME) { | ||
294 | throw new Error('videos_bucket should be set when object storage support is enabled.') | ||
295 | } | ||
296 | |||
297 | if (!CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME) { | ||
298 | throw new Error('streaming_playlists_bucket should be set when object storage support is enabled.') | ||
299 | } | ||
300 | |||
301 | if ( | ||
302 | CONFIG.OBJECT_STORAGE.WEB_VIDEOS.BUCKET_NAME === CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.BUCKET_NAME && | ||
303 | CONFIG.OBJECT_STORAGE.WEB_VIDEOS.PREFIX === CONFIG.OBJECT_STORAGE.STREAMING_PLAYLISTS.PREFIX | ||
304 | ) { | ||
305 | if (CONFIG.OBJECT_STORAGE.WEB_VIDEOS.PREFIX === '') { | ||
306 | throw new Error('Object storage bucket prefixes should be set when the same bucket is used for both types of video.') | ||
307 | } | ||
308 | |||
309 | throw new Error( | ||
310 | 'Object storage bucket prefixes should be set to different values when the same bucket is used for both types of video.' | ||
311 | ) | ||
312 | } | ||
313 | |||
314 | if (CONFIG.OBJECT_STORAGE.MAX_UPLOAD_PART > parseBytes('250MB')) { | ||
315 | // eslint-disable-next-line max-len | ||
316 | logger.warn(`Object storage max upload part seems to have a big value (${CONFIG.OBJECT_STORAGE.MAX_UPLOAD_PART} bytes). Consider using a lower one (like 100MB).`) | ||
317 | } | ||
318 | } | ||
319 | } | ||
320 | |||
321 | function checkVideoStudioConfig () { | ||
322 | if (CONFIG.VIDEO_STUDIO.ENABLED === true && CONFIG.TRANSCODING.ENABLED === false) { | ||
323 | throw new Error('Video studio cannot be enabled if transcoding is disabled') | ||
324 | } | ||
325 | } | ||