]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/shared/abstract-command.ts
Reduce 4k transcode test time
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / shared / abstract-command.ts
1 import { isAbsolute, join } from 'path'
2 import { root } from '../miscs/tests'
3 import {
4 makeDeleteRequest,
5 makeGetRequest,
6 makePostBodyRequest,
7 makePutBodyRequest,
8 makeUploadRequest,
9 unwrapBody,
10 unwrapText
11 } from '../requests/requests'
12 import { PeerTubeServer } from '../server/server'
13
14 export interface OverrideCommandOptions {
15 token?: string
16 expectedStatus?: number
17 }
18
19 interface InternalCommonCommandOptions extends OverrideCommandOptions {
20 // Default to server.url
21 url?: string
22
23 path: string
24 // If we automatically send the server token if the token is not provided
25 implicitToken: boolean
26 defaultExpectedStatus: number
27
28 // Common optional request parameters
29 contentType?: string
30 accept?: string
31 redirects?: number
32 range?: string
33 host?: string
34 headers?: { [ name: string ]: string }
35 requestType?: string
36 xForwardedFor?: string
37 }
38
39 interface InternalGetCommandOptions extends InternalCommonCommandOptions {
40 query?: { [ id: string ]: any }
41 }
42
43 abstract class AbstractCommand {
44
45 constructor (
46 protected server: PeerTubeServer
47 ) {
48
49 }
50
51 protected getRequestBody <T> (options: InternalGetCommandOptions) {
52 return unwrapBody<T>(this.getRequest(options))
53 }
54
55 protected getRequestText (options: InternalGetCommandOptions) {
56 return unwrapText(this.getRequest(options))
57 }
58
59 protected getRawRequest (options: Omit<InternalGetCommandOptions, 'path'>) {
60 const { url, range } = options
61 const { host, protocol, pathname } = new URL(url)
62
63 return this.getRequest({
64 ...options,
65
66 token: this.buildCommonRequestToken(options),
67 defaultExpectedStatus: this.buildExpectedStatus(options),
68
69 url: `${protocol}//${host}`,
70 path: pathname,
71 range
72 })
73 }
74
75 protected getRequest (options: InternalGetCommandOptions) {
76 const { query } = options
77
78 return makeGetRequest({
79 ...this.buildCommonRequestOptions(options),
80
81 query
82 })
83 }
84
85 protected deleteRequest (options: InternalCommonCommandOptions) {
86 return makeDeleteRequest(this.buildCommonRequestOptions(options))
87 }
88
89 protected putBodyRequest (options: InternalCommonCommandOptions & {
90 fields?: { [ fieldName: string ]: any }
91 }) {
92 const { fields } = options
93
94 return makePutBodyRequest({
95 ...this.buildCommonRequestOptions(options),
96
97 fields
98 })
99 }
100
101 protected postBodyRequest (options: InternalCommonCommandOptions & {
102 fields?: { [ fieldName: string ]: any }
103 }) {
104 const { fields } = options
105
106 return makePostBodyRequest({
107 ...this.buildCommonRequestOptions(options),
108
109 fields
110 })
111 }
112
113 protected postUploadRequest (options: InternalCommonCommandOptions & {
114 fields?: { [ fieldName: string ]: any }
115 attaches?: { [ fieldName: string ]: any }
116 }) {
117 const { fields, attaches } = options
118
119 return makeUploadRequest({
120 ...this.buildCommonRequestOptions(options),
121
122 method: 'POST',
123 fields,
124 attaches
125 })
126 }
127
128 protected putUploadRequest (options: InternalCommonCommandOptions & {
129 fields?: { [ fieldName: string ]: any }
130 attaches?: { [ fieldName: string ]: any }
131 }) {
132 const { fields, attaches } = options
133
134 return makeUploadRequest({
135 ...this.buildCommonRequestOptions(options),
136
137 method: 'PUT',
138 fields,
139 attaches
140 })
141 }
142
143 protected updateImageRequest (options: InternalCommonCommandOptions & {
144 fixture: string
145 fieldname: string
146 }) {
147 const filePath = isAbsolute(options.fixture)
148 ? options.fixture
149 : join(root(), 'server', 'tests', 'fixtures', options.fixture)
150
151 return this.postUploadRequest({
152 ...options,
153
154 fields: {},
155 attaches: { [options.fieldname]: filePath }
156 })
157 }
158
159 protected buildCommonRequestOptions (options: InternalCommonCommandOptions) {
160 const { url, path, redirects, contentType, accept, range, host, headers, requestType, xForwardedFor } = options
161
162 return {
163 url: url ?? this.server.url,
164 path,
165
166 token: this.buildCommonRequestToken(options),
167 expectedStatus: this.buildExpectedStatus(options),
168
169 redirects,
170 contentType,
171 range,
172 host,
173 accept,
174 headers,
175 type: requestType,
176 xForwardedFor
177 }
178 }
179
180 protected buildCommonRequestToken (options: Pick<InternalCommonCommandOptions, 'token' | 'implicitToken'>) {
181 const { token } = options
182
183 const fallbackToken = options.implicitToken
184 ? this.server.accessToken
185 : undefined
186
187 return token !== undefined ? token : fallbackToken
188 }
189
190 protected buildExpectedStatus (options: Pick<InternalCommonCommandOptions, 'expectedStatus' | 'defaultExpectedStatus'>) {
191 const { expectedStatus, defaultExpectedStatus } = options
192
193 return expectedStatus !== undefined ? expectedStatus : defaultExpectedStatus
194 }
195 }
196
197 export {
198 AbstractCommand
199 }