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/server-commands/src/runners/runners-command.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/server-commands/src/runners/runners-command.ts')
-rw-r--r-- | packages/server-commands/src/runners/runners-command.ts | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/packages/server-commands/src/runners/runners-command.ts b/packages/server-commands/src/runners/runners-command.ts new file mode 100644 index 000000000..376a1dff9 --- /dev/null +++ b/packages/server-commands/src/runners/runners-command.ts | |||
@@ -0,0 +1,85 @@ | |||
1 | import { pick } from '@peertube/peertube-core-utils' | ||
2 | import { | ||
3 | HttpStatusCode, | ||
4 | RegisterRunnerBody, | ||
5 | RegisterRunnerResult, | ||
6 | ResultList, | ||
7 | Runner, | ||
8 | UnregisterRunnerBody | ||
9 | } from '@peertube/peertube-models' | ||
10 | import { buildUUID } from '@peertube/peertube-node-utils' | ||
11 | import { unwrapBody } from '../requests/index.js' | ||
12 | import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js' | ||
13 | |||
14 | export class RunnersCommand extends AbstractCommand { | ||
15 | |||
16 | list (options: OverrideCommandOptions & { | ||
17 | start?: number | ||
18 | count?: number | ||
19 | sort?: string | ||
20 | } = {}) { | ||
21 | const path = '/api/v1/runners' | ||
22 | |||
23 | return this.getRequestBody<ResultList<Runner>>({ | ||
24 | ...options, | ||
25 | |||
26 | path, | ||
27 | query: pick(options, [ 'start', 'count', 'sort' ]), | ||
28 | implicitToken: true, | ||
29 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
30 | }) | ||
31 | } | ||
32 | |||
33 | register (options: OverrideCommandOptions & RegisterRunnerBody) { | ||
34 | const path = '/api/v1/runners/register' | ||
35 | |||
36 | return unwrapBody<RegisterRunnerResult>(this.postBodyRequest({ | ||
37 | ...options, | ||
38 | |||
39 | path, | ||
40 | fields: pick(options, [ 'name', 'registrationToken', 'description' ]), | ||
41 | implicitToken: true, | ||
42 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
43 | })) | ||
44 | } | ||
45 | |||
46 | unregister (options: OverrideCommandOptions & UnregisterRunnerBody) { | ||
47 | const path = '/api/v1/runners/unregister' | ||
48 | |||
49 | return this.postBodyRequest({ | ||
50 | ...options, | ||
51 | |||
52 | path, | ||
53 | fields: pick(options, [ 'runnerToken' ]), | ||
54 | implicitToken: false, | ||
55 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
56 | }) | ||
57 | } | ||
58 | |||
59 | delete (options: OverrideCommandOptions & { | ||
60 | id: number | ||
61 | }) { | ||
62 | const path = '/api/v1/runners/' + options.id | ||
63 | |||
64 | return this.deleteRequest({ | ||
65 | ...options, | ||
66 | |||
67 | path, | ||
68 | implicitToken: true, | ||
69 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
70 | }) | ||
71 | } | ||
72 | |||
73 | // --------------------------------------------------------------------------- | ||
74 | |||
75 | async autoRegisterRunner () { | ||
76 | const { data } = await this.server.runnerRegistrationTokens.list({ sort: 'createdAt' }) | ||
77 | |||
78 | const { runnerToken } = await this.register({ | ||
79 | name: 'runner ' + buildUUID(), | ||
80 | registrationToken: data[0].registrationToken | ||
81 | }) | ||
82 | |||
83 | return runnerToken | ||
84 | } | ||
85 | } | ||