aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/server-commands/src/server/object-storage-command.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /packages/server-commands/src/server/object-storage-command.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-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/server/object-storage-command.ts')
-rw-r--r--packages/server-commands/src/server/object-storage-command.ts165
1 files changed, 165 insertions, 0 deletions
diff --git a/packages/server-commands/src/server/object-storage-command.ts b/packages/server-commands/src/server/object-storage-command.ts
new file mode 100644
index 000000000..ff8d5d75c
--- /dev/null
+++ b/packages/server-commands/src/server/object-storage-command.ts
@@ -0,0 +1,165 @@
1import { randomInt } from 'crypto'
2import { HttpStatusCode } from '@peertube/peertube-models'
3import { makePostBodyRequest } from '../requests/index.js'
4
5export class ObjectStorageCommand {
6 static readonly DEFAULT_SCALEWAY_BUCKET = 'peertube-ci-test'
7
8 private readonly bucketsCreated: string[] = []
9 private readonly seed: number
10
11 // ---------------------------------------------------------------------------
12
13 constructor () {
14 this.seed = randomInt(0, 10000)
15 }
16
17 static getMockCredentialsConfig () {
18 return {
19 access_key_id: 'AKIAIOSFODNN7EXAMPLE',
20 secret_access_key: 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
21 }
22 }
23
24 static getMockEndpointHost () {
25 return 'localhost:9444'
26 }
27
28 static getMockRegion () {
29 return 'us-east-1'
30 }
31
32 getDefaultMockConfig () {
33 return {
34 object_storage: {
35 enabled: true,
36 endpoint: 'http://' + ObjectStorageCommand.getMockEndpointHost(),
37 region: ObjectStorageCommand.getMockRegion(),
38
39 credentials: ObjectStorageCommand.getMockCredentialsConfig(),
40
41 streaming_playlists: {
42 bucket_name: this.getMockStreamingPlaylistsBucketName()
43 },
44
45 web_videos: {
46 bucket_name: this.getMockWebVideosBucketName()
47 }
48 }
49 }
50 }
51
52 getMockWebVideosBaseUrl () {
53 return `http://${this.getMockWebVideosBucketName()}.${ObjectStorageCommand.getMockEndpointHost()}/`
54 }
55
56 getMockPlaylistBaseUrl () {
57 return `http://${this.getMockStreamingPlaylistsBucketName()}.${ObjectStorageCommand.getMockEndpointHost()}/`
58 }
59
60 async prepareDefaultMockBuckets () {
61 await this.createMockBucket(this.getMockStreamingPlaylistsBucketName())
62 await this.createMockBucket(this.getMockWebVideosBucketName())
63 }
64
65 async createMockBucket (name: string) {
66 this.bucketsCreated.push(name)
67
68 await this.deleteMockBucket(name)
69
70 await makePostBodyRequest({
71 url: ObjectStorageCommand.getMockEndpointHost(),
72 path: '/ui/' + name + '?create',
73 expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307
74 })
75
76 await makePostBodyRequest({
77 url: ObjectStorageCommand.getMockEndpointHost(),
78 path: '/ui/' + name + '?make-public',
79 expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307
80 })
81 }
82
83 async cleanupMock () {
84 for (const name of this.bucketsCreated) {
85 await this.deleteMockBucket(name)
86 }
87 }
88
89 getMockStreamingPlaylistsBucketName (name = 'streaming-playlists') {
90 return this.getMockBucketName(name)
91 }
92
93 getMockWebVideosBucketName (name = 'web-videos') {
94 return this.getMockBucketName(name)
95 }
96
97 getMockBucketName (name: string) {
98 return `${this.seed}-${name}`
99 }
100
101 private async deleteMockBucket (name: string) {
102 await makePostBodyRequest({
103 url: ObjectStorageCommand.getMockEndpointHost(),
104 path: '/ui/' + name + '?delete',
105 expectedStatus: HttpStatusCode.TEMPORARY_REDIRECT_307
106 })
107 }
108
109 // ---------------------------------------------------------------------------
110
111 static getDefaultScalewayConfig (options: {
112 serverNumber: number
113 enablePrivateProxy?: boolean // default true
114 privateACL?: 'private' | 'public-read' // default 'private'
115 }) {
116 const { serverNumber, enablePrivateProxy = true, privateACL = 'private' } = options
117
118 return {
119 object_storage: {
120 enabled: true,
121 endpoint: this.getScalewayEndpointHost(),
122 region: this.getScalewayRegion(),
123
124 credentials: this.getScalewayCredentialsConfig(),
125
126 upload_acl: {
127 private: privateACL
128 },
129
130 proxy: {
131 proxify_private_files: enablePrivateProxy
132 },
133
134 streaming_playlists: {
135 bucket_name: this.DEFAULT_SCALEWAY_BUCKET,
136 prefix: `test:server-${serverNumber}-streaming-playlists:`
137 },
138
139 web_videos: {
140 bucket_name: this.DEFAULT_SCALEWAY_BUCKET,
141 prefix: `test:server-${serverNumber}-web-videos:`
142 }
143 }
144 }
145 }
146
147 static getScalewayCredentialsConfig () {
148 return {
149 access_key_id: process.env.OBJECT_STORAGE_SCALEWAY_KEY_ID,
150 secret_access_key: process.env.OBJECT_STORAGE_SCALEWAY_ACCESS_KEY
151 }
152 }
153
154 static getScalewayEndpointHost () {
155 return 's3.fr-par.scw.cloud'
156 }
157
158 static getScalewayRegion () {
159 return 'fr-par'
160 }
161
162 static getScalewayBaseUrl () {
163 return `https://${this.DEFAULT_SCALEWAY_BUCKET}.${this.getScalewayEndpointHost()}/`
164 }
165}