aboutsummaryrefslogtreecommitdiffhomepage
path: root/packages/server-commands/src/shared
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/shared
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/shared')
-rw-r--r--packages/server-commands/src/shared/abstract-command.ts225
-rw-r--r--packages/server-commands/src/shared/index.ts1
2 files changed, 226 insertions, 0 deletions
diff --git a/packages/server-commands/src/shared/abstract-command.ts b/packages/server-commands/src/shared/abstract-command.ts
new file mode 100644
index 000000000..bb6522e07
--- /dev/null
+++ b/packages/server-commands/src/shared/abstract-command.ts
@@ -0,0 +1,225 @@
1import { isAbsolute } from 'path'
2import { HttpStatusCodeType } from '@peertube/peertube-models'
3import { buildAbsoluteFixturePath } from '@peertube/peertube-node-utils'
4import {
5 makeDeleteRequest,
6 makeGetRequest,
7 makePostBodyRequest,
8 makePutBodyRequest,
9 makeUploadRequest,
10 unwrapBody,
11 unwrapText
12} from '../requests/requests.js'
13
14import type { PeerTubeServer } from '../server/server.js'
15
16export interface OverrideCommandOptions {
17 token?: string
18 expectedStatus?: HttpStatusCodeType
19}
20
21interface InternalCommonCommandOptions extends OverrideCommandOptions {
22 // Default to server.url
23 url?: string
24
25 path: string
26 // If we automatically send the server token if the token is not provided
27 implicitToken: boolean
28 defaultExpectedStatus: HttpStatusCodeType
29
30 // Common optional request parameters
31 contentType?: string
32 accept?: string
33 redirects?: number
34 range?: string
35 host?: string
36 headers?: { [ name: string ]: string }
37 requestType?: string
38 responseType?: string
39 xForwardedFor?: string
40}
41
42interface InternalGetCommandOptions extends InternalCommonCommandOptions {
43 query?: { [ id: string ]: any }
44}
45
46interface InternalDeleteCommandOptions extends InternalCommonCommandOptions {
47 query?: { [ id: string ]: any }
48 rawQuery?: string
49}
50
51abstract class AbstractCommand {
52
53 constructor (
54 protected server: PeerTubeServer
55 ) {
56
57 }
58
59 protected getRequestBody <T> (options: InternalGetCommandOptions) {
60 return unwrapBody<T>(this.getRequest(options))
61 }
62
63 protected getRequestText (options: InternalGetCommandOptions) {
64 return unwrapText(this.getRequest(options))
65 }
66
67 protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) {
68 const { url, range } = options
69 const { host, protocol, pathname } = new URL(url)
70
71 return this.getRequest({
72 ...options,
73
74 token: this.buildCommonRequestToken(options),
75 defaultExpectedStatus: this.buildExpectedStatus(options),
76
77 url: `${protocol}//${host}`,
78 path: pathname,
79 range
80 })
81 }
82
83 protected getRequest (options: InternalGetCommandOptions) {
84 const { query } = options
85
86 return makeGetRequest({
87 ...this.buildCommonRequestOptions(options),
88
89 query
90 })
91 }
92
93 protected deleteRequest (options: InternalDeleteCommandOptions) {
94 const { query, rawQuery } = options
95
96 return makeDeleteRequest({
97 ...this.buildCommonRequestOptions(options),
98
99 query,
100 rawQuery
101 })
102 }
103
104 protected putBodyRequest (options: InternalCommonCommandOptions & {
105 fields?: { [ fieldName: string ]: any }
106 headers?: { [name: string]: string }
107 }) {
108 const { fields, headers } = options
109
110 return makePutBodyRequest({
111 ...this.buildCommonRequestOptions(options),
112
113 fields,
114 headers
115 })
116 }
117
118 protected postBodyRequest (options: InternalCommonCommandOptions & {
119 fields?: { [ fieldName: string ]: any }
120 headers?: { [name: string]: string }
121 }) {
122 const { fields, headers } = options
123
124 return makePostBodyRequest({
125 ...this.buildCommonRequestOptions(options),
126
127 fields,
128 headers
129 })
130 }
131
132 protected postUploadRequest (options: InternalCommonCommandOptions & {
133 fields?: { [ fieldName: string ]: any }
134 attaches?: { [ fieldName: string ]: any }
135 }) {
136 const { fields, attaches } = options
137
138 return makeUploadRequest({
139 ...this.buildCommonRequestOptions(options),
140
141 method: 'POST',
142 fields,
143 attaches
144 })
145 }
146
147 protected putUploadRequest (options: InternalCommonCommandOptions & {
148 fields?: { [ fieldName: string ]: any }
149 attaches?: { [ fieldName: string ]: any }
150 }) {
151 const { fields, attaches } = options
152
153 return makeUploadRequest({
154 ...this.buildCommonRequestOptions(options),
155
156 method: 'PUT',
157 fields,
158 attaches
159 })
160 }
161
162 protected updateImageRequest (options: InternalCommonCommandOptions & {
163 fixture: string
164 fieldname: string
165 }) {
166 const filePath = isAbsolute(options.fixture)
167 ? options.fixture
168 : buildAbsoluteFixturePath(options.fixture)
169
170 return this.postUploadRequest({
171 ...options,
172
173 fields: {},
174 attaches: { [options.fieldname]: filePath }
175 })
176 }
177
178 protected buildCommonRequestOptions (options: InternalCommonCommandOptions) {
179 const { url, path, redirects, contentType, accept, range, host, headers, requestType, xForwardedFor, responseType } = options
180
181 return {
182 url: url ?? this.server.url,
183 path,
184
185 token: this.buildCommonRequestToken(options),
186 expectedStatus: this.buildExpectedStatus(options),
187
188 redirects,
189 contentType,
190 range,
191 host,
192 accept,
193 headers,
194 type: requestType,
195 responseType,
196 xForwardedFor
197 }
198 }
199
200 protected buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
201 const { token } = options
202
203 const fallbackToken = options.implicitToken
204 ? this.server.accessToken
205 : undefined
206
207 return token !== undefined ? token : fallbackToken
208 }
209
210 protected buildExpectedStatus (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
211 const { expectedStatus, defaultExpectedStatus } = options
212
213 return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus
214 }
215
216 protected buildVideoPasswordHeader (videoPassword: string) {
217 return videoPassword !== undefined && videoPassword !== null
218 ? { 'x-peertube-video-password': videoPassword }
219 : undefined
220 }
221}
222
223export {
224 AbstractCommand
225}
diff --git a/packages/server-commands/src/shared/index.ts b/packages/server-commands/src/shared/index.ts
new file mode 100644
index 000000000..795db3d55
--- /dev/null
+++ b/packages/server-commands/src/shared/index.ts
@@ -0,0 +1 @@
export * from './abstract-command.js'