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 /packages/tests/src/shared/live.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 'packages/tests/src/shared/live.ts')
-rw-r--r-- | packages/tests/src/shared/live.ts | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/packages/tests/src/shared/live.ts b/packages/tests/src/shared/live.ts new file mode 100644 index 000000000..9c7991b0d --- /dev/null +++ b/packages/tests/src/shared/live.ts | |||
@@ -0,0 +1,186 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { expect } from 'chai' | ||
4 | import { pathExists } from 'fs-extra/esm' | ||
5 | import { readdir } from 'fs/promises' | ||
6 | import { join } from 'path' | ||
7 | import { sha1 } from '@peertube/peertube-node-utils' | ||
8 | import { LiveVideo, VideoStreamingPlaylistType } from '@peertube/peertube-models' | ||
9 | import { ObjectStorageCommand, PeerTubeServer } from '@peertube/peertube-server-commands' | ||
10 | import { SQLCommand } from './sql-command.js' | ||
11 | import { checkLiveSegmentHash, checkResolutionsInMasterPlaylist } from './streaming-playlists.js' | ||
12 | |||
13 | async function checkLiveCleanup (options: { | ||
14 | server: PeerTubeServer | ||
15 | videoUUID: string | ||
16 | permanent: boolean | ||
17 | savedResolutions?: number[] | ||
18 | }) { | ||
19 | const { server, videoUUID, permanent, savedResolutions = [] } = options | ||
20 | |||
21 | const basePath = server.servers.buildDirectory('streaming-playlists') | ||
22 | const hlsPath = join(basePath, 'hls', videoUUID) | ||
23 | |||
24 | if (permanent) { | ||
25 | if (!await pathExists(hlsPath)) return | ||
26 | |||
27 | const files = await readdir(hlsPath) | ||
28 | expect(files).to.have.lengthOf(0) | ||
29 | return | ||
30 | } | ||
31 | |||
32 | if (savedResolutions.length === 0) { | ||
33 | return checkUnsavedLiveCleanup(server, videoUUID, hlsPath) | ||
34 | } | ||
35 | |||
36 | return checkSavedLiveCleanup(hlsPath, savedResolutions) | ||
37 | } | ||
38 | |||
39 | // --------------------------------------------------------------------------- | ||
40 | |||
41 | async function testLiveVideoResolutions (options: { | ||
42 | sqlCommand: SQLCommand | ||
43 | originServer: PeerTubeServer | ||
44 | |||
45 | servers: PeerTubeServer[] | ||
46 | liveVideoId: string | ||
47 | resolutions: number[] | ||
48 | transcoded: boolean | ||
49 | |||
50 | objectStorage?: ObjectStorageCommand | ||
51 | objectStorageBaseUrl?: string | ||
52 | }) { | ||
53 | const { | ||
54 | originServer, | ||
55 | sqlCommand, | ||
56 | servers, | ||
57 | liveVideoId, | ||
58 | resolutions, | ||
59 | transcoded, | ||
60 | objectStorage, | ||
61 | objectStorageBaseUrl = objectStorage?.getMockPlaylistBaseUrl() | ||
62 | } = options | ||
63 | |||
64 | for (const server of servers) { | ||
65 | const { data } = await server.videos.list() | ||
66 | expect(data.find(v => v.uuid === liveVideoId)).to.exist | ||
67 | |||
68 | const video = await server.videos.get({ id: liveVideoId }) | ||
69 | expect(video.streamingPlaylists).to.have.lengthOf(1) | ||
70 | |||
71 | const hlsPlaylist = video.streamingPlaylists.find(s => s.type === VideoStreamingPlaylistType.HLS) | ||
72 | expect(hlsPlaylist).to.exist | ||
73 | expect(hlsPlaylist.files).to.have.lengthOf(0) // Only fragmented mp4 files are displayed | ||
74 | |||
75 | await checkResolutionsInMasterPlaylist({ | ||
76 | server, | ||
77 | playlistUrl: hlsPlaylist.playlistUrl, | ||
78 | resolutions, | ||
79 | transcoded, | ||
80 | withRetry: !!objectStorage | ||
81 | }) | ||
82 | |||
83 | if (objectStorage) { | ||
84 | expect(hlsPlaylist.playlistUrl).to.contain(objectStorageBaseUrl) | ||
85 | } | ||
86 | |||
87 | for (let i = 0; i < resolutions.length; i++) { | ||
88 | const segmentNum = 3 | ||
89 | const segmentName = `${i}-00000${segmentNum}.ts` | ||
90 | await originServer.live.waitUntilSegmentGeneration({ | ||
91 | server: originServer, | ||
92 | videoUUID: video.uuid, | ||
93 | playlistNumber: i, | ||
94 | segment: segmentNum, | ||
95 | objectStorage, | ||
96 | objectStorageBaseUrl | ||
97 | }) | ||
98 | |||
99 | const baseUrl = objectStorage | ||
100 | ? join(objectStorageBaseUrl, 'hls') | ||
101 | : originServer.url + '/static/streaming-playlists/hls' | ||
102 | |||
103 | if (objectStorage) { | ||
104 | expect(hlsPlaylist.segmentsSha256Url).to.contain(objectStorageBaseUrl) | ||
105 | } | ||
106 | |||
107 | const subPlaylist = await originServer.streamingPlaylists.get({ | ||
108 | url: `${baseUrl}/${video.uuid}/${i}.m3u8`, | ||
109 | withRetry: !!objectStorage // With object storage, the request may fail because of inconsistent data in S3 | ||
110 | }) | ||
111 | |||
112 | expect(subPlaylist).to.contain(segmentName) | ||
113 | |||
114 | await checkLiveSegmentHash({ | ||
115 | server, | ||
116 | baseUrlSegment: baseUrl, | ||
117 | videoUUID: video.uuid, | ||
118 | segmentName, | ||
119 | hlsPlaylist, | ||
120 | withRetry: !!objectStorage // With object storage, the request may fail because of inconsistent data in S3 | ||
121 | }) | ||
122 | |||
123 | if (originServer.internalServerNumber === server.internalServerNumber) { | ||
124 | const infohash = sha1(`${2 + hlsPlaylist.playlistUrl}+V${i}`) | ||
125 | const dbInfohashes = await sqlCommand.getPlaylistInfohash(hlsPlaylist.id) | ||
126 | |||
127 | expect(dbInfohashes).to.include(infohash) | ||
128 | } | ||
129 | } | ||
130 | } | ||
131 | } | ||
132 | |||
133 | // --------------------------------------------------------------------------- | ||
134 | |||
135 | export { | ||
136 | checkLiveCleanup, | ||
137 | testLiveVideoResolutions | ||
138 | } | ||
139 | |||
140 | // --------------------------------------------------------------------------- | ||
141 | |||
142 | async function checkSavedLiveCleanup (hlsPath: string, savedResolutions: number[] = []) { | ||
143 | const files = await readdir(hlsPath) | ||
144 | |||
145 | // fragmented file and playlist per resolution + master playlist + segments sha256 json file | ||
146 | expect(files, `Directory content: ${files.join(', ')}`).to.have.lengthOf(savedResolutions.length * 2 + 2) | ||
147 | |||
148 | for (const resolution of savedResolutions) { | ||
149 | const fragmentedFile = files.find(f => f.endsWith(`-${resolution}-fragmented.mp4`)) | ||
150 | expect(fragmentedFile).to.exist | ||
151 | |||
152 | const playlistFile = files.find(f => f.endsWith(`${resolution}.m3u8`)) | ||
153 | expect(playlistFile).to.exist | ||
154 | } | ||
155 | |||
156 | const masterPlaylistFile = files.find(f => f.endsWith('-master.m3u8')) | ||
157 | expect(masterPlaylistFile).to.exist | ||
158 | |||
159 | const shaFile = files.find(f => f.endsWith('-segments-sha256.json')) | ||
160 | expect(shaFile).to.exist | ||
161 | } | ||
162 | |||
163 | async function checkUnsavedLiveCleanup (server: PeerTubeServer, videoUUID: string, hlsPath: string) { | ||
164 | let live: LiveVideo | ||
165 | |||
166 | try { | ||
167 | live = await server.live.get({ videoId: videoUUID }) | ||
168 | } catch {} | ||
169 | |||
170 | if (live?.permanentLive) { | ||
171 | expect(await pathExists(hlsPath)).to.be.true | ||
172 | |||
173 | const hlsFiles = await readdir(hlsPath) | ||
174 | expect(hlsFiles).to.have.lengthOf(1) // Only replays directory | ||
175 | |||
176 | const replayDir = join(hlsPath, 'replay') | ||
177 | expect(await pathExists(replayDir)).to.be.true | ||
178 | |||
179 | const replayFiles = await readdir(join(hlsPath, 'replay')) | ||
180 | expect(replayFiles).to.have.lengthOf(0) | ||
181 | |||
182 | return | ||
183 | } | ||
184 | |||
185 | expect(await pathExists(hlsPath)).to.be.false | ||
186 | } | ||