diff options
author | Chocobozzz <me@florianbigard.com> | 2022-10-11 11:07:40 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-10-11 11:11:04 +0200 |
commit | 9d4c60dccc8e7e777ad139a82e9f61feda9d21fc (patch) | |
tree | 2931338f340b398d36c43575fea95cf1fbbfeb4c /server/lib/plugins/plugin-manager.ts | |
parent | 9866921cbf3f8f0925f7ffb3a231d5dfe2d30953 (diff) | |
download | PeerTube-9d4c60dccc8e7e777ad139a82e9f61feda9d21fc.tar.gz PeerTube-9d4c60dccc8e7e777ad139a82e9f61feda9d21fc.tar.zst PeerTube-9d4c60dccc8e7e777ad139a82e9f61feda9d21fc.zip |
Add ability for plugins to register ws routes
Diffstat (limited to 'server/lib/plugins/plugin-manager.ts')
-rw-r--r-- | server/lib/plugins/plugin-manager.ts | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index a46b97fa4..c4d9b6574 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | import express from 'express' | 1 | import express from 'express' |
2 | import { createReadStream, createWriteStream } from 'fs' | 2 | import { createReadStream, createWriteStream } from 'fs' |
3 | import { ensureDir, outputFile, readJSON } from 'fs-extra' | 3 | import { ensureDir, outputFile, readJSON } from 'fs-extra' |
4 | import { Server } from 'http' | ||
4 | import { basename, join } from 'path' | 5 | import { basename, join } from 'path' |
5 | import { decachePlugin } from '@server/helpers/decache' | 6 | import { decachePlugin } from '@server/helpers/decache' |
6 | import { ApplicationModel } from '@server/models/application/application' | 7 | import { ApplicationModel } from '@server/models/application/application' |
@@ -67,9 +68,37 @@ export class PluginManager implements ServerHook { | |||
67 | private hooks: { [name: string]: HookInformationValue[] } = {} | 68 | private hooks: { [name: string]: HookInformationValue[] } = {} |
68 | private translations: PluginLocalesTranslations = {} | 69 | private translations: PluginLocalesTranslations = {} |
69 | 70 | ||
71 | private server: Server | ||
72 | |||
70 | private constructor () { | 73 | private constructor () { |
71 | } | 74 | } |
72 | 75 | ||
76 | init (server: Server) { | ||
77 | this.server = server | ||
78 | } | ||
79 | |||
80 | registerWebSocketRouter () { | ||
81 | this.server.on('upgrade', (request, socket, head) => { | ||
82 | const url = request.url | ||
83 | |||
84 | const matched = url.match(`/plugins/([^/]+)/([^/]+/)?ws(/.*)`) | ||
85 | if (!matched) return | ||
86 | |||
87 | const npmName = PluginModel.buildNpmName(matched[1], PluginType.PLUGIN) | ||
88 | const subRoute = matched[3] | ||
89 | |||
90 | const result = this.getRegisteredPluginOrTheme(npmName) | ||
91 | if (!result) return | ||
92 | |||
93 | const routes = result.registerHelpers.getWebSocketRoutes() | ||
94 | |||
95 | const wss = routes.find(r => r.route.startsWith(subRoute)) | ||
96 | if (!wss) return | ||
97 | |||
98 | wss.handler(request, socket, head) | ||
99 | }) | ||
100 | } | ||
101 | |||
73 | // ###################### Getters ###################### | 102 | // ###################### Getters ###################### |
74 | 103 | ||
75 | isRegistered (npmName: string) { | 104 | isRegistered (npmName: string) { |
@@ -581,7 +610,7 @@ export class PluginManager implements ServerHook { | |||
581 | }) | 610 | }) |
582 | } | 611 | } |
583 | 612 | ||
584 | const registerHelpers = new RegisterHelpers(npmName, plugin, onHookAdded.bind(this)) | 613 | const registerHelpers = new RegisterHelpers(npmName, plugin, this.server, onHookAdded.bind(this)) |
585 | 614 | ||
586 | return { | 615 | return { |
587 | registerStore: registerHelpers, | 616 | registerStore: registerHelpers, |