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/scripts/migrations/peertube-4.0.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/scripts/migrations/peertube-4.0.ts')
-rw-r--r-- | server/scripts/migrations/peertube-4.0.ts | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/server/scripts/migrations/peertube-4.0.ts b/server/scripts/migrations/peertube-4.0.ts new file mode 100644 index 000000000..619c1da71 --- /dev/null +++ b/server/scripts/migrations/peertube-4.0.ts | |||
@@ -0,0 +1,110 @@ | |||
1 | import Bluebird from 'bluebird' | ||
2 | import { move } from 'fs-extra/esm' | ||
3 | import { readFile, writeFile } from 'fs/promises' | ||
4 | import { join } from 'path' | ||
5 | import { initDatabaseModels } from '@server/initializers/database.js' | ||
6 | import { federateVideoIfNeeded } from '@server/lib/activitypub/videos/index.js' | ||
7 | import { JobQueue } from '@server/lib/job-queue/index.js' | ||
8 | import { | ||
9 | generateHLSMasterPlaylistFilename, | ||
10 | generateHlsSha256SegmentsFilename, | ||
11 | getHlsResolutionPlaylistFilename | ||
12 | } from '@server/lib/paths.js' | ||
13 | import { VideoPathManager } from '@server/lib/video-path-manager.js' | ||
14 | import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist.js' | ||
15 | import { VideoModel } from '@server/models/video/video.js' | ||
16 | |||
17 | run() | ||
18 | .then(() => process.exit(0)) | ||
19 | .catch(err => { | ||
20 | console.error(err) | ||
21 | process.exit(-1) | ||
22 | |||
23 | }) | ||
24 | |||
25 | async function run () { | ||
26 | console.log('Migrate old HLS paths to new format.') | ||
27 | |||
28 | await initDatabaseModels(true) | ||
29 | |||
30 | JobQueue.Instance.init() | ||
31 | |||
32 | const ids = await VideoModel.listLocalIds() | ||
33 | |||
34 | await Bluebird.map(ids, async id => { | ||
35 | try { | ||
36 | await processVideo(id) | ||
37 | } catch (err) { | ||
38 | console.error('Cannot process video %s.', { err }) | ||
39 | } | ||
40 | }, { concurrency: 5 }) | ||
41 | |||
42 | console.log('Migration finished!') | ||
43 | } | ||
44 | |||
45 | async function processVideo (videoId: number) { | ||
46 | const video = await VideoModel.loadWithFiles(videoId) | ||
47 | |||
48 | const hls = video.getHLSPlaylist() | ||
49 | if (video.isLive || !hls || hls.playlistFilename !== 'master.m3u8' || hls.VideoFiles.length === 0) { | ||
50 | return | ||
51 | } | ||
52 | |||
53 | console.log(`Renaming HLS playlist files of video ${video.name}.`) | ||
54 | |||
55 | const playlist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id) | ||
56 | const hlsDirPath = VideoPathManager.Instance.getFSHLSOutputPath(video) | ||
57 | |||
58 | const masterPlaylistPath = join(hlsDirPath, playlist.playlistFilename) | ||
59 | let masterPlaylistContent = await readFile(masterPlaylistPath, 'utf8') | ||
60 | |||
61 | for (const videoFile of hls.VideoFiles) { | ||
62 | const srcName = `${videoFile.resolution}.m3u8` | ||
63 | const dstName = getHlsResolutionPlaylistFilename(videoFile.filename) | ||
64 | |||
65 | const src = join(hlsDirPath, srcName) | ||
66 | const dst = join(hlsDirPath, dstName) | ||
67 | |||
68 | try { | ||
69 | await move(src, dst) | ||
70 | |||
71 | masterPlaylistContent = masterPlaylistContent.replace(new RegExp('^' + srcName + '$', 'm'), dstName) | ||
72 | } catch (err) { | ||
73 | console.error('Cannot move video file %s to %s.', src, dst, err) | ||
74 | } | ||
75 | } | ||
76 | |||
77 | await writeFile(masterPlaylistPath, masterPlaylistContent) | ||
78 | |||
79 | if (playlist.segmentsSha256Filename === 'segments-sha256.json') { | ||
80 | try { | ||
81 | const newName = generateHlsSha256SegmentsFilename(video.isLive) | ||
82 | |||
83 | const dst = join(hlsDirPath, newName) | ||
84 | await move(join(hlsDirPath, playlist.segmentsSha256Filename), dst) | ||
85 | playlist.segmentsSha256Filename = newName | ||
86 | } catch (err) { | ||
87 | console.error(`Cannot rename ${video.name} segments-sha256.json file to a new name`, err) | ||
88 | } | ||
89 | } | ||
90 | |||
91 | if (playlist.playlistFilename === 'master.m3u8') { | ||
92 | try { | ||
93 | const newName = generateHLSMasterPlaylistFilename(video.isLive) | ||
94 | |||
95 | const dst = join(hlsDirPath, newName) | ||
96 | await move(join(hlsDirPath, playlist.playlistFilename), dst) | ||
97 | playlist.playlistFilename = newName | ||
98 | } catch (err) { | ||
99 | console.error(`Cannot rename ${video.name} master.m3u8 file to a new name`, err) | ||
100 | } | ||
101 | } | ||
102 | |||
103 | // Everything worked, we can save the playlist now | ||
104 | await playlist.save() | ||
105 | |||
106 | const allVideo = await VideoModel.loadFull(video.id) | ||
107 | await federateVideoIfNeeded(allVideo, false) | ||
108 | |||
109 | console.log(`Successfully moved HLS files of ${video.name}.`) | ||
110 | } | ||