aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/plugins/plugin-index.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/lib/plugins/plugin-index.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/lib/plugins/plugin-index.ts')
-rw-r--r--server/lib/plugins/plugin-index.ts85
1 files changed, 0 insertions, 85 deletions
diff --git a/server/lib/plugins/plugin-index.ts b/server/lib/plugins/plugin-index.ts
deleted file mode 100644
index 119cee8e0..000000000
--- a/server/lib/plugins/plugin-index.ts
+++ /dev/null
@@ -1,85 +0,0 @@
1import { sanitizeUrl } from '@server/helpers/core-utils'
2import { logger } from '@server/helpers/logger'
3import { doJSONRequest } from '@server/helpers/requests'
4import { CONFIG } from '@server/initializers/config'
5import { PEERTUBE_VERSION } from '@server/initializers/constants'
6import { PluginModel } from '@server/models/server/plugin'
7import {
8 PeerTubePluginIndex,
9 PeertubePluginIndexList,
10 PeertubePluginLatestVersionRequest,
11 PeertubePluginLatestVersionResponse,
12 ResultList
13} from '@shared/models'
14import { PluginManager } from './plugin-manager'
15
16async function listAvailablePluginsFromIndex (options: PeertubePluginIndexList) {
17 const { start = 0, count = 20, search, sort = 'npmName', pluginType } = options
18
19 const searchParams: PeertubePluginIndexList & Record<string, string | number> = {
20 start,
21 count,
22 sort,
23 pluginType,
24 search,
25 currentPeerTubeEngine: options.currentPeerTubeEngine || PEERTUBE_VERSION
26 }
27
28 const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins'
29
30 try {
31 const { body } = await doJSONRequest<any>(uri, { searchParams })
32
33 logger.debug('Got result from PeerTube index.', { body })
34
35 addInstanceInformation(body)
36
37 return body as ResultList<PeerTubePluginIndex>
38 } catch (err) {
39 logger.error('Cannot list available plugins from index %s.', uri, { err })
40 return undefined
41 }
42}
43
44function addInstanceInformation (result: ResultList<PeerTubePluginIndex>) {
45 for (const d of result.data) {
46 d.installed = PluginManager.Instance.isRegistered(d.npmName)
47 d.name = PluginModel.normalizePluginName(d.npmName)
48 }
49
50 return result
51}
52
53async function getLatestPluginsVersion (npmNames: string[]): Promise<PeertubePluginLatestVersionResponse> {
54 const bodyRequest: PeertubePluginLatestVersionRequest = {
55 npmNames,
56 currentPeerTubeEngine: PEERTUBE_VERSION
57 }
58
59 const uri = sanitizeUrl(CONFIG.PLUGINS.INDEX.URL) + '/api/v1/plugins/latest-version'
60
61 const options = {
62 json: bodyRequest,
63 method: 'POST' as 'POST'
64 }
65 const { body } = await doJSONRequest<PeertubePluginLatestVersionResponse>(uri, options)
66
67 return body
68}
69
70async function getLatestPluginVersion (npmName: string) {
71 const results = await getLatestPluginsVersion([ npmName ])
72
73 if (Array.isArray(results) === false || results.length !== 1) {
74 logger.warn('Cannot get latest supported plugin version of %s.', npmName)
75 return undefined
76 }
77
78 return results[0].latestVersion
79}
80
81export {
82 listAvailablePluginsFromIndex,
83 getLatestPluginVersion,
84 getLatestPluginsVersion
85}