aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/plugins.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 /server/middlewares/validators/plugins.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 'server/middlewares/validators/plugins.ts')
-rw-r--r--server/middlewares/validators/plugins.ts218
1 files changed, 0 insertions, 218 deletions
diff --git a/server/middlewares/validators/plugins.ts b/server/middlewares/validators/plugins.ts
deleted file mode 100644
index 64bef2648..000000000
--- a/server/middlewares/validators/plugins.ts
+++ /dev/null
@@ -1,218 +0,0 @@
1import express from 'express'
2import { body, param, query, ValidationChain } from 'express-validator'
3import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
4import { PluginType } from '../../../shared/models/plugins/plugin.type'
5import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/server/api/install-plugin.model'
6import { exists, isBooleanValid, isSafePath, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc'
7import {
8 isNpmPluginNameValid,
9 isPluginNameValid,
10 isPluginStableOrUnstableVersionValid,
11 isPluginTypeValid
12} from '../../helpers/custom-validators/plugins'
13import { CONFIG } from '../../initializers/config'
14import { PluginManager } from '../../lib/plugins/plugin-manager'
15import { PluginModel } from '../../models/server/plugin'
16import { areValidationErrors } from './shared'
17
18const getPluginValidator = (pluginType: PluginType, withVersion = true) => {
19 const validators: (ValidationChain | express.Handler)[] = [
20 param('pluginName')
21 .custom(isPluginNameValid)
22 ]
23
24 if (withVersion) {
25 validators.push(
26 param('pluginVersion')
27 .custom(isPluginStableOrUnstableVersionValid)
28 )
29 }
30
31 return validators.concat([
32 (req: express.Request, res: express.Response, next: express.NextFunction) => {
33 if (areValidationErrors(req, res)) return
34
35 const npmName = PluginModel.buildNpmName(req.params.pluginName, pluginType)
36 const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(npmName)
37
38 if (!plugin) {
39 return res.fail({
40 status: HttpStatusCode.NOT_FOUND_404,
41 message: 'No plugin found named ' + npmName
42 })
43 }
44 if (withVersion && plugin.version !== req.params.pluginVersion) {
45 return res.fail({
46 status: HttpStatusCode.NOT_FOUND_404,
47 message: 'No plugin found named ' + npmName + ' with version ' + req.params.pluginVersion
48 })
49 }
50
51 res.locals.registeredPlugin = plugin
52
53 return next()
54 }
55 ])
56}
57
58const getExternalAuthValidator = [
59 param('authName')
60 .custom(exists),
61
62 (req: express.Request, res: express.Response, next: express.NextFunction) => {
63 if (areValidationErrors(req, res)) return
64
65 const plugin = res.locals.registeredPlugin
66 if (!plugin.registerHelpers) {
67 return res.fail({
68 status: HttpStatusCode.NOT_FOUND_404,
69 message: 'No registered helpers were found for this plugin'
70 })
71 }
72
73 const externalAuth = plugin.registerHelpers.getExternalAuths().find(a => a.authName === req.params.authName)
74 if (!externalAuth) {
75 return res.fail({
76 status: HttpStatusCode.NOT_FOUND_404,
77 message: 'No external auths were found for this plugin'
78 })
79 }
80
81 res.locals.externalAuth = externalAuth
82
83 return next()
84 }
85]
86
87const pluginStaticDirectoryValidator = [
88 param('staticEndpoint')
89 .custom(isSafePath),
90
91 (req: express.Request, res: express.Response, next: express.NextFunction) => {
92 if (areValidationErrors(req, res)) return
93
94 return next()
95 }
96]
97
98const listPluginsValidator = [
99 query('pluginType')
100 .optional()
101 .customSanitizer(toIntOrNull)
102 .custom(isPluginTypeValid),
103 query('uninstalled')
104 .optional()
105 .customSanitizer(toBooleanOrNull)
106 .custom(isBooleanValid),
107
108 (req: express.Request, res: express.Response, next: express.NextFunction) => {
109 if (areValidationErrors(req, res)) return
110
111 return next()
112 }
113]
114
115const installOrUpdatePluginValidator = [
116 body('npmName')
117 .optional()
118 .custom(isNpmPluginNameValid),
119 body('pluginVersion')
120 .optional()
121 .custom(isPluginStableOrUnstableVersionValid),
122 body('path')
123 .optional()
124 .custom(isSafePath),
125
126 (req: express.Request, res: express.Response, next: express.NextFunction) => {
127 if (areValidationErrors(req, res)) return
128
129 const body: InstallOrUpdatePlugin = req.body
130 if (!body.path && !body.npmName) {
131 return res.fail({ message: 'Should have either a npmName or a path' })
132 }
133 if (body.pluginVersion && !body.npmName) {
134 return res.fail({ message: 'Should have a npmName when specifying a pluginVersion' })
135 }
136
137 return next()
138 }
139]
140
141const uninstallPluginValidator = [
142 body('npmName')
143 .custom(isNpmPluginNameValid),
144
145 (req: express.Request, res: express.Response, next: express.NextFunction) => {
146 if (areValidationErrors(req, res)) return
147
148 return next()
149 }
150]
151
152const existingPluginValidator = [
153 param('npmName')
154 .custom(isNpmPluginNameValid),
155
156 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
157 if (areValidationErrors(req, res)) return
158
159 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
160 if (!plugin) {
161 return res.fail({
162 status: HttpStatusCode.NOT_FOUND_404,
163 message: 'Plugin not found'
164 })
165 }
166
167 res.locals.plugin = plugin
168 return next()
169 }
170]
171
172const updatePluginSettingsValidator = [
173 body('settings')
174 .exists(),
175
176 (req: express.Request, res: express.Response, next: express.NextFunction) => {
177 if (areValidationErrors(req, res)) return
178
179 return next()
180 }
181]
182
183const listAvailablePluginsValidator = [
184 query('search')
185 .optional()
186 .exists(),
187 query('pluginType')
188 .optional()
189 .customSanitizer(toIntOrNull)
190 .custom(isPluginTypeValid),
191 query('currentPeerTubeEngine')
192 .optional()
193 .custom(isPluginStableOrUnstableVersionValid),
194
195 (req: express.Request, res: express.Response, next: express.NextFunction) => {
196 if (areValidationErrors(req, res)) return
197
198 if (CONFIG.PLUGINS.INDEX.ENABLED === false) {
199 return res.fail({ message: 'Plugin index is not enabled' })
200 }
201
202 return next()
203 }
204]
205
206// ---------------------------------------------------------------------------
207
208export {
209 pluginStaticDirectoryValidator,
210 getPluginValidator,
211 updatePluginSettingsValidator,
212 uninstallPluginValidator,
213 listAvailablePluginsValidator,
214 existingPluginValidator,
215 installOrUpdatePluginValidator,
216 listPluginsValidator,
217 getExternalAuthValidator
218}