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/tests/shared/peertube-runner-process.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/tests/shared/peertube-runner-process.ts')
-rw-r--r-- | server/tests/shared/peertube-runner-process.ts | 98 |
1 files changed, 0 insertions, 98 deletions
diff --git a/server/tests/shared/peertube-runner-process.ts b/server/tests/shared/peertube-runner-process.ts deleted file mode 100644 index 9304ebcc8..000000000 --- a/server/tests/shared/peertube-runner-process.ts +++ /dev/null | |||
@@ -1,98 +0,0 @@ | |||
1 | import { ChildProcess, fork } from 'child_process' | ||
2 | import execa from 'execa' | ||
3 | import { join } from 'path' | ||
4 | import { root } from '@shared/core-utils' | ||
5 | import { PeerTubeServer } from '@shared/server-commands' | ||
6 | |||
7 | export class PeerTubeRunnerProcess { | ||
8 | private app?: ChildProcess | ||
9 | |||
10 | constructor (private readonly server: PeerTubeServer) { | ||
11 | |||
12 | } | ||
13 | |||
14 | runServer (options: { | ||
15 | hideLogs?: boolean // default true | ||
16 | } = {}) { | ||
17 | const { hideLogs = true } = options | ||
18 | |||
19 | return new Promise<void>((res, rej) => { | ||
20 | const args = [ 'server', '--verbose', ...this.buildIdArg() ] | ||
21 | |||
22 | const forkOptions = { | ||
23 | detached: false, | ||
24 | silent: true | ||
25 | } | ||
26 | this.app = fork(this.getRunnerPath(), args, forkOptions) | ||
27 | |||
28 | this.app.stdout.on('data', data => { | ||
29 | const str = data.toString() as string | ||
30 | |||
31 | if (!hideLogs) { | ||
32 | console.log(str) | ||
33 | } | ||
34 | }) | ||
35 | |||
36 | res() | ||
37 | }) | ||
38 | } | ||
39 | |||
40 | registerPeerTubeInstance (options: { | ||
41 | registrationToken: string | ||
42 | runnerName: string | ||
43 | runnerDescription?: string | ||
44 | }) { | ||
45 | const { registrationToken, runnerName, runnerDescription } = options | ||
46 | |||
47 | const args = [ | ||
48 | 'register', | ||
49 | '--url', this.server.url, | ||
50 | '--registration-token', registrationToken, | ||
51 | '--runner-name', runnerName, | ||
52 | ...this.buildIdArg() | ||
53 | ] | ||
54 | |||
55 | if (runnerDescription) { | ||
56 | args.push('--runner-description') | ||
57 | args.push(runnerDescription) | ||
58 | } | ||
59 | |||
60 | return execa.node(this.getRunnerPath(), args) | ||
61 | } | ||
62 | |||
63 | unregisterPeerTubeInstance (options: { | ||
64 | runnerName: string | ||
65 | }) { | ||
66 | const { runnerName } = options | ||
67 | |||
68 | const args = [ 'unregister', '--url', this.server.url, '--runner-name', runnerName, ...this.buildIdArg() ] | ||
69 | return execa.node(this.getRunnerPath(), args) | ||
70 | } | ||
71 | |||
72 | async listRegisteredPeerTubeInstances () { | ||
73 | const args = [ 'list-registered', ...this.buildIdArg() ] | ||
74 | const { stdout } = await execa.node(this.getRunnerPath(), args) | ||
75 | |||
76 | return stdout | ||
77 | } | ||
78 | |||
79 | kill () { | ||
80 | if (!this.app) return | ||
81 | |||
82 | process.kill(this.app.pid) | ||
83 | |||
84 | this.app = null | ||
85 | } | ||
86 | |||
87 | getId () { | ||
88 | return 'test-' + this.server.internalServerNumber | ||
89 | } | ||
90 | |||
91 | private getRunnerPath () { | ||
92 | return join(root(), 'packages', 'peertube-runner', 'dist', 'peertube-runner.js') | ||
93 | } | ||
94 | |||
95 | private buildIdArg () { | ||
96 | return [ '--id', this.getId() ] | ||
97 | } | ||
98 | } | ||