]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/plugins/plugin-manager.ts
Prevent uninstall error on install error
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / plugin-manager.ts
index a46b97fa46cfb069a2dec56379bf41a23f59ab11..9086a4c8e855b2343fb77ddb9f49262bd001a81b 100644 (file)
@@ -1,6 +1,7 @@
 import express from 'express'
 import { createReadStream, createWriteStream } from 'fs'
 import { ensureDir, outputFile, readJSON } from 'fs-extra'
+import { Server } from 'http'
 import { basename, join } from 'path'
 import { decachePlugin } from '@server/helpers/decache'
 import { ApplicationModel } from '@server/models/application/application'
@@ -67,9 +68,45 @@ export class PluginManager implements ServerHook {
   private hooks: { [name: string]: HookInformationValue[] } = {}
   private translations: PluginLocalesTranslations = {}
 
+  private server: Server
+
   private constructor () {
   }
 
+  init (server: Server) {
+    this.server = server
+  }
+
+  registerWebSocketRouter () {
+    this.server.on('upgrade', (request, socket, head) => {
+      // Check if it's a plugin websocket connection
+      // No need to destroy the stream when we abort the request
+      // Other handlers in PeerTube will catch this upgrade event too (socket.io, tracker etc)
+
+      const url = request.url
+
+      const matched = url.match(`/plugins/([^/]+)/([^/]+/)?ws(/.*)`)
+      if (!matched) return
+
+      const npmName = PluginModel.buildNpmName(matched[1], PluginType.PLUGIN)
+      const subRoute = matched[3]
+
+      const result = this.getRegisteredPluginOrTheme(npmName)
+      if (!result) return
+
+      const routes = result.registerHelpers.getWebSocketRoutes()
+
+      const wss = routes.find(r => r.route.startsWith(subRoute))
+      if (!wss) return
+
+      try {
+        wss.handler(request, socket, head)
+      } catch (err) {
+        logger.error('Exception in plugin handler ' + npmName, { err })
+      }
+    })
+  }
+
   // ###################### Getters ######################
 
   isRegistered (npmName: string) {
@@ -288,7 +325,14 @@ export class PluginManager implements ServerHook {
 
   // ###################### Installation ######################
 
-  async install (toInstall: string, version?: string, fromDisk = false) {
+  async install (options: {
+    toInstall: string
+    version?: string
+    fromDisk?: boolean // default false
+    register?: boolean // default true
+  }) {
+    const { toInstall, version, fromDisk = false, register = true } = options
+
     let plugin: PluginModel
     let npmName: string
 
@@ -320,19 +364,23 @@ export class PluginManager implements ServerHook {
 
       logger.info('Successful installation of plugin %s.', toInstall)
 
-      await this.registerPluginOrTheme(plugin)
+      if (register) {
+        await this.registerPluginOrTheme(plugin)
+      }
     } catch (rootErr) {
       logger.error('Cannot install plugin %s, removing it...', toInstall, { err: rootErr })
 
-      try {
-        await this.uninstall(npmName)
-      } catch (err) {
-        logger.error('Cannot uninstall plugin %s after failed installation.', toInstall, { err })
-
+      if (npmName) {
         try {
-          await removeNpmPlugin(npmName)
+          await this.uninstall({ npmName })
         } catch (err) {
-          logger.error('Cannot remove plugin %s after failed installation.', toInstall, { err })
+          logger.error('Cannot uninstall plugin %s after failed installation.', toInstall, { err })
+
+          try {
+            await removeNpmPlugin(npmName)
+          } catch (err) {
+            logger.error('Cannot remove plugin %s after failed installation.', toInstall, { err })
+          }
         }
       }
 
@@ -357,16 +405,23 @@ export class PluginManager implements ServerHook {
     // Unregister old hooks
     await this.unregister(npmName)
 
-    return this.install(toUpdate, version, fromDisk)
+    return this.install({ toInstall: toUpdate, version, fromDisk })
   }
 
-  async uninstall (npmName: string) {
+  async uninstall (options: {
+    npmName: string
+    unregister?: boolean // default true
+  }) {
+    const { npmName, unregister = true } = options
+
     logger.info('Uninstalling plugin %s.', npmName)
 
-    try {
-      await this.unregister(npmName)
-    } catch (err) {
-      logger.warn('Cannot unregister plugin %s.', npmName, { err })
+    if (unregister) {
+      try {
+        await this.unregister(npmName)
+      } catch (err) {
+        logger.warn('Cannot unregister plugin %s.', npmName, { err })
+      }
     }
 
     const plugin = await PluginModel.loadByNpmName(npmName)
@@ -581,7 +636,7 @@ export class PluginManager implements ServerHook {
       })
     }
 
-    const registerHelpers = new RegisterHelpers(npmName, plugin, onHookAdded.bind(this))
+    const registerHelpers = new RegisterHelpers(npmName, plugin, this.server, onHookAdded.bind(this))
 
     return {
       registerStore: registerHelpers,