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/lib/live/live-utils.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/lib/live/live-utils.ts')
-rw-r--r-- | server/lib/live/live-utils.ts | 99 |
1 files changed, 0 insertions, 99 deletions
diff --git a/server/lib/live/live-utils.ts b/server/lib/live/live-utils.ts deleted file mode 100644 index 3fb3ce1ce..000000000 --- a/server/lib/live/live-utils.ts +++ /dev/null | |||
@@ -1,99 +0,0 @@ | |||
1 | import { pathExists, readdir, remove } from 'fs-extra' | ||
2 | import { basename, join } from 'path' | ||
3 | import { logger } from '@server/helpers/logger' | ||
4 | import { VIDEO_LIVE } from '@server/initializers/constants' | ||
5 | import { MStreamingPlaylist, MStreamingPlaylistVideo, MVideo } from '@server/types/models' | ||
6 | import { LiveVideoLatencyMode, VideoStorage } from '@shared/models' | ||
7 | import { listHLSFileKeysOf, removeHLSFileObjectStorageByFullKey, removeHLSObjectStorage } from '../object-storage' | ||
8 | import { getLiveDirectory } from '../paths' | ||
9 | |||
10 | function buildConcatenatedName (segmentOrPlaylistPath: string) { | ||
11 | const num = basename(segmentOrPlaylistPath).match(/^(\d+)(-|\.)/) | ||
12 | |||
13 | return 'concat-' + num[1] + '.ts' | ||
14 | } | ||
15 | |||
16 | async function cleanupAndDestroyPermanentLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { | ||
17 | await cleanupTMPLiveFiles(video, streamingPlaylist) | ||
18 | |||
19 | await streamingPlaylist.destroy() | ||
20 | } | ||
21 | |||
22 | async function cleanupUnsavedNormalLive (video: MVideo, streamingPlaylist: MStreamingPlaylist) { | ||
23 | const hlsDirectory = getLiveDirectory(video) | ||
24 | |||
25 | // We uploaded files to object storage too, remove them | ||
26 | if (streamingPlaylist.storage === VideoStorage.OBJECT_STORAGE) { | ||
27 | await removeHLSObjectStorage(streamingPlaylist.withVideo(video)) | ||
28 | } | ||
29 | |||
30 | await remove(hlsDirectory) | ||
31 | |||
32 | await streamingPlaylist.destroy() | ||
33 | } | ||
34 | |||
35 | async function cleanupTMPLiveFiles (video: MVideo, streamingPlaylist: MStreamingPlaylist) { | ||
36 | await cleanupTMPLiveFilesFromObjectStorage(streamingPlaylist.withVideo(video)) | ||
37 | |||
38 | await cleanupTMPLiveFilesFromFilesystem(video) | ||
39 | } | ||
40 | |||
41 | function getLiveSegmentTime (latencyMode: LiveVideoLatencyMode) { | ||
42 | if (latencyMode === LiveVideoLatencyMode.SMALL_LATENCY) { | ||
43 | return VIDEO_LIVE.SEGMENT_TIME_SECONDS.SMALL_LATENCY | ||
44 | } | ||
45 | |||
46 | return VIDEO_LIVE.SEGMENT_TIME_SECONDS.DEFAULT_LATENCY | ||
47 | } | ||
48 | |||
49 | export { | ||
50 | cleanupAndDestroyPermanentLive, | ||
51 | cleanupUnsavedNormalLive, | ||
52 | cleanupTMPLiveFiles, | ||
53 | getLiveSegmentTime, | ||
54 | buildConcatenatedName | ||
55 | } | ||
56 | |||
57 | // --------------------------------------------------------------------------- | ||
58 | |||
59 | function isTMPLiveFile (name: string) { | ||
60 | return name.endsWith('.ts') || | ||
61 | name.endsWith('.m3u8') || | ||
62 | name.endsWith('.json') || | ||
63 | name.endsWith('.mpd') || | ||
64 | name.endsWith('.m4s') || | ||
65 | name.endsWith('.tmp') | ||
66 | } | ||
67 | |||
68 | async function cleanupTMPLiveFilesFromFilesystem (video: MVideo) { | ||
69 | const hlsDirectory = getLiveDirectory(video) | ||
70 | |||
71 | if (!await pathExists(hlsDirectory)) return | ||
72 | |||
73 | logger.info('Cleanup TMP live files from filesystem of %s.', hlsDirectory) | ||
74 | |||
75 | const files = await readdir(hlsDirectory) | ||
76 | |||
77 | for (const filename of files) { | ||
78 | if (isTMPLiveFile(filename)) { | ||
79 | const p = join(hlsDirectory, filename) | ||
80 | |||
81 | remove(p) | ||
82 | .catch(err => logger.error('Cannot remove %s.', p, { err })) | ||
83 | } | ||
84 | } | ||
85 | } | ||
86 | |||
87 | async function cleanupTMPLiveFilesFromObjectStorage (streamingPlaylist: MStreamingPlaylistVideo) { | ||
88 | if (streamingPlaylist.storage !== VideoStorage.OBJECT_STORAGE) return | ||
89 | |||
90 | logger.info('Cleanup TMP live files from object storage for %s.', streamingPlaylist.Video.uuid) | ||
91 | |||
92 | const keys = await listHLSFileKeysOf(streamingPlaylist) | ||
93 | |||
94 | for (const key of keys) { | ||
95 | if (isTMPLiveFile(key)) { | ||
96 | await removeHLSFileObjectStorageByFullKey(key) | ||
97 | } | ||
98 | } | ||
99 | } | ||