ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

plugin-index.ts 2.5 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { sanitizeUrl } from '@server/helpers/core-utils.js'
  2. import { logger } from '@server/helpers/logger.js'
  3. import { doJSONRequest } from '@server/helpers/requests.js'
  4. import { CONFIG } from '@server/initializers/config.js'
  5. import { PEERTUBE_VERSION } from '@server/initializers/constants.js'
  6. import { PluginModel } from '@server/models/server/plugin.js'
  7. import {
  8. PeerTubePluginIndex,
  9. PeertubePluginIndexList,
  10. PeertubePluginLatestVersionRequest,
  11. PeertubePluginLatestVersionResponse,
  12. ResultList
  13. } from '@peertube/peertube-models'
  14. import { PluginManager } from './plugin-manager.js'
  15. async function listAvailablePluginsFromIndex (options: PeertubePluginIndexList) {
  16. const { start = 0, count = 20, search, sort = 'npmName', pluginType } = options
  17. const searchParams: PeertubePluginIndexList & Record<string, string | number> = {
  18. start,
  19. count,
  20. sort,
  21. pluginType,
  22. search,
  23. currentPeerTubeEngine: options.currentPeerTubeEngine || PEERTUBE_VERSION
  24. }
  25. const uri = CONFIG.PLUGINS.INDEX.URL + '/api/v1/plugins'
  26. try {
  27. const { body } = await doJSONRequest<any>(uri, { searchParams })
  28. logger.debug('Got result from PeerTube index.', { body })
  29. addInstanceInformation(body)
  30. return body as ResultList<PeerTubePluginIndex>
  31. } catch (err) {
  32. logger.error('Cannot list available plugins from index %s.', uri, { err })
  33. return undefined
  34. }
  35. }
  36. function addInstanceInformation (result: ResultList<PeerTubePluginIndex>) {
  37. for (const d of result.data) {
  38. d.installed = PluginManager.Instance.isRegistered(d.npmName)
  39. d.name = PluginModel.normalizePluginName(d.npmName)
  40. }
  41. return result
  42. }
  43. async function getLatestPluginsVersion (npmNames: string[]): Promise<PeertubePluginLatestVersionResponse> {
  44. const bodyRequest: PeertubePluginLatestVersionRequest = {
  45. npmNames,
  46. currentPeerTubeEngine: PEERTUBE_VERSION
  47. }
  48. const uri = sanitizeUrl(CONFIG.PLUGINS.INDEX.URL) + '/api/v1/plugins/latest-version'
  49. const options = {
  50. json: bodyRequest,
  51. method: 'POST' as 'POST'
  52. }
  53. const { body } = await doJSONRequest<PeertubePluginLatestVersionResponse>(uri, options)
  54. return body
  55. }
  56. async function getLatestPluginVersion (npmName: string) {
  57. const results = await getLatestPluginsVersion([ npmName ])
  58. if (Array.isArray(results) === false || results.length !== 1) {
  59. logger.warn('Cannot get latest supported plugin version of %s.', npmName)
  60. return undefined
  61. }
  62. return results[0].latestVersion
  63. }
  64. export {
  65. listAvailablePluginsFromIndex,
  66. getLatestPluginVersion,
  67. getLatestPluginsVersion
  68. }